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