os161-1.99
 All Data Structures
proc_syscalls.c
00001 #include <types.h>
00002 #include <kern/errno.h>
00003 #include <kern/unistd.h>
00004 #include <kern/wait.h>
00005 #include <lib.h>
00006 #include <syscall.h>
00007 #include <current.h>
00008 #include <proc.h>
00009 #include <thread.h>
00010 #include <addrspace.h>
00011 #include <copyinout.h>
00012 
00013   /* this implementation of sys__exit does not do anything with the exit code */
00014   /* this needs to be fixed to get exit() and waitpid() working properly */
00015 
00016 void sys__exit(int exitcode) {
00017 
00018   struct addrspace *as;
00019   struct proc *p = curproc;
00020   /* for now, just include this to keep the compiler from complaining about
00021      an unused variable */
00022   (void)exitcode;
00023 
00024   DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
00025 
00026   KASSERT(curproc->p_addrspace != NULL);
00027   as_deactivate();
00028   /*
00029    * clear p_addrspace before calling as_destroy. Otherwise if
00030    * as_destroy sleeps (which is quite possible) when we
00031    * come back we'll be calling as_activate on a
00032    * half-destroyed address space. This tends to be
00033    * messily fatal.
00034    */
00035   as = curproc_setas(NULL);
00036   as_destroy(as);
00037 
00038   /* detach this thread from its process */
00039   /* note: curproc cannot be used after this call */
00040   proc_remthread(curthread);
00041 
00042   /* if this is the last user process in the system, proc_destroy()
00043      will wake up the kernel menu thread */
00044   proc_destroy(p);
00045   
00046   thread_exit();
00047   /* thread_exit() does not return, so we should never get here */
00048   panic("return from thread_exit in sys_exit\n");
00049 }
00050 
00051 
00052 /* stub handler for getpid() system call                */
00053 int
00054 sys_getpid(pid_t *retval)
00055 {
00056   /* for now, this is just a stub that always returns a PID of 1 */
00057   /* you need to fix this to make it work properly */
00058   *retval = 1;
00059   return(0);
00060 }
00061 
00062 /* stub handler for waitpid() system call                */
00063 
00064 int
00065 sys_waitpid(pid_t pid,
00066             userptr_t status,
00067             int options,
00068             pid_t *retval)
00069 {
00070   int exitstatus;
00071   int result;
00072 
00073   /* this is just a stub implementation that always reports an
00074      exit status of 0, regardless of the actual exit status of
00075      the specified process.   
00076      In fact, this will return 0 even if the specified process
00077      is still running, and even if it never existed in the first place.
00078 
00079      Fix this!
00080   */
00081 
00082   if (options != 0) {
00083     return(EINVAL);
00084   }
00085   /* for now, just pretend the exitstatus is 0 */
00086   exitstatus = 0;
00087   result = copyout((void *)&exitstatus,status,sizeof(int));
00088   if (result) {
00089     return(result);
00090   }
00091   *retval = pid;
00092   return(0);
00093 }
00094 
 All Data Structures