linux_pipe.c revision 1.23
1/*	$NetBSD: linux_pipe.c,v 1.23 1995/12/09 04:01:42 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
89	if (WIFSIGNALED(*status))
90		*status = (*status & ~0177) |
91		    bsd_to_linux_sig[WTERMSIG(*status)];
92	else if (WIFSTOPPED(*status))
93		*status = (*status & ~0xff00) |
94		    (bsd_to_linux_sig[WSTOPSIG(*status)] << 8);
95}
96
97/*
98 * waitpid(2). Passed on to the NetBSD call, surrounded by code to
99 * reserve some space for a NetBSD-style wait status, and converting
100 * it to what Linux wants.
101 */
102int
103linux_sys_waitpid(p, v, retval)
104	struct proc *p;
105	void *v;
106	register_t *retval;
107{
108	struct linux_sys_waitpid_args /* {
109		syscallarg(int) pid;
110		syscallarg(int *) status;
111		syscallarg(int) options;
112	} */ *uap = v;
113	struct sys_wait4_args w4a;
114	int error, *status, tstat;
115	caddr_t sg;
116
117	if (SCARG(uap, status) != NULL) {
118		sg = stackgap_init(p->p_emul);
119		status = (int *) stackgap_alloc(&sg, sizeof status);
120	} else
121		status = NULL;
122
123	SCARG(&w4a, pid) = SCARG(uap, pid);
124	SCARG(&w4a, status) = status;
125	SCARG(&w4a, options) = SCARG(uap, options);
126	SCARG(&w4a, rusage) = NULL;
127
128	if ((error = sys_wait4(p, &w4a, retval)))
129		return error;
130
131	p->p_siglist &= ~sigmask(SIGCHLD);
132
133	if (status != NULL) {
134		if ((error = copyin(status, &tstat, sizeof tstat)))
135			return error;
136
137		bsd_to_linux_wstat(&tstat);
138		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
139	}
140
141	return 0;
142}
143
144/*
145 * This is very much the same as waitpid()
146 */
147int
148linux_sys_wait4(p, v, retval)
149	struct proc *p;
150	void *v;
151	register_t *retval;
152{
153	struct linux_sys_wait4_args /* {
154		syscallarg(int) pid;
155		syscallarg(int *) status;
156		syscallarg(int) options;
157		syscallarg(struct rusage *) rusage;
158	} */ *uap = v;
159	struct sys_wait4_args w4a;
160	int error, *status, tstat;
161	caddr_t sg;
162
163	if (SCARG(uap, status) != NULL) {
164		sg = stackgap_init(p->p_emul);
165		status = (int *) stackgap_alloc(&sg, sizeof status);
166	} else
167		status = NULL;
168
169	SCARG(&w4a, pid) = SCARG(uap, pid);
170	SCARG(&w4a, status) = status;
171	SCARG(&w4a, options) = SCARG(uap, options);
172	SCARG(&w4a, rusage) = SCARG(uap, rusage);
173
174	if ((error = sys_wait4(p, &w4a, retval)))
175		return error;
176
177	p->p_siglist &= ~sigmask(SIGCHLD);
178
179	if (status != NULL) {
180		if ((error = copyin(status, &tstat, sizeof tstat)))
181			return error;
182
183		bsd_to_linux_wstat(&tstat);
184
185		return copyout(&tstat, SCARG(uap, status), sizeof tstat);
186	}
187
188	return 0;
189}
190
191/*
192 * This is the old brk(2) call. I don't think anything in the Linux
193 * world uses this anymore
194 */
195int
196linux_sys_break(p, v, retval)
197	struct proc *p;
198	void *v;
199	register_t *retval;
200{
201	struct linux_sys_brk_args /* {
202		syscallarg(char *) nsize;
203	} */ *uap = v;
204
205	return ENOSYS;
206}
207
208/*
209 * Linux brk(2). The check if the new address is >= the old one is
210 * done in the kernel in Linux. NetBSD does it in the library.
211 */
212int
213linux_sys_brk(p, v, retval)
214	struct proc *p;
215	void *v;
216	register_t *retval;
217{
218	struct linux_sys_brk_args /* {
219		syscallarg(char *) nsize;
220	} */ *uap = v;
221	char *nbrk = SCARG(uap, nsize);
222	struct sys_obreak_args oba;
223	struct vmspace *vm = p->p_vmspace;
224	int error = 0;
225	caddr_t oldbrk, newbrk;
226
227	oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
228	/*
229	 * XXX inconsistent.. Linux always returns at least the old
230	 * brk value, but it will be page-aligned if this fails,
231	 * and possibly not page aligned if it succeeds (the user
232	 * supplied pointer is returned).
233	 */
234	SCARG(&oba, nsize) = nbrk;
235
236	if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
237		retval[0] = (register_t)nbrk;
238	else
239		retval[0] = (register_t)oldbrk;
240
241	return 0;
242}
243
244/*
245 * I wonder why Linux has gettimeofday() _and_ time().. Still, we
246 * need to deal with it.
247 */
248int
249linux_sys_time(p, v, retval)
250	struct proc *p;
251	void *v;
252	register_t *retval;
253{
254	struct linux_sys_time_args /* {
255		linux_time_t *t;
256	} */ *uap = v;
257	struct timeval atv;
258	linux_time_t tt;
259	int error;
260
261	microtime(&atv);
262
263	tt = atv.tv_sec;
264	if (SCARG(uap, t) && (error = copyout(&tt, SCARG(uap, t), sizeof tt)))
265		return error;
266
267	retval[0] = tt;
268	return 0;
269}
270
271/*
272 * Convert BSD statfs structure to Linux statfs structure.
273 * The Linux structure has less fields, and it also wants
274 * the length of a name in a dir entry in a field, which
275 * we fake (probably the wrong way).
276 */
277static void
278bsd_to_linux_statfs(bsp, lsp)
279	struct statfs *bsp;
280	struct linux_statfs *lsp;
281{
282
283	lsp->l_ftype = bsp->f_type;
284	lsp->l_fbsize = bsp->f_bsize;
285	lsp->l_fblocks = bsp->f_blocks;
286	lsp->l_fbfree = bsp->f_bfree;
287	lsp->l_fbavail = bsp->f_bavail;
288	lsp->l_ffiles = bsp->f_files;
289	lsp->l_fffree = bsp->f_ffree;
290	lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
291	lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
292	lsp->l_fnamelen = MAXNAMLEN;	/* XXX */
293}
294
295/*
296 * Implement the fs stat functions. Straightforward.
297 */
298int
299linux_sys_statfs(p, v, retval)
300	struct proc *p;
301	void *v;
302	register_t *retval;
303{
304	struct linux_sys_statfs_args /* {
305		syscallarg(char *) path;
306		syscallarg(struct linux_statfs *) sp;
307	} */ *uap = v;
308	struct statfs btmp, *bsp;
309	struct linux_statfs ltmp;
310	struct sys_statfs_args bsa;
311	caddr_t sg;
312	int error;
313
314	sg = stackgap_init(p->p_emul);
315	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
316
317	LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
318
319	SCARG(&bsa, path) = SCARG(uap, path);
320	SCARG(&bsa, buf) = bsp;
321
322	if ((error = sys_statfs(p, &bsa, retval)))
323		return error;
324
325	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
326		return error;
327
328	bsd_to_linux_statfs(&btmp, &ltmp);
329
330	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
331}
332
333int
334linux_sys_fstatfs(p, v, retval)
335	struct proc *p;
336	void *v;
337	register_t *retval;
338{
339	struct linux_sys_fstatfs_args /* {
340		syscallarg(int) fd;
341		syscallarg(struct linux_statfs *) sp;
342	} */ *uap = v;
343	struct statfs btmp, *bsp;
344	struct linux_statfs ltmp;
345	struct sys_fstatfs_args bsa;
346	caddr_t sg;
347	int error;
348
349	sg = stackgap_init(p->p_emul);
350	bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
351
352	SCARG(&bsa, fd) = SCARG(uap, fd);
353	SCARG(&bsa, buf) = bsp;
354
355	if ((error = sys_fstatfs(p, &bsa, retval)))
356		return error;
357
358	if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
359		return error;
360
361	bsd_to_linux_statfs(&btmp, &ltmp);
362
363	return copyout((caddr_t) &ltmp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
364}
365
366/*
367 * uname(). Just copy the info from the various strings stored in the
368 * kernel, and put it in the Linux utsname structure. That structure
369 * is almost the same as the NetBSD one, only it has fields 65 characters
370 * long, and an extra domainname field.
371 */
372int
373linux_sys_uname(p, v, retval)
374	struct proc *p;
375	void *v;
376	register_t *retval;
377{
378	struct linux_sys_uname_args /* {
379		syscallarg(struct linux_utsname *) up;
380	} */ *uap = v;
381	extern char ostype[], hostname[], osrelease[], version[], machine[],
382	    domainname[];
383	struct linux_utsname luts;
384	int len;
385	char *cp;
386
387	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
388	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
389	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
390	strncpy(luts.l_version, version, sizeof(luts.l_version));
391	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
392	strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
393
394	/* This part taken from the the uname() in libc */
395	len = sizeof(luts.l_version);
396	for (cp = luts.l_version; len--; ++cp)
397		if (*cp == '\n' || *cp == '\t')
398			if (len > 1)
399				*cp = ' ';
400			else
401				*cp = '\0';
402
403	return copyout(&luts, SCARG(uap, up), sizeof(luts));
404}
405
406int
407linux_sys_olduname(p, v, retval)
408	struct proc *p;
409	void *v;
410	register_t *retval;
411{
412	struct linux_sys_uname_args /* {
413		syscallarg(struct linux_oldutsname *) up;
414	} */ *uap = v;
415	extern char ostype[], hostname[], osrelease[], version[], machine[];
416	struct linux_oldutsname luts;
417	int len;
418	char *cp;
419
420	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
421	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
422	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
423	strncpy(luts.l_version, version, sizeof(luts.l_version));
424	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
425
426	/* This part taken from the the uname() in libc */
427	len = sizeof(luts.l_version);
428	for (cp = luts.l_version; len--; ++cp)
429		if (*cp == '\n' || *cp == '\t')
430			if (len > 1)
431				*cp = ' ';
432			else
433				*cp = '\0';
434
435	return copyout(&luts, SCARG(uap, up), sizeof(luts));
436}
437
438int
439linux_sys_oldolduname(p, v, retval)
440	struct proc *p;
441	void *v;
442	register_t *retval;
443{
444	struct linux_sys_uname_args /* {
445		syscallarg(struct linux_oldoldutsname *) up;
446	} */ *uap = v;
447	extern char ostype[], hostname[], osrelease[], version[], machine[];
448	struct linux_oldoldutsname luts;
449	int len;
450	char *cp;
451
452	strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
453	strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
454	strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
455	strncpy(luts.l_version, version, sizeof(luts.l_version));
456	strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
457
458	/* This part taken from the the uname() in libc */
459	len = sizeof(luts.l_version);
460	for (cp = luts.l_version; len--; ++cp)
461		if (*cp == '\n' || *cp == '\t')
462			if (len > 1)
463				*cp = ' ';
464			else
465				*cp = '\0';
466
467	return copyout(&luts, SCARG(uap, up), sizeof(luts));
468}
469
470/*
471 * Linux wants to pass everything to a syscall in registers. However,
472 * mmap() has 6 of them. Oops: out of register error. They just pass
473 * everything in a structure.
474 */
475int
476linux_sys_mmap(p, v, retval)
477	struct proc *p;
478	void *v;
479	register_t *retval;
480{
481	struct linux_sys_mmap_args /* {
482		syscallarg(struct linux_mmap *) lmp;
483	} */ *uap = v;
484	struct linux_mmap lmap;
485	struct sys_mmap_args cma;
486	int error, flags;
487
488	if ((error = copyin(SCARG(uap, lmp), &lmap, sizeof lmap)))
489		return error;
490
491	flags = 0;
492	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_SHARED, MAP_SHARED);
493	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_PRIVATE, MAP_PRIVATE);
494	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_FIXED, MAP_FIXED);
495	flags |= cvtto_bsd_mask(lmap.lm_flags, LINUX_MAP_ANON, MAP_ANON);
496
497	SCARG(&cma,addr) = lmap.lm_addr;
498	SCARG(&cma,len) = lmap.lm_len;
499 	SCARG(&cma,prot) = lmap.lm_prot;
500	SCARG(&cma,flags) = flags;
501	SCARG(&cma,fd) = lmap.lm_fd;
502	SCARG(&cma,pad) = 0;
503	SCARG(&cma,pos) = lmap.lm_pos;
504
505	return sys_mmap(p, &cma, retval);
506}
507
508/*
509 * This code is partly stolen from src/lib/libc/compat-43/times.c
510 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
511 */
512
513#define CLK_TCK 100
514#define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
515
516int
517linux_sys_times(p, v, retval)
518	struct proc *p;
519	void *v;
520	register_t *retval;
521{
522	struct linux_sys_times_args /* {
523		syscallarg(struct times *) tms;
524	} */ *uap = v;
525	struct timeval t;
526	struct linux_tms ltms;
527	struct rusage ru;
528	int error, s;
529
530	calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
531	ltms.ltms_utime = CONVTCK(ru.ru_utime);
532	ltms.ltms_stime = CONVTCK(ru.ru_stime);
533
534	ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
535	ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
536
537	if ((error = copyout(&ltms, SCARG(uap, tms), sizeof ltms)))
538		return error;
539
540	s = splclock();
541	timersub(&time, &boottime, &t);
542	splx(s);
543
544	retval[0] = ((linux_clock_t)(CONVTCK(t)));
545	return 0;
546}
547
548/*
549 * NetBSD passes fd[0] in retval[0], and fd[1] in retval[1].
550 * Linux directly passes the pointer.
551 */
552int
553linux_sys_pipe(p, v, retval)
554	struct proc *p;
555	void *v;
556	register_t *retval;
557{
558	struct linux_sys_pipe_args /* {
559		syscallarg(int *) pfds;
560	} */ *uap = v;
561	int error;
562
563	if ((error = sys_pipe(p, 0, retval)))
564		return error;
565
566	/* Assumes register_t is an int */
567
568	if ((error = copyout(retval, SCARG(uap, pfds), 2 * sizeof (int))))
569		return error;
570
571	retval[0] = 0;
572	return 0;
573}
574
575/*
576 * Alarm. This is a libc call which uses setitimer(2) in NetBSD.
577 * Fiddle with the timers to make it work.
578 */
579int
580linux_sys_alarm(p, v, retval)
581	struct proc *p;
582	void *v;
583	register_t *retval;
584{
585	struct linux_sys_alarm_args /* {
586		syscallarg(unsigned int) secs;
587	} */ *uap = v;
588	int error, s;
589	struct itimerval *itp, it;
590
591	itp = &p->p_realtimer;
592	s = splclock();
593	/*
594	 * Clear any pending timer alarms.
595	 */
596	untimeout(realitexpire, p);
597	timerclear(&itp->it_interval);
598	if (timerisset(&itp->it_value) &&
599	    timercmp(&itp->it_value, &time, >))
600		timersub(&itp->it_value, &time, &itp->it_value);
601	/*
602	 * Return how many seconds were left (rounded up)
603	 */
604	retval[0] = itp->it_value.tv_sec;
605	if (itp->it_value.tv_usec)
606		retval[0]++;
607
608	/*
609	 * alarm(0) just resets the timer.
610	 */
611	if (SCARG(uap, secs) == 0) {
612		timerclear(&itp->it_value);
613		splx(s);
614		return 0;
615	}
616
617	/*
618	 * Check the new alarm time for sanity, and set it.
619	 */
620	timerclear(&it.it_interval);
621	it.it_value.tv_sec = SCARG(uap, secs);
622	it.it_value.tv_usec = 0;
623	if (itimerfix(&it.it_value) || itimerfix(&it.it_interval)) {
624		splx(s);
625		return (EINVAL);
626	}
627
628	if (timerisset(&it.it_value)) {
629		timeradd(&it.it_value, &time, &it.it_value);
630		timeout(realitexpire, p, hzto(&it.it_value));
631	}
632	p->p_realtimer = it;
633	splx(s);
634
635	return 0;
636}
637
638/*
639 * utime(). Do conversion to things that utimes() understands,
640 * and pass it on.
641 */
642int
643linux_sys_utime(p, v, retval)
644	struct proc *p;
645	void *v;
646	register_t *retval;
647{
648	struct linux_sys_utime_args /* {
649		syscallarg(char *) path;
650		syscallarg(struct linux_utimbuf *)times;
651	} */ *uap = v;
652	caddr_t sg;
653	int error;
654	struct sys_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 sys_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_sys_readdir(p, v, retval)
691	struct proc *p;
692	void *v;
693	register_t *retval;
694{
695	struct linux_sys_readdir_args /* {
696		syscallarg(int) fd;
697		syscallarg(struct linux_dirent *) dent;
698		syscallarg(unsigned int) count;
699	} */ *uap = v;
700
701	SCARG(uap, count) = 1;
702	return linux_sys_getdents(p, uap, retval);
703}
704
705/*
706 * Linux 'readdir' call. This code is mostly taken from the
707 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
708 * an attempt has been made to keep it a little cleaner (failing
709 * miserably, because of the cruft needed if count 1 is passed).
710 *
711 * The d_off field should contain the offset of the next valid entry,
712 * but in Linux it has the offset of the entry itself. We emulate
713 * that bug here.
714 *
715 * Read in BSD-style entries, convert them, and copy them out.
716 *
717 * Note that this doesn't handle union-mounted filesystems.
718 */
719int
720linux_sys_getdents(p, v, retval)
721	struct proc *p;
722	void *v;
723	register_t *retval;
724{
725	struct linux_sys_readdir_args /* {
726		syscallarg(int) fd;
727		syscallarg(caddr_t) dent;
728		syscallarg(unsigned int) count;
729	} */ *uap = v;
730	register struct dirent *bdp;
731	struct vnode *vp;
732	caddr_t	inp, buf;	/* BSD-format */
733	int len, reclen;	/* BSD-format */
734	caddr_t outp;		/* Linux-format */
735	int resid, linux_reclen;/* Linux-format */
736	struct file *fp;
737	struct uio auio;
738	struct iovec aiov;
739	struct linux_dirent idb;
740	off_t off;		/* true file offset */
741	int buflen, error, eofflag, nbytes, oldcall;
742	struct vattr va;
743	u_long *cookiebuf, *cookie;
744	int ncookies;
745
746	if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
747		return (error);
748
749	if ((fp->f_flag & FREAD) == 0)
750		return (EBADF);
751
752	vp = (struct vnode *)fp->f_data;
753
754	if (vp->v_type != VDIR)	/* XXX  vnode readdir op should do this */
755		return (EINVAL);
756
757	if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
758		return error;
759
760	nbytes = SCARG(uap, count);
761	if (nbytes == 1) {	/* emulating old, broken behaviour */
762		nbytes = sizeof (struct linux_dirent);
763		buflen = max(va.va_blocksize, nbytes);
764		oldcall = 1;
765	} else {
766		buflen = min(MAXBSIZE, nbytes);
767		oldcall = 0;
768	}
769	buf = malloc(buflen, M_TEMP, M_WAITOK);
770	ncookies = buflen / 16;
771	cookiebuf = malloc(ncookies * sizeof(*cookiebuf), M_TEMP, M_WAITOK);
772	VOP_LOCK(vp);
773	off = fp->f_offset;
774again:
775	aiov.iov_base = buf;
776	aiov.iov_len = buflen;
777	auio.uio_iov = &aiov;
778	auio.uio_iovcnt = 1;
779	auio.uio_rw = UIO_READ;
780	auio.uio_segflg = UIO_SYSSPACE;
781	auio.uio_procp = p;
782	auio.uio_resid = buflen;
783	auio.uio_offset = off;
784	/*
785         * First we read into the malloc'ed buffer, then
786         * we massage it into user space, one record at a time.
787         */
788	error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, cookiebuf,
789	    ncookies);
790	if (error)
791		goto out;
792
793	inp = buf;
794	outp = SCARG(uap, dent);
795	resid = nbytes;
796	if ((len = buflen - auio.uio_resid) == 0)
797		goto eof;
798
799	for (cookie = cookiebuf; len > 0; len -= reclen) {
800		bdp = (struct dirent *)inp;
801		reclen = bdp->d_reclen;
802		if (reclen & 3)
803			panic("linux_readdir");
804		if (bdp->d_fileno == 0) {
805			inp += reclen;	/* it is a hole; squish it out */
806			off = *cookie++;
807			continue;
808		}
809		linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
810		if (reclen > len || resid < linux_reclen) {
811			/* entry too big for buffer, so just stop */
812			outp++;
813			off = *cookie++;
814			break;
815		}
816		/*
817		 * Massage in place to make a Linux-shaped dirent (otherwise
818		 * we have to worry about touching user memory outside of
819		 * the copyout() call).
820		 */
821		idb.d_ino = (linux_ino_t)bdp->d_fileno;
822		/*
823		 * The old readdir() call misuses the offset and reclen fields.
824		 */
825		if (oldcall) {
826			idb.d_off = (linux_off_t)linux_reclen;
827			idb.d_reclen = (u_short)bdp->d_namlen;
828		} else {
829			idb.d_off = (linux_off_t)off;
830			idb.d_reclen = (u_short)linux_reclen;
831		}
832		strcpy(idb.d_name, bdp->d_name);
833		if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
834			goto out;
835		/* advance past this real entry */
836		inp += reclen;
837		off = *cookie++;	/* each entry points to itself */
838		/* advance output past Linux-shaped entry */
839		outp += linux_reclen;
840		resid -= linux_reclen;
841		if (oldcall)
842			break;
843	}
844
845	/* if we squished out the whole block, try again */
846	if (outp == SCARG(uap, dent))
847		goto again;
848	fp->f_offset = off;	/* update the vnode offset */
849
850	if (oldcall)
851		nbytes = resid + linux_reclen;
852
853eof:
854	*retval = nbytes - resid;
855out:
856	VOP_UNLOCK(vp);
857	free(cookiebuf, M_TEMP);
858	free(buf, M_TEMP);
859	return error;
860}
861
862/*
863 * Not sure why the arguments to this older version of select() were put
864 * into a structure, because there are 5, and that can all be handled
865 * in registers on the i386 like Linux wants to.
866 */
867int
868linux_sys_oldselect(p, v, retval)
869	struct proc *p;
870	void *v;
871	register_t *retval;
872{
873	struct linux_sys_oldselect_args /* {
874		syscallarg(struct linux_select *) lsp;
875	} */ *uap = v;
876	struct linux_select ls;
877	int error;
878
879	if ((error = copyin(SCARG(uap, lsp), &ls, sizeof(ls))))
880		return error;
881
882	return linux_select1(p, retval, ls.nfds, ls.readfds, ls.writefds,
883	    ls.exceptfds, ls.timeout);
884}
885
886/*
887 * Even when just using registers to pass arguments to syscalls you can
888 * have 5 of them on the i386. So this newer version of select() does
889 * this.
890 */
891int
892linux_sys_select(p, v, retval)
893	struct proc *p;
894	void *v;
895	register_t *retval;
896{
897	struct linux_sys_select_args /* {
898		syscallarg(int) nfds;
899		syscallarg(fd_set *) readfds;
900		syscallarg(fd_set *) writefds;
901		syscallarg(fd_set *) exceptfds;
902		syscallarg(struct timeval *) timeout;
903	} */ *uap = v;
904
905	return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
906	    SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
907}
908
909/*
910 * Common code for the old and new versions of select(). A couple of
911 * things are important:
912 * 1) return the amount of time left in the 'timeout' parameter
913 * 2) select never returns ERESTART on Linux, always return EINTR
914 */
915int
916linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
917	struct proc *p;
918	register_t *retval;
919	int nfds;
920	fd_set *readfds, *writefds, *exceptfds;
921	struct timeval *timeout;
922{
923	struct sys_select_args bsa;
924	struct timeval tv0, tv1, utv, *tvp;
925	caddr_t sg;
926	int error;
927
928	SCARG(&bsa, nd) = nfds;
929	SCARG(&bsa, in) = readfds;
930	SCARG(&bsa, ou) = writefds;
931	SCARG(&bsa, ex) = exceptfds;
932	SCARG(&bsa, tv) = timeout;
933
934	/*
935	 * Store current time for computation of the amount of
936	 * time left.
937	 */
938	if (timeout) {
939		if ((error = copyin(timeout, &utv, sizeof(utv))))
940			return error;
941		if (itimerfix(&utv)) {
942			/*
943			 * The timeval was invalid.  Convert it to something
944			 * valid that will act as it does under Linux.
945			 */
946			sg = stackgap_init(p->p_emul);
947			tvp = stackgap_alloc(&sg, sizeof(utv));
948			utv.tv_sec += utv.tv_usec / 1000000;
949			utv.tv_usec %= 1000000;
950			if (utv.tv_usec < 0) {
951				utv.tv_sec -= 1;
952				utv.tv_usec += 1000000;
953			}
954			if (utv.tv_sec < 0)
955				timerclear(&utv);
956			if ((error = copyout(&utv, tvp, sizeof(utv))))
957				return error;
958			SCARG(&bsa, tv) = tvp;
959		}
960		microtime(&tv0);
961	}
962
963	error = sys_select(p, &bsa, retval);
964	if (error) {
965		/*
966		 * See fs/select.c in the Linux kernel.  Without this,
967		 * Maelstrom doesn't work.
968		 */
969		if (error == ERESTART)
970			error = EINTR;
971		return error;
972	}
973
974	if (timeout) {
975		if (*retval) {
976			/*
977			 * Compute how much time was left of the timeout,
978			 * by subtracting the current time and the time
979			 * before we started the call, and subtracting
980			 * that result from the user-supplied value.
981			 */
982			microtime(&tv1);
983			timersub(&tv1, &tv0, &tv1);
984			timersub(&utv, &tv1, &utv);
985			if (utv.tv_sec < 0)
986				timerclear(&utv);
987		} else
988			timerclear(&utv);
989		if ((error = copyout(&utv, timeout, sizeof(utv))))
990			return error;
991	}
992
993	return 0;
994}
995
996/*
997 * Get the process group of a certain process. Look it up
998 * and return the value.
999 */
1000int
1001linux_sys_getpgid(p, v, retval)
1002	struct proc *p;
1003	void *v;
1004	register_t *retval;
1005{
1006	struct linux_sys_getpgid_args /* {
1007		syscallarg(int) pid;
1008	} */ *uap = v;
1009	struct proc *targp;
1010
1011	if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid)
1012		if ((targp = pfind(SCARG(uap, pid))) == 0)
1013			return ESRCH;
1014	else
1015		targp = p;
1016
1017	retval[0] = targp->p_pgid;
1018	return 0;
1019}
1020
1021/*
1022 * Set the 'personality' (emulation mode) for the current process. Only
1023 * accept the Linux personality here (0). This call is needed because
1024 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1025 * ELF binaries run in Linux mode, not SVR4 mode.
1026 */
1027int
1028linux_sys_personality(p, v, retval)
1029	struct proc *p;
1030	void *v;
1031	register_t *retval;
1032{
1033	struct linux_sys_personality_args /* {
1034		syscallarg(int) per;
1035	} */ *uap = v;
1036
1037	if (SCARG(uap, per) != 0)
1038		return EINVAL;
1039	retval[0] = 0;
1040	return 0;
1041}
1042
1043/*
1044 * The calls are here because of type conversions.
1045 */
1046int
1047linux_sys_setreuid(p, v, retval)
1048	struct proc *p;
1049	void *v;
1050	register_t *retval;
1051{
1052	struct linux_sys_setreuid_args /* {
1053		syscallarg(int) ruid;
1054		syscallarg(int) euid;
1055	} */ *uap = v;
1056	struct compat_43_sys_setreuid_args bsa;
1057
1058	SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1059		(uid_t)-1 : SCARG(uap, ruid);
1060	SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1061		(uid_t)-1 : SCARG(uap, euid);
1062
1063	return compat_43_sys_setreuid(p, &bsa, retval);
1064}
1065
1066int
1067linux_sys_setregid(p, v, retval)
1068	struct proc *p;
1069	void *v;
1070	register_t *retval;
1071{
1072	struct linux_sys_setregid_args /* {
1073		syscallarg(int) rgid;
1074		syscallarg(int) egid;
1075	} */ *uap = v;
1076	struct compat_43_sys_setregid_args bsa;
1077
1078	SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1079		(uid_t)-1 : SCARG(uap, rgid);
1080	SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1081		(uid_t)-1 : SCARG(uap, egid);
1082
1083	return compat_43_sys_setregid(p, &bsa, retval);
1084}
1085