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/syscalls.h>
24 #include <linux/ptrace.h>
25 #include <linux/signal.h>
26 #include <linux/audit.h>
27 #include <linux/capability.h>
28 #include <asm/param.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/siginfo.h>
34 * SLAB caches for signal bits.
37 static kmem_cache_t *sigqueue_cachep;
40 * In POSIX a signal is sent either to a specific thread (Linux task)
41 * or to the process as a whole (Linux thread group). How the signal
42 * is sent determines whether it's to one thread or the whole group,
43 * which determines which signal mask(s) are involved in blocking it
44 * from being delivered until later. When the signal is delivered,
45 * either it's caught or ignored by a user handler or it has a default
46 * effect that applies to the whole thread group (POSIX process).
48 * The possible effects an unblocked signal set to SIG_DFL can have are:
49 * ignore - Nothing Happens
50 * terminate - kill the process, i.e. all threads in the group,
51 * similar to exit_group. The group leader (only) reports
52 * WIFSIGNALED status to its parent.
53 * coredump - write a core dump file describing all threads using
54 * the same mm and then kill all those threads
55 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
57 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
58 * Other signals when not blocked and set to SIG_DFL behaves as follows.
59 * The job control signals also have other special effects.
61 * +--------------------+------------------+
62 * | POSIX signal | default action |
63 * +--------------------+------------------+
64 * | SIGHUP | terminate |
65 * | SIGINT | terminate |
66 * | SIGQUIT | coredump |
67 * | SIGILL | coredump |
68 * | SIGTRAP | coredump |
69 * | SIGABRT/SIGIOT | coredump |
70 * | SIGBUS | coredump |
71 * | SIGFPE | coredump |
72 * | SIGKILL | terminate(+) |
73 * | SIGUSR1 | terminate |
74 * | SIGSEGV | coredump |
75 * | SIGUSR2 | terminate |
76 * | SIGPIPE | terminate |
77 * | SIGALRM | terminate |
78 * | SIGTERM | terminate |
79 * | SIGCHLD | ignore |
80 * | SIGCONT | ignore(*) |
81 * | SIGSTOP | stop(*)(+) |
82 * | SIGTSTP | stop(*) |
83 * | SIGTTIN | stop(*) |
84 * | SIGTTOU | stop(*) |
86 * | SIGXCPU | coredump |
87 * | SIGXFSZ | coredump |
88 * | SIGVTALRM | terminate |
89 * | SIGPROF | terminate |
90 * | SIGPOLL/SIGIO | terminate |
91 * | SIGSYS/SIGUNUSED | coredump |
92 * | SIGSTKFLT | terminate |
93 * | SIGWINCH | ignore |
94 * | SIGPWR | terminate |
95 * | SIGRTMIN-SIGRTMAX | terminate |
96 * +--------------------+------------------+
97 * | non-POSIX signal | default action |
98 * +--------------------+------------------+
99 * | SIGEMT | coredump |
100 * +--------------------+------------------+
102 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
103 * (*) Special job control effects:
104 * When SIGCONT is sent, it resumes the process (all threads in the group)
105 * from TASK_STOPPED state and also clears any pending/queued stop signals
106 * (any of those marked with "stop(*)"). This happens regardless of blocking,
107 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
108 * any pending/queued SIGCONT signals; this happens regardless of blocking,
109 * catching, or ignored the stop signal, though (except for SIGSTOP) the
110 * default action of stopping the process may happen later or never.
114 #define M_SIGEMT M(SIGEMT)
119 #if SIGRTMIN > BITS_PER_LONG
120 #define M(sig) (1ULL << ((sig)-1))
122 #define M(sig) (1UL << ((sig)-1))
124 #define T(sig, mask) (M(sig) & (mask))
126 #define SIG_KERNEL_ONLY_MASK (\
127 M(SIGKILL) | M(SIGSTOP) )
129 #define SIG_KERNEL_STOP_MASK (\
130 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
132 #define SIG_KERNEL_COREDUMP_MASK (\
133 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
134 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
135 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
137 #define SIG_KERNEL_IGNORE_MASK (\
138 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
140 #define sig_kernel_only(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
142 #define sig_kernel_coredump(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
144 #define sig_kernel_ignore(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
146 #define sig_kernel_stop(sig) \
147 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
149 #define sig_needs_tasklist(sig) \
150 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK | M(SIGCONT)))
152 #define sig_user_defined(t, signr) \
153 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
154 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
156 #define sig_fatal(t, signr) \
157 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
158 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
160 static int sig_ignored(struct task_struct *t, int sig)
162 void __user * handler;
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 ||
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 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
271 struct sigqueue *q = NULL;
273 atomic_inc(&t->user->sigpending);
274 if (override_rlimit ||
275 atomic_read(&t->user->sigpending) <=
276 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
277 q = kmem_cache_alloc(sigqueue_cachep, flags);
278 if (unlikely(q == NULL)) {
279 atomic_dec(&t->user->sigpending);
281 INIT_LIST_HEAD(&q->list);
283 q->user = get_uid(t->user);
288 static void __sigqueue_free(struct sigqueue *q)
290 if (q->flags & SIGQUEUE_PREALLOC)
292 atomic_dec(&q->user->sigpending);
294 kmem_cache_free(sigqueue_cachep, q);
297 void flush_sigqueue(struct sigpending *queue)
301 sigemptyset(&queue->signal);
302 while (!list_empty(&queue->list)) {
303 q = list_entry(queue->list.next, struct sigqueue , list);
304 list_del_init(&q->list);
310 * Flush all pending signals for a task.
312 void flush_signals(struct task_struct *t)
316 spin_lock_irqsave(&t->sighand->siglock, flags);
317 clear_tsk_thread_flag(t,TIF_SIGPENDING);
318 flush_sigqueue(&t->pending);
319 flush_sigqueue(&t->signal->shared_pending);
320 spin_unlock_irqrestore(&t->sighand->siglock, flags);
324 * Flush all handlers for a task.
328 flush_signal_handlers(struct task_struct *t, int force_default)
331 struct k_sigaction *ka = &t->sighand->action[0];
332 for (i = _NSIG ; i != 0 ; i--) {
333 if (force_default || ka->sa.sa_handler != SIG_IGN)
334 ka->sa.sa_handler = SIG_DFL;
336 sigemptyset(&ka->sa.sa_mask);
342 /* Notify the system that a driver wants to block all signals for this
343 * process, and wants to be notified if any signals at all were to be
344 * sent/acted upon. If the notifier routine returns non-zero, then the
345 * signal will be acted upon after all. If the notifier routine returns 0,
346 * then then signal will be blocked. Only one block per process is
347 * allowed. priv is a pointer to private data that the notifier routine
348 * can use to determine if the signal should be blocked or not. */
351 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
355 spin_lock_irqsave(¤t->sighand->siglock, flags);
356 current->notifier_mask = mask;
357 current->notifier_data = priv;
358 current->notifier = notifier;
359 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
362 /* Notify the system that blocking has ended. */
365 unblock_all_signals(void)
369 spin_lock_irqsave(¤t->sighand->siglock, flags);
370 current->notifier = NULL;
371 current->notifier_data = NULL;
373 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
376 static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
378 struct sigqueue *q, *first = NULL;
379 int still_pending = 0;
381 if (unlikely(!sigismember(&list->signal, sig)))
385 * Collect the siginfo appropriate to this signal. Check if
386 * there is another siginfo for the same signal.
388 list_for_each_entry(q, &list->list, list) {
389 if (q->info.si_signo == sig) {
398 list_del_init(&first->list);
399 copy_siginfo(info, &first->info);
400 __sigqueue_free(first);
402 sigdelset(&list->signal, sig);
405 /* Ok, it wasn't in the queue. This must be
406 a fast-pathed signal or we must have been
407 out of queue space. So zero out the info.
409 sigdelset(&list->signal, sig);
410 info->si_signo = sig;
419 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
424 sig = next_signal(pending, mask);
426 if (current->notifier) {
427 if (sigismember(current->notifier_mask, sig)) {
428 if (!(current->notifier)(current->notifier_data)) {
429 clear_thread_flag(TIF_SIGPENDING);
435 if (!collect_signal(sig, pending, info))
445 * Dequeue a signal and return the element to the caller, which is
446 * expected to free it.
448 * All callers have to hold the siglock.
450 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
452 int signr = __dequeue_signal(&tsk->pending, mask, info);
454 signr = __dequeue_signal(&tsk->signal->shared_pending,
456 if (signr && unlikely(sig_kernel_stop(signr))) {
458 * Set a marker that we have dequeued a stop signal. Our
459 * caller might release the siglock and then the pending
460 * stop signal it is about to process is no longer in the
461 * pending bitmasks, but must still be cleared by a SIGCONT
462 * (and overruled by a SIGKILL). So those cases clear this
463 * shared flag after we've set it. Note that this flag may
464 * remain set after the signal we return is ignored or
465 * handled. That doesn't matter because its only purpose
466 * is to alert stop-signal processing code when another
467 * processor has come along and cleared the flag.
469 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
470 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
473 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
474 info->si_sys_private){
476 * Release the siglock to ensure proper locking order
477 * of timer locks outside of siglocks. Note, we leave
478 * irqs disabled here, since the posix-timers code is
479 * about to disable them again anyway.
481 spin_unlock(&tsk->sighand->siglock);
482 do_schedule_next_timer(info);
483 spin_lock(&tsk->sighand->siglock);
489 * Tell a process that it has a new active signal..
491 * NOTE! we rely on the previous spin_lock to
492 * lock interrupts for us! We can only be called with
493 * "siglock" held, and the local interrupt must
494 * have been disabled when that got acquired!
496 * No need to set need_resched since signal event passing
497 * goes through ->blocked
499 void signal_wake_up(struct task_struct *t, int resume)
503 set_tsk_thread_flag(t, TIF_SIGPENDING);
506 * For SIGKILL, we want to wake it up in the stopped/traced case.
507 * We don't check t->state here because there is a race with it
508 * executing another processor and just now entering stopped state.
509 * By using wake_up_state, we ensure the process will wake up and
510 * handle its death signal.
512 mask = TASK_INTERRUPTIBLE;
514 mask |= TASK_STOPPED | TASK_TRACED;
515 if (!wake_up_state(t, mask))
520 * Remove signals in mask from the pending set and queue.
521 * Returns 1 if any signals were found.
523 * All callers must be holding the siglock.
525 * This version takes a sigset mask and looks at all signals,
526 * not just those in the first mask word.
528 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
530 struct sigqueue *q, *n;
533 sigandsets(&m, mask, &s->signal);
534 if (sigisemptyset(&m))
537 signandsets(&s->signal, &s->signal, mask);
538 list_for_each_entry_safe(q, n, &s->list, list) {
539 if (sigismember(mask, q->info.si_signo)) {
540 list_del_init(&q->list);
547 * Remove signals in mask from the pending set and queue.
548 * Returns 1 if any signals were found.
550 * All callers must be holding the siglock.
552 static int rm_from_queue(unsigned long mask, struct sigpending *s)
554 struct sigqueue *q, *n;
556 if (!sigtestsetmask(&s->signal, mask))
559 sigdelsetmask(&s->signal, mask);
560 list_for_each_entry_safe(q, n, &s->list, list) {
561 if (q->info.si_signo < SIGRTMIN &&
562 (mask & sigmask(q->info.si_signo))) {
563 list_del_init(&q->list);
571 * Bad permissions for sending the signal
573 static int check_kill_permission(int sig, struct siginfo *info,
574 struct task_struct *t)
577 if (!valid_signal(sig))
580 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
581 && ((sig != SIGCONT) ||
582 (current->signal->session != t->signal->session))
583 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
584 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
585 && !capable(CAP_KILL))
588 error = security_task_kill(t, info, sig);
590 audit_signal_info(sig, t); /* Let audit system see the signal */
595 static void do_notify_parent_cldstop(struct task_struct *tsk,
600 * Handle magic process-wide effects of stop/continue signals.
601 * Unlike the signal actions, these happen immediately at signal-generation
602 * time regardless of blocking, ignoring, or handling. This does the
603 * actual continuing for SIGCONT, but not the actual stopping for stop
604 * signals. The process stop is done as a signal action for SIG_DFL.
606 static void handle_stop_signal(int sig, struct task_struct *p)
608 struct task_struct *t;
610 if (p->signal->flags & SIGNAL_GROUP_EXIT)
612 * The process is in the middle of dying already.
616 if (sig_kernel_stop(sig)) {
618 * This is a stop signal. Remove SIGCONT from all queues.
620 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
623 rm_from_queue(sigmask(SIGCONT), &t->pending);
626 } else if (sig == SIGCONT) {
628 * Remove all stop signals from all queues,
629 * and wake all threads.
631 if (unlikely(p->signal->group_stop_count > 0)) {
633 * There was a group stop in progress. We'll
634 * pretend it finished before we got here. We are
635 * obliged to report it to the parent: if the
636 * SIGSTOP happened "after" this SIGCONT, then it
637 * would have cleared this pending SIGCONT. If it
638 * happened "before" this SIGCONT, then the parent
639 * got the SIGCHLD about the stop finishing before
640 * the continue happened. We do the notification
641 * now, and it's as if the stop had finished and
642 * the SIGCHLD was pending on entry to this kill.
644 p->signal->group_stop_count = 0;
645 p->signal->flags = SIGNAL_STOP_CONTINUED;
646 spin_unlock(&p->sighand->siglock);
647 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_STOPPED);
648 spin_lock(&p->sighand->siglock);
650 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
654 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
657 * If there is a handler for SIGCONT, we must make
658 * sure that no thread returns to user mode before
659 * we post the signal, in case it was the only
660 * thread eligible to run the signal handler--then
661 * it must not do anything between resuming and
662 * running the handler. With the TIF_SIGPENDING
663 * flag set, the thread will pause and acquire the
664 * siglock that we hold now and until we've queued
665 * the pending signal.
667 * Wake up the stopped thread _after_ setting
670 state = TASK_STOPPED;
671 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
672 set_tsk_thread_flag(t, TIF_SIGPENDING);
673 state |= TASK_INTERRUPTIBLE;
675 wake_up_state(t, state);
680 if (p->signal->flags & SIGNAL_STOP_STOPPED) {
682 * We were in fact stopped, and are now continued.
683 * Notify the parent with CLD_CONTINUED.
685 p->signal->flags = SIGNAL_STOP_CONTINUED;
686 p->signal->group_exit_code = 0;
687 spin_unlock(&p->sighand->siglock);
688 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_CONTINUED);
689 spin_lock(&p->sighand->siglock);
692 * We are not stopped, but there could be a stop
693 * signal in the middle of being processed after
694 * being removed from the queue. Clear that too.
696 p->signal->flags = 0;
698 } else if (sig == SIGKILL) {
700 * Make sure that any pending stop signal already dequeued
701 * is undone by the wakeup for SIGKILL.
703 p->signal->flags = 0;
707 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
708 struct sigpending *signals)
710 struct sigqueue * q = NULL;
714 * fast-pathed signals for kernel-internal things like SIGSTOP
717 if (info == SEND_SIG_FORCED)
720 /* Real-time signals must be queued if sent by sigqueue, or
721 some other real-time mechanism. It is implementation
722 defined whether kill() does so. We attempt to do so, on
723 the principle of least surprise, but since kill is not
724 allowed to fail with EAGAIN when low on memory we just
725 make sure at least one signal gets delivered and don't
726 pass on the info struct. */
728 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
729 (is_si_special(info) ||
730 info->si_code >= 0)));
732 list_add_tail(&q->list, &signals->list);
733 switch ((unsigned long) info) {
734 case (unsigned long) SEND_SIG_NOINFO:
735 q->info.si_signo = sig;
736 q->info.si_errno = 0;
737 q->info.si_code = SI_USER;
738 q->info.si_pid = current->pid;
739 q->info.si_uid = current->uid;
741 case (unsigned long) SEND_SIG_PRIV:
742 q->info.si_signo = sig;
743 q->info.si_errno = 0;
744 q->info.si_code = SI_KERNEL;
749 copy_siginfo(&q->info, info);
752 } else if (!is_si_special(info)) {
753 if (sig >= SIGRTMIN && info->si_code != SI_USER)
755 * Queue overflow, abort. We may abort if the signal was rt
756 * and sent by user using something other than kill().
762 sigaddset(&signals->signal, sig);
766 #define LEGACY_QUEUE(sigptr, sig) \
767 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
771 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
775 if (!irqs_disabled())
777 assert_spin_locked(&t->sighand->siglock);
779 /* Short-circuit ignored signals. */
780 if (sig_ignored(t, sig))
783 /* Support queueing exactly one non-rt signal, so that we
784 can get more detailed information about the cause of
786 if (LEGACY_QUEUE(&t->pending, sig))
789 ret = send_signal(sig, info, t, &t->pending);
790 if (!ret && !sigismember(&t->blocked, sig))
791 signal_wake_up(t, sig == SIGKILL);
797 * Force a signal that the process can't ignore: if necessary
798 * we unblock the signal and change any SIG_IGN to SIG_DFL.
802 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
804 unsigned long int flags;
807 spin_lock_irqsave(&t->sighand->siglock, flags);
808 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
809 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
811 if (sigismember(&t->blocked, sig)) {
812 sigdelset(&t->blocked, sig);
814 recalc_sigpending_tsk(t);
815 ret = specific_send_sig_info(sig, info, t);
816 spin_unlock_irqrestore(&t->sighand->siglock, flags);
822 force_sig_specific(int sig, struct task_struct *t)
824 force_sig_info(sig, SEND_SIG_FORCED, t);
828 * Test if P wants to take SIG. After we've checked all threads with this,
829 * it's equivalent to finding no threads not blocking SIG. Any threads not
830 * blocking SIG were ruled out because they are not running and already
831 * have pending signals. Such threads will dequeue from the shared queue
832 * as soon as they're available, so putting the signal on the shared queue
833 * will be equivalent to sending it to one such thread.
835 static inline int wants_signal(int sig, struct task_struct *p)
837 if (sigismember(&p->blocked, sig))
839 if (p->flags & PF_EXITING)
843 if (p->state & (TASK_STOPPED | TASK_TRACED))
845 return task_curr(p) || !signal_pending(p);
849 __group_complete_signal(int sig, struct task_struct *p)
851 struct task_struct *t;
854 * Now find a thread we can wake up to take the signal off the queue.
856 * If the main thread wants the signal, it gets first crack.
857 * Probably the least surprising to the average bear.
859 if (wants_signal(sig, p))
861 else if (thread_group_empty(p))
863 * There is just one thread and it does not need to be woken.
864 * It will dequeue unblocked signals before it runs again.
869 * Otherwise try to find a suitable thread.
871 t = p->signal->curr_target;
873 /* restart balancing at this thread */
874 t = p->signal->curr_target = p;
875 BUG_ON(t->tgid != p->tgid);
877 while (!wants_signal(sig, t)) {
879 if (t == p->signal->curr_target)
881 * No thread needs to be woken.
882 * Any eligible threads will see
883 * the signal in the queue soon.
887 p->signal->curr_target = t;
891 * Found a killable thread. If the signal will be fatal,
892 * then start taking the whole group down immediately.
894 if (sig_fatal(p, sig) && !(p->signal->flags & SIGNAL_GROUP_EXIT) &&
895 !sigismember(&t->real_blocked, sig) &&
896 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
898 * This signal will be fatal to the whole group.
900 if (!sig_kernel_coredump(sig)) {
902 * Start a group exit and wake everybody up.
903 * This way we don't have other threads
904 * running and doing things after a slower
905 * thread has the fatal signal pending.
907 p->signal->flags = SIGNAL_GROUP_EXIT;
908 p->signal->group_exit_code = sig;
909 p->signal->group_stop_count = 0;
912 sigaddset(&t->pending.signal, SIGKILL);
913 signal_wake_up(t, 1);
920 * There will be a core dump. We make all threads other
921 * than the chosen one go into a group stop so that nothing
922 * happens until it gets scheduled, takes the signal off
923 * the shared queue, and does the core dump. This is a
924 * little more complicated than strictly necessary, but it
925 * keeps the signal state that winds up in the core dump
926 * unchanged from the death state, e.g. which thread had
927 * the core-dump signal unblocked.
929 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
930 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
931 p->signal->group_stop_count = 0;
932 p->signal->group_exit_task = t;
935 p->signal->group_stop_count++;
936 signal_wake_up(t, 0);
939 wake_up_process(p->signal->group_exit_task);
944 * The signal is already in the shared-pending queue.
945 * Tell the chosen thread to wake up and dequeue it.
947 signal_wake_up(t, sig == SIGKILL);
952 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
956 assert_spin_locked(&p->sighand->siglock);
957 handle_stop_signal(sig, p);
959 /* Short-circuit ignored signals. */
960 if (sig_ignored(p, sig))
963 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
964 /* This is a non-RT signal and we already have one queued. */
968 * Put this signal on the shared-pending queue, or fail with EAGAIN.
969 * We always use the shared queue for process-wide signals,
970 * to avoid several races.
972 ret = send_signal(sig, info, p, &p->signal->shared_pending);
976 __group_complete_signal(sig, p);
981 * Nuke all other threads in the group.
983 void zap_other_threads(struct task_struct *p)
985 struct task_struct *t;
987 p->signal->flags = SIGNAL_GROUP_EXIT;
988 p->signal->group_stop_count = 0;
990 if (thread_group_empty(p))
993 for (t = next_thread(p); t != p; t = next_thread(t)) {
995 * Don't bother with already dead threads
1001 * We don't want to notify the parent, since we are
1002 * killed as part of a thread group due to another
1003 * thread doing an execve() or similar. So set the
1004 * exit signal to -1 to allow immediate reaping of
1005 * the process. But don't detach the thread group
1008 if (t != p->group_leader)
1009 t->exit_signal = -1;
1011 /* SIGKILL will be handled before any pending SIGSTOP */
1012 sigaddset(&t->pending.signal, SIGKILL);
1013 signal_wake_up(t, 1);
1018 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
1020 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1022 struct sighand_struct *sighand;
1025 sighand = rcu_dereference(tsk->sighand);
1026 if (unlikely(sighand == NULL))
1029 spin_lock_irqsave(&sighand->siglock, *flags);
1030 if (likely(sighand == tsk->sighand))
1032 spin_unlock_irqrestore(&sighand->siglock, *flags);
1038 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1040 unsigned long flags;
1043 ret = check_kill_permission(sig, info, p);
1047 if (lock_task_sighand(p, &flags)) {
1048 ret = __group_send_sig_info(sig, info, p);
1049 unlock_task_sighand(p, &flags);
1057 * kill_pg_info() sends a signal to a process group: this is what the tty
1058 * control characters do (^C, ^Z etc)
1061 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1063 struct task_struct *p = NULL;
1064 int retval, success;
1071 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1072 int err = group_send_sig_info(sig, info, p);
1075 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1076 return success ? 0 : retval;
1080 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1084 read_lock(&tasklist_lock);
1085 retval = __kill_pg_info(sig, info, pgrp);
1086 read_unlock(&tasklist_lock);
1092 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1095 int acquired_tasklist_lock = 0;
1096 struct task_struct *p;
1099 if (unlikely(sig_needs_tasklist(sig))) {
1100 read_lock(&tasklist_lock);
1101 acquired_tasklist_lock = 1;
1103 p = find_task_by_pid(pid);
1106 error = group_send_sig_info(sig, info, p);
1107 if (unlikely(acquired_tasklist_lock))
1108 read_unlock(&tasklist_lock);
1113 /* like kill_proc_info(), but doesn't use uid/euid of "current" */
1114 int kill_proc_info_as_uid(int sig, struct siginfo *info, pid_t pid,
1115 uid_t uid, uid_t euid)
1118 struct task_struct *p;
1120 if (!valid_signal(sig))
1123 read_lock(&tasklist_lock);
1124 p = find_task_by_pid(pid);
1129 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
1130 && (euid != p->suid) && (euid != p->uid)
1131 && (uid != p->suid) && (uid != p->uid)) {
1135 if (sig && p->sighand) {
1136 unsigned long flags;
1137 spin_lock_irqsave(&p->sighand->siglock, flags);
1138 ret = __group_send_sig_info(sig, info, p);
1139 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1142 read_unlock(&tasklist_lock);
1145 EXPORT_SYMBOL_GPL(kill_proc_info_as_uid);
1148 * kill_something_info() interprets pid in interesting ways just like kill(2).
1150 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1151 * is probably wrong. Should make it like BSD or SYSV.
1154 static int kill_something_info(int sig, struct siginfo *info, int pid)
1157 return kill_pg_info(sig, info, process_group(current));
1158 } else if (pid == -1) {
1159 int retval = 0, count = 0;
1160 struct task_struct * p;
1162 read_lock(&tasklist_lock);
1163 for_each_process(p) {
1164 if (p->pid > 1 && p->tgid != current->tgid) {
1165 int err = group_send_sig_info(sig, info, p);
1171 read_unlock(&tasklist_lock);
1172 return count ? retval : -ESRCH;
1173 } else if (pid < 0) {
1174 return kill_pg_info(sig, info, -pid);
1176 return kill_proc_info(sig, info, pid);
1181 * These are for backward compatibility with the rest of the kernel source.
1185 * These two are the most common entry points. They send a signal
1186 * just to the specific thread.
1189 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1192 unsigned long flags;
1195 * Make sure legacy kernel users don't send in bad values
1196 * (normal paths check this in check_kill_permission).
1198 if (!valid_signal(sig))
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);
1215 #define __si_special(priv) \
1216 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1219 send_sig(int sig, struct task_struct *p, int priv)
1221 return send_sig_info(sig, __si_special(priv), p);
1225 * This is the entry point for "process-wide" signals.
1226 * They will go to an appropriate thread in the thread group.
1229 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1232 read_lock(&tasklist_lock);
1233 ret = group_send_sig_info(sig, info, p);
1234 read_unlock(&tasklist_lock);
1239 force_sig(int sig, struct task_struct *p)
1241 force_sig_info(sig, SEND_SIG_PRIV, p);
1245 * When things go south during signal handling, we
1246 * will force a SIGSEGV. And if the signal that caused
1247 * the problem was already a SIGSEGV, we'll want to
1248 * make sure we don't even try to deliver the signal..
1251 force_sigsegv(int sig, struct task_struct *p)
1253 if (sig == SIGSEGV) {
1254 unsigned long flags;
1255 spin_lock_irqsave(&p->sighand->siglock, flags);
1256 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1257 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1259 force_sig(SIGSEGV, p);
1264 kill_pg(pid_t pgrp, int sig, int priv)
1266 return kill_pg_info(sig, __si_special(priv), pgrp);
1270 kill_proc(pid_t pid, int sig, int priv)
1272 return kill_proc_info(sig, __si_special(priv), pid);
1276 * These functions support sending signals using preallocated sigqueue
1277 * structures. This is needed "because realtime applications cannot
1278 * afford to lose notifications of asynchronous events, like timer
1279 * expirations or I/O completions". In the case of Posix Timers
1280 * we allocate the sigqueue structure from the timer_create. If this
1281 * allocation fails we are able to report the failure to the application
1282 * with an EAGAIN error.
1285 struct sigqueue *sigqueue_alloc(void)
1289 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1290 q->flags |= SIGQUEUE_PREALLOC;
1294 void sigqueue_free(struct sigqueue *q)
1296 unsigned long flags;
1297 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1299 * If the signal is still pending remove it from the
1302 if (unlikely(!list_empty(&q->list))) {
1303 spinlock_t *lock = ¤t->sighand->siglock;
1304 read_lock(&tasklist_lock);
1305 spin_lock_irqsave(lock, flags);
1306 if (!list_empty(&q->list))
1307 list_del_init(&q->list);
1308 spin_unlock_irqrestore(lock, flags);
1309 read_unlock(&tasklist_lock);
1311 q->flags &= ~SIGQUEUE_PREALLOC;
1316 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1318 unsigned long flags;
1320 struct sighand_struct *sh;
1322 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1325 * The rcu based delayed sighand destroy makes it possible to
1326 * run this without tasklist lock held. The task struct itself
1327 * cannot go away as create_timer did get_task_struct().
1329 * We return -1, when the task is marked exiting, so
1330 * posix_timer_event can redirect it to the group leader
1334 if (unlikely(p->flags & PF_EXITING)) {
1340 sh = rcu_dereference(p->sighand);
1342 spin_lock_irqsave(&sh->siglock, flags);
1343 if (p->sighand != sh) {
1344 /* We raced with exec() in a multithreaded process... */
1345 spin_unlock_irqrestore(&sh->siglock, flags);
1350 * We do the check here again to handle the following scenario:
1355 * interrupt exit code running
1357 * lock sighand->siglock
1358 * unlock sighand->siglock
1360 * add(tsk->pending) flush_sigqueue(tsk->pending)
1364 if (unlikely(p->flags & PF_EXITING)) {
1369 if (unlikely(!list_empty(&q->list))) {
1371 * If an SI_TIMER entry is already queue just increment
1372 * the overrun count.
1374 if (q->info.si_code != SI_TIMER)
1376 q->info.si_overrun++;
1379 /* Short-circuit ignored signals. */
1380 if (sig_ignored(p, sig)) {
1385 list_add_tail(&q->list, &p->pending.list);
1386 sigaddset(&p->pending.signal, sig);
1387 if (!sigismember(&p->blocked, sig))
1388 signal_wake_up(p, sig == SIGKILL);
1391 spin_unlock_irqrestore(&sh->siglock, flags);
1399 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1401 unsigned long flags;
1404 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1406 read_lock(&tasklist_lock);
1407 /* Since it_lock is held, p->sighand cannot be NULL. */
1408 spin_lock_irqsave(&p->sighand->siglock, flags);
1409 handle_stop_signal(sig, p);
1411 /* Short-circuit ignored signals. */
1412 if (sig_ignored(p, sig)) {
1417 if (unlikely(!list_empty(&q->list))) {
1419 * If an SI_TIMER entry is already queue just increment
1420 * the overrun count. Other uses should not try to
1421 * send the signal multiple times.
1423 if (q->info.si_code != SI_TIMER)
1425 q->info.si_overrun++;
1430 * Put this signal on the shared-pending queue.
1431 * We always use the shared queue for process-wide signals,
1432 * to avoid several races.
1434 list_add_tail(&q->list, &p->signal->shared_pending.list);
1435 sigaddset(&p->signal->shared_pending.signal, sig);
1437 __group_complete_signal(sig, p);
1439 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1440 read_unlock(&tasklist_lock);
1445 * Wake up any threads in the parent blocked in wait* syscalls.
1447 static inline void __wake_up_parent(struct task_struct *p,
1448 struct task_struct *parent)
1450 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1454 * Let a parent know about the death of a child.
1455 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1458 void do_notify_parent(struct task_struct *tsk, int sig)
1460 struct siginfo info;
1461 unsigned long flags;
1462 struct sighand_struct *psig;
1466 /* do_notify_parent_cldstop should have been called instead. */
1467 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1469 BUG_ON(!tsk->ptrace &&
1470 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1472 info.si_signo = sig;
1474 info.si_pid = tsk->pid;
1475 info.si_uid = tsk->uid;
1477 /* FIXME: find out whether or not this is supposed to be c*time. */
1478 info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
1479 tsk->signal->utime));
1480 info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
1481 tsk->signal->stime));
1483 info.si_status = tsk->exit_code & 0x7f;
1484 if (tsk->exit_code & 0x80)
1485 info.si_code = CLD_DUMPED;
1486 else if (tsk->exit_code & 0x7f)
1487 info.si_code = CLD_KILLED;
1489 info.si_code = CLD_EXITED;
1490 info.si_status = tsk->exit_code >> 8;
1493 psig = tsk->parent->sighand;
1494 spin_lock_irqsave(&psig->siglock, flags);
1495 if (!tsk->ptrace && sig == SIGCHLD &&
1496 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1497 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1499 * We are exiting and our parent doesn't care. POSIX.1
1500 * defines special semantics for setting SIGCHLD to SIG_IGN
1501 * or setting the SA_NOCLDWAIT flag: we should be reaped
1502 * automatically and not left for our parent's wait4 call.
1503 * Rather than having the parent do it as a magic kind of
1504 * signal handler, we just set this to tell do_exit that we
1505 * can be cleaned up without becoming a zombie. Note that
1506 * we still call __wake_up_parent in this case, because a
1507 * blocked sys_wait4 might now return -ECHILD.
1509 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1510 * is implementation-defined: we do (if you don't want
1511 * it, just use SIG_IGN instead).
1513 tsk->exit_signal = -1;
1514 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1517 if (valid_signal(sig) && sig > 0)
1518 __group_send_sig_info(sig, &info, tsk->parent);
1519 __wake_up_parent(tsk, tsk->parent);
1520 spin_unlock_irqrestore(&psig->siglock, flags);
1523 static void do_notify_parent_cldstop(struct task_struct *tsk, int to_self, int why)
1525 struct siginfo info;
1526 unsigned long flags;
1527 struct task_struct *parent;
1528 struct sighand_struct *sighand;
1531 parent = tsk->parent;
1533 tsk = tsk->group_leader;
1534 parent = tsk->real_parent;
1537 info.si_signo = SIGCHLD;
1539 info.si_pid = tsk->pid;
1540 info.si_uid = tsk->uid;
1542 /* FIXME: find out whether or not this is supposed to be c*time. */
1543 info.si_utime = cputime_to_jiffies(tsk->utime);
1544 info.si_stime = cputime_to_jiffies(tsk->stime);
1549 info.si_status = SIGCONT;
1552 info.si_status = tsk->signal->group_exit_code & 0x7f;
1555 info.si_status = tsk->exit_code & 0x7f;
1561 sighand = parent->sighand;
1562 spin_lock_irqsave(&sighand->siglock, flags);
1563 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1564 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1565 __group_send_sig_info(SIGCHLD, &info, parent);
1567 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1569 __wake_up_parent(tsk, parent);
1570 spin_unlock_irqrestore(&sighand->siglock, flags);
1574 * This must be called with current->sighand->siglock held.
1576 * This should be the path for all ptrace stops.
1577 * We always set current->last_siginfo while stopped here.
1578 * That makes it a way to test a stopped process for
1579 * being ptrace-stopped vs being job-control-stopped.
1581 * If we actually decide not to stop at all because the tracer is gone,
1582 * we leave nostop_code in current->exit_code.
1584 static void ptrace_stop(int exit_code, int nostop_code, siginfo_t *info)
1587 * If there is a group stop in progress,
1588 * we must participate in the bookkeeping.
1590 if (current->signal->group_stop_count > 0)
1591 --current->signal->group_stop_count;
1593 current->last_siginfo = info;
1594 current->exit_code = exit_code;
1596 /* Let the debugger run. */
1597 set_current_state(TASK_TRACED);
1598 spin_unlock_irq(¤t->sighand->siglock);
1599 read_lock(&tasklist_lock);
1600 if (likely(current->ptrace & PT_PTRACED) &&
1601 likely(current->parent != current->real_parent ||
1602 !(current->ptrace & PT_ATTACHED)) &&
1603 (likely(current->parent->signal != current->signal) ||
1604 !unlikely(current->signal->flags & SIGNAL_GROUP_EXIT))) {
1605 do_notify_parent_cldstop(current, 1, CLD_TRAPPED);
1606 read_unlock(&tasklist_lock);
1610 * By the time we got the lock, our tracer went away.
1613 read_unlock(&tasklist_lock);
1614 set_current_state(TASK_RUNNING);
1615 current->exit_code = nostop_code;
1619 * We are back. Now reacquire the siglock before touching
1620 * last_siginfo, so that we are sure to have synchronized with
1621 * any signal-sending on another CPU that wants to examine it.
1623 spin_lock_irq(¤t->sighand->siglock);
1624 current->last_siginfo = NULL;
1627 * Queued signals ignored us while we were stopped for tracing.
1628 * So check for any that we should take before resuming user mode.
1630 recalc_sigpending();
1633 void ptrace_notify(int exit_code)
1637 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1639 memset(&info, 0, sizeof info);
1640 info.si_signo = SIGTRAP;
1641 info.si_code = exit_code;
1642 info.si_pid = current->pid;
1643 info.si_uid = current->uid;
1645 /* Let the debugger run. */
1646 spin_lock_irq(¤t->sighand->siglock);
1647 ptrace_stop(exit_code, 0, &info);
1648 spin_unlock_irq(¤t->sighand->siglock);
1652 finish_stop(int stop_count)
1657 * If there are no other threads in the group, or if there is
1658 * a group stop in progress and we are the last to stop,
1659 * report to the parent. When ptraced, every thread reports itself.
1661 if (stop_count < 0 || (current->ptrace & PT_PTRACED))
1663 else if (stop_count == 0)
1668 read_lock(&tasklist_lock);
1669 do_notify_parent_cldstop(current, to_self, CLD_STOPPED);
1670 read_unlock(&tasklist_lock);
1675 * Now we don't run again until continued.
1677 current->exit_code = 0;
1681 * This performs the stopping for SIGSTOP and other stop signals.
1682 * We have to stop all threads in the thread group.
1683 * Returns nonzero if we've actually stopped and released the siglock.
1684 * Returns zero if we didn't stop and still hold the siglock.
1687 do_signal_stop(int signr)
1689 struct signal_struct *sig = current->signal;
1690 struct sighand_struct *sighand = current->sighand;
1691 int stop_count = -1;
1693 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED))
1696 if (sig->group_stop_count > 0) {
1698 * There is a group stop in progress. We don't need to
1699 * start another one.
1701 signr = sig->group_exit_code;
1702 stop_count = --sig->group_stop_count;
1703 current->exit_code = signr;
1704 set_current_state(TASK_STOPPED);
1705 if (stop_count == 0)
1706 sig->flags = SIGNAL_STOP_STOPPED;
1707 spin_unlock_irq(&sighand->siglock);
1709 else if (thread_group_empty(current)) {
1711 * Lock must be held through transition to stopped state.
1713 current->exit_code = current->signal->group_exit_code = signr;
1714 set_current_state(TASK_STOPPED);
1715 sig->flags = SIGNAL_STOP_STOPPED;
1716 spin_unlock_irq(&sighand->siglock);
1720 * There is no group stop already in progress.
1721 * We must initiate one now, but that requires
1722 * dropping siglock to get both the tasklist lock
1723 * and siglock again in the proper order. Note that
1724 * this allows an intervening SIGCONT to be posted.
1725 * We need to check for that and bail out if necessary.
1727 struct task_struct *t;
1729 spin_unlock_irq(&sighand->siglock);
1731 /* signals can be posted during this window */
1733 read_lock(&tasklist_lock);
1734 spin_lock_irq(&sighand->siglock);
1736 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED)) {
1738 * Another stop or continue happened while we
1739 * didn't have the lock. We can just swallow this
1740 * signal now. If we raced with a SIGCONT, that
1741 * should have just cleared it now. If we raced
1742 * with another processor delivering a stop signal,
1743 * then the SIGCONT that wakes us up should clear it.
1745 read_unlock(&tasklist_lock);
1749 if (sig->group_stop_count == 0) {
1750 sig->group_exit_code = signr;
1752 for (t = next_thread(current); t != current;
1755 * Setting state to TASK_STOPPED for a group
1756 * stop is always done with the siglock held,
1757 * so this check has no races.
1759 if (!t->exit_state &&
1760 !(t->state & (TASK_STOPPED|TASK_TRACED))) {
1762 signal_wake_up(t, 0);
1764 sig->group_stop_count = stop_count;
1767 /* A race with another thread while unlocked. */
1768 signr = sig->group_exit_code;
1769 stop_count = --sig->group_stop_count;
1772 current->exit_code = signr;
1773 set_current_state(TASK_STOPPED);
1774 if (stop_count == 0)
1775 sig->flags = SIGNAL_STOP_STOPPED;
1777 spin_unlock_irq(&sighand->siglock);
1778 read_unlock(&tasklist_lock);
1781 finish_stop(stop_count);
1786 * Do appropriate magic when group_stop_count > 0.
1787 * We return nonzero if we stopped, after releasing the siglock.
1788 * We return zero if we still hold the siglock and should look
1789 * for another signal without checking group_stop_count again.
1791 static int handle_group_stop(void)
1795 if (current->signal->group_exit_task == current) {
1797 * Group stop is so we can do a core dump,
1798 * We are the initiating thread, so get on with it.
1800 current->signal->group_exit_task = NULL;
1804 if (current->signal->flags & SIGNAL_GROUP_EXIT)
1806 * Group stop is so another thread can do a core dump,
1807 * or else we are racing against a death signal.
1808 * Just punt the stop so we can get the next signal.
1813 * There is a group stop in progress. We stop
1814 * without any associated signal being in our queue.
1816 stop_count = --current->signal->group_stop_count;
1817 if (stop_count == 0)
1818 current->signal->flags = SIGNAL_STOP_STOPPED;
1819 current->exit_code = current->signal->group_exit_code;
1820 set_current_state(TASK_STOPPED);
1821 spin_unlock_irq(¤t->sighand->siglock);
1822 finish_stop(stop_count);
1826 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1827 struct pt_regs *regs, void *cookie)
1829 sigset_t *mask = ¤t->blocked;
1835 spin_lock_irq(¤t->sighand->siglock);
1837 struct k_sigaction *ka;
1839 if (unlikely(current->signal->group_stop_count > 0) &&
1840 handle_group_stop())
1843 signr = dequeue_signal(current, mask, info);
1846 break; /* will return 0 */
1848 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1849 ptrace_signal_deliver(regs, cookie);
1851 /* Let the debugger run. */
1852 ptrace_stop(signr, signr, info);
1854 /* We're back. Did the debugger cancel the sig or group_exit? */
1855 signr = current->exit_code;
1856 if (signr == 0 || current->signal->flags & SIGNAL_GROUP_EXIT)
1859 current->exit_code = 0;
1861 /* Update the siginfo structure if the signal has
1862 changed. If the debugger wanted something
1863 specific in the siginfo structure then it should
1864 have updated *info via PTRACE_SETSIGINFO. */
1865 if (signr != info->si_signo) {
1866 info->si_signo = signr;
1868 info->si_code = SI_USER;
1869 info->si_pid = current->parent->pid;
1870 info->si_uid = current->parent->uid;
1873 /* If the (new) signal is now blocked, requeue it. */
1874 if (sigismember(¤t->blocked, signr)) {
1875 specific_send_sig_info(signr, info, current);
1880 ka = ¤t->sighand->action[signr-1];
1881 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1883 if (ka->sa.sa_handler != SIG_DFL) {
1884 /* Run the handler. */
1887 if (ka->sa.sa_flags & SA_ONESHOT)
1888 ka->sa.sa_handler = SIG_DFL;
1890 break; /* will return non-zero "signr" value */
1894 * Now we are doing the default action for this signal.
1896 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1899 /* Init gets no signals it doesn't want. */
1900 if (current == child_reaper)
1903 if (sig_kernel_stop(signr)) {
1905 * The default action is to stop all threads in
1906 * the thread group. The job control signals
1907 * do nothing in an orphaned pgrp, but SIGSTOP
1908 * always works. Note that siglock needs to be
1909 * dropped during the call to is_orphaned_pgrp()
1910 * because of lock ordering with tasklist_lock.
1911 * This allows an intervening SIGCONT to be posted.
1912 * We need to check for that and bail out if necessary.
1914 if (signr != SIGSTOP) {
1915 spin_unlock_irq(¤t->sighand->siglock);
1917 /* signals can be posted during this window */
1919 if (is_orphaned_pgrp(process_group(current)))
1922 spin_lock_irq(¤t->sighand->siglock);
1925 if (likely(do_signal_stop(signr))) {
1926 /* It released the siglock. */
1931 * We didn't actually stop, due to a race
1932 * with SIGCONT or something like that.
1937 spin_unlock_irq(¤t->sighand->siglock);
1940 * Anything else is fatal, maybe with a core dump.
1942 current->flags |= PF_SIGNALED;
1943 if (sig_kernel_coredump(signr)) {
1945 * If it was able to dump core, this kills all
1946 * other threads in the group and synchronizes with
1947 * their demise. If we lost the race with another
1948 * thread getting here, it set group_exit_code
1949 * first and our do_group_exit call below will use
1950 * that value and ignore the one we pass it.
1952 do_coredump((long)signr, signr, regs);
1956 * Death signals, no core dump.
1958 do_group_exit(signr);
1961 spin_unlock_irq(¤t->sighand->siglock);
1965 EXPORT_SYMBOL(recalc_sigpending);
1966 EXPORT_SYMBOL_GPL(dequeue_signal);
1967 EXPORT_SYMBOL(flush_signals);
1968 EXPORT_SYMBOL(force_sig);
1969 EXPORT_SYMBOL(kill_pg);
1970 EXPORT_SYMBOL(kill_proc);
1971 EXPORT_SYMBOL(ptrace_notify);
1972 EXPORT_SYMBOL(send_sig);
1973 EXPORT_SYMBOL(send_sig_info);
1974 EXPORT_SYMBOL(sigprocmask);
1975 EXPORT_SYMBOL(block_all_signals);
1976 EXPORT_SYMBOL(unblock_all_signals);
1980 * System call entry points.
1983 asmlinkage long sys_restart_syscall(void)
1985 struct restart_block *restart = ¤t_thread_info()->restart_block;
1986 return restart->fn(restart);
1989 long do_no_restart_syscall(struct restart_block *param)
1995 * We don't need to get the kernel lock - this is all local to this
1996 * particular thread.. (and that's good, because this is _heavily_
1997 * used by various programs)
2001 * This is also useful for kernel threads that want to temporarily
2002 * (or permanently) block certain signals.
2004 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2005 * interface happily blocks "unblockable" signals like SIGKILL
2008 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2012 spin_lock_irq(¤t->sighand->siglock);
2014 *oldset = current->blocked;
2019 sigorsets(¤t->blocked, ¤t->blocked, set);
2022 signandsets(¤t->blocked, ¤t->blocked, set);
2025 current->blocked = *set;
2030 recalc_sigpending();
2031 spin_unlock_irq(¤t->sighand->siglock);
2037 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2039 int error = -EINVAL;
2040 sigset_t old_set, new_set;
2042 /* XXX: Don't preclude handling different sized sigset_t's. */
2043 if (sigsetsize != sizeof(sigset_t))
2048 if (copy_from_user(&new_set, set, sizeof(*set)))
2050 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2052 error = sigprocmask(how, &new_set, &old_set);
2058 spin_lock_irq(¤t->sighand->siglock);
2059 old_set = current->blocked;
2060 spin_unlock_irq(¤t->sighand->siglock);
2064 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2072 long do_sigpending(void __user *set, unsigned long sigsetsize)
2074 long error = -EINVAL;
2077 if (sigsetsize > sizeof(sigset_t))
2080 spin_lock_irq(¤t->sighand->siglock);
2081 sigorsets(&pending, ¤t->pending.signal,
2082 ¤t->signal->shared_pending.signal);
2083 spin_unlock_irq(¤t->sighand->siglock);
2085 /* Outside the lock because only this thread touches it. */
2086 sigandsets(&pending, ¤t->blocked, &pending);
2089 if (!copy_to_user(set, &pending, sigsetsize))
2097 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2099 return do_sigpending(set, sigsetsize);
2102 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2104 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2108 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2110 if (from->si_code < 0)
2111 return __copy_to_user(to, from, sizeof(siginfo_t))
2114 * If you change siginfo_t structure, please be sure
2115 * this code is fixed accordingly.
2116 * It should never copy any pad contained in the structure
2117 * to avoid security leaks, but must copy the generic
2118 * 3 ints plus the relevant union member.
2120 err = __put_user(from->si_signo, &to->si_signo);
2121 err |= __put_user(from->si_errno, &to->si_errno);
2122 err |= __put_user((short)from->si_code, &to->si_code);
2123 switch (from->si_code & __SI_MASK) {
2125 err |= __put_user(from->si_pid, &to->si_pid);
2126 err |= __put_user(from->si_uid, &to->si_uid);
2129 err |= __put_user(from->si_tid, &to->si_tid);
2130 err |= __put_user(from->si_overrun, &to->si_overrun);
2131 err |= __put_user(from->si_ptr, &to->si_ptr);
2134 err |= __put_user(from->si_band, &to->si_band);
2135 err |= __put_user(from->si_fd, &to->si_fd);
2138 err |= __put_user(from->si_addr, &to->si_addr);
2139 #ifdef __ARCH_SI_TRAPNO
2140 err |= __put_user(from->si_trapno, &to->si_trapno);
2144 err |= __put_user(from->si_pid, &to->si_pid);
2145 err |= __put_user(from->si_uid, &to->si_uid);
2146 err |= __put_user(from->si_status, &to->si_status);
2147 err |= __put_user(from->si_utime, &to->si_utime);
2148 err |= __put_user(from->si_stime, &to->si_stime);
2150 case __SI_RT: /* This is not generated by the kernel as of now. */
2151 case __SI_MESGQ: /* But this is */
2152 err |= __put_user(from->si_pid, &to->si_pid);
2153 err |= __put_user(from->si_uid, &to->si_uid);
2154 err |= __put_user(from->si_ptr, &to->si_ptr);
2156 default: /* this is just in case for now ... */
2157 err |= __put_user(from->si_pid, &to->si_pid);
2158 err |= __put_user(from->si_uid, &to->si_uid);
2167 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2168 siginfo_t __user *uinfo,
2169 const struct timespec __user *uts,
2178 /* XXX: Don't preclude handling different sized sigset_t's. */
2179 if (sigsetsize != sizeof(sigset_t))
2182 if (copy_from_user(&these, uthese, sizeof(these)))
2186 * Invert the set of allowed signals to get those we
2189 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2193 if (copy_from_user(&ts, uts, sizeof(ts)))
2195 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2200 spin_lock_irq(¤t->sighand->siglock);
2201 sig = dequeue_signal(current, &these, &info);
2203 timeout = MAX_SCHEDULE_TIMEOUT;
2205 timeout = (timespec_to_jiffies(&ts)
2206 + (ts.tv_sec || ts.tv_nsec));
2209 /* None ready -- temporarily unblock those we're
2210 * interested while we are sleeping in so that we'll
2211 * be awakened when they arrive. */
2212 current->real_blocked = current->blocked;
2213 sigandsets(¤t->blocked, ¤t->blocked, &these);
2214 recalc_sigpending();
2215 spin_unlock_irq(¤t->sighand->siglock);
2217 timeout = schedule_timeout_interruptible(timeout);
2219 spin_lock_irq(¤t->sighand->siglock);
2220 sig = dequeue_signal(current, &these, &info);
2221 current->blocked = current->real_blocked;
2222 siginitset(¤t->real_blocked, 0);
2223 recalc_sigpending();
2226 spin_unlock_irq(¤t->sighand->siglock);
2231 if (copy_siginfo_to_user(uinfo, &info))
2244 sys_kill(int pid, int sig)
2246 struct siginfo info;
2248 info.si_signo = sig;
2250 info.si_code = SI_USER;
2251 info.si_pid = current->tgid;
2252 info.si_uid = current->uid;
2254 return kill_something_info(sig, &info, pid);
2257 static int do_tkill(int tgid, int pid, int sig)
2260 struct siginfo info;
2261 struct task_struct *p;
2264 info.si_signo = sig;
2266 info.si_code = SI_TKILL;
2267 info.si_pid = current->tgid;
2268 info.si_uid = current->uid;
2270 read_lock(&tasklist_lock);
2271 p = find_task_by_pid(pid);
2272 if (p && (tgid <= 0 || p->tgid == tgid)) {
2273 error = check_kill_permission(sig, &info, p);
2275 * The null signal is a permissions and process existence
2276 * probe. No signal is actually delivered.
2278 if (!error && sig && p->sighand) {
2279 spin_lock_irq(&p->sighand->siglock);
2280 handle_stop_signal(sig, p);
2281 error = specific_send_sig_info(sig, &info, p);
2282 spin_unlock_irq(&p->sighand->siglock);
2285 read_unlock(&tasklist_lock);
2291 * sys_tgkill - send signal to one specific thread
2292 * @tgid: the thread group ID of the thread
2293 * @pid: the PID of the thread
2294 * @sig: signal to be sent
2296 * This syscall also checks the tgid and returns -ESRCH even if the PID
2297 * exists but it's not belonging to the target process anymore. This
2298 * method solves the problem of threads exiting and PIDs getting reused.
2300 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2302 /* This is only valid for single tasks */
2303 if (pid <= 0 || tgid <= 0)
2306 return do_tkill(tgid, pid, sig);
2310 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2313 sys_tkill(int pid, int sig)
2315 /* This is only valid for single tasks */
2319 return do_tkill(0, pid, sig);
2323 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2327 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2330 /* Not even root can pretend to send signals from the kernel.
2331 Nor can they impersonate a kill(), which adds source info. */
2332 if (info.si_code >= 0)
2334 info.si_signo = sig;
2336 /* POSIX.1b doesn't mention process groups. */
2337 return kill_proc_info(sig, &info, pid);
2341 do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2343 struct k_sigaction *k;
2346 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2349 k = ¤t->sighand->action[sig-1];
2351 spin_lock_irq(¤t->sighand->siglock);
2352 if (signal_pending(current)) {
2354 * If there might be a fatal signal pending on multiple
2355 * threads, make sure we take it before changing the action.
2357 spin_unlock_irq(¤t->sighand->siglock);
2358 return -ERESTARTNOINTR;
2365 sigdelsetmask(&act->sa.sa_mask,
2366 sigmask(SIGKILL) | sigmask(SIGSTOP));
2369 * "Setting a signal action to SIG_IGN for a signal that is
2370 * pending shall cause the pending signal to be discarded,
2371 * whether or not it is blocked."
2373 * "Setting a signal action to SIG_DFL for a signal that is
2374 * pending and whose default action is to ignore the signal
2375 * (for example, SIGCHLD), shall cause the pending signal to
2376 * be discarded, whether or not it is blocked"
2378 if (act->sa.sa_handler == SIG_IGN ||
2379 (act->sa.sa_handler == SIG_DFL &&
2380 sig_kernel_ignore(sig))) {
2382 * This is a fairly rare case, so we only take the
2383 * tasklist_lock once we're sure we'll need it.
2384 * Now we must do this little unlock and relock
2385 * dance to maintain the lock hierarchy.
2387 struct task_struct *t = current;
2388 spin_unlock_irq(&t->sighand->siglock);
2389 read_lock(&tasklist_lock);
2390 spin_lock_irq(&t->sighand->siglock);
2393 sigaddset(&mask, sig);
2394 rm_from_queue_full(&mask, &t->signal->shared_pending);
2396 rm_from_queue_full(&mask, &t->pending);
2397 recalc_sigpending_tsk(t);
2399 } while (t != current);
2400 spin_unlock_irq(¤t->sighand->siglock);
2401 read_unlock(&tasklist_lock);
2408 spin_unlock_irq(¤t->sighand->siglock);
2413 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2419 oss.ss_sp = (void __user *) current->sas_ss_sp;
2420 oss.ss_size = current->sas_ss_size;
2421 oss.ss_flags = sas_ss_flags(sp);
2430 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2431 || __get_user(ss_sp, &uss->ss_sp)
2432 || __get_user(ss_flags, &uss->ss_flags)
2433 || __get_user(ss_size, &uss->ss_size))
2437 if (on_sig_stack(sp))
2443 * Note - this code used to test ss_flags incorrectly
2444 * old code may have been written using ss_flags==0
2445 * to mean ss_flags==SS_ONSTACK (as this was the only
2446 * way that worked) - this fix preserves that older
2449 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2452 if (ss_flags == SS_DISABLE) {
2457 if (ss_size < MINSIGSTKSZ)
2461 current->sas_ss_sp = (unsigned long) ss_sp;
2462 current->sas_ss_size = ss_size;
2467 if (copy_to_user(uoss, &oss, sizeof(oss)))
2476 #ifdef __ARCH_WANT_SYS_SIGPENDING
2479 sys_sigpending(old_sigset_t __user *set)
2481 return do_sigpending(set, sizeof(*set));
2486 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2487 /* Some platforms have their own version with special arguments others
2488 support only sys_rt_sigprocmask. */
2491 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2494 old_sigset_t old_set, new_set;
2498 if (copy_from_user(&new_set, set, sizeof(*set)))
2500 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2502 spin_lock_irq(¤t->sighand->siglock);
2503 old_set = current->blocked.sig[0];
2511 sigaddsetmask(¤t->blocked, new_set);
2514 sigdelsetmask(¤t->blocked, new_set);
2517 current->blocked.sig[0] = new_set;
2521 recalc_sigpending();
2522 spin_unlock_irq(¤t->sighand->siglock);
2528 old_set = current->blocked.sig[0];
2531 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2538 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2540 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2542 sys_rt_sigaction(int sig,
2543 const struct sigaction __user *act,
2544 struct sigaction __user *oact,
2547 struct k_sigaction new_sa, old_sa;
2550 /* XXX: Don't preclude handling different sized sigset_t's. */
2551 if (sigsetsize != sizeof(sigset_t))
2555 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2559 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2562 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2568 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2570 #ifdef __ARCH_WANT_SYS_SGETMASK
2573 * For backwards compatibility. Functionality superseded by sigprocmask.
2579 return current->blocked.sig[0];
2583 sys_ssetmask(int newmask)
2587 spin_lock_irq(¤t->sighand->siglock);
2588 old = current->blocked.sig[0];
2590 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2592 recalc_sigpending();
2593 spin_unlock_irq(¤t->sighand->siglock);
2597 #endif /* __ARCH_WANT_SGETMASK */
2599 #ifdef __ARCH_WANT_SYS_SIGNAL
2601 * For backwards compatibility. Functionality superseded by sigaction.
2603 asmlinkage unsigned long
2604 sys_signal(int sig, __sighandler_t handler)
2606 struct k_sigaction new_sa, old_sa;
2609 new_sa.sa.sa_handler = handler;
2610 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2611 sigemptyset(&new_sa.sa.sa_mask);
2613 ret = do_sigaction(sig, &new_sa, &old_sa);
2615 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2617 #endif /* __ARCH_WANT_SYS_SIGNAL */
2619 #ifdef __ARCH_WANT_SYS_PAUSE
2624 current->state = TASK_INTERRUPTIBLE;
2626 return -ERESTARTNOHAND;
2631 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2632 asmlinkage long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
2636 /* XXX: Don't preclude handling different sized sigset_t's. */
2637 if (sigsetsize != sizeof(sigset_t))
2640 if (copy_from_user(&newset, unewset, sizeof(newset)))
2642 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2644 spin_lock_irq(¤t->sighand->siglock);
2645 current->saved_sigmask = current->blocked;
2646 current->blocked = newset;
2647 recalc_sigpending();
2648 spin_unlock_irq(¤t->sighand->siglock);
2650 current->state = TASK_INTERRUPTIBLE;
2652 set_thread_flag(TIF_RESTORE_SIGMASK);
2653 return -ERESTARTNOHAND;
2655 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2657 void __init signals_init(void)
2660 kmem_cache_create("sigqueue",
2661 sizeof(struct sigqueue),
2662 __alignof__(struct sigqueue),
2663 SLAB_PANIC, NULL, NULL);