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/slab.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
18 #include <linux/tty.h>
19 #include <linux/binfmts.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/ptrace.h>
23 #include <linux/signal.h>
24 #include <linux/signalfd.h>
25 #include <linux/tracehook.h>
26 #include <linux/capability.h>
27 #include <linux/freezer.h>
28 #include <linux/pid_namespace.h>
29 #include <linux/nsproxy.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/signal.h>
33 #include <asm/param.h>
34 #include <asm/uaccess.h>
35 #include <asm/unistd.h>
36 #include <asm/siginfo.h>
37 #include "audit.h" /* audit_signal_info() */
40 * SLAB caches for signal bits.
43 static struct kmem_cache *sigqueue_cachep;
45 static void __user *sig_handler(struct task_struct *t, int sig)
47 return t->sighand->action[sig - 1].sa.sa_handler;
50 static int sig_handler_ignored(void __user *handler, int sig)
52 /* Is it explicitly or implicitly ignored? */
53 return handler == SIG_IGN ||
54 (handler == SIG_DFL && sig_kernel_ignore(sig));
57 static int sig_task_ignored(struct task_struct *t, int sig,
62 handler = sig_handler(t, sig);
64 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
65 handler == SIG_DFL && !from_ancestor_ns)
68 return sig_handler_ignored(handler, sig);
71 static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
74 * Blocked signals are never ignored, since the
75 * signal handler may change by the time it is
78 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
81 if (!sig_task_ignored(t, sig, from_ancestor_ns))
85 * Tracers may want to know about even ignored signals.
87 return !tracehook_consider_ignored_signal(t, sig);
91 * Re-calculate pending state from the set of locally pending
92 * signals, globally pending signals, and blocked signals.
94 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
99 switch (_NSIG_WORDS) {
101 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
102 ready |= signal->sig[i] &~ blocked->sig[i];
105 case 4: ready = signal->sig[3] &~ blocked->sig[3];
106 ready |= signal->sig[2] &~ blocked->sig[2];
107 ready |= signal->sig[1] &~ blocked->sig[1];
108 ready |= signal->sig[0] &~ blocked->sig[0];
111 case 2: ready = signal->sig[1] &~ blocked->sig[1];
112 ready |= signal->sig[0] &~ blocked->sig[0];
115 case 1: ready = signal->sig[0] &~ blocked->sig[0];
120 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
122 static int recalc_sigpending_tsk(struct task_struct *t)
124 if (t->signal->group_stop_count > 0 ||
125 PENDING(&t->pending, &t->blocked) ||
126 PENDING(&t->signal->shared_pending, &t->blocked)) {
127 set_tsk_thread_flag(t, TIF_SIGPENDING);
131 * We must never clear the flag in another thread, or in current
132 * when it's possible the current syscall is returning -ERESTART*.
133 * So we don't clear it here, and only callers who know they should do.
139 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
140 * This is superfluous when called on current, the wakeup is a harmless no-op.
142 void recalc_sigpending_and_wake(struct task_struct *t)
144 if (recalc_sigpending_tsk(t))
145 signal_wake_up(t, 0);
148 void recalc_sigpending(void)
150 if (unlikely(tracehook_force_sigpending()))
151 set_thread_flag(TIF_SIGPENDING);
152 else if (!recalc_sigpending_tsk(current) && !freezing(current))
153 clear_thread_flag(TIF_SIGPENDING);
157 /* Given the mask, find the first available signal that should be serviced. */
159 int next_signal(struct sigpending *pending, sigset_t *mask)
161 unsigned long i, *s, *m, x;
164 s = pending->signal.sig;
166 switch (_NSIG_WORDS) {
168 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
169 if ((x = *s &~ *m) != 0) {
170 sig = ffz(~x) + i*_NSIG_BPW + 1;
175 case 2: if ((x = s[0] &~ m[0]) != 0)
177 else if ((x = s[1] &~ m[1]) != 0)
184 case 1: if ((x = *s &~ *m) != 0)
193 * allocate a new signal queue record
194 * - this may be called without locks if and only if t == current, otherwise an
195 * appopriate lock must be held to stop the target task from exiting
197 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
200 struct sigqueue *q = NULL;
201 struct user_struct *user;
204 * We won't get problems with the target's UID changing under us
205 * because changing it requires RCU be used, and if t != current, the
206 * caller must be holding the RCU readlock (by way of a spinlock) and
207 * we use RCU protection here
209 user = get_uid(__task_cred(t)->user);
210 atomic_inc(&user->sigpending);
211 if (override_rlimit ||
212 atomic_read(&user->sigpending) <=
213 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
214 q = kmem_cache_alloc(sigqueue_cachep, flags);
215 if (unlikely(q == NULL)) {
216 atomic_dec(&user->sigpending);
219 INIT_LIST_HEAD(&q->list);
227 static void __sigqueue_free(struct sigqueue *q)
229 if (q->flags & SIGQUEUE_PREALLOC)
231 atomic_dec(&q->user->sigpending);
233 kmem_cache_free(sigqueue_cachep, q);
236 void flush_sigqueue(struct sigpending *queue)
240 sigemptyset(&queue->signal);
241 while (!list_empty(&queue->list)) {
242 q = list_entry(queue->list.next, struct sigqueue , list);
243 list_del_init(&q->list);
249 * Flush all pending signals for a task.
251 void __flush_signals(struct task_struct *t)
253 clear_tsk_thread_flag(t, TIF_SIGPENDING);
254 flush_sigqueue(&t->pending);
255 flush_sigqueue(&t->signal->shared_pending);
258 void flush_signals(struct task_struct *t)
262 spin_lock_irqsave(&t->sighand->siglock, flags);
264 spin_unlock_irqrestore(&t->sighand->siglock, flags);
267 static void __flush_itimer_signals(struct sigpending *pending)
269 sigset_t signal, retain;
270 struct sigqueue *q, *n;
272 signal = pending->signal;
273 sigemptyset(&retain);
275 list_for_each_entry_safe(q, n, &pending->list, list) {
276 int sig = q->info.si_signo;
278 if (likely(q->info.si_code != SI_TIMER)) {
279 sigaddset(&retain, sig);
281 sigdelset(&signal, sig);
282 list_del_init(&q->list);
287 sigorsets(&pending->signal, &signal, &retain);
290 void flush_itimer_signals(void)
292 struct task_struct *tsk = current;
295 spin_lock_irqsave(&tsk->sighand->siglock, flags);
296 __flush_itimer_signals(&tsk->pending);
297 __flush_itimer_signals(&tsk->signal->shared_pending);
298 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
301 void ignore_signals(struct task_struct *t)
305 for (i = 0; i < _NSIG; ++i)
306 t->sighand->action[i].sa.sa_handler = SIG_IGN;
312 * Flush all handlers for a task.
316 flush_signal_handlers(struct task_struct *t, int force_default)
319 struct k_sigaction *ka = &t->sighand->action[0];
320 for (i = _NSIG ; i != 0 ; i--) {
321 if (force_default || ka->sa.sa_handler != SIG_IGN)
322 ka->sa.sa_handler = SIG_DFL;
324 sigemptyset(&ka->sa.sa_mask);
329 int unhandled_signal(struct task_struct *tsk, int sig)
331 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
332 if (is_global_init(tsk))
334 if (handler != SIG_IGN && handler != SIG_DFL)
336 return !tracehook_consider_fatal_signal(tsk, sig);
340 /* Notify the system that a driver wants to block all signals for this
341 * process, and wants to be notified if any signals at all were to be
342 * sent/acted upon. If the notifier routine returns non-zero, then the
343 * signal will be acted upon after all. If the notifier routine returns 0,
344 * then then signal will be blocked. Only one block per process is
345 * allowed. priv is a pointer to private data that the notifier routine
346 * can use to determine if the signal should be blocked or not. */
349 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
353 spin_lock_irqsave(¤t->sighand->siglock, flags);
354 current->notifier_mask = mask;
355 current->notifier_data = priv;
356 current->notifier = notifier;
357 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
360 /* Notify the system that blocking has ended. */
363 unblock_all_signals(void)
367 spin_lock_irqsave(¤t->sighand->siglock, flags);
368 current->notifier = NULL;
369 current->notifier_data = NULL;
371 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
374 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
376 struct sigqueue *q, *first = NULL;
379 * Collect the siginfo appropriate to this signal. Check if
380 * there is another siginfo for the same signal.
382 list_for_each_entry(q, &list->list, list) {
383 if (q->info.si_signo == sig) {
390 sigdelset(&list->signal, sig);
394 list_del_init(&first->list);
395 copy_siginfo(info, &first->info);
396 __sigqueue_free(first);
398 /* Ok, it wasn't in the queue. This must be
399 a fast-pathed signal or we must have been
400 out of queue space. So zero out the info.
402 info->si_signo = sig;
410 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
413 int sig = next_signal(pending, mask);
416 if (current->notifier) {
417 if (sigismember(current->notifier_mask, sig)) {
418 if (!(current->notifier)(current->notifier_data)) {
419 clear_thread_flag(TIF_SIGPENDING);
425 collect_signal(sig, pending, info);
432 * Dequeue a signal and return the element to the caller, which is
433 * expected to free it.
435 * All callers have to hold the siglock.
437 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
441 /* We only dequeue private signals from ourselves, we don't let
442 * signalfd steal them
444 signr = __dequeue_signal(&tsk->pending, mask, info);
446 signr = __dequeue_signal(&tsk->signal->shared_pending,
451 * itimers are process shared and we restart periodic
452 * itimers in the signal delivery path to prevent DoS
453 * attacks in the high resolution timer case. This is
454 * compliant with the old way of self restarting
455 * itimers, as the SIGALRM is a legacy signal and only
456 * queued once. Changing the restart behaviour to
457 * restart the timer in the signal dequeue path is
458 * reducing the timer noise on heavy loaded !highres
461 if (unlikely(signr == SIGALRM)) {
462 struct hrtimer *tmr = &tsk->signal->real_timer;
464 if (!hrtimer_is_queued(tmr) &&
465 tsk->signal->it_real_incr.tv64 != 0) {
466 hrtimer_forward(tmr, tmr->base->get_time(),
467 tsk->signal->it_real_incr);
468 hrtimer_restart(tmr);
477 if (unlikely(sig_kernel_stop(signr))) {
479 * Set a marker that we have dequeued a stop signal. Our
480 * caller might release the siglock and then the pending
481 * stop signal it is about to process is no longer in the
482 * pending bitmasks, but must still be cleared by a SIGCONT
483 * (and overruled by a SIGKILL). So those cases clear this
484 * shared flag after we've set it. Note that this flag may
485 * remain set after the signal we return is ignored or
486 * handled. That doesn't matter because its only purpose
487 * is to alert stop-signal processing code when another
488 * processor has come along and cleared the flag.
490 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
492 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
494 * Release the siglock to ensure proper locking order
495 * of timer locks outside of siglocks. Note, we leave
496 * irqs disabled here, since the posix-timers code is
497 * about to disable them again anyway.
499 spin_unlock(&tsk->sighand->siglock);
500 do_schedule_next_timer(info);
501 spin_lock(&tsk->sighand->siglock);
507 * Tell a process that it has a new active signal..
509 * NOTE! we rely on the previous spin_lock to
510 * lock interrupts for us! We can only be called with
511 * "siglock" held, and the local interrupt must
512 * have been disabled when that got acquired!
514 * No need to set need_resched since signal event passing
515 * goes through ->blocked
517 void signal_wake_up(struct task_struct *t, int resume)
521 set_tsk_thread_flag(t, TIF_SIGPENDING);
524 * For SIGKILL, we want to wake it up in the stopped/traced/killable
525 * case. We don't check t->state here because there is a race with it
526 * executing another processor and just now entering stopped state.
527 * By using wake_up_state, we ensure the process will wake up and
528 * handle its death signal.
530 mask = TASK_INTERRUPTIBLE;
532 mask |= TASK_WAKEKILL;
533 if (!wake_up_state(t, mask))
538 * Remove signals in mask from the pending set and queue.
539 * Returns 1 if any signals were found.
541 * All callers must be holding the siglock.
543 * This version takes a sigset mask and looks at all signals,
544 * not just those in the first mask word.
546 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
548 struct sigqueue *q, *n;
551 sigandsets(&m, mask, &s->signal);
552 if (sigisemptyset(&m))
555 signandsets(&s->signal, &s->signal, mask);
556 list_for_each_entry_safe(q, n, &s->list, list) {
557 if (sigismember(mask, q->info.si_signo)) {
558 list_del_init(&q->list);
565 * Remove signals in mask from the pending set and queue.
566 * Returns 1 if any signals were found.
568 * All callers must be holding the siglock.
570 static int rm_from_queue(unsigned long mask, struct sigpending *s)
572 struct sigqueue *q, *n;
574 if (!sigtestsetmask(&s->signal, mask))
577 sigdelsetmask(&s->signal, mask);
578 list_for_each_entry_safe(q, n, &s->list, list) {
579 if (q->info.si_signo < SIGRTMIN &&
580 (mask & sigmask(q->info.si_signo))) {
581 list_del_init(&q->list);
589 * Bad permissions for sending the signal
590 * - the caller must hold at least the RCU read lock
592 static int check_kill_permission(int sig, struct siginfo *info,
593 struct task_struct *t)
595 const struct cred *cred = current_cred(), *tcred;
599 if (!valid_signal(sig))
602 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
605 error = audit_signal_info(sig, t); /* Let audit system see the signal */
609 tcred = __task_cred(t);
610 if ((cred->euid ^ tcred->suid) &&
611 (cred->euid ^ tcred->uid) &&
612 (cred->uid ^ tcred->suid) &&
613 (cred->uid ^ tcred->uid) &&
614 !capable(CAP_KILL)) {
617 sid = task_session(t);
619 * We don't return the error if sid == NULL. The
620 * task was unhashed, the caller must notice this.
622 if (!sid || sid == task_session(current))
629 return security_task_kill(t, info, sig, 0);
633 * Handle magic process-wide effects of stop/continue signals. Unlike
634 * the signal actions, these happen immediately at signal-generation
635 * time regardless of blocking, ignoring, or handling. This does the
636 * actual continuing for SIGCONT, but not the actual stopping for stop
637 * signals. The process stop is done as a signal action for SIG_DFL.
639 * Returns true if the signal should be actually delivered, otherwise
640 * it should be dropped.
642 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
644 struct signal_struct *signal = p->signal;
645 struct task_struct *t;
647 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
649 * The process is in the middle of dying, nothing to do.
651 } else if (sig_kernel_stop(sig)) {
653 * This is a stop signal. Remove SIGCONT from all queues.
655 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
658 rm_from_queue(sigmask(SIGCONT), &t->pending);
659 } while_each_thread(p, t);
660 } else if (sig == SIGCONT) {
663 * Remove all stop signals from all queues,
664 * and wake all threads.
666 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
670 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
672 * If there is a handler for SIGCONT, we must make
673 * sure that no thread returns to user mode before
674 * we post the signal, in case it was the only
675 * thread eligible to run the signal handler--then
676 * it must not do anything between resuming and
677 * running the handler. With the TIF_SIGPENDING
678 * flag set, the thread will pause and acquire the
679 * siglock that we hold now and until we've queued
680 * the pending signal.
682 * Wake up the stopped thread _after_ setting
685 state = __TASK_STOPPED;
686 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
687 set_tsk_thread_flag(t, TIF_SIGPENDING);
688 state |= TASK_INTERRUPTIBLE;
690 wake_up_state(t, state);
691 } while_each_thread(p, t);
694 * Notify the parent with CLD_CONTINUED if we were stopped.
696 * If we were in the middle of a group stop, we pretend it
697 * was already finished, and then continued. Since SIGCHLD
698 * doesn't queue we report only CLD_STOPPED, as if the next
699 * CLD_CONTINUED was dropped.
702 if (signal->flags & SIGNAL_STOP_STOPPED)
703 why |= SIGNAL_CLD_CONTINUED;
704 else if (signal->group_stop_count)
705 why |= SIGNAL_CLD_STOPPED;
709 * The first thread which returns from do_signal_stop()
710 * will take ->siglock, notice SIGNAL_CLD_MASK, and
711 * notify its parent. See get_signal_to_deliver().
713 signal->flags = why | SIGNAL_STOP_CONTINUED;
714 signal->group_stop_count = 0;
715 signal->group_exit_code = 0;
718 * We are not stopped, but there could be a stop
719 * signal in the middle of being processed after
720 * being removed from the queue. Clear that too.
722 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
726 return !sig_ignored(p, sig, from_ancestor_ns);
730 * Test if P wants to take SIG. After we've checked all threads with this,
731 * it's equivalent to finding no threads not blocking SIG. Any threads not
732 * blocking SIG were ruled out because they are not running and already
733 * have pending signals. Such threads will dequeue from the shared queue
734 * as soon as they're available, so putting the signal on the shared queue
735 * will be equivalent to sending it to one such thread.
737 static inline int wants_signal(int sig, struct task_struct *p)
739 if (sigismember(&p->blocked, sig))
741 if (p->flags & PF_EXITING)
745 if (task_is_stopped_or_traced(p))
747 return task_curr(p) || !signal_pending(p);
750 static void complete_signal(int sig, struct task_struct *p, int group)
752 struct signal_struct *signal = p->signal;
753 struct task_struct *t;
756 * Now find a thread we can wake up to take the signal off the queue.
758 * If the main thread wants the signal, it gets first crack.
759 * Probably the least surprising to the average bear.
761 if (wants_signal(sig, p))
763 else if (!group || thread_group_empty(p))
765 * There is just one thread and it does not need to be woken.
766 * It will dequeue unblocked signals before it runs again.
771 * Otherwise try to find a suitable thread.
773 t = signal->curr_target;
774 while (!wants_signal(sig, t)) {
776 if (t == signal->curr_target)
778 * No thread needs to be woken.
779 * Any eligible threads will see
780 * the signal in the queue soon.
784 signal->curr_target = t;
788 * Found a killable thread. If the signal will be fatal,
789 * then start taking the whole group down immediately.
791 if (sig_fatal(p, sig) &&
792 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
793 !sigismember(&t->real_blocked, sig) &&
795 !tracehook_consider_fatal_signal(t, sig))) {
797 * This signal will be fatal to the whole group.
799 if (!sig_kernel_coredump(sig)) {
801 * Start a group exit and wake everybody up.
802 * This way we don't have other threads
803 * running and doing things after a slower
804 * thread has the fatal signal pending.
806 signal->flags = SIGNAL_GROUP_EXIT;
807 signal->group_exit_code = sig;
808 signal->group_stop_count = 0;
811 sigaddset(&t->pending.signal, SIGKILL);
812 signal_wake_up(t, 1);
813 } while_each_thread(p, t);
819 * The signal is already in the shared-pending queue.
820 * Tell the chosen thread to wake up and dequeue it.
822 signal_wake_up(t, sig == SIGKILL);
826 static inline int legacy_queue(struct sigpending *signals, int sig)
828 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
831 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
832 int group, int from_ancestor_ns)
834 struct sigpending *pending;
838 trace_signal_generate(sig, info, t);
840 assert_spin_locked(&t->sighand->siglock);
842 if (!prepare_signal(sig, t, from_ancestor_ns))
845 pending = group ? &t->signal->shared_pending : &t->pending;
847 * Short-circuit ignored signals and support queuing
848 * exactly one non-rt signal, so that we can get more
849 * detailed information about the cause of the signal.
851 if (legacy_queue(pending, sig))
854 * fast-pathed signals for kernel-internal things like SIGSTOP
857 if (info == SEND_SIG_FORCED)
860 /* Real-time signals must be queued if sent by sigqueue, or
861 some other real-time mechanism. It is implementation
862 defined whether kill() does so. We attempt to do so, on
863 the principle of least surprise, but since kill is not
864 allowed to fail with EAGAIN when low on memory we just
865 make sure at least one signal gets delivered and don't
866 pass on the info struct. */
869 override_rlimit = (is_si_special(info) || info->si_code >= 0);
873 q = __sigqueue_alloc(t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
876 list_add_tail(&q->list, &pending->list);
877 switch ((unsigned long) info) {
878 case (unsigned long) SEND_SIG_NOINFO:
879 q->info.si_signo = sig;
880 q->info.si_errno = 0;
881 q->info.si_code = SI_USER;
882 q->info.si_pid = task_tgid_nr_ns(current,
883 task_active_pid_ns(t));
884 q->info.si_uid = current_uid();
886 case (unsigned long) SEND_SIG_PRIV:
887 q->info.si_signo = sig;
888 q->info.si_errno = 0;
889 q->info.si_code = SI_KERNEL;
894 copy_siginfo(&q->info, info);
895 if (from_ancestor_ns)
899 } else if (!is_si_special(info)) {
900 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
902 * Queue overflow, abort. We may abort if the
903 * signal was rt and sent by user using something
906 trace_signal_overflow_fail(sig, group, info);
910 * This is a silent loss of information. We still
911 * send the signal, but the *info bits are lost.
913 trace_signal_lose_info(sig, group, info);
918 signalfd_notify(t, sig);
919 sigaddset(&pending->signal, sig);
920 complete_signal(sig, t, group);
924 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
927 int from_ancestor_ns = 0;
930 if (!is_si_special(info) && SI_FROMUSER(info) &&
931 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
932 from_ancestor_ns = 1;
935 return __send_signal(sig, info, t, group, from_ancestor_ns);
938 int print_fatal_signals;
940 static void print_fatal_signal(struct pt_regs *regs, int signr)
942 printk("%s/%d: potentially unexpected fatal signal %d.\n",
943 current->comm, task_pid_nr(current), signr);
945 #if defined(__i386__) && !defined(__arch_um__)
946 printk("code at %08lx: ", regs->ip);
949 for (i = 0; i < 16; i++) {
952 __get_user(insn, (unsigned char *)(regs->ip + i));
953 printk("%02x ", insn);
963 static int __init setup_print_fatal_signals(char *str)
965 get_option (&str, &print_fatal_signals);
970 __setup("print-fatal-signals=", setup_print_fatal_signals);
973 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
975 return send_signal(sig, info, p, 1);
979 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
981 return send_signal(sig, info, t, 0);
984 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
990 if (lock_task_sighand(p, &flags)) {
991 ret = send_signal(sig, info, p, group);
992 unlock_task_sighand(p, &flags);
999 * Force a signal that the process can't ignore: if necessary
1000 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1002 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1003 * since we do not want to have a signal handler that was blocked
1004 * be invoked when user space had explicitly blocked it.
1006 * We don't want to have recursive SIGSEGV's etc, for example,
1007 * that is why we also clear SIGNAL_UNKILLABLE.
1010 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1012 unsigned long int flags;
1013 int ret, blocked, ignored;
1014 struct k_sigaction *action;
1016 spin_lock_irqsave(&t->sighand->siglock, flags);
1017 action = &t->sighand->action[sig-1];
1018 ignored = action->sa.sa_handler == SIG_IGN;
1019 blocked = sigismember(&t->blocked, sig);
1020 if (blocked || ignored) {
1021 action->sa.sa_handler = SIG_DFL;
1023 sigdelset(&t->blocked, sig);
1024 recalc_sigpending_and_wake(t);
1027 if (action->sa.sa_handler == SIG_DFL)
1028 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1029 ret = specific_send_sig_info(sig, info, t);
1030 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1036 force_sig_specific(int sig, struct task_struct *t)
1038 force_sig_info(sig, SEND_SIG_FORCED, t);
1042 * Nuke all other threads in the group.
1044 void zap_other_threads(struct task_struct *p)
1046 struct task_struct *t;
1048 p->signal->group_stop_count = 0;
1050 for (t = next_thread(p); t != p; t = next_thread(t)) {
1052 * Don't bother with already dead threads
1057 /* SIGKILL will be handled before any pending SIGSTOP */
1058 sigaddset(&t->pending.signal, SIGKILL);
1059 signal_wake_up(t, 1);
1063 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1065 struct sighand_struct *sighand;
1069 sighand = rcu_dereference(tsk->sighand);
1070 if (unlikely(sighand == NULL))
1073 spin_lock_irqsave(&sighand->siglock, *flags);
1074 if (likely(sighand == tsk->sighand))
1076 spin_unlock_irqrestore(&sighand->siglock, *flags);
1084 * send signal info to all the members of a group
1085 * - the caller must hold the RCU read lock at least
1087 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1089 int ret = check_kill_permission(sig, info, p);
1092 ret = do_send_sig_info(sig, info, p, true);
1098 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1099 * control characters do (^C, ^Z etc)
1100 * - the caller must hold at least a readlock on tasklist_lock
1102 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1104 struct task_struct *p = NULL;
1105 int retval, success;
1109 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1110 int err = group_send_sig_info(sig, info, p);
1113 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1114 return success ? 0 : retval;
1117 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1120 struct task_struct *p;
1124 p = pid_task(pid, PIDTYPE_PID);
1126 error = group_send_sig_info(sig, info, p);
1127 if (unlikely(error == -ESRCH))
1129 * The task was unhashed in between, try again.
1130 * If it is dead, pid_task() will return NULL,
1131 * if we race with de_thread() it will find the
1142 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1146 error = kill_pid_info(sig, info, find_vpid(pid));
1151 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1152 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1153 uid_t uid, uid_t euid, u32 secid)
1156 struct task_struct *p;
1157 const struct cred *pcred;
1159 if (!valid_signal(sig))
1162 read_lock(&tasklist_lock);
1163 p = pid_task(pid, PIDTYPE_PID);
1168 pcred = __task_cred(p);
1169 if ((info == SEND_SIG_NOINFO ||
1170 (!is_si_special(info) && SI_FROMUSER(info))) &&
1171 euid != pcred->suid && euid != pcred->uid &&
1172 uid != pcred->suid && uid != pcred->uid) {
1176 ret = security_task_kill(p, info, sig, secid);
1179 if (sig && p->sighand) {
1180 unsigned long flags;
1181 spin_lock_irqsave(&p->sighand->siglock, flags);
1182 ret = __send_signal(sig, info, p, 1, 0);
1183 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1186 read_unlock(&tasklist_lock);
1189 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1192 * kill_something_info() interprets pid in interesting ways just like kill(2).
1194 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1195 * is probably wrong. Should make it like BSD or SYSV.
1198 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1204 ret = kill_pid_info(sig, info, find_vpid(pid));
1209 read_lock(&tasklist_lock);
1211 ret = __kill_pgrp_info(sig, info,
1212 pid ? find_vpid(-pid) : task_pgrp(current));
1214 int retval = 0, count = 0;
1215 struct task_struct * p;
1217 for_each_process(p) {
1218 if (task_pid_vnr(p) > 1 &&
1219 !same_thread_group(p, current)) {
1220 int err = group_send_sig_info(sig, info, p);
1226 ret = count ? retval : -ESRCH;
1228 read_unlock(&tasklist_lock);
1234 * These are for backward compatibility with the rest of the kernel source.
1238 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1241 * Make sure legacy kernel users don't send in bad values
1242 * (normal paths check this in check_kill_permission).
1244 if (!valid_signal(sig))
1247 return do_send_sig_info(sig, info, p, false);
1250 #define __si_special(priv) \
1251 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1254 send_sig(int sig, struct task_struct *p, int priv)
1256 return send_sig_info(sig, __si_special(priv), p);
1260 force_sig(int sig, struct task_struct *p)
1262 force_sig_info(sig, SEND_SIG_PRIV, p);
1266 * When things go south during signal handling, we
1267 * will force a SIGSEGV. And if the signal that caused
1268 * the problem was already a SIGSEGV, we'll want to
1269 * make sure we don't even try to deliver the signal..
1272 force_sigsegv(int sig, struct task_struct *p)
1274 if (sig == SIGSEGV) {
1275 unsigned long flags;
1276 spin_lock_irqsave(&p->sighand->siglock, flags);
1277 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1278 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1280 force_sig(SIGSEGV, p);
1284 int kill_pgrp(struct pid *pid, int sig, int priv)
1288 read_lock(&tasklist_lock);
1289 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1290 read_unlock(&tasklist_lock);
1294 EXPORT_SYMBOL(kill_pgrp);
1296 int kill_pid(struct pid *pid, int sig, int priv)
1298 return kill_pid_info(sig, __si_special(priv), pid);
1300 EXPORT_SYMBOL(kill_pid);
1303 * These functions support sending signals using preallocated sigqueue
1304 * structures. This is needed "because realtime applications cannot
1305 * afford to lose notifications of asynchronous events, like timer
1306 * expirations or I/O completions". In the case of Posix Timers
1307 * we allocate the sigqueue structure from the timer_create. If this
1308 * allocation fails we are able to report the failure to the application
1309 * with an EAGAIN error.
1312 struct sigqueue *sigqueue_alloc(void)
1316 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1317 q->flags |= SIGQUEUE_PREALLOC;
1321 void sigqueue_free(struct sigqueue *q)
1323 unsigned long flags;
1324 spinlock_t *lock = ¤t->sighand->siglock;
1326 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1328 * We must hold ->siglock while testing q->list
1329 * to serialize with collect_signal() or with
1330 * __exit_signal()->flush_sigqueue().
1332 spin_lock_irqsave(lock, flags);
1333 q->flags &= ~SIGQUEUE_PREALLOC;
1335 * If it is queued it will be freed when dequeued,
1336 * like the "regular" sigqueue.
1338 if (!list_empty(&q->list))
1340 spin_unlock_irqrestore(lock, flags);
1346 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1348 int sig = q->info.si_signo;
1349 struct sigpending *pending;
1350 unsigned long flags;
1353 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1356 if (!likely(lock_task_sighand(t, &flags)))
1359 ret = 1; /* the signal is ignored */
1360 if (!prepare_signal(sig, t, 0))
1364 if (unlikely(!list_empty(&q->list))) {
1366 * If an SI_TIMER entry is already queue just increment
1367 * the overrun count.
1369 BUG_ON(q->info.si_code != SI_TIMER);
1370 q->info.si_overrun++;
1373 q->info.si_overrun = 0;
1375 signalfd_notify(t, sig);
1376 pending = group ? &t->signal->shared_pending : &t->pending;
1377 list_add_tail(&q->list, &pending->list);
1378 sigaddset(&pending->signal, sig);
1379 complete_signal(sig, t, group);
1381 unlock_task_sighand(t, &flags);
1387 * Let a parent know about the death of a child.
1388 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1390 * Returns -1 if our parent ignored us and so we've switched to
1391 * self-reaping, or else @sig.
1393 int do_notify_parent(struct task_struct *tsk, int sig)
1395 struct siginfo info;
1396 unsigned long flags;
1397 struct sighand_struct *psig;
1402 /* do_notify_parent_cldstop should have been called instead. */
1403 BUG_ON(task_is_stopped_or_traced(tsk));
1405 BUG_ON(!task_ptrace(tsk) &&
1406 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1408 info.si_signo = sig;
1411 * we are under tasklist_lock here so our parent is tied to
1412 * us and cannot exit and release its namespace.
1414 * the only it can is to switch its nsproxy with sys_unshare,
1415 * bu uncharing pid namespaces is not allowed, so we'll always
1416 * see relevant namespace
1418 * write_lock() currently calls preempt_disable() which is the
1419 * same as rcu_read_lock(), but according to Oleg, this is not
1420 * correct to rely on this
1423 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1424 info.si_uid = __task_cred(tsk)->uid;
1427 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1428 tsk->signal->utime));
1429 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1430 tsk->signal->stime));
1432 info.si_status = tsk->exit_code & 0x7f;
1433 if (tsk->exit_code & 0x80)
1434 info.si_code = CLD_DUMPED;
1435 else if (tsk->exit_code & 0x7f)
1436 info.si_code = CLD_KILLED;
1438 info.si_code = CLD_EXITED;
1439 info.si_status = tsk->exit_code >> 8;
1442 psig = tsk->parent->sighand;
1443 spin_lock_irqsave(&psig->siglock, flags);
1444 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1445 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1446 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1448 * We are exiting and our parent doesn't care. POSIX.1
1449 * defines special semantics for setting SIGCHLD to SIG_IGN
1450 * or setting the SA_NOCLDWAIT flag: we should be reaped
1451 * automatically and not left for our parent's wait4 call.
1452 * Rather than having the parent do it as a magic kind of
1453 * signal handler, we just set this to tell do_exit that we
1454 * can be cleaned up without becoming a zombie. Note that
1455 * we still call __wake_up_parent in this case, because a
1456 * blocked sys_wait4 might now return -ECHILD.
1458 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1459 * is implementation-defined: we do (if you don't want
1460 * it, just use SIG_IGN instead).
1462 ret = tsk->exit_signal = -1;
1463 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1466 if (valid_signal(sig) && sig > 0)
1467 __group_send_sig_info(sig, &info, tsk->parent);
1468 __wake_up_parent(tsk, tsk->parent);
1469 spin_unlock_irqrestore(&psig->siglock, flags);
1474 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1476 struct siginfo info;
1477 unsigned long flags;
1478 struct task_struct *parent;
1479 struct sighand_struct *sighand;
1481 if (task_ptrace(tsk))
1482 parent = tsk->parent;
1484 tsk = tsk->group_leader;
1485 parent = tsk->real_parent;
1488 info.si_signo = SIGCHLD;
1491 * see comment in do_notify_parent() abot the following 3 lines
1494 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1495 info.si_uid = __task_cred(tsk)->uid;
1498 info.si_utime = cputime_to_clock_t(tsk->utime);
1499 info.si_stime = cputime_to_clock_t(tsk->stime);
1504 info.si_status = SIGCONT;
1507 info.si_status = tsk->signal->group_exit_code & 0x7f;
1510 info.si_status = tsk->exit_code & 0x7f;
1516 sighand = parent->sighand;
1517 spin_lock_irqsave(&sighand->siglock, flags);
1518 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1519 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1520 __group_send_sig_info(SIGCHLD, &info, parent);
1522 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1524 __wake_up_parent(tsk, parent);
1525 spin_unlock_irqrestore(&sighand->siglock, flags);
1528 static inline int may_ptrace_stop(void)
1530 if (!likely(task_ptrace(current)))
1533 * Are we in the middle of do_coredump?
1534 * If so and our tracer is also part of the coredump stopping
1535 * is a deadlock situation, and pointless because our tracer
1536 * is dead so don't allow us to stop.
1537 * If SIGKILL was already sent before the caller unlocked
1538 * ->siglock we must see ->core_state != NULL. Otherwise it
1539 * is safe to enter schedule().
1541 if (unlikely(current->mm->core_state) &&
1542 unlikely(current->mm == current->parent->mm))
1549 * Return nonzero if there is a SIGKILL that should be waking us up.
1550 * Called with the siglock held.
1552 static int sigkill_pending(struct task_struct *tsk)
1554 return sigismember(&tsk->pending.signal, SIGKILL) ||
1555 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1559 * This must be called with current->sighand->siglock held.
1561 * This should be the path for all ptrace stops.
1562 * We always set current->last_siginfo while stopped here.
1563 * That makes it a way to test a stopped process for
1564 * being ptrace-stopped vs being job-control-stopped.
1566 * If we actually decide not to stop at all because the tracer
1567 * is gone, we keep current->exit_code unless clear_code.
1569 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1571 if (arch_ptrace_stop_needed(exit_code, info)) {
1573 * The arch code has something special to do before a
1574 * ptrace stop. This is allowed to block, e.g. for faults
1575 * on user stack pages. We can't keep the siglock while
1576 * calling arch_ptrace_stop, so we must release it now.
1577 * To preserve proper semantics, we must do this before
1578 * any signal bookkeeping like checking group_stop_count.
1579 * Meanwhile, a SIGKILL could come in before we retake the
1580 * siglock. That must prevent us from sleeping in TASK_TRACED.
1581 * So after regaining the lock, we must check for SIGKILL.
1583 spin_unlock_irq(¤t->sighand->siglock);
1584 arch_ptrace_stop(exit_code, info);
1585 spin_lock_irq(¤t->sighand->siglock);
1586 if (sigkill_pending(current))
1591 * If there is a group stop in progress,
1592 * we must participate in the bookkeeping.
1594 if (current->signal->group_stop_count > 0)
1595 --current->signal->group_stop_count;
1597 current->last_siginfo = info;
1598 current->exit_code = exit_code;
1600 /* Let the debugger run. */
1601 __set_current_state(TASK_TRACED);
1602 spin_unlock_irq(¤t->sighand->siglock);
1603 read_lock(&tasklist_lock);
1604 if (may_ptrace_stop()) {
1605 do_notify_parent_cldstop(current, CLD_TRAPPED);
1607 * Don't want to allow preemption here, because
1608 * sys_ptrace() needs this task to be inactive.
1610 * XXX: implement read_unlock_no_resched().
1613 read_unlock(&tasklist_lock);
1614 preempt_enable_no_resched();
1618 * By the time we got the lock, our tracer went away.
1619 * Don't drop the lock yet, another tracer may come.
1621 __set_current_state(TASK_RUNNING);
1623 current->exit_code = 0;
1624 read_unlock(&tasklist_lock);
1628 * While in TASK_TRACED, we were considered "frozen enough".
1629 * Now that we woke up, it's crucial if we're supposed to be
1630 * frozen that we freeze now before running anything substantial.
1635 * We are back. Now reacquire the siglock before touching
1636 * last_siginfo, so that we are sure to have synchronized with
1637 * any signal-sending on another CPU that wants to examine it.
1639 spin_lock_irq(¤t->sighand->siglock);
1640 current->last_siginfo = NULL;
1643 * Queued signals ignored us while we were stopped for tracing.
1644 * So check for any that we should take before resuming user mode.
1645 * This sets TIF_SIGPENDING, but never clears it.
1647 recalc_sigpending_tsk(current);
1650 void ptrace_notify(int exit_code)
1654 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1656 memset(&info, 0, sizeof info);
1657 info.si_signo = SIGTRAP;
1658 info.si_code = exit_code;
1659 info.si_pid = task_pid_vnr(current);
1660 info.si_uid = current_uid();
1662 /* Let the debugger run. */
1663 spin_lock_irq(¤t->sighand->siglock);
1664 ptrace_stop(exit_code, 1, &info);
1665 spin_unlock_irq(¤t->sighand->siglock);
1669 * This performs the stopping for SIGSTOP and other stop signals.
1670 * We have to stop all threads in the thread group.
1671 * Returns nonzero if we've actually stopped and released the siglock.
1672 * Returns zero if we didn't stop and still hold the siglock.
1674 static int do_signal_stop(int signr)
1676 struct signal_struct *sig = current->signal;
1679 if (!sig->group_stop_count) {
1680 struct task_struct *t;
1682 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1683 unlikely(signal_group_exit(sig)))
1686 * There is no group stop already in progress.
1687 * We must initiate one now.
1689 sig->group_exit_code = signr;
1691 sig->group_stop_count = 1;
1692 for (t = next_thread(current); t != current; t = next_thread(t))
1694 * Setting state to TASK_STOPPED for a group
1695 * stop is always done with the siglock held,
1696 * so this check has no races.
1698 if (!(t->flags & PF_EXITING) &&
1699 !task_is_stopped_or_traced(t)) {
1700 sig->group_stop_count++;
1701 signal_wake_up(t, 0);
1705 * If there are no other threads in the group, or if there is
1706 * a group stop in progress and we are the last to stop, report
1707 * to the parent. When ptraced, every thread reports itself.
1709 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1710 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1712 * tracehook_notify_jctl() can drop and reacquire siglock, so
1713 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1714 * or SIGKILL comes in between ->group_stop_count == 0.
1716 if (sig->group_stop_count) {
1717 if (!--sig->group_stop_count)
1718 sig->flags = SIGNAL_STOP_STOPPED;
1719 current->exit_code = sig->group_exit_code;
1720 __set_current_state(TASK_STOPPED);
1722 spin_unlock_irq(¤t->sighand->siglock);
1725 read_lock(&tasklist_lock);
1726 do_notify_parent_cldstop(current, notify);
1727 read_unlock(&tasklist_lock);
1730 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1733 } while (try_to_freeze());
1735 tracehook_finish_jctl();
1736 current->exit_code = 0;
1741 static int ptrace_signal(int signr, siginfo_t *info,
1742 struct pt_regs *regs, void *cookie)
1744 if (!task_ptrace(current))
1747 ptrace_signal_deliver(regs, cookie);
1749 /* Let the debugger run. */
1750 ptrace_stop(signr, 0, info);
1752 /* We're back. Did the debugger cancel the sig? */
1753 signr = current->exit_code;
1757 current->exit_code = 0;
1759 /* Update the siginfo structure if the signal has
1760 changed. If the debugger wanted something
1761 specific in the siginfo structure then it should
1762 have updated *info via PTRACE_SETSIGINFO. */
1763 if (signr != info->si_signo) {
1764 info->si_signo = signr;
1766 info->si_code = SI_USER;
1767 info->si_pid = task_pid_vnr(current->parent);
1768 info->si_uid = task_uid(current->parent);
1771 /* If the (new) signal is now blocked, requeue it. */
1772 if (sigismember(¤t->blocked, signr)) {
1773 specific_send_sig_info(signr, info, current);
1780 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1781 struct pt_regs *regs, void *cookie)
1783 struct sighand_struct *sighand = current->sighand;
1784 struct signal_struct *signal = current->signal;
1789 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1790 * While in TASK_STOPPED, we were considered "frozen enough".
1791 * Now that we woke up, it's crucial if we're supposed to be
1792 * frozen that we freeze now before running anything substantial.
1796 spin_lock_irq(&sighand->siglock);
1798 * Every stopped thread goes here after wakeup. Check to see if
1799 * we should notify the parent, prepare_signal(SIGCONT) encodes
1800 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1802 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1803 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1804 ? CLD_CONTINUED : CLD_STOPPED;
1805 signal->flags &= ~SIGNAL_CLD_MASK;
1807 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1808 spin_unlock_irq(&sighand->siglock);
1811 read_lock(&tasklist_lock);
1812 do_notify_parent_cldstop(current->group_leader, why);
1813 read_unlock(&tasklist_lock);
1819 struct k_sigaction *ka;
1821 if (unlikely(signal->group_stop_count > 0) &&
1826 * Tracing can induce an artifical signal and choose sigaction.
1827 * The return value in @signr determines the default action,
1828 * but @info->si_signo is the signal number we will report.
1830 signr = tracehook_get_signal(current, regs, info, return_ka);
1831 if (unlikely(signr < 0))
1833 if (unlikely(signr != 0))
1836 signr = dequeue_signal(current, ¤t->blocked,
1840 break; /* will return 0 */
1842 if (signr != SIGKILL) {
1843 signr = ptrace_signal(signr, info,
1849 ka = &sighand->action[signr-1];
1852 /* Trace actually delivered signals. */
1853 trace_signal_deliver(signr, info, ka);
1855 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1857 if (ka->sa.sa_handler != SIG_DFL) {
1858 /* Run the handler. */
1861 if (ka->sa.sa_flags & SA_ONESHOT)
1862 ka->sa.sa_handler = SIG_DFL;
1864 break; /* will return non-zero "signr" value */
1868 * Now we are doing the default action for this signal.
1870 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1874 * Global init gets no signals it doesn't want.
1875 * Container-init gets no signals it doesn't want from same
1878 * Note that if global/container-init sees a sig_kernel_only()
1879 * signal here, the signal must have been generated internally
1880 * or must have come from an ancestor namespace. In either
1881 * case, the signal cannot be dropped.
1883 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1884 !sig_kernel_only(signr))
1887 if (sig_kernel_stop(signr)) {
1889 * The default action is to stop all threads in
1890 * the thread group. The job control signals
1891 * do nothing in an orphaned pgrp, but SIGSTOP
1892 * always works. Note that siglock needs to be
1893 * dropped during the call to is_orphaned_pgrp()
1894 * because of lock ordering with tasklist_lock.
1895 * This allows an intervening SIGCONT to be posted.
1896 * We need to check for that and bail out if necessary.
1898 if (signr != SIGSTOP) {
1899 spin_unlock_irq(&sighand->siglock);
1901 /* signals can be posted during this window */
1903 if (is_current_pgrp_orphaned())
1906 spin_lock_irq(&sighand->siglock);
1909 if (likely(do_signal_stop(info->si_signo))) {
1910 /* It released the siglock. */
1915 * We didn't actually stop, due to a race
1916 * with SIGCONT or something like that.
1921 spin_unlock_irq(&sighand->siglock);
1924 * Anything else is fatal, maybe with a core dump.
1926 current->flags |= PF_SIGNALED;
1928 if (sig_kernel_coredump(signr)) {
1929 if (print_fatal_signals)
1930 print_fatal_signal(regs, info->si_signo);
1932 * If it was able to dump core, this kills all
1933 * other threads in the group and synchronizes with
1934 * their demise. If we lost the race with another
1935 * thread getting here, it set group_exit_code
1936 * first and our do_group_exit call below will use
1937 * that value and ignore the one we pass it.
1939 do_coredump(info->si_signo, info->si_signo, regs);
1943 * Death signals, no core dump.
1945 do_group_exit(info->si_signo);
1948 spin_unlock_irq(&sighand->siglock);
1952 void exit_signals(struct task_struct *tsk)
1955 struct task_struct *t;
1957 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1958 tsk->flags |= PF_EXITING;
1962 spin_lock_irq(&tsk->sighand->siglock);
1964 * From now this task is not visible for group-wide signals,
1965 * see wants_signal(), do_signal_stop().
1967 tsk->flags |= PF_EXITING;
1968 if (!signal_pending(tsk))
1971 /* It could be that __group_complete_signal() choose us to
1972 * notify about group-wide signal. Another thread should be
1973 * woken now to take the signal since we will not.
1975 for (t = tsk; (t = next_thread(t)) != tsk; )
1976 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1977 recalc_sigpending_and_wake(t);
1979 if (unlikely(tsk->signal->group_stop_count) &&
1980 !--tsk->signal->group_stop_count) {
1981 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1982 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
1985 spin_unlock_irq(&tsk->sighand->siglock);
1987 if (unlikely(group_stop)) {
1988 read_lock(&tasklist_lock);
1989 do_notify_parent_cldstop(tsk, group_stop);
1990 read_unlock(&tasklist_lock);
1994 EXPORT_SYMBOL(recalc_sigpending);
1995 EXPORT_SYMBOL_GPL(dequeue_signal);
1996 EXPORT_SYMBOL(flush_signals);
1997 EXPORT_SYMBOL(force_sig);
1998 EXPORT_SYMBOL(send_sig);
1999 EXPORT_SYMBOL(send_sig_info);
2000 EXPORT_SYMBOL(sigprocmask);
2001 EXPORT_SYMBOL(block_all_signals);
2002 EXPORT_SYMBOL(unblock_all_signals);
2006 * System call entry points.
2009 SYSCALL_DEFINE0(restart_syscall)
2011 struct restart_block *restart = ¤t_thread_info()->restart_block;
2012 return restart->fn(restart);
2015 long do_no_restart_syscall(struct restart_block *param)
2021 * We don't need to get the kernel lock - this is all local to this
2022 * particular thread.. (and that's good, because this is _heavily_
2023 * used by various programs)
2027 * This is also useful for kernel threads that want to temporarily
2028 * (or permanently) block certain signals.
2030 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2031 * interface happily blocks "unblockable" signals like SIGKILL
2034 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2038 spin_lock_irq(¤t->sighand->siglock);
2040 *oldset = current->blocked;
2045 sigorsets(¤t->blocked, ¤t->blocked, set);
2048 signandsets(¤t->blocked, ¤t->blocked, set);
2051 current->blocked = *set;
2056 recalc_sigpending();
2057 spin_unlock_irq(¤t->sighand->siglock);
2062 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2063 sigset_t __user *, oset, size_t, sigsetsize)
2065 int error = -EINVAL;
2066 sigset_t old_set, new_set;
2068 /* XXX: Don't preclude handling different sized sigset_t's. */
2069 if (sigsetsize != sizeof(sigset_t))
2074 if (copy_from_user(&new_set, set, sizeof(*set)))
2076 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2078 error = sigprocmask(how, &new_set, &old_set);
2084 spin_lock_irq(¤t->sighand->siglock);
2085 old_set = current->blocked;
2086 spin_unlock_irq(¤t->sighand->siglock);
2090 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2098 long do_sigpending(void __user *set, unsigned long sigsetsize)
2100 long error = -EINVAL;
2103 if (sigsetsize > sizeof(sigset_t))
2106 spin_lock_irq(¤t->sighand->siglock);
2107 sigorsets(&pending, ¤t->pending.signal,
2108 ¤t->signal->shared_pending.signal);
2109 spin_unlock_irq(¤t->sighand->siglock);
2111 /* Outside the lock because only this thread touches it. */
2112 sigandsets(&pending, ¤t->blocked, &pending);
2115 if (!copy_to_user(set, &pending, sigsetsize))
2122 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2124 return do_sigpending(set, sigsetsize);
2127 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2129 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2133 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2135 if (from->si_code < 0)
2136 return __copy_to_user(to, from, sizeof(siginfo_t))
2139 * If you change siginfo_t structure, please be sure
2140 * this code is fixed accordingly.
2141 * Please remember to update the signalfd_copyinfo() function
2142 * inside fs/signalfd.c too, in case siginfo_t changes.
2143 * It should never copy any pad contained in the structure
2144 * to avoid security leaks, but must copy the generic
2145 * 3 ints plus the relevant union member.
2147 err = __put_user(from->si_signo, &to->si_signo);
2148 err |= __put_user(from->si_errno, &to->si_errno);
2149 err |= __put_user((short)from->si_code, &to->si_code);
2150 switch (from->si_code & __SI_MASK) {
2152 err |= __put_user(from->si_pid, &to->si_pid);
2153 err |= __put_user(from->si_uid, &to->si_uid);
2156 err |= __put_user(from->si_tid, &to->si_tid);
2157 err |= __put_user(from->si_overrun, &to->si_overrun);
2158 err |= __put_user(from->si_ptr, &to->si_ptr);
2161 err |= __put_user(from->si_band, &to->si_band);
2162 err |= __put_user(from->si_fd, &to->si_fd);
2165 err |= __put_user(from->si_addr, &to->si_addr);
2166 #ifdef __ARCH_SI_TRAPNO
2167 err |= __put_user(from->si_trapno, &to->si_trapno);
2171 err |= __put_user(from->si_pid, &to->si_pid);
2172 err |= __put_user(from->si_uid, &to->si_uid);
2173 err |= __put_user(from->si_status, &to->si_status);
2174 err |= __put_user(from->si_utime, &to->si_utime);
2175 err |= __put_user(from->si_stime, &to->si_stime);
2177 case __SI_RT: /* This is not generated by the kernel as of now. */
2178 case __SI_MESGQ: /* But this is */
2179 err |= __put_user(from->si_pid, &to->si_pid);
2180 err |= __put_user(from->si_uid, &to->si_uid);
2181 err |= __put_user(from->si_ptr, &to->si_ptr);
2183 default: /* this is just in case for now ... */
2184 err |= __put_user(from->si_pid, &to->si_pid);
2185 err |= __put_user(from->si_uid, &to->si_uid);
2193 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2194 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2203 /* XXX: Don't preclude handling different sized sigset_t's. */
2204 if (sigsetsize != sizeof(sigset_t))
2207 if (copy_from_user(&these, uthese, sizeof(these)))
2211 * Invert the set of allowed signals to get those we
2214 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2218 if (copy_from_user(&ts, uts, sizeof(ts)))
2220 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2225 spin_lock_irq(¤t->sighand->siglock);
2226 sig = dequeue_signal(current, &these, &info);
2228 timeout = MAX_SCHEDULE_TIMEOUT;
2230 timeout = (timespec_to_jiffies(&ts)
2231 + (ts.tv_sec || ts.tv_nsec));
2234 /* None ready -- temporarily unblock those we're
2235 * interested while we are sleeping in so that we'll
2236 * be awakened when they arrive. */
2237 current->real_blocked = current->blocked;
2238 sigandsets(¤t->blocked, ¤t->blocked, &these);
2239 recalc_sigpending();
2240 spin_unlock_irq(¤t->sighand->siglock);
2242 timeout = schedule_timeout_interruptible(timeout);
2244 spin_lock_irq(¤t->sighand->siglock);
2245 sig = dequeue_signal(current, &these, &info);
2246 current->blocked = current->real_blocked;
2247 siginitset(¤t->real_blocked, 0);
2248 recalc_sigpending();
2251 spin_unlock_irq(¤t->sighand->siglock);
2256 if (copy_siginfo_to_user(uinfo, &info))
2268 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2270 struct siginfo info;
2272 info.si_signo = sig;
2274 info.si_code = SI_USER;
2275 info.si_pid = task_tgid_vnr(current);
2276 info.si_uid = current_uid();
2278 return kill_something_info(sig, &info, pid);
2282 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2284 struct task_struct *p;
2288 p = find_task_by_vpid(pid);
2289 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2290 error = check_kill_permission(sig, info, p);
2292 * The null signal is a permissions and process existence
2293 * probe. No signal is actually delivered.
2295 if (!error && sig) {
2296 error = do_send_sig_info(sig, info, p, false);
2298 * If lock_task_sighand() failed we pretend the task
2299 * dies after receiving the signal. The window is tiny,
2300 * and the signal is private anyway.
2302 if (unlikely(error == -ESRCH))
2311 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2313 struct siginfo info;
2315 info.si_signo = sig;
2317 info.si_code = SI_TKILL;
2318 info.si_pid = task_tgid_vnr(current);
2319 info.si_uid = current_uid();
2321 return do_send_specific(tgid, pid, sig, &info);
2325 * sys_tgkill - send signal to one specific thread
2326 * @tgid: the thread group ID of the thread
2327 * @pid: the PID of the thread
2328 * @sig: signal to be sent
2330 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2331 * exists but it's not belonging to the target process anymore. This
2332 * method solves the problem of threads exiting and PIDs getting reused.
2334 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2336 /* This is only valid for single tasks */
2337 if (pid <= 0 || tgid <= 0)
2340 return do_tkill(tgid, pid, sig);
2344 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2346 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2348 /* This is only valid for single tasks */
2352 return do_tkill(0, pid, sig);
2355 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2356 siginfo_t __user *, uinfo)
2360 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2363 /* Not even root can pretend to send signals from the kernel.
2364 Nor can they impersonate a kill(), which adds source info. */
2365 if (info.si_code >= 0)
2367 info.si_signo = sig;
2369 /* POSIX.1b doesn't mention process groups. */
2370 return kill_proc_info(sig, &info, pid);
2373 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2375 /* This is only valid for single tasks */
2376 if (pid <= 0 || tgid <= 0)
2379 /* Not even root can pretend to send signals from the kernel.
2380 Nor can they impersonate a kill(), which adds source info. */
2381 if (info->si_code >= 0)
2383 info->si_signo = sig;
2385 return do_send_specific(tgid, pid, sig, info);
2388 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2389 siginfo_t __user *, uinfo)
2393 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2396 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2399 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2401 struct task_struct *t = current;
2402 struct k_sigaction *k;
2405 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2408 k = &t->sighand->action[sig-1];
2410 spin_lock_irq(¤t->sighand->siglock);
2415 sigdelsetmask(&act->sa.sa_mask,
2416 sigmask(SIGKILL) | sigmask(SIGSTOP));
2420 * "Setting a signal action to SIG_IGN for a signal that is
2421 * pending shall cause the pending signal to be discarded,
2422 * whether or not it is blocked."
2424 * "Setting a signal action to SIG_DFL for a signal that is
2425 * pending and whose default action is to ignore the signal
2426 * (for example, SIGCHLD), shall cause the pending signal to
2427 * be discarded, whether or not it is blocked"
2429 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2431 sigaddset(&mask, sig);
2432 rm_from_queue_full(&mask, &t->signal->shared_pending);
2434 rm_from_queue_full(&mask, &t->pending);
2436 } while (t != current);
2440 spin_unlock_irq(¤t->sighand->siglock);
2445 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2450 oss.ss_sp = (void __user *) current->sas_ss_sp;
2451 oss.ss_size = current->sas_ss_size;
2452 oss.ss_flags = sas_ss_flags(sp);
2460 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2462 error = __get_user(ss_sp, &uss->ss_sp) |
2463 __get_user(ss_flags, &uss->ss_flags) |
2464 __get_user(ss_size, &uss->ss_size);
2469 if (on_sig_stack(sp))
2475 * Note - this code used to test ss_flags incorrectly
2476 * old code may have been written using ss_flags==0
2477 * to mean ss_flags==SS_ONSTACK (as this was the only
2478 * way that worked) - this fix preserves that older
2481 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2484 if (ss_flags == SS_DISABLE) {
2489 if (ss_size < MINSIGSTKSZ)
2493 current->sas_ss_sp = (unsigned long) ss_sp;
2494 current->sas_ss_size = ss_size;
2500 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2502 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2503 __put_user(oss.ss_size, &uoss->ss_size) |
2504 __put_user(oss.ss_flags, &uoss->ss_flags);
2511 #ifdef __ARCH_WANT_SYS_SIGPENDING
2513 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2515 return do_sigpending(set, sizeof(*set));
2520 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2521 /* Some platforms have their own version with special arguments others
2522 support only sys_rt_sigprocmask. */
2524 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2525 old_sigset_t __user *, oset)
2528 old_sigset_t old_set, new_set;
2532 if (copy_from_user(&new_set, set, sizeof(*set)))
2534 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2536 spin_lock_irq(¤t->sighand->siglock);
2537 old_set = current->blocked.sig[0];
2545 sigaddsetmask(¤t->blocked, new_set);
2548 sigdelsetmask(¤t->blocked, new_set);
2551 current->blocked.sig[0] = new_set;
2555 recalc_sigpending();
2556 spin_unlock_irq(¤t->sighand->siglock);
2562 old_set = current->blocked.sig[0];
2565 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2572 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2574 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2575 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2576 const struct sigaction __user *, act,
2577 struct sigaction __user *, oact,
2580 struct k_sigaction new_sa, old_sa;
2583 /* XXX: Don't preclude handling different sized sigset_t's. */
2584 if (sigsetsize != sizeof(sigset_t))
2588 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2592 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2595 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2601 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2603 #ifdef __ARCH_WANT_SYS_SGETMASK
2606 * For backwards compatibility. Functionality superseded by sigprocmask.
2608 SYSCALL_DEFINE0(sgetmask)
2611 return current->blocked.sig[0];
2614 SYSCALL_DEFINE1(ssetmask, int, newmask)
2618 spin_lock_irq(¤t->sighand->siglock);
2619 old = current->blocked.sig[0];
2621 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2623 recalc_sigpending();
2624 spin_unlock_irq(¤t->sighand->siglock);
2628 #endif /* __ARCH_WANT_SGETMASK */
2630 #ifdef __ARCH_WANT_SYS_SIGNAL
2632 * For backwards compatibility. Functionality superseded by sigaction.
2634 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2636 struct k_sigaction new_sa, old_sa;
2639 new_sa.sa.sa_handler = handler;
2640 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2641 sigemptyset(&new_sa.sa.sa_mask);
2643 ret = do_sigaction(sig, &new_sa, &old_sa);
2645 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2647 #endif /* __ARCH_WANT_SYS_SIGNAL */
2649 #ifdef __ARCH_WANT_SYS_PAUSE
2651 SYSCALL_DEFINE0(pause)
2653 current->state = TASK_INTERRUPTIBLE;
2655 return -ERESTARTNOHAND;
2660 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2661 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2665 /* XXX: Don't preclude handling different sized sigset_t's. */
2666 if (sigsetsize != sizeof(sigset_t))
2669 if (copy_from_user(&newset, unewset, sizeof(newset)))
2671 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2673 spin_lock_irq(¤t->sighand->siglock);
2674 current->saved_sigmask = current->blocked;
2675 current->blocked = newset;
2676 recalc_sigpending();
2677 spin_unlock_irq(¤t->sighand->siglock);
2679 current->state = TASK_INTERRUPTIBLE;
2681 set_restore_sigmask();
2682 return -ERESTARTNOHAND;
2684 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2686 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2691 void __init signals_init(void)
2693 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);