Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.34
      1 /*	$NetBSD: linux_misc.c,v 1.34 1997/10/10 06:25:34 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Frank van der Linden
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project
     18  *      by Frank van der Linden
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Linux compatibility module. Try to deal with various Linux system calls.
     36  */
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/namei.h>
     41 #include <sys/proc.h>
     42 #include <sys/dirent.h>
     43 #include <sys/file.h>
     44 #include <sys/stat.h>
     45 #include <sys/filedesc.h>
     46 #include <sys/ioctl.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mbuf.h>
     50 #include <sys/mman.h>
     51 #include <sys/mount.h>
     52 #include <sys/ptrace.h>
     53 #include <sys/resource.h>
     54 #include <sys/resourcevar.h>
     55 #include <sys/signal.h>
     56 #include <sys/signalvar.h>
     57 #include <sys/socket.h>
     58 #include <sys/time.h>
     59 #include <sys/times.h>
     60 #include <sys/vnode.h>
     61 #include <sys/uio.h>
     62 #include <sys/wait.h>
     63 #include <sys/utsname.h>
     64 #include <sys/unistd.h>
     65 
     66 #include <sys/syscallargs.h>
     67 
     68 #include <vm/vm.h>
     69 #include <vm/vm_param.h>
     70 
     71 #include <compat/linux/linux_types.h>
     72 #include <compat/linux/linux_fcntl.h>
     73 #include <compat/linux/linux_mmap.h>
     74 #include <compat/linux/linux_signal.h>
     75 #include <compat/linux/linux_syscallargs.h>
     76 #include <compat/linux/linux_util.h>
     77 #include <compat/linux/linux_dirent.h>
     78 
     79 /* linux_misc.c */
     80 static void bsd_to_linux_wstat __P((int *));
     81 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
     82 int linux_select1 __P((struct proc *, register_t *, int, fd_set *, fd_set *,
     83 		       fd_set *, struct timeval *));
     84 
     85 /*
     86  * The information on a terminated (or stopped) process needs
     87  * to be converted in order for Linux binaries to get a valid signal
     88  * number out of it.
     89  */
     90 static void
     91 bsd_to_linux_wstat(status)
     92 	int *status;
     93 {
     94 
     95 	if (WIFSIGNALED(*status))
     96 		*status = (*status & ~0177) |
     97 		    bsd_to_linux_sig[WTERMSIG(*status)];
     98 	else if (WIFSTOPPED(*status))
     99 		*status = (*status & ~0xff00) |
    100 		    (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
    101 }
    102 
    103 /*
    104  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
    105  * reserve some space for a NetBSD-style wait status, and converting
    106  * it to what Linux wants.
    107  */
    108 int
    109 linux_sys_waitpid(p, v, retval)
    110 	struct proc *p;
    111 	void *v;
    112 	register_t *retval;
    113 {
    114 	struct linux_sys_waitpid_args /* {
    115 		syscallarg(int) pid;
    116 		syscallarg(int *) status;
    117 		syscallarg(int) options;
    118 	} */ *uap = v;
    119 	struct sys_wait4_args w4a;
    120 	int error, *status, tstat;
    121 	caddr_t sg;
    122 
    123 	if (SCARG(uap, status) != NULL) {
    124 		sg = stackgap_init(p->p_emul);
    125 		status = (int *) stackgap_alloc(&sg, sizeof status);
    126 	} else
    127 		status = NULL;
    128 
    129 	SCARG(&w4a, pid) = SCARG(uap, pid);
    130 	SCARG(&w4a, status) = status;
    131 	SCARG(&w4a, options) = SCARG(uap, options);
    132 	SCARG(&w4a, rusage) = NULL;
    133 
    134 	if ((error = sys_wait4(p, &w4a, retval)))
    135 		return error;
    136 
    137 	p->p_siglist &= ~sigmask(SIGCHLD);
    138 
    139 	if (status != NULL) {
    140 		if ((error = copyin(status, &tstat, sizeof tstat)))
    141 			return error;
    142 
    143 		bsd_to_linux_wstat(&tstat);
    144 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    145 	}
    146 
    147 	return 0;
    148 }
    149 
    150 /*
    151  * This is very much the same as waitpid()
    152  */
    153 int
    154 linux_sys_wait4(p, v, retval)
    155 	struct proc *p;
    156 	void *v;
    157 	register_t *retval;
    158 {
    159 	struct linux_sys_wait4_args /* {
    160 		syscallarg(int) pid;
    161 		syscallarg(int *) status;
    162 		syscallarg(int) options;
    163 		syscallarg(struct rusage *) rusage;
    164 	} */ *uap = v;
    165 	struct sys_wait4_args w4a;
    166 	int error, *status, tstat;
    167 	caddr_t sg;
    168 
    169 	if (SCARG(uap, status) != NULL) {
    170 		sg = stackgap_init(p->p_emul);
    171 		status = (int *) stackgap_alloc(&sg, sizeof status);
    172 	} else
    173 		status = NULL;
    174 
    175 	SCARG(&w4a, pid) = SCARG(uap, pid);
    176 	SCARG(&w4a, status) = status;
    177 	SCARG(&w4a, options) = SCARG(uap, options);
    178 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    179 
    180 	if ((error = sys_wait4(p, &w4a, retval)))
    181 		return error;
    182 
    183 	p->p_siglist &= ~sigmask(SIGCHLD);
    184 
    185 	if (status != NULL) {
    186 		if ((error = copyin(status, &tstat, sizeof tstat)))
    187 			return error;
    188 
    189 		bsd_to_linux_wstat(&tstat);
    190 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    191 	}
    192 
    193 	return 0;
    194 }
    195 
    196 /*
    197  * This is the old brk(2) call. I don't think anything in the Linux
    198  * world uses this anymore
    199  */
    200 int
    201 linux_sys_break(p, v, retval)
    202 	struct proc *p;
    203 	void *v;
    204 	register_t *retval;
    205 {
    206 #if 0
    207 	struct linux_sys_brk_args /* {
    208 		syscallarg(char *) nsize;
    209 	} */ *uap = v;
    210 #endif
    211 
    212 	return ENOSYS;
    213 }
    214 
    215 /*
    216  * Linux brk(2). The check if the new address is >= the old one is
    217  * done in the kernel in Linux. NetBSD does it in the library.
    218  */
    219 int
    220 linux_sys_brk(p, v, retval)
    221 	struct proc *p;
    222 	void *v;
    223 	register_t *retval;
    224 {
    225 	struct linux_sys_brk_args /* {
    226 		syscallarg(char *) nsize;
    227 	} */ *uap = v;
    228 	char *nbrk = SCARG(uap, nsize);
    229 	struct sys_obreak_args oba;
    230 	struct vmspace *vm = p->p_vmspace;
    231 	caddr_t oldbrk;
    232 
    233 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
    234 	/*
    235 	 * XXX inconsistent.. Linux always returns at least the old
    236 	 * brk value, but it will be page-aligned if this fails,
    237 	 * and possibly not page aligned if it succeeds (the user
    238 	 * supplied pointer is returned).
    239 	 */
    240 	SCARG(&oba, nsize) = nbrk;
    241 
    242 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
    243 		retval[0] = (register_t)nbrk;
    244 	else
    245 		retval[0] = (register_t)oldbrk;
    246 
    247 	return 0;
    248 }
    249 
    250 /*
    251  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
    252  * need to deal with it.
    253  */
    254 int
    255 linux_sys_time(p, v, retval)
    256 	struct proc *p;
    257 	void *v;
    258 	register_t *retval;
    259 {
    260 	struct linux_sys_time_args /* {
    261 		linux_time_t *t;
    262 	} */ *uap = v;
    263 	struct timeval atv;
    264 	linux_time_t tt;
    265 	int error;
    266 
    267 	microtime(&atv);
    268 
    269 	tt = atv.tv_sec;
    270 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
    271 		return error;
    272 
    273 	retval[0] = tt;
    274 	return 0;
    275 }
    276 
    277 /*
    278  * Convert BSD statfs structure to Linux statfs structure.
    279  * The Linux structure has less fields, and it also wants
    280  * the length of a name in a dir entry in a field, which
    281  * we fake (probably the wrong way).
    282  */
    283 static void
    284 bsd_to_linux_statfs(bsp, lsp)
    285 	struct statfs *bsp;
    286 	struct linux_statfs *lsp;
    287 {
    288 
    289 	lsp->l_ftype = bsp->f_type;
    290 	lsp->l_fbsize = bsp->f_bsize;
    291 	lsp->l_fblocks = bsp->f_blocks;
    292 	lsp->l_fbfree = bsp->f_bfree;
    293 	lsp->l_fbavail = bsp->f_bavail;
    294 	lsp->l_ffiles = bsp->f_files;
    295 	lsp->l_fffree = bsp->f_ffree;
    296 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    297 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    298 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    299 }
    300 
    301 /*
    302  * Implement the fs stat functions. Straightforward.
    303  */
    304 int
    305 linux_sys_statfs(p, v, retval)
    306 	struct proc *p;
    307 	void *v;
    308 	register_t *retval;
    309 {
    310 	struct linux_sys_statfs_args /* {
    311 		syscallarg(char *) path;
    312 		syscallarg(struct linux_statfs *) sp;
    313 	} */ *uap = v;
    314 	struct statfs btmp, *bsp;
    315 	struct linux_statfs ltmp;
    316 	struct sys_statfs_args bsa;
    317 	caddr_t sg;
    318 	int error;
    319 
    320 	sg = stackgap_init(p->p_emul);
    321 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    322 
    323 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    324 
    325 	SCARG(&bsa, path) = SCARG(uap, path);
    326 	SCARG(&bsa, buf) = bsp;
    327 
    328 	if ((error = sys_statfs(p, &bsa, retval)))
    329 		return error;
    330 
    331 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    332 		return error;
    333 
    334 	bsd_to_linux_statfs(&btmp, &ltmp);
    335 
    336 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    337 }
    338 
    339 int
    340 linux_sys_fstatfs(p, v, retval)
    341 	struct proc *p;
    342 	void *v;
    343 	register_t *retval;
    344 {
    345 	struct linux_sys_fstatfs_args /* {
    346 		syscallarg(int) fd;
    347 		syscallarg(struct linux_statfs *) sp;
    348 	} */ *uap = v;
    349 	struct statfs btmp, *bsp;
    350 	struct linux_statfs ltmp;
    351 	struct sys_fstatfs_args bsa;
    352 	caddr_t sg;
    353 	int error;
    354 
    355 	sg = stackgap_init(p->p_emul);
    356 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    357 
    358 	SCARG(&bsa, fd) = SCARG(uap, fd);
    359 	SCARG(&bsa, buf) = bsp;
    360 
    361 	if ((error = sys_fstatfs(p, &bsa, retval)))
    362 		return error;
    363 
    364 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    365 		return error;
    366 
    367 	bsd_to_linux_statfs(&btmp, &ltmp);
    368 
    369 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    370 }
    371 
    372 /*
    373  * uname(). Just copy the info from the various strings stored in the
    374  * kernel, and put it in the Linux utsname structure. That structure
    375  * is almost the same as the NetBSD one, only it has fields 65 characters
    376  * long, and an extra domainname field.
    377  */
    378 int
    379 linux_sys_uname(p, v, retval)
    380 	struct proc *p;
    381 	void *v;
    382 	register_t *retval;
    383 {
    384 	struct linux_sys_uname_args /* {
    385 		syscallarg(struct linux_utsname *) up;
    386 	} */ *uap = v;
    387 	extern char ostype[], hostname[], osrelease[], version[], machine[],
    388 	    domainname[];
    389 	struct linux_utsname luts;
    390 	int len;
    391 	char *cp;
    392 
    393 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
    394 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    395 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
    396 	strncpy(luts.l_version, version, sizeof(luts.l_version));
    397 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    398 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    399 
    400 	/* This part taken from the the uname() in libc */
    401 	len = sizeof(luts.l_version);
    402 	for (cp = luts.l_version; len--; ++cp)
    403 		if (*cp == '\n' || *cp == '\t')
    404 			if (len > 1)
    405 				*cp = ' ';
    406 			else
    407 				*cp = '\0';
    408 
    409 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    410 }
    411 
    412 int
    413 linux_sys_olduname(p, v, retval)
    414 	struct proc *p;
    415 	void *v;
    416 	register_t *retval;
    417 {
    418 	struct linux_sys_uname_args /* {
    419 		syscallarg(struct linux_oldutsname *) up;
    420 	} */ *uap = v;
    421 	extern char ostype[], hostname[], osrelease[], version[], machine[];
    422 	struct linux_oldutsname luts;
    423 	int len;
    424 	char *cp;
    425 
    426 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
    427 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    428 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
    429 	strncpy(luts.l_version, version, sizeof(luts.l_version));
    430 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    431 
    432 	/* This part taken from the the uname() in libc */
    433 	len = sizeof(luts.l_version);
    434 	for (cp = luts.l_version; len--; ++cp)
    435 		if (*cp == '\n' || *cp == '\t')
    436 			if (len > 1)
    437 				*cp = ' ';
    438 			else
    439 				*cp = '\0';
    440 
    441 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    442 }
    443 
    444 int
    445 linux_sys_oldolduname(p, v, retval)
    446 	struct proc *p;
    447 	void *v;
    448 	register_t *retval;
    449 {
    450 	struct linux_sys_uname_args /* {
    451 		syscallarg(struct linux_oldoldutsname *) up;
    452 	} */ *uap = v;
    453 	extern char ostype[], hostname[], osrelease[], version[], machine[];
    454 	struct linux_oldoldutsname luts;
    455 	int len;
    456 	char *cp;
    457 
    458 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
    459 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    460 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
    461 	strncpy(luts.l_version, version, sizeof(luts.l_version));
    462 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    463 
    464 	/* This part taken from the the uname() in libc */
    465 	len = sizeof(luts.l_version);
    466 	for (cp = luts.l_version; len--; ++cp)
    467 		if (*cp == '\n' || *cp == '\t')
    468 			if (len > 1)
    469 				*cp = ' ';
    470 			else
    471 				*cp = '\0';
    472 
    473 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    474 }
    475 
    476 /*
    477  * Linux wants to pass everything to a syscall in registers. However,
    478  * mmap() has 6 of them. Oops: out of register error. They just pass
    479  * everything in a structure.
    480  */
    481 int
    482 linux_sys_mmap(p, v, retval)
    483 	struct proc *p;
    484 	void *v;
    485 	register_t *retval;
    486 {
    487 	struct linux_sys_mmap_args /* {
    488 		syscallarg(struct linux_mmap *) lmp;
    489 	} */ *uap = v;
    490 	struct linux_mmap lmap;
    491 	struct sys_mmap_args cma;
    492 	int error, flags;
    493 
    494 	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
    495 		return error;
    496 
    497 	flags = 0;
    498 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
    499 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
    500 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
    501 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
    502 
    503 	SCARG(&cma,addr) = lmap.lm_addr;
    504 	SCARG(&cma,len) = lmap.lm_len;
    505 	if (lmap.lm_prot & VM_PROT_WRITE) /* XXX */
    506 		lmap.lm_prot |= VM_PROT_READ;
    507  	SCARG(&cma,prot) = lmap.lm_prot;
    508 	SCARG(&cma,flags) = flags;
    509 	SCARG(&cma,fd) = lmap.lm_fd;
    510 	SCARG(&cma,pad) = 0;
    511 	SCARG(&cma,pos) = lmap.lm_pos;
    512 
    513 	return sys_mmap(p, &cma, retval);
    514 }
    515 
    516 int
    517 linux_sys_mremap(p, v, retval)
    518 	struct proc *p;
    519 	void *v;
    520 	register_t *retval;
    521 {
    522 #ifdef notyet
    523 	struct linux_sys_mremap_args /* {
    524 		syscallarg(void *) old_address;
    525 		syscallarg(size_t) old_size;
    526 		syscallarg(size_t) new_size;
    527 		syscallarg(u_long) flags;
    528 	} */ *uap = v;
    529 #endif
    530 
    531 	return ENOMEM;
    532 }
    533 
    534 int
    535 linux_sys_msync(p, v, retval)
    536 	struct proc *p;
    537 	void *v;
    538 	register_t *retval;
    539 {
    540 	struct linux_sys_msync_args /* {
    541 		syscallarg(caddr_t) addr;
    542 		syscallarg(int) len;
    543 		syscallarg(int) fl;
    544 	} */ *uap = v;
    545 
    546 	struct sys_msync_args bma;
    547 
    548 	/* flags are ignored */
    549 	SCARG(&bma, addr) = SCARG(uap, addr);
    550 	SCARG(&bma, len) = SCARG(uap, len);
    551 
    552 	return sys_msync(p, &bma, retval);
    553 }
    554 
    555 /*
    556  * This code is partly stolen from src/lib/libc/compat-43/times.c
    557  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    558  */
    559 
    560 #define CLK_TCK 100
    561 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    562 
    563 int
    564 linux_sys_times(p, v, retval)
    565 	struct proc *p;
    566 	void *v;
    567 	register_t *retval;
    568 {
    569 	struct linux_sys_times_args /* {
    570 		syscallarg(struct times *) tms;
    571 	} */ *uap = v;
    572 	struct timeval t;
    573 	struct linux_tms ltms;
    574 	struct rusage ru;
    575 	int error, s;
    576 
    577 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    578 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    579 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    580 
    581 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    582 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    583 
    584 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    585 		return error;
    586 
    587 	s = splclock();
    588 	timersub(&time, &boottime, &t);
    589 	splx(s);
    590 
    591 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    592 	return 0;
    593 }
    594 
    595 /*
    596  * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
    597  * Linux directly passes the pointer.
    598  */
    599 int
    600 linux_sys_pipe(p, v, retval)
    601 	struct proc *p;
    602 	void *v;
    603 	register_t *retval;
    604 {
    605 	struct linux_sys_pipe_args /* {
    606 		syscallarg(int *) pfds;
    607 	} */ *uap = v;
    608 	int error;
    609 
    610 	if ((error = sys_pipe(p, 0, retval)))
    611 		return error;
    612 
    613 	/* Assumes register_t is an int */
    614 
    615 	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
    616 		return error;
    617 
    618 	retval[0] = 0;
    619 	return 0;
    620 }
    621 
    622 /*
    623  * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
    624  * Fiddle with the timers to make it work.
    625  */
    626 int
    627 linux_sys_alarm(p, v, retval)
    628 	struct proc *p;
    629 	void *v;
    630 	register_t *retval;
    631 {
    632 	struct linux_sys_alarm_args /* {
    633 		syscallarg(unsigned int) secs;
    634 	} */ *uap = v;
    635 	int s;
    636 	struct itimerval *itp, it;
    637 
    638 	itp = &p->p_realtimer;
    639 	s = splclock();
    640 	/*
    641 	 * Clear any pending timer alarms.
    642 	 */
    643 	untimeout(realitexpire, p);
    644 	timerclear(&itp->it_interval);
    645 	if (timerisset(&itp->it_value) &&
    646 	    timercmp(&itp->it_value, &time, >))
    647 		timersub(&itp->it_value, &time, &itp->it_value);
    648 	/*
    649 	 * Return how many seconds were left (rounded up)
    650 	 */
    651 	retval[0] = itp->it_value.tv_sec;
    652 	if (itp->it_value.tv_usec)
    653 		retval[0]++;
    654 
    655 	/*
    656 	 * alarm(0) just resets the timer.
    657 	 */
    658 	if (SCARG(uap, secs) == 0) {
    659 		timerclear(&itp->it_value);
    660 		splx(s);
    661 		return 0;
    662 	}
    663 
    664 	/*
    665 	 * Check the new alarm time for sanity, and set it.
    666 	 */
    667 	timerclear(&it.it_interval);
    668 	it.it_value.tv_sec = SCARG(uap, secs);
    669 	it.it_value.tv_usec = 0;
    670 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
    671 		splx(s);
    672 		return (EINVAL);
    673 	}
    674 
    675 	if (timerisset(&it.it_value)) {
    676 		timeradd(&it.it_value, &time, &it.it_value);
    677 		timeout(realitexpire, p, hzto(&it.it_value));
    678 	}
    679 	p->p_realtimer = it;
    680 	splx(s);
    681 
    682 	return 0;
    683 }
    684 
    685 /*
    686  * utime(). Do conversion to things that utimes() understands,
    687  * and pass it on.
    688  */
    689 int
    690 linux_sys_utime(p, v, retval)
    691 	struct proc *p;
    692 	void *v;
    693 	register_t *retval;
    694 {
    695 	struct linux_sys_utime_args /* {
    696 		syscallarg(char *) path;
    697 		syscallarg(struct linux_utimbuf *)times;
    698 	} */ *uap = v;
    699 	caddr_t sg;
    700 	int error;
    701 	struct sys_utimes_args ua;
    702 	struct timeval tv[2], *tvp;
    703 	struct linux_utimbuf lut;
    704 
    705 	sg = stackgap_init(p->p_emul);
    706 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    707 
    708 	SCARG(&ua, path) = SCARG(uap, path);
    709 
    710 	if (SCARG(uap, times) != NULL) {
    711 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
    712 			return error;
    713 		tv[0].tv_usec = tv[1].tv_usec = 0;
    714 		tv[0].tv_sec = lut.l_actime;
    715 		tv[1].tv_sec = lut.l_modtime;
    716 		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
    717 		if ((error = copyout(tv, tvp, sizeof tv)))
    718 			return error;
    719 		SCARG(&ua, tptr) = tvp;
    720 	}
    721 	else
    722 		SCARG(&ua, tptr) = NULL;
    723 
    724 	return sys_utimes(p, uap, retval);
    725 }
    726 
    727 /*
    728  * The old Linux readdir was only able to read one entry at a time,
    729  * even though it had a 'count' argument. In fact, the emulation
    730  * of the old call was better than the original, because it did handle
    731  * the count arg properly. Don't bother with it anymore now, and use
    732  * it to distinguish between old and new. The difference is that the
    733  * newer one actually does multiple entries, and the reclen field
    734  * really is the reclen, not the namelength.
    735  */
    736 int
    737 linux_sys_readdir(p, v, retval)
    738 	struct proc *p;
    739 	void *v;
    740 	register_t *retval;
    741 {
    742 	struct linux_sys_readdir_args /* {
    743 		syscallarg(int) fd;
    744 		syscallarg(struct linux_dirent *) dent;
    745 		syscallarg(unsigned int) count;
    746 	} */ *uap = v;
    747 
    748 	SCARG(uap, count) = 1;
    749 	return linux_sys_getdents(p, uap, retval);
    750 }
    751 
    752 /*
    753  * Linux 'readdir' call. This code is mostly taken from the
    754  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    755  * an attempt has been made to keep it a little cleaner (failing
    756  * miserably, because of the cruft needed if count 1 is passed).
    757  *
    758  * The d_off field should contain the offset of the next valid entry,
    759  * but in Linux it has the offset of the entry itself. We emulate
    760  * that bug here.
    761  *
    762  * Read in BSD-style entries, convert them, and copy them out.
    763  *
    764  * Note that this doesn't handle union-mounted filesystems.
    765  */
    766 int
    767 linux_sys_getdents(p, v, retval)
    768 	struct proc *p;
    769 	void *v;
    770 	register_t *retval;
    771 {
    772 	struct linux_sys_readdir_args /* {
    773 		syscallarg(int) fd;
    774 		syscallarg(caddr_t) dent;
    775 		syscallarg(unsigned int) count;
    776 	} */ *uap = v;
    777 	register struct dirent *bdp;
    778 	struct vnode *vp;
    779 	caddr_t	inp, buf;		/* BSD-format */
    780 	int len, reclen;		/* BSD-format */
    781 	caddr_t outp;			/* Linux-format */
    782 	int resid, linux_reclen = 0;	/* Linux-format */
    783 	struct file *fp;
    784 	struct uio auio;
    785 	struct iovec aiov;
    786 	struct linux_dirent idb;
    787 	off_t off;		/* true file offset */
    788 	int buflen, error, eofflag, nbytes, oldcall;
    789 	struct vattr va;
    790 	off_t *cookiebuf, *cookie;
    791 	int ncookies;
    792 
    793 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    794 		return (error);
    795 
    796 	if ((fp->f_flag & FREAD) == 0)
    797 		return (EBADF);
    798 
    799 	vp = (struct vnode *)fp->f_data;
    800 
    801 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
    802 		return (EINVAL);
    803 
    804 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    805 		return error;
    806 
    807 	nbytes = SCARG(uap, count);
    808 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    809 		nbytes = sizeof (struct linux_dirent);
    810 		buflen = max(va.va_blocksize, nbytes);
    811 		oldcall = 1;
    812 	} else {
    813 		buflen = min(MAXBSIZE, nbytes);
    814 		if (buflen < va.va_blocksize)
    815 			buflen = va.va_blocksize;
    816 		oldcall = 0;
    817 	}
    818 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    819 	ncookies = buflen / 16;
    820 	cookiebuf = malloc(ncookies * sizeof(*cookiebuf), M_TEMP, M_WAITOK);
    821 
    822 	VOP_LOCK(vp);
    823 	off = fp->f_offset;
    824 again:
    825 	aiov.iov_base = buf;
    826 	aiov.iov_len = buflen;
    827 	auio.uio_iov = &aiov;
    828 	auio.uio_iovcnt = 1;
    829 	auio.uio_rw = UIO_READ;
    830 	auio.uio_segflg = UIO_SYSSPACE;
    831 	auio.uio_procp = p;
    832 	auio.uio_resid = buflen;
    833 	auio.uio_offset = off;
    834 	/*
    835          * First we read into the malloc'ed buffer, then
    836          * we massage it into user space, one record at a time.
    837          */
    838 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookiebuf,
    839 	    ncookies);
    840 	if (error)
    841 		goto out;
    842 
    843 	inp = buf;
    844 	outp = SCARG(uap, dent);
    845 	resid = nbytes;
    846 	if (eofflag || (len = buflen - auio.uio_resid) == 0)
    847 		goto eof;
    848 
    849 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    850 		bdp = (struct dirent *)inp;
    851 		reclen = bdp->d_reclen;
    852 		if (reclen & 3)
    853 			panic("linux_readdir");
    854 		if (bdp->d_fileno == 0) {
    855 			inp += reclen;	/* it is a hole; squish it out */
    856 			off = *cookie++;
    857 			continue;
    858 		}
    859 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    860 		if (reclen > len || resid < linux_reclen) {
    861 			/* entry too big for buffer, so just stop */
    862 			outp++;
    863 			break;
    864 		}
    865 		/*
    866 		 * Massage in place to make a Linux-shaped dirent (otherwise
    867 		 * we have to worry about touching user memory outside of
    868 		 * the copyout() call).
    869 		 */
    870 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
    871 		/*
    872 		 * The old readdir() call misuses the offset and reclen fields.
    873 		 */
    874 		if (oldcall) {
    875 			idb.d_off = (linux_off_t)linux_reclen;
    876 			idb.d_reclen = (u_short)bdp->d_namlen;
    877 		} else {
    878 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
    879 				compat_offseterr(vp, "linux_getdents");
    880 				error = EINVAL;
    881 				goto out;
    882 			}
    883 			idb.d_off = (linux_off_t)off;
    884 			idb.d_reclen = (u_short)linux_reclen;
    885 		}
    886 		strcpy(idb.d_name, bdp->d_name);
    887 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    888 			goto out;
    889 		/* advance past this real entry */
    890 		inp += reclen;
    891 		off = *cookie++;	/* each entry points to itself */
    892 		/* advance output past Linux-shaped entry */
    893 		outp += linux_reclen;
    894 		resid -= linux_reclen;
    895 		if (oldcall)
    896 			break;
    897 	}
    898 
    899 	/* if we squished out the whole block, try again */
    900 	if (outp == SCARG(uap, dent))
    901 		goto again;
    902 	fp->f_offset = off;	/* update the vnode offset */
    903 
    904 	if (oldcall)
    905 		nbytes = resid + linux_reclen;
    906 
    907 eof:
    908 	*retval = nbytes - resid;
    909 out:
    910 	VOP_UNLOCK(vp);
    911 	free(cookiebuf, M_TEMP);
    912 	free(buf, M_TEMP);
    913 	return error;
    914 }
    915 
    916 /*
    917  * Not sure why the arguments to this older version of select() were put
    918  * into a structure, because there are 5, and that can all be handled
    919  * in registers on the i386 like Linux wants to.
    920  */
    921 int
    922 linux_sys_oldselect(p, v, retval)
    923 	struct proc *p;
    924 	void *v;
    925 	register_t *retval;
    926 {
    927 	struct linux_sys_oldselect_args /* {
    928 		syscallarg(struct linux_select *) lsp;
    929 	} */ *uap = v;
    930 	struct linux_select ls;
    931 	int error;
    932 
    933 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
    934 		return error;
    935 
    936 	return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
    937 	    ls.exceptfds, ls.timeout);
    938 }
    939 
    940 /*
    941  * Even when just using registers to pass arguments to syscalls you can
    942  * have 5 of them on the i386. So this newer version of select() does
    943  * this.
    944  */
    945 int
    946 linux_sys_select(p, v, retval)
    947 	struct proc *p;
    948 	void *v;
    949 	register_t *retval;
    950 {
    951 	struct linux_sys_select_args /* {
    952 		syscallarg(int) nfds;
    953 		syscallarg(fd_set *) readfds;
    954 		syscallarg(fd_set *) writefds;
    955 		syscallarg(fd_set *) exceptfds;
    956 		syscallarg(struct timeval *) timeout;
    957 	} */ *uap = v;
    958 
    959 	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    960 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    961 }
    962 
    963 /*
    964  * Common code for the old and new versions of select(). A couple of
    965  * things are important:
    966  * 1) return the amount of time left in the 'timeout' parameter
    967  * 2) select never returns ERESTART on Linux, always return EINTR
    968  */
    969 int
    970 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
    971 	struct proc *p;
    972 	register_t *retval;
    973 	int nfds;
    974 	fd_set *readfds, *writefds, *exceptfds;
    975 	struct timeval *timeout;
    976 {
    977 	struct sys_select_args bsa;
    978 	struct timeval tv0, tv1, utv, *tvp;
    979 	caddr_t sg;
    980 	int error;
    981 
    982 	SCARG(&bsa, nd) = nfds;
    983 	SCARG(&bsa, in) = readfds;
    984 	SCARG(&bsa, ou) = writefds;
    985 	SCARG(&bsa, ex) = exceptfds;
    986 	SCARG(&bsa, tv) = timeout;
    987 
    988 	/*
    989 	 * Store current time for computation of the amount of
    990 	 * time left.
    991 	 */
    992 	if (timeout) {
    993 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    994 			return error;
    995 		if (itimerfix(&utv)) {
    996 			/*
    997 			 * The timeval was invalid.  Convert it to something
    998 			 * valid that will act as it does under Linux.
    999 			 */
   1000 			sg = stackgap_init(p->p_emul);
   1001 			tvp = stackgap_alloc(&sg, sizeof(utv));
   1002 			utv.tv_sec += utv.tv_usec / 1000000;
   1003 			utv.tv_usec %= 1000000;
   1004 			if (utv.tv_usec < 0) {
   1005 				utv.tv_sec -= 1;
   1006 				utv.tv_usec += 1000000;
   1007 			}
   1008 			if (utv.tv_sec < 0)
   1009 				timerclear(&utv);
   1010 			if ((error = copyout(&utv, tvp, sizeof(utv))))
   1011 				return error;
   1012 			SCARG(&bsa, tv) = tvp;
   1013 		}
   1014 		microtime(&tv0);
   1015 	}
   1016 
   1017 	error = sys_select(p, &bsa, retval);
   1018 	if (error) {
   1019 		/*
   1020 		 * See fs/select.c in the Linux kernel.  Without this,
   1021 		 * Maelstrom doesn't work.
   1022 		 */
   1023 		if (error == ERESTART)
   1024 			error = EINTR;
   1025 		return error;
   1026 	}
   1027 
   1028 	if (timeout) {
   1029 		if (*retval) {
   1030 			/*
   1031 			 * Compute how much time was left of the timeout,
   1032 			 * by subtracting the current time and the time
   1033 			 * before we started the call, and subtracting
   1034 			 * that result from the user-supplied value.
   1035 			 */
   1036 			microtime(&tv1);
   1037 			timersub(&tv1, &tv0, &tv1);
   1038 			timersub(&utv, &tv1, &utv);
   1039 			if (utv.tv_sec < 0)
   1040 				timerclear(&utv);
   1041 		} else
   1042 			timerclear(&utv);
   1043 		if ((error = copyout(&utv, timeout, sizeof(utv))))
   1044 			return error;
   1045 	}
   1046 
   1047 	return 0;
   1048 }
   1049 
   1050 /*
   1051  * Get the process group of a certain process. Look it up
   1052  * and return the value.
   1053  */
   1054 int
   1055 linux_sys_getpgid(p, v, retval)
   1056 	struct proc *p;
   1057 	void *v;
   1058 	register_t *retval;
   1059 {
   1060 	struct linux_sys_getpgid_args /* {
   1061 		syscallarg(int) pid;
   1062 	} */ *uap = v;
   1063 	struct proc *targp;
   1064 
   1065 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
   1066 		if ((targp = pfind(SCARG(uap, pid))) == 0)
   1067 			return ESRCH;
   1068 	}
   1069 	else
   1070 		targp = p;
   1071 
   1072 	retval[0] = targp->p_pgid;
   1073 	return 0;
   1074 }
   1075 
   1076 /*
   1077  * Set the 'personality' (emulation mode) for the current process. Only
   1078  * accept the Linux personality here (0). This call is needed because
   1079  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
   1080  * ELF binaries run in Linux mode, not SVR4 mode.
   1081  */
   1082 int
   1083 linux_sys_personality(p, v, retval)
   1084 	struct proc *p;
   1085 	void *v;
   1086 	register_t *retval;
   1087 {
   1088 	struct linux_sys_personality_args /* {
   1089 		syscallarg(int) per;
   1090 	} */ *uap = v;
   1091 
   1092 	if (SCARG(uap, per) != 0)
   1093 		return EINVAL;
   1094 	retval[0] = 0;
   1095 	return 0;
   1096 }
   1097 
   1098 /*
   1099  * The calls are here because of type conversions.
   1100  */
   1101 int
   1102 linux_sys_setreuid(p, v, retval)
   1103 	struct proc *p;
   1104 	void *v;
   1105 	register_t *retval;
   1106 {
   1107 	struct linux_sys_setreuid_args /* {
   1108 		syscallarg(int) ruid;
   1109 		syscallarg(int) euid;
   1110 	} */ *uap = v;
   1111 	struct sys_setreuid_args bsa;
   1112 
   1113 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
   1114 		(uid_t)-1 : SCARG(uap, ruid);
   1115 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
   1116 		(uid_t)-1 : SCARG(uap, euid);
   1117 
   1118 	return sys_setreuid(p, &bsa, retval);
   1119 }
   1120 
   1121 int
   1122 linux_sys_setregid(p, v, retval)
   1123 	struct proc *p;
   1124 	void *v;
   1125 	register_t *retval;
   1126 {
   1127 	struct linux_sys_setregid_args /* {
   1128 		syscallarg(int) rgid;
   1129 		syscallarg(int) egid;
   1130 	} */ *uap = v;
   1131 	struct sys_setregid_args bsa;
   1132 
   1133 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
   1134 		(uid_t)-1 : SCARG(uap, rgid);
   1135 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
   1136 		(uid_t)-1 : SCARG(uap, egid);
   1137 
   1138 	return sys_setregid(p, &bsa, retval);
   1139 }
   1140 
   1141 int
   1142 linux_sys_getsid(p, v, retval)
   1143 	struct proc *p;
   1144 	void *v;
   1145 	register_t *retval;
   1146 {
   1147 	struct linux_sys_getsid_args /* {
   1148 		syscallarg(int) pid;
   1149 	} */ *uap = v;
   1150 	struct proc *p1;
   1151 	pid_t pid;
   1152 
   1153 	pid = (pid_t)SCARG(uap, pid);
   1154 
   1155 	if (pid == 0) {
   1156 		retval[0] = (int)p->p_session;	/* XXX Oh well */
   1157 		return 0;
   1158 	}
   1159 
   1160 	p1 = pfind((int)pid);
   1161 	if (p1 == NULL)
   1162 		return ESRCH;
   1163 
   1164 	retval[0] = (int)p1->p_session;
   1165 	return 0;
   1166 }
   1167 
   1168 int
   1169 linux_sys___sysctl(p, v, retval)
   1170 	struct proc *p;
   1171 	void *v;
   1172 	register_t *retval;
   1173 {
   1174 	struct linux_sys___sysctl_args /* {
   1175 		syscallarg(struct linux___sysctl *) lsp;
   1176 	} */ *uap = v;
   1177 	struct linux___sysctl ls;
   1178 	struct sys___sysctl_args bsa;
   1179 	int error;
   1180 
   1181 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
   1182 		return error;
   1183 	SCARG(&bsa, name) = ls.name;
   1184 	SCARG(&bsa, namelen) = ls.namelen;
   1185 	SCARG(&bsa, old) = ls.old;
   1186 	SCARG(&bsa, oldlenp) = ls.oldlenp;
   1187 	SCARG(&bsa, new) = ls.new;
   1188 	SCARG(&bsa, newlen) = ls.newlen;
   1189 
   1190 	return sys___sysctl(p, &bsa, retval);
   1191 }
   1192 
   1193 int
   1194 linux_sys_nice(p, v, retval)
   1195 	struct proc *p;
   1196 	void *v;
   1197 	register_t *retval;
   1198 {
   1199 	struct linux_sys_nice_args /* {
   1200 		syscallarg(int) incr;
   1201 	} */ *uap = v;
   1202         struct sys_setpriority_args bsa;
   1203 
   1204         SCARG(&bsa, which) = PRIO_PROCESS;
   1205         SCARG(&bsa, who) = 0;
   1206 	SCARG(&bsa, prio) = SCARG(uap, incr);
   1207         return sys_setpriority(p, &bsa, retval);
   1208 }
   1209