2 * process related system call shims and definitions
4 * Copyright (c) 2013-2014 Stacey D. Son
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.
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.
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/>.
23 #include <sys/resource.h>
26 #include "gdbstub/syscalls.h"
27 #include "qemu/plugin.h"
29 extern int _getlogin(char*, int);
30 int bsd_get_ncpu(void);
33 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
36 qemu_plugin_user_exit();
43 static inline abi_long do_bsd_getgroups(abi_long gidsetsize, abi_long arg2)
46 uint32_t *target_grouplist;
47 g_autofree gid_t *grouplist;
50 grouplist = g_try_new(gid_t, gidsetsize);
51 ret = get_errno(getgroups(gidsetsize, grouplist));
52 if (gidsetsize != 0) {
54 target_grouplist = lock_user(VERIFY_WRITE, arg2, gidsetsize * 2, 0);
55 if (!target_grouplist) {
56 return -TARGET_EFAULT;
58 for (i = 0; i < ret; i++) {
59 target_grouplist[i] = tswap32(grouplist[i]);
61 unlock_user(target_grouplist, arg2, gidsetsize * 2);
68 static inline abi_long do_bsd_setgroups(abi_long gidsetsize, abi_long arg2)
70 uint32_t *target_grouplist;
71 g_autofree gid_t *grouplist;
74 grouplist = g_try_new(gid_t, gidsetsize);
75 target_grouplist = lock_user(VERIFY_READ, arg2, gidsetsize * 2, 1);
76 if (!target_grouplist) {
77 return -TARGET_EFAULT;
79 for (i = 0; i < gidsetsize; i++) {
80 grouplist[i] = tswap32(target_grouplist[i]);
82 unlock_user(target_grouplist, arg2, 0);
83 return get_errno(setgroups(gidsetsize, grouplist));
87 static inline abi_long do_bsd_umask(abi_long arg1)
89 return get_errno(umask(arg1));
93 static inline abi_long do_bsd_setlogin(abi_long arg1)
98 p = lock_user_string(arg1);
100 return -TARGET_EFAULT;
102 ret = get_errno(setlogin(p));
103 unlock_user(p, arg1, 0);
109 static inline abi_long do_bsd_getlogin(abi_long arg1, abi_long arg2)
114 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
116 return -TARGET_EFAULT;
118 ret = get_errno(_getlogin(p, arg2));
119 unlock_user(p, arg1, arg2);
125 static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr)
128 struct rusage rusage;
130 ret = get_errno(getrusage(who, &rusage));
131 if (!is_error(ret)) {
132 host_to_target_rusage(target_addr, &rusage);
138 static inline abi_long do_bsd_getrlimit(abi_long arg1, abi_ulong arg2)
141 int resource = target_to_host_resource(arg1);
142 struct target_rlimit *target_rlim;
147 rlim.rlim_cur = target_dflssiz;
148 rlim.rlim_max = target_maxssiz;
153 rlim.rlim_cur = target_dfldsiz;
154 rlim.rlim_max = target_maxdsiz;
159 ret = get_errno(getrlimit(resource, &rlim));
162 if (!is_error(ret)) {
163 if (!lock_user_struct(VERIFY_WRITE, target_rlim, arg2, 0)) {
164 return -TARGET_EFAULT;
166 target_rlim->rlim_cur = host_to_target_rlim(rlim.rlim_cur);
167 target_rlim->rlim_max = host_to_target_rlim(rlim.rlim_max);
168 unlock_user_struct(target_rlim, arg2, 1);
174 static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2)
177 int resource = target_to_host_resource(arg1);
178 struct target_rlimit *target_rlim;
181 if (RLIMIT_STACK == resource) {
182 /* XXX We should, maybe, allow the stack size to shrink */
185 if (!lock_user_struct(VERIFY_READ, target_rlim, arg2, 1)) {
186 return -TARGET_EFAULT;
188 rlim.rlim_cur = target_to_host_rlim(target_rlim->rlim_cur);
189 rlim.rlim_max = target_to_host_rlim(target_rlim->rlim_max);
190 unlock_user_struct(target_rlim, arg2, 0);
191 ret = get_errno(setrlimit(resource, &rlim));
197 static inline abi_long do_bsd_getpid(void)
199 return get_errno(getpid());
203 static inline abi_long do_bsd_getppid(void)
205 return get_errno(getppid());
209 static inline abi_long do_bsd_getuid(void)
211 return get_errno(getuid());
215 static inline abi_long do_bsd_geteuid(void)
217 return get_errno(geteuid());
221 static inline abi_long do_bsd_getgid(void)
223 return get_errno(getgid());
227 static inline abi_long do_bsd_getegid(void)
229 return get_errno(getegid());
233 static inline abi_long do_bsd_setuid(abi_long arg1)
235 return get_errno(setuid(arg1));
239 static inline abi_long do_bsd_seteuid(abi_long arg1)
241 return get_errno(seteuid(arg1));
245 static inline abi_long do_bsd_setgid(abi_long arg1)
247 return get_errno(setgid(arg1));
251 static inline abi_long do_bsd_setegid(abi_long arg1)
253 return get_errno(setegid(arg1));
257 static inline abi_long do_bsd_getpgid(pid_t pid)
259 return get_errno(getpgid(pid));
263 static inline abi_long do_bsd_setpgid(int pid, int pgrp)
265 return get_errno(setpgid(pid, pgrp));
269 static inline abi_long do_bsd_getpgrp(void)
271 return get_errno(getpgrp());
275 static inline abi_long do_bsd_setreuid(abi_long arg1, abi_long arg2)
277 return get_errno(setreuid(arg1, arg2));
281 static inline abi_long do_bsd_setregid(abi_long arg1, abi_long arg2)
283 return get_errno(setregid(arg1, arg2));
287 static inline abi_long do_bsd_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
289 return get_errno(setresgid(rgid, egid, sgid));
293 static inline abi_long do_bsd_setresuid(uid_t ruid, uid_t euid, uid_t suid)
295 return get_errno(setresuid(ruid, euid, suid));
299 static inline abi_long do_bsd_getresuid(abi_ulong arg1, abi_ulong arg2,
303 uid_t ruid, euid, suid;
305 ret = get_errno(getresuid(&ruid, &euid, &suid));
309 if (put_user_s32(ruid, arg1)) {
310 return -TARGET_EFAULT;
312 if (put_user_s32(euid, arg2)) {
313 return -TARGET_EFAULT;
315 if (put_user_s32(suid, arg3)) {
316 return -TARGET_EFAULT;
322 static inline abi_long do_bsd_getresgid(abi_ulong arg1, abi_ulong arg2,
326 uid_t ruid, euid, suid;
328 ret = get_errno(getresgid(&ruid, &euid, &suid));
332 if (put_user_s32(ruid, arg1)) {
333 return -TARGET_EFAULT;
335 if (put_user_s32(euid, arg2)) {
336 return -TARGET_EFAULT;
338 if (put_user_s32(suid, arg3)) {
339 return -TARGET_EFAULT;
345 static inline abi_long do_bsd_getsid(abi_long arg1)
347 return get_errno(getsid(arg1));
351 static inline abi_long do_bsd_setsid(void)
353 return get_errno(setsid());
357 static inline abi_long do_bsd_issetugid(void)
359 return get_errno(issetugid());
363 static inline abi_long do_bsd_profil(abi_long arg1, abi_long arg2,
364 abi_long arg3, abi_long arg4)
366 return -TARGET_ENOSYS;
370 static inline abi_long do_bsd_ktrace(abi_long arg1, abi_long arg2,
371 abi_long arg3, abi_long arg4)
373 return -TARGET_ENOSYS;
377 static inline abi_long do_bsd_utrace(abi_long arg1, abi_long arg2)
379 return -TARGET_ENOSYS;
384 static inline abi_long do_bsd_ptrace(abi_long arg1, abi_long arg2,
385 abi_long arg3, abi_long arg4)
387 return -TARGET_ENOSYS;
391 static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
395 * Note that negative values are valid for getpriority, so we must
396 * differentiate based on errno settings.
399 ret = getpriority(which, who);
400 if (ret == -1 && errno != 0) {
401 return -host_to_target_errno(errno);
408 static inline abi_long do_bsd_setpriority(abi_long which, abi_long who,
411 return get_errno(setpriority(which, who, prio));
414 #endif /* !BSD_PROC_H_ */