linux_misc.c revision 1.71 1 /* $NetBSD: linux_misc.c,v 1.71 2000/06/29 02:40:39 mrg 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/reboot.h>
82 #include <sys/resource.h>
83 #include <sys/resourcevar.h>
84 #include <sys/signal.h>
85 #include <sys/signalvar.h>
86 #include <sys/socket.h>
87 #include <sys/time.h>
88 #include <sys/times.h>
89 #include <sys/vnode.h>
90 #include <sys/uio.h>
91 #include <sys/wait.h>
92 #include <sys/utsname.h>
93 #include <sys/unistd.h>
94
95 #include <sys/syscallargs.h>
96
97 #include <compat/linux/common/linux_types.h>
98 #include <compat/linux/common/linux_signal.h>
99
100 #include <compat/linux/linux_syscallargs.h>
101
102 #include <compat/linux/common/linux_fcntl.h>
103 #include <compat/linux/common/linux_mmap.h>
104 #include <compat/linux/common/linux_dirent.h>
105 #include <compat/linux/common/linux_util.h>
106 #include <compat/linux/common/linux_misc.h>
107 #include <compat/linux/common/linux_ptrace.h>
108 #include <compat/linux/common/linux_reboot.h>
109
110 int linux_ptrace_request_map[] = {
111 LINUX_PTRACE_TRACEME, PT_TRACE_ME,
112 LINUX_PTRACE_PEEKTEXT, PT_READ_I,
113 LINUX_PTRACE_PEEKDATA, PT_READ_D,
114 LINUX_PTRACE_POKETEXT, PT_WRITE_I,
115 LINUX_PTRACE_POKEDATA, PT_WRITE_D,
116 LINUX_PTRACE_CONT, PT_CONTINUE,
117 LINUX_PTRACE_KILL, PT_KILL,
118 LINUX_PTRACE_ATTACH, PT_ATTACH,
119 LINUX_PTRACE_DETACH, PT_DETACH,
120 -1
121 };
122
123 /* Local linux_misc.c functions: */
124 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
125
126 /*
127 * The information on a terminated (or stopped) process needs
128 * to be converted in order for Linux binaries to get a valid signal
129 * number out of it.
130 */
131 void
132 bsd_to_linux_wstat(st)
133 int *st;
134 {
135
136 int sig;
137
138 if (WIFSIGNALED(*st)) {
139 sig = WTERMSIG(*st);
140 if (sig >= 0 && sig < NSIG)
141 *st= (*st& ~0177) | native_to_linux_sig[sig];
142 } else if (WIFSTOPPED(*st)) {
143 sig = WSTOPSIG(*st);
144 if (sig >= 0 && sig < NSIG)
145 *st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
146 }
147 }
148
149 /*
150 * This is very much the same as waitpid()
151 */
152 int
153 linux_sys_wait4(p, v, retval)
154 struct proc *p;
155 void *v;
156 register_t *retval;
157 {
158 struct linux_sys_wait4_args /* {
159 syscallarg(int) pid;
160 syscallarg(int *) status;
161 syscallarg(int) options;
162 syscallarg(struct rusage *) rusage;
163 } */ *uap = v;
164 struct sys_wait4_args w4a;
165 int error, *status, tstat, options, linux_options;
166 caddr_t sg;
167
168 if (SCARG(uap, status) != NULL) {
169 sg = stackgap_init(p->p_emul);
170 status = (int *) stackgap_alloc(&sg, sizeof *status);
171 } else
172 status = NULL;
173
174 linux_options = SCARG(uap, options);
175 options = 0;
176 if (linux_options &
177 ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WCLONE))
178 return (EINVAL);
179
180 if (linux_options & LINUX_WAIT4_WNOHANG)
181 options |= WNOHANG;
182 if (linux_options & LINUX_WAIT4_WUNTRACED)
183 options |= WUNTRACED;
184 if (linux_options & LINUX_WAIT4_WCLONE)
185 options |= WALTSIG;
186
187 SCARG(&w4a, pid) = SCARG(uap, pid);
188 SCARG(&w4a, status) = status;
189 SCARG(&w4a, options) = options;
190 SCARG(&w4a, rusage) = SCARG(uap, rusage);
191
192 if ((error = sys_wait4(p, &w4a, retval)))
193 return error;
194
195 sigdelset(&p->p_siglist, SIGCHLD);
196
197 if (status != NULL) {
198 if ((error = copyin(status, &tstat, sizeof tstat)))
199 return error;
200
201 bsd_to_linux_wstat(&tstat);
202 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
203 }
204
205 return 0;
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 */
212 int
213 linux_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 caddr_t oldbrk;
225
226 oldbrk = vm->vm_daddr + ctob(vm->vm_dsize);
227 /*
228 * XXX inconsistent.. Linux always returns at least the old
229 * brk value, but it will be page-aligned if this fails,
230 * and possibly not page aligned if it succeeds (the user
231 * supplied pointer is returned).
232 */
233 SCARG(&oba, nsize) = nbrk;
234
235 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
236 retval[0] = (register_t)nbrk;
237 else
238 retval[0] = (register_t)oldbrk;
239
240 return 0;
241 }
242
243 /*
244 * Convert BSD statfs structure to Linux statfs structure.
245 * The Linux structure has less fields, and it also wants
246 * the length of a name in a dir entry in a field, which
247 * we fake (probably the wrong way).
248 */
249 static void
250 bsd_to_linux_statfs(bsp, lsp)
251 struct statfs *bsp;
252 struct linux_statfs *lsp;
253 {
254
255 lsp->l_ftype = bsp->f_type;
256 lsp->l_fbsize = bsp->f_bsize;
257 lsp->l_fblocks = bsp->f_blocks;
258 lsp->l_fbfree = bsp->f_bfree;
259 lsp->l_fbavail = bsp->f_bavail;
260 lsp->l_ffiles = bsp->f_files;
261 lsp->l_fffree = bsp->f_ffree;
262 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
263 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
264 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
265 }
266
267 /*
268 * Implement the fs stat functions. Straightforward.
269 */
270 int
271 linux_sys_statfs(p, v, retval)
272 struct proc *p;
273 void *v;
274 register_t *retval;
275 {
276 struct linux_sys_statfs_args /* {
277 syscallarg(const char *) path;
278 syscallarg(struct linux_statfs *) sp;
279 } */ *uap = v;
280 struct statfs btmp, *bsp;
281 struct linux_statfs ltmp;
282 struct sys_statfs_args bsa;
283 caddr_t sg;
284 int error;
285
286 sg = stackgap_init(p->p_emul);
287 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
288
289 LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
290
291 SCARG(&bsa, path) = SCARG(uap, path);
292 SCARG(&bsa, buf) = bsp;
293
294 if ((error = sys_statfs(p, &bsa, retval)))
295 return error;
296
297 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
298 return error;
299
300 bsd_to_linux_statfs(&btmp, <mp);
301
302 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
303 }
304
305 int
306 linux_sys_fstatfs(p, v, retval)
307 struct proc *p;
308 void *v;
309 register_t *retval;
310 {
311 struct linux_sys_fstatfs_args /* {
312 syscallarg(int) fd;
313 syscallarg(struct linux_statfs *) sp;
314 } */ *uap = v;
315 struct statfs btmp, *bsp;
316 struct linux_statfs ltmp;
317 struct sys_fstatfs_args bsa;
318 caddr_t sg;
319 int error;
320
321 sg = stackgap_init(p->p_emul);
322 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
323
324 SCARG(&bsa, fd) = SCARG(uap, fd);
325 SCARG(&bsa, buf) = bsp;
326
327 if ((error = sys_fstatfs(p, &bsa, retval)))
328 return error;
329
330 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
331 return error;
332
333 bsd_to_linux_statfs(&btmp, <mp);
334
335 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
336 }
337
338 /*
339 * uname(). Just copy the info from the various strings stored in the
340 * kernel, and put it in the Linux utsname structure. That structure
341 * is almost the same as the NetBSD one, only it has fields 65 characters
342 * long, and an extra domainname field.
343 */
344 int
345 linux_sys_uname(p, v, retval)
346 struct proc *p;
347 void *v;
348 register_t *retval;
349 {
350 struct linux_sys_uname_args /* {
351 syscallarg(struct linux_utsname *) up;
352 } */ *uap = v;
353 struct linux_utsname luts;
354 int len;
355 char *cp;
356
357 strncpy(luts.l_sysname, ostype, sizeof(luts.l_sysname));
358 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
359 strncpy(luts.l_release, osrelease, sizeof(luts.l_release));
360 strncpy(luts.l_version, version, sizeof(luts.l_version));
361 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
362 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
363
364 /* This part taken from the uname() in libc */
365 len = sizeof(luts.l_version);
366 for (cp = luts.l_version; len--; ++cp) {
367 if (*cp == '\n' || *cp == '\t') {
368 if (len > 1)
369 *cp = ' ';
370 else
371 *cp = '\0';
372 }
373 }
374
375 return copyout(&luts, SCARG(uap, up), sizeof(luts));
376 }
377
378 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
379 /* Used indirectly on: arm, i386, m68k */
380
381 /*
382 * New type Linux mmap call.
383 * Only called directly on machines with >= 6 free regs.
384 */
385 int
386 linux_sys_mmap(p, v, retval)
387 struct proc *p;
388 void *v;
389 register_t *retval;
390 {
391 struct linux_sys_mmap_args /* {
392 syscallarg(unsigned long) addr;
393 syscallarg(size_t) len;
394 syscallarg(int) prot;
395 syscallarg(int) flags;
396 syscallarg(int) fd;
397 syscallarg(off_t) offset;
398 } */ *uap = v;
399 struct sys_mmap_args cma;
400 int flags;
401
402 flags = 0;
403 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
404 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
405 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
406 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
407 /* XXX XAX ERH: Any other flags here? There are more defined... */
408
409 SCARG(&cma,addr) = (void *)SCARG(uap, addr);
410 SCARG(&cma,len) = SCARG(uap, len);
411 SCARG(&cma,prot) = SCARG(uap, prot);
412 if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
413 SCARG(&cma,prot) |= VM_PROT_READ;
414 SCARG(&cma,flags) = flags;
415 SCARG(&cma,fd) = SCARG(uap, fd);
416 SCARG(&cma,pad) = 0;
417 SCARG(&cma,pos) = SCARG(uap, offset);
418
419 return sys_mmap(p, &cma, retval);
420 }
421
422 int
423 linux_sys_mremap(p, v, retval)
424 struct proc *p;
425 void *v;
426 register_t *retval;
427 {
428 struct linux_sys_mremap_args /* {
429 syscallarg(void *) old_address;
430 syscallarg(size_t) old_size;
431 syscallarg(size_t) new_size;
432 syscallarg(u_long) flags;
433 } */ *uap = v;
434 struct sys_munmap_args mua;
435 size_t old_size, new_size;
436 int error;
437
438 old_size = round_page(SCARG(uap, old_size));
439 new_size = round_page(SCARG(uap, new_size));
440
441 /*
442 * Growing mapped region.
443 */
444 if (new_size > old_size) {
445 /*
446 * XXX Implement me. What we probably want to do is
447 * XXX dig out the guts of the old mapping, mmap that
448 * XXX object again with the new size, then munmap
449 * XXX the old mapping.
450 */
451 *retval = 0;
452 return (ENOMEM);
453 }
454
455 /*
456 * Shrinking mapped region.
457 */
458 if (new_size < old_size) {
459 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
460 new_size;
461 SCARG(&mua, len) = old_size - new_size;
462 error = sys_munmap(p, &mua, retval);
463 *retval = error ? 0 : (register_t)SCARG(uap, old_address);
464 return (error);
465 }
466
467 /*
468 * No change.
469 */
470 *retval = (register_t)SCARG(uap, old_address);
471 return (0);
472 }
473
474 int
475 linux_sys_msync(p, v, retval)
476 struct proc *p;
477 void *v;
478 register_t *retval;
479 {
480 struct linux_sys_msync_args /* {
481 syscallarg(caddr_t) addr;
482 syscallarg(int) len;
483 syscallarg(int) fl;
484 } */ *uap = v;
485
486 struct sys___msync13_args bma;
487
488 /* flags are ignored */
489 SCARG(&bma, addr) = SCARG(uap, addr);
490 SCARG(&bma, len) = SCARG(uap, len);
491 SCARG(&bma, flags) = SCARG(uap, fl);
492
493 return sys___msync13(p, &bma, retval);
494 }
495
496 /*
497 * This code is partly stolen from src/lib/libc/compat-43/times.c
498 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
499 */
500
501 #define CLK_TCK 100
502 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
503
504 int
505 linux_sys_times(p, v, retval)
506 struct proc *p;
507 void *v;
508 register_t *retval;
509 {
510 struct linux_sys_times_args /* {
511 syscallarg(struct times *) tms;
512 } */ *uap = v;
513 struct timeval t;
514 struct linux_tms ltms;
515 struct rusage ru;
516 int error, s;
517
518 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
519 ltms.ltms_utime = CONVTCK(ru.ru_utime);
520 ltms.ltms_stime = CONVTCK(ru.ru_stime);
521
522 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
523 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
524
525 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
526 return error;
527
528 s = splclock();
529 timersub(&time, &boottime, &t);
530 splx(s);
531
532 retval[0] = ((linux_clock_t)(CONVTCK(t)));
533 return 0;
534 }
535
536 /*
537 * Linux 'readdir' call. This code is mostly taken from the
538 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
539 * an attempt has been made to keep it a little cleaner (failing
540 * miserably, because of the cruft needed if count 1 is passed).
541 *
542 * The d_off field should contain the offset of the next valid entry,
543 * but in Linux it has the offset of the entry itself. We emulate
544 * that bug here.
545 *
546 * Read in BSD-style entries, convert them, and copy them out.
547 *
548 * Note that this doesn't handle union-mounted filesystems.
549 */
550 int
551 linux_sys_getdents(p, v, retval)
552 struct proc *p;
553 void *v;
554 register_t *retval;
555 {
556 struct linux_sys_getdents_args /* {
557 syscallarg(int) fd;
558 syscallarg(struct linux_dirent *) dent;
559 syscallarg(unsigned int) count;
560 } */ *uap = v;
561 struct dirent *bdp;
562 struct vnode *vp;
563 caddr_t inp, buf; /* BSD-format */
564 int len, reclen; /* BSD-format */
565 caddr_t outp; /* Linux-format */
566 int resid, linux_reclen = 0; /* Linux-format */
567 struct file *fp;
568 struct uio auio;
569 struct iovec aiov;
570 struct linux_dirent idb;
571 off_t off; /* true file offset */
572 int buflen, error, eofflag, nbytes, oldcall;
573 struct vattr va;
574 off_t *cookiebuf = NULL, *cookie;
575 int ncookies;
576
577 /* getvnode() will use the descriptor for us */
578 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
579 return (error);
580
581 if ((fp->f_flag & FREAD) == 0) {
582 error = EBADF;
583 goto out1;
584 }
585
586 vp = (struct vnode *)fp->f_data;
587 if (vp->v_type != VDIR) {
588 error = EINVAL;
589 goto out1;
590 }
591
592 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
593 goto out1;
594
595 nbytes = SCARG(uap, count);
596 if (nbytes == 1) { /* emulating old, broken behaviour */
597 nbytes = sizeof (struct linux_dirent);
598 buflen = max(va.va_blocksize, nbytes);
599 oldcall = 1;
600 } else {
601 buflen = min(MAXBSIZE, nbytes);
602 if (buflen < va.va_blocksize)
603 buflen = va.va_blocksize;
604 oldcall = 0;
605 }
606 buf = malloc(buflen, M_TEMP, M_WAITOK);
607
608 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
609 off = fp->f_offset;
610 again:
611 aiov.iov_base = buf;
612 aiov.iov_len = buflen;
613 auio.uio_iov = &aiov;
614 auio.uio_iovcnt = 1;
615 auio.uio_rw = UIO_READ;
616 auio.uio_segflg = UIO_SYSSPACE;
617 auio.uio_procp = p;
618 auio.uio_resid = buflen;
619 auio.uio_offset = off;
620 /*
621 * First we read into the malloc'ed buffer, then
622 * we massage it into user space, one record at a time.
623 */
624 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
625 &ncookies);
626 if (error)
627 goto out;
628
629 inp = buf;
630 outp = (caddr_t)SCARG(uap, dent);
631 resid = nbytes;
632 if ((len = buflen - auio.uio_resid) == 0)
633 goto eof;
634
635 for (cookie = cookiebuf; len > 0; len -= reclen) {
636 bdp = (struct dirent *)inp;
637 reclen = bdp->d_reclen;
638 if (reclen & 3)
639 panic("linux_readdir");
640 if (bdp->d_fileno == 0) {
641 inp += reclen; /* it is a hole; squish it out */
642 off = *cookie++;
643 continue;
644 }
645 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
646 if (reclen > len || resid < linux_reclen) {
647 /* entry too big for buffer, so just stop */
648 outp++;
649 break;
650 }
651 /*
652 * Massage in place to make a Linux-shaped dirent (otherwise
653 * we have to worry about touching user memory outside of
654 * the copyout() call).
655 */
656 idb.d_ino = (linux_ino_t)bdp->d_fileno;
657 /*
658 * The old readdir() call misuses the offset and reclen fields.
659 */
660 if (oldcall) {
661 idb.d_off = (linux_off_t)linux_reclen;
662 idb.d_reclen = (u_short)bdp->d_namlen;
663 } else {
664 if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
665 compat_offseterr(vp, "linux_getdents");
666 error = EINVAL;
667 goto out;
668 }
669 idb.d_off = (linux_off_t)off;
670 idb.d_reclen = (u_short)linux_reclen;
671 }
672 strcpy(idb.d_name, bdp->d_name);
673 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
674 goto out;
675 /* advance past this real entry */
676 inp += reclen;
677 off = *cookie++; /* each entry points to itself */
678 /* advance output past Linux-shaped entry */
679 outp += linux_reclen;
680 resid -= linux_reclen;
681 if (oldcall)
682 break;
683 }
684
685 /* if we squished out the whole block, try again */
686 if (outp == (caddr_t)SCARG(uap, dent))
687 goto again;
688 fp->f_offset = off; /* update the vnode offset */
689
690 if (oldcall)
691 nbytes = resid + linux_reclen;
692
693 eof:
694 *retval = nbytes - resid;
695 out:
696 VOP_UNLOCK(vp, 0);
697 if (cookiebuf)
698 free(cookiebuf, M_TEMP);
699 free(buf, M_TEMP);
700 out1:
701 FILE_UNUSE(fp, p);
702 return error;
703 }
704
705 /*
706 * Even when just using registers to pass arguments to syscalls you can
707 * have 5 of them on the i386. So this newer version of select() does
708 * this.
709 */
710 int
711 linux_sys_select(p, v, retval)
712 struct proc *p;
713 void *v;
714 register_t *retval;
715 {
716 struct linux_sys_select_args /* {
717 syscallarg(int) nfds;
718 syscallarg(fd_set *) readfds;
719 syscallarg(fd_set *) writefds;
720 syscallarg(fd_set *) exceptfds;
721 syscallarg(struct timeval *) timeout;
722 } */ *uap = v;
723
724 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
725 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
726 }
727
728 /*
729 * Common code for the old and new versions of select(). A couple of
730 * things are important:
731 * 1) return the amount of time left in the 'timeout' parameter
732 * 2) select never returns ERESTART on Linux, always return EINTR
733 */
734 int
735 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
736 struct proc *p;
737 register_t *retval;
738 int nfds;
739 fd_set *readfds, *writefds, *exceptfds;
740 struct timeval *timeout;
741 {
742 struct sys_select_args bsa;
743 struct timeval tv0, tv1, utv, *tvp;
744 caddr_t sg;
745 int error;
746
747 SCARG(&bsa, nd) = nfds;
748 SCARG(&bsa, in) = readfds;
749 SCARG(&bsa, ou) = writefds;
750 SCARG(&bsa, ex) = exceptfds;
751 SCARG(&bsa, tv) = timeout;
752
753 /*
754 * Store current time for computation of the amount of
755 * time left.
756 */
757 if (timeout) {
758 if ((error = copyin(timeout, &utv, sizeof(utv))))
759 return error;
760 if (itimerfix(&utv)) {
761 /*
762 * The timeval was invalid. Convert it to something
763 * valid that will act as it does under Linux.
764 */
765 sg = stackgap_init(p->p_emul);
766 tvp = stackgap_alloc(&sg, sizeof(utv));
767 utv.tv_sec += utv.tv_usec / 1000000;
768 utv.tv_usec %= 1000000;
769 if (utv.tv_usec < 0) {
770 utv.tv_sec -= 1;
771 utv.tv_usec += 1000000;
772 }
773 if (utv.tv_sec < 0)
774 timerclear(&utv);
775 if ((error = copyout(&utv, tvp, sizeof(utv))))
776 return error;
777 SCARG(&bsa, tv) = tvp;
778 }
779 microtime(&tv0);
780 }
781
782 error = sys_select(p, &bsa, retval);
783 if (error) {
784 /*
785 * See fs/select.c in the Linux kernel. Without this,
786 * Maelstrom doesn't work.
787 */
788 if (error == ERESTART)
789 error = EINTR;
790 return error;
791 }
792
793 if (timeout) {
794 if (*retval) {
795 /*
796 * Compute how much time was left of the timeout,
797 * by subtracting the current time and the time
798 * before we started the call, and subtracting
799 * that result from the user-supplied value.
800 */
801 microtime(&tv1);
802 timersub(&tv1, &tv0, &tv1);
803 timersub(&utv, &tv1, &utv);
804 if (utv.tv_sec < 0)
805 timerclear(&utv);
806 } else
807 timerclear(&utv);
808 if ((error = copyout(&utv, timeout, sizeof(utv))))
809 return error;
810 }
811
812 return 0;
813 }
814
815 /*
816 * Get the process group of a certain process. Look it up
817 * and return the value.
818 */
819 int
820 linux_sys_getpgid(p, v, retval)
821 struct proc *p;
822 void *v;
823 register_t *retval;
824 {
825 struct linux_sys_getpgid_args /* {
826 syscallarg(int) pid;
827 } */ *uap = v;
828 struct proc *targp;
829
830 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
831 if ((targp = pfind(SCARG(uap, pid))) == 0)
832 return ESRCH;
833 }
834 else
835 targp = p;
836
837 retval[0] = targp->p_pgid;
838 return 0;
839 }
840
841 /*
842 * Set the 'personality' (emulation mode) for the current process. Only
843 * accept the Linux personality here (0). This call is needed because
844 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
845 * ELF binaries run in Linux mode, not SVR4 mode.
846 */
847 int
848 linux_sys_personality(p, v, retval)
849 struct proc *p;
850 void *v;
851 register_t *retval;
852 {
853 struct linux_sys_personality_args /* {
854 syscallarg(int) per;
855 } */ *uap = v;
856
857 if (SCARG(uap, per) != 0)
858 return EINVAL;
859 retval[0] = 0;
860 return 0;
861 }
862
863 /*
864 * The calls are here because of type conversions.
865 */
866 int
867 linux_sys_setreuid(p, v, retval)
868 struct proc *p;
869 void *v;
870 register_t *retval;
871 {
872 struct linux_sys_setreuid_args /* {
873 syscallarg(int) ruid;
874 syscallarg(int) euid;
875 } */ *uap = v;
876 struct sys_setreuid_args bsa;
877
878 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
879 (uid_t)-1 : SCARG(uap, ruid);
880 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
881 (uid_t)-1 : SCARG(uap, euid);
882
883 return sys_setreuid(p, &bsa, retval);
884 }
885
886 int
887 linux_sys_setregid(p, v, retval)
888 struct proc *p;
889 void *v;
890 register_t *retval;
891 {
892 struct linux_sys_setregid_args /* {
893 syscallarg(int) rgid;
894 syscallarg(int) egid;
895 } */ *uap = v;
896 struct sys_setregid_args bsa;
897
898 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
899 (uid_t)-1 : SCARG(uap, rgid);
900 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
901 (uid_t)-1 : SCARG(uap, egid);
902
903 return sys_setregid(p, &bsa, retval);
904 }
905
906 /*
907 * We have nonexistent fsuid equal to uid.
908 * If modification is requested, refuse.
909 */
910 int
911 linux_sys_setfsuid(p, v, retval)
912 struct proc *p;
913 void *v;
914 register_t *retval;
915 {
916 struct linux_sys_setfsuid_args /* {
917 syscallarg(uid_t) uid;
918 } */ *uap = v;
919 uid_t uid;
920
921 uid = SCARG(uap, uid);
922 if (p->p_cred->p_ruid != uid)
923 return sys_nosys(p, v, retval);
924 else
925 return (0);
926 }
927
928 /* XXX XXX XXX */
929 #ifndef alpha
930 int
931 linux_sys_getfsuid(p, v, retval)
932 struct proc *p;
933 void *v;
934 register_t *retval;
935 {
936 return sys_getuid(p, v, retval);
937 }
938 #endif
939
940 int
941 linux_sys___sysctl(p, v, retval)
942 struct proc *p;
943 void *v;
944 register_t *retval;
945 {
946 struct linux_sys___sysctl_args /* {
947 syscallarg(struct linux___sysctl *) lsp;
948 } */ *uap = v;
949 struct linux___sysctl ls;
950 struct sys___sysctl_args bsa;
951 int error;
952
953 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
954 return error;
955 SCARG(&bsa, name) = ls.name;
956 SCARG(&bsa, namelen) = ls.namelen;
957 SCARG(&bsa, old) = ls.old;
958 SCARG(&bsa, oldlenp) = ls.oldlenp;
959 SCARG(&bsa, new) = ls.new;
960 SCARG(&bsa, newlen) = ls.newlen;
961
962 return sys___sysctl(p, &bsa, retval);
963 }
964
965 int
966 linux_sys_setresuid(p, v, retval)
967 struct proc *p;
968 void *v;
969 register_t *retval;
970 {
971 struct linux_sys_setresuid_args /* {
972 syscallarg(uid_t) ruid;
973 syscallarg(uid_t) euid;
974 syscallarg(uid_t) suid;
975 } */ *uap = v;
976 struct pcred *pc = p->p_cred;
977 uid_t ruid, euid, suid;
978 int error;
979
980 ruid = SCARG(uap, ruid);
981 euid = SCARG(uap, euid);
982 suid = SCARG(uap, suid);
983
984 /*
985 * Note: These checks are a little different than the NetBSD
986 * setreuid(2) call performs. This precisely follows the
987 * behavior of the Linux kernel.
988 */
989 if (ruid != (uid_t)-1 &&
990 ruid != pc->p_ruid &&
991 ruid != pc->pc_ucred->cr_uid &&
992 ruid != pc->p_svuid &&
993 (error = suser(pc->pc_ucred, &p->p_acflag)))
994 return (error);
995
996 if (euid != (uid_t)-1 &&
997 euid != pc->p_ruid &&
998 euid != pc->pc_ucred->cr_uid &&
999 euid != pc->p_svuid &&
1000 (error = suser(pc->pc_ucred, &p->p_acflag)))
1001 return (error);
1002
1003 if (suid != (uid_t)-1 &&
1004 suid != pc->p_ruid &&
1005 suid != pc->pc_ucred->cr_uid &&
1006 suid != pc->p_svuid &&
1007 (error = suser(pc->pc_ucred, &p->p_acflag)))
1008 return (error);
1009
1010 /*
1011 * Now assign the new real, effective, and saved UIDs.
1012 * Note that Linux, unlike NetBSD in setreuid(2), does not
1013 * set the saved UID in this call unless the user specifies
1014 * it.
1015 */
1016 if (ruid != (uid_t)-1) {
1017 (void)chgproccnt(pc->p_ruid, -1);
1018 (void)chgproccnt(ruid, 1);
1019 pc->p_ruid = ruid;
1020 }
1021
1022 if (euid != (uid_t)-1) {
1023 pc->pc_ucred = crcopy(pc->pc_ucred);
1024 pc->pc_ucred->cr_uid = euid;
1025 }
1026
1027 if (suid != (uid_t)-1)
1028 pc->p_svuid = suid;
1029
1030 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1031 p->p_flag |= P_SUGID;
1032 return (0);
1033 }
1034
1035 int
1036 linux_sys_getresuid(p, v, retval)
1037 struct proc *p;
1038 void *v;
1039 register_t *retval;
1040 {
1041 struct linux_sys_getresuid_args /* {
1042 syscallarg(uid_t *) ruid;
1043 syscallarg(uid_t *) euid;
1044 syscallarg(uid_t *) suid;
1045 } */ *uap = v;
1046 struct pcred *pc = p->p_cred;
1047 int error;
1048
1049 /*
1050 * Linux copies these values out to userspace like so:
1051 *
1052 * 1. Copy out ruid.
1053 * 2. If that succeeds, copy out euid.
1054 * 3. If both of those succeed, copy out suid.
1055 */
1056 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1057 sizeof(uid_t))) != 0)
1058 return (error);
1059
1060 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1061 sizeof(uid_t))) != 0)
1062 return (error);
1063
1064 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1065 }
1066
1067 int
1068 linux_sys_ptrace(p, v, retval)
1069 struct proc *p;
1070 void *v;
1071 register_t *retval;
1072 {
1073 struct linux_sys_ptrace_args /* {
1074 i386, m68k: T=int
1075 alpha: T=long
1076 syscallarg(T) request;
1077 syscallarg(T) pid;
1078 syscallarg(T) addr;
1079 syscallarg(T) data;
1080 } */ *uap = v;
1081 int *ptr, request;
1082
1083 ptr = linux_ptrace_request_map;
1084 request = SCARG(uap, request);
1085 while (*ptr != -1)
1086 if (*ptr++ == request) {
1087 struct sys_ptrace_args pta;
1088 caddr_t sg;
1089
1090 sg = stackgap_init(p->p_emul);
1091
1092 SCARG(&pta, req) = *ptr;
1093 SCARG(&pta, pid) = SCARG(uap, pid);
1094 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1095 SCARG(&pta, data) = SCARG(uap, data);
1096
1097 return sys_ptrace(p, &pta, retval);
1098 }
1099 else
1100 ptr++;
1101
1102 return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
1103 }
1104
1105 int
1106 linux_sys_reboot(struct proc *p, void *v, register_t *retval)
1107 {
1108 struct linux_sys_reboot_args /* {
1109 syscallarg(int) magic1;
1110 syscallarg(int) magic2;
1111 syscallarg(int) cmd;
1112 syscallarg(void *) arg;
1113 } */ *uap = v;
1114 struct sys_reboot_args /* {
1115 syscallarg(int) opt;
1116 syscallarg(char *) bootstr;
1117 } */ sra;
1118 int error;
1119
1120 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1121 return(error);
1122
1123 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1124 return(EINVAL);
1125 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1126 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1127 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1128 return(EINVAL);
1129
1130 switch (SCARG(uap, cmd)) {
1131 case LINUX_REBOOT_CMD_RESTART:
1132 SCARG(&sra, opt) = RB_AUTOBOOT;
1133 break;
1134 case LINUX_REBOOT_CMD_HALT:
1135 SCARG(&sra, opt) = RB_HALT;
1136 break;
1137 case LINUX_REBOOT_CMD_POWER_OFF:
1138 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1139 break;
1140 case LINUX_REBOOT_CMD_RESTART2:
1141 /* Reboot with an argument. */
1142 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1143 SCARG(&sra, bootstr) = SCARG(uap, arg);
1144 break;
1145 case LINUX_REBOOT_CMD_CAD_ON:
1146 return(EINVAL); /* We don't implement ctrl-alt-delete */
1147 case LINUX_REBOOT_CMD_CAD_OFF:
1148 return(0);
1149 default:
1150 return(EINVAL);
1151 }
1152
1153 return(sys_reboot(p, &sra, retval));
1154 }
1155