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