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