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