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