1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
|
/*
* Copyright (c) 1996, 1998, 1999, 2000 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Posix signals, or something approximating them. The goal is to get
* the obvious stuff nearly correct, but not go overboard.
*/
#include <threads/pthread_internal.h>
#include <strings.h>
#include <oskit/c/string.h>
#include <oskit/c/malloc.h>
#include "pthread_cond.h"
#include "pthread_signal.h"
/*
* The sigactions array. Protected by its own lock.
*/
static struct sigaction sigactions[NSIG];
static pthread_lock_t sigactions_lock = PTHREAD_LOCK_INITIALIZER;
/*
* There is a global (or "process") set of pending signals.
* kill() and sigqueue() affect the process pending set.
*/
static sigset_t sigpending;
/*
* A queue of all threads waiting in sigwait.
*/
static queue_head_t sigwaiters;
/*
* An array of queues of pending signals posted with sigqueue().
*/
static queue_head_t sigqueued[NSIG];
/*
* We avoid malloc in interrupt handlers by preallocating the queue
* entries for sigqueued above.
*/
static queue_head_t sigqueue_free;
/*
* Lock to protect the global signal stuff.
*/
static pthread_lock_t siglock = PTHREAD_LOCK_INITIALIZER;
/*
* Prototypes.
*/
int pthread_kill_locked(pthread_thread_t *pthread, int signo);
void really_deliver_signal(int sig, siginfo_t *code,struct sigcontext *scp);
void oskit_deliver_async_signal(int sig);
void oskit_deliver_process_signal(int sig);
int
pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
{
pthread_thread_t *pthread = CURPTHREAD();
int err = 0;
/* siglock and pthread siglock are taken from an interrupt handler */
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&pthread->siglock);
if (oset)
*oset = pthread->sigmask;
if (set) {
switch (how) {
case SIG_BLOCK:
pthread->sigmask |= *set;
break;
case SIG_UNBLOCK:
pthread->sigmask &= ~*set;
break;
case SIG_SETMASK:
pthread->sigmask = *set;
break;
default:
err = EINVAL;
}
}
/*
* Look for process pending signals that are unblocked, and deliver.
*/
pthread_lock(&siglock);
while (sigpending & ~pthread->sigmask) {
int sig = ffs(sigpending & ~pthread->sigmask);
/* Call with siglock and thread siglock locked */
oskit_deliver_process_signal(sig);
}
pthread_unlock(&siglock);
/*
* Look for pthread pending signals that are unblocked, and deliver.
*/
while (pthread->sigpending & ~pthread->sigmask) {
int sig = ffs(pthread->sigpending & ~pthread->sigmask);
/* Call with thread siglock locked */
oskit_deliver_async_signal(sig);
}
pthread_unlock(&pthread->siglock);
enable_interrupts();
return err;
}
/*
* This can be called out of an interrupt handler, say from an alarm
* expiration. Therefore, must not take the pthread lock. We use a
* separate lock to protect the signal state.
*/
int
pthread_kill(pthread_t tid, int signo)
{
pthread_thread_t *pthread;
int enabled;
/* Error check? Sure! */
if (!signo)
return 0;
if (signo < 0 || signo >= NSIG)
return EINVAL;
if ((pthread = tidtothread(tid)) == NULL_THREADPTR)
return EINVAL;
/* siglock and pthread siglock are taken from an interrupt handler */
save_interrupt_enable(enabled);
disable_interrupts();
pthread_lock(&pthread->siglock);
/*
* pthread_kill_locked() will take care of unlocking.
*/
pthread_kill_locked(pthread, signo);
/*
* If not in an interrupt, use this opportunity to deliver
* pending unblocked signals to the current thread.
*/
if (! IN_AN_INTERRUPT()) {
pthread = CURPTHREAD();
SIGCHECK(pthread);
}
restore_interrupt_enable(enabled);
return 0;
}
/*
* This does all the work.
*
* Should be called with interrupts disabled, and siglock and pthread
* siglock are held.
* Returns with the locks released (caller must restore interrupts).
*/
int
pthread_kill_locked(pthread_thread_t *pthread, int signo)
{
/*
* Look at the process sigactions. If the "process" is ignoring
* the signal, then the signal is not placed in the pending list.
*/
pthread_lock(&sigactions_lock);
if (sigactions[signo].sa_handler == SIG_IGN) {
pthread_unlock(&pthread->siglock);
pthread_unlock(&sigactions_lock);
return 0;
}
/* Don't need the global siglock anymore. */
pthread_unlock(&sigactions_lock);
/*
* First have to check for sigwaiting, since that overrides sigmask.
*/
pthread_lock(&pthread->waitlock);
if ((pthread->waitflags & THREAD_WS_SIGWAIT) &&
sigismember(&pthread->sigwaiting, signo)) {
sigaddset(&pthread->sigpending, signo);
sigemptyset(&pthread->sigwaiting);
pthread_unlock(&pthread->siglock);
/* pthread waitlock will be released. */
pthread_wakeup_locked(pthread);
return 0;
}
pthread_unlock(&pthread->waitlock);
/*
* Add the signal to list of pending signals for the target thread.
*/
sigaddset(&pthread->sigpending, signo);
/*
* If the signal is currently blocked, then do nothing else.
* It will be noticed when the signal is unblocked.
*/
if (sigismember(&pthread->sigmask, signo)) {
pthread_unlock(&pthread->siglock);
return 0;
}
/*
* Is the current thread sending itself a signal? This is okay, as
* long as its not from within an interrupt handler, say, from an
* alarm expiration. The caller is going to look for pending signals
* to the current thread.
*
* If this *is* from within an interrupt, must wait until later
* since delivering it now is really the wrong way to go. Lets
* schedule a soft interrupt, and get the thread switched out.
* The signal will be delivered when it comes back in.
*/
if (pthread == CURPTHREAD()) {
if (IN_AN_INTERRUPT()) {
softint_request(SOFTINT_ASYNCREQ);
}
pthread_unlock(&pthread->siglock);
return 0;
}
/* Don't need the pthread siglock anymore; waking up the thread */
pthread_unlock(&pthread->siglock);
/*
* Below here, we muck with the waitflags, which are accessed
* from interrupt handlers. Interrupts are already disabled,
* so take the thread waitlock.
*/
pthread_lock(&pthread->waitlock);
/*
* If the thread is in an osenv_sleep(), issue a wakeup.
* The thread will be allowed to return through the sleep, to
* be caught sometime later. This allows driver state to be
* cleaned up before the thread is actually signaled.
*/
if (pthread->waitflags & THREAD_WS_OSENVSLEEP) {
pthread_unlock(&pthread->waitlock);
osenv_wakeup(pthread->sleeprec, OSENV_SLEEP_CANCELED);
return 0;
}
/*
* If the thread is THREAD_WS_SLEEPING, then restart it.
*/
if (pthread->waitflags & THREAD_WS_SLEEPING) {
/* pthread waitlock will be released. */
pthread_wakeup_locked(pthread);
return 0;
}
/*
* If the thread is THREAD_WS_CONDWAIT, then restart it. The wrinkle
* is a race condition between the time the thread is taken off
* the condition queue and the time the thread state is changed.
*/
if (pthread->waitflags & THREAD_WS_CONDWAIT) {
struct pthread_cond_impl *pimpl = pthread->waitcond->impl;
pthread_lock(&(pimpl->lock));
/*
* The thread was still on the Q, so its safe to change
* its state to reflect that it is not longer waiting
* on the condition.
*
* If the thread was not on the Q, we caught the race,
* and do not have to do anything.
*/
if (pthread_remove_fromQ(&(pimpl->waiters), pthread)) {
pthread->waitflags &= ~THREAD_WS_CONDWAIT;
pthread->waitcond = 0;
pthread_unlock(&(pimpl->lock));
pthread_unlock(&pthread->waitlock);
pthread_sched_setrunnable(pthread);
return 0;
}
pthread_unlock(&(pimpl->lock));
pthread_unlock(&pthread->waitlock);
return 0;
}
/*
* IPC Wait.
*/
if (pthread->waitflags & THREAD_WS_IPCWAIT_FLAG) {
/* pthread waitlock will be released. */
pthread_ipc_cancel(pthread);
return 0;
}
/*
* Done with waitflags.
*/
pthread_unlock(&pthread->waitlock);
/*
* Must be running on another CPU. Must wait for it to be noticed.
*/
return 0;
}
/*
* The rest of the Posix signal code. This stuff replaces the corresponding
* code in the Posix library, since I don't see how it can be done unless
* its all in one place.
*/
/*
* sigaction
*/
int
sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
{
if (sig < 0 || sig >= NSIG)
return errno = EINVAL, -1;
assert_preemption_enabled();
/* siglock and pthread siglock are taken from an interrupt handler */
assert_interrupts_enabled();
disable_interrupts();
pthread_lock(&sigactions_lock);
if (oact)
*oact = sigactions[sig];
if (act)
sigactions[sig] = *act;
pthread_unlock(&sigactions_lock);
/*
* If the action for this signal is being set to SIG_IGN or SIG_DFL,
* and that signal is process pending, then clear it.
*/
pthread_lock(&siglock);
if (act && (act->sa_handler == SIG_IGN ||
act->sa_handler == SIG_DFL)) {
while (! queue_empty(&sigqueued[sig])) {
sigqueue_thingie_t *pthingie;
queue_remove_first(&sigqueued[sig], pthingie,
sigqueue_thingie_t *, chain);
queue_enter(&sigqueue_free, pthingie,
sigqueue_thingie_t *, chain);
}
sigdelset(&sigpending, sig);
}
pthread_unlock(&siglock);
enable_interrupts();
return 0;
}
/*
* sigprocmask. In a multithreaded program, this is just pthread_sigmask
*/
int
sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
return pthread_sigmask(how, set, oset);
}
/*
* raise. In a multithreaded program, raise is pthread_kill on itself.
*/
int
raise(int sig)
{
return pthread_kill(pthread_self(), sig);
}
/*
* kill. What does it mean to kill() in a multithreaded program? The POSIX
* spec says that a signal sent to a "process" shall be delivered to only
* one thread. If no thread has that signal unblocked, then the first
* thread to unblock the signal is the lucky winner. Well, that means we
* need to have a global sigpending to record process pending signals.
*/
int
kill(pid_t pid, int signo)
{
pthread_thread_t *pthread;
int enabled, i;
struct sigaction act;
extern pthread_lock_t pthread_create_lock;
/* Error check? Sure! */
if (!signo)
return 0;
/* siglock and pthread siglock are taken from an interrupt handler */
save_interrupt_enable(enabled);
disable_interrupts();
/*
* Look at the process sigactions. If the "process" is ignoring
* the signal, then the signal is not placed in the pending list.
*/
pthread_lock(&sigactions_lock);
act = sigactions[signo];
pthread_unlock(&sigactions_lock);
if (act.sa_handler == SIG_IGN) {
restore_interrupt_enable(enabled);
return 0;
}
/*
* Take and hold the global siglock. This is needed to prevent
* a race with sigwait.
*/
pthread_lock(&siglock);
/*
* Kill does not queue. If the signal is already pending, this
* one is tossed.
*/
if (sigismember(&sigpending, signo)) {
pthread_unlock(&siglock);
restore_interrupt_enable(enabled);
return 0;
}
/*
* Make the signal process pending.
*/
sigaddset(&sigpending, signo);
/*
* Look through the threads in sigwait to see if any of them
* is waiting for the signal. This is done as a separate pass
* since the value of the pthread sigmask is ignored (threads
* in sigwait will have blocked the signals being waited for).
*/
queue_iterate(&sigwaiters, pthread, pthread_thread_t *, chain) {
pthread_lock(&pthread->siglock);
pthread_lock(&pthread->waitlock);
if ((pthread->waitflags & THREAD_WS_SIGWAIT) &&
sigismember(&pthread->sigwaiting, signo)) {
/*
* Bingo. Make the signal thread pending and
* wake it up.
*/
sigaddset(&pthread->sigpending, signo);
sigemptyset(&pthread->sigwaiting);
pthread_unlock(&pthread->siglock);
/* pthread waitlock will be released. */
pthread_wakeup_locked(pthread);
pthread_unlock(&siglock);
restore_interrupt_enable(enabled);
return 0;
}
pthread_unlock(&pthread->waitlock);
pthread_unlock(&pthread->siglock);
}
/*
* No threads in sigwait. Too bad. Must find another thread to
* deliver it to.
*/
pthread_lock(&pthread_create_lock);
for (i = 1; i < THREADS_MAX_THREAD; i++) {
if ((pthread = threads_tidtothread[i]) != NULL_THREADPTR) {
pthread_lock(&pthread->siglock);
if (! sigismember(&pthread->sigmask, signo)) {
pthread_unlock(&pthread_create_lock);
/* Thread siglock will be released. */
pthread_kill_locked(pthread, signo);
break;
}
pthread_unlock(&pthread->siglock);
}
}
pthread_unlock(&pthread_create_lock);
pthread_unlock(&siglock);
/*
* If not in an interrupt, use this opportunity to deliver
* pending unblocked signals to the current thread.
*/
if (! IN_AN_INTERRUPT()) {
pthread = CURPTHREAD();
SIGCHECK(pthread);
}
restore_interrupt_enable(enabled);
return 0;
}
/*
* sigqueue.
*/
int
sigqueue(pid_t pid, int signo, const union sigval value)
{
pthread_thread_t *pthread;
int enabled;
int i;
sigqueue_thingie_t *pthingie = NULL;
struct sigaction act;
extern pthread_lock_t pthread_create_lock;
/* Error check? Sure! */
if (!signo)
return 0;
/* siglock and pthread siglock are taken from an interrupt handler */
save_interrupt_enable(enabled);
disable_interrupts();
/*
* Look at the process sigactions. If the "process" is ignoring
* the signal, then the signal is not placed in the pending list.
*/
pthread_lock(&sigactions_lock);
act = sigactions[signo];
pthread_unlock(&sigactions_lock);
if (act.sa_handler == SIG_IGN) {
restore_interrupt_enable(enabled);
return 0;
}
/*
* Take and hold the global siglock. This is needed to prevent
* a race with sigwait.
*/
pthread_lock(&siglock);
/*
* If the flags does not include SA_SIGINFO, and there is already
* a signal pending, this new one is dropped.
*/
if ((! (act.sa_flags & SA_SIGINFO)) &&
sigismember(&sigpending, signo)) {
pthread_unlock(&siglock);
restore_interrupt_enable(enabled);
return 0;
}
/*
* Gotta have space for the new signal.
*/
if (queue_empty(&sigqueue_free)) {
pthread_unlock(&siglock);
restore_interrupt_enable(enabled);
return EAGAIN;
}
/*
* Create a queue entry.
*/
queue_remove_first(&sigqueue_free,
pthingie, sigqueue_thingie_t *, chain);
pthingie->info.si_signo = signo;
pthingie->info.si_code = SI_QUEUE;
pthingie->info.si_value = value;
/*
* Queue the signal on the process.
*/
queue_enter(&sigqueued[signo], pthingie, sigqueue_thingie_t *, chain);
sigaddset(&sigpending, signo);
/*
* Look through the threads in sigwait to see if any of them
* is waiting for the signal. This is done as a separate pass
* since the value of the pthread sigmask is ignored (threads
* in sigwait will have blocked the signals being waited for).
* If we find one, wakeup that thread. Note that POSIX says that
* if multiple threads are sigwaiting for the same signal number,
* exactly one thread is woken up. The problem is how to maintain
* the FIFO order, and how to prevent lost signals in the case that
* a thread calls sigwait before the woken thread runs and gets it.
*/
queue_iterate(&sigwaiters, pthread, pthread_thread_t *, chain) {
pthread_lock(&pthread->siglock);
pthread_lock(&pthread->waitlock);
if ((pthread->waitflags & THREAD_WS_SIGWAIT) &&
sigismember(&pthread->sigwaiting, signo)) {
/*
* Bingo. Wake it up.
*/
sigemptyset(&pthread->sigwaiting);
pthread_unlock(&pthread->siglock);
/* pthread waitlock will be released. */
pthread_wakeup_locked(pthread);
pthread_unlock(&siglock);
restore_interrupt_enable(enabled);
return 0;
}
pthread_unlock(&pthread->waitlock);
pthread_unlock(&pthread->siglock);
}
/*
* Need to find a thread to deliver the signal to. Look for the
* first thread that is not blocking the signal, and send it the
* signal. It is my opinion that any program that is using sigwait,
* and has not blocked signals in all of its threads, is bogus. The
* same is true if the program is not using sigwait, and has the
* signal unblocked in more than one thread.
* Why? You might wake up a thread, but not have an actual queue
* entry left by the time it runs again and looks, since another
* thread could call sigwait and get that queue entry, or if there
* are multiple threads that can take the signal, one thread could
* get all the entries. This could result in an interrupted thread,
* but with no signal to deliver. Well, not much to do about it.
* Lets just queue the signal for the process, and let the chips
* fall where they may.
*/
pthread_lock(&pthread_create_lock);
for (i = 1; i < THREADS_MAX_THREAD; i++) {
if ((pthread = threads_tidtothread[i]) != NULL_THREADPTR) {
pthread_lock(&pthread->siglock);
if (! sigismember(&pthread->sigmask, signo)) {
pthread_unlock(&pthread_create_lock);
/* Thread siglock will be released. */
pthread_kill_locked(pthread, signo);
break;
}
pthread_unlock(&pthread->siglock);
}
}
pthread_unlock(&pthread_create_lock);
pthread_unlock(&siglock);
/*
* If not in an interrupt, use this opportunity to deliver
* pending unblocked signals to the current thread.
*/
if (! IN_AN_INTERRUPT()) {
pthread = CURPTHREAD();
SIGCHECK(pthread);
}
restore_interrupt_enable(enabled);
return 0;
}
/*
* Sigwait. Sigwait overrides the state of the pthread sigmask and the global
* sigactions. The caller *must* block the set of signals in "set", before
* calling sigwait, otherwise the behaviour is undefined (which means that
* the caller will take an async signal anyway, and sigwait will return EINTR.
*/
oskit_error_t
oskit_sigwait_internal(const sigset_t *set,
siginfo_t *info, const oskit_timespec_t *timeout)
{
pthread_thread_t *pthread = CURPTHREAD();
int thissig, rval;
pthread_testcancel();
assert_preemption_enabled();
/* siglock and pthread siglock are taken from an interrupt handler */
assert_interrupts_enabled();
disable_interrupts();
/*
* First check for process pending signals. Must take and hold
* the global siglock to prevent races with kill() and sigqueue().
*/
pthread_lock(&pthread->siglock);
pthread_lock(&siglock);
if (sigpending & *set) {
sigqueue_thingie_t *pthingie;
thissig = ffs(sigpending & *set);
/*
* Sent with kill(). Using sigwait and kill is Bogus!
*/
if (queue_empty(&sigqueued[thissig])) {
info->si_signo = thissig;
info->si_code = SI_USER;
info->si_value.sival_int = 0;
sigdelset(&pthread->sigpending, thissig);
sigdelset(&sigpending, thissig);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* Grab the first queue entry.
*/
queue_remove_first(&sigqueued[thissig],
pthingie, sigqueue_thingie_t *, chain);
/*
* If that was the last one, reset the process sigpending.
*/
if (queue_empty(&sigqueued[thissig]))
sigdelset(&sigpending, thissig);
sigdelset(&pthread->sigpending, thissig);
/*
* Copy the information and free the queue entry.
*/
info->si_signo = pthingie->info.si_signo;
info->si_code = pthingie->info.si_code;
info->si_value.sival_int = pthingie->info.si_value.sival_int;
queue_enter(&sigqueue_free,
pthingie, sigqueue_thingie_t *, chain);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* Now check for pthread pending signals.
*/
if (pthread->sigpending & *set) {
thissig = ffs(pthread->sigpending & *set);
info->si_signo = thissig;
info->si_code = SI_USER;
info->si_value.sival_int = 0;
sigdelset(&pthread->sigpending, thissig);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* For timed wait, if nothing is available and the timeout value
* is zero, its an error.
*/
if (timeout && timeout->tv_sec == 0 && timeout->tv_nsec == 0) {
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return EAGAIN;
}
/*
* Grab the wait lock and set the sigwaiting mask. Once that is done,
* release the thread siglock; Another thread can try and wake this
* thread up as a result of seeing it in sigwait, but the actual
* wakeup will be delayed until the waitlock is released in the switch
* code.
*/
pthread_lock(&pthread->waitlock);
pthread->sigwaiting = *set;
pthread_unlock(&pthread->siglock);
/*
* Add this thread to the list of threads in sigwait. Once that is
* done, it is safe to release the global siglock, which will allow
* another thread to scan the sigwaiters list. As above, it might
* find a thread in sigwait, but it will not be able to wake it up
* until the waitlock is released in the switch code.
*/
queue_enter(&sigwaiters, pthread, pthread_thread_t *, chain);
pthread_unlock(&siglock);
/* and block */
rval = oskit_pthread_sleep_withflags(THREAD_WS_SIGWAIT, timeout);
pthread_lock(&pthread->siglock);
pthread->sigwaiting = 0;
/*
* Remove from the list of threads in sigwait.
*/
pthread_lock(&siglock);
queue_remove(&sigwaiters, pthread, pthread_thread_t *, chain);
/*
* Look for timeout.
*/
if (rval == ETIMEDOUT) {
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return EAGAIN;
}
/*
* Look for a wakeup to deliver a queued signal. This would come
* either from kill() or from sigqueue().
*/
if (sigpending & *set) {
sigqueue_thingie_t *pthingie;
thissig = ffs(sigpending & *set);
/*
* Sent with kill(). Using sigwait and kill is Bogus!
*/
if (queue_empty(&sigqueued[thissig])) {
info->si_signo = thissig;
info->si_code = SI_USER;
info->si_value.sival_int = 0;
sigdelset(&sigpending, thissig);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* Grab the first queue entry.
*/
queue_remove_first(&sigqueued[thissig],
pthingie, sigqueue_thingie_t *, chain);
/*
* If that was the last one, reset the process sigpending.
*/
if (queue_empty(&sigqueued[thissig]))
sigdelset(&sigpending, thissig);
/*
* Copy the information and free the queue entry.
*/
info->si_signo = pthingie->info.si_signo;
info->si_code = pthingie->info.si_code;
info->si_value.sival_int = pthingie->info.si_value.sival_int;
queue_enter(&sigqueue_free,
pthingie, sigqueue_thingie_t *, chain);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* Well, at the moment I am going to assume that if this thread
* wakes up, and there is no signal pending in the waitset, the
* thread wait was interrupted for some other reason. Return EINTR.
*/
if (! (pthread->sigpending & *set)) {
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return EINTR;
}
/*
* Otherwise, get the first signal and return it.
*/
thissig = ffs(pthread->sigpending & *set);
info->si_signo = thissig;
info->si_code = SI_USER;
info->si_value.sival_int = 0;
sigdelset(&pthread->sigpending, thissig);
pthread_unlock(&siglock);
pthread_unlock(&pthread->siglock);
enable_interrupts();
return 0;
}
/*
* Sigwait.
*/
int
sigwait(const sigset_t *set, int *sig)
{
siginfo_t info;
oskit_error_t rc;
memset(&info, 0, sizeof(info));
rc = oskit_sigwait_internal(set, &info, 0);
if (rc)
return rc;
*sig = info.si_signo;
return 0;
}
/*
* Sigwaitinfo.
*/
int
sigwaitinfo(const sigset_t *set, siginfo_t *info)
{
oskit_error_t rc;
rc = oskit_sigwait_internal(set, info, 0);
if (rc)
return rc;
return 0;
}
/*
* Sigtimedwait.
*/
int
sigtimedwait(const sigset_t *set,
siginfo_t *info, const struct oskit_timespec *timeout)
{
oskit_error_t rc;
if (! timeout)
return EINVAL;
rc = oskit_sigwait_internal(set, info, timeout);
if (rc)
return rc;
return 0;
}
/*
* XXX we could probably implement this
*/
int
sigsuspend(const sigset_t *sigmask)
{
return ENOSYS;
}
/*
* Internal stuff.
*/
/*
* Deliver a signal generated from a trap. This is the upcall from the
* machine dependent code that created the sigcontext structure from
* the trap state.
*/
void
oskit_libc_sendsig(int sig, int code, struct sigcontext *scp)
{
pthread_thread_t *pthread = CURPTHREAD();
siginfo_t siginfo;
int enabled;
siginfo.si_signo = sig;
siginfo.si_code = SI_EXCEP;
siginfo.si_value.sival_int = code;
/*
* Need to call really_deliver_signal() with interrupts blocked
* and the pthread siglock held.
*/
save_interrupt_enable(enabled);
disable_interrupts();
pthread_lock(&pthread->siglock);
really_deliver_signal(sig, &siginfo, scp);
pthread_unlock(&pthread->siglock);
restore_interrupt_enable(enabled);
}
/*
* Deliver an asynchronous signal. This must be called with interrupts
* blocked and the pthread siglock held.
*/
void
oskit_deliver_async_signal(int sig)
{
struct sigcontext sc;
siginfo_t siginfo;
/* create a stub sigcontext_t. */
bzero(&sc, sizeof(sc));
siginfo.si_signo = sig;
siginfo.si_code = SI_USER;
siginfo.si_value.sival_int = 0;
really_deliver_signal(sig, &siginfo, &sc);
}
/*
* Deliver a process signals. This must be called with interrupts
* blocked and the siglock and pthread siglock held.
*/
void
oskit_deliver_process_signal(int sig)
{
struct sigcontext sc;
siginfo_t siginfo;
sigqueue_thingie_t *pthingie;
/* create a stub sigcontext_t. */
bzero(&sc, sizeof(sc));
/*
* Sent with kill(). Using sigwait and kill is Bogus!
*/
if (queue_empty(&sigqueued[sig])) {
siginfo.si_signo = sig;
siginfo.si_code = SI_USER;
siginfo.si_value.sival_int = 0;
sigdelset(&sigpending, sig);
goto deliver;
}
/*
* Grab the first queue entry.
*/
queue_remove_first(&sigqueued[sig],
pthingie, sigqueue_thingie_t *, chain);
/*
* If that was the last one, reset the process sigpending.
*/
if (queue_empty(&sigqueued[sig]))
sigdelset(&sigpending, sig);
/*
* Copy the information and free the queue entry.
*/
siginfo.si_signo = pthingie->info.si_signo;
siginfo.si_code = pthingie->info.si_code;
siginfo.si_value.sival_int = pthingie->info.si_value.sival_int;
queue_enter(&sigqueue_free, pthingie, sigqueue_thingie_t *, chain);
/*
* Release the global siglock for the delivery.
*/
deliver:
pthread_unlock(&siglock);
really_deliver_signal(sig, &siginfo, &sc);
/*
* Reacquire since the caller expects it.
*/
pthread_lock(&siglock);
}
/*
* Deliver any pending signals. Called out of the context switch code
* when a thread switches in, and there are pending signals.
*
* Interrupts are blocked and the thread siglock is locked.
*/
void
oskit_deliver_pending_signals(void)
{
pthread_thread_t *pthread = CURPTHREAD();
/*
* Look for process pending signals that are unblocked, and deliver.
*/
pthread_lock(&siglock);
while (sigpending & ~pthread->sigmask) {
int sig = ffs(sigpending & ~pthread->sigmask);
/* Call with siglock and thread siglock locked */
oskit_deliver_process_signal(sig);
}
pthread_unlock(&siglock);
/*
* Now deliver any pthread pending signals that are left.
*/
while (pthread->sigpending & ~pthread->sigmask) {
int sig = ffs(pthread->sigpending & ~pthread->sigmask);
/* Call at splhigh and thread locked */
oskit_deliver_async_signal(sig);
}
}
/*
* Actually deliver the signal to the thread. At this point the signal
* is going to be delivered, so it no longer matters if it is blocked.
*/
void
really_deliver_signal(int sig, siginfo_t *info, struct sigcontext *scp)
{
pthread_thread_t *pthread = CURPTHREAD();
sigset_t sigmask, oldmask;
struct sigaction act;
int enabled;
assert_interrupts_disabled();
save_preemption_enable(enabled);
pthread_lock(&sigactions_lock);
act = sigactions[sig];
pthread_unlock(&sigactions_lock);
/*
* Ignored?
*/
if (act.sa_handler == SIG_IGN || act.sa_handler == SIG_ERR)
return;
if (act.sa_handler == SIG_DFL) {
/* Default action for all signals is termination */
if (info->si_code == SI_EXCEP) {
printf("TID %p: exception, code=0x%x\n",
pthread->tid, info->si_value.sival_int);
sigcontext_dump(scp);
}
panic("Sendsig: Signal %d caught but no handler", sig);
}
/*
* Set the signal mask for calling the handler.
*/
oldmask = sigmask = pthread->sigmask;
sigaddset(&sigmask, sig);
sigmask |= act.sa_mask;
sigdelset(&pthread->sigpending, sig);
pthread->sigmask = sigmask;
pthread_unlock(&pthread->siglock);
enable_interrupts();
enable_preemption();
/*
* and call the handler ...
*/
if (act.sa_flags & SA_SIGINFO)
act.sa_sigaction(sig, info, (void *) scp);
else
((void (*)(int, int, struct sigcontext *))act.sa_handler)
(sig, info->si_value.sival_int, scp);
disable_interrupts();
restore_preemption_enable(enabled);
pthread_lock(&pthread->siglock);
pthread->sigmask = oldmask;
/*
* and return with thread siglock locked at splhigh.
*/
}
void
pthread_init_signals()
{
int i;
sigqueue_thingie_t *pthingie;
queue_init(&sigwaiters);
queue_init(&sigqueue_free);
/* Initialize the default signal actions. */
for (i = 0; i < NSIG; i++)
sigactions[i].sa_handler = SIG_DFL;
/* Initialize the signal queue headers. */
for (i = 0; i < NSIG; i++)
queue_init(&sigqueued[i]);
/* Create a free list of queue thingies. */
if ((pthingie = (sigqueue_thingie_t *)
smalloc(sizeof(sigqueue_thingie_t) * SIGQUEUE_MAX))
== NULL)
panic("pthread_init_signals: Out of memory");
for (i = 0; i < SIGQUEUE_MAX; i++) {
queue_enter(&sigqueue_free,
pthingie, sigqueue_thingie_t *, chain);
pthingie++;
}
libc_sendsig_init();
}
void
signals_init(void)
{
/* Called by libc. Initialization was already done above. */
}
|