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