Dropped char* error file to unify common error strings handling.
authorDavide Libenzi <dlibenzi@google.com>
Wed, 7 Oct 2015 14:30:14 +0000 (07:30 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 14 Oct 2015 18:51:21 +0000 (14:51 -0400)
[Touched up an off-by-one with error <= MAX_ERRNO]

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
15 files changed:
kern/drivers/dev/mnt.c
kern/include/err.h
kern/include/error.h
kern/src/net/dial.c
kern/src/net/ipifc.c
kern/src/net/tcp.c
kern/src/ns/Kbuild
kern/src/ns/chan.c
kern/src/ns/dev.c
kern/src/ns/error.c [deleted file]
kern/src/ns/parse.c
kern/src/ns/qio.c
kern/src/ns/sysfile.c
kern/src/printfmt.c
kern/src/syscall.c

index 013ca3b..80dd1f4 100644 (file)
@@ -828,8 +828,7 @@ void mountio(struct mnt *m, struct mntrpc *r)
                 * the old request to a flsh (mntflushalloc) and try again.  We'll
                 * always try to flush, and you can't get out until the flush either
                 * succeeds or errors out with a non-abort/Eintr error. */
-               if (strcmp(current_errstr(), "syscall aborted") &&
-                   strcmp(current_errstr(), Eintr)) {
+               if (get_errno() != EINTR) {
                        /* all other errors (not abort or Eintr) */
                        mntflushfree(m, r);
                        nexterror();
index b2c5fd0..ddfbd22 100644 (file)
@@ -5,20 +5,30 @@
 
 #include <setjmp.h>
 #include <kthread.h>
+#include <error.h>
 
 #define ERRSTACK(x) struct errbuf *prev_errbuf; struct errbuf errstack[(x)];   \
                     int curindex = 0;
 #define waserror() (errpush(errstack, ARRAY_SIZE(errstack), &curindex,         \
                             &prev_errbuf) ||                                   \
                     setjmp(&(get_cur_errbuf()->jmpbuf)))
-#define error(e, x,...) do {set_errstr(x, ##__VA_ARGS__);                              \
-                                                       set_errno(e);                                                           \
-                                                       longjmp(&get_cur_errbuf()->jmpbuf, 1);} while(0)
-#define nexterror() do {errpop(errstack, ARRAY_SIZE(errstack), &curindex,      \
-                            prev_errbuf);                                      \
-                     longjmp(&(get_cur_errbuf())->jmpbuf, 1);} while (0)
-#define poperror() do {errpop(errstack, ARRAY_SIZE(errstack), &curindex,       \
-                       prev_errbuf);} while (0)
+#define error(e, x, ...)                                               \
+       do {                                                                            \
+               if (x != NULL)                                                                                                  \
+                       set_errstr(x, ##__VA_ARGS__);                                                           \
+               else                                                                                                                    \
+                       set_errstr(errno_to_string(e));                                                         \
+               set_errno(e);                                                                                                   \
+               longjmp(&get_cur_errbuf()->jmpbuf, 1);                                                  \
+       } while(0)
+#define nexterror() \
+       do {                                                                                                                            \
+               errpop(errstack, ARRAY_SIZE(errstack), &curindex,                               \
+                          prev_errbuf);                                                                                        \
+               longjmp(&(get_cur_errbuf())->jmpbuf, 1);                                                \
+       } while (0)
+#define poperror() errpop(errstack, ARRAY_SIZE(errstack), &curindex,   \
+                                                 prev_errbuf)
 
 int errpush(struct errbuf *errstack, int stacksize, int *curindex,
             struct errbuf **prev_errbuf);
index d0d2d83..365be16 100644 (file)
@@ -13,4 +13,16 @@ extern const int MAX_ERRNO;
 #define PTR_ERR(ptr)  ((uintptr_t)(ptr))
 #define IS_ERR(ptr)   ((uintptr_t)-(uintptr_t)(ptr) <= MAX_ERRNO)
 
+/* Plan9 wants to return non-const char* all over the place, so even if a const
+ * char* would have made much more sense, unless we want to refactor a huge
+ * amount of code, we need to return a char*.
+ */
+static inline char *errno_to_string(int error)
+{
+       extern const char *const errno_strings[];
+
+       return error >= 0 && error < MAX_ERRNO && errno_strings[error] != NULL ?
+               (char *) errno_strings[error]: (char *) "Unknown error";
+}
+                
 #endif // !ROS_INC_ERROR_H */
index 6c80ab4..7cdadd7 100644 (file)
@@ -128,7 +128,7 @@ static int csdial(DS * ds)
         *  we get one that works.
         */
        *besterr = 0;
-       strncpy(err, Egreg, ERRMAX);
+       strncpy(err, errno_to_string(ECONNRESET), ERRMAX);
        sysseek(fd, 0, 0);
        while ((n = sysread(fd, buf, Maxstring - 1)) > 0) {
                buf[n] = 0;
index a6d7d12..4750532 100644 (file)
@@ -111,7 +111,7 @@ static char *ipifcbind(struct conv *c, char **argv, int argc)
        struct medium *m;
 
        if (argc < 2)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
 
        ifc = (struct Ipifc *)c->ptcl;
 
@@ -347,12 +347,12 @@ char *ipifcsetmtu(struct Ipifc *ifc, char **argv, int argc)
        int mtu;
 
        if (argc < 2)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
        if (ifc->m == NULL)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
        mtu = strtoul(argv[1], 0, 0);
        if (mtu < ifc->m->mintu || mtu > ifc->m->maxtu)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
        ifc->maxtu = mtu;
        return NULL;
 }
@@ -408,8 +408,7 @@ char *ipifcadd(struct Ipifc *ifc, char **argv, int argc, int tentative,
                        maskip(rem, mask, net);
                        break;
                default:
-                       return Ebadarg;
-                       break;
+                       return errno_to_string(EINVAL);
        }
        if (isv4(ip))
                tentative = 0;
@@ -588,7 +587,7 @@ char *ipifcrem(struct Ipifc *ifc, char **argv, int argc)
        char *rv;
 
        if (argc < 3)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
 
        parseip(ip, argv[1]);
        parseipmask(mask, argv[2]);
@@ -706,7 +705,7 @@ char *ipifcsetpar6(struct Ipifc *ifc, char **argv, int argc)
        i = 1;
 
        if (argsleft % 2 != 0)
-               return Ebadarg;
+               return errno_to_string(EINVAL);
 
        while (argsleft > 1) {
                if (strcmp(argv[i], "recvra") == 0)
@@ -732,7 +731,7 @@ char *ipifcsetpar6(struct Ipifc *ifc, char **argv, int argc)
                else if (strcmp(argv[i], "routerlt") == 0)
                        ifc->rp.routerlt = atoi(argv[i + 1]);
                else
-                       return Ebadarg;
+                       return errno_to_string(EINVAL);
 
                argsleft -= 2;
                i += 2;
@@ -742,7 +741,7 @@ char *ipifcsetpar6(struct Ipifc *ifc, char **argv, int argc)
        if (ifc->rp.maxraint < ifc->rp.minraint) {
                ifc->rp.maxraint = vmax;
                ifc->rp.minraint = vmin;
-               return Ebadarg;
+               return errno_to_string(EINVAL);
        }
 
        return NULL;
@@ -1642,13 +1641,13 @@ char *ipifcaddpref6(struct Ipifc *ifc, char **argv, int argc)
                case 2:
                        break;
                default:
-                       return Ebadarg;
+                       return errno_to_string(EINVAL);
        }
 
        if ((parseip(prefix, argv[1]) != 6) ||
                (validlt < preflt) || (plen < 0) || (plen > 64) || (islinklocal(prefix))
                )
-               return Ebadarg;
+               return errno_to_string(EINVAL);
 
        lifc = kzmalloc(sizeof(struct Iplifc), 0);
        lifc->onlink = (onlink != 0);
@@ -1660,7 +1659,7 @@ char *ipifcaddpref6(struct Ipifc *ifc, char **argv, int argc)
        if (ifc->m->pref2addr != NULL)
                ifc->m->pref2addr(prefix, ifc->mac);
        else
-               return Ebadarg;
+               return errno_to_string(EINVAL);
 
        snprintf(addr, sizeof(addr), "%I", prefix);
        snprintf(preflen, sizeof(preflen), "/%d", plen);
index fd80f87..138d8ae 100644 (file)
@@ -2082,7 +2082,7 @@ reset:
                        }
                        if (seg.flags & RST) {
                                if (seg.flags & ACK)
-                                       localclose(s, Econrefused);
+                                       localclose(s, errno_to_string(ECONNREFUSED));
                                goto raise;
                        }
 
@@ -2187,7 +2187,7 @@ reset:
                                                 s->raddr, s->rport, s->laddr, s->lport, tcb->rcv.nxt,
                                                 seg.seq);
                        }
-                       localclose(s, Econrefused);
+                       localclose(s, errno_to_string(ECONNREFUSED));
                        goto raise;
                }
 
@@ -2716,7 +2716,7 @@ void tcpkeepalive(void *v)
        qlock(&s->qlock);
        if (tcb->state != Closed) {
                if (--(tcb->kacounter) <= 0) {
-                       localclose(s, Etimedout);
+                       localclose(s, errno_to_string(ETIMEDOUT));
                } else {
                        tcpsendka(s);
                        tcpgo(s->p->priv, &tcb->katimer);
@@ -2809,7 +2809,7 @@ void tcptimeout(void *arg)
                                maxback = MAXBACKMS;
                        tcb->backedoff += tcb->timer.start * MSPTICK;
                        if (tcb->backedoff >= maxback) {
-                               localclose(s, Etimedout);
+                               localclose(s, errno_to_string(ETIMEDOUT));
                                break;
                        }
                        netlog(s->p->f, Logtcprxmt, "timeout rexmit 0x%lx %llu/%llu\n",
index f2be64f..17f40b8 100644 (file)
@@ -9,7 +9,6 @@ obj-y                                           += convM2S.o
 obj-y                                          += convS2M.o
 obj-y                                          += dev.o
 obj-y                                          += devtab.o
-obj-y                                          += error.o
 obj-y                                          += getfields.o
 obj-y                                          += parse.o
 obj-y                                          += pgrp.o
index fe2cc85..95b96e0 100644 (file)
@@ -687,7 +687,7 @@ int walk(struct chan **cp, char **names, int nnames, bool can_mount, int *nerror
                                *nerror = nhave;
                        cnameclose(cname);
                        cclose(c);
-                       set_errstr(Enotdir);
+                       set_error(ENOTDIR, NULL);
                        if (mh != NULL)
                                putmhead(mh);
                        return -1;
@@ -767,7 +767,7 @@ int walk(struct chan **cp, char **names, int nnames, bool can_mount, int *nerror
                                        } else {
                                                if (nerror)
                                                        *nerror = nhave + wq->nqid;
-                                               set_errstr(Enotdir);
+                                               set_errstr(errno_to_string(ENOTDIR));
                                        }
                                        kfree(wq);
                                        if (mh != NULL)
@@ -1405,7 +1405,7 @@ void validname(char *aname, int slashok)
        ename = memchr(name, 0, (1 << 16));
 
        if (ename == NULL || ename - name >= (1 << 16))
-               error(EFAIL, "name too long");
+               error(EINVAL, "Name too long");
 
        while (*name) {
                /* all characters above '~' are ok */
@@ -1419,7 +1419,7 @@ void validname(char *aname, int slashok)
                } else {
                        if (isfrog[c])
                                if (!slashok || c != '/') {
-                                       error(EFAIL, "%s: %s (%p), at char %c", Ebadchar, aname, aname, c);
+                                       error(EINVAL, "%s (%p), at char %c", aname, aname, c);
                                }
                        name++;
                }
index f1a4f53..9fa47e0 100644 (file)
@@ -216,10 +216,10 @@ Accept:
                                        printd("DEVWALK -1, i was %d, want path %p\n", i,
                                                   c->qid.path);
 Notfound:
-                                       set_errno(ENOENT);
                                        if (j == 0)
                                                error(ENODEV, NULL);
-                                       set_errstr(Enonexist);
+                                       set_errno(ENOENT);
+                                       set_errstr(errno_to_string(ENOENT));
                                        goto Done;
                                case 0:
                                        printd("DEVWALK continue, i was %d\n", i);
@@ -361,8 +361,7 @@ void devpermcheck(char *fileuid, uint32_t perm, int omode)
         * seem to handle O_EXEC being mixed readable or writable. */
        rwx = omode_to_rwx(omode);
        if ((rwx & perm) != rwx)
-               error(EFAIL, "%s: devpermcheck(%s, 0%o, 0%o) failed", Eperm, fileuid, perm,
-                         omode);
+               error(EPERM, "devpermcheck(%s, 0%o, 0%o) failed", fileuid, perm, omode);
 }
 
 struct chan *devopen(struct chan *c, int omode, struct dirtab *tab, int ntab,
diff --git a/kern/src/ns/error.c b/kern/src/ns/error.c
deleted file mode 100644 (file)
index 79820b2..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-// INFERNO
-char Emount[] = "inconsistent mount";
-char Eunmount[] = "not mounted";
-char Eunion[] = "not in union";
-char Emountrpc[] = "mount rpc error";
-char Enocreate[] = "mounted directory forbids creation";
-char Enonexist[] = "file does not exist";
-char Eexist[] = "file already exists";
-char Enotdir[] = "not a directory";
-char Eisdir[] = "file is a directory";
-char Ebadchar[] = "bad character in file name";
-char Efilename[] = "file name syntax";
-char Eperm[] = "permission denied";
-char Ebadusefd[] = "inappropriate use of fd";
-char Ebadarg[] = "bad arg in system call";
-char Einuse[] = "device or object already in use";
-char Eio[] = "i/o error";
-char Etoobig[] = "read or write too large";
-char Etoosmall[] = "read or write too small";
-char Enoifc[] = "bad interface or no free interface slots";
-char Ehungup[] = "i/o on hungup channel";
-char Ebadctl[] = "bad process or channel control request";
-char Enodev[] = "no free devices";
-char Enochild[] = "no living children";
-char Enovmem[] = "out of memory: virtual memory";
-char Eisstream[] = "seek on a stream";
-char Etimedout[] = "connection timed out";
-char Econrefused[] = "connection refused";
-char Econinuse[] = "connection in use";
-char Eintr[] = "interrupted";
-char Enomem[] = "out of memory: kernel";
-char Eshort[] = "i/o count too small";
-char Egreg[] = "jim'll fix it";
-char Ebadspec[] = "bad attach specifier";
-char Enoattach[] = "mount/attach disallowed";
-char Eshortstat[] = "stat buffer too small";
-char Enegoff[] = "negative i/o offset";
-char Ebadstat[] = "malformed stat buffer";
-char Ecmdargs[] = "wrong #args in control message";
-char Enofd[] = "no free file descriptors";
-char Eprocdied[] = "process died";
index d00d3db..ed61c07 100644 (file)
@@ -118,8 +118,9 @@ struct cmdtab *lookupcmd(struct cmdbuf *cb, struct cmdtab *ctab, int nctab)
                        if (strcmp(ct->cmd, cb->f[0]) != 0)
                                continue;
                if (ct->narg != 0 && ct->narg != cb->nf) {
-                       // oh how I hate plan 9 error handling sometimes.
-                       printk("%s for %s have %d want %d", Ecmdargs, cb->f[0], cb->nf, ct->narg);
+                       // oh how I hate plan 9 error handling sometimes (DL: sometimes?!?).
+                       printk("%s: for %s have %d want %d\n", errno_to_string(EINVAL),
+                                  cb->f[0], cb->nf, ct->narg);
                        cmderror(cb, "bring me another fucking rock");
                }
                return ct;
index 6f9aee3..263abb7 100644 (file)
@@ -1271,7 +1271,7 @@ static bool qwait(struct queue *q)
                                spin_unlock_irqsave(&q->lock);
                                error(EFAIL, "multiple reads on a closed queue");
                        }
-                       if (*q->err && strcmp(q->err, Ehungup) != 0) {
+                       if (*q->err && strcmp(q->err, errno_to_string(ECONNABORTED)) != 0) {
                                spin_unlock_irqsave(&q->lock);
                                error(EFAIL, q->err);
                        }
@@ -1865,7 +1865,7 @@ void qclose(struct queue *q)
        spin_lock_irqsave(&q->lock);
        q->state |= Qclosed;
        q->state &= ~(Qflow | Qstarve | Qdropoverflow | Qnonblock);
-       strncpy(q->err, Ehungup, sizeof(q->err));
+       strncpy(q->err, errno_to_string(ECONNABORTED), sizeof(q->err));
        bfirst = q->bfirst;
        q->bfirst = 0;
        q->len = 0;
@@ -1891,7 +1891,7 @@ void qhangup(struct queue *q, char *msg)
        spin_lock_irqsave(&q->lock);
        q->state |= Qclosed;
        if (msg == 0 || *msg == 0)
-               strncpy(q->err, Ehungup, sizeof(q->err));
+               strncpy(q->err, errno_to_string(ECONNABORTED), sizeof(q->err));
        else
                strncpy(q->err, msg, ERRMAX - 1);
        spin_unlock_irqsave(&q->lock);
index cc983fe..192481a 100644 (file)
@@ -575,9 +575,9 @@ long unionread(struct chan *c, void *va, long n)
                /* Error causes component of union to be skipped */
                if (mount->to) {
                        /* normally we want to discard the error, but for our ghetto kdirent
-                        * hack, we need to repeat unionread if we saw a Eshort */
+                        * hack, we need to repeat unionread if we saw a ENODATA */
                        if (waserror()) {
-                               if (!strcmp(current_errstr(), Eshort)) {
+                               if (!strcmp(current_errstr(), errno_to_string(ENODATA))) {
                                        runlock(&m->lock);
                                        qunlock(&c->umqlock);
                                        nexterror();
@@ -733,9 +733,9 @@ out:
 }
 
 /* Reads exactly n bytes from chan c, starting at its offset.  Can block, but if
- * we get 0 back too soon (EOF or error), then we'll error out with Eshort.
+ * we get 0 back too soon (EOF or error), then we'll error out with ENODATA.
  * That might need a little work - if there was a previous error, then we
- * clobbered it and only know Eshort but not why we completed early. */
+ * clobbered it and only know ENODATA but not why we completed early. */
 void read_exactly_n(struct chan *c, void *vp, long n)
 {
        char *p;
@@ -747,7 +747,7 @@ void read_exactly_n(struct chan *c, void *vp, long n)
                nn = devtab[c->type].read(c, p, n, c->offset);
                printd("readn: Got %d@%lld\n", nn, c->offset);
                if (nn == 0)
-                       error(EFAIL, "%s: wanted %d, got %d", Eshort, want, total);
+                       error(ENODATA, "wanted %d, got %d", want, total);
                spin_lock(&c->lock);
                c->offset += nn;
                spin_unlock(&c->lock);
index 493bf51..9c27346 100644 (file)
@@ -139,7 +139,7 @@ void vprintfmt(void (*putch)(int, void**), void **putdat, const char *fmt, va_li
                        if (err >= MAX_ERRNO)
                                printfmt(putch, putdat, "error %d(%s)", err, get_cur_errbuf());
                        else
-                               printfmt(putch, putdat, "%s (%s)", errno_strings[err], get_cur_errbuf());
+                               printfmt(putch, putdat, "%s (%s)", errno_to_string(err), get_cur_errbuf());
                        break;
 
                case 'E': // ENET MAC
index 61117ba..1513e36 100644 (file)
@@ -2084,7 +2084,7 @@ static int vfs_wstat(struct file *file, uint8_t *stat_m, size_t stat_sz,
        dir = kzmalloc(sizeof(struct dir) + stat_sz, KMALLOC_WAIT);
        m_sz = convM2D(stat_m, stat_sz, &dir[0], (char*)&dir[1]);
        if (m_sz != stat_sz) {
-               set_errstr(Eshortstat);
+               set_errstr(errno_to_string(EINVAL));
                set_errno(EINVAL);
                kfree(dir);
                return -1;