linux_pipe.c revision 1.11
1/*	$NetBSD: linux_pipe.c,v 1.11 1995/08/14 01:27:53 mycroft 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_signal.h>
75#include <compat/linux/linux_syscallargs.h>
76#include <compat/linux/linux_util.h>
77#include <compat/linux/linux_dirent.h>
78
79/*
80 * The information on a terminated (or stopped) process needs
81 * to be converted in order for Linux binaries to get a valid signal
82 * number out of it.
83 */
84static int
85bsd_to_linux_wstat(status)
86	int *status;
87{
88	if (WIFSIGNALED(*status))
89		*status = (*status & ~0177) |
90		    bsd_to_linux_sig(WTERMSIG(*status));
91	else if (WIFSTOPPED(*status))
92		*status = (*status & ~0xff00) |
93		    (bsd_to_linux_sig(WSTOPSIG(*status)) << 8);
94}
95
96/*
97 * waitpid(2). Passed on to the NetBSD call, surrounded by code to
98 * reserve some space for a NetBSD-style wait status, and converting
99 * it to what Linux wants.
100 */
101int
102linux_waitpid(p, uap, retval)
103	struct proc *p;
104	struct linux_waitpid_args /* {
105		syscallarg(int) pid;
106		syscallarg(int *) status;
107		syscallarg(int) options;
108	} */ *uap;
109	register_t *retval;
110{
111	struct wait4_args w4a;
112	int error, *status, tstat;
113	caddr_t sg;
114
115	sg = stackgap_init(p->p_emul);
116	status = (int *) stackgap_alloc(&sg, sizeof status);
117
118	SCARG(&w4a, pid) = SCARG(uap, pid);
119	SCARG(&w4a, status) = status;
120	SCARG(&w4a, options) = SCARG(uap, options);
121	SCARG(&w4a, rusage) = NULL;
122
123	if ((error = wait4(p, &w4a, retval)))
124		return error;
125
126	if ((error = copyin(status, &tstat, sizeof tstat)))
127		return error;
128
129	bsd_to_linux_wstat(&tstat);
130
131	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
132}
133
134/*
135 * This is very much the same as waitpid()
136 */
137int
138linux_wait4(p, uap, retval)
139	struct proc *p;
140	struct linux_wait4_args /* {
141		syscallarg(int) pid;
142		syscallarg(int *) status;
143		syscallarg(int) options;
144		syscallarg(struct rusage *) rusage;
145	} */ *uap;
146	register_t *retval;
147{
148	struct wait4_args w4a;
149	int error, *status, tstat;
150	caddr_t sg;
151
152	sg = stackgap_init(p->p_emul);
153	status = (int *) stackgap_alloc(&sg, sizeof status);
154
155	SCARG(&w4a, pid) = SCARG(uap, pid);
156	SCARG(&w4a, status) = status;
157	SCARG(&w4a, options) = SCARG(uap, options);
158	SCARG(&w4a, rusage) = SCARG(uap, rusage);
159
160	if ((error = wait4(p, &w4a, retval)))
161		return error;
162
163	if ((error = copyin(status, &tstat, sizeof tstat)))
164		return error;
165
166	bsd_to_linux_wstat(&tstat);
167
168	return copyout(&tstat, SCARG(uap, status), sizeof tstat);
169}
170
171/*
172 * This is the old brk(2) call. I don't think anything in the Linux
173 * world uses this anymore
174 */
175int
176linux_break(p, uap, retval)
177	struct proc *p;
178	struct linux_brk_args /* {
179		syscallarg(char *) nsize;
180	} */ *uap;
181	register_t *retval;
182{
183	return ENOSYS;
184}
185
186/*
187 * Linux brk(2). The check if the new address is >= the old one is
188 * done in the kernel in Linux. NetBSD does it in the library.
189 */
190int
191linux_brk(p, uap, retval)
192	struct proc *p;
193	struct linux_brk_args /* {
194		syscallarg(char *) nsize;
195	} */ *uap;
196	register_t *retval;
197{
198	char *nbrk = SCARG(uap, nsize);
199	struct obreak_args oba;
200	struct vmspace *vm = p->p_vmspace;
201	int error = 0;
202	caddr_t oldbrk, newbrk;
203
204	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
205	/*
206	 * XXX inconsistent.. Linux always returns at least the old
207	 * brk value, but it will be page-aligned if this fails,
208	 * and possibly not page aligned if it succeeds (the user
209	 * supplied pointer is returned).
210	 */
211	SCARG(&oba, nsize) = nbrk;
212
213	if ((caddr_t) nbrk > vm->vm_daddr && obreak(p, &oba, retval) == 0)
214		retval[0] = (register_t) nbrk;
215	else
216		retval[0] = (register_t) oldbrk;
217
218	return 0;
219}
220
221/*
222 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
223 * need to deal with it.
224 */
225int
226linux_time(p, uap, retval)
227	struct proc *p;
228	struct linux_time_args /* {
229		linux_time_t *t;
230	} */ *uap;
231	register_t *retval;
232{
233	struct timeval atv;
234	linux_time_t tt;
235	int error;
236
237	microtime(&atv);
238
239	tt = atv.tv_sec;
240	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
241		return error;
242
243	retval[0] = tt;
244	return 0;
245}
246
247/*
248 * Convert BSD statfs structure to Linux statfs structure.
249 * The Linux structure has less fields, and it also wants
250 * the length of a name in a dir entry in a field, which
251 * we fake (probably the wrong way).
252 */
253static void
254bsd_to_linux_statfs(bsp, lsp)
255	struct statfs *bsp;
256	struct linux_statfs *lsp;
257{
258	lsp->l_ftype = bsp->f_type;
259	lsp->l_fbsize = bsp->f_bsize;
260	lsp->l_fblocks = bsp->f_blocks;
261	lsp->l_fbfree = bsp->f_bfree;
262	lsp->l_fbavail = bsp->f_bavail;
263	lsp->l_ffiles = bsp->f_files;
264	lsp->l_fffree = bsp->f_ffree;
265	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
266	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
267	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
268}
269
270/*
271 * Implement the fs stat functions. Straightforward.
272 */
273int
274linux_statfs(p, uap, retval)
275	struct proc *p;
276	struct linux_statfs_args /* {
277		syscallarg(char *) path;
278		syscallarg(struct linux_statfs *) sp;
279	} */ *uap;
280	register_t *retval;
281{
282	struct statfs btmp, *bsp;
283	struct linux_statfs ltmp;
284	struct statfs_args bsa;
285	caddr_t sg;
286	int error;
287
288	sg = stackgap_init(p->p_emul);
289	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
290
291	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
292
293	SCARG(&bsa, path) = SCARG(uap, path);
294	SCARG(&bsa, buf) = bsp;
295
296	if ((error = statfs(p, &bsa, retval)))
297		return error;
298
299	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
300		return error;
301
302	bsd_to_linux_statfs(&btmp, &ltmp);
303
304	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
305}
306
307int
308linux_fstatfs(p, uap, retval)
309	struct proc *p;
310	struct linux_fstatfs_args /* {
311		syscallarg(int) fd;
312		syscallarg(struct linux_statfs *) sp;
313	} */ *uap;
314	register_t *retval;
315{
316	struct statfs btmp, *bsp;
317	struct linux_statfs ltmp;
318	struct fstatfs_args bsa;
319	caddr_t sg;
320	int error;
321
322	sg = stackgap_init(p->p_emul);
323	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
324
325	SCARG(&bsa, fd) = SCARG(uap, fd);
326	SCARG(&bsa, buf) = bsp;
327
328	if ((error = statfs(p, &bsa, retval)))
329		return error;
330
331	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
332		return error;
333
334	bsd_to_linux_statfs(&btmp, &ltmp);
335
336	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
337}
338
339/*
340 * uname(). Just copy the info from the various strings stored in the
341 * kernel, and put it in the Linux utsname structure. That structure
342 * is almost the same as the NetBSD one, only it has fields 65 characters
343 * long, and an extra domainname field.
344 */
345int
346linux_uname(p, uap, retval)
347	struct proc *p;
348	struct linux_uname_args /* {
349		syscallarg(struct linux_utsname *) up;
350	} */ *uap;
351	register_t *retval;
352{
353	extern char ostype[], osrelease[], version[], hostname[], domainname[];
354	extern char machine[];
355	struct linux_utsname tluts;
356	int len;
357	char *cp;
358
359	strncpy(tluts.l_sysname, ostype, sizeof (tluts.l_sysname));
360	strncpy(tluts.l_nodename, hostname, sizeof (tluts.l_nodename));
361	strncpy(tluts.l_release, osrelease, sizeof (tluts.l_release));
362	strncpy(tluts.l_machine, machine, sizeof (tluts.l_machine));
363	strncpy(tluts.l_domainname, domainname, sizeof (tluts.l_domainname));
364	strncpy(tluts.l_version, version, sizeof (tluts.l_version));
365
366	/* This part taken from the the uname() in libc */
367	len = sizeof (tluts.l_version);
368	for (cp = tluts.l_version; len--; ++cp)
369		if (*cp == '\n' || *cp == '\t')
370			if (len > 1)
371				*cp = ' ';
372			else
373				*cp = '\0';
374
375	return copyout(&tluts, SCARG(uap, up), sizeof tluts);
376}
377
378/*
379 * Linux wants to pass everything to a syscall in registers. However,
380 * mmap() has 6 of them. Oops: out of register error. They just pass
381 * everything in a structure.
382 */
383int
384linux_mmap(p, uap, retval)
385	struct proc *p;
386	struct linux_mmap_args /* {
387		syscallarg(struct linux_mmap *) lmp;
388	} */ *uap;
389	register_t *retval;
390{
391	struct linux_mmap lmap;
392	struct mmap_args cma;
393	int error, flags;
394
395	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
396		return error;
397
398	flags = 0;
399	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
400	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
401	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
402	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
403
404	SCARG(&cma,addr) = lmap.lm_addr;
405	SCARG(&cma,len) = lmap.lm_len;
406 	SCARG(&cma,prot) = lmap.lm_prot;
407	SCARG(&cma,flags) = flags;
408	SCARG(&cma,fd) = lmap.lm_fd;
409	SCARG(&cma,pad) = 0;
410	SCARG(&cma,pos) = lmap.lm_pos;
411
412	return mmap(p, &cma, retval);
413}
414
415/*
416 * Linux doesn't use the retval[1] value to determine whether
417 * we are the child or parent.
418 */
419int
420linux_fork(p, uap, retval)
421	struct proc *p;
422	void *uap;
423	register_t *retval;
424{
425	int error;
426
427	if ((error = fork(p, uap, retval)))
428		return error;
429
430	if (retval[1] == 1)
431		retval[0] = 0;
432
433	return 0;
434}
435
436/*
437 * This code is partly stolen from src/lib/libc/compat-43/times.c
438 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
439 */
440
441#define CLK_TCK 100
442#define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
443
444int
445linux_times(p, uap, retval)
446	struct proc *p;
447	struct linux_times_args /* {
448		syscallarg(struct times *) tms;
449	} */ *uap;
450	register_t *retval;
451{
452	struct timeval t;
453	struct linux_tms ltms;
454	struct rusage ru;
455	int error, s;
456
457	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
458	ltms.ltms_utime = CONVTCK(ru.ru_utime);
459	ltms.ltms_stime = CONVTCK(ru.ru_stime);
460
461	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
462	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
463
464	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
465		return error;
466
467	s = splclock();
468	timersub(&time, &boottime, &t);
469	splx(s);
470
471	retval[0] = ((linux_clock_t)(CONVTCK(t)));
472	return 0;
473}
474
475/*
476 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
477 * Linux directly passes the pointer.
478 */
479int
480linux_pipe(p, uap, retval)
481	struct proc *p;
482	struct linux_pipe_args /* {
483		syscallarg(int *) pfds;
484	} */ *uap;
485	register_t *retval;
486{
487	int error;
488
489	if ((error = pipe(p, 0, retval)))
490		return error;
491
492	/* Assumes register_t is an int */
493
494	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
495		return error;
496
497	retval[0] = 0;
498	return 0;
499}
500
501/*
502 * Alarm. This is a libc call which used setitimer(2) in NetBSD.
503 * Fiddle with the timers to make it work.
504 */
505int
506linux_alarm(p, uap, retval)
507	struct proc *p;
508	struct linux_alarm_args /* {
509		syscallarg(unsigned int) secs;
510	} */ *uap;
511	register_t *retval;
512{
513	int error, s;
514	struct itimerval *itp, it;
515
516	itp = &p->p_realtimer;
517	s = splclock();
518	/*
519	 * Clear any pending timer alarms.
520	 */
521	untimeout(realitexpire, p);
522	timerclear(&itp->it_interval);
523	if (timerisset(&itp->it_value) &&
524	    timercmp(&itp->it_value, &time, >))
525		timersub(&itp->it_value, &time, &itp->it_value);
526	/*
527	 * Return how many seconds were left (rounded up)
528	 */
529	retval[0] = itp->it_value.tv_sec;
530	if (itp->it_value.tv_usec)
531		retval[0]++;
532
533	/*
534	 * alarm(0) just resets the timer.
535	 */
536	if (SCARG(uap, secs) == 0) {
537		timerclear(&itp->it_value);
538		splx(s);
539		return 0;
540	}
541
542	/*
543	 * Check the new alarm time for sanity, and set it.
544	 */
545	timerclear(&it.it_interval);
546	it.it_value.tv_sec = SCARG(uap, secs);
547	it.it_value.tv_usec = 0;
548	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
549		splx(s);
550		return (EINVAL);
551	}
552
553	if (timerisset(&it.it_value)) {
554		timeradd(&it.it_value, &time, &it.it_value);
555		timeout(realitexpire, p, hzto(&it.it_value));
556	}
557	p->p_realtimer = it;
558	splx(s);
559
560	return 0;
561}
562
563/*
564 * utime(). Do conversion to things that utimes() understands,
565 * and pass it on.
566 */
567int
568linux_utime(p, uap, retval)
569	struct proc *p;
570	struct linux_utime_args /* {
571		syscallarg(char *) path;
572		syscallarg(struct linux_utimbuf *)times;
573	} */ *uap;
574	register_t *retval;
575{
576	caddr_t sg;
577	int error;
578	struct utimes_args ua;
579	struct timeval tv[2], *tvp;
580	struct linux_utimbuf lut;
581
582	sg = stackgap_init(p->p_emul);
583	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
584
585	SCARG(&ua, path) = SCARG(uap, path);
586
587	if (SCARG(uap, times) != NULL) {
588		if ((error = copyin(SCARG(uap, times), &lut, sizeof lut)))
589			return error;
590		tv[0].tv_usec = tv[1].tv_usec = 0;
591		tv[0].tv_sec = lut.l_actime;
592		tv[1].tv_sec = lut.l_modtime;
593		tvp = (struct timeval *) stackgap_alloc(&sg, sizeof(tv));
594		if ((error = copyout(tv, tvp, sizeof tv)))
595			return error;
596		SCARG(&ua, tptr) = tvp;
597	}
598	else
599		SCARG(&ua, tptr) = NULL;
600
601	return utimes(p, uap, retval);
602}
603
604/*
605 * Linux 'readdir' call. This code is mostly taken from the
606 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
607 * an attempt has been made to keep it a little cleaner (failing
608 * miserably, because of the cruft needed if count 1 is passed).
609 *
610 * Read in BSD-style entries, convert them, and copy them out.
611 * Note that the Linux d_reclen is actually the name length,
612 * and d_off is the reclen.
613 *
614 * Note that this doesn't handle union-mounted filesystems.
615 */
616int
617linux_readdir(p, uap, retval)
618	struct proc *p;
619	struct linux_readdir_args /* {
620		syscallarg(int) fd;
621		syscallarg(struct linux_dirent *) dent;
622		syscallarg(unsigned int) count;
623	} */ *uap;
624	register_t *retval;
625{
626	register struct dirent *bdp;
627	struct vnode *vp;
628	caddr_t	inp, buf;	/* BSD-format */
629	int len, reclen;	/* BSD-format */
630	caddr_t outp;		/* Linux-format */
631	int resid, linuxreclen;	/* Linux-format */
632	struct file *fp;
633	struct uio auio;
634	struct iovec aiov;
635	struct linux_dirent idb;
636	off_t off;		/* true file offset */
637	linux_off_t soff;	/* Linux file offset */
638	int buflen, error, eofflag, nbytes, justone;
639	struct vattr va;
640
641	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
642		return (error);
643
644	if ((fp->f_flag & FREAD) == 0)
645		return (EBADF);
646
647	vp = (struct vnode *)fp->f_data;
648
649	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
650		return (EINVAL);
651
652	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
653		return error;
654
655	nbytes = SCARG(uap, count);
656	if (nbytes == 1) {	/* Need this for older Linux libs, apparently */
657		nbytes = sizeof (struct linux_dirent);
658		buflen = max(va.va_blocksize, nbytes);
659		justone = 1;
660	} else {
661		buflen = min(MAXBSIZE, nbytes);
662		justone = 0;
663	}
664	buf = malloc(buflen, M_TEMP, M_WAITOK);
665	VOP_LOCK(vp);
666	off = fp->f_offset;
667again:
668	aiov.iov_base = buf;
669	aiov.iov_len = buflen;
670	auio.uio_iov = &aiov;
671	auio.uio_iovcnt = 1;
672	auio.uio_rw = UIO_READ;
673	auio.uio_segflg = UIO_SYSSPACE;
674	auio.uio_procp = p;
675	auio.uio_resid = buflen;
676	auio.uio_offset = off;
677	/*
678         * First we read into the malloc'ed buffer, then
679         * we massage it into user space, one record at a time.
680         */
681	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
682	if (error)
683		goto out;
684
685	inp = buf;
686	outp = (caddr_t) SCARG(uap, dent);
687	resid = nbytes;
688	if ((len = buflen - auio.uio_resid) == 0)
689		goto eof;
690
691	for (; len > 0; len -= reclen) {
692		bdp = (struct dirent *)inp;
693		reclen = bdp->d_reclen;
694		if (reclen & 3)
695			panic("linux_readdir");
696		off += reclen;	/* each entry points to next */
697		if (bdp->d_fileno == 0) {
698			inp += reclen;	/* it is a hole; squish it out */
699			continue;
700		}
701		linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
702		if (reclen > len || resid < linuxreclen) {
703			/* entry too big for buffer, so just stop */
704			outp++;
705			break;
706		}
707		/*
708		 * Massage in place to make a Linux-shaped dirent (otherwise
709		 * we have to worry about touching user memory outside of
710		 * the copyout() call).
711		 */
712		idb.d_ino = (long)bdp->d_fileno;
713		idb.d_off = (linux_off_t)linuxreclen;
714		idb.d_reclen = (u_short)bdp->d_namlen;
715		strcpy(idb.d_name, bdp->d_name);
716		if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
717			goto out;
718		/* advance past this real entry */
719		inp += reclen;
720		/* advance output past Linux-shaped entry */
721		outp += linuxreclen;
722		resid -= linuxreclen;
723		if (justone)
724			break;
725	}
726
727	/* if we squished out the whole block, try again */
728	if (outp == (caddr_t) SCARG(uap, dent))
729		goto again;
730	fp->f_offset = off;	/* update the vnode offset */
731
732	if (justone)
733		nbytes = resid + linuxreclen;
734
735eof:
736	*retval = nbytes - resid;
737out:
738	VOP_UNLOCK(vp);
739	free(buf, M_TEMP);
740	return error;
741}
742
743/*
744 * Out of register error once more.. Also, Linux copies the amount of
745 * time left into the user-supplied timeval structure.
746 */
747int
748linux_select(p, uap, retval)
749	struct proc *p;
750	struct linux_select_args /* {
751		syscallarg(struct linux_select *) lsp;
752	} */ *uap;
753	register_t *retval;
754{
755	struct linux_select ls;
756	struct select_args bsa;
757	struct timeval tv0, tv1, utv;
758	int error;
759
760	if ((error = copyin(SCARG(uap, lsp), (caddr_t)&ls, sizeof ls)))
761		return error;
762
763	SCARG(&bsa, nd) = ls.nfds;
764	SCARG(&bsa, in) = ls.readfds;
765	SCARG(&bsa, ou) = ls.writefds;
766	SCARG(&bsa, ex) = ls.exceptfds;
767	SCARG(&bsa, tv) = ls.timeout;
768
769	/*
770	 * Store current time for computation of the amount of
771	 * time left.
772	 */
773	if (ls.timeout)
774		microtime(&tv0);
775
776	error = select(p, &bsa, retval);
777	if (error) {
778		/*
779		 * See fs/select.c in the Linux kernel.  Without this,
780		 * Maelstrom doesn't work.
781		 */
782		if (error == ERESTART)
783			error = EINTR;
784		return error;
785	}
786
787	if (ls.timeout) {
788		if (!*retval) {
789			utv.tv_sec = 0;
790			utv.tv_usec = 0;
791		} else {
792			/*
793			 * Compute how many time was left of the timeout,
794			 * by subtracting the current time and the time
795			 * before we started the call, and subtracting
796			 * that result from the user-supplied value.
797			 */
798			microtime(&tv1);
799			if ((error = copyin((caddr_t)ls.timeout, (caddr_t)&utv,
800			    sizeof utv)))
801				return error;
802			timersub(&tv1, &tv0, &tv1);
803			timersub(&utv, &tv1, &utv);
804		}
805		if ((error = copyout((caddr_t)&utv, (caddr_t)ls.timeout,
806		    sizeof utv)))
807			return error;
808	}
809	return 0;
810}
811
812/*
813 * Get the process group of a certain process. Look it up
814 * and return the value.
815 */
816int
817linux_getpgid(p, uap, retval)
818	struct proc *p;
819	struct linux_getpgid_args /* {
820		syscallarg(int) pid;
821	} */ *uap;
822	register_t *retval;
823{
824	struct proc *targp;
825
826	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
827		if ((targp = pfind(SCARG(uap, pid))) == 0)
828			return ESRCH;
829	else
830		targp = p;
831
832	retval[0] = targp->p_pgid;
833	return 0;
834}
835
836/*
837 * Set the 'personality' (emulation mode) for the current process. Only
838 * accept the Linux personality here (0). This call is needed because
839 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
840 * ELF binaries run in Linux mode, not SVR4 mode.
841 */
842int
843linux_personality(p, uap, retval)
844	struct proc *p;
845	struct linux_personality_args /* P
846		syscallarg(int) per;
847	} */ *uap;
848	register_t *retval;
849{
850	if (SCARG(uap, per) != 0)
851		return EINVAL;
852	retval[0] = 0;
853	return 0;
854}
855