file-exec.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Run exec_exec in a forked process.
  2. Copyright (C) 2008 Free Software Foundation, Inc.
  3. Written by FlÃvio Cruz <flaviocruz@gmail.com>
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  15. #include <sys/types.h>
  16. #include <sys/wait.h>
  17. #include <stdlib.h>
  18. #include <hurd.h>
  19. #include <hurd/exec.h>
  20. #include <unistd.h>
  21. pid_t
  22. do_exec_exec (file_t execserver,
  23. mach_port_t file,
  24. mach_msg_type_name_t filePoly,
  25. mach_port_t oldtask,
  26. int flags,
  27. data_t argv,
  28. mach_msg_type_number_t argvCnt,
  29. data_t envp,
  30. mach_msg_type_number_t envpCnt,
  31. portarray_t dtable,
  32. mach_msg_type_name_t dtablePoly,
  33. mach_msg_type_number_t dtableCnt,
  34. portarray_t portarray,
  35. mach_msg_type_name_t portarrayPoly,
  36. mach_msg_type_number_t portarrayCnt,
  37. intarray_t intarray,
  38. mach_msg_type_number_t intarrayCnt,
  39. mach_port_array_t deallocnames,
  40. mach_msg_type_number_t deallocnamesCnt,
  41. mach_port_array_t destroynames,
  42. mach_msg_type_number_t destroynamesCnt)
  43. {
  44. pid_t pid = fork ();
  45. unsigned i;
  46. if (pid == 0)
  47. { /* child */
  48. error_t err = 0;
  49. pid_t parent = getppid ();
  50. task_t parent_task = pid2task (parent);
  51. mach_msg_type_name_t foo;
  52. mach_port_t new_execserver;
  53. mach_port_t file_copy;
  54. err = mach_port_extract_right (parent_task, execserver,
  55. MACH_MSG_TYPE_COPY_SEND, &new_execserver,
  56. &foo);
  57. if (!err)
  58. {
  59. err = mach_port_extract_right (parent_task, file,
  60. MACH_MSG_TYPE_MOVE_SEND, &file_copy,
  61. &foo);
  62. }
  63. if (!err)
  64. {
  65. err = exec_exec (new_execserver, file_copy, filePoly,
  66. oldtask, flags, argv, argvCnt,
  67. envp, envpCnt, dtable, dtablePoly,
  68. dtableCnt, portarray, portarrayPoly,
  69. portarrayCnt, intarray, intarrayCnt,
  70. deallocnames, deallocnamesCnt,
  71. destroynames, destroynamesCnt);
  72. }
  73. mach_port_deallocate (mach_task_self (), parent_task);
  74. mach_port_deallocate (mach_task_self (), file_copy);
  75. mach_port_deallocate (mach_task_self (), oldtask);
  76. for (i = 0; i < dtableCnt; ++i)
  77. {
  78. mach_port_deallocate (mach_task_self (), dtable[i]);
  79. }
  80. for (i = 0; i < portarrayCnt; ++i)
  81. {
  82. mach_port_deallocate (mach_task_self (), portarray[i]);
  83. }
  84. exit (err);
  85. }
  86. mach_port_deallocate (mach_task_self (), oldtask);
  87. for (i = 0; i < dtableCnt; ++i)
  88. {
  89. mach_port_deallocate (mach_task_self (), dtable[i]);
  90. }
  91. for (i = 0; i < portarrayCnt; ++i)
  92. {
  93. mach_port_deallocate (mach_task_self (), portarray[i]);
  94. }
  95. return (pid);
  96. }
  97. int
  98. exec_finished (pid_t pid, int *status)
  99. {
  100. int ret = waitpid (pid, status, WNOHANG);
  101. if (ret > 0)
  102. {
  103. *status = WEXITSTATUS (*status);
  104. }
  105. return (ret > 0);
  106. }