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