2 * linux/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
13 #include <linux/config.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/smp_lock.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
20 #include <linux/tty.h>
21 #include <linux/binfmts.h>
22 #include <linux/security.h>
23 #include <linux/ptrace.h>
24 #include <asm/param.h>
25 #include <asm/uaccess.h>
26 #include <asm/siginfo.h>
29 * SLAB caches for signal bits.
32 static kmem_cache_t *sigqueue_cachep;
34 atomic_t nr_queued_signals;
35 int max_queued_signals = 1024;
38 * In POSIX a signal is sent either to a specific thread (Linux task)
39 * or to the process as a whole (Linux thread group). How the signal
40 * is sent determines whether it's to one thread or the whole group,
41 * which determines which signal mask(s) are involved in blocking it
42 * from being delivered until later. When the signal is delivered,
43 * either it's caught or ignored by a user handler or it has a default
44 * effect that applies to the whole thread group (POSIX process).
46 * The possible effects an unblocked signal set to SIG_DFL can have are:
47 * ignore - Nothing Happens
48 * terminate - kill the process, i.e. all threads in the group,
49 * similar to exit_group. The group leader (only) reports
50 * WIFSIGNALED status to its parent.
51 * coredump - write a core dump file describing all threads using
52 * the same mm and then kill all those threads
53 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
55 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
56 * Other signals when not blocked and set to SIG_DFL behaves as follows.
57 * The job control signals also have other special effects.
59 * +--------------------+------------------+
60 * | POSIX signal | default action |
61 * +--------------------+------------------+
62 * | SIGHUP | terminate |
63 * | SIGINT | terminate |
64 * | SIGQUIT | coredump |
65 * | SIGILL | coredump |
66 * | SIGTRAP | coredump |
67 * | SIGABRT/SIGIOT | coredump |
68 * | SIGBUS | coredump |
69 * | SIGFPE | coredump |
70 * | SIGKILL | terminate(+) |
71 * | SIGUSR1 | terminate |
72 * | SIGSEGV | coredump |
73 * | SIGUSR2 | terminate |
74 * | SIGPIPE | terminate |
75 * | SIGALRM | terminate |
76 * | SIGTERM | terminate |
77 * | SIGCHLD | ignore |
78 * | SIGCONT | ignore(*) |
79 * | SIGSTOP | stop(*)(+) |
80 * | SIGTSTP | stop(*) |
81 * | SIGTTIN | stop(*) |
82 * | SIGTTOU | stop(*) |
84 * | SIGXCPU | coredump |
85 * | SIGXFSZ | coredump |
86 * | SIGVTALRM | terminate |
87 * | SIGPROF | terminate |
88 * | SIGPOLL/SIGIO | terminate |
89 * | SIGSYS/SIGUNUSED | coredump |
90 * | SIGSTKFLT | terminate |
91 * | SIGWINCH | ignore |
92 * | SIGPWR | terminate |
93 * | SIGRTMIN-SIGRTMAX | terminate |
94 * +--------------------+------------------+
95 * | non-POSIX signal | default action |
96 * +--------------------+------------------+
97 * | SIGEMT | coredump |
98 * +--------------------+------------------+
100 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
101 * (*) Special job control effects:
102 * When SIGCONT is sent, it resumes the process (all threads in the group)
103 * from TASK_STOPPED state and also clears any pending/queued stop signals
104 * (any of those marked with "stop(*)"). This happens regardless of blocking,
105 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
106 * any pending/queued SIGCONT signals; this happens regardless of blocking,
107 * catching, or ignored the stop signal, though (except for SIGSTOP) the
108 * default action of stopping the process may happen later or never.
112 #define M_SIGEMT M(SIGEMT)
117 #if SIGRTMIN > BITS_PER_LONG
118 #define M(sig) (1ULL << ((sig)-1))
120 #define M(sig) (1UL << ((sig)-1))
122 #define T(sig, mask) (M(sig) & (mask))
124 #define SIG_KERNEL_ONLY_MASK (\
125 M(SIGKILL) | M(SIGSTOP) )
127 #define SIG_KERNEL_STOP_MASK (\
128 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
130 #define SIG_KERNEL_COREDUMP_MASK (\
131 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
132 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
133 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
135 #define SIG_KERNEL_IGNORE_MASK (\
136 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
138 #define sig_kernel_only(sig) \
139 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
140 #define sig_kernel_coredump(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
142 #define sig_kernel_ignore(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
144 #define sig_kernel_stop(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
147 #define sig_user_defined(t, signr) \
148 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
149 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
151 #define sig_fatal(t, signr) \
152 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
153 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
155 #define sig_avoid_stop_race() \
156 (sigtestsetmask(¤t->pending.signal, M(SIGCONT) | M(SIGKILL)) || \
157 sigtestsetmask(¤t->signal->shared_pending.signal, \
158 M(SIGCONT) | M(SIGKILL)))
160 static int sig_ignored(struct task_struct *t, int sig)
165 * Tracers always want to know about signals..
167 if (t->ptrace & PT_PTRACED)
171 * Blocked signals are never ignored, since the
172 * signal handler may change by the time it is
175 if (sigismember(&t->blocked, sig))
178 /* Is it explicitly or implicitly ignored? */
179 handler = t->sighand->action[sig-1].sa.sa_handler;
180 return handler == SIG_IGN ||
181 (handler == SIG_DFL && sig_kernel_ignore(sig));
185 * Re-calculate pending state from the set of locally pending
186 * signals, globally pending signals, and blocked signals.
188 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
193 switch (_NSIG_WORDS) {
195 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
196 ready |= signal->sig[i] &~ blocked->sig[i];
199 case 4: ready = signal->sig[3] &~ blocked->sig[3];
200 ready |= signal->sig[2] &~ blocked->sig[2];
201 ready |= signal->sig[1] &~ blocked->sig[1];
202 ready |= signal->sig[0] &~ blocked->sig[0];
205 case 2: ready = signal->sig[1] &~ blocked->sig[1];
206 ready |= signal->sig[0] &~ blocked->sig[0];
209 case 1: ready = signal->sig[0] &~ blocked->sig[0];
214 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
216 fastcall void recalc_sigpending_tsk(struct task_struct *t)
218 if (t->signal->group_stop_count > 0 ||
219 PENDING(&t->pending, &t->blocked) ||
220 PENDING(&t->signal->shared_pending, &t->blocked))
221 set_tsk_thread_flag(t, TIF_SIGPENDING);
223 clear_tsk_thread_flag(t, TIF_SIGPENDING);
226 void recalc_sigpending(void)
228 recalc_sigpending_tsk(current);
231 /* Given the mask, find the first available signal that should be serviced. */
234 next_signal(struct sigpending *pending, sigset_t *mask)
236 unsigned long i, *s, *m, x;
239 s = pending->signal.sig;
241 switch (_NSIG_WORDS) {
243 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
244 if ((x = *s &~ *m) != 0) {
245 sig = ffz(~x) + i*_NSIG_BPW + 1;
250 case 2: if ((x = s[0] &~ m[0]) != 0)
252 else if ((x = s[1] &~ m[1]) != 0)
259 case 1: if ((x = *s &~ *m) != 0)
267 struct sigqueue *__sigqueue_alloc(void)
269 struct sigqueue *q = 0;
271 if (atomic_read(&nr_queued_signals) < max_queued_signals)
272 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
274 atomic_inc(&nr_queued_signals);
275 INIT_LIST_HEAD(&q->list);
282 static inline void __sigqueue_free(struct sigqueue *q)
284 if (q->flags & SIGQUEUE_PREALLOC)
286 kmem_cache_free(sigqueue_cachep, q);
287 atomic_dec(&nr_queued_signals);
290 static void flush_sigqueue(struct sigpending *queue)
294 sigemptyset(&queue->signal);
295 while (!list_empty(&queue->list)) {
296 q = list_entry(queue->list.next, struct sigqueue , list);
297 list_del_init(&q->list);
303 * Flush all pending signals for a task.
307 flush_signals(struct task_struct *t)
311 spin_lock_irqsave(&t->sighand->siglock, flags);
312 clear_tsk_thread_flag(t,TIF_SIGPENDING);
313 flush_sigqueue(&t->pending);
314 flush_sigqueue(&t->signal->shared_pending);
315 spin_unlock_irqrestore(&t->sighand->siglock, flags);
319 * This function expects the tasklist_lock write-locked.
321 void __exit_sighand(struct task_struct *tsk)
323 struct sighand_struct * sighand = tsk->sighand;
325 /* Ok, we're done with the signal handlers */
327 if (atomic_dec_and_test(&sighand->count))
328 kmem_cache_free(sighand_cachep, sighand);
331 void exit_sighand(struct task_struct *tsk)
333 write_lock_irq(&tasklist_lock);
335 write_unlock_irq(&tasklist_lock);
339 * This function expects the tasklist_lock write-locked.
341 void __exit_signal(struct task_struct *tsk)
343 struct signal_struct * sig = tsk->signal;
344 struct sighand_struct * sighand = tsk->sighand;
348 if (!atomic_read(&sig->count))
350 spin_lock(&sighand->siglock);
351 if (atomic_dec_and_test(&sig->count)) {
352 if (tsk == sig->curr_target)
353 sig->curr_target = next_thread(tsk);
355 spin_unlock(&sighand->siglock);
356 flush_sigqueue(&sig->shared_pending);
359 * If there is any task waiting for the group exit
362 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
363 wake_up_process(sig->group_exit_task);
364 sig->group_exit_task = NULL;
366 if (tsk == sig->curr_target)
367 sig->curr_target = next_thread(tsk);
369 spin_unlock(&sighand->siglock);
370 sig = NULL; /* Marker for below. */
372 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
373 flush_sigqueue(&tsk->pending);
376 * We are cleaning up the signal_struct here. We delayed
377 * calling exit_itimers until after flush_sigqueue, just in
378 * case our thread-local pending queue contained a queued
379 * timer signal that would have been cleared in
380 * exit_itimers. When that called sigqueue_free, it would
381 * attempt to re-take the tasklist_lock and deadlock. This
382 * can never happen if we ensure that all queues the
383 * timer's signal might be queued on have been flushed
384 * first. The shared_pending queue, and our own pending
385 * queue are the only queues the timer could be on, since
386 * there are no other threads left in the group and timer
387 * signals are constrained to threads inside the group.
390 kmem_cache_free(signal_cachep, sig);
394 void exit_signal(struct task_struct *tsk)
396 write_lock_irq(&tasklist_lock);
398 write_unlock_irq(&tasklist_lock);
402 * Flush all handlers for a task.
406 flush_signal_handlers(struct task_struct *t, int force_default)
409 struct k_sigaction *ka = &t->sighand->action[0];
410 for (i = _NSIG ; i != 0 ; i--) {
411 if (force_default || ka->sa.sa_handler != SIG_IGN)
412 ka->sa.sa_handler = SIG_DFL;
414 sigemptyset(&ka->sa.sa_mask);
420 /* Notify the system that a driver wants to block all signals for this
421 * process, and wants to be notified if any signals at all were to be
422 * sent/acted upon. If the notifier routine returns non-zero, then the
423 * signal will be acted upon after all. If the notifier routine returns 0,
424 * then then signal will be blocked. Only one block per process is
425 * allowed. priv is a pointer to private data that the notifier routine
426 * can use to determine if the signal should be blocked or not. */
429 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
433 spin_lock_irqsave(¤t->sighand->siglock, flags);
434 current->notifier_mask = mask;
435 current->notifier_data = priv;
436 current->notifier = notifier;
437 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
440 /* Notify the system that blocking has ended. */
443 unblock_all_signals(void)
447 spin_lock_irqsave(¤t->sighand->siglock, flags);
448 current->notifier = NULL;
449 current->notifier_data = NULL;
451 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
454 static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
456 struct sigqueue *q, *first = 0;
457 int still_pending = 0;
459 if (unlikely(!sigismember(&list->signal, sig)))
463 * Collect the siginfo appropriate to this signal. Check if
464 * there is another siginfo for the same signal.
466 list_for_each_entry(q, &list->list, list) {
467 if (q->info.si_signo == sig) {
476 list_del_init(&first->list);
477 copy_siginfo(info, &first->info);
478 __sigqueue_free(first);
480 sigdelset(&list->signal, sig);
483 /* Ok, it wasn't in the queue. This must be
484 a fast-pathed signal or we must have been
485 out of queue space. So zero out the info.
487 sigdelset(&list->signal, sig);
488 info->si_signo = sig;
497 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
502 sig = next_signal(pending, mask);
504 if (current->notifier) {
505 if (sigismember(current->notifier_mask, sig)) {
506 if (!(current->notifier)(current->notifier_data)) {
507 clear_thread_flag(TIF_SIGPENDING);
513 if (!collect_signal(sig, pending, info))
523 * Dequeue a signal and return the element to the caller, which is
524 * expected to free it.
526 * All callers have to hold the siglock.
528 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
530 int signr = __dequeue_signal(&tsk->pending, mask, info);
532 signr = __dequeue_signal(&tsk->signal->shared_pending,
535 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
536 info->si_sys_private){
537 do_schedule_next_timer(info);
543 * Tell a process that it has a new active signal..
545 * NOTE! we rely on the previous spin_lock to
546 * lock interrupts for us! We can only be called with
547 * "siglock" held, and the local interrupt must
548 * have been disabled when that got acquired!
550 * No need to set need_resched since signal event passing
551 * goes through ->blocked
553 void signal_wake_up(struct task_struct *t, int resume)
557 set_tsk_thread_flag(t, TIF_SIGPENDING);
560 * If resume is set, we want to wake it up in the TASK_STOPPED case.
561 * We don't check for TASK_STOPPED because there is a race with it
562 * executing another processor and just now entering stopped state.
563 * By calling wake_up_process any time resume is set, we ensure
564 * the process will wake up and handle its stop or death signal.
566 mask = TASK_INTERRUPTIBLE;
568 mask |= TASK_STOPPED;
569 if (!wake_up_state(t, mask))
574 * Remove signals in mask from the pending set and queue.
575 * Returns 1 if any signals were found.
577 * All callers must be holding the siglock.
579 static int rm_from_queue(unsigned long mask, struct sigpending *s)
581 struct sigqueue *q, *n;
583 if (!sigtestsetmask(&s->signal, mask))
586 sigdelsetmask(&s->signal, mask);
587 list_for_each_entry_safe(q, n, &s->list, list) {
588 if (q->info.si_signo < SIGRTMIN &&
589 (mask & sigmask(q->info.si_signo))) {
590 list_del_init(&q->list);
598 * Bad permissions for sending the signal
600 static int check_kill_permission(int sig, struct siginfo *info,
601 struct task_struct *t)
604 if (sig < 0 || sig > _NSIG)
607 if ((!info || ((unsigned long)info != 1 &&
608 (unsigned long)info != 2 && SI_FROMUSER(info)))
609 && ((sig != SIGCONT) ||
610 (current->signal->session != t->signal->session))
611 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
612 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
613 && !capable(CAP_KILL))
615 return security_task_kill(t, info, sig);
619 static void do_notify_parent_cldstop(struct task_struct *tsk,
620 struct task_struct *parent);
623 * Handle magic process-wide effects of stop/continue signals.
624 * Unlike the signal actions, these happen immediately at signal-generation
625 * time regardless of blocking, ignoring, or handling. This does the
626 * actual continuing for SIGCONT, but not the actual stopping for stop
627 * signals. The process stop is done as a signal action for SIG_DFL.
629 static void handle_stop_signal(int sig, struct task_struct *p)
631 struct task_struct *t;
633 if (sig_kernel_stop(sig)) {
635 * This is a stop signal. Remove SIGCONT from all queues.
637 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
640 rm_from_queue(sigmask(SIGCONT), &t->pending);
643 } else if (sig == SIGCONT) {
645 * Remove all stop signals from all queues,
646 * and wake all threads.
648 if (unlikely(p->signal->group_stop_count > 0)) {
650 * There was a group stop in progress. We'll
651 * pretend it finished before we got here. We are
652 * obliged to report it to the parent: if the
653 * SIGSTOP happened "after" this SIGCONT, then it
654 * would have cleared this pending SIGCONT. If it
655 * happened "before" this SIGCONT, then the parent
656 * got the SIGCHLD about the stop finishing before
657 * the continue happened. We do the notification
658 * now, and it's as if the stop had finished and
659 * the SIGCHLD was pending on entry to this kill.
661 p->signal->group_stop_count = 0;
662 if (p->ptrace & PT_PTRACED)
663 do_notify_parent_cldstop(p, p->parent);
665 do_notify_parent_cldstop(
667 p->group_leader->real_parent);
669 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
673 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
676 * If there is a handler for SIGCONT, we must make
677 * sure that no thread returns to user mode before
678 * we post the signal, in case it was the only
679 * thread eligible to run the signal handler--then
680 * it must not do anything between resuming and
681 * running the handler. With the TIF_SIGPENDING
682 * flag set, the thread will pause and acquire the
683 * siglock that we hold now and until we've queued
684 * the pending signal.
686 * Wake up the stopped thread _after_ setting
689 state = TASK_STOPPED;
690 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
691 set_tsk_thread_flag(t, TIF_SIGPENDING);
692 state |= TASK_INTERRUPTIBLE;
694 wake_up_state(t, state);
701 static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
703 struct sigqueue * q = NULL;
707 * fast-pathed signals for kernel-internal things like SIGSTOP
710 if ((unsigned long)info == 2)
713 /* Real-time signals must be queued if sent by sigqueue, or
714 some other real-time mechanism. It is implementation
715 defined whether kill() does so. We attempt to do so, on
716 the principle of least surprise, but since kill is not
717 allowed to fail with EAGAIN when low on memory we just
718 make sure at least one signal gets delivered and don't
719 pass on the info struct. */
721 if (atomic_read(&nr_queued_signals) < max_queued_signals)
722 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
725 atomic_inc(&nr_queued_signals);
727 list_add_tail(&q->list, &signals->list);
728 switch ((unsigned long) info) {
730 q->info.si_signo = sig;
731 q->info.si_errno = 0;
732 q->info.si_code = SI_USER;
733 q->info.si_pid = current->pid;
734 q->info.si_uid = current->uid;
737 q->info.si_signo = sig;
738 q->info.si_errno = 0;
739 q->info.si_code = SI_KERNEL;
744 copy_siginfo(&q->info, info);
748 if (sig >= SIGRTMIN && info && (unsigned long)info != 1
749 && info->si_code != SI_USER)
751 * Queue overflow, abort. We may abort if the signal was rt
752 * and sent by user using something other than kill().
755 if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
757 * Set up a return to indicate that we dropped
760 ret = info->si_sys_private;
764 sigaddset(&signals->signal, sig);
768 #define LEGACY_QUEUE(sigptr, sig) \
769 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
773 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
777 if (!irqs_disabled())
780 if (!spin_is_locked(&t->sighand->siglock))
784 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
786 * Set up a return to indicate that we dropped the signal.
788 ret = info->si_sys_private;
790 /* Short-circuit ignored signals. */
791 if (sig_ignored(t, sig))
794 /* Support queueing exactly one non-rt signal, so that we
795 can get more detailed information about the cause of
797 if (LEGACY_QUEUE(&t->pending, sig))
800 ret = send_signal(sig, info, &t->pending);
801 if (!ret && !sigismember(&t->blocked, sig))
802 signal_wake_up(t, sig == SIGKILL);
808 * Force a signal that the process can't ignore: if necessary
809 * we unblock the signal and change any SIG_IGN to SIG_DFL.
813 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
815 unsigned long int flags;
818 spin_lock_irqsave(&t->sighand->siglock, flags);
819 if (sigismember(&t->blocked, sig) || t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
820 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
821 sigdelset(&t->blocked, sig);
822 recalc_sigpending_tsk(t);
824 ret = specific_send_sig_info(sig, info, t);
825 spin_unlock_irqrestore(&t->sighand->siglock, flags);
831 force_sig_specific(int sig, struct task_struct *t)
833 unsigned long int flags;
835 spin_lock_irqsave(&t->sighand->siglock, flags);
836 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN)
837 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
838 sigdelset(&t->blocked, sig);
839 recalc_sigpending_tsk(t);
840 specific_send_sig_info(sig, (void *)2, t);
841 spin_unlock_irqrestore(&t->sighand->siglock, flags);
845 * Test if P wants to take SIG. After we've checked all threads with this,
846 * it's equivalent to finding no threads not blocking SIG. Any threads not
847 * blocking SIG were ruled out because they are not running and already
848 * have pending signals. Such threads will dequeue from the shared queue
849 * as soon as they're available, so putting the signal on the shared queue
850 * will be equivalent to sending it to one such thread.
852 #define wants_signal(sig, p, mask) \
853 (!sigismember(&(p)->blocked, sig) \
854 && !((p)->state & mask) \
855 && !((p)->flags & PF_EXITING) \
856 && (task_curr(p) || !signal_pending(p)))
860 __group_complete_signal(int sig, struct task_struct *p, unsigned int mask)
862 struct task_struct *t;
865 * Now find a thread we can wake up to take the signal off the queue.
867 * If the main thread wants the signal, it gets first crack.
868 * Probably the least surprising to the average bear.
870 if (wants_signal(sig, p, mask))
872 else if (thread_group_empty(p))
874 * There is just one thread and it does not need to be woken.
875 * It will dequeue unblocked signals before it runs again.
880 * Otherwise try to find a suitable thread.
882 t = p->signal->curr_target;
884 /* restart balancing at this thread */
885 t = p->signal->curr_target = p;
886 BUG_ON(t->tgid != p->tgid);
888 while (!wants_signal(sig, t, mask)) {
890 if (t == p->signal->curr_target)
892 * No thread needs to be woken.
893 * Any eligible threads will see
894 * the signal in the queue soon.
898 p->signal->curr_target = t;
902 * Found a killable thread. If the signal will be fatal,
903 * then start taking the whole group down immediately.
905 if (sig_fatal(p, sig) && !p->signal->group_exit &&
906 !sigismember(&t->real_blocked, sig) &&
907 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
909 * This signal will be fatal to the whole group.
911 if (!sig_kernel_coredump(sig)) {
913 * Start a group exit and wake everybody up.
914 * This way we don't have other threads
915 * running and doing things after a slower
916 * thread has the fatal signal pending.
918 p->signal->group_exit = 1;
919 p->signal->group_exit_code = sig;
920 p->signal->group_stop_count = 0;
923 sigaddset(&t->pending.signal, SIGKILL);
924 signal_wake_up(t, 1);
931 * There will be a core dump. We make all threads other
932 * than the chosen one go into a group stop so that nothing
933 * happens until it gets scheduled, takes the signal off
934 * the shared queue, and does the core dump. This is a
935 * little more complicated than strictly necessary, but it
936 * keeps the signal state that winds up in the core dump
937 * unchanged from the death state, e.g. which thread had
938 * the core-dump signal unblocked.
940 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
941 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
942 p->signal->group_stop_count = 0;
943 p->signal->group_exit_task = t;
946 p->signal->group_stop_count++;
947 signal_wake_up(t, 0);
950 wake_up_process(p->signal->group_exit_task);
955 * The signal is already in the shared-pending queue.
956 * Tell the chosen thread to wake up and dequeue it.
958 signal_wake_up(t, sig == SIGKILL);
963 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
969 if (!spin_is_locked(&p->sighand->siglock))
972 handle_stop_signal(sig, p);
974 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
976 * Set up a return to indicate that we dropped the signal.
978 ret = info->si_sys_private;
980 /* Short-circuit ignored signals. */
981 if (sig_ignored(p, sig))
984 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
985 /* This is a non-RT signal and we already have one queued. */
989 * Don't bother zombies and stopped tasks (but
990 * SIGKILL will punch through stopped state)
992 mask = TASK_DEAD | TASK_ZOMBIE;
994 mask |= TASK_STOPPED;
997 * Put this signal on the shared-pending queue, or fail with EAGAIN.
998 * We always use the shared queue for process-wide signals,
999 * to avoid several races.
1001 ret = send_signal(sig, info, &p->signal->shared_pending);
1005 __group_complete_signal(sig, p, mask);
1010 * Nuke all other threads in the group.
1012 void zap_other_threads(struct task_struct *p)
1014 struct task_struct *t;
1016 p->signal->group_stop_count = 0;
1018 if (thread_group_empty(p))
1021 for (t = next_thread(p); t != p; t = next_thread(t)) {
1023 * Don't bother with already dead threads
1025 if (t->state & (TASK_ZOMBIE|TASK_DEAD))
1029 * We don't want to notify the parent, since we are
1030 * killed as part of a thread group due to another
1031 * thread doing an execve() or similar. So set the
1032 * exit signal to -1 to allow immediate reaping of
1033 * the process. But don't detach the thread group
1036 if (t != p->group_leader)
1037 t->exit_signal = -1;
1039 sigaddset(&t->pending.signal, SIGKILL);
1040 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1041 signal_wake_up(t, 1);
1046 * Must be called with the tasklist_lock held for reading!
1048 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1050 unsigned long flags;
1053 ret = check_kill_permission(sig, info, p);
1054 if (!ret && sig && p->sighand) {
1055 spin_lock_irqsave(&p->sighand->siglock, flags);
1056 ret = __group_send_sig_info(sig, info, p);
1057 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1064 * kill_pg_info() sends a signal to a process group: this is what the tty
1065 * control characters do (^C, ^Z etc)
1068 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1070 struct task_struct *p;
1071 struct list_head *l;
1081 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
1085 err = group_send_sig_info(sig, info, p);
1089 return found ? retval : -ESRCH;
1093 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1097 read_lock(&tasklist_lock);
1098 retval = __kill_pg_info(sig, info, pgrp);
1099 read_unlock(&tasklist_lock);
1105 * kill_sl_info() sends a signal to the session leader: this is used
1106 * to send SIGHUP to the controlling process of a terminal when
1107 * the connection is lost.
1112 kill_sl_info(int sig, struct siginfo *info, pid_t sid)
1114 int err, retval = -EINVAL;
1116 struct list_head *l;
1117 struct task_struct *p;
1123 read_lock(&tasklist_lock);
1124 for_each_task_pid(sid, PIDTYPE_SID, p, l, pid) {
1125 if (!p->signal->leader)
1127 err = group_send_sig_info(sig, info, p);
1131 read_unlock(&tasklist_lock);
1137 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1140 struct task_struct *p;
1142 read_lock(&tasklist_lock);
1143 p = find_task_by_pid(pid);
1146 error = group_send_sig_info(sig, info, p);
1147 read_unlock(&tasklist_lock);
1153 * kill_something_info() interprets pid in interesting ways just like kill(2).
1155 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1156 * is probably wrong. Should make it like BSD or SYSV.
1159 static int kill_something_info(int sig, struct siginfo *info, int pid)
1162 return kill_pg_info(sig, info, process_group(current));
1163 } else if (pid == -1) {
1164 int retval = 0, count = 0;
1165 struct task_struct * p;
1167 read_lock(&tasklist_lock);
1168 for_each_process(p) {
1169 if (p->pid > 1 && p->tgid != current->tgid) {
1170 int err = group_send_sig_info(sig, info, p);
1176 read_unlock(&tasklist_lock);
1177 return count ? retval : -ESRCH;
1178 } else if (pid < 0) {
1179 return kill_pg_info(sig, info, -pid);
1181 return kill_proc_info(sig, info, pid);
1186 * These are for backward compatibility with the rest of the kernel source.
1190 * These two are the most common entry points. They send a signal
1191 * just to the specific thread.
1194 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1197 unsigned long flags;
1200 * We need the tasklist lock even for the specific
1201 * thread case (when we don't need to follow the group
1202 * lists) in order to avoid races with "p->sighand"
1203 * going away or changing from under us.
1205 read_lock(&tasklist_lock);
1206 spin_lock_irqsave(&p->sighand->siglock, flags);
1207 ret = specific_send_sig_info(sig, info, p);
1208 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1209 read_unlock(&tasklist_lock);
1214 send_sig(int sig, struct task_struct *p, int priv)
1216 return send_sig_info(sig, (void*)(long)(priv != 0), p);
1220 * This is the entry point for "process-wide" signals.
1221 * They will go to an appropriate thread in the thread group.
1224 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1227 read_lock(&tasklist_lock);
1228 ret = group_send_sig_info(sig, info, p);
1229 read_unlock(&tasklist_lock);
1234 force_sig(int sig, struct task_struct *p)
1236 force_sig_info(sig, (void*)1L, p);
1240 kill_pg(pid_t pgrp, int sig, int priv)
1242 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1246 kill_sl(pid_t sess, int sig, int priv)
1248 return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
1252 kill_proc(pid_t pid, int sig, int priv)
1254 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1258 * These functions support sending signals using preallocated sigqueue
1259 * structures. This is needed "because realtime applications cannot
1260 * afford to lose notifications of asynchronous events, like timer
1261 * expirations or I/O completions". In the case of Posix Timers
1262 * we allocate the sigqueue structure from the timer_create. If this
1263 * allocation fails we are able to report the failure to the application
1264 * with an EAGAIN error.
1267 struct sigqueue *sigqueue_alloc(void)
1271 if ((q = __sigqueue_alloc()))
1272 q->flags |= SIGQUEUE_PREALLOC;
1276 void sigqueue_free(struct sigqueue *q)
1278 unsigned long flags;
1279 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1281 * If the signal is still pending remove it from the
1284 if (unlikely(!list_empty(&q->list))) {
1285 read_lock(&tasklist_lock);
1286 spin_lock_irqsave(q->lock, flags);
1287 if (!list_empty(&q->list))
1288 list_del_init(&q->list);
1289 spin_unlock_irqrestore(q->lock, flags);
1290 read_unlock(&tasklist_lock);
1292 q->flags &= ~SIGQUEUE_PREALLOC;
1297 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1299 unsigned long flags;
1303 * We need the tasklist lock even for the specific
1304 * thread case (when we don't need to follow the group
1305 * lists) in order to avoid races with "p->sighand"
1306 * going away or changing from under us.
1308 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1309 read_lock(&tasklist_lock);
1310 spin_lock_irqsave(&p->sighand->siglock, flags);
1312 if (unlikely(!list_empty(&q->list))) {
1314 * If an SI_TIMER entry is already queue just increment
1315 * the overrun count.
1317 if (q->info.si_code != SI_TIMER)
1319 q->info.si_overrun++;
1322 /* Short-circuit ignored signals. */
1323 if (sig_ignored(p, sig)) {
1328 q->lock = &p->sighand->siglock;
1329 list_add_tail(&q->list, &p->pending.list);
1330 sigaddset(&p->pending.signal, sig);
1331 if (!sigismember(&p->blocked, sig))
1332 signal_wake_up(p, sig == SIGKILL);
1335 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1336 read_unlock(&tasklist_lock);
1341 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1343 unsigned long flags;
1347 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1348 read_lock(&tasklist_lock);
1349 spin_lock_irqsave(&p->sighand->siglock, flags);
1350 handle_stop_signal(sig, p);
1352 /* Short-circuit ignored signals. */
1353 if (sig_ignored(p, sig)) {
1358 if (unlikely(!list_empty(&q->list))) {
1360 * If an SI_TIMER entry is already queue just increment
1361 * the overrun count. Other uses should not try to
1362 * send the signal multiple times.
1364 if (q->info.si_code != SI_TIMER)
1366 q->info.si_overrun++;
1370 * Don't bother zombies and stopped tasks (but
1371 * SIGKILL will punch through stopped state)
1373 mask = TASK_DEAD | TASK_ZOMBIE;
1375 mask |= TASK_STOPPED;
1378 * Put this signal on the shared-pending queue.
1379 * We always use the shared queue for process-wide signals,
1380 * to avoid several races.
1382 q->lock = &p->sighand->siglock;
1383 list_add_tail(&q->list, &p->signal->shared_pending.list);
1384 sigaddset(&p->signal->shared_pending.signal, sig);
1386 __group_complete_signal(sig, p, mask);
1388 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1389 read_unlock(&tasklist_lock);
1394 * Joy. Or not. Pthread wants us to wake up every thread
1395 * in our parent group.
1397 static void __wake_up_parent(struct task_struct *p,
1398 struct task_struct *parent)
1400 struct task_struct *tsk = parent;
1403 * Fortunately this is not necessary for thread groups:
1405 if (p->tgid == tsk->tgid) {
1406 wake_up_interruptible(&tsk->wait_chldexit);
1411 wake_up_interruptible(&tsk->wait_chldexit);
1412 tsk = next_thread(tsk);
1413 if (tsk->signal != parent->signal)
1415 } while (tsk != parent);
1419 * Let a parent know about a status change of a child.
1422 void do_notify_parent(struct task_struct *tsk, int sig)
1424 struct siginfo info;
1425 unsigned long flags;
1427 struct sighand_struct *psig;
1432 BUG_ON(tsk->group_leader != tsk && tsk->group_leader->state != TASK_ZOMBIE && !tsk->ptrace);
1433 BUG_ON(tsk->group_leader == tsk && !thread_group_empty(tsk) && !tsk->ptrace);
1435 info.si_signo = sig;
1437 info.si_pid = tsk->pid;
1438 info.si_uid = tsk->uid;
1440 /* FIXME: find out whether or not this is supposed to be c*time. */
1441 info.si_utime = tsk->utime;
1442 info.si_stime = tsk->stime;
1444 status = tsk->exit_code & 0x7f;
1445 why = SI_KERNEL; /* shouldn't happen */
1446 switch (tsk->state) {
1448 /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
1449 if (tsk->ptrace & PT_PTRACED)
1456 if (tsk->exit_code & 0x80)
1458 else if (tsk->exit_code & 0x7f)
1462 status = tsk->exit_code >> 8;
1467 info.si_status = status;
1469 psig = tsk->parent->sighand;
1470 spin_lock_irqsave(&psig->siglock, flags);
1471 if (sig == SIGCHLD && tsk->state != TASK_STOPPED &&
1472 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1473 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1475 * We are exiting and our parent doesn't care. POSIX.1
1476 * defines special semantics for setting SIGCHLD to SIG_IGN
1477 * or setting the SA_NOCLDWAIT flag: we should be reaped
1478 * automatically and not left for our parent's wait4 call.
1479 * Rather than having the parent do it as a magic kind of
1480 * signal handler, we just set this to tell do_exit that we
1481 * can be cleaned up without becoming a zombie. Note that
1482 * we still call __wake_up_parent in this case, because a
1483 * blocked sys_wait4 might now return -ECHILD.
1485 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1486 * is implementation-defined: we do (if you don't want
1487 * it, just use SIG_IGN instead).
1489 tsk->exit_signal = -1;
1490 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1493 if (sig > 0 && sig <= _NSIG)
1494 __group_send_sig_info(sig, &info, tsk->parent);
1495 __wake_up_parent(tsk, tsk->parent);
1496 spin_unlock_irqrestore(&psig->siglock, flags);
1501 * We need the tasklist lock because it's the only
1502 * thing that protects out "parent" pointer.
1504 * exit.c calls "do_notify_parent()" directly, because
1505 * it already has the tasklist lock.
1508 notify_parent(struct task_struct *tsk, int sig)
1511 read_lock(&tasklist_lock);
1512 do_notify_parent(tsk, sig);
1513 read_unlock(&tasklist_lock);
1518 do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent)
1520 struct siginfo info;
1521 unsigned long flags;
1522 struct sighand_struct *sighand;
1524 info.si_signo = SIGCHLD;
1526 info.si_pid = tsk->pid;
1527 info.si_uid = tsk->uid;
1529 /* FIXME: find out whether or not this is supposed to be c*time. */
1530 info.si_utime = tsk->utime;
1531 info.si_stime = tsk->stime;
1533 info.si_status = tsk->exit_code & 0x7f;
1534 info.si_code = CLD_STOPPED;
1536 sighand = parent->sighand;
1537 spin_lock_irqsave(&sighand->siglock, flags);
1538 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1539 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1540 __group_send_sig_info(SIGCHLD, &info, parent);
1542 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1544 __wake_up_parent(tsk, parent);
1545 spin_unlock_irqrestore(&sighand->siglock, flags);
1549 #ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1552 finish_stop(int stop_count)
1555 * If there are no other threads in the group, or if there is
1556 * a group stop in progress and we are the last to stop,
1557 * report to the parent. When ptraced, every thread reports itself.
1559 if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1560 read_lock(&tasklist_lock);
1561 do_notify_parent_cldstop(current, current->parent);
1562 read_unlock(&tasklist_lock);
1564 else if (stop_count == 0) {
1565 read_lock(&tasklist_lock);
1566 do_notify_parent_cldstop(current->group_leader,
1567 current->group_leader->real_parent);
1568 read_unlock(&tasklist_lock);
1573 * Now we don't run again until continued.
1575 current->exit_code = 0;
1579 * This performs the stopping for SIGSTOP and other stop signals.
1580 * We have to stop all threads in the thread group.
1583 do_signal_stop(int signr)
1585 struct signal_struct *sig = current->signal;
1586 struct sighand_struct *sighand = current->sighand;
1587 int stop_count = -1;
1589 /* spin_lock_irq(&sighand->siglock) is now done in caller */
1591 if (sig->group_stop_count > 0) {
1593 * There is a group stop in progress. We don't need to
1594 * start another one.
1596 signr = sig->group_exit_code;
1597 stop_count = --sig->group_stop_count;
1598 current->exit_code = signr;
1599 set_current_state(TASK_STOPPED);
1600 spin_unlock_irq(&sighand->siglock);
1602 else if (thread_group_empty(current)) {
1604 * Lock must be held through transition to stopped state.
1606 current->exit_code = signr;
1607 set_current_state(TASK_STOPPED);
1608 spin_unlock_irq(&sighand->siglock);
1612 * There is no group stop already in progress.
1613 * We must initiate one now, but that requires
1614 * dropping siglock to get both the tasklist lock
1615 * and siglock again in the proper order. Note that
1616 * this allows an intervening SIGCONT to be posted.
1617 * We need to check for that and bail out if necessary.
1619 struct task_struct *t;
1621 spin_unlock_irq(&sighand->siglock);
1623 /* signals can be posted during this window */
1625 read_lock(&tasklist_lock);
1626 spin_lock_irq(&sighand->siglock);
1628 if (unlikely(sig->group_exit)) {
1630 * There is a group exit in progress now.
1631 * We'll just ignore the stop and process the
1632 * associated fatal signal.
1634 spin_unlock_irq(&sighand->siglock);
1635 read_unlock(&tasklist_lock);
1639 if (unlikely(sig_avoid_stop_race())) {
1641 * Either a SIGCONT or a SIGKILL signal was
1642 * posted in the siglock-not-held window.
1644 spin_unlock_irq(&sighand->siglock);
1645 read_unlock(&tasklist_lock);
1649 if (sig->group_stop_count == 0) {
1650 sig->group_exit_code = signr;
1652 for (t = next_thread(current); t != current;
1655 * Setting state to TASK_STOPPED for a group
1656 * stop is always done with the siglock held,
1657 * so this check has no races.
1659 if (t->state < TASK_STOPPED) {
1661 signal_wake_up(t, 0);
1663 sig->group_stop_count = stop_count;
1666 /* A race with another thread while unlocked. */
1667 signr = sig->group_exit_code;
1668 stop_count = --sig->group_stop_count;
1671 current->exit_code = signr;
1672 set_current_state(TASK_STOPPED);
1674 spin_unlock_irq(&sighand->siglock);
1675 read_unlock(&tasklist_lock);
1678 finish_stop(stop_count);
1682 * Do appropriate magic when group_stop_count > 0.
1683 * We return nonzero if we stopped, after releasing the siglock.
1684 * We return zero if we still hold the siglock and should look
1685 * for another signal without checking group_stop_count again.
1687 static inline int handle_group_stop(void)
1691 if (current->signal->group_exit_task == current) {
1693 * Group stop is so we can do a core dump,
1694 * We are the initiating thread, so get on with it.
1696 current->signal->group_exit_task = NULL;
1700 if (current->signal->group_exit)
1702 * Group stop is so another thread can do a core dump,
1703 * or else we are racing against a death signal.
1704 * Just punt the stop so we can get the next signal.
1709 * There is a group stop in progress. We stop
1710 * without any associated signal being in our queue.
1712 stop_count = --current->signal->group_stop_count;
1713 current->exit_code = current->signal->group_exit_code;
1714 set_current_state(TASK_STOPPED);
1715 spin_unlock_irq(¤t->sighand->siglock);
1716 finish_stop(stop_count);
1720 int get_signal_to_deliver(siginfo_t *info, struct pt_regs *regs, void *cookie)
1722 sigset_t *mask = ¤t->blocked;
1726 spin_lock_irq(¤t->sighand->siglock);
1728 struct k_sigaction *ka;
1730 if (unlikely(current->signal->group_stop_count > 0) &&
1731 handle_group_stop())
1734 signr = dequeue_signal(current, mask, info);
1737 break; /* will return 0 */
1739 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1740 ptrace_signal_deliver(regs, cookie);
1743 * If there is a group stop in progress,
1744 * we must participate in the bookkeeping.
1746 if (current->signal->group_stop_count > 0)
1747 --current->signal->group_stop_count;
1749 /* Let the debugger run. */
1750 current->exit_code = signr;
1751 current->last_siginfo = info;
1752 set_current_state(TASK_STOPPED);
1753 spin_unlock_irq(¤t->sighand->siglock);
1754 notify_parent(current, SIGCHLD);
1757 current->last_siginfo = NULL;
1759 /* We're back. Did the debugger cancel the sig? */
1760 spin_lock_irq(¤t->sighand->siglock);
1761 signr = current->exit_code;
1765 current->exit_code = 0;
1767 /* Update the siginfo structure if the signal has
1768 changed. If the debugger wanted something
1769 specific in the siginfo structure then it should
1770 have updated *info via PTRACE_SETSIGINFO. */
1771 if (signr != info->si_signo) {
1772 info->si_signo = signr;
1774 info->si_code = SI_USER;
1775 info->si_pid = current->parent->pid;
1776 info->si_uid = current->parent->uid;
1779 /* If the (new) signal is now blocked, requeue it. */
1780 if (sigismember(¤t->blocked, signr)) {
1781 specific_send_sig_info(signr, info, current);
1786 ka = ¤t->sighand->action[signr-1];
1787 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1789 if (ka->sa.sa_handler != SIG_DFL) /* Run the handler. */
1790 break; /* will return non-zero "signr" value */
1793 * Now we are doing the default action for this signal.
1795 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1798 /* Init gets no signals it doesn't want. */
1799 if (current->pid == 1)
1802 if (sig_kernel_stop(signr)) {
1804 * The default action is to stop all threads in
1805 * the thread group. The job control signals
1806 * do nothing in an orphaned pgrp, but SIGSTOP
1807 * always works. Note that siglock needs to be
1808 * dropped during the call to is_orphaned_pgrp()
1809 * because of lock ordering with tasklist_lock.
1810 * This allows an intervening SIGCONT to be posted.
1811 * We need to check for that and bail out if necessary.
1813 if (signr == SIGSTOP) {
1814 do_signal_stop(signr); /* releases siglock */
1817 spin_unlock_irq(¤t->sighand->siglock);
1819 /* signals can be posted during this window */
1821 if (is_orphaned_pgrp(process_group(current)))
1824 spin_lock_irq(¤t->sighand->siglock);
1825 if (unlikely(sig_avoid_stop_race())) {
1827 * Either a SIGCONT or a SIGKILL signal was
1828 * posted in the siglock-not-held window.
1833 do_signal_stop(signr); /* releases siglock */
1837 spin_unlock_irq(¤t->sighand->siglock);
1840 * Anything else is fatal, maybe with a core dump.
1842 current->flags |= PF_SIGNALED;
1843 if (sig_kernel_coredump(signr) &&
1844 do_coredump((long)signr, signr, regs)) {
1846 * That killed all other threads in the group and
1847 * synchronized with their demise, so there can't
1848 * be any more left to kill now. The group_exit
1849 * flags are set by do_coredump. Note that
1850 * thread_group_empty won't always be true yet,
1851 * because those threads were blocked in __exit_mm
1852 * and we just let them go to finish dying.
1854 const int code = signr | 0x80;
1855 BUG_ON(!current->signal->group_exit);
1856 BUG_ON(current->signal->group_exit_code != code);
1862 * Death signals, no core dump.
1864 do_group_exit(signr);
1867 spin_unlock_irq(¤t->sighand->siglock);
1873 EXPORT_SYMBOL(recalc_sigpending);
1874 EXPORT_SYMBOL_GPL(dequeue_signal);
1875 EXPORT_SYMBOL(flush_signals);
1876 EXPORT_SYMBOL(force_sig);
1877 EXPORT_SYMBOL(force_sig_info);
1878 EXPORT_SYMBOL(kill_pg);
1879 EXPORT_SYMBOL(kill_pg_info);
1880 EXPORT_SYMBOL(kill_proc);
1881 EXPORT_SYMBOL(kill_proc_info);
1882 EXPORT_SYMBOL(kill_sl);
1883 EXPORT_SYMBOL(kill_sl_info);
1884 EXPORT_SYMBOL(notify_parent);
1885 EXPORT_SYMBOL(send_sig);
1886 EXPORT_SYMBOL(send_sig_info);
1887 EXPORT_SYMBOL(send_group_sig_info);
1888 EXPORT_SYMBOL(sigqueue_alloc);
1889 EXPORT_SYMBOL(sigqueue_free);
1890 EXPORT_SYMBOL(send_sigqueue);
1891 EXPORT_SYMBOL(send_group_sigqueue);
1892 EXPORT_SYMBOL(sigprocmask);
1893 EXPORT_SYMBOL(block_all_signals);
1894 EXPORT_SYMBOL(unblock_all_signals);
1898 * System call entry points.
1901 asmlinkage long sys_restart_syscall(void)
1903 struct restart_block *restart = ¤t_thread_info()->restart_block;
1904 return restart->fn(restart);
1907 long do_no_restart_syscall(struct restart_block *param)
1913 * We don't need to get the kernel lock - this is all local to this
1914 * particular thread.. (and that's good, because this is _heavily_
1915 * used by various programs)
1919 * This is also useful for kernel threads that want to temporarily
1920 * (or permanently) block certain signals.
1922 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
1923 * interface happily blocks "unblockable" signals like SIGKILL
1926 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
1931 spin_lock_irq(¤t->sighand->siglock);
1932 old_block = current->blocked;
1936 sigorsets(¤t->blocked, ¤t->blocked, set);
1939 signandsets(¤t->blocked, ¤t->blocked, set);
1942 current->blocked = *set;
1947 recalc_sigpending();
1948 spin_unlock_irq(¤t->sighand->siglock);
1950 *oldset = old_block;
1955 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
1957 int error = -EINVAL;
1958 sigset_t old_set, new_set;
1960 /* XXX: Don't preclude handling different sized sigset_t's. */
1961 if (sigsetsize != sizeof(sigset_t))
1966 if (copy_from_user(&new_set, set, sizeof(*set)))
1968 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
1970 error = sigprocmask(how, &new_set, &old_set);
1976 spin_lock_irq(¤t->sighand->siglock);
1977 old_set = current->blocked;
1978 spin_unlock_irq(¤t->sighand->siglock);
1982 if (copy_to_user(oset, &old_set, sizeof(*oset)))
1990 long do_sigpending(void __user *set, unsigned long sigsetsize)
1992 long error = -EINVAL;
1995 if (sigsetsize > sizeof(sigset_t))
1998 spin_lock_irq(¤t->sighand->siglock);
1999 sigorsets(&pending, ¤t->pending.signal,
2000 ¤t->signal->shared_pending.signal);
2001 spin_unlock_irq(¤t->sighand->siglock);
2003 /* Outside the lock because only this thread touches it. */
2004 sigandsets(&pending, ¤t->blocked, &pending);
2007 if (!copy_to_user(set, &pending, sigsetsize))
2015 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2017 return do_sigpending(set, sigsetsize);
2020 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2022 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2026 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2028 if (from->si_code < 0)
2029 return __copy_to_user(to, from, sizeof(siginfo_t))
2032 * If you change siginfo_t structure, please be sure
2033 * this code is fixed accordingly.
2034 * It should never copy any pad contained in the structure
2035 * to avoid security leaks, but must copy the generic
2036 * 3 ints plus the relevant union member.
2038 err = __put_user(from->si_signo, &to->si_signo);
2039 err |= __put_user(from->si_errno, &to->si_errno);
2040 err |= __put_user((short)from->si_code, &to->si_code);
2041 switch (from->si_code & __SI_MASK) {
2043 err |= __put_user(from->si_pid, &to->si_pid);
2044 err |= __put_user(from->si_uid, &to->si_uid);
2047 err |= __put_user(from->si_tid, &to->si_tid);
2048 err |= __put_user(from->si_overrun, &to->si_overrun);
2049 err |= __put_user(from->si_ptr, &to->si_ptr);
2052 err |= __put_user(from->si_band, &to->si_band);
2053 err |= __put_user(from->si_fd, &to->si_fd);
2056 err |= __put_user(from->si_addr, &to->si_addr);
2057 #ifdef __ARCH_SI_TRAPNO
2058 err |= __put_user(from->si_trapno, &to->si_trapno);
2062 err |= __put_user(from->si_pid, &to->si_pid);
2063 err |= __put_user(from->si_uid, &to->si_uid);
2064 err |= __put_user(from->si_status, &to->si_status);
2065 err |= __put_user(from->si_utime, &to->si_utime);
2066 err |= __put_user(from->si_stime, &to->si_stime);
2068 case __SI_RT: /* This is not generated by the kernel as of now. */
2069 case __SI_MESGQ: /* But this is */
2070 err |= __put_user(from->si_pid, &to->si_pid);
2071 err |= __put_user(from->si_uid, &to->si_uid);
2072 err |= __put_user(from->si_ptr, &to->si_ptr);
2074 default: /* this is just in case for now ... */
2075 err |= __put_user(from->si_pid, &to->si_pid);
2076 err |= __put_user(from->si_uid, &to->si_uid);
2085 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2086 siginfo_t __user *uinfo,
2087 const struct timespec __user *uts,
2096 /* XXX: Don't preclude handling different sized sigset_t's. */
2097 if (sigsetsize != sizeof(sigset_t))
2100 if (copy_from_user(&these, uthese, sizeof(these)))
2104 * Invert the set of allowed signals to get those we
2107 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2111 if (copy_from_user(&ts, uts, sizeof(ts)))
2113 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2118 spin_lock_irq(¤t->sighand->siglock);
2119 sig = dequeue_signal(current, &these, &info);
2121 timeout = MAX_SCHEDULE_TIMEOUT;
2123 timeout = (timespec_to_jiffies(&ts)
2124 + (ts.tv_sec || ts.tv_nsec));
2127 /* None ready -- temporarily unblock those we're
2128 * interested while we are sleeping in so that we'll
2129 * be awakened when they arrive. */
2130 current->real_blocked = current->blocked;
2131 sigandsets(¤t->blocked, ¤t->blocked, &these);
2132 recalc_sigpending();
2133 spin_unlock_irq(¤t->sighand->siglock);
2135 current->state = TASK_INTERRUPTIBLE;
2136 timeout = schedule_timeout(timeout);
2138 spin_lock_irq(¤t->sighand->siglock);
2139 sig = dequeue_signal(current, &these, &info);
2140 current->blocked = current->real_blocked;
2141 siginitset(¤t->real_blocked, 0);
2142 recalc_sigpending();
2145 spin_unlock_irq(¤t->sighand->siglock);
2150 if (copy_siginfo_to_user(uinfo, &info))
2163 sys_kill(int pid, int sig)
2165 struct siginfo info;
2167 info.si_signo = sig;
2169 info.si_code = SI_USER;
2170 info.si_pid = current->tgid;
2171 info.si_uid = current->uid;
2173 return kill_something_info(sig, &info, pid);
2177 * sys_tkill - send signal to one specific thread
2178 * @tgid: the thread group ID of the thread
2179 * @pid: the PID of the thread
2180 * @sig: signal to be sent
2182 * This syscall also checks the tgid and returns -ESRCH even if the PID
2183 * exists but it's not belonging to the target process anymore. This
2184 * method solves the problem of threads exiting and PIDs getting reused.
2186 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2188 struct siginfo info;
2190 struct task_struct *p;
2192 /* This is only valid for single tasks */
2193 if (pid <= 0 || tgid <= 0)
2196 info.si_signo = sig;
2198 info.si_code = SI_TKILL;
2199 info.si_pid = current->tgid;
2200 info.si_uid = current->uid;
2202 read_lock(&tasklist_lock);
2203 p = find_task_by_pid(pid);
2205 if (p && (p->tgid == tgid)) {
2206 error = check_kill_permission(sig, &info, p);
2208 * The null signal is a permissions and process existence
2209 * probe. No signal is actually delivered.
2211 if (!error && sig && p->sighand) {
2212 spin_lock_irq(&p->sighand->siglock);
2213 handle_stop_signal(sig, p);
2214 error = specific_send_sig_info(sig, &info, p);
2215 spin_unlock_irq(&p->sighand->siglock);
2218 read_unlock(&tasklist_lock);
2223 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2226 sys_tkill(int pid, int sig)
2228 struct siginfo info;
2230 struct task_struct *p;
2232 /* This is only valid for single tasks */
2236 info.si_signo = sig;
2238 info.si_code = SI_TKILL;
2239 info.si_pid = current->tgid;
2240 info.si_uid = current->uid;
2242 read_lock(&tasklist_lock);
2243 p = find_task_by_pid(pid);
2246 error = check_kill_permission(sig, &info, p);
2248 * The null signal is a permissions and process existence
2249 * probe. No signal is actually delivered.
2251 if (!error && sig && p->sighand) {
2252 spin_lock_irq(&p->sighand->siglock);
2253 handle_stop_signal(sig, p);
2254 error = specific_send_sig_info(sig, &info, p);
2255 spin_unlock_irq(&p->sighand->siglock);
2258 read_unlock(&tasklist_lock);
2263 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2267 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2270 /* Not even root can pretend to send signals from the kernel.
2271 Nor can they impersonate a kill(), which adds source info. */
2272 if (info.si_code >= 0)
2274 info.si_signo = sig;
2276 /* POSIX.1b doesn't mention process groups. */
2277 return kill_proc_info(sig, &info, pid);
2281 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2283 struct k_sigaction *k;
2285 if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2288 k = ¤t->sighand->action[sig-1];
2290 spin_lock_irq(¤t->sighand->siglock);
2291 if (signal_pending(current)) {
2293 * If there might be a fatal signal pending on multiple
2294 * threads, make sure we take it before changing the action.
2296 spin_unlock_irq(¤t->sighand->siglock);
2297 return -ERESTARTNOINTR;
2306 * "Setting a signal action to SIG_IGN for a signal that is
2307 * pending shall cause the pending signal to be discarded,
2308 * whether or not it is blocked."
2310 * "Setting a signal action to SIG_DFL for a signal that is
2311 * pending and whose default action is to ignore the signal
2312 * (for example, SIGCHLD), shall cause the pending signal to
2313 * be discarded, whether or not it is blocked"
2315 if (act->sa.sa_handler == SIG_IGN ||
2316 (act->sa.sa_handler == SIG_DFL &&
2317 sig_kernel_ignore(sig))) {
2319 * This is a fairly rare case, so we only take the
2320 * tasklist_lock once we're sure we'll need it.
2321 * Now we must do this little unlock and relock
2322 * dance to maintain the lock hierarchy.
2324 struct task_struct *t = current;
2325 spin_unlock_irq(&t->sighand->siglock);
2326 read_lock(&tasklist_lock);
2327 spin_lock_irq(&t->sighand->siglock);
2329 sigdelsetmask(&k->sa.sa_mask,
2330 sigmask(SIGKILL) | sigmask(SIGSTOP));
2331 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2333 rm_from_queue(sigmask(sig), &t->pending);
2334 recalc_sigpending_tsk(t);
2336 } while (t != current);
2337 spin_unlock_irq(¤t->sighand->siglock);
2338 read_unlock(&tasklist_lock);
2343 sigdelsetmask(&k->sa.sa_mask,
2344 sigmask(SIGKILL) | sigmask(SIGSTOP));
2347 spin_unlock_irq(¤t->sighand->siglock);
2352 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2358 oss.ss_sp = (void *) current->sas_ss_sp;
2359 oss.ss_size = current->sas_ss_size;
2360 oss.ss_flags = sas_ss_flags(sp);
2369 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2370 || __get_user(ss_sp, &uss->ss_sp)
2371 || __get_user(ss_flags, &uss->ss_flags)
2372 || __get_user(ss_size, &uss->ss_size))
2376 if (on_sig_stack(sp))
2382 * Note - this code used to test ss_flags incorrectly
2383 * old code may have been written using ss_flags==0
2384 * to mean ss_flags==SS_ONSTACK (as this was the only
2385 * way that worked) - this fix preserves that older
2388 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2391 if (ss_flags == SS_DISABLE) {
2396 if (ss_size < MINSIGSTKSZ)
2400 current->sas_ss_sp = (unsigned long) ss_sp;
2401 current->sas_ss_size = ss_size;
2406 if (copy_to_user(uoss, &oss, sizeof(oss)))
2416 sys_sigpending(old_sigset_t __user *set)
2418 return do_sigpending(set, sizeof(*set));
2421 #if !defined(__alpha__)
2422 /* Alpha has its own versions with special arguments. */
2425 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2428 old_sigset_t old_set, new_set;
2432 if (copy_from_user(&new_set, set, sizeof(*set)))
2434 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2436 spin_lock_irq(¤t->sighand->siglock);
2437 old_set = current->blocked.sig[0];
2445 sigaddsetmask(¤t->blocked, new_set);
2448 sigdelsetmask(¤t->blocked, new_set);
2451 current->blocked.sig[0] = new_set;
2455 recalc_sigpending();
2456 spin_unlock_irq(¤t->sighand->siglock);
2462 old_set = current->blocked.sig[0];
2465 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2475 sys_rt_sigaction(int sig,
2476 const struct sigaction __user *act,
2477 struct sigaction __user *oact,
2480 struct k_sigaction new_sa, old_sa;
2483 /* XXX: Don't preclude handling different sized sigset_t's. */
2484 if (sigsetsize != sizeof(sigset_t))
2488 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2492 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2495 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2501 #endif /* __sparc__ */
2504 #if !defined(__alpha__) && !defined(__ia64__) && \
2505 !defined(__arm__) && !defined(__s390__)
2507 * For backwards compatibility. Functionality superseded by sigprocmask.
2513 return current->blocked.sig[0];
2517 sys_ssetmask(int newmask)
2521 spin_lock_irq(¤t->sighand->siglock);
2522 old = current->blocked.sig[0];
2524 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2526 recalc_sigpending();
2527 spin_unlock_irq(¤t->sighand->siglock);
2531 #endif /* !defined(__alpha__) */
2533 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__) && \
2536 * For backwards compatibility. Functionality superseded by sigaction.
2538 asmlinkage unsigned long
2539 sys_signal(int sig, __sighandler_t handler)
2541 struct k_sigaction new_sa, old_sa;
2544 new_sa.sa.sa_handler = handler;
2545 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2547 ret = do_sigaction(sig, &new_sa, &old_sa);
2549 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2551 #endif /* !alpha && !__ia64__ && !defined(__mips__) && !defined(__arm__) */
2553 #ifndef HAVE_ARCH_SYS_PAUSE
2558 current->state = TASK_INTERRUPTIBLE;
2560 return -ERESTARTNOHAND;
2563 #endif /* HAVE_ARCH_SYS_PAUSE */
2565 void __init signals_init(void)
2568 kmem_cache_create("sigqueue",
2569 sizeof(struct sigqueue),
2570 __alignof__(struct sigqueue),
2572 if (!sigqueue_cachep)
2573 panic("signals_init(): cannot create sigqueue SLAB cache");