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