Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / bsd-user / bsd-proc.h
blob8b1c2deea37d847bb20d0b6566780cac098a2928
1 /*
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/>.
20 #ifndef BSD_PROC_H_
21 #define BSD_PROC_H_
23 #include <sys/resource.h>
25 #include "qemu-bsd.h"
26 #include "gdbstub/syscalls.h"
27 #include "qemu/plugin.h"
29 extern int _getlogin(char*, int);
30 int bsd_get_ncpu(void);
32 /* exit(2) */
33 static inline abi_long do_bsd_exit(void *cpu_env, abi_long arg1)
35 gdb_exit(arg1);
36 qemu_plugin_user_exit();
37 _exit(arg1);
39 return 0;
42 /* getgroups(2) */
43 static inline abi_long do_bsd_getgroups(abi_long gidsetsize, abi_long arg2)
45 abi_long ret;
46 uint32_t *target_grouplist;
47 g_autofree gid_t *grouplist;
48 int i;
50 grouplist = g_try_new(gid_t, gidsetsize);
51 ret = get_errno(getgroups(gidsetsize, grouplist));
52 if (gidsetsize != 0) {
53 if (!is_error(ret)) {
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);
64 return ret;
67 /* setgroups(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;
72 int i;
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));
86 /* umask(2) */
87 static inline abi_long do_bsd_umask(abi_long arg1)
89 return get_errno(umask(arg1));
92 /* setlogin(2) */
93 static inline abi_long do_bsd_setlogin(abi_long arg1)
95 abi_long ret;
96 void *p;
98 p = lock_user_string(arg1);
99 if (p == NULL) {
100 return -TARGET_EFAULT;
102 ret = get_errno(setlogin(p));
103 unlock_user(p, arg1, 0);
105 return ret;
108 /* getlogin(2) */
109 static inline abi_long do_bsd_getlogin(abi_long arg1, abi_long arg2)
111 abi_long ret;
112 void *p;
114 p = lock_user(VERIFY_WRITE, arg1, arg2, 0);
115 if (p == NULL) {
116 return -TARGET_EFAULT;
118 ret = get_errno(_getlogin(p, arg2));
119 unlock_user(p, arg1, arg2);
121 return ret;
124 /* getrusage(2) */
125 static inline abi_long do_bsd_getrusage(abi_long who, abi_ulong target_addr)
127 abi_long ret;
128 struct rusage rusage;
130 ret = get_errno(getrusage(who, &rusage));
131 if (!is_error(ret)) {
132 host_to_target_rusage(target_addr, &rusage);
134 return ret;
137 /* getrlimit(2) */
138 static inline abi_long do_bsd_getrlimit(abi_long arg1, abi_ulong arg2)
140 abi_long ret;
141 int resource = target_to_host_resource(arg1);
142 struct target_rlimit *target_rlim;
143 struct rlimit rlim;
145 switch (resource) {
146 case RLIMIT_STACK:
147 rlim.rlim_cur = target_dflssiz;
148 rlim.rlim_max = target_maxssiz;
149 ret = 0;
150 break;
152 case RLIMIT_DATA:
153 rlim.rlim_cur = target_dfldsiz;
154 rlim.rlim_max = target_maxdsiz;
155 ret = 0;
156 break;
158 default:
159 ret = get_errno(getrlimit(resource, &rlim));
160 break;
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);
170 return ret;
173 /* setrlimit(2) */
174 static inline abi_long do_bsd_setrlimit(abi_long arg1, abi_ulong arg2)
176 abi_long ret;
177 int resource = target_to_host_resource(arg1);
178 struct target_rlimit *target_rlim;
179 struct rlimit rlim;
181 if (RLIMIT_STACK == resource) {
182 /* XXX We should, maybe, allow the stack size to shrink */
183 ret = -TARGET_EPERM;
184 } else {
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));
193 return ret;
196 /* getpid(2) */
197 static inline abi_long do_bsd_getpid(void)
199 return get_errno(getpid());
202 /* getppid(2) */
203 static inline abi_long do_bsd_getppid(void)
205 return get_errno(getppid());
208 /* getuid(2) */
209 static inline abi_long do_bsd_getuid(void)
211 return get_errno(getuid());
214 /* geteuid(2) */
215 static inline abi_long do_bsd_geteuid(void)
217 return get_errno(geteuid());
220 /* getgid(2) */
221 static inline abi_long do_bsd_getgid(void)
223 return get_errno(getgid());
226 /* getegid(2) */
227 static inline abi_long do_bsd_getegid(void)
229 return get_errno(getegid());
232 /* setuid(2) */
233 static inline abi_long do_bsd_setuid(abi_long arg1)
235 return get_errno(setuid(arg1));
238 /* seteuid(2) */
239 static inline abi_long do_bsd_seteuid(abi_long arg1)
241 return get_errno(seteuid(arg1));
244 /* setgid(2) */
245 static inline abi_long do_bsd_setgid(abi_long arg1)
247 return get_errno(setgid(arg1));
250 /* setegid(2) */
251 static inline abi_long do_bsd_setegid(abi_long arg1)
253 return get_errno(setegid(arg1));
256 /* getpgid(2) */
257 static inline abi_long do_bsd_getpgid(pid_t pid)
259 return get_errno(getpgid(pid));
262 /* setpgid(2) */
263 static inline abi_long do_bsd_setpgid(int pid, int pgrp)
265 return get_errno(setpgid(pid, pgrp));
268 /* getpgrp(2) */
269 static inline abi_long do_bsd_getpgrp(void)
271 return get_errno(getpgrp());
274 /* setreuid(2) */
275 static inline abi_long do_bsd_setreuid(abi_long arg1, abi_long arg2)
277 return get_errno(setreuid(arg1, arg2));
280 /* setregid(2) */
281 static inline abi_long do_bsd_setregid(abi_long arg1, abi_long arg2)
283 return get_errno(setregid(arg1, arg2));
286 /* setresgid(2) */
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));
292 /* setresuid(2) */
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));
298 /* getresuid(2) */
299 static inline abi_long do_bsd_getresuid(abi_ulong arg1, abi_ulong arg2,
300 abi_ulong arg3)
302 abi_long ret;
303 uid_t ruid, euid, suid;
305 ret = get_errno(getresuid(&ruid, &euid, &suid));
306 if (is_error(ret)) {
307 return ret;
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;
318 return ret;
321 /* getresgid(2) */
322 static inline abi_long do_bsd_getresgid(abi_ulong arg1, abi_ulong arg2,
323 abi_ulong arg3)
325 abi_long ret;
326 uid_t ruid, euid, suid;
328 ret = get_errno(getresgid(&ruid, &euid, &suid));
329 if (is_error(ret)) {
330 return ret;
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;
341 return ret;
344 /* getsid(2) */
345 static inline abi_long do_bsd_getsid(abi_long arg1)
347 return get_errno(getsid(arg1));
350 /* setsid(2) */
351 static inline abi_long do_bsd_setsid(void)
353 return get_errno(setsid());
356 /* issetugid(2) */
357 static inline abi_long do_bsd_issetugid(void)
359 return get_errno(issetugid());
362 /* profil(2) */
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;
369 /* ktrace(2) */
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;
376 /* utrace(2) */
377 static inline abi_long do_bsd_utrace(abi_long arg1, abi_long arg2)
379 return -TARGET_ENOSYS;
383 /* ptrace(2) */
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;
390 /* getpriority(2) */
391 static inline abi_long do_bsd_getpriority(abi_long which, abi_long who)
393 abi_long ret;
395 * Note that negative values are valid for getpriority, so we must
396 * differentiate based on errno settings.
398 errno = 0;
399 ret = getpriority(which, who);
400 if (ret == -1 && errno != 0) {
401 return -host_to_target_errno(errno);
404 return ret;
407 /* setpriority(2) */
408 static inline abi_long do_bsd_setpriority(abi_long which, abi_long who,
409 abi_long prio)
411 return get_errno(setpriority(which, who, prio));
414 #endif /* !BSD_PROC_H_ */