1 /*- 2 * Copyright (c) 2002 Doug Rabson 3 * Copyright (c) 1994-1995 S�ren Schmidt 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_compat.h" 34 #include "opt_mac.h" 35 36 #include <sys/param.h> 37 #include <sys/blist.h> 38 #include <sys/fcntl.h> 39 #if defined(__i386__) 40 #include <sys/imgact_aout.h> 41 #endif 42 #include <sys/jail.h> 43 #include <sys/kernel.h> 44 #include <sys/limits.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mman.h> 48 #include <sys/mount.h> 49 #include <sys/mutex.h> 50 #include <sys/namei.h> 51 #include <sys/priv.h> 52 #include <sys/proc.h> 53 #include <sys/reboot.h> 54 #include <sys/resourcevar.h> 55 #include <sys/sched.h> 56 #include <sys/signalvar.h> 57 #include <sys/stat.h> 58 #include <sys/syscallsubr.h> 59 #include <sys/sysctl.h> 60 #include <sys/sysproto.h> 61 #include <sys/systm.h> 62 #include <sys/time.h> 63 #include <sys/vmmeter.h> 64 #include <sys/vnode.h> 65 #include <sys/wait.h> 66 67 #include <security/mac/mac_framework.h> 68 69 #include <vm/vm.h> 70 #include <vm/pmap.h> 71 #include <vm/vm_kern.h> 72 #include <vm/vm_map.h> 73 #include <vm/vm_extern.h> 74 #include <vm/vm_object.h> 75 #include <vm/swap_pager.h> 76 77 #include <compat/linux/linux_sysproto.h> 78 #include <compat/linux/linux_emul.h> 79 #include <compat/linux/linux_misc.h> 80 81 #ifdef COMPAT_LINUX32 82 #include <machine/../linux32/linux.h> 83 #include <machine/../linux32/linux32_proto.h> 84 #else 85 #include <machine/../linux/linux.h> 86 #include <machine/../linux/linux_proto.h> 87 #endif 88 89 #include <compat/linux/linux_mib.h> 90 #include <compat/linux/linux_signal.h> 91 #include <compat/linux/linux_util.h> 92 93 #ifdef __i386__ 94 #include <machine/cputypes.h> 95 #endif 96 97 #define BSD_TO_LINUX_SIGNAL(sig) \ 98 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 99 100 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 101 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 102 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 103 RLIMIT_MEMLOCK, RLIMIT_AS 104 }; 105 106 struct l_sysinfo { 107 l_long uptime; /* Seconds since boot */ 108 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 109 #define LINUX_SYSINFO_LOADS_SCALE 65536 110 l_ulong totalram; /* Total usable main memory size */ 111 l_ulong freeram; /* Available memory size */ 112 l_ulong sharedram; /* Amount of shared memory */ 113 l_ulong bufferram; /* Memory used by buffers */ 114 l_ulong totalswap; /* Total swap space size */ 115 l_ulong freeswap; /* swap space still available */ 116 l_ushort procs; /* Number of current processes */ 117 l_ushort pads; 118 l_ulong totalbig; 119 l_ulong freebig; 120 l_uint mem_unit; 121 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 122 }; 123 int 124 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 125 { 126 struct l_sysinfo sysinfo; 127 vm_object_t object; 128 int i, j; 129 struct timespec ts; 130 131 getnanouptime(&ts); 132 if (ts.tv_nsec != 0) 133 ts.tv_sec++; 134 sysinfo.uptime = ts.tv_sec; 135 136 /* Use the information from the mib to get our load averages */ 137 for (i = 0; i < 3; i++) 138 sysinfo.loads[i] = averunnable.ldavg[i] * 139 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 140 141 sysinfo.totalram = physmem * PAGE_SIZE; 142 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 143 144 sysinfo.sharedram = 0; 145 mtx_lock(&vm_object_list_mtx); 146 TAILQ_FOREACH(object, &vm_object_list, object_list) 147 if (object->shadow_count > 1) 148 sysinfo.sharedram += object->resident_page_count; 149 mtx_unlock(&vm_object_list_mtx); 150 151 sysinfo.sharedram *= PAGE_SIZE; 152 sysinfo.bufferram = 0; 153 154 swap_pager_status(&i, &j); 155 sysinfo.totalswap = i * PAGE_SIZE; 156 sysinfo.freeswap = (i - j) * PAGE_SIZE; 157 158 sysinfo.procs = nprocs; 159 160 /* The following are only present in newer Linux kernels. */ 161 sysinfo.totalbig = 0; 162 sysinfo.freebig = 0; 163 sysinfo.mem_unit = 1; 164 165 return copyout(&sysinfo, args->info, sizeof(sysinfo)); 166 } 167 168 int 169 linux_alarm(struct thread *td, struct linux_alarm_args *args) 170 { 171 struct itimerval it, old_it; 172 int error; 173 174 #ifdef DEBUG 175 if (ldebug(alarm)) 176 printf(ARGS(alarm, "%u"), args->secs); 177 #endif 178 179 if (args->secs > 100000000) 180 return (EINVAL); 181 182 it.it_value.tv_sec = (long)args->secs; 183 it.it_value.tv_usec = 0; 184 it.it_interval.tv_sec = 0; 185 it.it_interval.tv_usec = 0; 186 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 187 if (error) 188 return (error); 189 if (timevalisset(&old_it.it_value)) { 190 if (old_it.it_value.tv_usec != 0) 191 old_it.it_value.tv_sec++; 192 td->td_retval[0] = old_it.it_value.tv_sec; 193 } 194 return (0); 195 } 196 197 int 198 linux_brk(struct thread *td, struct linux_brk_args *args) 199 { 200 struct vmspace *vm = td->td_proc->p_vmspace; 201 vm_offset_t new, old; 202 struct obreak_args /* { 203 char * nsize; 204 } */ tmp; 205 206 #ifdef DEBUG 207 if (ldebug(brk)) 208 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 209 #endif 210 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 211 new = (vm_offset_t)args->dsend; 212 tmp.nsize = (char *)new; 213 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 214 td->td_retval[0] = (long)new; 215 else 216 td->td_retval[0] = (long)old; 217 218 return 0; 219 } 220 221 #if defined(__i386__) 222 /* XXX: what about amd64/linux32? */ 223 224 int 225 linux_uselib(struct thread *td, struct linux_uselib_args *args) 226 { 227 struct nameidata ni; 228 struct vnode *vp; 229 struct exec *a_out; 230 struct vattr attr; 231 vm_offset_t vmaddr; 232 unsigned long file_offset; 233 vm_offset_t buffer; 234 unsigned long bss_size; 235 char *library; 236 int error; 237 int locked, vfslocked; 238 239 LCONVPATHEXIST(td, args->library, &library); 240 241 #ifdef DEBUG 242 if (ldebug(uselib)) 243 printf(ARGS(uselib, "%s"), library); 244 #endif 245 246 a_out = NULL; 247 vfslocked = 0; 248 locked = 0; 249 vp = NULL; 250 251 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1, 252 UIO_SYSSPACE, library, td); 253 error = namei(&ni); 254 LFREEPATH(library); 255 if (error) 256 goto cleanup; 257 258 vp = ni.ni_vp; 259 vfslocked = NDHASGIANT(&ni); 260 NDFREE(&ni, NDF_ONLY_PNBUF); 261 262 /* 263 * From here on down, we have a locked vnode that must be unlocked. 264 * XXX: The code below largely duplicates exec_check_permissions(). 265 */ 266 locked = 1; 267 268 /* Writable? */ 269 if (vp->v_writecount) { 270 error = ETXTBSY; 271 goto cleanup; 272 } 273 274 /* Executable? */ 275 error = VOP_GETATTR(vp, &attr, td->td_ucred, td); 276 if (error) 277 goto cleanup; 278 279 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 280 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 281 /* EACCESS is what exec(2) returns. */ 282 error = ENOEXEC; 283 goto cleanup; 284 } 285 286 /* Sensible size? */ 287 if (attr.va_size == 0) { 288 error = ENOEXEC; 289 goto cleanup; 290 } 291 292 /* Can we access it? */ 293 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 294 if (error) 295 goto cleanup; 296 297 /* 298 * XXX: This should use vn_open() so that it is properly authorized, 299 * and to reduce code redundancy all over the place here. 300 * XXX: Not really, it duplicates far more of exec_check_permissions() 301 * than vn_open(). 302 */ 303 #ifdef MAC 304 error = mac_vnode_check_open(td->td_ucred, vp, FREAD); 305 if (error) 306 goto cleanup; 307 #endif 308 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 309 if (error) 310 goto cleanup; 311 312 /* Pull in executable header into kernel_map */ 313 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 314 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 315 if (error) 316 goto cleanup; 317 318 /* Is it a Linux binary ? */ 319 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 320 error = ENOEXEC; 321 goto cleanup; 322 } 323 324 /* 325 * While we are here, we should REALLY do some more checks 326 */ 327 328 /* Set file/virtual offset based on a.out variant. */ 329 switch ((int)(a_out->a_magic & 0xffff)) { 330 case 0413: /* ZMAGIC */ 331 file_offset = 1024; 332 break; 333 case 0314: /* QMAGIC */ 334 file_offset = 0; 335 break; 336 default: 337 error = ENOEXEC; 338 goto cleanup; 339 } 340 341 bss_size = round_page(a_out->a_bss); 342 343 /* Check various fields in header for validity/bounds. */ 344 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 345 error = ENOEXEC; 346 goto cleanup; 347 } 348 349 /* text + data can't exceed file size */ 350 if (a_out->a_data + a_out->a_text > attr.va_size) { 351 error = EFAULT; 352 goto cleanup; 353 } 354 355 /* 356 * text/data/bss must not exceed limits 357 * XXX - this is not complete. it should check current usage PLUS 358 * the resources needed by this library. 359 */ 360 PROC_LOCK(td->td_proc); 361 if (a_out->a_text > maxtsiz || 362 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) { 363 PROC_UNLOCK(td->td_proc); 364 error = ENOMEM; 365 goto cleanup; 366 } 367 PROC_UNLOCK(td->td_proc); 368 369 /* 370 * Prevent more writers. 371 * XXX: Note that if any of the VM operations fail below we don't 372 * clear this flag. 373 */ 374 vp->v_vflag |= VV_TEXT; 375 376 /* 377 * Lock no longer needed 378 */ 379 locked = 0; 380 VOP_UNLOCK(vp, 0); 381 VFS_UNLOCK_GIANT(vfslocked); 382 383 /* 384 * Check if file_offset page aligned. Currently we cannot handle 385 * misalinged file offsets, and so we read in the entire image 386 * (what a waste). 387 */ 388 if (file_offset & PAGE_MASK) { 389 #ifdef DEBUG 390 printf("uselib: Non page aligned binary %lu\n", file_offset); 391 #endif 392 /* Map text+data read/write/execute */ 393 394 /* a_entry is the load address and is page aligned */ 395 vmaddr = trunc_page(a_out->a_entry); 396 397 /* get anon user mapping, read+write+execute */ 398 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 399 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 400 VM_PROT_ALL, 0); 401 if (error) 402 goto cleanup; 403 404 /* map file into kernel_map */ 405 error = vm_mmap(kernel_map, &buffer, 406 round_page(a_out->a_text + a_out->a_data + file_offset), 407 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 408 trunc_page(file_offset)); 409 if (error) 410 goto cleanup; 411 412 /* copy from kernel VM space to user space */ 413 error = copyout(PTRIN(buffer + file_offset), 414 (void *)vmaddr, a_out->a_text + a_out->a_data); 415 416 /* release temporary kernel space */ 417 vm_map_remove(kernel_map, buffer, buffer + 418 round_page(a_out->a_text + a_out->a_data + file_offset)); 419 420 if (error) 421 goto cleanup; 422 } else { 423 #ifdef DEBUG 424 printf("uselib: Page aligned binary %lu\n", file_offset); 425 #endif 426 /* 427 * for QMAGIC, a_entry is 20 bytes beyond the load address 428 * to skip the executable header 429 */ 430 vmaddr = trunc_page(a_out->a_entry); 431 432 /* 433 * Map it all into the process's space as a single 434 * copy-on-write "data" segment. 435 */ 436 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 437 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 438 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 439 if (error) 440 goto cleanup; 441 } 442 #ifdef DEBUG 443 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 444 ((long *)vmaddr)[1]); 445 #endif 446 if (bss_size != 0) { 447 /* Calculate BSS start address */ 448 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 449 a_out->a_data; 450 451 /* allocate some 'anon' space */ 452 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 453 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 454 if (error) 455 goto cleanup; 456 } 457 458 cleanup: 459 /* Unlock vnode if needed */ 460 if (locked) { 461 VOP_UNLOCK(vp, 0); 462 VFS_UNLOCK_GIANT(vfslocked); 463 } 464 465 /* Release the kernel mapping. */ 466 if (a_out) 467 vm_map_remove(kernel_map, (vm_offset_t)a_out, 468 (vm_offset_t)a_out + PAGE_SIZE); 469 470 return error; 471 } 472 473 #endif /* __i386__ */ 474 475 int 476 linux_select(struct thread *td, struct linux_select_args *args) 477 { 478 l_timeval ltv; 479 struct timeval tv0, tv1, utv, *tvp; 480 int error; 481 482 #ifdef DEBUG 483 if (ldebug(select)) 484 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 485 (void *)args->readfds, (void *)args->writefds, 486 (void *)args->exceptfds, (void *)args->timeout); 487 #endif 488 489 /* 490 * Store current time for computation of the amount of 491 * time left. 492 */ 493 if (args->timeout) { 494 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 495 goto select_out; 496 utv.tv_sec = ltv.tv_sec; 497 utv.tv_usec = ltv.tv_usec; 498 #ifdef DEBUG 499 if (ldebug(select)) 500 printf(LMSG("incoming timeout (%jd/%ld)"), 501 (intmax_t)utv.tv_sec, utv.tv_usec); 502 #endif 503 504 if (itimerfix(&utv)) { 505 /* 506 * The timeval was invalid. Convert it to something 507 * valid that will act as it does under Linux. 508 */ 509 utv.tv_sec += utv.tv_usec / 1000000; 510 utv.tv_usec %= 1000000; 511 if (utv.tv_usec < 0) { 512 utv.tv_sec -= 1; 513 utv.tv_usec += 1000000; 514 } 515 if (utv.tv_sec < 0) 516 timevalclear(&utv); 517 } 518 microtime(&tv0); 519 tvp = &utv; 520 } else 521 tvp = NULL; 522 523 error = kern_select(td, args->nfds, args->readfds, args->writefds, 524 args->exceptfds, tvp); 525 526 #ifdef DEBUG 527 if (ldebug(select)) 528 printf(LMSG("real select returns %d"), error); 529 #endif 530 if (error) { 531 /* 532 * See fs/select.c in the Linux kernel. Without this, 533 * Maelstrom doesn't work. 534 */ 535 if (error == ERESTART) 536 error = EINTR; 537 goto select_out; 538 } 539 540 if (args->timeout) { 541 if (td->td_retval[0]) { 542 /* 543 * Compute how much time was left of the timeout, 544 * by subtracting the current time and the time 545 * before we started the call, and subtracting 546 * that result from the user-supplied value. 547 */ 548 microtime(&tv1); 549 timevalsub(&tv1, &tv0); 550 timevalsub(&utv, &tv1); 551 if (utv.tv_sec < 0) 552 timevalclear(&utv); 553 } else 554 timevalclear(&utv); 555 #ifdef DEBUG 556 if (ldebug(select)) 557 printf(LMSG("outgoing timeout (%jd/%ld)"), 558 (intmax_t)utv.tv_sec, utv.tv_usec); 559 #endif 560 ltv.tv_sec = utv.tv_sec; 561 ltv.tv_usec = utv.tv_usec; 562 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 563 goto select_out; 564 } 565 566 select_out: 567 #ifdef DEBUG 568 if (ldebug(select)) 569 printf(LMSG("select_out -> %d"), error); 570 #endif 571 return error; 572 } 573 574 int 575 linux_mremap(struct thread *td, struct linux_mremap_args *args) 576 { 577 struct munmap_args /* { 578 void *addr; 579 size_t len; 580 } */ bsd_args; 581 int error = 0; 582 583 #ifdef DEBUG 584 if (ldebug(mremap)) 585 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 586 (void *)(uintptr_t)args->addr, 587 (unsigned long)args->old_len, 588 (unsigned long)args->new_len, 589 (unsigned long)args->flags); 590 #endif 591 592 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 593 td->td_retval[0] = 0; 594 return (EINVAL); 595 } 596 597 /* 598 * Check for the page alignment. 599 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 600 */ 601 if (args->addr & PAGE_MASK) { 602 td->td_retval[0] = 0; 603 return (EINVAL); 604 } 605 606 args->new_len = round_page(args->new_len); 607 args->old_len = round_page(args->old_len); 608 609 if (args->new_len > args->old_len) { 610 td->td_retval[0] = 0; 611 return ENOMEM; 612 } 613 614 if (args->new_len < args->old_len) { 615 bsd_args.addr = 616 (caddr_t)((uintptr_t)args->addr + args->new_len); 617 bsd_args.len = args->old_len - args->new_len; 618 error = munmap(td, &bsd_args); 619 } 620 621 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 622 return error; 623 } 624 625 #define LINUX_MS_ASYNC 0x0001 626 #define LINUX_MS_INVALIDATE 0x0002 627 #define LINUX_MS_SYNC 0x0004 628 629 int 630 linux_msync(struct thread *td, struct linux_msync_args *args) 631 { 632 struct msync_args bsd_args; 633 634 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 635 bsd_args.len = (uintptr_t)args->len; 636 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 637 638 return msync(td, &bsd_args); 639 } 640 641 int 642 linux_time(struct thread *td, struct linux_time_args *args) 643 { 644 struct timeval tv; 645 l_time_t tm; 646 int error; 647 648 #ifdef DEBUG 649 if (ldebug(time)) 650 printf(ARGS(time, "*")); 651 #endif 652 653 microtime(&tv); 654 tm = tv.tv_sec; 655 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 656 return error; 657 td->td_retval[0] = tm; 658 return 0; 659 } 660 661 struct l_times_argv { 662 l_long tms_utime; 663 l_long tms_stime; 664 l_long tms_cutime; 665 l_long tms_cstime; 666 }; 667 668 #define CLK_TCK 100 /* Linux uses 100 */ 669 670 #define CONVTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 671 672 int 673 linux_times(struct thread *td, struct linux_times_args *args) 674 { 675 struct timeval tv, utime, stime, cutime, cstime; 676 struct l_times_argv tms; 677 struct proc *p; 678 int error; 679 680 #ifdef DEBUG 681 if (ldebug(times)) 682 printf(ARGS(times, "*")); 683 #endif 684 685 if (args->buf != NULL) { 686 p = td->td_proc; 687 PROC_LOCK(p); 688 PROC_SLOCK(p); 689 calcru(p, &utime, &stime); 690 PROC_SUNLOCK(p); 691 calccru(p, &cutime, &cstime); 692 PROC_UNLOCK(p); 693 694 tms.tms_utime = CONVTCK(utime); 695 tms.tms_stime = CONVTCK(stime); 696 697 tms.tms_cutime = CONVTCK(cutime); 698 tms.tms_cstime = CONVTCK(cstime); 699 700 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 701 return error; 702 } 703 704 microuptime(&tv); 705 td->td_retval[0] = (int)CONVTCK(tv); 706 return 0; 707 } 708 709 int 710 linux_newuname(struct thread *td, struct linux_newuname_args *args) 711 { 712 struct l_new_utsname utsname; 713 char osname[LINUX_MAX_UTSNAME]; 714 char osrelease[LINUX_MAX_UTSNAME]; 715 char *p; 716 717 #ifdef DEBUG 718 if (ldebug(newuname)) 719 printf(ARGS(newuname, "*")); 720 #endif 721 722 linux_get_osname(td, osname); 723 linux_get_osrelease(td, osrelease); 724 725 bzero(&utsname, sizeof(utsname)); 726 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 727 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 728 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 729 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 730 for (p = utsname.version; *p != '\0'; ++p) 731 if (*p == '\n') { 732 *p = '\0'; 733 break; 734 } 735 #ifdef __i386__ 736 { 737 const char *class; 738 739 switch (cpu_class) { 740 case CPUCLASS_686: 741 class = "i686"; 742 break; 743 case CPUCLASS_586: 744 class = "i586"; 745 break; 746 case CPUCLASS_486: 747 class = "i486"; 748 break; 749 default: 750 class = "i386"; 751 } 752 strlcpy(utsname.machine, class, LINUX_MAX_UTSNAME); 753 } 754 #elif defined(__amd64__) /* XXX: Linux can change 'personality'. */ 755 #ifdef COMPAT_LINUX32 756 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 757 #else 758 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 759 #endif /* COMPAT_LINUX32 */ 760 #else /* something other than i386 or amd64 - assume we and Linux agree */ 761 strlcpy(utsname.machine, machine, LINUX_MAX_UTSNAME); 762 #endif /* __i386__ */ 763 strlcpy(utsname.domainname, domainname, LINUX_MAX_UTSNAME); 764 765 return (copyout(&utsname, args->buf, sizeof(utsname))); 766 } 767 768 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 769 struct l_utimbuf { 770 l_time_t l_actime; 771 l_time_t l_modtime; 772 }; 773 774 int 775 linux_utime(struct thread *td, struct linux_utime_args *args) 776 { 777 struct timeval tv[2], *tvp; 778 struct l_utimbuf lut; 779 char *fname; 780 int error; 781 782 LCONVPATHEXIST(td, args->fname, &fname); 783 784 #ifdef DEBUG 785 if (ldebug(utime)) 786 printf(ARGS(utime, "%s, *"), fname); 787 #endif 788 789 if (args->times) { 790 if ((error = copyin(args->times, &lut, sizeof lut))) { 791 LFREEPATH(fname); 792 return error; 793 } 794 tv[0].tv_sec = lut.l_actime; 795 tv[0].tv_usec = 0; 796 tv[1].tv_sec = lut.l_modtime; 797 tv[1].tv_usec = 0; 798 tvp = tv; 799 } else 800 tvp = NULL; 801 802 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 803 LFREEPATH(fname); 804 return (error); 805 } 806 807 int 808 linux_utimes(struct thread *td, struct linux_utimes_args *args) 809 { 810 l_timeval ltv[2]; 811 struct timeval tv[2], *tvp = NULL; 812 char *fname; 813 int error; 814 815 LCONVPATHEXIST(td, args->fname, &fname); 816 817 #ifdef DEBUG 818 if (ldebug(utimes)) 819 printf(ARGS(utimes, "%s, *"), fname); 820 #endif 821 822 if (args->tptr != NULL) { 823 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 824 LFREEPATH(fname); 825 return (error); 826 } 827 tv[0].tv_sec = ltv[0].tv_sec; 828 tv[0].tv_usec = ltv[0].tv_usec; 829 tv[1].tv_sec = ltv[1].tv_sec; 830 tv[1].tv_usec = ltv[1].tv_usec; 831 tvp = tv; 832 } 833 834 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 835 LFREEPATH(fname); 836 return (error); 837 } 838 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 839 840 #define __WCLONE 0x80000000 841 842 int 843 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 844 { 845 int error, options, tmpstat; 846 847 #ifdef DEBUG 848 if (ldebug(waitpid)) 849 printf(ARGS(waitpid, "%d, %p, %d"), 850 args->pid, (void *)args->status, args->options); 851 #endif 852 /* 853 * this is necessary because the test in kern_wait doesn't work 854 * because we mess with the options here 855 */ 856 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE)) 857 return (EINVAL); 858 859 options = (args->options & (WNOHANG | WUNTRACED)); 860 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 861 if (args->options & __WCLONE) 862 options |= WLINUXCLONE; 863 864 error = kern_wait(td, args->pid, &tmpstat, options, NULL); 865 if (error) 866 return error; 867 868 if (args->status) { 869 tmpstat &= 0xffff; 870 if (WIFSIGNALED(tmpstat)) 871 tmpstat = (tmpstat & 0xffffff80) | 872 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 873 else if (WIFSTOPPED(tmpstat)) 874 tmpstat = (tmpstat & 0xffff00ff) | 875 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 876 return copyout(&tmpstat, args->status, sizeof(int)); 877 } 878 879 return 0; 880 } 881 882 int 883 linux_wait4(struct thread *td, struct linux_wait4_args *args) 884 { 885 int error, options, tmpstat; 886 struct rusage ru, *rup; 887 struct proc *p; 888 889 #ifdef DEBUG 890 if (ldebug(wait4)) 891 printf(ARGS(wait4, "%d, %p, %d, %p"), 892 args->pid, (void *)args->status, args->options, 893 (void *)args->rusage); 894 #endif 895 896 options = (args->options & (WNOHANG | WUNTRACED)); 897 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 898 if (args->options & __WCLONE) 899 options |= WLINUXCLONE; 900 901 if (args->rusage != NULL) 902 rup = &ru; 903 else 904 rup = NULL; 905 error = kern_wait(td, args->pid, &tmpstat, options, rup); 906 if (error) 907 return error; 908 909 p = td->td_proc; 910 PROC_LOCK(p); 911 sigqueue_delete(&p->p_sigqueue, SIGCHLD); 912 PROC_UNLOCK(p); 913 914 if (args->status) { 915 tmpstat &= 0xffff; 916 if (WIFSIGNALED(tmpstat)) 917 tmpstat = (tmpstat & 0xffffff80) | 918 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 919 else if (WIFSTOPPED(tmpstat)) 920 tmpstat = (tmpstat & 0xffff00ff) | 921 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 922 error = copyout(&tmpstat, args->status, sizeof(int)); 923 } 924 if (args->rusage != NULL && error == 0) 925 error = copyout(&ru, args->rusage, sizeof(ru)); 926 927 return (error); 928 } 929 930 int 931 linux_mknod(struct thread *td, struct linux_mknod_args *args) 932 { 933 char *path; 934 int error; 935 936 LCONVPATHCREAT(td, args->path, &path); 937 938 #ifdef DEBUG 939 if (ldebug(mknod)) 940 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev); 941 #endif 942 943 switch (args->mode & S_IFMT) { 944 case S_IFIFO: 945 case S_IFSOCK: 946 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode); 947 break; 948 949 case S_IFCHR: 950 case S_IFBLK: 951 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode, 952 args->dev); 953 break; 954 955 case S_IFDIR: 956 error = EPERM; 957 break; 958 959 case 0: 960 args->mode |= S_IFREG; 961 /* FALLTHROUGH */ 962 case S_IFREG: 963 error = kern_open(td, path, UIO_SYSSPACE, 964 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 965 break; 966 967 default: 968 error = EINVAL; 969 break; 970 } 971 LFREEPATH(path); 972 return (error); 973 } 974 975 /* 976 * UGH! This is just about the dumbest idea I've ever heard!! 977 */ 978 int 979 linux_personality(struct thread *td, struct linux_personality_args *args) 980 { 981 #ifdef DEBUG 982 if (ldebug(personality)) 983 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 984 #endif 985 if (args->per != 0) 986 return EINVAL; 987 988 /* Yes Jim, it's still a Linux... */ 989 td->td_retval[0] = 0; 990 return 0; 991 } 992 993 struct l_itimerval { 994 l_timeval it_interval; 995 l_timeval it_value; 996 }; 997 998 #define B2L_ITIMERVAL(bip, lip) \ 999 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 1000 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 1001 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 1002 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 1003 1004 int 1005 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 1006 { 1007 int error; 1008 struct l_itimerval ls; 1009 struct itimerval aitv, oitv; 1010 1011 #ifdef DEBUG 1012 if (ldebug(setitimer)) 1013 printf(ARGS(setitimer, "%p, %p"), 1014 (void *)uap->itv, (void *)uap->oitv); 1015 #endif 1016 1017 if (uap->itv == NULL) { 1018 uap->itv = uap->oitv; 1019 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1020 } 1021 1022 error = copyin(uap->itv, &ls, sizeof(ls)); 1023 if (error != 0) 1024 return (error); 1025 B2L_ITIMERVAL(&aitv, &ls); 1026 #ifdef DEBUG 1027 if (ldebug(setitimer)) { 1028 printf("setitimer: value: sec: %jd, usec: %ld\n", 1029 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1030 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1031 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1032 } 1033 #endif 1034 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1035 if (error != 0 || uap->oitv == NULL) 1036 return (error); 1037 B2L_ITIMERVAL(&ls, &oitv); 1038 1039 return (copyout(&ls, uap->oitv, sizeof(ls))); 1040 } 1041 1042 int 1043 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1044 { 1045 int error; 1046 struct l_itimerval ls; 1047 struct itimerval aitv; 1048 1049 #ifdef DEBUG 1050 if (ldebug(getitimer)) 1051 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1052 #endif 1053 error = kern_getitimer(td, uap->which, &aitv); 1054 if (error != 0) 1055 return (error); 1056 B2L_ITIMERVAL(&ls, &aitv); 1057 return (copyout(&ls, uap->itv, sizeof(ls))); 1058 } 1059 1060 int 1061 linux_nice(struct thread *td, struct linux_nice_args *args) 1062 { 1063 struct setpriority_args bsd_args; 1064 1065 bsd_args.which = PRIO_PROCESS; 1066 bsd_args.who = 0; /* current process */ 1067 bsd_args.prio = args->inc; 1068 return setpriority(td, &bsd_args); 1069 } 1070 1071 int 1072 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1073 { 1074 struct ucred *newcred, *oldcred; 1075 l_gid_t linux_gidset[NGROUPS]; 1076 gid_t *bsd_gidset; 1077 int ngrp, error; 1078 struct proc *p; 1079 1080 ngrp = args->gidsetsize; 1081 if (ngrp < 0 || ngrp >= NGROUPS) 1082 return (EINVAL); 1083 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1084 if (error) 1085 return (error); 1086 newcred = crget(); 1087 p = td->td_proc; 1088 PROC_LOCK(p); 1089 oldcred = p->p_ucred; 1090 1091 /* 1092 * cr_groups[0] holds egid. Setting the whole set from 1093 * the supplied set will cause egid to be changed too. 1094 * Keep cr_groups[0] unchanged to prevent that. 1095 */ 1096 1097 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) { 1098 PROC_UNLOCK(p); 1099 crfree(newcred); 1100 return (error); 1101 } 1102 1103 crcopy(newcred, oldcred); 1104 if (ngrp > 0) { 1105 newcred->cr_ngroups = ngrp + 1; 1106 1107 bsd_gidset = newcred->cr_groups; 1108 ngrp--; 1109 while (ngrp >= 0) { 1110 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1111 ngrp--; 1112 } 1113 } else 1114 newcred->cr_ngroups = 1; 1115 1116 setsugid(p); 1117 p->p_ucred = newcred; 1118 PROC_UNLOCK(p); 1119 crfree(oldcred); 1120 return (0); 1121 } 1122 1123 int 1124 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1125 { 1126 struct ucred *cred; 1127 l_gid_t linux_gidset[NGROUPS]; 1128 gid_t *bsd_gidset; 1129 int bsd_gidsetsz, ngrp, error; 1130 1131 cred = td->td_ucred; 1132 bsd_gidset = cred->cr_groups; 1133 bsd_gidsetsz = cred->cr_ngroups - 1; 1134 1135 /* 1136 * cr_groups[0] holds egid. Returning the whole set 1137 * here will cause a duplicate. Exclude cr_groups[0] 1138 * to prevent that. 1139 */ 1140 1141 if ((ngrp = args->gidsetsize) == 0) { 1142 td->td_retval[0] = bsd_gidsetsz; 1143 return (0); 1144 } 1145 1146 if (ngrp < bsd_gidsetsz) 1147 return (EINVAL); 1148 1149 ngrp = 0; 1150 while (ngrp < bsd_gidsetsz) { 1151 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1152 ngrp++; 1153 } 1154 1155 if ((error = copyout(linux_gidset, args->grouplist, 1156 ngrp * sizeof(l_gid_t)))) 1157 return (error); 1158 1159 td->td_retval[0] = ngrp; 1160 return (0); 1161 } 1162 1163 int 1164 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1165 { 1166 struct rlimit bsd_rlim; 1167 struct l_rlimit rlim; 1168 u_int which; 1169 int error; 1170 1171 #ifdef DEBUG 1172 if (ldebug(setrlimit)) 1173 printf(ARGS(setrlimit, "%d, %p"), 1174 args->resource, (void *)args->rlim); 1175 #endif 1176 1177 if (args->resource >= LINUX_RLIM_NLIMITS) 1178 return (EINVAL); 1179 1180 which = linux_to_bsd_resource[args->resource]; 1181 if (which == -1) 1182 return (EINVAL); 1183 1184 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1185 if (error) 1186 return (error); 1187 1188 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1189 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1190 return (kern_setrlimit(td, which, &bsd_rlim)); 1191 } 1192 1193 int 1194 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1195 { 1196 struct l_rlimit rlim; 1197 struct proc *p = td->td_proc; 1198 struct rlimit bsd_rlim; 1199 u_int which; 1200 1201 #ifdef DEBUG 1202 if (ldebug(old_getrlimit)) 1203 printf(ARGS(old_getrlimit, "%d, %p"), 1204 args->resource, (void *)args->rlim); 1205 #endif 1206 1207 if (args->resource >= LINUX_RLIM_NLIMITS) 1208 return (EINVAL); 1209 1210 which = linux_to_bsd_resource[args->resource]; 1211 if (which == -1) 1212 return (EINVAL); 1213 1214 PROC_LOCK(p); 1215 lim_rlimit(p, which, &bsd_rlim); 1216 PROC_UNLOCK(p); 1217 1218 #ifdef COMPAT_LINUX32 1219 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1220 if (rlim.rlim_cur == UINT_MAX) 1221 rlim.rlim_cur = INT_MAX; 1222 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1223 if (rlim.rlim_max == UINT_MAX) 1224 rlim.rlim_max = INT_MAX; 1225 #else 1226 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1227 if (rlim.rlim_cur == ULONG_MAX) 1228 rlim.rlim_cur = LONG_MAX; 1229 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1230 if (rlim.rlim_max == ULONG_MAX) 1231 rlim.rlim_max = LONG_MAX; 1232 #endif 1233 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1234 } 1235 1236 int 1237 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1238 { 1239 struct l_rlimit rlim; 1240 struct proc *p = td->td_proc; 1241 struct rlimit bsd_rlim; 1242 u_int which; 1243 1244 #ifdef DEBUG 1245 if (ldebug(getrlimit)) 1246 printf(ARGS(getrlimit, "%d, %p"), 1247 args->resource, (void *)args->rlim); 1248 #endif 1249 1250 if (args->resource >= LINUX_RLIM_NLIMITS) 1251 return (EINVAL); 1252 1253 which = linux_to_bsd_resource[args->resource]; 1254 if (which == -1) 1255 return (EINVAL); 1256 1257 PROC_LOCK(p); 1258 lim_rlimit(p, which, &bsd_rlim); 1259 PROC_UNLOCK(p); 1260 1261 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1262 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1263 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1264 } 1265 1266 int 1267 linux_sched_setscheduler(struct thread *td, 1268 struct linux_sched_setscheduler_args *args) 1269 { 1270 struct sched_setscheduler_args bsd; 1271 1272 #ifdef DEBUG 1273 if (ldebug(sched_setscheduler)) 1274 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1275 args->pid, args->policy, (const void *)args->param); 1276 #endif 1277 1278 switch (args->policy) { 1279 case LINUX_SCHED_OTHER: 1280 bsd.policy = SCHED_OTHER; 1281 break; 1282 case LINUX_SCHED_FIFO: 1283 bsd.policy = SCHED_FIFO; 1284 break; 1285 case LINUX_SCHED_RR: 1286 bsd.policy = SCHED_RR; 1287 break; 1288 default: 1289 return EINVAL; 1290 } 1291 1292 bsd.pid = args->pid; 1293 bsd.param = (struct sched_param *)args->param; 1294 return sched_setscheduler(td, &bsd); 1295 } 1296 1297 int 1298 linux_sched_getscheduler(struct thread *td, 1299 struct linux_sched_getscheduler_args *args) 1300 { 1301 struct sched_getscheduler_args bsd; 1302 int error; 1303 1304 #ifdef DEBUG 1305 if (ldebug(sched_getscheduler)) 1306 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1307 #endif 1308 1309 bsd.pid = args->pid; 1310 error = sched_getscheduler(td, &bsd); 1311 1312 switch (td->td_retval[0]) { 1313 case SCHED_OTHER: 1314 td->td_retval[0] = LINUX_SCHED_OTHER; 1315 break; 1316 case SCHED_FIFO: 1317 td->td_retval[0] = LINUX_SCHED_FIFO; 1318 break; 1319 case SCHED_RR: 1320 td->td_retval[0] = LINUX_SCHED_RR; 1321 break; 1322 } 1323 1324 return error; 1325 } 1326 1327 int 1328 linux_sched_get_priority_max(struct thread *td, 1329 struct linux_sched_get_priority_max_args *args) 1330 { 1331 struct sched_get_priority_max_args bsd; 1332 1333 #ifdef DEBUG 1334 if (ldebug(sched_get_priority_max)) 1335 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1336 #endif 1337 1338 switch (args->policy) { 1339 case LINUX_SCHED_OTHER: 1340 bsd.policy = SCHED_OTHER; 1341 break; 1342 case LINUX_SCHED_FIFO: 1343 bsd.policy = SCHED_FIFO; 1344 break; 1345 case LINUX_SCHED_RR: 1346 bsd.policy = SCHED_RR; 1347 break; 1348 default: 1349 return EINVAL; 1350 } 1351 return sched_get_priority_max(td, &bsd); 1352 } 1353 1354 int 1355 linux_sched_get_priority_min(struct thread *td, 1356 struct linux_sched_get_priority_min_args *args) 1357 { 1358 struct sched_get_priority_min_args bsd; 1359 1360 #ifdef DEBUG 1361 if (ldebug(sched_get_priority_min)) 1362 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1363 #endif 1364 1365 switch (args->policy) { 1366 case LINUX_SCHED_OTHER: 1367 bsd.policy = SCHED_OTHER; 1368 break; 1369 case LINUX_SCHED_FIFO: 1370 bsd.policy = SCHED_FIFO; 1371 break; 1372 case LINUX_SCHED_RR: 1373 bsd.policy = SCHED_RR; 1374 break; 1375 default: 1376 return EINVAL; 1377 } 1378 return sched_get_priority_min(td, &bsd); 1379 } 1380 1381 #define REBOOT_CAD_ON 0x89abcdef 1382 #define REBOOT_CAD_OFF 0 1383 #define REBOOT_HALT 0xcdef0123 1384 #define REBOOT_RESTART 0x01234567 1385 #define REBOOT_RESTART2 0xA1B2C3D4 1386 #define REBOOT_POWEROFF 0x4321FEDC 1387 #define REBOOT_MAGIC1 0xfee1dead 1388 #define REBOOT_MAGIC2 0x28121969 1389 #define REBOOT_MAGIC2A 0x05121996 1390 #define REBOOT_MAGIC2B 0x16041998 1391 1392 int 1393 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1394 { 1395 struct reboot_args bsd_args; 1396 1397 #ifdef DEBUG 1398 if (ldebug(reboot)) 1399 printf(ARGS(reboot, "0x%x"), args->cmd); 1400 #endif 1401 1402 if (args->magic1 != REBOOT_MAGIC1) 1403 return EINVAL; 1404 1405 switch (args->magic2) { 1406 case REBOOT_MAGIC2: 1407 case REBOOT_MAGIC2A: 1408 case REBOOT_MAGIC2B: 1409 break; 1410 default: 1411 return EINVAL; 1412 } 1413 1414 switch (args->cmd) { 1415 case REBOOT_CAD_ON: 1416 case REBOOT_CAD_OFF: 1417 return (priv_check(td, PRIV_REBOOT)); 1418 case REBOOT_HALT: 1419 bsd_args.opt = RB_HALT; 1420 break; 1421 case REBOOT_RESTART: 1422 case REBOOT_RESTART2: 1423 bsd_args.opt = 0; 1424 break; 1425 case REBOOT_POWEROFF: 1426 bsd_args.opt = RB_POWEROFF; 1427 break; 1428 default: 1429 return EINVAL; 1430 } 1431 return reboot(td, &bsd_args); 1432 } 1433 1434 1435 /* 1436 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1437 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that 1438 * are assumed to be preserved. The following lightweight syscalls fixes 1439 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c 1440 * 1441 * linux_getpid() - MP SAFE 1442 * linux_getgid() - MP SAFE 1443 * linux_getuid() - MP SAFE 1444 */ 1445 1446 int 1447 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1448 { 1449 struct linux_emuldata *em; 1450 1451 #ifdef DEBUG 1452 if (ldebug(getpid)) 1453 printf(ARGS(getpid, "")); 1454 #endif 1455 1456 if (linux_use26(td)) { 1457 em = em_find(td->td_proc, EMUL_DONTLOCK); 1458 KASSERT(em != NULL, ("getpid: emuldata not found.\n")); 1459 td->td_retval[0] = em->shared->group_pid; 1460 } else { 1461 td->td_retval[0] = td->td_proc->p_pid; 1462 } 1463 1464 return (0); 1465 } 1466 1467 int 1468 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1469 { 1470 1471 #ifdef DEBUG 1472 if (ldebug(gettid)) 1473 printf(ARGS(gettid, "")); 1474 #endif 1475 1476 td->td_retval[0] = td->td_proc->p_pid; 1477 return (0); 1478 } 1479 1480 1481 int 1482 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1483 { 1484 struct linux_emuldata *em; 1485 struct proc *p, *pp; 1486 1487 #ifdef DEBUG 1488 if (ldebug(getppid)) 1489 printf(ARGS(getppid, "")); 1490 #endif 1491 1492 if (!linux_use26(td)) { 1493 PROC_LOCK(td->td_proc); 1494 td->td_retval[0] = td->td_proc->p_pptr->p_pid; 1495 PROC_UNLOCK(td->td_proc); 1496 return (0); 1497 } 1498 1499 em = em_find(td->td_proc, EMUL_DONTLOCK); 1500 1501 KASSERT(em != NULL, ("getppid: process emuldata not found.\n")); 1502 1503 /* find the group leader */ 1504 p = pfind(em->shared->group_pid); 1505 1506 if (p == NULL) { 1507 #ifdef DEBUG 1508 printf(LMSG("parent process not found.\n")); 1509 #endif 1510 return (0); 1511 } 1512 1513 pp = p->p_pptr; /* switch to parent */ 1514 PROC_LOCK(pp); 1515 PROC_UNLOCK(p); 1516 1517 /* if its also linux process */ 1518 if (pp->p_sysent == &elf_linux_sysvec) { 1519 em = em_find(pp, EMUL_DONTLOCK); 1520 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n")); 1521 1522 td->td_retval[0] = em->shared->group_pid; 1523 } else 1524 td->td_retval[0] = pp->p_pid; 1525 1526 PROC_UNLOCK(pp); 1527 1528 return (0); 1529 } 1530 1531 int 1532 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1533 { 1534 1535 #ifdef DEBUG 1536 if (ldebug(getgid)) 1537 printf(ARGS(getgid, "")); 1538 #endif 1539 1540 td->td_retval[0] = td->td_ucred->cr_rgid; 1541 return (0); 1542 } 1543 1544 int 1545 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1546 { 1547 1548 #ifdef DEBUG 1549 if (ldebug(getuid)) 1550 printf(ARGS(getuid, "")); 1551 #endif 1552 1553 td->td_retval[0] = td->td_ucred->cr_ruid; 1554 return (0); 1555 } 1556 1557 1558 int 1559 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1560 { 1561 struct getsid_args bsd; 1562 1563 #ifdef DEBUG 1564 if (ldebug(getsid)) 1565 printf(ARGS(getsid, "%i"), args->pid); 1566 #endif 1567 1568 bsd.pid = args->pid; 1569 return getsid(td, &bsd); 1570 } 1571 1572 int 1573 linux_nosys(struct thread *td, struct nosys_args *ignore) 1574 { 1575 1576 return (ENOSYS); 1577 } 1578 1579 int 1580 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1581 { 1582 struct getpriority_args bsd_args; 1583 int error; 1584 1585 #ifdef DEBUG 1586 if (ldebug(getpriority)) 1587 printf(ARGS(getpriority, "%i, %i"), args->which, args->who); 1588 #endif 1589 1590 bsd_args.which = args->which; 1591 bsd_args.who = args->who; 1592 error = getpriority(td, &bsd_args); 1593 td->td_retval[0] = 20 - td->td_retval[0]; 1594 return error; 1595 } 1596 1597 int 1598 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1599 { 1600 int name[2]; 1601 1602 #ifdef DEBUG 1603 if (ldebug(sethostname)) 1604 printf(ARGS(sethostname, "*, %i"), args->len); 1605 #endif 1606 1607 name[0] = CTL_KERN; 1608 name[1] = KERN_HOSTNAME; 1609 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1610 args->len, 0, 0)); 1611 } 1612 1613 int 1614 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1615 { 1616 struct linux_emuldata *em, *td_em, *tmp_em; 1617 struct proc *sp; 1618 1619 #ifdef DEBUG 1620 if (ldebug(exit_group)) 1621 printf(ARGS(exit_group, "%i"), args->error_code); 1622 #endif 1623 1624 if (linux_use26(td)) { 1625 td_em = em_find(td->td_proc, EMUL_DONTLOCK); 1626 1627 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n")); 1628 1629 EMUL_SHARED_RLOCK(&emul_shared_lock); 1630 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) { 1631 if (em->pid == td_em->pid) 1632 continue; 1633 1634 sp = pfind(em->pid); 1635 psignal(sp, SIGKILL); 1636 PROC_UNLOCK(sp); 1637 #ifdef DEBUG 1638 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid); 1639 #endif 1640 } 1641 1642 EMUL_SHARED_RUNLOCK(&emul_shared_lock); 1643 } 1644 /* 1645 * XXX: we should send a signal to the parent if 1646 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1647 * as it doesnt occur often. 1648 */ 1649 exit1(td, W_EXITCODE(args->error_code, 0)); 1650 1651 return (0); 1652 } 1653 1654 int 1655 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1656 { 1657 int error = 0, max_size; 1658 struct proc *p = td->td_proc; 1659 char comm[LINUX_MAX_COMM_LEN]; 1660 struct linux_emuldata *em; 1661 int pdeath_signal; 1662 1663 #ifdef DEBUG 1664 if (ldebug(prctl)) 1665 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option, 1666 args->arg2, args->arg3, args->arg4, args->arg5); 1667 #endif 1668 1669 switch (args->option) { 1670 case LINUX_PR_SET_PDEATHSIG: 1671 if (!LINUX_SIG_VALID(args->arg2)) 1672 return (EINVAL); 1673 em = em_find(p, EMUL_DOLOCK); 1674 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1675 em->pdeath_signal = args->arg2; 1676 EMUL_UNLOCK(&emul_lock); 1677 break; 1678 case LINUX_PR_GET_PDEATHSIG: 1679 em = em_find(p, EMUL_DOLOCK); 1680 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1681 pdeath_signal = em->pdeath_signal; 1682 EMUL_UNLOCK(&emul_lock); 1683 error = copyout(&pdeath_signal, 1684 (void *)(register_t)args->arg2, 1685 sizeof(pdeath_signal)); 1686 break; 1687 case LINUX_PR_SET_NAME: 1688 /* 1689 * To be on the safe side we need to make sure to not 1690 * overflow the size a linux program expects. We already 1691 * do this here in the copyin, so that we don't need to 1692 * check on copyout. 1693 */ 1694 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1695 error = copyinstr((void *)(register_t)args->arg2, comm, 1696 max_size, NULL); 1697 1698 /* Linux silently truncates the name if it is too long. */ 1699 if (error == ENAMETOOLONG) { 1700 /* 1701 * XXX: copyinstr() isn't documented to populate the 1702 * array completely, so do a copyin() to be on the 1703 * safe side. This should be changed in case 1704 * copyinstr() is changed to guarantee this. 1705 */ 1706 error = copyin((void *)(register_t)args->arg2, comm, 1707 max_size - 1); 1708 comm[max_size - 1] = '\0'; 1709 } 1710 if (error) 1711 return (error); 1712 1713 PROC_LOCK(p); 1714 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 1715 PROC_UNLOCK(p); 1716 break; 1717 case LINUX_PR_GET_NAME: 1718 PROC_LOCK(p); 1719 strlcpy(comm, p->p_comm, sizeof(comm)); 1720 PROC_UNLOCK(p); 1721 error = copyout(comm, (void *)(register_t)args->arg2, 1722 strlen(comm) + 1); 1723 break; 1724 default: 1725 error = EINVAL; 1726 break; 1727 } 1728 1729 return (error); 1730 } 1731 1732 /* 1733 * XXX: fake one.. waiting for real implementation of affinity mask. 1734 */ 1735 int 1736 linux_sched_getaffinity(struct thread *td, 1737 struct linux_sched_getaffinity_args *args) 1738 { 1739 int error; 1740 cpumask_t i = ~0; 1741 1742 if (args->len < sizeof(cpumask_t)) 1743 return (EINVAL); 1744 1745 error = copyout(&i, args->user_mask_ptr, sizeof(cpumask_t)); 1746 if (error) 1747 return (EFAULT); 1748 1749 td->td_retval[0] = sizeof(cpumask_t); 1750 return (0); 1751 } 1752