xref: /netbsd-src/sys/compat/linux/common/linux_break.c (revision 182c23c8bc953f735285f652e318b26abdb110d5)
1 /*	$NetBSD: linux_break.c,v 1.2 1995/03/05 23:23:41 fvdl Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Frank van der Linden
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed for the NetBSD Project
18  *      by Frank van der Linden
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Linux compatibility module. Try to deal with various Linux system calls.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/proc.h>
42 #include <sys/dir.h>
43 #include <sys/file.h>
44 #include <sys/stat.h>
45 #include <sys/filedesc.h>
46 #include <sys/ioctl.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mbuf.h>
50 #include <sys/mman.h>
51 #include <sys/mount.h>
52 #include <sys/ptrace.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/signal.h>
56 #include <sys/signalvar.h>
57 #include <sys/socket.h>
58 #include <sys/time.h>
59 #include <sys/times.h>
60 #include <sys/vnode.h>
61 #include <sys/uio.h>
62 #include <sys/wait.h>
63 #include <sys/utsname.h>
64 #include <sys/unistd.h>
65 
66 #include <sys/syscallargs.h>
67 
68 #include <vm/vm.h>
69 #include <vm/vm_param.h>
70 
71 #include <compat/linux/linux_types.h>
72 #include <compat/linux/linux_fcntl.h>
73 #include <compat/linux/linux_mmap.h>
74 #include <compat/linux/linux_syscallargs.h>
75 #include <compat/linux/linux_util.h>
76 #include <compat/linux/linux_dirent.h>
77 
78 /*
79  * The information on a terminated (or stopped) process needs
80  * to be converted in order for Linux binaries to get a valid signal
81  * number out of it.
82  */
83 static int
84 bsd_to_linux_wstat(status)
85 	int *status;
86 {
87 	if (WIFSIGNALED(*status))
88 		*status = (*status & ~0177) |
89 		    bsd_to_linux_sig(WTERMSIG(*status));
90 	else if (WIFSTOPPED(*status))
91 		*status = (*status & ~0xff00) |
92 		    (bsd_to_linux_sig(WSTOPSIG(*status)) << 8);
93 }
94 
95 /*
96  * waitpid(2). Passed on to the NetBSD call, surrounded by code to
97  * reserve some space for a NetBSD-style wait status, and converting
98  * it to what Linux wants.
99  */
100 int
101 linux_waitpid(p, uap, retval)
102 	struct proc *p;
103 	struct linux_waitpid_args /* {
104 		syscallarg(int) pid;
105 		syscallarg(int *) status;
106 		syscallarg(int) options;
107 	} */ *uap;
108 	register_t *retval;
109 {
110 	struct wait4_args w4a;
111 	int error, *status, tstat;
112 	caddr_t sg;
113 
114 	sg = stackgap_init();
115 	status = (int *) stackgap_alloc(&sg, sizeof status);
116 
117 	SCARG(&w4a, pid) = SCARG(uap, pid);
118 	SCARG(&w4a, status) = status;
119 	SCARG(&w4a, options) = SCARG(uap, options);
120 	SCARG(&w4a, rusage) = NULL;
121 
122 	if ((error = wait4(p, &w4a, retval)))
123 		return error;
124 
125 	if ((error = copyin(status, &tstat, sizeof tstat)))
126 		return error;
127 
128 	bsd_to_linux_wstat(&tstat);
129 
130 	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
131 }
132 
133 /*
134  * This is very much the same as waitpid()
135  */
136 int
137 linux_wait4(p, uap, retval)
138 	struct proc *p;
139 	struct linux_wait4_args /* {
140 		syscallarg(int) pid;
141 		syscallarg(int *) status;
142 		syscallarg(int) options;
143 		syscallarg(struct rusage *) rusage;
144 	} */ *uap;
145 	register_t *retval;
146 {
147 	struct wait4_args w4a;
148 	int error, *status, tstat;
149 	caddr_t sg;
150 
151 	sg = stackgap_init();
152 	status = (int *) stackgap_alloc(&sg, sizeof status);
153 
154 	SCARG(&w4a, pid) = SCARG(uap, pid);
155 	SCARG(&w4a, status) = status;
156 	SCARG(&w4a, options) = SCARG(uap, options);
157 	SCARG(&w4a, rusage) = SCARG(uap, rusage);
158 
159 	if ((error = wait4(p, &w4a, retval)))
160 		return error;
161 
162 	if ((error = copyin(status, &tstat, sizeof tstat)))
163 		return error;
164 
165 	bsd_to_linux_wstat(&tstat);
166 
167 	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
168 }
169 
170 /*
171  * This is the old brk(2) call. I don't think anything in the Linux
172  * world uses this anymore
173  */
174 int
175 linux_break(p, uap, retval)
176 	struct proc *p;
177 	struct linux_brk_args /* {
178 		syscallarg(char *) nsize;
179 	} */ *uap;
180 	register_t *retval;
181 {
182 	return ENOSYS;
183 }
184 
185 /*
186  * Linux brk(2). The check if the new address is >= the old one is
187  * done in the kernel in Linux. NetBSD does it in the library.
188  */
189 int
190 linux_brk(p, uap, retval)
191 	struct proc *p;
192 	struct linux_brk_args /* {
193 		syscallarg(char *) nsize;
194 	} */ *uap;
195 	register_t *retval;
196 {
197 	char *nbrk = SCARG(uap, nsize);
198 	struct obreak_args oba;
199 	struct vmspace *vm = p->p_vmspace;
200 	int error = 0;
201 	caddr_t oldbrk, newbrk;
202 
203 	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
204 	/*
205 	 * XXX inconsistent.. Linux always returns at least the old
206 	 * brk value, but it will be page-aligned if this fails,
207 	 * and possibly not page aligned if it succeeds (the user
208 	 * supplied pointer is returned).
209 	 */
210 	SCARG(&oba, nsize) = nbrk;
211 
212 	if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
213 		retval[0] = (register_t) nbrk;
214 	else
215 		retval[0] = (register_t) oldbrk;
216 
217 	return 0;
218 }
219 
220 /*
221  * I wonder why Linux has gettimeofday() _and_ time().. Still, we
222  * need to deal with it.
223  */
224 int
225 linux_time(p, uap, retval)
226 	struct proc *p;
227 	struct linux_time_args /* {
228 		linux_time_t *t;
229 	} */ *uap;
230 	register_t *retval;
231 {
232 	struct timeval atv;
233 	linux_time_t tt;
234 	int error;
235 
236 	microtime(&atv);
237 
238 	tt = atv.tv_sec;
239 	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
240 		return error;
241 
242 	retval[0] = tt;
243 	return 0;
244 }
245 
246 /*
247  * Convert BSD statfs structure to Linux statfs structure.
248  * The Linux structure has less fields, and it also wants
249  * the length of a name in a dir entry in a field, which
250  * we fake (probably the wrong way).
251  */
252 static void
253 bsd_to_linux_statfs(bsp, lsp)
254 	struct statfs *bsp;
255 	struct linux_statfs *lsp;
256 {
257 	lsp->l_ftype = bsp->f_type;
258 	lsp->l_fbsize = bsp->f_bsize;
259 	lsp->l_fblocks = bsp->f_blocks;
260 	lsp->l_fbfree = bsp->f_bfree;
261 	lsp->l_fbavail = bsp->f_bavail;
262 	lsp->l_ffiles = bsp->f_files;
263 	lsp->l_fffree = bsp->f_ffree;
264 	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
265 	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
266 	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
267 }
268 
269 /*
270  * Implement the fs stat functions. Straightforward.
271  */
272 int
273 linux_statfs(p, uap, retval)
274 	struct proc *p;
275 	struct linux_statfs_args /* {
276 		syscallarg(char *) path;
277 		syscallarg(struct linux_statfs *) sp;
278 	} */ *uap;
279 	register_t *retval;
280 {
281 	struct statfs btmp, *bsp;
282 	struct linux_statfs ltmp;
283 	struct statfs_args bsa;
284 	caddr_t sg;
285 	int error;
286 
287 	sg = stackgap_init();
288 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
289 
290 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
291 
292 	SCARG(&bsa, path) = SCARG(uap, path);
293 	SCARG(&bsa, buf) = bsp;
294 
295 	if ((error = statfs(p, &bsa, retval)))
296 		return error;
297 
298 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
299 		return error;
300 
301 	bsd_to_linux_statfs(&btmp, &ltmp);
302 
303 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
304 }
305 
306 int
307 linux_fstatfs(p, uap, retval)
308 	struct proc *p;
309 	struct linux_fstatfs_args /* {
310 		syscallarg(int) fd;
311 		syscallarg(struct linux_statfs *) sp;
312 	} */ *uap;
313 	register_t *retval;
314 {
315 	struct statfs btmp, *bsp;
316 	struct linux_statfs ltmp;
317 	struct fstatfs_args bsa;
318 	caddr_t sg;
319 	int error;
320 
321 	sg = stackgap_init();
322 	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
323 
324 	SCARG(&bsa, fd) = SCARG(uap, fd);
325 	SCARG(&bsa, buf) = bsp;
326 
327 	if ((error = statfs(p, &bsa, retval)))
328 		return error;
329 
330 	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
331 		return error;
332 
333 	bsd_to_linux_statfs(&btmp, &ltmp);
334 
335 	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
336 }
337 
338 /*
339  * uname(). Just copy the info from the various strings stored in the
340  * kernel, and put it in the Linux utsname structure. That structure
341  * is almost the same as the NetBSD one, only it has fields 65 characters
342  * long, and an extra domainname field.
343  */
344 int
345 linux_uname(p, uap, retval)
346 	struct proc *p;
347 	struct linux_uname_args /* {
348 		syscallarg(struct linux_utsname *) up;
349 	} */ *uap;
350 	register_t *retval;
351 {
352 	extern char ostype[], osrelease[], version[], hostname[], domainname[];
353 	extern char machine[];
354 	struct linux_utsname tluts;
355 	int len;
356 	char *cp;
357 
358 	strncpy(tluts.l_sysname, ostype, sizeof (tluts.l_sysname));
359 	strncpy(tluts.l_nodename, hostname, sizeof (tluts.l_nodename));
360 	strncpy(tluts.l_release, osrelease, sizeof (tluts.l_release));
361 	strncpy(tluts.l_machine, machine, sizeof (tluts.l_machine));
362 	strncpy(tluts.l_domainname, domainname, sizeof (tluts.l_domainname));
363 	strncpy(tluts.l_version, version, sizeof (tluts.l_version));
364 
365 	/* This part taken from the the uname() in libc */
366 	len = sizeof (tluts.l_version);
367 	for (cp = tluts.l_version; len--; ++cp)
368 		if (*cp == '\n' || *cp == '\t')
369 			if (len > 1)
370 				*cp = ' ';
371 			else
372 				*cp = '\0';
373 
374 	return copyout(&tluts, SCARG(uap, up), sizeof tluts);
375 }
376 
377 /*
378  * Linux wants to pass everything to a syscall in registers. However,
379  * mmap() has 6 of them. Oops: out of register error. They just pass
380  * everything in a structure.
381  */
382 int
383 linux_mmap(p, uap, retval)
384 	struct proc *p;
385 	struct linux_mmap_args /* {
386 		syscallarg(struct linux_mmap *) lmp;
387 	} */ *uap;
388 	register_t *retval;
389 {
390 	struct linux_mmap lmap;
391 	struct mmap_args cma;
392 	int error, flags;
393 
394 	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
395 		return error;
396 
397 	flags = 0;
398 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
399 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
400 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
401 	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
402 
403 	SCARG(&cma,addr) = lmap.lm_addr;
404 	SCARG(&cma,len) = lmap.lm_len;
405  	SCARG(&cma,prot) = lmap.lm_prot;
406 	SCARG(&cma,flags) = flags;
407 	SCARG(&cma,fd) = lmap.lm_fd;
408 	SCARG(&cma,pad) = 0;
409 	SCARG(&cma,pos) = lmap.lm_pos;
410 
411 	return mmap(p, &cma, retval);
412 }
413 
414 /*
415  * Linux doesn't use the retval[1] value to determine whether
416  * we are the child or parent.
417  */
418 int
419 linux_fork(p, uap, retval)
420 	struct proc *p;
421 	void *uap;
422 	register_t *retval;
423 {
424 	int error;
425 
426 	if ((error = fork(p, uap, retval)))
427 		return error;
428 
429 	if (retval[1] == 1)
430 		retval[0] = 0;
431 
432 	return 0;
433 }
434 
435 /*
436  * This code is partly stolen from src/lib/libc/compat-43/times.c
437  * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
438  */
439 
440 #define CLK_TCK 100
441 #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
442 
443 int
444 linux_times(p, uap, retval)
445 	struct proc *p;
446 	struct linux_times_args /* {
447 		syscallarg(struct times *) tms;
448 	} */ *uap;
449 	register_t *retval;
450 {
451 	struct timeval t;
452 	struct linux_tms ltms;
453 	struct rusage ru;
454 	int error;
455 
456 	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
457 	ltms.ltms_utime = CONVTCK(ru.ru_utime);
458 	ltms.ltms_stime = CONVTCK(ru.ru_stime);
459 
460 	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
461 	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
462 
463 	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
464 		return error;
465 
466 	microtime(&t);
467 
468 	retval[0] = ((linux_clock_t)(CONVTCK(t)));
469 	return 0;
470 }
471 
472 /*
473  * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
474  * Linux directly passes the pointer.
475  */
476 int
477 linux_pipe(p, uap, retval)
478 	struct proc *p;
479 	struct linux_pipe_args /* {
480 		syscallarg(int *) pfds;
481 	} */ *uap;
482 	register_t *retval;
483 {
484 	int error;
485 
486 	if ((error = pipe(p, 0, retval)))
487 		return error;
488 
489 	/* Assumes register_t is an int */
490 
491 	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
492 		return error;
493 
494 	retval[0] = 0;
495 	return 0;
496 }
497 
498 /*
499  * Alarm. This is a libc call which used setitimer(2) in NetBSD.
500  * Fiddle with the timers to make it work.
501  */
502 int
503 linux_alarm(p, uap, retval)
504 	struct proc *p;
505 	struct linux_alarm_args /* {
506 		syscallarg(unsigned int) secs;
507 	} */ *uap;
508 	register_t *retval;
509 {
510 	int error, s;
511 	struct itimerval *itp, it;
512 
513 	itp = &p->p_realtimer;
514 	s = splclock();
515 	/*
516 	 * Clear any pending timer alarms.
517 	 */
518 	untimeout(realitexpire, p);
519 	timerclear(&itp->it_interval);
520 	if (timerisset(&itp->it_value) &&
521 	    timercmp(&itp->it_value, &time, >))
522 		__timersub(&itp->it_value, &time);
523 	/*
524 	 * Return how many seconds were left (rounded up)
525 	 */
526 	retval[0] = itp->it_value.tv_sec;
527 	if (itp->it_value.tv_usec)
528 		retval[0]++;
529 
530 	/*
531 	 * alarm(0) just resets the timer.
532 	 */
533 	if (SCARG(uap, secs) == 0) {
534 		timerclear(&itp->it_value);
535 		splx(s);
536 		return 0;
537 	}
538 
539 	/*
540 	 * Check the new alarm time for sanity, and set it.
541 	 */
542 	timerclear(&it.it_interval);
543 	it.it_value.tv_sec = SCARG(uap, secs);
544 	it.it_value.tv_usec = 0;
545 	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
546 		splx(s);
547 		return (EINVAL);
548 	}
549 
550 	if (timerisset(&it.it_value)) {
551 		__timeradd(&it.it_value, &time);
552 		timeout(realitexpire, p, hzto(&it.it_value));
553 	}
554 	p->p_realtimer = it;
555 	splx(s);
556 
557 	return 0;
558 }
559 
560 /*
561  * utime(). Do conversion to things that utimes() understands,
562  * and pass it on.
563  */
564 int
565 linux_utime(p, uap, retval)
566 	struct proc *p;
567 	struct linux_utime_args /* {
568 		syscallarg(char *) path;
569 		syscallarg(struct linux_utimbuf *)times;
570 	} */ *uap;
571 	register_t *retval;
572 {
573 	caddr_t sg;
574 	int error;
575 	struct utimes_args ua;
576 	struct timeval tv[2], *tvp;
577 	struct linux_utimbuf lut;
578 
579 	sg = stackgap_init();
580 	CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
581 
582 	SCARG(&ua, path) = SCARG(uap, path);
583 
584 	if (SCARG(uap, times) != NULL) {
585 		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
586 			return error;
587 		tv[0].tv_usec = tv[1].tv_usec = 0;
588 		tv[0].tv_sec = lut.l_actime;
589 		tv[1].tv_sec = lut.l_modtime;
590 		tvp = (struct timeval *) stackgap_alloc(sizeof tv);
591 		if ((error = copyout(tv, tvp, sizeof tv)))
592 			return error;
593 		SCARG(&ua, tptr) = tvp;
594 	}
595 	else
596 		SCARG(&ua, tptr) = NULL;
597 
598 	return utimes(p, uap, retval);
599 }
600 
601 /*
602  * Linux 'readdir' call. This code is mostly taken from the
603  * SunOS getdents call (see compat/sunos/sunos_misc.c), though
604  * an attempt has been made to keep it a little cleaner (failing
605  * miserably, because of the cruft needed if count 1 is passed).
606  *
607  * Read in BSD-style entries, convert them, and copy them out.
608  * Note that the Linux d_reclen is actually the name length,
609  * and d_off is the reclen.
610  *
611  * Note that this doesn't handle union-mounted filesystems.
612  */
613 int
614 linux_readdir(p, uap, retval)
615 	struct proc *p;
616 	struct linux_readdir_args /* {
617 		syscallarg(int) fd;
618 		syscallarg(struct linux_dirent *) dent;
619 		syscallarg(unsigned int) count;
620 	} */ *uap;
621 	register_t *retval;
622 {
623 	register struct dirent *bdp;
624 	struct vnode *vp;
625 	caddr_t	inp, buf;	/* BSD-format */
626 	int len, reclen;	/* BSD-format */
627 	caddr_t outp;		/* Linux-format */
628 	int resid, linuxreclen;	/* Linux-format */
629 	struct file *fp;
630 	struct uio auio;
631 	struct iovec aiov;
632 	struct linux_dirent idb;
633 	off_t off;		/* true file offset */
634 	linux_off_t soff;	/* Linux file offset */
635 	int buflen, error, eofflag, nbytes, justone;
636 	struct vattr va;
637 
638 	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
639 		return (error);
640 
641 	if ((fp->f_flag & FREAD) == 0)
642 		return (EBADF);
643 
644 	vp = (struct vnode *) fp->f_data;
645 
646 	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
647 		return (EINVAL);
648 
649 	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
650 		return error;
651 
652 	nbytes = SCARG(uap, count);
653 	if (nbytes == 1) {	/* Need this for older Linux libs, apparently */
654 		nbytes = sizeof (struct linux_dirent);
655 		justone = 1;
656 	}
657 	else
658 		justone = 0;
659 
660 	buflen = max(va.va_blocksize, nbytes);
661 	buf = malloc(buflen, M_TEMP, M_WAITOK);
662 	VOP_LOCK(vp);
663 	off = fp->f_offset;
664 again:
665 	aiov.iov_base = buf;
666 	aiov.iov_len = buflen;
667 	auio.uio_iov = &aiov;
668 	auio.uio_iovcnt = 1;
669 	auio.uio_rw = UIO_READ;
670 	auio.uio_segflg = UIO_SYSSPACE;
671 	auio.uio_procp = p;
672 	auio.uio_resid = buflen;
673 	auio.uio_offset = off;
674 	/*
675          * First we read into the malloc'ed buffer, then
676          * we massage it into user space, one record at a time.
677          */
678 	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *) 0, 0);
679 	if (error)
680 		goto out;
681 
682 	inp = buf;
683 	outp = (caddr_t) SCARG(uap, dent);
684 	resid = nbytes;
685 	if ((len = buflen - auio.uio_resid) == 0)
686 		goto eof;
687 
688 	for (; len > 0; len -= reclen) {
689 		reclen = ((struct dirent *) inp)->d_reclen;
690 		if (reclen & 3)
691 			panic("linux_readdir");
692 		off += reclen;	/* each entry points to next */
693 		bdp = (struct dirent *) inp;
694 		if (bdp->d_fileno == 0) {
695 			inp += reclen;	/* it is a hole; squish it out */
696 			continue;
697 		}
698 		linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
699 		if (reclen > len || resid < linuxreclen) {
700 			/* entry too big for buffer, so just stop */
701 			outp++;
702 			break;
703 		}
704 		/*
705 		 * Massage in place to make a Linux-shaped dirent (otherwise
706 		 * we have to worry about touching user memory outside of
707 		 * the copyout() call).
708 		 */
709 		idb.l_dino = (long) bdp->d_fileno;
710 		idb.l_doff = (linux_off_t) linuxreclen;
711 		idb.l_dreclen = (u_short) bdp->d_namlen;	/* sigh */
712 		strcpy(idb.l_dname, bdp->d_name);
713 		if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
714 			goto out;
715 		/* advance past this real entry */
716 		inp += reclen;
717 		/* advance output past Linux-shaped entry */
718 		outp += linuxreclen;
719 		resid -= linuxreclen;
720 		if (justone)
721 			break;
722 	}
723 
724 	/* if we squished out the whole block, try again */
725 	if (outp == (caddr_t) SCARG(uap, dent))
726 		goto again;
727 	fp->f_offset = off;	/* update the vnode offset */
728 
729 	if (justone)
730 		nbytes = resid + linuxreclen;
731 
732 eof:
733 	*retval = nbytes - resid;
734 out:
735 	VOP_UNLOCK(vp);
736 	free(buf, M_TEMP);
737 	return error;
738 }
739 
740 /*
741  * Out of register error once more.. Apart from that, no difference.
742  */
743 int
744 linux_select(p, uap, retval)
745 	struct proc *p;
746 	struct linux_select_args /* {
747 		syscallarg(struct linux_select *) lsp;
748 	} */ *uap;
749 	register_t *retval;
750 {
751 	struct linux_select ls;
752 	struct select_args bsa;
753 	int error;
754 
755 	if ((error = copyin(SCARG(uap, lsp), (caddr_t) &ls, sizeof ls)))
756 		return error;
757 
758 	SCARG(&bsa, nd) = ls.nfds;
759 	SCARG(&bsa, in) = ls.readfds;
760 	SCARG(&bsa, ou) = ls.writefds;
761 	SCARG(&bsa, ex) = ls.exceptfds;
762 	SCARG(&bsa, tv) = ls.timeout;
763 
764 	return select(p, &bsa, retval);
765 }
766 
767 /*
768  * Get the process group of a certain process. Look it up
769  * and return the value.
770  */
771 int
772 linux_getpgid(p, uap, retval)
773 	struct proc *p;
774 	struct linux_getpgid_args /* {
775 		syscallarg(int) pid;
776 	} */ *uap;
777 	register_t *retval;
778 {
779 	struct proc *targp;
780 
781 	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
782 		if ((targp = pfind(SCARG(uap, pid))) == 0)
783 			return ESRCH;
784 	else
785 		targp = p;
786 
787 	retval[0] = targp->p_pgid;
788 	return 0;
789 }
790