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