Port tests/timerfd.c
[akaros.git] / tests / cs.c
index 9bd58ae..d0de3fc 100644 (file)
@@ -9,7 +9,7 @@
 #include <stdlib.h>
 
 #include <stdio.h>
-#include <parlib.h>
+#include <parlib/parlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <ctype.h>
 #include <error.h>
-#include <iplib.h>
-#include <ndb.h>
-#include <fcallfmt.h>
+#include <iplib/iplib.h>
+#include <ndblib/ndb.h>
+#include <ndblib/fcallfmt.h>
 #include <fcall.h>
+#include <parlib/spinlock.h>
+
+#define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
 
 enum
 {
@@ -94,8 +97,7 @@ struct Job
        pthread_t thread;
 };
 
-//spinlock_t   joblock;
-int joblock;
+spinlock_t joblock = SPINLOCK_INITIALIZER;
 Job    *joblist;
 
 Mlist  *mlist;
@@ -141,8 +143,8 @@ void        cleanmf(Mfile*);
 
 extern void    paralloc(void);
 
-spinlock_t     dblock;         /* mutex on database operations */
-spinlock_t     netlock;        /* mutex for netinit() */
+spinlock_t dblock = SPINLOCK_INITIALIZER;      /* mutex on database operations */
+spinlock_t netlock = SPINLOCK_INITIALIZER;     /* mutex for netinit() */
 
 char   *logfile = "cs";
 char   *paranoiafile = "cs.paranoia";
@@ -188,7 +190,7 @@ Network network[] = {
        { 0 },
 };
 
-spinlock_t ipifclock;
+spinlock_t ipifclock = SPINLOCK_INITIALIZER;
 struct ipifc *ipifcs;
 
 char   eaddr[16];              /* ascii ethernet address */
@@ -234,7 +236,7 @@ procsetname(char *fmt, ...)
        va_end(arg);
        if (cmdname == NULL)
                return;
-       snprintf(buf, sizeof buf, "#p/%d/args", getpid());
+       snprintf(buf, sizeof buf, "#proc/%d/args", getpid());
        if((fd = open(buf, OWRITE)) >= 0){
                write(fd, cmdname, strlen(cmdname)+1);
                close(fd);
@@ -251,6 +253,7 @@ main(int argc, char *argv[])
        argv0 = argv[0];
        justsetname = 0;
        setnetmtpt(mntpt, sizeof(mntpt), NULL);
+       register_printf_specifier('F', printf_fcall, printf_fcall_info);
        ext[0] = 0;
        argc--, argv++;
        while (argc && **argv == '-'){
@@ -282,10 +285,13 @@ main(int argc, char *argv[])
        }
 
        //rfork(RFREND|RFNOTEG);
+       /* Make us an SCP with a 2LS */
+       parlib_wants_to_be_mcp = FALSE;
 
-       snprintf(servefile, sizeof(servefile), "#s/cs%s", ext);
+       snprintf(servefile, sizeof(servefile), "#srv/cs%s", ext);
        snprintf(netndb, sizeof(netndb), "%s/ndb", mntpt);
-       syscall(SYS_nunmount, (unsigned long)servefile, (unsigned long)mntpt);
+       syscall(SYS_nunmount, (unsigned long)servefile, strlen(servefile),
+               (unsigned long)mntpt, strlen(mntpt));
        remove(servefile);
 
        ndbinit();
@@ -327,7 +333,7 @@ mountinit(char *service, char *mntpt)
        char buf[32];
        int ret;
 
-       ret = syscall(SYS_pipe, (unsigned long)p);
+       ret = pipe(p);
        if (ret < 0){
                error(1, 0, "pipe: %r");
                exit(1);
@@ -343,9 +349,9 @@ mountinit(char *service, char *mntpt)
        snprintf(buf, sizeof(buf), "%d", p[1]);
        if(write(f, buf, strlen(buf)) != strlen(buf))
                error(1, 0, "Write %s: %r", service);
-       /* using #s: we create a pipe and drop it into #s
+       /* using #s: we create a pipe and drop it into #srv.
         * we no longer mount. That's up to you.
-        * #s will route requests to us.
+        * #srv will route requests to us.
         */
        close(p[1]);
 
@@ -398,11 +404,11 @@ newjob(void)
        if (! job){
                error(1, 0, "%s: %r","job calloc");
        }
-//     //lock(&joblock);
+       spinlock_lock(&joblock);
        job->next = joblist;
        joblist = job;
        job->request.tag = -1;
-//     //unlock(&joblock);
+       spinlock_unlock(&joblock);
        return job;
 }
 
@@ -410,15 +416,18 @@ void
 freejob(Job *job)
 {
        Job **l;
-       //lock(&joblock);
+       Job *to_free = 0;
+       spinlock_lock(&joblock);
        for(l = &joblist; *l; l = &(*l)->next){
                if((*l) == job){
                        *l = job->next;
-                       free(job);
+                       to_free = job;
                        break;
                }
        }
-       //unlock(&joblock);
+       spinlock_unlock(&joblock);
+       if (to_free)
+               free(to_free);
 }
 
 void
@@ -426,21 +435,21 @@ flushjob(int tag)
 {
        Job *job;
 
-       //lock(&joblock);
+       spinlock_lock(&joblock);
        for(job = joblist; job; job = job->next){
                if(job->request.tag == tag && job->request.type != Tflush){
                        job->flushed = 1;
                        break;
                }
        }
-       //unlock(&joblock);
+       spinlock_unlock(&joblock);
 }
 
 void *job_thread(void* arg)
 {
        Mfile *mf;
        Job *job = arg;
-       //lock(&dblock);
+       spinlock_lock(&dblock);
        mf = newfid(job->request.fid);
 
        if(debug)
@@ -489,7 +498,7 @@ void *job_thread(void* arg)
                rwstat(job, mf);
                break;
        }
-       //unlock(&dblock);
+       spinlock_unlock(&dblock);
 
        freejob(job);
 
@@ -518,6 +527,8 @@ io(void)
                        error(1, 0, "%s: %r","mount read");
                job = newjob();
                if(convM2S(mdata, n, &job->request) != n){
+                       fprintf(stderr, "convM2S went south: format error %ux %ux %ux %ux %ux",
+                               mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
                        error(1, 0, "format error %ux %ux %ux %ux %ux",
                                mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
                        freejob(job);
@@ -526,15 +537,10 @@ io(void)
                /* stash the thread in the job so we can join them all
                 * later if we want to.
                 */
-#if 0
                if (pthread_create(&job->thread, NULL, &job_thread, job)) {
                        error(1, 0, "%s: %r","Failed to create job");
                        continue;
                }
-#endif
-       job_thread(job);
-
-
        }
 }
 
@@ -637,8 +643,7 @@ rwalk(Job *job, Mfile *mf)
                                qid.path = Qcs;
                                goto Found;
                        }
-                       err = malloc(4096);
-                       snprintf(err, 4096,"%s:file does not exist", elems[i]);
+                       err = "file does not exist";
                        break;
                }
        }
@@ -654,10 +659,6 @@ rwalk(Job *job, Mfile *mf)
        if(err == NULL)
                mf->qid = qid;
        sendmsg(job, err);
-       free(err);
-       /* shoot me. */
-       /* we can fix this or we can get a real language. Guess how Ron votes? */
-       err = "file does not exist";
        return err;
 }
 
@@ -958,11 +959,11 @@ sendmsg(Job *job, char *err)
                fprintf(stderr,  "CS:sendmsg convS2M of %F returns 0", &job->reply);
                abort();
        }
-       //lock(&joblock);
+       spinlock_lock(&joblock);
        if(job->flushed == 0)
                if(write(mfd[1], mdata, n)!=n)
                        error(1, 0, "%s: %r","mount write");
-       //unlock(&joblock);
+       spinlock_unlock(&joblock);
        if(debug)
                fprintf(stderr,  "CS:%F %d", &job->reply, n);
 }
@@ -1446,7 +1447,7 @@ iplookup(Network *np, char *host, char *serv, int nolookup)
        /*
         * reorder according to our interfaces
         */
-       //lock(&ipifclock);
+       spinlock_lock(&ipifclock);
        for(ifc = ipifcs; ifc != NULL; ifc = ifc->next){
                for(lifc = ifc->lifc; lifc != NULL; lifc = lifc->next){
                        maskip(lifc->ip, lifc->mask, net);
@@ -1457,13 +1458,13 @@ iplookup(Network *np, char *host, char *serv, int nolookup)
                                maskip(ip, lifc->mask, tnet);
                                if(memcmp(net, tnet, IPaddrlen) == 0){
                                        t = reorder(t, nt);
-                                       //unlock(&ipifclock);
+                                       spinlock_unlock(&ipifclock);
                                        return t;
                                }
                        }
                }
        }
-       //unlock(&ipifclock);
+       spinlock_unlock(&ipifclock);
 
        return t;
 }
@@ -1603,7 +1604,7 @@ dnsiplookup(char *host, struct ndbs *s)
        char buf[Maxreply];
        struct ndbtuple *t;
 
-       //unlock(&dblock);
+       spinlock_unlock(&dblock);
 
        /* save the name */
        snprintf(buf, sizeof(buf), "%s", host);
@@ -1626,7 +1627,7 @@ dnsiplookup(char *host, struct ndbs *s)
                        werrstr("temporary problem: %s", buf);
        }
 
-       //lock(&dblock);
+       spinlock_lock(&dblock);
        return t;
 }