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