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