1 /* $NetBSD: linux_signal.c,v 1.84 2021/09/07 11:43:04 riastradh Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Frank van der Linden and Eric Haszlakiewicz. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * heavily from: svr4_signal.c,v 1.7 1995/01/09 01:04:21 christos Exp 33 */ 34 35 /* 36 * Functions in multiarch: 37 * linux_sys_signal : linux_sig_notalpha.c 38 * linux_sys_siggetmask : linux_sig_notalpha.c 39 * linux_sys_sigsetmask : linux_sig_notalpha.c 40 * linux_sys_pause : linux_sig_notalpha.c 41 * linux_sys_sigaction : linux_sigaction.c 42 * 43 */ 44 45 /* 46 * Unimplemented: 47 * linux_sys_rt_sigtimedwait : sigsuspend w/timeout. 48 */ 49 50 #include <sys/cdefs.h> 51 __KERNEL_RCSID(0, "$NetBSD: linux_signal.c,v 1.84 2021/09/07 11:43:04 riastradh Exp $"); 52 53 #define COMPAT_LINUX 1 54 55 #include <sys/param.h> 56 #include <sys/systm.h> 57 #include <sys/namei.h> 58 #include <sys/proc.h> 59 #include <sys/filedesc.h> 60 #include <sys/ioctl.h> 61 #include <sys/mount.h> 62 #include <sys/kernel.h> 63 #include <sys/signal.h> 64 #include <sys/signalvar.h> 65 #include <sys/wait.h> 66 67 #include <sys/syscallargs.h> 68 69 #include <compat/linux/common/linux_types.h> 70 #include <compat/linux/common/linux_signal.h> 71 #include <compat/linux/common/linux_emuldata.h> 72 #include <compat/linux/common/linux_siginfo.h> 73 #include <compat/linux/common/linux_sigevent.h> 74 #include <compat/linux/common/linux_util.h> 75 #include <compat/linux/common/linux_ipc.h> 76 #include <compat/linux/common/linux_sem.h> 77 #include <compat/linux/common/linux_errno.h> 78 #include <compat/linux/common/linux_sched.h> 79 80 #include <compat/linux/linux_syscallargs.h> 81 82 /* Locally used defines (in bsd<->linux conversion functions): */ 83 #define linux_sigemptyset(s) memset((s), 0, sizeof(*(s))) 84 #define linux_sigismember(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \ 85 & (1L << ((n) - 1) % LINUX__NSIG_BPW)) 86 #define linux_sigaddset(s, n) ((s)->sig[((n) - 1) / LINUX__NSIG_BPW] \ 87 |= (1L << ((n) - 1) % LINUX__NSIG_BPW)) 88 89 #ifdef DEBUG_LINUX 90 #define DPRINTF(a) uprintf a 91 #else 92 #define DPRINTF(a) 93 #endif 94 95 extern const int native_to_linux_signo[]; 96 extern const int linux_to_native_signo[]; 97 98 /* 99 * Convert between Linux and BSD signal sets. 100 */ 101 #if LINUX__NSIG_WORDS > 1 102 void 103 linux_old_extra_to_native_sigset(sigset_t *bss, const linux_old_sigset_t *lss, const unsigned long *extra) 104 { 105 linux_sigset_t lsnew; 106 107 /* convert old sigset to new sigset */ 108 linux_sigemptyset(&lsnew); 109 lsnew.sig[0] = *lss; 110 if (extra) 111 memcpy(&lsnew.sig[1], extra, 112 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t)); 113 114 linux_to_native_sigset(bss, &lsnew); 115 } 116 117 void 118 native_to_linux_old_extra_sigset(linux_old_sigset_t *lss, unsigned long *extra, const sigset_t *bss) 119 { 120 linux_sigset_t lsnew; 121 122 native_to_linux_sigset(&lsnew, bss); 123 124 /* convert new sigset to old sigset */ 125 *lss = lsnew.sig[0]; 126 if (extra) 127 memcpy(extra, &lsnew.sig[1], 128 sizeof(linux_sigset_t) - sizeof(linux_old_sigset_t)); 129 } 130 #endif /* LINUX__NSIG_WORDS > 1 */ 131 132 void 133 linux_to_native_sigset(sigset_t *bss, const linux_sigset_t *lss) 134 { 135 int i, newsig; 136 137 sigemptyset(bss); 138 for (i = 1; i < LINUX__NSIG; i++) { 139 if (linux_sigismember(lss, i)) { 140 newsig = linux_to_native_signo[i]; 141 if (newsig) 142 sigaddset(bss, newsig); 143 } 144 } 145 } 146 147 void 148 native_to_linux_sigset(linux_sigset_t *lss, const sigset_t *bss) 149 { 150 int i, newsig; 151 152 linux_sigemptyset(lss); 153 for (i = 1; i < NSIG; i++) { 154 if (sigismember(bss, i)) { 155 newsig = native_to_linux_signo[i]; 156 if (newsig) 157 linux_sigaddset(lss, newsig); 158 } 159 } 160 } 161 162 void 163 native_to_linux_siginfo(linux_siginfo_t *lsi, const struct _ksiginfo *ksi) 164 { 165 memset(lsi, 0, sizeof(*lsi)); 166 167 lsi->lsi_signo = native_to_linux_signo[ksi->_signo]; 168 lsi->lsi_errno = native_to_linux_errno[ksi->_errno]; 169 lsi->lsi_code = native_to_linux_si_code(ksi->_code); 170 171 switch (ksi->_code) { 172 case SI_NOINFO: 173 break; 174 175 case SI_USER: 176 lsi->lsi_pid = ksi->_reason._rt._pid; 177 lsi->lsi_uid = ksi->_reason._rt._uid; 178 if (lsi->lsi_signo == LINUX_SIGALRM || 179 lsi->lsi_signo >= LINUX_SIGRTMIN) 180 lsi->lsi_value.sival_ptr = 181 ksi->_reason._rt._value.sival_ptr; 182 break; 183 184 case SI_TIMER: 185 case SI_QUEUE: 186 lsi->lsi_uid = ksi->_reason._rt._uid; 187 lsi->lsi_uid = ksi->_reason._rt._uid; 188 lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr; 189 break; 190 191 case SI_ASYNCIO: 192 case SI_MESGQ: 193 lsi->lsi_value.sival_ptr = ksi->_reason._rt._value.sival_ptr; 194 break; 195 196 default: 197 switch (ksi->_signo) { 198 case SIGCHLD: 199 lsi->lsi_uid = ksi->_reason._child._uid; 200 lsi->lsi_pid = ksi->_reason._child._pid; 201 lsi->lsi_status = native_to_linux_si_status( 202 ksi->_code, ksi->_reason._child._status); 203 lsi->lsi_utime = ksi->_reason._child._utime; 204 lsi->lsi_stime = ksi->_reason._child._stime; 205 break; 206 207 case SIGILL: 208 case SIGFPE: 209 case SIGSEGV: 210 case SIGBUS: 211 case SIGTRAP: 212 lsi->lsi_addr = ksi->_reason._fault._addr; 213 break; 214 215 case SIGIO: 216 lsi->lsi_fd = ksi->_reason._poll._fd; 217 lsi->lsi_band = ksi->_reason._poll._band; 218 break; 219 default: 220 break; 221 } 222 } 223 } 224 225 unsigned int 226 native_to_linux_sigflags(const int bsf) 227 { 228 unsigned int lsf = 0; 229 if ((bsf & SA_NOCLDSTOP) != 0) 230 lsf |= LINUX_SA_NOCLDSTOP; 231 if ((bsf & SA_NOCLDWAIT) != 0) 232 lsf |= LINUX_SA_NOCLDWAIT; 233 if ((bsf & SA_ONSTACK) != 0) 234 lsf |= LINUX_SA_ONSTACK; 235 if ((bsf & SA_RESTART) != 0) 236 lsf |= LINUX_SA_RESTART; 237 if ((bsf & SA_NODEFER) != 0) 238 lsf |= LINUX_SA_NOMASK; 239 if ((bsf & SA_RESETHAND) != 0) 240 lsf |= LINUX_SA_ONESHOT; 241 if ((bsf & SA_SIGINFO) != 0) 242 lsf |= LINUX_SA_SIGINFO; 243 return lsf; 244 } 245 246 int 247 linux_to_native_sigflags(const unsigned long lsf) 248 { 249 int bsf = 0; 250 if ((lsf & LINUX_SA_NOCLDSTOP) != 0) 251 bsf |= SA_NOCLDSTOP; 252 if ((lsf & LINUX_SA_NOCLDWAIT) != 0) 253 bsf |= SA_NOCLDWAIT; 254 if ((lsf & LINUX_SA_ONSTACK) != 0) 255 bsf |= SA_ONSTACK; 256 if ((lsf & LINUX_SA_RESTART) != 0) 257 bsf |= SA_RESTART; 258 if ((lsf & LINUX_SA_ONESHOT) != 0) 259 bsf |= SA_RESETHAND; 260 if ((lsf & LINUX_SA_NOMASK) != 0) 261 bsf |= SA_NODEFER; 262 if ((lsf & LINUX_SA_SIGINFO) != 0) 263 bsf |= SA_SIGINFO; 264 if ((lsf & ~LINUX_SA_ALLBITS) != 0) { 265 DPRINTF(("linux_old_to_native_sigflags: " 266 "%lx extra bits ignored\n", lsf)); 267 } 268 return bsf; 269 } 270 271 /* 272 * Convert between Linux and BSD sigaction structures. 273 */ 274 void 275 linux_old_to_native_sigaction(struct sigaction *bsa, const struct linux_old_sigaction *lsa) 276 { 277 278 memset(bsa, 0, sizeof(*bsa)); 279 bsa->sa_handler = lsa->linux_sa_handler; 280 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask); 281 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags); 282 } 283 284 void 285 native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa) 286 { 287 288 memset(lsa, 0, sizeof(*lsa)); 289 lsa->linux_sa_handler = bsa->sa_handler; 290 native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask); 291 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags); 292 #ifndef __alpha__ 293 lsa->linux_sa_restorer = NULL; 294 #endif 295 } 296 297 /* ...and the new sigaction conversion funcs. */ 298 void 299 linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa) 300 { 301 302 memset(bsa, 0, sizeof(*bsa)); 303 bsa->sa_handler = lsa->linux_sa_handler; 304 linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask); 305 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags); 306 } 307 308 void 309 native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa) 310 { 311 312 memset(lsa, 0, sizeof(*lsa)); 313 lsa->linux_sa_handler = bsa->sa_handler; 314 native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask); 315 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags); 316 #ifndef __alpha__ 317 lsa->linux_sa_restorer = NULL; 318 #endif 319 } 320 321 /* ----------------------------------------------------------------------- */ 322 323 /* 324 * The Linux sigaction() system call. Do the usual conversions, 325 * and just call sigaction(). Some flags and values are silently 326 * ignored (see above). 327 */ 328 int 329 linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval) 330 { 331 /* { 332 syscallarg(int) signum; 333 syscallarg(const struct linux_sigaction *) nsa; 334 syscallarg(struct linux_sigaction *) osa; 335 syscallarg(size_t) sigsetsize; 336 } */ 337 struct linux_sigaction nlsa, olsa; 338 struct sigaction nbsa, obsa; 339 int error, sig; 340 void *tramp = NULL; 341 int vers = 0; 342 #ifdef LINUX_SA_RESTORER 343 struct sigacts *ps = l->l_proc->p_sigacts; 344 #endif 345 346 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 347 return EINVAL; 348 349 if (SCARG(uap, nsa)) { 350 error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa)); 351 if (error) 352 return error; 353 linux_to_native_sigaction(&nbsa, &nlsa); 354 } 355 356 sig = SCARG(uap, signum); 357 /* 358 * XXX: Linux has 33 realtime signals, the go binary wants to 359 * reset all of them; nothing else uses the last RT signal, so for 360 * now ignore it. 361 */ 362 if (sig == LINUX__NSIG) { 363 uprintf("%s: setting signal %d ignored\n", __func__, sig); 364 sig--; /* back to 63 which is ignored */ 365 } 366 if (sig < 0 || sig >= LINUX__NSIG) 367 return EINVAL; 368 if (sig > 0 && !linux_to_native_signo[sig]) { 369 /* Pretend that we did something useful for unknown signals. */ 370 obsa.sa_handler = SIG_IGN; 371 sigemptyset(&obsa.sa_mask); 372 obsa.sa_flags = 0; 373 } else { 374 #ifdef LINUX_SA_RESTORER 375 if (SCARG(uap, nsa) && 376 (nlsa.linux_sa_flags & LINUX_SA_RESTORER) && 377 (tramp = nlsa.linux_sa_restorer) != NULL) 378 vers = 2; 379 #endif 380 381 error = sigaction1(l, linux_to_native_signo[sig], 382 SCARG(uap, nsa) ? &nbsa : NULL, 383 SCARG(uap, osa) ? &obsa : NULL, 384 tramp, vers); 385 if (error) 386 return error; 387 } 388 if (SCARG(uap, osa)) { 389 native_to_linux_sigaction(&olsa, &obsa); 390 391 #ifdef LINUX_SA_RESTORER 392 if (ps->sa_sigdesc[sig].sd_vers != 0) { 393 olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp; 394 olsa.linux_sa_flags |= LINUX_SA_RESTORER; 395 } 396 #endif 397 398 error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa)); 399 if (error) 400 return error; 401 } 402 return 0; 403 } 404 405 int 406 linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset) 407 { 408 struct proc *p = l->l_proc; 409 linux_old_sigset_t nlss, olss; 410 sigset_t nbss, obss; 411 int error; 412 413 switch (how) { 414 case LINUX_SIG_BLOCK: 415 how = SIG_BLOCK; 416 break; 417 case LINUX_SIG_UNBLOCK: 418 how = SIG_UNBLOCK; 419 break; 420 case LINUX_SIG_SETMASK: 421 how = SIG_SETMASK; 422 break; 423 default: 424 return EINVAL; 425 } 426 427 if (set) { 428 error = copyin(set, &nlss, sizeof(nlss)); 429 if (error) 430 return error; 431 linux_old_to_native_sigset(&nbss, &nlss); 432 } 433 mutex_enter(p->p_lock); 434 error = sigprocmask1(l, how, 435 set ? &nbss : NULL, oset ? &obss : NULL); 436 mutex_exit(p->p_lock); 437 if (error) 438 return error; 439 if (oset) { 440 native_to_linux_old_sigset(&olss, &obss); 441 error = copyout(&olss, oset, sizeof(olss)); 442 if (error) 443 return error; 444 } 445 return error; 446 } 447 448 int 449 linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval) 450 { 451 /* { 452 syscallarg(int) how; 453 syscallarg(const linux_sigset_t *) set; 454 syscallarg(linux_sigset_t *) oset; 455 syscallarg(size_t) sigsetsize; 456 } */ 457 linux_sigset_t nlss, olss, *oset; 458 const linux_sigset_t *set; 459 struct proc *p = l->l_proc; 460 sigset_t nbss, obss; 461 int error, how; 462 463 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 464 return EINVAL; 465 466 switch (SCARG(uap, how)) { 467 case LINUX_SIG_BLOCK: 468 how = SIG_BLOCK; 469 break; 470 case LINUX_SIG_UNBLOCK: 471 how = SIG_UNBLOCK; 472 break; 473 case LINUX_SIG_SETMASK: 474 how = SIG_SETMASK; 475 break; 476 default: 477 return EINVAL; 478 } 479 480 set = SCARG(uap, set); 481 oset = SCARG(uap, oset); 482 483 if (set) { 484 error = copyin(set, &nlss, sizeof(nlss)); 485 if (error) 486 return error; 487 linux_to_native_sigset(&nbss, &nlss); 488 } 489 mutex_enter(p->p_lock); 490 error = sigprocmask1(l, how, 491 set ? &nbss : NULL, oset ? &obss : NULL); 492 mutex_exit(p->p_lock); 493 if (!error && oset) { 494 native_to_linux_sigset(&olss, &obss); 495 error = copyout(&olss, oset, sizeof(olss)); 496 } 497 return error; 498 } 499 500 int 501 linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval) 502 { 503 /* { 504 syscallarg(linux_sigset_t *) set; 505 syscallarg(size_t) sigsetsize; 506 } */ 507 sigset_t bss; 508 linux_sigset_t lss; 509 510 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 511 return EINVAL; 512 513 sigpending1(l, &bss); 514 native_to_linux_sigset(&lss, &bss); 515 return copyout(&lss, SCARG(uap, set), sizeof(lss)); 516 } 517 518 #ifndef __amd64__ 519 int 520 linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval) 521 { 522 /* { 523 syscallarg(linux_old_sigset_t *) mask; 524 } */ 525 sigset_t bss; 526 linux_old_sigset_t lss; 527 528 sigpending1(l, &bss); 529 native_to_linux_old_sigset(&lss, &bss); 530 return copyout(&lss, SCARG(uap, set), sizeof(lss)); 531 } 532 533 int 534 linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval) 535 { 536 /* { 537 syscallarg(void *) restart; 538 syscallarg(int) oldmask; 539 syscallarg(int) mask; 540 } */ 541 linux_old_sigset_t lss; 542 sigset_t bss; 543 544 lss = SCARG(uap, mask); 545 linux_old_to_native_sigset(&bss, &lss); 546 return sigsuspend1(l, &bss); 547 } 548 #endif /* __amd64__ */ 549 550 int 551 linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval) 552 { 553 /* { 554 syscallarg(linux_sigset_t *) unewset; 555 syscallarg(size_t) sigsetsize; 556 } */ 557 linux_sigset_t lss; 558 sigset_t bss; 559 int error; 560 561 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 562 return EINVAL; 563 564 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t)); 565 if (error) 566 return error; 567 568 linux_to_native_sigset(&bss, &lss); 569 570 return sigsuspend1(l, &bss); 571 } 572 573 static int 574 fetchss(const void *u, void *s, size_t len) 575 { 576 int error; 577 linux_sigset_t lss; 578 579 if ((error = copyin(u, &lss, sizeof(lss))) != 0) 580 return error; 581 582 linux_to_native_sigset(s, &lss); 583 return 0; 584 } 585 586 static int 587 fetchts(const void *u, void *s, size_t len) 588 { 589 int error; 590 struct linux_timespec lts; 591 592 if ((error = copyin(u, <s, sizeof(lts))) != 0) 593 return error; 594 595 linux_to_native_timespec(s, <s); 596 return 0; 597 } 598 599 static int 600 fakestorets(const void *u, void *s, size_t len) 601 { 602 /* Do nothing, sigtimedwait does not alter timeout like ours */ 603 return 0; 604 } 605 606 static int 607 storeinfo(const void *s, void *u, size_t len) 608 { 609 struct linux_siginfo lsi; 610 611 native_to_linux_siginfo(&lsi, &((const siginfo_t *)s)->_info); 612 return copyout(&lsi, u, sizeof(lsi)); 613 } 614 615 int 616 linux_sys_rt_sigtimedwait(struct lwp *l, 617 const struct linux_sys_rt_sigtimedwait_args *uap, register_t *retval) 618 { 619 /* { 620 syscallarg(const linux_sigset_t *) set; 621 syscallarg(linux_siginfo_t *) info); 622 syscallarg(const struct linux_timespec *) timeout; 623 } */ 624 625 return sigtimedwait1(l, (const struct sys_____sigtimedwait50_args *)uap, 626 retval, fetchss, storeinfo, fetchts, fakestorets); 627 } 628 629 /* 630 * Once more: only a signal conversion is needed. 631 * Note: also used as sys_rt_queueinfo. The info field is ignored. 632 */ 633 int 634 linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval) 635 { 636 /* 637 syscallarg(int) pid; 638 syscallarg(int) signum; 639 syscallarg(linix_siginfo_t *) uinfo; 640 */ 641 int error; 642 linux_siginfo_t info; 643 644 error = copyin(SCARG(uap, uinfo), &info, sizeof(info)); 645 if (error) 646 return error; 647 if (info.lsi_code >= 0) 648 return EPERM; 649 650 /* XXX To really implement this we need to */ 651 /* XXX keep a list of queued signals somewhere. */ 652 return linux_sys_kill(l, (const void *)uap, retval); 653 } 654 655 int 656 linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval) 657 { 658 /* { 659 syscallarg(int) pid; 660 syscallarg(int) signum; 661 } */ 662 663 struct sys_kill_args ka; 664 int sig; 665 666 SCARG(&ka, pid) = SCARG(uap, pid); 667 sig = SCARG(uap, signum); 668 if (sig < 0 || sig >= LINUX__NSIG) 669 return EINVAL; 670 SCARG(&ka, signum) = linux_to_native_signo[sig]; 671 return sys_kill(l, &ka, retval); 672 } 673 674 #ifdef LINUX_SS_ONSTACK 675 static void linux_to_native_sigaltstack(struct sigaltstack *, 676 const struct linux_sigaltstack *); 677 678 static void 679 linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss) 680 { 681 bss->ss_sp = lss->ss_sp; 682 bss->ss_size = lss->ss_size; 683 if (lss->ss_flags & LINUX_SS_ONSTACK) 684 bss->ss_flags = SS_ONSTACK; 685 else if (lss->ss_flags & LINUX_SS_DISABLE) 686 bss->ss_flags = SS_DISABLE; 687 else 688 bss->ss_flags = 0; 689 } 690 691 void 692 native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss) 693 { 694 memset(lss, 0, sizeof(*lss)); 695 lss->ss_sp = bss->ss_sp; 696 lss->ss_size = bss->ss_size; 697 if (bss->ss_flags & SS_ONSTACK) 698 lss->ss_flags = LINUX_SS_ONSTACK; 699 else if (bss->ss_flags & SS_DISABLE) 700 lss->ss_flags = LINUX_SS_DISABLE; 701 else 702 lss->ss_flags = 0; 703 } 704 705 int 706 linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval) 707 { 708 /* { 709 syscallarg(const struct linux_sigaltstack *) ss; 710 syscallarg(struct linux_sigaltstack *) oss; 711 } */ 712 struct linux_sigaltstack ss; 713 struct sigaltstack nss; 714 struct proc *p = l->l_proc; 715 int error = 0; 716 717 if (SCARG(uap, oss)) { 718 native_to_linux_sigaltstack(&ss, &l->l_sigstk); 719 if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0) 720 return error; 721 } 722 723 if (SCARG(uap, ss) != NULL) { 724 if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0) 725 return error; 726 linux_to_native_sigaltstack(&nss, &ss); 727 728 mutex_enter(p->p_lock); 729 730 if (nss.ss_flags & ~SS_ALLBITS) 731 error = EINVAL; 732 else if (nss.ss_flags & SS_DISABLE) { 733 if (l->l_sigstk.ss_flags & SS_ONSTACK) 734 error = EINVAL; 735 } else if (nss.ss_size < LINUX_MINSIGSTKSZ) 736 error = ENOMEM; 737 738 if (error == 0) 739 l->l_sigstk = nss; 740 741 mutex_exit(p->p_lock); 742 } 743 744 return error; 745 } 746 #endif /* LINUX_SS_ONSTACK */ 747 748 static int 749 linux_do_tkill(struct lwp *l, int tgid, int tid, int signum) 750 { 751 struct proc *p; 752 struct lwp *t; 753 ksiginfo_t ksi; 754 int error; 755 756 if (signum < 0 || signum >= LINUX__NSIG) 757 return EINVAL; 758 signum = linux_to_native_signo[signum]; 759 760 KSI_INIT(&ksi); 761 ksi.ksi_signo = signum; 762 ksi.ksi_code = SI_LWP; 763 ksi.ksi_pid = l->l_proc->p_pid; 764 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 765 ksi.ksi_lid = tid; 766 767 mutex_enter(&proc_lock); 768 if (tgid != -1) 769 p = proc_find(tgid); 770 else 771 p = proc_find_lwpid(tid); 772 if (p == NULL) { 773 mutex_exit(&proc_lock); 774 return ESRCH; 775 } 776 mutex_enter(p->p_lock); 777 error = kauth_authorize_process(l->l_cred, 778 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL); 779 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL) 780 error = ESRCH; 781 else if (signum != 0) 782 kpsignal2(p, &ksi); 783 mutex_exit(p->p_lock); 784 mutex_exit(&proc_lock); 785 786 return error; 787 } 788 789 int 790 linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval) 791 { 792 /* { 793 syscallarg(int) tid; 794 syscallarg(int) sig; 795 } */ 796 797 if (SCARG(uap, tid) <= 0) 798 return EINVAL; 799 800 return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig)); 801 } 802 803 int 804 linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval) 805 { 806 /* { 807 syscallarg(int) tgid; 808 syscallarg(int) tid; 809 syscallarg(int) sig; 810 } */ 811 812 if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1) 813 return EINVAL; 814 815 return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig)); 816 } 817 818 int 819 native_to_linux_si_code(int code) 820 { 821 int si_codes[] = { 822 LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO, 823 LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */ 824 }; 825 826 if (code <= 0 && -code < __arraycount(si_codes)) 827 return si_codes[-code]; 828 829 return code; 830 } 831 832 int 833 native_to_linux_si_status(int code, int status) 834 { 835 int sts; 836 837 switch (code) { 838 case CLD_CONTINUED: 839 sts = LINUX_SIGCONT; 840 break; 841 case CLD_EXITED: 842 sts = WEXITSTATUS(status); 843 break; 844 case CLD_STOPPED: 845 case CLD_TRAPPED: 846 case CLD_DUMPED: 847 case CLD_KILLED: 848 default: 849 sts = native_to_linux_signo[WTERMSIG(status)]; 850 break; 851 } 852 853 return sts; 854 } 855