Fixed file insertion of stdin/stdout/stderr
authorBarret Rhoden <brho@cs.berkeley.edu>
Tue, 24 Aug 2010 22:06:28 +0000 (15:06 -0700)
committerKevin Klues <klueska@cs.berkeley.edu>
Thu, 3 Nov 2011 00:35:53 +0000 (17:35 -0700)
Fork clones, so we don't want to bother inserting for those.  If we did,
we could run into issues with programs that dup then close the original
FDs.  This fix handles that and is reasonable enough.

kern/src/process.c
kern/src/syscall.c
kern/src/vfs.c

index 13a1a82..998f45f 100644 (file)
@@ -332,10 +332,6 @@ error_t proc_alloc(struct proc **pp, struct proc *parent)
        p->open_files.max_fdset = NR_FILE_DESC_DEFAULT;
        p->open_files.fd = p->open_files.fd_array;
        p->open_files.open_fds = (struct fd_set*)&p->open_files.open_fds_init;
-       /* Connect to stdin, stdout, stderr */
-       assert(insert_file(&p->open_files, dev_stdin,  0) == 0);
-       assert(insert_file(&p->open_files, dev_stdout, 0) == 1);
-       assert(insert_file(&p->open_files, dev_stderr, 0) == 2);
 
        atomic_inc(&num_envs);
        frontend_proc_init(p);
@@ -366,6 +362,10 @@ struct proc *proc_create(struct file *prog, char **argv, char **envp)
                panic("proc_create: %e", r);    /* one of 3 quaint usages of %e */
        procinfo_pack_args(p->procinfo, argv, envp);
        assert(load_elf(p, prog) == 0);
+       /* Connect to stdin, stdout, stderr */
+       assert(insert_file(&p->open_files, dev_stdin,  0) == 0);
+       assert(insert_file(&p->open_files, dev_stdout, 0) == 1);
+       assert(insert_file(&p->open_files, dev_stderr, 0) == 2);
        __proc_ready(p);
        return p;
 }
index 9e7e927..28dbfb9 100644 (file)
@@ -30,7 +30,8 @@
 #include <colored_caches.h>
 #include <hashtable.h>
 #include <arch/bitmask.h>
-#include <kfs.h> // eventually replace this with vfs.h
+#include <vfs.h>
+#include <devfs.h>
 #include <smp.h>
 #include <arsc_server.h>
 
@@ -230,6 +231,10 @@ static int sys_proc_create(struct proc *p, char *path, size_t path_l,
        if (load_elf(new_p, program))
                goto late_error;
        kref_put(&program->f_kref);
+       /* Connect to stdin, stdout, stderr (part of proc_create()) */
+       assert(insert_file(&new_p->open_files, dev_stdin,  0) == 0);
+       assert(insert_file(&new_p->open_files, dev_stdout, 0) == 1);
+       assert(insert_file(&new_p->open_files, dev_stderr, 0) == 2);
        __proc_ready(new_p);
        pid = new_p->pid;
        kref_put(&new_p->kref); /* give up the reference created in proc_create() */
@@ -386,7 +391,6 @@ static ssize_t sys_fork(env_t* e)
        // when the parent dies, or at least decref it
 
        printd("[PID %d] fork PID %d\n",e->pid,env->pid);
-
        return env->pid;
 }
 
index 0f57ee8..464714d 100644 (file)
@@ -1637,11 +1637,13 @@ void clone_files(struct files_struct *src, struct files_struct *dst)
                         * have a valid fdset higher than files */
                        assert(i < src->max_files);
                        file = src->fd[i];
-                       SET_BITMASK_BIT(dst->open_fds->fds_bits, i);
                        assert(i < dst->max_files && dst->fd[i] == 0);
+                       SET_BITMASK_BIT(dst->open_fds->fds_bits, i);
                        dst->fd[i] = file;
                        assert(file);
                        kref_get(&file->f_kref, 1);
+                       if (i >= dst->next_fd)
+                               dst->next_fd = i + 1;
                }
        }
        spin_unlock(&dst->lock);