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