1 /*- 2 * Copyright (c) 2002 Doug Rabson 3 * Copyright (c) 1994-1995 S�ren Schmidt 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include "opt_compat.h" 34 35 #include <sys/param.h> 36 #include <sys/blist.h> 37 #include <sys/fcntl.h> 38 #if defined(__i386__) 39 #include <sys/imgact_aout.h> 40 #endif 41 #include <sys/jail.h> 42 #include <sys/kernel.h> 43 #include <sys/limits.h> 44 #include <sys/lock.h> 45 #include <sys/malloc.h> 46 #include <sys/mman.h> 47 #include <sys/mount.h> 48 #include <sys/mutex.h> 49 #include <sys/namei.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/reboot.h> 53 #include <sys/resourcevar.h> 54 #include <sys/sched.h> 55 #include <sys/signalvar.h> 56 #include <sys/stat.h> 57 #include <sys/syscallsubr.h> 58 #include <sys/sysctl.h> 59 #include <sys/sysproto.h> 60 #include <sys/systm.h> 61 #include <sys/time.h> 62 #include <sys/vmmeter.h> 63 #include <sys/vnode.h> 64 #include <sys/wait.h> 65 #include <sys/cpuset.h> 66 67 #include <security/mac/mac_framework.h> 68 69 #include <vm/vm.h> 70 #include <vm/pmap.h> 71 #include <vm/vm_kern.h> 72 #include <vm/vm_map.h> 73 #include <vm/vm_extern.h> 74 #include <vm/vm_object.h> 75 #include <vm/swap_pager.h> 76 77 #ifdef COMPAT_LINUX32 78 #include <machine/../linux32/linux.h> 79 #include <machine/../linux32/linux32_proto.h> 80 #else 81 #include <machine/../linux/linux.h> 82 #include <machine/../linux/linux_proto.h> 83 #endif 84 85 #include <compat/linux/linux_file.h> 86 #include <compat/linux/linux_mib.h> 87 #include <compat/linux/linux_signal.h> 88 #include <compat/linux/linux_util.h> 89 #include <compat/linux/linux_sysproto.h> 90 #include <compat/linux/linux_emul.h> 91 #include <compat/linux/linux_misc.h> 92 93 int stclohz; /* Statistics clock frequency */ 94 95 #define BSD_TO_LINUX_SIGNAL(sig) \ 96 (((sig) <= LINUX_SIGTBLSZ) ? bsd_to_linux_signal[_SIG_IDX(sig)] : sig) 97 98 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 99 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 100 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 101 RLIMIT_MEMLOCK, RLIMIT_AS 102 }; 103 104 struct l_sysinfo { 105 l_long uptime; /* Seconds since boot */ 106 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 107 #define LINUX_SYSINFO_LOADS_SCALE 65536 108 l_ulong totalram; /* Total usable main memory size */ 109 l_ulong freeram; /* Available memory size */ 110 l_ulong sharedram; /* Amount of shared memory */ 111 l_ulong bufferram; /* Memory used by buffers */ 112 l_ulong totalswap; /* Total swap space size */ 113 l_ulong freeswap; /* swap space still available */ 114 l_ushort procs; /* Number of current processes */ 115 l_ushort pads; 116 l_ulong totalbig; 117 l_ulong freebig; 118 l_uint mem_unit; 119 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 120 }; 121 int 122 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 123 { 124 struct l_sysinfo sysinfo; 125 vm_object_t object; 126 int i, j; 127 struct timespec ts; 128 129 getnanouptime(&ts); 130 if (ts.tv_nsec != 0) 131 ts.tv_sec++; 132 sysinfo.uptime = ts.tv_sec; 133 134 /* Use the information from the mib to get our load averages */ 135 for (i = 0; i < 3; i++) 136 sysinfo.loads[i] = averunnable.ldavg[i] * 137 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 138 139 sysinfo.totalram = physmem * PAGE_SIZE; 140 sysinfo.freeram = sysinfo.totalram - cnt.v_wire_count * PAGE_SIZE; 141 142 sysinfo.sharedram = 0; 143 mtx_lock(&vm_object_list_mtx); 144 TAILQ_FOREACH(object, &vm_object_list, object_list) 145 if (object->shadow_count > 1) 146 sysinfo.sharedram += object->resident_page_count; 147 mtx_unlock(&vm_object_list_mtx); 148 149 sysinfo.sharedram *= PAGE_SIZE; 150 sysinfo.bufferram = 0; 151 152 swap_pager_status(&i, &j); 153 sysinfo.totalswap = i * PAGE_SIZE; 154 sysinfo.freeswap = (i - j) * PAGE_SIZE; 155 156 sysinfo.procs = nprocs; 157 158 /* The following are only present in newer Linux kernels. */ 159 sysinfo.totalbig = 0; 160 sysinfo.freebig = 0; 161 sysinfo.mem_unit = 1; 162 163 return copyout(&sysinfo, args->info, sizeof(sysinfo)); 164 } 165 166 int 167 linux_alarm(struct thread *td, struct linux_alarm_args *args) 168 { 169 struct itimerval it, old_it; 170 u_int secs; 171 int error; 172 173 #ifdef DEBUG 174 if (ldebug(alarm)) 175 printf(ARGS(alarm, "%u"), args->secs); 176 #endif 177 178 secs = args->secs; 179 180 if (secs > INT_MAX) 181 secs = INT_MAX; 182 183 it.it_value.tv_sec = (long) secs; 184 it.it_value.tv_usec = 0; 185 it.it_interval.tv_sec = 0; 186 it.it_interval.tv_usec = 0; 187 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 188 if (error) 189 return (error); 190 if (timevalisset(&old_it.it_value)) { 191 if (old_it.it_value.tv_usec != 0) 192 old_it.it_value.tv_sec++; 193 td->td_retval[0] = old_it.it_value.tv_sec; 194 } 195 return (0); 196 } 197 198 int 199 linux_brk(struct thread *td, struct linux_brk_args *args) 200 { 201 struct vmspace *vm = td->td_proc->p_vmspace; 202 vm_offset_t new, old; 203 struct obreak_args /* { 204 char * nsize; 205 } */ tmp; 206 207 #ifdef DEBUG 208 if (ldebug(brk)) 209 printf(ARGS(brk, "%p"), (void *)(uintptr_t)args->dsend); 210 #endif 211 old = (vm_offset_t)vm->vm_daddr + ctob(vm->vm_dsize); 212 new = (vm_offset_t)args->dsend; 213 tmp.nsize = (char *)new; 214 if (((caddr_t)new > vm->vm_daddr) && !obreak(td, &tmp)) 215 td->td_retval[0] = (long)new; 216 else 217 td->td_retval[0] = (long)old; 218 219 return 0; 220 } 221 222 #if defined(__i386__) 223 /* XXX: what about amd64/linux32? */ 224 225 int 226 linux_uselib(struct thread *td, struct linux_uselib_args *args) 227 { 228 struct nameidata ni; 229 struct vnode *vp; 230 struct exec *a_out; 231 struct vattr attr; 232 vm_offset_t vmaddr; 233 unsigned long file_offset; 234 vm_offset_t buffer; 235 unsigned long bss_size; 236 char *library; 237 int error; 238 int locked, vfslocked; 239 240 LCONVPATHEXIST(td, args->library, &library); 241 242 #ifdef DEBUG 243 if (ldebug(uselib)) 244 printf(ARGS(uselib, "%s"), library); 245 #endif 246 247 a_out = NULL; 248 vfslocked = 0; 249 locked = 0; 250 vp = NULL; 251 252 NDINIT(&ni, LOOKUP, ISOPEN | FOLLOW | LOCKLEAF | MPSAFE | AUDITVNODE1, 253 UIO_SYSSPACE, library, td); 254 error = namei(&ni); 255 LFREEPATH(library); 256 if (error) 257 goto cleanup; 258 259 vp = ni.ni_vp; 260 vfslocked = NDHASGIANT(&ni); 261 NDFREE(&ni, NDF_ONLY_PNBUF); 262 263 /* 264 * From here on down, we have a locked vnode that must be unlocked. 265 * XXX: The code below largely duplicates exec_check_permissions(). 266 */ 267 locked = 1; 268 269 /* Writable? */ 270 if (vp->v_writecount) { 271 error = ETXTBSY; 272 goto cleanup; 273 } 274 275 /* Executable? */ 276 error = VOP_GETATTR(vp, &attr, td->td_ucred); 277 if (error) 278 goto cleanup; 279 280 if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 281 ((attr.va_mode & 0111) == 0) || (attr.va_type != VREG)) { 282 /* EACCESS is what exec(2) returns. */ 283 error = ENOEXEC; 284 goto cleanup; 285 } 286 287 /* Sensible size? */ 288 if (attr.va_size == 0) { 289 error = ENOEXEC; 290 goto cleanup; 291 } 292 293 /* Can we access it? */ 294 error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 295 if (error) 296 goto cleanup; 297 298 /* 299 * XXX: This should use vn_open() so that it is properly authorized, 300 * and to reduce code redundancy all over the place here. 301 * XXX: Not really, it duplicates far more of exec_check_permissions() 302 * than vn_open(). 303 */ 304 #ifdef MAC 305 error = mac_vnode_check_open(td->td_ucred, vp, VREAD); 306 if (error) 307 goto cleanup; 308 #endif 309 error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL); 310 if (error) 311 goto cleanup; 312 313 /* Pull in executable header into kernel_map */ 314 error = vm_mmap(kernel_map, (vm_offset_t *)&a_out, PAGE_SIZE, 315 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 0); 316 if (error) 317 goto cleanup; 318 319 /* Is it a Linux binary ? */ 320 if (((a_out->a_magic >> 16) & 0xff) != 0x64) { 321 error = ENOEXEC; 322 goto cleanup; 323 } 324 325 /* 326 * While we are here, we should REALLY do some more checks 327 */ 328 329 /* Set file/virtual offset based on a.out variant. */ 330 switch ((int)(a_out->a_magic & 0xffff)) { 331 case 0413: /* ZMAGIC */ 332 file_offset = 1024; 333 break; 334 case 0314: /* QMAGIC */ 335 file_offset = 0; 336 break; 337 default: 338 error = ENOEXEC; 339 goto cleanup; 340 } 341 342 bss_size = round_page(a_out->a_bss); 343 344 /* Check various fields in header for validity/bounds. */ 345 if (a_out->a_text & PAGE_MASK || a_out->a_data & PAGE_MASK) { 346 error = ENOEXEC; 347 goto cleanup; 348 } 349 350 /* text + data can't exceed file size */ 351 if (a_out->a_data + a_out->a_text > attr.va_size) { 352 error = EFAULT; 353 goto cleanup; 354 } 355 356 /* 357 * text/data/bss must not exceed limits 358 * XXX - this is not complete. it should check current usage PLUS 359 * the resources needed by this library. 360 */ 361 PROC_LOCK(td->td_proc); 362 if (a_out->a_text > maxtsiz || 363 a_out->a_data + bss_size > lim_cur(td->td_proc, RLIMIT_DATA)) { 364 PROC_UNLOCK(td->td_proc); 365 error = ENOMEM; 366 goto cleanup; 367 } 368 PROC_UNLOCK(td->td_proc); 369 370 /* 371 * Prevent more writers. 372 * XXX: Note that if any of the VM operations fail below we don't 373 * clear this flag. 374 */ 375 vp->v_vflag |= VV_TEXT; 376 377 /* 378 * Lock no longer needed 379 */ 380 locked = 0; 381 VOP_UNLOCK(vp, 0); 382 VFS_UNLOCK_GIANT(vfslocked); 383 384 /* 385 * Check if file_offset page aligned. Currently we cannot handle 386 * misalinged file offsets, and so we read in the entire image 387 * (what a waste). 388 */ 389 if (file_offset & PAGE_MASK) { 390 #ifdef DEBUG 391 printf("uselib: Non page aligned binary %lu\n", file_offset); 392 #endif 393 /* Map text+data read/write/execute */ 394 395 /* a_entry is the load address and is page aligned */ 396 vmaddr = trunc_page(a_out->a_entry); 397 398 /* get anon user mapping, read+write+execute */ 399 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 400 &vmaddr, a_out->a_text + a_out->a_data, FALSE, VM_PROT_ALL, 401 VM_PROT_ALL, 0); 402 if (error) 403 goto cleanup; 404 405 /* map file into kernel_map */ 406 error = vm_mmap(kernel_map, &buffer, 407 round_page(a_out->a_text + a_out->a_data + file_offset), 408 VM_PROT_READ, VM_PROT_READ, 0, OBJT_VNODE, vp, 409 trunc_page(file_offset)); 410 if (error) 411 goto cleanup; 412 413 /* copy from kernel VM space to user space */ 414 error = copyout(PTRIN(buffer + file_offset), 415 (void *)vmaddr, a_out->a_text + a_out->a_data); 416 417 /* release temporary kernel space */ 418 vm_map_remove(kernel_map, buffer, buffer + 419 round_page(a_out->a_text + a_out->a_data + file_offset)); 420 421 if (error) 422 goto cleanup; 423 } else { 424 #ifdef DEBUG 425 printf("uselib: Page aligned binary %lu\n", file_offset); 426 #endif 427 /* 428 * for QMAGIC, a_entry is 20 bytes beyond the load address 429 * to skip the executable header 430 */ 431 vmaddr = trunc_page(a_out->a_entry); 432 433 /* 434 * Map it all into the process's space as a single 435 * copy-on-write "data" segment. 436 */ 437 error = vm_mmap(&td->td_proc->p_vmspace->vm_map, &vmaddr, 438 a_out->a_text + a_out->a_data, VM_PROT_ALL, VM_PROT_ALL, 439 MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, file_offset); 440 if (error) 441 goto cleanup; 442 } 443 #ifdef DEBUG 444 printf("mem=%08lx = %08lx %08lx\n", (long)vmaddr, ((long *)vmaddr)[0], 445 ((long *)vmaddr)[1]); 446 #endif 447 if (bss_size != 0) { 448 /* Calculate BSS start address */ 449 vmaddr = trunc_page(a_out->a_entry) + a_out->a_text + 450 a_out->a_data; 451 452 /* allocate some 'anon' space */ 453 error = vm_map_find(&td->td_proc->p_vmspace->vm_map, NULL, 0, 454 &vmaddr, bss_size, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0); 455 if (error) 456 goto cleanup; 457 } 458 459 cleanup: 460 /* Unlock vnode if needed */ 461 if (locked) { 462 VOP_UNLOCK(vp, 0); 463 VFS_UNLOCK_GIANT(vfslocked); 464 } 465 466 /* Release the kernel mapping. */ 467 if (a_out) 468 vm_map_remove(kernel_map, (vm_offset_t)a_out, 469 (vm_offset_t)a_out + PAGE_SIZE); 470 471 return error; 472 } 473 474 #endif /* __i386__ */ 475 476 int 477 linux_select(struct thread *td, struct linux_select_args *args) 478 { 479 l_timeval ltv; 480 struct timeval tv0, tv1, utv, *tvp; 481 int error; 482 483 #ifdef DEBUG 484 if (ldebug(select)) 485 printf(ARGS(select, "%d, %p, %p, %p, %p"), args->nfds, 486 (void *)args->readfds, (void *)args->writefds, 487 (void *)args->exceptfds, (void *)args->timeout); 488 #endif 489 490 /* 491 * Store current time for computation of the amount of 492 * time left. 493 */ 494 if (args->timeout) { 495 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 496 goto select_out; 497 utv.tv_sec = ltv.tv_sec; 498 utv.tv_usec = ltv.tv_usec; 499 #ifdef DEBUG 500 if (ldebug(select)) 501 printf(LMSG("incoming timeout (%jd/%ld)"), 502 (intmax_t)utv.tv_sec, utv.tv_usec); 503 #endif 504 505 if (itimerfix(&utv)) { 506 /* 507 * The timeval was invalid. Convert it to something 508 * valid that will act as it does under Linux. 509 */ 510 utv.tv_sec += utv.tv_usec / 1000000; 511 utv.tv_usec %= 1000000; 512 if (utv.tv_usec < 0) { 513 utv.tv_sec -= 1; 514 utv.tv_usec += 1000000; 515 } 516 if (utv.tv_sec < 0) 517 timevalclear(&utv); 518 } 519 microtime(&tv0); 520 tvp = &utv; 521 } else 522 tvp = NULL; 523 524 error = kern_select(td, args->nfds, args->readfds, args->writefds, 525 args->exceptfds, tvp); 526 527 #ifdef DEBUG 528 if (ldebug(select)) 529 printf(LMSG("real select returns %d"), error); 530 #endif 531 if (error) 532 goto select_out; 533 534 if (args->timeout) { 535 if (td->td_retval[0]) { 536 /* 537 * Compute how much time was left of the timeout, 538 * by subtracting the current time and the time 539 * before we started the call, and subtracting 540 * that result from the user-supplied value. 541 */ 542 microtime(&tv1); 543 timevalsub(&tv1, &tv0); 544 timevalsub(&utv, &tv1); 545 if (utv.tv_sec < 0) 546 timevalclear(&utv); 547 } else 548 timevalclear(&utv); 549 #ifdef DEBUG 550 if (ldebug(select)) 551 printf(LMSG("outgoing timeout (%jd/%ld)"), 552 (intmax_t)utv.tv_sec, utv.tv_usec); 553 #endif 554 ltv.tv_sec = utv.tv_sec; 555 ltv.tv_usec = utv.tv_usec; 556 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 557 goto select_out; 558 } 559 560 select_out: 561 #ifdef DEBUG 562 if (ldebug(select)) 563 printf(LMSG("select_out -> %d"), error); 564 #endif 565 return error; 566 } 567 568 int 569 linux_mremap(struct thread *td, struct linux_mremap_args *args) 570 { 571 struct munmap_args /* { 572 void *addr; 573 size_t len; 574 } */ bsd_args; 575 int error = 0; 576 577 #ifdef DEBUG 578 if (ldebug(mremap)) 579 printf(ARGS(mremap, "%p, %08lx, %08lx, %08lx"), 580 (void *)(uintptr_t)args->addr, 581 (unsigned long)args->old_len, 582 (unsigned long)args->new_len, 583 (unsigned long)args->flags); 584 #endif 585 586 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 587 td->td_retval[0] = 0; 588 return (EINVAL); 589 } 590 591 /* 592 * Check for the page alignment. 593 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 594 */ 595 if (args->addr & PAGE_MASK) { 596 td->td_retval[0] = 0; 597 return (EINVAL); 598 } 599 600 args->new_len = round_page(args->new_len); 601 args->old_len = round_page(args->old_len); 602 603 if (args->new_len > args->old_len) { 604 td->td_retval[0] = 0; 605 return ENOMEM; 606 } 607 608 if (args->new_len < args->old_len) { 609 bsd_args.addr = 610 (caddr_t)((uintptr_t)args->addr + args->new_len); 611 bsd_args.len = args->old_len - args->new_len; 612 error = munmap(td, &bsd_args); 613 } 614 615 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 616 return error; 617 } 618 619 #define LINUX_MS_ASYNC 0x0001 620 #define LINUX_MS_INVALIDATE 0x0002 621 #define LINUX_MS_SYNC 0x0004 622 623 int 624 linux_msync(struct thread *td, struct linux_msync_args *args) 625 { 626 struct msync_args bsd_args; 627 628 bsd_args.addr = (caddr_t)(uintptr_t)args->addr; 629 bsd_args.len = (uintptr_t)args->len; 630 bsd_args.flags = args->fl & ~LINUX_MS_SYNC; 631 632 return msync(td, &bsd_args); 633 } 634 635 int 636 linux_time(struct thread *td, struct linux_time_args *args) 637 { 638 struct timeval tv; 639 l_time_t tm; 640 int error; 641 642 #ifdef DEBUG 643 if (ldebug(time)) 644 printf(ARGS(time, "*")); 645 #endif 646 647 microtime(&tv); 648 tm = tv.tv_sec; 649 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 650 return error; 651 td->td_retval[0] = tm; 652 return 0; 653 } 654 655 struct l_times_argv { 656 l_clock_t tms_utime; 657 l_clock_t tms_stime; 658 l_clock_t tms_cutime; 659 l_clock_t tms_cstime; 660 }; 661 662 663 /* 664 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value. 665 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK 666 * auxiliary vector entry. 667 */ 668 #define CLK_TCK 100 669 670 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 671 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) 672 673 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER_2004000 ? \ 674 CONVNTCK(r) : CONVOTCK(r)) 675 676 int 677 linux_times(struct thread *td, struct linux_times_args *args) 678 { 679 struct timeval tv, utime, stime, cutime, cstime; 680 struct l_times_argv tms; 681 struct proc *p; 682 int error; 683 684 #ifdef DEBUG 685 if (ldebug(times)) 686 printf(ARGS(times, "*")); 687 #endif 688 689 if (args->buf != NULL) { 690 p = td->td_proc; 691 PROC_LOCK(p); 692 PROC_SLOCK(p); 693 calcru(p, &utime, &stime); 694 PROC_SUNLOCK(p); 695 calccru(p, &cutime, &cstime); 696 PROC_UNLOCK(p); 697 698 tms.tms_utime = CONVTCK(utime); 699 tms.tms_stime = CONVTCK(stime); 700 701 tms.tms_cutime = CONVTCK(cutime); 702 tms.tms_cstime = CONVTCK(cstime); 703 704 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 705 return error; 706 } 707 708 microuptime(&tv); 709 td->td_retval[0] = (int)CONVTCK(tv); 710 return 0; 711 } 712 713 int 714 linux_newuname(struct thread *td, struct linux_newuname_args *args) 715 { 716 struct l_new_utsname utsname; 717 char osname[LINUX_MAX_UTSNAME]; 718 char osrelease[LINUX_MAX_UTSNAME]; 719 struct prison *pr; 720 char *p; 721 722 #ifdef DEBUG 723 if (ldebug(newuname)) 724 printf(ARGS(newuname, "*")); 725 #endif 726 727 linux_get_osname(td, osname); 728 linux_get_osrelease(td, osrelease); 729 730 bzero(&utsname, sizeof(utsname)); 731 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 732 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 733 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 734 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 735 for (p = utsname.version; *p != '\0'; ++p) 736 if (*p == '\n') { 737 *p = '\0'; 738 break; 739 } 740 strlcpy(utsname.machine, linux_platform, LINUX_MAX_UTSNAME); 741 742 pr = td->td_ucred->cr_prison; 743 mtx_lock(&pr->pr_mtx); 744 strlcpy(utsname.domainname, pr->pr_domain, LINUX_MAX_UTSNAME); 745 mtx_unlock(&pr->pr_mtx); 746 747 return (copyout(&utsname, args->buf, sizeof(utsname))); 748 } 749 750 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 751 struct l_utimbuf { 752 l_time_t l_actime; 753 l_time_t l_modtime; 754 }; 755 756 int 757 linux_utime(struct thread *td, struct linux_utime_args *args) 758 { 759 struct timeval tv[2], *tvp; 760 struct l_utimbuf lut; 761 char *fname; 762 int error; 763 764 LCONVPATHEXIST(td, args->fname, &fname); 765 766 #ifdef DEBUG 767 if (ldebug(utime)) 768 printf(ARGS(utime, "%s, *"), fname); 769 #endif 770 771 if (args->times) { 772 if ((error = copyin(args->times, &lut, sizeof lut))) { 773 LFREEPATH(fname); 774 return error; 775 } 776 tv[0].tv_sec = lut.l_actime; 777 tv[0].tv_usec = 0; 778 tv[1].tv_sec = lut.l_modtime; 779 tv[1].tv_usec = 0; 780 tvp = tv; 781 } else 782 tvp = NULL; 783 784 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 785 LFREEPATH(fname); 786 return (error); 787 } 788 789 int 790 linux_utimes(struct thread *td, struct linux_utimes_args *args) 791 { 792 l_timeval ltv[2]; 793 struct timeval tv[2], *tvp = NULL; 794 char *fname; 795 int error; 796 797 LCONVPATHEXIST(td, args->fname, &fname); 798 799 #ifdef DEBUG 800 if (ldebug(utimes)) 801 printf(ARGS(utimes, "%s, *"), fname); 802 #endif 803 804 if (args->tptr != NULL) { 805 if ((error = copyin(args->tptr, ltv, sizeof ltv))) { 806 LFREEPATH(fname); 807 return (error); 808 } 809 tv[0].tv_sec = ltv[0].tv_sec; 810 tv[0].tv_usec = ltv[0].tv_usec; 811 tv[1].tv_sec = ltv[1].tv_sec; 812 tv[1].tv_usec = ltv[1].tv_usec; 813 tvp = tv; 814 } 815 816 error = kern_utimes(td, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 817 LFREEPATH(fname); 818 return (error); 819 } 820 821 int 822 linux_futimesat(struct thread *td, struct linux_futimesat_args *args) 823 { 824 l_timeval ltv[2]; 825 struct timeval tv[2], *tvp = NULL; 826 char *fname; 827 int error, dfd; 828 829 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 830 LCONVPATHEXIST_AT(td, args->filename, &fname, dfd); 831 832 #ifdef DEBUG 833 if (ldebug(futimesat)) 834 printf(ARGS(futimesat, "%s, *"), fname); 835 #endif 836 837 if (args->utimes != NULL) { 838 if ((error = copyin(args->utimes, ltv, sizeof ltv))) { 839 LFREEPATH(fname); 840 return (error); 841 } 842 tv[0].tv_sec = ltv[0].tv_sec; 843 tv[0].tv_usec = ltv[0].tv_usec; 844 tv[1].tv_sec = ltv[1].tv_sec; 845 tv[1].tv_usec = ltv[1].tv_usec; 846 tvp = tv; 847 } 848 849 error = kern_utimesat(td, dfd, fname, UIO_SYSSPACE, tvp, UIO_SYSSPACE); 850 LFREEPATH(fname); 851 return (error); 852 } 853 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 854 855 #define __WCLONE 0x80000000 856 857 int 858 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 859 { 860 int error, options, tmpstat; 861 862 #ifdef DEBUG 863 if (ldebug(waitpid)) 864 printf(ARGS(waitpid, "%d, %p, %d"), 865 args->pid, (void *)args->status, args->options); 866 #endif 867 /* 868 * this is necessary because the test in kern_wait doesn't work 869 * because we mess with the options here 870 */ 871 if (args->options & ~(WUNTRACED | WNOHANG | WCONTINUED | __WCLONE)) 872 return (EINVAL); 873 874 options = (args->options & (WNOHANG | WUNTRACED)); 875 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 876 if (args->options & __WCLONE) 877 options |= WLINUXCLONE; 878 879 error = kern_wait(td, args->pid, &tmpstat, options, NULL); 880 if (error) 881 return error; 882 883 if (args->status) { 884 tmpstat &= 0xffff; 885 if (WIFSIGNALED(tmpstat)) 886 tmpstat = (tmpstat & 0xffffff80) | 887 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 888 else if (WIFSTOPPED(tmpstat)) 889 tmpstat = (tmpstat & 0xffff00ff) | 890 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 891 return copyout(&tmpstat, args->status, sizeof(int)); 892 } 893 894 return 0; 895 } 896 897 int 898 linux_wait4(struct thread *td, struct linux_wait4_args *args) 899 { 900 int error, options, tmpstat; 901 struct rusage ru, *rup; 902 struct proc *p; 903 904 #ifdef DEBUG 905 if (ldebug(wait4)) 906 printf(ARGS(wait4, "%d, %p, %d, %p"), 907 args->pid, (void *)args->status, args->options, 908 (void *)args->rusage); 909 #endif 910 911 options = (args->options & (WNOHANG | WUNTRACED)); 912 /* WLINUXCLONE should be equal to __WCLONE, but we make sure */ 913 if (args->options & __WCLONE) 914 options |= WLINUXCLONE; 915 916 if (args->rusage != NULL) 917 rup = &ru; 918 else 919 rup = NULL; 920 error = kern_wait(td, args->pid, &tmpstat, options, rup); 921 if (error) 922 return error; 923 924 p = td->td_proc; 925 PROC_LOCK(p); 926 sigqueue_delete(&p->p_sigqueue, SIGCHLD); 927 PROC_UNLOCK(p); 928 929 if (args->status) { 930 tmpstat &= 0xffff; 931 if (WIFSIGNALED(tmpstat)) 932 tmpstat = (tmpstat & 0xffffff80) | 933 BSD_TO_LINUX_SIGNAL(WTERMSIG(tmpstat)); 934 else if (WIFSTOPPED(tmpstat)) 935 tmpstat = (tmpstat & 0xffff00ff) | 936 (BSD_TO_LINUX_SIGNAL(WSTOPSIG(tmpstat)) << 8); 937 error = copyout(&tmpstat, args->status, sizeof(int)); 938 } 939 if (args->rusage != NULL && error == 0) 940 error = copyout(&ru, args->rusage, sizeof(ru)); 941 942 return (error); 943 } 944 945 int 946 linux_mknod(struct thread *td, struct linux_mknod_args *args) 947 { 948 char *path; 949 int error; 950 951 LCONVPATHCREAT(td, args->path, &path); 952 953 #ifdef DEBUG 954 if (ldebug(mknod)) 955 printf(ARGS(mknod, "%s, %d, %d"), path, args->mode, args->dev); 956 #endif 957 958 switch (args->mode & S_IFMT) { 959 case S_IFIFO: 960 case S_IFSOCK: 961 error = kern_mkfifo(td, path, UIO_SYSSPACE, args->mode); 962 break; 963 964 case S_IFCHR: 965 case S_IFBLK: 966 error = kern_mknod(td, path, UIO_SYSSPACE, args->mode, 967 args->dev); 968 break; 969 970 case S_IFDIR: 971 error = EPERM; 972 break; 973 974 case 0: 975 args->mode |= S_IFREG; 976 /* FALLTHROUGH */ 977 case S_IFREG: 978 error = kern_open(td, path, UIO_SYSSPACE, 979 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 980 if (error == 0) 981 kern_close(td, td->td_retval[0]); 982 break; 983 984 default: 985 error = EINVAL; 986 break; 987 } 988 LFREEPATH(path); 989 return (error); 990 } 991 992 int 993 linux_mknodat(struct thread *td, struct linux_mknodat_args *args) 994 { 995 char *path; 996 int error, dfd; 997 998 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 999 LCONVPATHCREAT_AT(td, args->filename, &path, dfd); 1000 1001 #ifdef DEBUG 1002 if (ldebug(mknodat)) 1003 printf(ARGS(mknodat, "%s, %d, %d"), path, args->mode, args->dev); 1004 #endif 1005 1006 switch (args->mode & S_IFMT) { 1007 case S_IFIFO: 1008 case S_IFSOCK: 1009 error = kern_mkfifoat(td, dfd, path, UIO_SYSSPACE, args->mode); 1010 break; 1011 1012 case S_IFCHR: 1013 case S_IFBLK: 1014 error = kern_mknodat(td, dfd, path, UIO_SYSSPACE, args->mode, 1015 args->dev); 1016 break; 1017 1018 case S_IFDIR: 1019 error = EPERM; 1020 break; 1021 1022 case 0: 1023 args->mode |= S_IFREG; 1024 /* FALLTHROUGH */ 1025 case S_IFREG: 1026 error = kern_openat(td, dfd, path, UIO_SYSSPACE, 1027 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 1028 if (error == 0) 1029 kern_close(td, td->td_retval[0]); 1030 break; 1031 1032 default: 1033 error = EINVAL; 1034 break; 1035 } 1036 LFREEPATH(path); 1037 return (error); 1038 } 1039 1040 /* 1041 * UGH! This is just about the dumbest idea I've ever heard!! 1042 */ 1043 int 1044 linux_personality(struct thread *td, struct linux_personality_args *args) 1045 { 1046 #ifdef DEBUG 1047 if (ldebug(personality)) 1048 printf(ARGS(personality, "%lu"), (unsigned long)args->per); 1049 #endif 1050 if (args->per != 0) 1051 return EINVAL; 1052 1053 /* Yes Jim, it's still a Linux... */ 1054 td->td_retval[0] = 0; 1055 return 0; 1056 } 1057 1058 struct l_itimerval { 1059 l_timeval it_interval; 1060 l_timeval it_value; 1061 }; 1062 1063 #define B2L_ITIMERVAL(bip, lip) \ 1064 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 1065 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 1066 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 1067 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 1068 1069 int 1070 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 1071 { 1072 int error; 1073 struct l_itimerval ls; 1074 struct itimerval aitv, oitv; 1075 1076 #ifdef DEBUG 1077 if (ldebug(setitimer)) 1078 printf(ARGS(setitimer, "%p, %p"), 1079 (void *)uap->itv, (void *)uap->oitv); 1080 #endif 1081 1082 if (uap->itv == NULL) { 1083 uap->itv = uap->oitv; 1084 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 1085 } 1086 1087 error = copyin(uap->itv, &ls, sizeof(ls)); 1088 if (error != 0) 1089 return (error); 1090 B2L_ITIMERVAL(&aitv, &ls); 1091 #ifdef DEBUG 1092 if (ldebug(setitimer)) { 1093 printf("setitimer: value: sec: %jd, usec: %ld\n", 1094 (intmax_t)aitv.it_value.tv_sec, aitv.it_value.tv_usec); 1095 printf("setitimer: interval: sec: %jd, usec: %ld\n", 1096 (intmax_t)aitv.it_interval.tv_sec, aitv.it_interval.tv_usec); 1097 } 1098 #endif 1099 error = kern_setitimer(td, uap->which, &aitv, &oitv); 1100 if (error != 0 || uap->oitv == NULL) 1101 return (error); 1102 B2L_ITIMERVAL(&ls, &oitv); 1103 1104 return (copyout(&ls, uap->oitv, sizeof(ls))); 1105 } 1106 1107 int 1108 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1109 { 1110 int error; 1111 struct l_itimerval ls; 1112 struct itimerval aitv; 1113 1114 #ifdef DEBUG 1115 if (ldebug(getitimer)) 1116 printf(ARGS(getitimer, "%p"), (void *)uap->itv); 1117 #endif 1118 error = kern_getitimer(td, uap->which, &aitv); 1119 if (error != 0) 1120 return (error); 1121 B2L_ITIMERVAL(&ls, &aitv); 1122 return (copyout(&ls, uap->itv, sizeof(ls))); 1123 } 1124 1125 int 1126 linux_nice(struct thread *td, struct linux_nice_args *args) 1127 { 1128 struct setpriority_args bsd_args; 1129 1130 bsd_args.which = PRIO_PROCESS; 1131 bsd_args.who = 0; /* current process */ 1132 bsd_args.prio = args->inc; 1133 return setpriority(td, &bsd_args); 1134 } 1135 1136 int 1137 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1138 { 1139 struct ucred *newcred, *oldcred; 1140 l_gid_t linux_gidset[NGROUPS]; 1141 gid_t *bsd_gidset; 1142 int ngrp, error; 1143 struct proc *p; 1144 1145 ngrp = args->gidsetsize; 1146 if (ngrp < 0 || ngrp >= NGROUPS) 1147 return (EINVAL); 1148 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1149 if (error) 1150 return (error); 1151 newcred = crget(); 1152 p = td->td_proc; 1153 PROC_LOCK(p); 1154 oldcred = p->p_ucred; 1155 1156 /* 1157 * cr_groups[0] holds egid. Setting the whole set from 1158 * the supplied set will cause egid to be changed too. 1159 * Keep cr_groups[0] unchanged to prevent that. 1160 */ 1161 1162 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS, 0)) != 0) { 1163 PROC_UNLOCK(p); 1164 crfree(newcred); 1165 return (error); 1166 } 1167 1168 crcopy(newcred, oldcred); 1169 if (ngrp > 0) { 1170 newcred->cr_ngroups = ngrp + 1; 1171 1172 bsd_gidset = newcred->cr_groups; 1173 ngrp--; 1174 while (ngrp >= 0) { 1175 bsd_gidset[ngrp + 1] = linux_gidset[ngrp]; 1176 ngrp--; 1177 } 1178 } else 1179 newcred->cr_ngroups = 1; 1180 1181 setsugid(p); 1182 p->p_ucred = newcred; 1183 PROC_UNLOCK(p); 1184 crfree(oldcred); 1185 return (0); 1186 } 1187 1188 int 1189 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1190 { 1191 struct ucred *cred; 1192 l_gid_t linux_gidset[NGROUPS]; 1193 gid_t *bsd_gidset; 1194 int bsd_gidsetsz, ngrp, error; 1195 1196 cred = td->td_ucred; 1197 bsd_gidset = cred->cr_groups; 1198 bsd_gidsetsz = cred->cr_ngroups - 1; 1199 1200 /* 1201 * cr_groups[0] holds egid. Returning the whole set 1202 * here will cause a duplicate. Exclude cr_groups[0] 1203 * to prevent that. 1204 */ 1205 1206 if ((ngrp = args->gidsetsize) == 0) { 1207 td->td_retval[0] = bsd_gidsetsz; 1208 return (0); 1209 } 1210 1211 if (ngrp < bsd_gidsetsz) 1212 return (EINVAL); 1213 1214 ngrp = 0; 1215 while (ngrp < bsd_gidsetsz) { 1216 linux_gidset[ngrp] = bsd_gidset[ngrp + 1]; 1217 ngrp++; 1218 } 1219 1220 if ((error = copyout(linux_gidset, args->grouplist, 1221 ngrp * sizeof(l_gid_t)))) 1222 return (error); 1223 1224 td->td_retval[0] = ngrp; 1225 return (0); 1226 } 1227 1228 int 1229 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1230 { 1231 struct rlimit bsd_rlim; 1232 struct l_rlimit rlim; 1233 u_int which; 1234 int error; 1235 1236 #ifdef DEBUG 1237 if (ldebug(setrlimit)) 1238 printf(ARGS(setrlimit, "%d, %p"), 1239 args->resource, (void *)args->rlim); 1240 #endif 1241 1242 if (args->resource >= LINUX_RLIM_NLIMITS) 1243 return (EINVAL); 1244 1245 which = linux_to_bsd_resource[args->resource]; 1246 if (which == -1) 1247 return (EINVAL); 1248 1249 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1250 if (error) 1251 return (error); 1252 1253 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1254 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1255 return (kern_setrlimit(td, which, &bsd_rlim)); 1256 } 1257 1258 int 1259 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1260 { 1261 struct l_rlimit rlim; 1262 struct proc *p = td->td_proc; 1263 struct rlimit bsd_rlim; 1264 u_int which; 1265 1266 #ifdef DEBUG 1267 if (ldebug(old_getrlimit)) 1268 printf(ARGS(old_getrlimit, "%d, %p"), 1269 args->resource, (void *)args->rlim); 1270 #endif 1271 1272 if (args->resource >= LINUX_RLIM_NLIMITS) 1273 return (EINVAL); 1274 1275 which = linux_to_bsd_resource[args->resource]; 1276 if (which == -1) 1277 return (EINVAL); 1278 1279 PROC_LOCK(p); 1280 lim_rlimit(p, which, &bsd_rlim); 1281 PROC_UNLOCK(p); 1282 1283 #ifdef COMPAT_LINUX32 1284 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1285 if (rlim.rlim_cur == UINT_MAX) 1286 rlim.rlim_cur = INT_MAX; 1287 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1288 if (rlim.rlim_max == UINT_MAX) 1289 rlim.rlim_max = INT_MAX; 1290 #else 1291 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1292 if (rlim.rlim_cur == ULONG_MAX) 1293 rlim.rlim_cur = LONG_MAX; 1294 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1295 if (rlim.rlim_max == ULONG_MAX) 1296 rlim.rlim_max = LONG_MAX; 1297 #endif 1298 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1299 } 1300 1301 int 1302 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1303 { 1304 struct l_rlimit rlim; 1305 struct proc *p = td->td_proc; 1306 struct rlimit bsd_rlim; 1307 u_int which; 1308 1309 #ifdef DEBUG 1310 if (ldebug(getrlimit)) 1311 printf(ARGS(getrlimit, "%d, %p"), 1312 args->resource, (void *)args->rlim); 1313 #endif 1314 1315 if (args->resource >= LINUX_RLIM_NLIMITS) 1316 return (EINVAL); 1317 1318 which = linux_to_bsd_resource[args->resource]; 1319 if (which == -1) 1320 return (EINVAL); 1321 1322 PROC_LOCK(p); 1323 lim_rlimit(p, which, &bsd_rlim); 1324 PROC_UNLOCK(p); 1325 1326 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1327 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1328 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1329 } 1330 1331 int 1332 linux_sched_setscheduler(struct thread *td, 1333 struct linux_sched_setscheduler_args *args) 1334 { 1335 struct sched_setscheduler_args bsd; 1336 1337 #ifdef DEBUG 1338 if (ldebug(sched_setscheduler)) 1339 printf(ARGS(sched_setscheduler, "%d, %d, %p"), 1340 args->pid, args->policy, (const void *)args->param); 1341 #endif 1342 1343 switch (args->policy) { 1344 case LINUX_SCHED_OTHER: 1345 bsd.policy = SCHED_OTHER; 1346 break; 1347 case LINUX_SCHED_FIFO: 1348 bsd.policy = SCHED_FIFO; 1349 break; 1350 case LINUX_SCHED_RR: 1351 bsd.policy = SCHED_RR; 1352 break; 1353 default: 1354 return EINVAL; 1355 } 1356 1357 bsd.pid = args->pid; 1358 bsd.param = (struct sched_param *)args->param; 1359 return sched_setscheduler(td, &bsd); 1360 } 1361 1362 int 1363 linux_sched_getscheduler(struct thread *td, 1364 struct linux_sched_getscheduler_args *args) 1365 { 1366 struct sched_getscheduler_args bsd; 1367 int error; 1368 1369 #ifdef DEBUG 1370 if (ldebug(sched_getscheduler)) 1371 printf(ARGS(sched_getscheduler, "%d"), args->pid); 1372 #endif 1373 1374 bsd.pid = args->pid; 1375 error = sched_getscheduler(td, &bsd); 1376 1377 switch (td->td_retval[0]) { 1378 case SCHED_OTHER: 1379 td->td_retval[0] = LINUX_SCHED_OTHER; 1380 break; 1381 case SCHED_FIFO: 1382 td->td_retval[0] = LINUX_SCHED_FIFO; 1383 break; 1384 case SCHED_RR: 1385 td->td_retval[0] = LINUX_SCHED_RR; 1386 break; 1387 } 1388 1389 return error; 1390 } 1391 1392 int 1393 linux_sched_get_priority_max(struct thread *td, 1394 struct linux_sched_get_priority_max_args *args) 1395 { 1396 struct sched_get_priority_max_args bsd; 1397 1398 #ifdef DEBUG 1399 if (ldebug(sched_get_priority_max)) 1400 printf(ARGS(sched_get_priority_max, "%d"), args->policy); 1401 #endif 1402 1403 switch (args->policy) { 1404 case LINUX_SCHED_OTHER: 1405 bsd.policy = SCHED_OTHER; 1406 break; 1407 case LINUX_SCHED_FIFO: 1408 bsd.policy = SCHED_FIFO; 1409 break; 1410 case LINUX_SCHED_RR: 1411 bsd.policy = SCHED_RR; 1412 break; 1413 default: 1414 return EINVAL; 1415 } 1416 return sched_get_priority_max(td, &bsd); 1417 } 1418 1419 int 1420 linux_sched_get_priority_min(struct thread *td, 1421 struct linux_sched_get_priority_min_args *args) 1422 { 1423 struct sched_get_priority_min_args bsd; 1424 1425 #ifdef DEBUG 1426 if (ldebug(sched_get_priority_min)) 1427 printf(ARGS(sched_get_priority_min, "%d"), args->policy); 1428 #endif 1429 1430 switch (args->policy) { 1431 case LINUX_SCHED_OTHER: 1432 bsd.policy = SCHED_OTHER; 1433 break; 1434 case LINUX_SCHED_FIFO: 1435 bsd.policy = SCHED_FIFO; 1436 break; 1437 case LINUX_SCHED_RR: 1438 bsd.policy = SCHED_RR; 1439 break; 1440 default: 1441 return EINVAL; 1442 } 1443 return sched_get_priority_min(td, &bsd); 1444 } 1445 1446 #define REBOOT_CAD_ON 0x89abcdef 1447 #define REBOOT_CAD_OFF 0 1448 #define REBOOT_HALT 0xcdef0123 1449 #define REBOOT_RESTART 0x01234567 1450 #define REBOOT_RESTART2 0xA1B2C3D4 1451 #define REBOOT_POWEROFF 0x4321FEDC 1452 #define REBOOT_MAGIC1 0xfee1dead 1453 #define REBOOT_MAGIC2 0x28121969 1454 #define REBOOT_MAGIC2A 0x05121996 1455 #define REBOOT_MAGIC2B 0x16041998 1456 1457 int 1458 linux_reboot(struct thread *td, struct linux_reboot_args *args) 1459 { 1460 struct reboot_args bsd_args; 1461 1462 #ifdef DEBUG 1463 if (ldebug(reboot)) 1464 printf(ARGS(reboot, "0x%x"), args->cmd); 1465 #endif 1466 1467 if (args->magic1 != REBOOT_MAGIC1) 1468 return EINVAL; 1469 1470 switch (args->magic2) { 1471 case REBOOT_MAGIC2: 1472 case REBOOT_MAGIC2A: 1473 case REBOOT_MAGIC2B: 1474 break; 1475 default: 1476 return EINVAL; 1477 } 1478 1479 switch (args->cmd) { 1480 case REBOOT_CAD_ON: 1481 case REBOOT_CAD_OFF: 1482 return (priv_check(td, PRIV_REBOOT)); 1483 case REBOOT_HALT: 1484 bsd_args.opt = RB_HALT; 1485 break; 1486 case REBOOT_RESTART: 1487 case REBOOT_RESTART2: 1488 bsd_args.opt = 0; 1489 break; 1490 case REBOOT_POWEROFF: 1491 bsd_args.opt = RB_POWEROFF; 1492 break; 1493 default: 1494 return EINVAL; 1495 } 1496 return reboot(td, &bsd_args); 1497 } 1498 1499 1500 /* 1501 * The FreeBSD native getpid(2), getgid(2) and getuid(2) also modify 1502 * td->td_retval[1] when COMPAT_43 is defined. This clobbers registers that 1503 * are assumed to be preserved. The following lightweight syscalls fixes 1504 * this. See also linux_getgid16() and linux_getuid16() in linux_uid16.c 1505 * 1506 * linux_getpid() - MP SAFE 1507 * linux_getgid() - MP SAFE 1508 * linux_getuid() - MP SAFE 1509 */ 1510 1511 int 1512 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1513 { 1514 struct linux_emuldata *em; 1515 1516 #ifdef DEBUG 1517 if (ldebug(getpid)) 1518 printf(ARGS(getpid, "")); 1519 #endif 1520 1521 if (linux_use26(td)) { 1522 em = em_find(td->td_proc, EMUL_DONTLOCK); 1523 KASSERT(em != NULL, ("getpid: emuldata not found.\n")); 1524 td->td_retval[0] = em->shared->group_pid; 1525 } else { 1526 td->td_retval[0] = td->td_proc->p_pid; 1527 } 1528 1529 return (0); 1530 } 1531 1532 int 1533 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1534 { 1535 1536 #ifdef DEBUG 1537 if (ldebug(gettid)) 1538 printf(ARGS(gettid, "")); 1539 #endif 1540 1541 td->td_retval[0] = td->td_proc->p_pid; 1542 return (0); 1543 } 1544 1545 1546 int 1547 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1548 { 1549 struct linux_emuldata *em; 1550 struct proc *p, *pp; 1551 1552 #ifdef DEBUG 1553 if (ldebug(getppid)) 1554 printf(ARGS(getppid, "")); 1555 #endif 1556 1557 if (!linux_use26(td)) { 1558 PROC_LOCK(td->td_proc); 1559 td->td_retval[0] = td->td_proc->p_pptr->p_pid; 1560 PROC_UNLOCK(td->td_proc); 1561 return (0); 1562 } 1563 1564 em = em_find(td->td_proc, EMUL_DONTLOCK); 1565 1566 KASSERT(em != NULL, ("getppid: process emuldata not found.\n")); 1567 1568 /* find the group leader */ 1569 p = pfind(em->shared->group_pid); 1570 1571 if (p == NULL) { 1572 #ifdef DEBUG 1573 printf(LMSG("parent process not found.\n")); 1574 #endif 1575 return (0); 1576 } 1577 1578 pp = p->p_pptr; /* switch to parent */ 1579 PROC_LOCK(pp); 1580 PROC_UNLOCK(p); 1581 1582 /* if its also linux process */ 1583 if (pp->p_sysent == &elf_linux_sysvec) { 1584 em = em_find(pp, EMUL_DONTLOCK); 1585 KASSERT(em != NULL, ("getppid: parent emuldata not found.\n")); 1586 1587 td->td_retval[0] = em->shared->group_pid; 1588 } else 1589 td->td_retval[0] = pp->p_pid; 1590 1591 PROC_UNLOCK(pp); 1592 1593 return (0); 1594 } 1595 1596 int 1597 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1598 { 1599 1600 #ifdef DEBUG 1601 if (ldebug(getgid)) 1602 printf(ARGS(getgid, "")); 1603 #endif 1604 1605 td->td_retval[0] = td->td_ucred->cr_rgid; 1606 return (0); 1607 } 1608 1609 int 1610 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1611 { 1612 1613 #ifdef DEBUG 1614 if (ldebug(getuid)) 1615 printf(ARGS(getuid, "")); 1616 #endif 1617 1618 td->td_retval[0] = td->td_ucred->cr_ruid; 1619 return (0); 1620 } 1621 1622 1623 int 1624 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1625 { 1626 struct getsid_args bsd; 1627 1628 #ifdef DEBUG 1629 if (ldebug(getsid)) 1630 printf(ARGS(getsid, "%i"), args->pid); 1631 #endif 1632 1633 bsd.pid = args->pid; 1634 return getsid(td, &bsd); 1635 } 1636 1637 int 1638 linux_nosys(struct thread *td, struct nosys_args *ignore) 1639 { 1640 1641 return (ENOSYS); 1642 } 1643 1644 int 1645 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1646 { 1647 struct getpriority_args bsd_args; 1648 int error; 1649 1650 #ifdef DEBUG 1651 if (ldebug(getpriority)) 1652 printf(ARGS(getpriority, "%i, %i"), args->which, args->who); 1653 #endif 1654 1655 bsd_args.which = args->which; 1656 bsd_args.who = args->who; 1657 error = getpriority(td, &bsd_args); 1658 td->td_retval[0] = 20 - td->td_retval[0]; 1659 return error; 1660 } 1661 1662 int 1663 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1664 { 1665 int name[2]; 1666 1667 #ifdef DEBUG 1668 if (ldebug(sethostname)) 1669 printf(ARGS(sethostname, "*, %i"), args->len); 1670 #endif 1671 1672 name[0] = CTL_KERN; 1673 name[1] = KERN_HOSTNAME; 1674 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1675 args->len, 0, 0)); 1676 } 1677 1678 int 1679 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args) 1680 { 1681 int name[2]; 1682 1683 #ifdef DEBUG 1684 if (ldebug(setdomainname)) 1685 printf(ARGS(setdomainname, "*, %i"), args->len); 1686 #endif 1687 1688 name[0] = CTL_KERN; 1689 name[1] = KERN_NISDOMAINNAME; 1690 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name, 1691 args->len, 0, 0)); 1692 } 1693 1694 int 1695 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1696 { 1697 struct linux_emuldata *em, *td_em, *tmp_em; 1698 struct proc *sp; 1699 1700 #ifdef DEBUG 1701 if (ldebug(exit_group)) 1702 printf(ARGS(exit_group, "%i"), args->error_code); 1703 #endif 1704 1705 if (linux_use26(td)) { 1706 td_em = em_find(td->td_proc, EMUL_DONTLOCK); 1707 1708 KASSERT(td_em != NULL, ("exit_group: emuldata not found.\n")); 1709 1710 EMUL_SHARED_RLOCK(&emul_shared_lock); 1711 LIST_FOREACH_SAFE(em, &td_em->shared->threads, threads, tmp_em) { 1712 if (em->pid == td_em->pid) 1713 continue; 1714 1715 sp = pfind(em->pid); 1716 psignal(sp, SIGKILL); 1717 PROC_UNLOCK(sp); 1718 #ifdef DEBUG 1719 printf(LMSG("linux_sys_exit_group: kill PID %d\n"), em->pid); 1720 #endif 1721 } 1722 1723 EMUL_SHARED_RUNLOCK(&emul_shared_lock); 1724 } 1725 /* 1726 * XXX: we should send a signal to the parent if 1727 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1728 * as it doesnt occur often. 1729 */ 1730 exit1(td, W_EXITCODE(args->error_code, 0)); 1731 1732 return (0); 1733 } 1734 1735 int 1736 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1737 { 1738 int error = 0, max_size; 1739 struct proc *p = td->td_proc; 1740 char comm[LINUX_MAX_COMM_LEN]; 1741 struct linux_emuldata *em; 1742 int pdeath_signal; 1743 1744 #ifdef DEBUG 1745 if (ldebug(prctl)) 1746 printf(ARGS(prctl, "%d, %d, %d, %d, %d"), args->option, 1747 args->arg2, args->arg3, args->arg4, args->arg5); 1748 #endif 1749 1750 switch (args->option) { 1751 case LINUX_PR_SET_PDEATHSIG: 1752 if (!LINUX_SIG_VALID(args->arg2)) 1753 return (EINVAL); 1754 em = em_find(p, EMUL_DOLOCK); 1755 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1756 em->pdeath_signal = args->arg2; 1757 EMUL_UNLOCK(&emul_lock); 1758 break; 1759 case LINUX_PR_GET_PDEATHSIG: 1760 em = em_find(p, EMUL_DOLOCK); 1761 KASSERT(em != NULL, ("prctl: emuldata not found.\n")); 1762 pdeath_signal = em->pdeath_signal; 1763 EMUL_UNLOCK(&emul_lock); 1764 error = copyout(&pdeath_signal, 1765 (void *)(register_t)args->arg2, 1766 sizeof(pdeath_signal)); 1767 break; 1768 case LINUX_PR_SET_NAME: 1769 /* 1770 * To be on the safe side we need to make sure to not 1771 * overflow the size a linux program expects. We already 1772 * do this here in the copyin, so that we don't need to 1773 * check on copyout. 1774 */ 1775 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1776 error = copyinstr((void *)(register_t)args->arg2, comm, 1777 max_size, NULL); 1778 1779 /* Linux silently truncates the name if it is too long. */ 1780 if (error == ENAMETOOLONG) { 1781 /* 1782 * XXX: copyinstr() isn't documented to populate the 1783 * array completely, so do a copyin() to be on the 1784 * safe side. This should be changed in case 1785 * copyinstr() is changed to guarantee this. 1786 */ 1787 error = copyin((void *)(register_t)args->arg2, comm, 1788 max_size - 1); 1789 comm[max_size - 1] = '\0'; 1790 } 1791 if (error) 1792 return (error); 1793 1794 PROC_LOCK(p); 1795 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 1796 PROC_UNLOCK(p); 1797 break; 1798 case LINUX_PR_GET_NAME: 1799 PROC_LOCK(p); 1800 strlcpy(comm, p->p_comm, sizeof(comm)); 1801 PROC_UNLOCK(p); 1802 error = copyout(comm, (void *)(register_t)args->arg2, 1803 strlen(comm) + 1); 1804 break; 1805 default: 1806 error = EINVAL; 1807 break; 1808 } 1809 1810 return (error); 1811 } 1812 1813 /* 1814 * Get affinity of a process. 1815 */ 1816 int 1817 linux_sched_getaffinity(struct thread *td, 1818 struct linux_sched_getaffinity_args *args) 1819 { 1820 int error; 1821 struct cpuset_getaffinity_args cga; 1822 1823 #ifdef DEBUG 1824 if (ldebug(sched_getaffinity)) 1825 printf(ARGS(sched_getaffinity, "%d, %d, *"), args->pid, 1826 args->len); 1827 #endif 1828 if (args->len < sizeof(cpuset_t)) 1829 return (EINVAL); 1830 1831 cga.level = CPU_LEVEL_WHICH; 1832 cga.which = CPU_WHICH_PID; 1833 cga.id = args->pid; 1834 cga.cpusetsize = sizeof(cpuset_t); 1835 cga.mask = (cpuset_t *) args->user_mask_ptr; 1836 1837 if ((error = cpuset_getaffinity(td, &cga)) == 0) 1838 td->td_retval[0] = sizeof(cpuset_t); 1839 1840 return (error); 1841 } 1842 1843 /* 1844 * Set affinity of a process. 1845 */ 1846 int 1847 linux_sched_setaffinity(struct thread *td, 1848 struct linux_sched_setaffinity_args *args) 1849 { 1850 struct cpuset_setaffinity_args csa; 1851 1852 #ifdef DEBUG 1853 if (ldebug(sched_setaffinity)) 1854 printf(ARGS(sched_setaffinity, "%d, %d, *"), args->pid, 1855 args->len); 1856 #endif 1857 if (args->len < sizeof(cpuset_t)) 1858 return (EINVAL); 1859 1860 csa.level = CPU_LEVEL_WHICH; 1861 csa.which = CPU_WHICH_PID; 1862 csa.id = args->pid; 1863 csa.cpusetsize = sizeof(cpuset_t); 1864 csa.mask = (cpuset_t *) args->user_mask_ptr; 1865 1866 return (cpuset_setaffinity(td, &csa)); 1867 } 1868