xref: /netbsd-src/sys/compat/linux/common/linux_misc.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: linux_misc.c,v 1.61 1999/10/05 09:22:04 tron 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/ptrace.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 
94 #include <sys/syscallargs.h>
95 
96 #include <vm/vm.h>
97 #include <vm/vm_param.h>
98 
99 #include <compat/linux/common/linux_types.h>
100 #include <compat/linux/common/linux_signal.h>
101 
102 #include <compat/linux/linux_syscallargs.h>
103 
104 #include <compat/linux/common/linux_fcntl.h>
105 #include <compat/linux/common/linux_mmap.h>
106 #include <compat/linux/common/linux_dirent.h>
107 #include <compat/linux/common/linux_util.h>
108 #include <compat/linux/common/linux_misc.h>
109 
110 
111 /* Local linux_misc.c functions: */
112 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
113 
114 /*
115  * The information on a terminated (or stopped) process needs
116  * to be converted in order for Linux binaries to get a valid signal
117  * number out of it.
118  */
119 void
120 bsd_to_linux_wstat(st)
121 	int *st;
122 {
123 
124 	int sig;
125 
126 	if (WIFSIGNALED(*st)) {
127 		sig = WTERMSIG(*st);
128 		if (sig >= 0 && sig < NSIG)
129 			*st= (*st& ~0177) | native_to_linux_sig[sig];
130 	} else if (WIFSTOPPED(*st)) {
131 		sig = WSTOPSIG(*st);
132 		if (sig >= 0 && sig < NSIG)
133 			*st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
134 	}
135 }
136 
137 /*
138  * This is very much the same as waitpid()
139  */
140 int
141 linux_sys_wait4(p, v, retval)
142 	struct proc *p;
143 	void *v;
144 	register_t *retval;
145 {
146 	struct linux_sys_wait4_args /* {
147 		syscallarg(int) pid;
148 		syscallarg(int *) status;
149 		syscallarg(int) options;
150 		syscallarg(struct rusage *) rusage;
151 	} */ *uap = v;
152 	struct sys_wait4_args w4a;
153 	int error, *status, tstat, options, linux_options;
154 	caddr_t sg;
155 
156 	if (SCARG(uap, status) != NULL) {
157 		sg = stackgap_init(p->p_emul);
158 		status = (int *) stackgap_alloc(&sg, sizeof *status);
159 	} else
160 		status = NULL;
161 
162 	linux_options = SCARG(uap, options);
163 	options = 0;
164 	if (linux_options &
165 	    ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
166 		return (EINVAL);
167 
168 	if (linux_options & LINUX_WAIT4_WNOHANG)
169 		options |= WNOHANG;
170 	if (linux_options & LINUX_WAIT4_WUNTRACED)
171 		options |= WUNTRACED;
172 	if (linux_options & LINUX_WAIT4_WCLONE)
173 		options |= WALTSIG;
174 
175 	SCARG(&w4a, pid) = SCARG(uap, pid);
176 	SCARG(&w4a, status) = status;
177 	SCARG(&w4a, options) = options;
178 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
179 
180 	if ((error = sys_wait4(p, &w4a, retval)))
181 		return error;
182 
183 	sigdelset(&p->p_siglist, SIGCHLD);
184 
185 	if (status != NULL) {
186 		if ((error = copyin(status, &tstat, sizeof tstat)))
187 			return error;
188 
189 		bsd_to_linux_wstat(&tstat);
190 		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
191 	}
192 
193 	return 0;
194 }
195 
196 /*
197  * Linux brk(2). The check if the new address is >= the old one is
198  * done in the kernel in Linux. NetBSD does it in the library.
199  */
200 int
201 linux_sys_brk(p, v, retval)
202 	struct proc *p;
203 	void *v;
204 	register_t *retval;
205 {
206 	struct linux_sys_brk_args /* {
207 		syscallarg(char *) nsize;
208 	} */ *uap = v;
209 	char *nbrk = SCARG(uap, nsize);
210 	struct sys_obreak_args oba;
211 	struct vmspace *vm = p->p_vmspace;
212 	caddr_t oldbrk;
213 
214 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
215 	/*
216 	 * XXX inconsistent.. Linux always returns at least the old
217 	 * brk value, but it will be page-aligned if this fails,
218 	 * and possibly not page aligned if it succeeds (the user
219 	 * supplied pointer is returned).
220 	 */
221 	SCARG(&oba, nsize) = nbrk;
222 
223 	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
224 		retval[0] = (register_t)nbrk;
225 	else
226 		retval[0] = (register_t)oldbrk;
227 
228 	return 0;
229 }
230 
231 /*
232  * Convert BSD statfs structure to Linux statfs structure.
233  * The Linux structure has less fields, and it also wants
234  * the length of a name in a dir entry in a field, which
235  * we fake (probably the wrong way).
236  */
237 static void
238 bsd_to_linux_statfs(bsp, lsp)
239 	struct statfs *bsp;
240 	struct linux_statfs *lsp;
241 {
242 
243 	lsp->l_ftype = bsp->f_type;
244 	lsp->l_fbsize = bsp->f_bsize;
245 	lsp->l_fblocks = bsp->f_blocks;
246 	lsp->l_fbfree = bsp->f_bfree;
247 	lsp->l_fbavail = bsp->f_bavail;
248 	lsp->l_ffiles = bsp->f_files;
249 	lsp->l_fffree = bsp->f_ffree;
250 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
251 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
252 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
253 }
254 
255 /*
256  * Implement the fs stat functions. Straightforward.
257  */
258 int
259 linux_sys_statfs(p, v, retval)
260 	struct proc *p;
261 	void *v;
262 	register_t *retval;
263 {
264 	struct linux_sys_statfs_args /* {
265 		syscallarg(const char *) path;
266 		syscallarg(struct linux_statfs *) sp;
267 	} */ *uap = v;
268 	struct statfs btmp, *bsp;
269 	struct linux_statfs ltmp;
270 	struct sys_statfs_args bsa;
271 	caddr_t sg;
272 	int error;
273 
274 	sg = stackgap_init(p->p_emul);
275 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
276 
277 	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
278 
279 	SCARG(&bsa, path) = SCARG(uap, path);
280 	SCARG(&bsa, buf) = bsp;
281 
282 	if ((error = sys_statfs(p, &bsa, retval)))
283 		return error;
284 
285 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
286 		return error;
287 
288 	bsd_to_linux_statfs(&btmp, &ltmp);
289 
290 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
291 }
292 
293 int
294 linux_sys_fstatfs(p, v, retval)
295 	struct proc *p;
296 	void *v;
297 	register_t *retval;
298 {
299 	struct linux_sys_fstatfs_args /* {
300 		syscallarg(int) fd;
301 		syscallarg(struct linux_statfs *) sp;
302 	} */ *uap = v;
303 	struct statfs btmp, *bsp;
304 	struct linux_statfs ltmp;
305 	struct sys_fstatfs_args bsa;
306 	caddr_t sg;
307 	int error;
308 
309 	sg = stackgap_init(p->p_emul);
310 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
311 
312 	SCARG(&bsa, fd) = SCARG(uap, fd);
313 	SCARG(&bsa, buf) = bsp;
314 
315 	if ((error = sys_fstatfs(p, &bsa, retval)))
316 		return error;
317 
318 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
319 		return error;
320 
321 	bsd_to_linux_statfs(&btmp, &ltmp);
322 
323 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
324 }
325 
326 /*
327  * uname(). Just copy the info from the various strings stored in the
328  * kernel, and put it in the Linux utsname structure. That structure
329  * is almost the same as the NetBSD one, only it has fields 65 characters
330  * long, and an extra domainname field.
331  */
332 int
333 linux_sys_uname(p, v, retval)
334 	struct proc *p;
335 	void *v;
336 	register_t *retval;
337 {
338 	struct linux_sys_uname_args /* {
339 		syscallarg(struct linux_utsname *) up;
340 	} */ *uap = v;
341 	extern char ostype[], hostname[], osrelease[], version[], machine[],
342 	    domainname[];
343 	struct linux_utsname luts;
344 	int len;
345 	char *cp;
346 
347 	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
348 	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
349 	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
350 	strncpy(luts.l_version, version, sizeof(luts.l_version));
351 	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
352 	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
353 
354 	/* This part taken from the the uname() in libc */
355 	len = sizeof(luts.l_version);
356 	for (cp = luts.l_version; len--; ++cp) {
357 		if (*cp == '\n' || *cp == '\t') {
358 			if (len > 1)
359 				*cp = ' ';
360 			else
361 				*cp = '\0';
362 		}
363 	}
364 
365 	return copyout(&luts, SCARG(uap, up), sizeof(luts));
366 }
367 
368 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
369 /* Used indirectly on: arm, i386, m68k */
370 
371 /*
372  * New type Linux mmap call.
373  * Only called directly on machines with >= 6 free regs.
374  */
375 int
376 linux_sys_mmap(p, v, retval)
377 	struct proc *p;
378 	void *v;
379 	register_t *retval;
380 {
381 	struct linux_sys_mmap_args /* {
382 		syscallarg(unsigned long) addr;
383 		syscallarg(size_t) len;
384 		syscallarg(int) prot;
385 		syscallarg(int) flags;
386 		syscallarg(int) fd;
387 		syscallarg(off_t) offset;
388 	} */ *uap = v;
389 	struct sys_mmap_args cma;
390 	int flags;
391 
392 	flags = 0;
393 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
394 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
395 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
396 	flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
397 	/* XXX XAX ERH: Any other flags here?  There are more defined... */
398 
399 	SCARG(&cma,addr) = (void *)SCARG(uap, addr);
400 	SCARG(&cma,len) = SCARG(uap, len);
401 	SCARG(&cma,prot) = SCARG(uap, prot);
402 	if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
403 		SCARG(&cma,prot) |= VM_PROT_READ;
404 	SCARG(&cma,flags) = flags;
405 	SCARG(&cma,fd) = SCARG(uap, fd);
406 	SCARG(&cma,pad) = 0;
407 	SCARG(&cma,pos) = SCARG(uap, offset);
408 
409 	return sys_mmap(p, &cma, retval);
410 }
411 
412 int
413 linux_sys_mremap(p, v, retval)
414 	struct proc *p;
415 	void *v;
416 	register_t *retval;
417 {
418 	struct linux_sys_mremap_args /* {
419 		syscallarg(void *) old_address;
420 		syscallarg(size_t) old_size;
421 		syscallarg(size_t) new_size;
422 		syscallarg(u_long) flags;
423 	} */ *uap = v;
424 	struct sys_munmap_args mua;
425 	size_t old_size, new_size;
426 	int error;
427 
428 	old_size = round_page(SCARG(uap, old_size));
429 	new_size = round_page(SCARG(uap, new_size));
430 
431 	/*
432 	 * Growing mapped region.
433 	 */
434 	if (new_size > old_size) {
435 		/*
436 		 * XXX Implement me.  What we probably want to do is
437 		 * XXX dig out the guts of the old mapping, mmap that
438 		 * XXX object again with the new size, then munmap
439 		 * XXX the old mapping.
440 		 */
441 		*retval = 0;
442 		return (ENOMEM);
443 	}
444 
445 	/*
446 	 * Shrinking mapped region.
447 	 */
448 	if (new_size < old_size) {
449 		SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
450 		    new_size;
451 		SCARG(&mua, len) = old_size - new_size;
452 		error = sys_munmap(p, &mua, retval);
453 		*retval = error ? 0 : (register_t)SCARG(uap, old_address);
454 		return (error);
455 	}
456 
457 	/*
458 	 * No change.
459 	 */
460 	*retval = (register_t)SCARG(uap, old_address);
461 	return (0);
462 }
463 
464 int
465 linux_sys_msync(p, v, retval)
466 	struct proc *p;
467 	void *v;
468 	register_t *retval;
469 {
470 	struct linux_sys_msync_args /* {
471 		syscallarg(caddr_t) addr;
472 		syscallarg(int) len;
473 		syscallarg(int) fl;
474 	} */ *uap = v;
475 
476 	struct sys___msync13_args bma;
477 
478 	/* flags are ignored */
479 	SCARG(&bma, addr) = SCARG(uap, addr);
480 	SCARG(&bma, len) = SCARG(uap, len);
481 	SCARG(&bma, flags) = SCARG(uap, fl);
482 
483 	return sys___msync13(p, &bma, retval);
484 }
485 
486 /*
487  * This code is partly stolen from src/lib/libc/compat-43/times.c
488  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
489  */
490 
491 #define CLK_TCK 100
492 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
493 
494 int
495 linux_sys_times(p, v, retval)
496 	struct proc *p;
497 	void *v;
498 	register_t *retval;
499 {
500 	struct linux_sys_times_args /* {
501 		syscallarg(struct times *) tms;
502 	} */ *uap = v;
503 	struct timeval t;
504 	struct linux_tms ltms;
505 	struct rusage ru;
506 	int error, s;
507 
508 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
509 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
510 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
511 
512 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
513 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
514 
515 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
516 		return error;
517 
518 	s = splclock();
519 	timersub(&time, &boottime, &t);
520 	splx(s);
521 
522 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
523 	return 0;
524 }
525 
526 /*
527  * Linux 'readdir' call. This code is mostly taken from the
528  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
529  * an attempt has been made to keep it a little cleaner (failing
530  * miserably, because of the cruft needed if count 1 is passed).
531  *
532  * The d_off field should contain the offset of the next valid entry,
533  * but in Linux it has the offset of the entry itself. We emulate
534  * that bug here.
535  *
536  * Read in BSD-style entries, convert them, and copy them out.
537  *
538  * Note that this doesn't handle union-mounted filesystems.
539  */
540 int
541 linux_sys_getdents(p, v, retval)
542 	struct proc *p;
543 	void *v;
544 	register_t *retval;
545 {
546 	struct linux_sys_getdents_args /* {
547 		syscallarg(int) fd;
548 		syscallarg(struct linux_dirent *) dent;
549 		syscallarg(unsigned int) count;
550 	} */ *uap = v;
551 	register struct dirent *bdp;
552 	struct vnode *vp;
553 	caddr_t	inp, buf;		/* BSD-format */
554 	int len, reclen;		/* BSD-format */
555 	caddr_t outp;			/* Linux-format */
556 	int resid, linux_reclen = 0;	/* Linux-format */
557 	struct file *fp;
558 	struct uio auio;
559 	struct iovec aiov;
560 	struct linux_dirent idb;
561 	off_t off;		/* true file offset */
562 	int buflen, error, eofflag, nbytes, oldcall;
563 	struct vattr va;
564 	off_t *cookiebuf = NULL, *cookie;
565 	int ncookies;
566 
567 	/* getvnode() will use the descriptor for us */
568 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
569 		return (error);
570 
571 	if ((fp->f_flag & FREAD) == 0) {
572 		error = EBADF;
573 		goto out1;
574 	}
575 
576 	vp = (struct vnode *)fp->f_data;
577 	if (vp->v_type != VDIR) {
578 		error = EINVAL;
579 		goto out1;
580 	}
581 
582 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
583 		goto out1;
584 
585 	nbytes = SCARG(uap, count);
586 	if (nbytes == 1) {	/* emulating old, broken behaviour */
587 		nbytes = sizeof (struct linux_dirent);
588 		buflen = max(va.va_blocksize, nbytes);
589 		oldcall = 1;
590 	} else {
591 		buflen = min(MAXBSIZE, nbytes);
592 		if (buflen < va.va_blocksize)
593 			buflen = va.va_blocksize;
594 		oldcall = 0;
595 	}
596 	buf = malloc(buflen, M_TEMP, M_WAITOK);
597 
598 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
599 	off = fp->f_offset;
600 again:
601 	aiov.iov_base = buf;
602 	aiov.iov_len = buflen;
603 	auio.uio_iov = &aiov;
604 	auio.uio_iovcnt = 1;
605 	auio.uio_rw = UIO_READ;
606 	auio.uio_segflg = UIO_SYSSPACE;
607 	auio.uio_procp = p;
608 	auio.uio_resid = buflen;
609 	auio.uio_offset = off;
610 	/*
611          * First we read into the malloc'ed buffer, then
612          * we massage it into user space, one record at a time.
613          */
614 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
615 	    &ncookies);
616 	if (error)
617 		goto out;
618 
619 	inp = buf;
620 	outp = (caddr_t)SCARG(uap, dent);
621 	resid = nbytes;
622 	if ((len = buflen - auio.uio_resid) == 0)
623 		goto eof;
624 
625 	for (cookie = cookiebuf; len > 0; len -= reclen) {
626 		bdp = (struct dirent *)inp;
627 		reclen = bdp->d_reclen;
628 		if (reclen & 3)
629 			panic("linux_readdir");
630 		if (bdp->d_fileno == 0) {
631 			inp += reclen;	/* it is a hole; squish it out */
632 			off = *cookie++;
633 			continue;
634 		}
635 		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
636 		if (reclen > len || resid < linux_reclen) {
637 			/* entry too big for buffer, so just stop */
638 			outp++;
639 			break;
640 		}
641 		/*
642 		 * Massage in place to make a Linux-shaped dirent (otherwise
643 		 * we have to worry about touching user memory outside of
644 		 * the copyout() call).
645 		 */
646 		idb.d_ino = (linux_ino_t)bdp->d_fileno;
647 		/*
648 		 * The old readdir() call misuses the offset and reclen fields.
649 		 */
650 		if (oldcall) {
651 			idb.d_off = (linux_off_t)linux_reclen;
652 			idb.d_reclen = (u_short)bdp->d_namlen;
653 		} else {
654 			if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
655 				compat_offseterr(vp, "linux_getdents");
656 				error = EINVAL;
657 				goto out;
658 			}
659 			idb.d_off = (linux_off_t)off;
660 			idb.d_reclen = (u_short)linux_reclen;
661 		}
662 		strcpy(idb.d_name, bdp->d_name);
663 		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
664 			goto out;
665 		/* advance past this real entry */
666 		inp += reclen;
667 		off = *cookie++;	/* each entry points to itself */
668 		/* advance output past Linux-shaped entry */
669 		outp += linux_reclen;
670 		resid -= linux_reclen;
671 		if (oldcall)
672 			break;
673 	}
674 
675 	/* if we squished out the whole block, try again */
676 	if (outp == (caddr_t)SCARG(uap, dent))
677 		goto again;
678 	fp->f_offset = off;	/* update the vnode offset */
679 
680 	if (oldcall)
681 		nbytes = resid + linux_reclen;
682 
683 eof:
684 	*retval = nbytes - resid;
685 out:
686 	VOP_UNLOCK(vp, 0);
687 	if (cookiebuf)
688 		free(cookiebuf, M_TEMP);
689 	free(buf, M_TEMP);
690  out1:
691 	FILE_UNUSE(fp, p);
692 	return error;
693 }
694 
695 /*
696  * Even when just using registers to pass arguments to syscalls you can
697  * have 5 of them on the i386. So this newer version of select() does
698  * this.
699  */
700 int
701 linux_sys_select(p, v, retval)
702 	struct proc *p;
703 	void *v;
704 	register_t *retval;
705 {
706 	struct linux_sys_select_args /* {
707 		syscallarg(int) nfds;
708 		syscallarg(fd_set *) readfds;
709 		syscallarg(fd_set *) writefds;
710 		syscallarg(fd_set *) exceptfds;
711 		syscallarg(struct timeval *) timeout;
712 	} */ *uap = v;
713 
714 	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
715 	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
716 }
717 
718 /*
719  * Common code for the old and new versions of select(). A couple of
720  * things are important:
721  * 1) return the amount of time left in the 'timeout' parameter
722  * 2) select never returns ERESTART on Linux, always return EINTR
723  */
724 int
725 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
726 	struct proc *p;
727 	register_t *retval;
728 	int nfds;
729 	fd_set *readfds, *writefds, *exceptfds;
730 	struct timeval *timeout;
731 {
732 	struct sys_select_args bsa;
733 	struct timeval tv0, tv1, utv, *tvp;
734 	caddr_t sg;
735 	int error;
736 
737 	SCARG(&bsa, nd) = nfds;
738 	SCARG(&bsa, in) = readfds;
739 	SCARG(&bsa, ou) = writefds;
740 	SCARG(&bsa, ex) = exceptfds;
741 	SCARG(&bsa, tv) = timeout;
742 
743 	/*
744 	 * Store current time for computation of the amount of
745 	 * time left.
746 	 */
747 	if (timeout) {
748 		if ((error = copyin(timeout, &utv, sizeof(utv))))
749 			return error;
750 		if (itimerfix(&utv)) {
751 			/*
752 			 * The timeval was invalid.  Convert it to something
753 			 * valid that will act as it does under Linux.
754 			 */
755 			sg = stackgap_init(p->p_emul);
756 			tvp = stackgap_alloc(&sg, sizeof(utv));
757 			utv.tv_sec += utv.tv_usec / 1000000;
758 			utv.tv_usec %= 1000000;
759 			if (utv.tv_usec < 0) {
760 				utv.tv_sec -= 1;
761 				utv.tv_usec += 1000000;
762 			}
763 			if (utv.tv_sec < 0)
764 				timerclear(&utv);
765 			if ((error = copyout(&utv, tvp, sizeof(utv))))
766 				return error;
767 			SCARG(&bsa, tv) = tvp;
768 		}
769 		microtime(&tv0);
770 	}
771 
772 	error = sys_select(p, &bsa, retval);
773 	if (error) {
774 		/*
775 		 * See fs/select.c in the Linux kernel.  Without this,
776 		 * Maelstrom doesn't work.
777 		 */
778 		if (error == ERESTART)
779 			error = EINTR;
780 		return error;
781 	}
782 
783 	if (timeout) {
784 		if (*retval) {
785 			/*
786 			 * Compute how much time was left of the timeout,
787 			 * by subtracting the current time and the time
788 			 * before we started the call, and subtracting
789 			 * that result from the user-supplied value.
790 			 */
791 			microtime(&tv1);
792 			timersub(&tv1, &tv0, &tv1);
793 			timersub(&utv, &tv1, &utv);
794 			if (utv.tv_sec < 0)
795 				timerclear(&utv);
796 		} else
797 			timerclear(&utv);
798 		if ((error = copyout(&utv, timeout, sizeof(utv))))
799 			return error;
800 	}
801 
802 	return 0;
803 }
804 
805 /*
806  * Get the process group of a certain process. Look it up
807  * and return the value.
808  */
809 int
810 linux_sys_getpgid(p, v, retval)
811 	struct proc *p;
812 	void *v;
813 	register_t *retval;
814 {
815 	struct linux_sys_getpgid_args /* {
816 		syscallarg(int) pid;
817 	} */ *uap = v;
818 	struct proc *targp;
819 
820 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
821 		if ((targp = pfind(SCARG(uap, pid))) == 0)
822 			return ESRCH;
823 	}
824 	else
825 		targp = p;
826 
827 	retval[0] = targp->p_pgid;
828 	return 0;
829 }
830 
831 /*
832  * Set the 'personality' (emulation mode) for the current process. Only
833  * accept the Linux personality here (0). This call is needed because
834  * the Linux ELF crt0 issues it in an ugly kludge to make sure that
835  * ELF binaries run in Linux mode, not SVR4 mode.
836  */
837 int
838 linux_sys_personality(p, v, retval)
839 	struct proc *p;
840 	void *v;
841 	register_t *retval;
842 {
843 	struct linux_sys_personality_args /* {
844 		syscallarg(int) per;
845 	} */ *uap = v;
846 
847 	if (SCARG(uap, per) != 0)
848 		return EINVAL;
849 	retval[0] = 0;
850 	return 0;
851 }
852 
853 /*
854  * The calls are here because of type conversions.
855  */
856 int
857 linux_sys_setreuid(p, v, retval)
858 	struct proc *p;
859 	void *v;
860 	register_t *retval;
861 {
862 	struct linux_sys_setreuid_args /* {
863 		syscallarg(int) ruid;
864 		syscallarg(int) euid;
865 	} */ *uap = v;
866 	struct sys_setreuid_args bsa;
867 
868 	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
869 		(uid_t)-1 : SCARG(uap, ruid);
870 	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
871 		(uid_t)-1 : SCARG(uap, euid);
872 
873 	return sys_setreuid(p, &bsa, retval);
874 }
875 
876 int
877 linux_sys_setregid(p, v, retval)
878 	struct proc *p;
879 	void *v;
880 	register_t *retval;
881 {
882 	struct linux_sys_setregid_args /* {
883 		syscallarg(int) rgid;
884 		syscallarg(int) egid;
885 	} */ *uap = v;
886 	struct sys_setregid_args bsa;
887 
888 	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
889 		(uid_t)-1 : SCARG(uap, rgid);
890 	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
891 		(uid_t)-1 : SCARG(uap, egid);
892 
893 	return sys_setregid(p, &bsa, retval);
894 }
895 
896 int
897 linux_sys___sysctl(p, v, retval)
898 	struct proc *p;
899 	void *v;
900 	register_t *retval;
901 {
902 	struct linux_sys___sysctl_args /* {
903 		syscallarg(struct linux___sysctl *) lsp;
904 	} */ *uap = v;
905 	struct linux___sysctl ls;
906 	struct sys___sysctl_args bsa;
907 	int error;
908 
909 	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
910 		return error;
911 	SCARG(&bsa, name) = ls.name;
912 	SCARG(&bsa, namelen) = ls.namelen;
913 	SCARG(&bsa, old) = ls.old;
914 	SCARG(&bsa, oldlenp) = ls.oldlenp;
915 	SCARG(&bsa, new) = ls.new;
916 	SCARG(&bsa, newlen) = ls.newlen;
917 
918 	return sys___sysctl(p, &bsa, retval);
919 }
920 
921 int
922 linux_sys_setresuid(p, v, retval)
923 	struct proc *p;
924 	void *v;
925 	register_t *retval;
926 {
927 	struct linux_sys_setresuid_args /* {
928 		syscallarg(uid_t) ruid;
929 		syscallarg(uid_t) euid;
930 		syscallarg(uid_t) suid;
931 	} */ *uap = v;
932 	struct pcred *pc = p->p_cred;
933 	uid_t ruid, euid, suid;
934 	int error;
935 
936 	ruid = SCARG(uap, ruid);
937 	euid = SCARG(uap, euid);
938 	suid = SCARG(uap, suid);
939 
940 	/*
941 	 * Note: These checks are a little different than the NetBSD
942 	 * setreuid(2) call performs.  This precisely follows the
943 	 * behavior of the Linux kernel.
944 	 */
945 	if (ruid != (uid_t)-1 &&
946 	    ruid != pc->p_ruid &&
947 	    ruid != pc->pc_ucred->cr_uid &&
948 	    ruid != pc->p_svuid &&
949 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
950 		return (error);
951 
952 	if (euid != (uid_t)-1 &&
953 	    euid != pc->p_ruid &&
954 	    euid != pc->pc_ucred->cr_uid &&
955 	    euid != pc->p_svuid &&
956 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
957 		return (error);
958 
959 	if (suid != (uid_t)-1 &&
960 	    suid != pc->p_ruid &&
961 	    suid != pc->pc_ucred->cr_uid &&
962 	    suid != pc->p_svuid &&
963 	    (error = suser(pc->pc_ucred, &p->p_acflag)))
964 		return (error);
965 
966 	/*
967 	 * Now assign the new real, effective, and saved UIDs.
968 	 * Note that Linux, unlike NetBSD in setreuid(2), does not
969 	 * set the saved UID in this call unless the user specifies
970 	 * it.
971 	 */
972 	if (ruid != (uid_t)-1) {
973 		(void)chgproccnt(pc->p_ruid, -1);
974 		(void)chgproccnt(ruid, 1);
975 		pc->p_ruid = ruid;
976 	}
977 
978 	if (euid != (uid_t)-1) {
979 		pc->pc_ucred = crcopy(pc->pc_ucred);
980 		pc->pc_ucred->cr_uid = euid;
981 	}
982 
983 	if (suid != (uid_t)-1)
984 		pc->p_svuid = suid;
985 
986 	if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
987 		p->p_flag |= P_SUGID;
988 	return (0);
989 }
990 
991 int
992 linux_sys_getresuid(p, v, retval)
993 	struct proc *p;
994 	void *v;
995 	register_t *retval;
996 {
997 	struct linux_sys_getresuid_args /* {
998 		syscallarg(uid_t *) ruid;
999 		syscallarg(uid_t *) euid;
1000 		syscallarg(uid_t *) suid;
1001 	} */ *uap = v;
1002 	struct pcred *pc = p->p_cred;
1003 	int error;
1004 
1005 	/*
1006 	 * Linux copies these values out to userspace like so:
1007 	 *
1008 	 *	1. Copy out ruid.
1009 	 *	2. If that succeeds, copy out euid.
1010 	 *	3. If both of those succeed, copy out suid.
1011 	 */
1012 	if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1013 			     sizeof(uid_t))) != 0)
1014 		return (error);
1015 
1016 	if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1017 			     sizeof(uid_t))) != 0)
1018 		return (error);
1019 
1020 	return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1021 }
1022