Home | History | Annotate | Line # | Download | only in common
linux_misc.c revision 1.83.2.1
      1 /*	$NetBSD: linux_misc.c,v 1.83.2.1 2001/03/05 22:49:25 nathanw Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 1999 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; by Jason R. Thorpe
      9  * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Linux compatibility module. Try to deal with various Linux system calls.
     42  */
     43 
     44 /*
     45  * These functions have been moved to multiarch to allow
     46  * selection of which machines include them to be
     47  * determined by the individual files.linux_<arch> files.
     48  *
     49  * Function in multiarch:
     50  *	linux_sys_break			: linux_break.c
     51  *	linux_sys_alarm			: linux_misc_notalpha.c
     52  *	linux_sys_getresgid		: linux_misc_notalpha.c
     53  *	linux_sys_nice			: linux_misc_notalpha.c
     54  *	linux_sys_readdir		: linux_misc_notalpha.c
     55  *	linux_sys_setresgid		: linux_misc_notalpha.c
     56  *	linux_sys_time			: linux_misc_notalpha.c
     57  *	linux_sys_utime			: linux_misc_notalpha.c
     58  *	linux_sys_waitpid		: linux_misc_notalpha.c
     59  *	linux_sys_old_mmap		: linux_oldmmap.c
     60  *	linux_sys_oldolduname		: linux_oldolduname.c
     61  *	linux_sys_oldselect		: linux_oldselect.c
     62  *	linux_sys_olduname		: linux_olduname.c
     63  *	linux_sys_pipe			: linux_pipe.c
     64  */
     65 
     66 #include <sys/param.h>
     67 #include <sys/systm.h>
     68 #include <sys/namei.h>
     69 #include <sys/lwp.h>
     70 #include <sys/proc.h>
     71 #include <sys/dirent.h>
     72 #include <sys/file.h>
     73 #include <sys/stat.h>
     74 #include <sys/filedesc.h>
     75 #include <sys/ioctl.h>
     76 #include <sys/kernel.h>
     77 #include <sys/malloc.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/mman.h>
     80 #include <sys/mount.h>
     81 #include <sys/reboot.h>
     82 #include <sys/resource.h>
     83 #include <sys/resourcevar.h>
     84 #include <sys/signal.h>
     85 #include <sys/signalvar.h>
     86 #include <sys/socket.h>
     87 #include <sys/time.h>
     88 #include <sys/times.h>
     89 #include <sys/vnode.h>
     90 #include <sys/uio.h>
     91 #include <sys/wait.h>
     92 #include <sys/utsname.h>
     93 #include <sys/unistd.h>
     94 #include <sys/swap.h>		/* for SWAP_ON */
     95 #include <sys/sysctl.h>		/* for KERN_DOMAINNAME */
     96 
     97 #include <sys/ptrace.h>
     98 #include <machine/ptrace.h>
     99 
    100 #include <sys/syscallargs.h>
    101 
    102 #include <compat/linux/common/linux_types.h>
    103 #include <compat/linux/common/linux_signal.h>
    104 
    105 #include <compat/linux/linux_syscallargs.h>
    106 
    107 #include <compat/linux/common/linux_fcntl.h>
    108 #include <compat/linux/common/linux_mmap.h>
    109 #include <compat/linux/common/linux_dirent.h>
    110 #include <compat/linux/common/linux_util.h>
    111 #include <compat/linux/common/linux_misc.h>
    112 #include <compat/linux/common/linux_ptrace.h>
    113 #include <compat/linux/common/linux_reboot.h>
    114 
    115 const int linux_ptrace_request_map[] = {
    116 	LINUX_PTRACE_TRACEME,	PT_TRACE_ME,
    117 	LINUX_PTRACE_PEEKTEXT,	PT_READ_I,
    118 	LINUX_PTRACE_PEEKDATA,	PT_READ_D,
    119 	LINUX_PTRACE_POKETEXT,	PT_WRITE_I,
    120 	LINUX_PTRACE_POKEDATA,	PT_WRITE_D,
    121 	LINUX_PTRACE_CONT,	PT_CONTINUE,
    122 	LINUX_PTRACE_KILL,	PT_KILL,
    123 	LINUX_PTRACE_ATTACH,	PT_ATTACH,
    124 	LINUX_PTRACE_DETACH,	PT_DETACH,
    125 #ifdef PT_STEP
    126 	LINUX_PTRACE_SINGLESTEP,	PT_STEP,
    127 #endif
    128 	-1
    129 };
    130 
    131 /* Local linux_misc.c functions: */
    132 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
    133 
    134 /*
    135  * The information on a terminated (or stopped) process needs
    136  * to be converted in order for Linux binaries to get a valid signal
    137  * number out of it.
    138  */
    139 void
    140 bsd_to_linux_wstat(st)
    141 	int *st;
    142 {
    143 
    144 	int sig;
    145 
    146 	if (WIFSIGNALED(*st)) {
    147 		sig = WTERMSIG(*st);
    148 		if (sig >= 0 && sig < NSIG)
    149 			*st= (*st& ~0177) | native_to_linux_sig[sig];
    150 	} else if (WIFSTOPPED(*st)) {
    151 		sig = WSTOPSIG(*st);
    152 		if (sig >= 0 && sig < NSIG)
    153 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
    154 	}
    155 }
    156 
    157 /*
    158  * This is very much the same as waitpid()
    159  */
    160 int
    161 linux_sys_wait4(l, v, retval)
    162 	struct lwp *l;
    163 	void *v;
    164 	register_t *retval;
    165 {
    166 	struct linux_sys_wait4_args /* {
    167 		syscallarg(int) pid;
    168 		syscallarg(int *) status;
    169 		syscallarg(int) options;
    170 		syscallarg(struct rusage *) rusage;
    171 	} */ *uap = v;
    172 	struct proc *p = l->l_proc;
    173 	struct sys_wait4_args w4a;
    174 	int error, *status, tstat, options, linux_options;
    175 	caddr_t sg;
    176 
    177 	if (SCARG(uap, status) != NULL) {
    178 		sg = stackgap_init(p->p_emul);
    179 		status = (int *) stackgap_alloc(&sg, sizeof *status);
    180 	} else
    181 		status = NULL;
    182 
    183 	linux_options = SCARG(uap, options);
    184 	options = 0;
    185 	if (linux_options &
    186 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
    187 		return (EINVAL);
    188 
    189 	if (linux_options & LINUX_WAIT4_WNOHANG)
    190 		options |= WNOHANG;
    191 	if (linux_options & LINUX_WAIT4_WUNTRACED)
    192 		options |= WUNTRACED;
    193 	if (linux_options & LINUX_WAIT4_WCLONE)
    194 		options |= WALTSIG;
    195 
    196 	SCARG(&w4a, pid) = SCARG(uap, pid);
    197 	SCARG(&w4a, status) = status;
    198 	SCARG(&w4a, options) = options;
    199 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
    200 
    201 	if ((error = sys_wait4(l, &w4a, retval)))
    202 		return error;
    203 
    204 	sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
    205 
    206 	if (status != NULL) {
    207 		if ((error = copyin(status, &tstat, sizeof tstat)))
    208 			return error;
    209 
    210 		bsd_to_linux_wstat(&tstat);
    211 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
    212 	}
    213 
    214 	return 0;
    215 }
    216 
    217 /*
    218  * Linux brk(2). The check if the new address is >= the old one is
    219  * done in the kernel in Linux. NetBSD does it in the library.
    220  */
    221 int
    222 linux_sys_brk(l, v, retval)
    223 	struct lwp *l;
    224 	void *v;
    225 	register_t *retval;
    226 {
    227 	struct linux_sys_brk_args /* {
    228 		syscallarg(char *) nsize;
    229 	} */ *uap = v;
    230 	struct proc *p = l->l_proc;
    231 	char *nbrk = SCARG(uap, nsize);
    232 	struct sys_obreak_args oba;
    233 	struct vmspace *vm = p->p_vmspace;
    234 	caddr_t oldbrk;
    235 
    236 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
    237 	/*
    238 	 * XXX inconsistent.. Linux always returns at least the old
    239 	 * brk value, but it will be page-aligned if this fails,
    240 	 * and possibly not page aligned if it succeeds (the user
    241 	 * supplied pointer is returned).
    242 	 */
    243 	SCARG(&oba, nsize) = nbrk;
    244 
    245 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(l, &oba, retval) == 0)
    246 		retval[0] = (register_t)nbrk;
    247 	else
    248 		retval[0] = (register_t)oldbrk;
    249 
    250 	return 0;
    251 }
    252 
    253 /*
    254  * Convert BSD statfs structure to Linux statfs structure.
    255  * The Linux structure has less fields, and it also wants
    256  * the length of a name in a dir entry in a field, which
    257  * we fake (probably the wrong way).
    258  */
    259 static void
    260 bsd_to_linux_statfs(bsp, lsp)
    261 	struct statfs *bsp;
    262 	struct linux_statfs *lsp;
    263 {
    264 
    265 	lsp->l_ftype = bsp->f_type;
    266 	lsp->l_fbsize = bsp->f_bsize;
    267 	lsp->l_fblocks = bsp->f_blocks;
    268 	lsp->l_fbfree = bsp->f_bfree;
    269 	lsp->l_fbavail = bsp->f_bavail;
    270 	lsp->l_ffiles = bsp->f_files;
    271 	lsp->l_fffree = bsp->f_ffree;
    272 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
    273 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
    274 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
    275 }
    276 
    277 /*
    278  * Implement the fs stat functions. Straightforward.
    279  */
    280 int
    281 linux_sys_statfs(l, v, retval)
    282 	struct lwp *l;
    283 	void *v;
    284 	register_t *retval;
    285 {
    286 	struct linux_sys_statfs_args /* {
    287 		syscallarg(const char *) path;
    288 		syscallarg(struct linux_statfs *) sp;
    289 	} */ *uap = v;
    290 	struct proc *p = l->l_proc;
    291 	struct statfs btmp, *bsp;
    292 	struct linux_statfs ltmp;
    293 	struct sys_statfs_args bsa;
    294 	caddr_t sg;
    295 	int error;
    296 
    297 	sg = stackgap_init(p->p_emul);
    298 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    299 
    300 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
    301 
    302 	SCARG(&bsa, path) = SCARG(uap, path);
    303 	SCARG(&bsa, buf) = bsp;
    304 
    305 	if ((error = sys_statfs(l, &bsa, retval)))
    306 		return error;
    307 
    308 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    309 		return error;
    310 
    311 	bsd_to_linux_statfs(&btmp, &ltmp);
    312 
    313 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    314 }
    315 
    316 int
    317 linux_sys_fstatfs(l, v, retval)
    318 	struct lwp *l;
    319 	void *v;
    320 	register_t *retval;
    321 {
    322 	struct linux_sys_fstatfs_args /* {
    323 		syscallarg(int) fd;
    324 		syscallarg(struct linux_statfs *) sp;
    325 	} */ *uap = v;
    326 	struct proc *p = l->l_proc;
    327 	struct statfs btmp, *bsp;
    328 	struct linux_statfs ltmp;
    329 	struct sys_fstatfs_args bsa;
    330 	caddr_t sg;
    331 	int error;
    332 
    333 	sg = stackgap_init(p->p_emul);
    334 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
    335 
    336 	SCARG(&bsa, fd) = SCARG(uap, fd);
    337 	SCARG(&bsa, buf) = bsp;
    338 
    339 	if ((error = sys_fstatfs(l, &bsa, retval)))
    340 		return error;
    341 
    342 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
    343 		return error;
    344 
    345 	bsd_to_linux_statfs(&btmp, &ltmp);
    346 
    347 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
    348 }
    349 
    350 char linux_sysname[] = "Linux";
    351 char linux_release[] = "2.0.38";
    352 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
    353 
    354 /*
    355  * uname(). Just copy the info from the various strings stored in the
    356  * kernel, and put it in the Linux utsname structure. That structure
    357  * is almost the same as the NetBSD one, only it has fields 65 characters
    358  * long, and an extra domainname field.
    359  */
    360 int
    361 linux_sys_uname(l, v, retval)
    362 	struct lwp *l;
    363 	void *v;
    364 	register_t *retval;
    365 {
    366 	struct linux_sys_uname_args /* {
    367 		syscallarg(struct linux_utsname *) up;
    368 	} */ *uap = v;
    369 	struct linux_utsname luts;
    370 
    371 	strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
    372 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
    373 	strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
    374 	strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
    375 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
    376 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
    377 
    378 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
    379 }
    380 
    381 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
    382 /* Used indirectly on: arm, i386, m68k */
    383 
    384 /*
    385  * New type Linux mmap call.
    386  * Only called directly on machines with >= 6 free regs.
    387  */
    388 int
    389 linux_sys_mmap(l, v, retval)
    390 	struct lwp *l;
    391 	void *v;
    392 	register_t *retval;
    393 {
    394 	struct linux_sys_mmap_args /* {
    395 		syscallarg(unsigned long) addr;
    396 		syscallarg(size_t) len;
    397 		syscallarg(int) prot;
    398 		syscallarg(int) flags;
    399 		syscallarg(int) fd;
    400 		syscallarg(off_t) offset;
    401 	} */ *uap = v;
    402 	struct sys_mmap_args cma;
    403 	int flags;
    404 
    405 	flags = 0;
    406 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
    407 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
    408 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
    409 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
    410 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
    411 
    412 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
    413 	SCARG(&cma,len) = SCARG(uap, len);
    414 	SCARG(&cma,prot) = SCARG(uap, prot);
    415 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
    416 		SCARG(&cma,prot) |= VM_PROT_READ;
    417 	SCARG(&cma,flags) = flags;
    418 	SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
    419 	SCARG(&cma,pad) = 0;
    420 	SCARG(&cma,pos) = SCARG(uap, offset);
    421 
    422 	return sys_mmap(l, &cma, retval);
    423 }
    424 
    425 int
    426 linux_sys_mremap(l, v, retval)
    427 	struct lwp *l;
    428 	void *v;
    429 	register_t *retval;
    430 {
    431 	struct linux_sys_mremap_args /* {
    432 		syscallarg(void *) old_address;
    433 		syscallarg(size_t) old_size;
    434 		syscallarg(size_t) new_size;
    435 		syscallarg(u_long) flags;
    436 	} */ *uap = v;
    437 	struct sys_munmap_args mua;
    438 	size_t old_size, new_size;
    439 	int error;
    440 
    441 	old_size = round_page(SCARG(uap, old_size));
    442 	new_size = round_page(SCARG(uap, new_size));
    443 
    444 	/*
    445 	 * Growing mapped region.
    446 	 */
    447 	if (new_size > old_size) {
    448 		/*
    449 		 * XXX Implement me.  What we probably want to do is
    450 		 * XXX dig out the guts of the old mapping, mmap that
    451 		 * XXX object again with the new size, then munmap
    452 		 * XXX the old mapping.
    453 		 */
    454 		*retval = 0;
    455 		return (ENOMEM);
    456 	}
    457 
    458 	/*
    459 	 * Shrinking mapped region.
    460 	 */
    461 	if (new_size < old_size) {
    462 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
    463 		    new_size;
    464 		SCARG(&mua, len) = old_size - new_size;
    465 		error = sys_munmap(l, &mua, retval);
    466 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
    467 		return (error);
    468 	}
    469 
    470 	/*
    471 	 * No change.
    472 	 */
    473 	*retval = (register_t)SCARG(uap, old_address);
    474 	return (0);
    475 }
    476 
    477 int
    478 linux_sys_msync(l, v, retval)
    479 	struct lwp *l;
    480 	void *v;
    481 	register_t *retval;
    482 {
    483 	struct linux_sys_msync_args /* {
    484 		syscallarg(caddr_t) addr;
    485 		syscallarg(int) len;
    486 		syscallarg(int) fl;
    487 	} */ *uap = v;
    488 
    489 	struct sys___msync13_args bma;
    490 
    491 	/* flags are ignored */
    492 	SCARG(&bma, addr) = SCARG(uap, addr);
    493 	SCARG(&bma, len) = SCARG(uap, len);
    494 	SCARG(&bma, flags) = SCARG(uap, fl);
    495 
    496 	return sys___msync13(l, &bma, retval);
    497 }
    498 
    499 /*
    500  * This code is partly stolen from src/lib/libc/compat-43/times.c
    501  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
    502  */
    503 
    504 #define CLK_TCK 100
    505 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
    506 
    507 int
    508 linux_sys_times(l, v, retval)
    509 	struct lwp *l;
    510 	void *v;
    511 	register_t *retval;
    512 {
    513 	struct linux_sys_times_args /* {
    514 		syscallarg(struct times *) tms;
    515 	} */ *uap = v;
    516 	struct proc *p = l->l_proc;
    517 	struct timeval t;
    518 	struct linux_tms ltms;
    519 	struct rusage ru;
    520 	int error, s;
    521 
    522 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
    523 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
    524 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
    525 
    526 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
    527 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
    528 
    529 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
    530 		return error;
    531 
    532 	s = splclock();
    533 	timersub(&time, &boottime, &t);
    534 	splx(s);
    535 
    536 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
    537 	return 0;
    538 }
    539 
    540 /*
    541  * Linux 'readdir' call. This code is mostly taken from the
    542  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
    543  * an attempt has been made to keep it a little cleaner (failing
    544  * miserably, because of the cruft needed if count 1 is passed).
    545  *
    546  * The d_off field should contain the offset of the next valid entry,
    547  * but in Linux it has the offset of the entry itself. We emulate
    548  * that bug here.
    549  *
    550  * Read in BSD-style entries, convert them, and copy them out.
    551  *
    552  * Note that this doesn't handle union-mounted filesystems.
    553  */
    554 int
    555 linux_sys_getdents(l, v, retval)
    556 	struct lwp *l;
    557 	void *v;
    558 	register_t *retval;
    559 {
    560 	struct linux_sys_getdents_args /* {
    561 		syscallarg(int) fd;
    562 		syscallarg(struct linux_dirent *) dent;
    563 		syscallarg(unsigned int) count;
    564 	} */ *uap = v;
    565 	struct proc *p = l->l_proc;
    566 	struct dirent *bdp;
    567 	struct vnode *vp;
    568 	caddr_t	inp, buf;		/* BSD-format */
    569 	int len, reclen;		/* BSD-format */
    570 	caddr_t outp;			/* Linux-format */
    571 	int resid, linux_reclen = 0;	/* Linux-format */
    572 	struct file *fp;
    573 	struct uio auio;
    574 	struct iovec aiov;
    575 	struct linux_dirent idb;
    576 	off_t off;		/* true file offset */
    577 	int buflen, error, eofflag, nbytes, oldcall;
    578 	struct vattr va;
    579 	off_t *cookiebuf = NULL, *cookie;
    580 	int ncookies;
    581 
    582 	/* getvnode() will use the descriptor for us */
    583 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
    584 		return (error);
    585 
    586 	if ((fp->f_flag & FREAD) == 0) {
    587 		error = EBADF;
    588 		goto out1;
    589 	}
    590 
    591 	vp = (struct vnode *)fp->f_data;
    592 	if (vp->v_type != VDIR) {
    593 		error = EINVAL;
    594 		goto out1;
    595 	}
    596 
    597 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
    598 		goto out1;
    599 
    600 	nbytes = SCARG(uap, count);
    601 	if (nbytes == 1) {	/* emulating old, broken behaviour */
    602 		nbytes = sizeof (struct linux_dirent);
    603 		buflen = max(va.va_blocksize, nbytes);
    604 		oldcall = 1;
    605 	} else {
    606 		buflen = min(MAXBSIZE, nbytes);
    607 		if (buflen < va.va_blocksize)
    608 			buflen = va.va_blocksize;
    609 		oldcall = 0;
    610 	}
    611 	buf = malloc(buflen, M_TEMP, M_WAITOK);
    612 
    613 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    614 	off = fp->f_offset;
    615 again:
    616 	aiov.iov_base = buf;
    617 	aiov.iov_len = buflen;
    618 	auio.uio_iov = &aiov;
    619 	auio.uio_iovcnt = 1;
    620 	auio.uio_rw = UIO_READ;
    621 	auio.uio_segflg = UIO_SYSSPACE;
    622 	auio.uio_procp = p;
    623 	auio.uio_resid = buflen;
    624 	auio.uio_offset = off;
    625 	/*
    626          * First we read into the malloc'ed buffer, then
    627          * we massage it into user space, one record at a time.
    628          */
    629 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
    630 	    &ncookies);
    631 	if (error)
    632 		goto out;
    633 
    634 	inp = buf;
    635 	outp = (caddr_t)SCARG(uap, dent);
    636 	resid = nbytes;
    637 	if ((len = buflen - auio.uio_resid) == 0)
    638 		goto eof;
    639 
    640 	for (cookie = cookiebuf; len > 0; len -= reclen) {
    641 		bdp = (struct dirent *)inp;
    642 		reclen = bdp->d_reclen;
    643 		if (reclen & 3)
    644 			panic("linux_readdir");
    645 		if (bdp->d_fileno == 0) {
    646 			inp += reclen;	/* it is a hole; squish it out */
    647 			off = *cookie++;
    648 			continue;
    649 		}
    650 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
    651 		if (reclen > len || resid < linux_reclen) {
    652 			/* entry too big for buffer, so just stop */
    653 			outp++;
    654 			break;
    655 		}
    656 		/*
    657 		 * Massage in place to make a Linux-shaped dirent (otherwise
    658 		 * we have to worry about touching user memory outside of
    659 		 * the copyout() call).
    660 		 */
    661 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
    662 		/*
    663 		 * The old readdir() call misuses the offset and reclen fields.
    664 		 */
    665 		if (oldcall) {
    666 			idb.d_off = (linux_off_t)linux_reclen;
    667 			idb.d_reclen = (u_short)bdp->d_namlen;
    668 		} else {
    669 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
    670 				compat_offseterr(vp, "linux_getdents");
    671 				error = EINVAL;
    672 				goto out;
    673 			}
    674 			idb.d_off = (linux_off_t)off;
    675 			idb.d_reclen = (u_short)linux_reclen;
    676 		}
    677 		strcpy(idb.d_name, bdp->d_name);
    678 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
    679 			goto out;
    680 		/* advance past this real entry */
    681 		inp += reclen;
    682 		off = *cookie++;	/* each entry points to itself */
    683 		/* advance output past Linux-shaped entry */
    684 		outp += linux_reclen;
    685 		resid -= linux_reclen;
    686 		if (oldcall)
    687 			break;
    688 	}
    689 
    690 	/* if we squished out the whole block, try again */
    691 	if (outp == (caddr_t)SCARG(uap, dent))
    692 		goto again;
    693 	fp->f_offset = off;	/* update the vnode offset */
    694 
    695 	if (oldcall)
    696 		nbytes = resid + linux_reclen;
    697 
    698 eof:
    699 	*retval = nbytes - resid;
    700 out:
    701 	VOP_UNLOCK(vp, 0);
    702 	if (cookiebuf)
    703 		free(cookiebuf, M_TEMP);
    704 	free(buf, M_TEMP);
    705  out1:
    706 	FILE_UNUSE(fp, p);
    707 	return error;
    708 }
    709 
    710 /*
    711  * Even when just using registers to pass arguments to syscalls you can
    712  * have 5 of them on the i386. So this newer version of select() does
    713  * this.
    714  */
    715 int
    716 linux_sys_select(l, v, retval)
    717 	struct lwp *l;
    718 	void *v;
    719 	register_t *retval;
    720 {
    721 	struct linux_sys_select_args /* {
    722 		syscallarg(int) nfds;
    723 		syscallarg(fd_set *) readfds;
    724 		syscallarg(fd_set *) writefds;
    725 		syscallarg(fd_set *) exceptfds;
    726 		syscallarg(struct timeval *) timeout;
    727 	} */ *uap = v;
    728 
    729 	return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
    730 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
    731 }
    732 
    733 /*
    734  * Common code for the old and new versions of select(). A couple of
    735  * things are important:
    736  * 1) return the amount of time left in the 'timeout' parameter
    737  * 2) select never returns ERESTART on Linux, always return EINTR
    738  */
    739 int
    740 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
    741 	struct lwp *l;
    742 	register_t *retval;
    743 	int nfds;
    744 	fd_set *readfds, *writefds, *exceptfds;
    745 	struct timeval *timeout;
    746 {
    747 	struct sys_select_args bsa;
    748 	struct proc *p = l->l_proc;
    749 	struct timeval tv0, tv1, utv, *tvp;
    750 	caddr_t sg;
    751 	int error;
    752 
    753 	SCARG(&bsa, nd) = nfds;
    754 	SCARG(&bsa, in) = readfds;
    755 	SCARG(&bsa, ou) = writefds;
    756 	SCARG(&bsa, ex) = exceptfds;
    757 	SCARG(&bsa, tv) = timeout;
    758 
    759 	/*
    760 	 * Store current time for computation of the amount of
    761 	 * time left.
    762 	 */
    763 	if (timeout) {
    764 		if ((error = copyin(timeout, &utv, sizeof(utv))))
    765 			return error;
    766 		if (itimerfix(&utv)) {
    767 			/*
    768 			 * The timeval was invalid.  Convert it to something
    769 			 * valid that will act as it does under Linux.
    770 			 */
    771 			sg = stackgap_init(p->p_emul);
    772 			tvp = stackgap_alloc(&sg, sizeof(utv));
    773 			utv.tv_sec += utv.tv_usec / 1000000;
    774 			utv.tv_usec %= 1000000;
    775 			if (utv.tv_usec < 0) {
    776 				utv.tv_sec -= 1;
    777 				utv.tv_usec += 1000000;
    778 			}
    779 			if (utv.tv_sec < 0)
    780 				timerclear(&utv);
    781 			if ((error = copyout(&utv, tvp, sizeof(utv))))
    782 				return error;
    783 			SCARG(&bsa, tv) = tvp;
    784 		}
    785 		microtime(&tv0);
    786 	}
    787 
    788 	error = sys_select(l, &bsa, retval);
    789 	if (error) {
    790 		/*
    791 		 * See fs/select.c in the Linux kernel.  Without this,
    792 		 * Maelstrom doesn't work.
    793 		 */
    794 		if (error == ERESTART)
    795 			error = EINTR;
    796 		return error;
    797 	}
    798 
    799 	if (timeout) {
    800 		if (*retval) {
    801 			/*
    802 			 * Compute how much time was left of the timeout,
    803 			 * by subtracting the current time and the time
    804 			 * before we started the call, and subtracting
    805 			 * that result from the user-supplied value.
    806 			 */
    807 			microtime(&tv1);
    808 			timersub(&tv1, &tv0, &tv1);
    809 			timersub(&utv, &tv1, &utv);
    810 			if (utv.tv_sec < 0)
    811 				timerclear(&utv);
    812 		} else
    813 			timerclear(&utv);
    814 		if ((error = copyout(&utv, timeout, sizeof(utv))))
    815 			return error;
    816 	}
    817 
    818 	return 0;
    819 }
    820 
    821 /*
    822  * Get the process group of a certain process. Look it up
    823  * and return the value.
    824  */
    825 int
    826 linux_sys_getpgid(l, v, retval)
    827 	struct lwp *l;
    828 	void *v;
    829 	register_t *retval;
    830 {
    831 	struct linux_sys_getpgid_args /* {
    832 		syscallarg(int) pid;
    833 	} */ *uap = v;
    834 	struct proc *p = l->l_proc;
    835 	struct proc *targp;
    836 
    837 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
    838 		if ((targp = pfind(SCARG(uap, pid))) == 0)
    839 			return ESRCH;
    840 	}
    841 	else
    842 		targp = p;
    843 
    844 	retval[0] = targp->p_pgid;
    845 	return 0;
    846 }
    847 
    848 /*
    849  * Set the 'personality' (emulation mode) for the current process. Only
    850  * accept the Linux personality here (0). This call is needed because
    851  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
    852  * ELF binaries run in Linux mode, not SVR4 mode.
    853  */
    854 int
    855 linux_sys_personality(l, v, retval)
    856 	struct lwp *l;
    857 	void *v;
    858 	register_t *retval;
    859 {
    860 	struct linux_sys_personality_args /* {
    861 		syscallarg(int) per;
    862 	} */ *uap = v;
    863 
    864 	if (SCARG(uap, per) != 0)
    865 		return EINVAL;
    866 	retval[0] = 0;
    867 	return 0;
    868 }
    869 
    870 #if defined(__i386__) || defined(__m68k__)
    871 /*
    872  * The calls are here because of type conversions.
    873  */
    874 int
    875 linux_sys_setreuid16(l, v, retval)
    876 	struct lwp *l;
    877 	void *v;
    878 	register_t *retval;
    879 {
    880 	struct linux_sys_setreuid16_args /* {
    881 		syscallarg(int) ruid;
    882 		syscallarg(int) euid;
    883 	} */ *uap = v;
    884 	struct sys_setreuid_args bsa;
    885 
    886 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    887 		(uid_t)-1 : SCARG(uap, ruid);
    888 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    889 		(uid_t)-1 : SCARG(uap, euid);
    890 
    891 	return sys_setreuid(l, &bsa, retval);
    892 }
    893 
    894 int
    895 linux_sys_setregid16(l, v, retval)
    896 	struct lwp *l;
    897 	void *v;
    898 	register_t *retval;
    899 {
    900 	struct linux_sys_setregid16_args /* {
    901 		syscallarg(int) rgid;
    902 		syscallarg(int) egid;
    903 	} */ *uap = v;
    904 	struct sys_setregid_args bsa;
    905 
    906 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    907 		(uid_t)-1 : SCARG(uap, rgid);
    908 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    909 		(uid_t)-1 : SCARG(uap, egid);
    910 
    911 	return sys_setregid(l, &bsa, retval);
    912 }
    913 
    914 int
    915 linux_sys_setresuid16(l, v, retval)
    916 	struct lwp *l;
    917 	void *v;
    918 	register_t *retval;
    919 {
    920 	struct linux_sys_setresuid16_args /* {
    921 		syscallarg(uid_t) ruid;
    922 		syscallarg(uid_t) euid;
    923 		syscallarg(uid_t) suid;
    924 	} */ *uap = v;
    925 	struct linux_sys_setresuid16_args lsa;
    926 
    927 	SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
    928 		(uid_t)-1 : SCARG(uap, ruid);
    929 	SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
    930 		(uid_t)-1 : SCARG(uap, euid);
    931 	SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
    932 		(uid_t)-1 : SCARG(uap, suid);
    933 
    934 	return linux_sys_setresuid(l, &lsa, retval);
    935 }
    936 
    937 int
    938 linux_sys_setresgid16(l, v, retval)
    939 	struct lwp *l;
    940 	void *v;
    941 	register_t *retval;
    942 {
    943 	struct linux_sys_setresgid16_args /* {
    944 		syscallarg(gid_t) rgid;
    945 		syscallarg(gid_t) egid;
    946 		syscallarg(gid_t) sgid;
    947 	} */ *uap = v;
    948 	struct linux_sys_setresgid16_args lsa;
    949 
    950 	SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
    951 		(gid_t)-1 : SCARG(uap, rgid);
    952 	SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
    953 		(gid_t)-1 : SCARG(uap, egid);
    954 	SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
    955 		(gid_t)-1 : SCARG(uap, sgid);
    956 
    957 	return linux_sys_setresgid(l, &lsa, retval);
    958 }
    959 
    960 int
    961 linux_sys_getgroups16(l, v, retval)
    962 	struct lwp *l;
    963 	void *v;
    964 	register_t *retval;
    965 {
    966 	struct linux_sys_getgroups16_args /* {
    967 		syscallarg(int) gidsetsize;
    968 		syscallarg(linux_gid_t *) gidset;
    969 	} */ *uap = v;
    970 	struct proc *p = l->l_proc;
    971 	caddr_t sg;
    972 	int n, error, i;
    973 	struct sys_getgroups_args bsa;
    974 	gid_t *bset, *kbset;
    975 	linux_gid_t *lset;
    976 	struct pcred *pc = p->p_cred;
    977 
    978 	n = SCARG(uap, gidsetsize);
    979 	if (n < 0)
    980 		return EINVAL;
    981 	error = 0;
    982 	bset = kbset = NULL;
    983 	lset = NULL;
    984 	if (n > 0) {
    985 		n = min(pc->pc_ucred->cr_ngroups, n);
    986 		sg = stackgap_init(p->p_emul);
    987 		bset = stackgap_alloc(&sg, n * sizeof (gid_t));
    988 		kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
    989 		lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
    990 		if (bset == NULL || kbset == NULL || lset == NULL)
    991 			return ENOMEM;
    992 		SCARG(&bsa, gidsetsize) = n;
    993 		SCARG(&bsa, gidset) = bset;
    994 		error = sys_getgroups(l, &bsa, retval);
    995 		if (error != 0)
    996 			goto out;
    997 		error = copyin(bset, kbset, n * sizeof (gid_t));
    998 		if (error != 0)
    999 			goto out;
   1000 		for (i = 0; i < n; i++)
   1001 			lset[i] = (linux_gid_t)kbset[i];
   1002 		error = copyout(lset, SCARG(uap, gidset),
   1003 		    n * sizeof (linux_gid_t));
   1004 	} else
   1005 		*retval = pc->pc_ucred->cr_ngroups;
   1006 out:
   1007 	if (kbset != NULL)
   1008 		free(kbset, M_TEMP);
   1009 	if (lset != NULL)
   1010 		free(lset, M_TEMP);
   1011 	return error;
   1012 }
   1013 
   1014 int
   1015 linux_sys_setgroups16(l, v, retval)
   1016 	struct lwp *l;
   1017 	void *v;
   1018 	register_t *retval;
   1019 {
   1020 	struct linux_sys_setgroups16_args /* {
   1021 		syscallarg(int) gidsetsize;
   1022 		syscallarg(linux_gid_t *) gidset;
   1023 	} */ *uap = v;
   1024 	struct proc *p = l->l_proc;
   1025 	caddr_t sg;
   1026 	int n;
   1027 	int error, i;
   1028 	struct sys_setgroups_args bsa;
   1029 	gid_t *bset, *kbset;
   1030 	linux_gid_t *lset;
   1031 
   1032 	n = SCARG(uap, gidsetsize);
   1033 	if (n < 0 || n > NGROUPS)
   1034 		return EINVAL;
   1035 	sg = stackgap_init(p->p_emul);
   1036 	bset = stackgap_alloc(&sg, n * sizeof (gid_t));
   1037 	lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1038 	kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
   1039 	if (lset == NULL || bset == NULL)
   1040 		return ENOMEM;
   1041 	error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
   1042 	if (error != 0)
   1043 		goto out;
   1044 	for (i = 0; i < n; i++)
   1045 		kbset[i] = (gid_t)lset[i];
   1046 	error = copyout(kbset, bset, n * sizeof (gid_t));
   1047 	if (error != 0)
   1048 		goto out;
   1049 	SCARG(&bsa, gidsetsize) = n;
   1050 	SCARG(&bsa, gidset) = bset;
   1051 	error = sys_setgroups(l, &bsa, retval);
   1052 
   1053 out:
   1054 	if (lset != NULL)
   1055 		free(lset, M_TEMP);
   1056 	if (kbset != NULL)
   1057 		free(kbset, M_TEMP);
   1058 
   1059 	return error;
   1060 }
   1061 
   1062 #endif /* __i386__ || __m68k__ */
   1063 
   1064 /*
   1065  * We have nonexistent fsuid equal to uid.
   1066  * If modification is requested, refuse.
   1067  */
   1068 int
   1069 linux_sys_setfsuid(l, v, retval)
   1070 	 struct lwp *l;
   1071 	 void *v;
   1072 	 register_t *retval;
   1073 {
   1074 	 struct linux_sys_setfsuid_args /* {
   1075 		 syscallarg(uid_t) uid;
   1076 	 } */ *uap = v;
   1077 	 struct proc *p = l->l_proc;
   1078 	 uid_t uid;
   1079 
   1080 	 uid = SCARG(uap, uid);
   1081 	 if (p->p_cred->p_ruid != uid)
   1082 		 return sys_nosys(l, v, retval);
   1083 	 else
   1084 		 return (0);
   1085 }
   1086 
   1087 /* XXX XXX XXX */
   1088 #ifndef alpha
   1089 int
   1090 linux_sys_getfsuid(l, v, retval)
   1091 	struct lwp *l;
   1092 	void *v;
   1093 	register_t *retval;
   1094 {
   1095 	return sys_getuid(l, v, retval);
   1096 }
   1097 #endif
   1098 
   1099 int
   1100 linux_sys___sysctl(l, v, retval)
   1101 	struct lwp *l;
   1102 	void *v;
   1103 	register_t *retval;
   1104 {
   1105 	struct linux_sys___sysctl_args /* {
   1106 		syscallarg(struct linux___sysctl *) lsp;
   1107 	} */ *uap = v;
   1108 	struct linux___sysctl ls;
   1109 	struct sys___sysctl_args bsa;
   1110 	int error;
   1111 
   1112 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
   1113 		return error;
   1114 	SCARG(&bsa, name) = ls.name;
   1115 	SCARG(&bsa, namelen) = ls.namelen;
   1116 	SCARG(&bsa, old) = ls.old;
   1117 	SCARG(&bsa, oldlenp) = ls.oldlenp;
   1118 	SCARG(&bsa, new) = ls.new;
   1119 	SCARG(&bsa, newlen) = ls.newlen;
   1120 
   1121 	return sys___sysctl(l, &bsa, retval);
   1122 }
   1123 
   1124 int
   1125 linux_sys_setresuid(l, v, retval)
   1126 	struct lwp *l;
   1127 	void *v;
   1128 	register_t *retval;
   1129 {
   1130 	struct linux_sys_setresuid_args /* {
   1131 		syscallarg(uid_t) ruid;
   1132 		syscallarg(uid_t) euid;
   1133 		syscallarg(uid_t) suid;
   1134 	} */ *uap = v;
   1135 	struct proc *p = l->l_proc;
   1136 	struct pcred *pc = p->p_cred;
   1137 	uid_t ruid, euid, suid;
   1138 	int error;
   1139 
   1140 	ruid = SCARG(uap, ruid);
   1141 	euid = SCARG(uap, euid);
   1142 	suid = SCARG(uap, suid);
   1143 
   1144 	/*
   1145 	 * Note: These checks are a little different than the NetBSD
   1146 	 * setreuid(2) call performs.  This precisely follows the
   1147 	 * behavior of the Linux kernel.
   1148 	 */
   1149 	if (ruid != (uid_t)-1 &&
   1150 	    ruid != pc->p_ruid &&
   1151 	    ruid != pc->pc_ucred->cr_uid &&
   1152 	    ruid != pc->p_svuid &&
   1153 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1154 		return (error);
   1155 
   1156 	if (euid != (uid_t)-1 &&
   1157 	    euid != pc->p_ruid &&
   1158 	    euid != pc->pc_ucred->cr_uid &&
   1159 	    euid != pc->p_svuid &&
   1160 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1161 		return (error);
   1162 
   1163 	if (suid != (uid_t)-1 &&
   1164 	    suid != pc->p_ruid &&
   1165 	    suid != pc->pc_ucred->cr_uid &&
   1166 	    suid != pc->p_svuid &&
   1167 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
   1168 		return (error);
   1169 
   1170 	/*
   1171 	 * Now assign the new real, effective, and saved UIDs.
   1172 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
   1173 	 * set the saved UID in this call unless the user specifies
   1174 	 * it.
   1175 	 */
   1176 	if (ruid != (uid_t)-1) {
   1177 		(void)chgproccnt(pc->p_ruid, -1);
   1178 		(void)chgproccnt(ruid, 1);
   1179 		pc->p_ruid = ruid;
   1180 	}
   1181 
   1182 	if (euid != (uid_t)-1) {
   1183 		pc->pc_ucred = crcopy(pc->pc_ucred);
   1184 		pc->pc_ucred->cr_uid = euid;
   1185 	}
   1186 
   1187 	if (suid != (uid_t)-1)
   1188 		pc->p_svuid = suid;
   1189 
   1190 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
   1191 		p->p_flag |= P_SUGID;
   1192 	return (0);
   1193 }
   1194 
   1195 int
   1196 linux_sys_getresuid(l, v, retval)
   1197 	struct lwp *l;
   1198 	void *v;
   1199 	register_t *retval;
   1200 {
   1201 	struct linux_sys_getresuid_args /* {
   1202 		syscallarg(uid_t *) ruid;
   1203 		syscallarg(uid_t *) euid;
   1204 		syscallarg(uid_t *) suid;
   1205 	} */ *uap = v;
   1206 	struct proc *p = l->l_proc;
   1207 	struct pcred *pc = p->p_cred;
   1208 	int error;
   1209 
   1210 	/*
   1211 	 * Linux copies these values out to userspace like so:
   1212 	 *
   1213 	 *	1. Copy out ruid.
   1214 	 *	2. If that succeeds, copy out euid.
   1215 	 *	3. If both of those succeed, copy out suid.
   1216 	 */
   1217 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
   1218 			     sizeof(uid_t))) != 0)
   1219 		return (error);
   1220 
   1221 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
   1222 			     sizeof(uid_t))) != 0)
   1223 		return (error);
   1224 
   1225 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
   1226 }
   1227 
   1228 int
   1229 linux_sys_ptrace(l, v, retval)
   1230 	struct lwp *l;
   1231 	void *v;
   1232 	register_t *retval;
   1233 {
   1234 	struct linux_sys_ptrace_args /* {
   1235 		i386, m68k: T=int
   1236 		alpha: T=long
   1237 		syscallarg(T) request;
   1238 		syscallarg(T) pid;
   1239 		syscallarg(T) addr;
   1240 		syscallarg(T) data;
   1241 	} */ *uap = v;
   1242 	struct proc *p = l->l_proc;
   1243 	const int *ptr;
   1244 	int request;
   1245 
   1246 	ptr = linux_ptrace_request_map;
   1247 	request = SCARG(uap, request);
   1248 	while (*ptr != -1)
   1249 		if (*ptr++ == request) {
   1250 			struct sys_ptrace_args pta;
   1251 			caddr_t sg;
   1252 
   1253 			sg = stackgap_init(p->p_emul);
   1254 
   1255 			SCARG(&pta, req) = *ptr;
   1256 			SCARG(&pta, pid) = SCARG(uap, pid);
   1257 			SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
   1258 			SCARG(&pta, data) = SCARG(uap, data);
   1259 
   1260 			/*
   1261 			 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
   1262 			 * to continue as the process left off previously,
   1263 			 * i.e. same as if NetBSD ptrace called with
   1264 			 * addr == (caddr_t) 1.
   1265 			 */
   1266 			if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
   1267 				SCARG(&pta, addr) = (caddr_t) 1;
   1268 
   1269 			return sys_ptrace(l, &pta, retval);
   1270 		}
   1271 		else
   1272 			ptr++;
   1273 
   1274 	return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
   1275 }
   1276 
   1277 int
   1278 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
   1279 {
   1280 	struct linux_sys_reboot_args /* {
   1281 		syscallarg(int) magic1;
   1282 		syscallarg(int) magic2;
   1283 		syscallarg(int) cmd;
   1284 		syscallarg(void *) arg;
   1285 	} */ *uap = v;
   1286 	struct sys_reboot_args /* {
   1287 		syscallarg(int) opt;
   1288 		syscallarg(char *) bootstr;
   1289 	} */ sra;
   1290 	struct proc *p = l->l_proc;
   1291 	int error;
   1292 
   1293 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1294 		return(error);
   1295 
   1296 	if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
   1297 		return(EINVAL);
   1298 	if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
   1299 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
   1300 	    SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
   1301 		return(EINVAL);
   1302 
   1303 	switch (SCARG(uap, cmd)) {
   1304 	case LINUX_REBOOT_CMD_RESTART:
   1305 		SCARG(&sra, opt) = RB_AUTOBOOT;
   1306 		break;
   1307 	case LINUX_REBOOT_CMD_HALT:
   1308 		SCARG(&sra, opt) = RB_HALT;
   1309 		break;
   1310 	case LINUX_REBOOT_CMD_POWER_OFF:
   1311 		SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
   1312 		break;
   1313 	case LINUX_REBOOT_CMD_RESTART2:
   1314 		/* Reboot with an argument. */
   1315 		SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
   1316 		SCARG(&sra, bootstr) = SCARG(uap, arg);
   1317 		break;
   1318 	case LINUX_REBOOT_CMD_CAD_ON:
   1319 		return(EINVAL);	/* We don't implement ctrl-alt-delete */
   1320 	case LINUX_REBOOT_CMD_CAD_OFF:
   1321 		return(0);
   1322 	default:
   1323 		return(EINVAL);
   1324 	}
   1325 
   1326 	return(sys_reboot(l, &sra, retval));
   1327 }
   1328 
   1329 /*
   1330  * Copy of compat_12_sys_swapon().
   1331  */
   1332 int
   1333 linux_sys_swapon(l, v, retval)
   1334 	struct lwp *l;
   1335 	void *v;
   1336 	register_t *retval;
   1337 {
   1338 	struct sys_swapctl_args ua;
   1339 	struct linux_sys_swapon_args /* {
   1340 		syscallarg(const char *) name;
   1341 	} */ *uap = v;
   1342 
   1343 	SCARG(&ua, cmd) = SWAP_ON;
   1344 	SCARG(&ua, arg) = (void *)SCARG(uap, name);
   1345 	SCARG(&ua, misc) = 0;	/* priority */
   1346 	return (sys_swapctl(l, &ua, retval));
   1347 }
   1348 
   1349 /*
   1350  * Stop swapping to the file or block device specified by path.
   1351  */
   1352 int
   1353 linux_sys_swapoff(l, v, retval)
   1354 	struct lwp *l;
   1355 	void *v;
   1356 	register_t *retval;
   1357 {
   1358 	struct sys_swapctl_args ua;
   1359 	struct linux_sys_swapoff_args /* {
   1360 		syscallarg(const char *) path;
   1361 	} */ *uap = v;
   1362 
   1363 	SCARG(&ua, cmd) = SWAP_OFF;
   1364 	SCARG(&ua, arg) = (void *)SCARG(uap, path);
   1365 	return (sys_swapctl(l, &ua, retval));
   1366 }
   1367 
   1368 /*
   1369  * Copy of compat_09_sys_setdomainname()
   1370  */
   1371 /* ARGSUSED */
   1372 int
   1373 linux_sys_setdomainname(l, v, retval)
   1374 	struct lwp *l;
   1375 	void *v;
   1376 	register_t *retval;
   1377 {
   1378 	struct linux_sys_setdomainname_args /* {
   1379 		syscallarg(char *) domainname;
   1380 		syscallarg(int) len;
   1381 	} */ *uap = v;
   1382 	struct proc *p = l->l_proc;
   1383 	int name;
   1384 	int error;
   1385 
   1386 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
   1387 		return (error);
   1388 	name = KERN_DOMAINNAME;
   1389 	return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
   1390 			    SCARG(uap, len), p));
   1391 }
   1392 
   1393 /*
   1394  * sysinfo()
   1395  */
   1396 /* ARGSUSED */
   1397 int
   1398 linux_sys_sysinfo(l, v, retval)
   1399 	struct lwp *l;
   1400 	void *v;
   1401 	register_t *retval;
   1402 {
   1403 	struct linux_sys_sysinfo_args /* {
   1404 		syscallarg(struct linux_sysinfo *) arg;
   1405 	} */ *uap = v;
   1406 	struct linux_sysinfo si;
   1407 	struct loadavg *la;
   1408 
   1409 	si.uptime = time.tv_sec - boottime.tv_sec;
   1410 	la = &averunnable;
   1411 	si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1412 	si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1413 	si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
   1414 	si.totalram = ctob(physmem);
   1415 	si.freeram = uvmexp.free * uvmexp.pagesize;
   1416 	si.sharedram = 0;	/* XXX */
   1417 	si.bufferram = uvmexp.vnodepages * uvmexp.pagesize;
   1418 	si.totalswap = uvmexp.swpages * uvmexp.pagesize;
   1419 	si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
   1420 	si.procs = nprocs;
   1421 
   1422 	/* The following are only present in newer Linux kernels. */
   1423 	si.totalbig = 0;
   1424 	si.freebig = 0;
   1425 	si.mem_unit = 1;
   1426 
   1427 	return (copyout(&si, SCARG(uap, arg), sizeof si));
   1428 }
   1429