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