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