linux_misc.c revision 1.57 1 /* $NetBSD: linux_misc.c,v 1.57 1999/05/14 18:44:50 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Eric Haszlakiewicz; by Jason R. Thorpe
9 * of the Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Linux compatibility module. Try to deal with various Linux system calls.
42 */
43
44 /*
45 * These functions have been moved to multiarch to allow
46 * selection of which machines include them to be
47 * determined by the individual files.linux_<arch> files.
48 *
49 * Function in multiarch:
50 * linux_sys_break : linux_break.c
51 * linux_sys_alarm : linux_misc_notalpha.c
52 * linux_sys_getresgid : linux_misc_notalpha.c
53 * linux_sys_nice : linux_misc_notalpha.c
54 * linux_sys_readdir : linux_misc_notalpha.c
55 * linux_sys_setresgid : linux_misc_notalpha.c
56 * linux_sys_time : linux_misc_notalpha.c
57 * linux_sys_utime : linux_misc_notalpha.c
58 * linux_sys_waitpid : linux_misc_notalpha.c
59 * linux_sys_old_mmap : linux_oldmmap.c
60 * linux_sys_oldolduname : linux_oldolduname.c
61 * linux_sys_oldselect : linux_oldselect.c
62 * linux_sys_olduname : linux_olduname.c
63 * linux_sys_pipe : linux_pipe.c
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/namei.h>
69 #include <sys/proc.h>
70 #include <sys/dirent.h>
71 #include <sys/file.h>
72 #include <sys/stat.h>
73 #include <sys/filedesc.h>
74 #include <sys/ioctl.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/mman.h>
79 #include <sys/mount.h>
80 #include <sys/ptrace.h>
81 #include <sys/resource.h>
82 #include <sys/resourcevar.h>
83 #include <sys/signal.h>
84 #include <sys/signalvar.h>
85 #include <sys/socket.h>
86 #include <sys/time.h>
87 #include <sys/times.h>
88 #include <sys/vnode.h>
89 #include <sys/uio.h>
90 #include <sys/wait.h>
91 #include <sys/utsname.h>
92 #include <sys/unistd.h>
93
94 #include <sys/syscallargs.h>
95
96 #include <vm/vm.h>
97 #include <vm/vm_param.h>
98
99 #include <compat/linux/common/linux_types.h>
100 #include <compat/linux/common/linux_signal.h>
101
102 #include <compat/linux/linux_syscallargs.h>
103
104 #include <compat/linux/common/linux_fcntl.h>
105 #include <compat/linux/common/linux_mmap.h>
106 #include <compat/linux/common/linux_dirent.h>
107 #include <compat/linux/common/linux_util.h>
108 #include <compat/linux/common/linux_misc.h>
109 #include <compat/linux/common/linux_sched.h>
110
111
112 /* Local linux_misc.c functions: */
113 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
114
115 /*
116 * The information on a terminated (or stopped) process needs
117 * to be converted in order for Linux binaries to get a valid signal
118 * number out of it.
119 */
120 void
121 bsd_to_linux_wstat(st)
122 int *st;
123 {
124
125 int sig;
126
127 if (WIFSIGNALED(*st)) {
128 sig = WTERMSIG(*st);
129 if (sig >= 0 && sig < NSIG)
130 *st= (*st& ~0177) | native_to_linux_sig[sig];
131 } else if (WIFSTOPPED(*st)) {
132 sig = WSTOPSIG(*st);
133 if (sig >= 0 && sig < NSIG)
134 *st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
135 }
136 }
137
138 /*
139 * This is very much the same as waitpid()
140 */
141 int
142 linux_sys_wait4(p, v, retval)
143 struct proc *p;
144 void *v;
145 register_t *retval;
146 {
147 struct linux_sys_wait4_args /* {
148 syscallarg(int) pid;
149 syscallarg(int *) status;
150 syscallarg(int) options;
151 syscallarg(struct rusage *) rusage;
152 } */ *uap = v;
153 struct sys_wait4_args w4a;
154 int error, *status, tstat, options, linux_options;
155 caddr_t sg;
156
157 if (SCARG(uap, status) != NULL) {
158 sg = stackgap_init(p->p_emul);
159 status = (int *) stackgap_alloc(&sg, sizeof *status);
160 } else
161 status = NULL;
162
163 linux_options = SCARG(uap, options);
164 options = 0;
165 if (linux_options &
166 ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
167 return (EINVAL);
168
169 if (linux_options & LINUX_WAIT4_WNOHANG)
170 options |= WNOHANG;
171 if (linux_options & LINUX_WAIT4_WUNTRACED)
172 options |= WUNTRACED;
173 if (linux_options & LINUX_WAIT4_WCLONE)
174 options |= WALTSIG;
175
176 SCARG(&w4a, pid) = SCARG(uap, pid);
177 SCARG(&w4a, status) = status;
178 SCARG(&w4a, options) = options;
179 SCARG(&w4a, rusage) = SCARG(uap, rusage);
180
181 if ((error = sys_wait4(p, &w4a, retval)))
182 return error;
183
184 sigdelset(&p->p_siglist, SIGCHLD);
185
186 if (status != NULL) {
187 if ((error = copyin(status, &tstat, sizeof tstat)))
188 return error;
189
190 bsd_to_linux_wstat(&tstat);
191 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
192 }
193
194 return 0;
195 }
196
197 /*
198 * Linux brk(2). The check if the new address is >= the old one is
199 * done in the kernel in Linux. NetBSD does it in the library.
200 */
201 int
202 linux_sys_brk(p, v, retval)
203 struct proc *p;
204 void *v;
205 register_t *retval;
206 {
207 struct linux_sys_brk_args /* {
208 syscallarg(char *) nsize;
209 } */ *uap = v;
210 char *nbrk = SCARG(uap, nsize);
211 struct sys_obreak_args oba;
212 struct vmspace *vm = p->p_vmspace;
213 caddr_t oldbrk;
214
215 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
216 /*
217 * XXX inconsistent.. Linux always returns at least the old
218 * brk value, but it will be page-aligned if this fails,
219 * and possibly not page aligned if it succeeds (the user
220 * supplied pointer is returned).
221 */
222 SCARG(&oba, nsize) = nbrk;
223
224 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
225 retval[0] = (register_t)nbrk;
226 else
227 retval[0] = (register_t)oldbrk;
228
229 return 0;
230 }
231
232 /*
233 * Convert BSD statfs structure to Linux statfs structure.
234 * The Linux structure has less fields, and it also wants
235 * the length of a name in a dir entry in a field, which
236 * we fake (probably the wrong way).
237 */
238 static void
239 bsd_to_linux_statfs(bsp, lsp)
240 struct statfs *bsp;
241 struct linux_statfs *lsp;
242 {
243
244 lsp->l_ftype = bsp->f_type;
245 lsp->l_fbsize = bsp->f_bsize;
246 lsp->l_fblocks = bsp->f_blocks;
247 lsp->l_fbfree = bsp->f_bfree;
248 lsp->l_fbavail = bsp->f_bavail;
249 lsp->l_ffiles = bsp->f_files;
250 lsp->l_fffree = bsp->f_ffree;
251 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
252 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
253 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
254 }
255
256 /*
257 * Implement the fs stat functions. Straightforward.
258 */
259 int
260 linux_sys_statfs(p, v, retval)
261 struct proc *p;
262 void *v;
263 register_t *retval;
264 {
265 struct linux_sys_statfs_args /* {
266 syscallarg(const char *) path;
267 syscallarg(struct linux_statfs *) sp;
268 } */ *uap = v;
269 struct statfs btmp, *bsp;
270 struct linux_statfs ltmp;
271 struct sys_statfs_args bsa;
272 caddr_t sg;
273 int error;
274
275 sg = stackgap_init(p->p_emul);
276 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
277
278 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
279
280 SCARG(&bsa, path) = SCARG(uap, path);
281 SCARG(&bsa, buf) = bsp;
282
283 if ((error = sys_statfs(p, &bsa, retval)))
284 return error;
285
286 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
287 return error;
288
289 bsd_to_linux_statfs(&btmp, <mp);
290
291 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
292 }
293
294 int
295 linux_sys_fstatfs(p, v, retval)
296 struct proc *p;
297 void *v;
298 register_t *retval;
299 {
300 struct linux_sys_fstatfs_args /* {
301 syscallarg(int) fd;
302 syscallarg(struct linux_statfs *) sp;
303 } */ *uap = v;
304 struct statfs btmp, *bsp;
305 struct linux_statfs ltmp;
306 struct sys_fstatfs_args bsa;
307 caddr_t sg;
308 int error;
309
310 sg = stackgap_init(p->p_emul);
311 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
312
313 SCARG(&bsa, fd) = SCARG(uap, fd);
314 SCARG(&bsa, buf) = bsp;
315
316 if ((error = sys_fstatfs(p, &bsa, retval)))
317 return error;
318
319 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
320 return error;
321
322 bsd_to_linux_statfs(&btmp, <mp);
323
324 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
325 }
326
327 /*
328 * uname(). Just copy the info from the various strings stored in the
329 * kernel, and put it in the Linux utsname structure. That structure
330 * is almost the same as the NetBSD one, only it has fields 65 characters
331 * long, and an extra domainname field.
332 */
333 int
334 linux_sys_uname(p, v, retval)
335 struct proc *p;
336 void *v;
337 register_t *retval;
338 {
339 struct linux_sys_uname_args /* {
340 syscallarg(struct linux_utsname *) up;
341 } */ *uap = v;
342 extern char ostype[], hostname[], osrelease[], version[], machine[],
343 domainname[];
344 struct linux_utsname luts;
345 int len;
346 char *cp;
347
348 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
349 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
350 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
351 strncpy(luts.l_version, version, sizeof(luts.l_version));
352 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
353 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
354
355 /* This part taken from the the uname() in libc */
356 len = sizeof(luts.l_version);
357 for (cp = luts.l_version; len--; ++cp) {
358 if (*cp == '\n' || *cp == '\t') {
359 if (len > 1)
360 *cp = ' ';
361 else
362 *cp = '\0';
363 }
364 }
365
366 return copyout(&luts, SCARG(uap, up), sizeof(luts));
367 }
368
369 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
370 /* Used indirectly on: arm, i386, m68k */
371
372 /*
373 * New type Linux mmap call.
374 * Only called directly on machines with >= 6 free regs.
375 */
376 int
377 linux_sys_mmap(p, v, retval)
378 struct proc *p;
379 void *v;
380 register_t *retval;
381 {
382 struct linux_sys_mmap_args /* {
383 syscallarg(unsigned long) addr;
384 syscallarg(size_t) len;
385 syscallarg(int) prot;
386 syscallarg(int) flags;
387 syscallarg(int) fd;
388 syscallarg(off_t) offset;
389 } */ *uap = v;
390 struct sys_mmap_args cma;
391 int flags;
392
393 flags = 0;
394 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
395 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
396 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
397 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
398 /* XXX XAX ERH: Any other flags here? There are more defined... */
399
400 SCARG(&cma,addr) = (void *)SCARG(uap, addr);
401 SCARG(&cma,len) = SCARG(uap, len);
402 SCARG(&cma,prot) = SCARG(uap, prot);
403 if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
404 SCARG(&cma,prot) |= VM_PROT_READ;
405 SCARG(&cma,flags) = flags;
406 SCARG(&cma,fd) = SCARG(uap, fd);
407 SCARG(&cma,pad) = 0;
408 SCARG(&cma,pos) = SCARG(uap, offset);
409
410 return sys_mmap(p, &cma, retval);
411 }
412
413 int
414 linux_sys_mremap(p, v, retval)
415 struct proc *p;
416 void *v;
417 register_t *retval;
418 {
419 struct linux_sys_mremap_args /* {
420 syscallarg(void *) old_address;
421 syscallarg(size_t) old_size;
422 syscallarg(size_t) new_size;
423 syscallarg(u_long) flags;
424 } */ *uap = v;
425 struct sys_munmap_args mua;
426 size_t old_size, new_size;
427 int error;
428
429 old_size = round_page(SCARG(uap, old_size));
430 new_size = round_page(SCARG(uap, new_size));
431
432 /*
433 * Growing mapped region.
434 */
435 if (new_size > old_size) {
436 /*
437 * XXX Implement me. What we probably want to do is
438 * XXX dig out the guts of the old mapping, mmap that
439 * XXX object again with the new size, then munmap
440 * XXX the old mapping.
441 */
442 *retval = 0;
443 return (ENOMEM);
444 }
445
446 /*
447 * Shrinking mapped region.
448 */
449 if (new_size < old_size) {
450 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
451 new_size;
452 SCARG(&mua, len) = old_size - new_size;
453 error = sys_munmap(p, &mua, retval);
454 *retval = error ? 0 : (register_t)SCARG(uap, old_address);
455 return (error);
456 }
457
458 /*
459 * No change.
460 */
461 *retval = (register_t)SCARG(uap, old_address);
462 return (0);
463 }
464
465 int
466 linux_sys_msync(p, v, retval)
467 struct proc *p;
468 void *v;
469 register_t *retval;
470 {
471 struct linux_sys_msync_args /* {
472 syscallarg(caddr_t) addr;
473 syscallarg(int) len;
474 syscallarg(int) fl;
475 } */ *uap = v;
476
477 struct sys___msync13_args bma;
478
479 /* flags are ignored */
480 SCARG(&bma, addr) = SCARG(uap, addr);
481 SCARG(&bma, len) = SCARG(uap, len);
482 SCARG(&bma, flags) = SCARG(uap, fl);
483
484 return sys___msync13(p, &bma, retval);
485 }
486
487 /*
488 * This code is partly stolen from src/lib/libc/compat-43/times.c
489 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
490 */
491
492 #define CLK_TCK 100
493 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
494
495 int
496 linux_sys_times(p, v, retval)
497 struct proc *p;
498 void *v;
499 register_t *retval;
500 {
501 struct linux_sys_times_args /* {
502 syscallarg(struct times *) tms;
503 } */ *uap = v;
504 struct timeval t;
505 struct linux_tms ltms;
506 struct rusage ru;
507 int error, s;
508
509 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
510 ltms.ltms_utime = CONVTCK(ru.ru_utime);
511 ltms.ltms_stime = CONVTCK(ru.ru_stime);
512
513 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
514 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
515
516 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
517 return error;
518
519 s = splclock();
520 timersub(&time, &boottime, &t);
521 splx(s);
522
523 retval[0] = ((linux_clock_t)(CONVTCK(t)));
524 return 0;
525 }
526
527 /*
528 * Linux 'readdir' call. This code is mostly taken from the
529 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
530 * an attempt has been made to keep it a little cleaner (failing
531 * miserably, because of the cruft needed if count 1 is passed).
532 *
533 * The d_off field should contain the offset of the next valid entry,
534 * but in Linux it has the offset of the entry itself. We emulate
535 * that bug here.
536 *
537 * Read in BSD-style entries, convert them, and copy them out.
538 *
539 * Note that this doesn't handle union-mounted filesystems.
540 */
541 int
542 linux_sys_getdents(p, v, retval)
543 struct proc *p;
544 void *v;
545 register_t *retval;
546 {
547 struct linux_sys_getdents_args /* {
548 syscallarg(int) fd;
549 syscallarg(struct linux_dirent *) dent;
550 syscallarg(unsigned int) count;
551 } */ *uap = v;
552 register struct dirent *bdp;
553 struct vnode *vp;
554 caddr_t inp, buf; /* BSD-format */
555 int len, reclen; /* BSD-format */
556 caddr_t outp; /* Linux-format */
557 int resid, linux_reclen = 0; /* Linux-format */
558 struct file *fp;
559 struct uio auio;
560 struct iovec aiov;
561 struct linux_dirent idb;
562 off_t off; /* true file offset */
563 int buflen, error, eofflag, nbytes, oldcall;
564 struct vattr va;
565 off_t *cookiebuf = NULL, *cookie;
566 int ncookies;
567
568 /* getvnode() will use the descriptor for us */
569 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
570 return (error);
571
572 if ((fp->f_flag & FREAD) == 0) {
573 error = EBADF;
574 goto out1;
575 }
576
577 vp = (struct vnode *)fp->f_data;
578 if (vp->v_type != VDIR) {
579 error = EINVAL;
580 goto out1;
581 }
582
583 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
584 goto out1;
585
586 nbytes = SCARG(uap, count);
587 if (nbytes == 1) { /* emulating old, broken behaviour */
588 nbytes = sizeof (struct linux_dirent);
589 buflen = max(va.va_blocksize, nbytes);
590 oldcall = 1;
591 } else {
592 buflen = min(MAXBSIZE, nbytes);
593 if (buflen < va.va_blocksize)
594 buflen = va.va_blocksize;
595 oldcall = 0;
596 }
597 buf = malloc(buflen, M_TEMP, M_WAITOK);
598
599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
600 off = fp->f_offset;
601 again:
602 aiov.iov_base = buf;
603 aiov.iov_len = buflen;
604 auio.uio_iov = &aiov;
605 auio.uio_iovcnt = 1;
606 auio.uio_rw = UIO_READ;
607 auio.uio_segflg = UIO_SYSSPACE;
608 auio.uio_procp = p;
609 auio.uio_resid = buflen;
610 auio.uio_offset = off;
611 /*
612 * First we read into the malloc'ed buffer, then
613 * we massage it into user space, one record at a time.
614 */
615 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
616 &ncookies);
617 if (error)
618 goto out;
619
620 inp = buf;
621 outp = (caddr_t)SCARG(uap, dent);
622 resid = nbytes;
623 if ((len = buflen - auio.uio_resid) == 0)
624 goto eof;
625
626 for (cookie = cookiebuf; len > 0; len -= reclen) {
627 bdp = (struct dirent *)inp;
628 reclen = bdp->d_reclen;
629 if (reclen & 3)
630 panic("linux_readdir");
631 if (bdp->d_fileno == 0) {
632 inp += reclen; /* it is a hole; squish it out */
633 off = *cookie++;
634 continue;
635 }
636 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
637 if (reclen > len || resid < linux_reclen) {
638 /* entry too big for buffer, so just stop */
639 outp++;
640 break;
641 }
642 /*
643 * Massage in place to make a Linux-shaped dirent (otherwise
644 * we have to worry about touching user memory outside of
645 * the copyout() call).
646 */
647 idb.d_ino = (linux_ino_t)bdp->d_fileno;
648 /*
649 * The old readdir() call misuses the offset and reclen fields.
650 */
651 if (oldcall) {
652 idb.d_off = (linux_off_t)linux_reclen;
653 idb.d_reclen = (u_short)bdp->d_namlen;
654 } else {
655 if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
656 compat_offseterr(vp, "linux_getdents");
657 error = EINVAL;
658 goto out;
659 }
660 idb.d_off = (linux_off_t)off;
661 idb.d_reclen = (u_short)linux_reclen;
662 }
663 strcpy(idb.d_name, bdp->d_name);
664 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
665 goto out;
666 /* advance past this real entry */
667 inp += reclen;
668 off = *cookie++; /* each entry points to itself */
669 /* advance output past Linux-shaped entry */
670 outp += linux_reclen;
671 resid -= linux_reclen;
672 if (oldcall)
673 break;
674 }
675
676 /* if we squished out the whole block, try again */
677 if (outp == (caddr_t)SCARG(uap, dent))
678 goto again;
679 fp->f_offset = off; /* update the vnode offset */
680
681 if (oldcall)
682 nbytes = resid + linux_reclen;
683
684 eof:
685 *retval = nbytes - resid;
686 out:
687 VOP_UNLOCK(vp, 0);
688 if (cookiebuf)
689 free(cookiebuf, M_TEMP);
690 free(buf, M_TEMP);
691 out1:
692 FILE_UNUSE(fp, p);
693 return error;
694 }
695
696 /*
697 * Even when just using registers to pass arguments to syscalls you can
698 * have 5 of them on the i386. So this newer version of select() does
699 * this.
700 */
701 int
702 linux_sys_select(p, v, retval)
703 struct proc *p;
704 void *v;
705 register_t *retval;
706 {
707 struct linux_sys_select_args /* {
708 syscallarg(int) nfds;
709 syscallarg(fd_set *) readfds;
710 syscallarg(fd_set *) writefds;
711 syscallarg(fd_set *) exceptfds;
712 syscallarg(struct timeval *) timeout;
713 } */ *uap = v;
714
715 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
716 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
717 }
718
719 /*
720 * Common code for the old and new versions of select(). A couple of
721 * things are important:
722 * 1) return the amount of time left in the 'timeout' parameter
723 * 2) select never returns ERESTART on Linux, always return EINTR
724 */
725 int
726 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
727 struct proc *p;
728 register_t *retval;
729 int nfds;
730 fd_set *readfds, *writefds, *exceptfds;
731 struct timeval *timeout;
732 {
733 struct sys_select_args bsa;
734 struct timeval tv0, tv1, utv, *tvp;
735 caddr_t sg;
736 int error;
737
738 SCARG(&bsa, nd) = nfds;
739 SCARG(&bsa, in) = readfds;
740 SCARG(&bsa, ou) = writefds;
741 SCARG(&bsa, ex) = exceptfds;
742 SCARG(&bsa, tv) = timeout;
743
744 /*
745 * Store current time for computation of the amount of
746 * time left.
747 */
748 if (timeout) {
749 if ((error = copyin(timeout, &utv, sizeof(utv))))
750 return error;
751 if (itimerfix(&utv)) {
752 /*
753 * The timeval was invalid. Convert it to something
754 * valid that will act as it does under Linux.
755 */
756 sg = stackgap_init(p->p_emul);
757 tvp = stackgap_alloc(&sg, sizeof(utv));
758 utv.tv_sec += utv.tv_usec / 1000000;
759 utv.tv_usec %= 1000000;
760 if (utv.tv_usec < 0) {
761 utv.tv_sec -= 1;
762 utv.tv_usec += 1000000;
763 }
764 if (utv.tv_sec < 0)
765 timerclear(&utv);
766 if ((error = copyout(&utv, tvp, sizeof(utv))))
767 return error;
768 SCARG(&bsa, tv) = tvp;
769 }
770 microtime(&tv0);
771 }
772
773 error = sys_select(p, &bsa, retval);
774 if (error) {
775 /*
776 * See fs/select.c in the Linux kernel. Without this,
777 * Maelstrom doesn't work.
778 */
779 if (error == ERESTART)
780 error = EINTR;
781 return error;
782 }
783
784 if (timeout) {
785 if (*retval) {
786 /*
787 * Compute how much time was left of the timeout,
788 * by subtracting the current time and the time
789 * before we started the call, and subtracting
790 * that result from the user-supplied value.
791 */
792 microtime(&tv1);
793 timersub(&tv1, &tv0, &tv1);
794 timersub(&utv, &tv1, &utv);
795 if (utv.tv_sec < 0)
796 timerclear(&utv);
797 } else
798 timerclear(&utv);
799 if ((error = copyout(&utv, timeout, sizeof(utv))))
800 return error;
801 }
802
803 return 0;
804 }
805
806 /*
807 * Get the process group of a certain process. Look it up
808 * and return the value.
809 */
810 int
811 linux_sys_getpgid(p, v, retval)
812 struct proc *p;
813 void *v;
814 register_t *retval;
815 {
816 struct linux_sys_getpgid_args /* {
817 syscallarg(int) pid;
818 } */ *uap = v;
819 struct proc *targp;
820
821 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
822 if ((targp = pfind(SCARG(uap, pid))) == 0)
823 return ESRCH;
824 }
825 else
826 targp = p;
827
828 retval[0] = targp->p_pgid;
829 return 0;
830 }
831
832 /*
833 * Set the 'personality' (emulation mode) for the current process. Only
834 * accept the Linux personality here (0). This call is needed because
835 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
836 * ELF binaries run in Linux mode, not SVR4 mode.
837 */
838 int
839 linux_sys_personality(p, v, retval)
840 struct proc *p;
841 void *v;
842 register_t *retval;
843 {
844 struct linux_sys_personality_args /* {
845 syscallarg(int) per;
846 } */ *uap = v;
847
848 if (SCARG(uap, per) != 0)
849 return EINVAL;
850 retval[0] = 0;
851 return 0;
852 }
853
854 /*
855 * The calls are here because of type conversions.
856 */
857 int
858 linux_sys_setreuid(p, v, retval)
859 struct proc *p;
860 void *v;
861 register_t *retval;
862 {
863 struct linux_sys_setreuid_args /* {
864 syscallarg(int) ruid;
865 syscallarg(int) euid;
866 } */ *uap = v;
867 struct sys_setreuid_args bsa;
868
869 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
870 (uid_t)-1 : SCARG(uap, ruid);
871 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
872 (uid_t)-1 : SCARG(uap, euid);
873
874 return sys_setreuid(p, &bsa, retval);
875 }
876
877 int
878 linux_sys_setregid(p, v, retval)
879 struct proc *p;
880 void *v;
881 register_t *retval;
882 {
883 struct linux_sys_setregid_args /* {
884 syscallarg(int) rgid;
885 syscallarg(int) egid;
886 } */ *uap = v;
887 struct sys_setregid_args bsa;
888
889 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
890 (uid_t)-1 : SCARG(uap, rgid);
891 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
892 (uid_t)-1 : SCARG(uap, egid);
893
894 return sys_setregid(p, &bsa, retval);
895 }
896
897 int
898 linux_sys___sysctl(p, v, retval)
899 struct proc *p;
900 void *v;
901 register_t *retval;
902 {
903 struct linux_sys___sysctl_args /* {
904 syscallarg(struct linux___sysctl *) lsp;
905 } */ *uap = v;
906 struct linux___sysctl ls;
907 struct sys___sysctl_args bsa;
908 int error;
909
910 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
911 return error;
912 SCARG(&bsa, name) = ls.name;
913 SCARG(&bsa, namelen) = ls.namelen;
914 SCARG(&bsa, old) = ls.old;
915 SCARG(&bsa, oldlenp) = ls.oldlenp;
916 SCARG(&bsa, new) = ls.new;
917 SCARG(&bsa, newlen) = ls.newlen;
918
919 return sys___sysctl(p, &bsa, retval);
920 }
921
922 int
923 linux_sys_clone(p, v, retval)
924 struct proc *p;
925 void *v;
926 register_t *retval;
927 {
928 struct linux_sys_clone_args /* {
929 syscallarg(int) flags;
930 syscallarg(void *) stack;
931 } */ *uap = v;
932 int flags, sig;
933
934 /*
935 * We don't support the Linux CLONE_PID or CLONE_PTRACE flags.
936 */
937 if (SCARG(uap, flags) & (LINUX_CLONE_PID|LINUX_CLONE_PTRACE))
938 return (EINVAL);
939
940 flags = 0;
941
942 if (SCARG(uap, flags) & LINUX_CLONE_VM)
943 flags |= FORK_SHAREVM;
944 if (SCARG(uap, flags) & LINUX_CLONE_FS)
945 flags |= FORK_SHARECWD;
946 if (SCARG(uap, flags) & LINUX_CLONE_FILES)
947 flags |= FORK_SHAREFILES;
948 if (SCARG(uap, flags) & LINUX_CLONE_SIGHAND)
949 flags |= FORK_SHARESIGS;
950 if (SCARG(uap, flags) & LINUX_CLONE_VFORK)
951 flags |= FORK_PPWAIT;
952
953 sig = SCARG(uap, flags) & LINUX_CLONE_CSIGNAL;
954
955 /*
956 * Note that Linux does not provide a portable way of specifying
957 * the stack area; the caller must know if the stack grows up
958 * or down. So, we pass a stack size of 0, so that the code
959 * that makes this adjustment is a noop.
960 */
961 return (fork1(p, flags, sig, SCARG(uap, stack), 0, retval, NULL));
962 }
963
964 int
965 linux_sys_setresuid(p, v, retval)
966 struct proc *p;
967 void *v;
968 register_t *retval;
969 {
970 struct linux_sys_setresuid_args /* {
971 syscallarg(uid_t) ruid;
972 syscallarg(uid_t) euid;
973 syscallarg(uid_t) suid;
974 } */ *uap = v;
975 struct pcred *pc = p->p_cred;
976 uid_t ruid, euid, suid;
977 int error;
978
979 ruid = SCARG(uap, ruid);
980 euid = SCARG(uap, euid);
981 suid = SCARG(uap, suid);
982
983 /*
984 * Note: These checks are a little different than the NetBSD
985 * setreuid(2) call performs. This precisely follows the
986 * behavior of the Linux kernel.
987 */
988 if (ruid != (uid_t)-1 &&
989 ruid != pc->p_ruid &&
990 ruid != pc->pc_ucred->cr_uid &&
991 ruid != pc->p_svuid &&
992 (error = suser(pc->pc_ucred, &p->p_acflag)))
993 return (error);
994
995 if (euid != (uid_t)-1 &&
996 euid != pc->p_ruid &&
997 euid != pc->pc_ucred->cr_uid &&
998 euid != pc->p_svuid &&
999 (error = suser(pc->pc_ucred, &p->p_acflag)))
1000 return (error);
1001
1002 if (suid != (uid_t)-1 &&
1003 suid != pc->p_ruid &&
1004 suid != pc->pc_ucred->cr_uid &&
1005 suid != pc->p_svuid &&
1006 (error = suser(pc->pc_ucred, &p->p_acflag)))
1007 return (error);
1008
1009 /*
1010 * Now assign the new real, effective, and saved UIDs.
1011 * Note that Linux, unlike NetBSD in setreuid(2), does not
1012 * set the saved UID in this call unless the user specifies
1013 * it.
1014 */
1015 if (ruid != (uid_t)-1) {
1016 (void)chgproccnt(pc->p_ruid, -1);
1017 (void)chgproccnt(ruid, 1);
1018 pc->p_ruid = ruid;
1019 }
1020
1021 if (euid != (uid_t)-1) {
1022 pc->pc_ucred = crcopy(pc->pc_ucred);
1023 pc->pc_ucred->cr_uid = euid;
1024 }
1025
1026 if (suid != (uid_t)-1)
1027 pc->p_svuid = suid;
1028
1029 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1030 p->p_flag |= P_SUGID;
1031 return (0);
1032 }
1033
1034 int
1035 linux_sys_getresuid(p, v, retval)
1036 struct proc *p;
1037 void *v;
1038 register_t *retval;
1039 {
1040 struct linux_sys_getresuid_args /* {
1041 syscallarg(uid_t *) ruid;
1042 syscallarg(uid_t *) euid;
1043 syscallarg(uid_t *) suid;
1044 } */ *uap = v;
1045 struct pcred *pc = p->p_cred;
1046 int error;
1047
1048 /*
1049 * Linux copies these values out to userspace like so:
1050 *
1051 * 1. Copy out ruid.
1052 * 2. If that succeeds, copy out euid.
1053 * 3. If both of those succeed, copy out suid.
1054 */
1055 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1056 sizeof(uid_t))) != 0)
1057 return (error);
1058
1059 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1060 sizeof(uid_t))) != 0)
1061 return (error);
1062
1063 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1064 }
1065