1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2002 Doug Rabson 5 * Copyright (c) 1994-1995 Søren Schmidt 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/param.h> 33 #include <sys/fcntl.h> 34 #include <sys/jail.h> 35 #include <sys/imgact.h> 36 #include <sys/limits.h> 37 #include <sys/lock.h> 38 #include <sys/msgbuf.h> 39 #include <sys/mqueue.h> 40 #include <sys/mutex.h> 41 #include <sys/poll.h> 42 #include <sys/priv.h> 43 #include <sys/proc.h> 44 #include <sys/procctl.h> 45 #include <sys/reboot.h> 46 #include <sys/random.h> 47 #include <sys/resourcevar.h> 48 #include <sys/rtprio.h> 49 #include <sys/sched.h> 50 #include <sys/smp.h> 51 #include <sys/stat.h> 52 #include <sys/syscallsubr.h> 53 #include <sys/sysctl.h> 54 #include <sys/sysent.h> 55 #include <sys/sysproto.h> 56 #include <sys/time.h> 57 #include <sys/vmmeter.h> 58 #include <sys/vnode.h> 59 60 #include <security/audit/audit.h> 61 #include <security/mac/mac_framework.h> 62 63 #include <vm/pmap.h> 64 #include <vm/vm_map.h> 65 #include <vm/swap_pager.h> 66 67 #ifdef COMPAT_LINUX32 68 #include <machine/../linux32/linux.h> 69 #include <machine/../linux32/linux32_proto.h> 70 #else 71 #include <machine/../linux/linux.h> 72 #include <machine/../linux/linux_proto.h> 73 #endif 74 75 #include <compat/linux/linux_common.h> 76 #include <compat/linux/linux_dtrace.h> 77 #include <compat/linux/linux_file.h> 78 #include <compat/linux/linux_mib.h> 79 #include <compat/linux/linux_mmap.h> 80 #include <compat/linux/linux_signal.h> 81 #include <compat/linux/linux_time.h> 82 #include <compat/linux/linux_util.h> 83 #include <compat/linux/linux_emul.h> 84 #include <compat/linux/linux_misc.h> 85 86 int stclohz; /* Statistics clock frequency */ 87 88 static unsigned int linux_to_bsd_resource[LINUX_RLIM_NLIMITS] = { 89 RLIMIT_CPU, RLIMIT_FSIZE, RLIMIT_DATA, RLIMIT_STACK, 90 RLIMIT_CORE, RLIMIT_RSS, RLIMIT_NPROC, RLIMIT_NOFILE, 91 RLIMIT_MEMLOCK, RLIMIT_AS 92 }; 93 94 struct l_sysinfo { 95 l_long uptime; /* Seconds since boot */ 96 l_ulong loads[3]; /* 1, 5, and 15 minute load averages */ 97 #define LINUX_SYSINFO_LOADS_SCALE 65536 98 l_ulong totalram; /* Total usable main memory size */ 99 l_ulong freeram; /* Available memory size */ 100 l_ulong sharedram; /* Amount of shared memory */ 101 l_ulong bufferram; /* Memory used by buffers */ 102 l_ulong totalswap; /* Total swap space size */ 103 l_ulong freeswap; /* swap space still available */ 104 l_ushort procs; /* Number of current processes */ 105 l_ushort pads; 106 l_ulong totalhigh; 107 l_ulong freehigh; 108 l_uint mem_unit; 109 char _f[20-2*sizeof(l_long)-sizeof(l_int)]; /* padding */ 110 }; 111 112 struct l_pselect6arg { 113 l_uintptr_t ss; 114 l_size_t ss_len; 115 }; 116 117 static int linux_utimensat_lts_to_ts(struct l_timespec *, 118 struct timespec *); 119 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 120 static int linux_utimensat_lts64_to_ts(struct l_timespec64 *, 121 struct timespec *); 122 #endif 123 static int linux_common_utimensat(struct thread *, int, 124 const char *, struct timespec *, int); 125 static int linux_common_pselect6(struct thread *, l_int, 126 l_fd_set *, l_fd_set *, l_fd_set *, 127 struct timespec *, l_uintptr_t *); 128 static int linux_common_ppoll(struct thread *, struct pollfd *, 129 uint32_t, struct timespec *, l_sigset_t *, 130 l_size_t); 131 static int linux_pollin(struct thread *, struct pollfd *, 132 struct pollfd *, u_int); 133 static int linux_pollout(struct thread *, struct pollfd *, 134 struct pollfd *, u_int); 135 136 int 137 linux_sysinfo(struct thread *td, struct linux_sysinfo_args *args) 138 { 139 struct l_sysinfo sysinfo; 140 int i, j; 141 struct timespec ts; 142 143 bzero(&sysinfo, sizeof(sysinfo)); 144 getnanouptime(&ts); 145 if (ts.tv_nsec != 0) 146 ts.tv_sec++; 147 sysinfo.uptime = ts.tv_sec; 148 149 /* Use the information from the mib to get our load averages */ 150 for (i = 0; i < 3; i++) 151 sysinfo.loads[i] = averunnable.ldavg[i] * 152 LINUX_SYSINFO_LOADS_SCALE / averunnable.fscale; 153 154 sysinfo.totalram = physmem * PAGE_SIZE; 155 sysinfo.freeram = (u_long)vm_free_count() * PAGE_SIZE; 156 157 /* 158 * sharedram counts pages allocated to named, swap-backed objects such 159 * as shared memory segments and tmpfs files. There is no cheap way to 160 * compute this, so just leave the field unpopulated. Linux itself only 161 * started setting this field in the 3.x timeframe. 162 */ 163 sysinfo.sharedram = 0; 164 sysinfo.bufferram = 0; 165 166 swap_pager_status(&i, &j); 167 sysinfo.totalswap = i * PAGE_SIZE; 168 sysinfo.freeswap = (i - j) * PAGE_SIZE; 169 170 sysinfo.procs = nprocs; 171 172 /* 173 * Platforms supported by the emulation layer do not have a notion of 174 * high memory. 175 */ 176 sysinfo.totalhigh = 0; 177 sysinfo.freehigh = 0; 178 179 sysinfo.mem_unit = 1; 180 181 return (copyout(&sysinfo, args->info, sizeof(sysinfo))); 182 } 183 184 #ifdef LINUX_LEGACY_SYSCALLS 185 int 186 linux_alarm(struct thread *td, struct linux_alarm_args *args) 187 { 188 struct itimerval it, old_it; 189 u_int secs; 190 int error __diagused; 191 192 secs = args->secs; 193 /* 194 * Linux alarm() is always successful. Limit secs to INT32_MAX / 2 195 * to match kern_setitimer()'s limit to avoid error from it. 196 * 197 * XXX. Linux limit secs to INT_MAX on 32 and does not limit on 64-bit 198 * platforms. 199 */ 200 if (secs > INT32_MAX / 2) 201 secs = INT32_MAX / 2; 202 203 it.it_value.tv_sec = secs; 204 it.it_value.tv_usec = 0; 205 timevalclear(&it.it_interval); 206 error = kern_setitimer(td, ITIMER_REAL, &it, &old_it); 207 KASSERT(error == 0, ("kern_setitimer returns %d", error)); 208 209 if ((old_it.it_value.tv_sec == 0 && old_it.it_value.tv_usec > 0) || 210 old_it.it_value.tv_usec >= 500000) 211 old_it.it_value.tv_sec++; 212 td->td_retval[0] = old_it.it_value.tv_sec; 213 return (0); 214 } 215 #endif 216 217 int 218 linux_brk(struct thread *td, struct linux_brk_args *args) 219 { 220 struct vmspace *vm = td->td_proc->p_vmspace; 221 uintptr_t new, old; 222 223 old = (uintptr_t)vm->vm_daddr + ctob(vm->vm_dsize); 224 new = (uintptr_t)args->dsend; 225 if ((caddr_t)new > vm->vm_daddr && !kern_break(td, &new)) 226 td->td_retval[0] = (register_t)new; 227 else 228 td->td_retval[0] = (register_t)old; 229 230 return (0); 231 } 232 233 #ifdef LINUX_LEGACY_SYSCALLS 234 int 235 linux_select(struct thread *td, struct linux_select_args *args) 236 { 237 l_timeval ltv; 238 struct timeval tv0, tv1, utv, *tvp; 239 int error; 240 241 /* 242 * Store current time for computation of the amount of 243 * time left. 244 */ 245 if (args->timeout) { 246 if ((error = copyin(args->timeout, <v, sizeof(ltv)))) 247 goto select_out; 248 utv.tv_sec = ltv.tv_sec; 249 utv.tv_usec = ltv.tv_usec; 250 251 if (itimerfix(&utv)) { 252 /* 253 * The timeval was invalid. Convert it to something 254 * valid that will act as it does under Linux. 255 */ 256 utv.tv_sec += utv.tv_usec / 1000000; 257 utv.tv_usec %= 1000000; 258 if (utv.tv_usec < 0) { 259 utv.tv_sec -= 1; 260 utv.tv_usec += 1000000; 261 } 262 if (utv.tv_sec < 0) 263 timevalclear(&utv); 264 } 265 microtime(&tv0); 266 tvp = &utv; 267 } else 268 tvp = NULL; 269 270 error = kern_select(td, args->nfds, args->readfds, args->writefds, 271 args->exceptfds, tvp, LINUX_NFDBITS); 272 if (error) 273 goto select_out; 274 275 if (args->timeout) { 276 if (td->td_retval[0]) { 277 /* 278 * Compute how much time was left of the timeout, 279 * by subtracting the current time and the time 280 * before we started the call, and subtracting 281 * that result from the user-supplied value. 282 */ 283 microtime(&tv1); 284 timevalsub(&tv1, &tv0); 285 timevalsub(&utv, &tv1); 286 if (utv.tv_sec < 0) 287 timevalclear(&utv); 288 } else 289 timevalclear(&utv); 290 ltv.tv_sec = utv.tv_sec; 291 ltv.tv_usec = utv.tv_usec; 292 if ((error = copyout(<v, args->timeout, sizeof(ltv)))) 293 goto select_out; 294 } 295 296 select_out: 297 return (error); 298 } 299 #endif 300 301 int 302 linux_mremap(struct thread *td, struct linux_mremap_args *args) 303 { 304 uintptr_t addr; 305 size_t len; 306 int error = 0; 307 308 if (args->flags & ~(LINUX_MREMAP_FIXED | LINUX_MREMAP_MAYMOVE)) { 309 td->td_retval[0] = 0; 310 return (EINVAL); 311 } 312 313 /* 314 * Check for the page alignment. 315 * Linux defines PAGE_MASK to be FreeBSD ~PAGE_MASK. 316 */ 317 if (args->addr & PAGE_MASK) { 318 td->td_retval[0] = 0; 319 return (EINVAL); 320 } 321 322 args->new_len = round_page(args->new_len); 323 args->old_len = round_page(args->old_len); 324 325 if (args->new_len > args->old_len) { 326 td->td_retval[0] = 0; 327 return (ENOMEM); 328 } 329 330 if (args->new_len < args->old_len) { 331 addr = args->addr + args->new_len; 332 len = args->old_len - args->new_len; 333 error = kern_munmap(td, addr, len); 334 } 335 336 td->td_retval[0] = error ? 0 : (uintptr_t)args->addr; 337 return (error); 338 } 339 340 #define LINUX_MS_ASYNC 0x0001 341 #define LINUX_MS_INVALIDATE 0x0002 342 #define LINUX_MS_SYNC 0x0004 343 344 int 345 linux_msync(struct thread *td, struct linux_msync_args *args) 346 { 347 348 return (kern_msync(td, args->addr, args->len, 349 args->fl & ~LINUX_MS_SYNC)); 350 } 351 352 int 353 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 354 { 355 356 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, 357 uap->prot)); 358 } 359 360 int 361 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 362 { 363 364 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, 365 uap->behav)); 366 } 367 368 int 369 linux_mmap2(struct thread *td, struct linux_mmap2_args *uap) 370 { 371 #if defined(LINUX_ARCHWANT_MMAP2PGOFF) 372 /* 373 * For architectures with sizeof (off_t) < sizeof (loff_t) mmap is 374 * implemented with mmap2 syscall and the offset is represented in 375 * multiples of page size. 376 */ 377 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, 378 uap->flags, uap->fd, (uint64_t)(uint32_t)uap->pgoff * PAGE_SIZE)); 379 #else 380 return (linux_mmap_common(td, PTROUT(uap->addr), uap->len, uap->prot, 381 uap->flags, uap->fd, uap->pgoff)); 382 #endif 383 } 384 385 #ifdef LINUX_LEGACY_SYSCALLS 386 int 387 linux_time(struct thread *td, struct linux_time_args *args) 388 { 389 struct timeval tv; 390 l_time_t tm; 391 int error; 392 393 microtime(&tv); 394 tm = tv.tv_sec; 395 if (args->tm && (error = copyout(&tm, args->tm, sizeof(tm)))) 396 return (error); 397 td->td_retval[0] = tm; 398 return (0); 399 } 400 #endif 401 402 struct l_times_argv { 403 l_clock_t tms_utime; 404 l_clock_t tms_stime; 405 l_clock_t tms_cutime; 406 l_clock_t tms_cstime; 407 }; 408 409 /* 410 * Glibc versions prior to 2.2.1 always use hard-coded CLK_TCK value. 411 * Since 2.2.1 Glibc uses value exported from kernel via AT_CLKTCK 412 * auxiliary vector entry. 413 */ 414 #define CLK_TCK 100 415 416 #define CONVOTCK(r) (r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK)) 417 #define CONVNTCK(r) (r.tv_sec * stclohz + r.tv_usec / (1000000 / stclohz)) 418 419 #define CONVTCK(r) (linux_kernver(td) >= LINUX_KERNVER(2,4,0) ? \ 420 CONVNTCK(r) : CONVOTCK(r)) 421 422 int 423 linux_times(struct thread *td, struct linux_times_args *args) 424 { 425 struct timeval tv, utime, stime, cutime, cstime; 426 struct l_times_argv tms; 427 struct proc *p; 428 int error; 429 430 if (args->buf != NULL) { 431 p = td->td_proc; 432 PROC_LOCK(p); 433 PROC_STATLOCK(p); 434 calcru(p, &utime, &stime); 435 PROC_STATUNLOCK(p); 436 calccru(p, &cutime, &cstime); 437 PROC_UNLOCK(p); 438 439 tms.tms_utime = CONVTCK(utime); 440 tms.tms_stime = CONVTCK(stime); 441 442 tms.tms_cutime = CONVTCK(cutime); 443 tms.tms_cstime = CONVTCK(cstime); 444 445 if ((error = copyout(&tms, args->buf, sizeof(tms)))) 446 return (error); 447 } 448 449 microuptime(&tv); 450 td->td_retval[0] = (int)CONVTCK(tv); 451 return (0); 452 } 453 454 int 455 linux_newuname(struct thread *td, struct linux_newuname_args *args) 456 { 457 struct l_new_utsname utsname; 458 char osname[LINUX_MAX_UTSNAME]; 459 char osrelease[LINUX_MAX_UTSNAME]; 460 char *p; 461 462 linux_get_osname(td, osname); 463 linux_get_osrelease(td, osrelease); 464 465 bzero(&utsname, sizeof(utsname)); 466 strlcpy(utsname.sysname, osname, LINUX_MAX_UTSNAME); 467 getcredhostname(td->td_ucred, utsname.nodename, LINUX_MAX_UTSNAME); 468 getcreddomainname(td->td_ucred, utsname.domainname, LINUX_MAX_UTSNAME); 469 strlcpy(utsname.release, osrelease, LINUX_MAX_UTSNAME); 470 strlcpy(utsname.version, version, LINUX_MAX_UTSNAME); 471 for (p = utsname.version; *p != '\0'; ++p) 472 if (*p == '\n') { 473 *p = '\0'; 474 break; 475 } 476 #if defined(__amd64__) 477 /* 478 * On amd64, Linux uname(2) needs to return "x86_64" 479 * for both 64-bit and 32-bit applications. On 32-bit, 480 * the string returned by getauxval(AT_PLATFORM) needs 481 * to remain "i686", though. 482 */ 483 #if defined(COMPAT_LINUX32) 484 if (linux32_emulate_i386) 485 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 486 else 487 #endif 488 strlcpy(utsname.machine, "x86_64", LINUX_MAX_UTSNAME); 489 #elif defined(__aarch64__) 490 strlcpy(utsname.machine, "aarch64", LINUX_MAX_UTSNAME); 491 #elif defined(__i386__) 492 strlcpy(utsname.machine, "i686", LINUX_MAX_UTSNAME); 493 #endif 494 495 return (copyout(&utsname, args->buf, sizeof(utsname))); 496 } 497 498 struct l_utimbuf { 499 l_time_t l_actime; 500 l_time_t l_modtime; 501 }; 502 503 #ifdef LINUX_LEGACY_SYSCALLS 504 int 505 linux_utime(struct thread *td, struct linux_utime_args *args) 506 { 507 struct timeval tv[2], *tvp; 508 struct l_utimbuf lut; 509 int error; 510 511 if (args->times) { 512 if ((error = copyin(args->times, &lut, sizeof lut)) != 0) 513 return (error); 514 tv[0].tv_sec = lut.l_actime; 515 tv[0].tv_usec = 0; 516 tv[1].tv_sec = lut.l_modtime; 517 tv[1].tv_usec = 0; 518 tvp = tv; 519 } else 520 tvp = NULL; 521 522 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE, 523 tvp, UIO_SYSSPACE)); 524 } 525 #endif 526 527 #ifdef LINUX_LEGACY_SYSCALLS 528 int 529 linux_utimes(struct thread *td, struct linux_utimes_args *args) 530 { 531 l_timeval ltv[2]; 532 struct timeval tv[2], *tvp = NULL; 533 int error; 534 535 if (args->tptr != NULL) { 536 if ((error = copyin(args->tptr, ltv, sizeof ltv)) != 0) 537 return (error); 538 tv[0].tv_sec = ltv[0].tv_sec; 539 tv[0].tv_usec = ltv[0].tv_usec; 540 tv[1].tv_sec = ltv[1].tv_sec; 541 tv[1].tv_usec = ltv[1].tv_usec; 542 tvp = tv; 543 } 544 545 return (kern_utimesat(td, AT_FDCWD, args->fname, UIO_USERSPACE, 546 tvp, UIO_SYSSPACE)); 547 } 548 #endif 549 550 static int 551 linux_utimensat_lts_to_ts(struct l_timespec *l_times, struct timespec *times) 552 { 553 554 if (l_times->tv_nsec != LINUX_UTIME_OMIT && 555 l_times->tv_nsec != LINUX_UTIME_NOW && 556 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999)) 557 return (EINVAL); 558 559 times->tv_sec = l_times->tv_sec; 560 switch (l_times->tv_nsec) 561 { 562 case LINUX_UTIME_OMIT: 563 times->tv_nsec = UTIME_OMIT; 564 break; 565 case LINUX_UTIME_NOW: 566 times->tv_nsec = UTIME_NOW; 567 break; 568 default: 569 times->tv_nsec = l_times->tv_nsec; 570 } 571 572 return (0); 573 } 574 575 static int 576 linux_common_utimensat(struct thread *td, int ldfd, const char *pathname, 577 struct timespec *timesp, int lflags) 578 { 579 int dfd, flags = 0; 580 581 dfd = (ldfd == LINUX_AT_FDCWD) ? AT_FDCWD : ldfd; 582 583 if (lflags & ~(LINUX_AT_SYMLINK_NOFOLLOW | LINUX_AT_EMPTY_PATH)) 584 return (EINVAL); 585 586 if (timesp != NULL) { 587 /* This breaks POSIX, but is what the Linux kernel does 588 * _on purpose_ (documented in the man page for utimensat(2)), 589 * so we must follow that behaviour. */ 590 if (timesp[0].tv_nsec == UTIME_OMIT && 591 timesp[1].tv_nsec == UTIME_OMIT) 592 return (0); 593 } 594 595 if (lflags & LINUX_AT_SYMLINK_NOFOLLOW) 596 flags |= AT_SYMLINK_NOFOLLOW; 597 if (lflags & LINUX_AT_EMPTY_PATH) 598 flags |= AT_EMPTY_PATH; 599 600 if (pathname != NULL) 601 return (kern_utimensat(td, dfd, pathname, 602 UIO_USERSPACE, timesp, UIO_SYSSPACE, flags)); 603 604 if (lflags != 0) 605 return (EINVAL); 606 607 return (kern_futimens(td, dfd, timesp, UIO_SYSSPACE)); 608 } 609 610 int 611 linux_utimensat(struct thread *td, struct linux_utimensat_args *args) 612 { 613 struct l_timespec l_times[2]; 614 struct timespec times[2], *timesp; 615 int error; 616 617 if (args->times != NULL) { 618 error = copyin(args->times, l_times, sizeof(l_times)); 619 if (error != 0) 620 return (error); 621 622 error = linux_utimensat_lts_to_ts(&l_times[0], ×[0]); 623 if (error != 0) 624 return (error); 625 error = linux_utimensat_lts_to_ts(&l_times[1], ×[1]); 626 if (error != 0) 627 return (error); 628 timesp = times; 629 } else 630 timesp = NULL; 631 632 return (linux_common_utimensat(td, args->dfd, args->pathname, 633 timesp, args->flags)); 634 } 635 636 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 637 static int 638 linux_utimensat_lts64_to_ts(struct l_timespec64 *l_times, struct timespec *times) 639 { 640 641 /* Zero out the padding in compat mode. */ 642 l_times->tv_nsec &= 0xFFFFFFFFUL; 643 644 if (l_times->tv_nsec != LINUX_UTIME_OMIT && 645 l_times->tv_nsec != LINUX_UTIME_NOW && 646 (l_times->tv_nsec < 0 || l_times->tv_nsec > 999999999)) 647 return (EINVAL); 648 649 times->tv_sec = l_times->tv_sec; 650 switch (l_times->tv_nsec) 651 { 652 case LINUX_UTIME_OMIT: 653 times->tv_nsec = UTIME_OMIT; 654 break; 655 case LINUX_UTIME_NOW: 656 times->tv_nsec = UTIME_NOW; 657 break; 658 default: 659 times->tv_nsec = l_times->tv_nsec; 660 } 661 662 return (0); 663 } 664 665 int 666 linux_utimensat_time64(struct thread *td, struct linux_utimensat_time64_args *args) 667 { 668 struct l_timespec64 l_times[2]; 669 struct timespec times[2], *timesp; 670 int error; 671 672 if (args->times64 != NULL) { 673 error = copyin(args->times64, l_times, sizeof(l_times)); 674 if (error != 0) 675 return (error); 676 677 error = linux_utimensat_lts64_to_ts(&l_times[0], ×[0]); 678 if (error != 0) 679 return (error); 680 error = linux_utimensat_lts64_to_ts(&l_times[1], ×[1]); 681 if (error != 0) 682 return (error); 683 timesp = times; 684 } else 685 timesp = NULL; 686 687 return (linux_common_utimensat(td, args->dfd, args->pathname, 688 timesp, args->flags)); 689 } 690 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 691 692 #ifdef LINUX_LEGACY_SYSCALLS 693 int 694 linux_futimesat(struct thread *td, struct linux_futimesat_args *args) 695 { 696 l_timeval ltv[2]; 697 struct timeval tv[2], *tvp = NULL; 698 int error, dfd; 699 700 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 701 702 if (args->utimes != NULL) { 703 if ((error = copyin(args->utimes, ltv, sizeof ltv)) != 0) 704 return (error); 705 tv[0].tv_sec = ltv[0].tv_sec; 706 tv[0].tv_usec = ltv[0].tv_usec; 707 tv[1].tv_sec = ltv[1].tv_sec; 708 tv[1].tv_usec = ltv[1].tv_usec; 709 tvp = tv; 710 } 711 712 return (kern_utimesat(td, dfd, args->filename, UIO_USERSPACE, 713 tvp, UIO_SYSSPACE)); 714 } 715 #endif 716 717 static int 718 linux_common_wait(struct thread *td, idtype_t idtype, int id, int *statusp, 719 int options, void *rup, l_siginfo_t *infop) 720 { 721 l_siginfo_t lsi; 722 siginfo_t siginfo; 723 struct __wrusage wru; 724 int error, status, tmpstat, sig; 725 726 error = kern_wait6(td, idtype, id, &status, options, 727 rup != NULL ? &wru : NULL, &siginfo); 728 729 if (error == 0 && statusp) { 730 tmpstat = status & 0xffff; 731 if (WIFSIGNALED(tmpstat)) { 732 tmpstat = (tmpstat & 0xffffff80) | 733 bsd_to_linux_signal(WTERMSIG(tmpstat)); 734 } else if (WIFSTOPPED(tmpstat)) { 735 tmpstat = (tmpstat & 0xffff00ff) | 736 (bsd_to_linux_signal(WSTOPSIG(tmpstat)) << 8); 737 #if defined(__aarch64__) || (defined(__amd64__) && !defined(COMPAT_LINUX32)) 738 if (WSTOPSIG(status) == SIGTRAP) { 739 tmpstat = linux_ptrace_status(td, 740 siginfo.si_pid, tmpstat); 741 } 742 #endif 743 } else if (WIFCONTINUED(tmpstat)) { 744 tmpstat = 0xffff; 745 } 746 error = copyout(&tmpstat, statusp, sizeof(int)); 747 } 748 if (error == 0 && rup != NULL) 749 error = linux_copyout_rusage(&wru.wru_self, rup); 750 if (error == 0 && infop != NULL && td->td_retval[0] != 0) { 751 sig = bsd_to_linux_signal(siginfo.si_signo); 752 siginfo_to_lsiginfo(&siginfo, &lsi, sig); 753 error = copyout(&lsi, infop, sizeof(lsi)); 754 } 755 756 return (error); 757 } 758 759 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 760 int 761 linux_waitpid(struct thread *td, struct linux_waitpid_args *args) 762 { 763 struct linux_wait4_args wait4_args = { 764 .pid = args->pid, 765 .status = args->status, 766 .options = args->options, 767 .rusage = NULL, 768 }; 769 770 return (linux_wait4(td, &wait4_args)); 771 } 772 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 773 774 int 775 linux_wait4(struct thread *td, struct linux_wait4_args *args) 776 { 777 struct proc *p; 778 int options, id, idtype; 779 780 if (args->options & ~(LINUX_WUNTRACED | LINUX_WNOHANG | 781 LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL)) 782 return (EINVAL); 783 784 /* -INT_MIN is not defined. */ 785 if (args->pid == INT_MIN) 786 return (ESRCH); 787 788 options = 0; 789 linux_to_bsd_waitopts(args->options, &options); 790 791 /* 792 * For backward compatibility we implicitly add flags WEXITED 793 * and WTRAPPED here. 794 */ 795 options |= WEXITED | WTRAPPED; 796 797 if (args->pid == WAIT_ANY) { 798 idtype = P_ALL; 799 id = 0; 800 } else if (args->pid < 0) { 801 idtype = P_PGID; 802 id = (id_t)-args->pid; 803 } else if (args->pid == 0) { 804 idtype = P_PGID; 805 p = td->td_proc; 806 PROC_LOCK(p); 807 id = p->p_pgid; 808 PROC_UNLOCK(p); 809 } else { 810 idtype = P_PID; 811 id = (id_t)args->pid; 812 } 813 814 return (linux_common_wait(td, idtype, id, args->status, options, 815 args->rusage, NULL)); 816 } 817 818 int 819 linux_waitid(struct thread *td, struct linux_waitid_args *args) 820 { 821 idtype_t idtype; 822 int error, options; 823 struct proc *p; 824 pid_t id; 825 826 if (args->options & ~(LINUX_WNOHANG | LINUX_WNOWAIT | LINUX_WEXITED | 827 LINUX_WSTOPPED | LINUX_WCONTINUED | __WCLONE | __WNOTHREAD | __WALL)) 828 return (EINVAL); 829 830 options = 0; 831 linux_to_bsd_waitopts(args->options, &options); 832 833 id = args->id; 834 switch (args->idtype) { 835 case LINUX_P_ALL: 836 idtype = P_ALL; 837 break; 838 case LINUX_P_PID: 839 if (args->id <= 0) 840 return (EINVAL); 841 idtype = P_PID; 842 break; 843 case LINUX_P_PGID: 844 if (linux_kernver(td) >= LINUX_KERNVER(5,4,0) && args->id == 0) { 845 p = td->td_proc; 846 PROC_LOCK(p); 847 id = p->p_pgid; 848 PROC_UNLOCK(p); 849 } else if (args->id <= 0) 850 return (EINVAL); 851 idtype = P_PGID; 852 break; 853 case LINUX_P_PIDFD: 854 LINUX_RATELIMIT_MSG("unsupported waitid P_PIDFD idtype"); 855 return (ENOSYS); 856 default: 857 return (EINVAL); 858 } 859 860 error = linux_common_wait(td, idtype, id, NULL, options, 861 args->rusage, args->info); 862 td->td_retval[0] = 0; 863 864 return (error); 865 } 866 867 #ifdef LINUX_LEGACY_SYSCALLS 868 int 869 linux_mknod(struct thread *td, struct linux_mknod_args *args) 870 { 871 int error; 872 873 switch (args->mode & S_IFMT) { 874 case S_IFIFO: 875 case S_IFSOCK: 876 error = kern_mkfifoat(td, AT_FDCWD, args->path, UIO_USERSPACE, 877 args->mode); 878 break; 879 880 case S_IFCHR: 881 case S_IFBLK: 882 error = kern_mknodat(td, AT_FDCWD, args->path, UIO_USERSPACE, 883 args->mode, linux_decode_dev(args->dev)); 884 break; 885 886 case S_IFDIR: 887 error = EPERM; 888 break; 889 890 case 0: 891 args->mode |= S_IFREG; 892 /* FALLTHROUGH */ 893 case S_IFREG: 894 error = kern_openat(td, AT_FDCWD, args->path, UIO_USERSPACE, 895 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 896 if (error == 0) 897 kern_close(td, td->td_retval[0]); 898 break; 899 900 default: 901 error = EINVAL; 902 break; 903 } 904 return (error); 905 } 906 #endif 907 908 int 909 linux_mknodat(struct thread *td, struct linux_mknodat_args *args) 910 { 911 int error, dfd; 912 913 dfd = (args->dfd == LINUX_AT_FDCWD) ? AT_FDCWD : args->dfd; 914 915 switch (args->mode & S_IFMT) { 916 case S_IFIFO: 917 case S_IFSOCK: 918 error = kern_mkfifoat(td, dfd, args->filename, UIO_USERSPACE, 919 args->mode); 920 break; 921 922 case S_IFCHR: 923 case S_IFBLK: 924 error = kern_mknodat(td, dfd, args->filename, UIO_USERSPACE, 925 args->mode, linux_decode_dev(args->dev)); 926 break; 927 928 case S_IFDIR: 929 error = EPERM; 930 break; 931 932 case 0: 933 args->mode |= S_IFREG; 934 /* FALLTHROUGH */ 935 case S_IFREG: 936 error = kern_openat(td, dfd, args->filename, UIO_USERSPACE, 937 O_WRONLY | O_CREAT | O_TRUNC, args->mode); 938 if (error == 0) 939 kern_close(td, td->td_retval[0]); 940 break; 941 942 default: 943 error = EINVAL; 944 break; 945 } 946 return (error); 947 } 948 949 /* 950 * UGH! This is just about the dumbest idea I've ever heard!! 951 */ 952 int 953 linux_personality(struct thread *td, struct linux_personality_args *args) 954 { 955 struct linux_pemuldata *pem; 956 struct proc *p = td->td_proc; 957 uint32_t old; 958 959 PROC_LOCK(p); 960 pem = pem_find(p); 961 old = pem->persona; 962 if (args->per != 0xffffffff) 963 pem->persona = args->per; 964 PROC_UNLOCK(p); 965 966 td->td_retval[0] = old; 967 return (0); 968 } 969 970 struct l_itimerval { 971 l_timeval it_interval; 972 l_timeval it_value; 973 }; 974 975 #define B2L_ITIMERVAL(bip, lip) \ 976 (bip)->it_interval.tv_sec = (lip)->it_interval.tv_sec; \ 977 (bip)->it_interval.tv_usec = (lip)->it_interval.tv_usec; \ 978 (bip)->it_value.tv_sec = (lip)->it_value.tv_sec; \ 979 (bip)->it_value.tv_usec = (lip)->it_value.tv_usec; 980 981 int 982 linux_setitimer(struct thread *td, struct linux_setitimer_args *uap) 983 { 984 int error; 985 struct l_itimerval ls; 986 struct itimerval aitv, oitv; 987 988 if (uap->itv == NULL) { 989 uap->itv = uap->oitv; 990 return (linux_getitimer(td, (struct linux_getitimer_args *)uap)); 991 } 992 993 error = copyin(uap->itv, &ls, sizeof(ls)); 994 if (error != 0) 995 return (error); 996 B2L_ITIMERVAL(&aitv, &ls); 997 error = kern_setitimer(td, uap->which, &aitv, &oitv); 998 if (error != 0 || uap->oitv == NULL) 999 return (error); 1000 B2L_ITIMERVAL(&ls, &oitv); 1001 1002 return (copyout(&ls, uap->oitv, sizeof(ls))); 1003 } 1004 1005 int 1006 linux_getitimer(struct thread *td, struct linux_getitimer_args *uap) 1007 { 1008 int error; 1009 struct l_itimerval ls; 1010 struct itimerval aitv; 1011 1012 error = kern_getitimer(td, uap->which, &aitv); 1013 if (error != 0) 1014 return (error); 1015 B2L_ITIMERVAL(&ls, &aitv); 1016 return (copyout(&ls, uap->itv, sizeof(ls))); 1017 } 1018 1019 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1020 int 1021 linux_nice(struct thread *td, struct linux_nice_args *args) 1022 { 1023 1024 return (kern_setpriority(td, PRIO_PROCESS, 0, args->inc)); 1025 } 1026 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1027 1028 int 1029 linux_setgroups(struct thread *td, struct linux_setgroups_args *args) 1030 { 1031 struct ucred *newcred, *oldcred; 1032 l_gid_t *linux_gidset; 1033 int ngrp, error; 1034 struct proc *p; 1035 1036 ngrp = args->gidsetsize; 1037 if (ngrp < 0 || ngrp >= ngroups_max) 1038 return (EINVAL); 1039 linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK); 1040 error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t)); 1041 if (error) 1042 goto out; 1043 newcred = crget(); 1044 crextend(newcred, ngrp); 1045 p = td->td_proc; 1046 PROC_LOCK(p); 1047 oldcred = p->p_ucred; 1048 crcopy(newcred, oldcred); 1049 1050 if ((error = priv_check_cred(oldcred, PRIV_CRED_SETGROUPS)) != 0) { 1051 PROC_UNLOCK(p); 1052 crfree(newcred); 1053 goto out; 1054 } 1055 1056 newcred->cr_ngroups = ngrp; 1057 for (int i = 0; i < ngrp; i++) 1058 newcred->cr_groups[i] = linux_gidset[i]; 1059 1060 setsugid(p); 1061 proc_set_cred(p, newcred); 1062 PROC_UNLOCK(p); 1063 crfree(oldcred); 1064 error = 0; 1065 out: 1066 free(linux_gidset, M_LINUX); 1067 return (error); 1068 } 1069 1070 int 1071 linux_getgroups(struct thread *td, struct linux_getgroups_args *args) 1072 { 1073 struct ucred *cred; 1074 l_gid_t *linux_gidset; 1075 gid_t *bsd_gidset; 1076 int bsd_gidsetsz, ngrp, error; 1077 1078 cred = td->td_ucred; 1079 bsd_gidset = cred->cr_groups; 1080 bsd_gidsetsz = cred->cr_ngroups; 1081 1082 if ((ngrp = args->gidsetsize) == 0) { 1083 td->td_retval[0] = bsd_gidsetsz; 1084 return (0); 1085 } 1086 1087 if (ngrp < bsd_gidsetsz) 1088 return (EINVAL); 1089 1090 ngrp = 0; 1091 linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset), 1092 M_LINUX, M_WAITOK); 1093 while (ngrp < bsd_gidsetsz) { 1094 linux_gidset[ngrp] = bsd_gidset[ngrp]; 1095 ngrp++; 1096 } 1097 1098 error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t)); 1099 free(linux_gidset, M_LINUX); 1100 if (error) 1101 return (error); 1102 1103 td->td_retval[0] = ngrp; 1104 return (0); 1105 } 1106 1107 static bool 1108 linux_get_dummy_limit(struct thread *td, l_uint resource, struct rlimit *rlim) 1109 { 1110 ssize_t size; 1111 int res, error; 1112 1113 if (linux_dummy_rlimits == 0) 1114 return (false); 1115 1116 switch (resource) { 1117 case LINUX_RLIMIT_LOCKS: 1118 case LINUX_RLIMIT_RTTIME: 1119 rlim->rlim_cur = LINUX_RLIM_INFINITY; 1120 rlim->rlim_max = LINUX_RLIM_INFINITY; 1121 return (true); 1122 case LINUX_RLIMIT_NICE: 1123 case LINUX_RLIMIT_RTPRIO: 1124 rlim->rlim_cur = 0; 1125 rlim->rlim_max = 0; 1126 return (true); 1127 case LINUX_RLIMIT_SIGPENDING: 1128 error = kernel_sysctlbyname(td, 1129 "kern.sigqueue.max_pending_per_proc", 1130 &res, &size, 0, 0, 0, 0); 1131 if (error != 0) 1132 return (false); 1133 rlim->rlim_cur = res; 1134 rlim->rlim_max = res; 1135 return (true); 1136 case LINUX_RLIMIT_MSGQUEUE: 1137 error = kernel_sysctlbyname(td, 1138 "kern.ipc.msgmnb", &res, &size, 0, 0, 0, 0); 1139 if (error != 0) 1140 return (false); 1141 rlim->rlim_cur = res; 1142 rlim->rlim_max = res; 1143 return (true); 1144 default: 1145 return (false); 1146 } 1147 } 1148 1149 int 1150 linux_setrlimit(struct thread *td, struct linux_setrlimit_args *args) 1151 { 1152 struct rlimit bsd_rlim; 1153 struct l_rlimit rlim; 1154 u_int which; 1155 int error; 1156 1157 if (args->resource >= LINUX_RLIM_NLIMITS) 1158 return (EINVAL); 1159 1160 which = linux_to_bsd_resource[args->resource]; 1161 if (which == -1) 1162 return (EINVAL); 1163 1164 error = copyin(args->rlim, &rlim, sizeof(rlim)); 1165 if (error) 1166 return (error); 1167 1168 bsd_rlim.rlim_cur = (rlim_t)rlim.rlim_cur; 1169 bsd_rlim.rlim_max = (rlim_t)rlim.rlim_max; 1170 return (kern_setrlimit(td, which, &bsd_rlim)); 1171 } 1172 1173 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1174 int 1175 linux_old_getrlimit(struct thread *td, struct linux_old_getrlimit_args *args) 1176 { 1177 struct l_rlimit rlim; 1178 struct rlimit bsd_rlim; 1179 u_int which; 1180 1181 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) { 1182 rlim.rlim_cur = bsd_rlim.rlim_cur; 1183 rlim.rlim_max = bsd_rlim.rlim_max; 1184 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1185 } 1186 1187 if (args->resource >= LINUX_RLIM_NLIMITS) 1188 return (EINVAL); 1189 1190 which = linux_to_bsd_resource[args->resource]; 1191 if (which == -1) 1192 return (EINVAL); 1193 1194 lim_rlimit(td, which, &bsd_rlim); 1195 1196 #ifdef COMPAT_LINUX32 1197 rlim.rlim_cur = (unsigned int)bsd_rlim.rlim_cur; 1198 if (rlim.rlim_cur == UINT_MAX) 1199 rlim.rlim_cur = INT_MAX; 1200 rlim.rlim_max = (unsigned int)bsd_rlim.rlim_max; 1201 if (rlim.rlim_max == UINT_MAX) 1202 rlim.rlim_max = INT_MAX; 1203 #else 1204 rlim.rlim_cur = (unsigned long)bsd_rlim.rlim_cur; 1205 if (rlim.rlim_cur == ULONG_MAX) 1206 rlim.rlim_cur = LONG_MAX; 1207 rlim.rlim_max = (unsigned long)bsd_rlim.rlim_max; 1208 if (rlim.rlim_max == ULONG_MAX) 1209 rlim.rlim_max = LONG_MAX; 1210 #endif 1211 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1212 } 1213 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1214 1215 int 1216 linux_getrlimit(struct thread *td, struct linux_getrlimit_args *args) 1217 { 1218 struct l_rlimit rlim; 1219 struct rlimit bsd_rlim; 1220 u_int which; 1221 1222 if (linux_get_dummy_limit(td, args->resource, &bsd_rlim)) { 1223 rlim.rlim_cur = bsd_rlim.rlim_cur; 1224 rlim.rlim_max = bsd_rlim.rlim_max; 1225 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1226 } 1227 1228 if (args->resource >= LINUX_RLIM_NLIMITS) 1229 return (EINVAL); 1230 1231 which = linux_to_bsd_resource[args->resource]; 1232 if (which == -1) 1233 return (EINVAL); 1234 1235 lim_rlimit(td, which, &bsd_rlim); 1236 1237 rlim.rlim_cur = (l_ulong)bsd_rlim.rlim_cur; 1238 rlim.rlim_max = (l_ulong)bsd_rlim.rlim_max; 1239 return (copyout(&rlim, args->rlim, sizeof(rlim))); 1240 } 1241 1242 int 1243 linux_sched_setscheduler(struct thread *td, 1244 struct linux_sched_setscheduler_args *args) 1245 { 1246 struct sched_param sched_param; 1247 struct thread *tdt; 1248 int error, policy; 1249 1250 switch (args->policy) { 1251 case LINUX_SCHED_OTHER: 1252 policy = SCHED_OTHER; 1253 break; 1254 case LINUX_SCHED_FIFO: 1255 policy = SCHED_FIFO; 1256 break; 1257 case LINUX_SCHED_RR: 1258 policy = SCHED_RR; 1259 break; 1260 default: 1261 return (EINVAL); 1262 } 1263 1264 error = copyin(args->param, &sched_param, sizeof(sched_param)); 1265 if (error) 1266 return (error); 1267 1268 if (linux_map_sched_prio) { 1269 switch (policy) { 1270 case SCHED_OTHER: 1271 if (sched_param.sched_priority != 0) 1272 return (EINVAL); 1273 1274 sched_param.sched_priority = 1275 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; 1276 break; 1277 case SCHED_FIFO: 1278 case SCHED_RR: 1279 if (sched_param.sched_priority < 1 || 1280 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) 1281 return (EINVAL); 1282 1283 /* 1284 * Map [1, LINUX_MAX_RT_PRIO - 1] to 1285 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). 1286 */ 1287 sched_param.sched_priority = 1288 (sched_param.sched_priority - 1) * 1289 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / 1290 (LINUX_MAX_RT_PRIO - 1); 1291 break; 1292 } 1293 } 1294 1295 tdt = linux_tdfind(td, args->pid, -1); 1296 if (tdt == NULL) 1297 return (ESRCH); 1298 1299 error = kern_sched_setscheduler(td, tdt, policy, &sched_param); 1300 PROC_UNLOCK(tdt->td_proc); 1301 return (error); 1302 } 1303 1304 int 1305 linux_sched_getscheduler(struct thread *td, 1306 struct linux_sched_getscheduler_args *args) 1307 { 1308 struct thread *tdt; 1309 int error, policy; 1310 1311 tdt = linux_tdfind(td, args->pid, -1); 1312 if (tdt == NULL) 1313 return (ESRCH); 1314 1315 error = kern_sched_getscheduler(td, tdt, &policy); 1316 PROC_UNLOCK(tdt->td_proc); 1317 1318 switch (policy) { 1319 case SCHED_OTHER: 1320 td->td_retval[0] = LINUX_SCHED_OTHER; 1321 break; 1322 case SCHED_FIFO: 1323 td->td_retval[0] = LINUX_SCHED_FIFO; 1324 break; 1325 case SCHED_RR: 1326 td->td_retval[0] = LINUX_SCHED_RR; 1327 break; 1328 } 1329 return (error); 1330 } 1331 1332 int 1333 linux_sched_get_priority_max(struct thread *td, 1334 struct linux_sched_get_priority_max_args *args) 1335 { 1336 struct sched_get_priority_max_args bsd; 1337 1338 if (linux_map_sched_prio) { 1339 switch (args->policy) { 1340 case LINUX_SCHED_OTHER: 1341 td->td_retval[0] = 0; 1342 return (0); 1343 case LINUX_SCHED_FIFO: 1344 case LINUX_SCHED_RR: 1345 td->td_retval[0] = LINUX_MAX_RT_PRIO - 1; 1346 return (0); 1347 default: 1348 return (EINVAL); 1349 } 1350 } 1351 1352 switch (args->policy) { 1353 case LINUX_SCHED_OTHER: 1354 bsd.policy = SCHED_OTHER; 1355 break; 1356 case LINUX_SCHED_FIFO: 1357 bsd.policy = SCHED_FIFO; 1358 break; 1359 case LINUX_SCHED_RR: 1360 bsd.policy = SCHED_RR; 1361 break; 1362 default: 1363 return (EINVAL); 1364 } 1365 return (sys_sched_get_priority_max(td, &bsd)); 1366 } 1367 1368 int 1369 linux_sched_get_priority_min(struct thread *td, 1370 struct linux_sched_get_priority_min_args *args) 1371 { 1372 struct sched_get_priority_min_args bsd; 1373 1374 if (linux_map_sched_prio) { 1375 switch (args->policy) { 1376 case LINUX_SCHED_OTHER: 1377 td->td_retval[0] = 0; 1378 return (0); 1379 case LINUX_SCHED_FIFO: 1380 case LINUX_SCHED_RR: 1381 td->td_retval[0] = 1; 1382 return (0); 1383 default: 1384 return (EINVAL); 1385 } 1386 } 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 (sys_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 if (args->magic1 != REBOOT_MAGIC1) 1421 return (EINVAL); 1422 1423 switch (args->magic2) { 1424 case REBOOT_MAGIC2: 1425 case REBOOT_MAGIC2A: 1426 case REBOOT_MAGIC2B: 1427 break; 1428 default: 1429 return (EINVAL); 1430 } 1431 1432 switch (args->cmd) { 1433 case REBOOT_CAD_ON: 1434 case REBOOT_CAD_OFF: 1435 return (priv_check(td, PRIV_REBOOT)); 1436 case REBOOT_HALT: 1437 bsd_args.opt = RB_HALT; 1438 break; 1439 case REBOOT_RESTART: 1440 case REBOOT_RESTART2: 1441 bsd_args.opt = 0; 1442 break; 1443 case REBOOT_POWEROFF: 1444 bsd_args.opt = RB_POWEROFF; 1445 break; 1446 default: 1447 return (EINVAL); 1448 } 1449 return (sys_reboot(td, &bsd_args)); 1450 } 1451 1452 int 1453 linux_getpid(struct thread *td, struct linux_getpid_args *args) 1454 { 1455 1456 td->td_retval[0] = td->td_proc->p_pid; 1457 1458 return (0); 1459 } 1460 1461 int 1462 linux_gettid(struct thread *td, struct linux_gettid_args *args) 1463 { 1464 struct linux_emuldata *em; 1465 1466 em = em_find(td); 1467 KASSERT(em != NULL, ("gettid: emuldata not found.\n")); 1468 1469 td->td_retval[0] = em->em_tid; 1470 1471 return (0); 1472 } 1473 1474 int 1475 linux_getppid(struct thread *td, struct linux_getppid_args *args) 1476 { 1477 1478 td->td_retval[0] = kern_getppid(td); 1479 return (0); 1480 } 1481 1482 int 1483 linux_getgid(struct thread *td, struct linux_getgid_args *args) 1484 { 1485 1486 td->td_retval[0] = td->td_ucred->cr_rgid; 1487 return (0); 1488 } 1489 1490 int 1491 linux_getuid(struct thread *td, struct linux_getuid_args *args) 1492 { 1493 1494 td->td_retval[0] = td->td_ucred->cr_ruid; 1495 return (0); 1496 } 1497 1498 int 1499 linux_getsid(struct thread *td, struct linux_getsid_args *args) 1500 { 1501 1502 return (kern_getsid(td, args->pid)); 1503 } 1504 1505 int 1506 linux_getpriority(struct thread *td, struct linux_getpriority_args *args) 1507 { 1508 int error; 1509 1510 error = kern_getpriority(td, args->which, args->who); 1511 td->td_retval[0] = 20 - td->td_retval[0]; 1512 return (error); 1513 } 1514 1515 int 1516 linux_sethostname(struct thread *td, struct linux_sethostname_args *args) 1517 { 1518 int name[2]; 1519 1520 name[0] = CTL_KERN; 1521 name[1] = KERN_HOSTNAME; 1522 return (userland_sysctl(td, name, 2, 0, 0, 0, args->hostname, 1523 args->len, 0, 0)); 1524 } 1525 1526 int 1527 linux_setdomainname(struct thread *td, struct linux_setdomainname_args *args) 1528 { 1529 int name[2]; 1530 1531 name[0] = CTL_KERN; 1532 name[1] = KERN_NISDOMAINNAME; 1533 return (userland_sysctl(td, name, 2, 0, 0, 0, args->name, 1534 args->len, 0, 0)); 1535 } 1536 1537 int 1538 linux_exit_group(struct thread *td, struct linux_exit_group_args *args) 1539 { 1540 1541 LINUX_CTR2(exit_group, "thread(%d) (%d)", td->td_tid, 1542 args->error_code); 1543 1544 /* 1545 * XXX: we should send a signal to the parent if 1546 * SIGNAL_EXIT_GROUP is set. We ignore that (temporarily?) 1547 * as it doesnt occur often. 1548 */ 1549 exit1(td, args->error_code, 0); 1550 /* NOTREACHED */ 1551 } 1552 1553 #define _LINUX_CAPABILITY_VERSION_1 0x19980330 1554 #define _LINUX_CAPABILITY_VERSION_2 0x20071026 1555 #define _LINUX_CAPABILITY_VERSION_3 0x20080522 1556 1557 struct l_user_cap_header { 1558 l_int version; 1559 l_int pid; 1560 }; 1561 1562 struct l_user_cap_data { 1563 l_int effective; 1564 l_int permitted; 1565 l_int inheritable; 1566 }; 1567 1568 int 1569 linux_capget(struct thread *td, struct linux_capget_args *uap) 1570 { 1571 struct l_user_cap_header luch; 1572 struct l_user_cap_data lucd[2]; 1573 int error, u32s; 1574 1575 if (uap->hdrp == NULL) 1576 return (EFAULT); 1577 1578 error = copyin(uap->hdrp, &luch, sizeof(luch)); 1579 if (error != 0) 1580 return (error); 1581 1582 switch (luch.version) { 1583 case _LINUX_CAPABILITY_VERSION_1: 1584 u32s = 1; 1585 break; 1586 case _LINUX_CAPABILITY_VERSION_2: 1587 case _LINUX_CAPABILITY_VERSION_3: 1588 u32s = 2; 1589 break; 1590 default: 1591 luch.version = _LINUX_CAPABILITY_VERSION_1; 1592 error = copyout(&luch, uap->hdrp, sizeof(luch)); 1593 if (error) 1594 return (error); 1595 return (EINVAL); 1596 } 1597 1598 if (luch.pid) 1599 return (EPERM); 1600 1601 if (uap->datap) { 1602 /* 1603 * The current implementation doesn't support setting 1604 * a capability (it's essentially a stub) so indicate 1605 * that no capabilities are currently set or available 1606 * to request. 1607 */ 1608 memset(&lucd, 0, u32s * sizeof(lucd[0])); 1609 error = copyout(&lucd, uap->datap, u32s * sizeof(lucd[0])); 1610 } 1611 1612 return (error); 1613 } 1614 1615 int 1616 linux_capset(struct thread *td, struct linux_capset_args *uap) 1617 { 1618 struct l_user_cap_header luch; 1619 struct l_user_cap_data lucd[2]; 1620 int error, i, u32s; 1621 1622 if (uap->hdrp == NULL || uap->datap == NULL) 1623 return (EFAULT); 1624 1625 error = copyin(uap->hdrp, &luch, sizeof(luch)); 1626 if (error != 0) 1627 return (error); 1628 1629 switch (luch.version) { 1630 case _LINUX_CAPABILITY_VERSION_1: 1631 u32s = 1; 1632 break; 1633 case _LINUX_CAPABILITY_VERSION_2: 1634 case _LINUX_CAPABILITY_VERSION_3: 1635 u32s = 2; 1636 break; 1637 default: 1638 luch.version = _LINUX_CAPABILITY_VERSION_1; 1639 error = copyout(&luch, uap->hdrp, sizeof(luch)); 1640 if (error) 1641 return (error); 1642 return (EINVAL); 1643 } 1644 1645 if (luch.pid) 1646 return (EPERM); 1647 1648 error = copyin(uap->datap, &lucd, u32s * sizeof(lucd[0])); 1649 if (error != 0) 1650 return (error); 1651 1652 /* We currently don't support setting any capabilities. */ 1653 for (i = 0; i < u32s; i++) { 1654 if (lucd[i].effective || lucd[i].permitted || 1655 lucd[i].inheritable) { 1656 linux_msg(td, 1657 "capset[%d] effective=0x%x, permitted=0x%x, " 1658 "inheritable=0x%x is not implemented", i, 1659 (int)lucd[i].effective, (int)lucd[i].permitted, 1660 (int)lucd[i].inheritable); 1661 return (EPERM); 1662 } 1663 } 1664 1665 return (0); 1666 } 1667 1668 int 1669 linux_prctl(struct thread *td, struct linux_prctl_args *args) 1670 { 1671 int error = 0, max_size, arg; 1672 struct proc *p = td->td_proc; 1673 char comm[LINUX_MAX_COMM_LEN]; 1674 int pdeath_signal, trace_state; 1675 1676 switch (args->option) { 1677 case LINUX_PR_SET_PDEATHSIG: 1678 if (!LINUX_SIG_VALID(args->arg2)) 1679 return (EINVAL); 1680 pdeath_signal = linux_to_bsd_signal(args->arg2); 1681 return (kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_CTL, 1682 &pdeath_signal)); 1683 case LINUX_PR_GET_PDEATHSIG: 1684 error = kern_procctl(td, P_PID, 0, PROC_PDEATHSIG_STATUS, 1685 &pdeath_signal); 1686 if (error != 0) 1687 return (error); 1688 pdeath_signal = bsd_to_linux_signal(pdeath_signal); 1689 return (copyout(&pdeath_signal, 1690 (void *)(register_t)args->arg2, 1691 sizeof(pdeath_signal))); 1692 /* 1693 * In Linux, this flag controls if set[gu]id processes can coredump. 1694 * There are additional semantics imposed on processes that cannot 1695 * coredump: 1696 * - Such processes can not be ptraced. 1697 * - There are some semantics around ownership of process-related files 1698 * in the /proc namespace. 1699 * 1700 * In FreeBSD, we can (and by default, do) disable setuid coredump 1701 * system-wide with 'sugid_coredump.' We control tracability on a 1702 * per-process basis with the procctl PROC_TRACE (=> P2_NOTRACE flag). 1703 * By happy coincidence, P2_NOTRACE also prevents coredumping. So the 1704 * procctl is roughly analogous to Linux's DUMPABLE. 1705 * 1706 * So, proxy these knobs to the corresponding PROC_TRACE setting. 1707 */ 1708 case LINUX_PR_GET_DUMPABLE: 1709 error = kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_STATUS, 1710 &trace_state); 1711 if (error != 0) 1712 return (error); 1713 td->td_retval[0] = (trace_state != -1); 1714 return (0); 1715 case LINUX_PR_SET_DUMPABLE: 1716 /* 1717 * It is only valid for userspace to set one of these two 1718 * flags, and only one at a time. 1719 */ 1720 switch (args->arg2) { 1721 case LINUX_SUID_DUMP_DISABLE: 1722 trace_state = PROC_TRACE_CTL_DISABLE_EXEC; 1723 break; 1724 case LINUX_SUID_DUMP_USER: 1725 trace_state = PROC_TRACE_CTL_ENABLE; 1726 break; 1727 default: 1728 return (EINVAL); 1729 } 1730 return (kern_procctl(td, P_PID, p->p_pid, PROC_TRACE_CTL, 1731 &trace_state)); 1732 case LINUX_PR_GET_KEEPCAPS: 1733 /* 1734 * Indicate that we always clear the effective and 1735 * permitted capability sets when the user id becomes 1736 * non-zero (actually the capability sets are simply 1737 * always zero in the current implementation). 1738 */ 1739 td->td_retval[0] = 0; 1740 break; 1741 case LINUX_PR_SET_KEEPCAPS: 1742 /* 1743 * Ignore requests to keep the effective and permitted 1744 * capability sets when the user id becomes non-zero. 1745 */ 1746 break; 1747 case LINUX_PR_SET_NAME: 1748 /* 1749 * To be on the safe side we need to make sure to not 1750 * overflow the size a Linux program expects. We already 1751 * do this here in the copyin, so that we don't need to 1752 * check on copyout. 1753 */ 1754 max_size = MIN(sizeof(comm), sizeof(p->p_comm)); 1755 error = copyinstr((void *)(register_t)args->arg2, comm, 1756 max_size, NULL); 1757 1758 /* Linux silently truncates the name if it is too long. */ 1759 if (error == ENAMETOOLONG) { 1760 /* 1761 * XXX: copyinstr() isn't documented to populate the 1762 * array completely, so do a copyin() to be on the 1763 * safe side. This should be changed in case 1764 * copyinstr() is changed to guarantee this. 1765 */ 1766 error = copyin((void *)(register_t)args->arg2, comm, 1767 max_size - 1); 1768 comm[max_size - 1] = '\0'; 1769 } 1770 if (error) 1771 return (error); 1772 1773 PROC_LOCK(p); 1774 strlcpy(p->p_comm, comm, sizeof(p->p_comm)); 1775 PROC_UNLOCK(p); 1776 break; 1777 case LINUX_PR_GET_NAME: 1778 PROC_LOCK(p); 1779 strlcpy(comm, p->p_comm, sizeof(comm)); 1780 PROC_UNLOCK(p); 1781 error = copyout(comm, (void *)(register_t)args->arg2, 1782 strlen(comm) + 1); 1783 break; 1784 case LINUX_PR_GET_SECCOMP: 1785 case LINUX_PR_SET_SECCOMP: 1786 /* 1787 * Same as returned by Linux without CONFIG_SECCOMP enabled. 1788 */ 1789 error = EINVAL; 1790 break; 1791 case LINUX_PR_CAPBSET_READ: 1792 #if 0 1793 /* 1794 * This makes too much noise with Ubuntu Focal. 1795 */ 1796 linux_msg(td, "unsupported prctl PR_CAPBSET_READ %d", 1797 (int)args->arg2); 1798 #endif 1799 error = EINVAL; 1800 break; 1801 case LINUX_PR_SET_CHILD_SUBREAPER: 1802 if (args->arg2 == 0) { 1803 return (kern_procctl(td, P_PID, 0, PROC_REAP_RELEASE, 1804 NULL)); 1805 } 1806 1807 return (kern_procctl(td, P_PID, 0, PROC_REAP_ACQUIRE, 1808 NULL)); 1809 case LINUX_PR_SET_NO_NEW_PRIVS: 1810 arg = args->arg2 == 1 ? 1811 PROC_NO_NEW_PRIVS_ENABLE : PROC_NO_NEW_PRIVS_DISABLE; 1812 error = kern_procctl(td, P_PID, p->p_pid, 1813 PROC_NO_NEW_PRIVS_CTL, &arg); 1814 break; 1815 case LINUX_PR_SET_PTRACER: 1816 linux_msg(td, "unsupported prctl PR_SET_PTRACER"); 1817 error = EINVAL; 1818 break; 1819 default: 1820 linux_msg(td, "unsupported prctl option %d", args->option); 1821 error = EINVAL; 1822 break; 1823 } 1824 1825 return (error); 1826 } 1827 1828 int 1829 linux_sched_setparam(struct thread *td, 1830 struct linux_sched_setparam_args *uap) 1831 { 1832 struct sched_param sched_param; 1833 struct thread *tdt; 1834 int error, policy; 1835 1836 error = copyin(uap->param, &sched_param, sizeof(sched_param)); 1837 if (error) 1838 return (error); 1839 1840 tdt = linux_tdfind(td, uap->pid, -1); 1841 if (tdt == NULL) 1842 return (ESRCH); 1843 1844 if (linux_map_sched_prio) { 1845 error = kern_sched_getscheduler(td, tdt, &policy); 1846 if (error) 1847 goto out; 1848 1849 switch (policy) { 1850 case SCHED_OTHER: 1851 if (sched_param.sched_priority != 0) { 1852 error = EINVAL; 1853 goto out; 1854 } 1855 sched_param.sched_priority = 1856 PRI_MAX_TIMESHARE - PRI_MIN_TIMESHARE; 1857 break; 1858 case SCHED_FIFO: 1859 case SCHED_RR: 1860 if (sched_param.sched_priority < 1 || 1861 sched_param.sched_priority >= LINUX_MAX_RT_PRIO) { 1862 error = EINVAL; 1863 goto out; 1864 } 1865 /* 1866 * Map [1, LINUX_MAX_RT_PRIO - 1] to 1867 * [0, RTP_PRIO_MAX - RTP_PRIO_MIN] (rounding down). 1868 */ 1869 sched_param.sched_priority = 1870 (sched_param.sched_priority - 1) * 1871 (RTP_PRIO_MAX - RTP_PRIO_MIN + 1) / 1872 (LINUX_MAX_RT_PRIO - 1); 1873 break; 1874 } 1875 } 1876 1877 error = kern_sched_setparam(td, tdt, &sched_param); 1878 out: PROC_UNLOCK(tdt->td_proc); 1879 return (error); 1880 } 1881 1882 int 1883 linux_sched_getparam(struct thread *td, 1884 struct linux_sched_getparam_args *uap) 1885 { 1886 struct sched_param sched_param; 1887 struct thread *tdt; 1888 int error, policy; 1889 1890 tdt = linux_tdfind(td, uap->pid, -1); 1891 if (tdt == NULL) 1892 return (ESRCH); 1893 1894 error = kern_sched_getparam(td, tdt, &sched_param); 1895 if (error) { 1896 PROC_UNLOCK(tdt->td_proc); 1897 return (error); 1898 } 1899 1900 if (linux_map_sched_prio) { 1901 error = kern_sched_getscheduler(td, tdt, &policy); 1902 PROC_UNLOCK(tdt->td_proc); 1903 if (error) 1904 return (error); 1905 1906 switch (policy) { 1907 case SCHED_OTHER: 1908 sched_param.sched_priority = 0; 1909 break; 1910 case SCHED_FIFO: 1911 case SCHED_RR: 1912 /* 1913 * Map [0, RTP_PRIO_MAX - RTP_PRIO_MIN] to 1914 * [1, LINUX_MAX_RT_PRIO - 1] (rounding up). 1915 */ 1916 sched_param.sched_priority = 1917 (sched_param.sched_priority * 1918 (LINUX_MAX_RT_PRIO - 1) + 1919 (RTP_PRIO_MAX - RTP_PRIO_MIN - 1)) / 1920 (RTP_PRIO_MAX - RTP_PRIO_MIN) + 1; 1921 break; 1922 } 1923 } else 1924 PROC_UNLOCK(tdt->td_proc); 1925 1926 error = copyout(&sched_param, uap->param, sizeof(sched_param)); 1927 return (error); 1928 } 1929 1930 /* 1931 * Get affinity of a process. 1932 */ 1933 int 1934 linux_sched_getaffinity(struct thread *td, 1935 struct linux_sched_getaffinity_args *args) 1936 { 1937 struct thread *tdt; 1938 cpuset_t *mask; 1939 size_t size; 1940 int error; 1941 id_t tid; 1942 1943 tdt = linux_tdfind(td, args->pid, -1); 1944 if (tdt == NULL) 1945 return (ESRCH); 1946 tid = tdt->td_tid; 1947 PROC_UNLOCK(tdt->td_proc); 1948 1949 mask = malloc(sizeof(cpuset_t), M_LINUX, M_WAITOK | M_ZERO); 1950 size = min(args->len, sizeof(cpuset_t)); 1951 error = kern_cpuset_getaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID, 1952 tid, size, mask); 1953 if (error == ERANGE) 1954 error = EINVAL; 1955 if (error == 0) 1956 error = copyout(mask, args->user_mask_ptr, size); 1957 if (error == 0) 1958 td->td_retval[0] = size; 1959 free(mask, M_LINUX); 1960 return (error); 1961 } 1962 1963 /* 1964 * Set affinity of a process. 1965 */ 1966 int 1967 linux_sched_setaffinity(struct thread *td, 1968 struct linux_sched_setaffinity_args *args) 1969 { 1970 struct thread *tdt; 1971 cpuset_t *mask; 1972 int cpu, error; 1973 size_t len; 1974 id_t tid; 1975 1976 tdt = linux_tdfind(td, args->pid, -1); 1977 if (tdt == NULL) 1978 return (ESRCH); 1979 tid = tdt->td_tid; 1980 PROC_UNLOCK(tdt->td_proc); 1981 1982 len = min(args->len, sizeof(cpuset_t)); 1983 mask = malloc(sizeof(cpuset_t), M_TEMP, M_WAITOK | M_ZERO); 1984 error = copyin(args->user_mask_ptr, mask, len); 1985 if (error != 0) 1986 goto out; 1987 /* Linux ignore high bits */ 1988 CPU_FOREACH_ISSET(cpu, mask) 1989 if (cpu > mp_maxid) 1990 CPU_CLR(cpu, mask); 1991 1992 error = kern_cpuset_setaffinity(td, CPU_LEVEL_WHICH, CPU_WHICH_TID, 1993 tid, mask); 1994 if (error == EDEADLK) 1995 error = EINVAL; 1996 out: 1997 free(mask, M_TEMP); 1998 return (error); 1999 } 2000 2001 struct linux_rlimit64 { 2002 uint64_t rlim_cur; 2003 uint64_t rlim_max; 2004 }; 2005 2006 int 2007 linux_prlimit64(struct thread *td, struct linux_prlimit64_args *args) 2008 { 2009 struct rlimit rlim, nrlim; 2010 struct linux_rlimit64 lrlim; 2011 struct proc *p; 2012 u_int which; 2013 int flags; 2014 int error; 2015 2016 if (args->new == NULL && args->old != NULL) { 2017 if (linux_get_dummy_limit(td, args->resource, &rlim)) { 2018 lrlim.rlim_cur = rlim.rlim_cur; 2019 lrlim.rlim_max = rlim.rlim_max; 2020 return (copyout(&lrlim, args->old, sizeof(lrlim))); 2021 } 2022 } 2023 2024 if (args->resource >= LINUX_RLIM_NLIMITS) 2025 return (EINVAL); 2026 2027 which = linux_to_bsd_resource[args->resource]; 2028 if (which == -1) 2029 return (EINVAL); 2030 2031 if (args->new != NULL) { 2032 /* 2033 * Note. Unlike FreeBSD where rlim is signed 64-bit Linux 2034 * rlim is unsigned 64-bit. FreeBSD treats negative limits 2035 * as INFINITY so we do not need a conversion even. 2036 */ 2037 error = copyin(args->new, &nrlim, sizeof(nrlim)); 2038 if (error != 0) 2039 return (error); 2040 } 2041 2042 flags = PGET_HOLD | PGET_NOTWEXIT; 2043 if (args->new != NULL) 2044 flags |= PGET_CANDEBUG; 2045 else 2046 flags |= PGET_CANSEE; 2047 if (args->pid == 0) { 2048 p = td->td_proc; 2049 PHOLD(p); 2050 } else { 2051 error = pget(args->pid, flags, &p); 2052 if (error != 0) 2053 return (error); 2054 } 2055 if (args->old != NULL) { 2056 PROC_LOCK(p); 2057 lim_rlimit_proc(p, which, &rlim); 2058 PROC_UNLOCK(p); 2059 if (rlim.rlim_cur == RLIM_INFINITY) 2060 lrlim.rlim_cur = LINUX_RLIM_INFINITY; 2061 else 2062 lrlim.rlim_cur = rlim.rlim_cur; 2063 if (rlim.rlim_max == RLIM_INFINITY) 2064 lrlim.rlim_max = LINUX_RLIM_INFINITY; 2065 else 2066 lrlim.rlim_max = rlim.rlim_max; 2067 error = copyout(&lrlim, args->old, sizeof(lrlim)); 2068 if (error != 0) 2069 goto out; 2070 } 2071 2072 if (args->new != NULL) 2073 error = kern_proc_setrlimit(td, p, which, &nrlim); 2074 2075 out: 2076 PRELE(p); 2077 return (error); 2078 } 2079 2080 int 2081 linux_pselect6(struct thread *td, struct linux_pselect6_args *args) 2082 { 2083 struct timespec ts, *tsp; 2084 int error; 2085 2086 if (args->tsp != NULL) { 2087 error = linux_get_timespec(&ts, args->tsp); 2088 if (error != 0) 2089 return (error); 2090 tsp = &ts; 2091 } else 2092 tsp = NULL; 2093 2094 error = linux_common_pselect6(td, args->nfds, args->readfds, 2095 args->writefds, args->exceptfds, tsp, args->sig); 2096 2097 if (args->tsp != NULL) 2098 linux_put_timespec(&ts, args->tsp); 2099 return (error); 2100 } 2101 2102 static int 2103 linux_common_pselect6(struct thread *td, l_int nfds, l_fd_set *readfds, 2104 l_fd_set *writefds, l_fd_set *exceptfds, struct timespec *tsp, 2105 l_uintptr_t *sig) 2106 { 2107 struct timeval utv, tv0, tv1, *tvp; 2108 struct l_pselect6arg lpse6; 2109 sigset_t *ssp; 2110 sigset_t ss; 2111 int error; 2112 2113 ssp = NULL; 2114 if (sig != NULL) { 2115 error = copyin(sig, &lpse6, sizeof(lpse6)); 2116 if (error != 0) 2117 return (error); 2118 error = linux_copyin_sigset(td, PTRIN(lpse6.ss), 2119 lpse6.ss_len, &ss, &ssp); 2120 if (error != 0) 2121 return (error); 2122 } else 2123 ssp = NULL; 2124 2125 /* 2126 * Currently glibc changes nanosecond number to microsecond. 2127 * This mean losing precision but for now it is hardly seen. 2128 */ 2129 if (tsp != NULL) { 2130 TIMESPEC_TO_TIMEVAL(&utv, tsp); 2131 if (itimerfix(&utv)) 2132 return (EINVAL); 2133 2134 microtime(&tv0); 2135 tvp = &utv; 2136 } else 2137 tvp = NULL; 2138 2139 error = kern_pselect(td, nfds, readfds, writefds, 2140 exceptfds, tvp, ssp, LINUX_NFDBITS); 2141 2142 if (tsp != NULL) { 2143 /* 2144 * Compute how much time was left of the timeout, 2145 * by subtracting the current time and the time 2146 * before we started the call, and subtracting 2147 * that result from the user-supplied value. 2148 */ 2149 microtime(&tv1); 2150 timevalsub(&tv1, &tv0); 2151 timevalsub(&utv, &tv1); 2152 if (utv.tv_sec < 0) 2153 timevalclear(&utv); 2154 TIMEVAL_TO_TIMESPEC(&utv, tsp); 2155 } 2156 return (error); 2157 } 2158 2159 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2160 int 2161 linux_pselect6_time64(struct thread *td, 2162 struct linux_pselect6_time64_args *args) 2163 { 2164 struct timespec ts, *tsp; 2165 int error; 2166 2167 if (args->tsp != NULL) { 2168 error = linux_get_timespec64(&ts, args->tsp); 2169 if (error != 0) 2170 return (error); 2171 tsp = &ts; 2172 } else 2173 tsp = NULL; 2174 2175 error = linux_common_pselect6(td, args->nfds, args->readfds, 2176 args->writefds, args->exceptfds, tsp, args->sig); 2177 2178 if (args->tsp != NULL) 2179 linux_put_timespec64(&ts, args->tsp); 2180 return (error); 2181 } 2182 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 2183 2184 int 2185 linux_ppoll(struct thread *td, struct linux_ppoll_args *args) 2186 { 2187 struct timespec uts, *tsp; 2188 int error; 2189 2190 if (args->tsp != NULL) { 2191 error = linux_get_timespec(&uts, args->tsp); 2192 if (error != 0) 2193 return (error); 2194 tsp = &uts; 2195 } else 2196 tsp = NULL; 2197 2198 error = linux_common_ppoll(td, args->fds, args->nfds, tsp, 2199 args->sset, args->ssize); 2200 if (error == 0 && args->tsp != NULL) 2201 error = linux_put_timespec(&uts, args->tsp); 2202 return (error); 2203 } 2204 2205 static int 2206 linux_common_ppoll(struct thread *td, struct pollfd *fds, uint32_t nfds, 2207 struct timespec *tsp, l_sigset_t *sset, l_size_t ssize) 2208 { 2209 struct timespec ts0, ts1; 2210 struct pollfd stackfds[32]; 2211 struct pollfd *kfds; 2212 sigset_t *ssp; 2213 sigset_t ss; 2214 int error; 2215 2216 if (kern_poll_maxfds(nfds)) 2217 return (EINVAL); 2218 if (sset != NULL) { 2219 error = linux_copyin_sigset(td, sset, ssize, &ss, &ssp); 2220 if (error != 0) 2221 return (error); 2222 } else 2223 ssp = NULL; 2224 if (tsp != NULL) 2225 nanotime(&ts0); 2226 2227 if (nfds > nitems(stackfds)) 2228 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); 2229 else 2230 kfds = stackfds; 2231 error = linux_pollin(td, kfds, fds, nfds); 2232 if (error != 0) 2233 goto out; 2234 2235 error = kern_poll_kfds(td, kfds, nfds, tsp, ssp); 2236 if (error == 0) 2237 error = linux_pollout(td, kfds, fds, nfds); 2238 2239 if (error == 0 && tsp != NULL) { 2240 if (td->td_retval[0]) { 2241 nanotime(&ts1); 2242 timespecsub(&ts1, &ts0, &ts1); 2243 timespecsub(tsp, &ts1, tsp); 2244 if (tsp->tv_sec < 0) 2245 timespecclear(tsp); 2246 } else 2247 timespecclear(tsp); 2248 } 2249 2250 out: 2251 if (nfds > nitems(stackfds)) 2252 free(kfds, M_TEMP); 2253 return (error); 2254 } 2255 2256 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2257 int 2258 linux_ppoll_time64(struct thread *td, struct linux_ppoll_time64_args *args) 2259 { 2260 struct timespec uts, *tsp; 2261 int error; 2262 2263 if (args->tsp != NULL) { 2264 error = linux_get_timespec64(&uts, args->tsp); 2265 if (error != 0) 2266 return (error); 2267 tsp = &uts; 2268 } else 2269 tsp = NULL; 2270 error = linux_common_ppoll(td, args->fds, args->nfds, tsp, 2271 args->sset, args->ssize); 2272 if (error == 0 && args->tsp != NULL) 2273 error = linux_put_timespec64(&uts, args->tsp); 2274 return (error); 2275 } 2276 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 2277 2278 static int 2279 linux_pollin(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 2280 { 2281 int error; 2282 u_int i; 2283 2284 error = copyin(ufds, fds, nfd * sizeof(*fds)); 2285 if (error != 0) 2286 return (error); 2287 2288 for (i = 0; i < nfd; i++) { 2289 if (fds->events != 0) 2290 linux_to_bsd_poll_events(td, fds->fd, 2291 fds->events, &fds->events); 2292 fds++; 2293 } 2294 return (0); 2295 } 2296 2297 static int 2298 linux_pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd) 2299 { 2300 int error = 0; 2301 u_int i, n = 0; 2302 2303 for (i = 0; i < nfd; i++) { 2304 if (fds->revents != 0) { 2305 bsd_to_linux_poll_events(fds->revents, 2306 &fds->revents); 2307 n++; 2308 } 2309 error = copyout(&fds->revents, &ufds->revents, 2310 sizeof(ufds->revents)); 2311 if (error) 2312 return (error); 2313 fds++; 2314 ufds++; 2315 } 2316 td->td_retval[0] = n; 2317 return (0); 2318 } 2319 2320 static int 2321 linux_sched_rr_get_interval_common(struct thread *td, pid_t pid, 2322 struct timespec *ts) 2323 { 2324 struct thread *tdt; 2325 int error; 2326 2327 /* 2328 * According to man in case the invalid pid specified 2329 * EINVAL should be returned. 2330 */ 2331 if (pid < 0) 2332 return (EINVAL); 2333 2334 tdt = linux_tdfind(td, pid, -1); 2335 if (tdt == NULL) 2336 return (ESRCH); 2337 2338 error = kern_sched_rr_get_interval_td(td, tdt, ts); 2339 PROC_UNLOCK(tdt->td_proc); 2340 return (error); 2341 } 2342 2343 int 2344 linux_sched_rr_get_interval(struct thread *td, 2345 struct linux_sched_rr_get_interval_args *uap) 2346 { 2347 struct timespec ts; 2348 int error; 2349 2350 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts); 2351 if (error != 0) 2352 return (error); 2353 return (linux_put_timespec(&ts, uap->interval)); 2354 } 2355 2356 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2357 int 2358 linux_sched_rr_get_interval_time64(struct thread *td, 2359 struct linux_sched_rr_get_interval_time64_args *uap) 2360 { 2361 struct timespec ts; 2362 int error; 2363 2364 error = linux_sched_rr_get_interval_common(td, uap->pid, &ts); 2365 if (error != 0) 2366 return (error); 2367 return (linux_put_timespec64(&ts, uap->interval)); 2368 } 2369 #endif 2370 2371 /* 2372 * In case when the Linux thread is the initial thread in 2373 * the thread group thread id is equal to the process id. 2374 * Glibc depends on this magic (assert in pthread_getattr_np.c). 2375 */ 2376 struct thread * 2377 linux_tdfind(struct thread *td, lwpid_t tid, pid_t pid) 2378 { 2379 struct linux_emuldata *em; 2380 struct thread *tdt; 2381 struct proc *p; 2382 2383 tdt = NULL; 2384 if (tid == 0 || tid == td->td_tid) { 2385 if (pid != -1 && td->td_proc->p_pid != pid) 2386 return (NULL); 2387 PROC_LOCK(td->td_proc); 2388 return (td); 2389 } else if (tid > PID_MAX) 2390 return (tdfind(tid, pid)); 2391 2392 /* 2393 * Initial thread where the tid equal to the pid. 2394 */ 2395 p = pfind(tid); 2396 if (p != NULL) { 2397 if (SV_PROC_ABI(p) != SV_ABI_LINUX || 2398 (pid != -1 && tid != pid)) { 2399 /* 2400 * p is not a Linuxulator process. 2401 */ 2402 PROC_UNLOCK(p); 2403 return (NULL); 2404 } 2405 FOREACH_THREAD_IN_PROC(p, tdt) { 2406 em = em_find(tdt); 2407 if (tid == em->em_tid) 2408 return (tdt); 2409 } 2410 PROC_UNLOCK(p); 2411 } 2412 return (NULL); 2413 } 2414 2415 void 2416 linux_to_bsd_waitopts(int options, int *bsdopts) 2417 { 2418 2419 if (options & LINUX_WNOHANG) 2420 *bsdopts |= WNOHANG; 2421 if (options & LINUX_WUNTRACED) 2422 *bsdopts |= WUNTRACED; 2423 if (options & LINUX_WEXITED) 2424 *bsdopts |= WEXITED; 2425 if (options & LINUX_WCONTINUED) 2426 *bsdopts |= WCONTINUED; 2427 if (options & LINUX_WNOWAIT) 2428 *bsdopts |= WNOWAIT; 2429 2430 if (options & __WCLONE) 2431 *bsdopts |= WLINUXCLONE; 2432 } 2433 2434 int 2435 linux_getrandom(struct thread *td, struct linux_getrandom_args *args) 2436 { 2437 struct uio uio; 2438 struct iovec iov; 2439 int error; 2440 2441 if (args->flags & ~(LINUX_GRND_NONBLOCK|LINUX_GRND_RANDOM)) 2442 return (EINVAL); 2443 if (args->count > INT_MAX) 2444 args->count = INT_MAX; 2445 2446 iov.iov_base = args->buf; 2447 iov.iov_len = args->count; 2448 2449 uio.uio_iov = &iov; 2450 uio.uio_iovcnt = 1; 2451 uio.uio_resid = iov.iov_len; 2452 uio.uio_segflg = UIO_USERSPACE; 2453 uio.uio_rw = UIO_READ; 2454 uio.uio_td = td; 2455 2456 error = read_random_uio(&uio, args->flags & LINUX_GRND_NONBLOCK); 2457 if (error == 0) 2458 td->td_retval[0] = args->count - uio.uio_resid; 2459 return (error); 2460 } 2461 2462 int 2463 linux_mincore(struct thread *td, struct linux_mincore_args *args) 2464 { 2465 2466 /* Needs to be page-aligned */ 2467 if (args->start & PAGE_MASK) 2468 return (EINVAL); 2469 return (kern_mincore(td, args->start, args->len, args->vec)); 2470 } 2471 2472 #define SYSLOG_TAG "<6>" 2473 2474 int 2475 linux_syslog(struct thread *td, struct linux_syslog_args *args) 2476 { 2477 char buf[128], *src, *dst; 2478 u_int seq; 2479 int buflen, error; 2480 2481 if (args->type != LINUX_SYSLOG_ACTION_READ_ALL) { 2482 linux_msg(td, "syslog unsupported type 0x%x", args->type); 2483 return (EINVAL); 2484 } 2485 2486 if (args->len < 6) { 2487 td->td_retval[0] = 0; 2488 return (0); 2489 } 2490 2491 error = priv_check(td, PRIV_MSGBUF); 2492 if (error) 2493 return (error); 2494 2495 mtx_lock(&msgbuf_lock); 2496 msgbuf_peekbytes(msgbufp, NULL, 0, &seq); 2497 mtx_unlock(&msgbuf_lock); 2498 2499 dst = args->buf; 2500 error = copyout(&SYSLOG_TAG, dst, sizeof(SYSLOG_TAG)); 2501 /* The -1 is to skip the trailing '\0'. */ 2502 dst += sizeof(SYSLOG_TAG) - 1; 2503 2504 while (error == 0) { 2505 mtx_lock(&msgbuf_lock); 2506 buflen = msgbuf_peekbytes(msgbufp, buf, sizeof(buf), &seq); 2507 mtx_unlock(&msgbuf_lock); 2508 2509 if (buflen == 0) 2510 break; 2511 2512 for (src = buf; src < buf + buflen && error == 0; src++) { 2513 if (*src == '\0') 2514 continue; 2515 2516 if (dst >= args->buf + args->len) 2517 goto out; 2518 2519 error = copyout(src, dst, 1); 2520 dst++; 2521 2522 if (*src == '\n' && *(src + 1) != '<' && 2523 dst + sizeof(SYSLOG_TAG) < args->buf + args->len) { 2524 error = copyout(&SYSLOG_TAG, 2525 dst, sizeof(SYSLOG_TAG)); 2526 dst += sizeof(SYSLOG_TAG) - 1; 2527 } 2528 } 2529 } 2530 out: 2531 td->td_retval[0] = dst - args->buf; 2532 return (error); 2533 } 2534 2535 int 2536 linux_getcpu(struct thread *td, struct linux_getcpu_args *args) 2537 { 2538 int cpu, error, node; 2539 2540 cpu = td->td_oncpu; /* Make sure it doesn't change during copyout(9) */ 2541 error = 0; 2542 node = cpuid_to_pcpu[cpu]->pc_domain; 2543 2544 if (args->cpu != NULL) 2545 error = copyout(&cpu, args->cpu, sizeof(l_int)); 2546 if (args->node != NULL) 2547 error = copyout(&node, args->node, sizeof(l_int)); 2548 return (error); 2549 } 2550 2551 #if defined(__i386__) || defined(__amd64__) 2552 int 2553 linux_poll(struct thread *td, struct linux_poll_args *args) 2554 { 2555 struct timespec ts, *tsp; 2556 2557 if (args->timeout != INFTIM) { 2558 if (args->timeout < 0) 2559 return (EINVAL); 2560 ts.tv_sec = args->timeout / 1000; 2561 ts.tv_nsec = (args->timeout % 1000) * 1000000; 2562 tsp = &ts; 2563 } else 2564 tsp = NULL; 2565 2566 return (linux_common_ppoll(td, args->fds, args->nfds, 2567 tsp, NULL, 0)); 2568 } 2569 #endif /* __i386__ || __amd64__ */ 2570 2571 int 2572 linux_seccomp(struct thread *td, struct linux_seccomp_args *args) 2573 { 2574 2575 switch (args->op) { 2576 case LINUX_SECCOMP_GET_ACTION_AVAIL: 2577 return (EOPNOTSUPP); 2578 default: 2579 /* 2580 * Ignore unknown operations, just like Linux kernel built 2581 * without CONFIG_SECCOMP. 2582 */ 2583 return (EINVAL); 2584 } 2585 } 2586 2587 /* 2588 * Custom version of exec_copyin_args(), to copy out argument and environment 2589 * strings from the old process address space into the temporary string buffer. 2590 * Based on freebsd32_exec_copyin_args. 2591 */ 2592 static int 2593 linux_exec_copyin_args(struct image_args *args, const char *fname, 2594 l_uintptr_t *argv, l_uintptr_t *envv) 2595 { 2596 char *argp, *envp; 2597 l_uintptr_t *ptr, arg; 2598 int error; 2599 2600 bzero(args, sizeof(*args)); 2601 if (argv == NULL) 2602 return (EFAULT); 2603 2604 /* 2605 * Allocate demand-paged memory for the file name, argument, and 2606 * environment strings. 2607 */ 2608 error = exec_alloc_args(args); 2609 if (error != 0) 2610 return (error); 2611 2612 /* 2613 * Copy the file name. 2614 */ 2615 error = exec_args_add_fname(args, fname, UIO_USERSPACE); 2616 if (error != 0) 2617 goto err_exit; 2618 2619 /* 2620 * extract arguments first 2621 */ 2622 ptr = argv; 2623 for (;;) { 2624 error = copyin(ptr++, &arg, sizeof(arg)); 2625 if (error) 2626 goto err_exit; 2627 if (arg == 0) 2628 break; 2629 argp = PTRIN(arg); 2630 error = exec_args_add_arg(args, argp, UIO_USERSPACE); 2631 if (error != 0) 2632 goto err_exit; 2633 } 2634 2635 /* 2636 * This comment is from Linux do_execveat_common: 2637 * When argv is empty, add an empty string ("") as argv[0] to 2638 * ensure confused userspace programs that start processing 2639 * from argv[1] won't end up walking envp. 2640 */ 2641 if (args->argc == 0 && 2642 (error = exec_args_add_arg(args, "", UIO_SYSSPACE) != 0)) 2643 goto err_exit; 2644 2645 /* 2646 * extract environment strings 2647 */ 2648 if (envv) { 2649 ptr = envv; 2650 for (;;) { 2651 error = copyin(ptr++, &arg, sizeof(arg)); 2652 if (error) 2653 goto err_exit; 2654 if (arg == 0) 2655 break; 2656 envp = PTRIN(arg); 2657 error = exec_args_add_env(args, envp, UIO_USERSPACE); 2658 if (error != 0) 2659 goto err_exit; 2660 } 2661 } 2662 2663 return (0); 2664 2665 err_exit: 2666 exec_free_args(args); 2667 return (error); 2668 } 2669 2670 int 2671 linux_execve(struct thread *td, struct linux_execve_args *args) 2672 { 2673 struct image_args eargs; 2674 int error; 2675 2676 LINUX_CTR(execve); 2677 2678 error = linux_exec_copyin_args(&eargs, args->path, args->argp, 2679 args->envp); 2680 if (error == 0) 2681 error = linux_common_execve(td, &eargs); 2682 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 2683 return (error); 2684 } 2685 2686 static void 2687 linux_up_rtprio_if(struct thread *td1, struct rtprio *rtp) 2688 { 2689 struct rtprio rtp2; 2690 2691 pri_to_rtp(td1, &rtp2); 2692 if (rtp2.type < rtp->type || 2693 (rtp2.type == rtp->type && 2694 rtp2.prio < rtp->prio)) { 2695 rtp->type = rtp2.type; 2696 rtp->prio = rtp2.prio; 2697 } 2698 } 2699 2700 #define LINUX_PRIO_DIVIDER RTP_PRIO_MAX / LINUX_IOPRIO_MAX 2701 2702 static int 2703 linux_rtprio2ioprio(struct rtprio *rtp) 2704 { 2705 int ioprio, prio; 2706 2707 switch (rtp->type) { 2708 case RTP_PRIO_IDLE: 2709 prio = RTP_PRIO_MIN; 2710 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_IDLE, prio); 2711 break; 2712 case RTP_PRIO_NORMAL: 2713 prio = rtp->prio / LINUX_PRIO_DIVIDER; 2714 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_BE, prio); 2715 break; 2716 case RTP_PRIO_REALTIME: 2717 prio = rtp->prio / LINUX_PRIO_DIVIDER; 2718 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_RT, prio); 2719 break; 2720 default: 2721 prio = RTP_PRIO_MIN; 2722 ioprio = LINUX_IOPRIO_PRIO(LINUX_IOPRIO_CLASS_NONE, prio); 2723 break; 2724 } 2725 return (ioprio); 2726 } 2727 2728 static int 2729 linux_ioprio2rtprio(int ioprio, struct rtprio *rtp) 2730 { 2731 2732 switch (LINUX_IOPRIO_PRIO_CLASS(ioprio)) { 2733 case LINUX_IOPRIO_CLASS_IDLE: 2734 rtp->prio = RTP_PRIO_MIN; 2735 rtp->type = RTP_PRIO_IDLE; 2736 break; 2737 case LINUX_IOPRIO_CLASS_BE: 2738 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER; 2739 rtp->type = RTP_PRIO_NORMAL; 2740 break; 2741 case LINUX_IOPRIO_CLASS_RT: 2742 rtp->prio = LINUX_IOPRIO_PRIO_DATA(ioprio) * LINUX_PRIO_DIVIDER; 2743 rtp->type = RTP_PRIO_REALTIME; 2744 break; 2745 default: 2746 return (EINVAL); 2747 } 2748 return (0); 2749 } 2750 #undef LINUX_PRIO_DIVIDER 2751 2752 int 2753 linux_ioprio_get(struct thread *td, struct linux_ioprio_get_args *args) 2754 { 2755 struct thread *td1; 2756 struct rtprio rtp; 2757 struct pgrp *pg; 2758 struct proc *p; 2759 int error, found; 2760 2761 p = NULL; 2762 td1 = NULL; 2763 error = 0; 2764 found = 0; 2765 rtp.type = RTP_PRIO_IDLE; 2766 rtp.prio = RTP_PRIO_MAX; 2767 switch (args->which) { 2768 case LINUX_IOPRIO_WHO_PROCESS: 2769 if (args->who == 0) { 2770 td1 = td; 2771 p = td1->td_proc; 2772 PROC_LOCK(p); 2773 } else if (args->who > PID_MAX) { 2774 td1 = linux_tdfind(td, args->who, -1); 2775 if (td1 != NULL) 2776 p = td1->td_proc; 2777 } else 2778 p = pfind(args->who); 2779 if (p == NULL) 2780 return (ESRCH); 2781 if ((error = p_cansee(td, p))) { 2782 PROC_UNLOCK(p); 2783 break; 2784 } 2785 if (td1 != NULL) { 2786 pri_to_rtp(td1, &rtp); 2787 } else { 2788 FOREACH_THREAD_IN_PROC(p, td1) { 2789 linux_up_rtprio_if(td1, &rtp); 2790 } 2791 } 2792 found++; 2793 PROC_UNLOCK(p); 2794 break; 2795 case LINUX_IOPRIO_WHO_PGRP: 2796 sx_slock(&proctree_lock); 2797 if (args->who == 0) { 2798 pg = td->td_proc->p_pgrp; 2799 PGRP_LOCK(pg); 2800 } else { 2801 pg = pgfind(args->who); 2802 if (pg == NULL) { 2803 sx_sunlock(&proctree_lock); 2804 error = ESRCH; 2805 break; 2806 } 2807 } 2808 sx_sunlock(&proctree_lock); 2809 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 2810 PROC_LOCK(p); 2811 if (p->p_state == PRS_NORMAL && 2812 p_cansee(td, p) == 0) { 2813 FOREACH_THREAD_IN_PROC(p, td1) { 2814 linux_up_rtprio_if(td1, &rtp); 2815 found++; 2816 } 2817 } 2818 PROC_UNLOCK(p); 2819 } 2820 PGRP_UNLOCK(pg); 2821 break; 2822 case LINUX_IOPRIO_WHO_USER: 2823 if (args->who == 0) 2824 args->who = td->td_ucred->cr_uid; 2825 sx_slock(&allproc_lock); 2826 FOREACH_PROC_IN_SYSTEM(p) { 2827 PROC_LOCK(p); 2828 if (p->p_state == PRS_NORMAL && 2829 p->p_ucred->cr_uid == args->who && 2830 p_cansee(td, p) == 0) { 2831 FOREACH_THREAD_IN_PROC(p, td1) { 2832 linux_up_rtprio_if(td1, &rtp); 2833 found++; 2834 } 2835 } 2836 PROC_UNLOCK(p); 2837 } 2838 sx_sunlock(&allproc_lock); 2839 break; 2840 default: 2841 error = EINVAL; 2842 break; 2843 } 2844 if (error == 0) { 2845 if (found != 0) 2846 td->td_retval[0] = linux_rtprio2ioprio(&rtp); 2847 else 2848 error = ESRCH; 2849 } 2850 return (error); 2851 } 2852 2853 int 2854 linux_ioprio_set(struct thread *td, struct linux_ioprio_set_args *args) 2855 { 2856 struct thread *td1; 2857 struct rtprio rtp; 2858 struct pgrp *pg; 2859 struct proc *p; 2860 int error; 2861 2862 if ((error = linux_ioprio2rtprio(args->ioprio, &rtp)) != 0) 2863 return (error); 2864 /* Attempts to set high priorities (REALTIME) require su privileges. */ 2865 if (RTP_PRIO_BASE(rtp.type) == RTP_PRIO_REALTIME && 2866 (error = priv_check(td, PRIV_SCHED_RTPRIO)) != 0) 2867 return (error); 2868 2869 p = NULL; 2870 td1 = NULL; 2871 switch (args->which) { 2872 case LINUX_IOPRIO_WHO_PROCESS: 2873 if (args->who == 0) { 2874 td1 = td; 2875 p = td1->td_proc; 2876 PROC_LOCK(p); 2877 } else if (args->who > PID_MAX) { 2878 td1 = linux_tdfind(td, args->who, -1); 2879 if (td1 != NULL) 2880 p = td1->td_proc; 2881 } else 2882 p = pfind(args->who); 2883 if (p == NULL) 2884 return (ESRCH); 2885 if ((error = p_cansched(td, p))) { 2886 PROC_UNLOCK(p); 2887 break; 2888 } 2889 if (td1 != NULL) { 2890 error = rtp_to_pri(&rtp, td1); 2891 } else { 2892 FOREACH_THREAD_IN_PROC(p, td1) { 2893 if ((error = rtp_to_pri(&rtp, td1)) != 0) 2894 break; 2895 } 2896 } 2897 PROC_UNLOCK(p); 2898 break; 2899 case LINUX_IOPRIO_WHO_PGRP: 2900 sx_slock(&proctree_lock); 2901 if (args->who == 0) { 2902 pg = td->td_proc->p_pgrp; 2903 PGRP_LOCK(pg); 2904 } else { 2905 pg = pgfind(args->who); 2906 if (pg == NULL) { 2907 sx_sunlock(&proctree_lock); 2908 error = ESRCH; 2909 break; 2910 } 2911 } 2912 sx_sunlock(&proctree_lock); 2913 LIST_FOREACH(p, &pg->pg_members, p_pglist) { 2914 PROC_LOCK(p); 2915 if (p->p_state == PRS_NORMAL && 2916 p_cansched(td, p) == 0) { 2917 FOREACH_THREAD_IN_PROC(p, td1) { 2918 if ((error = rtp_to_pri(&rtp, td1)) != 0) 2919 break; 2920 } 2921 } 2922 PROC_UNLOCK(p); 2923 if (error != 0) 2924 break; 2925 } 2926 PGRP_UNLOCK(pg); 2927 break; 2928 case LINUX_IOPRIO_WHO_USER: 2929 if (args->who == 0) 2930 args->who = td->td_ucred->cr_uid; 2931 sx_slock(&allproc_lock); 2932 FOREACH_PROC_IN_SYSTEM(p) { 2933 PROC_LOCK(p); 2934 if (p->p_state == PRS_NORMAL && 2935 p->p_ucred->cr_uid == args->who && 2936 p_cansched(td, p) == 0) { 2937 FOREACH_THREAD_IN_PROC(p, td1) { 2938 if ((error = rtp_to_pri(&rtp, td1)) != 0) 2939 break; 2940 } 2941 } 2942 PROC_UNLOCK(p); 2943 if (error != 0) 2944 break; 2945 } 2946 sx_sunlock(&allproc_lock); 2947 break; 2948 default: 2949 error = EINVAL; 2950 break; 2951 } 2952 return (error); 2953 } 2954 2955 /* The only flag is O_NONBLOCK */ 2956 #define B2L_MQ_FLAGS(bflags) ((bflags) != 0 ? LINUX_O_NONBLOCK : 0) 2957 #define L2B_MQ_FLAGS(lflags) ((lflags) != 0 ? O_NONBLOCK : 0) 2958 2959 int 2960 linux_mq_open(struct thread *td, struct linux_mq_open_args *args) 2961 { 2962 struct mq_attr attr; 2963 int error, flags; 2964 2965 flags = linux_common_openflags(args->oflag); 2966 if ((flags & O_ACCMODE) == O_ACCMODE || (flags & O_EXEC) != 0) 2967 return (EINVAL); 2968 flags = FFLAGS(flags); 2969 if ((flags & O_CREAT) != 0 && args->attr != NULL) { 2970 error = copyin(args->attr, &attr, sizeof(attr)); 2971 if (error != 0) 2972 return (error); 2973 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags); 2974 } 2975 2976 return (kern_kmq_open(td, args->name, flags, args->mode, 2977 args->attr != NULL ? &attr : NULL)); 2978 } 2979 2980 int 2981 linux_mq_unlink(struct thread *td, struct linux_mq_unlink_args *args) 2982 { 2983 struct kmq_unlink_args bsd_args = { 2984 .path = PTRIN(args->name) 2985 }; 2986 2987 return (sys_kmq_unlink(td, &bsd_args)); 2988 } 2989 2990 int 2991 linux_mq_timedsend(struct thread *td, struct linux_mq_timedsend_args *args) 2992 { 2993 struct timespec ts, *abs_timeout; 2994 int error; 2995 2996 if (args->abs_timeout == NULL) 2997 abs_timeout = NULL; 2998 else { 2999 error = linux_get_timespec(&ts, args->abs_timeout); 3000 if (error != 0) 3001 return (error); 3002 abs_timeout = &ts; 3003 } 3004 3005 return (kern_kmq_timedsend(td, args->mqd, PTRIN(args->msg_ptr), 3006 args->msg_len, args->msg_prio, abs_timeout)); 3007 } 3008 3009 int 3010 linux_mq_timedreceive(struct thread *td, struct linux_mq_timedreceive_args *args) 3011 { 3012 struct timespec ts, *abs_timeout; 3013 int error; 3014 3015 if (args->abs_timeout == NULL) 3016 abs_timeout = NULL; 3017 else { 3018 error = linux_get_timespec(&ts, args->abs_timeout); 3019 if (error != 0) 3020 return (error); 3021 abs_timeout = &ts; 3022 } 3023 3024 return (kern_kmq_timedreceive(td, args->mqd, PTRIN(args->msg_ptr), 3025 args->msg_len, args->msg_prio, abs_timeout)); 3026 } 3027 3028 int 3029 linux_mq_notify(struct thread *td, struct linux_mq_notify_args *args) 3030 { 3031 struct sigevent ev, *evp; 3032 struct l_sigevent l_ev; 3033 int error; 3034 3035 if (args->sevp == NULL) 3036 evp = NULL; 3037 else { 3038 error = copyin(args->sevp, &l_ev, sizeof(l_ev)); 3039 if (error != 0) 3040 return (error); 3041 error = linux_convert_l_sigevent(&l_ev, &ev); 3042 if (error != 0) 3043 return (error); 3044 evp = &ev; 3045 } 3046 3047 return (kern_kmq_notify(td, args->mqd, evp)); 3048 } 3049 3050 int 3051 linux_mq_getsetattr(struct thread *td, struct linux_mq_getsetattr_args *args) 3052 { 3053 struct mq_attr attr, oattr; 3054 int error; 3055 3056 if (args->attr != NULL) { 3057 error = copyin(args->attr, &attr, sizeof(attr)); 3058 if (error != 0) 3059 return (error); 3060 attr.mq_flags = L2B_MQ_FLAGS(attr.mq_flags); 3061 } 3062 3063 error = kern_kmq_setattr(td, args->mqd, args->attr != NULL ? &attr : NULL, 3064 &oattr); 3065 if (error == 0 && args->oattr != NULL) { 3066 oattr.mq_flags = B2L_MQ_FLAGS(oattr.mq_flags); 3067 bzero(oattr.__reserved, sizeof(oattr.__reserved)); 3068 error = copyout(&oattr, args->oattr, sizeof(oattr)); 3069 } 3070 3071 return (error); 3072 } 3073 3074 MODULE_DEPEND(linux, mqueuefs, 1, 1, 1); 3075