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/export.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/sched/user.h>
18 #include <linux/sched/debug.h>
19 #include <linux/sched/task.h>
20 #include <linux/sched/task_stack.h>
21 #include <linux/sched/cputime.h>
23 #include <linux/tty.h>
24 #include <linux/binfmts.h>
25 #include <linux/coredump.h>
26 #include <linux/security.h>
27 #include <linux/syscalls.h>
28 #include <linux/ptrace.h>
29 #include <linux/signal.h>
30 #include <linux/signalfd.h>
31 #include <linux/ratelimit.h>
32 #include <linux/tracehook.h>
33 #include <linux/capability.h>
34 #include <linux/freezer.h>
35 #include <linux/pid_namespace.h>
36 #include <linux/nsproxy.h>
37 #include <linux/user_namespace.h>
38 #include <linux/uprobes.h>
39 #include <linux/compat.h>
40 #include <linux/cn_proc.h>
41 #include <linux/compiler.h>
42 #include <linux/posix-timers.h>
43 #include <linux/livepatch.h>
45 #define CREATE_TRACE_POINTS
46 #include <trace/events/signal.h>
48 #include <asm/param.h>
49 #include <linux/uaccess.h>
50 #include <asm/unistd.h>
51 #include <asm/siginfo.h>
52 #include <asm/cacheflush.h>
53 #include "audit.h" /* audit_signal_info() */
56 * SLAB caches for signal bits.
59 static struct kmem_cache *sigqueue_cachep;
61 int print_fatal_signals __read_mostly;
63 static void __user *sig_handler(struct task_struct *t, int sig)
65 return t->sighand->action[sig - 1].sa.sa_handler;
68 static inline bool sig_handler_ignored(void __user *handler, int sig)
70 /* Is it explicitly or implicitly ignored? */
71 return handler == SIG_IGN ||
72 (handler == SIG_DFL && sig_kernel_ignore(sig));
75 static bool sig_task_ignored(struct task_struct *t, int sig, bool force)
79 handler = sig_handler(t, sig);
81 /* SIGKILL and SIGSTOP may not be sent to the global init */
82 if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
85 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
86 handler == SIG_DFL && !(force && sig_kernel_only(sig)))
89 return sig_handler_ignored(handler, sig);
92 static bool sig_ignored(struct task_struct *t, int sig, bool force)
95 * Blocked signals are never ignored, since the
96 * signal handler may change by the time it is
99 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
103 * Tracers may want to know about even ignored signal unless it
104 * is SIGKILL which can't be reported anyway but can be ignored
105 * by SIGNAL_UNKILLABLE task.
107 if (t->ptrace && sig != SIGKILL)
110 return sig_task_ignored(t, sig, force);
114 * Re-calculate pending state from the set of locally pending
115 * signals, globally pending signals, and blocked signals.
117 static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)
122 switch (_NSIG_WORDS) {
124 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
125 ready |= signal->sig[i] &~ blocked->sig[i];
128 case 4: ready = signal->sig[3] &~ blocked->sig[3];
129 ready |= signal->sig[2] &~ blocked->sig[2];
130 ready |= signal->sig[1] &~ blocked->sig[1];
131 ready |= signal->sig[0] &~ blocked->sig[0];
134 case 2: ready = signal->sig[1] &~ blocked->sig[1];
135 ready |= signal->sig[0] &~ blocked->sig[0];
138 case 1: ready = signal->sig[0] &~ blocked->sig[0];
143 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
145 static bool recalc_sigpending_tsk(struct task_struct *t)
147 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
148 PENDING(&t->pending, &t->blocked) ||
149 PENDING(&t->signal->shared_pending, &t->blocked)) {
150 set_tsk_thread_flag(t, TIF_SIGPENDING);
155 * We must never clear the flag in another thread, or in current
156 * when it's possible the current syscall is returning -ERESTART*.
157 * So we don't clear it here, and only callers who know they should do.
163 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
164 * This is superfluous when called on current, the wakeup is a harmless no-op.
166 void recalc_sigpending_and_wake(struct task_struct *t)
168 if (recalc_sigpending_tsk(t))
169 signal_wake_up(t, 0);
172 void recalc_sigpending(void)
174 if (!recalc_sigpending_tsk(current) && !freezing(current) &&
175 !klp_patch_pending(current))
176 clear_thread_flag(TIF_SIGPENDING);
180 void calculate_sigpending(void)
182 /* Have any signals or users of TIF_SIGPENDING been delayed
185 spin_lock_irq(¤t->sighand->siglock);
186 set_tsk_thread_flag(current, TIF_SIGPENDING);
188 spin_unlock_irq(¤t->sighand->siglock);
191 /* Given the mask, find the first available signal that should be serviced. */
193 #define SYNCHRONOUS_MASK \
194 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
195 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
197 int next_signal(struct sigpending *pending, sigset_t *mask)
199 unsigned long i, *s, *m, x;
202 s = pending->signal.sig;
206 * Handle the first word specially: it contains the
207 * synchronous signals that need to be dequeued first.
211 if (x & SYNCHRONOUS_MASK)
212 x &= SYNCHRONOUS_MASK;
217 switch (_NSIG_WORDS) {
219 for (i = 1; i < _NSIG_WORDS; ++i) {
223 sig = ffz(~x) + i*_NSIG_BPW + 1;
232 sig = ffz(~x) + _NSIG_BPW + 1;
243 static inline void print_dropped_signal(int sig)
245 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
247 if (!print_fatal_signals)
250 if (!__ratelimit(&ratelimit_state))
253 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
254 current->comm, current->pid, sig);
258 * task_set_jobctl_pending - set jobctl pending bits
260 * @mask: pending bits to set
262 * Clear @mask from @task->jobctl. @mask must be subset of
263 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
264 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
265 * cleared. If @task is already being killed or exiting, this function
269 * Must be called with @task->sighand->siglock held.
272 * %true if @mask is set, %false if made noop because @task was dying.
274 bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
276 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
277 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
278 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
280 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
283 if (mask & JOBCTL_STOP_SIGMASK)
284 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
286 task->jobctl |= mask;
291 * task_clear_jobctl_trapping - clear jobctl trapping bit
294 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
295 * Clear it and wake up the ptracer. Note that we don't need any further
296 * locking. @task->siglock guarantees that @task->parent points to the
300 * Must be called with @task->sighand->siglock held.
302 void task_clear_jobctl_trapping(struct task_struct *task)
304 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
305 task->jobctl &= ~JOBCTL_TRAPPING;
306 smp_mb(); /* advised by wake_up_bit() */
307 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
312 * task_clear_jobctl_pending - clear jobctl pending bits
314 * @mask: pending bits to clear
316 * Clear @mask from @task->jobctl. @mask must be subset of
317 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
318 * STOP bits are cleared together.
320 * If clearing of @mask leaves no stop or trap pending, this function calls
321 * task_clear_jobctl_trapping().
324 * Must be called with @task->sighand->siglock held.
326 void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
328 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
330 if (mask & JOBCTL_STOP_PENDING)
331 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
333 task->jobctl &= ~mask;
335 if (!(task->jobctl & JOBCTL_PENDING_MASK))
336 task_clear_jobctl_trapping(task);
340 * task_participate_group_stop - participate in a group stop
341 * @task: task participating in a group stop
343 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
344 * Group stop states are cleared and the group stop count is consumed if
345 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
346 * stop, the appropriate %SIGNAL_* flags are set.
349 * Must be called with @task->sighand->siglock held.
352 * %true if group stop completion should be notified to the parent, %false
355 static bool task_participate_group_stop(struct task_struct *task)
357 struct signal_struct *sig = task->signal;
358 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
360 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
362 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
367 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
368 sig->group_stop_count--;
371 * Tell the caller to notify completion iff we are entering into a
372 * fresh group stop. Read comment in do_signal_stop() for details.
374 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
375 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
381 void task_join_group_stop(struct task_struct *task)
383 /* Have the new thread join an on-going signal group stop */
384 unsigned long jobctl = current->jobctl;
385 if (jobctl & JOBCTL_STOP_PENDING) {
386 struct signal_struct *sig = current->signal;
387 unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK;
388 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
389 if (task_set_jobctl_pending(task, signr | gstop)) {
390 sig->group_stop_count++;
396 * allocate a new signal queue record
397 * - this may be called without locks if and only if t == current, otherwise an
398 * appropriate lock must be held to stop the target task from exiting
400 static struct sigqueue *
401 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
403 struct sigqueue *q = NULL;
404 struct user_struct *user;
407 * Protect access to @t credentials. This can go away when all
408 * callers hold rcu read lock.
411 user = get_uid(__task_cred(t)->user);
412 atomic_inc(&user->sigpending);
415 if (override_rlimit ||
416 atomic_read(&user->sigpending) <=
417 task_rlimit(t, RLIMIT_SIGPENDING)) {
418 q = kmem_cache_alloc(sigqueue_cachep, flags);
420 print_dropped_signal(sig);
423 if (unlikely(q == NULL)) {
424 atomic_dec(&user->sigpending);
427 INIT_LIST_HEAD(&q->list);
435 static void __sigqueue_free(struct sigqueue *q)
437 if (q->flags & SIGQUEUE_PREALLOC)
439 atomic_dec(&q->user->sigpending);
441 kmem_cache_free(sigqueue_cachep, q);
444 void flush_sigqueue(struct sigpending *queue)
448 sigemptyset(&queue->signal);
449 while (!list_empty(&queue->list)) {
450 q = list_entry(queue->list.next, struct sigqueue , list);
451 list_del_init(&q->list);
457 * Flush all pending signals for this kthread.
459 void flush_signals(struct task_struct *t)
463 spin_lock_irqsave(&t->sighand->siglock, flags);
464 clear_tsk_thread_flag(t, TIF_SIGPENDING);
465 flush_sigqueue(&t->pending);
466 flush_sigqueue(&t->signal->shared_pending);
467 spin_unlock_irqrestore(&t->sighand->siglock, flags);
470 #ifdef CONFIG_POSIX_TIMERS
471 static void __flush_itimer_signals(struct sigpending *pending)
473 sigset_t signal, retain;
474 struct sigqueue *q, *n;
476 signal = pending->signal;
477 sigemptyset(&retain);
479 list_for_each_entry_safe(q, n, &pending->list, list) {
480 int sig = q->info.si_signo;
482 if (likely(q->info.si_code != SI_TIMER)) {
483 sigaddset(&retain, sig);
485 sigdelset(&signal, sig);
486 list_del_init(&q->list);
491 sigorsets(&pending->signal, &signal, &retain);
494 void flush_itimer_signals(void)
496 struct task_struct *tsk = current;
499 spin_lock_irqsave(&tsk->sighand->siglock, flags);
500 __flush_itimer_signals(&tsk->pending);
501 __flush_itimer_signals(&tsk->signal->shared_pending);
502 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
506 void ignore_signals(struct task_struct *t)
510 for (i = 0; i < _NSIG; ++i)
511 t->sighand->action[i].sa.sa_handler = SIG_IGN;
517 * Flush all handlers for a task.
521 flush_signal_handlers(struct task_struct *t, int force_default)
524 struct k_sigaction *ka = &t->sighand->action[0];
525 for (i = _NSIG ; i != 0 ; i--) {
526 if (force_default || ka->sa.sa_handler != SIG_IGN)
527 ka->sa.sa_handler = SIG_DFL;
529 #ifdef __ARCH_HAS_SA_RESTORER
530 ka->sa.sa_restorer = NULL;
532 sigemptyset(&ka->sa.sa_mask);
537 bool unhandled_signal(struct task_struct *tsk, int sig)
539 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
540 if (is_global_init(tsk))
543 if (handler != SIG_IGN && handler != SIG_DFL)
546 /* if ptraced, let the tracer determine */
550 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
553 struct sigqueue *q, *first = NULL;
556 * Collect the siginfo appropriate to this signal. Check if
557 * there is another siginfo for the same signal.
559 list_for_each_entry(q, &list->list, list) {
560 if (q->info.si_signo == sig) {
567 sigdelset(&list->signal, sig);
571 list_del_init(&first->list);
572 copy_siginfo(info, &first->info);
575 (first->flags & SIGQUEUE_PREALLOC) &&
576 (info->si_code == SI_TIMER) &&
577 (info->si_sys_private);
579 __sigqueue_free(first);
582 * Ok, it wasn't in the queue. This must be
583 * a fast-pathed signal or we must have been
584 * out of queue space. So zero out the info.
587 info->si_signo = sig;
589 info->si_code = SI_USER;
595 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
596 siginfo_t *info, bool *resched_timer)
598 int sig = next_signal(pending, mask);
601 collect_signal(sig, pending, info, resched_timer);
606 * Dequeue a signal and return the element to the caller, which is
607 * expected to free it.
609 * All callers have to hold the siglock.
611 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
613 bool resched_timer = false;
616 /* We only dequeue private signals from ourselves, we don't let
617 * signalfd steal them
619 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
621 signr = __dequeue_signal(&tsk->signal->shared_pending,
622 mask, info, &resched_timer);
623 #ifdef CONFIG_POSIX_TIMERS
627 * itimers are process shared and we restart periodic
628 * itimers in the signal delivery path to prevent DoS
629 * attacks in the high resolution timer case. This is
630 * compliant with the old way of self-restarting
631 * itimers, as the SIGALRM is a legacy signal and only
632 * queued once. Changing the restart behaviour to
633 * restart the timer in the signal dequeue path is
634 * reducing the timer noise on heavy loaded !highres
637 if (unlikely(signr == SIGALRM)) {
638 struct hrtimer *tmr = &tsk->signal->real_timer;
640 if (!hrtimer_is_queued(tmr) &&
641 tsk->signal->it_real_incr != 0) {
642 hrtimer_forward(tmr, tmr->base->get_time(),
643 tsk->signal->it_real_incr);
644 hrtimer_restart(tmr);
654 if (unlikely(sig_kernel_stop(signr))) {
656 * Set a marker that we have dequeued a stop signal. Our
657 * caller might release the siglock and then the pending
658 * stop signal it is about to process is no longer in the
659 * pending bitmasks, but must still be cleared by a SIGCONT
660 * (and overruled by a SIGKILL). So those cases clear this
661 * shared flag after we've set it. Note that this flag may
662 * remain set after the signal we return is ignored or
663 * handled. That doesn't matter because its only purpose
664 * is to alert stop-signal processing code when another
665 * processor has come along and cleared the flag.
667 current->jobctl |= JOBCTL_STOP_DEQUEUED;
669 #ifdef CONFIG_POSIX_TIMERS
672 * Release the siglock to ensure proper locking order
673 * of timer locks outside of siglocks. Note, we leave
674 * irqs disabled here, since the posix-timers code is
675 * about to disable them again anyway.
677 spin_unlock(&tsk->sighand->siglock);
678 posixtimer_rearm(info);
679 spin_lock(&tsk->sighand->siglock);
681 /* Don't expose the si_sys_private value to userspace */
682 info->si_sys_private = 0;
688 static int dequeue_synchronous_signal(siginfo_t *info)
690 struct task_struct *tsk = current;
691 struct sigpending *pending = &tsk->pending;
692 struct sigqueue *q, *sync = NULL;
695 * Might a synchronous signal be in the queue?
697 if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
701 * Return the first synchronous signal in the queue.
703 list_for_each_entry(q, &pending->list, list) {
704 /* Synchronous signals have a postive si_code */
705 if ((q->info.si_code > SI_USER) &&
706 (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
714 * Check if there is another siginfo for the same signal.
716 list_for_each_entry_continue(q, &pending->list, list) {
717 if (q->info.si_signo == sync->info.si_signo)
721 sigdelset(&pending->signal, sync->info.si_signo);
724 list_del_init(&sync->list);
725 copy_siginfo(info, &sync->info);
726 __sigqueue_free(sync);
727 return info->si_signo;
731 * Tell a process that it has a new active signal..
733 * NOTE! we rely on the previous spin_lock to
734 * lock interrupts for us! We can only be called with
735 * "siglock" held, and the local interrupt must
736 * have been disabled when that got acquired!
738 * No need to set need_resched since signal event passing
739 * goes through ->blocked
741 void signal_wake_up_state(struct task_struct *t, unsigned int state)
743 set_tsk_thread_flag(t, TIF_SIGPENDING);
745 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
746 * case. We don't check t->state here because there is a race with it
747 * executing another processor and just now entering stopped state.
748 * By using wake_up_state, we ensure the process will wake up and
749 * handle its death signal.
751 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
756 * Remove signals in mask from the pending set and queue.
757 * Returns 1 if any signals were found.
759 * All callers must be holding the siglock.
761 static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
763 struct sigqueue *q, *n;
766 sigandsets(&m, mask, &s->signal);
767 if (sigisemptyset(&m))
770 sigandnsets(&s->signal, &s->signal, mask);
771 list_for_each_entry_safe(q, n, &s->list, list) {
772 if (sigismember(mask, q->info.si_signo)) {
773 list_del_init(&q->list);
779 static inline int is_si_special(const struct siginfo *info)
781 return info <= SEND_SIG_FORCED;
784 static inline bool si_fromuser(const struct siginfo *info)
786 return info == SEND_SIG_NOINFO ||
787 (!is_si_special(info) && SI_FROMUSER(info));
791 * called with RCU read lock from check_kill_permission()
793 static bool kill_ok_by_cred(struct task_struct *t)
795 const struct cred *cred = current_cred();
796 const struct cred *tcred = __task_cred(t);
798 return uid_eq(cred->euid, tcred->suid) ||
799 uid_eq(cred->euid, tcred->uid) ||
800 uid_eq(cred->uid, tcred->suid) ||
801 uid_eq(cred->uid, tcred->uid) ||
802 ns_capable(tcred->user_ns, CAP_KILL);
806 * Bad permissions for sending the signal
807 * - the caller must hold the RCU read lock
809 static int check_kill_permission(int sig, struct siginfo *info,
810 struct task_struct *t)
815 if (!valid_signal(sig))
818 if (!si_fromuser(info))
821 error = audit_signal_info(sig, t); /* Let audit system see the signal */
825 if (!same_thread_group(current, t) &&
826 !kill_ok_by_cred(t)) {
829 sid = task_session(t);
831 * We don't return the error if sid == NULL. The
832 * task was unhashed, the caller must notice this.
834 if (!sid || sid == task_session(current))
841 return security_task_kill(t, info, sig, NULL);
845 * ptrace_trap_notify - schedule trap to notify ptracer
846 * @t: tracee wanting to notify tracer
848 * This function schedules sticky ptrace trap which is cleared on the next
849 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
852 * If @t is running, STOP trap will be taken. If trapped for STOP and
853 * ptracer is listening for events, tracee is woken up so that it can
854 * re-trap for the new event. If trapped otherwise, STOP trap will be
855 * eventually taken without returning to userland after the existing traps
856 * are finished by PTRACE_CONT.
859 * Must be called with @task->sighand->siglock held.
861 static void ptrace_trap_notify(struct task_struct *t)
863 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
864 assert_spin_locked(&t->sighand->siglock);
866 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
867 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
871 * Handle magic process-wide effects of stop/continue signals. Unlike
872 * the signal actions, these happen immediately at signal-generation
873 * time regardless of blocking, ignoring, or handling. This does the
874 * actual continuing for SIGCONT, but not the actual stopping for stop
875 * signals. The process stop is done as a signal action for SIG_DFL.
877 * Returns true if the signal should be actually delivered, otherwise
878 * it should be dropped.
880 static bool prepare_signal(int sig, struct task_struct *p, bool force)
882 struct signal_struct *signal = p->signal;
883 struct task_struct *t;
886 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
887 if (!(signal->flags & SIGNAL_GROUP_EXIT))
888 return sig == SIGKILL;
890 * The process is in the middle of dying, nothing to do.
892 } else if (sig_kernel_stop(sig)) {
894 * This is a stop signal. Remove SIGCONT from all queues.
896 siginitset(&flush, sigmask(SIGCONT));
897 flush_sigqueue_mask(&flush, &signal->shared_pending);
898 for_each_thread(p, t)
899 flush_sigqueue_mask(&flush, &t->pending);
900 } else if (sig == SIGCONT) {
903 * Remove all stop signals from all queues, wake all threads.
905 siginitset(&flush, SIG_KERNEL_STOP_MASK);
906 flush_sigqueue_mask(&flush, &signal->shared_pending);
907 for_each_thread(p, t) {
908 flush_sigqueue_mask(&flush, &t->pending);
909 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
910 if (likely(!(t->ptrace & PT_SEIZED)))
911 wake_up_state(t, __TASK_STOPPED);
913 ptrace_trap_notify(t);
917 * Notify the parent with CLD_CONTINUED if we were stopped.
919 * If we were in the middle of a group stop, we pretend it
920 * was already finished, and then continued. Since SIGCHLD
921 * doesn't queue we report only CLD_STOPPED, as if the next
922 * CLD_CONTINUED was dropped.
925 if (signal->flags & SIGNAL_STOP_STOPPED)
926 why |= SIGNAL_CLD_CONTINUED;
927 else if (signal->group_stop_count)
928 why |= SIGNAL_CLD_STOPPED;
932 * The first thread which returns from do_signal_stop()
933 * will take ->siglock, notice SIGNAL_CLD_MASK, and
934 * notify its parent. See get_signal_to_deliver().
936 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
937 signal->group_stop_count = 0;
938 signal->group_exit_code = 0;
942 return !sig_ignored(p, sig, force);
946 * Test if P wants to take SIG. After we've checked all threads with this,
947 * it's equivalent to finding no threads not blocking SIG. Any threads not
948 * blocking SIG were ruled out because they are not running and already
949 * have pending signals. Such threads will dequeue from the shared queue
950 * as soon as they're available, so putting the signal on the shared queue
951 * will be equivalent to sending it to one such thread.
953 static inline bool wants_signal(int sig, struct task_struct *p)
955 if (sigismember(&p->blocked, sig))
958 if (p->flags & PF_EXITING)
964 if (task_is_stopped_or_traced(p))
967 return task_curr(p) || !signal_pending(p);
970 static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
972 struct signal_struct *signal = p->signal;
973 struct task_struct *t;
976 * Now find a thread we can wake up to take the signal off the queue.
978 * If the main thread wants the signal, it gets first crack.
979 * Probably the least surprising to the average bear.
981 if (wants_signal(sig, p))
983 else if ((type == PIDTYPE_PID) || thread_group_empty(p))
985 * There is just one thread and it does not need to be woken.
986 * It will dequeue unblocked signals before it runs again.
991 * Otherwise try to find a suitable thread.
993 t = signal->curr_target;
994 while (!wants_signal(sig, t)) {
996 if (t == signal->curr_target)
998 * No thread needs to be woken.
999 * Any eligible threads will see
1000 * the signal in the queue soon.
1004 signal->curr_target = t;
1008 * Found a killable thread. If the signal will be fatal,
1009 * then start taking the whole group down immediately.
1011 if (sig_fatal(p, sig) &&
1012 !(signal->flags & SIGNAL_GROUP_EXIT) &&
1013 !sigismember(&t->real_blocked, sig) &&
1014 (sig == SIGKILL || !p->ptrace)) {
1016 * This signal will be fatal to the whole group.
1018 if (!sig_kernel_coredump(sig)) {
1020 * Start a group exit and wake everybody up.
1021 * This way we don't have other threads
1022 * running and doing things after a slower
1023 * thread has the fatal signal pending.
1025 signal->flags = SIGNAL_GROUP_EXIT;
1026 signal->group_exit_code = sig;
1027 signal->group_stop_count = 0;
1030 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1031 sigaddset(&t->pending.signal, SIGKILL);
1032 signal_wake_up(t, 1);
1033 } while_each_thread(p, t);
1039 * The signal is already in the shared-pending queue.
1040 * Tell the chosen thread to wake up and dequeue it.
1042 signal_wake_up(t, sig == SIGKILL);
1046 static inline bool legacy_queue(struct sigpending *signals, int sig)
1048 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1051 #ifdef CONFIG_USER_NS
1052 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1054 if (current_user_ns() == task_cred_xxx(t, user_ns))
1057 if (SI_FROMKERNEL(info))
1061 info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1062 make_kuid(current_user_ns(), info->si_uid));
1066 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1072 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1073 enum pid_type type, int from_ancestor_ns)
1075 struct sigpending *pending;
1077 int override_rlimit;
1078 int ret = 0, result;
1080 assert_spin_locked(&t->sighand->siglock);
1082 result = TRACE_SIGNAL_IGNORED;
1083 if (!prepare_signal(sig, t,
1084 from_ancestor_ns || (info == SEND_SIG_PRIV) || (info == SEND_SIG_FORCED)))
1087 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1089 * Short-circuit ignored signals and support queuing
1090 * exactly one non-rt signal, so that we can get more
1091 * detailed information about the cause of the signal.
1093 result = TRACE_SIGNAL_ALREADY_PENDING;
1094 if (legacy_queue(pending, sig))
1097 result = TRACE_SIGNAL_DELIVERED;
1099 * fast-pathed signals for kernel-internal things like SIGSTOP
1102 if (info == SEND_SIG_FORCED)
1106 * Real-time signals must be queued if sent by sigqueue, or
1107 * some other real-time mechanism. It is implementation
1108 * defined whether kill() does so. We attempt to do so, on
1109 * the principle of least surprise, but since kill is not
1110 * allowed to fail with EAGAIN when low on memory we just
1111 * make sure at least one signal gets delivered and don't
1112 * pass on the info struct.
1115 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1117 override_rlimit = 0;
1119 q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
1121 list_add_tail(&q->list, &pending->list);
1122 switch ((unsigned long) info) {
1123 case (unsigned long) SEND_SIG_NOINFO:
1124 clear_siginfo(&q->info);
1125 q->info.si_signo = sig;
1126 q->info.si_errno = 0;
1127 q->info.si_code = SI_USER;
1128 q->info.si_pid = task_tgid_nr_ns(current,
1129 task_active_pid_ns(t));
1130 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1132 case (unsigned long) SEND_SIG_PRIV:
1133 clear_siginfo(&q->info);
1134 q->info.si_signo = sig;
1135 q->info.si_errno = 0;
1136 q->info.si_code = SI_KERNEL;
1141 copy_siginfo(&q->info, info);
1142 if (from_ancestor_ns)
1147 userns_fixup_signal_uid(&q->info, t);
1149 } else if (!is_si_special(info)) {
1150 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1152 * Queue overflow, abort. We may abort if the
1153 * signal was rt and sent by user using something
1154 * other than kill().
1156 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1161 * This is a silent loss of information. We still
1162 * send the signal, but the *info bits are lost.
1164 result = TRACE_SIGNAL_LOSE_INFO;
1169 signalfd_notify(t, sig);
1170 sigaddset(&pending->signal, sig);
1172 /* Let multiprocess signals appear after on-going forks */
1173 if (type > PIDTYPE_TGID) {
1174 struct multiprocess_signals *delayed;
1175 hlist_for_each_entry(delayed, &t->signal->multiprocess, node) {
1176 sigset_t *signal = &delayed->signal;
1177 /* Can't queue both a stop and a continue signal */
1179 sigdelsetmask(signal, SIG_KERNEL_STOP_MASK);
1180 else if (sig_kernel_stop(sig))
1181 sigdelset(signal, SIGCONT);
1182 sigaddset(signal, sig);
1186 complete_signal(sig, t, type);
1188 trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
1192 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1195 int from_ancestor_ns = 0;
1197 #ifdef CONFIG_PID_NS
1198 from_ancestor_ns = si_fromuser(info) &&
1199 !task_pid_nr_ns(current, task_active_pid_ns(t));
1202 return __send_signal(sig, info, t, type, from_ancestor_ns);
1205 static void print_fatal_signal(int signr)
1207 struct pt_regs *regs = signal_pt_regs();
1208 pr_info("potentially unexpected fatal signal %d.\n", signr);
1210 #if defined(__i386__) && !defined(__arch_um__)
1211 pr_info("code at %08lx: ", regs->ip);
1214 for (i = 0; i < 16; i++) {
1217 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1219 pr_cont("%02x ", insn);
1229 static int __init setup_print_fatal_signals(char *str)
1231 get_option (&str, &print_fatal_signals);
1236 __setup("print-fatal-signals=", setup_print_fatal_signals);
1239 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1241 return send_signal(sig, info, p, PIDTYPE_TGID);
1245 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1247 return send_signal(sig, info, t, PIDTYPE_PID);
1250 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1253 unsigned long flags;
1256 if (lock_task_sighand(p, &flags)) {
1257 ret = send_signal(sig, info, p, type);
1258 unlock_task_sighand(p, &flags);
1265 * Force a signal that the process can't ignore: if necessary
1266 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1268 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1269 * since we do not want to have a signal handler that was blocked
1270 * be invoked when user space had explicitly blocked it.
1272 * We don't want to have recursive SIGSEGV's etc, for example,
1273 * that is why we also clear SIGNAL_UNKILLABLE.
1276 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1278 unsigned long int flags;
1279 int ret, blocked, ignored;
1280 struct k_sigaction *action;
1282 spin_lock_irqsave(&t->sighand->siglock, flags);
1283 action = &t->sighand->action[sig-1];
1284 ignored = action->sa.sa_handler == SIG_IGN;
1285 blocked = sigismember(&t->blocked, sig);
1286 if (blocked || ignored) {
1287 action->sa.sa_handler = SIG_DFL;
1289 sigdelset(&t->blocked, sig);
1290 recalc_sigpending_and_wake(t);
1294 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
1295 * debugging to leave init killable.
1297 if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
1298 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1299 ret = specific_send_sig_info(sig, info, t);
1300 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1306 * Nuke all other threads in the group.
1308 int zap_other_threads(struct task_struct *p)
1310 struct task_struct *t = p;
1313 p->signal->group_stop_count = 0;
1315 while_each_thread(p, t) {
1316 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1319 /* Don't bother with already dead threads */
1322 sigaddset(&t->pending.signal, SIGKILL);
1323 signal_wake_up(t, 1);
1329 struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1330 unsigned long *flags)
1332 struct sighand_struct *sighand;
1336 sighand = rcu_dereference(tsk->sighand);
1337 if (unlikely(sighand == NULL))
1341 * This sighand can be already freed and even reused, but
1342 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
1343 * initializes ->siglock: this slab can't go away, it has
1344 * the same object type, ->siglock can't be reinitialized.
1346 * We need to ensure that tsk->sighand is still the same
1347 * after we take the lock, we can race with de_thread() or
1348 * __exit_signal(). In the latter case the next iteration
1349 * must see ->sighand == NULL.
1351 spin_lock_irqsave(&sighand->siglock, *flags);
1352 if (likely(sighand == tsk->sighand))
1354 spin_unlock_irqrestore(&sighand->siglock, *flags);
1362 * send signal info to all the members of a group
1364 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1370 ret = check_kill_permission(sig, info, p);
1374 ret = do_send_sig_info(sig, info, p, type);
1380 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1381 * control characters do (^C, ^Z etc)
1382 * - the caller must hold at least a readlock on tasklist_lock
1384 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1386 struct task_struct *p = NULL;
1387 int retval, success;
1391 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1392 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
1395 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1396 return success ? 0 : retval;
1399 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1402 struct task_struct *p;
1406 p = pid_task(pid, PIDTYPE_PID);
1408 error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
1410 if (likely(!p || error != -ESRCH))
1414 * The task was unhashed in between, try again. If it
1415 * is dead, pid_task() will return NULL, if we race with
1416 * de_thread() it will find the new leader.
1421 static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1425 error = kill_pid_info(sig, info, find_vpid(pid));
1430 static inline bool kill_as_cred_perm(const struct cred *cred,
1431 struct task_struct *target)
1433 const struct cred *pcred = __task_cred(target);
1435 return uid_eq(cred->euid, pcred->suid) ||
1436 uid_eq(cred->euid, pcred->uid) ||
1437 uid_eq(cred->uid, pcred->suid) ||
1438 uid_eq(cred->uid, pcred->uid);
1441 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1442 int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1443 const struct cred *cred)
1446 struct task_struct *p;
1447 unsigned long flags;
1449 if (!valid_signal(sig))
1453 p = pid_task(pid, PIDTYPE_PID);
1458 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
1462 ret = security_task_kill(p, info, sig, cred);
1467 if (lock_task_sighand(p, &flags)) {
1468 ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
1469 unlock_task_sighand(p, &flags);
1477 EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
1480 * kill_something_info() interprets pid in interesting ways just like kill(2).
1482 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1483 * is probably wrong. Should make it like BSD or SYSV.
1486 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1492 ret = kill_pid_info(sig, info, find_vpid(pid));
1497 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1501 read_lock(&tasklist_lock);
1503 ret = __kill_pgrp_info(sig, info,
1504 pid ? find_vpid(-pid) : task_pgrp(current));
1506 int retval = 0, count = 0;
1507 struct task_struct * p;
1509 for_each_process(p) {
1510 if (task_pid_vnr(p) > 1 &&
1511 !same_thread_group(p, current)) {
1512 int err = group_send_sig_info(sig, info, p,
1519 ret = count ? retval : -ESRCH;
1521 read_unlock(&tasklist_lock);
1527 * These are for backward compatibility with the rest of the kernel source.
1530 int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1533 * Make sure legacy kernel users don't send in bad values
1534 * (normal paths check this in check_kill_permission).
1536 if (!valid_signal(sig))
1539 return do_send_sig_info(sig, info, p, PIDTYPE_PID);
1542 #define __si_special(priv) \
1543 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1546 send_sig(int sig, struct task_struct *p, int priv)
1548 return send_sig_info(sig, __si_special(priv), p);
1551 void force_sig(int sig, struct task_struct *p)
1553 force_sig_info(sig, SEND_SIG_PRIV, p);
1557 * When things go south during signal handling, we
1558 * will force a SIGSEGV. And if the signal that caused
1559 * the problem was already a SIGSEGV, we'll want to
1560 * make sure we don't even try to deliver the signal..
1562 void force_sigsegv(int sig, struct task_struct *p)
1564 if (sig == SIGSEGV) {
1565 unsigned long flags;
1566 spin_lock_irqsave(&p->sighand->siglock, flags);
1567 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1568 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1570 force_sig(SIGSEGV, p);
1573 int force_sig_fault(int sig, int code, void __user *addr
1574 ___ARCH_SI_TRAPNO(int trapno)
1575 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1576 , struct task_struct *t)
1578 struct siginfo info;
1580 clear_siginfo(&info);
1581 info.si_signo = sig;
1583 info.si_code = code;
1584 info.si_addr = addr;
1585 #ifdef __ARCH_SI_TRAPNO
1586 info.si_trapno = trapno;
1590 info.si_flags = flags;
1593 return force_sig_info(info.si_signo, &info, t);
1596 int send_sig_fault(int sig, int code, void __user *addr
1597 ___ARCH_SI_TRAPNO(int trapno)
1598 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1599 , struct task_struct *t)
1601 struct siginfo info;
1603 clear_siginfo(&info);
1604 info.si_signo = sig;
1606 info.si_code = code;
1607 info.si_addr = addr;
1608 #ifdef __ARCH_SI_TRAPNO
1609 info.si_trapno = trapno;
1613 info.si_flags = flags;
1616 return send_sig_info(info.si_signo, &info, t);
1619 int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1621 struct siginfo info;
1623 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1624 clear_siginfo(&info);
1625 info.si_signo = SIGBUS;
1627 info.si_code = code;
1628 info.si_addr = addr;
1629 info.si_addr_lsb = lsb;
1630 return force_sig_info(info.si_signo, &info, t);
1633 int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1635 struct siginfo info;
1637 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1638 clear_siginfo(&info);
1639 info.si_signo = SIGBUS;
1641 info.si_code = code;
1642 info.si_addr = addr;
1643 info.si_addr_lsb = lsb;
1644 return send_sig_info(info.si_signo, &info, t);
1646 EXPORT_SYMBOL(send_sig_mceerr);
1648 int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
1650 struct siginfo info;
1652 clear_siginfo(&info);
1653 info.si_signo = SIGSEGV;
1655 info.si_code = SEGV_BNDERR;
1656 info.si_addr = addr;
1657 info.si_lower = lower;
1658 info.si_upper = upper;
1659 return force_sig_info(info.si_signo, &info, current);
1663 int force_sig_pkuerr(void __user *addr, u32 pkey)
1665 struct siginfo info;
1667 clear_siginfo(&info);
1668 info.si_signo = SIGSEGV;
1670 info.si_code = SEGV_PKUERR;
1671 info.si_addr = addr;
1672 info.si_pkey = pkey;
1673 return force_sig_info(info.si_signo, &info, current);
1677 /* For the crazy architectures that include trap information in
1678 * the errno field, instead of an actual errno value.
1680 int force_sig_ptrace_errno_trap(int errno, void __user *addr)
1682 struct siginfo info;
1684 clear_siginfo(&info);
1685 info.si_signo = SIGTRAP;
1686 info.si_errno = errno;
1687 info.si_code = TRAP_HWBKPT;
1688 info.si_addr = addr;
1689 return force_sig_info(info.si_signo, &info, current);
1692 int kill_pgrp(struct pid *pid, int sig, int priv)
1696 read_lock(&tasklist_lock);
1697 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1698 read_unlock(&tasklist_lock);
1702 EXPORT_SYMBOL(kill_pgrp);
1704 int kill_pid(struct pid *pid, int sig, int priv)
1706 return kill_pid_info(sig, __si_special(priv), pid);
1708 EXPORT_SYMBOL(kill_pid);
1711 * These functions support sending signals using preallocated sigqueue
1712 * structures. This is needed "because realtime applications cannot
1713 * afford to lose notifications of asynchronous events, like timer
1714 * expirations or I/O completions". In the case of POSIX Timers
1715 * we allocate the sigqueue structure from the timer_create. If this
1716 * allocation fails we are able to report the failure to the application
1717 * with an EAGAIN error.
1719 struct sigqueue *sigqueue_alloc(void)
1721 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1724 q->flags |= SIGQUEUE_PREALLOC;
1729 void sigqueue_free(struct sigqueue *q)
1731 unsigned long flags;
1732 spinlock_t *lock = ¤t->sighand->siglock;
1734 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1736 * We must hold ->siglock while testing q->list
1737 * to serialize with collect_signal() or with
1738 * __exit_signal()->flush_sigqueue().
1740 spin_lock_irqsave(lock, flags);
1741 q->flags &= ~SIGQUEUE_PREALLOC;
1743 * If it is queued it will be freed when dequeued,
1744 * like the "regular" sigqueue.
1746 if (!list_empty(&q->list))
1748 spin_unlock_irqrestore(lock, flags);
1754 int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
1756 int sig = q->info.si_signo;
1757 struct sigpending *pending;
1758 struct task_struct *t;
1759 unsigned long flags;
1762 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1766 t = pid_task(pid, type);
1767 if (!t || !likely(lock_task_sighand(t, &flags)))
1770 ret = 1; /* the signal is ignored */
1771 result = TRACE_SIGNAL_IGNORED;
1772 if (!prepare_signal(sig, t, false))
1776 if (unlikely(!list_empty(&q->list))) {
1778 * If an SI_TIMER entry is already queue just increment
1779 * the overrun count.
1781 BUG_ON(q->info.si_code != SI_TIMER);
1782 q->info.si_overrun++;
1783 result = TRACE_SIGNAL_ALREADY_PENDING;
1786 q->info.si_overrun = 0;
1788 signalfd_notify(t, sig);
1789 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
1790 list_add_tail(&q->list, &pending->list);
1791 sigaddset(&pending->signal, sig);
1792 complete_signal(sig, t, type);
1793 result = TRACE_SIGNAL_DELIVERED;
1795 trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
1796 unlock_task_sighand(t, &flags);
1803 * Let a parent know about the death of a child.
1804 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1806 * Returns true if our parent ignored us and so we've switched to
1809 bool do_notify_parent(struct task_struct *tsk, int sig)
1811 struct siginfo info;
1812 unsigned long flags;
1813 struct sighand_struct *psig;
1814 bool autoreap = false;
1819 /* do_notify_parent_cldstop should have been called instead. */
1820 BUG_ON(task_is_stopped_or_traced(tsk));
1822 BUG_ON(!tsk->ptrace &&
1823 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1825 if (sig != SIGCHLD) {
1827 * This is only possible if parent == real_parent.
1828 * Check if it has changed security domain.
1830 if (tsk->parent_exec_id != tsk->parent->self_exec_id)
1834 clear_siginfo(&info);
1835 info.si_signo = sig;
1838 * We are under tasklist_lock here so our parent is tied to
1839 * us and cannot change.
1841 * task_active_pid_ns will always return the same pid namespace
1842 * until a task passes through release_task.
1844 * write_lock() currently calls preempt_disable() which is the
1845 * same as rcu_read_lock(), but according to Oleg, this is not
1846 * correct to rely on this
1849 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
1850 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1854 task_cputime(tsk, &utime, &stime);
1855 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
1856 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
1858 info.si_status = tsk->exit_code & 0x7f;
1859 if (tsk->exit_code & 0x80)
1860 info.si_code = CLD_DUMPED;
1861 else if (tsk->exit_code & 0x7f)
1862 info.si_code = CLD_KILLED;
1864 info.si_code = CLD_EXITED;
1865 info.si_status = tsk->exit_code >> 8;
1868 psig = tsk->parent->sighand;
1869 spin_lock_irqsave(&psig->siglock, flags);
1870 if (!tsk->ptrace && sig == SIGCHLD &&
1871 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1872 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1874 * We are exiting and our parent doesn't care. POSIX.1
1875 * defines special semantics for setting SIGCHLD to SIG_IGN
1876 * or setting the SA_NOCLDWAIT flag: we should be reaped
1877 * automatically and not left for our parent's wait4 call.
1878 * Rather than having the parent do it as a magic kind of
1879 * signal handler, we just set this to tell do_exit that we
1880 * can be cleaned up without becoming a zombie. Note that
1881 * we still call __wake_up_parent in this case, because a
1882 * blocked sys_wait4 might now return -ECHILD.
1884 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1885 * is implementation-defined: we do (if you don't want
1886 * it, just use SIG_IGN instead).
1889 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1892 if (valid_signal(sig) && sig)
1893 __group_send_sig_info(sig, &info, tsk->parent);
1894 __wake_up_parent(tsk, tsk->parent);
1895 spin_unlock_irqrestore(&psig->siglock, flags);
1901 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1902 * @tsk: task reporting the state change
1903 * @for_ptracer: the notification is for ptracer
1904 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1906 * Notify @tsk's parent that the stopped/continued state has changed. If
1907 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1908 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1911 * Must be called with tasklist_lock at least read locked.
1913 static void do_notify_parent_cldstop(struct task_struct *tsk,
1914 bool for_ptracer, int why)
1916 struct siginfo info;
1917 unsigned long flags;
1918 struct task_struct *parent;
1919 struct sighand_struct *sighand;
1923 parent = tsk->parent;
1925 tsk = tsk->group_leader;
1926 parent = tsk->real_parent;
1929 clear_siginfo(&info);
1930 info.si_signo = SIGCHLD;
1933 * see comment in do_notify_parent() about the following 4 lines
1936 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
1937 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
1940 task_cputime(tsk, &utime, &stime);
1941 info.si_utime = nsec_to_clock_t(utime);
1942 info.si_stime = nsec_to_clock_t(stime);
1947 info.si_status = SIGCONT;
1950 info.si_status = tsk->signal->group_exit_code & 0x7f;
1953 info.si_status = tsk->exit_code & 0x7f;
1959 sighand = parent->sighand;
1960 spin_lock_irqsave(&sighand->siglock, flags);
1961 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1962 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1963 __group_send_sig_info(SIGCHLD, &info, parent);
1965 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1967 __wake_up_parent(tsk, parent);
1968 spin_unlock_irqrestore(&sighand->siglock, flags);
1971 static inline bool may_ptrace_stop(void)
1973 if (!likely(current->ptrace))
1976 * Are we in the middle of do_coredump?
1977 * If so and our tracer is also part of the coredump stopping
1978 * is a deadlock situation, and pointless because our tracer
1979 * is dead so don't allow us to stop.
1980 * If SIGKILL was already sent before the caller unlocked
1981 * ->siglock we must see ->core_state != NULL. Otherwise it
1982 * is safe to enter schedule().
1984 * This is almost outdated, a task with the pending SIGKILL can't
1985 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1986 * after SIGKILL was already dequeued.
1988 if (unlikely(current->mm->core_state) &&
1989 unlikely(current->mm == current->parent->mm))
1996 * Return non-zero if there is a SIGKILL that should be waking us up.
1997 * Called with the siglock held.
1999 static bool sigkill_pending(struct task_struct *tsk)
2001 return sigismember(&tsk->pending.signal, SIGKILL) ||
2002 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
2006 * This must be called with current->sighand->siglock held.
2008 * This should be the path for all ptrace stops.
2009 * We always set current->last_siginfo while stopped here.
2010 * That makes it a way to test a stopped process for
2011 * being ptrace-stopped vs being job-control-stopped.
2013 * If we actually decide not to stop at all because the tracer
2014 * is gone, we keep current->exit_code unless clear_code.
2016 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
2017 __releases(¤t->sighand->siglock)
2018 __acquires(¤t->sighand->siglock)
2020 bool gstop_done = false;
2022 if (arch_ptrace_stop_needed(exit_code, info)) {
2024 * The arch code has something special to do before a
2025 * ptrace stop. This is allowed to block, e.g. for faults
2026 * on user stack pages. We can't keep the siglock while
2027 * calling arch_ptrace_stop, so we must release it now.
2028 * To preserve proper semantics, we must do this before
2029 * any signal bookkeeping like checking group_stop_count.
2030 * Meanwhile, a SIGKILL could come in before we retake the
2031 * siglock. That must prevent us from sleeping in TASK_TRACED.
2032 * So after regaining the lock, we must check for SIGKILL.
2034 spin_unlock_irq(¤t->sighand->siglock);
2035 arch_ptrace_stop(exit_code, info);
2036 spin_lock_irq(¤t->sighand->siglock);
2037 if (sigkill_pending(current))
2041 set_special_state(TASK_TRACED);
2044 * We're committing to trapping. TRACED should be visible before
2045 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
2046 * Also, transition to TRACED and updates to ->jobctl should be
2047 * atomic with respect to siglock and should be done after the arch
2048 * hook as siglock is released and regrabbed across it.
2053 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
2055 * set_current_state() smp_wmb();
2057 * wait_task_stopped()
2058 * task_stopped_code()
2059 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
2063 current->last_siginfo = info;
2064 current->exit_code = exit_code;
2067 * If @why is CLD_STOPPED, we're trapping to participate in a group
2068 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
2069 * across siglock relocks since INTERRUPT was scheduled, PENDING
2070 * could be clear now. We act as if SIGCONT is received after
2071 * TASK_TRACED is entered - ignore it.
2073 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
2074 gstop_done = task_participate_group_stop(current);
2076 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
2077 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
2078 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
2079 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
2081 /* entering a trap, clear TRAPPING */
2082 task_clear_jobctl_trapping(current);
2084 spin_unlock_irq(¤t->sighand->siglock);
2085 read_lock(&tasklist_lock);
2086 if (may_ptrace_stop()) {
2088 * Notify parents of the stop.
2090 * While ptraced, there are two parents - the ptracer and
2091 * the real_parent of the group_leader. The ptracer should
2092 * know about every stop while the real parent is only
2093 * interested in the completion of group stop. The states
2094 * for the two don't interact with each other. Notify
2095 * separately unless they're gonna be duplicates.
2097 do_notify_parent_cldstop(current, true, why);
2098 if (gstop_done && ptrace_reparented(current))
2099 do_notify_parent_cldstop(current, false, why);
2102 * Don't want to allow preemption here, because
2103 * sys_ptrace() needs this task to be inactive.
2105 * XXX: implement read_unlock_no_resched().
2108 read_unlock(&tasklist_lock);
2109 preempt_enable_no_resched();
2110 freezable_schedule();
2113 * By the time we got the lock, our tracer went away.
2114 * Don't drop the lock yet, another tracer may come.
2116 * If @gstop_done, the ptracer went away between group stop
2117 * completion and here. During detach, it would have set
2118 * JOBCTL_STOP_PENDING on us and we'll re-enter
2119 * TASK_STOPPED in do_signal_stop() on return, so notifying
2120 * the real parent of the group stop completion is enough.
2123 do_notify_parent_cldstop(current, false, why);
2125 /* tasklist protects us from ptrace_freeze_traced() */
2126 __set_current_state(TASK_RUNNING);
2128 current->exit_code = 0;
2129 read_unlock(&tasklist_lock);
2133 * We are back. Now reacquire the siglock before touching
2134 * last_siginfo, so that we are sure to have synchronized with
2135 * any signal-sending on another CPU that wants to examine it.
2137 spin_lock_irq(¤t->sighand->siglock);
2138 current->last_siginfo = NULL;
2140 /* LISTENING can be set only during STOP traps, clear it */
2141 current->jobctl &= ~JOBCTL_LISTENING;
2144 * Queued signals ignored us while we were stopped for tracing.
2145 * So check for any that we should take before resuming user mode.
2146 * This sets TIF_SIGPENDING, but never clears it.
2148 recalc_sigpending_tsk(current);
2151 static void ptrace_do_notify(int signr, int exit_code, int why)
2155 clear_siginfo(&info);
2156 info.si_signo = signr;
2157 info.si_code = exit_code;
2158 info.si_pid = task_pid_vnr(current);
2159 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2161 /* Let the debugger run. */
2162 ptrace_stop(exit_code, why, 1, &info);
2165 void ptrace_notify(int exit_code)
2167 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2168 if (unlikely(current->task_works))
2171 spin_lock_irq(¤t->sighand->siglock);
2172 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
2173 spin_unlock_irq(¤t->sighand->siglock);
2177 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2178 * @signr: signr causing group stop if initiating
2180 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2181 * and participate in it. If already set, participate in the existing
2182 * group stop. If participated in a group stop (and thus slept), %true is
2183 * returned with siglock released.
2185 * If ptraced, this function doesn't handle stop itself. Instead,
2186 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2187 * untouched. The caller must ensure that INTERRUPT trap handling takes
2188 * places afterwards.
2191 * Must be called with @current->sighand->siglock held, which is released
2195 * %false if group stop is already cancelled or ptrace trap is scheduled.
2196 * %true if participated in group stop.
2198 static bool do_signal_stop(int signr)
2199 __releases(¤t->sighand->siglock)
2201 struct signal_struct *sig = current->signal;
2203 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2204 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2205 struct task_struct *t;
2207 /* signr will be recorded in task->jobctl for retries */
2208 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2210 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2211 unlikely(signal_group_exit(sig)))
2214 * There is no group stop already in progress. We must
2217 * While ptraced, a task may be resumed while group stop is
2218 * still in effect and then receive a stop signal and
2219 * initiate another group stop. This deviates from the
2220 * usual behavior as two consecutive stop signals can't
2221 * cause two group stops when !ptraced. That is why we
2222 * also check !task_is_stopped(t) below.
2224 * The condition can be distinguished by testing whether
2225 * SIGNAL_STOP_STOPPED is already set. Don't generate
2226 * group_exit_code in such case.
2228 * This is not necessary for SIGNAL_STOP_CONTINUED because
2229 * an intervening stop signal is required to cause two
2230 * continued events regardless of ptrace.
2232 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2233 sig->group_exit_code = signr;
2235 sig->group_stop_count = 0;
2237 if (task_set_jobctl_pending(current, signr | gstop))
2238 sig->group_stop_count++;
2241 while_each_thread(current, t) {
2243 * Setting state to TASK_STOPPED for a group
2244 * stop is always done with the siglock held,
2245 * so this check has no races.
2247 if (!task_is_stopped(t) &&
2248 task_set_jobctl_pending(t, signr | gstop)) {
2249 sig->group_stop_count++;
2250 if (likely(!(t->ptrace & PT_SEIZED)))
2251 signal_wake_up(t, 0);
2253 ptrace_trap_notify(t);
2258 if (likely(!current->ptrace)) {
2262 * If there are no other threads in the group, or if there
2263 * is a group stop in progress and we are the last to stop,
2264 * report to the parent.
2266 if (task_participate_group_stop(current))
2267 notify = CLD_STOPPED;
2269 set_special_state(TASK_STOPPED);
2270 spin_unlock_irq(¤t->sighand->siglock);
2273 * Notify the parent of the group stop completion. Because
2274 * we're not holding either the siglock or tasklist_lock
2275 * here, ptracer may attach inbetween; however, this is for
2276 * group stop and should always be delivered to the real
2277 * parent of the group leader. The new ptracer will get
2278 * its notification when this task transitions into
2282 read_lock(&tasklist_lock);
2283 do_notify_parent_cldstop(current, false, notify);
2284 read_unlock(&tasklist_lock);
2287 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2288 freezable_schedule();
2292 * While ptraced, group stop is handled by STOP trap.
2293 * Schedule it and let the caller deal with it.
2295 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2301 * do_jobctl_trap - take care of ptrace jobctl traps
2303 * When PT_SEIZED, it's used for both group stop and explicit
2304 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2305 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2306 * the stop signal; otherwise, %SIGTRAP.
2308 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2309 * number as exit_code and no siginfo.
2312 * Must be called with @current->sighand->siglock held, which may be
2313 * released and re-acquired before returning with intervening sleep.
2315 static void do_jobctl_trap(void)
2317 struct signal_struct *signal = current->signal;
2318 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2320 if (current->ptrace & PT_SEIZED) {
2321 if (!signal->group_stop_count &&
2322 !(signal->flags & SIGNAL_STOP_STOPPED))
2324 WARN_ON_ONCE(!signr);
2325 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2328 WARN_ON_ONCE(!signr);
2329 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2330 current->exit_code = 0;
2334 static int ptrace_signal(int signr, siginfo_t *info)
2337 * We do not check sig_kernel_stop(signr) but set this marker
2338 * unconditionally because we do not know whether debugger will
2339 * change signr. This flag has no meaning unless we are going
2340 * to stop after return from ptrace_stop(). In this case it will
2341 * be checked in do_signal_stop(), we should only stop if it was
2342 * not cleared by SIGCONT while we were sleeping. See also the
2343 * comment in dequeue_signal().
2345 current->jobctl |= JOBCTL_STOP_DEQUEUED;
2346 ptrace_stop(signr, CLD_TRAPPED, 0, info);
2348 /* We're back. Did the debugger cancel the sig? */
2349 signr = current->exit_code;
2353 current->exit_code = 0;
2356 * Update the siginfo structure if the signal has
2357 * changed. If the debugger wanted something
2358 * specific in the siginfo structure then it should
2359 * have updated *info via PTRACE_SETSIGINFO.
2361 if (signr != info->si_signo) {
2362 clear_siginfo(info);
2363 info->si_signo = signr;
2365 info->si_code = SI_USER;
2367 info->si_pid = task_pid_vnr(current->parent);
2368 info->si_uid = from_kuid_munged(current_user_ns(),
2369 task_uid(current->parent));
2373 /* If the (new) signal is now blocked, requeue it. */
2374 if (sigismember(¤t->blocked, signr)) {
2375 specific_send_sig_info(signr, info, current);
2382 bool get_signal(struct ksignal *ksig)
2384 struct sighand_struct *sighand = current->sighand;
2385 struct signal_struct *signal = current->signal;
2388 if (unlikely(current->task_works))
2391 if (unlikely(uprobe_deny_signal()))
2395 * Do this once, we can't return to user-mode if freezing() == T.
2396 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2397 * thus do not need another check after return.
2402 spin_lock_irq(&sighand->siglock);
2404 * Every stopped thread goes here after wakeup. Check to see if
2405 * we should notify the parent, prepare_signal(SIGCONT) encodes
2406 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2408 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2411 if (signal->flags & SIGNAL_CLD_CONTINUED)
2412 why = CLD_CONTINUED;
2416 signal->flags &= ~SIGNAL_CLD_MASK;
2418 spin_unlock_irq(&sighand->siglock);
2421 * Notify the parent that we're continuing. This event is
2422 * always per-process and doesn't make whole lot of sense
2423 * for ptracers, who shouldn't consume the state via
2424 * wait(2) either, but, for backward compatibility, notify
2425 * the ptracer of the group leader too unless it's gonna be
2428 read_lock(&tasklist_lock);
2429 do_notify_parent_cldstop(current, false, why);
2431 if (ptrace_reparented(current->group_leader))
2432 do_notify_parent_cldstop(current->group_leader,
2434 read_unlock(&tasklist_lock);
2439 /* Has this task already been marked for death? */
2440 if (signal_group_exit(signal)) {
2441 ksig->info.si_signo = signr = SIGKILL;
2442 sigdelset(¤t->pending.signal, SIGKILL);
2443 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2444 &sighand->action[SIGKILL - 1]);
2445 recalc_sigpending();
2450 struct k_sigaction *ka;
2452 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2456 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2458 spin_unlock_irq(&sighand->siglock);
2463 * Signals generated by the execution of an instruction
2464 * need to be delivered before any other pending signals
2465 * so that the instruction pointer in the signal stack
2466 * frame points to the faulting instruction.
2468 signr = dequeue_synchronous_signal(&ksig->info);
2470 signr = dequeue_signal(current, ¤t->blocked, &ksig->info);
2473 break; /* will return 0 */
2475 if (unlikely(current->ptrace) && signr != SIGKILL) {
2476 signr = ptrace_signal(signr, &ksig->info);
2481 ka = &sighand->action[signr-1];
2483 /* Trace actually delivered signals. */
2484 trace_signal_deliver(signr, &ksig->info, ka);
2486 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2488 if (ka->sa.sa_handler != SIG_DFL) {
2489 /* Run the handler. */
2492 if (ka->sa.sa_flags & SA_ONESHOT)
2493 ka->sa.sa_handler = SIG_DFL;
2495 break; /* will return non-zero "signr" value */
2499 * Now we are doing the default action for this signal.
2501 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2505 * Global init gets no signals it doesn't want.
2506 * Container-init gets no signals it doesn't want from same
2509 * Note that if global/container-init sees a sig_kernel_only()
2510 * signal here, the signal must have been generated internally
2511 * or must have come from an ancestor namespace. In either
2512 * case, the signal cannot be dropped.
2514 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2515 !sig_kernel_only(signr))
2518 if (sig_kernel_stop(signr)) {
2520 * The default action is to stop all threads in
2521 * the thread group. The job control signals
2522 * do nothing in an orphaned pgrp, but SIGSTOP
2523 * always works. Note that siglock needs to be
2524 * dropped during the call to is_orphaned_pgrp()
2525 * because of lock ordering with tasklist_lock.
2526 * This allows an intervening SIGCONT to be posted.
2527 * We need to check for that and bail out if necessary.
2529 if (signr != SIGSTOP) {
2530 spin_unlock_irq(&sighand->siglock);
2532 /* signals can be posted during this window */
2534 if (is_current_pgrp_orphaned())
2537 spin_lock_irq(&sighand->siglock);
2540 if (likely(do_signal_stop(ksig->info.si_signo))) {
2541 /* It released the siglock. */
2546 * We didn't actually stop, due to a race
2547 * with SIGCONT or something like that.
2553 spin_unlock_irq(&sighand->siglock);
2556 * Anything else is fatal, maybe with a core dump.
2558 current->flags |= PF_SIGNALED;
2560 if (sig_kernel_coredump(signr)) {
2561 if (print_fatal_signals)
2562 print_fatal_signal(ksig->info.si_signo);
2563 proc_coredump_connector(current);
2565 * If it was able to dump core, this kills all
2566 * other threads in the group and synchronizes with
2567 * their demise. If we lost the race with another
2568 * thread getting here, it set group_exit_code
2569 * first and our do_group_exit call below will use
2570 * that value and ignore the one we pass it.
2572 do_coredump(&ksig->info);
2576 * Death signals, no core dump.
2578 do_group_exit(ksig->info.si_signo);
2581 spin_unlock_irq(&sighand->siglock);
2584 return ksig->sig > 0;
2588 * signal_delivered -
2589 * @ksig: kernel signal struct
2590 * @stepping: nonzero if debugger single-step or block-step in use
2592 * This function should be called when a signal has successfully been
2593 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2594 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2595 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
2597 static void signal_delivered(struct ksignal *ksig, int stepping)
2601 /* A signal was successfully delivered, and the
2602 saved sigmask was stored on the signal frame,
2603 and will be restored by sigreturn. So we can
2604 simply clear the restore sigmask flag. */
2605 clear_restore_sigmask();
2607 sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
2608 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2609 sigaddset(&blocked, ksig->sig);
2610 set_current_blocked(&blocked);
2611 tracehook_signal_handler(stepping);
2614 void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2617 force_sigsegv(ksig->sig, current);
2619 signal_delivered(ksig, stepping);
2623 * It could be that complete_signal() picked us to notify about the
2624 * group-wide signal. Other threads should be notified now to take
2625 * the shared signals in @which since we will not.
2627 static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2630 struct task_struct *t;
2632 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2633 if (sigisemptyset(&retarget))
2637 while_each_thread(tsk, t) {
2638 if (t->flags & PF_EXITING)
2641 if (!has_pending_signals(&retarget, &t->blocked))
2643 /* Remove the signals this thread can handle. */
2644 sigandsets(&retarget, &retarget, &t->blocked);
2646 if (!signal_pending(t))
2647 signal_wake_up(t, 0);
2649 if (sigisemptyset(&retarget))
2654 void exit_signals(struct task_struct *tsk)
2660 * @tsk is about to have PF_EXITING set - lock out users which
2661 * expect stable threadgroup.
2663 cgroup_threadgroup_change_begin(tsk);
2665 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2666 tsk->flags |= PF_EXITING;
2667 cgroup_threadgroup_change_end(tsk);
2671 spin_lock_irq(&tsk->sighand->siglock);
2673 * From now this task is not visible for group-wide signals,
2674 * see wants_signal(), do_signal_stop().
2676 tsk->flags |= PF_EXITING;
2678 cgroup_threadgroup_change_end(tsk);
2680 if (!signal_pending(tsk))
2683 unblocked = tsk->blocked;
2684 signotset(&unblocked);
2685 retarget_shared_pending(tsk, &unblocked);
2687 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2688 task_participate_group_stop(tsk))
2689 group_stop = CLD_STOPPED;
2691 spin_unlock_irq(&tsk->sighand->siglock);
2694 * If group stop has completed, deliver the notification. This
2695 * should always go to the real parent of the group leader.
2697 if (unlikely(group_stop)) {
2698 read_lock(&tasklist_lock);
2699 do_notify_parent_cldstop(tsk, false, group_stop);
2700 read_unlock(&tasklist_lock);
2704 EXPORT_SYMBOL(recalc_sigpending);
2705 EXPORT_SYMBOL_GPL(dequeue_signal);
2706 EXPORT_SYMBOL(flush_signals);
2707 EXPORT_SYMBOL(force_sig);
2708 EXPORT_SYMBOL(send_sig);
2709 EXPORT_SYMBOL(send_sig_info);
2710 EXPORT_SYMBOL(sigprocmask);
2713 * System call entry points.
2717 * sys_restart_syscall - restart a system call
2719 SYSCALL_DEFINE0(restart_syscall)
2721 struct restart_block *restart = ¤t->restart_block;
2722 return restart->fn(restart);
2725 long do_no_restart_syscall(struct restart_block *param)
2730 static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2732 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2733 sigset_t newblocked;
2734 /* A set of now blocked but previously unblocked signals. */
2735 sigandnsets(&newblocked, newset, ¤t->blocked);
2736 retarget_shared_pending(tsk, &newblocked);
2738 tsk->blocked = *newset;
2739 recalc_sigpending();
2743 * set_current_blocked - change current->blocked mask
2746 * It is wrong to change ->blocked directly, this helper should be used
2747 * to ensure the process can't miss a shared signal we are going to block.
2749 void set_current_blocked(sigset_t *newset)
2751 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2752 __set_current_blocked(newset);
2755 void __set_current_blocked(const sigset_t *newset)
2757 struct task_struct *tsk = current;
2760 * In case the signal mask hasn't changed, there is nothing we need
2761 * to do. The current->blocked shouldn't be modified by other task.
2763 if (sigequalsets(&tsk->blocked, newset))
2766 spin_lock_irq(&tsk->sighand->siglock);
2767 __set_task_blocked(tsk, newset);
2768 spin_unlock_irq(&tsk->sighand->siglock);
2772 * This is also useful for kernel threads that want to temporarily
2773 * (or permanently) block certain signals.
2775 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2776 * interface happily blocks "unblockable" signals like SIGKILL
2779 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2781 struct task_struct *tsk = current;
2784 /* Lockless, only current can change ->blocked, never from irq */
2786 *oldset = tsk->blocked;
2790 sigorsets(&newset, &tsk->blocked, set);
2793 sigandnsets(&newset, &tsk->blocked, set);
2802 __set_current_blocked(&newset);
2807 * sys_rt_sigprocmask - change the list of currently blocked signals
2808 * @how: whether to add, remove, or set signals
2809 * @nset: stores pending signals
2810 * @oset: previous value of signal mask if non-null
2811 * @sigsetsize: size of sigset_t type
2813 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2814 sigset_t __user *, oset, size_t, sigsetsize)
2816 sigset_t old_set, new_set;
2819 /* XXX: Don't preclude handling different sized sigset_t's. */
2820 if (sigsetsize != sizeof(sigset_t))
2823 old_set = current->blocked;
2826 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2828 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2830 error = sigprocmask(how, &new_set, NULL);
2836 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2843 #ifdef CONFIG_COMPAT
2844 COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2845 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
2847 sigset_t old_set = current->blocked;
2849 /* XXX: Don't preclude handling different sized sigset_t's. */
2850 if (sigsetsize != sizeof(sigset_t))
2856 if (get_compat_sigset(&new_set, nset))
2858 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2860 error = sigprocmask(how, &new_set, NULL);
2864 return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
2868 static void do_sigpending(sigset_t *set)
2870 spin_lock_irq(¤t->sighand->siglock);
2871 sigorsets(set, ¤t->pending.signal,
2872 ¤t->signal->shared_pending.signal);
2873 spin_unlock_irq(¤t->sighand->siglock);
2875 /* Outside the lock because only this thread touches it. */
2876 sigandsets(set, ¤t->blocked, set);
2880 * sys_rt_sigpending - examine a pending signal that has been raised
2882 * @uset: stores pending signals
2883 * @sigsetsize: size of sigset_t type or larger
2885 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
2889 if (sigsetsize > sizeof(*uset))
2892 do_sigpending(&set);
2894 if (copy_to_user(uset, &set, sigsetsize))
2900 #ifdef CONFIG_COMPAT
2901 COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2902 compat_size_t, sigsetsize)
2906 if (sigsetsize > sizeof(*uset))
2909 do_sigpending(&set);
2911 return put_compat_sigset(uset, &set, sigsetsize);
2915 enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
2917 enum siginfo_layout layout = SIL_KILL;
2918 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
2919 static const struct {
2920 unsigned char limit, layout;
2922 [SIGILL] = { NSIGILL, SIL_FAULT },
2923 [SIGFPE] = { NSIGFPE, SIL_FAULT },
2924 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
2925 [SIGBUS] = { NSIGBUS, SIL_FAULT },
2926 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
2927 #if defined(SIGEMT) && defined(NSIGEMT)
2928 [SIGEMT] = { NSIGEMT, SIL_FAULT },
2930 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
2931 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
2932 [SIGSYS] = { NSIGSYS, SIL_SYS },
2934 if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit)) {
2935 layout = filter[sig].layout;
2936 /* Handle the exceptions */
2937 if ((sig == SIGBUS) &&
2938 (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
2939 layout = SIL_FAULT_MCEERR;
2940 else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
2941 layout = SIL_FAULT_BNDERR;
2943 else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
2944 layout = SIL_FAULT_PKUERR;
2947 else if (si_code <= NSIGPOLL)
2950 if (si_code == SI_TIMER)
2952 else if (si_code == SI_SIGIO)
2954 else if (si_code < 0)
2960 int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
2962 if (copy_to_user(to, from , sizeof(struct siginfo)))
2967 #ifdef CONFIG_COMPAT
2968 int copy_siginfo_to_user32(struct compat_siginfo __user *to,
2969 const struct siginfo *from)
2970 #if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
2972 return __copy_siginfo_to_user32(to, from, in_x32_syscall());
2974 int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
2975 const struct siginfo *from, bool x32_ABI)
2978 struct compat_siginfo new;
2979 memset(&new, 0, sizeof(new));
2981 new.si_signo = from->si_signo;
2982 new.si_errno = from->si_errno;
2983 new.si_code = from->si_code;
2984 switch(siginfo_layout(from->si_signo, from->si_code)) {
2986 new.si_pid = from->si_pid;
2987 new.si_uid = from->si_uid;
2990 new.si_tid = from->si_tid;
2991 new.si_overrun = from->si_overrun;
2992 new.si_int = from->si_int;
2995 new.si_band = from->si_band;
2996 new.si_fd = from->si_fd;
2999 new.si_addr = ptr_to_compat(from->si_addr);
3000 #ifdef __ARCH_SI_TRAPNO
3001 new.si_trapno = from->si_trapno;
3004 case SIL_FAULT_MCEERR:
3005 new.si_addr = ptr_to_compat(from->si_addr);
3006 #ifdef __ARCH_SI_TRAPNO
3007 new.si_trapno = from->si_trapno;
3009 new.si_addr_lsb = from->si_addr_lsb;
3011 case SIL_FAULT_BNDERR:
3012 new.si_addr = ptr_to_compat(from->si_addr);
3013 #ifdef __ARCH_SI_TRAPNO
3014 new.si_trapno = from->si_trapno;
3016 new.si_lower = ptr_to_compat(from->si_lower);
3017 new.si_upper = ptr_to_compat(from->si_upper);
3019 case SIL_FAULT_PKUERR:
3020 new.si_addr = ptr_to_compat(from->si_addr);
3021 #ifdef __ARCH_SI_TRAPNO
3022 new.si_trapno = from->si_trapno;
3024 new.si_pkey = from->si_pkey;
3027 new.si_pid = from->si_pid;
3028 new.si_uid = from->si_uid;
3029 new.si_status = from->si_status;
3030 #ifdef CONFIG_X86_X32_ABI
3032 new._sifields._sigchld_x32._utime = from->si_utime;
3033 new._sifields._sigchld_x32._stime = from->si_stime;
3037 new.si_utime = from->si_utime;
3038 new.si_stime = from->si_stime;
3042 new.si_pid = from->si_pid;
3043 new.si_uid = from->si_uid;
3044 new.si_int = from->si_int;
3047 new.si_call_addr = ptr_to_compat(from->si_call_addr);
3048 new.si_syscall = from->si_syscall;
3049 new.si_arch = from->si_arch;
3053 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
3059 int copy_siginfo_from_user32(struct siginfo *to,
3060 const struct compat_siginfo __user *ufrom)
3062 struct compat_siginfo from;
3064 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3068 to->si_signo = from.si_signo;
3069 to->si_errno = from.si_errno;
3070 to->si_code = from.si_code;
3071 switch(siginfo_layout(from.si_signo, from.si_code)) {
3073 to->si_pid = from.si_pid;
3074 to->si_uid = from.si_uid;
3077 to->si_tid = from.si_tid;
3078 to->si_overrun = from.si_overrun;
3079 to->si_int = from.si_int;
3082 to->si_band = from.si_band;
3083 to->si_fd = from.si_fd;
3086 to->si_addr = compat_ptr(from.si_addr);
3087 #ifdef __ARCH_SI_TRAPNO
3088 to->si_trapno = from.si_trapno;
3091 case SIL_FAULT_MCEERR:
3092 to->si_addr = compat_ptr(from.si_addr);
3093 #ifdef __ARCH_SI_TRAPNO
3094 to->si_trapno = from.si_trapno;
3096 to->si_addr_lsb = from.si_addr_lsb;
3098 case SIL_FAULT_BNDERR:
3099 to->si_addr = compat_ptr(from.si_addr);
3100 #ifdef __ARCH_SI_TRAPNO
3101 to->si_trapno = from.si_trapno;
3103 to->si_lower = compat_ptr(from.si_lower);
3104 to->si_upper = compat_ptr(from.si_upper);
3106 case SIL_FAULT_PKUERR:
3107 to->si_addr = compat_ptr(from.si_addr);
3108 #ifdef __ARCH_SI_TRAPNO
3109 to->si_trapno = from.si_trapno;
3111 to->si_pkey = from.si_pkey;
3114 to->si_pid = from.si_pid;
3115 to->si_uid = from.si_uid;
3116 to->si_status = from.si_status;
3117 #ifdef CONFIG_X86_X32_ABI
3118 if (in_x32_syscall()) {
3119 to->si_utime = from._sifields._sigchld_x32._utime;
3120 to->si_stime = from._sifields._sigchld_x32._stime;
3124 to->si_utime = from.si_utime;
3125 to->si_stime = from.si_stime;
3129 to->si_pid = from.si_pid;
3130 to->si_uid = from.si_uid;
3131 to->si_int = from.si_int;
3134 to->si_call_addr = compat_ptr(from.si_call_addr);
3135 to->si_syscall = from.si_syscall;
3136 to->si_arch = from.si_arch;
3141 #endif /* CONFIG_COMPAT */
3144 * do_sigtimedwait - wait for queued signals specified in @which
3145 * @which: queued signals to wait for
3146 * @info: if non-null, the signal's siginfo is returned here
3147 * @ts: upper bound on process time suspension
3149 static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
3150 const struct timespec *ts)
3152 ktime_t *to = NULL, timeout = KTIME_MAX;
3153 struct task_struct *tsk = current;
3154 sigset_t mask = *which;
3158 if (!timespec_valid(ts))
3160 timeout = timespec_to_ktime(*ts);
3165 * Invert the set of allowed signals to get those we want to block.
3167 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
3170 spin_lock_irq(&tsk->sighand->siglock);
3171 sig = dequeue_signal(tsk, &mask, info);
3172 if (!sig && timeout) {
3174 * None ready, temporarily unblock those we're interested
3175 * while we are sleeping in so that we'll be awakened when
3176 * they arrive. Unblocking is always fine, we can avoid
3177 * set_current_blocked().
3179 tsk->real_blocked = tsk->blocked;
3180 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
3181 recalc_sigpending();
3182 spin_unlock_irq(&tsk->sighand->siglock);
3184 __set_current_state(TASK_INTERRUPTIBLE);
3185 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
3187 spin_lock_irq(&tsk->sighand->siglock);
3188 __set_task_blocked(tsk, &tsk->real_blocked);
3189 sigemptyset(&tsk->real_blocked);
3190 sig = dequeue_signal(tsk, &mask, info);
3192 spin_unlock_irq(&tsk->sighand->siglock);
3196 return ret ? -EINTR : -EAGAIN;
3200 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
3202 * @uthese: queued signals to wait for
3203 * @uinfo: if non-null, the signal's siginfo is returned here
3204 * @uts: upper bound on process time suspension
3205 * @sigsetsize: size of sigset_t type
3207 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
3208 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
3216 /* XXX: Don't preclude handling different sized sigset_t's. */
3217 if (sigsetsize != sizeof(sigset_t))
3220 if (copy_from_user(&these, uthese, sizeof(these)))
3224 if (copy_from_user(&ts, uts, sizeof(ts)))
3228 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3230 if (ret > 0 && uinfo) {
3231 if (copy_siginfo_to_user(uinfo, &info))
3238 #ifdef CONFIG_COMPAT
3239 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
3240 struct compat_siginfo __user *, uinfo,
3241 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
3248 if (sigsetsize != sizeof(sigset_t))
3251 if (get_compat_sigset(&s, uthese))
3255 if (compat_get_timespec(&t, uts))
3259 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3261 if (ret > 0 && uinfo) {
3262 if (copy_siginfo_to_user32(uinfo, &info))
3271 * sys_kill - send a signal to a process
3272 * @pid: the PID of the process
3273 * @sig: signal to be sent
3275 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
3277 struct siginfo info;
3279 clear_siginfo(&info);
3280 info.si_signo = sig;
3282 info.si_code = SI_USER;
3283 info.si_pid = task_tgid_vnr(current);
3284 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3286 return kill_something_info(sig, &info, pid);
3290 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
3292 struct task_struct *p;
3296 p = find_task_by_vpid(pid);
3297 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
3298 error = check_kill_permission(sig, info, p);
3300 * The null signal is a permissions and process existence
3301 * probe. No signal is actually delivered.
3303 if (!error && sig) {
3304 error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
3306 * If lock_task_sighand() failed we pretend the task
3307 * dies after receiving the signal. The window is tiny,
3308 * and the signal is private anyway.
3310 if (unlikely(error == -ESRCH))
3319 static int do_tkill(pid_t tgid, pid_t pid, int sig)
3321 struct siginfo info;
3323 clear_siginfo(&info);
3324 info.si_signo = sig;
3326 info.si_code = SI_TKILL;
3327 info.si_pid = task_tgid_vnr(current);
3328 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3330 return do_send_specific(tgid, pid, sig, &info);
3334 * sys_tgkill - send signal to one specific thread
3335 * @tgid: the thread group ID of the thread
3336 * @pid: the PID of the thread
3337 * @sig: signal to be sent
3339 * This syscall also checks the @tgid and returns -ESRCH even if the PID
3340 * exists but it's not belonging to the target process anymore. This
3341 * method solves the problem of threads exiting and PIDs getting reused.
3343 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3345 /* This is only valid for single tasks */
3346 if (pid <= 0 || tgid <= 0)
3349 return do_tkill(tgid, pid, sig);
3353 * sys_tkill - send signal to one specific task
3354 * @pid: the PID of the task
3355 * @sig: signal to be sent
3357 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3359 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
3361 /* This is only valid for single tasks */
3365 return do_tkill(0, pid, sig);
3368 static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
3370 /* Not even root can pretend to send signals from the kernel.
3371 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3373 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3374 (task_pid_vnr(current) != pid))
3377 info->si_signo = sig;
3379 /* POSIX.1b doesn't mention process groups. */
3380 return kill_proc_info(sig, info, pid);
3384 * sys_rt_sigqueueinfo - send signal information to a signal
3385 * @pid: the PID of the thread
3386 * @sig: signal to be sent
3387 * @uinfo: signal info to be sent
3389 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3390 siginfo_t __user *, uinfo)
3393 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3395 return do_rt_sigqueueinfo(pid, sig, &info);
3398 #ifdef CONFIG_COMPAT
3399 COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3402 struct compat_siginfo __user *, uinfo)
3405 int ret = copy_siginfo_from_user32(&info, uinfo);
3408 return do_rt_sigqueueinfo(pid, sig, &info);
3412 static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
3414 /* This is only valid for single tasks */
3415 if (pid <= 0 || tgid <= 0)
3418 /* Not even root can pretend to send signals from the kernel.
3419 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3421 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3422 (task_pid_vnr(current) != pid))
3425 info->si_signo = sig;
3427 return do_send_specific(tgid, pid, sig, info);
3430 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3431 siginfo_t __user *, uinfo)
3435 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3438 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3441 #ifdef CONFIG_COMPAT
3442 COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3446 struct compat_siginfo __user *, uinfo)
3450 if (copy_siginfo_from_user32(&info, uinfo))
3452 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3457 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3459 void kernel_sigaction(int sig, __sighandler_t action)
3461 spin_lock_irq(¤t->sighand->siglock);
3462 current->sighand->action[sig - 1].sa.sa_handler = action;
3463 if (action == SIG_IGN) {
3467 sigaddset(&mask, sig);
3469 flush_sigqueue_mask(&mask, ¤t->signal->shared_pending);
3470 flush_sigqueue_mask(&mask, ¤t->pending);
3471 recalc_sigpending();
3473 spin_unlock_irq(¤t->sighand->siglock);
3475 EXPORT_SYMBOL(kernel_sigaction);
3477 void __weak sigaction_compat_abi(struct k_sigaction *act,
3478 struct k_sigaction *oact)
3482 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
3484 struct task_struct *p = current, *t;
3485 struct k_sigaction *k;
3488 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
3491 k = &p->sighand->action[sig-1];
3493 spin_lock_irq(&p->sighand->siglock);
3497 sigaction_compat_abi(act, oact);
3500 sigdelsetmask(&act->sa.sa_mask,
3501 sigmask(SIGKILL) | sigmask(SIGSTOP));
3505 * "Setting a signal action to SIG_IGN for a signal that is
3506 * pending shall cause the pending signal to be discarded,
3507 * whether or not it is blocked."
3509 * "Setting a signal action to SIG_DFL for a signal that is
3510 * pending and whose default action is to ignore the signal
3511 * (for example, SIGCHLD), shall cause the pending signal to
3512 * be discarded, whether or not it is blocked"
3514 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3516 sigaddset(&mask, sig);
3517 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3518 for_each_thread(p, t)
3519 flush_sigqueue_mask(&mask, &t->pending);
3523 spin_unlock_irq(&p->sighand->siglock);
3528 do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
3531 struct task_struct *t = current;
3534 memset(oss, 0, sizeof(stack_t));
3535 oss->ss_sp = (void __user *) t->sas_ss_sp;
3536 oss->ss_size = t->sas_ss_size;
3537 oss->ss_flags = sas_ss_flags(sp) |
3538 (current->sas_ss_flags & SS_FLAG_BITS);
3542 void __user *ss_sp = ss->ss_sp;
3543 size_t ss_size = ss->ss_size;
3544 unsigned ss_flags = ss->ss_flags;
3547 if (unlikely(on_sig_stack(sp)))
3550 ss_mode = ss_flags & ~SS_FLAG_BITS;
3551 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3555 if (ss_mode == SS_DISABLE) {
3559 if (unlikely(ss_size < min_ss_size))
3563 t->sas_ss_sp = (unsigned long) ss_sp;
3564 t->sas_ss_size = ss_size;
3565 t->sas_ss_flags = ss_flags;
3570 SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3574 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
3576 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
3577 current_user_stack_pointer(),
3579 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
3584 int restore_altstack(const stack_t __user *uss)
3587 if (copy_from_user(&new, uss, sizeof(stack_t)))
3589 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
3591 /* squash all but EFAULT for now */
3595 int __save_altstack(stack_t __user *uss, unsigned long sp)
3597 struct task_struct *t = current;
3598 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3599 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3600 __put_user(t->sas_ss_size, &uss->ss_size);
3603 if (t->sas_ss_flags & SS_AUTODISARM)
3608 #ifdef CONFIG_COMPAT
3609 static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
3610 compat_stack_t __user *uoss_ptr)
3616 compat_stack_t uss32;
3617 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3619 uss.ss_sp = compat_ptr(uss32.ss_sp);
3620 uss.ss_flags = uss32.ss_flags;
3621 uss.ss_size = uss32.ss_size;
3623 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
3624 compat_user_stack_pointer(),
3625 COMPAT_MINSIGSTKSZ);
3626 if (ret >= 0 && uoss_ptr) {
3628 memset(&old, 0, sizeof(old));
3629 old.ss_sp = ptr_to_compat(uoss.ss_sp);
3630 old.ss_flags = uoss.ss_flags;
3631 old.ss_size = uoss.ss_size;
3632 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
3638 COMPAT_SYSCALL_DEFINE2(sigaltstack,
3639 const compat_stack_t __user *, uss_ptr,
3640 compat_stack_t __user *, uoss_ptr)
3642 return do_compat_sigaltstack(uss_ptr, uoss_ptr);
3645 int compat_restore_altstack(const compat_stack_t __user *uss)
3647 int err = do_compat_sigaltstack(uss, NULL);
3648 /* squash all but -EFAULT for now */
3649 return err == -EFAULT ? err : 0;
3652 int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3655 struct task_struct *t = current;
3656 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3658 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3659 __put_user(t->sas_ss_size, &uss->ss_size);
3662 if (t->sas_ss_flags & SS_AUTODISARM)
3668 #ifdef __ARCH_WANT_SYS_SIGPENDING
3671 * sys_sigpending - examine pending signals
3672 * @uset: where mask of pending signal is returned
3674 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
3678 if (sizeof(old_sigset_t) > sizeof(*uset))
3681 do_sigpending(&set);
3683 if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
3689 #ifdef CONFIG_COMPAT
3690 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
3694 do_sigpending(&set);
3696 return put_user(set.sig[0], set32);
3702 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
3704 * sys_sigprocmask - examine and change blocked signals
3705 * @how: whether to add, remove, or set signals
3706 * @nset: signals to add or remove (if non-null)
3707 * @oset: previous value of signal mask if non-null
3709 * Some platforms have their own version with special arguments;
3710 * others support only sys_rt_sigprocmask.
3713 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
3714 old_sigset_t __user *, oset)
3716 old_sigset_t old_set, new_set;
3717 sigset_t new_blocked;
3719 old_set = current->blocked.sig[0];
3722 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3725 new_blocked = current->blocked;
3729 sigaddsetmask(&new_blocked, new_set);
3732 sigdelsetmask(&new_blocked, new_set);
3735 new_blocked.sig[0] = new_set;
3741 set_current_blocked(&new_blocked);
3745 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3751 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3753 #ifndef CONFIG_ODD_RT_SIGACTION
3755 * sys_rt_sigaction - alter an action taken by a process
3756 * @sig: signal to be sent
3757 * @act: new sigaction
3758 * @oact: used to save the previous sigaction
3759 * @sigsetsize: size of sigset_t type
3761 SYSCALL_DEFINE4(rt_sigaction, int, sig,
3762 const struct sigaction __user *, act,
3763 struct sigaction __user *, oact,
3766 struct k_sigaction new_sa, old_sa;
3769 /* XXX: Don't preclude handling different sized sigset_t's. */
3770 if (sigsetsize != sizeof(sigset_t))
3773 if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3776 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3780 if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3785 #ifdef CONFIG_COMPAT
3786 COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3787 const struct compat_sigaction __user *, act,
3788 struct compat_sigaction __user *, oact,
3789 compat_size_t, sigsetsize)
3791 struct k_sigaction new_ka, old_ka;
3792 #ifdef __ARCH_HAS_SA_RESTORER
3793 compat_uptr_t restorer;
3797 /* XXX: Don't preclude handling different sized sigset_t's. */
3798 if (sigsetsize != sizeof(compat_sigset_t))
3802 compat_uptr_t handler;
3803 ret = get_user(handler, &act->sa_handler);
3804 new_ka.sa.sa_handler = compat_ptr(handler);
3805 #ifdef __ARCH_HAS_SA_RESTORER
3806 ret |= get_user(restorer, &act->sa_restorer);
3807 new_ka.sa.sa_restorer = compat_ptr(restorer);
3809 ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
3810 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
3815 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3817 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
3819 ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
3820 sizeof(oact->sa_mask));
3821 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
3822 #ifdef __ARCH_HAS_SA_RESTORER
3823 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3824 &oact->sa_restorer);
3830 #endif /* !CONFIG_ODD_RT_SIGACTION */
3832 #ifdef CONFIG_OLD_SIGACTION
3833 SYSCALL_DEFINE3(sigaction, int, sig,
3834 const struct old_sigaction __user *, act,
3835 struct old_sigaction __user *, oact)
3837 struct k_sigaction new_ka, old_ka;
3842 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3843 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3844 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3845 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3846 __get_user(mask, &act->sa_mask))
3848 #ifdef __ARCH_HAS_KA_RESTORER
3849 new_ka.ka_restorer = NULL;
3851 siginitset(&new_ka.sa.sa_mask, mask);
3854 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3857 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3858 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3859 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3860 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3861 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3868 #ifdef CONFIG_COMPAT_OLD_SIGACTION
3869 COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3870 const struct compat_old_sigaction __user *, act,
3871 struct compat_old_sigaction __user *, oact)
3873 struct k_sigaction new_ka, old_ka;
3875 compat_old_sigset_t mask;
3876 compat_uptr_t handler, restorer;
3879 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3880 __get_user(handler, &act->sa_handler) ||
3881 __get_user(restorer, &act->sa_restorer) ||
3882 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3883 __get_user(mask, &act->sa_mask))
3886 #ifdef __ARCH_HAS_KA_RESTORER
3887 new_ka.ka_restorer = NULL;
3889 new_ka.sa.sa_handler = compat_ptr(handler);
3890 new_ka.sa.sa_restorer = compat_ptr(restorer);
3891 siginitset(&new_ka.sa.sa_mask, mask);
3894 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3897 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3898 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3899 &oact->sa_handler) ||
3900 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3901 &oact->sa_restorer) ||
3902 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3903 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3910 #ifdef CONFIG_SGETMASK_SYSCALL
3913 * For backwards compatibility. Functionality superseded by sigprocmask.
3915 SYSCALL_DEFINE0(sgetmask)
3918 return current->blocked.sig[0];
3921 SYSCALL_DEFINE1(ssetmask, int, newmask)
3923 int old = current->blocked.sig[0];
3926 siginitset(&newset, newmask);
3927 set_current_blocked(&newset);
3931 #endif /* CONFIG_SGETMASK_SYSCALL */
3933 #ifdef __ARCH_WANT_SYS_SIGNAL
3935 * For backwards compatibility. Functionality superseded by sigaction.
3937 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
3939 struct k_sigaction new_sa, old_sa;
3942 new_sa.sa.sa_handler = handler;
3943 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
3944 sigemptyset(&new_sa.sa.sa_mask);
3946 ret = do_sigaction(sig, &new_sa, &old_sa);
3948 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3950 #endif /* __ARCH_WANT_SYS_SIGNAL */
3952 #ifdef __ARCH_WANT_SYS_PAUSE
3954 SYSCALL_DEFINE0(pause)
3956 while (!signal_pending(current)) {
3957 __set_current_state(TASK_INTERRUPTIBLE);
3960 return -ERESTARTNOHAND;
3965 static int sigsuspend(sigset_t *set)
3967 current->saved_sigmask = current->blocked;
3968 set_current_blocked(set);
3970 while (!signal_pending(current)) {
3971 __set_current_state(TASK_INTERRUPTIBLE);
3974 set_restore_sigmask();
3975 return -ERESTARTNOHAND;
3979 * sys_rt_sigsuspend - replace the signal mask for a value with the
3980 * @unewset value until a signal is received
3981 * @unewset: new signal mask value
3982 * @sigsetsize: size of sigset_t type
3984 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3988 /* XXX: Don't preclude handling different sized sigset_t's. */
3989 if (sigsetsize != sizeof(sigset_t))
3992 if (copy_from_user(&newset, unewset, sizeof(newset)))
3994 return sigsuspend(&newset);
3997 #ifdef CONFIG_COMPAT
3998 COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
4002 /* XXX: Don't preclude handling different sized sigset_t's. */
4003 if (sigsetsize != sizeof(sigset_t))
4006 if (get_compat_sigset(&newset, unewset))
4008 return sigsuspend(&newset);
4012 #ifdef CONFIG_OLD_SIGSUSPEND
4013 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
4016 siginitset(&blocked, mask);
4017 return sigsuspend(&blocked);
4020 #ifdef CONFIG_OLD_SIGSUSPEND3
4021 SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
4024 siginitset(&blocked, mask);
4025 return sigsuspend(&blocked);
4029 __weak const char *arch_vma_name(struct vm_area_struct *vma)
4034 void __init signals_init(void)
4036 /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
4037 BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
4038 != offsetof(struct siginfo, _sifields._pad));
4039 BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
4041 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
4044 #ifdef CONFIG_KGDB_KDB
4045 #include <linux/kdb.h>
4047 * kdb_send_sig - Allows kdb to send signals without exposing
4048 * signal internals. This function checks if the required locks are
4049 * available before calling the main signal code, to avoid kdb
4052 void kdb_send_sig(struct task_struct *t, int sig)
4054 static struct task_struct *kdb_prev_t;
4056 if (!spin_trylock(&t->sighand->siglock)) {
4057 kdb_printf("Can't do kill command now.\n"
4058 "The sigmask lock is held somewhere else in "
4059 "kernel, try again later\n");
4062 new_t = kdb_prev_t != t;
4064 if (t->state != TASK_RUNNING && new_t) {
4065 spin_unlock(&t->sighand->siglock);
4066 kdb_printf("Process is not RUNNING, sending a signal from "
4067 "kdb risks deadlock\n"
4068 "on the run queue locks. "
4069 "The signal has _not_ been sent.\n"
4070 "Reissue the kill command if you want to risk "
4074 ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
4075 spin_unlock(&t->sighand->siglock);
4077 kdb_printf("Fail to deliver Signal %d to process %d.\n",
4080 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
4082 #endif /* CONFIG_KGDB_KDB */