linux32_unistd.c revision 1.6.2.2 1 /* $NetBSD: linux32_unistd.c,v 1.6.2.2 2007/05/27 14:35:14 ad 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.6.2.2 2007/05/27 14:35:14 ad 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
50 #include <machine/types.h>
51
52 #include <sys/syscallargs.h>
53
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/netbsd32/netbsd32_conv.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.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/linux_syscallargs.h>
64
65 #include <compat/linux32/common/linux32_types.h>
66 #include <compat/linux32/common/linux32_signal.h>
67 #include <compat/linux32/common/linux32_machdep.h>
68 #include <compat/linux32/common/linux32_sysctl.h>
69 #include <compat/linux32/common/linux32_socketcall.h>
70 #include <compat/linux32/linux32_syscallargs.h>
71
72 static int linux32_select1(struct lwp *, register_t *,
73 int, fd_set *, fd_set *, fd_set *, struct timeval *);
74
75 int
76 linux32_sys_brk(l, v, retval)
77 struct lwp *l;
78 void *v;
79 register_t *retval;
80 {
81 struct linux32_sys_brk_args /* {
82 syscallarg(netbsd32_charp) nsize;
83 } */ *uap = v;
84 struct linux_sys_brk_args ua;
85
86 NETBSD32TOP_UAP(nsize, char);
87 return linux_sys_brk(l, &ua, retval);
88 }
89
90 int
91 linux32_sys_access(l, v, retval)
92 struct lwp *l;
93 void *v;
94 register_t *retval;
95 {
96 struct linux32_sys_access_args /* {
97 syscallarg(const netbsd32_charp) path;
98 syscallarg(int) flags;
99 } */ *uap = v;
100 struct sys_access_args ua;
101
102 NETBSD32TOP_UAP(path, const char);
103 NETBSD32TO64_UAP(flags);
104
105 return sys_access(l, &ua, retval);
106 }
107
108 int
109 linux32_sys_llseek(l, v, retval)
110 struct lwp *l;
111 void *v;
112 register_t *retval;
113 {
114 struct linux32_sys_llseek_args /* {
115 syscallcarg(int) fd;
116 syscallarg(u_int32_t) ohigh;
117 syscallarg(u_int32_t) olow;
118 syscallarg(netbsd32_void *) res;
119 syscallcarg(int) whence;
120 } */ *uap = v;
121 struct linux_sys_llseek_args ua;
122
123 NETBSD32TO64_UAP(fd);
124 NETBSD32TO64_UAP(ohigh);
125 NETBSD32TO64_UAP(olow);
126 NETBSD32TOP_UAP(res, char);
127 NETBSD32TO64_UAP(whence);
128
129 return linux_sys_llseek(l, &ua, retval);
130 }
131
132 int
133 linux32_sys_readlink(l, v, retval)
134 struct lwp *l;
135 void *v;
136 register_t *retval;
137 {
138 struct linux32_sys_readlink_args /* {
139 syscallarg(const netbsd32_charp) name;
140 syscallarg(netbsd32_charp) buf;
141 syscallarg(int) count;
142 } */ *uap = v;
143 struct linux_sys_readlink_args ua;
144
145 NETBSD32TOP_UAP(name, const char);
146 NETBSD32TOP_UAP(buf, char)
147 NETBSD32TO64_UAP(count);
148
149 return linux_sys_readlink(l, &ua, retval);
150 }
151
152
153 int
154 linux32_sys_select(l, v, retval)
155 struct lwp *l;
156 void *v;
157 register_t *retval;
158 {
159 struct linux32_sys_select_args /* {
160 syscallarg(int) nfds;
161 syscallarg(netbsd32_fd_setp_t) readfds;
162 syscallarg(netbsd32_fd_setp_t) writefds;
163 syscallarg(netbsd32_fd_setp_t) exceptfds;
164 syscallarg(netbsd32_timevalp_t) timeout;
165 } */ *uap = v;
166
167 return linux32_select1(l, retval, SCARG(uap, nfds),
168 SCARG_P32(uap, readfds),
169 SCARG_P32(uap, writefds),
170 SCARG_P32(uap, exceptfds),
171 SCARG_P32(uap, timeout));
172 }
173
174 int
175 linux32_sys_oldselect(l, v, retval)
176 struct lwp *l;
177 void *v;
178 register_t *retval;
179 {
180 struct linux32_sys_oldselect_args /* {
181 syscallarg(linux32_oldselectp_t) lsp;
182 } */ *uap = v;
183 struct linux32_oldselect lsp32;
184 int error;
185
186 if ((error = copyin(SCARG_P32(uap, lsp), &lsp32, sizeof(lsp32))) != 0)
187 return error;
188
189 return linux32_select1(l, retval, lsp32.nfds,
190 NETBSD32PTR64(lsp32.readfds), NETBSD32PTR64(lsp32.writefds),
191 NETBSD32PTR64(lsp32.exceptfds), NETBSD32PTR64(lsp32.timeout));
192 }
193
194 static int
195 linux32_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
196 struct lwp *l;
197 register_t *retval;
198 int nfds;
199 fd_set *readfds, *writefds, *exceptfds;
200 struct timeval *timeout;
201 {
202 struct timeval tv0, tv1, utv, otv, *tv = NULL;
203 struct netbsd32_timeval utv32;
204 int error;
205
206 timerclear(&otv); /* XXX GCC4 */
207
208 /*
209 * Store current time for computation of the amount of
210 * time left.
211 */
212 if (timeout) {
213 if ((error = copyin(timeout, &utv32, sizeof(utv32))))
214 return error;
215
216 netbsd32_to_timeval(&utv32, &utv);
217 otv = utv;
218
219 if (itimerfix(&utv)) {
220 /*
221 * The timeval was invalid. Convert it to something
222 * valid that will act as it does under Linux.
223 */
224 utv.tv_sec += utv.tv_usec / 1000000;
225 utv.tv_usec %= 1000000;
226 if (utv.tv_usec < 0) {
227 utv.tv_sec -= 1;
228 utv.tv_usec += 1000000;
229 }
230 if (utv.tv_sec < 0)
231 timerclear(&utv);
232 }
233 microtime(&tv0);
234 tv = &utv;
235 }
236
237 error = selcommon(l, retval, nfds,
238 readfds, writefds, exceptfds, tv, NULL);
239
240 if (error) {
241 /*
242 * See fs/select.c in the Linux kernel. Without this,
243 * Maelstrom doesn't work.
244 */
245 if (error == ERESTART)
246 error = EINTR;
247 return error;
248 }
249
250 if (timeout) {
251 if (*retval) {
252 /*
253 * Compute how much time was left of the timeout,
254 * by subtracting the current time and the time
255 * before we started the call, and subtracting
256 * that result from the user-supplied value.
257 */
258 microtime(&tv1);
259 timersub(&tv1, &tv0, &tv1);
260 timersub(&otv, &tv1, &utv);
261 if (utv.tv_sec < 0)
262 timerclear(&utv);
263 } else {
264 timerclear(&utv);
265 }
266
267 netbsd32_from_timeval(&utv, &utv32);
268
269 if ((error = copyout(&utv32, timeout, sizeof(utv32))))
270 return error;
271 }
272
273 return 0;
274 }
275
276 int
277 linux32_sys_pipe(l, v, retval)
278 struct lwp *l;
279 void *v;
280 register_t *retval;
281 {
282 struct linux32_sys_pipe_args /* {
283 syscallarg(netbsd32_intp) fd;
284 } */ *uap = v;
285 int error;
286 int pfds[2];
287
288 if ((error = sys_pipe(l, 0, retval)))
289 return error;
290
291 pfds[0] = (int)retval[0];
292 pfds[1] = (int)retval[1];
293
294 if ((error = copyout(pfds, SCARG_P32(uap, fd), 2 * sizeof (int))) != 0)
295 return error;
296
297 retval[0] = 0;
298 retval[1] = 0;
299
300 return 0;
301 }
302
303
304 int
305 linux32_sys_unlink(l, v, retval)
306 struct lwp *l;
307 void *v;
308 register_t *retval;
309 {
310 struct linux32_sys_unlink_args /* {
311 syscallarg(const netbsd32_charp) path;
312 } */ *uap = v;
313 struct linux_sys_unlink_args ua;
314
315 NETBSD32TOP_UAP(path, const char);
316
317 return linux_sys_unlink(l, &ua, retval);
318 }
319
320 int
321 linux32_sys_chdir(l, v, retval)
322 struct lwp *l;
323 void *v;
324 register_t *retval;
325 {
326 struct linux32_sys_chdir_args /* {
327 syscallarg(const netbsd32_charp) path;
328 } */ *uap = v;
329 struct sys_chdir_args ua;
330
331 NETBSD32TOP_UAP(path, const char);
332
333 return sys_chdir(l, &ua, retval);
334 }
335
336 int
337 linux32_sys_link(l, v, retval)
338 struct lwp *l;
339 void *v;
340 register_t *retval;
341 {
342 struct linux32_sys_link_args /* {
343 syscallarg(const netbsd32_charp) path;
344 syscallarg(const netbsd32_charp) link;
345 } */ *uap = v;
346 struct sys_link_args ua;
347
348 NETBSD32TOP_UAP(path, const char);
349 NETBSD32TOP_UAP(link, const char);
350
351 return sys_link(l, &ua, retval);
352 }
353
354 int
355 linux32_sys_creat(l, v, retval)
356 struct lwp *l;
357 void *v;
358 register_t *retval;
359 {
360 struct linux32_sys_creat_args /* {
361 syscallarg(const netbsd32_charp) path;
362 syscallarg(int) mode;
363 } */ *uap = v;
364 struct sys_open_args ua;
365
366 NETBSD32TOP_UAP(path, const char);
367 SCARG(&ua, flags) = O_CREAT | O_TRUNC | O_WRONLY;
368 NETBSD32TO64_UAP(mode);
369
370 return sys_open(l, &ua, retval);
371 }
372
373 int
374 linux32_sys_mknod(l, v, retval)
375 struct lwp *l;
376 void *v;
377 register_t *retval;
378 {
379 struct linux32_sys_mknod_args /* {
380 syscallarg(const netbsd32_charp) path;
381 syscallarg(int) mode;
382 syscallarg(int) dev;
383 } */ *uap = v;
384 struct linux_sys_mknod_args ua;
385
386 NETBSD32TOP_UAP(path, const char);
387 NETBSD32TO64_UAP(mode);
388 NETBSD32TO64_UAP(dev);
389
390 return linux_sys_mknod(l, &ua, retval);
391 }
392
393 int
394 linux32_sys_chmod(l, v, retval)
395 struct lwp *l;
396 void *v;
397 register_t *retval;
398 {
399 struct linux32_sys_chmod_args /* {
400 syscallarg(const netbsd32_charp) path;
401 syscallarg(int) mode;
402 } */ *uap = v;
403 struct sys_chmod_args ua;
404
405 NETBSD32TOP_UAP(path, const char);
406 NETBSD32TO64_UAP(mode);
407
408 return sys_chmod(l, &ua, retval);
409 }
410
411 int
412 linux32_sys_lchown16(l, v, retval)
413 struct lwp *l;
414 void *v;
415 register_t *retval;
416 {
417 struct linux32_sys_lchown16_args /* {
418 syscallarg(const netbsd32_charp) path;
419 syscallarg(int) uid;
420 syscallarg(int) gid;
421 } */ *uap = v;
422 struct sys___posix_lchown_args ua;
423
424 NETBSD32TOP_UAP(path, const char);
425
426 if ((linux32_uid_t)SCARG(uap, uid) == (linux32_uid_t)-1)
427 SCARG(&ua, uid) = (uid_t)-1;
428 else
429 SCARG(&ua, uid) = SCARG(uap, uid);
430
431 if ((linux32_gid_t)SCARG(uap, gid) == (linux32_gid_t)-1)
432 SCARG(&ua, gid) = (gid_t)-1;
433 else
434 SCARG(&ua, gid) = SCARG(uap, gid);
435
436 return sys___posix_lchown(l, &ua, retval);
437 }
438
439 int
440 linux32_sys_break(l, v, retval)
441 struct lwp *l;
442 void *v;
443 register_t *retval;
444 {
445 #if 0
446 struct linux32_sys_break_args /* {
447 syscallarg(const netbsd32_charp) nsize;
448 } */ *uap = v;
449 #endif
450
451 return ENOSYS;
452 }
453
454 int
455 linux32_sys_rename(l, v, retval)
456 struct lwp *l;
457 void *v;
458 register_t *retval;
459 {
460 struct linux32_sys_rename_args /* {
461 syscallarg(const netbsd32_charp) from;
462 syscallarg(const netbsd32_charp) to;
463 } */ *uap = v;
464 struct sys_rename_args ua;
465
466 NETBSD32TOP_UAP(from, const char);
467 NETBSD32TOP_UAP(to, const char);
468
469 return sys___posix_rename(l, &ua, retval);
470 }
471
472 int
473 linux32_sys_mkdir(l, v, retval)
474 struct lwp *l;
475 void *v;
476 register_t *retval;
477 {
478 struct linux32_sys_mkdir_args /* {
479 syscallarg(const netbsd32_charp) path;
480 syscallarg(int) mode;
481 } */ *uap = v;
482 struct sys_mkdir_args ua;
483
484 NETBSD32TOP_UAP(path, const char);
485 NETBSD32TO64_UAP(mode);
486
487 return sys_mkdir(l, &ua, retval);
488 }
489
490 int
491 linux32_sys_rmdir(l, v, retval)
492 struct lwp *l;
493 void *v;
494 register_t *retval;
495 {
496 struct linux32_sys_rmdir_args /* {
497 syscallarg(const netbsd32_charp) path;
498 } */ *uap = v;
499 struct sys_rmdir_args ua;
500
501 NETBSD32TOP_UAP(path, const char);
502
503 return sys_rmdir(l, &ua, retval);
504 }
505
506 int
507 linux32_sys_getgroups16(l, v, retval)
508 struct lwp *l;
509 void *v;
510 register_t *retval;
511 {
512 struct linux32_sys_getgroups16_args /* {
513 syscallarg(int) gidsetsize;
514 syscallarg(linux32_gidp_t) gidset;
515 } */ *uap = v;
516 struct linux_sys_getgroups16_args ua;
517
518 NETBSD32TO64_UAP(gidsetsize);
519 NETBSD32TOP_UAP(gidset, linux_gid_t);
520
521 return linux_sys_getgroups16(l, &ua, retval);
522 }
523
524 int
525 linux32_sys_setgroups16(l, v, retval)
526 struct lwp *l;
527 void *v;
528 register_t *retval;
529 {
530 struct linux32_sys_setgroups16_args /* {
531 syscallarg(int) gidsetsize;
532 syscallarg(linux32_gidp_t) gidset;
533 } */ *uap = v;
534 struct linux_sys_setgroups16_args ua;
535
536 NETBSD32TO64_UAP(gidsetsize);
537 NETBSD32TOP_UAP(gidset, linux_gid_t);
538
539 return linux_sys_setgroups16(l, &ua, retval);
540 }
541
542 int
543 linux32_sys_symlink(l, v, retval)
544 struct lwp *l;
545 void *v;
546 register_t *retval;
547 {
548 struct linux32_sys_symlink_args /* {
549 syscallarg(const netbsd32_charp) path;
550 syscallarg(const netbsd32_charp) link;
551 } */ *uap = v;
552 struct sys_symlink_args ua;
553
554 NETBSD32TOP_UAP(path, const char);
555 NETBSD32TOP_UAP(link, const char);
556
557 return sys_symlink(l, &ua, retval);
558 }
559
560
561 int
562 linux32_sys_swapon(l, v, retval)
563 struct lwp *l;
564 void *v;
565 register_t *retval;
566 {
567 struct linux32_sys_swapon_args /* {
568 syscallarg(const netbsd32_charp) name;
569 } */ *uap = v;
570 struct sys_swapctl_args ua;
571
572 SCARG(&ua, cmd) = SWAP_ON;
573 SCARG(&ua, arg) = SCARG_P32(uap, name);
574 SCARG(&ua, misc) = 0; /* priority */
575 return (sys_swapctl(l, &ua, retval));
576 }
577
578 int
579 linux32_sys_swapoff(l, v, retval)
580 struct lwp *l;
581 void *v;
582 register_t *retval;
583 {
584 struct linux32_sys_swapoff_args /* {
585 syscallarg(const netbsd32_charp) path;
586 } */ *uap = v;
587 struct sys_swapctl_args ua;
588
589 SCARG(&ua, cmd) = SWAP_OFF;
590 SCARG(&ua, arg) = SCARG_P32(uap, path);
591 SCARG(&ua, misc) = 0; /* priority */
592 return (sys_swapctl(l, &ua, retval));
593 }
594
595
596 int
597 linux32_sys_reboot(l, v, retval)
598 struct lwp *l;
599 void *v;
600 register_t *retval;
601 {
602 struct linux32_sys_reboot_args /* {
603 syscallarg(int) magic1;
604 syscallarg(int) magic2;
605 syscallarg(int) cmd;
606 syscallarg(netbsd32_voidp) arg;
607 } */ *uap = v;
608 struct linux_sys_reboot_args ua;
609
610 NETBSD32TO64_UAP(magic1);
611 NETBSD32TO64_UAP(magic2);
612 NETBSD32TO64_UAP(cmd);
613 NETBSD32TOP_UAP(arg, void);
614
615 return linux_sys_reboot(l, &ua, retval);
616 }
617
618 int
619 linux32_sys_truncate(l, v, retval)
620 struct lwp *l;
621 void *v;
622 register_t *retval;
623 {
624 struct linux32_sys_truncate_args /* {
625 syscallarg(const netbsd32_charp) path;
626 syscallarg(netbsd32_charp) buf;
627 syscallarg(int) count;
628 } */ *uap = v;
629 struct compat_43_sys_truncate_args ua;
630
631 NETBSD32TOP_UAP(path, const char);
632 NETBSD32TO64_UAP(length);
633
634 return compat_43_sys_truncate(l, &ua, retval);
635 }
636
637 int
638 linux32_sys_fchown16(l, v, retval)
639 struct lwp *l;
640 void *v;
641 register_t *retval;
642 {
643 struct linux32_sys_fchown16_args /* {
644 syscallarg(int) fd;
645 syscallarg(int) uid;
646 syscallarg(int) gid;
647 } */ *uap = v;
648 struct sys___posix_fchown_args ua;
649
650 SCARG(&ua, fd) = SCARG(uap, fd);
651
652 if ((linux32_uid_t)SCARG(uap, uid) == (linux32_uid_t)-1)
653 SCARG(&ua, uid) = (uid_t)-1;
654 else
655 SCARG(&ua, uid) = SCARG(uap, uid);
656
657 if ((linux32_gid_t)SCARG(uap, gid) == (linux32_gid_t)-1)
658 SCARG(&ua, gid) = (gid_t)-1;
659 else
660 SCARG(&ua, gid) = SCARG(uap, gid);
661
662 return sys___posix_fchown(l, &ua, retval);
663 }
664
665 int
666 linux32_sys_setresuid(l, v, retval)
667 struct lwp *l;
668 void *v;
669 register_t *retval;
670 {
671 struct linux32_sys_setresuid_args /* {
672 syscallarg(uid_t) ruid;
673 syscallarg(uid_t) euid;
674 syscallarg(uid_t) suid;
675 } */ *uap = v;
676 struct linux_sys_setresuid_args ua;
677
678 SCARG(&ua, ruid) = (SCARG(uap, ruid) == -1) ? -1 : SCARG(uap, ruid);
679 SCARG(&ua, euid) = (SCARG(uap, euid) == -1) ? -1 : SCARG(uap, euid);
680 SCARG(&ua, suid) = (SCARG(uap, suid) == -1) ? -1 : SCARG(uap, suid);
681
682 return linux_sys_setresuid(l, &ua, retval);
683 }
684
685 int
686 linux32_sys_setresgid(l, v, retval)
687 struct lwp *l;
688 void *v;
689 register_t *retval;
690 {
691 struct linux32_sys_setresgid_args /* {
692 syscallarg(gid_t) rgid;
693 syscallarg(gid_t) egid;
694 syscallarg(gid_t) sgid;
695 } */ *uap = v;
696 struct linux_sys_setresgid_args ua;
697
698 SCARG(&ua, rgid) = (SCARG(uap, rgid) == -1) ? -1 : SCARG(uap, rgid);
699 SCARG(&ua, egid) = (SCARG(uap, egid) == -1) ? -1 : SCARG(uap, egid);
700 SCARG(&ua, sgid) = (SCARG(uap, sgid) == -1) ? -1 : SCARG(uap, sgid);
701
702 return linux_sys_setresgid(l, &ua, retval);
703 }
704