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