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 35 #include <sys/param.h> 36 #include <sys/blist.h> 37 #include <sys/fcntl.h> 38 #if defined(__i386__) 39 #include <sys/imgact_aout.h> 40 #endif 41 #include <sys/jail.h> 42 #include <sys/kernel.h> 43 #include <sys/limits.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mman.h> 47 #include <sys/mount.h> 48 #include <sys/mutex.h> 49 #include <sys/namei.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/reboot.h> 53 #include <sys/racct.h> 54 #include <sys/resourcevar.h> 55 #include <sys/sched.h> 56 #include <sys/sdt.h> 57 #include <sys/signalvar.h> 58 #include <sys/stat.h> 59 #include <sys/syscallsubr.h> 60 #include <sys/sysctl.h> 61 #include <sys/sysproto.h> 62 #include <sys/systm.h> 63 #include <sys/time.h> 64 #include <sys/vmmeter.h> 65 #include <sys/vnode.h> 66 #include <sys/wait.h> 67 #include <sys/cpuset.h> 68 69 #include <security/mac/mac_framework.h> 70 71 #include <vm/vm.h> 72 #include <vm/pmap.h> 73 #include <vm/vm_kern.h> 74 #include <vm/vm_map.h> 75 #include <vm/vm_extern.h> 76 #include <vm/vm_object.h> 77 #include <vm/swap_pager.h> 78 79 #ifdef COMPAT_LINUX32 80 #include <machine/../linux32/linux.h> 81 #include <machine/../linux32/linux32_proto.h> 82 #else 83 #include <machine/../linux/linux.h> 84 #include <machine/../linux/linux_proto.h> 85 #endif 86 87 #include <compat/linux/linux_dtrace.h> 88 #include <compat/linux/linux_file.h> 89 #include <compat/linux/linux_mib.h> 90 #include <compat/linux/linux_signal.h> 91 #include <compat/linux/linux_timer.h> 92 #include <compat/linux/linux_util.h> 93 #include <compat/linux/linux_sysproto.h> 94 #include <compat/linux/linux_emul.h> 95 #include <compat/linux/linux_misc.h> 96 97 /** 98 * Special DTrace provider for the linuxulator. 99 * 100 * In this file we define the provider for the entire linuxulator. All 101 * modules (= files of the linuxulator) use it. 102 * 103 * We define a different name depending on the emulated bitsize, see 104 * ../../<ARCH>/linux{,32}/linux.h, e.g.: 105 * native bitsize = linuxulator 106 * amd64, 32bit emulation = linuxulator32 107 */ 108 LIN_SDT_PROVIDER_DEFINE(LINUX_DTRACE); 109 110 int stclohz; /* Statistics clock frequency */ 111 112 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 113 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 114 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 115 RLIMIT_MEMLOCK, RLIMIT_AS 116 }; 117 118 struct l_sysinfo { 119 l_long uptime; /* Seconds since boot */ 120 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 121 #define LINUX_SYSINFO_LOADS_SCALE 65536 122 l_ulong totalram; /* Total usable main memory size */ 123 l_ulong freeram; /* Available memory size */ 124 l_ulong sharedram; /* Amount of shared memory */ 125 l_ulong bufferram; /* Memory used by buffers */ 126 l_ulong totalswap; /* Total swap space size */ 127 l_ulong freeswap; /* swap space still available */ 128 l_ushort procs; /* Number of current processes */ 129 l_ushort pads; 130 l_ulong totalbig; 131 l_ulong freebig; 132 l_uint mem_unit; 133 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 134 }; 135 136 struct l_pselect6arg { 137 l_uintptr_t ss; 138 l_size_t ss_len; 139 }; 140 141 static int linux_utimensat_nsec_valid(l_long); 142 143 144 int 145 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 146 { 147 struct l_sysinfo sysinfo; 148 vm_object_t object; 149 int i, j; 150 struct timespec ts; 151 152 getnanouptime(&ts); 153 if (ts.tv_nsec != 0) 154 ts.tv_sec++; 155 sysinfo.uptime = ts.tv_sec; 156 157 /* Use the information from the mib to get our load averages */ 158 for (i = 0; i < 3; i++) 159 sysinfo.loads[i] = averunnable.ldavg[i] * 160 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 161 162 sysinfo.totalram = physmem * PAGE_SIZE; 163 sysinfo.freeram = sysinfo.totalram - vm_cnt.v_wire_count * PAGE_SIZE; 164 165 sysinfo.sharedram = 0; 166 mtx_lock(&vm_object_list_mtx); 167 TAILQ_FOREACH(object, &vm_object_list, object_list) 168 if (object->shadow_count > 1) 169 sysinfo.sharedram += object->resident_page_count; 170 mtx_unlock(&vm_object_list_mtx); 171 172 sysinfo.sharedram *= PAGE_SIZE; 173 sysinfo.bufferram = 0; 174 175 swap_pager_status(&i, &j); 176 sysinfo.totalswap = i * PAGE_SIZE; 177 sysinfo.freeswap = (i - j) * PAGE_SIZE; 178 179 sysinfo.procs = nprocs; 180 181 /* The following are only present in newer Linux kernels. */ 182 sysinfo.totalbig = 0; 183 sysinfo.freebig = 0; 184 sysinfo.mem_unit = 1; 185 186 return (copyout(&sysinfo, args->info, sizeof(sysinfo))); 187 } 188 189 int 190 linux_alarm(struct thread *td, struct linux_alarm_args *args) 191 { 192 struct itimerval it, old_it; 193 u_int secs; 194 int error; 195 196 #ifdef DEBUG 197 if (ldebug(alarm)) 198 printf(ARGS(alarm, "%u"), args->secs); 199 #endif 200 201 secs = args->secs; 202 203 if (secs > INT_MAX) 204 secs = INT_MAX; 205 206 it.it_value.tv_sec = (long) secs; 207 it.it_value.tv_usec = 0; 208 it.it_interval.tv_sec = 0; 209 it.it_interval.tv_usec = 0; 210 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 211 if (error) 212 return (error); 213 if (timevalisset(&old_it.it_value)) { 214 if (old_it.it_value.tv_usec != 0) 215 old_it.it_value.tv_sec++; 216 td->td_retval[0] = old_it.it_value.tv_sec; 217 } 218 return (0); 219 } 220 221 int 222 linux_brk(struct thread *td, struct linux_brk_args *args) 223 { 224 struct vmspace *vm = td->td_proc->p_vmspace; 225 vm_offset_t new, old; 226 struct obreak_args /* { 227 char * nsize; 228 } */ tmp; 229 230 #ifdef DEBUG 231 if (ldebug(brk)) 232 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 233 #endif 234 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 235 new = (vm_offset_t)args->dsend; 236 tmp.nsize = (char *)new; 237 if (((caddr_t)new > vm->vm_daddr) && !sys_obreak(td, &tmp)) 238 td->td_retval[0] = (long)new; 239 else 240 td->td_retval[0] = (long)old; 241 242 return (0); 243 } 244 245 #if defined(__i386__) 246 /* XXX: what about amd64/linux32? */ 247 248 int 249 linux_uselib(struct thread *td, struct linux_uselib_args *args) 250 { 251 struct nameidata ni; 252 struct vnode *vp; 253 struct exec *a_out; 254 struct vattr attr; 255 vm_offset_t vmaddr; 256 unsigned long file_offset; 257 unsigned long bss_size; 258 char *library; 259 ssize_t aresid; 260 int error, locked, writecount; 261 262 LCONVPATHEXIST(td, args->library, &library); 263 264 #ifdef DEBUG 265 if (ldebug(uselib)) 266 printf(ARGS(uselib, "%s"), library); 267 #endif 268 269 a_out = NULL; 270 locked = 0; 271 vp = NULL; 272 273 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | AUDITVNODE1, 274 UIO_SYSSPACE, library, td); 275 error = namei(&ni); 276 LFREEPATH(library); 277 if (error) 278 goto cleanup; 279 280 vp = ni.ni_vp; 281 NDFREE(&ni, NDF_ONLY_PNBUF); 282 283 /* 284 * From here on down, we have a locked vnode that must be unlocked. 285 * XXX: The code below largely duplicates exec_check_permissions(). 286 */ 287 locked = 1; 288 289 /* Writable? */ 290 error = VOP_GET_WRITECOUNT(vp, &writecount); 291 if (error != 0) 292 goto cleanup; 293 if (writecount != 0) { 294 error = ETXTBSY; 295 goto cleanup; 296 } 297 298 /* Executable? */ 299 error = VOP_GETATTR(vp, &attr, td->td_ucred); 300 if (error) 301 goto cleanup; 302 303 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 304 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 305 /* EACCESS is what exec(2) returns. */ 306 error = ENOEXEC; 307 goto cleanup; 308 } 309 310 /* Sensible size? */ 311 if (attr.va_size == 0) { 312 error = ENOEXEC; 313 goto cleanup; 314 } 315 316 /* Can we access it? */ 317 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 318 if (error) 319 goto cleanup; 320 321 /* 322 * XXX: This should use vn_open() so that it is properly authorized, 323 * and to reduce code redundancy all over the place here. 324 * XXX: Not really, it duplicates far more of exec_check_permissions() 325 * than vn_open(). 326 */ 327 #ifdef MAC 328 error = mac_vnode_check_open(td->td_ucred, vp, VREAD); 329 if (error) 330 goto cleanup; 331 #endif 332 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 333 if (error) 334 goto cleanup; 335 336 /* Pull in executable header into exec_map */ 337 error = vm_mmap(exec_map, (vm_offset_t *)&a_out, PAGE_SIZE, 338 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 339 if (error) 340 goto cleanup; 341 342 /* Is it a Linux binary ? */ 343 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 344 error = ENOEXEC; 345 goto cleanup; 346 } 347 348 /* 349 * While we are here, we should REALLY do some more checks 350 */ 351 352 /* Set file/virtual offset based on a.out variant. */ 353 switch ((int)(a_out->a_magic & 0xffff)) { 354 case 0413: /* ZMAGIC */ 355 file_offset = 1024; 356 break; 357 case 0314: /* QMAGIC */ 358 file_offset = 0; 359 break; 360 default: 361 error = ENOEXEC; 362 goto cleanup; 363 } 364 365 bss_size = round_page(a_out->a_bss); 366 367 /* Check various fields in header for validity/bounds. */ 368 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 369 error = ENOEXEC; 370 goto cleanup; 371 } 372 373 /* text + data can't exceed file size */ 374 if (a_out->a_data + a_out->a_text > attr.va_size) { 375 error = EFAULT; 376 goto cleanup; 377 } 378 379 /* 380 * text/data/bss must not exceed limits 381 * XXX - this is not complete. it should check current usage PLUS 382 * the resources needed by this library. 383 */ 384 PROC_LOCK(td->td_proc); 385 if (a_out->a_text > maxtsiz || 386 a_out->a_data + bss_size > lim_cur_proc(td->td_proc, RLIMIT_DATA) || 387 racct_set(td->td_proc, RACCT_DATA, a_out->a_data + 388 bss_size) != 0) { 389 PROC_UNLOCK(td->td_proc); 390 error = ENOMEM; 391 goto cleanup; 392 } 393 PROC_UNLOCK(td->td_proc); 394 395 /* 396 * Prevent more writers. 397 * XXX: Note that if any of the VM operations fail below we don't 398 * clear this flag. 399 */ 400 VOP_SET_TEXT(vp); 401 402 /* 403 * Lock no longer needed 404 */ 405 locked = 0; 406 VOP_UNLOCK(vp, 0); 407 408 /* 409 * Check if file_offset page aligned. Currently we cannot handle 410 * misalinged file offsets, and so we read in the entire image 411 * (what a waste). 412 */ 413 if (file_offset & PAGE_MASK) { 414 #ifdef DEBUG 415 printf("uselib: Non page aligned binary %lu\n", file_offset); 416 #endif 417 /* Map text+data read/write/execute */ 418 419 /* a_entry is the load address and is page aligned */ 420 vmaddr = trunc_page(a_out->a_entry); 421 422 /* get anon user mapping, read+write+execute */ 423 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 424 &vmaddr, a_out->a_text + a_out->a_data, 0, VMFS_NO_SPACE, 425 VM_PROT_ALL, VM_PROT_ALL, 0); 426 if (error) 427 goto cleanup; 428 429 error = vn_rdwr(UIO_READ, vp, (void *)vmaddr, file_offset, 430 a_out->a_text + a_out->a_data, UIO_USERSPACE, 0, 431 td->td_ucred, NOCRED, &aresid, td); 432 if (error != 0) 433 goto cleanup; 434 if (aresid != 0) { 435 error = ENOEXEC; 436 goto cleanup; 437 } 438 } else { 439 #ifdef DEBUG 440 printf("uselib: Page aligned binary %lu\n", file_offset); 441 #endif 442 /* 443 * for QMAGIC, a_entry is 20 bytes beyond the load address 444 * to skip the executable header 445 */ 446 vmaddr = trunc_page(a_out->a_entry); 447 448 /* 449 * Map it all into the process's space as a single 450 * copy-on-write "data" segment. 451 */ 452 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 453 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 454 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 455 if (error) 456 goto cleanup; 457 } 458 #ifdef DEBUG 459 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 460 ((long *)vmaddr)[1]); 461 #endif 462 if (bss_size != 0) { 463 /* Calculate BSS start address */ 464 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 465 a_out->a_data; 466 467 /* allocate some 'anon' space */ 468 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 469 &vmaddr, bss_size, 0, VMFS_NO_SPACE, VM_PROT_ALL, 470 VM_PROT_ALL, 0); 471 if (error) 472 goto cleanup; 473 } 474 475 cleanup: 476 /* Unlock vnode if needed */ 477 if (locked) 478 VOP_UNLOCK(vp, 0); 479 480 /* Release the temporary mapping. */ 481 if (a_out) 482 kmap_free_wakeup(exec_map, (vm_offset_t)a_out, PAGE_SIZE); 483 484 return (error); 485 } 486 487 #endif /* __i386__ */ 488 489 int 490 linux_select(struct thread *td, struct linux_select_args *args) 491 { 492 l_timeval ltv; 493 struct timeval tv0, tv1, utv, *tvp; 494 int error; 495 496 #ifdef DEBUG 497 if (ldebug(select)) 498 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 499 (void *)args->readfds, (void *)args->writefds, 500 (void *)args->exceptfds, (void *)args->timeout); 501 #endif 502 503 /* 504 * Store current time for computation of the amount of 505 * time left. 506 */ 507 if (args->timeout) { 508 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 509 goto select_out; 510 utv.tv_sec = ltv.tv_sec; 511 utv.tv_usec = ltv.tv_usec; 512 #ifdef DEBUG 513 if (ldebug(select)) 514 printf(LMSG("incoming timeout (%jd/%ld)"), 515 (intmax_t)utv.tv_sec, utv.tv_usec); 516 #endif 517 518 if (itimerfix(&utv)) { 519 /* 520 * The timeval was invalid. Convert it to something 521 * valid that will act as it does under Linux. 522 */ 523 utv.tv_sec += utv.tv_usec / 1000000; 524 utv.tv_usec %= 1000000; 525 if (utv.tv_usec < 0) { 526 utv.tv_sec -= 1; 527 utv.tv_usec += 1000000; 528 } 529 if (utv.tv_sec < 0) 530 timevalclear(&utv); 531 } 532 microtime(&tv0); 533 tvp = &utv; 534 } else 535 tvp = NULL; 536 537 error = kern_select(td, args->nfds, args->readfds, args->writefds, 538 args->exceptfds, tvp, LINUX_NFDBITS); 539 540 #ifdef DEBUG 541 if (ldebug(select)) 542 printf(LMSG("real select returns %d"), error); 543 #endif 544 if (error) 545 goto select_out; 546 547 if (args->timeout) { 548 if (td->td_retval[0]) { 549 /* 550 * Compute how much time was left of the timeout, 551 * by subtracting the current time and the time 552 * before we started the call, and subtracting 553 * that result from the user-supplied value. 554 */ 555 microtime(&tv1); 556 timevalsub(&tv1, &tv0); 557 timevalsub(&utv, &tv1); 558 if (utv.tv_sec < 0) 559 timevalclear(&utv); 560 } else 561 timevalclear(&utv); 562 #ifdef DEBUG 563 if (ldebug(select)) 564 printf(LMSG("outgoing timeout (%jd/%ld)"), 565 (intmax_t)utv.tv_sec, utv.tv_usec); 566 #endif 567 ltv.tv_sec = utv.tv_sec; 568 ltv.tv_usec = utv.tv_usec; 569 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 570 goto select_out; 571 } 572 573 select_out: 574 #ifdef DEBUG 575 if (ldebug(select)) 576 printf(LMSG("select_out -> %d"), error); 577 #endif 578 return (error); 579 } 580 581 int 582 linux_mremap(struct thread *td, struct linux_mremap_args *args) 583 { 584 struct munmap_args /* { 585 void *addr; 586 size_t len; 587 } */ bsd_args; 588 int error = 0; 589 590 #ifdef DEBUG 591 if (ldebug(mremap)) 592 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 593 (void *)(uintptr_t)args->addr, 594 (unsigned long)args->old_len, 595 (unsigned long)args->new_len, 596 (unsigned long)args->flags); 597 #endif 598 599 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 600 td->td_retval[0] = 0; 601 return (EINVAL); 602 } 603 604 /* 605 * Check for the page alignment. 606 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 607 */ 608 if (args->addr & PAGE_MASK) { 609 td->td_retval[0] = 0; 610 return (EINVAL); 611 } 612 613 args->new_len = round_page(args->new_len); 614 args->old_len = round_page(args->old_len); 615 616 if (args->new_len > args->old_len) { 617 td->td_retval[0] = 0; 618 return (ENOMEM); 619 } 620 621 if (args->new_len < args->old_len) { 622 bsd_args.addr = 623 (caddr_t)((uintptr_t)args->addr + args->new_len); 624 bsd_args.len = args->old_len - args->new_len; 625 error = sys_munmap(td, &bsd_args); 626 } 627 628 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 629 return (error); 630 } 631 632 #define LINUX_MS_ASYNC 0x0001 633 #define LINUX_MS_INVALIDATE 0x0002 634 #define LINUX_MS_SYNC 0x0004 635 636 int 637 linux_msync(struct thread *td, struct linux_msync_args *args) 638 { 639 struct msync_args bsd_args; 640 641 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 642 bsd_args.len = (uintptr_t)args->len; 643 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 644 645 return (sys_msync(td, &bsd_args)); 646 } 647 648 int 649 linux_time(struct thread *td, struct linux_time_args *args) 650 { 651 struct timeval tv; 652 l_time_t tm; 653 int error; 654 655 #ifdef DEBUG 656 if (ldebug(time)) 657 printf(ARGS(time, "*")); 658 #endif 659 660 microtime(&tv); 661 tm = tv.tv_sec; 662 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 663 return (error); 664 td->td_retval[0] = tm; 665 return (0); 666 } 667 668 struct l_times_argv { 669 l_clock_t tms_utime; 670 l_clock_t tms_stime; 671 l_clock_t tms_cutime; 672 l_clock_t tms_cstime; 673 }; 674 675 676 /* 677 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value. 678 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK 679 * auxiliary vector entry. 680 */ 681 #define CLK_TCK 100 682 683 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 684 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) 685 686 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \ 687 CONVNTCK(r) : CONVOTCK(r)) 688 689 int 690 linux_times(struct thread *td, struct linux_times_args *args) 691 { 692 struct timeval tv, utime, stime, cutime, cstime; 693 struct l_times_argv tms; 694 struct proc *p; 695 int error; 696 697 #ifdef DEBUG 698 if (ldebug(times)) 699 printf(ARGS(times, "*")); 700 #endif 701 702 if (args->buf != NULL) { 703 p = td->td_proc; 704 PROC_LOCK(p); 705 PROC_STATLOCK(p); 706 calcru(p, &utime, &stime); 707 PROC_STATUNLOCK(p); 708 calccru(p, &cutime, &cstime); 709 PROC_UNLOCK(p); 710 711 tms.tms_utime = CONVTCK(utime); 712 tms.tms_stime = CONVTCK(stime); 713 714 tms.tms_cutime = CONVTCK(cutime); 715 tms.tms_cstime = CONVTCK(cstime); 716 717 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 718 return (error); 719 } 720 721 microuptime(&tv); 722 td->td_retval[0] = (int)CONVTCK(tv); 723 return (0); 724 } 725 726 int 727 linux_newuname(struct thread *td, struct linux_newuname_args *args) 728 { 729 struct l_new_utsname utsname; 730 char osname[LINUX_MAX_UTSNAME]; 731 char osrelease[LINUX_MAX_UTSNAME]; 732 char *p; 733 734 #ifdef DEBUG 735 if (ldebug(newuname)) 736 printf(ARGS(newuname, "*")); 737 #endif 738 739 linux_get_osname(td, osname); 740 linux_get_osrelease(td, osrelease); 741 742 bzero(&utsname, sizeof(utsname)); 743 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 744 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 745 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME); 746 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 747 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 748 for (p = utsname.version; *p != '\0'; ++p) 749 if (*p == '\n') { 750 *p = '\0'; 751 break; 752 } 753 strlcpy(utsname.machine, linux_kplatform, LINUX_MAX_UTSNAME); 754 755 return (copyout(&utsname, args->buf, sizeof(utsname))); 756 } 757 758 struct l_utimbuf { 759 l_time_t l_actime; 760 l_time_t l_modtime; 761 }; 762 763 int 764 linux_utime(struct thread *td, struct linux_utime_args *args) 765 { 766 struct timeval tv[2], *tvp; 767 struct l_utimbuf lut; 768 char *fname; 769 int error; 770 771 LCONVPATHEXIST(td, args->fname, &fname); 772 773 #ifdef DEBUG 774 if (ldebug(utime)) 775 printf(ARGS(utime, "%s, *"), fname); 776 #endif 777 778 if (args->times) { 779 if ((error = copyin(args->times, &lut, sizeof lut))) { 780 LFREEPATH(fname); 781 return (error); 782 } 783 tv[0].tv_sec = lut.l_actime; 784 tv[0].tv_usec = 0; 785 tv[1].tv_sec = lut.l_modtime; 786 tv[1].tv_usec = 0; 787 tvp = tv; 788 } else 789 tvp = NULL; 790 791 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, tvp, 792 UIO_SYSSPACE); 793 LFREEPATH(fname); 794 return (error); 795 } 796 797 int 798 linux_utimes(struct thread *td, struct linux_utimes_args *args) 799 { 800 l_timeval ltv[2]; 801 struct timeval tv[2], *tvp = NULL; 802 char *fname; 803 int error; 804 805 LCONVPATHEXIST(td, args->fname, &fname); 806 807 #ifdef DEBUG 808 if (ldebug(utimes)) 809 printf(ARGS(utimes, "%s, *"), fname); 810 #endif 811 812 if (args->tptr != NULL) { 813 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 814 LFREEPATH(fname); 815 return (error); 816 } 817 tv[0].tv_sec = ltv[0].tv_sec; 818 tv[0].tv_usec = ltv[0].tv_usec; 819 tv[1].tv_sec = ltv[1].tv_sec; 820 tv[1].tv_usec = ltv[1].tv_usec; 821 tvp = tv; 822 } 823 824 error = kern_utimesat(td, AT_FDCWD, fname, UIO_SYSSPACE, 825 tvp, UIO_SYSSPACE); 826 LFREEPATH(fname); 827 return (error); 828 } 829 830 static int 831 linux_utimensat_nsec_valid(l_long nsec) 832 { 833 834 if (nsec == LINUX_UTIME_OMIT || nsec == LINUX_UTIME_NOW) 835 return (0); 836 if (nsec >= 0 && nsec <= 999999999) 837 return (0); 838 return (1); 839 } 840 841 int 842 linux_utimensat(struct thread *td, struct linux_utimensat_args *args) 843 { 844 struct l_timespec l_times[2]; 845 struct timespec times[2], *timesp = NULL; 846 char *path = NULL; 847 int error, dfd, flags = 0; 848 849 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 850 851 #ifdef DEBUG 852 if (ldebug(utimensat)) 853 printf(ARGS(utimensat, "%d, *"), dfd); 854 #endif 855 856 if (args->flags & ~LINUX_AT_SYMLINK_NOFOLLOW) 857 return (EINVAL); 858 859 if (args->times != NULL) { 860 error = copyin(args->times, l_times, sizeof(l_times)); 861 if (error != 0) 862 return (error); 863 864 if (linux_utimensat_nsec_valid(l_times[0].tv_nsec) != 0 || 865 linux_utimensat_nsec_valid(l_times[1].tv_nsec) != 0) 866 return (EINVAL); 867 868 times[0].tv_sec = l_times[0].tv_sec; 869 switch (l_times[0].tv_nsec) 870 { 871 case LINUX_UTIME_OMIT: 872 times[0].tv_nsec = UTIME_OMIT; 873 break; 874 case LINUX_UTIME_NOW: 875 times[0].tv_nsec = UTIME_NOW; 876 break; 877 default: 878 times[0].tv_nsec = l_times[0].tv_nsec; 879 } 880 881 times[1].tv_sec = l_times[1].tv_sec; 882 switch (l_times[1].tv_nsec) 883 { 884 case LINUX_UTIME_OMIT: 885 times[1].tv_nsec = UTIME_OMIT; 886 break; 887 case LINUX_UTIME_NOW: 888 times[1].tv_nsec = UTIME_NOW; 889 break; 890 default: 891 times[1].tv_nsec = l_times[1].tv_nsec; 892 break; 893 } 894 timesp = times; 895 } 896 897 if (times[0].tv_nsec == UTIME_OMIT && times[1].tv_nsec == UTIME_OMIT) 898 /* This breaks POSIX, but is what the Linux kernel does 899 * _on purpose_ (documented in the man page for utimensat(2)), 900 * so we must follow that behaviour. */ 901 return (0); 902 903 if (args->pathname != NULL) 904 LCONVPATHEXIST_AT(td, args->pathname, &path, dfd); 905 else if (args->flags != 0) 906 return (EINVAL); 907 908 if (args->flags & LINUX_AT_SYMLINK_NOFOLLOW) 909 flags |= AT_SYMLINK_NOFOLLOW; 910 911 if (path == NULL) 912 error = kern_futimens(td, dfd, timesp, UIO_SYSSPACE); 913 else { 914 error = kern_utimensat(td, dfd, path, UIO_SYSSPACE, timesp, 915 UIO_SYSSPACE, flags); 916 LFREEPATH(path); 917 } 918 919 return (error); 920 } 921 922 int 923 linux_futimesat(struct thread *td, struct linux_futimesat_args *args) 924 { 925 l_timeval ltv[2]; 926 struct timeval tv[2], *tvp = NULL; 927 char *fname; 928 int error, dfd; 929 930 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 931 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd); 932 933 #ifdef DEBUG 934 if (ldebug(futimesat)) 935 printf(ARGS(futimesat, "%s, *"), fname); 936 #endif 937 938 if (args->utimes != NULL) { 939 if ((error = copyin(args->utimes, ltv, sizeof ltv))) { 940 LFREEPATH(fname); 941 return (error); 942 } 943 tv[0].tv_sec = ltv[0].tv_sec; 944 tv[0].tv_usec = ltv[0].tv_usec; 945 tv[1].tv_sec = ltv[1].tv_sec; 946 tv[1].tv_usec = ltv[1].tv_usec; 947 tvp = tv; 948 } 949 950 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 951 LFREEPATH(fname); 952 return (error); 953 } 954 955 int 956 linux_common_wait(struct thread *td, int pid, int *status, 957 int options, struct rusage *ru) 958 { 959 int error, tmpstat; 960 961 error = kern_wait(td, pid, &tmpstat, options, ru); 962 if (error) 963 return (error); 964 965 if (status) { 966 tmpstat &= 0xffff; 967 if (WIFSIGNALED(tmpstat)) 968 tmpstat = (tmpstat & 0xffffff80) | 969 bsd_to_linux_signal(WTERMSIG(tmpstat)); 970 else if (WIFSTOPPED(tmpstat)) 971 tmpstat = (tmpstat & 0xffff00ff) | 972 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8); 973 else if (WIFCONTINUED(tmpstat)) 974 tmpstat = 0xffff; 975 error = copyout(&tmpstat, status, sizeof(int)); 976 } 977 978 return (error); 979 } 980 981 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 982 int 983 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 984 { 985 struct linux_wait4_args wait4_args; 986 987 #ifdef DEBUG 988 if (ldebug(waitpid)) 989 printf(ARGS(waitpid, "%d, %p, %d"), 990 args->pid, (void *)args->status, args->options); 991 #endif 992 993 wait4_args.pid = args->pid; 994 wait4_args.status = args->status; 995 wait4_args.options = args->options; 996 wait4_args.rusage = NULL; 997 998 return (linux_wait4(td, &wait4_args)); 999 } 1000 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1001 1002 int 1003 linux_wait4(struct thread *td, struct linux_wait4_args *args) 1004 { 1005 int error, options; 1006 struct rusage ru, *rup; 1007 1008 #ifdef DEBUG 1009 if (ldebug(wait4)) 1010 printf(ARGS(wait4, "%d, %p, %d, %p"), 1011 args->pid, (void *)args->status, args->options, 1012 (void *)args->rusage); 1013 #endif 1014 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG | 1015 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL)) 1016 return (EINVAL); 1017 1018 options = WEXITED; 1019 linux_to_bsd_waitopts(args->options, &options); 1020 1021 if (args->rusage != NULL) 1022 rup = &ru; 1023 else 1024 rup = NULL; 1025 error = linux_common_wait(td, args->pid, args->status, options, rup); 1026 if (error != 0) 1027 return (error); 1028 if (args->rusage != NULL) 1029 error = linux_copyout_rusage(&ru, args->rusage); 1030 return (error); 1031 } 1032 1033 int 1034 linux_waitid(struct thread *td, struct linux_waitid_args *args) 1035 { 1036 int status, options, sig; 1037 struct __wrusage wru; 1038 siginfo_t siginfo; 1039 l_siginfo_t lsi; 1040 idtype_t idtype; 1041 struct proc *p; 1042 int error; 1043 1044 options = 0; 1045 linux_to_bsd_waitopts(args->options, &options); 1046 1047 if (options & ~(WNOHANG | WNOWAIT | WEXITED | WUNTRACED | WCONTINUED)) 1048 return (EINVAL); 1049 if (!(options & (WEXITED | WUNTRACED | WCONTINUED))) 1050 return (EINVAL); 1051 1052 switch (args->idtype) { 1053 case LINUX_P_ALL: 1054 idtype = P_ALL; 1055 break; 1056 case LINUX_P_PID: 1057 if (args->id <= 0) 1058 return (EINVAL); 1059 idtype = P_PID; 1060 break; 1061 case LINUX_P_PGID: 1062 if (args->id <= 0) 1063 return (EINVAL); 1064 idtype = P_PGID; 1065 break; 1066 default: 1067 return (EINVAL); 1068 } 1069 1070 error = kern_wait6(td, idtype, args->id, &status, options, 1071 &wru, &siginfo); 1072 if (error != 0) 1073 return (error); 1074 if (args->rusage != NULL) { 1075 error = linux_copyout_rusage(&wru.wru_children, 1076 args->rusage); 1077 if (error != 0) 1078 return (error); 1079 } 1080 if (args->info != NULL) { 1081 p = td->td_proc; 1082 if (td->td_retval[0] == 0) 1083 bzero(&lsi, sizeof(lsi)); 1084 else { 1085 sig = bsd_to_linux_signal(siginfo.si_signo); 1086 siginfo_to_lsiginfo(&siginfo, &lsi, sig); 1087 } 1088 error = copyout(&lsi, args->info, sizeof(lsi)); 1089 } 1090 td->td_retval[0] = 0; 1091 1092 return (error); 1093 } 1094 1095 int 1096 linux_mknod(struct thread *td, struct linux_mknod_args *args) 1097 { 1098 char *path; 1099 int error; 1100 1101 LCONVPATHCREAT(td, args->path, &path); 1102 1103 #ifdef DEBUG 1104 if (ldebug(mknod)) 1105 printf(ARGS(mknod, "%s, %d, %ju"), path, args->mode, 1106 (uintmax_t)args->dev); 1107 #endif 1108 1109 switch (args->mode & S_IFMT) { 1110 case S_IFIFO: 1111 case S_IFSOCK: 1112 error = kern_mkfifoat(td, AT_FDCWD, path, UIO_SYSSPACE, 1113 args->mode); 1114 break; 1115 1116 case S_IFCHR: 1117 case S_IFBLK: 1118 error = kern_mknodat(td, AT_FDCWD, path, UIO_SYSSPACE, 1119 args->mode, args->dev); 1120 break; 1121 1122 case S_IFDIR: 1123 error = EPERM; 1124 break; 1125 1126 case 0: 1127 args->mode |= S_IFREG; 1128 /* FALLTHROUGH */ 1129 case S_IFREG: 1130 error = kern_openat(td, AT_FDCWD, path, UIO_SYSSPACE, 1131 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 1132 if (error == 0) 1133 kern_close(td, td->td_retval[0]); 1134 break; 1135 1136 default: 1137 error = EINVAL; 1138 break; 1139 } 1140 LFREEPATH(path); 1141 return (error); 1142 } 1143 1144 int 1145 linux_mknodat(struct thread *td, struct linux_mknodat_args *args) 1146 { 1147 char *path; 1148 int error, dfd; 1149 1150 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 1151 LCONVPATHCREAT_AT(td, args->filename, &path, dfd); 1152 1153 #ifdef DEBUG 1154 if (ldebug(mknodat)) 1155 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev); 1156 #endif 1157 1158 switch (args->mode & S_IFMT) { 1159 case S_IFIFO: 1160 case S_IFSOCK: 1161 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode); 1162 break; 1163 1164 case S_IFCHR: 1165 case S_IFBLK: 1166 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode, 1167 args->dev); 1168 break; 1169 1170 case S_IFDIR: 1171 error = EPERM; 1172 break; 1173 1174 case 0: 1175 args->mode |= S_IFREG; 1176 /* FALLTHROUGH */ 1177 case S_IFREG: 1178 error = kern_openat(td, dfd, path, UIO_SYSSPACE, 1179 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 1180 if (error == 0) 1181 kern_close(td, td->td_retval[0]); 1182 break; 1183 1184 default: 1185 error = EINVAL; 1186 break; 1187 } 1188 LFREEPATH(path); 1189 return (error); 1190 } 1191 1192 /* 1193 * UGH! This is just about the dumbest idea I've ever heard!! 1194 */ 1195 int 1196 linux_personality(struct thread *td, struct linux_personality_args *args) 1197 { 1198 #ifdef DEBUG 1199 if (ldebug(personality)) 1200 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 1201 #endif 1202 if (args->per != 0) 1203 return (EINVAL); 1204 1205 /* Yes Jim, it's still a Linux... */ 1206 td->td_retval[0] = 0; 1207 return (0); 1208 } 1209 1210 struct l_itimerval { 1211 l_timeval it_interval; 1212 l_timeval it_value; 1213 }; 1214 1215 #define B2L_ITIMERVAL(bip, lip) \ 1216 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 1217 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 1218 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 1219 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 1220 1221 int 1222 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 1223 { 1224 int error; 1225 struct l_itimerval ls; 1226 struct itimerval aitv, oitv; 1227 1228 #ifdef DEBUG 1229 if (ldebug(setitimer)) 1230 printf(ARGS(setitimer, "%p, %p"), 1231 (void *)uap->itv, (void *)uap->oitv); 1232 #endif 1233 1234 if (uap->itv == NULL) { 1235 uap->itv = uap->oitv; 1236 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1237 } 1238 1239 error = copyin(uap->itv, &ls, sizeof(ls)); 1240 if (error != 0) 1241 return (error); 1242 B2L_ITIMERVAL(&aitv, &ls); 1243 #ifdef DEBUG 1244 if (ldebug(setitimer)) { 1245 printf("setitimer: value: sec: %jd, usec: %ld\n", 1246 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1247 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1248 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1249 } 1250 #endif 1251 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1252 if (error != 0 || uap->oitv == NULL) 1253 return (error); 1254 B2L_ITIMERVAL(&ls, &oitv); 1255 1256 return (copyout(&ls, uap->oitv, sizeof(ls))); 1257 } 1258 1259 int 1260 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1261 { 1262 int error; 1263 struct l_itimerval ls; 1264 struct itimerval aitv; 1265 1266 #ifdef DEBUG 1267 if (ldebug(getitimer)) 1268 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1269 #endif 1270 error = kern_getitimer(td, uap->which, &aitv); 1271 if (error != 0) 1272 return (error); 1273 B2L_ITIMERVAL(&ls, &aitv); 1274 return (copyout(&ls, uap->itv, sizeof(ls))); 1275 } 1276 1277 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1278 int 1279 linux_nice(struct thread *td, struct linux_nice_args *args) 1280 { 1281 struct setpriority_args bsd_args; 1282 1283 bsd_args.which = PRIO_PROCESS; 1284 bsd_args.who = 0; /* current process */ 1285 bsd_args.prio = args->inc; 1286 return (sys_setpriority(td, &bsd_args)); 1287 } 1288 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1289 1290 int 1291 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1292 { 1293 struct ucred *newcred, *oldcred; 1294 l_gid_t *linux_gidset; 1295 gid_t *bsd_gidset; 1296 int ngrp, error; 1297 struct proc *p; 1298 1299 ngrp = args->gidsetsize; 1300 if (ngrp < 0 || ngrp >= ngroups_max + 1) 1301 return (EINVAL); 1302 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 1303 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1304 if (error) 1305 goto out; 1306 newcred = crget(); 1307 crextend(newcred, ngrp + 1); 1308 p = td->td_proc; 1309 PROC_LOCK(p); 1310 oldcred = p->p_ucred; 1311 crcopy(newcred, oldcred); 1312 1313 /* 1314 * cr_groups[0] holds egid. Setting the whole set from 1315 * the supplied set will cause egid to be changed too. 1316 * Keep cr_groups[0] unchanged to prevent that. 1317 */ 1318 1319 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) { 1320 PROC_UNLOCK(p); 1321 crfree(newcred); 1322 goto out; 1323 } 1324 1325 if (ngrp > 0) { 1326 newcred->cr_ngroups = ngrp + 1; 1327 1328 bsd_gidset = newcred->cr_groups; 1329 ngrp--; 1330 while (ngrp >= 0) { 1331 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1332 ngrp--; 1333 } 1334 } else 1335 newcred->cr_ngroups = 1; 1336 1337 setsugid(p); 1338 proc_set_cred(p, newcred); 1339 PROC_UNLOCK(p); 1340 crfree(oldcred); 1341 error = 0; 1342 out: 1343 free(linux_gidset, M_LINUX); 1344 return (error); 1345 } 1346 1347 int 1348 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1349 { 1350 struct ucred *cred; 1351 l_gid_t *linux_gidset; 1352 gid_t *bsd_gidset; 1353 int bsd_gidsetsz, ngrp, error; 1354 1355 cred = td->td_ucred; 1356 bsd_gidset = cred->cr_groups; 1357 bsd_gidsetsz = cred->cr_ngroups - 1; 1358 1359 /* 1360 * cr_groups[0] holds egid. Returning the whole set 1361 * here will cause a duplicate. Exclude cr_groups[0] 1362 * to prevent that. 1363 */ 1364 1365 if ((ngrp = args->gidsetsize) == 0) { 1366 td->td_retval[0] = bsd_gidsetsz; 1367 return (0); 1368 } 1369 1370 if (ngrp < bsd_gidsetsz) 1371 return (EINVAL); 1372 1373 ngrp = 0; 1374 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset), 1375 M_LINUX, M_WAITOK); 1376 while (ngrp < bsd_gidsetsz) { 1377 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1378 ngrp++; 1379 } 1380 1381 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t)); 1382 free(linux_gidset, M_LINUX); 1383 if (error) 1384 return (error); 1385 1386 td->td_retval[0] = ngrp; 1387 return (0); 1388 } 1389 1390 int 1391 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1392 { 1393 struct rlimit bsd_rlim; 1394 struct l_rlimit rlim; 1395 u_int which; 1396 int error; 1397 1398 #ifdef DEBUG 1399 if (ldebug(setrlimit)) 1400 printf(ARGS(setrlimit, "%d, %p"), 1401 args->resource, (void *)args->rlim); 1402 #endif 1403 1404 if (args->resource >= LINUX_RLIM_NLIMITS) 1405 return (EINVAL); 1406 1407 which = linux_to_bsd_resource[args->resource]; 1408 if (which == -1) 1409 return (EINVAL); 1410 1411 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1412 if (error) 1413 return (error); 1414 1415 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1416 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1417 return (kern_setrlimit(td, which, &bsd_rlim)); 1418 } 1419 1420 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1421 int 1422 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1423 { 1424 struct l_rlimit rlim; 1425 struct rlimit bsd_rlim; 1426 u_int which; 1427 1428 #ifdef DEBUG 1429 if (ldebug(old_getrlimit)) 1430 printf(ARGS(old_getrlimit, "%d, %p"), 1431 args->resource, (void *)args->rlim); 1432 #endif 1433 1434 if (args->resource >= LINUX_RLIM_NLIMITS) 1435 return (EINVAL); 1436 1437 which = linux_to_bsd_resource[args->resource]; 1438 if (which == -1) 1439 return (EINVAL); 1440 1441 lim_rlimit(td, which, &bsd_rlim); 1442 1443 #ifdef COMPAT_LINUX32 1444 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1445 if (rlim.rlim_cur == UINT_MAX) 1446 rlim.rlim_cur = INT_MAX; 1447 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1448 if (rlim.rlim_max == UINT_MAX) 1449 rlim.rlim_max = INT_MAX; 1450 #else 1451 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1452 if (rlim.rlim_cur == ULONG_MAX) 1453 rlim.rlim_cur = LONG_MAX; 1454 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1455 if (rlim.rlim_max == ULONG_MAX) 1456 rlim.rlim_max = LONG_MAX; 1457 #endif 1458 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1459 } 1460 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1461 1462 int 1463 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1464 { 1465 struct l_rlimit rlim; 1466 struct rlimit bsd_rlim; 1467 u_int which; 1468 1469 #ifdef DEBUG 1470 if (ldebug(getrlimit)) 1471 printf(ARGS(getrlimit, "%d, %p"), 1472 args->resource, (void *)args->rlim); 1473 #endif 1474 1475 if (args->resource >= LINUX_RLIM_NLIMITS) 1476 return (EINVAL); 1477 1478 which = linux_to_bsd_resource[args->resource]; 1479 if (which == -1) 1480 return (EINVAL); 1481 1482 lim_rlimit(td, which, &bsd_rlim); 1483 1484 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1485 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1486 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1487 } 1488 1489 int 1490 linux_sched_setscheduler(struct thread *td, 1491 struct linux_sched_setscheduler_args *args) 1492 { 1493 struct sched_param sched_param; 1494 struct thread *tdt; 1495 int error, policy; 1496 1497 #ifdef DEBUG 1498 if (ldebug(sched_setscheduler)) 1499 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1500 args->pid, args->policy, (const void *)args->param); 1501 #endif 1502 1503 switch (args->policy) { 1504 case LINUX_SCHED_OTHER: 1505 policy = SCHED_OTHER; 1506 break; 1507 case LINUX_SCHED_FIFO: 1508 policy = SCHED_FIFO; 1509 break; 1510 case LINUX_SCHED_RR: 1511 policy = SCHED_RR; 1512 break; 1513 default: 1514 return (EINVAL); 1515 } 1516 1517 error = copyin(args->param, &sched_param, sizeof(sched_param)); 1518 if (error) 1519 return (error); 1520 1521 tdt = linux_tdfind(td, args->pid, -1); 1522 if (tdt == NULL) 1523 return (ESRCH); 1524 1525 error = kern_sched_setscheduler(td, tdt, policy, &sched_param); 1526 PROC_UNLOCK(tdt->td_proc); 1527 return (error); 1528 } 1529 1530 int 1531 linux_sched_getscheduler(struct thread *td, 1532 struct linux_sched_getscheduler_args *args) 1533 { 1534 struct thread *tdt; 1535 int error, policy; 1536 1537 #ifdef DEBUG 1538 if (ldebug(sched_getscheduler)) 1539 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1540 #endif 1541 1542 tdt = linux_tdfind(td, args->pid, -1); 1543 if (tdt == NULL) 1544 return (ESRCH); 1545 1546 error = kern_sched_getscheduler(td, tdt, &policy); 1547 PROC_UNLOCK(tdt->td_proc); 1548 1549 switch (policy) { 1550 case SCHED_OTHER: 1551 td->td_retval[0] = LINUX_SCHED_OTHER; 1552 break; 1553 case SCHED_FIFO: 1554 td->td_retval[0] = LINUX_SCHED_FIFO; 1555 break; 1556 case SCHED_RR: 1557 td->td_retval[0] = LINUX_SCHED_RR; 1558 break; 1559 } 1560 return (error); 1561 } 1562 1563 int 1564 linux_sched_get_priority_max(struct thread *td, 1565 struct linux_sched_get_priority_max_args *args) 1566 { 1567 struct sched_get_priority_max_args bsd; 1568 1569 #ifdef DEBUG 1570 if (ldebug(sched_get_priority_max)) 1571 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1572 #endif 1573 1574 switch (args->policy) { 1575 case LINUX_SCHED_OTHER: 1576 bsd.policy = SCHED_OTHER; 1577 break; 1578 case LINUX_SCHED_FIFO: 1579 bsd.policy = SCHED_FIFO; 1580 break; 1581 case LINUX_SCHED_RR: 1582 bsd.policy = SCHED_RR; 1583 break; 1584 default: 1585 return (EINVAL); 1586 } 1587 return (sys_sched_get_priority_max(td, &bsd)); 1588 } 1589 1590 int 1591 linux_sched_get_priority_min(struct thread *td, 1592 struct linux_sched_get_priority_min_args *args) 1593 { 1594 struct sched_get_priority_min_args bsd; 1595 1596 #ifdef DEBUG 1597 if (ldebug(sched_get_priority_min)) 1598 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1599 #endif 1600 1601 switch (args->policy) { 1602 case LINUX_SCHED_OTHER: 1603 bsd.policy = SCHED_OTHER; 1604 break; 1605 case LINUX_SCHED_FIFO: 1606 bsd.policy = SCHED_FIFO; 1607 break; 1608 case LINUX_SCHED_RR: 1609 bsd.policy = SCHED_RR; 1610 break; 1611 default: 1612 return (EINVAL); 1613 } 1614 return (sys_sched_get_priority_min(td, &bsd)); 1615 } 1616 1617 #define REBOOT_CAD_ON 0x89abcdef 1618 #define REBOOT_CAD_OFF 0 1619 #define REBOOT_HALT 0xcdef0123 1620 #define REBOOT_RESTART 0x01234567 1621 #define REBOOT_RESTART2 0xA1B2C3D4 1622 #define REBOOT_POWEROFF 0x4321FEDC 1623 #define REBOOT_MAGIC1 0xfee1dead 1624 #define REBOOT_MAGIC2 0x28121969 1625 #define REBOOT_MAGIC2A 0x05121996 1626 #define REBOOT_MAGIC2B 0x16041998 1627 1628 int 1629 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1630 { 1631 struct reboot_args bsd_args; 1632 1633 #ifdef DEBUG 1634 if (ldebug(reboot)) 1635 printf(ARGS(reboot, "0x%x"), args->cmd); 1636 #endif 1637 1638 if (args->magic1 != REBOOT_MAGIC1) 1639 return (EINVAL); 1640 1641 switch (args->magic2) { 1642 case REBOOT_MAGIC2: 1643 case REBOOT_MAGIC2A: 1644 case REBOOT_MAGIC2B: 1645 break; 1646 default: 1647 return (EINVAL); 1648 } 1649 1650 switch (args->cmd) { 1651 case REBOOT_CAD_ON: 1652 case REBOOT_CAD_OFF: 1653 return (priv_check(td, PRIV_REBOOT)); 1654 case REBOOT_HALT: 1655 bsd_args.opt = RB_HALT; 1656 break; 1657 case REBOOT_RESTART: 1658 case REBOOT_RESTART2: 1659 bsd_args.opt = 0; 1660 break; 1661 case REBOOT_POWEROFF: 1662 bsd_args.opt = RB_POWEROFF; 1663 break; 1664 default: 1665 return (EINVAL); 1666 } 1667 return (sys_reboot(td, &bsd_args)); 1668 } 1669 1670 1671 /* 1672 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1673 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that 1674 * are assumed to be preserved. The following lightweight syscalls fixes 1675 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c 1676 * 1677 * linux_getpid() - MP SAFE 1678 * linux_getgid() - MP SAFE 1679 * linux_getuid() - MP SAFE 1680 */ 1681 1682 int 1683 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1684 { 1685 1686 #ifdef DEBUG 1687 if (ldebug(getpid)) 1688 printf(ARGS(getpid, "")); 1689 #endif 1690 td->td_retval[0] = td->td_proc->p_pid; 1691 1692 return (0); 1693 } 1694 1695 int 1696 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1697 { 1698 struct linux_emuldata *em; 1699 1700 #ifdef DEBUG 1701 if (ldebug(gettid)) 1702 printf(ARGS(gettid, "")); 1703 #endif 1704 1705 em = em_find(td); 1706 KASSERT(em != NULL, ("gettid: emuldata not found.\n")); 1707 1708 td->td_retval[0] = em->em_tid; 1709 1710 return (0); 1711 } 1712 1713 1714 int 1715 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1716 { 1717 1718 #ifdef DEBUG 1719 if (ldebug(getppid)) 1720 printf(ARGS(getppid, "")); 1721 #endif 1722 1723 PROC_LOCK(td->td_proc); 1724 td->td_retval[0] = td->td_proc->p_pptr->p_pid; 1725 PROC_UNLOCK(td->td_proc); 1726 return (0); 1727 } 1728 1729 int 1730 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1731 { 1732 1733 #ifdef DEBUG 1734 if (ldebug(getgid)) 1735 printf(ARGS(getgid, "")); 1736 #endif 1737 1738 td->td_retval[0] = td->td_ucred->cr_rgid; 1739 return (0); 1740 } 1741 1742 int 1743 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1744 { 1745 1746 #ifdef DEBUG 1747 if (ldebug(getuid)) 1748 printf(ARGS(getuid, "")); 1749 #endif 1750 1751 td->td_retval[0] = td->td_ucred->cr_ruid; 1752 return (0); 1753 } 1754 1755 1756 int 1757 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1758 { 1759 struct getsid_args bsd; 1760 1761 #ifdef DEBUG 1762 if (ldebug(getsid)) 1763 printf(ARGS(getsid, "%i"), args->pid); 1764 #endif 1765 1766 bsd.pid = args->pid; 1767 return (sys_getsid(td, &bsd)); 1768 } 1769 1770 int 1771 linux_nosys(struct thread *td, struct nosys_args *ignore) 1772 { 1773 1774 return (ENOSYS); 1775 } 1776 1777 int 1778 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1779 { 1780 struct getpriority_args bsd_args; 1781 int error; 1782 1783 #ifdef DEBUG 1784 if (ldebug(getpriority)) 1785 printf(ARGS(getpriority, "%i, %i"), args->which, args->who); 1786 #endif 1787 1788 bsd_args.which = args->which; 1789 bsd_args.who = args->who; 1790 error = sys_getpriority(td, &bsd_args); 1791 td->td_retval[0] = 20 - td->td_retval[0]; 1792 return (error); 1793 } 1794 1795 int 1796 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1797 { 1798 int name[2]; 1799 1800 #ifdef DEBUG 1801 if (ldebug(sethostname)) 1802 printf(ARGS(sethostname, "*, %i"), args->len); 1803 #endif 1804 1805 name[0] = CTL_KERN; 1806 name[1] = KERN_HOSTNAME; 1807 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1808 args->len, 0, 0)); 1809 } 1810 1811 int 1812 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args) 1813 { 1814 int name[2]; 1815 1816 #ifdef DEBUG 1817 if (ldebug(setdomainname)) 1818 printf(ARGS(setdomainname, "*, %i"), args->len); 1819 #endif 1820 1821 name[0] = CTL_KERN; 1822 name[1] = KERN_NISDOMAINNAME; 1823 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name, 1824 args->len, 0, 0)); 1825 } 1826 1827 int 1828 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1829 { 1830 1831 #ifdef DEBUG 1832 if (ldebug(exit_group)) 1833 printf(ARGS(exit_group, "%i"), args->error_code); 1834 #endif 1835 1836 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid, 1837 args->error_code); 1838 1839 /* 1840 * XXX: we should send a signal to the parent if 1841 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1842 * as it doesnt occur often. 1843 */ 1844 exit1(td, args->error_code, 0); 1845 /* NOTREACHED */ 1846 } 1847 1848 #define _LINUX_CAPABILITY_VERSION 0x19980330 1849 1850 struct l_user_cap_header { 1851 l_int version; 1852 l_int pid; 1853 }; 1854 1855 struct l_user_cap_data { 1856 l_int effective; 1857 l_int permitted; 1858 l_int inheritable; 1859 }; 1860 1861 int 1862 linux_capget(struct thread *td, struct linux_capget_args *args) 1863 { 1864 struct l_user_cap_header luch; 1865 struct l_user_cap_data lucd; 1866 int error; 1867 1868 if (args->hdrp == NULL) 1869 return (EFAULT); 1870 1871 error = copyin(args->hdrp, &luch, sizeof(luch)); 1872 if (error != 0) 1873 return (error); 1874 1875 if (luch.version != _LINUX_CAPABILITY_VERSION) { 1876 luch.version = _LINUX_CAPABILITY_VERSION; 1877 error = copyout(&luch, args->hdrp, sizeof(luch)); 1878 if (error) 1879 return (error); 1880 return (EINVAL); 1881 } 1882 1883 if (luch.pid) 1884 return (EPERM); 1885 1886 if (args->datap) { 1887 /* 1888 * The current implementation doesn't support setting 1889 * a capability (it's essentially a stub) so indicate 1890 * that no capabilities are currently set or available 1891 * to request. 1892 */ 1893 bzero (&lucd, sizeof(lucd)); 1894 error = copyout(&lucd, args->datap, sizeof(lucd)); 1895 } 1896 1897 return (error); 1898 } 1899 1900 int 1901 linux_capset(struct thread *td, struct linux_capset_args *args) 1902 { 1903 struct l_user_cap_header luch; 1904 struct l_user_cap_data lucd; 1905 int error; 1906 1907 if (args->hdrp == NULL || args->datap == NULL) 1908 return (EFAULT); 1909 1910 error = copyin(args->hdrp, &luch, sizeof(luch)); 1911 if (error != 0) 1912 return (error); 1913 1914 if (luch.version != _LINUX_CAPABILITY_VERSION) { 1915 luch.version = _LINUX_CAPABILITY_VERSION; 1916 error = copyout(&luch, args->hdrp, sizeof(luch)); 1917 if (error) 1918 return (error); 1919 return (EINVAL); 1920 } 1921 1922 if (luch.pid) 1923 return (EPERM); 1924 1925 error = copyin(args->datap, &lucd, sizeof(lucd)); 1926 if (error != 0) 1927 return (error); 1928 1929 /* We currently don't support setting any capabilities. */ 1930 if (lucd.effective || lucd.permitted || lucd.inheritable) { 1931 linux_msg(td, 1932 "capset effective=0x%x, permitted=0x%x, " 1933 "inheritable=0x%x is not implemented", 1934 (int)lucd.effective, (int)lucd.permitted, 1935 (int)lucd.inheritable); 1936 return (EPERM); 1937 } 1938 1939 return (0); 1940 } 1941 1942 int 1943 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1944 { 1945 int error = 0, max_size; 1946 struct proc *p = td->td_proc; 1947 char comm[LINUX_MAX_COMM_LEN]; 1948 struct linux_emuldata *em; 1949 int pdeath_signal; 1950 1951 #ifdef DEBUG 1952 if (ldebug(prctl)) 1953 printf(ARGS(prctl, "%d, %ju, %ju, %ju, %ju"), args->option, 1954 (uintmax_t)args->arg2, (uintmax_t)args->arg3, 1955 (uintmax_t)args->arg4, (uintmax_t)args->arg5); 1956 #endif 1957 1958 switch (args->option) { 1959 case LINUX_PR_SET_PDEATHSIG: 1960 if (!LINUX_SIG_VALID(args->arg2)) 1961 return (EINVAL); 1962 em = em_find(td); 1963 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1964 em->pdeath_signal = args->arg2; 1965 break; 1966 case LINUX_PR_GET_PDEATHSIG: 1967 em = em_find(td); 1968 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1969 pdeath_signal = em->pdeath_signal; 1970 error = copyout(&pdeath_signal, 1971 (void *)(register_t)args->arg2, 1972 sizeof(pdeath_signal)); 1973 break; 1974 case LINUX_PR_GET_KEEPCAPS: 1975 /* 1976 * Indicate that we always clear the effective and 1977 * permitted capability sets when the user id becomes 1978 * non-zero (actually the capability sets are simply 1979 * always zero in the current implementation). 1980 */ 1981 td->td_retval[0] = 0; 1982 break; 1983 case LINUX_PR_SET_KEEPCAPS: 1984 /* 1985 * Ignore requests to keep the effective and permitted 1986 * capability sets when the user id becomes non-zero. 1987 */ 1988 break; 1989 case LINUX_PR_SET_NAME: 1990 /* 1991 * To be on the safe side we need to make sure to not 1992 * overflow the size a linux program expects. We already 1993 * do this here in the copyin, so that we don't need to 1994 * check on copyout. 1995 */ 1996 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1997 error = copyinstr((void *)(register_t)args->arg2, comm, 1998 max_size, NULL); 1999 2000 /* Linux silently truncates the name if it is too long. */ 2001 if (error == ENAMETOOLONG) { 2002 /* 2003 * XXX: copyinstr() isn't documented to populate the 2004 * array completely, so do a copyin() to be on the 2005 * safe side. This should be changed in case 2006 * copyinstr() is changed to guarantee this. 2007 */ 2008 error = copyin((void *)(register_t)args->arg2, comm, 2009 max_size - 1); 2010 comm[max_size - 1] = '\0'; 2011 } 2012 if (error) 2013 return (error); 2014 2015 PROC_LOCK(p); 2016 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 2017 PROC_UNLOCK(p); 2018 break; 2019 case LINUX_PR_GET_NAME: 2020 PROC_LOCK(p); 2021 strlcpy(comm, p->p_comm, sizeof(comm)); 2022 PROC_UNLOCK(p); 2023 error = copyout(comm, (void *)(register_t)args->arg2, 2024 strlen(comm) + 1); 2025 break; 2026 default: 2027 error = EINVAL; 2028 break; 2029 } 2030 2031 return (error); 2032 } 2033 2034 int 2035 linux_sched_setparam(struct thread *td, 2036 struct linux_sched_setparam_args *uap) 2037 { 2038 struct sched_param sched_param; 2039 struct thread *tdt; 2040 int error; 2041 2042 #ifdef DEBUG 2043 if (ldebug(sched_setparam)) 2044 printf(ARGS(sched_setparam, "%d, *"), uap->pid); 2045 #endif 2046 2047 error = copyin(uap->param, &sched_param, sizeof(sched_param)); 2048 if (error) 2049 return (error); 2050 2051 tdt = linux_tdfind(td, uap->pid, -1); 2052 if (tdt == NULL) 2053 return (ESRCH); 2054 2055 error = kern_sched_setparam(td, tdt, &sched_param); 2056 PROC_UNLOCK(tdt->td_proc); 2057 return (error); 2058 } 2059 2060 int 2061 linux_sched_getparam(struct thread *td, 2062 struct linux_sched_getparam_args *uap) 2063 { 2064 struct sched_param sched_param; 2065 struct thread *tdt; 2066 int error; 2067 2068 #ifdef DEBUG 2069 if (ldebug(sched_getparam)) 2070 printf(ARGS(sched_getparam, "%d, *"), uap->pid); 2071 #endif 2072 2073 tdt = linux_tdfind(td, uap->pid, -1); 2074 if (tdt == NULL) 2075 return (ESRCH); 2076 2077 error = kern_sched_getparam(td, tdt, &sched_param); 2078 PROC_UNLOCK(tdt->td_proc); 2079 if (error == 0) 2080 error = copyout(&sched_param, uap->param, 2081 sizeof(sched_param)); 2082 return (error); 2083 } 2084 2085 /* 2086 * Get affinity of a process. 2087 */ 2088 int 2089 linux_sched_getaffinity(struct thread *td, 2090 struct linux_sched_getaffinity_args *args) 2091 { 2092 int error; 2093 struct thread *tdt; 2094 struct cpuset_getaffinity_args cga; 2095 2096 #ifdef DEBUG 2097 if (ldebug(sched_getaffinity)) 2098 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid, 2099 args->len); 2100 #endif 2101 if (args->len < sizeof(cpuset_t)) 2102 return (EINVAL); 2103 2104 tdt = linux_tdfind(td, args->pid, -1); 2105 if (tdt == NULL) 2106 return (ESRCH); 2107 2108 PROC_UNLOCK(tdt->td_proc); 2109 cga.level = CPU_LEVEL_WHICH; 2110 cga.which = CPU_WHICH_TID; 2111 cga.id = tdt->td_tid; 2112 cga.cpusetsize = sizeof(cpuset_t); 2113 cga.mask = (cpuset_t *) args->user_mask_ptr; 2114 2115 if ((error = sys_cpuset_getaffinity(td, &cga)) == 0) 2116 td->td_retval[0] = sizeof(cpuset_t); 2117 2118 return (error); 2119 } 2120 2121 /* 2122 * Set affinity of a process. 2123 */ 2124 int 2125 linux_sched_setaffinity(struct thread *td, 2126 struct linux_sched_setaffinity_args *args) 2127 { 2128 struct cpuset_setaffinity_args csa; 2129 struct thread *tdt; 2130 2131 #ifdef DEBUG 2132 if (ldebug(sched_setaffinity)) 2133 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid, 2134 args->len); 2135 #endif 2136 if (args->len < sizeof(cpuset_t)) 2137 return (EINVAL); 2138 2139 tdt = linux_tdfind(td, args->pid, -1); 2140 if (tdt == NULL) 2141 return (ESRCH); 2142 2143 PROC_UNLOCK(tdt->td_proc); 2144 csa.level = CPU_LEVEL_WHICH; 2145 csa.which = CPU_WHICH_TID; 2146 csa.id = tdt->td_tid; 2147 csa.cpusetsize = sizeof(cpuset_t); 2148 csa.mask = (cpuset_t *) args->user_mask_ptr; 2149 2150 return (sys_cpuset_setaffinity(td, &csa)); 2151 } 2152 2153 struct linux_rlimit64 { 2154 uint64_t rlim_cur; 2155 uint64_t rlim_max; 2156 }; 2157 2158 int 2159 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args) 2160 { 2161 struct rlimit rlim, nrlim; 2162 struct linux_rlimit64 lrlim; 2163 struct proc *p; 2164 u_int which; 2165 int flags; 2166 int error; 2167 2168 #ifdef DEBUG 2169 if (ldebug(prlimit64)) 2170 printf(ARGS(prlimit64, "%d, %d, %p, %p"), args->pid, 2171 args->resource, (void *)args->new, (void *)args->old); 2172 #endif 2173 2174 if (args->resource >= LINUX_RLIM_NLIMITS) 2175 return (EINVAL); 2176 2177 which = linux_to_bsd_resource[args->resource]; 2178 if (which == -1) 2179 return (EINVAL); 2180 2181 if (args->new != NULL) { 2182 /* 2183 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux 2184 * rlim is unsigned 64-bit. FreeBSD treats negative limits 2185 * as INFINITY so we do not need a conversion even. 2186 */ 2187 error = copyin(args->new, &nrlim, sizeof(nrlim)); 2188 if (error != 0) 2189 return (error); 2190 } 2191 2192 flags = PGET_HOLD | PGET_NOTWEXIT; 2193 if (args->new != NULL) 2194 flags |= PGET_CANDEBUG; 2195 else 2196 flags |= PGET_CANSEE; 2197 error = pget(args->pid, flags, &p); 2198 if (error != 0) 2199 return (error); 2200 2201 if (args->old != NULL) { 2202 PROC_LOCK(p); 2203 lim_rlimit_proc(p, which, &rlim); 2204 PROC_UNLOCK(p); 2205 if (rlim.rlim_cur == RLIM_INFINITY) 2206 lrlim.rlim_cur = LINUX_RLIM_INFINITY; 2207 else 2208 lrlim.rlim_cur = rlim.rlim_cur; 2209 if (rlim.rlim_max == RLIM_INFINITY) 2210 lrlim.rlim_max = LINUX_RLIM_INFINITY; 2211 else 2212 lrlim.rlim_max = rlim.rlim_max; 2213 error = copyout(&lrlim, args->old, sizeof(lrlim)); 2214 if (error != 0) 2215 goto out; 2216 } 2217 2218 if (args->new != NULL) 2219 error = kern_proc_setrlimit(td, p, which, &nrlim); 2220 2221 out: 2222 PRELE(p); 2223 return (error); 2224 } 2225 2226 int 2227 linux_pselect6(struct thread *td, struct linux_pselect6_args *args) 2228 { 2229 struct timeval utv, tv0, tv1, *tvp; 2230 struct l_pselect6arg lpse6; 2231 struct l_timespec lts; 2232 struct timespec uts; 2233 l_sigset_t l_ss; 2234 sigset_t *ssp; 2235 sigset_t ss; 2236 int error; 2237 2238 ssp = NULL; 2239 if (args->sig != NULL) { 2240 error = copyin(args->sig, &lpse6, sizeof(lpse6)); 2241 if (error != 0) 2242 return (error); 2243 if (lpse6.ss_len != sizeof(l_ss)) 2244 return (EINVAL); 2245 if (lpse6.ss != 0) { 2246 error = copyin(PTRIN(lpse6.ss), &l_ss, 2247 sizeof(l_ss)); 2248 if (error != 0) 2249 return (error); 2250 linux_to_bsd_sigset(&l_ss, &ss); 2251 ssp = &ss; 2252 } 2253 } 2254 2255 /* 2256 * Currently glibc changes nanosecond number to microsecond. 2257 * This mean losing precision but for now it is hardly seen. 2258 */ 2259 if (args->tsp != NULL) { 2260 error = copyin(args->tsp, <s, sizeof(lts)); 2261 if (error != 0) 2262 return (error); 2263 error = linux_to_native_timespec(&uts, <s); 2264 if (error != 0) 2265 return (error); 2266 2267 TIMESPEC_TO_TIMEVAL(&utv, &uts); 2268 if (itimerfix(&utv)) 2269 return (EINVAL); 2270 2271 microtime(&tv0); 2272 tvp = &utv; 2273 } else 2274 tvp = NULL; 2275 2276 error = kern_pselect(td, args->nfds, args->readfds, args->writefds, 2277 args->exceptfds, tvp, ssp, LINUX_NFDBITS); 2278 2279 if (error == 0 && args->tsp != NULL) { 2280 if (td->td_retval[0] != 0) { 2281 /* 2282 * Compute how much time was left of the timeout, 2283 * by subtracting the current time and the time 2284 * before we started the call, and subtracting 2285 * that result from the user-supplied value. 2286 */ 2287 2288 microtime(&tv1); 2289 timevalsub(&tv1, &tv0); 2290 timevalsub(&utv, &tv1); 2291 if (utv.tv_sec < 0) 2292 timevalclear(&utv); 2293 } else 2294 timevalclear(&utv); 2295 2296 TIMEVAL_TO_TIMESPEC(&utv, &uts); 2297 2298 native_to_linux_timespec(<s, &uts); 2299 error = copyout(<s, args->tsp, sizeof(lts)); 2300 } 2301 2302 return (error); 2303 } 2304 2305 int 2306 linux_ppoll(struct thread *td, struct linux_ppoll_args *args) 2307 { 2308 struct timespec ts0, ts1; 2309 struct l_timespec lts; 2310 struct timespec uts, *tsp; 2311 l_sigset_t l_ss; 2312 sigset_t *ssp; 2313 sigset_t ss; 2314 int error; 2315 2316 if (args->sset != NULL) { 2317 if (args->ssize != sizeof(l_ss)) 2318 return (EINVAL); 2319 error = copyin(args->sset, &l_ss, sizeof(l_ss)); 2320 if (error) 2321 return (error); 2322 linux_to_bsd_sigset(&l_ss, &ss); 2323 ssp = &ss; 2324 } else 2325 ssp = NULL; 2326 if (args->tsp != NULL) { 2327 error = copyin(args->tsp, <s, sizeof(lts)); 2328 if (error) 2329 return (error); 2330 error = linux_to_native_timespec(&uts, <s); 2331 if (error != 0) 2332 return (error); 2333 2334 nanotime(&ts0); 2335 tsp = &uts; 2336 } else 2337 tsp = NULL; 2338 2339 error = kern_poll(td, args->fds, args->nfds, tsp, ssp); 2340 2341 if (error == 0 && args->tsp != NULL) { 2342 if (td->td_retval[0]) { 2343 nanotime(&ts1); 2344 timespecsub(&ts1, &ts0); 2345 timespecsub(&uts, &ts1); 2346 if (uts.tv_sec < 0) 2347 timespecclear(&uts); 2348 } else 2349 timespecclear(&uts); 2350 2351 native_to_linux_timespec(<s, &uts); 2352 error = copyout(<s, args->tsp, sizeof(lts)); 2353 } 2354 2355 return (error); 2356 } 2357 2358 #if defined(DEBUG) || defined(KTR) 2359 /* XXX: can be removed when every ldebug(...) and KTR stuff are removed. */ 2360 2361 #ifdef COMPAT_LINUX32 2362 #define L_MAXSYSCALL LINUX32_SYS_MAXSYSCALL 2363 #else 2364 #define L_MAXSYSCALL LINUX_SYS_MAXSYSCALL 2365 #endif 2366 2367 u_char linux_debug_map[howmany(L_MAXSYSCALL, sizeof(u_char))]; 2368 2369 static int 2370 linux_debug(int syscall, int toggle, int global) 2371 { 2372 2373 if (global) { 2374 char c = toggle ? 0 : 0xff; 2375 2376 memset(linux_debug_map, c, sizeof(linux_debug_map)); 2377 return (0); 2378 } 2379 if (syscall < 0 || syscall >= L_MAXSYSCALL) 2380 return (EINVAL); 2381 if (toggle) 2382 clrbit(linux_debug_map, syscall); 2383 else 2384 setbit(linux_debug_map, syscall); 2385 return (0); 2386 } 2387 #undef L_MAXSYSCALL 2388 2389 /* 2390 * Usage: sysctl linux.debug=<syscall_nr>.<0/1> 2391 * 2392 * E.g.: sysctl linux.debug=21.0 2393 * 2394 * As a special case, syscall "all" will apply to all syscalls globally. 2395 */ 2396 #define LINUX_MAX_DEBUGSTR 16 2397 int 2398 linux_sysctl_debug(SYSCTL_HANDLER_ARGS) 2399 { 2400 char value[LINUX_MAX_DEBUGSTR], *p; 2401 int error, sysc, toggle; 2402 int global = 0; 2403 2404 value[0] = '\0'; 2405 error = sysctl_handle_string(oidp, value, LINUX_MAX_DEBUGSTR, req); 2406 if (error || req->newptr == NULL) 2407 return (error); 2408 for (p = value; *p != '\0' && *p != '.'; p++); 2409 if (*p == '\0') 2410 return (EINVAL); 2411 *p++ = '\0'; 2412 sysc = strtol(value, NULL, 0); 2413 toggle = strtol(p, NULL, 0); 2414 if (strcmp(value, "all") == 0) 2415 global = 1; 2416 error = linux_debug(sysc, toggle, global); 2417 return (error); 2418 } 2419 2420 #endif /* DEBUG || KTR */ 2421 2422 int 2423 linux_sched_rr_get_interval(struct thread *td, 2424 struct linux_sched_rr_get_interval_args *uap) 2425 { 2426 struct timespec ts; 2427 struct l_timespec lts; 2428 struct thread *tdt; 2429 int error; 2430 2431 /* 2432 * According to man in case the invalid pid specified 2433 * EINVAL should be returned. 2434 */ 2435 if (uap->pid < 0) 2436 return (EINVAL); 2437 2438 tdt = linux_tdfind(td, uap->pid, -1); 2439 if (tdt == NULL) 2440 return (ESRCH); 2441 2442 error = kern_sched_rr_get_interval_td(td, tdt, &ts); 2443 PROC_UNLOCK(tdt->td_proc); 2444 if (error != 0) 2445 return (error); 2446 native_to_linux_timespec(<s, &ts); 2447 return (copyout(<s, uap->interval, sizeof(lts))); 2448 } 2449 2450 /* 2451 * In case when the Linux thread is the initial thread in 2452 * the thread group thread id is equal to the process id. 2453 * Glibc depends on this magic (assert in pthread_getattr_np.c). 2454 */ 2455 struct thread * 2456 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid) 2457 { 2458 struct linux_emuldata *em; 2459 struct thread *tdt; 2460 struct proc *p; 2461 2462 tdt = NULL; 2463 if (tid == 0 || tid == td->td_tid) { 2464 tdt = td; 2465 PROC_LOCK(tdt->td_proc); 2466 } else if (tid > PID_MAX) 2467 tdt = tdfind(tid, pid); 2468 else { 2469 /* 2470 * Initial thread where the tid equal to the pid. 2471 */ 2472 p = pfind(tid); 2473 if (p != NULL) { 2474 if (SV_PROC_ABI(p) != SV_ABI_LINUX) { 2475 /* 2476 * p is not a Linuxulator process. 2477 */ 2478 PROC_UNLOCK(p); 2479 return (NULL); 2480 } 2481 FOREACH_THREAD_IN_PROC(p, tdt) { 2482 em = em_find(tdt); 2483 if (tid == em->em_tid) 2484 return (tdt); 2485 } 2486 PROC_UNLOCK(p); 2487 } 2488 return (NULL); 2489 } 2490 2491 return (tdt); 2492 } 2493 2494 void 2495 linux_to_bsd_waitopts(int options, int *bsdopts) 2496 { 2497 2498 if (options & LINUX_WNOHANG) 2499 *bsdopts |= WNOHANG; 2500 if (options & LINUX_WUNTRACED) 2501 *bsdopts |= WUNTRACED; 2502 if (options & LINUX_WEXITED) 2503 *bsdopts |= WEXITED; 2504 if (options & LINUX_WCONTINUED) 2505 *bsdopts |= WCONTINUED; 2506 if (options & LINUX_WNOWAIT) 2507 *bsdopts |= WNOWAIT; 2508 2509 if (options & __WCLONE) 2510 *bsdopts |= WLINUXCLONE; 2511 } 2512