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