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/unistd.h>
27 #include <asm/siginfo.h>
30 * SLAB caches for signal bits.
33 static kmem_cache_t *sigqueue_cachep;
35 atomic_t nr_queued_signals;
36 int max_queued_signals = 1024;
39 * In POSIX a signal is sent either to a specific thread (Linux task)
40 * or to the process as a whole (Linux thread group). How the signal
41 * is sent determines whether it's to one thread or the whole group,
42 * which determines which signal mask(s) are involved in blocking it
43 * from being delivered until later. When the signal is delivered,
44 * either it's caught or ignored by a user handler or it has a default
45 * effect that applies to the whole thread group (POSIX process).
47 * The possible effects an unblocked signal set to SIG_DFL can have are:
48 * ignore - Nothing Happens
49 * terminate - kill the process, i.e. all threads in the group,
50 * similar to exit_group. The group leader (only) reports
51 * WIFSIGNALED status to its parent.
52 * coredump - write a core dump file describing all threads using
53 * the same mm and then kill all those threads
54 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
56 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
57 * Other signals when not blocked and set to SIG_DFL behaves as follows.
58 * The job control signals also have other special effects.
60 * +--------------------+------------------+
61 * | POSIX signal | default action |
62 * +--------------------+------------------+
63 * | SIGHUP | terminate |
64 * | SIGINT | terminate |
65 * | SIGQUIT | coredump |
66 * | SIGILL | coredump |
67 * | SIGTRAP | coredump |
68 * | SIGABRT/SIGIOT | coredump |
69 * | SIGBUS | coredump |
70 * | SIGFPE | coredump |
71 * | SIGKILL | terminate(+) |
72 * | SIGUSR1 | terminate |
73 * | SIGSEGV | coredump |
74 * | SIGUSR2 | terminate |
75 * | SIGPIPE | terminate |
76 * | SIGALRM | terminate |
77 * | SIGTERM | terminate |
78 * | SIGCHLD | ignore |
79 * | SIGCONT | ignore(*) |
80 * | SIGSTOP | stop(*)(+) |
81 * | SIGTSTP | stop(*) |
82 * | SIGTTIN | stop(*) |
83 * | SIGTTOU | stop(*) |
85 * | SIGXCPU | coredump |
86 * | SIGXFSZ | coredump |
87 * | SIGVTALRM | terminate |
88 * | SIGPROF | terminate |
89 * | SIGPOLL/SIGIO | terminate |
90 * | SIGSYS/SIGUNUSED | coredump |
91 * | SIGSTKFLT | terminate |
92 * | SIGWINCH | ignore |
93 * | SIGPWR | terminate |
94 * | SIGRTMIN-SIGRTMAX | terminate |
95 * +--------------------+------------------+
96 * | non-POSIX signal | default action |
97 * +--------------------+------------------+
98 * | SIGEMT | coredump |
99 * +--------------------+------------------+
101 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
102 * (*) Special job control effects:
103 * When SIGCONT is sent, it resumes the process (all threads in the group)
104 * from TASK_STOPPED state and also clears any pending/queued stop signals
105 * (any of those marked with "stop(*)"). This happens regardless of blocking,
106 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
107 * any pending/queued SIGCONT signals; this happens regardless of blocking,
108 * catching, or ignored the stop signal, though (except for SIGSTOP) the
109 * default action of stopping the process may happen later or never.
113 #define M_SIGEMT M(SIGEMT)
118 #if SIGRTMIN > BITS_PER_LONG
119 #define M(sig) (1ULL << ((sig)-1))
121 #define M(sig) (1UL << ((sig)-1))
123 #define T(sig, mask) (M(sig) & (mask))
125 #define SIG_KERNEL_ONLY_MASK (\
126 M(SIGKILL) | M(SIGSTOP) )
128 #define SIG_KERNEL_STOP_MASK (\
129 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
131 #define SIG_KERNEL_COREDUMP_MASK (\
132 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
133 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
134 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
136 #define SIG_KERNEL_IGNORE_MASK (\
137 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
139 #define sig_kernel_only(sig) \
140 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
141 #define sig_kernel_coredump(sig) \
142 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
143 #define sig_kernel_ignore(sig) \
144 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
145 #define sig_kernel_stop(sig) \
146 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
148 #define sig_user_defined(t, signr) \
149 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
150 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
152 #define sig_fatal(t, signr) \
153 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
154 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
156 #define sig_avoid_stop_race() \
157 (sigtestsetmask(¤t->pending.signal, M(SIGCONT) | M(SIGKILL)) || \
158 sigtestsetmask(¤t->signal->shared_pending.signal, \
159 M(SIGCONT) | M(SIGKILL)))
161 static int sig_ignored(struct task_struct *t, int sig)
166 * Tracers always want to know about signals..
168 if (t->ptrace & PT_PTRACED)
172 * Blocked signals are never ignored, since the
173 * signal handler may change by the time it is
176 if (sigismember(&t->blocked, sig))
179 /* Is it explicitly or implicitly ignored? */
180 handler = t->sighand->action[sig-1].sa.sa_handler;
181 return handler == SIG_IGN ||
182 (handler == SIG_DFL && sig_kernel_ignore(sig));
186 * Re-calculate pending state from the set of locally pending
187 * signals, globally pending signals, and blocked signals.
189 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
194 switch (_NSIG_WORDS) {
196 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
197 ready |= signal->sig[i] &~ blocked->sig[i];
200 case 4: ready = signal->sig[3] &~ blocked->sig[3];
201 ready |= signal->sig[2] &~ blocked->sig[2];
202 ready |= signal->sig[1] &~ blocked->sig[1];
203 ready |= signal->sig[0] &~ blocked->sig[0];
206 case 2: ready = signal->sig[1] &~ blocked->sig[1];
207 ready |= signal->sig[0] &~ blocked->sig[0];
210 case 1: ready = signal->sig[0] &~ blocked->sig[0];
215 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
217 fastcall void recalc_sigpending_tsk(struct task_struct *t)
219 if (t->signal->group_stop_count > 0 ||
220 PENDING(&t->pending, &t->blocked) ||
221 PENDING(&t->signal->shared_pending, &t->blocked))
222 set_tsk_thread_flag(t, TIF_SIGPENDING);
224 clear_tsk_thread_flag(t, TIF_SIGPENDING);
227 void recalc_sigpending(void)
229 recalc_sigpending_tsk(current);
232 /* Given the mask, find the first available signal that should be serviced. */
235 next_signal(struct sigpending *pending, sigset_t *mask)
237 unsigned long i, *s, *m, x;
240 s = pending->signal.sig;
242 switch (_NSIG_WORDS) {
244 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
245 if ((x = *s &~ *m) != 0) {
246 sig = ffz(~x) + i*_NSIG_BPW + 1;
251 case 2: if ((x = s[0] &~ m[0]) != 0)
253 else if ((x = s[1] &~ m[1]) != 0)
260 case 1: if ((x = *s &~ *m) != 0)
268 struct sigqueue *__sigqueue_alloc(void)
270 struct sigqueue *q = 0;
272 if (atomic_read(&nr_queued_signals) < max_queued_signals)
273 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
275 atomic_inc(&nr_queued_signals);
276 INIT_LIST_HEAD(&q->list);
283 static inline void __sigqueue_free(struct sigqueue *q)
285 if (q->flags & SIGQUEUE_PREALLOC)
287 kmem_cache_free(sigqueue_cachep, q);
288 atomic_dec(&nr_queued_signals);
291 static void flush_sigqueue(struct sigpending *queue)
295 sigemptyset(&queue->signal);
296 while (!list_empty(&queue->list)) {
297 q = list_entry(queue->list.next, struct sigqueue , list);
298 list_del_init(&q->list);
304 * Flush all pending signals for a task.
308 flush_signals(struct task_struct *t)
312 spin_lock_irqsave(&t->sighand->siglock, flags);
313 clear_tsk_thread_flag(t,TIF_SIGPENDING);
314 flush_sigqueue(&t->pending);
315 flush_sigqueue(&t->signal->shared_pending);
316 spin_unlock_irqrestore(&t->sighand->siglock, flags);
320 * This function expects the tasklist_lock write-locked.
322 void __exit_sighand(struct task_struct *tsk)
324 struct sighand_struct * sighand = tsk->sighand;
326 /* Ok, we're done with the signal handlers */
328 if (atomic_dec_and_test(&sighand->count))
329 kmem_cache_free(sighand_cachep, sighand);
332 void exit_sighand(struct task_struct *tsk)
334 write_lock_irq(&tasklist_lock);
336 write_unlock_irq(&tasklist_lock);
340 * This function expects the tasklist_lock write-locked.
342 void __exit_signal(struct task_struct *tsk)
344 struct signal_struct * sig = tsk->signal;
345 struct sighand_struct * sighand = tsk->sighand;
349 if (!atomic_read(&sig->count))
351 spin_lock(&sighand->siglock);
352 if (atomic_dec_and_test(&sig->count)) {
353 if (tsk == sig->curr_target)
354 sig->curr_target = next_thread(tsk);
356 spin_unlock(&sighand->siglock);
357 flush_sigqueue(&sig->shared_pending);
360 * If there is any task waiting for the group exit
363 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
364 wake_up_process(sig->group_exit_task);
365 sig->group_exit_task = NULL;
367 if (tsk == sig->curr_target)
368 sig->curr_target = next_thread(tsk);
370 spin_unlock(&sighand->siglock);
371 sig = NULL; /* Marker for below. */
373 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
374 flush_sigqueue(&tsk->pending);
377 * We are cleaning up the signal_struct here. We delayed
378 * calling exit_itimers until after flush_sigqueue, just in
379 * case our thread-local pending queue contained a queued
380 * timer signal that would have been cleared in
381 * exit_itimers. When that called sigqueue_free, it would
382 * attempt to re-take the tasklist_lock and deadlock. This
383 * can never happen if we ensure that all queues the
384 * timer's signal might be queued on have been flushed
385 * first. The shared_pending queue, and our own pending
386 * queue are the only queues the timer could be on, since
387 * there are no other threads left in the group and timer
388 * signals are constrained to threads inside the group.
391 kmem_cache_free(signal_cachep, sig);
395 void exit_signal(struct task_struct *tsk)
397 write_lock_irq(&tasklist_lock);
399 write_unlock_irq(&tasklist_lock);
403 * Flush all handlers for a task.
407 flush_signal_handlers(struct task_struct *t, int force_default)
410 struct k_sigaction *ka = &t->sighand->action[0];
411 for (i = _NSIG ; i != 0 ; i--) {
412 if (force_default || ka->sa.sa_handler != SIG_IGN)
413 ka->sa.sa_handler = SIG_DFL;
415 sigemptyset(&ka->sa.sa_mask);
420 EXPORT_SYMBOL_GPL(flush_signal_handlers);
422 /* Notify the system that a driver wants to block all signals for this
423 * process, and wants to be notified if any signals at all were to be
424 * sent/acted upon. If the notifier routine returns non-zero, then the
425 * signal will be acted upon after all. If the notifier routine returns 0,
426 * then then signal will be blocked. Only one block per process is
427 * allowed. priv is a pointer to private data that the notifier routine
428 * can use to determine if the signal should be blocked or not. */
431 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
435 spin_lock_irqsave(¤t->sighand->siglock, flags);
436 current->notifier_mask = mask;
437 current->notifier_data = priv;
438 current->notifier = notifier;
439 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
442 /* Notify the system that blocking has ended. */
445 unblock_all_signals(void)
449 spin_lock_irqsave(¤t->sighand->siglock, flags);
450 current->notifier = NULL;
451 current->notifier_data = NULL;
453 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
456 static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
458 struct sigqueue *q, *first = 0;
459 int still_pending = 0;
461 if (unlikely(!sigismember(&list->signal, sig)))
465 * Collect the siginfo appropriate to this signal. Check if
466 * there is another siginfo for the same signal.
468 list_for_each_entry(q, &list->list, list) {
469 if (q->info.si_signo == sig) {
478 list_del_init(&first->list);
479 copy_siginfo(info, &first->info);
480 __sigqueue_free(first);
482 sigdelset(&list->signal, sig);
485 /* Ok, it wasn't in the queue. This must be
486 a fast-pathed signal or we must have been
487 out of queue space. So zero out the info.
489 sigdelset(&list->signal, sig);
490 info->si_signo = sig;
499 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
504 sig = next_signal(pending, mask);
506 if (current->notifier) {
507 if (sigismember(current->notifier_mask, sig)) {
508 if (!(current->notifier)(current->notifier_data)) {
509 clear_thread_flag(TIF_SIGPENDING);
515 if (!collect_signal(sig, pending, info))
525 * Dequeue a signal and return the element to the caller, which is
526 * expected to free it.
528 * All callers have to hold the siglock.
530 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
532 int signr = __dequeue_signal(&tsk->pending, mask, info);
534 signr = __dequeue_signal(&tsk->signal->shared_pending,
537 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
538 info->si_sys_private){
539 do_schedule_next_timer(info);
545 * Tell a process that it has a new active signal..
547 * NOTE! we rely on the previous spin_lock to
548 * lock interrupts for us! We can only be called with
549 * "siglock" held, and the local interrupt must
550 * have been disabled when that got acquired!
552 * No need to set need_resched since signal event passing
553 * goes through ->blocked
555 void signal_wake_up(struct task_struct *t, int resume)
559 set_tsk_thread_flag(t, TIF_SIGPENDING);
562 * If resume is set, we want to wake it up in the TASK_STOPPED case.
563 * We don't check for TASK_STOPPED because there is a race with it
564 * executing another processor and just now entering stopped state.
565 * By calling wake_up_process any time resume is set, we ensure
566 * the process will wake up and handle its stop or death signal.
568 mask = TASK_INTERRUPTIBLE;
570 mask |= TASK_STOPPED;
571 if (!wake_up_state(t, mask))
576 * Remove signals in mask from the pending set and queue.
577 * Returns 1 if any signals were found.
579 * All callers must be holding the siglock.
581 static int rm_from_queue(unsigned long mask, struct sigpending *s)
583 struct sigqueue *q, *n;
585 if (!sigtestsetmask(&s->signal, mask))
588 sigdelsetmask(&s->signal, mask);
589 list_for_each_entry_safe(q, n, &s->list, list) {
590 if (q->info.si_signo < SIGRTMIN &&
591 (mask & sigmask(q->info.si_signo))) {
592 list_del_init(&q->list);
600 * Bad permissions for sending the signal
602 static int check_kill_permission(int sig, struct siginfo *info,
603 struct task_struct *t)
606 if (sig < 0 || sig > _NSIG)
609 if ((!info || ((unsigned long)info != 1 &&
610 (unsigned long)info != 2 && SI_FROMUSER(info)))
611 && ((sig != SIGCONT) ||
612 (current->signal->session != t->signal->session))
613 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
614 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
615 && !capable(CAP_KILL))
617 return security_task_kill(t, info, sig);
621 static void do_notify_parent_cldstop(struct task_struct *tsk,
622 struct task_struct *parent);
625 * Handle magic process-wide effects of stop/continue signals.
626 * Unlike the signal actions, these happen immediately at signal-generation
627 * time regardless of blocking, ignoring, or handling. This does the
628 * actual continuing for SIGCONT, but not the actual stopping for stop
629 * signals. The process stop is done as a signal action for SIG_DFL.
631 static void handle_stop_signal(int sig, struct task_struct *p)
633 struct task_struct *t;
635 if (sig_kernel_stop(sig)) {
637 * This is a stop signal. Remove SIGCONT from all queues.
639 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
642 rm_from_queue(sigmask(SIGCONT), &t->pending);
645 } else if (sig == SIGCONT) {
647 * Remove all stop signals from all queues,
648 * and wake all threads.
650 if (unlikely(p->signal->group_stop_count > 0)) {
652 * There was a group stop in progress. We'll
653 * pretend it finished before we got here. We are
654 * obliged to report it to the parent: if the
655 * SIGSTOP happened "after" this SIGCONT, then it
656 * would have cleared this pending SIGCONT. If it
657 * happened "before" this SIGCONT, then the parent
658 * got the SIGCHLD about the stop finishing before
659 * the continue happened. We do the notification
660 * now, and it's as if the stop had finished and
661 * the SIGCHLD was pending on entry to this kill.
663 p->signal->group_stop_count = 0;
664 if (p->ptrace & PT_PTRACED)
665 do_notify_parent_cldstop(p, p->parent);
667 do_notify_parent_cldstop(
669 p->group_leader->real_parent);
671 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
675 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
678 * If there is a handler for SIGCONT, we must make
679 * sure that no thread returns to user mode before
680 * we post the signal, in case it was the only
681 * thread eligible to run the signal handler--then
682 * it must not do anything between resuming and
683 * running the handler. With the TIF_SIGPENDING
684 * flag set, the thread will pause and acquire the
685 * siglock that we hold now and until we've queued
686 * the pending signal.
688 * Wake up the stopped thread _after_ setting
691 state = TASK_STOPPED;
692 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
693 set_tsk_thread_flag(t, TIF_SIGPENDING);
694 state |= TASK_INTERRUPTIBLE;
696 wake_up_state(t, state);
703 static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
705 struct sigqueue * q = NULL;
709 * fast-pathed signals for kernel-internal things like SIGSTOP
712 if ((unsigned long)info == 2)
715 /* Real-time signals must be queued if sent by sigqueue, or
716 some other real-time mechanism. It is implementation
717 defined whether kill() does so. We attempt to do so, on
718 the principle of least surprise, but since kill is not
719 allowed to fail with EAGAIN when low on memory we just
720 make sure at least one signal gets delivered and don't
721 pass on the info struct. */
723 if (atomic_read(&nr_queued_signals) < max_queued_signals)
724 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
727 atomic_inc(&nr_queued_signals);
729 list_add_tail(&q->list, &signals->list);
730 switch ((unsigned long) info) {
732 q->info.si_signo = sig;
733 q->info.si_errno = 0;
734 q->info.si_code = SI_USER;
735 q->info.si_pid = current->pid;
736 q->info.si_uid = current->uid;
739 q->info.si_signo = sig;
740 q->info.si_errno = 0;
741 q->info.si_code = SI_KERNEL;
746 copy_siginfo(&q->info, info);
750 if (sig >= SIGRTMIN && info && (unsigned long)info != 1
751 && info->si_code != SI_USER)
753 * Queue overflow, abort. We may abort if the signal was rt
754 * and sent by user using something other than kill().
757 if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
759 * Set up a return to indicate that we dropped
762 ret = info->si_sys_private;
766 sigaddset(&signals->signal, sig);
770 #define LEGACY_QUEUE(sigptr, sig) \
771 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
775 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
779 if (!irqs_disabled())
782 if (!spin_is_locked(&t->sighand->siglock))
786 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
788 * Set up a return to indicate that we dropped the signal.
790 ret = info->si_sys_private;
792 /* Short-circuit ignored signals. */
793 if (sig_ignored(t, sig))
796 /* Support queueing exactly one non-rt signal, so that we
797 can get more detailed information about the cause of
799 if (LEGACY_QUEUE(&t->pending, sig))
802 ret = send_signal(sig, info, &t->pending);
803 if (!ret && !sigismember(&t->blocked, sig))
804 signal_wake_up(t, sig == SIGKILL);
810 * Force a signal that the process can't ignore: if necessary
811 * we unblock the signal and change any SIG_IGN to SIG_DFL.
815 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
817 unsigned long int flags;
820 spin_lock_irqsave(&t->sighand->siglock, flags);
821 if (sigismember(&t->blocked, sig) || t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
822 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
823 sigdelset(&t->blocked, sig);
824 recalc_sigpending_tsk(t);
826 ret = specific_send_sig_info(sig, info, t);
827 spin_unlock_irqrestore(&t->sighand->siglock, flags);
833 force_sig_specific(int sig, struct task_struct *t)
835 unsigned long int flags;
837 spin_lock_irqsave(&t->sighand->siglock, flags);
838 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN)
839 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
840 sigdelset(&t->blocked, sig);
841 recalc_sigpending_tsk(t);
842 specific_send_sig_info(sig, (void *)2, t);
843 spin_unlock_irqrestore(&t->sighand->siglock, flags);
847 * Test if P wants to take SIG. After we've checked all threads with this,
848 * it's equivalent to finding no threads not blocking SIG. Any threads not
849 * blocking SIG were ruled out because they are not running and already
850 * have pending signals. Such threads will dequeue from the shared queue
851 * as soon as they're available, so putting the signal on the shared queue
852 * will be equivalent to sending it to one such thread.
854 #define wants_signal(sig, p, mask) \
855 (!sigismember(&(p)->blocked, sig) \
856 && !((p)->state & mask) \
857 && !((p)->flags & PF_EXITING) \
858 && (task_curr(p) || !signal_pending(p)))
862 __group_complete_signal(int sig, struct task_struct *p, unsigned int mask)
864 struct task_struct *t;
867 * Now find a thread we can wake up to take the signal off the queue.
869 * If the main thread wants the signal, it gets first crack.
870 * Probably the least surprising to the average bear.
872 if (wants_signal(sig, p, mask))
874 else if (thread_group_empty(p))
876 * There is just one thread and it does not need to be woken.
877 * It will dequeue unblocked signals before it runs again.
882 * Otherwise try to find a suitable thread.
884 t = p->signal->curr_target;
886 /* restart balancing at this thread */
887 t = p->signal->curr_target = p;
888 BUG_ON(t->tgid != p->tgid);
890 while (!wants_signal(sig, t, mask)) {
892 if (t == p->signal->curr_target)
894 * No thread needs to be woken.
895 * Any eligible threads will see
896 * the signal in the queue soon.
900 p->signal->curr_target = t;
904 * Found a killable thread. If the signal will be fatal,
905 * then start taking the whole group down immediately.
907 if (sig_fatal(p, sig) && !p->signal->group_exit &&
908 !sigismember(&t->real_blocked, sig) &&
909 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
911 * This signal will be fatal to the whole group.
913 if (!sig_kernel_coredump(sig)) {
915 * Start a group exit and wake everybody up.
916 * This way we don't have other threads
917 * running and doing things after a slower
918 * thread has the fatal signal pending.
920 p->signal->group_exit = 1;
921 p->signal->group_exit_code = sig;
922 p->signal->group_stop_count = 0;
925 sigaddset(&t->pending.signal, SIGKILL);
926 signal_wake_up(t, 1);
933 * There will be a core dump. We make all threads other
934 * than the chosen one go into a group stop so that nothing
935 * happens until it gets scheduled, takes the signal off
936 * the shared queue, and does the core dump. This is a
937 * little more complicated than strictly necessary, but it
938 * keeps the signal state that winds up in the core dump
939 * unchanged from the death state, e.g. which thread had
940 * the core-dump signal unblocked.
942 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
943 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
944 p->signal->group_stop_count = 0;
945 p->signal->group_exit_task = t;
948 p->signal->group_stop_count++;
949 signal_wake_up(t, 0);
952 wake_up_process(p->signal->group_exit_task);
957 * The signal is already in the shared-pending queue.
958 * Tell the chosen thread to wake up and dequeue it.
960 signal_wake_up(t, sig == SIGKILL);
965 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
971 if (!spin_is_locked(&p->sighand->siglock))
974 handle_stop_signal(sig, p);
976 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
978 * Set up a return to indicate that we dropped the signal.
980 ret = info->si_sys_private;
982 /* Short-circuit ignored signals. */
983 if (sig_ignored(p, sig))
986 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
987 /* This is a non-RT signal and we already have one queued. */
991 * Don't bother zombies and stopped tasks (but
992 * SIGKILL will punch through stopped state)
994 mask = TASK_DEAD | TASK_ZOMBIE;
996 mask |= TASK_STOPPED;
999 * Put this signal on the shared-pending queue, or fail with EAGAIN.
1000 * We always use the shared queue for process-wide signals,
1001 * to avoid several races.
1003 ret = send_signal(sig, info, &p->signal->shared_pending);
1007 __group_complete_signal(sig, p, mask);
1012 * Nuke all other threads in the group.
1014 void zap_other_threads(struct task_struct *p)
1016 struct task_struct *t;
1018 p->signal->group_stop_count = 0;
1020 if (thread_group_empty(p))
1023 for (t = next_thread(p); t != p; t = next_thread(t)) {
1025 * Don't bother with already dead threads
1027 if (t->state & (TASK_ZOMBIE|TASK_DEAD))
1031 * We don't want to notify the parent, since we are
1032 * killed as part of a thread group due to another
1033 * thread doing an execve() or similar. So set the
1034 * exit signal to -1 to allow immediate reaping of
1035 * the process. But don't detach the thread group
1038 if (t != p->group_leader)
1039 t->exit_signal = -1;
1041 sigaddset(&t->pending.signal, SIGKILL);
1042 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1043 signal_wake_up(t, 1);
1048 * Must be called with the tasklist_lock held for reading!
1050 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1052 unsigned long flags;
1055 ret = check_kill_permission(sig, info, p);
1056 if (!ret && sig && p->sighand) {
1057 spin_lock_irqsave(&p->sighand->siglock, flags);
1058 ret = __group_send_sig_info(sig, info, p);
1059 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1066 * kill_pg_info() sends a signal to a process group: this is what the tty
1067 * control characters do (^C, ^Z etc)
1070 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1072 struct task_struct *p;
1073 struct list_head *l;
1083 for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
1087 err = group_send_sig_info(sig, info, p);
1091 return found ? retval : -ESRCH;
1095 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1099 read_lock(&tasklist_lock);
1100 retval = __kill_pg_info(sig, info, pgrp);
1101 read_unlock(&tasklist_lock);
1107 * kill_sl_info() sends a signal to the session leader: this is used
1108 * to send SIGHUP to the controlling process of a terminal when
1109 * the connection is lost.
1114 kill_sl_info(int sig, struct siginfo *info, pid_t sid)
1116 int err, retval = -EINVAL;
1118 struct list_head *l;
1119 struct task_struct *p;
1125 read_lock(&tasklist_lock);
1126 for_each_task_pid(sid, PIDTYPE_SID, p, l, pid) {
1127 if (!p->signal->leader)
1129 err = group_send_sig_info(sig, info, p);
1133 read_unlock(&tasklist_lock);
1139 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1142 struct task_struct *p;
1144 read_lock(&tasklist_lock);
1145 p = find_task_by_pid(pid);
1148 error = group_send_sig_info(sig, info, p);
1149 read_unlock(&tasklist_lock);
1155 * kill_something_info() interprets pid in interesting ways just like kill(2).
1157 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1158 * is probably wrong. Should make it like BSD or SYSV.
1161 static int kill_something_info(int sig, struct siginfo *info, int pid)
1164 return kill_pg_info(sig, info, process_group(current));
1165 } else if (pid == -1) {
1166 int retval = 0, count = 0;
1167 struct task_struct * p;
1169 read_lock(&tasklist_lock);
1170 for_each_process(p) {
1171 if (p->pid > 1 && p->tgid != current->tgid) {
1172 int err = group_send_sig_info(sig, info, p);
1178 read_unlock(&tasklist_lock);
1179 return count ? retval : -ESRCH;
1180 } else if (pid < 0) {
1181 return kill_pg_info(sig, info, -pid);
1183 return kill_proc_info(sig, info, pid);
1188 * These are for backward compatibility with the rest of the kernel source.
1192 * These two are the most common entry points. They send a signal
1193 * just to the specific thread.
1196 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1199 unsigned long flags;
1202 * We need the tasklist lock even for the specific
1203 * thread case (when we don't need to follow the group
1204 * lists) in order to avoid races with "p->sighand"
1205 * going away or changing from under us.
1207 read_lock(&tasklist_lock);
1208 spin_lock_irqsave(&p->sighand->siglock, flags);
1209 ret = specific_send_sig_info(sig, info, p);
1210 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1211 read_unlock(&tasklist_lock);
1216 send_sig(int sig, struct task_struct *p, int priv)
1218 return send_sig_info(sig, (void*)(long)(priv != 0), p);
1222 * This is the entry point for "process-wide" signals.
1223 * They will go to an appropriate thread in the thread group.
1226 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1229 read_lock(&tasklist_lock);
1230 ret = group_send_sig_info(sig, info, p);
1231 read_unlock(&tasklist_lock);
1236 force_sig(int sig, struct task_struct *p)
1238 force_sig_info(sig, (void*)1L, p);
1242 kill_pg(pid_t pgrp, int sig, int priv)
1244 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1248 kill_sl(pid_t sess, int sig, int priv)
1250 return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
1254 kill_proc(pid_t pid, int sig, int priv)
1256 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1260 * These functions support sending signals using preallocated sigqueue
1261 * structures. This is needed "because realtime applications cannot
1262 * afford to lose notifications of asynchronous events, like timer
1263 * expirations or I/O completions". In the case of Posix Timers
1264 * we allocate the sigqueue structure from the timer_create. If this
1265 * allocation fails we are able to report the failure to the application
1266 * with an EAGAIN error.
1269 struct sigqueue *sigqueue_alloc(void)
1273 if ((q = __sigqueue_alloc()))
1274 q->flags |= SIGQUEUE_PREALLOC;
1278 void sigqueue_free(struct sigqueue *q)
1280 unsigned long flags;
1281 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1283 * If the signal is still pending remove it from the
1286 if (unlikely(!list_empty(&q->list))) {
1287 read_lock(&tasklist_lock);
1288 spin_lock_irqsave(q->lock, flags);
1289 if (!list_empty(&q->list))
1290 list_del_init(&q->list);
1291 spin_unlock_irqrestore(q->lock, flags);
1292 read_unlock(&tasklist_lock);
1294 q->flags &= ~SIGQUEUE_PREALLOC;
1299 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1301 unsigned long flags;
1305 * We need the tasklist lock even for the specific
1306 * thread case (when we don't need to follow the group
1307 * lists) in order to avoid races with "p->sighand"
1308 * going away or changing from under us.
1310 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1311 read_lock(&tasklist_lock);
1312 spin_lock_irqsave(&p->sighand->siglock, flags);
1314 if (unlikely(!list_empty(&q->list))) {
1316 * If an SI_TIMER entry is already queue just increment
1317 * the overrun count.
1319 if (q->info.si_code != SI_TIMER)
1321 q->info.si_overrun++;
1324 /* Short-circuit ignored signals. */
1325 if (sig_ignored(p, sig)) {
1330 q->lock = &p->sighand->siglock;
1331 list_add_tail(&q->list, &p->pending.list);
1332 sigaddset(&p->pending.signal, sig);
1333 if (!sigismember(&p->blocked, sig))
1334 signal_wake_up(p, sig == SIGKILL);
1337 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1338 read_unlock(&tasklist_lock);
1343 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1345 unsigned long flags;
1349 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1350 read_lock(&tasklist_lock);
1351 spin_lock_irqsave(&p->sighand->siglock, flags);
1352 handle_stop_signal(sig, p);
1354 /* Short-circuit ignored signals. */
1355 if (sig_ignored(p, sig)) {
1360 if (unlikely(!list_empty(&q->list))) {
1362 * If an SI_TIMER entry is already queue just increment
1363 * the overrun count. Other uses should not try to
1364 * send the signal multiple times.
1366 if (q->info.si_code != SI_TIMER)
1368 q->info.si_overrun++;
1372 * Don't bother zombies and stopped tasks (but
1373 * SIGKILL will punch through stopped state)
1375 mask = TASK_DEAD | TASK_ZOMBIE;
1377 mask |= TASK_STOPPED;
1380 * Put this signal on the shared-pending queue.
1381 * We always use the shared queue for process-wide signals,
1382 * to avoid several races.
1384 q->lock = &p->sighand->siglock;
1385 list_add_tail(&q->list, &p->signal->shared_pending.list);
1386 sigaddset(&p->signal->shared_pending.signal, sig);
1388 __group_complete_signal(sig, p, mask);
1390 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1391 read_unlock(&tasklist_lock);
1396 * Joy. Or not. Pthread wants us to wake up every thread
1397 * in our parent group.
1399 static void __wake_up_parent(struct task_struct *p,
1400 struct task_struct *parent)
1402 struct task_struct *tsk = parent;
1405 * Fortunately this is not necessary for thread groups:
1407 if (p->tgid == tsk->tgid) {
1408 wake_up_interruptible_sync(&tsk->wait_chldexit);
1413 wake_up_interruptible_sync(&tsk->wait_chldexit);
1414 tsk = next_thread(tsk);
1415 if (tsk->signal != parent->signal)
1417 } while (tsk != parent);
1421 * Let a parent know about a status change of a child.
1424 void do_notify_parent(struct task_struct *tsk, int sig)
1426 struct siginfo info;
1427 unsigned long flags;
1429 struct sighand_struct *psig;
1434 BUG_ON(tsk->group_leader != tsk && tsk->group_leader->state != TASK_ZOMBIE && !tsk->ptrace);
1435 BUG_ON(tsk->group_leader == tsk && !thread_group_empty(tsk) && !tsk->ptrace);
1437 info.si_signo = sig;
1439 info.si_pid = tsk->pid;
1440 info.si_uid = tsk->uid;
1442 /* FIXME: find out whether or not this is supposed to be c*time. */
1443 info.si_utime = tsk->utime;
1444 info.si_stime = tsk->stime;
1446 status = tsk->exit_code & 0x7f;
1447 why = SI_KERNEL; /* shouldn't happen */
1448 switch (tsk->state) {
1450 /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
1451 if (tsk->ptrace & PT_PTRACED)
1458 if (tsk->exit_code & 0x80)
1460 else if (tsk->exit_code & 0x7f)
1464 status = tsk->exit_code >> 8;
1469 info.si_status = status;
1471 psig = tsk->parent->sighand;
1472 spin_lock_irqsave(&psig->siglock, flags);
1473 if (sig == SIGCHLD && tsk->state != TASK_STOPPED &&
1474 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1475 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1477 * We are exiting and our parent doesn't care. POSIX.1
1478 * defines special semantics for setting SIGCHLD to SIG_IGN
1479 * or setting the SA_NOCLDWAIT flag: we should be reaped
1480 * automatically and not left for our parent's wait4 call.
1481 * Rather than having the parent do it as a magic kind of
1482 * signal handler, we just set this to tell do_exit that we
1483 * can be cleaned up without becoming a zombie. Note that
1484 * we still call __wake_up_parent in this case, because a
1485 * blocked sys_wait4 might now return -ECHILD.
1487 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1488 * is implementation-defined: we do (if you don't want
1489 * it, just use SIG_IGN instead).
1491 tsk->exit_signal = -1;
1492 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1495 if (sig > 0 && sig <= _NSIG)
1496 __group_send_sig_info(sig, &info, tsk->parent);
1497 __wake_up_parent(tsk, tsk->parent);
1498 spin_unlock_irqrestore(&psig->siglock, flags);
1503 * We need the tasklist lock because it's the only
1504 * thing that protects out "parent" pointer.
1506 * exit.c calls "do_notify_parent()" directly, because
1507 * it already has the tasklist lock.
1510 notify_parent(struct task_struct *tsk, int sig)
1513 read_lock(&tasklist_lock);
1514 do_notify_parent(tsk, sig);
1515 read_unlock(&tasklist_lock);
1520 do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent)
1522 struct siginfo info;
1523 unsigned long flags;
1524 struct sighand_struct *sighand;
1526 info.si_signo = SIGCHLD;
1528 info.si_pid = tsk->pid;
1529 info.si_uid = tsk->uid;
1531 /* FIXME: find out whether or not this is supposed to be c*time. */
1532 info.si_utime = tsk->utime;
1533 info.si_stime = tsk->stime;
1535 info.si_status = tsk->exit_code & 0x7f;
1536 info.si_code = CLD_STOPPED;
1538 sighand = parent->sighand;
1539 spin_lock_irqsave(&sighand->siglock, flags);
1540 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1541 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1542 __group_send_sig_info(SIGCHLD, &info, parent);
1544 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1546 __wake_up_parent(tsk, parent);
1547 spin_unlock_irqrestore(&sighand->siglock, flags);
1550 int print_fatal_signals = 0;
1552 static void print_fatal_signal(struct pt_regs *regs, int signr)
1556 printk("%s/%d: potentially unexpected fatal signal %d.\n",
1557 current->comm, current->pid, signr);
1560 printk("code at %08lx: ", regs->eip);
1561 for (i = 0; i < 16; i++) {
1562 __get_user(insn, (unsigned char *)(regs->eip + i));
1563 printk("%02x ", insn);
1570 static int __init setup_print_fatal_signals(char *str)
1572 get_option (&str, &print_fatal_signals);
1577 __setup("print-fatal-signals=", setup_print_fatal_signals);
1579 #ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1582 finish_stop(int stop_count)
1585 * If there are no other threads in the group, or if there is
1586 * a group stop in progress and we are the last to stop,
1587 * report to the parent. When ptraced, every thread reports itself.
1589 if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1590 read_lock(&tasklist_lock);
1591 do_notify_parent_cldstop(current, current->parent);
1592 read_unlock(&tasklist_lock);
1594 else if (stop_count == 0) {
1595 read_lock(&tasklist_lock);
1596 do_notify_parent_cldstop(current->group_leader,
1597 current->group_leader->real_parent);
1598 read_unlock(&tasklist_lock);
1603 * Now we don't run again until continued.
1605 current->exit_code = 0;
1609 * This performs the stopping for SIGSTOP and other stop signals.
1610 * We have to stop all threads in the thread group.
1613 do_signal_stop(int signr)
1615 struct signal_struct *sig = current->signal;
1616 struct sighand_struct *sighand = current->sighand;
1617 int stop_count = -1;
1619 /* spin_lock_irq(&sighand->siglock) is now done in caller */
1621 if (sig->group_stop_count > 0) {
1623 * There is a group stop in progress. We don't need to
1624 * start another one.
1626 signr = sig->group_exit_code;
1627 stop_count = --sig->group_stop_count;
1628 current->exit_code = signr;
1629 set_current_state(TASK_STOPPED);
1630 spin_unlock_irq(&sighand->siglock);
1632 else if (thread_group_empty(current)) {
1634 * Lock must be held through transition to stopped state.
1636 current->exit_code = signr;
1637 set_current_state(TASK_STOPPED);
1638 spin_unlock_irq(&sighand->siglock);
1642 * There is no group stop already in progress.
1643 * We must initiate one now, but that requires
1644 * dropping siglock to get both the tasklist lock
1645 * and siglock again in the proper order. Note that
1646 * this allows an intervening SIGCONT to be posted.
1647 * We need to check for that and bail out if necessary.
1649 struct task_struct *t;
1651 spin_unlock_irq(&sighand->siglock);
1653 /* signals can be posted during this window */
1655 read_lock(&tasklist_lock);
1656 spin_lock_irq(&sighand->siglock);
1658 if (unlikely(sig->group_exit)) {
1660 * There is a group exit in progress now.
1661 * We'll just ignore the stop and process the
1662 * associated fatal signal.
1664 spin_unlock_irq(&sighand->siglock);
1665 read_unlock(&tasklist_lock);
1669 if (unlikely(sig_avoid_stop_race())) {
1671 * Either a SIGCONT or a SIGKILL signal was
1672 * posted in the siglock-not-held window.
1674 spin_unlock_irq(&sighand->siglock);
1675 read_unlock(&tasklist_lock);
1679 if (sig->group_stop_count == 0) {
1680 sig->group_exit_code = signr;
1682 for (t = next_thread(current); t != current;
1685 * Setting state to TASK_STOPPED for a group
1686 * stop is always done with the siglock held,
1687 * so this check has no races.
1689 if (t->state < TASK_STOPPED) {
1691 signal_wake_up(t, 0);
1693 sig->group_stop_count = stop_count;
1696 /* A race with another thread while unlocked. */
1697 signr = sig->group_exit_code;
1698 stop_count = --sig->group_stop_count;
1701 current->exit_code = signr;
1702 set_current_state(TASK_STOPPED);
1704 spin_unlock_irq(&sighand->siglock);
1705 read_unlock(&tasklist_lock);
1708 finish_stop(stop_count);
1712 * Do appropriate magic when group_stop_count > 0.
1713 * We return nonzero if we stopped, after releasing the siglock.
1714 * We return zero if we still hold the siglock and should look
1715 * for another signal without checking group_stop_count again.
1717 static inline int handle_group_stop(void)
1721 if (current->signal->group_exit_task == current) {
1723 * Group stop is so we can do a core dump,
1724 * We are the initiating thread, so get on with it.
1726 current->signal->group_exit_task = NULL;
1730 if (current->signal->group_exit)
1732 * Group stop is so another thread can do a core dump,
1733 * or else we are racing against a death signal.
1734 * Just punt the stop so we can get the next signal.
1739 * There is a group stop in progress. We stop
1740 * without any associated signal being in our queue.
1742 stop_count = --current->signal->group_stop_count;
1743 current->exit_code = current->signal->group_exit_code;
1744 set_current_state(TASK_STOPPED);
1745 spin_unlock_irq(¤t->sighand->siglock);
1746 finish_stop(stop_count);
1750 int get_signal_to_deliver(siginfo_t *info, struct pt_regs *regs, void *cookie)
1752 sigset_t *mask = ¤t->blocked;
1756 spin_lock_irq(¤t->sighand->siglock);
1758 struct k_sigaction *ka;
1760 if (unlikely(current->signal->group_stop_count > 0) &&
1761 handle_group_stop())
1764 signr = dequeue_signal(current, mask, info);
1767 break; /* will return 0 */
1769 if ((signr == SIGSEGV) && print_fatal_signals) {
1770 spin_unlock_irq(¤t->sighand->siglock);
1771 print_fatal_signal(regs, signr);
1772 spin_lock_irq(¤t->sighand->siglock);
1774 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1775 ptrace_signal_deliver(regs, cookie);
1778 * If there is a group stop in progress,
1779 * we must participate in the bookkeeping.
1781 if (current->signal->group_stop_count > 0)
1782 --current->signal->group_stop_count;
1784 /* Let the debugger run. */
1785 current->exit_code = signr;
1786 current->last_siginfo = info;
1787 set_current_state(TASK_STOPPED);
1788 spin_unlock_irq(¤t->sighand->siglock);
1789 notify_parent(current, SIGCHLD);
1792 current->last_siginfo = NULL;
1794 /* We're back. Did the debugger cancel the sig? */
1795 spin_lock_irq(¤t->sighand->siglock);
1796 signr = current->exit_code;
1800 current->exit_code = 0;
1802 /* Update the siginfo structure if the signal has
1803 changed. If the debugger wanted something
1804 specific in the siginfo structure then it should
1805 have updated *info via PTRACE_SETSIGINFO. */
1806 if (signr != info->si_signo) {
1807 info->si_signo = signr;
1809 info->si_code = SI_USER;
1810 info->si_pid = current->parent->pid;
1811 info->si_uid = current->parent->uid;
1814 /* If the (new) signal is now blocked, requeue it. */
1815 if (sigismember(¤t->blocked, signr)) {
1816 specific_send_sig_info(signr, info, current);
1821 ka = ¤t->sighand->action[signr-1];
1822 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1824 if (ka->sa.sa_handler != SIG_DFL) /* Run the handler. */
1825 break; /* will return non-zero "signr" value */
1828 * Now we are doing the default action for this signal.
1830 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1833 /* Init gets no signals it doesn't want. */
1834 if (current->pid == 1)
1837 if (sig_kernel_stop(signr)) {
1839 * The default action is to stop all threads in
1840 * the thread group. The job control signals
1841 * do nothing in an orphaned pgrp, but SIGSTOP
1842 * always works. Note that siglock needs to be
1843 * dropped during the call to is_orphaned_pgrp()
1844 * because of lock ordering with tasklist_lock.
1845 * This allows an intervening SIGCONT to be posted.
1846 * We need to check for that and bail out if necessary.
1848 if (signr == SIGSTOP) {
1849 do_signal_stop(signr); /* releases siglock */
1852 spin_unlock_irq(¤t->sighand->siglock);
1854 /* signals can be posted during this window */
1856 if (is_orphaned_pgrp(process_group(current)))
1859 spin_lock_irq(¤t->sighand->siglock);
1860 if (unlikely(sig_avoid_stop_race())) {
1862 * Either a SIGCONT or a SIGKILL signal was
1863 * posted in the siglock-not-held window.
1868 do_signal_stop(signr); /* releases siglock */
1872 spin_unlock_irq(¤t->sighand->siglock);
1875 * Anything else is fatal, maybe with a core dump.
1877 current->flags |= PF_SIGNALED;
1878 if (print_fatal_signals)
1879 print_fatal_signal(regs, signr);
1880 if (sig_kernel_coredump(signr) &&
1881 do_coredump((long)signr, signr, regs)) {
1883 * That killed all other threads in the group and
1884 * synchronized with their demise, so there can't
1885 * be any more left to kill now. The group_exit
1886 * flags are set by do_coredump. Note that
1887 * thread_group_empty won't always be true yet,
1888 * because those threads were blocked in __exit_mm
1889 * and we just let them go to finish dying.
1891 const int code = signr | 0x80;
1892 BUG_ON(!current->signal->group_exit);
1893 BUG_ON(current->signal->group_exit_code != code);
1899 * Death signals, no core dump.
1901 do_group_exit(signr);
1904 spin_unlock_irq(¤t->sighand->siglock);
1910 EXPORT_SYMBOL(recalc_sigpending);
1911 EXPORT_SYMBOL_GPL(dequeue_signal);
1912 EXPORT_SYMBOL(flush_signals);
1913 EXPORT_SYMBOL(force_sig);
1914 EXPORT_SYMBOL(force_sig_info);
1915 EXPORT_SYMBOL(kill_pg);
1916 EXPORT_SYMBOL(kill_pg_info);
1917 EXPORT_SYMBOL(kill_proc);
1918 EXPORT_SYMBOL(kill_proc_info);
1919 EXPORT_SYMBOL(kill_sl);
1920 EXPORT_SYMBOL(kill_sl_info);
1921 EXPORT_SYMBOL(notify_parent);
1922 EXPORT_SYMBOL(send_sig);
1923 EXPORT_SYMBOL(send_sig_info);
1924 EXPORT_SYMBOL(send_group_sig_info);
1925 EXPORT_SYMBOL(sigqueue_alloc);
1926 EXPORT_SYMBOL(sigqueue_free);
1927 EXPORT_SYMBOL(send_sigqueue);
1928 EXPORT_SYMBOL(send_group_sigqueue);
1929 EXPORT_SYMBOL(sigprocmask);
1930 EXPORT_SYMBOL(block_all_signals);
1931 EXPORT_SYMBOL(unblock_all_signals);
1935 * System call entry points.
1938 asmlinkage long sys_restart_syscall(void)
1940 struct restart_block *restart = ¤t_thread_info()->restart_block;
1941 return restart->fn(restart);
1944 long do_no_restart_syscall(struct restart_block *param)
1950 * We don't need to get the kernel lock - this is all local to this
1951 * particular thread.. (and that's good, because this is _heavily_
1952 * used by various programs)
1956 * This is also useful for kernel threads that want to temporarily
1957 * (or permanently) block certain signals.
1959 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
1960 * interface happily blocks "unblockable" signals like SIGKILL
1963 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
1968 spin_lock_irq(¤t->sighand->siglock);
1969 old_block = current->blocked;
1973 sigorsets(¤t->blocked, ¤t->blocked, set);
1976 signandsets(¤t->blocked, ¤t->blocked, set);
1979 current->blocked = *set;
1984 recalc_sigpending();
1985 spin_unlock_irq(¤t->sighand->siglock);
1987 *oldset = old_block;
1992 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
1994 int error = -EINVAL;
1995 sigset_t old_set, new_set;
1997 /* XXX: Don't preclude handling different sized sigset_t's. */
1998 if (sigsetsize != sizeof(sigset_t))
2003 if (copy_from_user(&new_set, set, sizeof(*set)))
2005 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2007 error = sigprocmask(how, &new_set, &old_set);
2013 spin_lock_irq(¤t->sighand->siglock);
2014 old_set = current->blocked;
2015 spin_unlock_irq(¤t->sighand->siglock);
2019 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2027 long do_sigpending(void __user *set, unsigned long sigsetsize)
2029 long error = -EINVAL;
2032 if (sigsetsize > sizeof(sigset_t))
2035 spin_lock_irq(¤t->sighand->siglock);
2036 sigorsets(&pending, ¤t->pending.signal,
2037 ¤t->signal->shared_pending.signal);
2038 spin_unlock_irq(¤t->sighand->siglock);
2040 /* Outside the lock because only this thread touches it. */
2041 sigandsets(&pending, ¤t->blocked, &pending);
2044 if (!copy_to_user(set, &pending, sigsetsize))
2052 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2054 return do_sigpending(set, sigsetsize);
2057 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2059 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2063 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2065 if (from->si_code < 0)
2066 return __copy_to_user(to, from, sizeof(siginfo_t))
2069 * If you change siginfo_t structure, please be sure
2070 * this code is fixed accordingly.
2071 * It should never copy any pad contained in the structure
2072 * to avoid security leaks, but must copy the generic
2073 * 3 ints plus the relevant union member.
2075 err = __put_user(from->si_signo, &to->si_signo);
2076 err |= __put_user(from->si_errno, &to->si_errno);
2077 err |= __put_user((short)from->si_code, &to->si_code);
2078 switch (from->si_code & __SI_MASK) {
2080 err |= __put_user(from->si_pid, &to->si_pid);
2081 err |= __put_user(from->si_uid, &to->si_uid);
2084 err |= __put_user(from->si_tid, &to->si_tid);
2085 err |= __put_user(from->si_overrun, &to->si_overrun);
2086 err |= __put_user(from->si_ptr, &to->si_ptr);
2089 err |= __put_user(from->si_band, &to->si_band);
2090 err |= __put_user(from->si_fd, &to->si_fd);
2093 err |= __put_user(from->si_addr, &to->si_addr);
2094 #ifdef __ARCH_SI_TRAPNO
2095 err |= __put_user(from->si_trapno, &to->si_trapno);
2099 err |= __put_user(from->si_pid, &to->si_pid);
2100 err |= __put_user(from->si_uid, &to->si_uid);
2101 err |= __put_user(from->si_status, &to->si_status);
2102 err |= __put_user(from->si_utime, &to->si_utime);
2103 err |= __put_user(from->si_stime, &to->si_stime);
2105 case __SI_RT: /* This is not generated by the kernel as of now. */
2106 case __SI_MESGQ: /* But this is */
2107 err |= __put_user(from->si_pid, &to->si_pid);
2108 err |= __put_user(from->si_uid, &to->si_uid);
2109 err |= __put_user(from->si_ptr, &to->si_ptr);
2111 default: /* this is just in case for now ... */
2112 err |= __put_user(from->si_pid, &to->si_pid);
2113 err |= __put_user(from->si_uid, &to->si_uid);
2122 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2123 siginfo_t __user *uinfo,
2124 const struct timespec __user *uts,
2133 /* XXX: Don't preclude handling different sized sigset_t's. */
2134 if (sigsetsize != sizeof(sigset_t))
2137 if (copy_from_user(&these, uthese, sizeof(these)))
2141 * Invert the set of allowed signals to get those we
2144 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2148 if (copy_from_user(&ts, uts, sizeof(ts)))
2150 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2155 spin_lock_irq(¤t->sighand->siglock);
2156 sig = dequeue_signal(current, &these, &info);
2158 timeout = MAX_SCHEDULE_TIMEOUT;
2160 timeout = (timespec_to_jiffies(&ts)
2161 + (ts.tv_sec || ts.tv_nsec));
2164 /* None ready -- temporarily unblock those we're
2165 * interested while we are sleeping in so that we'll
2166 * be awakened when they arrive. */
2167 current->real_blocked = current->blocked;
2168 sigandsets(¤t->blocked, ¤t->blocked, &these);
2169 recalc_sigpending();
2170 spin_unlock_irq(¤t->sighand->siglock);
2172 current->state = TASK_INTERRUPTIBLE;
2173 timeout = schedule_timeout(timeout);
2175 spin_lock_irq(¤t->sighand->siglock);
2176 sig = dequeue_signal(current, &these, &info);
2177 current->blocked = current->real_blocked;
2178 siginitset(¤t->real_blocked, 0);
2179 recalc_sigpending();
2182 spin_unlock_irq(¤t->sighand->siglock);
2187 if (copy_siginfo_to_user(uinfo, &info))
2200 sys_kill(int pid, int sig)
2202 struct siginfo info;
2204 info.si_signo = sig;
2206 info.si_code = SI_USER;
2207 info.si_pid = current->tgid;
2208 info.si_uid = current->uid;
2210 return kill_something_info(sig, &info, pid);
2214 * sys_tkill - send signal to one specific thread
2215 * @tgid: the thread group ID of the thread
2216 * @pid: the PID of the thread
2217 * @sig: signal to be sent
2219 * This syscall also checks the tgid and returns -ESRCH even if the PID
2220 * exists but it's not belonging to the target process anymore. This
2221 * method solves the problem of threads exiting and PIDs getting reused.
2223 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2225 struct siginfo info;
2227 struct task_struct *p;
2229 /* This is only valid for single tasks */
2230 if (pid <= 0 || tgid <= 0)
2233 info.si_signo = sig;
2235 info.si_code = SI_TKILL;
2236 info.si_pid = current->tgid;
2237 info.si_uid = current->uid;
2239 read_lock(&tasklist_lock);
2240 p = find_task_by_pid(pid);
2242 if (p && (p->tgid == tgid)) {
2243 error = check_kill_permission(sig, &info, p);
2245 * The null signal is a permissions and process existence
2246 * probe. No signal is actually delivered.
2248 if (!error && sig && p->sighand) {
2249 spin_lock_irq(&p->sighand->siglock);
2250 handle_stop_signal(sig, p);
2251 error = specific_send_sig_info(sig, &info, p);
2252 spin_unlock_irq(&p->sighand->siglock);
2255 read_unlock(&tasklist_lock);
2260 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2263 sys_tkill(int pid, int sig)
2265 struct siginfo info;
2267 struct task_struct *p;
2269 /* This is only valid for single tasks */
2273 info.si_signo = sig;
2275 info.si_code = SI_TKILL;
2276 info.si_pid = current->tgid;
2277 info.si_uid = current->uid;
2279 read_lock(&tasklist_lock);
2280 p = find_task_by_pid(pid);
2283 error = check_kill_permission(sig, &info, p);
2285 * The null signal is a permissions and process existence
2286 * probe. No signal is actually delivered.
2288 if (!error && sig && p->sighand) {
2289 spin_lock_irq(&p->sighand->siglock);
2290 handle_stop_signal(sig, p);
2291 error = specific_send_sig_info(sig, &info, p);
2292 spin_unlock_irq(&p->sighand->siglock);
2295 read_unlock(&tasklist_lock);
2300 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2304 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2307 /* Not even root can pretend to send signals from the kernel.
2308 Nor can they impersonate a kill(), which adds source info. */
2309 if (info.si_code >= 0)
2311 info.si_signo = sig;
2313 /* POSIX.1b doesn't mention process groups. */
2314 return kill_proc_info(sig, &info, pid);
2318 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2320 struct k_sigaction *k;
2322 if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2325 k = ¤t->sighand->action[sig-1];
2327 spin_lock_irq(¤t->sighand->siglock);
2328 if (signal_pending(current)) {
2330 * If there might be a fatal signal pending on multiple
2331 * threads, make sure we take it before changing the action.
2333 spin_unlock_irq(¤t->sighand->siglock);
2334 return -ERESTARTNOINTR;
2343 * "Setting a signal action to SIG_IGN for a signal that is
2344 * pending shall cause the pending signal to be discarded,
2345 * whether or not it is blocked."
2347 * "Setting a signal action to SIG_DFL for a signal that is
2348 * pending and whose default action is to ignore the signal
2349 * (for example, SIGCHLD), shall cause the pending signal to
2350 * be discarded, whether or not it is blocked"
2352 if (act->sa.sa_handler == SIG_IGN ||
2353 (act->sa.sa_handler == SIG_DFL &&
2354 sig_kernel_ignore(sig))) {
2356 * This is a fairly rare case, so we only take the
2357 * tasklist_lock once we're sure we'll need it.
2358 * Now we must do this little unlock and relock
2359 * dance to maintain the lock hierarchy.
2361 struct task_struct *t = current;
2362 spin_unlock_irq(&t->sighand->siglock);
2363 read_lock(&tasklist_lock);
2364 spin_lock_irq(&t->sighand->siglock);
2366 sigdelsetmask(&k->sa.sa_mask,
2367 sigmask(SIGKILL) | sigmask(SIGSTOP));
2368 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2370 rm_from_queue(sigmask(sig), &t->pending);
2371 recalc_sigpending_tsk(t);
2373 } while (t != current);
2374 spin_unlock_irq(¤t->sighand->siglock);
2375 read_unlock(&tasklist_lock);
2380 sigdelsetmask(&k->sa.sa_mask,
2381 sigmask(SIGKILL) | sigmask(SIGSTOP));
2384 spin_unlock_irq(¤t->sighand->siglock);
2389 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2395 oss.ss_sp = (void *) current->sas_ss_sp;
2396 oss.ss_size = current->sas_ss_size;
2397 oss.ss_flags = sas_ss_flags(sp);
2406 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2407 || __get_user(ss_sp, &uss->ss_sp)
2408 || __get_user(ss_flags, &uss->ss_flags)
2409 || __get_user(ss_size, &uss->ss_size))
2413 if (on_sig_stack(sp))
2419 * Note - this code used to test ss_flags incorrectly
2420 * old code may have been written using ss_flags==0
2421 * to mean ss_flags==SS_ONSTACK (as this was the only
2422 * way that worked) - this fix preserves that older
2425 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2428 if (ss_flags == SS_DISABLE) {
2433 if (ss_size < MINSIGSTKSZ)
2437 current->sas_ss_sp = (unsigned long) ss_sp;
2438 current->sas_ss_size = ss_size;
2443 if (copy_to_user(uoss, &oss, sizeof(oss)))
2452 #ifdef __ARCH_WANT_SYS_SIGPENDING
2455 sys_sigpending(old_sigset_t __user *set)
2457 return do_sigpending(set, sizeof(*set));
2462 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2463 /* Some platforms have their own version with special arguments others
2464 support only sys_rt_sigprocmask. */
2467 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2470 old_sigset_t old_set, new_set;
2474 if (copy_from_user(&new_set, set, sizeof(*set)))
2476 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2478 spin_lock_irq(¤t->sighand->siglock);
2479 old_set = current->blocked.sig[0];
2487 sigaddsetmask(¤t->blocked, new_set);
2490 sigdelsetmask(¤t->blocked, new_set);
2493 current->blocked.sig[0] = new_set;
2497 recalc_sigpending();
2498 spin_unlock_irq(¤t->sighand->siglock);
2504 old_set = current->blocked.sig[0];
2507 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2517 sys_rt_sigaction(int sig,
2518 const struct sigaction __user *act,
2519 struct sigaction __user *oact,
2522 struct k_sigaction new_sa, old_sa;
2525 /* XXX: Don't preclude handling different sized sigset_t's. */
2526 if (sigsetsize != sizeof(sigset_t))
2530 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2534 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2537 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2543 #endif /* __sparc__ */
2546 #ifdef __ARCH_WANT_SYS_SGETMASK
2549 * For backwards compatibility. Functionality superseded by sigprocmask.
2555 return current->blocked.sig[0];
2559 sys_ssetmask(int newmask)
2563 spin_lock_irq(¤t->sighand->siglock);
2564 old = current->blocked.sig[0];
2566 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2568 recalc_sigpending();
2569 spin_unlock_irq(¤t->sighand->siglock);
2573 #endif /* __ARCH_WANT_SGETMASK */
2575 #ifdef __ARCH_WANT_SYS_SIGNAL
2577 * For backwards compatibility. Functionality superseded by sigaction.
2579 asmlinkage unsigned long
2580 sys_signal(int sig, __sighandler_t handler)
2582 struct k_sigaction new_sa, old_sa;
2585 new_sa.sa.sa_handler = handler;
2586 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2588 ret = do_sigaction(sig, &new_sa, &old_sa);
2590 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2592 #endif /* __ARCH_WANT_SYS_SIGNAL */
2594 #ifdef __ARCH_WANT_SYS_PAUSE
2599 current->state = TASK_INTERRUPTIBLE;
2601 return -ERESTARTNOHAND;
2606 void __init signals_init(void)
2609 kmem_cache_create("sigqueue",
2610 sizeof(struct sigqueue),
2611 __alignof__(struct sigqueue),
2612 SLAB_PANIC, NULL, NULL);