File opening revisited
[akaros.git] / kern / src / syscall.c
index daf0704..19cb653 100644 (file)
@@ -206,39 +206,36 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
        struct proc *new_p;
 
        /* Copy in the path.  Consider putting an upper bound. */
-       t_path = kmalloc(path_l, 0);
-       if (!t_path) {
-               set_errno(current_tf, ENOMEM);
+       t_path = user_strdup_errno(p, path, path_l);
+       if (IS_ERR(t_path))
                return -1;
-       }
-       if (memcpy_from_user(p, t_path, path, path_l)) {
-               kfree(t_path);
-               set_errno(current_tf, EINVAL);
-               return -1;
-       }
-       program = path_to_file(t_path);
-       kfree(t_path);
+       program = do_file_open(t_path, 0, 0);
+       user_memdup_free(p, t_path);
        if (!program)
                return -1;                      /* presumably, errno is already set */
-       new_p = proc_create(program, 0, 0);
+       /* TODO: need to split the proc creation, since you must load after setting
+        * args/env, since auxp gets set up there. */
+       //new_p = proc_create(program, 0, 0);
+       if (proc_alloc(&new_p, current))
+               goto mid_error;
        /* Set the argument stuff needed by glibc */
-       if (memcpy_from_user(p, new_p->procinfo->argp, pi->argp, sizeof(pi->argp))){
-               atomic_dec(&program->f_refcnt); /* TODO: REF */
-               proc_destroy(new_p);
-               set_errno(current_tf, EINVAL);
-               return -1;
-       }
-       if (memcpy_from_user(p, new_p->procinfo->argbuf, pi->argbuf,
-                            sizeof(pi->argbuf))) {
-               atomic_dec(&program->f_refcnt); /* TODO: REF */
-               proc_destroy(new_p);
-               set_errno(current_tf, EINVAL);
-               return -1;
-       }
+       if (memcpy_from_user_errno(p, new_p->procinfo->argp, pi->argp,
+                                  sizeof(pi->argp)))
+               goto late_error;
+       if (memcpy_from_user_errno(p, new_p->procinfo->argbuf, pi->argbuf,
+                                  sizeof(pi->argbuf)))
+               goto late_error;
+       if (load_elf(new_p, program))
+               goto late_error;
+       kref_put(&program->f_kref);
        pid = new_p->pid;
-       proc_decref(new_p, 1);  /* give up the reference created in proc_create() */
-       atomic_dec(&program->f_refcnt);         /* TODO: REF / KREF */
+       kref_put(&new_p->kref); /* give up the reference created in proc_create() */
        return pid;
+late_error:
+       proc_destroy(new_p);
+mid_error:
+       kref_put(&program->f_kref);
+       return -1;
 }
 
 /* Makes process PID runnable.  Consider moving the functionality to process.c */
@@ -253,17 +250,17 @@ static error_t sys_proc_run(struct proc *p, unsigned pid)
        spin_lock(&p->proc_lock);
        // make sure we have access and it's in the right state to be activated
        if (!proc_controls(p, target)) {
-               proc_decref(target, 1);
+               kref_put(&target->kref);
                retval = -EPERM;
        } else if (target->state != PROC_CREATED) {
-               proc_decref(target, 1);
+               kref_put(&target->kref);
                retval = -EINVAL;
        } else {
                __proc_set_state(target, PROC_RUNNABLE_S);
                schedule_proc(target);
        }
        spin_unlock(&p->proc_lock);
-       proc_decref(target, 1);
+       kref_put(&target->kref);
        return retval;
 }
 
@@ -281,20 +278,20 @@ static error_t sys_proc_destroy(struct proc *p, pid_t pid, int exitcode)
                return -1;
        }
        if (!proc_controls(p, p_to_die)) {
-               proc_decref(p_to_die, 1);
+               kref_put(&p_to_die->kref);
                set_errno(current_tf, EPERM);
                return -1;
        }
        if (p_to_die == p) {
                // syscall code and pid2proc both have edible references, only need 1.
                p->exitcode = exitcode;
-               proc_decref(p, 1);
+               kref_put(&p_to_die->kref);
                printd("[PID %d] proc exiting gracefully (code %d)\n", p->pid,exitcode);
        } else {
                printd("[%d] destroying proc %d\n", p->pid, p_to_die->pid);
        }
        proc_destroy(p_to_die);
-       proc_decref(p_to_die, 1);
+       kref_put(&p_to_die->kref);
        return ESUCCESS;
 }
 
@@ -368,10 +365,12 @@ static ssize_t sys_fork(env_t* e)
        /* for now, just copy the contents of every present page in the entire
         * address space. */
        if (env_user_mem_walk(e, 0, UMAPTOP, &copy_page, env)) {
-               proc_decref(env,2);
+               proc_destroy(env);      /* this is prob what you want, not decref by 2 */
                set_errno(current_tf,ENOMEM);
                return -1;
        }
+       
+       /* TODO: copy all open files, except O_CLOEXEC */
 
        __proc_set_state(env, PROC_RUNNABLE_S);
        schedule_proc(env);
@@ -400,32 +399,20 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
        if(p->state != PROC_RUNNING_S)
                return -1;
        /* Copy in the path.  Consider putting an upper bound. */
-       t_path = kmalloc(path_l, 0);
-       if (!t_path) {
-               set_errno(current_tf, ENOMEM);
-               return -1;
-       }
-       if (memcpy_from_user(p, t_path, path, path_l)) {
-               kfree(t_path);
-               set_errno(current_tf, EINVAL);
+       t_path = user_strdup_errno(p, path, path_l);
+       if (IS_ERR(t_path))
                return -1;
-       }
-       program = path_to_file(t_path);
-       kfree(t_path);
+       program = do_file_open(t_path, 0, 0);
+       user_memdup_free(p, t_path);
        if (!program)
                return -1;                      /* presumably, errno is already set */
        /* Set the argument stuff needed by glibc */
-       if (memcpy_from_user(p, p->procinfo->argp, pi->argp, sizeof(pi->argp))) {
-               atomic_dec(&program->f_refcnt); /* TODO: REF */
-               set_errno(current_tf, EINVAL);
-               return -1;
-       }
-       if (memcpy_from_user(p, p->procinfo->argbuf, pi->argbuf,
-                            sizeof(pi->argbuf))) {
-               atomic_dec(&program->f_refcnt); /* TODO: REF */
-               set_errno(current_tf, EINVAL);
-               return -1;
-       }
+       if (memcpy_from_user_errno(p, p->procinfo->argp, pi->argp,
+                                  sizeof(pi->argp)))
+               goto mid_error;
+       if (memcpy_from_user_errno(p, p->procinfo->argbuf, pi->argbuf,
+                                  sizeof(pi->argbuf)))
+               goto mid_error;
        /* This is the point of no return for the process. */
        /* TODO: issues with this: Need to also assert there are no outstanding
         * users of the sysrings.  the ldt page will get freed shortly, so that's
@@ -434,13 +421,17 @@ static int sys_exec(struct proc *p, char *path, size_t path_l,
        memset(p->procdata, 0, sizeof(procdata_t));
        env_user_mem_free(p, 0, UMAPTOP);
        if (load_elf(p, program)) {
+               kref_put(&program->f_kref);
                proc_destroy(p);
                smp_idle();             /* syscall can't return on failure now */
        }
-       printk("[PID %d] exec %s\n", p->pid, file_name(program));
-       atomic_dec(&program->f_refcnt);         /* TODO: (REF) / KREF */
+       printd("[PID %d] exec %s\n", p->pid, file_name(program));
+       kref_put(&program->f_kref);
        *current_tf = p->env_tf;
        return 0;
+mid_error:
+       kref_put(&program->f_kref);
+       return -1;
 }
 
 static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
@@ -479,7 +470,9 @@ static ssize_t sys_trywait(env_t* e, pid_t pid, int* status)
                }
 
                // if the wait succeeded, decref twice
-               proc_decref(p,1 + (ret == 0));
+               if (ret == 0)
+                       kref_put(&p->kref);
+               kref_put(&p->kref);
                return ret;
        }
 
@@ -558,7 +551,7 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
        page_t* page;
        error_t e = upage_alloc(p1, &page,1);
        if (e < 0) {
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return e;
        }
 
@@ -566,7 +559,7 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
                        (void*SNT)UTEXT, (void*SNT)UTOP, p2_flags);
        if (p2_addr == NULL) {
                page_free(page);
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return -EFAIL;
        }
 
@@ -575,11 +568,11 @@ static ssize_t sys_shared_page_alloc(env_t* p1,
        if(p1_addr == NULL) {
                page_remove(p2->env_pgdir, p2_addr);
                page_free(page);
-               proc_decref(p2, 1);
+               kref_put(&p2->kref);
                return -EFAIL;
        }
        *addr = p1_addr;
-       proc_decref(p2, 1);
+       kref_put(&p2->kref);
        return ESUCCESS;
 }
 
@@ -604,14 +597,14 @@ static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
                return -1;
        }
        if (!proc_controls(p, target)) {
-               proc_decref(target, 1);
+               kref_put(&target->kref);
                set_errno(current_tf, EPERM);
                return -1;
        }
        /* if the user provided a notif_event, copy it in and use that */
        if (u_ne) {
                if (memcpy_from_user(p, &local_ne, u_ne, sizeof(struct notif_event))) {
-                       proc_decref(target, 1);
+                       kref_put(&target->kref);
                        set_errno(current_tf, EINVAL);
                        return -1;
                }
@@ -619,7 +612,7 @@ static int sys_notify(struct proc *p, int target_pid, unsigned int notif,
        } else {
                proc_notify(target, notif, 0);
        }
-       proc_decref(target, 1);
+       kref_put(&target->kref);
        return 0;
 }
 
@@ -859,23 +852,46 @@ intreg_t sys_pread(struct proc* p, int fd, void* buf, int len, int offset)
        return ret;
 }
 
-intreg_t sys_open(struct proc* p, const char* path, int oflag, int mode)
+/* Checks args/reads in the path, opens the file, and inserts it into the
+ * process's open file list. 
+ *
+ * TODO: take the path length */
+intreg_t sys_open(struct proc *p, const char *path, int oflag, int mode)
 {
-       printd("File Open, p: %p, path: %s, oflag: %d, mode: 0x%x\n", p, path, oflag, mode);
-       char* fn = user_strdup_errno(p,path,PGSIZE);
-       if(fn == NULL) {
-               printd("File Open, user_strdup_errno failed\n");
+       int fd = 0;
+       struct file *file;
+
+       char *t_path = user_strdup_errno(p, path, PGSIZE);
+       if (t_path == NULL)
+               return -1;
+       file = do_file_open(t_path, oflag, mode);
+       user_memdup_free(p, t_path);
+       if (!file)
+               return -1;
+       fd = insert_file(&p->open_files, file); /* stores the ref to file */
+       kref_put(&file->f_kref);
+       if (fd < 0) {
+               warn("File insertion failed");
                return -1;
        }
-       printd("File Open, About to open\n");
-       int ret = ufe(open,PADDR(fn),oflag,mode,0);
-       printd("File Open, res=%d\n", ret);
-       user_memdup_free(p,fn);
-       return ret;
+       printd("File Open, res=%d\n", fd);
+       return fd;
 }
-intreg_t sys_close(struct proc* p, int fd)
+
+intreg_t sys_close(struct proc *p, int fd)
 {
-       return ufe(close,fd,0,0,0);
+       struct file *file = put_file_from_fd(&p->open_files, fd);
+       if (!file) {
+               set_errno(current_tf, EBADF);
+               return -1;
+       }
+       /* TEMP TEST */
+       if (kref_refcnt(&file->f_kref)) {
+               printk("sys_close: Detected positive refcnt %d for file %s\n",
+                      kref_refcnt(&file->f_kref), file_name(file));
+               panic("Idiot.");
+       }
+       return 0;
 }
 
 #define NEWLIB_STAT_SIZE 64
@@ -1185,7 +1201,7 @@ intreg_t process_generic_syscalls(struct proc *p, size_t max)
         * the *p). */
        // TODO: ought to be unnecessary, if you called this right, kept here for
        // now in case anyone actually uses the ARSCs.
-       proc_incref(p, 1);
+       kref_get(&p->kref, 1);
 
        // max is the most we'll process.  max = 0 means do as many as possible
        while (RING_HAS_UNCONSUMED_REQUESTS(sysbr) && ((!max)||(count < max)) ) {
@@ -1215,7 +1231,7 @@ intreg_t process_generic_syscalls(struct proc *p, size_t max)
        }
        // load sane page tables (and don't rely on decref to do it for you).
        lcr3(boot_cr3);
-       proc_decref(p, 1);
+       kref_put(&p->kref);
        return (intreg_t)count;
 }