linux_misc.c revision 1.100 1 /* $NetBSD: linux_misc.c,v 1.100 2002/02/18 22:24:18 christos 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/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: linux_misc.c,v 1.100 2002/02/18 22:24:18 christos Exp $");
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/namei.h>
72 #include <sys/proc.h>
73 #include <sys/dirent.h>
74 #include <sys/file.h>
75 #include <sys/stat.h>
76 #include <sys/filedesc.h>
77 #include <sys/ioctl.h>
78 #include <sys/kernel.h>
79 #include <sys/malloc.h>
80 #include <sys/mbuf.h>
81 #include <sys/mman.h>
82 #include <sys/mount.h>
83 #include <sys/reboot.h>
84 #include <sys/resource.h>
85 #include <sys/resourcevar.h>
86 #include <sys/signal.h>
87 #include <sys/signalvar.h>
88 #include <sys/socket.h>
89 #include <sys/time.h>
90 #include <sys/times.h>
91 #include <sys/vnode.h>
92 #include <sys/uio.h>
93 #include <sys/wait.h>
94 #include <sys/utsname.h>
95 #include <sys/unistd.h>
96 #include <sys/swap.h> /* for SWAP_ON */
97 #include <sys/sysctl.h> /* for KERN_DOMAINNAME */
98
99 #include <sys/ptrace.h>
100 #include <machine/ptrace.h>
101
102 #include <sys/syscallargs.h>
103
104 #include <compat/linux/common/linux_types.h>
105 #include <compat/linux/common/linux_signal.h>
106
107 #include <compat/linux/linux_syscallargs.h>
108
109 #include <compat/linux/common/linux_fcntl.h>
110 #include <compat/linux/common/linux_mmap.h>
111 #include <compat/linux/common/linux_dirent.h>
112 #include <compat/linux/common/linux_util.h>
113 #include <compat/linux/common/linux_misc.h>
114 #include <compat/linux/common/linux_ptrace.h>
115 #include <compat/linux/common/linux_reboot.h>
116 #include <compat/linux/common/linux_emuldata.h>
117
118 const int linux_ptrace_request_map[] = {
119 LINUX_PTRACE_TRACEME, PT_TRACE_ME,
120 LINUX_PTRACE_PEEKTEXT, PT_READ_I,
121 LINUX_PTRACE_PEEKDATA, PT_READ_D,
122 LINUX_PTRACE_POKETEXT, PT_WRITE_I,
123 LINUX_PTRACE_POKEDATA, PT_WRITE_D,
124 LINUX_PTRACE_CONT, PT_CONTINUE,
125 LINUX_PTRACE_KILL, PT_KILL,
126 LINUX_PTRACE_ATTACH, PT_ATTACH,
127 LINUX_PTRACE_DETACH, PT_DETACH,
128 #ifdef PT_STEP
129 LINUX_PTRACE_SINGLESTEP, PT_STEP,
130 #endif
131 -1
132 };
133
134 /* Local linux_misc.c functions: */
135 static void bsd_to_linux_statfs __P((struct statfs *, struct linux_statfs *));
136 static int linux_to_bsd_limit __P((int));
137
138 /*
139 * The information on a terminated (or stopped) process needs
140 * to be converted in order for Linux binaries to get a valid signal
141 * number out of it.
142 */
143 void
144 bsd_to_linux_wstat(st)
145 int *st;
146 {
147
148 int sig;
149
150 if (WIFSIGNALED(*st)) {
151 sig = WTERMSIG(*st);
152 if (sig >= 0 && sig < NSIG)
153 *st= (*st& ~0177) | native_to_linux_sig[sig];
154 } else if (WIFSTOPPED(*st)) {
155 sig = WSTOPSIG(*st);
156 if (sig >= 0 && sig < NSIG)
157 *st = (*st & ~0xff00) | (native_to_linux_sig[sig] << 8);
158 }
159 }
160
161 /*
162 * This is very much the same as waitpid()
163 */
164 int
165 linux_sys_wait4(p, v, retval)
166 struct proc *p;
167 void *v;
168 register_t *retval;
169 {
170 struct linux_sys_wait4_args /* {
171 syscallarg(int) pid;
172 syscallarg(int *) status;
173 syscallarg(int) options;
174 syscallarg(struct rusage *) rusage;
175 } */ *uap = v;
176 struct sys_wait4_args w4a;
177 int error, *status, tstat, options, linux_options;
178 caddr_t sg;
179
180 if (SCARG(uap, status) != NULL) {
181 sg = stackgap_init(p->p_emul);
182 status = (int *) stackgap_alloc(&sg, sizeof *status);
183 } else
184 status = NULL;
185
186 linux_options = SCARG(uap, options);
187 options = 0;
188 if (linux_options &
189 ~(LINUX_WAIT4_WNOHANG|LINUX_WAIT4_WUNTRACED|LINUX_WAIT4_WALL|
190 LINUX_WAIT4_WCLONE))
191 return (EINVAL);
192
193 if (linux_options & LINUX_WAIT4_WNOHANG)
194 options |= WNOHANG;
195 if (linux_options & LINUX_WAIT4_WUNTRACED)
196 options |= WUNTRACED;
197 if (linux_options & LINUX_WAIT4_WALL)
198 options |= WALLSIG;
199 if (linux_options & LINUX_WAIT4_WCLONE)
200 options |= WALTSIG;
201
202 SCARG(&w4a, pid) = SCARG(uap, pid);
203 SCARG(&w4a, status) = status;
204 SCARG(&w4a, options) = options;
205 SCARG(&w4a, rusage) = SCARG(uap, rusage);
206
207 if ((error = sys_wait4(p, &w4a, retval)))
208 return error;
209
210 sigdelset(&p->p_sigctx.ps_siglist, SIGCHLD);
211
212 if (status != NULL) {
213 if ((error = copyin(status, &tstat, sizeof tstat)))
214 return error;
215
216 bsd_to_linux_wstat(&tstat);
217 return copyout(&tstat, SCARG(uap, status), sizeof tstat);
218 }
219
220 return 0;
221 }
222
223 /*
224 * Linux brk(2). The check if the new address is >= the old one is
225 * done in the kernel in Linux. NetBSD does it in the library.
226 */
227 int
228 linux_sys_brk(p, v, retval)
229 struct proc *p;
230 void *v;
231 register_t *retval;
232 {
233 struct linux_sys_brk_args /* {
234 syscallarg(char *) nsize;
235 } */ *uap = v;
236 char *nbrk = SCARG(uap, nsize);
237 struct sys_obreak_args oba;
238 struct vmspace *vm = p->p_vmspace;
239 struct linux_emuldata *ed = (struct linux_emuldata*)p->p_emuldata;
240
241 SCARG(&oba, nsize) = nbrk;
242
243 if ((caddr_t) nbrk > vm->vm_daddr && sys_obreak(p, &oba, retval) == 0)
244 ed->p_break = (char*)nbrk;
245 else
246 nbrk = ed->p_break;
247
248 retval[0] = (register_t)nbrk;
249
250 return 0;
251 }
252
253 /*
254 * Convert BSD statfs structure to Linux statfs structure.
255 * The Linux structure has less fields, and it also wants
256 * the length of a name in a dir entry in a field, which
257 * we fake (probably the wrong way).
258 */
259 static void
260 bsd_to_linux_statfs(bsp, lsp)
261 struct statfs *bsp;
262 struct linux_statfs *lsp;
263 {
264
265 lsp->l_ftype = bsp->f_type;
266 lsp->l_fbsize = bsp->f_bsize;
267 lsp->l_fblocks = bsp->f_blocks;
268 lsp->l_fbfree = bsp->f_bfree;
269 lsp->l_fbavail = bsp->f_bavail;
270 lsp->l_ffiles = bsp->f_files;
271 lsp->l_fffree = bsp->f_ffree;
272 lsp->l_ffsid.val[0] = bsp->f_fsid.val[0];
273 lsp->l_ffsid.val[1] = bsp->f_fsid.val[1];
274 lsp->l_fnamelen = MAXNAMLEN; /* XXX */
275 }
276
277 /*
278 * Implement the fs stat functions. Straightforward.
279 */
280 int
281 linux_sys_statfs(p, v, retval)
282 struct proc *p;
283 void *v;
284 register_t *retval;
285 {
286 struct linux_sys_statfs_args /* {
287 syscallarg(const char *) path;
288 syscallarg(struct linux_statfs *) sp;
289 } */ *uap = v;
290 struct statfs btmp, *bsp;
291 struct linux_statfs ltmp;
292 struct sys_statfs_args bsa;
293 caddr_t sg;
294 int error;
295
296 sg = stackgap_init(p->p_emul);
297 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
298
299 CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
300
301 SCARG(&bsa, path) = SCARG(uap, path);
302 SCARG(&bsa, buf) = bsp;
303
304 if ((error = sys_statfs(p, &bsa, retval)))
305 return error;
306
307 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
308 return error;
309
310 bsd_to_linux_statfs(&btmp, <mp);
311
312 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
313 }
314
315 int
316 linux_sys_fstatfs(p, v, retval)
317 struct proc *p;
318 void *v;
319 register_t *retval;
320 {
321 struct linux_sys_fstatfs_args /* {
322 syscallarg(int) fd;
323 syscallarg(struct linux_statfs *) sp;
324 } */ *uap = v;
325 struct statfs btmp, *bsp;
326 struct linux_statfs ltmp;
327 struct sys_fstatfs_args bsa;
328 caddr_t sg;
329 int error;
330
331 sg = stackgap_init(p->p_emul);
332 bsp = (struct statfs *) stackgap_alloc(&sg, sizeof (struct statfs));
333
334 SCARG(&bsa, fd) = SCARG(uap, fd);
335 SCARG(&bsa, buf) = bsp;
336
337 if ((error = sys_fstatfs(p, &bsa, retval)))
338 return error;
339
340 if ((error = copyin((caddr_t) bsp, (caddr_t) &btmp, sizeof btmp)))
341 return error;
342
343 bsd_to_linux_statfs(&btmp, <mp);
344
345 return copyout((caddr_t) <mp, (caddr_t) SCARG(uap, sp), sizeof ltmp);
346 }
347
348 /*
349 * NOTE: DO NOT CHANGE THIS
350 * Linux makes assumptions about specific features being present with
351 * more recent kernels. Specifically, LinuxThreads use RT queued
352 * signals if the kernel release is bigger. Since we don't support them
353 * yet, the version needs to stay this way until we'd have the RT queued
354 * signals implemented.
355 */
356 char linux_sysname[] = "Linux";
357 char linux_release[] = "2.0.38";
358 char linux_version[] = "#0 Sun Apr 1 11:11:11 MET 2000";
359
360 /*
361 * uname(). Just copy the info from the various strings stored in the
362 * kernel, and put it in the Linux utsname structure. That structure
363 * is almost the same as the NetBSD one, only it has fields 65 characters
364 * long, and an extra domainname field.
365 */
366 int
367 linux_sys_uname(p, v, retval)
368 struct proc *p;
369 void *v;
370 register_t *retval;
371 {
372 struct linux_sys_uname_args /* {
373 syscallarg(struct linux_utsname *) up;
374 } */ *uap = v;
375 struct linux_utsname luts;
376
377 strncpy(luts.l_sysname, linux_sysname, sizeof(luts.l_sysname));
378 strncpy(luts.l_nodename, hostname, sizeof(luts.l_nodename));
379 strncpy(luts.l_release, linux_release, sizeof(luts.l_release));
380 strncpy(luts.l_version, linux_version, sizeof(luts.l_version));
381 strncpy(luts.l_machine, machine, sizeof(luts.l_machine));
382 strncpy(luts.l_domainname, domainname, sizeof(luts.l_domainname));
383
384 return copyout(&luts, SCARG(uap, up), sizeof(luts));
385 }
386
387 /* Used directly on: alpha, mips, ppc, sparc, sparc64 */
388 /* Used indirectly on: arm, i386, m68k */
389
390 /*
391 * New type Linux mmap call.
392 * Only called directly on machines with >= 6 free regs.
393 */
394 int
395 linux_sys_mmap(p, v, retval)
396 struct proc *p;
397 void *v;
398 register_t *retval;
399 {
400 struct linux_sys_mmap_args /* {
401 syscallarg(unsigned long) addr;
402 syscallarg(size_t) len;
403 syscallarg(int) prot;
404 syscallarg(int) flags;
405 syscallarg(int) fd;
406 syscallarg(linux_off_t) offset;
407 } */ *uap = v;
408 struct sys_mmap_args cma;
409 int flags;
410
411 flags = 0;
412 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
413 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
414 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
415 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
416 /* XXX XAX ERH: Any other flags here? There are more defined... */
417
418 SCARG(&cma,addr) = (void *)SCARG(uap, addr);
419 SCARG(&cma,len) = SCARG(uap, len);
420 SCARG(&cma,prot) = SCARG(uap, prot);
421 if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
422 SCARG(&cma,prot) |= VM_PROT_READ;
423 SCARG(&cma,flags) = flags;
424 SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
425 SCARG(&cma,pad) = 0;
426 SCARG(&cma,pos) = (off_t)SCARG(uap, offset);
427
428 return sys_mmap(p, &cma, retval);
429 }
430
431 /*
432 * Newer type Linux mmap call.
433 */
434 int
435 linux_sys_mmap2(p, v, retval)
436 struct proc *p;
437 void *v;
438 register_t *retval;
439 {
440 struct linux_sys_mmap2_args /* {
441 syscallarg(void *) addr;
442 syscallarg(size_t) len;
443 syscallarg(int) prot;
444 syscallarg(int) flags;
445 syscallarg(int) fd;
446 syscallarg(off_t) offset;
447 } */ *uap = v;
448 struct sys_mmap_args cma;
449 int flags;
450
451 flags = 0;
452 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_SHARED, MAP_SHARED);
453 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_PRIVATE, MAP_PRIVATE);
454 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_FIXED, MAP_FIXED);
455 flags |= cvtto_bsd_mask(SCARG(uap,flags), LINUX_MAP_ANON, MAP_ANON);
456 /* XXX XAX ERH: Any other flags here? There are more defined... */
457
458 SCARG(&cma,addr) = (void *)SCARG(uap, addr);
459 SCARG(&cma,len) = SCARG(uap, len);
460 SCARG(&cma,prot) = SCARG(uap, prot);
461 if (SCARG(&cma,prot) & VM_PROT_WRITE) /* XXX */
462 SCARG(&cma,prot) |= VM_PROT_READ;
463 SCARG(&cma,flags) = flags;
464 SCARG(&cma,fd) = flags & MAP_ANON ? -1 : SCARG(uap, fd);
465 SCARG(&cma,pos) = (off_t)SCARG(uap, offset);
466
467 return sys_mmap(p, &cma, retval);
468 }
469
470 int
471 linux_sys_mremap(p, v, retval)
472 struct proc *p;
473 void *v;
474 register_t *retval;
475 {
476 struct linux_sys_mremap_args /* {
477 syscallarg(void *) old_address;
478 syscallarg(size_t) old_size;
479 syscallarg(size_t) new_size;
480 syscallarg(u_long) flags;
481 } */ *uap = v;
482 struct sys_munmap_args mua;
483 size_t old_size, new_size;
484 int error;
485
486 old_size = round_page(SCARG(uap, old_size));
487 new_size = round_page(SCARG(uap, new_size));
488
489 /*
490 * Growing mapped region.
491 */
492 if (new_size > old_size) {
493 /*
494 * XXX Implement me. What we probably want to do is
495 * XXX dig out the guts of the old mapping, mmap that
496 * XXX object again with the new size, then munmap
497 * XXX the old mapping.
498 */
499 *retval = 0;
500 return (ENOMEM);
501 }
502
503 /*
504 * Shrinking mapped region.
505 */
506 if (new_size < old_size) {
507 SCARG(&mua, addr) = (caddr_t)SCARG(uap, old_address) +
508 new_size;
509 SCARG(&mua, len) = old_size - new_size;
510 error = sys_munmap(p, &mua, retval);
511 *retval = error ? 0 : (register_t)SCARG(uap, old_address);
512 return (error);
513 }
514
515 /*
516 * No change.
517 */
518 *retval = (register_t)SCARG(uap, old_address);
519 return (0);
520 }
521
522 int
523 linux_sys_msync(p, v, retval)
524 struct proc *p;
525 void *v;
526 register_t *retval;
527 {
528 struct linux_sys_msync_args /* {
529 syscallarg(caddr_t) addr;
530 syscallarg(int) len;
531 syscallarg(int) fl;
532 } */ *uap = v;
533
534 struct sys___msync13_args bma;
535
536 /* flags are ignored */
537 SCARG(&bma, addr) = SCARG(uap, addr);
538 SCARG(&bma, len) = SCARG(uap, len);
539 SCARG(&bma, flags) = SCARG(uap, fl);
540
541 return sys___msync13(p, &bma, retval);
542 }
543
544 /*
545 * This code is partly stolen from src/lib/libc/compat-43/times.c
546 * XXX - CLK_TCK isn't declared in /sys, just in <time.h>, done here
547 */
548
549 #define CLK_TCK 100
550 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
551
552 int
553 linux_sys_times(p, v, retval)
554 struct proc *p;
555 void *v;
556 register_t *retval;
557 {
558 struct linux_sys_times_args /* {
559 syscallarg(struct times *) tms;
560 } */ *uap = v;
561 struct timeval t;
562 struct linux_tms ltms;
563 struct rusage ru;
564 int error, s;
565
566 calcru(p, &ru.ru_utime, &ru.ru_stime, NULL);
567 ltms.ltms_utime = CONVTCK(ru.ru_utime);
568 ltms.ltms_stime = CONVTCK(ru.ru_stime);
569
570 ltms.ltms_cutime = CONVTCK(p->p_stats->p_cru.ru_utime);
571 ltms.ltms_cstime = CONVTCK(p->p_stats->p_cru.ru_stime);
572
573 if ((error = copyout(<ms, SCARG(uap, tms), sizeof ltms)))
574 return error;
575
576 s = splclock();
577 timersub(&time, &boottime, &t);
578 splx(s);
579
580 retval[0] = ((linux_clock_t)(CONVTCK(t)));
581 return 0;
582 }
583
584 /*
585 * Linux 'readdir' call. This code is mostly taken from the
586 * SunOS getdents call (see compat/sunos/sunos_misc.c), though
587 * an attempt has been made to keep it a little cleaner (failing
588 * miserably, because of the cruft needed if count 1 is passed).
589 *
590 * The d_off field should contain the offset of the next valid entry,
591 * but in Linux it has the offset of the entry itself. We emulate
592 * that bug here.
593 *
594 * Read in BSD-style entries, convert them, and copy them out.
595 *
596 * Note that this doesn't handle union-mounted filesystems.
597 */
598 int
599 linux_sys_getdents(p, v, retval)
600 struct proc *p;
601 void *v;
602 register_t *retval;
603 {
604 struct linux_sys_getdents_args /* {
605 syscallarg(int) fd;
606 syscallarg(struct linux_dirent *) dent;
607 syscallarg(unsigned int) count;
608 } */ *uap = v;
609 struct dirent *bdp;
610 struct vnode *vp;
611 caddr_t inp, buf; /* BSD-format */
612 int len, reclen; /* BSD-format */
613 caddr_t outp; /* Linux-format */
614 int resid, linux_reclen = 0; /* Linux-format */
615 struct file *fp;
616 struct uio auio;
617 struct iovec aiov;
618 struct linux_dirent idb;
619 off_t off; /* true file offset */
620 int buflen, error, eofflag, nbytes, oldcall;
621 struct vattr va;
622 off_t *cookiebuf = NULL, *cookie;
623 int ncookies;
624
625 /* getvnode() will use the descriptor for us */
626 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
627 return (error);
628
629 if ((fp->f_flag & FREAD) == 0) {
630 error = EBADF;
631 goto out1;
632 }
633
634 vp = (struct vnode *)fp->f_data;
635 if (vp->v_type != VDIR) {
636 error = EINVAL;
637 goto out1;
638 }
639
640 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
641 goto out1;
642
643 nbytes = SCARG(uap, count);
644 if (nbytes == 1) { /* emulating old, broken behaviour */
645 nbytes = sizeof (struct linux_dirent);
646 buflen = max(va.va_blocksize, nbytes);
647 oldcall = 1;
648 } else {
649 buflen = min(MAXBSIZE, nbytes);
650 if (buflen < va.va_blocksize)
651 buflen = va.va_blocksize;
652 oldcall = 0;
653 }
654 buf = malloc(buflen, M_TEMP, M_WAITOK);
655
656 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
657 off = fp->f_offset;
658 again:
659 aiov.iov_base = buf;
660 aiov.iov_len = buflen;
661 auio.uio_iov = &aiov;
662 auio.uio_iovcnt = 1;
663 auio.uio_rw = UIO_READ;
664 auio.uio_segflg = UIO_SYSSPACE;
665 auio.uio_procp = p;
666 auio.uio_resid = buflen;
667 auio.uio_offset = off;
668 /*
669 * First we read into the malloc'ed buffer, then
670 * we massage it into user space, one record at a time.
671 */
672 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
673 &ncookies);
674 if (error)
675 goto out;
676
677 inp = buf;
678 outp = (caddr_t)SCARG(uap, dent);
679 resid = nbytes;
680 if ((len = buflen - auio.uio_resid) == 0)
681 goto eof;
682
683 for (cookie = cookiebuf; len > 0; len -= reclen) {
684 bdp = (struct dirent *)inp;
685 reclen = bdp->d_reclen;
686 if (reclen & 3)
687 panic("linux_readdir");
688 if (bdp->d_fileno == 0) {
689 inp += reclen; /* it is a hole; squish it out */
690 off = *cookie++;
691 continue;
692 }
693 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
694 if (reclen > len || resid < linux_reclen) {
695 /* entry too big for buffer, so just stop */
696 outp++;
697 break;
698 }
699 /*
700 * Massage in place to make a Linux-shaped dirent (otherwise
701 * we have to worry about touching user memory outside of
702 * the copyout() call).
703 */
704 idb.d_ino = (linux_ino_t)bdp->d_fileno;
705 /*
706 * The old readdir() call misuses the offset and reclen fields.
707 */
708 if (oldcall) {
709 idb.d_off = (linux_off_t)linux_reclen;
710 idb.d_reclen = (u_short)bdp->d_namlen;
711 } else {
712 if (sizeof (linux_off_t) < 4 && (off >> 32) != 0) {
713 compat_offseterr(vp, "linux_getdents");
714 error = EINVAL;
715 goto out;
716 }
717 idb.d_off = (linux_off_t)off;
718 idb.d_reclen = (u_short)linux_reclen;
719 }
720 strcpy(idb.d_name, bdp->d_name);
721 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
722 goto out;
723 /* advance past this real entry */
724 inp += reclen;
725 off = *cookie++; /* each entry points to itself */
726 /* advance output past Linux-shaped entry */
727 outp += linux_reclen;
728 resid -= linux_reclen;
729 if (oldcall)
730 break;
731 }
732
733 /* if we squished out the whole block, try again */
734 if (outp == (caddr_t)SCARG(uap, dent))
735 goto again;
736 fp->f_offset = off; /* update the vnode offset */
737
738 if (oldcall)
739 nbytes = resid + linux_reclen;
740
741 eof:
742 *retval = nbytes - resid;
743 out:
744 VOP_UNLOCK(vp, 0);
745 if (cookiebuf)
746 free(cookiebuf, M_TEMP);
747 free(buf, M_TEMP);
748 out1:
749 FILE_UNUSE(fp, p);
750 return error;
751 }
752
753 /*
754 * Even when just using registers to pass arguments to syscalls you can
755 * have 5 of them on the i386. So this newer version of select() does
756 * this.
757 */
758 int
759 linux_sys_select(p, v, retval)
760 struct proc *p;
761 void *v;
762 register_t *retval;
763 {
764 struct linux_sys_select_args /* {
765 syscallarg(int) nfds;
766 syscallarg(fd_set *) readfds;
767 syscallarg(fd_set *) writefds;
768 syscallarg(fd_set *) exceptfds;
769 syscallarg(struct timeval *) timeout;
770 } */ *uap = v;
771
772 return linux_select1(p, retval, SCARG(uap, nfds), SCARG(uap, readfds),
773 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
774 }
775
776 /*
777 * Common code for the old and new versions of select(). A couple of
778 * things are important:
779 * 1) return the amount of time left in the 'timeout' parameter
780 * 2) select never returns ERESTART on Linux, always return EINTR
781 */
782 int
783 linux_select1(p, retval, nfds, readfds, writefds, exceptfds, timeout)
784 struct proc *p;
785 register_t *retval;
786 int nfds;
787 fd_set *readfds, *writefds, *exceptfds;
788 struct timeval *timeout;
789 {
790 struct sys_select_args bsa;
791 struct timeval tv0, tv1, utv, *tvp;
792 caddr_t sg;
793 int error;
794
795 SCARG(&bsa, nd) = nfds;
796 SCARG(&bsa, in) = readfds;
797 SCARG(&bsa, ou) = writefds;
798 SCARG(&bsa, ex) = exceptfds;
799 SCARG(&bsa, tv) = timeout;
800
801 /*
802 * Store current time for computation of the amount of
803 * time left.
804 */
805 if (timeout) {
806 if ((error = copyin(timeout, &utv, sizeof(utv))))
807 return error;
808 if (itimerfix(&utv)) {
809 /*
810 * The timeval was invalid. Convert it to something
811 * valid that will act as it does under Linux.
812 */
813 sg = stackgap_init(p->p_emul);
814 tvp = stackgap_alloc(&sg, sizeof(utv));
815 utv.tv_sec += utv.tv_usec / 1000000;
816 utv.tv_usec %= 1000000;
817 if (utv.tv_usec < 0) {
818 utv.tv_sec -= 1;
819 utv.tv_usec += 1000000;
820 }
821 if (utv.tv_sec < 0)
822 timerclear(&utv);
823 if ((error = copyout(&utv, tvp, sizeof(utv))))
824 return error;
825 SCARG(&bsa, tv) = tvp;
826 }
827 microtime(&tv0);
828 }
829
830 error = sys_select(p, &bsa, retval);
831 if (error) {
832 /*
833 * See fs/select.c in the Linux kernel. Without this,
834 * Maelstrom doesn't work.
835 */
836 if (error == ERESTART)
837 error = EINTR;
838 return error;
839 }
840
841 if (timeout) {
842 if (*retval) {
843 /*
844 * Compute how much time was left of the timeout,
845 * by subtracting the current time and the time
846 * before we started the call, and subtracting
847 * that result from the user-supplied value.
848 */
849 microtime(&tv1);
850 timersub(&tv1, &tv0, &tv1);
851 timersub(&utv, &tv1, &utv);
852 if (utv.tv_sec < 0)
853 timerclear(&utv);
854 } else
855 timerclear(&utv);
856 if ((error = copyout(&utv, timeout, sizeof(utv))))
857 return error;
858 }
859
860 return 0;
861 }
862
863 /*
864 * Get the process group of a certain process. Look it up
865 * and return the value.
866 */
867 int
868 linux_sys_getpgid(p, v, retval)
869 struct proc *p;
870 void *v;
871 register_t *retval;
872 {
873 struct linux_sys_getpgid_args /* {
874 syscallarg(int) pid;
875 } */ *uap = v;
876 struct proc *targp;
877
878 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
879 if ((targp = pfind(SCARG(uap, pid))) == 0)
880 return ESRCH;
881 }
882 else
883 targp = p;
884
885 retval[0] = targp->p_pgid;
886 return 0;
887 }
888
889 /*
890 * Set the 'personality' (emulation mode) for the current process. Only
891 * accept the Linux personality here (0). This call is needed because
892 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
893 * ELF binaries run in Linux mode, not SVR4 mode.
894 */
895 int
896 linux_sys_personality(p, v, retval)
897 struct proc *p;
898 void *v;
899 register_t *retval;
900 {
901 struct linux_sys_personality_args /* {
902 syscallarg(int) per;
903 } */ *uap = v;
904
905 if (SCARG(uap, per) != 0)
906 return EINVAL;
907 retval[0] = 0;
908 return 0;
909 }
910
911 #if defined(__i386__) || defined(__m68k__)
912 /*
913 * The calls are here because of type conversions.
914 */
915 int
916 linux_sys_setreuid16(p, v, retval)
917 struct proc *p;
918 void *v;
919 register_t *retval;
920 {
921 struct linux_sys_setreuid16_args /* {
922 syscallarg(int) ruid;
923 syscallarg(int) euid;
924 } */ *uap = v;
925 struct sys_setreuid_args bsa;
926
927 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
928 (uid_t)-1 : SCARG(uap, ruid);
929 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
930 (uid_t)-1 : SCARG(uap, euid);
931
932 return sys_setreuid(p, &bsa, retval);
933 }
934
935 int
936 linux_sys_setregid16(p, v, retval)
937 struct proc *p;
938 void *v;
939 register_t *retval;
940 {
941 struct linux_sys_setregid16_args /* {
942 syscallarg(int) rgid;
943 syscallarg(int) egid;
944 } */ *uap = v;
945 struct sys_setregid_args bsa;
946
947 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
948 (uid_t)-1 : SCARG(uap, rgid);
949 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
950 (uid_t)-1 : SCARG(uap, egid);
951
952 return sys_setregid(p, &bsa, retval);
953 }
954
955 int
956 linux_sys_setresuid16(p, v, retval)
957 struct proc *p;
958 void *v;
959 register_t *retval;
960 {
961 struct linux_sys_setresuid16_args /* {
962 syscallarg(uid_t) ruid;
963 syscallarg(uid_t) euid;
964 syscallarg(uid_t) suid;
965 } */ *uap = v;
966 struct linux_sys_setresuid16_args lsa;
967
968 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
969 (uid_t)-1 : SCARG(uap, ruid);
970 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
971 (uid_t)-1 : SCARG(uap, euid);
972 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
973 (uid_t)-1 : SCARG(uap, suid);
974
975 return linux_sys_setresuid(p, &lsa, retval);
976 }
977
978 int
979 linux_sys_setresgid16(p, v, retval)
980 struct proc *p;
981 void *v;
982 register_t *retval;
983 {
984 struct linux_sys_setresgid16_args /* {
985 syscallarg(gid_t) rgid;
986 syscallarg(gid_t) egid;
987 syscallarg(gid_t) sgid;
988 } */ *uap = v;
989 struct linux_sys_setresgid16_args lsa;
990
991 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
992 (gid_t)-1 : SCARG(uap, rgid);
993 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
994 (gid_t)-1 : SCARG(uap, egid);
995 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
996 (gid_t)-1 : SCARG(uap, sgid);
997
998 return linux_sys_setresgid(p, &lsa, retval);
999 }
1000
1001 int
1002 linux_sys_getgroups16(p, v, retval)
1003 struct proc *p;
1004 void *v;
1005 register_t *retval;
1006 {
1007 struct linux_sys_getgroups16_args /* {
1008 syscallarg(int) gidsetsize;
1009 syscallarg(linux_gid_t *) gidset;
1010 } */ *uap = v;
1011 caddr_t sg;
1012 int n, error, i;
1013 struct sys_getgroups_args bsa;
1014 gid_t *bset, *kbset;
1015 linux_gid_t *lset;
1016 struct pcred *pc = p->p_cred;
1017
1018 n = SCARG(uap, gidsetsize);
1019 if (n < 0)
1020 return EINVAL;
1021 error = 0;
1022 bset = kbset = NULL;
1023 lset = NULL;
1024 if (n > 0) {
1025 n = min(pc->pc_ucred->cr_ngroups, n);
1026 sg = stackgap_init(p->p_emul);
1027 bset = stackgap_alloc(&sg, n * sizeof (gid_t));
1028 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1029 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1030 if (bset == NULL || kbset == NULL || lset == NULL)
1031 return ENOMEM;
1032 SCARG(&bsa, gidsetsize) = n;
1033 SCARG(&bsa, gidset) = bset;
1034 error = sys_getgroups(p, &bsa, retval);
1035 if (error != 0)
1036 goto out;
1037 error = copyin(bset, kbset, n * sizeof (gid_t));
1038 if (error != 0)
1039 goto out;
1040 for (i = 0; i < n; i++)
1041 lset[i] = (linux_gid_t)kbset[i];
1042 error = copyout(lset, SCARG(uap, gidset),
1043 n * sizeof (linux_gid_t));
1044 } else
1045 *retval = pc->pc_ucred->cr_ngroups;
1046 out:
1047 if (kbset != NULL)
1048 free(kbset, M_TEMP);
1049 if (lset != NULL)
1050 free(lset, M_TEMP);
1051 return error;
1052 }
1053
1054 int
1055 linux_sys_setgroups16(p, v, retval)
1056 struct proc *p;
1057 void *v;
1058 register_t *retval;
1059 {
1060 struct linux_sys_setgroups16_args /* {
1061 syscallarg(int) gidsetsize;
1062 syscallarg(linux_gid_t *) gidset;
1063 } */ *uap = v;
1064 caddr_t sg;
1065 int n;
1066 int error, i;
1067 struct sys_setgroups_args bsa;
1068 gid_t *bset, *kbset;
1069 linux_gid_t *lset;
1070
1071 n = SCARG(uap, gidsetsize);
1072 if (n < 0 || n > NGROUPS)
1073 return EINVAL;
1074 sg = stackgap_init(p->p_emul);
1075 bset = stackgap_alloc(&sg, n * sizeof (gid_t));
1076 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1077 kbset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1078 if (lset == NULL || bset == NULL)
1079 return ENOMEM;
1080 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1081 if (error != 0)
1082 goto out;
1083 for (i = 0; i < n; i++)
1084 kbset[i] = (gid_t)lset[i];
1085 error = copyout(kbset, bset, n * sizeof (gid_t));
1086 if (error != 0)
1087 goto out;
1088 SCARG(&bsa, gidsetsize) = n;
1089 SCARG(&bsa, gidset) = bset;
1090 error = sys_setgroups(p, &bsa, retval);
1091
1092 out:
1093 if (lset != NULL)
1094 free(lset, M_TEMP);
1095 if (kbset != NULL)
1096 free(kbset, M_TEMP);
1097
1098 return error;
1099 }
1100
1101 #endif /* __i386__ || __m68k__ */
1102
1103 /*
1104 * We have nonexistent fsuid equal to uid.
1105 * If modification is requested, refuse.
1106 */
1107 int
1108 linux_sys_setfsuid(p, v, retval)
1109 struct proc *p;
1110 void *v;
1111 register_t *retval;
1112 {
1113 struct linux_sys_setfsuid_args /* {
1114 syscallarg(uid_t) uid;
1115 } */ *uap = v;
1116 uid_t uid;
1117
1118 uid = SCARG(uap, uid);
1119 if (p->p_cred->p_ruid != uid)
1120 return sys_nosys(p, v, retval);
1121 else
1122 return (0);
1123 }
1124
1125 /* XXX XXX XXX */
1126 #ifndef alpha
1127 int
1128 linux_sys_getfsuid(p, v, retval)
1129 struct proc *p;
1130 void *v;
1131 register_t *retval;
1132 {
1133 return sys_getuid(p, v, retval);
1134 }
1135 #endif
1136
1137 int
1138 linux_sys_setresuid(p, v, retval)
1139 struct proc *p;
1140 void *v;
1141 register_t *retval;
1142 {
1143 struct linux_sys_setresuid_args /* {
1144 syscallarg(uid_t) ruid;
1145 syscallarg(uid_t) euid;
1146 syscallarg(uid_t) suid;
1147 } */ *uap = v;
1148 struct pcred *pc = p->p_cred;
1149 uid_t ruid, euid, suid;
1150 int error;
1151
1152 ruid = SCARG(uap, ruid);
1153 euid = SCARG(uap, euid);
1154 suid = SCARG(uap, suid);
1155
1156 /*
1157 * Note: These checks are a little different than the NetBSD
1158 * setreuid(2) call performs. This precisely follows the
1159 * behavior of the Linux kernel.
1160 */
1161 if (ruid != (uid_t)-1 &&
1162 ruid != pc->p_ruid &&
1163 ruid != pc->pc_ucred->cr_uid &&
1164 ruid != pc->p_svuid &&
1165 (error = suser(pc->pc_ucred, &p->p_acflag)))
1166 return (error);
1167
1168 if (euid != (uid_t)-1 &&
1169 euid != pc->p_ruid &&
1170 euid != pc->pc_ucred->cr_uid &&
1171 euid != pc->p_svuid &&
1172 (error = suser(pc->pc_ucred, &p->p_acflag)))
1173 return (error);
1174
1175 if (suid != (uid_t)-1 &&
1176 suid != pc->p_ruid &&
1177 suid != pc->pc_ucred->cr_uid &&
1178 suid != pc->p_svuid &&
1179 (error = suser(pc->pc_ucred, &p->p_acflag)))
1180 return (error);
1181
1182 /*
1183 * Now assign the new real, effective, and saved UIDs.
1184 * Note that Linux, unlike NetBSD in setreuid(2), does not
1185 * set the saved UID in this call unless the user specifies
1186 * it.
1187 */
1188 if (ruid != (uid_t)-1) {
1189 (void)chgproccnt(pc->p_ruid, -1);
1190 (void)chgproccnt(ruid, 1);
1191 pc->p_ruid = ruid;
1192 }
1193
1194 if (euid != (uid_t)-1) {
1195 pc->pc_ucred = crcopy(pc->pc_ucred);
1196 pc->pc_ucred->cr_uid = euid;
1197 }
1198
1199 if (suid != (uid_t)-1)
1200 pc->p_svuid = suid;
1201
1202 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1203 p->p_flag |= P_SUGID;
1204 return (0);
1205 }
1206
1207 int
1208 linux_sys_getresuid(p, v, retval)
1209 struct proc *p;
1210 void *v;
1211 register_t *retval;
1212 {
1213 struct linux_sys_getresuid_args /* {
1214 syscallarg(uid_t *) ruid;
1215 syscallarg(uid_t *) euid;
1216 syscallarg(uid_t *) suid;
1217 } */ *uap = v;
1218 struct pcred *pc = p->p_cred;
1219 int error;
1220
1221 /*
1222 * Linux copies these values out to userspace like so:
1223 *
1224 * 1. Copy out ruid.
1225 * 2. If that succeeds, copy out euid.
1226 * 3. If both of those succeed, copy out suid.
1227 */
1228 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1229 sizeof(uid_t))) != 0)
1230 return (error);
1231
1232 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1233 sizeof(uid_t))) != 0)
1234 return (error);
1235
1236 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1237 }
1238
1239 int
1240 linux_sys_ptrace(p, v, retval)
1241 struct proc *p;
1242 void *v;
1243 register_t *retval;
1244 {
1245 struct linux_sys_ptrace_args /* {
1246 i386, m68k, powerpc: T=int
1247 alpha: T=long
1248 syscallarg(T) request;
1249 syscallarg(T) pid;
1250 syscallarg(T) addr;
1251 syscallarg(T) data;
1252 } */ *uap = v;
1253 const int *ptr;
1254 int request;
1255 int error;
1256
1257 ptr = linux_ptrace_request_map;
1258 request = SCARG(uap, request);
1259 while (*ptr != -1)
1260 if (*ptr++ == request) {
1261 struct sys_ptrace_args pta;
1262
1263 SCARG(&pta, req) = *ptr;
1264 SCARG(&pta, pid) = SCARG(uap, pid);
1265 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1266 SCARG(&pta, data) = SCARG(uap, data);
1267
1268 /*
1269 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1270 * to continue where the process left off previously.
1271 * The same thing is achieved by addr == (caddr_t) 1
1272 * on NetBSD, so rewrite 'addr' appropriately.
1273 */
1274 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1275 SCARG(&pta, addr) = (caddr_t) 1;
1276
1277 error = sys_ptrace(p, &pta, retval);
1278 if (error)
1279 return error;
1280 switch (request) {
1281 case LINUX_PTRACE_PEEKTEXT:
1282 case LINUX_PTRACE_PEEKDATA:
1283 error = copyout (retval,
1284 (caddr_t)SCARG(uap, data), sizeof *retval);
1285 *retval = SCARG(uap, data);
1286 break;
1287 default:
1288 break;
1289 }
1290 return error;
1291 }
1292 else
1293 ptr++;
1294
1295 return LINUX_SYS_PTRACE_ARCH(p, uap, retval);
1296 }
1297
1298 int
1299 linux_sys_reboot(struct proc *p, void *v, register_t *retval)
1300 {
1301 struct linux_sys_reboot_args /* {
1302 syscallarg(int) magic1;
1303 syscallarg(int) magic2;
1304 syscallarg(int) cmd;
1305 syscallarg(void *) arg;
1306 } */ *uap = v;
1307 struct sys_reboot_args /* {
1308 syscallarg(int) opt;
1309 syscallarg(char *) bootstr;
1310 } */ sra;
1311 int error;
1312
1313 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1314 return(error);
1315
1316 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1317 return(EINVAL);
1318 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1319 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1320 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1321 return(EINVAL);
1322
1323 switch (SCARG(uap, cmd)) {
1324 case LINUX_REBOOT_CMD_RESTART:
1325 SCARG(&sra, opt) = RB_AUTOBOOT;
1326 break;
1327 case LINUX_REBOOT_CMD_HALT:
1328 SCARG(&sra, opt) = RB_HALT;
1329 break;
1330 case LINUX_REBOOT_CMD_POWER_OFF:
1331 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1332 break;
1333 case LINUX_REBOOT_CMD_RESTART2:
1334 /* Reboot with an argument. */
1335 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1336 SCARG(&sra, bootstr) = SCARG(uap, arg);
1337 break;
1338 case LINUX_REBOOT_CMD_CAD_ON:
1339 return(EINVAL); /* We don't implement ctrl-alt-delete */
1340 case LINUX_REBOOT_CMD_CAD_OFF:
1341 return(0);
1342 default:
1343 return(EINVAL);
1344 }
1345
1346 return(sys_reboot(p, &sra, retval));
1347 }
1348
1349 /*
1350 * Copy of compat_12_sys_swapon().
1351 */
1352 int
1353 linux_sys_swapon(p, v, retval)
1354 struct proc *p;
1355 void *v;
1356 register_t *retval;
1357 {
1358 struct sys_swapctl_args ua;
1359 struct linux_sys_swapon_args /* {
1360 syscallarg(const char *) name;
1361 } */ *uap = v;
1362
1363 SCARG(&ua, cmd) = SWAP_ON;
1364 SCARG(&ua, arg) = (void *)SCARG(uap, name);
1365 SCARG(&ua, misc) = 0; /* priority */
1366 return (sys_swapctl(p, &ua, retval));
1367 }
1368
1369 /*
1370 * Stop swapping to the file or block device specified by path.
1371 */
1372 int
1373 linux_sys_swapoff(p, v, retval)
1374 struct proc *p;
1375 void *v;
1376 register_t *retval;
1377 {
1378 struct sys_swapctl_args ua;
1379 struct linux_sys_swapoff_args /* {
1380 syscallarg(const char *) path;
1381 } */ *uap = v;
1382
1383 SCARG(&ua, cmd) = SWAP_OFF;
1384 SCARG(&ua, arg) = (void *)SCARG(uap, path);
1385 return (sys_swapctl(p, &ua, retval));
1386 }
1387
1388 /*
1389 * Copy of compat_09_sys_setdomainname()
1390 */
1391 /* ARGSUSED */
1392 int
1393 linux_sys_setdomainname(p, v, retval)
1394 struct proc *p;
1395 void *v;
1396 register_t *retval;
1397 {
1398 struct linux_sys_setdomainname_args /* {
1399 syscallarg(char *) domainname;
1400 syscallarg(int) len;
1401 } */ *uap = v;
1402 int name;
1403 int error;
1404
1405 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1406 return (error);
1407 name = KERN_DOMAINNAME;
1408 return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
1409 SCARG(uap, len), p));
1410 }
1411
1412 /*
1413 * sysinfo()
1414 */
1415 /* ARGSUSED */
1416 int
1417 linux_sys_sysinfo(p, v, retval)
1418 struct proc *p;
1419 void *v;
1420 register_t *retval;
1421 {
1422 struct linux_sys_sysinfo_args /* {
1423 syscallarg(struct linux_sysinfo *) arg;
1424 } */ *uap = v;
1425 struct linux_sysinfo si;
1426 struct loadavg *la;
1427
1428 si.uptime = time.tv_sec - boottime.tv_sec;
1429 la = &averunnable;
1430 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1431 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1432 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1433 si.totalram = ctob(physmem);
1434 si.freeram = uvmexp.free * uvmexp.pagesize;
1435 si.sharedram = 0; /* XXX */
1436 si.bufferram = uvmexp.filepages * uvmexp.pagesize;
1437 si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1438 si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1439 si.procs = nprocs;
1440
1441 /* The following are only present in newer Linux kernels. */
1442 si.totalbig = 0;
1443 si.freebig = 0;
1444 si.mem_unit = 1;
1445
1446 return (copyout(&si, SCARG(uap, arg), sizeof si));
1447 }
1448
1449 #define bsd_to_linux_rlimit1(l, b, f) \
1450 (l)->f = ((b)->f == RLIM_INFINITY || ((b)->f & 0xffffffff00000000) != 0) ? \
1451 LINUX_RLIM_INFINITY : (int32_t)(b)->f
1452 #define bsd_to_linux_rlimit(l, b) \
1453 bsd_to_linux_rlimit1(l, b, rlim_cur); \
1454 bsd_to_linux_rlimit1(l, b, rlim_max)
1455
1456 #define linux_to_bsd_rlimit1(b, l, f) \
1457 (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (b)->f
1458 #define linux_to_bsd_rlimit(b, l) \
1459 linux_to_bsd_rlimit1(b, l, rlim_cur); \
1460 linux_to_bsd_rlimit1(b, l, rlim_max)
1461
1462 static int
1463 linux_to_bsd_limit(lim)
1464 int lim;
1465 {
1466 switch (lim) {
1467 case LINUX_RLIMIT_CPU:
1468 return RLIMIT_CPU;
1469 case LINUX_RLIMIT_FSIZE:
1470 return RLIMIT_FSIZE;
1471 case LINUX_RLIMIT_DATA:
1472 return RLIMIT_DATA;
1473 case LINUX_RLIMIT_STACK:
1474 return RLIMIT_STACK;
1475 case LINUX_RLIMIT_CORE:
1476 return RLIMIT_CORE;
1477 case LINUX_RLIMIT_RSS:
1478 return RLIMIT_RSS;
1479 case LINUX_RLIMIT_NPROC:
1480 return RLIMIT_NPROC;
1481 case LINUX_RLIMIT_NOFILE:
1482 return RLIMIT_NOFILE;
1483 case LINUX_RLIMIT_MEMLOCK:
1484 return RLIMIT_MEMLOCK;
1485 case LINUX_RLIMIT_AS:
1486 case LINUX_RLIMIT_LOCKS:
1487 return -EOPNOTSUPP;
1488 default:
1489 return -EINVAL;
1490 }
1491 }
1492
1493
1494 int
1495 linux_sys_getrlimit(p, v, retval)
1496 struct proc *p;
1497 void *v;
1498 register_t *retval;
1499 {
1500 struct linux_sys_getrlimit_args /* {
1501 syscallarg(int) which;
1502 syscallarg(struct orlimit *) rlp;
1503 } */ *uap = v;
1504 caddr_t sg = stackgap_init(p->p_emul);
1505 struct sys_getrlimit_args ap;
1506 struct rlimit rl;
1507 struct orlimit orl;
1508 int error;
1509
1510 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1511 if ((error = SCARG(&ap, which)) < 0)
1512 return -error;
1513 SCARG(&ap, rlp) = stackgap_alloc(&sg, sizeof rl);
1514 if ((error = sys_getrlimit(p, &ap, retval)) != 0)
1515 return error;
1516 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1517 return error;
1518 bsd_to_linux_rlimit(&orl, &rl);
1519 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1520 }
1521
1522 int
1523 linux_sys_setrlimit(p, v, retval)
1524 struct proc *p;
1525 void *v;
1526 register_t *retval;
1527 {
1528 struct linux_sys_setrlimit_args /* {
1529 syscallarg(int) which;
1530 syscallarg(struct orlimit *) rlp;
1531 } */ *uap = v;
1532 caddr_t sg = stackgap_init(p->p_emul);
1533 struct sys_setrlimit_args ap;
1534 struct rlimit rl;
1535 struct orlimit orl;
1536 int error;
1537
1538 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1539 SCARG(&ap, rlp) = stackgap_alloc(&sg, sizeof rl);
1540 if ((error = SCARG(&ap, which)) < 0)
1541 return -error;
1542 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1543 return error;
1544 linux_to_bsd_rlimit(&rl, &orl);
1545 /* XXX: alpha complains about this */
1546 if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
1547 return error;
1548 return sys_setrlimit(p, &ap, retval);
1549 }
1550
1551 #ifndef __mips__
1552 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1553 int
1554 linux_sys_ugetrlimit(p, v, retval)
1555 struct proc *p;
1556 void *v;
1557 register_t *retval;
1558 {
1559 return linux_sys_getrlimit(p, v, retval);
1560 }
1561 #endif
1562
1563 /*
1564 * This gets called for unsupported syscalls. The difference to sys_nosys()
1565 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1566 * This is the way Linux does it and glibc depends on this behaviour.
1567 */
1568 int
1569 linux_sys_nosys(p, v, retval)
1570 struct proc *p;
1571 void *v;
1572 register_t *retval;
1573 {
1574 return (ENOSYS);
1575 }
1576