proc_syscalls.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <types.h>
  2. #include <kern/errno.h>
  3. #include <kern/unistd.h>
  4. #include <kern/wait.h>
  5. #include <lib.h>
  6. #include <syscall.h>
  7. #include <current.h>
  8. #include <proc.h>
  9. #include <thread.h>
  10. #include <addrspace.h>
  11. #include <copyinout.h>
  12. /* this implementation of sys__exit does not do anything with the exit code */
  13. /* this needs to be fixed to get exit() and waitpid() working properly */
  14. void sys__exit(int exitcode) {
  15. struct addrspace *as;
  16. struct proc *p = curproc;
  17. /* for now, just include this to keep the compiler from complaining about
  18. an unused variable */
  19. (void)exitcode;
  20. DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
  21. KASSERT(curproc->p_addrspace != NULL);
  22. as_deactivate();
  23. /*
  24. * clear p_addrspace before calling as_destroy. Otherwise if
  25. * as_destroy sleeps (which is quite possible) when we
  26. * come back we'll be calling as_activate on a
  27. * half-destroyed address space. This tends to be
  28. * messily fatal.
  29. */
  30. as = curproc_setas(NULL);
  31. as_destroy(as);
  32. /* detach this thread from its process */
  33. /* note: curproc cannot be used after this call */
  34. proc_remthread(curthread);
  35. /* if this is the last user process in the system, proc_destroy()
  36. will wake up the kernel menu thread */
  37. proc_destroy(p);
  38. thread_exit();
  39. /* thread_exit() does not return, so we should never get here */
  40. panic("return from thread_exit in sys_exit\n");
  41. }
  42. /* stub handler for getpid() system call */
  43. int
  44. sys_getpid(pid_t *retval)
  45. {
  46. /* for now, this is just a stub that always returns a PID of 1 */
  47. /* you need to fix this to make it work properly */
  48. *retval = 1;
  49. return(0);
  50. }
  51. /* stub handler for waitpid() system call */
  52. int
  53. sys_waitpid(pid_t pid,
  54. userptr_t status,
  55. int options,
  56. pid_t *retval)
  57. {
  58. int exitstatus;
  59. int result;
  60. /* this is just a stub implementation that always reports an
  61. exit status of 0, regardless of the actual exit status of
  62. the specified process.
  63. In fact, this will return 0 even if the specified process
  64. is still running, and even if it never existed in the first place.
  65. Fix this!
  66. */
  67. if (options != 0) {
  68. return(EINVAL);
  69. }
  70. /* for now, just pretend the exitstatus is 0 */
  71. exitstatus = 0;
  72. result = copyout((void *)&exitstatus,status,sizeof(int));
  73. if (result) {
  74. return(result);
  75. }
  76. *retval = pid;
  77. return(0);
  78. }