linux32_unistd.c revision 1.29 1 /* $NetBSD: linux32_unistd.c,v 1.29 2008/12/29 14:33:40 njoly Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35
36 __KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.29 2008/12/29 14:33:40 njoly Exp $");
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/fstypes.h>
41 #include <sys/signal.h>
42 #include <sys/dirent.h>
43 #include <sys/kernel.h>
44 #include <sys/fcntl.h>
45 #include <sys/select.h>
46 #include <sys/proc.h>
47 #include <sys/ucred.h>
48 #include <sys/swap.h>
49 #include <sys/kauth.h>
50
51 #include <machine/types.h>
52
53 #include <sys/syscallargs.h>
54
55 #include <compat/netbsd32/netbsd32.h>
56 #include <compat/netbsd32/netbsd32_conv.h>
57
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_machdep.h>
61 #include <compat/linux/common/linux_misc.h>
62 #include <compat/linux/common/linux_oldolduname.h>
63 #include <compat/linux/common/linux_ipc.h>
64 #include <compat/linux/common/linux_sem.h>
65 #include <compat/linux/linux_syscallargs.h>
66
67 #include <compat/linux32/common/linux32_types.h>
68 #include <compat/linux32/common/linux32_signal.h>
69 #include <compat/linux32/common/linux32_machdep.h>
70 #include <compat/linux32/common/linux32_sysctl.h>
71 #include <compat/linux32/common/linux32_socketcall.h>
72 #include <compat/linux32/linux32_syscallargs.h>
73
74 static int linux32_select1(struct lwp *, register_t *,
75 int, fd_set *, fd_set *, fd_set *, struct timeval *);
76
77 int
78 linux32_sys_brk(struct lwp *l, const struct linux32_sys_brk_args *uap, register_t *retval)
79 {
80 /* {
81 syscallarg(netbsd32_charp) nsize;
82 } */
83 struct linux_sys_brk_args ua;
84
85 NETBSD32TOP_UAP(nsize, char);
86 return linux_sys_brk(l, &ua, retval);
87 }
88
89 int
90 linux32_sys_llseek(struct lwp *l, const struct linux32_sys_llseek_args *uap, register_t *retval)
91 {
92 /* {
93 syscallarg(int) fd;
94 syscallarg(u_int32_t) ohigh;
95 syscallarg(u_int32_t) olow;
96 syscallarg(netbsd32_voidp) res;
97 syscallarg(int) whence;
98 } */
99 struct linux_sys_llseek_args ua;
100
101 NETBSD32TO64_UAP(fd);
102 NETBSD32TO64_UAP(ohigh);
103 NETBSD32TO64_UAP(olow);
104 NETBSD32TOP_UAP(res, void);
105 NETBSD32TO64_UAP(whence);
106
107 return linux_sys_llseek(l, &ua, retval);
108 }
109
110 int
111 linux32_sys_select(struct lwp *l, const struct linux32_sys_select_args *uap, register_t *retval)
112 {
113 /* {
114 syscallarg(int) nfds;
115 syscallarg(netbsd32_fd_setp_t) readfds;
116 syscallarg(netbsd32_fd_setp_t) writefds;
117 syscallarg(netbsd32_fd_setp_t) exceptfds;
118 syscallarg(netbsd32_timevalp_t) timeout;
119 } */
120
121 return linux32_select1(l, retval, SCARG(uap, nfds),
122 SCARG_P32(uap, readfds),
123 SCARG_P32(uap, writefds),
124 SCARG_P32(uap, exceptfds),
125 SCARG_P32(uap, timeout));
126 }
127
128 int
129 linux32_sys_oldselect(struct lwp *l, const struct linux32_sys_oldselect_args *uap, register_t *retval)
130 {
131 /* {
132 syscallarg(linux32_oldselectp_t) lsp;
133 } */
134 struct linux32_oldselect lsp32;
135 int error;
136
137 if ((error = copyin(SCARG_P32(uap, lsp), &lsp32, sizeof(lsp32))) != 0)
138 return error;
139
140 return linux32_select1(l, retval, lsp32.nfds,
141 NETBSD32PTR64(lsp32.readfds), NETBSD32PTR64(lsp32.writefds),
142 NETBSD32PTR64(lsp32.exceptfds), NETBSD32PTR64(lsp32.timeout));
143 }
144
145 static int
146 linux32_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
147 struct lwp *l;
148 register_t *retval;
149 int nfds;
150 fd_set *readfds, *writefds, *exceptfds;
151 struct timeval *timeout;
152 {
153 struct timeval tv0, tv1, utv, *tv = NULL;
154 struct netbsd32_timeval utv32;
155 int error;
156
157 timerclear(&utv); /* XXX GCC4 */
158
159 /*
160 * Store current time for computation of the amount of
161 * time left.
162 */
163 if (timeout) {
164 if ((error = copyin(timeout, &utv32, sizeof(utv32))))
165 return error;
166
167 netbsd32_to_timeval(&utv32, &utv);
168
169 if (itimerfix(&utv)) {
170 /*
171 * The timeval was invalid. Convert it to something
172 * valid that will act as it does under Linux.
173 */
174 utv.tv_sec += utv.tv_usec / 1000000;
175 utv.tv_usec %= 1000000;
176 if (utv.tv_usec < 0) {
177 utv.tv_sec -= 1;
178 utv.tv_usec += 1000000;
179 }
180 if (utv.tv_sec < 0)
181 timerclear(&utv);
182 }
183 microtime(&tv0);
184 tv = &utv;
185 }
186
187 error = selcommon(l, retval, nfds,
188 readfds, writefds, exceptfds, tv, NULL);
189
190 if (error) {
191 /*
192 * See fs/select.c in the Linux kernel. Without this,
193 * Maelstrom doesn't work.
194 */
195 if (error == ERESTART)
196 error = EINTR;
197 return error;
198 }
199
200 if (timeout) {
201 if (*retval) {
202 /*
203 * Compute how much time was left of the timeout,
204 * by subtracting the current time and the time
205 * before we started the call, and subtracting
206 * that result from the user-supplied value.
207 */
208 microtime(&tv1);
209 timersub(&tv1, &tv0, &tv1);
210 timersub(&utv, &tv1, &utv);
211 if (utv.tv_sec < 0)
212 timerclear(&utv);
213 } else {
214 timerclear(&utv);
215 }
216
217 netbsd32_from_timeval(&utv, &utv32);
218
219 if ((error = copyout(&utv32, timeout, sizeof(utv32))))
220 return error;
221 }
222
223 return 0;
224 }
225
226 int
227 linux32_sys_pipe(struct lwp *l, const struct linux32_sys_pipe_args *uap, register_t *retval)
228 {
229 /* {
230 syscallarg(netbsd32_intp) fd;
231 } */
232 int error;
233 int pfds[2];
234
235 if ((error = sys_pipe(l, 0, retval)))
236 return error;
237
238 pfds[0] = (int)retval[0];
239 pfds[1] = (int)retval[1];
240
241 if ((error = copyout(pfds, SCARG_P32(uap, fd), 2 * sizeof (int))) != 0)
242 return error;
243
244 retval[0] = 0;
245 retval[1] = 0;
246
247 return 0;
248 }
249
250
251 int
252 linux32_sys_unlink(struct lwp *l, const struct linux32_sys_unlink_args *uap, register_t *retval)
253 {
254 /* {
255 syscallarg(const netbsd32_charp) path;
256 } */
257 struct linux_sys_unlink_args ua;
258
259 NETBSD32TOP_UAP(path, const char);
260
261 return linux_sys_unlink(l, &ua, retval);
262 }
263
264 int
265 linux32_sys_creat(struct lwp *l, const struct linux32_sys_creat_args *uap, register_t *retval)
266 {
267 /* {
268 syscallarg(const netbsd32_charp) path;
269 syscallarg(int) mode;
270 } */
271 struct sys_open_args ua;
272
273 NETBSD32TOP_UAP(path, const char);
274 SCARG(&ua, flags) = O_CREAT | O_TRUNC | O_WRONLY;
275 NETBSD32TO64_UAP(mode);
276
277 return sys_open(l, &ua, retval);
278 }
279
280 int
281 linux32_sys_mknod(struct lwp *l, const struct linux32_sys_mknod_args *uap, register_t *retval)
282 {
283 /* {
284 syscallarg(const netbsd32_charp) path;
285 syscallarg(int) mode;
286 syscallarg(int) dev;
287 } */
288 struct linux_sys_mknod_args ua;
289
290 NETBSD32TOP_UAP(path, const char);
291 NETBSD32TO64_UAP(mode);
292 NETBSD32TO64_UAP(dev);
293
294 return linux_sys_mknod(l, &ua, retval);
295 }
296
297 int
298 linux32_sys_break(struct lwp *l, const struct linux32_sys_break_args *uap, register_t *retval)
299 {
300 #if 0
301 /* {
302 syscallarg(const netbsd32_charp) nsize;
303 } */
304 #endif
305
306 return ENOSYS;
307 }
308
309 int
310 linux32_sys_swapon(struct lwp *l, const struct linux32_sys_swapon_args *uap, register_t *retval)
311 {
312 /* {
313 syscallarg(const netbsd32_charp) name;
314 } */
315 struct sys_swapctl_args ua;
316
317 SCARG(&ua, cmd) = SWAP_ON;
318 SCARG(&ua, arg) = SCARG_P32(uap, name);
319 SCARG(&ua, misc) = 0; /* priority */
320 return (sys_swapctl(l, &ua, retval));
321 }
322
323 int
324 linux32_sys_swapoff(struct lwp *l, const struct linux32_sys_swapoff_args *uap, register_t *retval)
325 {
326 /* {
327 syscallarg(const netbsd32_charp) path;
328 } */
329 struct sys_swapctl_args ua;
330
331 SCARG(&ua, cmd) = SWAP_OFF;
332 SCARG(&ua, arg) = SCARG_P32(uap, path);
333 SCARG(&ua, misc) = 0; /* priority */
334 return (sys_swapctl(l, &ua, retval));
335 }
336
337
338 int
339 linux32_sys_reboot(struct lwp *l, const struct linux32_sys_reboot_args *uap, register_t *retval)
340 {
341 /* {
342 syscallarg(int) magic1;
343 syscallarg(int) magic2;
344 syscallarg(int) cmd;
345 syscallarg(netbsd32_voidp) arg;
346 } */
347 struct linux_sys_reboot_args ua;
348
349 NETBSD32TO64_UAP(magic1);
350 NETBSD32TO64_UAP(magic2);
351 NETBSD32TO64_UAP(cmd);
352 NETBSD32TOP_UAP(arg, void);
353
354 return linux_sys_reboot(l, &ua, retval);
355 }
356
357 int
358 linux32_sys_setresuid(struct lwp *l, const struct linux32_sys_setresuid_args *uap, register_t *retval)
359 {
360 /* {
361 syscallarg(uid_t) ruid;
362 syscallarg(uid_t) euid;
363 syscallarg(uid_t) suid;
364 } */
365 struct linux_sys_setresuid_args ua;
366
367 NETBSD32TO64_UAP(ruid);
368 NETBSD32TO64_UAP(euid);
369 NETBSD32TO64_UAP(suid);
370
371 return linux_sys_setresuid(l, &ua, retval);
372 }
373
374 int
375 linux32_sys_getresuid(struct lwp *l, const struct linux32_sys_getresuid_args *uap, register_t *retval)
376 {
377 /* {
378 syscallarg(linux32_uidp_t) ruid;
379 syscallarg(linux32_uidp_t) euid;
380 syscallarg(linux32_uidp_t) suid;
381 } */
382 kauth_cred_t pc = l->l_cred;
383 int error;
384 uid_t uid;
385
386 uid = kauth_cred_getuid(pc);
387 if ((error = copyout(&uid, SCARG_P32(uap, ruid), sizeof(uid_t))) != 0)
388 return error;
389
390 uid = kauth_cred_geteuid(pc);
391 if ((error = copyout(&uid, SCARG_P32(uap, euid), sizeof(uid_t))) != 0)
392 return error;
393
394 uid = kauth_cred_getsvuid(pc);
395 return copyout(&uid, SCARG_P32(uap, suid), sizeof(uid_t));
396 }
397
398 int
399 linux32_sys_setresgid(struct lwp *l, const struct linux32_sys_setresgid_args *uap, register_t *retval)
400 {
401 /* {
402 syscallarg(gid_t) rgid;
403 syscallarg(gid_t) egid;
404 syscallarg(gid_t) sgid;
405 } */
406 struct linux_sys_setresgid_args ua;
407
408 NETBSD32TO64_UAP(rgid);
409 NETBSD32TO64_UAP(egid);
410 NETBSD32TO64_UAP(sgid);
411
412 return linux_sys_setresgid(l, &ua, retval);
413 }
414
415 int
416 linux32_sys_getresgid(struct lwp *l, const struct linux32_sys_getresgid_args *uap, register_t *retval)
417 {
418 /* {
419 syscallarg(linux32_gidp_t) rgid;
420 syscallarg(linux32_gidp_t) egid;
421 syscallarg(linux32_gidp_t) sgid;
422 } */
423 kauth_cred_t pc = l->l_cred;
424 int error;
425 gid_t gid;
426
427 gid = kauth_cred_getgid(pc);
428 if ((error = copyout(&gid, SCARG_P32(uap, rgid), sizeof(gid_t))) != 0)
429 return error;
430
431 gid = kauth_cred_getegid(pc);
432 if ((error = copyout(&gid, SCARG_P32(uap, egid), sizeof(gid_t))) != 0)
433 return error;
434
435 gid = kauth_cred_getsvgid(pc);
436 return copyout(&gid, SCARG_P32(uap, sgid), sizeof(gid_t));
437 }
438
439 int
440 linux32_sys_nice(struct lwp *l, const struct linux32_sys_nice_args *uap, register_t *retval)
441 {
442 /* {
443 syscallarg(int) incr;
444 } */
445 struct proc *p = l->l_proc;
446 struct sys_setpriority_args bsa;
447
448 SCARG(&bsa, which) = PRIO_PROCESS;
449 SCARG(&bsa, who) = 0;
450 SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
451
452 return sys_setpriority(l, &bsa, retval);
453 }
454
455 int
456 linux32_sys_alarm(struct lwp *l, const struct linux32_sys_alarm_args *uap, register_t *retval)
457 {
458 /* {
459 syscallarg(unsigned int) secs;
460 } */
461 struct linux_sys_alarm_args ua;
462
463 NETBSD32TO64_UAP(secs);
464
465 return linux_sys_alarm(l, &ua, retval);
466 }
467
468 int
469 linux32_sys_fdatasync(struct lwp *l, const struct linux32_sys_fdatasync_args *uap, register_t *retval)
470 {
471 /* {
472 syscallarg(int) fd;
473 } */
474 struct linux_sys_fdatasync_args ua;
475
476 NETBSD32TO64_UAP(fd);
477
478 return linux_sys_fdatasync(l, &ua, retval);
479 }
480
481 int
482 linux32_sys_setfsuid(struct lwp *l, const struct linux32_sys_setfsuid_args *uap, register_t *retval)
483 {
484 /* {
485 syscallarg(uid_t) uid;
486 } */
487 struct linux_sys_setfsuid_args ua;
488
489 NETBSD32TO64_UAP(uid);
490
491 return linux_sys_setfsuid(l, &ua, retval);
492 }
493
494 int
495 linux32_sys_setfsgid(struct lwp *l, const struct linux32_sys_setfsgid_args *uap, register_t *retval)
496 {
497 /* {
498 syscallarg(gid_t) gid;
499 } */
500 struct linux_sys_setfsgid_args ua;
501
502 NETBSD32TO64_UAP(gid);
503
504 return linux_sys_setfsgid(l, &ua, retval);
505 }
506
507 /*
508 * pread(2).
509 */
510 int
511 linux32_sys_pread(struct lwp *l,
512 const struct linux32_sys_pread_args *uap, register_t *retval)
513 {
514 /* {
515 syscallarg(int) fd;
516 syscallarg(netbsd32_voidp) buf;
517 syscallarg(netbsd32_size_t) nbyte;
518 syscallarg(linux32_off_t) offset;
519 } */
520 struct sys_pread_args pra;
521
522 SCARG(&pra, fd) = SCARG(uap, fd);
523 SCARG(&pra, buf) = SCARG_P32(uap, buf);
524 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
525 SCARG(&pra, offset) = SCARG(uap, offset);
526
527 return sys_pread(l, &pra, retval);
528 }
529
530 /*
531 * pwrite(2).
532 */
533 int
534 linux32_sys_pwrite(struct lwp *l,
535 const struct linux32_sys_pwrite_args *uap, register_t *retval)
536 {
537 /* {
538 syscallarg(int) fd;
539 syscallarg(const netbsd32_voidp) buf;
540 syscallarg(netbsd32_size_t) nbyte;
541 syscallarg(linux32_off_t) offset;
542 } */
543 struct sys_pwrite_args pra;
544
545 SCARG(&pra, fd) = SCARG(uap, fd);
546 SCARG(&pra, buf) = SCARG_P32(uap, buf);
547 SCARG(&pra, nbyte) = SCARG(uap, nbyte);
548 SCARG(&pra, offset) = SCARG(uap, offset);
549
550 return sys_pwrite(l, &pra, retval);
551 }
552
553