Set errno when only errstr was called
[akaros.git] / kern / src / syscall.c
index 4509b26..e506d76 100644 (file)
@@ -37,7 +37,7 @@
 #include <socket.h>
 
 
-#ifdef __CONFIG_NETWORKING__
+#ifdef CONFIG_NETWORKING
 #include <net/nic_common.h>
 extern int (*send_frame)(const char *CT(len) data, size_t len);
 extern unsigned char device_mac[6];
@@ -102,6 +102,36 @@ void set_errno(int errno)
                pcpui->cur_sysc->err = errno;
 }
 
+void set_errstr(char *errstr)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       if (!pcpui->cur_sysc)
+               return;
+       strncpy(pcpui->cur_sysc->errstr, errstr, MAX_ERRSTR_LEN);
+       /* enforce null termination */
+       pcpui->cur_sysc->errstr[MAX_ERRSTR_LEN - 1] = '\0';
+}
+
+char *current_errstr(void)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       /* no one should call this that doesn't have a sysc */
+       assert(!pcpui->cur_sysc);
+       return pcpui->cur_sysc->errstr;
+}
+
+struct errbuf *get_cur_errbuf(void)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       return (struct errbuf*)pcpui->cur_errbuf;
+}
+
+void set_cur_errbuf(struct errbuf *ebuf)
+{
+       struct per_cpu_info *pcpui = &per_cpu_info[core_id()];
+       pcpui->cur_errbuf = ebuf;
+}
+
 /************** Utility Syscalls **************/
 
 static int sys_null(void)
@@ -201,7 +231,7 @@ static int sys_cache_buster(struct proc *p, uint32_t num_writes,
 
 static int sys_cache_invalidate(void)
 {
-       #ifdef __i386__
+       #ifdef CONFIG_X86
                wbinvd();
        #endif
        return 0;
@@ -442,7 +472,7 @@ static ssize_t sys_fork(env_t* e)
        memcpy(env->procinfo->argp, e->procinfo->argp, sizeof(e->procinfo->argp));
        memcpy(env->procinfo->argbuf, e->procinfo->argbuf,
               sizeof(e->procinfo->argbuf));
-       #ifdef __i386__
+       #ifdef CONFIG_X86
        /* new guy needs to know about ldt (everything else in procdata is fresh */
        env->procdata->ldt = e->procdata->ldt;
        #endif
@@ -520,7 +550,7 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
                                   sizeof(pi->argbuf)))
                goto mid_error;
        /* This is the point of no return for the process. */
-       #ifdef __i386__
+       #ifdef CONFIG_X86
        /* clear this, so the new program knows to get an LDT */
        p->procdata->ldt = 0;
        #endif
@@ -946,7 +976,7 @@ static ssize_t sys_serial_read(env_t* e, char *DANGEROUS _buf, size_t len)
        if (len == 0)
                return 0;
 
-       #ifdef __CONFIG_SERIAL_IO__
+       #ifdef CONFIG_SERIAL_IO
            char *COUNT(len) buf = user_mem_assert(e, _buf, len, 1, PTE_USER_RO);
                size_t bytes_read = 0;
                int c;
@@ -966,7 +996,7 @@ static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
        printk("[kernel] serial writing is deprecated.\n");
        if (len == 0)
                return 0;
-       #ifdef __CONFIG_SERIAL_IO__
+       #ifdef CONFIG_SERIAL_IO
                char *COUNT(len) _buf = user_mem_assert(e, buf, len, 1, PTE_USER_RO);
                for(int i =0; i<len; i++)
                        serial_send_byte(buf[i]);
@@ -976,7 +1006,7 @@ static ssize_t sys_serial_write(env_t* e, const char *DANGEROUS buf, size_t len)
        #endif
 }
 
-#ifdef __CONFIG_NETWORKING__
+#ifdef CONFIG_NETWORKING
 // This is not a syscall we want. Its hacky. Here just for syscall stuff until get a stack.
 static ssize_t sys_eth_read(env_t* e, char *DANGEROUS buf)
 {
@@ -1460,6 +1490,45 @@ intreg_t sys_rmdir(struct proc *p, const char *path, size_t path_l)
        return retval;
 }
 
+intreg_t sys_pipe(struct proc *p, int *u_pipefd, int flags)
+{
+       int pipefd[2] = {0};
+       int fd;
+       int retval = 0;
+       struct file *pipe_files[2] = {0};
+
+       if (do_pipe(pipe_files, flags))
+               return -1;
+       fd = insert_file(&p->open_files, pipe_files[0], 0);
+       if (!fd) {
+               set_errno(ENFILE);
+               goto failed_first;
+       }
+       pipefd[0] = fd;
+       fd = insert_file(&p->open_files, pipe_files[1], 0);
+       if (!fd) {
+               set_errno(ENFILE);
+               goto failed_second;
+       }
+       pipefd[1] = fd;
+       if (memcpy_to_user_errno(p, u_pipefd, pipefd, sizeof(pipefd))) {
+               set_errno(EFAULT);
+               goto failed_memcpy;
+       }
+       goto all_out;
+
+failed_memcpy:
+       put_file_from_fd(&p->open_files, pipefd[1]);
+failed_second:
+       put_file_from_fd(&p->open_files, pipefd[0]);
+failed_first:
+       retval = -1;
+all_out:
+       kref_put(&pipe_files[0]->f_kref);
+       kref_put(&pipe_files[1]->f_kref);
+       return retval;
+}
+
 intreg_t sys_gettimeofday(struct proc *p, int *buf)
 {
        static spinlock_t gtod_lock = SPINLOCK_INITIALIZER;
@@ -1468,7 +1537,7 @@ intreg_t sys_gettimeofday(struct proc *p, int *buf)
        spin_lock(&gtod_lock);
        if(t0 == 0)
 
-#if (defined __CONFIG_APPSERVER__)
+#if (defined CONFIG_APPSERVER)
        t0 = ufe(time,0,0,0,0);
 #else
        // Nanwan's birthday, bitches!!
@@ -1588,17 +1657,17 @@ const static struct sys_table_entry syscall_table[] = {
        [SYS_self_notify] = {(syscall_t)sys_self_notify, "self_notify"},
        [SYS_vc_entry] = {(syscall_t)sys_vc_entry, "vc_entry"},
        [SYS_halt_core] = {(syscall_t)sys_halt_core, "halt_core"},
-#ifdef __CONFIG_SERIAL_IO__
+#ifdef CONFIG_SERIAL_IO
        [SYS_serial_read] = {(syscall_t)sys_serial_read, "ser_read"},
        [SYS_serial_write] = {(syscall_t)sys_serial_write, "ser_write"},
 #endif
-#ifdef __CONFIG_NETWORKING__
+#ifdef CONFIG_NETWORKING
        [SYS_eth_read] = {(syscall_t)sys_eth_read, "eth_read"},
        [SYS_eth_write] = {(syscall_t)sys_eth_write, "eth_write"},
        [SYS_eth_get_mac_addr] = {(syscall_t)sys_eth_get_mac_addr, "get_mac"},
        [SYS_eth_recv_check] = {(syscall_t)sys_eth_recv_check, "recv_check"},
 #endif
-#ifdef __CONFIG_ARSC_SERVER__
+#ifdef CONFIG_ARSC_SERVER
        [SYS_init_arsc] = {(syscall_t)sys_init_arsc, "init_arsc"},
 #endif
        [SYS_change_to_m] = {(syscall_t)sys_change_to_m, "change_to_m"},
@@ -1637,6 +1706,7 @@ const static struct sys_table_entry syscall_table[] = {
        [SYS_getcwd] = {(syscall_t)sys_getcwd, "getcwd"},
        [SYS_mkdir] = {(syscall_t)sys_mkdir, "mkdri"},
        [SYS_rmdir] = {(syscall_t)sys_rmdir, "rmdir"},
+       [SYS_pipe] = {(syscall_t)sys_pipe, "pipe"},
        [SYS_gettimeofday] = {(syscall_t)sys_gettimeofday, "gettime"},
        [SYS_tcgetattr] = {(syscall_t)sys_tcgetattr, "tcgetattr"},
        [SYS_tcsetattr] = {(syscall_t)sys_tcsetattr, "tcsetattr"},
@@ -1702,13 +1772,19 @@ void run_local_syscall(struct syscall *sysc)
 
        /* TODO: (UMEM) assert / pin the memory for the sysc */
        assert(irq_is_enabled());       /* in case we proc destroy */
-       user_mem_assert(pcpui->cur_proc, sysc, sizeof(struct syscall),
-                       sizeof(uintptr_t), PTE_USER_RW);
+       /* Abort on mem check failure, for now */
+       if (!user_mem_check(pcpui->cur_proc, sysc, sizeof(struct syscall),
+                           sizeof(uintptr_t), PTE_USER_RW))
+               return;
        pcpui->cur_sysc = sysc;                 /* let the core know which sysc it is */
        sysc->retval = syscall(pcpui->cur_proc, sysc->num, sysc->arg0, sysc->arg1,
                               sysc->arg2, sysc->arg3, sysc->arg4, sysc->arg5);
        /* Need to re-load pcpui, in case we migrated */
        pcpui = &per_cpu_info[core_id()];
+       /* Some 9ns paths set errstr, but not errno.  glibc will ignore errstr.
+        * this is somewhat hacky, since errno might get set unnecessarily */
+       if ((current_errstr()[0] != 0) && (!sysc->err))
+               sysc->err = EUNSPECIFIED;
        finish_sysc(sysc, pcpui->cur_proc);
        /* Can unpin (UMEM) at this point */
        pcpui->cur_sysc = 0;    /* no longer working on sysc */