1 /* $NetBSD: linux32_unistd.c,v 1.13 2007/11/07 00:24:31 njoly Exp $ */
4 * Copyright (c) 2006 Emmanuel Dreyfus, all rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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.
34 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: linux32_unistd.c,v 1.13 2007/11/07 00:24:31 njoly Exp $");
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>
47 #include <sys/ucred.h>
50 #include <machine/types.h>
52 #include <sys/syscallargs.h>
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/netbsd32/netbsd32_conv.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
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>
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>
72 static int linux32_select1(struct lwp
*, register_t
*,
73 int, fd_set
*, fd_set
*, fd_set
*, struct timeval
*);
76 linux32_sys_brk(l
, v
, retval
)
81 struct linux32_sys_brk_args
/* {
82 syscallarg(netbsd32_charp) nsize;
84 struct linux_sys_brk_args ua
;
86 NETBSD32TOP_UAP(nsize
, char);
87 return linux_sys_brk(l
, &ua
, retval
);
91 linux32_sys_llseek(l
, v
, retval
)
96 struct linux32_sys_llseek_args
/* {
98 syscallarg(u_int32_t) ohigh;
99 syscallarg(u_int32_t) olow;
100 syscallarg(netbsd32_void *) res;
101 syscallcarg(int) whence;
103 struct linux_sys_llseek_args ua
;
105 NETBSD32TO64_UAP(fd
);
106 NETBSD32TO64_UAP(ohigh
);
107 NETBSD32TO64_UAP(olow
);
108 NETBSD32TOP_UAP(res
, char);
109 NETBSD32TO64_UAP(whence
);
111 return linux_sys_llseek(l
, &ua
, retval
);
115 linux32_sys_select(l
, v
, retval
)
120 struct linux32_sys_select_args
/* {
121 syscallarg(int) nfds;
122 syscallarg(netbsd32_fd_setp_t) readfds;
123 syscallarg(netbsd32_fd_setp_t) writefds;
124 syscallarg(netbsd32_fd_setp_t) exceptfds;
125 syscallarg(netbsd32_timevalp_t) timeout;
128 return linux32_select1(l
, retval
, SCARG(uap
, nfds
),
129 SCARG_P32(uap
, readfds
),
130 SCARG_P32(uap
, writefds
),
131 SCARG_P32(uap
, exceptfds
),
132 SCARG_P32(uap
, timeout
));
136 linux32_sys_oldselect(l
, v
, retval
)
141 struct linux32_sys_oldselect_args
/* {
142 syscallarg(linux32_oldselectp_t) lsp;
144 struct linux32_oldselect lsp32
;
147 if ((error
= copyin(SCARG_P32(uap
, lsp
), &lsp32
, sizeof(lsp32
))) != 0)
150 return linux32_select1(l
, retval
, lsp32
.nfds
,
151 NETBSD32PTR64(lsp32
.readfds
), NETBSD32PTR64(lsp32
.writefds
),
152 NETBSD32PTR64(lsp32
.exceptfds
), NETBSD32PTR64(lsp32
.timeout
));
156 linux32_select1(l
, retval
, nfds
, readfds
, writefds
, exceptfds
, timeout
)
160 fd_set
*readfds
, *writefds
, *exceptfds
;
161 struct timeval
*timeout
;
163 struct timeval tv0
, tv1
, utv
, *tv
= NULL
;
164 struct netbsd32_timeval utv32
;
167 timerclear(&utv
); /* XXX GCC4 */
170 * Store current time for computation of the amount of
174 if ((error
= copyin(timeout
, &utv32
, sizeof(utv32
))))
177 netbsd32_to_timeval(&utv32
, &utv
);
179 if (itimerfix(&utv
)) {
181 * The timeval was invalid. Convert it to something
182 * valid that will act as it does under Linux.
184 utv
.tv_sec
+= utv
.tv_usec
/ 1000000;
185 utv
.tv_usec
%= 1000000;
186 if (utv
.tv_usec
< 0) {
188 utv
.tv_usec
+= 1000000;
197 error
= selcommon(l
, retval
, nfds
,
198 readfds
, writefds
, exceptfds
, tv
, NULL
);
202 * See fs/select.c in the Linux kernel. Without this,
203 * Maelstrom doesn't work.
205 if (error
== ERESTART
)
213 * Compute how much time was left of the timeout,
214 * by subtracting the current time and the time
215 * before we started the call, and subtracting
216 * that result from the user-supplied value.
219 timersub(&tv1
, &tv0
, &tv1
);
220 timersub(&utv
, &tv1
, &utv
);
227 netbsd32_from_timeval(&utv
, &utv32
);
229 if ((error
= copyout(&utv32
, timeout
, sizeof(utv32
))))
237 linux32_sys_pipe(l
, v
, retval
)
242 struct linux32_sys_pipe_args
/* {
243 syscallarg(netbsd32_intp) fd;
248 if ((error
= sys_pipe(l
, 0, retval
)))
251 pfds
[0] = (int)retval
[0];
252 pfds
[1] = (int)retval
[1];
254 if ((error
= copyout(pfds
, SCARG_P32(uap
, fd
), 2 * sizeof (int))) != 0)
265 linux32_sys_unlink(l
, v
, retval
)
270 struct linux32_sys_unlink_args
/* {
271 syscallarg(const netbsd32_charp) path;
273 struct linux_sys_unlink_args ua
;
275 NETBSD32TOP_UAP(path
, const char);
277 return linux_sys_unlink(l
, &ua
, retval
);
281 linux32_sys_creat(l
, v
, retval
)
286 struct linux32_sys_creat_args
/* {
287 syscallarg(const netbsd32_charp) path;
288 syscallarg(int) mode;
290 struct sys_open_args ua
;
292 NETBSD32TOP_UAP(path
, const char);
293 SCARG(&ua
, flags
) = O_CREAT
| O_TRUNC
| O_WRONLY
;
294 NETBSD32TO64_UAP(mode
);
296 return sys_open(l
, &ua
, retval
);
300 linux32_sys_mknod(l
, v
, retval
)
305 struct linux32_sys_mknod_args
/* {
306 syscallarg(const netbsd32_charp) path;
307 syscallarg(int) mode;
310 struct linux_sys_mknod_args ua
;
312 NETBSD32TOP_UAP(path
, const char);
313 NETBSD32TO64_UAP(mode
);
314 NETBSD32TO64_UAP(dev
);
316 return linux_sys_mknod(l
, &ua
, retval
);
320 linux32_sys_chown16(l
, v
, retval
)
325 struct linux32_sys_chown16_args
/* {
326 syscallarg(const netbsd32_charp) path;
330 struct sys___posix_chown_args ua
;
332 NETBSD32TOP_UAP(path
, const char);
334 if ((linux32_uid_t
)SCARG(uap
, uid
) == (linux32_uid_t
)-1)
335 SCARG(&ua
, uid
) = (uid_t
)-1;
337 SCARG(&ua
, uid
) = SCARG(uap
, uid
);
339 if ((linux32_gid_t
)SCARG(uap
, gid
) == (linux32_gid_t
)-1)
340 SCARG(&ua
, gid
) = (gid_t
)-1;
342 SCARG(&ua
, gid
) = SCARG(uap
, gid
);
344 return sys___posix_chown(l
, &ua
, retval
);
348 linux32_sys_lchown16(l
, v
, retval
)
353 struct linux32_sys_lchown16_args
/* {
354 syscallarg(const netbsd32_charp) path;
358 struct sys___posix_lchown_args ua
;
360 NETBSD32TOP_UAP(path
, const char);
362 if ((linux32_uid_t
)SCARG(uap
, uid
) == (linux32_uid_t
)-1)
363 SCARG(&ua
, uid
) = (uid_t
)-1;
365 SCARG(&ua
, uid
) = SCARG(uap
, uid
);
367 if ((linux32_gid_t
)SCARG(uap
, gid
) == (linux32_gid_t
)-1)
368 SCARG(&ua
, gid
) = (gid_t
)-1;
370 SCARG(&ua
, gid
) = SCARG(uap
, gid
);
372 return sys___posix_lchown(l
, &ua
, retval
);
376 linux32_sys_break(l
, v
, retval
)
382 struct linux32_sys_break_args
/* {
383 syscallarg(const netbsd32_charp) nsize;
391 linux32_sys_rename(l
, v
, retval
)
396 struct linux32_sys_rename_args
/* {
397 syscallarg(const netbsd32_charp) from;
398 syscallarg(const netbsd32_charp) to;
400 struct sys_rename_args ua
;
402 NETBSD32TOP_UAP(from
, const char);
403 NETBSD32TOP_UAP(to
, const char);
405 return sys___posix_rename(l
, &ua
, retval
);
409 linux32_sys_getgroups16(l
, v
, retval
)
414 struct linux32_sys_getgroups16_args
/* {
415 syscallarg(int) gidsetsize;
416 syscallarg(linux32_gidp_t) gidset;
418 struct linux_sys_getgroups16_args ua
;
420 NETBSD32TO64_UAP(gidsetsize
);
421 NETBSD32TOP_UAP(gidset
, linux_gid_t
);
423 return linux_sys_getgroups16(l
, &ua
, retval
);
427 linux32_sys_setgroups16(l
, v
, retval
)
432 struct linux32_sys_setgroups16_args
/* {
433 syscallarg(int) gidsetsize;
434 syscallarg(linux32_gidp_t) gidset;
436 struct linux_sys_setgroups16_args ua
;
438 NETBSD32TO64_UAP(gidsetsize
);
439 NETBSD32TOP_UAP(gidset
, linux_gid_t
);
441 return linux_sys_setgroups16(l
, &ua
, retval
);
445 linux32_sys_swapon(l
, v
, retval
)
450 struct linux32_sys_swapon_args
/* {
451 syscallarg(const netbsd32_charp) name;
453 struct sys_swapctl_args ua
;
455 SCARG(&ua
, cmd
) = SWAP_ON
;
456 SCARG(&ua
, arg
) = SCARG_P32(uap
, name
);
457 SCARG(&ua
, misc
) = 0; /* priority */
458 return (sys_swapctl(l
, &ua
, retval
));
462 linux32_sys_swapoff(l
, v
, retval
)
467 struct linux32_sys_swapoff_args
/* {
468 syscallarg(const netbsd32_charp) path;
470 struct sys_swapctl_args ua
;
472 SCARG(&ua
, cmd
) = SWAP_OFF
;
473 SCARG(&ua
, arg
) = SCARG_P32(uap
, path
);
474 SCARG(&ua
, misc
) = 0; /* priority */
475 return (sys_swapctl(l
, &ua
, retval
));
480 linux32_sys_reboot(l
, v
, retval
)
485 struct linux32_sys_reboot_args
/* {
486 syscallarg(int) magic1;
487 syscallarg(int) magic2;
489 syscallarg(netbsd32_voidp) arg;
491 struct linux_sys_reboot_args ua
;
493 NETBSD32TO64_UAP(magic1
);
494 NETBSD32TO64_UAP(magic2
);
495 NETBSD32TO64_UAP(cmd
);
496 NETBSD32TOP_UAP(arg
, void);
498 return linux_sys_reboot(l
, &ua
, retval
);
502 linux32_sys_truncate(l
, v
, retval
)
507 struct linux32_sys_truncate_args
/* {
508 syscallarg(const netbsd32_charp) path;
509 syscallarg(netbsd32_charp) buf;
510 syscallarg(int) count;
512 struct compat_43_sys_truncate_args ua
;
514 NETBSD32TOP_UAP(path
, const char);
515 NETBSD32TO64_UAP(length
);
517 return compat_43_sys_truncate(l
, &ua
, retval
);
521 linux32_sys_fchown16(l
, v
, retval
)
526 struct linux32_sys_fchown16_args
/* {
531 struct sys___posix_fchown_args ua
;
533 SCARG(&ua
, fd
) = SCARG(uap
, fd
);
535 if ((linux32_uid_t
)SCARG(uap
, uid
) == (linux32_uid_t
)-1)
536 SCARG(&ua
, uid
) = (uid_t
)-1;
538 SCARG(&ua
, uid
) = SCARG(uap
, uid
);
540 if ((linux32_gid_t
)SCARG(uap
, gid
) == (linux32_gid_t
)-1)
541 SCARG(&ua
, gid
) = (gid_t
)-1;
543 SCARG(&ua
, gid
) = SCARG(uap
, gid
);
545 return sys___posix_fchown(l
, &ua
, retval
);
549 linux32_sys_setresuid(l
, v
, retval
)
554 struct linux32_sys_setresuid_args
/* {
555 syscallarg(uid_t) ruid;
556 syscallarg(uid_t) euid;
557 syscallarg(uid_t) suid;
559 struct linux_sys_setresuid_args ua
;
561 SCARG(&ua
, ruid
) = (SCARG(uap
, ruid
) == -1) ? -1 : SCARG(uap
, ruid
);
562 SCARG(&ua
, euid
) = (SCARG(uap
, euid
) == -1) ? -1 : SCARG(uap
, euid
);
563 SCARG(&ua
, suid
) = (SCARG(uap
, suid
) == -1) ? -1 : SCARG(uap
, suid
);
565 return linux_sys_setresuid(l
, &ua
, retval
);
569 linux32_sys_setresgid(l
, v
, retval
)
574 struct linux32_sys_setresgid_args
/* {
575 syscallarg(gid_t) rgid;
576 syscallarg(gid_t) egid;
577 syscallarg(gid_t) sgid;
579 struct linux_sys_setresgid_args ua
;
581 SCARG(&ua
, rgid
) = (SCARG(uap
, rgid
) == -1) ? -1 : SCARG(uap
, rgid
);
582 SCARG(&ua
, egid
) = (SCARG(uap
, egid
) == -1) ? -1 : SCARG(uap
, egid
);
583 SCARG(&ua
, sgid
) = (SCARG(uap
, sgid
) == -1) ? -1 : SCARG(uap
, sgid
);
585 return linux_sys_setresgid(l
, &ua
, retval
);
589 linux32_sys_nice(l
, v
, retval
)
594 struct linux32_sys_nice_args
/* {
595 syscallarg(int) incr;
597 struct netbsd32_setpriority_args bsa
;
599 SCARG(&bsa
, which
) = PRIO_PROCESS
;
600 SCARG(&bsa
, who
) = 0;
601 SCARG(&bsa
, prio
) = SCARG(uap
, incr
);
603 return netbsd32_setpriority(l
, &bsa
, retval
);
607 linux32_sys_alarm(l
, v
, retval
)
612 struct linux32_sys_alarm_args
/* {
613 syscallarg(unsigned int) secs;
615 struct linux_sys_alarm_args ua
;
617 NETBSD32TO64_UAP(secs
);
619 return linux_sys_alarm(l
, &ua
, retval
);
623 linux32_sys_fdatasync(l
, v
, retval
)
628 struct linux32_sys_fdatasync_args
/* {
631 struct linux_sys_fdatasync_args ua
;
633 NETBSD32TO64_UAP(fd
);
635 return linux_sys_fdatasync(l
, &ua
, retval
);
639 linux32_sys_setfsuid(l
, v
, retval
)
644 struct linux32_sys_setfsuid_args
/* {
645 syscallarg(uid_t) uid;
647 struct linux_sys_setfsuid_args ua
;
649 NETBSD32TO64_UAP(uid
);
651 return linux_sys_setfsuid(l
, &ua
, retval
);
655 linux32_sys_setreuid16(l
, v
, retval
)
660 struct linux32_sys_setreuid16_args
/* {
661 syscallarg(int) ruid;
662 syscallarg(int) euid;
664 struct sys_setreuid_args bsa
;
666 if ((linux32_uid_t
)SCARG(uap
, ruid
) == (linux32_uid_t
)-1)
667 SCARG(&bsa
, ruid
) = (uid_t
)-1;
669 SCARG(&bsa
, ruid
) = SCARG(uap
, ruid
);
670 if ((linux32_uid_t
)SCARG(uap
, euid
) == (linux32_uid_t
)-1)
671 SCARG(&bsa
, euid
) = (uid_t
)-1;
673 SCARG(&bsa
, euid
) = SCARG(uap
, euid
);
675 return sys_setreuid(l
, &bsa
, retval
);
679 linux32_sys_setregid16(l
, v
, retval
)
684 struct linux32_sys_setregid16_args
/* {
685 syscallarg(int) rgid;
686 syscallarg(int) egid;
688 struct sys_setregid_args bsa
;
690 if ((linux32_gid_t
)SCARG(uap
, rgid
) == (linux32_gid_t
)-1)
691 SCARG(&bsa
, rgid
) = (gid_t
)-1;
693 SCARG(&bsa
, rgid
) = SCARG(uap
, rgid
);
694 if ((linux32_gid_t
)SCARG(uap
, egid
) == (linux32_gid_t
)-1)
695 SCARG(&bsa
, egid
) = (gid_t
)-1;
697 SCARG(&bsa
, egid
) = SCARG(uap
, egid
);
699 return sys_setregid(l
, &bsa
, retval
);
703 linux32_sys_setresuid16(l
, v
, retval
)
708 struct linux32_sys_setresuid16_args
/* {
709 syscallarg(uid_t) ruid;
710 syscallarg(uid_t) euid;
711 syscallarg(uid_t) suid;
713 struct linux32_sys_setresuid_args lsa
;
715 if ((linux32_uid_t
)SCARG(uap
, ruid
) == (linux32_uid_t
)-1)
716 SCARG(&lsa
, ruid
) = (uid_t
)-1;
718 SCARG(&lsa
, ruid
) = SCARG(uap
, ruid
);
719 if ((linux32_uid_t
)SCARG(uap
, euid
) == (linux32_uid_t
)-1)
720 SCARG(&lsa
, euid
) = (uid_t
)-1;
722 SCARG(&lsa
, euid
) = SCARG(uap
, euid
);
723 if ((linux32_uid_t
)SCARG(uap
, suid
) == (linux32_uid_t
)-1)
724 SCARG(&lsa
, suid
) = (uid_t
)-1;
726 SCARG(&lsa
, suid
) = SCARG(uap
, euid
);
728 return linux32_sys_setresuid(l
, &lsa
, retval
);
732 linux32_sys_setresgid16(l
, v
, retval
)
737 struct linux32_sys_setresgid16_args
/* {
738 syscallarg(gid_t) rgid;
739 syscallarg(gid_t) egid;
740 syscallarg(gid_t) sgid;
742 struct linux32_sys_setresgid_args lsa
;
744 if ((linux32_gid_t
)SCARG(uap
, rgid
) == (linux32_gid_t
)-1)
745 SCARG(&lsa
, rgid
) = (gid_t
)-1;
747 SCARG(&lsa
, rgid
) = SCARG(uap
, rgid
);
748 if ((linux32_gid_t
)SCARG(uap
, egid
) == (linux32_gid_t
)-1)
749 SCARG(&lsa
, egid
) = (gid_t
)-1;
751 SCARG(&lsa
, egid
) = SCARG(uap
, egid
);
752 if ((linux32_gid_t
)SCARG(uap
, sgid
) == (linux32_gid_t
)-1)
753 SCARG(&lsa
, sgid
) = (gid_t
)-1;
755 SCARG(&lsa
, sgid
) = SCARG(uap
, sgid
);
757 return linux32_sys_setresgid(l
, &lsa
, retval
);