Added set_error() API to have a single point of entry for setting for errno and errstr.
authorDavide Libenzi <dlibenzi@google.com>
Wed, 7 Oct 2015 20:09:23 +0000 (13:09 -0700)
committerBarret Rhoden <brho@cs.berkeley.edu>
Wed, 14 Oct 2015 19:09:18 +0000 (15:09 -0400)
Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/drivers/dev/eventfd.c
kern/include/err.h
kern/include/syscall.h
kern/src/fdtap.c
kern/src/ns/dev.c
kern/src/syscall.c

index 9155cf0..d66081a 100644 (file)
@@ -286,9 +286,8 @@ static int efd_tapfd(struct chan *c, struct fd_tap *tap, int cmd)
        switch (c->qid.path) {
                case Qefd:
                        if (tap->filter & ~EFD_LEGAL_TAPS) {
-                               set_errno(ENOSYS);
-                               set_errstr("Unsupported #%s tap, must be %p, got %p", devname(),
-                                          EFD_LEGAL_TAPS, tap->filter);
+                               set_error(ENOSYS, "Unsupported #%s tap, must be %p", devname(),
+                                                 EFD_LEGAL_TAPS);
                                return -1;
                        }
                        spin_lock(&efd->tap_lock);
@@ -302,17 +301,15 @@ static int efd_tapfd(struct chan *c, struct fd_tap *tap, int cmd)
                                        ret = 0;
                                        break;
                                default:
-                                       set_errno(ENOSYS);
-                                       set_errstr("Unsupported #%s tap command %p",
-                                                  devname(), cmd);
+                                       set_error(ENOSYS, "Unsupported #%s tap command %p",
+                                                         devname(), cmd);
                                        ret = -1;
                        }
                        spin_unlock(&efd->tap_lock);
                        return ret;
                default:
-                       set_errno(ENOSYS);
-                       set_errstr("Can't tap #%s file type %d", devname(),
-                                  c->qid.path);
+                       set_error(ENOSYS, "Can't tap #%s file type %d", devname(),
+                                 c->qid.path);
                        return -1;
        }
 }
index 84cb8a7..b26f8da 100644 (file)
                     int curindex = 0;
 #define waserror() setjmp(&(errpush(errstack, ARRAY_SIZE(errstack), &curindex, \
                                                                        &prev_errbuf)->jmpbuf))
-#define error(e, x, ...)                                               \
-       do {                                                                            \
-               if (x != NULL)                                                                                                  \
-                       set_errstr(x, ##__VA_ARGS__);                                                           \
-               else                                                                                                                    \
-                       set_errstr(errno_to_string(e));                                                         \
-               set_errno(e);                                                                                                   \
+#define error(e, x, ...)                                                                                               \
+       do {                                                                                                                            \
+               set_error(e, x, ##__VA_ARGS__);                                                                 \
                longjmp(&get_cur_errbuf()->jmpbuf, 1);                                                  \
        } while(0)
 #define nexterror() longjmp(&(errpop(errstack, ARRAY_SIZE(errstack), &curindex, \
index b94c23d..c998db6 100644 (file)
@@ -58,8 +58,10 @@ intreg_t syscall(struct proc *p, uintreg_t sc_num, uintreg_t a0, uintreg_t a1,
 void set_errno(int errno);
 int get_errno(void);
 void unset_errno(void);
+void vset_errstr(const char *fmt, va_list ap);
 void set_errstr(const char *errstr, ...);
 char *current_errstr(void);
+void set_error(int error, const char *fmt, ...);
 struct errbuf *get_cur_errbuf(void);
 void set_cur_errbuf(struct errbuf *ebuf);
 char *get_cur_genbuf(void);
index 4c93e5a..643fdd8 100644 (file)
@@ -62,19 +62,17 @@ int add_fd_tap(struct proc *p, struct fd_tap_req *tap_req)
                goto out_with_lock;
        }
        if (!fdt->fd[fd].fd_chan) {
-               set_errno(EINVAL);
-               set_errstr("Can't tap a VFS file");
+               set_error(EINVAL, "Can't tap a VFS file");
                goto out_with_lock;
        }
        chan = fdt->fd[fd].fd_chan;
        if (fdt->fd[fd].fd_tap) {
-               set_errno(EBUSY);
-               set_errstr("FD %d already has a tap", fd);
+               set_error(EBUSY, "FD %d already has a tap", fd);
                goto out_with_lock;
        }
        if (!devtab[chan->type].tapfd) {
-               set_errno(ENOSYS);
-               set_errstr("Device %s does not handle taps", devtab[chan->type].name);
+               set_error(ENOSYS, "Device %s does not handle taps",
+                                 devtab[chan->type].name);
                goto out_with_lock;
        }
        /* need to keep chan alive for our call to the device.  someone else
@@ -135,8 +133,7 @@ int remove_fd_tap(struct proc *p, int fd)
                kref_put(&tap->kref);
                return 0;
        } else {
-               set_errno(EBADF);
-               set_errstr("FD %d was not tapped", fd);
+               set_error(EBADF, "FD %d was not tapped", fd);
                return -1;
        }
 }
index 33d994f..a2db8bf 100644 (file)
@@ -218,8 +218,7 @@ Accept:
 Notfound:
                                        if (j == 0)
                                                error(ENODEV, NULL);
-                                       set_errno(ENOENT);
-                                       set_errstr(errno_to_string(ENOENT));
+                                       set_error(ENOENT, NULL);
                                        goto Done;
                                case 0:
                                        printd("DEVWALK continue, i was %d\n", i);
index 1513e36..18dcdb1 100644 (file)
@@ -244,23 +244,28 @@ void unset_errno(void)
        pcpui->cur_kthread->sysc->errstr[0] = '\0';
 }
 
-void set_errstr(const char *fmt, ...)
+void vset_errstr(const char *fmt, va_list ap)
 {
-       va_list ap;
-       int rc;
-
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+
        if (!pcpui->cur_kthread || !pcpui->cur_kthread->sysc)
                return;
 
-       va_start(ap, fmt);
-       rc = vsnprintf(pcpui->cur_kthread->sysc->errstr, MAX_ERRSTR_LEN, fmt, ap);
-       va_end(ap);
+       vsnprintf(pcpui->cur_kthread->sysc->errstr, MAX_ERRSTR_LEN, fmt, ap);
 
        /* TODO: likely not needed */
        pcpui->cur_kthread->sysc->errstr[MAX_ERRSTR_LEN - 1] = '\0';
 }
 
+void set_errstr(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       vset_errstr(fmt, ap);
+       va_end(ap);
+}
+
 char *current_errstr(void)
 {
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
@@ -269,6 +274,17 @@ char *current_errstr(void)
        return pcpui->cur_kthread->sysc->errstr;
 }
 
+void set_error(int error, const char *fmt, ...)
+{
+       va_list ap;
+
+       set_errno(error);
+
+       va_start(ap, fmt);
+       vset_errstr(fmt != NULL ? fmt: errno_to_string(error), ap);
+       va_end(ap);
+}
+
 struct errbuf *get_cur_errbuf(void)
 {
        struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
@@ -548,8 +564,8 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
 
        /* Check the size of the argenv array, error out if too large. */
        if ((argenv_l < sizeof(struct argenv)) || (argenv_l > ARG_MAX)) {
-               set_errno(EINVAL);
-               set_errstr("The argenv array has an invalid size: %lu\n", argenv_l);
+               set_error(EINVAL, "The argenv array has an invalid size: %lu\n",
+                                 argenv_l);
                return -1;
        }
        /* Copy the argenv array into a kernel buffer. Delay processing of the
@@ -799,8 +815,8 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
 
        /* Check the size of the argenv array, error out if too large. */
        if ((argenv_l < sizeof(struct argenv)) || (argenv_l > ARG_MAX)) {
-               set_errno(EINVAL);
-               set_errstr("The argenv array has an invalid size: %lu\n", argenv_l);
+               set_error(EINVAL, "The argenv array has an invalid size: %lu\n",
+                                 argenv_l);
                return -1;
        }
        /* Copy the argenv array into a kernel buffer. */
@@ -813,8 +829,7 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
         * done along side this as well. */
        if (unpack_argenv(kargenv, argenv_l, &argc, &argv, &envc, &envp)) {
                user_memdup_free(p, kargenv);
-               set_errno(EINVAL);
-               set_errstr("Failed to unpack the args");
+               set_error(EINVAL, "Failed to unpack the args");
                return -1;
        }
 
@@ -1371,8 +1386,7 @@ static intreg_t sys_openat(struct proc *p, int fromfd, const char *path,
 
        printd("File %s Open attempt oflag %x mode %x\n", path, oflag, mode);
        if ((oflag & O_PATH) && (oflag & O_ACCMODE)) {
-               set_errno(EINVAL);
-               set_errstr("Cannot open O_PATH with any I/O perms (O%o)", oflag);
+               set_error(EINVAL, "Cannot open O_PATH with any I/O perms (O%o)", oflag);
                return -1;
        }
        t_path = copy_in_path(p, path, path_l);
@@ -1803,8 +1817,7 @@ intreg_t sys_getcwd(struct proc *p, char *u_cwd, size_t cwd_l)
        if (!k_cwd)
                return -1;              /* errno set by do_getcwd */
        if (strlen(k_cwd) + 1 > cwd_l) {
-               set_errno(ERANGE);
-               set_errstr("getcwd buf too small, needed %d", strlen(k_cwd) + 1);
+               set_error(ERANGE, "getcwd buf too small, needed %d", strlen(k_cwd) + 1);
                retval = -1;
                goto out;
        }
@@ -2062,8 +2075,7 @@ intreg_t sys_fd2path(struct proc *p, int fd, void *u_buf, size_t len)
        }
        ch = fdtochan(&current->open_files, fd, -1, FALSE, TRUE);
        if (snprintf(u_buf, len, "%s", channame(ch)) >= len) {
-               set_errno(ERANGE);
-               set_errstr("fd2path buf too small, needed %d", ret);
+               set_error(ERANGE, "fd2path buf too small, needed %d", ret);
                ret = -1;
        }
        cclose(ch);
@@ -2084,8 +2096,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(errno_to_string(EINVAL));
-               set_errno(EINVAL);
+               set_error(EINVAL, NULL);
                kfree(dir);
                return -1;
        }
@@ -2334,8 +2345,7 @@ static int handle_tap_req(struct proc *p, struct fd_tap_req *req)
                case (FDTAP_CMD_REM):
                        return remove_fd_tap(p, req->fd);
                default:
-                       set_errno(ENOSYS);
-                       set_errstr("FD Tap Command %d not supported", req->cmd);
+                       set_error(ENOSYS, "FD Tap Command %d not supported", req->cmd);
                        return -1;
        }
 }