linux_misc.c revision 1.83.2.13 1 /* $NetBSD: linux_misc.c,v 1.83.2.13 2002/04/17 02:01:20 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.13 2002/04/17 02:01:20 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(l, v, retval)
816 struct lwp *l;
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 proc *p = l->l_proc;
826 struct dirent *bdp;
827 struct vnode *vp;
828 caddr_t inp, buf; /* BSD-format */
829 int len, reclen; /* BSD-format */
830 caddr_t outp; /* Linux-format */
831 int resid, linux_reclen = 0; /* Linux-format */
832 struct file *fp;
833 struct uio auio;
834 struct iovec aiov;
835 struct linux_dirent64 idb;
836 off_t off; /* true file offset */
837 int buflen, error, eofflag, nbytes, oldcall;
838 struct vattr va;
839 off_t *cookiebuf = NULL, *cookie;
840 int ncookies;
841
842 /* getvnode() will use the descriptor for us */
843 if ((error = getvnode(p->p_fd, SCARG(uap, fd), &fp)) != 0)
844 return (error);
845
846 if ((fp->f_flag & FREAD) == 0) {
847 error = EBADF;
848 goto out1;
849 }
850
851 vp = (struct vnode *)fp->f_data;
852 if (vp->v_type != VDIR) {
853 error = EINVAL;
854 goto out1;
855 }
856
857 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)))
858 goto out1;
859
860 nbytes = SCARG(uap, count);
861 if (nbytes == 1) { /* emulating old, broken behaviour */
862 nbytes = sizeof (idb);
863 buflen = max(va.va_blocksize, nbytes);
864 oldcall = 1;
865 } else {
866 buflen = min(MAXBSIZE, nbytes);
867 if (buflen < va.va_blocksize)
868 buflen = va.va_blocksize;
869 oldcall = 0;
870 }
871 buf = malloc(buflen, M_TEMP, M_WAITOK);
872
873 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
874 off = fp->f_offset;
875 again:
876 aiov.iov_base = buf;
877 aiov.iov_len = buflen;
878 auio.uio_iov = &aiov;
879 auio.uio_iovcnt = 1;
880 auio.uio_rw = UIO_READ;
881 auio.uio_segflg = UIO_SYSSPACE;
882 auio.uio_procp = p;
883 auio.uio_resid = buflen;
884 auio.uio_offset = off;
885 /*
886 * First we read into the malloc'ed buffer, then
887 * we massage it into user space, one record at a time.
888 */
889 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, &cookiebuf,
890 &ncookies);
891 if (error)
892 goto out;
893
894 inp = buf;
895 outp = (caddr_t)SCARG(uap, dent);
896 resid = nbytes;
897 if ((len = buflen - auio.uio_resid) == 0)
898 goto eof;
899
900 for (cookie = cookiebuf; len > 0; len -= reclen) {
901 bdp = (struct dirent *)inp;
902 reclen = bdp->d_reclen;
903 if (reclen & 3)
904 panic("linux_readdir");
905 if (bdp->d_fileno == 0) {
906 inp += reclen; /* it is a hole; squish it out */
907 off = *cookie++;
908 continue;
909 }
910 linux_reclen = LINUX_RECLEN(&idb, bdp->d_namlen);
911 if (reclen > len || resid < linux_reclen) {
912 /* entry too big for buffer, so just stop */
913 outp++;
914 break;
915 }
916 /*
917 * Massage in place to make a Linux-shaped dirent (otherwise
918 * we have to worry about touching user memory outside of
919 * the copyout() call).
920 */
921 idb.d_ino = bdp->d_fileno;
922 idb.d_type = bdp->d_type;
923 /*
924 * The old readdir() call misuses the offset and reclen fields.
925 */
926 if (oldcall) {
927 idb.d_off = linux_reclen;
928 idb.d_reclen = (u_short)bdp->d_namlen;
929 } else {
930 if (sizeof (idb.d_off) < 4 && (off >> 32) != 0) {
931 compat_offseterr(vp, "linux_getdents");
932 error = EINVAL;
933 goto out;
934 }
935 idb.d_off = off;
936 idb.d_reclen = (u_short)linux_reclen;
937 }
938 strcpy(idb.d_name, bdp->d_name);
939 if ((error = copyout((caddr_t)&idb, outp, linux_reclen)))
940 goto out;
941 /* advance past this real entry */
942 inp += reclen;
943 off = *cookie++; /* each entry points to itself */
944 /* advance output past Linux-shaped entry */
945 outp += linux_reclen;
946 resid -= linux_reclen;
947 if (oldcall)
948 break;
949 }
950
951 /* if we squished out the whole block, try again */
952 if (outp == (caddr_t)SCARG(uap, dent))
953 goto again;
954 fp->f_offset = off; /* update the vnode offset */
955
956 if (oldcall)
957 nbytes = resid + linux_reclen;
958
959 eof:
960 *retval = nbytes - resid;
961 out:
962 VOP_UNLOCK(vp, 0);
963 if (cookiebuf)
964 free(cookiebuf, M_TEMP);
965 free(buf, M_TEMP);
966 out1:
967 FILE_UNUSE(fp, p);
968 return error;
969 }
970
971 /*
972 * Even when just using registers to pass arguments to syscalls you can
973 * have 5 of them on the i386. So this newer version of select() does
974 * this.
975 */
976 int
977 linux_sys_select(l, v, retval)
978 struct lwp *l;
979 void *v;
980 register_t *retval;
981 {
982 struct linux_sys_select_args /* {
983 syscallarg(int) nfds;
984 syscallarg(fd_set *) readfds;
985 syscallarg(fd_set *) writefds;
986 syscallarg(fd_set *) exceptfds;
987 syscallarg(struct timeval *) timeout;
988 } */ *uap = v;
989
990 return linux_select1(l, retval, SCARG(uap, nfds), SCARG(uap, readfds),
991 SCARG(uap, writefds), SCARG(uap, exceptfds), SCARG(uap, timeout));
992 }
993
994 /*
995 * Common code for the old and new versions of select(). A couple of
996 * things are important:
997 * 1) return the amount of time left in the 'timeout' parameter
998 * 2) select never returns ERESTART on Linux, always return EINTR
999 */
1000 int
1001 linux_select1(l, retval, nfds, readfds, writefds, exceptfds, timeout)
1002 struct lwp *l;
1003 register_t *retval;
1004 int nfds;
1005 fd_set *readfds, *writefds, *exceptfds;
1006 struct timeval *timeout;
1007 {
1008 struct sys_select_args bsa;
1009 struct proc *p = l->l_proc;
1010 struct timeval tv0, tv1, utv, *tvp;
1011 caddr_t sg;
1012 int error;
1013
1014 SCARG(&bsa, nd) = nfds;
1015 SCARG(&bsa, in) = readfds;
1016 SCARG(&bsa, ou) = writefds;
1017 SCARG(&bsa, ex) = exceptfds;
1018 SCARG(&bsa, tv) = timeout;
1019
1020 /*
1021 * Store current time for computation of the amount of
1022 * time left.
1023 */
1024 if (timeout) {
1025 if ((error = copyin(timeout, &utv, sizeof(utv))))
1026 return error;
1027 if (itimerfix(&utv)) {
1028 /*
1029 * The timeval was invalid. Convert it to something
1030 * valid that will act as it does under Linux.
1031 */
1032 sg = stackgap_init(p, 0);
1033 tvp = stackgap_alloc(p, &sg, sizeof(utv));
1034 utv.tv_sec += utv.tv_usec / 1000000;
1035 utv.tv_usec %= 1000000;
1036 if (utv.tv_usec < 0) {
1037 utv.tv_sec -= 1;
1038 utv.tv_usec += 1000000;
1039 }
1040 if (utv.tv_sec < 0)
1041 timerclear(&utv);
1042 if ((error = copyout(&utv, tvp, sizeof(utv))))
1043 return error;
1044 SCARG(&bsa, tv) = tvp;
1045 }
1046 microtime(&tv0);
1047 }
1048
1049 error = sys_select(l, &bsa, retval);
1050 if (error) {
1051 /*
1052 * See fs/select.c in the Linux kernel. Without this,
1053 * Maelstrom doesn't work.
1054 */
1055 if (error == ERESTART)
1056 error = EINTR;
1057 return error;
1058 }
1059
1060 if (timeout) {
1061 if (*retval) {
1062 /*
1063 * Compute how much time was left of the timeout,
1064 * by subtracting the current time and the time
1065 * before we started the call, and subtracting
1066 * that result from the user-supplied value.
1067 */
1068 microtime(&tv1);
1069 timersub(&tv1, &tv0, &tv1);
1070 timersub(&utv, &tv1, &utv);
1071 if (utv.tv_sec < 0)
1072 timerclear(&utv);
1073 } else
1074 timerclear(&utv);
1075 if ((error = copyout(&utv, timeout, sizeof(utv))))
1076 return error;
1077 }
1078
1079 return 0;
1080 }
1081
1082 /*
1083 * Get the process group of a certain process. Look it up
1084 * and return the value.
1085 */
1086 int
1087 linux_sys_getpgid(l, v, retval)
1088 struct lwp *l;
1089 void *v;
1090 register_t *retval;
1091 {
1092 struct linux_sys_getpgid_args /* {
1093 syscallarg(int) pid;
1094 } */ *uap = v;
1095 struct proc *p = l->l_proc;
1096 struct proc *targp;
1097
1098 if (SCARG(uap, pid) != 0 && SCARG(uap, pid) != p->p_pid) {
1099 if ((targp = pfind(SCARG(uap, pid))) == 0)
1100 return ESRCH;
1101 }
1102 else
1103 targp = p;
1104
1105 retval[0] = targp->p_pgid;
1106 return 0;
1107 }
1108
1109 /*
1110 * Set the 'personality' (emulation mode) for the current process. Only
1111 * accept the Linux personality here (0). This call is needed because
1112 * the Linux ELF crt0 issues it in an ugly kludge to make sure that
1113 * ELF binaries run in Linux mode, not SVR4 mode.
1114 */
1115 int
1116 linux_sys_personality(l, v, retval)
1117 struct lwp *l;
1118 void *v;
1119 register_t *retval;
1120 {
1121 struct linux_sys_personality_args /* {
1122 syscallarg(int) per;
1123 } */ *uap = v;
1124
1125 if (SCARG(uap, per) != 0)
1126 return EINVAL;
1127 retval[0] = 0;
1128 return 0;
1129 }
1130
1131 #if defined(__i386__) || defined(__m68k__)
1132 /*
1133 * The calls are here because of type conversions.
1134 */
1135 int
1136 linux_sys_setreuid16(l, v, retval)
1137 struct lwp *l;
1138 void *v;
1139 register_t *retval;
1140 {
1141 struct linux_sys_setreuid16_args /* {
1142 syscallarg(int) ruid;
1143 syscallarg(int) euid;
1144 } */ *uap = v;
1145 struct sys_setreuid_args bsa;
1146
1147 SCARG(&bsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1148 (uid_t)-1 : SCARG(uap, ruid);
1149 SCARG(&bsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1150 (uid_t)-1 : SCARG(uap, euid);
1151
1152 return sys_setreuid(l, &bsa, retval);
1153 }
1154
1155 int
1156 linux_sys_setregid16(l, v, retval)
1157 struct lwp *l;
1158 void *v;
1159 register_t *retval;
1160 {
1161 struct linux_sys_setregid16_args /* {
1162 syscallarg(int) rgid;
1163 syscallarg(int) egid;
1164 } */ *uap = v;
1165 struct sys_setregid_args bsa;
1166
1167 SCARG(&bsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1168 (uid_t)-1 : SCARG(uap, rgid);
1169 SCARG(&bsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1170 (uid_t)-1 : SCARG(uap, egid);
1171
1172 return sys_setregid(l, &bsa, retval);
1173 }
1174
1175 int
1176 linux_sys_setresuid16(l, v, retval)
1177 struct lwp *l;
1178 void *v;
1179 register_t *retval;
1180 {
1181 struct linux_sys_setresuid16_args /* {
1182 syscallarg(uid_t) ruid;
1183 syscallarg(uid_t) euid;
1184 syscallarg(uid_t) suid;
1185 } */ *uap = v;
1186 struct linux_sys_setresuid16_args lsa;
1187
1188 SCARG(&lsa, ruid) = ((linux_uid_t)SCARG(uap, ruid) == (linux_uid_t)-1) ?
1189 (uid_t)-1 : SCARG(uap, ruid);
1190 SCARG(&lsa, euid) = ((linux_uid_t)SCARG(uap, euid) == (linux_uid_t)-1) ?
1191 (uid_t)-1 : SCARG(uap, euid);
1192 SCARG(&lsa, suid) = ((linux_uid_t)SCARG(uap, suid) == (linux_uid_t)-1) ?
1193 (uid_t)-1 : SCARG(uap, suid);
1194
1195 return linux_sys_setresuid(l, &lsa, retval);
1196 }
1197
1198 int
1199 linux_sys_setresgid16(l, v, retval)
1200 struct lwp *l;
1201 void *v;
1202 register_t *retval;
1203 {
1204 struct linux_sys_setresgid16_args /* {
1205 syscallarg(gid_t) rgid;
1206 syscallarg(gid_t) egid;
1207 syscallarg(gid_t) sgid;
1208 } */ *uap = v;
1209 struct linux_sys_setresgid16_args lsa;
1210
1211 SCARG(&lsa, rgid) = ((linux_gid_t)SCARG(uap, rgid) == (linux_gid_t)-1) ?
1212 (gid_t)-1 : SCARG(uap, rgid);
1213 SCARG(&lsa, egid) = ((linux_gid_t)SCARG(uap, egid) == (linux_gid_t)-1) ?
1214 (gid_t)-1 : SCARG(uap, egid);
1215 SCARG(&lsa, sgid) = ((linux_gid_t)SCARG(uap, sgid) == (linux_gid_t)-1) ?
1216 (gid_t)-1 : SCARG(uap, sgid);
1217
1218 return linux_sys_setresgid(l, &lsa, retval);
1219 }
1220
1221 int
1222 linux_sys_getgroups16(l, v, retval)
1223 struct lwp *l;
1224 void *v;
1225 register_t *retval;
1226 {
1227 struct linux_sys_getgroups16_args /* {
1228 syscallarg(int) gidsetsize;
1229 syscallarg(linux_gid_t *) gidset;
1230 } */ *uap = v;
1231 struct proc *p = l->l_proc;
1232 caddr_t sg;
1233 int n, error, i;
1234 struct sys_getgroups_args bsa;
1235 gid_t *bset, *kbset;
1236 linux_gid_t *lset;
1237 struct pcred *pc = p->p_cred;
1238
1239 n = SCARG(uap, gidsetsize);
1240 if (n < 0)
1241 return EINVAL;
1242 error = 0;
1243 bset = kbset = NULL;
1244 lset = NULL;
1245 if (n > 0) {
1246 n = min(pc->pc_ucred->cr_ngroups, n);
1247 sg = stackgap_init(p, 0);
1248 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1249 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1250 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1251 if (bset == NULL || kbset == NULL || lset == NULL)
1252 return ENOMEM;
1253 SCARG(&bsa, gidsetsize) = n;
1254 SCARG(&bsa, gidset) = bset;
1255 error = sys_getgroups(l, &bsa, retval);
1256 if (error != 0)
1257 goto out;
1258 error = copyin(bset, kbset, n * sizeof (gid_t));
1259 if (error != 0)
1260 goto out;
1261 for (i = 0; i < n; i++)
1262 lset[i] = (linux_gid_t)kbset[i];
1263 error = copyout(lset, SCARG(uap, gidset),
1264 n * sizeof (linux_gid_t));
1265 } else
1266 *retval = pc->pc_ucred->cr_ngroups;
1267 out:
1268 if (kbset != NULL)
1269 free(kbset, M_TEMP);
1270 if (lset != NULL)
1271 free(lset, M_TEMP);
1272 return error;
1273 }
1274
1275 int
1276 linux_sys_setgroups16(l, v, retval)
1277 struct lwp *l;
1278 void *v;
1279 register_t *retval;
1280 {
1281 struct linux_sys_setgroups16_args /* {
1282 syscallarg(int) gidsetsize;
1283 syscallarg(linux_gid_t *) gidset;
1284 } */ *uap = v;
1285 struct proc *p = l->l_proc;
1286 caddr_t sg;
1287 int n;
1288 int error, i;
1289 struct sys_setgroups_args bsa;
1290 gid_t *bset, *kbset;
1291 linux_gid_t *lset;
1292
1293 n = SCARG(uap, gidsetsize);
1294 if (n < 0 || n > NGROUPS)
1295 return EINVAL;
1296 sg = stackgap_init(p, 0);
1297 bset = stackgap_alloc(p, &sg, n * sizeof (gid_t));
1298 lset = malloc(n * sizeof (linux_gid_t), M_TEMP, M_WAITOK);
1299 kbset = malloc(n * sizeof (gid_t), M_TEMP, M_WAITOK);
1300 if (lset == NULL || bset == NULL)
1301 return ENOMEM;
1302 error = copyin(SCARG(uap, gidset), lset, n * sizeof (linux_gid_t));
1303 if (error != 0)
1304 goto out;
1305 for (i = 0; i < n; i++)
1306 kbset[i] = (gid_t)lset[i];
1307 error = copyout(kbset, bset, n * sizeof (gid_t));
1308 if (error != 0)
1309 goto out;
1310 SCARG(&bsa, gidsetsize) = n;
1311 SCARG(&bsa, gidset) = bset;
1312 error = sys_setgroups(l, &bsa, retval);
1313
1314 out:
1315 if (lset != NULL)
1316 free(lset, M_TEMP);
1317 if (kbset != NULL)
1318 free(kbset, M_TEMP);
1319
1320 return error;
1321 }
1322
1323 #endif /* __i386__ || __m68k__ */
1324
1325 /*
1326 * We have nonexistent fsuid equal to uid.
1327 * If modification is requested, refuse.
1328 */
1329 int
1330 linux_sys_setfsuid(l, v, retval)
1331 struct lwp *l;
1332 void *v;
1333 register_t *retval;
1334 {
1335 struct linux_sys_setfsuid_args /* {
1336 syscallarg(uid_t) uid;
1337 } */ *uap = v;
1338 struct proc *p = l->l_proc;
1339 uid_t uid;
1340
1341 uid = SCARG(uap, uid);
1342 if (p->p_cred->p_ruid != uid)
1343 return sys_nosys(l, v, retval);
1344 else
1345 return (0);
1346 }
1347
1348 /* XXX XXX XXX */
1349 #ifndef alpha
1350 int
1351 linux_sys_getfsuid(l, v, retval)
1352 struct lwp *l;
1353 void *v;
1354 register_t *retval;
1355 {
1356 return sys_getuid(l, v, retval);
1357 }
1358 #endif
1359
1360 int
1361 linux_sys_setresuid(l, v, retval)
1362 struct lwp *l;
1363 void *v;
1364 register_t *retval;
1365 {
1366 struct linux_sys_setresuid_args /* {
1367 syscallarg(uid_t) ruid;
1368 syscallarg(uid_t) euid;
1369 syscallarg(uid_t) suid;
1370 } */ *uap = v;
1371 struct proc *p = l->l_proc;
1372 struct pcred *pc = p->p_cred;
1373 uid_t ruid, euid, suid;
1374 int error;
1375
1376 ruid = SCARG(uap, ruid);
1377 euid = SCARG(uap, euid);
1378 suid = SCARG(uap, suid);
1379
1380 /*
1381 * Note: These checks are a little different than the NetBSD
1382 * setreuid(2) call performs. This precisely follows the
1383 * behavior of the Linux kernel.
1384 */
1385 if (ruid != (uid_t)-1 &&
1386 ruid != pc->p_ruid &&
1387 ruid != pc->pc_ucred->cr_uid &&
1388 ruid != pc->p_svuid &&
1389 (error = suser(pc->pc_ucred, &p->p_acflag)))
1390 return (error);
1391
1392 if (euid != (uid_t)-1 &&
1393 euid != pc->p_ruid &&
1394 euid != pc->pc_ucred->cr_uid &&
1395 euid != pc->p_svuid &&
1396 (error = suser(pc->pc_ucred, &p->p_acflag)))
1397 return (error);
1398
1399 if (suid != (uid_t)-1 &&
1400 suid != pc->p_ruid &&
1401 suid != pc->pc_ucred->cr_uid &&
1402 suid != pc->p_svuid &&
1403 (error = suser(pc->pc_ucred, &p->p_acflag)))
1404 return (error);
1405
1406 /*
1407 * Now assign the new real, effective, and saved UIDs.
1408 * Note that Linux, unlike NetBSD in setreuid(2), does not
1409 * set the saved UID in this call unless the user specifies
1410 * it.
1411 */
1412 if (ruid != (uid_t)-1) {
1413 (void)chgproccnt(pc->p_ruid, -1);
1414 (void)chgproccnt(ruid, 1);
1415 pc->p_ruid = ruid;
1416 }
1417
1418 if (euid != (uid_t)-1) {
1419 pc->pc_ucred = crcopy(pc->pc_ucred);
1420 pc->pc_ucred->cr_uid = euid;
1421 }
1422
1423 if (suid != (uid_t)-1)
1424 pc->p_svuid = suid;
1425
1426 if (ruid != (uid_t)-1 && euid != (uid_t)-1 && suid != (uid_t)-1)
1427 p->p_flag |= P_SUGID;
1428 return (0);
1429 }
1430
1431 int
1432 linux_sys_getresuid(l, v, retval)
1433 struct lwp *l;
1434 void *v;
1435 register_t *retval;
1436 {
1437 struct linux_sys_getresuid_args /* {
1438 syscallarg(uid_t *) ruid;
1439 syscallarg(uid_t *) euid;
1440 syscallarg(uid_t *) suid;
1441 } */ *uap = v;
1442 struct proc *p = l->l_proc;
1443 struct pcred *pc = p->p_cred;
1444 int error;
1445
1446 /*
1447 * Linux copies these values out to userspace like so:
1448 *
1449 * 1. Copy out ruid.
1450 * 2. If that succeeds, copy out euid.
1451 * 3. If both of those succeed, copy out suid.
1452 */
1453 if ((error = copyout(&pc->p_ruid, SCARG(uap, ruid),
1454 sizeof(uid_t))) != 0)
1455 return (error);
1456
1457 if ((error = copyout(&pc->pc_ucred->cr_uid, SCARG(uap, euid),
1458 sizeof(uid_t))) != 0)
1459 return (error);
1460
1461 return (copyout(&pc->p_svuid, SCARG(uap, suid), sizeof(uid_t)));
1462 }
1463
1464 int
1465 linux_sys_ptrace(l, v, retval)
1466 struct lwp *l;
1467 void *v;
1468 register_t *retval;
1469 {
1470 struct linux_sys_ptrace_args /* {
1471 i386, m68k, powerpc: T=int
1472 alpha: T=long
1473 syscallarg(T) request;
1474 syscallarg(T) pid;
1475 syscallarg(T) addr;
1476 syscallarg(T) data;
1477 } */ *uap = v;
1478 const int *ptr;
1479 int request;
1480 int error;
1481
1482 ptr = linux_ptrace_request_map;
1483 request = SCARG(uap, request);
1484 while (*ptr != -1)
1485 if (*ptr++ == request) {
1486 struct sys_ptrace_args pta;
1487
1488 SCARG(&pta, req) = *ptr;
1489 SCARG(&pta, pid) = SCARG(uap, pid);
1490 SCARG(&pta, addr) = (caddr_t)SCARG(uap, addr);
1491 SCARG(&pta, data) = SCARG(uap, data);
1492
1493 /*
1494 * Linux ptrace(PTRACE_CONT, pid, 0, 0) means actually
1495 * to continue where the process left off previously.
1496 * The same thing is achieved by addr == (caddr_t) 1
1497 * on NetBSD, so rewrite 'addr' appropriately.
1498 */
1499 if (request == LINUX_PTRACE_CONT && SCARG(uap, addr)==0)
1500 SCARG(&pta, addr) = (caddr_t) 1;
1501
1502 error = sys_ptrace(l, &pta, retval);
1503 if (error)
1504 return error;
1505 switch (request) {
1506 case LINUX_PTRACE_PEEKTEXT:
1507 case LINUX_PTRACE_PEEKDATA:
1508 error = copyout (retval,
1509 (caddr_t)SCARG(uap, data), sizeof *retval);
1510 *retval = SCARG(uap, data);
1511 break;
1512 default:
1513 break;
1514 }
1515 return error;
1516 }
1517 else
1518 ptr++;
1519
1520 return LINUX_SYS_PTRACE_ARCH(l, uap, retval);
1521 }
1522
1523 int
1524 linux_sys_reboot(struct lwp *l, void *v, register_t *retval)
1525 {
1526 struct linux_sys_reboot_args /* {
1527 syscallarg(int) magic1;
1528 syscallarg(int) magic2;
1529 syscallarg(int) cmd;
1530 syscallarg(void *) arg;
1531 } */ *uap = v;
1532 struct sys_reboot_args /* {
1533 syscallarg(int) opt;
1534 syscallarg(char *) bootstr;
1535 } */ sra;
1536 struct proc *p = l->l_proc;
1537 int error;
1538
1539 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1540 return(error);
1541
1542 if (SCARG(uap, magic1) != LINUX_REBOOT_MAGIC1)
1543 return(EINVAL);
1544 if (SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2 &&
1545 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2A &&
1546 SCARG(uap, magic2) != LINUX_REBOOT_MAGIC2B)
1547 return(EINVAL);
1548
1549 switch (SCARG(uap, cmd)) {
1550 case LINUX_REBOOT_CMD_RESTART:
1551 SCARG(&sra, opt) = RB_AUTOBOOT;
1552 break;
1553 case LINUX_REBOOT_CMD_HALT:
1554 SCARG(&sra, opt) = RB_HALT;
1555 break;
1556 case LINUX_REBOOT_CMD_POWER_OFF:
1557 SCARG(&sra, opt) = RB_HALT|RB_POWERDOWN;
1558 break;
1559 case LINUX_REBOOT_CMD_RESTART2:
1560 /* Reboot with an argument. */
1561 SCARG(&sra, opt) = RB_AUTOBOOT|RB_STRING;
1562 SCARG(&sra, bootstr) = SCARG(uap, arg);
1563 break;
1564 case LINUX_REBOOT_CMD_CAD_ON:
1565 return(EINVAL); /* We don't implement ctrl-alt-delete */
1566 case LINUX_REBOOT_CMD_CAD_OFF:
1567 return(0);
1568 default:
1569 return(EINVAL);
1570 }
1571
1572 return(sys_reboot(l, &sra, retval));
1573 }
1574
1575 /*
1576 * Copy of compat_12_sys_swapon().
1577 */
1578 int
1579 linux_sys_swapon(l, v, retval)
1580 struct lwp *l;
1581 void *v;
1582 register_t *retval;
1583 {
1584 struct sys_swapctl_args ua;
1585 struct linux_sys_swapon_args /* {
1586 syscallarg(const char *) name;
1587 } */ *uap = v;
1588
1589 SCARG(&ua, cmd) = SWAP_ON;
1590 SCARG(&ua, arg) = (void *)SCARG(uap, name);
1591 SCARG(&ua, misc) = 0; /* priority */
1592 return (sys_swapctl(l, &ua, retval));
1593 }
1594
1595 /*
1596 * Stop swapping to the file or block device specified by path.
1597 */
1598 int
1599 linux_sys_swapoff(l, v, retval)
1600 struct lwp *l;
1601 void *v;
1602 register_t *retval;
1603 {
1604 struct sys_swapctl_args ua;
1605 struct linux_sys_swapoff_args /* {
1606 syscallarg(const char *) path;
1607 } */ *uap = v;
1608
1609 SCARG(&ua, cmd) = SWAP_OFF;
1610 SCARG(&ua, arg) = (void *)SCARG(uap, path);
1611 return (sys_swapctl(l, &ua, retval));
1612 }
1613
1614 /*
1615 * Copy of compat_09_sys_setdomainname()
1616 */
1617 /* ARGSUSED */
1618 int
1619 linux_sys_setdomainname(l, v, retval)
1620 struct lwp *l;
1621 void *v;
1622 register_t *retval;
1623 {
1624 struct linux_sys_setdomainname_args /* {
1625 syscallarg(char *) domainname;
1626 syscallarg(int) len;
1627 } */ *uap = v;
1628 struct proc *p = l->l_proc;
1629 int name;
1630 int error;
1631
1632 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1633 return (error);
1634 name = KERN_DOMAINNAME;
1635 return (kern_sysctl(&name, 1, 0, 0, SCARG(uap, domainname),
1636 SCARG(uap, len), p));
1637 }
1638
1639 /*
1640 * sysinfo()
1641 */
1642 /* ARGSUSED */
1643 int
1644 linux_sys_sysinfo(l, v, retval)
1645 struct lwp *l;
1646 void *v;
1647 register_t *retval;
1648 {
1649 struct linux_sys_sysinfo_args /* {
1650 syscallarg(struct linux_sysinfo *) arg;
1651 } */ *uap = v;
1652 struct linux_sysinfo si;
1653 struct loadavg *la;
1654
1655 si.uptime = time.tv_sec - boottime.tv_sec;
1656 la = &averunnable;
1657 si.loads[0] = la->ldavg[0] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1658 si.loads[1] = la->ldavg[1] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1659 si.loads[2] = la->ldavg[2] * LINUX_SYSINFO_LOADS_SCALE / la->fscale;
1660 si.totalram = ctob(physmem);
1661 si.freeram = uvmexp.free * uvmexp.pagesize;
1662 si.sharedram = 0; /* XXX */
1663 si.bufferram = uvmexp.filepages * uvmexp.pagesize;
1664 si.totalswap = uvmexp.swpages * uvmexp.pagesize;
1665 si.freeswap = (uvmexp.swpages - uvmexp.swpginuse) * uvmexp.pagesize;
1666 si.procs = nprocs;
1667
1668 /* The following are only present in newer Linux kernels. */
1669 si.totalbig = 0;
1670 si.freebig = 0;
1671 si.mem_unit = 1;
1672
1673 return (copyout(&si, SCARG(uap, arg), sizeof si));
1674 }
1675
1676 #define bsd_to_linux_rlimit1(l, b, f) \
1677 (l)->f = ((b)->f == RLIM_INFINITY || ((b)->f & 0xffffffff00000000) != 0) ? \
1678 LINUX_RLIM_INFINITY : (int32_t)(b)->f
1679 #define bsd_to_linux_rlimit(l, b) \
1680 bsd_to_linux_rlimit1(l, b, rlim_cur); \
1681 bsd_to_linux_rlimit1(l, b, rlim_max)
1682
1683 #define linux_to_bsd_rlimit1(b, l, f) \
1684 (b)->f = (l)->f == LINUX_RLIM_INFINITY ? RLIM_INFINITY : (b)->f
1685 #define linux_to_bsd_rlimit(b, l) \
1686 linux_to_bsd_rlimit1(b, l, rlim_cur); \
1687 linux_to_bsd_rlimit1(b, l, rlim_max)
1688
1689 static int
1690 linux_to_bsd_limit(lim)
1691 int lim;
1692 {
1693 switch (lim) {
1694 case LINUX_RLIMIT_CPU:
1695 return RLIMIT_CPU;
1696 case LINUX_RLIMIT_FSIZE:
1697 return RLIMIT_FSIZE;
1698 case LINUX_RLIMIT_DATA:
1699 return RLIMIT_DATA;
1700 case LINUX_RLIMIT_STACK:
1701 return RLIMIT_STACK;
1702 case LINUX_RLIMIT_CORE:
1703 return RLIMIT_CORE;
1704 case LINUX_RLIMIT_RSS:
1705 return RLIMIT_RSS;
1706 case LINUX_RLIMIT_NPROC:
1707 return RLIMIT_NPROC;
1708 case LINUX_RLIMIT_NOFILE:
1709 return RLIMIT_NOFILE;
1710 case LINUX_RLIMIT_MEMLOCK:
1711 return RLIMIT_MEMLOCK;
1712 case LINUX_RLIMIT_AS:
1713 case LINUX_RLIMIT_LOCKS:
1714 return -EOPNOTSUPP;
1715 default:
1716 return -EINVAL;
1717 }
1718 }
1719
1720
1721 int
1722 linux_sys_getrlimit(l, v, retval)
1723 struct lwp *l;
1724 void *v;
1725 register_t *retval;
1726 {
1727 struct linux_sys_getrlimit_args /* {
1728 syscallarg(int) which;
1729 syscallarg(struct orlimit *) rlp;
1730 } */ *uap = v;
1731 struct proc *p = l->l_proc;
1732 caddr_t sg = stackgap_init(p, 0);
1733 struct sys_getrlimit_args ap;
1734 struct rlimit rl;
1735 struct orlimit orl;
1736 int error;
1737
1738 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1739 if ((error = SCARG(&ap, which)) < 0)
1740 return -error;
1741 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1742 if ((error = sys_getrlimit(l, &ap, retval)) != 0)
1743 return error;
1744 if ((error = copyin(SCARG(&ap, rlp), &rl, sizeof(rl))) != 0)
1745 return error;
1746 bsd_to_linux_rlimit(&orl, &rl);
1747 return copyout(&orl, SCARG(uap, rlp), sizeof(orl));
1748 }
1749
1750 int
1751 linux_sys_setrlimit(l, v, retval)
1752 struct lwp *l;
1753 void *v;
1754 register_t *retval;
1755 {
1756 struct linux_sys_setrlimit_args /* {
1757 syscallarg(int) which;
1758 syscallarg(struct orlimit *) rlp;
1759 } */ *uap = v;
1760 struct proc *p = l->l_proc;
1761 caddr_t sg = stackgap_init(p, 0);
1762 struct sys_setrlimit_args ap;
1763 struct rlimit rl;
1764 struct orlimit orl;
1765 int error;
1766
1767 SCARG(&ap, which) = linux_to_bsd_limit(SCARG(uap, which));
1768 SCARG(&ap, rlp) = stackgap_alloc(p, &sg, sizeof rl);
1769 if ((error = SCARG(&ap, which)) < 0)
1770 return -error;
1771 if ((error = copyin(SCARG(uap, rlp), &orl, sizeof(orl))) != 0)
1772 return error;
1773 linux_to_bsd_rlimit(&rl, &orl);
1774 /* XXX: alpha complains about this */
1775 if ((error = copyout(&rl, (void *)SCARG(&ap, rlp), sizeof(rl))) != 0)
1776 return error;
1777 return sys_setrlimit(l, &ap, retval);
1778 }
1779
1780 #ifndef __mips__
1781 /* XXX: this doesn't look 100% common, at least mips doesn't have it */
1782 int
1783 linux_sys_ugetrlimit(l, v, retval)
1784 struct lwp *l;
1785 void *v;
1786 register_t *retval;
1787 {
1788 return linux_sys_getrlimit(l, v, retval);
1789 }
1790 #endif
1791
1792 /*
1793 * This gets called for unsupported syscalls. The difference to sys_nosys()
1794 * is that process does not get SIGSYS, the call just returns with ENOSYS.
1795 * This is the way Linux does it and glibc depends on this behaviour.
1796 */
1797 int
1798 linux_sys_nosys(l, v, retval)
1799 struct lwp *l;
1800 void *v;
1801 register_t *retval;
1802 {
1803 return (ENOSYS);
1804 }
1805