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>
29 extern void k_getrusage(struct task_struct *, int, struct rusage *);
32 * SLAB caches for signal bits.
35 static kmem_cache_t *sigqueue_cachep;
38 * In POSIX a signal is sent either to a specific thread (Linux task)
39 * or to the process as a whole (Linux thread group). How the signal
40 * is sent determines whether it's to one thread or the whole group,
41 * which determines which signal mask(s) are involved in blocking it
42 * from being delivered until later. When the signal is delivered,
43 * either it's caught or ignored by a user handler or it has a default
44 * effect that applies to the whole thread group (POSIX process).
46 * The possible effects an unblocked signal set to SIG_DFL can have are:
47 * ignore - Nothing Happens
48 * terminate - kill the process, i.e. all threads in the group,
49 * similar to exit_group. The group leader (only) reports
50 * WIFSIGNALED status to its parent.
51 * coredump - write a core dump file describing all threads using
52 * the same mm and then kill all those threads
53 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
55 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
56 * Other signals when not blocked and set to SIG_DFL behaves as follows.
57 * The job control signals also have other special effects.
59 * +--------------------+------------------+
60 * | POSIX signal | default action |
61 * +--------------------+------------------+
62 * | SIGHUP | terminate |
63 * | SIGINT | terminate |
64 * | SIGQUIT | coredump |
65 * | SIGILL | coredump |
66 * | SIGTRAP | coredump |
67 * | SIGABRT/SIGIOT | coredump |
68 * | SIGBUS | coredump |
69 * | SIGFPE | coredump |
70 * | SIGKILL | terminate(+) |
71 * | SIGUSR1 | terminate |
72 * | SIGSEGV | coredump |
73 * | SIGUSR2 | terminate |
74 * | SIGPIPE | terminate |
75 * | SIGALRM | terminate |
76 * | SIGTERM | terminate |
77 * | SIGCHLD | ignore |
78 * | SIGCONT | ignore(*) |
79 * | SIGSTOP | stop(*)(+) |
80 * | SIGTSTP | stop(*) |
81 * | SIGTTIN | stop(*) |
82 * | SIGTTOU | stop(*) |
84 * | SIGXCPU | coredump |
85 * | SIGXFSZ | coredump |
86 * | SIGVTALRM | terminate |
87 * | SIGPROF | terminate |
88 * | SIGPOLL/SIGIO | terminate |
89 * | SIGSYS/SIGUNUSED | coredump |
90 * | SIGSTKFLT | terminate |
91 * | SIGWINCH | ignore |
92 * | SIGPWR | terminate |
93 * | SIGRTMIN-SIGRTMAX | terminate |
94 * +--------------------+------------------+
95 * | non-POSIX signal | default action |
96 * +--------------------+------------------+
97 * | SIGEMT | coredump |
98 * +--------------------+------------------+
100 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
101 * (*) Special job control effects:
102 * When SIGCONT is sent, it resumes the process (all threads in the group)
103 * from TASK_STOPPED state and also clears any pending/queued stop signals
104 * (any of those marked with "stop(*)"). This happens regardless of blocking,
105 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
106 * any pending/queued SIGCONT signals; this happens regardless of blocking,
107 * catching, or ignored the stop signal, though (except for SIGSTOP) the
108 * default action of stopping the process may happen later or never.
112 #define M_SIGEMT M(SIGEMT)
117 #if SIGRTMIN > BITS_PER_LONG
118 #define M(sig) (1ULL << ((sig)-1))
120 #define M(sig) (1UL << ((sig)-1))
122 #define T(sig, mask) (M(sig) & (mask))
124 #define SIG_KERNEL_ONLY_MASK (\
125 M(SIGKILL) | M(SIGSTOP) )
127 #define SIG_KERNEL_STOP_MASK (\
128 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
130 #define SIG_KERNEL_COREDUMP_MASK (\
131 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
132 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
133 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
135 #define SIG_KERNEL_IGNORE_MASK (\
136 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
138 #define sig_kernel_only(sig) \
139 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
140 #define sig_kernel_coredump(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
142 #define sig_kernel_ignore(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
144 #define sig_kernel_stop(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
147 #define sig_user_defined(t, signr) \
148 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
149 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
151 #define sig_fatal(t, signr) \
152 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
153 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
155 #define sig_avoid_stop_race() \
156 (sigtestsetmask(¤t->pending.signal, M(SIGCONT) | M(SIGKILL)) || \
157 sigtestsetmask(¤t->signal->shared_pending.signal, \
158 M(SIGCONT) | M(SIGKILL)))
160 static int sig_ignored(struct task_struct *t, int sig)
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 ||
219 PENDING(&t->pending, &t->blocked) ||
220 PENDING(&t->signal->shared_pending, &t->blocked))
221 set_tsk_thread_flag(t, TIF_SIGPENDING);
223 clear_tsk_thread_flag(t, TIF_SIGPENDING);
226 void recalc_sigpending(void)
228 recalc_sigpending_tsk(current);
231 /* Given the mask, find the first available signal that should be serviced. */
234 next_signal(struct sigpending *pending, sigset_t *mask)
236 unsigned long i, *s, *m, x;
239 s = pending->signal.sig;
241 switch (_NSIG_WORDS) {
243 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
244 if ((x = *s &~ *m) != 0) {
245 sig = ffz(~x) + i*_NSIG_BPW + 1;
250 case 2: if ((x = s[0] &~ m[0]) != 0)
252 else if ((x = s[1] &~ m[1]) != 0)
259 case 1: if ((x = *s &~ *m) != 0)
267 static struct sigqueue *__sigqueue_alloc(void)
269 struct sigqueue *q = NULL;
271 if (atomic_read(¤t->user->sigpending) <
272 current->rlim[RLIMIT_SIGPENDING].rlim_cur)
273 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
275 INIT_LIST_HEAD(&q->list);
278 q->user = get_uid(current->user);
279 atomic_inc(&q->user->sigpending);
284 static inline void __sigqueue_free(struct sigqueue *q)
286 if (q->flags & SIGQUEUE_PREALLOC)
288 atomic_dec(&q->user->sigpending);
290 kmem_cache_free(sigqueue_cachep, q);
293 static void flush_sigqueue(struct sigpending *queue)
297 sigemptyset(&queue->signal);
298 while (!list_empty(&queue->list)) {
299 q = list_entry(queue->list.next, struct sigqueue , list);
300 list_del_init(&q->list);
306 * Flush all pending signals for a task.
310 flush_signals(struct task_struct *t)
314 spin_lock_irqsave(&t->sighand->siglock, flags);
315 clear_tsk_thread_flag(t,TIF_SIGPENDING);
316 flush_sigqueue(&t->pending);
317 flush_sigqueue(&t->signal->shared_pending);
318 spin_unlock_irqrestore(&t->sighand->siglock, flags);
322 * This function expects the tasklist_lock write-locked.
324 void __exit_sighand(struct task_struct *tsk)
326 struct sighand_struct * sighand = tsk->sighand;
328 /* Ok, we're done with the signal handlers */
330 if (atomic_dec_and_test(&sighand->count))
331 kmem_cache_free(sighand_cachep, sighand);
334 void exit_sighand(struct task_struct *tsk)
336 write_lock_irq(&tasklist_lock);
338 write_unlock_irq(&tasklist_lock);
342 * This function expects the tasklist_lock write-locked.
344 void __exit_signal(struct task_struct *tsk)
346 struct signal_struct * sig = tsk->signal;
347 struct sighand_struct * sighand = tsk->sighand;
351 if (!atomic_read(&sig->count))
353 spin_lock(&sighand->siglock);
354 if (atomic_dec_and_test(&sig->count)) {
355 if (tsk == sig->curr_target)
356 sig->curr_target = next_thread(tsk);
358 spin_unlock(&sighand->siglock);
359 flush_sigqueue(&sig->shared_pending);
362 * If there is any task waiting for the group exit
365 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
366 wake_up_process(sig->group_exit_task);
367 sig->group_exit_task = NULL;
369 if (tsk == sig->curr_target)
370 sig->curr_target = next_thread(tsk);
373 * Accumulate here the counters for all threads but the
374 * group leader as they die, so they can be added into
375 * the process-wide totals when those are taken.
376 * The group leader stays around as a zombie as long
377 * as there are other threads. When it gets reaped,
378 * the exit.c code will add its counts into these totals.
379 * We won't ever get here for the group leader, since it
380 * will have been the last reference on the signal_struct.
382 sig->utime += tsk->utime;
383 sig->stime += tsk->stime;
384 sig->min_flt += tsk->min_flt;
385 sig->maj_flt += tsk->maj_flt;
386 sig->nvcsw += tsk->nvcsw;
387 sig->nivcsw += tsk->nivcsw;
388 spin_unlock(&sighand->siglock);
389 sig = NULL; /* Marker for below. */
391 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
392 flush_sigqueue(&tsk->pending);
395 * We are cleaning up the signal_struct here. We delayed
396 * calling exit_itimers until after flush_sigqueue, just in
397 * case our thread-local pending queue contained a queued
398 * timer signal that would have been cleared in
399 * exit_itimers. When that called sigqueue_free, it would
400 * attempt to re-take the tasklist_lock and deadlock. This
401 * can never happen if we ensure that all queues the
402 * timer's signal might be queued on have been flushed
403 * first. The shared_pending queue, and our own pending
404 * queue are the only queues the timer could be on, since
405 * there are no other threads left in the group and timer
406 * signals are constrained to threads inside the group.
409 kmem_cache_free(signal_cachep, sig);
413 void exit_signal(struct task_struct *tsk)
415 write_lock_irq(&tasklist_lock);
417 write_unlock_irq(&tasklist_lock);
421 * Flush all handlers for a task.
425 flush_signal_handlers(struct task_struct *t, int force_default)
428 struct k_sigaction *ka = &t->sighand->action[0];
429 for (i = _NSIG ; i != 0 ; i--) {
430 if (force_default || ka->sa.sa_handler != SIG_IGN)
431 ka->sa.sa_handler = SIG_DFL;
433 sigemptyset(&ka->sa.sa_mask);
438 EXPORT_SYMBOL_GPL(flush_signal_handlers);
440 /* Notify the system that a driver wants to block all signals for this
441 * process, and wants to be notified if any signals at all were to be
442 * sent/acted upon. If the notifier routine returns non-zero, then the
443 * signal will be acted upon after all. If the notifier routine returns 0,
444 * then then signal will be blocked. Only one block per process is
445 * allowed. priv is a pointer to private data that the notifier routine
446 * can use to determine if the signal should be blocked or not. */
449 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
453 spin_lock_irqsave(¤t->sighand->siglock, flags);
454 current->notifier_mask = mask;
455 current->notifier_data = priv;
456 current->notifier = notifier;
457 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
460 /* Notify the system that blocking has ended. */
463 unblock_all_signals(void)
467 spin_lock_irqsave(¤t->sighand->siglock, flags);
468 current->notifier = NULL;
469 current->notifier_data = NULL;
471 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
474 static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
476 struct sigqueue *q, *first = NULL;
477 int still_pending = 0;
479 if (unlikely(!sigismember(&list->signal, sig)))
483 * Collect the siginfo appropriate to this signal. Check if
484 * there is another siginfo for the same signal.
486 list_for_each_entry(q, &list->list, list) {
487 if (q->info.si_signo == sig) {
496 list_del_init(&first->list);
497 copy_siginfo(info, &first->info);
498 __sigqueue_free(first);
500 sigdelset(&list->signal, sig);
503 /* Ok, it wasn't in the queue. This must be
504 a fast-pathed signal or we must have been
505 out of queue space. So zero out the info.
507 sigdelset(&list->signal, sig);
508 info->si_signo = sig;
517 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
522 sig = next_signal(pending, mask);
524 if (current->notifier) {
525 if (sigismember(current->notifier_mask, sig)) {
526 if (!(current->notifier)(current->notifier_data)) {
527 clear_thread_flag(TIF_SIGPENDING);
533 if (!collect_signal(sig, pending, info))
543 * Dequeue a signal and return the element to the caller, which is
544 * expected to free it.
546 * All callers have to hold the siglock.
548 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
550 int signr = __dequeue_signal(&tsk->pending, mask, info);
552 signr = __dequeue_signal(&tsk->signal->shared_pending,
555 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
556 info->si_sys_private){
557 do_schedule_next_timer(info);
563 * Tell a process that it has a new active signal..
565 * NOTE! we rely on the previous spin_lock to
566 * lock interrupts for us! We can only be called with
567 * "siglock" held, and the local interrupt must
568 * have been disabled when that got acquired!
570 * No need to set need_resched since signal event passing
571 * goes through ->blocked
573 void signal_wake_up(struct task_struct *t, int resume)
577 set_tsk_thread_flag(t, TIF_SIGPENDING);
580 * If resume is set, we want to wake it up in the TASK_STOPPED case.
581 * We don't check for TASK_STOPPED because there is a race with it
582 * executing another processor and just now entering stopped state.
583 * By calling wake_up_process any time resume is set, we ensure
584 * the process will wake up and handle its stop or death signal.
586 mask = TASK_INTERRUPTIBLE;
588 mask |= TASK_STOPPED;
589 if (!wake_up_state(t, mask))
594 * Remove signals in mask from the pending set and queue.
595 * Returns 1 if any signals were found.
597 * All callers must be holding the siglock.
599 static int rm_from_queue(unsigned long mask, struct sigpending *s)
601 struct sigqueue *q, *n;
603 if (!sigtestsetmask(&s->signal, mask))
606 sigdelsetmask(&s->signal, mask);
607 list_for_each_entry_safe(q, n, &s->list, list) {
608 if (q->info.si_signo < SIGRTMIN &&
609 (mask & sigmask(q->info.si_signo))) {
610 list_del_init(&q->list);
618 * Bad permissions for sending the signal
620 static int check_kill_permission(int sig, struct siginfo *info,
621 struct task_struct *t)
626 if (sig < 0 || sig > _NSIG)
630 (info != SEND_SIG_PRIV &&
631 info != SEND_SIG_FORCED &&
635 if (user && (sig != SIGCONT ||
636 current->signal->session != t->signal->session)
637 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
638 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
639 && !capable(CAP_KILL))
643 if (user && !vx_check(vx_task_xid(t), VX_ADMIN|VX_IDENT))
646 return security_task_kill(t, info, sig);
650 static void do_notify_parent_cldstop(struct task_struct *tsk,
651 struct task_struct *parent,
655 * Handle magic process-wide effects of stop/continue signals.
656 * Unlike the signal actions, these happen immediately at signal-generation
657 * time regardless of blocking, ignoring, or handling. This does the
658 * actual continuing for SIGCONT, but not the actual stopping for stop
659 * signals. The process stop is done as a signal action for SIG_DFL.
661 static void handle_stop_signal(int sig, struct task_struct *p)
663 struct task_struct *t;
665 if (sig_kernel_stop(sig)) {
667 * This is a stop signal. Remove SIGCONT from all queues.
669 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
672 rm_from_queue(sigmask(SIGCONT), &t->pending);
675 } else if (sig == SIGCONT) {
677 * Remove all stop signals from all queues,
678 * and wake all threads.
680 if (unlikely(p->signal->group_stop_count > 0)) {
682 * There was a group stop in progress. We'll
683 * pretend it finished before we got here. We are
684 * obliged to report it to the parent: if the
685 * SIGSTOP happened "after" this SIGCONT, then it
686 * would have cleared this pending SIGCONT. If it
687 * happened "before" this SIGCONT, then the parent
688 * got the SIGCHLD about the stop finishing before
689 * the continue happened. We do the notification
690 * now, and it's as if the stop had finished and
691 * the SIGCHLD was pending on entry to this kill.
693 p->signal->group_stop_count = 0;
694 p->signal->stop_state = 1;
695 spin_unlock(&p->sighand->siglock);
696 if (p->ptrace & PT_PTRACED)
697 do_notify_parent_cldstop(p, p->parent,
700 do_notify_parent_cldstop(
702 p->group_leader->real_parent,
704 spin_lock(&p->sighand->siglock);
706 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
710 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
713 * If there is a handler for SIGCONT, we must make
714 * sure that no thread returns to user mode before
715 * we post the signal, in case it was the only
716 * thread eligible to run the signal handler--then
717 * it must not do anything between resuming and
718 * running the handler. With the TIF_SIGPENDING
719 * flag set, the thread will pause and acquire the
720 * siglock that we hold now and until we've queued
721 * the pending signal.
723 * Wake up the stopped thread _after_ setting
726 state = TASK_STOPPED;
727 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
728 set_tsk_thread_flag(t, TIF_SIGPENDING);
729 state |= TASK_INTERRUPTIBLE;
731 wake_up_state(t, state);
736 if (p->signal->stop_state > 0) {
738 * We were in fact stopped, and are now continued.
739 * Notify the parent with CLD_CONTINUED.
741 p->signal->stop_state = -1;
742 p->signal->group_exit_code = 0;
743 spin_unlock(&p->sighand->siglock);
744 if (p->ptrace & PT_PTRACED)
745 do_notify_parent_cldstop(p, p->parent,
748 do_notify_parent_cldstop(
750 p->group_leader->real_parent,
752 spin_lock(&p->sighand->siglock);
757 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
758 struct sigpending *signals)
760 struct sigqueue * q = NULL;
764 * fast-pathed signals for kernel-internal things like SIGSTOP
767 if ((unsigned long)info == 2)
770 /* Real-time signals must be queued if sent by sigqueue, or
771 some other real-time mechanism. It is implementation
772 defined whether kill() does so. We attempt to do so, on
773 the principle of least surprise, but since kill is not
774 allowed to fail with EAGAIN when low on memory we just
775 make sure at least one signal gets delivered and don't
776 pass on the info struct. */
778 if (atomic_read(&t->user->sigpending) <
779 t->rlim[RLIMIT_SIGPENDING].rlim_cur)
780 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
784 q->user = get_uid(t->user);
785 atomic_inc(&q->user->sigpending);
786 list_add_tail(&q->list, &signals->list);
787 switch ((unsigned long) info) {
789 q->info.si_signo = sig;
790 q->info.si_errno = 0;
791 q->info.si_code = SI_USER;
792 q->info.si_pid = current->pid;
793 q->info.si_uid = current->uid;
796 q->info.si_signo = sig;
797 q->info.si_errno = 0;
798 q->info.si_code = SI_KERNEL;
803 copy_siginfo(&q->info, info);
807 if (sig >= SIGRTMIN && info && (unsigned long)info != 1
808 && info->si_code != SI_USER)
810 * Queue overflow, abort. We may abort if the signal was rt
811 * and sent by user using something other than kill().
814 if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
816 * Set up a return to indicate that we dropped
819 ret = info->si_sys_private;
823 sigaddset(&signals->signal, sig);
827 #define LEGACY_QUEUE(sigptr, sig) \
828 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
832 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
836 if (!irqs_disabled())
839 if (!spin_is_locked(&t->sighand->siglock))
843 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
845 * Set up a return to indicate that we dropped the signal.
847 ret = info->si_sys_private;
849 /* Short-circuit ignored signals. */
850 if (sig_ignored(t, sig))
853 /* Support queueing exactly one non-rt signal, so that we
854 can get more detailed information about the cause of
856 if (LEGACY_QUEUE(&t->pending, sig))
859 ret = send_signal(sig, info, t, &t->pending);
860 if (!ret && !sigismember(&t->blocked, sig))
861 signal_wake_up(t, sig == SIGKILL);
867 * Force a signal that the process can't ignore: if necessary
868 * we unblock the signal and change any SIG_IGN to SIG_DFL.
872 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
874 unsigned long int flags;
877 spin_lock_irqsave(&t->sighand->siglock, flags);
878 if (sigismember(&t->blocked, sig) || t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
879 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
880 sigdelset(&t->blocked, sig);
881 recalc_sigpending_tsk(t);
883 ret = specific_send_sig_info(sig, info, t);
884 spin_unlock_irqrestore(&t->sighand->siglock, flags);
890 force_sig_specific(int sig, struct task_struct *t)
892 unsigned long int flags;
894 spin_lock_irqsave(&t->sighand->siglock, flags);
895 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN)
896 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
897 sigdelset(&t->blocked, sig);
898 recalc_sigpending_tsk(t);
899 specific_send_sig_info(sig, (void *)2, t);
900 spin_unlock_irqrestore(&t->sighand->siglock, flags);
904 * Test if P wants to take SIG. After we've checked all threads with this,
905 * it's equivalent to finding no threads not blocking SIG. Any threads not
906 * blocking SIG were ruled out because they are not running and already
907 * have pending signals. Such threads will dequeue from the shared queue
908 * as soon as they're available, so putting the signal on the shared queue
909 * will be equivalent to sending it to one such thread.
911 #define wants_signal(sig, p, mask) \
912 (!sigismember(&(p)->blocked, sig) \
913 && !((p)->state & mask) \
914 && !((p)->flags & PF_EXITING) \
915 && (task_curr(p) || !signal_pending(p)))
919 __group_complete_signal(int sig, struct task_struct *p)
922 struct task_struct *t;
925 * Don't bother zombies and stopped tasks (but
926 * SIGKILL will punch through stopped state)
928 mask = EXIT_DEAD | EXIT_ZOMBIE | TASK_TRACED;
930 mask |= TASK_STOPPED;
933 * Now find a thread we can wake up to take the signal off the queue.
935 * If the main thread wants the signal, it gets first crack.
936 * Probably the least surprising to the average bear.
938 if (wants_signal(sig, p, mask))
940 else if (thread_group_empty(p))
942 * There is just one thread and it does not need to be woken.
943 * It will dequeue unblocked signals before it runs again.
948 * Otherwise try to find a suitable thread.
950 t = p->signal->curr_target;
952 /* restart balancing at this thread */
953 t = p->signal->curr_target = p;
954 BUG_ON(t->tgid != p->tgid);
956 while (!wants_signal(sig, t, mask)) {
958 if (t == p->signal->curr_target)
960 * No thread needs to be woken.
961 * Any eligible threads will see
962 * the signal in the queue soon.
966 p->signal->curr_target = t;
970 * Found a killable thread. If the signal will be fatal,
971 * then start taking the whole group down immediately.
973 if (sig_fatal(p, sig) && !p->signal->group_exit &&
974 !sigismember(&t->real_blocked, sig) &&
975 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
977 * This signal will be fatal to the whole group.
979 if (!sig_kernel_coredump(sig)) {
981 * Start a group exit and wake everybody up.
982 * This way we don't have other threads
983 * running and doing things after a slower
984 * thread has the fatal signal pending.
986 p->signal->group_exit = 1;
987 p->signal->group_exit_code = sig;
988 p->signal->group_stop_count = 0;
991 sigaddset(&t->pending.signal, SIGKILL);
992 signal_wake_up(t, 1);
999 * There will be a core dump. We make all threads other
1000 * than the chosen one go into a group stop so that nothing
1001 * happens until it gets scheduled, takes the signal off
1002 * the shared queue, and does the core dump. This is a
1003 * little more complicated than strictly necessary, but it
1004 * keeps the signal state that winds up in the core dump
1005 * unchanged from the death state, e.g. which thread had
1006 * the core-dump signal unblocked.
1008 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1009 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
1010 p->signal->group_stop_count = 0;
1011 p->signal->group_exit_task = t;
1014 p->signal->group_stop_count++;
1015 signal_wake_up(t, 0);
1018 wake_up_process(p->signal->group_exit_task);
1023 * The signal is already in the shared-pending queue.
1024 * Tell the chosen thread to wake up and dequeue it.
1026 signal_wake_up(t, sig == SIGKILL);
1031 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1036 if (!spin_is_locked(&p->sighand->siglock))
1039 handle_stop_signal(sig, p);
1041 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
1043 * Set up a return to indicate that we dropped the signal.
1045 ret = info->si_sys_private;
1047 /* Short-circuit ignored signals. */
1048 if (sig_ignored(p, sig))
1051 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
1052 /* This is a non-RT signal and we already have one queued. */
1056 * Put this signal on the shared-pending queue, or fail with EAGAIN.
1057 * We always use the shared queue for process-wide signals,
1058 * to avoid several races.
1060 ret = send_signal(sig, info, p, &p->signal->shared_pending);
1064 __group_complete_signal(sig, p);
1069 * Nuke all other threads in the group.
1071 void zap_other_threads(struct task_struct *p)
1073 struct task_struct *t;
1075 p->signal->group_stop_count = 0;
1077 if (thread_group_empty(p))
1080 for (t = next_thread(p); t != p; t = next_thread(t)) {
1082 * Don't bother with already dead threads
1084 if (t->exit_state & (EXIT_ZOMBIE|EXIT_DEAD))
1088 * We don't want to notify the parent, since we are
1089 * killed as part of a thread group due to another
1090 * thread doing an execve() or similar. So set the
1091 * exit signal to -1 to allow immediate reaping of
1092 * the process. But don't detach the thread group
1095 if (t != p->group_leader)
1096 t->exit_signal = -1;
1098 sigaddset(&t->pending.signal, SIGKILL);
1099 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1100 signal_wake_up(t, 1);
1105 * Must be called with the tasklist_lock held for reading!
1107 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1109 unsigned long flags;
1112 ret = check_kill_permission(sig, info, p);
1113 if (!ret && sig && p->sighand) {
1114 spin_lock_irqsave(&p->sighand->siglock, flags);
1115 ret = __group_send_sig_info(sig, info, p);
1116 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1123 * kill_pg_info() sends a signal to a process group: this is what the tty
1124 * control characters do (^C, ^Z etc)
1127 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1129 struct task_struct *p;
1130 int retval, success;
1137 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1138 int err = group_send_sig_info(sig, info, p);
1141 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1142 return success ? 0 : retval;
1146 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1150 read_lock(&tasklist_lock);
1151 retval = __kill_pg_info(sig, info, pgrp);
1152 read_unlock(&tasklist_lock);
1158 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1161 struct task_struct *p;
1163 read_lock(&tasklist_lock);
1164 p = find_task_by_pid(pid);
1167 error = group_send_sig_info(sig, info, p);
1168 read_unlock(&tasklist_lock);
1172 int print_fatal_signals = 0;
1174 static void print_fatal_signal(struct pt_regs *regs, int signr)
1178 printk("%s/%d: potentially unexpected fatal signal %d.\n",
1179 current->comm, current->pid, signr);
1182 printk("code at %08lx: ", regs->eip);
1183 for (i = 0; i < 16; i++) {
1184 __get_user(insn, (unsigned char *)(regs->eip + i));
1185 printk("%02x ", insn);
1192 static int __init setup_print_fatal_signals(char *str)
1194 get_option (&str, &print_fatal_signals);
1199 __setup("print-fatal-signals=", setup_print_fatal_signals);
1202 * kill_something_info() interprets pid in interesting ways just like kill(2).
1204 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1205 * is probably wrong. Should make it like BSD or SYSV.
1208 static int kill_something_info(int sig, struct siginfo *info, int pid)
1211 return kill_pg_info(sig, info, process_group(current));
1212 } else if (pid == -1) {
1213 int retval = 0, count = 0;
1214 struct task_struct * p;
1216 read_lock(&tasklist_lock);
1217 for_each_process(p) {
1218 if (p->pid > 1 && p->tgid != current->tgid) {
1219 int err = group_send_sig_info(sig, info, p);
1225 read_unlock(&tasklist_lock);
1226 return count ? retval : -ESRCH;
1227 } else if (pid < 0) {
1228 return kill_pg_info(sig, info, -pid);
1230 return kill_proc_info(sig, info, pid);
1235 * These are for backward compatibility with the rest of the kernel source.
1239 * These two are the most common entry points. They send a signal
1240 * just to the specific thread.
1243 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1246 unsigned long flags;
1249 * Make sure legacy kernel users don't send in bad values
1250 * (normal paths check this in check_kill_permission).
1252 if (sig < 0 || sig > _NSIG)
1256 * We need the tasklist lock even for the specific
1257 * thread case (when we don't need to follow the group
1258 * lists) in order to avoid races with "p->sighand"
1259 * going away or changing from under us.
1261 read_lock(&tasklist_lock);
1262 spin_lock_irqsave(&p->sighand->siglock, flags);
1263 ret = specific_send_sig_info(sig, info, p);
1264 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1265 read_unlock(&tasklist_lock);
1270 send_sig(int sig, struct task_struct *p, int priv)
1272 return send_sig_info(sig, (void*)(long)(priv != 0), p);
1276 * This is the entry point for "process-wide" signals.
1277 * They will go to an appropriate thread in the thread group.
1280 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1283 read_lock(&tasklist_lock);
1284 ret = group_send_sig_info(sig, info, p);
1285 read_unlock(&tasklist_lock);
1290 force_sig(int sig, struct task_struct *p)
1292 force_sig_info(sig, (void*)1L, p);
1296 * When things go south during signal handling, we
1297 * will force a SIGSEGV. And if the signal that caused
1298 * the problem was already a SIGSEGV, we'll want to
1299 * make sure we don't even try to deliver the signal..
1302 force_sigsegv(int sig, struct task_struct *p)
1304 if (sig == SIGSEGV) {
1305 unsigned long flags;
1306 spin_lock_irqsave(&p->sighand->siglock, flags);
1307 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1308 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1310 force_sig(SIGSEGV, p);
1315 kill_pg(pid_t pgrp, int sig, int priv)
1317 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1321 kill_proc(pid_t pid, int sig, int priv)
1323 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1327 * These functions support sending signals using preallocated sigqueue
1328 * structures. This is needed "because realtime applications cannot
1329 * afford to lose notifications of asynchronous events, like timer
1330 * expirations or I/O completions". In the case of Posix Timers
1331 * we allocate the sigqueue structure from the timer_create. If this
1332 * allocation fails we are able to report the failure to the application
1333 * with an EAGAIN error.
1336 struct sigqueue *sigqueue_alloc(void)
1340 if ((q = __sigqueue_alloc()))
1341 q->flags |= SIGQUEUE_PREALLOC;
1345 void sigqueue_free(struct sigqueue *q)
1347 unsigned long flags;
1348 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1350 * If the signal is still pending remove it from the
1353 if (unlikely(!list_empty(&q->list))) {
1354 read_lock(&tasklist_lock);
1355 spin_lock_irqsave(q->lock, flags);
1356 if (!list_empty(&q->list))
1357 list_del_init(&q->list);
1358 spin_unlock_irqrestore(q->lock, flags);
1359 read_unlock(&tasklist_lock);
1361 q->flags &= ~SIGQUEUE_PREALLOC;
1366 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1368 unsigned long flags;
1372 * We need the tasklist lock even for the specific
1373 * thread case (when we don't need to follow the group
1374 * lists) in order to avoid races with "p->sighand"
1375 * going away or changing from under us.
1377 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1378 read_lock(&tasklist_lock);
1379 spin_lock_irqsave(&p->sighand->siglock, flags);
1381 if (unlikely(!list_empty(&q->list))) {
1383 * If an SI_TIMER entry is already queue just increment
1384 * the overrun count.
1386 if (q->info.si_code != SI_TIMER)
1388 q->info.si_overrun++;
1391 /* Short-circuit ignored signals. */
1392 if (sig_ignored(p, sig)) {
1397 q->lock = &p->sighand->siglock;
1398 list_add_tail(&q->list, &p->pending.list);
1399 sigaddset(&p->pending.signal, sig);
1400 if (!sigismember(&p->blocked, sig))
1401 signal_wake_up(p, sig == SIGKILL);
1404 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1405 read_unlock(&tasklist_lock);
1410 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1412 unsigned long flags;
1415 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1416 read_lock(&tasklist_lock);
1417 spin_lock_irqsave(&p->sighand->siglock, flags);
1418 handle_stop_signal(sig, p);
1420 /* Short-circuit ignored signals. */
1421 if (sig_ignored(p, sig)) {
1426 if (unlikely(!list_empty(&q->list))) {
1428 * If an SI_TIMER entry is already queue just increment
1429 * the overrun count. Other uses should not try to
1430 * send the signal multiple times.
1432 if (q->info.si_code != SI_TIMER)
1434 q->info.si_overrun++;
1439 * Put this signal on the shared-pending queue.
1440 * We always use the shared queue for process-wide signals,
1441 * to avoid several races.
1443 q->lock = &p->sighand->siglock;
1444 list_add_tail(&q->list, &p->signal->shared_pending.list);
1445 sigaddset(&p->signal->shared_pending.signal, sig);
1447 __group_complete_signal(sig, p);
1449 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1450 read_unlock(&tasklist_lock);
1455 * Joy. Or not. Pthread wants us to wake up every thread
1456 * in our parent group.
1458 static void __wake_up_parent(struct task_struct *p,
1459 struct task_struct *parent)
1461 struct task_struct *tsk = parent;
1464 * Fortunately this is not necessary for thread groups:
1466 if (p->tgid == tsk->tgid) {
1467 wake_up_interruptible_sync(&tsk->wait_chldexit);
1472 wake_up_interruptible_sync(&tsk->wait_chldexit);
1473 tsk = next_thread(tsk);
1474 if (tsk->signal != parent->signal)
1476 } while (tsk != parent);
1480 * Let a parent know about the death of a child.
1481 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1484 void do_notify_parent(struct task_struct *tsk, int sig)
1486 struct siginfo info;
1487 unsigned long flags;
1488 struct sighand_struct *psig;
1493 /* do_notify_parent_cldstop should have been called instead. */
1494 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1496 BUG_ON(!tsk->ptrace &&
1497 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1499 info.si_signo = sig;
1501 info.si_pid = tsk->pid;
1502 info.si_uid = tsk->uid;
1504 /* FIXME: find out whether or not this is supposed to be c*time. */
1505 info.si_utime = tsk->utime + tsk->signal->utime;
1506 info.si_stime = tsk->stime + tsk->signal->stime;
1508 info.si_status = tsk->exit_code & 0x7f;
1509 if (tsk->exit_code & 0x80)
1510 info.si_code = CLD_DUMPED;
1511 else if (tsk->exit_code & 0x7f)
1512 info.si_code = CLD_KILLED;
1514 info.si_code = CLD_EXITED;
1515 info.si_status = tsk->exit_code >> 8;
1518 psig = tsk->parent->sighand;
1519 spin_lock_irqsave(&psig->siglock, flags);
1520 if (sig == SIGCHLD &&
1521 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1522 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1524 * We are exiting and our parent doesn't care. POSIX.1
1525 * defines special semantics for setting SIGCHLD to SIG_IGN
1526 * or setting the SA_NOCLDWAIT flag: we should be reaped
1527 * automatically and not left for our parent's wait4 call.
1528 * Rather than having the parent do it as a magic kind of
1529 * signal handler, we just set this to tell do_exit that we
1530 * can be cleaned up without becoming a zombie. Note that
1531 * we still call __wake_up_parent in this case, because a
1532 * blocked sys_wait4 might now return -ECHILD.
1534 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1535 * is implementation-defined: we do (if you don't want
1536 * it, just use SIG_IGN instead).
1538 tsk->exit_signal = -1;
1539 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1542 if (sig > 0 && sig <= _NSIG)
1543 __group_send_sig_info(sig, &info, tsk->parent);
1544 __wake_up_parent(tsk, tsk->parent);
1545 spin_unlock_irqrestore(&psig->siglock, flags);
1549 do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent,
1552 struct siginfo info;
1553 unsigned long flags;
1554 struct sighand_struct *sighand;
1556 info.si_signo = SIGCHLD;
1558 info.si_pid = tsk->pid;
1559 info.si_uid = tsk->uid;
1561 /* FIXME: find out whether or not this is supposed to be c*time. */
1562 info.si_utime = tsk->utime;
1563 info.si_stime = tsk->stime;
1568 info.si_status = SIGCONT;
1571 info.si_status = tsk->signal->group_exit_code & 0x7f;
1574 info.si_status = tsk->exit_code & 0x7f;
1580 sighand = parent->sighand;
1581 spin_lock_irqsave(&sighand->siglock, flags);
1582 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1583 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1584 __group_send_sig_info(SIGCHLD, &info, parent);
1586 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1588 __wake_up_parent(tsk, parent);
1589 spin_unlock_irqrestore(&sighand->siglock, flags);
1593 * This must be called with current->sighand->siglock held.
1595 * This should be the path for all ptrace stops.
1596 * We always set current->last_siginfo while stopped here.
1597 * That makes it a way to test a stopped process for
1598 * being ptrace-stopped vs being job-control-stopped.
1600 static void ptrace_stop(int exit_code, siginfo_t *info)
1602 BUG_ON(!(current->ptrace & PT_PTRACED));
1605 * If there is a group stop in progress,
1606 * we must participate in the bookkeeping.
1608 if (current->signal->group_stop_count > 0)
1609 --current->signal->group_stop_count;
1611 current->last_siginfo = info;
1612 current->exit_code = exit_code;
1614 /* Let the debugger run. */
1615 set_current_state(TASK_TRACED);
1616 spin_unlock_irq(¤t->sighand->siglock);
1617 read_lock(&tasklist_lock);
1618 do_notify_parent_cldstop(current, current->parent, CLD_TRAPPED);
1619 read_unlock(&tasklist_lock);
1623 * We are back. Now reacquire the siglock before touching
1624 * last_siginfo, so that we are sure to have synchronized with
1625 * any signal-sending on another CPU that wants to examine it.
1627 spin_lock_irq(¤t->sighand->siglock);
1628 current->last_siginfo = NULL;
1631 * Queued signals ignored us while we were stopped for tracing.
1632 * So check for any that we should take before resuming user mode.
1634 recalc_sigpending();
1637 void ptrace_notify(int exit_code)
1641 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1643 memset(&info, 0, sizeof info);
1644 info.si_signo = SIGTRAP;
1645 info.si_code = exit_code;
1646 info.si_pid = current->pid;
1647 info.si_uid = current->uid;
1649 /* Let the debugger run. */
1650 spin_lock_irq(¤t->sighand->siglock);
1651 ptrace_stop(exit_code, &info);
1652 spin_unlock_irq(¤t->sighand->siglock);
1655 #ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1658 finish_stop(int stop_count)
1661 * If there are no other threads in the group, or if there is
1662 * a group stop in progress and we are the last to stop,
1663 * report to the parent. When ptraced, every thread reports itself.
1665 if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1666 read_lock(&tasklist_lock);
1667 do_notify_parent_cldstop(current, current->parent,
1669 read_unlock(&tasklist_lock);
1671 else if (stop_count == 0) {
1672 read_lock(&tasklist_lock);
1673 do_notify_parent_cldstop(current->group_leader,
1674 current->group_leader->real_parent,
1676 read_unlock(&tasklist_lock);
1681 * Now we don't run again until continued.
1683 current->exit_code = 0;
1687 * This performs the stopping for SIGSTOP and other stop signals.
1688 * We have to stop all threads in the thread group.
1691 do_signal_stop(int signr)
1693 struct signal_struct *sig = current->signal;
1694 struct sighand_struct *sighand = current->sighand;
1695 int stop_count = -1;
1697 /* spin_lock_irq(&sighand->siglock) is now done in caller */
1699 if (sig->group_stop_count > 0) {
1701 * There is a group stop in progress. We don't need to
1702 * start another one.
1704 signr = sig->group_exit_code;
1705 stop_count = --sig->group_stop_count;
1706 current->exit_code = signr;
1707 set_current_state(TASK_STOPPED);
1708 if (stop_count == 0)
1709 sig->stop_state = 1;
1710 spin_unlock_irq(&sighand->siglock);
1712 else if (thread_group_empty(current)) {
1714 * Lock must be held through transition to stopped state.
1716 current->exit_code = current->signal->group_exit_code = signr;
1717 set_current_state(TASK_STOPPED);
1718 sig->stop_state = 1;
1719 spin_unlock_irq(&sighand->siglock);
1723 * There is no group stop already in progress.
1724 * We must initiate one now, but that requires
1725 * dropping siglock to get both the tasklist lock
1726 * and siglock again in the proper order. Note that
1727 * this allows an intervening SIGCONT to be posted.
1728 * We need to check for that and bail out if necessary.
1730 struct task_struct *t;
1732 spin_unlock_irq(&sighand->siglock);
1734 /* signals can be posted during this window */
1736 read_lock(&tasklist_lock);
1737 spin_lock_irq(&sighand->siglock);
1739 if (unlikely(sig->group_exit)) {
1741 * There is a group exit in progress now.
1742 * We'll just ignore the stop and process the
1743 * associated fatal signal.
1745 spin_unlock_irq(&sighand->siglock);
1746 read_unlock(&tasklist_lock);
1750 if (unlikely(sig_avoid_stop_race())) {
1752 * Either a SIGCONT or a SIGKILL signal was
1753 * posted in the siglock-not-held window.
1755 spin_unlock_irq(&sighand->siglock);
1756 read_unlock(&tasklist_lock);
1760 if (sig->group_stop_count == 0) {
1761 sig->group_exit_code = signr;
1763 for (t = next_thread(current); t != current;
1766 * Setting state to TASK_STOPPED for a group
1767 * stop is always done with the siglock held,
1768 * so this check has no races.
1770 if (t->state < TASK_STOPPED) {
1772 signal_wake_up(t, 0);
1774 sig->group_stop_count = stop_count;
1777 /* A race with another thread while unlocked. */
1778 signr = sig->group_exit_code;
1779 stop_count = --sig->group_stop_count;
1782 current->exit_code = signr;
1783 set_current_state(TASK_STOPPED);
1784 if (stop_count == 0)
1785 sig->stop_state = 1;
1787 spin_unlock_irq(&sighand->siglock);
1788 read_unlock(&tasklist_lock);
1791 finish_stop(stop_count);
1795 * Do appropriate magic when group_stop_count > 0.
1796 * We return nonzero if we stopped, after releasing the siglock.
1797 * We return zero if we still hold the siglock and should look
1798 * for another signal without checking group_stop_count again.
1800 static inline int handle_group_stop(void)
1804 if (current->signal->group_exit_task == current) {
1806 * Group stop is so we can do a core dump,
1807 * We are the initiating thread, so get on with it.
1809 current->signal->group_exit_task = NULL;
1813 if (current->signal->group_exit)
1815 * Group stop is so another thread can do a core dump,
1816 * or else we are racing against a death signal.
1817 * Just punt the stop so we can get the next signal.
1822 * There is a group stop in progress. We stop
1823 * without any associated signal being in our queue.
1825 stop_count = --current->signal->group_stop_count;
1826 if (stop_count == 0)
1827 current->signal->stop_state = 1;
1828 current->exit_code = current->signal->group_exit_code;
1829 set_current_state(TASK_STOPPED);
1830 spin_unlock_irq(¤t->sighand->siglock);
1831 finish_stop(stop_count);
1835 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1836 struct pt_regs *regs, void *cookie)
1838 sigset_t *mask = ¤t->blocked;
1842 spin_lock_irq(¤t->sighand->siglock);
1844 struct k_sigaction *ka;
1846 if (unlikely(current->signal->group_stop_count > 0) &&
1847 handle_group_stop())
1850 signr = dequeue_signal(current, mask, info);
1853 break; /* will return 0 */
1855 if ((signr == SIGSEGV) && print_fatal_signals) {
1856 spin_unlock_irq(¤t->sighand->siglock);
1857 print_fatal_signal(regs, signr);
1858 spin_lock_irq(¤t->sighand->siglock);
1860 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1861 ptrace_signal_deliver(regs, cookie);
1863 /* Let the debugger run. */
1864 ptrace_stop(signr, info);
1866 /* We're back. Did the debugger cancel the sig? */
1867 signr = current->exit_code;
1871 current->exit_code = 0;
1873 /* Update the siginfo structure if the signal has
1874 changed. If the debugger wanted something
1875 specific in the siginfo structure then it should
1876 have updated *info via PTRACE_SETSIGINFO. */
1877 if (signr != info->si_signo) {
1878 info->si_signo = signr;
1880 info->si_code = SI_USER;
1881 info->si_pid = current->parent->pid;
1882 info->si_uid = current->parent->uid;
1885 /* If the (new) signal is now blocked, requeue it. */
1886 if (sigismember(¤t->blocked, signr)) {
1887 specific_send_sig_info(signr, info, current);
1892 ka = ¤t->sighand->action[signr-1];
1893 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1895 if (ka->sa.sa_handler != SIG_DFL) {
1896 /* Run the handler. */
1899 if (ka->sa.sa_flags & SA_ONESHOT)
1900 ka->sa.sa_handler = SIG_DFL;
1902 break; /* will return non-zero "signr" value */
1906 * Now we are doing the default action for this signal.
1908 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1911 /* Init gets no signals it doesn't want. */
1912 if (current->pid == 1)
1915 if (sig_kernel_stop(signr)) {
1917 * The default action is to stop all threads in
1918 * the thread group. The job control signals
1919 * do nothing in an orphaned pgrp, but SIGSTOP
1920 * always works. Note that siglock needs to be
1921 * dropped during the call to is_orphaned_pgrp()
1922 * because of lock ordering with tasklist_lock.
1923 * This allows an intervening SIGCONT to be posted.
1924 * We need to check for that and bail out if necessary.
1926 if (signr == SIGSTOP) {
1927 do_signal_stop(signr); /* releases siglock */
1930 spin_unlock_irq(¤t->sighand->siglock);
1932 /* signals can be posted during this window */
1934 if (is_orphaned_pgrp(process_group(current)))
1937 spin_lock_irq(¤t->sighand->siglock);
1938 if (unlikely(sig_avoid_stop_race())) {
1940 * Either a SIGCONT or a SIGKILL signal was
1941 * posted in the siglock-not-held window.
1946 do_signal_stop(signr); /* releases siglock */
1950 spin_unlock_irq(¤t->sighand->siglock);
1953 * Anything else is fatal, maybe with a core dump.
1955 current->flags |= PF_SIGNALED;
1956 if (print_fatal_signals)
1957 print_fatal_signal(regs, signr);
1958 if (sig_kernel_coredump(signr) &&
1959 do_coredump((long)signr, signr, regs)) {
1961 * That killed all other threads in the group and
1962 * synchronized with their demise, so there can't
1963 * be any more left to kill now. The group_exit
1964 * flags are set by do_coredump. Note that
1965 * thread_group_empty won't always be true yet,
1966 * because those threads were blocked in __exit_mm
1967 * and we just let them go to finish dying.
1969 const int code = signr | 0x80;
1970 BUG_ON(!current->signal->group_exit);
1971 BUG_ON(current->signal->group_exit_code != code);
1977 * Death signals, no core dump.
1979 do_group_exit(signr);
1982 spin_unlock_irq(¤t->sighand->siglock);
1988 EXPORT_SYMBOL(recalc_sigpending);
1989 EXPORT_SYMBOL_GPL(dequeue_signal);
1990 EXPORT_SYMBOL(flush_signals);
1991 EXPORT_SYMBOL(force_sig);
1992 EXPORT_SYMBOL(kill_pg);
1993 EXPORT_SYMBOL(kill_proc);
1994 EXPORT_SYMBOL(ptrace_notify);
1995 EXPORT_SYMBOL(send_sig);
1996 EXPORT_SYMBOL(send_sig_info);
1997 EXPORT_SYMBOL(sigprocmask);
1998 EXPORT_SYMBOL(block_all_signals);
1999 EXPORT_SYMBOL(unblock_all_signals);
2002 * System call entry points.
2005 asmlinkage long sys_restart_syscall(void)
2007 struct restart_block *restart = ¤t_thread_info()->restart_block;
2008 return restart->fn(restart);
2011 long do_no_restart_syscall(struct restart_block *param)
2017 * We don't need to get the kernel lock - this is all local to this
2018 * particular thread.. (and that's good, because this is _heavily_
2019 * used by various programs)
2023 * This is also useful for kernel threads that want to temporarily
2024 * (or permanently) block certain signals.
2026 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2027 * interface happily blocks "unblockable" signals like SIGKILL
2030 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2035 spin_lock_irq(¤t->sighand->siglock);
2036 old_block = current->blocked;
2040 sigorsets(¤t->blocked, ¤t->blocked, set);
2043 signandsets(¤t->blocked, ¤t->blocked, set);
2046 current->blocked = *set;
2051 recalc_sigpending();
2052 spin_unlock_irq(¤t->sighand->siglock);
2054 *oldset = old_block;
2059 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2061 int error = -EINVAL;
2062 sigset_t old_set, new_set;
2064 /* XXX: Don't preclude handling different sized sigset_t's. */
2065 if (sigsetsize != sizeof(sigset_t))
2070 if (copy_from_user(&new_set, set, sizeof(*set)))
2072 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2074 error = sigprocmask(how, &new_set, &old_set);
2080 spin_lock_irq(¤t->sighand->siglock);
2081 old_set = current->blocked;
2082 spin_unlock_irq(¤t->sighand->siglock);
2086 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2094 long do_sigpending(void __user *set, unsigned long sigsetsize)
2096 long error = -EINVAL;
2099 if (sigsetsize > sizeof(sigset_t))
2102 spin_lock_irq(¤t->sighand->siglock);
2103 sigorsets(&pending, ¤t->pending.signal,
2104 ¤t->signal->shared_pending.signal);
2105 spin_unlock_irq(¤t->sighand->siglock);
2107 /* Outside the lock because only this thread touches it. */
2108 sigandsets(&pending, ¤t->blocked, &pending);
2111 if (!copy_to_user(set, &pending, sigsetsize))
2119 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2121 return do_sigpending(set, sigsetsize);
2124 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2126 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2130 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2132 if (from->si_code < 0)
2133 return __copy_to_user(to, from, sizeof(siginfo_t))
2136 * If you change siginfo_t structure, please be sure
2137 * this code is fixed accordingly.
2138 * It should never copy any pad contained in the structure
2139 * to avoid security leaks, but must copy the generic
2140 * 3 ints plus the relevant union member.
2142 err = __put_user(from->si_signo, &to->si_signo);
2143 err |= __put_user(from->si_errno, &to->si_errno);
2144 err |= __put_user((short)from->si_code, &to->si_code);
2145 switch (from->si_code & __SI_MASK) {
2147 err |= __put_user(from->si_pid, &to->si_pid);
2148 err |= __put_user(from->si_uid, &to->si_uid);
2151 err |= __put_user(from->si_tid, &to->si_tid);
2152 err |= __put_user(from->si_overrun, &to->si_overrun);
2153 err |= __put_user(from->si_ptr, &to->si_ptr);
2156 err |= __put_user(from->si_band, &to->si_band);
2157 err |= __put_user(from->si_fd, &to->si_fd);
2160 err |= __put_user(from->si_addr, &to->si_addr);
2161 #ifdef __ARCH_SI_TRAPNO
2162 err |= __put_user(from->si_trapno, &to->si_trapno);
2166 err |= __put_user(from->si_pid, &to->si_pid);
2167 err |= __put_user(from->si_uid, &to->si_uid);
2168 err |= __put_user(from->si_status, &to->si_status);
2169 err |= __put_user(from->si_utime, &to->si_utime);
2170 err |= __put_user(from->si_stime, &to->si_stime);
2172 case __SI_RT: /* This is not generated by the kernel as of now. */
2173 case __SI_MESGQ: /* But this is */
2174 err |= __put_user(from->si_pid, &to->si_pid);
2175 err |= __put_user(from->si_uid, &to->si_uid);
2176 err |= __put_user(from->si_ptr, &to->si_ptr);
2178 default: /* this is just in case for now ... */
2179 err |= __put_user(from->si_pid, &to->si_pid);
2180 err |= __put_user(from->si_uid, &to->si_uid);
2189 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2190 siginfo_t __user *uinfo,
2191 const struct timespec __user *uts,
2200 /* XXX: Don't preclude handling different sized sigset_t's. */
2201 if (sigsetsize != sizeof(sigset_t))
2204 if (copy_from_user(&these, uthese, sizeof(these)))
2208 * Invert the set of allowed signals to get those we
2211 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2215 if (copy_from_user(&ts, uts, sizeof(ts)))
2217 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2222 spin_lock_irq(¤t->sighand->siglock);
2223 sig = dequeue_signal(current, &these, &info);
2225 timeout = MAX_SCHEDULE_TIMEOUT;
2227 timeout = (timespec_to_jiffies(&ts)
2228 + (ts.tv_sec || ts.tv_nsec));
2231 /* None ready -- temporarily unblock those we're
2232 * interested while we are sleeping in so that we'll
2233 * be awakened when they arrive. */
2234 current->real_blocked = current->blocked;
2235 sigandsets(¤t->blocked, ¤t->blocked, &these);
2236 recalc_sigpending();
2237 spin_unlock_irq(¤t->sighand->siglock);
2239 current->state = TASK_INTERRUPTIBLE;
2240 timeout = schedule_timeout(timeout);
2242 spin_lock_irq(¤t->sighand->siglock);
2243 sig = dequeue_signal(current, &these, &info);
2244 current->blocked = current->real_blocked;
2245 siginitset(¤t->real_blocked, 0);
2246 recalc_sigpending();
2249 spin_unlock_irq(¤t->sighand->siglock);
2254 if (copy_siginfo_to_user(uinfo, &info))
2267 sys_kill(int pid, int sig)
2269 struct siginfo info;
2271 info.si_signo = sig;
2273 info.si_code = SI_USER;
2274 info.si_pid = current->tgid;
2275 info.si_uid = current->uid;
2277 return kill_something_info(sig, &info, pid);
2281 * sys_tgkill - send signal to one specific thread
2282 * @tgid: the thread group ID of the thread
2283 * @pid: the PID of the thread
2284 * @sig: signal to be sent
2286 * This syscall also checks the tgid and returns -ESRCH even if the PID
2287 * exists but it's not belonging to the target process anymore. This
2288 * method solves the problem of threads exiting and PIDs getting reused.
2290 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2292 struct siginfo info;
2294 struct task_struct *p;
2296 /* This is only valid for single tasks */
2297 if (pid <= 0 || tgid <= 0)
2300 info.si_signo = sig;
2302 info.si_code = SI_TKILL;
2303 info.si_pid = current->tgid;
2304 info.si_uid = current->uid;
2306 read_lock(&tasklist_lock);
2307 p = find_task_by_pid(pid);
2309 if (p && (p->tgid == tgid)) {
2310 error = check_kill_permission(sig, &info, p);
2312 * The null signal is a permissions and process existence
2313 * probe. No signal is actually delivered.
2315 if (!error && sig && p->sighand) {
2316 spin_lock_irq(&p->sighand->siglock);
2317 handle_stop_signal(sig, p);
2318 error = specific_send_sig_info(sig, &info, p);
2319 spin_unlock_irq(&p->sighand->siglock);
2322 read_unlock(&tasklist_lock);
2327 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2330 sys_tkill(int pid, int sig)
2332 struct siginfo info;
2334 struct task_struct *p;
2336 /* This is only valid for single tasks */
2340 info.si_signo = sig;
2342 info.si_code = SI_TKILL;
2343 info.si_pid = current->tgid;
2344 info.si_uid = current->uid;
2346 read_lock(&tasklist_lock);
2347 p = find_task_by_pid(pid);
2350 error = check_kill_permission(sig, &info, p);
2352 * The null signal is a permissions and process existence
2353 * probe. No signal is actually delivered.
2355 if (!error && sig && p->sighand) {
2356 spin_lock_irq(&p->sighand->siglock);
2357 handle_stop_signal(sig, p);
2358 error = specific_send_sig_info(sig, &info, p);
2359 spin_unlock_irq(&p->sighand->siglock);
2362 read_unlock(&tasklist_lock);
2367 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2371 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2374 /* Not even root can pretend to send signals from the kernel.
2375 Nor can they impersonate a kill(), which adds source info. */
2376 if (info.si_code >= 0)
2378 info.si_signo = sig;
2380 /* POSIX.1b doesn't mention process groups. */
2381 return kill_proc_info(sig, &info, pid);
2385 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2387 struct k_sigaction *k;
2389 if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2392 k = ¤t->sighand->action[sig-1];
2394 spin_lock_irq(¤t->sighand->siglock);
2395 if (signal_pending(current)) {
2397 * If there might be a fatal signal pending on multiple
2398 * threads, make sure we take it before changing the action.
2400 spin_unlock_irq(¤t->sighand->siglock);
2401 return -ERESTARTNOINTR;
2410 * "Setting a signal action to SIG_IGN for a signal that is
2411 * pending shall cause the pending signal to be discarded,
2412 * whether or not it is blocked."
2414 * "Setting a signal action to SIG_DFL for a signal that is
2415 * pending and whose default action is to ignore the signal
2416 * (for example, SIGCHLD), shall cause the pending signal to
2417 * be discarded, whether or not it is blocked"
2419 if (act->sa.sa_handler == SIG_IGN ||
2420 (act->sa.sa_handler == SIG_DFL &&
2421 sig_kernel_ignore(sig))) {
2423 * This is a fairly rare case, so we only take the
2424 * tasklist_lock once we're sure we'll need it.
2425 * Now we must do this little unlock and relock
2426 * dance to maintain the lock hierarchy.
2428 struct task_struct *t = current;
2429 spin_unlock_irq(&t->sighand->siglock);
2430 read_lock(&tasklist_lock);
2431 spin_lock_irq(&t->sighand->siglock);
2433 sigdelsetmask(&k->sa.sa_mask,
2434 sigmask(SIGKILL) | sigmask(SIGSTOP));
2435 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2437 rm_from_queue(sigmask(sig), &t->pending);
2438 recalc_sigpending_tsk(t);
2440 } while (t != current);
2441 spin_unlock_irq(¤t->sighand->siglock);
2442 read_unlock(&tasklist_lock);
2447 sigdelsetmask(&k->sa.sa_mask,
2448 sigmask(SIGKILL) | sigmask(SIGSTOP));
2451 spin_unlock_irq(¤t->sighand->siglock);
2456 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2462 oss.ss_sp = (void __user *) current->sas_ss_sp;
2463 oss.ss_size = current->sas_ss_size;
2464 oss.ss_flags = sas_ss_flags(sp);
2473 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2474 || __get_user(ss_sp, &uss->ss_sp)
2475 || __get_user(ss_flags, &uss->ss_flags)
2476 || __get_user(ss_size, &uss->ss_size))
2480 if (on_sig_stack(sp))
2486 * Note - this code used to test ss_flags incorrectly
2487 * old code may have been written using ss_flags==0
2488 * to mean ss_flags==SS_ONSTACK (as this was the only
2489 * way that worked) - this fix preserves that older
2492 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2495 if (ss_flags == SS_DISABLE) {
2500 if (ss_size < MINSIGSTKSZ)
2504 current->sas_ss_sp = (unsigned long) ss_sp;
2505 current->sas_ss_size = ss_size;
2510 if (copy_to_user(uoss, &oss, sizeof(oss)))
2519 #ifdef __ARCH_WANT_SYS_SIGPENDING
2522 sys_sigpending(old_sigset_t __user *set)
2524 return do_sigpending(set, sizeof(*set));
2529 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2530 /* Some platforms have their own version with special arguments others
2531 support only sys_rt_sigprocmask. */
2534 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2537 old_sigset_t old_set, new_set;
2541 if (copy_from_user(&new_set, set, sizeof(*set)))
2543 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2545 spin_lock_irq(¤t->sighand->siglock);
2546 old_set = current->blocked.sig[0];
2554 sigaddsetmask(¤t->blocked, new_set);
2557 sigdelsetmask(¤t->blocked, new_set);
2560 current->blocked.sig[0] = new_set;
2564 recalc_sigpending();
2565 spin_unlock_irq(¤t->sighand->siglock);
2571 old_set = current->blocked.sig[0];
2574 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2581 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2583 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2585 sys_rt_sigaction(int sig,
2586 const struct sigaction __user *act,
2587 struct sigaction __user *oact,
2590 struct k_sigaction new_sa, old_sa;
2593 /* XXX: Don't preclude handling different sized sigset_t's. */
2594 if (sigsetsize != sizeof(sigset_t))
2598 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2602 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2605 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2611 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2613 #ifdef __ARCH_WANT_SYS_SGETMASK
2616 * For backwards compatibility. Functionality superseded by sigprocmask.
2622 return current->blocked.sig[0];
2626 sys_ssetmask(int newmask)
2630 spin_lock_irq(¤t->sighand->siglock);
2631 old = current->blocked.sig[0];
2633 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2635 recalc_sigpending();
2636 spin_unlock_irq(¤t->sighand->siglock);
2640 #endif /* __ARCH_WANT_SGETMASK */
2642 #ifdef __ARCH_WANT_SYS_SIGNAL
2644 * For backwards compatibility. Functionality superseded by sigaction.
2646 asmlinkage unsigned long
2647 sys_signal(int sig, __sighandler_t handler)
2649 struct k_sigaction new_sa, old_sa;
2652 new_sa.sa.sa_handler = handler;
2653 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2655 ret = do_sigaction(sig, &new_sa, &old_sa);
2657 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2659 #endif /* __ARCH_WANT_SYS_SIGNAL */
2661 #ifdef __ARCH_WANT_SYS_PAUSE
2666 current->state = TASK_INTERRUPTIBLE;
2668 return -ERESTARTNOHAND;
2673 void __init signals_init(void)
2676 kmem_cache_create("sigqueue",
2677 sizeof(struct sigqueue),
2678 __alignof__(struct sigqueue),
2679 SLAB_PANIC, NULL, NULL);