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