linux_pipe.c revision 1.17
1/*	$NetBSD: linux_pipe.c,v 1.17 1995/08/23 20:17:28 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 * The old Linux readdir was only able to read one entry at a time,
682 * even though it had a 'count' argument. In fact, the emulation
683 * of the old call was better than the original, because it did handle
684 * the count arg properly. Don't bother with it anymore now, and use
685 * it to distinguish between old and new. The difference is that the
686 * newer one actually does multiple entries, and the reclen field
687 * really is the reclen, not the namelength.
688 */
689int
690linux_readdir(p, uap, retval)
691	struct proc *p;
692	struct linux_readdir_args /* {
693		syscallarg(int) fd;
694		syscallarg(struct linux_dirent *) dent;
695		syscallarg(unsigned int) count;
696	} */ *uap;
697	register_t *retval;
698{
699
700	SCARG(uap, count) = 1;
701	return linux_getdents(p, uap, retval);
702}
703
704/*
705 * Linux 'readdir' call. This code is mostly taken from the
706 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
707 * an attempt has been made to keep it a little cleaner (failing
708 * miserably, because of the cruft needed if count 1 is passed).
709 *
710 * The d_off field should contain the offset of the next valid entry,
711 * but in Linux it has the offset of the entry itself. We emulate
712 * that bug here.
713 *
714 * Read in BSD-style entries, convert them, and copy them out.
715 *
716 * Note that this doesn't handle union-mounted filesystems.
717 */
718int
719linux_getdents(p, uap, retval)
720	struct proc *p;
721	struct linux_readdir_args /* {
722		syscallarg(int) fd;
723		syscallarg(struct linux_dirent *) dent;
724		syscallarg(unsigned int) count;
725	} */ *uap;
726	register_t *retval;
727{
728	register struct dirent *bdp;
729	struct vnode *vp;
730	caddr_t	inp, buf;	/* BSD-format */
731	int len, reclen;	/* BSD-format */
732	caddr_t outp;		/* Linux-format */
733	int resid, linuxreclen;	/* Linux-format */
734	struct file *fp;
735	struct uio auio;
736	struct iovec aiov;
737	struct linux_dirent idb;
738	off_t off;		/* true file offset */
739	linux_off_t soff;	/* Linux file offset */
740	int buflen, error, eofflag, nbytes, oldcall;
741	struct vattr va;
742
743	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
744		return (error);
745
746	if ((fp->f_flag & FREAD) == 0)
747		return (EBADF);
748
749	vp = (struct vnode *)fp->f_data;
750
751	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
752		return (EINVAL);
753
754	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
755		return error;
756
757	nbytes = SCARG(uap, count);
758	if (nbytes == 1) {	/* emulating old, broken behaviour */
759		nbytes = sizeof (struct linux_dirent);
760		buflen = max(va.va_blocksize, nbytes);
761		oldcall = 1;
762	} else {
763		buflen = min(MAXBSIZE, nbytes);
764		oldcall = 0;
765	}
766	buf = malloc(buflen, M_TEMP, M_WAITOK);
767	VOP_LOCK(vp);
768	off = fp->f_offset;
769again:
770	aiov.iov_base = buf;
771	aiov.iov_len = buflen;
772	auio.uio_iov = &aiov;
773	auio.uio_iovcnt = 1;
774	auio.uio_rw = UIO_READ;
775	auio.uio_segflg = UIO_SYSSPACE;
776	auio.uio_procp = p;
777	auio.uio_resid = buflen;
778	auio.uio_offset = off;
779	/*
780         * First we read into the malloc'ed buffer, then
781         * we massage it into user space, one record at a time.
782         */
783	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, (u_long *)0, 0);
784	if (error)
785		goto out;
786
787	inp = buf;
788	outp = (caddr_t) SCARG(uap, dent);
789	resid = nbytes;
790	if ((len = buflen - auio.uio_resid) == 0)
791		goto eof;
792
793	for (; len > 0; len -= reclen) {
794		bdp = (struct dirent *)inp;
795		reclen = bdp->d_reclen;
796		if (reclen & 3)
797			panic("linux_readdir");
798		if (bdp->d_fileno == 0) {
799			inp += reclen;	/* it is a hole; squish it out */
800			continue;
801		}
802		linuxreclen = LINUX_RECLEN(&idb, bdp->d_namlen);
803		if (reclen > len || resid < linuxreclen) {
804			/* entry too big for buffer, so just stop */
805			outp++;
806			break;
807		}
808		/*
809		 * Massage in place to make a Linux-shaped dirent (otherwise
810		 * we have to worry about touching user memory outside of
811		 * the copyout() call).
812		 */
813		idb.d_ino = (long)bdp->d_fileno;
814		idb.d_off = off;
815		/*
816		 * The old readdir() call used the reclen field as namlen.
817		 */
818		idb.d_reclen = oldcall ? (u_short)bdp->d_namlen : linuxreclen;
819		strcpy(idb.d_name, bdp->d_name);
820		if ((error = copyout((caddr_t)&idb, outp, linuxreclen)))
821			goto out;
822		/* advance past this real entry */
823		inp += reclen;
824		off += reclen;
825		/* advance output past Linux-shaped entry */
826		outp += linuxreclen;
827		resid -= linuxreclen;
828		if (oldcall)
829			break;
830	}
831
832	/* if we squished out the whole block, try again */
833	if (outp == (caddr_t) SCARG(uap, dent))
834		goto again;
835	fp->f_offset = off;	/* update the vnode offset */
836
837	if (oldcall)
838		nbytes = resid + linuxreclen;
839
840eof:
841	*retval = nbytes - resid;
842out:
843	VOP_UNLOCK(vp);
844	free(buf, M_TEMP);
845	return error;
846}
847
848/*
849 * Not sure why the arguments to this older version of select() were put
850 * into a structure, because there are 5, and that can all be handled
851 * in registers on the i386 like Linux wants to.
852 */
853int
854linux_oldselect(p, uap, retval)
855	struct proc *p;
856	struct linux_oldselect_args /* {
857		syscallarg(struct linux_select *) lsp;
858	} */ *uap;
859	register_t *retval;
860{
861	struct linux_select ls;
862	int error;
863
864	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
865		return error;
866
867	return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
868	    ls.exceptfds, ls.timeout);
869}
870
871/*
872 * Even when just using registers to pass arguments to syscalls you can
873 * have 5 of them on the i386. So this newer version of select() does
874 * this.
875 */
876int
877linux_select(p, uap, retval)
878	struct proc *p;
879	struct linux_select_args /* {
880		syscallarg(int) nfds;
881		syscallarg(fd_set *) readfds;
882		syscallarg(fd_set *) writefds;
883		syscallarg(fd_set *) exceptfds;
884		syscallarg(struct timeval *) timeout;
885	} */ *uap;
886	register_t *retval;
887{
888	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
889	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
890}
891
892/*
893 * Common code for the old and new versions of select(). A couple of
894 * things are important:
895 * 1) return the amount of time left in the 'timeout' parameter
896 * 2) select never returns ERESTART on Linux, always return EINTR
897 */
898int
899linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
900	struct proc *p;
901	register_t *retval;
902	int nfds;
903	fd_set *readfds, *writefds, *exceptfds;
904	struct timeval *timeout;
905{
906	struct select_args bsa;
907	struct timeval tv0, tv1, utv, *tvp;
908	caddr_t sg;
909	int error;
910
911	SCARG(&bsa, nd) = nfds;
912	SCARG(&bsa, in) = readfds;
913	SCARG(&bsa, ou) = writefds;
914	SCARG(&bsa, ex) = exceptfds;
915	SCARG(&bsa, tv) = timeout;
916
917	/*
918	 * Store current time for computation of the amount of
919	 * time left.
920	 */
921	if (timeout) {
922		if ((error = copyin(timeout, &utv, sizeof(utv))))
923			return error;
924		if (itimerfix(&utv)) {
925			/*
926			 * The timeval was invalid.  Convert it to something
927			 * valid that will act as it does under Linux.
928			 */
929			sg = stackgap_init(p->p_emul);
930			tvp = stackgap_alloc(&sg, sizeof(utv));
931			utv.tv_sec += utv.tv_usec / 1000000;
932			utv.tv_usec %= 1000000;
933			if (utv.tv_usec < 0) {
934				utv.tv_sec -= 1;
935				utv.tv_usec += 1000000;
936			}
937			if (utv.tv_sec < 0)
938				timerclear(&utv);
939			if ((error = copyout(&utv, tvp, sizeof(utv))))
940				return error;
941			SCARG(&bsa, tv) = tvp;
942		}
943		microtime(&tv0);
944	}
945
946	error = select(p, &bsa, retval);
947	if (error) {
948		/*
949		 * See fs/select.c in the Linux kernel.  Without this,
950		 * Maelstrom doesn't work.
951		 */
952		if (error == ERESTART)
953			error = EINTR;
954		return error;
955	}
956
957	if (timeout) {
958		if (*retval) {
959			/*
960			 * Compute how much time was left of the timeout,
961			 * by subtracting the current time and the time
962			 * before we started the call, and subtracting
963			 * that result from the user-supplied value.
964			 */
965			microtime(&tv1);
966			timersub(&tv1, &tv0, &tv1);
967			timersub(&utv, &tv1, &utv);
968			if (utv.tv_sec < 0)
969				timerclear(&utv);
970		} else
971			timerclear(&utv);
972		if ((error = copyout(&utv, timeout, sizeof(utv))))
973			return error;
974	}
975
976	return 0;
977}
978
979/*
980 * Get the process group of a certain process. Look it up
981 * and return the value.
982 */
983int
984linux_getpgid(p, uap, retval)
985	struct proc *p;
986	struct linux_getpgid_args /* {
987		syscallarg(int) pid;
988	} */ *uap;
989	register_t *retval;
990{
991	struct proc *targp;
992
993	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
994		if ((targp = pfind(SCARG(uap, pid))) == 0)
995			return ESRCH;
996	else
997		targp = p;
998
999	retval[0] = targp->p_pgid;
1000	return 0;
1001}
1002
1003/*
1004 * Set the 'personality' (emulation mode) for the current process. Only
1005 * accept the Linux personality here (0). This call is needed because
1006 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1007 * ELF binaries run in Linux mode, not SVR4 mode.
1008 */
1009int
1010linux_personality(p, uap, retval)
1011	struct proc *p;
1012	struct linux_personality_args /* P
1013		syscallarg(int) per;
1014	} */ *uap;
1015	register_t *retval;
1016{
1017	if (SCARG(uap, per) != 0)
1018		return EINVAL;
1019	retval[0] = 0;
1020	return 0;
1021}
1022