linux_misc.c revision 1.69.4.3 1 /* $NetBSD: linux_misc.c,v 1.69.4.3 2001/05/01 08:53:53 he 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 char linux_sysname[] = "Linux";
342 char linux_release[] = "2.0.38";
343 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
344
345 /*
346 * uname(). Just copy the info from the various strings stored in the
347 * kernel, and put it in the Linux utsname structure. That structure
348 * is almost the same as the NetBSD one, only it has fields 65 characters
349 * long, and an extra domainname field.
350 */
351 int
352 linux_sys_uname(p, v, retval)
353 struct proc *p;
354 void *v;
355 register_t *retval;
356 {
357 struct linux_sys_uname_args /* {
358 syscallarg(struct linux_utsname *) up;
359 } */ *uap = v;
360 struct linux_utsname luts;
361
362 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
363 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
364 strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
365 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
366 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
367 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
368
369 return copyout(&luts, SCARG(uap, up), sizeof(luts));
370 }
371
372 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
373 /* Used indirectly on: arm, i386, m68k */
374
375 /*
376 * New type Linux mmap call.
377 * Only called directly on machines with >= 6 free regs.
378 */
379 int
380 linux_sys_mmap(p, v, retval)
381 struct proc *p;
382 void *v;
383 register_t *retval;
384 {
385 struct linux_sys_mmap_args /* {
386 syscallarg(unsigned long) addr;
387 syscallarg(size_t) len;
388 syscallarg(int) prot;
389 syscallarg(int) flags;
390 syscallarg(int) fd;
391 syscallarg(off_t) offset;
392 } */ *uap = v;
393 struct sys_mmap_args cma;
394 int flags;
395
396 flags = 0;
397 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
398 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
399 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
400 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
401 /* XXX XAX ERH: Any other flags here? There are more defined... */
402
403 SCARG(&cma,addr) = (void *)SCARG(uap, addr);
404 SCARG(&cma,len) = SCARG(uap, len);
405 SCARG(&cma,prot) = SCARG(uap, prot);
406 if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
407 SCARG(&cma,prot) |= VM_PROT_READ;
408 SCARG(&cma,flags) = flags;
409 SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
410 SCARG(&cma,pad) = 0;
411 SCARG(&cma,pos) = SCARG(uap, offset);
412
413 return sys_mmap(p, &cma, retval);
414 }
415
416 int
417 linux_sys_mremap(p, v, retval)
418 struct proc *p;
419 void *v;
420 register_t *retval;
421 {
422 struct linux_sys_mremap_args /* {
423 syscallarg(void *) old_address;
424 syscallarg(size_t) old_size;
425 syscallarg(size_t) new_size;
426 syscallarg(u_long) flags;
427 } */ *uap = v;
428 struct sys_munmap_args mua;
429 size_t old_size, new_size;
430 int error;
431
432 old_size = round_page(SCARG(uap, old_size));
433 new_size = round_page(SCARG(uap, new_size));
434
435 /*
436 * Growing mapped region.
437 */
438 if (new_size > old_size) {
439 /*
440 * XXX Implement me. What we probably want to do is
441 * XXX dig out the guts of the old mapping, mmap that
442 * XXX object again with the new size, then munmap
443 * XXX the old mapping.
444 */
445 *retval = 0;
446 return (ENOMEM);
447 }
448
449 /*
450 * Shrinking mapped region.
451 */
452 if (new_size < old_size) {
453 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
454 new_size;
455 SCARG(&mua, len) = old_size - new_size;
456 error = sys_munmap(p, &mua, retval);
457 *retval = error ? 0 : (register_t)SCARG(uap, old_address);
458 return (error);
459 }
460
461 /*
462 * No change.
463 */
464 *retval = (register_t)SCARG(uap, old_address);
465 return (0);
466 }
467
468 int
469 linux_sys_msync(p, v, retval)
470 struct proc *p;
471 void *v;
472 register_t *retval;
473 {
474 struct linux_sys_msync_args /* {
475 syscallarg(caddr_t) addr;
476 syscallarg(int) len;
477 syscallarg(int) fl;
478 } */ *uap = v;
479
480 struct sys___msync13_args bma;
481
482 /* flags are ignored */
483 SCARG(&bma, addr) = SCARG(uap, addr);
484 SCARG(&bma, len) = SCARG(uap, len);
485 SCARG(&bma, flags) = SCARG(uap, fl);
486
487 return sys___msync13(p, &bma, retval);
488 }
489
490 /*
491 * This code is partly stolen from src/lib/libc/compat-43/times.c
492 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
493 */
494
495 #define CLK_TCK 100
496 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
497
498 int
499 linux_sys_times(p, v, retval)
500 struct proc *p;
501 void *v;
502 register_t *retval;
503 {
504 struct linux_sys_times_args /* {
505 syscallarg(struct times *) tms;
506 } */ *uap = v;
507 struct timeval t;
508 struct linux_tms ltms;
509 struct rusage ru;
510 int error, s;
511
512 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
513 ltms.ltms_utime = CONVTCK(ru.ru_utime);
514 ltms.ltms_stime = CONVTCK(ru.ru_stime);
515
516 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
517 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
518
519 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
520 return error;
521
522 s = splclock();
523 timersub(&time, &boottime, &t);
524 splx(s);
525
526 retval[0] = ((linux_clock_t)(CONVTCK(t)));
527 return 0;
528 }
529
530 /*
531 * Linux 'readdir' call. This code is mostly taken from the
532 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
533 * an attempt has been made to keep it a little cleaner (failing
534 * miserably, because of the cruft needed if count 1 is passed).
535 *
536 * The d_off field should contain the offset of the next valid entry,
537 * but in Linux it has the offset of the entry itself. We emulate
538 * that bug here.
539 *
540 * Read in BSD-style entries, convert them, and copy them out.
541 *
542 * Note that this doesn't handle union-mounted filesystems.
543 */
544 int
545 linux_sys_getdents(p, v, retval)
546 struct proc *p;
547 void *v;
548 register_t *retval;
549 {
550 struct linux_sys_getdents_args /* {
551 syscallarg(int) fd;
552 syscallarg(struct linux_dirent *) dent;
553 syscallarg(unsigned int) count;
554 } */ *uap = v;
555 struct dirent *bdp;
556 struct vnode *vp;
557 caddr_t inp, buf; /* BSD-format */
558 int len, reclen; /* BSD-format */
559 caddr_t outp; /* Linux-format */
560 int resid, linux_reclen = 0; /* Linux-format */
561 struct file *fp;
562 struct uio auio;
563 struct iovec aiov;
564 struct linux_dirent idb;
565 off_t off; /* true file offset */
566 int buflen, error, eofflag, nbytes, oldcall;
567 struct vattr va;
568 off_t *cookiebuf = NULL, *cookie;
569 int ncookies;
570
571 /* getvnode() will use the descriptor for us */
572 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
573 return (error);
574
575 if ((fp->f_flag & FREAD) == 0) {
576 error = EBADF;
577 goto out1;
578 }
579
580 vp = (struct vnode *)fp->f_data;
581 if (vp->v_type != VDIR) {
582 error = EINVAL;
583 goto out1;
584 }
585
586 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
587 goto out1;
588
589 nbytes = SCARG(uap, count);
590 if (nbytes == 1) { /* emulating old, broken behaviour */
591 nbytes = sizeof (struct linux_dirent);
592 buflen = max(va.va_blocksize, nbytes);
593 oldcall = 1;
594 } else {
595 buflen = min(MAXBSIZE, nbytes);
596 if (buflen < va.va_blocksize)
597 buflen = va.va_blocksize;
598 oldcall = 0;
599 }
600 buf = malloc(buflen, M_TEMP, M_WAITOK);
601
602 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
603 off = fp->f_offset;
604 again:
605 aiov.iov_base = buf;
606 aiov.iov_len = buflen;
607 auio.uio_iov = &aiov;
608 auio.uio_iovcnt = 1;
609 auio.uio_rw = UIO_READ;
610 auio.uio_segflg = UIO_SYSSPACE;
611 auio.uio_procp = p;
612 auio.uio_resid = buflen;
613 auio.uio_offset = off;
614 /*
615 * First we read into the malloc'ed buffer, then
616 * we massage it into user space, one record at a time.
617 */
618 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
619 &ncookies);
620 if (error)
621 goto out;
622
623 inp = buf;
624 outp = (caddr_t)SCARG(uap, dent);
625 resid = nbytes;
626 if ((len = buflen - auio.uio_resid) == 0)
627 goto eof;
628
629 for (cookie = cookiebuf; len > 0; len -= reclen) {
630 bdp = (struct dirent *)inp;
631 reclen = bdp->d_reclen;
632 if (reclen & 3)
633 panic("linux_readdir");
634 if (bdp->d_fileno == 0) {
635 inp += reclen; /* it is a hole; squish it out */
636 off = *cookie++;
637 continue;
638 }
639 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
640 if (reclen > len || resid < linux_reclen) {
641 /* entry too big for buffer, so just stop */
642 outp++;
643 break;
644 }
645 /*
646 * Massage in place to make a Linux-shaped dirent (otherwise
647 * we have to worry about touching user memory outside of
648 * the copyout() call).
649 */
650 idb.d_ino = (linux_ino_t)bdp->d_fileno;
651 /*
652 * The old readdir() call misuses the offset and reclen fields.
653 */
654 if (oldcall) {
655 idb.d_off = (linux_off_t)linux_reclen;
656 idb.d_reclen = (u_short)bdp->d_namlen;
657 } else {
658 if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
659 compat_offseterr(vp, "linux_getdents");
660 error = EINVAL;
661 goto out;
662 }
663 idb.d_off = (linux_off_t)off;
664 idb.d_reclen = (u_short)linux_reclen;
665 }
666 strcpy(idb.d_name, bdp->d_name);
667 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
668 goto out;
669 /* advance past this real entry */
670 inp += reclen;
671 off = *cookie++; /* each entry points to itself */
672 /* advance output past Linux-shaped entry */
673 outp += linux_reclen;
674 resid -= linux_reclen;
675 if (oldcall)
676 break;
677 }
678
679 /* if we squished out the whole block, try again */
680 if (outp == (caddr_t)SCARG(uap, dent))
681 goto again;
682 fp->f_offset = off; /* update the vnode offset */
683
684 if (oldcall)
685 nbytes = resid + linux_reclen;
686
687 eof:
688 *retval = nbytes - resid;
689 out:
690 VOP_UNLOCK(vp, 0);
691 if (cookiebuf)
692 free(cookiebuf, M_TEMP);
693 free(buf, M_TEMP);
694 out1:
695 FILE_UNUSE(fp, p);
696 return error;
697 }
698
699 /*
700 * Even when just using registers to pass arguments to syscalls you can
701 * have 5 of them on the i386. So this newer version of select() does
702 * this.
703 */
704 int
705 linux_sys_select(p, v, retval)
706 struct proc *p;
707 void *v;
708 register_t *retval;
709 {
710 struct linux_sys_select_args /* {
711 syscallarg(int) nfds;
712 syscallarg(fd_set *) readfds;
713 syscallarg(fd_set *) writefds;
714 syscallarg(fd_set *) exceptfds;
715 syscallarg(struct timeval *) timeout;
716 } */ *uap = v;
717
718 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
719 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
720 }
721
722 /*
723 * Common code for the old and new versions of select(). A couple of
724 * things are important:
725 * 1) return the amount of time left in the 'timeout' parameter
726 * 2) select never returns ERESTART on Linux, always return EINTR
727 */
728 int
729 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
730 struct proc *p;
731 register_t *retval;
732 int nfds;
733 fd_set *readfds, *writefds, *exceptfds;
734 struct timeval *timeout;
735 {
736 struct sys_select_args bsa;
737 struct timeval tv0, tv1, utv, *tvp;
738 caddr_t sg;
739 int error;
740
741 SCARG(&bsa, nd) = nfds;
742 SCARG(&bsa, in) = readfds;
743 SCARG(&bsa, ou) = writefds;
744 SCARG(&bsa, ex) = exceptfds;
745 SCARG(&bsa, tv) = timeout;
746
747 /*
748 * Store current time for computation of the amount of
749 * time left.
750 */
751 if (timeout) {
752 if ((error = copyin(timeout, &utv, sizeof(utv))))
753 return error;
754 if (itimerfix(&utv)) {
755 /*
756 * The timeval was invalid. Convert it to something
757 * valid that will act as it does under Linux.
758 */
759 sg = stackgap_init(p->p_emul);
760 tvp = stackgap_alloc(&sg, sizeof(utv));
761 utv.tv_sec += utv.tv_usec / 1000000;
762 utv.tv_usec %= 1000000;
763 if (utv.tv_usec < 0) {
764 utv.tv_sec -= 1;
765 utv.tv_usec += 1000000;
766 }
767 if (utv.tv_sec < 0)
768 timerclear(&utv);
769 if ((error = copyout(&utv, tvp, sizeof(utv))))
770 return error;
771 SCARG(&bsa, tv) = tvp;
772 }
773 microtime(&tv0);
774 }
775
776 error = sys_select(p, &bsa, retval);
777 if (error) {
778 /*
779 * See fs/select.c in the Linux kernel. Without this,
780 * Maelstrom doesn't work.
781 */
782 if (error == ERESTART)
783 error = EINTR;
784 return error;
785 }
786
787 if (timeout) {
788 if (*retval) {
789 /*
790 * Compute how much time was left of the timeout,
791 * by subtracting the current time and the time
792 * before we started the call, and subtracting
793 * that result from the user-supplied value.
794 */
795 microtime(&tv1);
796 timersub(&tv1, &tv0, &tv1);
797 timersub(&utv, &tv1, &utv);
798 if (utv.tv_sec < 0)
799 timerclear(&utv);
800 } else
801 timerclear(&utv);
802 if ((error = copyout(&utv, timeout, sizeof(utv))))
803 return error;
804 }
805
806 return 0;
807 }
808
809 /*
810 * Get the process group of a certain process. Look it up
811 * and return the value.
812 */
813 int
814 linux_sys_getpgid(p, v, retval)
815 struct proc *p;
816 void *v;
817 register_t *retval;
818 {
819 struct linux_sys_getpgid_args /* {
820 syscallarg(int) pid;
821 } */ *uap = v;
822 struct proc *targp;
823
824 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
825 if ((targp = pfind(SCARG(uap, pid))) == 0)
826 return ESRCH;
827 }
828 else
829 targp = p;
830
831 retval[0] = targp->p_pgid;
832 return 0;
833 }
834
835 /*
836 * Set the 'personality' (emulation mode) for the current process. Only
837 * accept the Linux personality here (0). This call is needed because
838 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
839 * ELF binaries run in Linux mode, not SVR4 mode.
840 */
841 int
842 linux_sys_personality(p, v, retval)
843 struct proc *p;
844 void *v;
845 register_t *retval;
846 {
847 struct linux_sys_personality_args /* {
848 syscallarg(int) per;
849 } */ *uap = v;
850
851 if (SCARG(uap, per) != 0)
852 return EINVAL;
853 retval[0] = 0;
854 return 0;
855 }
856
857 /*
858 * The calls are here because of type conversions.
859 */
860 int
861 linux_sys_setreuid(p, v, retval)
862 struct proc *p;
863 void *v;
864 register_t *retval;
865 {
866 struct linux_sys_setreuid_args /* {
867 syscallarg(int) ruid;
868 syscallarg(int) euid;
869 } */ *uap = v;
870 struct sys_setreuid_args bsa;
871
872 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
873 (uid_t)-1 : SCARG(uap, ruid);
874 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
875 (uid_t)-1 : SCARG(uap, euid);
876
877 return sys_setreuid(p, &bsa, retval);
878 }
879
880 int
881 linux_sys_setregid(p, v, retval)
882 struct proc *p;
883 void *v;
884 register_t *retval;
885 {
886 struct linux_sys_setregid_args /* {
887 syscallarg(int) rgid;
888 syscallarg(int) egid;
889 } */ *uap = v;
890 struct sys_setregid_args bsa;
891
892 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
893 (uid_t)-1 : SCARG(uap, rgid);
894 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
895 (uid_t)-1 : SCARG(uap, egid);
896
897 return sys_setregid(p, &bsa, retval);
898 }
899
900 /*
901 * We have nonexistent fsuid equal to uid.
902 * If modification is requested, refuse.
903 */
904 int
905 linux_sys_setfsuid(p, v, retval)
906 struct proc *p;
907 void *v;
908 register_t *retval;
909 {
910 struct linux_sys_setfsuid_args /* {
911 syscallarg(uid_t) uid;
912 } */ *uap = v;
913 uid_t uid;
914
915 uid = SCARG(uap, uid);
916 if (p->p_cred->p_ruid != uid)
917 return sys_nosys(p, v, retval);
918 else
919 return (0);
920 }
921
922 /* XXX XXX XXX */
923 #ifndef alpha
924 int
925 linux_sys_getfsuid(p, v, retval)
926 struct proc *p;
927 void *v;
928 register_t *retval;
929 {
930 return sys_getuid(p, v, retval);
931 }
932 #endif
933
934 int
935 linux_sys___sysctl(p, v, retval)
936 struct proc *p;
937 void *v;
938 register_t *retval;
939 {
940 struct linux_sys___sysctl_args /* {
941 syscallarg(struct linux___sysctl *) lsp;
942 } */ *uap = v;
943 struct linux___sysctl ls;
944 struct sys___sysctl_args bsa;
945 int error;
946
947 if ((error = copyin(SCARG(uap, lsp), &ls, sizeof ls)))
948 return error;
949 SCARG(&bsa, name) = ls.name;
950 SCARG(&bsa, namelen) = ls.namelen;
951 SCARG(&bsa, old) = ls.old;
952 SCARG(&bsa, oldlenp) = ls.oldlenp;
953 SCARG(&bsa, new) = ls.new;
954 SCARG(&bsa, newlen) = ls.newlen;
955
956 return sys___sysctl(p, &bsa, retval);
957 }
958
959 int
960 linux_sys_setresuid(p, v, retval)
961 struct proc *p;
962 void *v;
963 register_t *retval;
964 {
965 struct linux_sys_setresuid_args /* {
966 syscallarg(uid_t) ruid;
967 syscallarg(uid_t) euid;
968 syscallarg(uid_t) suid;
969 } */ *uap = v;
970 struct pcred *pc = p->p_cred;
971 uid_t ruid, euid, suid;
972 int error;
973
974 ruid = SCARG(uap, ruid);
975 euid = SCARG(uap, euid);
976 suid = SCARG(uap, suid);
977
978 /*
979 * Note: These checks are a little different than the NetBSD
980 * setreuid(2) call performs. This precisely follows the
981 * behavior of the Linux kernel.
982 */
983 if (ruid != (uid_t)-1 &&
984 ruid != pc->p_ruid &&
985 ruid != pc->pc_ucred->cr_uid &&
986 ruid != pc->p_svuid &&
987 (error = suser(pc->pc_ucred, &p->p_acflag)))
988 return (error);
989
990 if (euid != (uid_t)-1 &&
991 euid != pc->p_ruid &&
992 euid != pc->pc_ucred->cr_uid &&
993 euid != pc->p_svuid &&
994 (error = suser(pc->pc_ucred, &p->p_acflag)))
995 return (error);
996
997 if (suid != (uid_t)-1 &&
998 suid != pc->p_ruid &&
999 suid != pc->pc_ucred->cr_uid &&
1000 suid != pc->p_svuid &&
1001 (error = suser(pc->pc_ucred, &p->p_acflag)))
1002 return (error);
1003
1004 /*
1005 * Now assign the new real, effective, and saved UIDs.
1006 * Note that Linux, unlike NetBSD in setreuid(2), does not
1007 * set the saved UID in this call unless the user specifies
1008 * it.
1009 */
1010 if (ruid != (uid_t)-1) {
1011 (void)chgproccnt(pc->p_ruid, -1);
1012 (void)chgproccnt(ruid, 1);
1013 pc->p_ruid = ruid;
1014 }
1015
1016 if (euid != (uid_t)-1) {
1017 pc->pc_ucred = crcopy(pc->pc_ucred);
1018 pc->pc_ucred->cr_uid = euid;
1019 }
1020
1021 if (suid != (uid_t)-1)
1022 pc->p_svuid = suid;
1023
1024 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1025 p->p_flag |= P_SUGID;
1026 return (0);
1027 }
1028
1029 int
1030 linux_sys_getresuid(p, v, retval)
1031 struct proc *p;
1032 void *v;
1033 register_t *retval;
1034 {
1035 struct linux_sys_getresuid_args /* {
1036 syscallarg(uid_t *) ruid;
1037 syscallarg(uid_t *) euid;
1038 syscallarg(uid_t *) suid;
1039 } */ *uap = v;
1040 struct pcred *pc = p->p_cred;
1041 int error;
1042
1043 /*
1044 * Linux copies these values out to userspace like so:
1045 *
1046 * 1. Copy out ruid.
1047 * 2. If that succeeds, copy out euid.
1048 * 3. If both of those succeed, copy out suid.
1049 */
1050 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1051 sizeof(uid_t))) != 0)
1052 return (error);
1053
1054 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1055 sizeof(uid_t))) != 0)
1056 return (error);
1057
1058 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1059 }
1060
1061 int
1062 linux_sys_ptrace(p, v, retval)
1063 struct proc *p;
1064 void *v;
1065 register_t *retval;
1066 {
1067 struct linux_sys_ptrace_args /* {
1068 i386, m68k: T=int
1069 alpha: T=long
1070 syscallarg(T) request;
1071 syscallarg(T) pid;
1072 syscallarg(T) addr;
1073 syscallarg(T) data;
1074 } */ *uap = v;
1075 int *ptr, request;
1076
1077 ptr = linux_ptrace_request_map;
1078 request = SCARG(uap, request);
1079 while (*ptr != -1)
1080 if (*ptr++ == request) {
1081 struct sys_ptrace_args pta;
1082 caddr_t sg;
1083
1084 sg = stackgap_init(p->p_emul);
1085
1086 SCARG(&pta, req) = *ptr;
1087 SCARG(&pta, pid) = SCARG(uap, pid);
1088 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1089 SCARG(&pta, data) = SCARG(uap, data);
1090
1091 return sys_ptrace(p, &pta, retval);
1092 }
1093 else
1094 ptr++;
1095
1096 return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
1097 }
1098
1099 int
1100 linux_sys_reboot(struct proc *p, void *v, register_t *retval)
1101 {
1102 struct linux_sys_reboot_args /* {
1103 syscallarg(int) magic1;
1104 syscallarg(int) magic2;
1105 syscallarg(int) cmd;
1106 syscallarg(void *) arg;
1107 } */ *uap = v;
1108 struct sys_reboot_args /* {
1109 syscallarg(int) opt;
1110 syscallarg(char *) bootstr;
1111 } */ sra;
1112 int error;
1113
1114 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1115 return(error);
1116
1117 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1118 return(EINVAL);
1119 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1120 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1121 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1122 return(EINVAL);
1123
1124 switch (SCARG(uap, cmd)) {
1125 case LINUX_REBOOT_CMD_RESTART:
1126 SCARG(&sra, opt) = RB_AUTOBOOT;
1127 break;
1128 case LINUX_REBOOT_CMD_HALT:
1129 SCARG(&sra, opt) = RB_HALT;
1130 break;
1131 case LINUX_REBOOT_CMD_POWER_OFF:
1132 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1133 break;
1134 case LINUX_REBOOT_CMD_RESTART2:
1135 /* Reboot with an argument. */
1136 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1137 SCARG(&sra, bootstr) = SCARG(uap, arg);
1138 break;
1139 case LINUX_REBOOT_CMD_CAD_ON:
1140 return(EINVAL); /* We don't implement ctrl-alt-delete */
1141 case LINUX_REBOOT_CMD_CAD_OFF:
1142 return(0);
1143 default:
1144 return(EINVAL);
1145 }
1146
1147 return(sys_reboot(p, &sra, retval));
1148 }
1149
1150 /*
1151 * This gets called for unsupported syscalls. The difference to sys_nosys()
1152 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1153 * This is the way Linux does it and glibc depends on this behaviour.
1154 */
1155 int
1156 linux_sys_nosys(p, v, retval)
1157 struct proc *p;
1158 void *v;
1159 register_t *retval;
1160 {
1161 return (ENOSYS);
1162 }
1163