Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.54
      1 /*	$NetBSD: linux_misc.c,v 1.54 1999/05/05 20:01:03 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Linux compatibility module. Try to deal with various Linux system calls.
     41  */
     42 
     43 /*
     44  * These functions have been moved to multiarch to allow
     45  * selection of which machines include them to be
     46  * determined by the individual files.linux_<arch> files.
     47  *
     48  * Function in multiarch:
     49  *	linux_sys_break			: linux_break.c
     50  *	linux_sys_alarm			: linux_misc_notalpha.c
     51  *	linux_sys_nice			: linux_misc_notalpha.c
     52  *	linux_sys_readdir		: linux_misc_notalpha.c
     53  *	linux_sys_time			: linux_misc_notalpha.c
     54  *	linux_sys_utime			: linux_misc_notalpha.c
     55  *	linux_sys_waitpid		: linux_misc_notalpha.c
     56  *	linux_sys_old_mmap		: linux_oldmmap.c
     57  *	linux_sys_oldolduname		: linux_oldolduname.c
     58  *	linux_sys_oldselect		: linux_oldselect.c
     59  *	linux_sys_olduname		: linux_olduname.c
     60  *	linux_sys_pipe			: linux_pipe.c
     61  */
     62 
     63 #include <sys/param.h>
     64 #include <sys/systm.h>
     65 #include <sys/namei.h>
     66 #include <sys/proc.h>
     67 #include <sys/dirent.h>
     68 #include <sys/file.h>
     69 #include <sys/stat.h>
     70 #include <sys/filedesc.h>
     71 #include <sys/ioctl.h>
     72 #include <sys/kernel.h>
     73 #include <sys/malloc.h>
     74 #include <sys/mbuf.h>
     75 #include <sys/mman.h>
     76 #include <sys/mount.h>
     77 #include <sys/ptrace.h>
     78 #include <sys/resource.h>
     79 #include <sys/resourcevar.h>
     80 #include <sys/signal.h>
     81 #include <sys/signalvar.h>
     82 #include <sys/socket.h>
     83 #include <sys/time.h>
     84 #include <sys/times.h>
     85 #include <sys/vnode.h>
     86 #include <sys/uio.h>
     87 #include <sys/wait.h>
     88 #include <sys/utsname.h>
     89 #include <sys/unistd.h>
     90 
     91 #include <sys/syscallargs.h>
     92 
     93 #include <vm/vm.h>
     94 #include <vm/vm_param.h>
     95 
     96 #include <compat/linux/common/linux_types.h>
     97 #include <compat/linux/common/linux_signal.h>
     98 
     99 #include <compat/linux/linux_syscallargs.h>
    100 
    101 #include <compat/linux/common/linux_fcntl.h>
    102 #include <compat/linux/common/linux_mmap.h>
    103 #include <compat/linux/common/linux_dirent.h>
    104 #include <compat/linux/common/linux_util.h>
    105 #include <compat/linux/common/linux_misc.h>
    106 
    107 
    108 /* Local linux_misc.c functions: */
    109 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
    110 
    111 /*
    112  * The information on a terminated (or stopped) process needs
    113  * to be converted in order for Linux binaries to get a valid signal
    114  * number out of it.
    115  */
    116 void
    117 bsd_to_linux_wstat(st)
    118 	int *st;
    119 {
    120 
    121 	int sig;
    122 
    123 	if (WIFSIGNALED(*st)) {
    124 		sig = WTERMSIG(*st);
    125 		if (sig >= 0 && sig < NSIG)
    126 			*st= (*st& ~0177) | native_to_linux_sig[sig];
    127 	} else if (WIFSTOPPED(*st)) {
    128 		sig = WSTOPSIG(*st);
    129 		if (sig >= 0 && sig < NSIG)
    130 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
    131 	}
    132 }
    133 
    134 /*
    135  * This is very much the same as waitpid()
    136  */
    137 int
    138 linux_sys_wait4(p, v, retval)
    139 	struct proc *p;
    140 	void *v;
    141 	register_t *retval;
    142 {
    143 	struct linux_sys_wait4_args /* {
    144 		syscallarg(int) pid;
    145 		syscallarg(int *) status;
    146 		syscallarg(int) options;
    147 		syscallarg(struct rusage *) rusage;
    148 	} */ *uap = v;
    149 	struct sys_wait4_args w4a;
    150 	int error, *status, tstat;
    151 	caddr_t sg;
    152 
    153 	if (SCARG(uap, status) != NULL) {
    154 		sg = stackgap_init(p->p_emul);
    155 		status = (int *) stackgap_alloc(&sg, sizeof *status);
    156 	} else
    157 		status = NULL;
    158 
    159 	SCARG(&w4a, pid) = SCARG(uap, pid);
    160 	SCARG(&w4a, status) = status;
    161 	SCARG(&w4a, options) = SCARG(uap, options);
    162 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    163 
    164 	if ((error = sys_wait4(p, &w4a, retval)))
    165 		return error;
    166 
    167 	sigdelset(&p->p_siglist, SIGCHLD);
    168 
    169 	if (status != NULL) {
    170 		if ((error = copyin(status, &tstat, sizeof tstat)))
    171 			return error;
    172 
    173 		bsd_to_linux_wstat(&tstat);
    174 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    175 	}
    176 
    177 	return 0;
    178 }
    179 
    180 /*
    181  * Linux brk(2). The check if the new address is >= the old one is
    182  * done in the kernel in Linux. NetBSD does it in the library.
    183  */
    184 int
    185 linux_sys_brk(p, v, retval)
    186 	struct proc *p;
    187 	void *v;
    188 	register_t *retval;
    189 {
    190 	struct linux_sys_brk_args /* {
    191 		syscallarg(char *) nsize;
    192 	} */ *uap = v;
    193 	char *nbrk = SCARG(uap, nsize);
    194 	struct sys_obreak_args oba;
    195 	struct vmspace *vm = p->p_vmspace;
    196 	caddr_t oldbrk;
    197 
    198 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
    199 	/*
    200 	 * XXX inconsistent.. Linux always returns at least the old
    201 	 * brk value, but it will be page-aligned if this fails,
    202 	 * and possibly not page aligned if it succeeds (the user
    203 	 * supplied pointer is returned).
    204 	 */
    205 	SCARG(&oba, nsize) = nbrk;
    206 
    207 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
    208 		retval[0] = (register_t)nbrk;
    209 	else
    210 		retval[0] = (register_t)oldbrk;
    211 
    212 	return 0;
    213 }
    214 
    215 /*
    216  * Convert BSD statfs structure to Linux statfs structure.
    217  * The Linux structure has less fields, and it also wants
    218  * the length of a name in a dir entry in a field, which
    219  * we fake (probably the wrong way).
    220  */
    221 static void
    222 bsd_to_linux_statfs(bsp, lsp)
    223 	struct statfs *bsp;
    224 	struct linux_statfs *lsp;
    225 {
    226 
    227 	lsp->l_ftype = bsp->f_type;
    228 	lsp->l_fbsize = bsp->f_bsize;
    229 	lsp->l_fblocks = bsp->f_blocks;
    230 	lsp->l_fbfree = bsp->f_bfree;
    231 	lsp->l_fbavail = bsp->f_bavail;
    232 	lsp->l_ffiles = bsp->f_files;
    233 	lsp->l_fffree = bsp->f_ffree;
    234 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    235 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    236 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    237 }
    238 
    239 /*
    240  * Implement the fs stat functions. Straightforward.
    241  */
    242 int
    243 linux_sys_statfs(p, v, retval)
    244 	struct proc *p;
    245 	void *v;
    246 	register_t *retval;
    247 {
    248 	struct linux_sys_statfs_args /* {
    249 		syscallarg(const char *) path;
    250 		syscallarg(struct linux_statfs *) sp;
    251 	} */ *uap = v;
    252 	struct statfs btmp, *bsp;
    253 	struct linux_statfs ltmp;
    254 	struct sys_statfs_args bsa;
    255 	caddr_t sg;
    256 	int error;
    257 
    258 	sg = stackgap_init(p->p_emul);
    259 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    260 
    261 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    262 
    263 	SCARG(&bsa, path) = SCARG(uap, path);
    264 	SCARG(&bsa, buf) = bsp;
    265 
    266 	if ((error = sys_statfs(p, &bsa, retval)))
    267 		return error;
    268 
    269 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    270 		return error;
    271 
    272 	bsd_to_linux_statfs(&btmp, &ltmp);
    273 
    274 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    275 }
    276 
    277 int
    278 linux_sys_fstatfs(p, v, retval)
    279 	struct proc *p;
    280 	void *v;
    281 	register_t *retval;
    282 {
    283 	struct linux_sys_fstatfs_args /* {
    284 		syscallarg(int) fd;
    285 		syscallarg(struct linux_statfs *) sp;
    286 	} */ *uap = v;
    287 	struct statfs btmp, *bsp;
    288 	struct linux_statfs ltmp;
    289 	struct sys_fstatfs_args bsa;
    290 	caddr_t sg;
    291 	int error;
    292 
    293 	sg = stackgap_init(p->p_emul);
    294 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    295 
    296 	SCARG(&bsa, fd) = SCARG(uap, fd);
    297 	SCARG(&bsa, buf) = bsp;
    298 
    299 	if ((error = sys_fstatfs(p, &bsa, retval)))
    300 		return error;
    301 
    302 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    303 		return error;
    304 
    305 	bsd_to_linux_statfs(&btmp, &ltmp);
    306 
    307 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    308 }
    309 
    310 /*
    311  * uname(). Just copy the info from the various strings stored in the
    312  * kernel, and put it in the Linux utsname structure. That structure
    313  * is almost the same as the NetBSD one, only it has fields 65 characters
    314  * long, and an extra domainname field.
    315  */
    316 int
    317 linux_sys_uname(p, v, retval)
    318 	struct proc *p;
    319 	void *v;
    320 	register_t *retval;
    321 {
    322 	struct linux_sys_uname_args /* {
    323 		syscallarg(struct linux_utsname *) up;
    324 	} */ *uap = v;
    325 	extern char ostype[], hostname[], osrelease[], version[], machine[],
    326 	    domainname[];
    327 	struct linux_utsname luts;
    328 	int len;
    329 	char *cp;
    330 
    331 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
    332 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    333 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
    334 	strncpy(luts.l_version, version, sizeof(luts.l_version));
    335 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    336 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    337 
    338 	/* This part taken from the the uname() in libc */
    339 	len = sizeof(luts.l_version);
    340 	for (cp = luts.l_version; len--; ++cp) {
    341 		if (*cp == '\n' || *cp == '\t') {
    342 			if (len > 1)
    343 				*cp = ' ';
    344 			else
    345 				*cp = '\0';
    346 		}
    347 	}
    348 
    349 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    350 }
    351 
    352 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    353 /* Used indirectly on: arm, i386, m68k */
    354 
    355 /*
    356  * New type Linux mmap call.
    357  * Only called directly on machines with >= 6 free regs.
    358  */
    359 int
    360 linux_sys_mmap(p, v, retval)
    361 	struct proc *p;
    362 	void *v;
    363 	register_t *retval;
    364 {
    365 	struct linux_sys_mmap_args /* {
    366 		syscallarg(unsigned long) addr;
    367 		syscallarg(size_t) len;
    368 		syscallarg(int) prot;
    369 		syscallarg(int) flags;
    370 		syscallarg(int) fd;
    371 		syscallarg(off_t) offset;
    372 	} */ *uap = v;
    373 	struct sys_mmap_args cma;
    374 	int flags;
    375 
    376 	flags = 0;
    377 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
    378 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
    379 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
    380 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
    381 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    382 
    383 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
    384 	SCARG(&cma,len) = SCARG(uap, len);
    385 	SCARG(&cma,prot) = SCARG(uap, prot);
    386 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
    387 		SCARG(&cma,prot) |= VM_PROT_READ;
    388 	SCARG(&cma,flags) = flags;
    389 	SCARG(&cma,fd) = SCARG(uap, fd);
    390 	SCARG(&cma,pad) = 0;
    391 	SCARG(&cma,pos) = SCARG(uap, offset);
    392 
    393 	return sys_mmap(p, &cma, retval);
    394 }
    395 
    396 int
    397 linux_sys_mremap(p, v, retval)
    398 	struct proc *p;
    399 	void *v;
    400 	register_t *retval;
    401 {
    402 	struct linux_sys_mremap_args /* {
    403 		syscallarg(void *) old_address;
    404 		syscallarg(size_t) old_size;
    405 		syscallarg(size_t) new_size;
    406 		syscallarg(u_long) flags;
    407 	} */ *uap = v;
    408 	struct sys_munmap_args mua;
    409 	size_t old_size, new_size;
    410 	int error;
    411 
    412 	old_size = round_page(SCARG(uap, old_size));
    413 	new_size = round_page(SCARG(uap, new_size));
    414 
    415 	/*
    416 	 * Growing mapped region.
    417 	 */
    418 	if (new_size > old_size) {
    419 		/*
    420 		 * XXX Implement me.  What we probably want to do is
    421 		 * XXX dig out the guts of the old mapping, mmap that
    422 		 * XXX object again with the new size, then munmap
    423 		 * XXX the old mapping.
    424 		 */
    425 		*retval = 0;
    426 		return (ENOMEM);
    427 	}
    428 
    429 	/*
    430 	 * Shrinking mapped region.
    431 	 */
    432 	if (new_size < old_size) {
    433 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    434 		    new_size;
    435 		SCARG(&mua, len) = old_size - new_size;
    436 		error = sys_munmap(p, &mua, retval);
    437 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    438 		return (error);
    439 	}
    440 
    441 	/*
    442 	 * No change.
    443 	 */
    444 	*retval = (register_t)SCARG(uap, old_address);
    445 	return (0);
    446 }
    447 
    448 int
    449 linux_sys_msync(p, v, retval)
    450 	struct proc *p;
    451 	void *v;
    452 	register_t *retval;
    453 {
    454 	struct linux_sys_msync_args /* {
    455 		syscallarg(caddr_t) addr;
    456 		syscallarg(int) len;
    457 		syscallarg(int) fl;
    458 	} */ *uap = v;
    459 
    460 	struct sys___msync13_args bma;
    461 
    462 	/* flags are ignored */
    463 	SCARG(&bma, addr) = SCARG(uap, addr);
    464 	SCARG(&bma, len) = SCARG(uap, len);
    465 	SCARG(&bma, flags) = SCARG(uap, fl);
    466 
    467 	return sys___msync13(p, &bma, retval);
    468 }
    469 
    470 /*
    471  * This code is partly stolen from src/lib/libc/compat-43/times.c
    472  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    473  */
    474 
    475 #define CLK_TCK 100
    476 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    477 
    478 int
    479 linux_sys_times(p, v, retval)
    480 	struct proc *p;
    481 	void *v;
    482 	register_t *retval;
    483 {
    484 	struct linux_sys_times_args /* {
    485 		syscallarg(struct times *) tms;
    486 	} */ *uap = v;
    487 	struct timeval t;
    488 	struct linux_tms ltms;
    489 	struct rusage ru;
    490 	int error, s;
    491 
    492 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    493 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    494 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    495 
    496 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    497 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    498 
    499 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    500 		return error;
    501 
    502 	s = splclock();
    503 	timersub(&time, &boottime, &t);
    504 	splx(s);
    505 
    506 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    507 	return 0;
    508 }
    509 
    510 /*
    511  * Linux 'readdir' call. This code is mostly taken from the
    512  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    513  * an attempt has been made to keep it a little cleaner (failing
    514  * miserably, because of the cruft needed if count 1 is passed).
    515  *
    516  * The d_off field should contain the offset of the next valid entry,
    517  * but in Linux it has the offset of the entry itself. We emulate
    518  * that bug here.
    519  *
    520  * Read in BSD-style entries, convert them, and copy them out.
    521  *
    522  * Note that this doesn't handle union-mounted filesystems.
    523  */
    524 int
    525 linux_sys_getdents(p, v, retval)
    526 	struct proc *p;
    527 	void *v;
    528 	register_t *retval;
    529 {
    530 	struct linux_sys_getdents_args /* {
    531 		syscallarg(int) fd;
    532 		syscallarg(struct linux_dirent *) dent;
    533 		syscallarg(unsigned int) count;
    534 	} */ *uap = v;
    535 	register struct dirent *bdp;
    536 	struct vnode *vp;
    537 	caddr_t	inp, buf;		/* BSD-format */
    538 	int len, reclen;		/* BSD-format */
    539 	caddr_t outp;			/* Linux-format */
    540 	int resid, linux_reclen = 0;	/* Linux-format */
    541 	struct file *fp;
    542 	struct uio auio;
    543 	struct iovec aiov;
    544 	struct linux_dirent idb;
    545 	off_t off;		/* true file offset */
    546 	int buflen, error, eofflag, nbytes, oldcall;
    547 	struct vattr va;
    548 	off_t *cookiebuf = NULL, *cookie;
    549 	int ncookies;
    550 
    551 	/* getvnode() will use the descriptor for us */
    552 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    553 		return (error);
    554 
    555 	if ((fp->f_flag & FREAD) == 0) {
    556 		error = EBADF;
    557 		goto out1;
    558 	}
    559 
    560 	vp = (struct vnode *)fp->f_data;
    561 	if (vp->v_type != VDIR) {
    562 		error = EINVAL;
    563 		goto out1;
    564 	}
    565 
    566 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    567 		goto out1;
    568 
    569 	nbytes = SCARG(uap, count);
    570 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    571 		nbytes = sizeof (struct linux_dirent);
    572 		buflen = max(va.va_blocksize, nbytes);
    573 		oldcall = 1;
    574 	} else {
    575 		buflen = min(MAXBSIZE, nbytes);
    576 		if (buflen < va.va_blocksize)
    577 			buflen = va.va_blocksize;
    578 		oldcall = 0;
    579 	}
    580 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    581 
    582 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    583 	off = fp->f_offset;
    584 again:
    585 	aiov.iov_base = buf;
    586 	aiov.iov_len = buflen;
    587 	auio.uio_iov = &aiov;
    588 	auio.uio_iovcnt = 1;
    589 	auio.uio_rw = UIO_READ;
    590 	auio.uio_segflg = UIO_SYSSPACE;
    591 	auio.uio_procp = p;
    592 	auio.uio_resid = buflen;
    593 	auio.uio_offset = off;
    594 	/*
    595          * First we read into the malloc'ed buffer, then
    596          * we massage it into user space, one record at a time.
    597          */
    598 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    599 	    &ncookies);
    600 	if (error)
    601 		goto out;
    602 
    603 	inp = buf;
    604 	outp = (caddr_t)SCARG(uap, dent);
    605 	resid = nbytes;
    606 	if ((len = buflen - auio.uio_resid) == 0)
    607 		goto eof;
    608 
    609 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    610 		bdp = (struct dirent *)inp;
    611 		reclen = bdp->d_reclen;
    612 		if (reclen & 3)
    613 			panic("linux_readdir");
    614 		if (bdp->d_fileno == 0) {
    615 			inp += reclen;	/* it is a hole; squish it out */
    616 			off = *cookie++;
    617 			continue;
    618 		}
    619 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    620 		if (reclen > len || resid < linux_reclen) {
    621 			/* entry too big for buffer, so just stop */
    622 			outp++;
    623 			break;
    624 		}
    625 		/*
    626 		 * Massage in place to make a Linux-shaped dirent (otherwise
    627 		 * we have to worry about touching user memory outside of
    628 		 * the copyout() call).
    629 		 */
    630 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
    631 		/*
    632 		 * The old readdir() call misuses the offset and reclen fields.
    633 		 */
    634 		if (oldcall) {
    635 			idb.d_off = (linux_off_t)linux_reclen;
    636 			idb.d_reclen = (u_short)bdp->d_namlen;
    637 		} else {
    638 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
    639 				compat_offseterr(vp, "linux_getdents");
    640 				error = EINVAL;
    641 				goto out;
    642 			}
    643 			idb.d_off = (linux_off_t)off;
    644 			idb.d_reclen = (u_short)linux_reclen;
    645 		}
    646 		strcpy(idb.d_name, bdp->d_name);
    647 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    648 			goto out;
    649 		/* advance past this real entry */
    650 		inp += reclen;
    651 		off = *cookie++;	/* each entry points to itself */
    652 		/* advance output past Linux-shaped entry */
    653 		outp += linux_reclen;
    654 		resid -= linux_reclen;
    655 		if (oldcall)
    656 			break;
    657 	}
    658 
    659 	/* if we squished out the whole block, try again */
    660 	if (outp == (caddr_t)SCARG(uap, dent))
    661 		goto again;
    662 	fp->f_offset = off;	/* update the vnode offset */
    663 
    664 	if (oldcall)
    665 		nbytes = resid + linux_reclen;
    666 
    667 eof:
    668 	*retval = nbytes - resid;
    669 out:
    670 	VOP_UNLOCK(vp, 0);
    671 	if (cookiebuf)
    672 		free(cookiebuf, M_TEMP);
    673 	free(buf, M_TEMP);
    674  out1:
    675 	FILE_UNUSE(fp, p);
    676 	return error;
    677 }
    678 
    679 /*
    680  * Even when just using registers to pass arguments to syscalls you can
    681  * have 5 of them on the i386. So this newer version of select() does
    682  * this.
    683  */
    684 int
    685 linux_sys_select(p, v, retval)
    686 	struct proc *p;
    687 	void *v;
    688 	register_t *retval;
    689 {
    690 	struct linux_sys_select_args /* {
    691 		syscallarg(int) nfds;
    692 		syscallarg(fd_set *) readfds;
    693 		syscallarg(fd_set *) writefds;
    694 		syscallarg(fd_set *) exceptfds;
    695 		syscallarg(struct timeval *) timeout;
    696 	} */ *uap = v;
    697 
    698 	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    699 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    700 }
    701 
    702 /*
    703  * Common code for the old and new versions of select(). A couple of
    704  * things are important:
    705  * 1) return the amount of time left in the 'timeout' parameter
    706  * 2) select never returns ERESTART on Linux, always return EINTR
    707  */
    708 int
    709 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
    710 	struct proc *p;
    711 	register_t *retval;
    712 	int nfds;
    713 	fd_set *readfds, *writefds, *exceptfds;
    714 	struct timeval *timeout;
    715 {
    716 	struct sys_select_args bsa;
    717 	struct timeval tv0, tv1, utv, *tvp;
    718 	caddr_t sg;
    719 	int error;
    720 
    721 	SCARG(&bsa, nd) = nfds;
    722 	SCARG(&bsa, in) = readfds;
    723 	SCARG(&bsa, ou) = writefds;
    724 	SCARG(&bsa, ex) = exceptfds;
    725 	SCARG(&bsa, tv) = timeout;
    726 
    727 	/*
    728 	 * Store current time for computation of the amount of
    729 	 * time left.
    730 	 */
    731 	if (timeout) {
    732 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    733 			return error;
    734 		if (itimerfix(&utv)) {
    735 			/*
    736 			 * The timeval was invalid.  Convert it to something
    737 			 * valid that will act as it does under Linux.
    738 			 */
    739 			sg = stackgap_init(p->p_emul);
    740 			tvp = stackgap_alloc(&sg, sizeof(utv));
    741 			utv.tv_sec += utv.tv_usec / 1000000;
    742 			utv.tv_usec %= 1000000;
    743 			if (utv.tv_usec < 0) {
    744 				utv.tv_sec -= 1;
    745 				utv.tv_usec += 1000000;
    746 			}
    747 			if (utv.tv_sec < 0)
    748 				timerclear(&utv);
    749 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    750 				return error;
    751 			SCARG(&bsa, tv) = tvp;
    752 		}
    753 		microtime(&tv0);
    754 	}
    755 
    756 	error = sys_select(p, &bsa, retval);
    757 	if (error) {
    758 		/*
    759 		 * See fs/select.c in the Linux kernel.  Without this,
    760 		 * Maelstrom doesn't work.
    761 		 */
    762 		if (error == ERESTART)
    763 			error = EINTR;
    764 		return error;
    765 	}
    766 
    767 	if (timeout) {
    768 		if (*retval) {
    769 			/*
    770 			 * Compute how much time was left of the timeout,
    771 			 * by subtracting the current time and the time
    772 			 * before we started the call, and subtracting
    773 			 * that result from the user-supplied value.
    774 			 */
    775 			microtime(&tv1);
    776 			timersub(&tv1, &tv0, &tv1);
    777 			timersub(&utv, &tv1, &utv);
    778 			if (utv.tv_sec < 0)
    779 				timerclear(&utv);
    780 		} else
    781 			timerclear(&utv);
    782 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    783 			return error;
    784 	}
    785 
    786 	return 0;
    787 }
    788 
    789 /*
    790  * Get the process group of a certain process. Look it up
    791  * and return the value.
    792  */
    793 int
    794 linux_sys_getpgid(p, v, retval)
    795 	struct proc *p;
    796 	void *v;
    797 	register_t *retval;
    798 {
    799 	struct linux_sys_getpgid_args /* {
    800 		syscallarg(int) pid;
    801 	} */ *uap = v;
    802 	struct proc *targp;
    803 
    804 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    805 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    806 			return ESRCH;
    807 	}
    808 	else
    809 		targp = p;
    810 
    811 	retval[0] = targp->p_pgid;
    812 	return 0;
    813 }
    814 
    815 /*
    816  * Set the 'personality' (emulation mode) for the current process. Only
    817  * accept the Linux personality here (0). This call is needed because
    818  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    819  * ELF binaries run in Linux mode, not SVR4 mode.
    820  */
    821 int
    822 linux_sys_personality(p, v, retval)
    823 	struct proc *p;
    824 	void *v;
    825 	register_t *retval;
    826 {
    827 	struct linux_sys_personality_args /* {
    828 		syscallarg(int) per;
    829 	} */ *uap = v;
    830 
    831 	if (SCARG(uap, per) != 0)
    832 		return EINVAL;
    833 	retval[0] = 0;
    834 	return 0;
    835 }
    836 
    837 /*
    838  * The calls are here because of type conversions.
    839  */
    840 int
    841 linux_sys_setreuid(p, v, retval)
    842 	struct proc *p;
    843 	void *v;
    844 	register_t *retval;
    845 {
    846 	struct linux_sys_setreuid_args /* {
    847 		syscallarg(int) ruid;
    848 		syscallarg(int) euid;
    849 	} */ *uap = v;
    850 	struct sys_setreuid_args bsa;
    851 
    852 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    853 		(uid_t)-1 : SCARG(uap, ruid);
    854 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    855 		(uid_t)-1 : SCARG(uap, euid);
    856 
    857 	return sys_setreuid(p, &bsa, retval);
    858 }
    859 
    860 int
    861 linux_sys_setregid(p, v, retval)
    862 	struct proc *p;
    863 	void *v;
    864 	register_t *retval;
    865 {
    866 	struct linux_sys_setregid_args /* {
    867 		syscallarg(int) rgid;
    868 		syscallarg(int) egid;
    869 	} */ *uap = v;
    870 	struct sys_setregid_args bsa;
    871 
    872 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    873 		(uid_t)-1 : SCARG(uap, rgid);
    874 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    875 		(uid_t)-1 : SCARG(uap, egid);
    876 
    877 	return sys_setregid(p, &bsa, retval);
    878 }
    879 
    880 int
    881 linux_sys___sysctl(p, v, retval)
    882 	struct proc *p;
    883 	void *v;
    884 	register_t *retval;
    885 {
    886 	struct linux_sys___sysctl_args /* {
    887 		syscallarg(struct linux___sysctl *) lsp;
    888 	} */ *uap = v;
    889 	struct linux___sysctl ls;
    890 	struct sys___sysctl_args bsa;
    891 	int error;
    892 
    893 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
    894 		return error;
    895 	SCARG(&bsa, name) = ls.name;
    896 	SCARG(&bsa, namelen) = ls.namelen;
    897 	SCARG(&bsa, old) = ls.old;
    898 	SCARG(&bsa, oldlenp) = ls.oldlenp;
    899 	SCARG(&bsa, new) = ls.new;
    900 	SCARG(&bsa, newlen) = ls.newlen;
    901 
    902 	return sys___sysctl(p, &bsa, retval);
    903 }
    904