2
0

os-proc.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * process related system call shims and definitions
  3. *
  4. * Copyright (c) 2013-14 Stacey D. Son
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef BSD_USER_FREEBSD_OS_PROC_H
  20. #define BSD_USER_FREEBSD_OS_PROC_H
  21. #include <sys/param.h>
  22. #include <sys/procctl.h>
  23. #include <sys/signal.h>
  24. #include <sys/types.h>
  25. #include <sys/procdesc.h>
  26. #include <sys/wait.h>
  27. #include <unistd.h>
  28. #include "target_arch_cpu.h"
  29. pid_t safe_wait4(pid_t wpid, int *status, int options, struct rusage *rusage);
  30. pid_t safe_wait6(idtype_t idtype, id_t id, int *status, int options,
  31. struct __wrusage *wrusage, siginfo_t *infop);
  32. extern int __setugid(int flag);
  33. /* execve(2) */
  34. static inline abi_long do_freebsd_execve(abi_ulong path_or_fd, abi_ulong argp,
  35. abi_ulong envp)
  36. {
  37. return freebsd_exec_common(path_or_fd, argp, envp, 0);
  38. }
  39. /* fexecve(2) */
  40. static inline abi_long do_freebsd_fexecve(abi_ulong path_or_fd, abi_ulong argp,
  41. abi_ulong envp)
  42. {
  43. return freebsd_exec_common(path_or_fd, argp, envp, 1);
  44. }
  45. /* wait4(2) */
  46. static inline abi_long do_freebsd_wait4(abi_long arg1, abi_ulong target_status,
  47. abi_long arg3, abi_ulong target_rusage)
  48. {
  49. abi_long ret;
  50. int status;
  51. struct rusage rusage, *rusage_ptr = NULL;
  52. if (target_rusage) {
  53. rusage_ptr = &rusage;
  54. }
  55. ret = get_errno(safe_wait4(arg1, &status, arg3, rusage_ptr));
  56. if (ret < 0) {
  57. return ret;
  58. }
  59. if (target_status != 0) {
  60. status = host_to_target_waitstatus(status);
  61. if (put_user_s32(status, target_status) != 0) {
  62. return -TARGET_EFAULT;
  63. }
  64. }
  65. if (target_rusage != 0) {
  66. host_to_target_rusage(target_rusage, &rusage);
  67. }
  68. return ret;
  69. }
  70. /* wait6(2) */
  71. static inline abi_long do_freebsd_wait6(void *cpu_env, abi_long idtype,
  72. abi_long id1, abi_long id2,
  73. abi_ulong target_status, abi_long options, abi_ulong target_wrusage,
  74. abi_ulong target_infop, abi_ulong pad1)
  75. {
  76. abi_long ret;
  77. int status;
  78. struct __wrusage wrusage, *wrusage_ptr = NULL;
  79. siginfo_t info;
  80. void *p;
  81. if (regpairs_aligned(cpu_env) != 0) {
  82. /* printf("shifting args\n"); */
  83. /* 64-bit id is aligned, so shift all the arguments over by one */
  84. id1 = id2;
  85. id2 = target_status;
  86. target_status = options;
  87. options = target_wrusage;
  88. target_wrusage = target_infop;
  89. target_infop = pad1;
  90. }
  91. if (target_wrusage) {
  92. wrusage_ptr = &wrusage;
  93. }
  94. ret = get_errno(safe_wait6(idtype, target_arg64(id1, id2),
  95. &status, options, wrusage_ptr, &info));
  96. if (ret < 0) {
  97. return ret;
  98. }
  99. if (target_status != 0) {
  100. status = host_to_target_waitstatus(status);
  101. if (put_user_s32(status, target_status) != 0) {
  102. return -TARGET_EFAULT;
  103. }
  104. }
  105. if (target_wrusage != 0) {
  106. host_to_target_wrusage(target_wrusage, &wrusage);
  107. }
  108. if (target_infop != 0) {
  109. p = lock_user(VERIFY_WRITE, target_infop, sizeof(target_siginfo_t), 0);
  110. if (p == NULL) {
  111. return -TARGET_EFAULT;
  112. }
  113. host_to_target_siginfo(p, &info);
  114. unlock_user(p, target_infop, sizeof(target_siginfo_t));
  115. }
  116. return ret;
  117. }
  118. /* setloginclass(2) */
  119. static inline abi_long do_freebsd_setloginclass(abi_ulong arg1)
  120. {
  121. abi_long ret;
  122. void *p;
  123. p = lock_user_string(arg1);
  124. if (p == NULL) {
  125. return -TARGET_EFAULT;
  126. }
  127. ret = get_errno(setloginclass(p));
  128. unlock_user(p, arg1, 0);
  129. return ret;
  130. }
  131. /* getloginclass(2) */
  132. static inline abi_long do_freebsd_getloginclass(abi_ulong arg1, abi_ulong arg2)
  133. {
  134. abi_long ret;
  135. void *p;
  136. p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
  137. if (p == NULL) {
  138. return -TARGET_EFAULT;
  139. }
  140. ret = get_errno(getloginclass(p, arg2));
  141. unlock_user(p, arg1, arg2);
  142. return ret;
  143. }
  144. /* pdgetpid(2) */
  145. static inline abi_long do_freebsd_pdgetpid(abi_long fd, abi_ulong target_pidp)
  146. {
  147. abi_long ret;
  148. pid_t pid;
  149. ret = get_errno(pdgetpid(fd, &pid));
  150. if (!is_error(ret)) {
  151. if (put_user_u32(pid, target_pidp)) {
  152. return -TARGET_EFAULT;
  153. }
  154. }
  155. return ret;
  156. }
  157. /* undocumented __setugid */
  158. static inline abi_long do_freebsd___setugid(abi_long arg1)
  159. {
  160. return -TARGET_ENOSYS;
  161. }
  162. /* fork(2) */
  163. static inline abi_long do_freebsd_fork(void *cpu_env)
  164. {
  165. abi_long ret;
  166. abi_ulong child_flag;
  167. fork_start();
  168. ret = fork();
  169. if (ret == 0) {
  170. /* child */
  171. child_flag = 1;
  172. target_cpu_clone_regs(cpu_env, 0);
  173. } else {
  174. /* parent */
  175. child_flag = 0;
  176. }
  177. /*
  178. * The fork system call sets a child flag in the second return
  179. * value: 0 for parent process, 1 for child process.
  180. */
  181. set_second_rval(cpu_env, child_flag);
  182. fork_end(ret);
  183. return ret;
  184. }
  185. /* vfork(2) */
  186. static inline abi_long do_freebsd_vfork(void *cpu_env)
  187. {
  188. return do_freebsd_fork(cpu_env);
  189. }
  190. /* rfork(2) */
  191. static inline abi_long do_freebsd_rfork(void *cpu_env, abi_long flags)
  192. {
  193. abi_long ret;
  194. abi_ulong child_flag;
  195. /*
  196. * XXX We need to handle RFMEM here, as well. Neither are safe to execute
  197. * as-is on x86 hosts because they'll split memory but not the stack,
  198. * wreaking havoc on host architectures that use the stack to store the
  199. * return address as both threads try to pop it off. Rejecting RFSPAWN
  200. * entirely for now is ok, the only consumer at the moment is posix_spawn
  201. * and it will fall back to classic vfork(2) if we return EINVAL.
  202. */
  203. if ((flags & TARGET_RFSPAWN) != 0) {
  204. return -TARGET_EINVAL;
  205. }
  206. fork_start();
  207. ret = rfork(flags);
  208. if (ret == 0) {
  209. /* child */
  210. child_flag = 1;
  211. target_cpu_clone_regs(cpu_env, 0);
  212. } else {
  213. /* parent */
  214. child_flag = 0;
  215. }
  216. /*
  217. * The fork system call sets a child flag in the second return
  218. * value: 0 for parent process, 1 for child process.
  219. */
  220. set_second_rval(cpu_env, child_flag);
  221. fork_end(ret);
  222. return ret;
  223. }
  224. /* pdfork(2) */
  225. static inline abi_long do_freebsd_pdfork(void *cpu_env, abi_ulong target_fdp,
  226. abi_long flags)
  227. {
  228. abi_long ret;
  229. abi_ulong child_flag;
  230. int fd;
  231. fork_start();
  232. ret = pdfork(&fd, flags);
  233. if (ret == 0) {
  234. /* child */
  235. child_flag = 1;
  236. target_cpu_clone_regs(cpu_env, 0);
  237. } else {
  238. /* parent */
  239. child_flag = 0;
  240. if (put_user_s32(fd, target_fdp)) {
  241. return -TARGET_EFAULT;
  242. }
  243. }
  244. /*
  245. * The fork system call sets a child flag in the second return
  246. * value: 0 for parent process, 1 for child process.
  247. */
  248. set_second_rval(cpu_env, child_flag);
  249. fork_end(ret);
  250. return ret;
  251. }
  252. #endif /* BSD_USER_FREEBSD_OS_PROC_H */