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