1 /* $NetBSD: linux_signal.c,v 1.80 2018/01/07 21:14:38 christos 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.80 2018/01/07 21:14:38 christos 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 bsa->sa_handler = lsa->linux_sa_handler; 278 linux_old_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask); 279 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags); 280 } 281 282 void 283 native_to_linux_old_sigaction(struct linux_old_sigaction *lsa, const struct sigaction *bsa) 284 { 285 lsa->linux_sa_handler = bsa->sa_handler; 286 native_to_linux_old_sigset(&lsa->linux_sa_mask, &bsa->sa_mask); 287 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags); 288 #ifndef __alpha__ 289 lsa->linux_sa_restorer = NULL; 290 #endif 291 } 292 293 /* ...and the new sigaction conversion funcs. */ 294 void 295 linux_to_native_sigaction(struct sigaction *bsa, const struct linux_sigaction *lsa) 296 { 297 bsa->sa_handler = lsa->linux_sa_handler; 298 linux_to_native_sigset(&bsa->sa_mask, &lsa->linux_sa_mask); 299 bsa->sa_flags = linux_to_native_sigflags(lsa->linux_sa_flags); 300 } 301 302 void 303 native_to_linux_sigaction(struct linux_sigaction *lsa, const struct sigaction *bsa) 304 { 305 lsa->linux_sa_handler = bsa->sa_handler; 306 native_to_linux_sigset(&lsa->linux_sa_mask, &bsa->sa_mask); 307 lsa->linux_sa_flags = native_to_linux_sigflags(bsa->sa_flags); 308 #ifndef __alpha__ 309 lsa->linux_sa_restorer = NULL; 310 #endif 311 } 312 313 /* ----------------------------------------------------------------------- */ 314 315 /* 316 * The Linux sigaction() system call. Do the usual conversions, 317 * and just call sigaction(). Some flags and values are silently 318 * ignored (see above). 319 */ 320 int 321 linux_sys_rt_sigaction(struct lwp *l, const struct linux_sys_rt_sigaction_args *uap, register_t *retval) 322 { 323 /* { 324 syscallarg(int) signum; 325 syscallarg(const struct linux_sigaction *) nsa; 326 syscallarg(struct linux_sigaction *) osa; 327 syscallarg(size_t) sigsetsize; 328 } */ 329 struct linux_sigaction nlsa, olsa; 330 struct sigaction nbsa, obsa; 331 int error, sig; 332 void *tramp = NULL; 333 int vers = 0; 334 #ifdef LINUX_SA_RESTORER 335 struct sigacts *ps = l->l_proc->p_sigacts; 336 #endif 337 338 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 339 return EINVAL; 340 341 if (SCARG(uap, nsa)) { 342 error = copyin(SCARG(uap, nsa), &nlsa, sizeof(nlsa)); 343 if (error) 344 return error; 345 linux_to_native_sigaction(&nbsa, &nlsa); 346 } 347 348 sig = SCARG(uap, signum); 349 /* 350 * XXX: Linux has 33 realtime signals, the go binary wants to 351 * reset all of them; nothing else uses the last RT signal, so for 352 * now ignore it. 353 */ 354 if (sig == LINUX__NSIG) { 355 uprintf("%s: setting signal %d ignored\n", __func__, sig); 356 sig--; /* back to 63 which is ignored */ 357 } 358 if (sig < 0 || sig >= LINUX__NSIG) 359 return EINVAL; 360 if (sig > 0 && !linux_to_native_signo[sig]) { 361 /* Pretend that we did something useful for unknown signals. */ 362 obsa.sa_handler = SIG_IGN; 363 sigemptyset(&obsa.sa_mask); 364 obsa.sa_flags = 0; 365 } else { 366 #ifdef LINUX_SA_RESTORER 367 if (SCARG(uap, nsa) && 368 (nlsa.linux_sa_flags & LINUX_SA_RESTORER) && 369 (tramp = nlsa.linux_sa_restorer) != NULL) 370 vers = 2; 371 #endif 372 373 error = sigaction1(l, linux_to_native_signo[sig], 374 SCARG(uap, nsa) ? &nbsa : NULL, 375 SCARG(uap, osa) ? &obsa : NULL, 376 tramp, vers); 377 if (error) 378 return error; 379 } 380 if (SCARG(uap, osa)) { 381 native_to_linux_sigaction(&olsa, &obsa); 382 383 #ifdef LINUX_SA_RESTORER 384 if (ps->sa_sigdesc[sig].sd_vers != 0) { 385 olsa.linux_sa_restorer = ps->sa_sigdesc[sig].sd_tramp; 386 olsa.linux_sa_flags |= LINUX_SA_RESTORER; 387 } 388 #endif 389 390 error = copyout(&olsa, SCARG(uap, osa), sizeof(olsa)); 391 if (error) 392 return error; 393 } 394 return 0; 395 } 396 397 int 398 linux_sigprocmask1(struct lwp *l, int how, const linux_old_sigset_t *set, linux_old_sigset_t *oset) 399 { 400 struct proc *p = l->l_proc; 401 linux_old_sigset_t nlss, olss; 402 sigset_t nbss, obss; 403 int error; 404 405 switch (how) { 406 case LINUX_SIG_BLOCK: 407 how = SIG_BLOCK; 408 break; 409 case LINUX_SIG_UNBLOCK: 410 how = SIG_UNBLOCK; 411 break; 412 case LINUX_SIG_SETMASK: 413 how = SIG_SETMASK; 414 break; 415 default: 416 return EINVAL; 417 } 418 419 if (set) { 420 error = copyin(set, &nlss, sizeof(nlss)); 421 if (error) 422 return error; 423 linux_old_to_native_sigset(&nbss, &nlss); 424 } 425 mutex_enter(p->p_lock); 426 error = sigprocmask1(l, how, 427 set ? &nbss : NULL, oset ? &obss : NULL); 428 mutex_exit(p->p_lock); 429 if (error) 430 return error; 431 if (oset) { 432 native_to_linux_old_sigset(&olss, &obss); 433 error = copyout(&olss, oset, sizeof(olss)); 434 if (error) 435 return error; 436 } 437 return error; 438 } 439 440 int 441 linux_sys_rt_sigprocmask(struct lwp *l, const struct linux_sys_rt_sigprocmask_args *uap, register_t *retval) 442 { 443 /* { 444 syscallarg(int) how; 445 syscallarg(const linux_sigset_t *) set; 446 syscallarg(linux_sigset_t *) oset; 447 syscallarg(size_t) sigsetsize; 448 } */ 449 linux_sigset_t nlss, olss, *oset; 450 const linux_sigset_t *set; 451 struct proc *p = l->l_proc; 452 sigset_t nbss, obss; 453 int error, how; 454 455 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 456 return EINVAL; 457 458 switch (SCARG(uap, how)) { 459 case LINUX_SIG_BLOCK: 460 how = SIG_BLOCK; 461 break; 462 case LINUX_SIG_UNBLOCK: 463 how = SIG_UNBLOCK; 464 break; 465 case LINUX_SIG_SETMASK: 466 how = SIG_SETMASK; 467 break; 468 default: 469 return EINVAL; 470 } 471 472 set = SCARG(uap, set); 473 oset = SCARG(uap, oset); 474 475 if (set) { 476 error = copyin(set, &nlss, sizeof(nlss)); 477 if (error) 478 return error; 479 linux_to_native_sigset(&nbss, &nlss); 480 } 481 mutex_enter(p->p_lock); 482 error = sigprocmask1(l, how, 483 set ? &nbss : NULL, oset ? &obss : NULL); 484 mutex_exit(p->p_lock); 485 if (!error && oset) { 486 native_to_linux_sigset(&olss, &obss); 487 error = copyout(&olss, oset, sizeof(olss)); 488 } 489 return error; 490 } 491 492 int 493 linux_sys_rt_sigpending(struct lwp *l, const struct linux_sys_rt_sigpending_args *uap, register_t *retval) 494 { 495 /* { 496 syscallarg(linux_sigset_t *) set; 497 syscallarg(size_t) sigsetsize; 498 } */ 499 sigset_t bss; 500 linux_sigset_t lss; 501 502 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 503 return EINVAL; 504 505 sigpending1(l, &bss); 506 native_to_linux_sigset(&lss, &bss); 507 return copyout(&lss, SCARG(uap, set), sizeof(lss)); 508 } 509 510 #ifndef __amd64__ 511 int 512 linux_sys_sigpending(struct lwp *l, const struct linux_sys_sigpending_args *uap, register_t *retval) 513 { 514 /* { 515 syscallarg(linux_old_sigset_t *) mask; 516 } */ 517 sigset_t bss; 518 linux_old_sigset_t lss; 519 520 sigpending1(l, &bss); 521 native_to_linux_old_sigset(&lss, &bss); 522 return copyout(&lss, SCARG(uap, set), sizeof(lss)); 523 } 524 525 int 526 linux_sys_sigsuspend(struct lwp *l, const struct linux_sys_sigsuspend_args *uap, register_t *retval) 527 { 528 /* { 529 syscallarg(void *) restart; 530 syscallarg(int) oldmask; 531 syscallarg(int) mask; 532 } */ 533 linux_old_sigset_t lss; 534 sigset_t bss; 535 536 lss = SCARG(uap, mask); 537 linux_old_to_native_sigset(&bss, &lss); 538 return sigsuspend1(l, &bss); 539 } 540 #endif /* __amd64__ */ 541 542 int 543 linux_sys_rt_sigsuspend(struct lwp *l, const struct linux_sys_rt_sigsuspend_args *uap, register_t *retval) 544 { 545 /* { 546 syscallarg(linux_sigset_t *) unewset; 547 syscallarg(size_t) sigsetsize; 548 } */ 549 linux_sigset_t lss; 550 sigset_t bss; 551 int error; 552 553 if (SCARG(uap, sigsetsize) != sizeof(linux_sigset_t)) 554 return EINVAL; 555 556 error = copyin(SCARG(uap, unewset), &lss, sizeof(linux_sigset_t)); 557 if (error) 558 return error; 559 560 linux_to_native_sigset(&bss, &lss); 561 562 return sigsuspend1(l, &bss); 563 } 564 565 static int 566 fetchss(const void *u, void *s, size_t len) 567 { 568 int error; 569 linux_sigset_t lss; 570 571 if ((error = copyin(u, &lss, sizeof(lss))) != 0) 572 return error; 573 574 linux_to_native_sigset(s, &lss); 575 return 0; 576 } 577 578 static int 579 fetchts(const void *u, void *s, size_t len) 580 { 581 int error; 582 struct linux_timespec lts; 583 584 if ((error = copyin(u, <s, sizeof(lts))) != 0) 585 return error; 586 587 linux_to_native_timespec(s, <s); 588 return 0; 589 } 590 591 static int 592 fakestorets(const void *u, void *s, size_t len) 593 { 594 /* Do nothing, sigtimedwait does not alter timeout like ours */ 595 return 0; 596 } 597 598 static int 599 storeinfo(const void *s, void *u, size_t len) 600 { 601 struct linux_siginfo lsi; 602 603 native_to_linux_siginfo(&lsi, &((const siginfo_t *)s)->_info); 604 return copyout(&lsi, u, sizeof(lsi)); 605 } 606 607 int 608 linux_sys_rt_sigtimedwait(struct lwp *l, 609 const struct linux_sys_rt_sigtimedwait_args *uap, register_t *retval) 610 { 611 /* { 612 syscallarg(const linux_sigset_t *) set; 613 syscallarg(linux_siginfo_t *) info); 614 syscallarg(const struct linux_timespec *) timeout; 615 } */ 616 617 return sigtimedwait1(l, (const struct sys_____sigtimedwait50_args *)uap, 618 retval, fetchss, storeinfo, fetchts, fakestorets); 619 } 620 621 /* 622 * Once more: only a signal conversion is needed. 623 * Note: also used as sys_rt_queueinfo. The info field is ignored. 624 */ 625 int 626 linux_sys_rt_queueinfo(struct lwp *l, const struct linux_sys_rt_queueinfo_args *uap, register_t *retval) 627 { 628 /* 629 syscallarg(int) pid; 630 syscallarg(int) signum; 631 syscallarg(linix_siginfo_t *) uinfo; 632 */ 633 int error; 634 linux_siginfo_t info; 635 636 error = copyin(SCARG(uap, uinfo), &info, sizeof(info)); 637 if (error) 638 return error; 639 if (info.lsi_code >= 0) 640 return EPERM; 641 642 /* XXX To really implement this we need to */ 643 /* XXX keep a list of queued signals somewhere. */ 644 return linux_sys_kill(l, (const void *)uap, retval); 645 } 646 647 int 648 linux_sys_kill(struct lwp *l, const struct linux_sys_kill_args *uap, register_t *retval) 649 { 650 /* { 651 syscallarg(int) pid; 652 syscallarg(int) signum; 653 } */ 654 655 struct sys_kill_args ka; 656 int sig; 657 658 SCARG(&ka, pid) = SCARG(uap, pid); 659 sig = SCARG(uap, signum); 660 if (sig < 0 || sig >= LINUX__NSIG) 661 return EINVAL; 662 SCARG(&ka, signum) = linux_to_native_signo[sig]; 663 return sys_kill(l, &ka, retval); 664 } 665 666 #ifdef LINUX_SS_ONSTACK 667 static void linux_to_native_sigaltstack(struct sigaltstack *, 668 const struct linux_sigaltstack *); 669 670 static void 671 linux_to_native_sigaltstack(struct sigaltstack *bss, const struct linux_sigaltstack *lss) 672 { 673 bss->ss_sp = lss->ss_sp; 674 bss->ss_size = lss->ss_size; 675 if (lss->ss_flags & LINUX_SS_ONSTACK) 676 bss->ss_flags = SS_ONSTACK; 677 else if (lss->ss_flags & LINUX_SS_DISABLE) 678 bss->ss_flags = SS_DISABLE; 679 else 680 bss->ss_flags = 0; 681 } 682 683 void 684 native_to_linux_sigaltstack(struct linux_sigaltstack *lss, const struct sigaltstack *bss) 685 { 686 lss->ss_sp = bss->ss_sp; 687 lss->ss_size = bss->ss_size; 688 if (bss->ss_flags & SS_ONSTACK) 689 lss->ss_flags = LINUX_SS_ONSTACK; 690 else if (bss->ss_flags & SS_DISABLE) 691 lss->ss_flags = LINUX_SS_DISABLE; 692 else 693 lss->ss_flags = 0; 694 } 695 696 int 697 linux_sys_sigaltstack(struct lwp *l, const struct linux_sys_sigaltstack_args *uap, register_t *retval) 698 { 699 /* { 700 syscallarg(const struct linux_sigaltstack *) ss; 701 syscallarg(struct linux_sigaltstack *) oss; 702 } */ 703 struct linux_sigaltstack ss; 704 struct sigaltstack nss; 705 struct proc *p = l->l_proc; 706 int error = 0; 707 708 if (SCARG(uap, oss)) { 709 native_to_linux_sigaltstack(&ss, &l->l_sigstk); 710 if ((error = copyout(&ss, SCARG(uap, oss), sizeof(ss))) != 0) 711 return error; 712 } 713 714 if (SCARG(uap, ss) != NULL) { 715 if ((error = copyin(SCARG(uap, ss), &ss, sizeof(ss))) != 0) 716 return error; 717 linux_to_native_sigaltstack(&nss, &ss); 718 719 mutex_enter(p->p_lock); 720 721 if (nss.ss_flags & ~SS_ALLBITS) 722 error = EINVAL; 723 else if (nss.ss_flags & SS_DISABLE) { 724 if (l->l_sigstk.ss_flags & SS_ONSTACK) 725 error = EINVAL; 726 } else if (nss.ss_size < LINUX_MINSIGSTKSZ) 727 error = ENOMEM; 728 729 if (error == 0) 730 l->l_sigstk = nss; 731 732 mutex_exit(p->p_lock); 733 } 734 735 return error; 736 } 737 #endif /* LINUX_SS_ONSTACK */ 738 739 static int 740 linux_do_tkill(struct lwp *l, int tgid, int tid, int signum) 741 { 742 struct proc *p; 743 struct lwp *t; 744 ksiginfo_t ksi; 745 int error; 746 747 if (signum < 0 || signum >= LINUX__NSIG) 748 return EINVAL; 749 signum = linux_to_native_signo[signum]; 750 751 if (tgid == -1) { 752 tgid = tid; 753 } 754 755 KSI_INIT(&ksi); 756 ksi.ksi_signo = signum; 757 ksi.ksi_code = SI_LWP; 758 ksi.ksi_pid = l->l_proc->p_pid; 759 ksi.ksi_uid = kauth_cred_geteuid(l->l_cred); 760 ksi.ksi_lid = tid; 761 762 mutex_enter(proc_lock); 763 p = proc_find(tgid); 764 if (p == NULL) { 765 mutex_exit(proc_lock); 766 return ESRCH; 767 } 768 mutex_enter(p->p_lock); 769 error = kauth_authorize_process(l->l_cred, 770 KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum), NULL, NULL); 771 if ((t = lwp_find(p, ksi.ksi_lid)) == NULL) 772 error = ESRCH; 773 else if (signum != 0) 774 kpsignal2(p, &ksi); 775 mutex_exit(p->p_lock); 776 mutex_exit(proc_lock); 777 778 return error; 779 } 780 781 int 782 linux_sys_tkill(struct lwp *l, const struct linux_sys_tkill_args *uap, register_t *retval) 783 { 784 /* { 785 syscallarg(int) tid; 786 syscallarg(int) sig; 787 } */ 788 789 if (SCARG(uap, tid) <= 0) 790 return EINVAL; 791 792 return linux_do_tkill(l, -1, SCARG(uap, tid), SCARG(uap, sig)); 793 } 794 795 int 796 linux_sys_tgkill(struct lwp *l, const struct linux_sys_tgkill_args *uap, register_t *retval) 797 { 798 /* { 799 syscallarg(int) tgid; 800 syscallarg(int) tid; 801 syscallarg(int) sig; 802 } */ 803 804 if (SCARG(uap, tid) <= 0 || SCARG(uap, tgid) < -1) 805 return EINVAL; 806 807 return linux_do_tkill(l, SCARG(uap, tgid), SCARG(uap, tid), SCARG(uap, sig)); 808 } 809 810 int 811 native_to_linux_si_code(int code) 812 { 813 int si_codes[] = { 814 LINUX_SI_USER, LINUX_SI_QUEUE, LINUX_SI_TIMER, LINUX_SI_ASYNCIO, 815 LINUX_SI_MESGQ, LINUX_SI_TKILL /* SI_LWP */ 816 }; 817 818 if (code <= 0 && -code < __arraycount(si_codes)) 819 return si_codes[-code]; 820 821 return code; 822 } 823 824 int 825 native_to_linux_si_status(int code, int status) 826 { 827 int sts; 828 829 switch (code) { 830 case CLD_CONTINUED: 831 sts = LINUX_SIGCONT; 832 break; 833 case CLD_EXITED: 834 sts = WEXITSTATUS(status); 835 break; 836 case CLD_STOPPED: 837 case CLD_TRAPPED: 838 case CLD_DUMPED: 839 case CLD_KILLED: 840 default: 841 sts = native_to_linux_signo[WTERMSIG(status)]; 842 break; 843 } 844 845 return sts; 846 } 847