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