Home | History | Annotate | Line # | Download | only in common
linux32_unistd.c revision 1.32
      1 /*	$NetBSD: linux32_unistd.c,v 1.32 2009/03/29 19:21:19 christos 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.32 2009/03/29 19:21:19 christos 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_timeval50p_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(struct lwp *l, register_t *retval, int nfds,
    147 		fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
    148 		struct timeval *timeout)
    149 {
    150 	struct timespec ts0, ts1, uts, *ts = NULL;
    151 	struct netbsd32_timeval50 utv32;
    152 	int error;
    153 
    154 
    155 	/*
    156 	 * Store current time for computation of the amount of
    157 	 * time left.
    158 	 */
    159 	if (timeout) {
    160 		if ((error = copyin(timeout, &utv32, sizeof(utv32))))
    161 			return error;
    162 
    163 		uts.tv_sec = utv32.tv_sec;
    164 		uts.tv_nsec = utv32.tv_usec * 1000;
    165 
    166 		if (itimespecfix(&uts)) {
    167 			/*
    168 			 * The timeval was invalid.  Convert it to something
    169 			 * valid that will act as it does under Linux.
    170 			 */
    171 			uts.tv_sec += uts.tv_nsec / 1000000000;
    172 			uts.tv_nsec %= 1000000000;
    173 			if (uts.tv_nsec < 0) {
    174 				uts.tv_sec -= 1;
    175 				uts.tv_nsec += 1000000000;
    176 			}
    177 			if (uts.tv_sec < 0)
    178 				timespecclear(&uts);
    179 		}
    180 		nanotime(&ts0);
    181 		ts = &uts;
    182 	} else
    183 		timespecclear(&uts); /* XXX GCC4 */
    184 
    185 	error = selcommon(l, retval, nfds,
    186 	    readfds, writefds, exceptfds, ts, NULL);
    187 
    188 	if (error) {
    189 		/*
    190 		 * See fs/select.c in the Linux kernel.  Without this,
    191 		 * Maelstrom doesn't work.
    192 		 */
    193 		if (error == ERESTART)
    194 			error = EINTR;
    195 		return error;
    196 	}
    197 
    198 	if (timeout) {
    199 		if (*retval) {
    200 			/*
    201 			 * Compute how much time was left of the timeout,
    202 			 * by subtracting the current time and the time
    203 			 * before we started the call, and subtracting
    204 			 * that result from the user-supplied value.
    205 			 */
    206 			nanotime(&ts1);
    207 			timespecsub(&ts1, &ts0, &ts1);
    208 			timespecsub(&uts, &ts1, &uts);
    209 			if (uts.tv_sec < 0)
    210 				timespecclear(&uts);
    211 		} else {
    212 			timespecclear(&uts);
    213 		}
    214 
    215 		utv32.tv_sec = uts.tv_sec;
    216 		utv32.tv_usec = uts.tv_nsec / 1000;
    217 
    218 		if ((error = copyout(&utv32, timeout, sizeof(utv32))))
    219 			return error;
    220 	}
    221 
    222 	return 0;
    223 }
    224 
    225 int
    226 linux32_sys_pipe(struct lwp *l, const struct linux32_sys_pipe_args *uap, register_t *retval)
    227 {
    228 	/* {
    229 		syscallarg(netbsd32_intp) fd;
    230 	} */
    231 	int error;
    232 	int pfds[2];
    233 
    234 	if ((error = sys_pipe(l, 0, retval)))
    235 		return error;
    236 
    237 	pfds[0] = (int)retval[0];
    238 	pfds[1] = (int)retval[1];
    239 
    240 	if ((error = copyout(pfds, SCARG_P32(uap, fd), 2 * sizeof (int))) != 0)
    241 		return error;
    242 
    243 	retval[0] = 0;
    244 	retval[1] = 0;
    245 
    246 	return 0;
    247 }
    248 
    249 
    250 int
    251 linux32_sys_unlink(struct lwp *l, const struct linux32_sys_unlink_args *uap, register_t *retval)
    252 {
    253 	/* {
    254 		syscallarg(const netbsd32_charp) path;
    255 	} */
    256 	struct linux_sys_unlink_args ua;
    257 
    258 	NETBSD32TOP_UAP(path, const char);
    259 
    260 	return linux_sys_unlink(l, &ua, retval);
    261 }
    262 
    263 int
    264 linux32_sys_creat(struct lwp *l, const struct linux32_sys_creat_args *uap, register_t *retval)
    265 {
    266 	/* {
    267 		syscallarg(const netbsd32_charp) path;
    268 		syscallarg(int) mode;
    269 	} */
    270 	struct sys_open_args ua;
    271 
    272 	NETBSD32TOP_UAP(path, const char);
    273 	SCARG(&ua, flags) = O_CREAT | O_TRUNC | O_WRONLY;
    274 	NETBSD32TO64_UAP(mode);
    275 
    276 	return sys_open(l, &ua, retval);
    277 }
    278 
    279 int
    280 linux32_sys_mknod(struct lwp *l, const struct linux32_sys_mknod_args *uap, register_t *retval)
    281 {
    282 	/* {
    283 		syscallarg(const netbsd32_charp) path;
    284 		syscallarg(int) mode;
    285 		syscallarg(int) dev;
    286 	} */
    287 	struct linux_sys_mknod_args ua;
    288 
    289 	NETBSD32TOP_UAP(path, const char);
    290 	NETBSD32TO64_UAP(mode);
    291 	NETBSD32TO64_UAP(dev);
    292 
    293 	return linux_sys_mknod(l, &ua, retval);
    294 }
    295 
    296 int
    297 linux32_sys_break(struct lwp *l, const struct linux32_sys_break_args *uap, register_t *retval)
    298 {
    299 #if 0
    300 	/* {
    301 		syscallarg(const netbsd32_charp) nsize;
    302 	} */
    303 #endif
    304 
    305 	return ENOSYS;
    306 }
    307 
    308 int
    309 linux32_sys_swapon(struct lwp *l, const struct linux32_sys_swapon_args *uap, register_t *retval)
    310 {
    311 	/* {
    312 		syscallarg(const netbsd32_charp) name;
    313 	} */
    314 	struct sys_swapctl_args ua;
    315 
    316         SCARG(&ua, cmd) = SWAP_ON;
    317         SCARG(&ua, arg) = SCARG_P32(uap, name);
    318         SCARG(&ua, misc) = 0;   /* priority */
    319         return (sys_swapctl(l, &ua, retval));
    320 }
    321 
    322 int
    323 linux32_sys_swapoff(struct lwp *l, const struct linux32_sys_swapoff_args *uap, register_t *retval)
    324 {
    325 	/* {
    326 		syscallarg(const netbsd32_charp) path;
    327 	} */
    328 	struct sys_swapctl_args ua;
    329 
    330         SCARG(&ua, cmd) = SWAP_OFF;
    331         SCARG(&ua, arg) = SCARG_P32(uap, path);
    332         SCARG(&ua, misc) = 0;   /* priority */
    333         return (sys_swapctl(l, &ua, retval));
    334 }
    335 
    336 
    337 int
    338 linux32_sys_reboot(struct lwp *l, const struct linux32_sys_reboot_args *uap, register_t *retval)
    339 {
    340 	/* {
    341 		syscallarg(int) magic1;
    342 		syscallarg(int) magic2;
    343 		syscallarg(int) cmd;
    344 		syscallarg(netbsd32_voidp) arg;
    345 	} */
    346 	struct linux_sys_reboot_args ua;
    347 
    348 	NETBSD32TO64_UAP(magic1);
    349 	NETBSD32TO64_UAP(magic2);
    350 	NETBSD32TO64_UAP(cmd);
    351 	NETBSD32TOP_UAP(arg, void);
    352 
    353 	return linux_sys_reboot(l, &ua, retval);
    354 }
    355 
    356 int
    357 linux32_sys_setresuid(struct lwp *l, const struct linux32_sys_setresuid_args *uap, register_t *retval)
    358 {
    359 	/* {
    360 		syscallarg(uid_t) ruid;
    361 		syscallarg(uid_t) euid;
    362 		syscallarg(uid_t) suid;
    363 	} */
    364 	struct linux_sys_setresuid_args ua;
    365 
    366 	NETBSD32TO64_UAP(ruid);
    367 	NETBSD32TO64_UAP(euid);
    368 	NETBSD32TO64_UAP(suid);
    369 
    370 	return linux_sys_setresuid(l, &ua, retval);
    371 }
    372 
    373 int
    374 linux32_sys_getresuid(struct lwp *l, const struct linux32_sys_getresuid_args *uap, register_t *retval)
    375 {
    376 	/* {
    377 		syscallarg(linux32_uidp_t) ruid;
    378 		syscallarg(linux32_uidp_t) euid;
    379 		syscallarg(linux32_uidp_t) suid;
    380 	} */
    381 	kauth_cred_t pc = l->l_cred;
    382 	int error;
    383 	uid_t uid;
    384 
    385 	uid = kauth_cred_getuid(pc);
    386 	if ((error = copyout(&uid, SCARG_P32(uap, ruid), sizeof(uid_t))) != 0)
    387 		return error;
    388 
    389 	uid = kauth_cred_geteuid(pc);
    390 	if ((error = copyout(&uid, SCARG_P32(uap, euid), sizeof(uid_t))) != 0)
    391 		return error;
    392 
    393 	uid = kauth_cred_getsvuid(pc);
    394 	return copyout(&uid, SCARG_P32(uap, suid), sizeof(uid_t));
    395 }
    396 
    397 int
    398 linux32_sys_setresgid(struct lwp *l, const struct linux32_sys_setresgid_args *uap, register_t *retval)
    399 {
    400 	/* {
    401 		syscallarg(gid_t) rgid;
    402 		syscallarg(gid_t) egid;
    403 		syscallarg(gid_t) sgid;
    404 	} */
    405 	struct linux_sys_setresgid_args ua;
    406 
    407 	NETBSD32TO64_UAP(rgid);
    408 	NETBSD32TO64_UAP(egid);
    409 	NETBSD32TO64_UAP(sgid);
    410 
    411 	return linux_sys_setresgid(l, &ua, retval);
    412 }
    413 
    414 int
    415 linux32_sys_getresgid(struct lwp *l, const struct linux32_sys_getresgid_args *uap, register_t *retval)
    416 {
    417 	/* {
    418 		syscallarg(linux32_gidp_t) rgid;
    419 		syscallarg(linux32_gidp_t) egid;
    420 		syscallarg(linux32_gidp_t) sgid;
    421 	} */
    422 	kauth_cred_t pc = l->l_cred;
    423 	int error;
    424 	gid_t gid;
    425 
    426 	gid = kauth_cred_getgid(pc);
    427 	if ((error = copyout(&gid, SCARG_P32(uap, rgid), sizeof(gid_t))) != 0)
    428 		return error;
    429 
    430 	gid = kauth_cred_getegid(pc);
    431 	if ((error = copyout(&gid, SCARG_P32(uap, egid), sizeof(gid_t))) != 0)
    432 		return error;
    433 
    434 	gid = kauth_cred_getsvgid(pc);
    435 	return copyout(&gid, SCARG_P32(uap, sgid), sizeof(gid_t));
    436 }
    437 
    438 int
    439 linux32_sys_nice(struct lwp *l, const struct linux32_sys_nice_args *uap, register_t *retval)
    440 {
    441 	/* {
    442 		syscallarg(int) incr;
    443 	} */
    444 	struct proc *p = l->l_proc;
    445 	struct sys_setpriority_args bsa;
    446 
    447 	SCARG(&bsa, which) = PRIO_PROCESS;
    448 	SCARG(&bsa, who) = 0;
    449 	SCARG(&bsa, prio) = p->p_nice - NZERO + SCARG(uap, incr);
    450 
    451 	return sys_setpriority(l, &bsa, retval);
    452 }
    453 
    454 int
    455 linux32_sys_alarm(struct lwp *l, const struct linux32_sys_alarm_args *uap, register_t *retval)
    456 {
    457 	/* {
    458 		syscallarg(unsigned int) secs;
    459 	} */
    460 	struct linux_sys_alarm_args ua;
    461 
    462 	NETBSD32TO64_UAP(secs);
    463 
    464 	return linux_sys_alarm(l, &ua, retval);
    465 }
    466 
    467 int
    468 linux32_sys_fdatasync(struct lwp *l, const struct linux32_sys_fdatasync_args *uap, register_t *retval)
    469 {
    470 	/* {
    471 		syscallarg(int) fd;
    472 	} */
    473 	struct linux_sys_fdatasync_args ua;
    474 
    475 	NETBSD32TO64_UAP(fd);
    476 
    477 	return linux_sys_fdatasync(l, &ua, retval);
    478 }
    479 
    480 int
    481 linux32_sys_setfsuid(struct lwp *l, const struct linux32_sys_setfsuid_args *uap, register_t *retval)
    482 {
    483 	/* {
    484 		syscallarg(uid_t) uid;
    485 	} */
    486 	struct linux_sys_setfsuid_args ua;
    487 
    488 	NETBSD32TO64_UAP(uid);
    489 
    490 	return linux_sys_setfsuid(l, &ua, retval);
    491 }
    492 
    493 int
    494 linux32_sys_setfsgid(struct lwp *l, const struct linux32_sys_setfsgid_args *uap, register_t *retval)
    495 {
    496 	/* {
    497 		syscallarg(gid_t) gid;
    498 	} */
    499 	struct linux_sys_setfsgid_args ua;
    500 
    501 	NETBSD32TO64_UAP(gid);
    502 
    503 	return linux_sys_setfsgid(l, &ua, retval);
    504 }
    505 
    506 /*
    507  * pread(2).
    508  */
    509 int
    510 linux32_sys_pread(struct lwp *l,
    511     const struct linux32_sys_pread_args *uap, register_t *retval)
    512 {
    513 	/* {
    514 		syscallarg(int) fd;
    515 		syscallarg(netbsd32_voidp) buf;
    516 		syscallarg(netbsd32_size_t) nbyte;
    517 		syscallarg(linux32_off_t) offset;
    518 	} */
    519 	struct sys_pread_args pra;
    520 
    521 	SCARG(&pra, fd) = SCARG(uap, fd);
    522 	SCARG(&pra, buf) = SCARG_P32(uap, buf);
    523 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    524 	SCARG(&pra, offset) = SCARG(uap, offset);
    525 
    526 	return sys_pread(l, &pra, retval);
    527 }
    528 
    529 /*
    530  * pwrite(2).
    531  */
    532 int
    533 linux32_sys_pwrite(struct lwp *l,
    534     const struct linux32_sys_pwrite_args *uap, register_t *retval)
    535 {
    536 	/* {
    537 		syscallarg(int) fd;
    538 		syscallarg(const netbsd32_voidp) buf;
    539 		syscallarg(netbsd32_size_t) nbyte;
    540 		syscallarg(linux32_off_t) offset;
    541 	} */
    542 	struct sys_pwrite_args pra;
    543 
    544 	SCARG(&pra, fd) = SCARG(uap, fd);
    545 	SCARG(&pra, buf) = SCARG_P32(uap, buf);
    546 	SCARG(&pra, nbyte) = SCARG(uap, nbyte);
    547 	SCARG(&pra, offset) = SCARG(uap, offset);
    548 
    549 	return sys_pwrite(l, &pra, retval);
    550 }
    551 
    552