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/ratelimit.h>
26 #include <linux/tracehook.h>
27 #include <linux/capability.h>
28 #include <linux/freezer.h>
29 #include <linux/pid_namespace.h>
30 #include <linux/nsproxy.h>
31 #include <trace/events/sched.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 int print_fatal_signals __read_mostly;
47 static void __user *sig_handler(struct task_struct *t, int sig)
49 return t->sighand->action[sig - 1].sa.sa_handler;
52 static int sig_handler_ignored(void __user *handler, int sig)
54 /* Is it explicitly or implicitly ignored? */
55 return handler == SIG_IGN ||
56 (handler == SIG_DFL && sig_kernel_ignore(sig));
59 static int sig_task_ignored(struct task_struct *t, int sig,
64 handler = sig_handler(t, sig);
66 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
67 handler == SIG_DFL && !from_ancestor_ns)
70 return sig_handler_ignored(handler, sig);
73 static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
76 * Blocked signals are never ignored, since the
77 * signal handler may change by the time it is
80 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
83 if (!sig_task_ignored(t, sig, from_ancestor_ns))
87 * Tracers may want to know about even ignored signals.
89 return !tracehook_consider_ignored_signal(t, sig);
93 * Re-calculate pending state from the set of locally pending
94 * signals, globally pending signals, and blocked signals.
96 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
101 switch (_NSIG_WORDS) {
103 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
104 ready |= signal->sig[i] &~ blocked->sig[i];
107 case 4: ready = signal->sig[3] &~ blocked->sig[3];
108 ready |= signal->sig[2] &~ blocked->sig[2];
109 ready |= signal->sig[1] &~ blocked->sig[1];
110 ready |= signal->sig[0] &~ blocked->sig[0];
113 case 2: ready = signal->sig[1] &~ blocked->sig[1];
114 ready |= signal->sig[0] &~ blocked->sig[0];
117 case 1: ready = signal->sig[0] &~ blocked->sig[0];
122 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
124 static int recalc_sigpending_tsk(struct task_struct *t)
126 if (t->signal->group_stop_count > 0 ||
127 PENDING(&t->pending, &t->blocked) ||
128 PENDING(&t->signal->shared_pending, &t->blocked)) {
129 set_tsk_thread_flag(t, TIF_SIGPENDING);
133 * We must never clear the flag in another thread, or in current
134 * when it's possible the current syscall is returning -ERESTART*.
135 * So we don't clear it here, and only callers who know they should do.
141 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
142 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 void recalc_sigpending_and_wake(struct task_struct *t)
146 if (recalc_sigpending_tsk(t))
147 signal_wake_up(t, 0);
150 void recalc_sigpending(void)
152 if (unlikely(tracehook_force_sigpending()))
153 set_thread_flag(TIF_SIGPENDING);
154 else if (!recalc_sigpending_tsk(current) && !freezing(current))
155 clear_thread_flag(TIF_SIGPENDING);
159 /* Given the mask, find the first available signal that should be serviced. */
161 int next_signal(struct sigpending *pending, sigset_t *mask)
163 unsigned long i, *s, *m, x;
166 s = pending->signal.sig;
168 switch (_NSIG_WORDS) {
170 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
171 if ((x = *s &~ *m) != 0) {
172 sig = ffz(~x) + i*_NSIG_BPW + 1;
177 case 2: if ((x = s[0] &~ m[0]) != 0)
179 else if ((x = s[1] &~ m[1]) != 0)
186 case 1: if ((x = *s &~ *m) != 0)
194 static inline void print_dropped_signal(int sig)
196 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
198 if (!print_fatal_signals)
201 if (!__ratelimit(&ratelimit_state))
204 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
205 current->comm, current->pid, sig);
209 * allocate a new signal queue record
210 * - this may be called without locks if and only if t == current, otherwise an
211 * appopriate lock must be held to stop the target task from exiting
213 static struct sigqueue *
214 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
216 struct sigqueue *q = NULL;
217 struct user_struct *user;
220 * We won't get problems with the target's UID changing under us
221 * because changing it requires RCU be used, and if t != current, the
222 * caller must be holding the RCU readlock (by way of a spinlock) and
223 * we use RCU protection here
225 user = get_uid(__task_cred(t)->user);
226 atomic_inc(&user->sigpending);
228 if (override_rlimit ||
229 atomic_read(&user->sigpending) <=
230 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) {
231 q = kmem_cache_alloc(sigqueue_cachep, flags);
233 print_dropped_signal(sig);
236 if (unlikely(q == NULL)) {
237 atomic_dec(&user->sigpending);
240 INIT_LIST_HEAD(&q->list);
248 static void __sigqueue_free(struct sigqueue *q)
250 if (q->flags & SIGQUEUE_PREALLOC)
252 atomic_dec(&q->user->sigpending);
254 kmem_cache_free(sigqueue_cachep, q);
257 void flush_sigqueue(struct sigpending *queue)
261 sigemptyset(&queue->signal);
262 while (!list_empty(&queue->list)) {
263 q = list_entry(queue->list.next, struct sigqueue , list);
264 list_del_init(&q->list);
270 * Flush all pending signals for a task.
272 void __flush_signals(struct task_struct *t)
274 clear_tsk_thread_flag(t, TIF_SIGPENDING);
275 flush_sigqueue(&t->pending);
276 flush_sigqueue(&t->signal->shared_pending);
279 void flush_signals(struct task_struct *t)
283 spin_lock_irqsave(&t->sighand->siglock, flags);
285 spin_unlock_irqrestore(&t->sighand->siglock, flags);
288 static void __flush_itimer_signals(struct sigpending *pending)
290 sigset_t signal, retain;
291 struct sigqueue *q, *n;
293 signal = pending->signal;
294 sigemptyset(&retain);
296 list_for_each_entry_safe(q, n, &pending->list, list) {
297 int sig = q->info.si_signo;
299 if (likely(q->info.si_code != SI_TIMER)) {
300 sigaddset(&retain, sig);
302 sigdelset(&signal, sig);
303 list_del_init(&q->list);
308 sigorsets(&pending->signal, &signal, &retain);
311 void flush_itimer_signals(void)
313 struct task_struct *tsk = current;
316 spin_lock_irqsave(&tsk->sighand->siglock, flags);
317 __flush_itimer_signals(&tsk->pending);
318 __flush_itimer_signals(&tsk->signal->shared_pending);
319 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
322 void ignore_signals(struct task_struct *t)
326 for (i = 0; i < _NSIG; ++i)
327 t->sighand->action[i].sa.sa_handler = SIG_IGN;
333 * Flush all handlers for a task.
337 flush_signal_handlers(struct task_struct *t, int force_default)
340 struct k_sigaction *ka = &t->sighand->action[0];
341 for (i = _NSIG ; i != 0 ; i--) {
342 if (force_default || ka->sa.sa_handler != SIG_IGN)
343 ka->sa.sa_handler = SIG_DFL;
345 sigemptyset(&ka->sa.sa_mask);
350 int unhandled_signal(struct task_struct *tsk, int sig)
352 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
353 if (is_global_init(tsk))
355 if (handler != SIG_IGN && handler != SIG_DFL)
357 return !tracehook_consider_fatal_signal(tsk, sig);
361 /* Notify the system that a driver wants to block all signals for this
362 * process, and wants to be notified if any signals at all were to be
363 * sent/acted upon. If the notifier routine returns non-zero, then the
364 * signal will be acted upon after all. If the notifier routine returns 0,
365 * then then signal will be blocked. Only one block per process is
366 * allowed. priv is a pointer to private data that the notifier routine
367 * can use to determine if the signal should be blocked or not. */
370 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
374 spin_lock_irqsave(¤t->sighand->siglock, flags);
375 current->notifier_mask = mask;
376 current->notifier_data = priv;
377 current->notifier = notifier;
378 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
381 /* Notify the system that blocking has ended. */
384 unblock_all_signals(void)
388 spin_lock_irqsave(¤t->sighand->siglock, flags);
389 current->notifier = NULL;
390 current->notifier_data = NULL;
392 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
395 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
397 struct sigqueue *q, *first = NULL;
400 * Collect the siginfo appropriate to this signal. Check if
401 * there is another siginfo for the same signal.
403 list_for_each_entry(q, &list->list, list) {
404 if (q->info.si_signo == sig) {
411 sigdelset(&list->signal, sig);
415 list_del_init(&first->list);
416 copy_siginfo(info, &first->info);
417 __sigqueue_free(first);
419 /* Ok, it wasn't in the queue. This must be
420 a fast-pathed signal or we must have been
421 out of queue space. So zero out the info.
423 info->si_signo = sig;
431 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
434 int sig = next_signal(pending, mask);
437 if (current->notifier) {
438 if (sigismember(current->notifier_mask, sig)) {
439 if (!(current->notifier)(current->notifier_data)) {
440 clear_thread_flag(TIF_SIGPENDING);
446 collect_signal(sig, pending, info);
453 * Dequeue a signal and return the element to the caller, which is
454 * expected to free it.
456 * All callers have to hold the siglock.
458 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
462 /* We only dequeue private signals from ourselves, we don't let
463 * signalfd steal them
465 signr = __dequeue_signal(&tsk->pending, mask, info);
467 signr = __dequeue_signal(&tsk->signal->shared_pending,
472 * itimers are process shared and we restart periodic
473 * itimers in the signal delivery path to prevent DoS
474 * attacks in the high resolution timer case. This is
475 * compliant with the old way of self restarting
476 * itimers, as the SIGALRM is a legacy signal and only
477 * queued once. Changing the restart behaviour to
478 * restart the timer in the signal dequeue path is
479 * reducing the timer noise on heavy loaded !highres
482 if (unlikely(signr == SIGALRM)) {
483 struct hrtimer *tmr = &tsk->signal->real_timer;
485 if (!hrtimer_is_queued(tmr) &&
486 tsk->signal->it_real_incr.tv64 != 0) {
487 hrtimer_forward(tmr, tmr->base->get_time(),
488 tsk->signal->it_real_incr);
489 hrtimer_restart(tmr);
498 if (unlikely(sig_kernel_stop(signr))) {
500 * Set a marker that we have dequeued a stop signal. Our
501 * caller might release the siglock and then the pending
502 * stop signal it is about to process is no longer in the
503 * pending bitmasks, but must still be cleared by a SIGCONT
504 * (and overruled by a SIGKILL). So those cases clear this
505 * shared flag after we've set it. Note that this flag may
506 * remain set after the signal we return is ignored or
507 * handled. That doesn't matter because its only purpose
508 * is to alert stop-signal processing code when another
509 * processor has come along and cleared the flag.
511 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
513 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
515 * Release the siglock to ensure proper locking order
516 * of timer locks outside of siglocks. Note, we leave
517 * irqs disabled here, since the posix-timers code is
518 * about to disable them again anyway.
520 spin_unlock(&tsk->sighand->siglock);
521 do_schedule_next_timer(info);
522 spin_lock(&tsk->sighand->siglock);
528 * Tell a process that it has a new active signal..
530 * NOTE! we rely on the previous spin_lock to
531 * lock interrupts for us! We can only be called with
532 * "siglock" held, and the local interrupt must
533 * have been disabled when that got acquired!
535 * No need to set need_resched since signal event passing
536 * goes through ->blocked
538 void signal_wake_up(struct task_struct *t, int resume)
542 set_tsk_thread_flag(t, TIF_SIGPENDING);
545 * For SIGKILL, we want to wake it up in the stopped/traced/killable
546 * case. We don't check t->state here because there is a race with it
547 * executing another processor and just now entering stopped state.
548 * By using wake_up_state, we ensure the process will wake up and
549 * handle its death signal.
551 mask = TASK_INTERRUPTIBLE;
553 mask |= TASK_WAKEKILL;
554 if (!wake_up_state(t, mask))
559 * Remove signals in mask from the pending set and queue.
560 * Returns 1 if any signals were found.
562 * All callers must be holding the siglock.
564 * This version takes a sigset mask and looks at all signals,
565 * not just those in the first mask word.
567 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
569 struct sigqueue *q, *n;
572 sigandsets(&m, mask, &s->signal);
573 if (sigisemptyset(&m))
576 signandsets(&s->signal, &s->signal, mask);
577 list_for_each_entry_safe(q, n, &s->list, list) {
578 if (sigismember(mask, q->info.si_signo)) {
579 list_del_init(&q->list);
586 * Remove signals in mask from the pending set and queue.
587 * Returns 1 if any signals were found.
589 * All callers must be holding the siglock.
591 static int rm_from_queue(unsigned long mask, struct sigpending *s)
593 struct sigqueue *q, *n;
595 if (!sigtestsetmask(&s->signal, mask))
598 sigdelsetmask(&s->signal, mask);
599 list_for_each_entry_safe(q, n, &s->list, list) {
600 if (q->info.si_signo < SIGRTMIN &&
601 (mask & sigmask(q->info.si_signo))) {
602 list_del_init(&q->list);
610 * Bad permissions for sending the signal
611 * - the caller must hold at least the RCU read lock
613 static int check_kill_permission(int sig, struct siginfo *info,
614 struct task_struct *t)
616 const struct cred *cred = current_cred(), *tcred;
620 if (!valid_signal(sig))
623 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
626 error = audit_signal_info(sig, t); /* Let audit system see the signal */
630 tcred = __task_cred(t);
631 if ((cred->euid ^ tcred->suid) &&
632 (cred->euid ^ tcred->uid) &&
633 (cred->uid ^ tcred->suid) &&
634 (cred->uid ^ tcred->uid) &&
635 !capable(CAP_KILL)) {
638 sid = task_session(t);
640 * We don't return the error if sid == NULL. The
641 * task was unhashed, the caller must notice this.
643 if (!sid || sid == task_session(current))
650 return security_task_kill(t, info, sig, 0);
654 * Handle magic process-wide effects of stop/continue signals. Unlike
655 * the signal actions, these happen immediately at signal-generation
656 * time regardless of blocking, ignoring, or handling. This does the
657 * actual continuing for SIGCONT, but not the actual stopping for stop
658 * signals. The process stop is done as a signal action for SIG_DFL.
660 * Returns true if the signal should be actually delivered, otherwise
661 * it should be dropped.
663 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
665 struct signal_struct *signal = p->signal;
666 struct task_struct *t;
668 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
670 * The process is in the middle of dying, nothing to do.
672 } else if (sig_kernel_stop(sig)) {
674 * This is a stop signal. Remove SIGCONT from all queues.
676 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
679 rm_from_queue(sigmask(SIGCONT), &t->pending);
680 } while_each_thread(p, t);
681 } else if (sig == SIGCONT) {
684 * Remove all stop signals from all queues,
685 * and wake all threads.
687 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
691 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
693 * If there is a handler for SIGCONT, we must make
694 * sure that no thread returns to user mode before
695 * we post the signal, in case it was the only
696 * thread eligible to run the signal handler--then
697 * it must not do anything between resuming and
698 * running the handler. With the TIF_SIGPENDING
699 * flag set, the thread will pause and acquire the
700 * siglock that we hold now and until we've queued
701 * the pending signal.
703 * Wake up the stopped thread _after_ setting
706 state = __TASK_STOPPED;
707 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
708 set_tsk_thread_flag(t, TIF_SIGPENDING);
709 state |= TASK_INTERRUPTIBLE;
711 wake_up_state(t, state);
712 } while_each_thread(p, t);
715 * Notify the parent with CLD_CONTINUED if we were stopped.
717 * If we were in the middle of a group stop, we pretend it
718 * was already finished, and then continued. Since SIGCHLD
719 * doesn't queue we report only CLD_STOPPED, as if the next
720 * CLD_CONTINUED was dropped.
723 if (signal->flags & SIGNAL_STOP_STOPPED)
724 why |= SIGNAL_CLD_CONTINUED;
725 else if (signal->group_stop_count)
726 why |= SIGNAL_CLD_STOPPED;
730 * The first thread which returns from do_signal_stop()
731 * will take ->siglock, notice SIGNAL_CLD_MASK, and
732 * notify its parent. See get_signal_to_deliver().
734 signal->flags = why | SIGNAL_STOP_CONTINUED;
735 signal->group_stop_count = 0;
736 signal->group_exit_code = 0;
739 * We are not stopped, but there could be a stop
740 * signal in the middle of being processed after
741 * being removed from the queue. Clear that too.
743 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
747 return !sig_ignored(p, sig, from_ancestor_ns);
751 * Test if P wants to take SIG. After we've checked all threads with this,
752 * it's equivalent to finding no threads not blocking SIG. Any threads not
753 * blocking SIG were ruled out because they are not running and already
754 * have pending signals. Such threads will dequeue from the shared queue
755 * as soon as they're available, so putting the signal on the shared queue
756 * will be equivalent to sending it to one such thread.
758 static inline int wants_signal(int sig, struct task_struct *p)
760 if (sigismember(&p->blocked, sig))
762 if (p->flags & PF_EXITING)
766 if (task_is_stopped_or_traced(p))
768 return task_curr(p) || !signal_pending(p);
771 static void complete_signal(int sig, struct task_struct *p, int group)
773 struct signal_struct *signal = p->signal;
774 struct task_struct *t;
777 * Now find a thread we can wake up to take the signal off the queue.
779 * If the main thread wants the signal, it gets first crack.
780 * Probably the least surprising to the average bear.
782 if (wants_signal(sig, p))
784 else if (!group || thread_group_empty(p))
786 * There is just one thread and it does not need to be woken.
787 * It will dequeue unblocked signals before it runs again.
792 * Otherwise try to find a suitable thread.
794 t = signal->curr_target;
795 while (!wants_signal(sig, t)) {
797 if (t == signal->curr_target)
799 * No thread needs to be woken.
800 * Any eligible threads will see
801 * the signal in the queue soon.
805 signal->curr_target = t;
809 * Found a killable thread. If the signal will be fatal,
810 * then start taking the whole group down immediately.
812 if (sig_fatal(p, sig) &&
813 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
814 !sigismember(&t->real_blocked, sig) &&
816 !tracehook_consider_fatal_signal(t, sig))) {
818 * This signal will be fatal to the whole group.
820 if (!sig_kernel_coredump(sig)) {
822 * Start a group exit and wake everybody up.
823 * This way we don't have other threads
824 * running and doing things after a slower
825 * thread has the fatal signal pending.
827 signal->flags = SIGNAL_GROUP_EXIT;
828 signal->group_exit_code = sig;
829 signal->group_stop_count = 0;
832 sigaddset(&t->pending.signal, SIGKILL);
833 signal_wake_up(t, 1);
834 } while_each_thread(p, t);
840 * The signal is already in the shared-pending queue.
841 * Tell the chosen thread to wake up and dequeue it.
843 signal_wake_up(t, sig == SIGKILL);
847 static inline int legacy_queue(struct sigpending *signals, int sig)
849 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
852 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
853 int group, int from_ancestor_ns)
855 struct sigpending *pending;
859 trace_sched_signal_send(sig, t);
861 assert_spin_locked(&t->sighand->siglock);
863 if (!prepare_signal(sig, t, from_ancestor_ns))
866 pending = group ? &t->signal->shared_pending : &t->pending;
868 * Short-circuit ignored signals and support queuing
869 * exactly one non-rt signal, so that we can get more
870 * detailed information about the cause of the signal.
872 if (legacy_queue(pending, sig))
875 * fast-pathed signals for kernel-internal things like SIGSTOP
878 if (info == SEND_SIG_FORCED)
881 /* Real-time signals must be queued if sent by sigqueue, or
882 some other real-time mechanism. It is implementation
883 defined whether kill() does so. We attempt to do so, on
884 the principle of least surprise, but since kill is not
885 allowed to fail with EAGAIN when low on memory we just
886 make sure at least one signal gets delivered and don't
887 pass on the info struct. */
890 override_rlimit = (is_si_special(info) || info->si_code >= 0);
894 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
897 list_add_tail(&q->list, &pending->list);
898 switch ((unsigned long) info) {
899 case (unsigned long) SEND_SIG_NOINFO:
900 q->info.si_signo = sig;
901 q->info.si_errno = 0;
902 q->info.si_code = SI_USER;
903 q->info.si_pid = task_tgid_nr_ns(current,
904 task_active_pid_ns(t));
905 q->info.si_uid = current_uid();
907 case (unsigned long) SEND_SIG_PRIV:
908 q->info.si_signo = sig;
909 q->info.si_errno = 0;
910 q->info.si_code = SI_KERNEL;
915 copy_siginfo(&q->info, info);
916 if (from_ancestor_ns)
920 } else if (!is_si_special(info)) {
921 if (sig >= SIGRTMIN && info->si_code != SI_USER)
923 * Queue overflow, abort. We may abort if the signal was rt
924 * and sent by user using something other than kill().
930 signalfd_notify(t, sig);
931 sigaddset(&pending->signal, sig);
932 complete_signal(sig, t, group);
936 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
939 int from_ancestor_ns = 0;
942 if (!is_si_special(info) && SI_FROMUSER(info) &&
943 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
944 from_ancestor_ns = 1;
947 return __send_signal(sig, info, t, group, from_ancestor_ns);
950 static void print_fatal_signal(struct pt_regs *regs, int signr)
952 printk("%s/%d: potentially unexpected fatal signal %d.\n",
953 current->comm, task_pid_nr(current), signr);
955 #if defined(__i386__) && !defined(__arch_um__)
956 printk("code at %08lx: ", regs->ip);
959 for (i = 0; i < 16; i++) {
962 __get_user(insn, (unsigned char *)(regs->ip + i));
963 printk("%02x ", insn);
973 static int __init setup_print_fatal_signals(char *str)
975 get_option (&str, &print_fatal_signals);
980 __setup("print-fatal-signals=", setup_print_fatal_signals);
983 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
985 return send_signal(sig, info, p, 1);
989 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
991 return send_signal(sig, info, t, 0);
994 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1000 if (lock_task_sighand(p, &flags)) {
1001 ret = send_signal(sig, info, p, group);
1002 unlock_task_sighand(p, &flags);
1009 * Force a signal that the process can't ignore: if necessary
1010 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1012 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1013 * since we do not want to have a signal handler that was blocked
1014 * be invoked when user space had explicitly blocked it.
1016 * We don't want to have recursive SIGSEGV's etc, for example,
1017 * that is why we also clear SIGNAL_UNKILLABLE.
1020 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1022 unsigned long int flags;
1023 int ret, blocked, ignored;
1024 struct k_sigaction *action;
1026 spin_lock_irqsave(&t->sighand->siglock, flags);
1027 action = &t->sighand->action[sig-1];
1028 ignored = action->sa.sa_handler == SIG_IGN;
1029 blocked = sigismember(&t->blocked, sig);
1030 if (blocked || ignored) {
1031 action->sa.sa_handler = SIG_DFL;
1033 sigdelset(&t->blocked, sig);
1034 recalc_sigpending_and_wake(t);
1037 if (action->sa.sa_handler == SIG_DFL)
1038 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1039 ret = specific_send_sig_info(sig, info, t);
1040 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1046 force_sig_specific(int sig, struct task_struct *t)
1048 force_sig_info(sig, SEND_SIG_FORCED, t);
1052 * Nuke all other threads in the group.
1054 void zap_other_threads(struct task_struct *p)
1056 struct task_struct *t;
1058 p->signal->group_stop_count = 0;
1060 for (t = next_thread(p); t != p; t = next_thread(t)) {
1062 * Don't bother with already dead threads
1067 /* SIGKILL will be handled before any pending SIGSTOP */
1068 sigaddset(&t->pending.signal, SIGKILL);
1069 signal_wake_up(t, 1);
1073 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1075 struct sighand_struct *sighand;
1079 sighand = rcu_dereference(tsk->sighand);
1080 if (unlikely(sighand == NULL))
1083 spin_lock_irqsave(&sighand->siglock, *flags);
1084 if (likely(sighand == tsk->sighand))
1086 spin_unlock_irqrestore(&sighand->siglock, *flags);
1094 * send signal info to all the members of a group
1095 * - the caller must hold the RCU read lock at least
1097 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1099 int ret = check_kill_permission(sig, info, p);
1102 ret = do_send_sig_info(sig, info, p, true);
1108 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1109 * control characters do (^C, ^Z etc)
1110 * - the caller must hold at least a readlock on tasklist_lock
1112 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1114 struct task_struct *p = NULL;
1115 int retval, success;
1119 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1120 int err = group_send_sig_info(sig, info, p);
1123 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1124 return success ? 0 : retval;
1127 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1130 struct task_struct *p;
1134 p = pid_task(pid, PIDTYPE_PID);
1136 error = group_send_sig_info(sig, info, p);
1137 if (unlikely(error == -ESRCH))
1139 * The task was unhashed in between, try again.
1140 * If it is dead, pid_task() will return NULL,
1141 * if we race with de_thread() it will find the
1152 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1156 error = kill_pid_info(sig, info, find_vpid(pid));
1161 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1162 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1163 uid_t uid, uid_t euid, u32 secid)
1166 struct task_struct *p;
1167 const struct cred *pcred;
1169 if (!valid_signal(sig))
1172 read_lock(&tasklist_lock);
1173 p = pid_task(pid, PIDTYPE_PID);
1178 pcred = __task_cred(p);
1179 if ((info == SEND_SIG_NOINFO ||
1180 (!is_si_special(info) && SI_FROMUSER(info))) &&
1181 euid != pcred->suid && euid != pcred->uid &&
1182 uid != pcred->suid && uid != pcred->uid) {
1186 ret = security_task_kill(p, info, sig, secid);
1189 if (sig && p->sighand) {
1190 unsigned long flags;
1191 spin_lock_irqsave(&p->sighand->siglock, flags);
1192 ret = __send_signal(sig, info, p, 1, 0);
1193 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1196 read_unlock(&tasklist_lock);
1199 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
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, pid_t pid)
1214 ret = kill_pid_info(sig, info, find_vpid(pid));
1219 read_lock(&tasklist_lock);
1221 ret = __kill_pgrp_info(sig, info,
1222 pid ? find_vpid(-pid) : task_pgrp(current));
1224 int retval = 0, count = 0;
1225 struct task_struct * p;
1227 for_each_process(p) {
1228 if (task_pid_vnr(p) > 1 &&
1229 !same_thread_group(p, current)) {
1230 int err = group_send_sig_info(sig, info, p);
1236 ret = count ? retval : -ESRCH;
1238 read_unlock(&tasklist_lock);
1244 * These are for backward compatibility with the rest of the kernel source.
1248 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1251 * Make sure legacy kernel users don't send in bad values
1252 * (normal paths check this in check_kill_permission).
1254 if (!valid_signal(sig))
1257 return do_send_sig_info(sig, info, p, false);
1260 #define __si_special(priv) \
1261 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1264 send_sig(int sig, struct task_struct *p, int priv)
1266 return send_sig_info(sig, __si_special(priv), p);
1270 force_sig(int sig, struct task_struct *p)
1272 force_sig_info(sig, SEND_SIG_PRIV, p);
1276 * When things go south during signal handling, we
1277 * will force a SIGSEGV. And if the signal that caused
1278 * the problem was already a SIGSEGV, we'll want to
1279 * make sure we don't even try to deliver the signal..
1282 force_sigsegv(int sig, struct task_struct *p)
1284 if (sig == SIGSEGV) {
1285 unsigned long flags;
1286 spin_lock_irqsave(&p->sighand->siglock, flags);
1287 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1288 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1290 force_sig(SIGSEGV, p);
1294 int kill_pgrp(struct pid *pid, int sig, int priv)
1298 read_lock(&tasklist_lock);
1299 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1300 read_unlock(&tasklist_lock);
1304 EXPORT_SYMBOL(kill_pgrp);
1306 int kill_pid(struct pid *pid, int sig, int priv)
1308 return kill_pid_info(sig, __si_special(priv), pid);
1310 EXPORT_SYMBOL(kill_pid);
1313 * These functions support sending signals using preallocated sigqueue
1314 * structures. This is needed "because realtime applications cannot
1315 * afford to lose notifications of asynchronous events, like timer
1316 * expirations or I/O completions". In the case of Posix Timers
1317 * we allocate the sigqueue structure from the timer_create. If this
1318 * allocation fails we are able to report the failure to the application
1319 * with an EAGAIN error.
1321 struct sigqueue *sigqueue_alloc(void)
1323 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1326 q->flags |= SIGQUEUE_PREALLOC;
1331 void sigqueue_free(struct sigqueue *q)
1333 unsigned long flags;
1334 spinlock_t *lock = ¤t->sighand->siglock;
1336 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1338 * We must hold ->siglock while testing q->list
1339 * to serialize with collect_signal() or with
1340 * __exit_signal()->flush_sigqueue().
1342 spin_lock_irqsave(lock, flags);
1343 q->flags &= ~SIGQUEUE_PREALLOC;
1345 * If it is queued it will be freed when dequeued,
1346 * like the "regular" sigqueue.
1348 if (!list_empty(&q->list))
1350 spin_unlock_irqrestore(lock, flags);
1356 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1358 int sig = q->info.si_signo;
1359 struct sigpending *pending;
1360 unsigned long flags;
1363 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1366 if (!likely(lock_task_sighand(t, &flags)))
1369 ret = 1; /* the signal is ignored */
1370 if (!prepare_signal(sig, t, 0))
1374 if (unlikely(!list_empty(&q->list))) {
1376 * If an SI_TIMER entry is already queue just increment
1377 * the overrun count.
1379 BUG_ON(q->info.si_code != SI_TIMER);
1380 q->info.si_overrun++;
1383 q->info.si_overrun = 0;
1385 signalfd_notify(t, sig);
1386 pending = group ? &t->signal->shared_pending : &t->pending;
1387 list_add_tail(&q->list, &pending->list);
1388 sigaddset(&pending->signal, sig);
1389 complete_signal(sig, t, group);
1391 unlock_task_sighand(t, &flags);
1397 * Let a parent know about the death of a child.
1398 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1400 * Returns -1 if our parent ignored us and so we've switched to
1401 * self-reaping, or else @sig.
1403 int do_notify_parent(struct task_struct *tsk, int sig)
1405 struct siginfo info;
1406 unsigned long flags;
1407 struct sighand_struct *psig;
1412 /* do_notify_parent_cldstop should have been called instead. */
1413 BUG_ON(task_is_stopped_or_traced(tsk));
1415 BUG_ON(!task_ptrace(tsk) &&
1416 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1418 info.si_signo = sig;
1421 * we are under tasklist_lock here so our parent is tied to
1422 * us and cannot exit and release its namespace.
1424 * the only it can is to switch its nsproxy with sys_unshare,
1425 * bu uncharing pid namespaces is not allowed, so we'll always
1426 * see relevant namespace
1428 * write_lock() currently calls preempt_disable() which is the
1429 * same as rcu_read_lock(), but according to Oleg, this is not
1430 * correct to rely on this
1433 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1434 info.si_uid = __task_cred(tsk)->uid;
1437 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1438 tsk->signal->utime));
1439 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1440 tsk->signal->stime));
1442 info.si_status = tsk->exit_code & 0x7f;
1443 if (tsk->exit_code & 0x80)
1444 info.si_code = CLD_DUMPED;
1445 else if (tsk->exit_code & 0x7f)
1446 info.si_code = CLD_KILLED;
1448 info.si_code = CLD_EXITED;
1449 info.si_status = tsk->exit_code >> 8;
1452 psig = tsk->parent->sighand;
1453 spin_lock_irqsave(&psig->siglock, flags);
1454 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1455 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1456 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1458 * We are exiting and our parent doesn't care. POSIX.1
1459 * defines special semantics for setting SIGCHLD to SIG_IGN
1460 * or setting the SA_NOCLDWAIT flag: we should be reaped
1461 * automatically and not left for our parent's wait4 call.
1462 * Rather than having the parent do it as a magic kind of
1463 * signal handler, we just set this to tell do_exit that we
1464 * can be cleaned up without becoming a zombie. Note that
1465 * we still call __wake_up_parent in this case, because a
1466 * blocked sys_wait4 might now return -ECHILD.
1468 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1469 * is implementation-defined: we do (if you don't want
1470 * it, just use SIG_IGN instead).
1472 ret = tsk->exit_signal = -1;
1473 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1476 if (valid_signal(sig) && sig > 0)
1477 __group_send_sig_info(sig, &info, tsk->parent);
1478 __wake_up_parent(tsk, tsk->parent);
1479 spin_unlock_irqrestore(&psig->siglock, flags);
1484 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1486 struct siginfo info;
1487 unsigned long flags;
1488 struct task_struct *parent;
1489 struct sighand_struct *sighand;
1491 if (task_ptrace(tsk))
1492 parent = tsk->parent;
1494 tsk = tsk->group_leader;
1495 parent = tsk->real_parent;
1498 info.si_signo = SIGCHLD;
1501 * see comment in do_notify_parent() abot the following 3 lines
1504 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1505 info.si_uid = __task_cred(tsk)->uid;
1508 info.si_utime = cputime_to_clock_t(tsk->utime);
1509 info.si_stime = cputime_to_clock_t(tsk->stime);
1514 info.si_status = SIGCONT;
1517 info.si_status = tsk->signal->group_exit_code & 0x7f;
1520 info.si_status = tsk->exit_code & 0x7f;
1526 sighand = parent->sighand;
1527 spin_lock_irqsave(&sighand->siglock, flags);
1528 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1529 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1530 __group_send_sig_info(SIGCHLD, &info, parent);
1532 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1534 __wake_up_parent(tsk, parent);
1535 spin_unlock_irqrestore(&sighand->siglock, flags);
1538 static inline int may_ptrace_stop(void)
1540 if (!likely(task_ptrace(current)))
1543 * Are we in the middle of do_coredump?
1544 * If so and our tracer is also part of the coredump stopping
1545 * is a deadlock situation, and pointless because our tracer
1546 * is dead so don't allow us to stop.
1547 * If SIGKILL was already sent before the caller unlocked
1548 * ->siglock we must see ->core_state != NULL. Otherwise it
1549 * is safe to enter schedule().
1551 if (unlikely(current->mm->core_state) &&
1552 unlikely(current->mm == current->parent->mm))
1559 * Return nonzero if there is a SIGKILL that should be waking us up.
1560 * Called with the siglock held.
1562 static int sigkill_pending(struct task_struct *tsk)
1564 return sigismember(&tsk->pending.signal, SIGKILL) ||
1565 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1569 * This must be called with current->sighand->siglock held.
1571 * This should be the path for all ptrace stops.
1572 * We always set current->last_siginfo while stopped here.
1573 * That makes it a way to test a stopped process for
1574 * being ptrace-stopped vs being job-control-stopped.
1576 * If we actually decide not to stop at all because the tracer
1577 * is gone, we keep current->exit_code unless clear_code.
1579 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1581 if (arch_ptrace_stop_needed(exit_code, info)) {
1583 * The arch code has something special to do before a
1584 * ptrace stop. This is allowed to block, e.g. for faults
1585 * on user stack pages. We can't keep the siglock while
1586 * calling arch_ptrace_stop, so we must release it now.
1587 * To preserve proper semantics, we must do this before
1588 * any signal bookkeeping like checking group_stop_count.
1589 * Meanwhile, a SIGKILL could come in before we retake the
1590 * siglock. That must prevent us from sleeping in TASK_TRACED.
1591 * So after regaining the lock, we must check for SIGKILL.
1593 spin_unlock_irq(¤t->sighand->siglock);
1594 arch_ptrace_stop(exit_code, info);
1595 spin_lock_irq(¤t->sighand->siglock);
1596 if (sigkill_pending(current))
1601 * If there is a group stop in progress,
1602 * we must participate in the bookkeeping.
1604 if (current->signal->group_stop_count > 0)
1605 --current->signal->group_stop_count;
1607 current->last_siginfo = info;
1608 current->exit_code = exit_code;
1610 /* Let the debugger run. */
1611 __set_current_state(TASK_TRACED);
1612 spin_unlock_irq(¤t->sighand->siglock);
1613 read_lock(&tasklist_lock);
1614 if (may_ptrace_stop()) {
1615 do_notify_parent_cldstop(current, CLD_TRAPPED);
1617 * Don't want to allow preemption here, because
1618 * sys_ptrace() needs this task to be inactive.
1620 * XXX: implement read_unlock_no_resched().
1623 read_unlock(&tasklist_lock);
1624 preempt_enable_no_resched();
1628 * By the time we got the lock, our tracer went away.
1629 * Don't drop the lock yet, another tracer may come.
1631 __set_current_state(TASK_RUNNING);
1633 current->exit_code = 0;
1634 read_unlock(&tasklist_lock);
1638 * While in TASK_TRACED, we were considered "frozen enough".
1639 * Now that we woke up, it's crucial if we're supposed to be
1640 * frozen that we freeze now before running anything substantial.
1645 * We are back. Now reacquire the siglock before touching
1646 * last_siginfo, so that we are sure to have synchronized with
1647 * any signal-sending on another CPU that wants to examine it.
1649 spin_lock_irq(¤t->sighand->siglock);
1650 current->last_siginfo = NULL;
1653 * Queued signals ignored us while we were stopped for tracing.
1654 * So check for any that we should take before resuming user mode.
1655 * This sets TIF_SIGPENDING, but never clears it.
1657 recalc_sigpending_tsk(current);
1660 void ptrace_notify(int exit_code)
1664 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1666 memset(&info, 0, sizeof info);
1667 info.si_signo = SIGTRAP;
1668 info.si_code = exit_code;
1669 info.si_pid = task_pid_vnr(current);
1670 info.si_uid = current_uid();
1672 /* Let the debugger run. */
1673 spin_lock_irq(¤t->sighand->siglock);
1674 ptrace_stop(exit_code, 1, &info);
1675 spin_unlock_irq(¤t->sighand->siglock);
1679 * This performs the stopping for SIGSTOP and other stop signals.
1680 * We have to stop all threads in the thread group.
1681 * Returns nonzero if we've actually stopped and released the siglock.
1682 * Returns zero if we didn't stop and still hold the siglock.
1684 static int do_signal_stop(int signr)
1686 struct signal_struct *sig = current->signal;
1689 if (!sig->group_stop_count) {
1690 struct task_struct *t;
1692 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1693 unlikely(signal_group_exit(sig)))
1696 * There is no group stop already in progress.
1697 * We must initiate one now.
1699 sig->group_exit_code = signr;
1701 sig->group_stop_count = 1;
1702 for (t = next_thread(current); t != current; t = next_thread(t))
1704 * Setting state to TASK_STOPPED for a group
1705 * stop is always done with the siglock held,
1706 * so this check has no races.
1708 if (!(t->flags & PF_EXITING) &&
1709 !task_is_stopped_or_traced(t)) {
1710 sig->group_stop_count++;
1711 signal_wake_up(t, 0);
1715 * If there are no other threads in the group, or if there is
1716 * a group stop in progress and we are the last to stop, report
1717 * to the parent. When ptraced, every thread reports itself.
1719 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1720 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1722 * tracehook_notify_jctl() can drop and reacquire siglock, so
1723 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1724 * or SIGKILL comes in between ->group_stop_count == 0.
1726 if (sig->group_stop_count) {
1727 if (!--sig->group_stop_count)
1728 sig->flags = SIGNAL_STOP_STOPPED;
1729 current->exit_code = sig->group_exit_code;
1730 __set_current_state(TASK_STOPPED);
1732 spin_unlock_irq(¤t->sighand->siglock);
1735 read_lock(&tasklist_lock);
1736 do_notify_parent_cldstop(current, notify);
1737 read_unlock(&tasklist_lock);
1740 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1743 } while (try_to_freeze());
1745 tracehook_finish_jctl();
1746 current->exit_code = 0;
1751 static int ptrace_signal(int signr, siginfo_t *info,
1752 struct pt_regs *regs, void *cookie)
1754 if (!task_ptrace(current))
1757 ptrace_signal_deliver(regs, cookie);
1759 /* Let the debugger run. */
1760 ptrace_stop(signr, 0, info);
1762 /* We're back. Did the debugger cancel the sig? */
1763 signr = current->exit_code;
1767 current->exit_code = 0;
1769 /* Update the siginfo structure if the signal has
1770 changed. If the debugger wanted something
1771 specific in the siginfo structure then it should
1772 have updated *info via PTRACE_SETSIGINFO. */
1773 if (signr != info->si_signo) {
1774 info->si_signo = signr;
1776 info->si_code = SI_USER;
1777 info->si_pid = task_pid_vnr(current->parent);
1778 info->si_uid = task_uid(current->parent);
1781 /* If the (new) signal is now blocked, requeue it. */
1782 if (sigismember(¤t->blocked, signr)) {
1783 specific_send_sig_info(signr, info, current);
1790 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1791 struct pt_regs *regs, void *cookie)
1793 struct sighand_struct *sighand = current->sighand;
1794 struct signal_struct *signal = current->signal;
1799 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1800 * While in TASK_STOPPED, we were considered "frozen enough".
1801 * Now that we woke up, it's crucial if we're supposed to be
1802 * frozen that we freeze now before running anything substantial.
1806 spin_lock_irq(&sighand->siglock);
1808 * Every stopped thread goes here after wakeup. Check to see if
1809 * we should notify the parent, prepare_signal(SIGCONT) encodes
1810 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1812 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1813 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1814 ? CLD_CONTINUED : CLD_STOPPED;
1815 signal->flags &= ~SIGNAL_CLD_MASK;
1817 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1818 spin_unlock_irq(&sighand->siglock);
1821 read_lock(&tasklist_lock);
1822 do_notify_parent_cldstop(current->group_leader, why);
1823 read_unlock(&tasklist_lock);
1829 struct k_sigaction *ka;
1831 if (unlikely(signal->group_stop_count > 0) &&
1836 * Tracing can induce an artifical signal and choose sigaction.
1837 * The return value in @signr determines the default action,
1838 * but @info->si_signo is the signal number we will report.
1840 signr = tracehook_get_signal(current, regs, info, return_ka);
1841 if (unlikely(signr < 0))
1843 if (unlikely(signr != 0))
1846 signr = dequeue_signal(current, ¤t->blocked,
1850 break; /* will return 0 */
1852 if (signr != SIGKILL) {
1853 signr = ptrace_signal(signr, info,
1859 ka = &sighand->action[signr-1];
1862 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1864 if (ka->sa.sa_handler != SIG_DFL) {
1865 /* Run the handler. */
1868 if (ka->sa.sa_flags & SA_ONESHOT)
1869 ka->sa.sa_handler = SIG_DFL;
1871 break; /* will return non-zero "signr" value */
1875 * Now we are doing the default action for this signal.
1877 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1881 * Global init gets no signals it doesn't want.
1882 * Container-init gets no signals it doesn't want from same
1885 * Note that if global/container-init sees a sig_kernel_only()
1886 * signal here, the signal must have been generated internally
1887 * or must have come from an ancestor namespace. In either
1888 * case, the signal cannot be dropped.
1890 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1891 !sig_kernel_only(signr))
1894 if (sig_kernel_stop(signr)) {
1896 * The default action is to stop all threads in
1897 * the thread group. The job control signals
1898 * do nothing in an orphaned pgrp, but SIGSTOP
1899 * always works. Note that siglock needs to be
1900 * dropped during the call to is_orphaned_pgrp()
1901 * because of lock ordering with tasklist_lock.
1902 * This allows an intervening SIGCONT to be posted.
1903 * We need to check for that and bail out if necessary.
1905 if (signr != SIGSTOP) {
1906 spin_unlock_irq(&sighand->siglock);
1908 /* signals can be posted during this window */
1910 if (is_current_pgrp_orphaned())
1913 spin_lock_irq(&sighand->siglock);
1916 if (likely(do_signal_stop(info->si_signo))) {
1917 /* It released the siglock. */
1922 * We didn't actually stop, due to a race
1923 * with SIGCONT or something like that.
1928 spin_unlock_irq(&sighand->siglock);
1931 * Anything else is fatal, maybe with a core dump.
1933 current->flags |= PF_SIGNALED;
1935 if (sig_kernel_coredump(signr)) {
1936 if (print_fatal_signals)
1937 print_fatal_signal(regs, info->si_signo);
1939 * If it was able to dump core, this kills all
1940 * other threads in the group and synchronizes with
1941 * their demise. If we lost the race with another
1942 * thread getting here, it set group_exit_code
1943 * first and our do_group_exit call below will use
1944 * that value and ignore the one we pass it.
1946 do_coredump(info->si_signo, info->si_signo, regs);
1950 * Death signals, no core dump.
1952 do_group_exit(info->si_signo);
1955 spin_unlock_irq(&sighand->siglock);
1959 void exit_signals(struct task_struct *tsk)
1962 struct task_struct *t;
1964 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1965 tsk->flags |= PF_EXITING;
1969 spin_lock_irq(&tsk->sighand->siglock);
1971 * From now this task is not visible for group-wide signals,
1972 * see wants_signal(), do_signal_stop().
1974 tsk->flags |= PF_EXITING;
1975 if (!signal_pending(tsk))
1978 /* It could be that __group_complete_signal() choose us to
1979 * notify about group-wide signal. Another thread should be
1980 * woken now to take the signal since we will not.
1982 for (t = tsk; (t = next_thread(t)) != tsk; )
1983 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1984 recalc_sigpending_and_wake(t);
1986 if (unlikely(tsk->signal->group_stop_count) &&
1987 !--tsk->signal->group_stop_count) {
1988 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1989 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
1992 spin_unlock_irq(&tsk->sighand->siglock);
1994 if (unlikely(group_stop)) {
1995 read_lock(&tasklist_lock);
1996 do_notify_parent_cldstop(tsk, group_stop);
1997 read_unlock(&tasklist_lock);
2001 EXPORT_SYMBOL(recalc_sigpending);
2002 EXPORT_SYMBOL_GPL(dequeue_signal);
2003 EXPORT_SYMBOL(flush_signals);
2004 EXPORT_SYMBOL(force_sig);
2005 EXPORT_SYMBOL(send_sig);
2006 EXPORT_SYMBOL(send_sig_info);
2007 EXPORT_SYMBOL(sigprocmask);
2008 EXPORT_SYMBOL(block_all_signals);
2009 EXPORT_SYMBOL(unblock_all_signals);
2013 * System call entry points.
2016 SYSCALL_DEFINE0(restart_syscall)
2018 struct restart_block *restart = ¤t_thread_info()->restart_block;
2019 return restart->fn(restart);
2022 long do_no_restart_syscall(struct restart_block *param)
2028 * We don't need to get the kernel lock - this is all local to this
2029 * particular thread.. (and that's good, because this is _heavily_
2030 * used by various programs)
2034 * This is also useful for kernel threads that want to temporarily
2035 * (or permanently) block certain signals.
2037 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2038 * interface happily blocks "unblockable" signals like SIGKILL
2041 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2045 spin_lock_irq(¤t->sighand->siglock);
2047 *oldset = current->blocked;
2052 sigorsets(¤t->blocked, ¤t->blocked, set);
2055 signandsets(¤t->blocked, ¤t->blocked, set);
2058 current->blocked = *set;
2063 recalc_sigpending();
2064 spin_unlock_irq(¤t->sighand->siglock);
2069 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2070 sigset_t __user *, oset, size_t, sigsetsize)
2072 int error = -EINVAL;
2073 sigset_t old_set, new_set;
2075 /* XXX: Don't preclude handling different sized sigset_t's. */
2076 if (sigsetsize != sizeof(sigset_t))
2081 if (copy_from_user(&new_set, set, sizeof(*set)))
2083 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2085 error = sigprocmask(how, &new_set, &old_set);
2091 spin_lock_irq(¤t->sighand->siglock);
2092 old_set = current->blocked;
2093 spin_unlock_irq(¤t->sighand->siglock);
2097 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2105 long do_sigpending(void __user *set, unsigned long sigsetsize)
2107 long error = -EINVAL;
2110 if (sigsetsize > sizeof(sigset_t))
2113 spin_lock_irq(¤t->sighand->siglock);
2114 sigorsets(&pending, ¤t->pending.signal,
2115 ¤t->signal->shared_pending.signal);
2116 spin_unlock_irq(¤t->sighand->siglock);
2118 /* Outside the lock because only this thread touches it. */
2119 sigandsets(&pending, ¤t->blocked, &pending);
2122 if (!copy_to_user(set, &pending, sigsetsize))
2129 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2131 return do_sigpending(set, sigsetsize);
2134 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2136 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2140 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2142 if (from->si_code < 0)
2143 return __copy_to_user(to, from, sizeof(siginfo_t))
2146 * If you change siginfo_t structure, please be sure
2147 * this code is fixed accordingly.
2148 * Please remember to update the signalfd_copyinfo() function
2149 * inside fs/signalfd.c too, in case siginfo_t changes.
2150 * It should never copy any pad contained in the structure
2151 * to avoid security leaks, but must copy the generic
2152 * 3 ints plus the relevant union member.
2154 err = __put_user(from->si_signo, &to->si_signo);
2155 err |= __put_user(from->si_errno, &to->si_errno);
2156 err |= __put_user((short)from->si_code, &to->si_code);
2157 switch (from->si_code & __SI_MASK) {
2159 err |= __put_user(from->si_pid, &to->si_pid);
2160 err |= __put_user(from->si_uid, &to->si_uid);
2163 err |= __put_user(from->si_tid, &to->si_tid);
2164 err |= __put_user(from->si_overrun, &to->si_overrun);
2165 err |= __put_user(from->si_ptr, &to->si_ptr);
2168 err |= __put_user(from->si_band, &to->si_band);
2169 err |= __put_user(from->si_fd, &to->si_fd);
2172 err |= __put_user(from->si_addr, &to->si_addr);
2173 #ifdef __ARCH_SI_TRAPNO
2174 err |= __put_user(from->si_trapno, &to->si_trapno);
2178 err |= __put_user(from->si_pid, &to->si_pid);
2179 err |= __put_user(from->si_uid, &to->si_uid);
2180 err |= __put_user(from->si_status, &to->si_status);
2181 err |= __put_user(from->si_utime, &to->si_utime);
2182 err |= __put_user(from->si_stime, &to->si_stime);
2184 case __SI_RT: /* This is not generated by the kernel as of now. */
2185 case __SI_MESGQ: /* But this is */
2186 err |= __put_user(from->si_pid, &to->si_pid);
2187 err |= __put_user(from->si_uid, &to->si_uid);
2188 err |= __put_user(from->si_ptr, &to->si_ptr);
2190 default: /* this is just in case for now ... */
2191 err |= __put_user(from->si_pid, &to->si_pid);
2192 err |= __put_user(from->si_uid, &to->si_uid);
2200 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2201 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2210 /* XXX: Don't preclude handling different sized sigset_t's. */
2211 if (sigsetsize != sizeof(sigset_t))
2214 if (copy_from_user(&these, uthese, sizeof(these)))
2218 * Invert the set of allowed signals to get those we
2221 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2225 if (copy_from_user(&ts, uts, sizeof(ts)))
2227 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2232 spin_lock_irq(¤t->sighand->siglock);
2233 sig = dequeue_signal(current, &these, &info);
2235 timeout = MAX_SCHEDULE_TIMEOUT;
2237 timeout = (timespec_to_jiffies(&ts)
2238 + (ts.tv_sec || ts.tv_nsec));
2241 /* None ready -- temporarily unblock those we're
2242 * interested while we are sleeping in so that we'll
2243 * be awakened when they arrive. */
2244 current->real_blocked = current->blocked;
2245 sigandsets(¤t->blocked, ¤t->blocked, &these);
2246 recalc_sigpending();
2247 spin_unlock_irq(¤t->sighand->siglock);
2249 timeout = schedule_timeout_interruptible(timeout);
2251 spin_lock_irq(¤t->sighand->siglock);
2252 sig = dequeue_signal(current, &these, &info);
2253 current->blocked = current->real_blocked;
2254 siginitset(¤t->real_blocked, 0);
2255 recalc_sigpending();
2258 spin_unlock_irq(¤t->sighand->siglock);
2263 if (copy_siginfo_to_user(uinfo, &info))
2275 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2277 struct siginfo info;
2279 info.si_signo = sig;
2281 info.si_code = SI_USER;
2282 info.si_pid = task_tgid_vnr(current);
2283 info.si_uid = current_uid();
2285 return kill_something_info(sig, &info, pid);
2289 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2291 struct task_struct *p;
2295 p = find_task_by_vpid(pid);
2296 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2297 error = check_kill_permission(sig, info, p);
2299 * The null signal is a permissions and process existence
2300 * probe. No signal is actually delivered.
2302 if (!error && sig) {
2303 error = do_send_sig_info(sig, info, p, false);
2305 * If lock_task_sighand() failed we pretend the task
2306 * dies after receiving the signal. The window is tiny,
2307 * and the signal is private anyway.
2309 if (unlikely(error == -ESRCH))
2318 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2320 struct siginfo info;
2322 info.si_signo = sig;
2324 info.si_code = SI_TKILL;
2325 info.si_pid = task_tgid_vnr(current);
2326 info.si_uid = current_uid();
2328 return do_send_specific(tgid, pid, sig, &info);
2332 * sys_tgkill - send signal to one specific thread
2333 * @tgid: the thread group ID of the thread
2334 * @pid: the PID of the thread
2335 * @sig: signal to be sent
2337 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2338 * exists but it's not belonging to the target process anymore. This
2339 * method solves the problem of threads exiting and PIDs getting reused.
2341 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2343 /* This is only valid for single tasks */
2344 if (pid <= 0 || tgid <= 0)
2347 return do_tkill(tgid, pid, sig);
2351 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2353 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2355 /* This is only valid for single tasks */
2359 return do_tkill(0, pid, sig);
2362 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2363 siginfo_t __user *, uinfo)
2367 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2370 /* Not even root can pretend to send signals from the kernel.
2371 Nor can they impersonate a kill(), which adds source info. */
2372 if (info.si_code >= 0)
2374 info.si_signo = sig;
2376 /* POSIX.1b doesn't mention process groups. */
2377 return kill_proc_info(sig, &info, pid);
2380 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2382 /* This is only valid for single tasks */
2383 if (pid <= 0 || tgid <= 0)
2386 /* Not even root can pretend to send signals from the kernel.
2387 Nor can they impersonate a kill(), which adds source info. */
2388 if (info->si_code >= 0)
2390 info->si_signo = sig;
2392 return do_send_specific(tgid, pid, sig, info);
2395 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2396 siginfo_t __user *, uinfo)
2400 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2403 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2406 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2408 struct task_struct *t = current;
2409 struct k_sigaction *k;
2412 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2415 k = &t->sighand->action[sig-1];
2417 spin_lock_irq(¤t->sighand->siglock);
2422 sigdelsetmask(&act->sa.sa_mask,
2423 sigmask(SIGKILL) | sigmask(SIGSTOP));
2427 * "Setting a signal action to SIG_IGN for a signal that is
2428 * pending shall cause the pending signal to be discarded,
2429 * whether or not it is blocked."
2431 * "Setting a signal action to SIG_DFL for a signal that is
2432 * pending and whose default action is to ignore the signal
2433 * (for example, SIGCHLD), shall cause the pending signal to
2434 * be discarded, whether or not it is blocked"
2436 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2438 sigaddset(&mask, sig);
2439 rm_from_queue_full(&mask, &t->signal->shared_pending);
2441 rm_from_queue_full(&mask, &t->pending);
2443 } while (t != current);
2447 spin_unlock_irq(¤t->sighand->siglock);
2452 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2457 oss.ss_sp = (void __user *) current->sas_ss_sp;
2458 oss.ss_size = current->sas_ss_size;
2459 oss.ss_flags = sas_ss_flags(sp);
2467 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2469 error = __get_user(ss_sp, &uss->ss_sp) |
2470 __get_user(ss_flags, &uss->ss_flags) |
2471 __get_user(ss_size, &uss->ss_size);
2476 if (on_sig_stack(sp))
2482 * Note - this code used to test ss_flags incorrectly
2483 * old code may have been written using ss_flags==0
2484 * to mean ss_flags==SS_ONSTACK (as this was the only
2485 * way that worked) - this fix preserves that older
2488 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2491 if (ss_flags == SS_DISABLE) {
2496 if (ss_size < MINSIGSTKSZ)
2500 current->sas_ss_sp = (unsigned long) ss_sp;
2501 current->sas_ss_size = ss_size;
2507 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2509 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2510 __put_user(oss.ss_size, &uoss->ss_size) |
2511 __put_user(oss.ss_flags, &uoss->ss_flags);
2518 #ifdef __ARCH_WANT_SYS_SIGPENDING
2520 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2522 return do_sigpending(set, sizeof(*set));
2527 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2528 /* Some platforms have their own version with special arguments others
2529 support only sys_rt_sigprocmask. */
2531 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2532 old_sigset_t __user *, oset)
2535 old_sigset_t old_set, new_set;
2539 if (copy_from_user(&new_set, set, sizeof(*set)))
2541 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2543 spin_lock_irq(¤t->sighand->siglock);
2544 old_set = current->blocked.sig[0];
2552 sigaddsetmask(¤t->blocked, new_set);
2555 sigdelsetmask(¤t->blocked, new_set);
2558 current->blocked.sig[0] = new_set;
2562 recalc_sigpending();
2563 spin_unlock_irq(¤t->sighand->siglock);
2569 old_set = current->blocked.sig[0];
2572 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2579 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2581 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2582 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2583 const struct sigaction __user *, act,
2584 struct sigaction __user *, oact,
2587 struct k_sigaction new_sa, old_sa;
2590 /* XXX: Don't preclude handling different sized sigset_t's. */
2591 if (sigsetsize != sizeof(sigset_t))
2595 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2599 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2602 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2608 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2610 #ifdef __ARCH_WANT_SYS_SGETMASK
2613 * For backwards compatibility. Functionality superseded by sigprocmask.
2615 SYSCALL_DEFINE0(sgetmask)
2618 return current->blocked.sig[0];
2621 SYSCALL_DEFINE1(ssetmask, int, newmask)
2625 spin_lock_irq(¤t->sighand->siglock);
2626 old = current->blocked.sig[0];
2628 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
2630 recalc_sigpending();
2631 spin_unlock_irq(¤t->sighand->siglock);
2635 #endif /* __ARCH_WANT_SGETMASK */
2637 #ifdef __ARCH_WANT_SYS_SIGNAL
2639 * For backwards compatibility. Functionality superseded by sigaction.
2641 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2643 struct k_sigaction new_sa, old_sa;
2646 new_sa.sa.sa_handler = handler;
2647 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2648 sigemptyset(&new_sa.sa.sa_mask);
2650 ret = do_sigaction(sig, &new_sa, &old_sa);
2652 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2654 #endif /* __ARCH_WANT_SYS_SIGNAL */
2656 #ifdef __ARCH_WANT_SYS_PAUSE
2658 SYSCALL_DEFINE0(pause)
2660 current->state = TASK_INTERRUPTIBLE;
2662 return -ERESTARTNOHAND;
2667 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2668 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2672 /* XXX: Don't preclude handling different sized sigset_t's. */
2673 if (sigsetsize != sizeof(sigset_t))
2676 if (copy_from_user(&newset, unewset, sizeof(newset)))
2678 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2680 spin_lock_irq(¤t->sighand->siglock);
2681 current->saved_sigmask = current->blocked;
2682 current->blocked = newset;
2683 recalc_sigpending();
2684 spin_unlock_irq(¤t->sighand->siglock);
2686 current->state = TASK_INTERRUPTIBLE;
2688 set_restore_sigmask();
2689 return -ERESTARTNOHAND;
2691 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2693 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2698 void __init signals_init(void)
2700 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);