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>
44 #define CREATE_TRACE_POINTS
45 #include <trace/events/signal.h>
47 #include <asm/param.h>
48 #include <linux/uaccess.h>
49 #include <asm/unistd.h>
50 #include <asm/siginfo.h>
51 #include <asm/cacheflush.h>
52 #include "audit.h" /* audit_signal_info() */
55 * SLAB caches for signal bits.
58 static struct kmem_cache *sigqueue_cachep;
60 int print_fatal_signals __read_mostly;
62 static void __user *sig_handler(struct task_struct *t, int sig)
64 return t->sighand->action[sig - 1].sa.sa_handler;
67 static int sig_handler_ignored(void __user *handler, int sig)
69 /* Is it explicitly or implicitly ignored? */
70 return handler == SIG_IGN ||
71 (handler == SIG_DFL && sig_kernel_ignore(sig));
74 static int sig_task_ignored(struct task_struct *t, int sig, bool force)
78 handler = sig_handler(t, sig);
80 /* SIGKILL and SIGSTOP may not be sent to the global init */
81 if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
84 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
85 handler == SIG_DFL && !(force && sig_kernel_only(sig)))
88 /* Only allow kernel generated signals to this kthread */
89 if (unlikely((t->flags & PF_KTHREAD) &&
90 (handler == SIG_KTHREAD_KERNEL) && !force))
93 return sig_handler_ignored(handler, sig);
96 static int sig_ignored(struct task_struct *t, int sig, bool force)
99 * Blocked signals are never ignored, since the
100 * signal handler may change by the time it is
103 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
107 * Tracers may want to know about even ignored signal unless it
108 * is SIGKILL which can't be reported anyway but can be ignored
109 * by SIGNAL_UNKILLABLE task.
111 if (t->ptrace && sig != SIGKILL)
114 return sig_task_ignored(t, sig, force);
118 * Re-calculate pending state from the set of locally pending
119 * signals, globally pending signals, and blocked signals.
121 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
126 switch (_NSIG_WORDS) {
128 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
129 ready |= signal->sig[i] &~ blocked->sig[i];
132 case 4: ready = signal->sig[3] &~ blocked->sig[3];
133 ready |= signal->sig[2] &~ blocked->sig[2];
134 ready |= signal->sig[1] &~ blocked->sig[1];
135 ready |= signal->sig[0] &~ blocked->sig[0];
138 case 2: ready = signal->sig[1] &~ blocked->sig[1];
139 ready |= signal->sig[0] &~ blocked->sig[0];
142 case 1: ready = signal->sig[0] &~ blocked->sig[0];
147 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
149 static int recalc_sigpending_tsk(struct task_struct *t)
151 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
152 PENDING(&t->pending, &t->blocked) ||
153 PENDING(&t->signal->shared_pending, &t->blocked)) {
154 set_tsk_thread_flag(t, TIF_SIGPENDING);
158 * We must never clear the flag in another thread, or in current
159 * when it's possible the current syscall is returning -ERESTART*.
160 * So we don't clear it here, and only callers who know they should do.
166 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
167 * This is superfluous when called on current, the wakeup is a harmless no-op.
169 void recalc_sigpending_and_wake(struct task_struct *t)
171 if (recalc_sigpending_tsk(t))
172 signal_wake_up(t, 0);
175 void recalc_sigpending(void)
177 if (!recalc_sigpending_tsk(current) && !freezing(current))
178 clear_thread_flag(TIF_SIGPENDING);
182 /* Given the mask, find the first available signal that should be serviced. */
184 #define SYNCHRONOUS_MASK \
185 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
186 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
188 int next_signal(struct sigpending *pending, sigset_t *mask)
190 unsigned long i, *s, *m, x;
193 s = pending->signal.sig;
197 * Handle the first word specially: it contains the
198 * synchronous signals that need to be dequeued first.
202 if (x & SYNCHRONOUS_MASK)
203 x &= SYNCHRONOUS_MASK;
208 switch (_NSIG_WORDS) {
210 for (i = 1; i < _NSIG_WORDS; ++i) {
214 sig = ffz(~x) + i*_NSIG_BPW + 1;
223 sig = ffz(~x) + _NSIG_BPW + 1;
234 static inline void print_dropped_signal(int sig)
236 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
238 if (!print_fatal_signals)
241 if (!__ratelimit(&ratelimit_state))
244 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
245 current->comm, current->pid, sig);
249 * task_set_jobctl_pending - set jobctl pending bits
251 * @mask: pending bits to set
253 * Clear @mask from @task->jobctl. @mask must be subset of
254 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
255 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
256 * cleared. If @task is already being killed or exiting, this function
260 * Must be called with @task->sighand->siglock held.
263 * %true if @mask is set, %false if made noop because @task was dying.
265 bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
267 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
268 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
269 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
271 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
274 if (mask & JOBCTL_STOP_SIGMASK)
275 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
277 task->jobctl |= mask;
282 * task_clear_jobctl_trapping - clear jobctl trapping bit
285 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
286 * Clear it and wake up the ptracer. Note that we don't need any further
287 * locking. @task->siglock guarantees that @task->parent points to the
291 * Must be called with @task->sighand->siglock held.
293 void task_clear_jobctl_trapping(struct task_struct *task)
295 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
296 task->jobctl &= ~JOBCTL_TRAPPING;
297 smp_mb(); /* advised by wake_up_bit() */
298 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
303 * task_clear_jobctl_pending - clear jobctl pending bits
305 * @mask: pending bits to clear
307 * Clear @mask from @task->jobctl. @mask must be subset of
308 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
309 * STOP bits are cleared together.
311 * If clearing of @mask leaves no stop or trap pending, this function calls
312 * task_clear_jobctl_trapping().
315 * Must be called with @task->sighand->siglock held.
317 void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
319 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
321 if (mask & JOBCTL_STOP_PENDING)
322 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
324 task->jobctl &= ~mask;
326 if (!(task->jobctl & JOBCTL_PENDING_MASK))
327 task_clear_jobctl_trapping(task);
331 * task_participate_group_stop - participate in a group stop
332 * @task: task participating in a group stop
334 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
335 * Group stop states are cleared and the group stop count is consumed if
336 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
337 * stop, the appropriate %SIGNAL_* flags are set.
340 * Must be called with @task->sighand->siglock held.
343 * %true if group stop completion should be notified to the parent, %false
346 static bool task_participate_group_stop(struct task_struct *task)
348 struct signal_struct *sig = task->signal;
349 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
351 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
353 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
358 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
359 sig->group_stop_count--;
362 * Tell the caller to notify completion iff we are entering into a
363 * fresh group stop. Read comment in do_signal_stop() for details.
365 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
366 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
373 * allocate a new signal queue record
374 * - this may be called without locks if and only if t == current, otherwise an
375 * appropriate lock must be held to stop the target task from exiting
377 static struct sigqueue *
378 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
380 struct sigqueue *q = NULL;
381 struct user_struct *user;
385 * Protect access to @t credentials. This can go away when all
386 * callers hold rcu read lock.
388 * NOTE! A pending signal will hold on to the user refcount,
389 * and we get/put the refcount only when the sigpending count
390 * changes from/to zero.
393 user = __task_cred(t)->user;
394 sigpending = atomic_inc_return(&user->sigpending);
399 if (override_rlimit || likely(sigpending <= task_rlimit(t, RLIMIT_SIGPENDING))) {
400 q = kmem_cache_alloc(sigqueue_cachep, flags);
402 print_dropped_signal(sig);
405 if (unlikely(q == NULL)) {
406 if (atomic_dec_and_test(&user->sigpending))
409 INIT_LIST_HEAD(&q->list);
417 static void __sigqueue_free(struct sigqueue *q)
419 if (q->flags & SIGQUEUE_PREALLOC)
421 if (atomic_dec_and_test(&q->user->sigpending))
423 kmem_cache_free(sigqueue_cachep, q);
426 void flush_sigqueue(struct sigpending *queue)
430 sigemptyset(&queue->signal);
431 while (!list_empty(&queue->list)) {
432 q = list_entry(queue->list.next, struct sigqueue , list);
433 list_del_init(&q->list);
439 * Flush all pending signals for this kthread.
441 void flush_signals(struct task_struct *t)
445 spin_lock_irqsave(&t->sighand->siglock, flags);
446 clear_tsk_thread_flag(t, TIF_SIGPENDING);
447 flush_sigqueue(&t->pending);
448 flush_sigqueue(&t->signal->shared_pending);
449 spin_unlock_irqrestore(&t->sighand->siglock, flags);
452 #ifdef CONFIG_POSIX_TIMERS
453 static void __flush_itimer_signals(struct sigpending *pending)
455 sigset_t signal, retain;
456 struct sigqueue *q, *n;
458 signal = pending->signal;
459 sigemptyset(&retain);
461 list_for_each_entry_safe(q, n, &pending->list, list) {
462 int sig = q->info.si_signo;
464 if (likely(q->info.si_code != SI_TIMER)) {
465 sigaddset(&retain, sig);
467 sigdelset(&signal, sig);
468 list_del_init(&q->list);
473 sigorsets(&pending->signal, &signal, &retain);
476 void flush_itimer_signals(void)
478 struct task_struct *tsk = current;
481 spin_lock_irqsave(&tsk->sighand->siglock, flags);
482 __flush_itimer_signals(&tsk->pending);
483 __flush_itimer_signals(&tsk->signal->shared_pending);
484 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
488 void ignore_signals(struct task_struct *t)
492 for (i = 0; i < _NSIG; ++i)
493 t->sighand->action[i].sa.sa_handler = SIG_IGN;
499 * Flush all handlers for a task.
503 flush_signal_handlers(struct task_struct *t, int force_default)
506 struct k_sigaction *ka = &t->sighand->action[0];
507 for (i = _NSIG ; i != 0 ; i--) {
508 if (force_default || ka->sa.sa_handler != SIG_IGN)
509 ka->sa.sa_handler = SIG_DFL;
511 #ifdef __ARCH_HAS_SA_RESTORER
512 ka->sa.sa_restorer = NULL;
514 sigemptyset(&ka->sa.sa_mask);
519 int unhandled_signal(struct task_struct *tsk, int sig)
521 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
522 if (is_global_init(tsk))
524 if (handler != SIG_IGN && handler != SIG_DFL)
526 /* if ptraced, let the tracer determine */
530 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
533 struct sigqueue *q, *first = NULL;
536 * Collect the siginfo appropriate to this signal. Check if
537 * there is another siginfo for the same signal.
539 list_for_each_entry(q, &list->list, list) {
540 if (q->info.si_signo == sig) {
547 sigdelset(&list->signal, sig);
551 list_del_init(&first->list);
552 copy_siginfo(info, &first->info);
555 (first->flags & SIGQUEUE_PREALLOC) &&
556 (info->si_code == SI_TIMER) &&
557 (info->si_sys_private);
559 __sigqueue_free(first);
562 * Ok, it wasn't in the queue. This must be
563 * a fast-pathed signal or we must have been
564 * out of queue space. So zero out the info.
566 info->si_signo = sig;
568 info->si_code = SI_USER;
574 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
575 siginfo_t *info, bool *resched_timer)
577 int sig = next_signal(pending, mask);
580 collect_signal(sig, pending, info, resched_timer);
585 * Dequeue a signal and return the element to the caller, which is
586 * expected to free it.
588 * All callers have to hold the siglock.
590 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
592 bool resched_timer = false;
595 /* We only dequeue private signals from ourselves, we don't let
596 * signalfd steal them
598 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
600 signr = __dequeue_signal(&tsk->signal->shared_pending,
601 mask, info, &resched_timer);
602 #ifdef CONFIG_POSIX_TIMERS
606 * itimers are process shared and we restart periodic
607 * itimers in the signal delivery path to prevent DoS
608 * attacks in the high resolution timer case. This is
609 * compliant with the old way of self-restarting
610 * itimers, as the SIGALRM is a legacy signal and only
611 * queued once. Changing the restart behaviour to
612 * restart the timer in the signal dequeue path is
613 * reducing the timer noise on heavy loaded !highres
616 if (unlikely(signr == SIGALRM)) {
617 struct hrtimer *tmr = &tsk->signal->real_timer;
619 if (!hrtimer_is_queued(tmr) &&
620 tsk->signal->it_real_incr != 0) {
621 hrtimer_forward(tmr, tmr->base->get_time(),
622 tsk->signal->it_real_incr);
623 hrtimer_restart(tmr);
633 if (unlikely(sig_kernel_stop(signr))) {
635 * Set a marker that we have dequeued a stop signal. Our
636 * caller might release the siglock and then the pending
637 * stop signal it is about to process is no longer in the
638 * pending bitmasks, but must still be cleared by a SIGCONT
639 * (and overruled by a SIGKILL). So those cases clear this
640 * shared flag after we've set it. Note that this flag may
641 * remain set after the signal we return is ignored or
642 * handled. That doesn't matter because its only purpose
643 * is to alert stop-signal processing code when another
644 * processor has come along and cleared the flag.
646 current->jobctl |= JOBCTL_STOP_DEQUEUED;
648 #ifdef CONFIG_POSIX_TIMERS
651 * Release the siglock to ensure proper locking order
652 * of timer locks outside of siglocks. Note, we leave
653 * irqs disabled here, since the posix-timers code is
654 * about to disable them again anyway.
656 spin_unlock(&tsk->sighand->siglock);
657 posixtimer_rearm(info);
658 spin_lock(&tsk->sighand->siglock);
665 * Tell a process that it has a new active signal..
667 * NOTE! we rely on the previous spin_lock to
668 * lock interrupts for us! We can only be called with
669 * "siglock" held, and the local interrupt must
670 * have been disabled when that got acquired!
672 * No need to set need_resched since signal event passing
673 * goes through ->blocked
675 void signal_wake_up_state(struct task_struct *t, unsigned int state)
677 set_tsk_thread_flag(t, TIF_SIGPENDING);
679 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
680 * case. We don't check t->state here because there is a race with it
681 * executing another processor and just now entering stopped state.
682 * By using wake_up_state, we ensure the process will wake up and
683 * handle its death signal.
685 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
689 static int dequeue_synchronous_signal(siginfo_t *info)
691 struct task_struct *tsk = current;
692 struct sigpending *pending = &tsk->pending;
693 struct sigqueue *q, *sync = NULL;
696 * Might a synchronous signal be in the queue?
698 if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
702 * Return the first synchronous signal in the queue.
704 list_for_each_entry(q, &pending->list, list) {
705 /* Synchronous signals have a postive si_code */
706 if ((q->info.si_code > SI_USER) &&
707 (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
715 * Check if there is another siginfo for the same signal.
717 list_for_each_entry_continue(q, &pending->list, list) {
718 if (q->info.si_signo == sync->info.si_signo)
722 sigdelset(&pending->signal, sync->info.si_signo);
725 list_del_init(&sync->list);
726 copy_siginfo(info, &sync->info);
727 __sigqueue_free(sync);
728 return info->si_signo;
732 * Remove signals in mask from the pending set and queue.
733 * Returns 1 if any signals were found.
735 * All callers must be holding the siglock.
737 static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
739 struct sigqueue *q, *n;
742 sigandsets(&m, mask, &s->signal);
743 if (sigisemptyset(&m))
746 sigandnsets(&s->signal, &s->signal, mask);
747 list_for_each_entry_safe(q, n, &s->list, list) {
748 if (sigismember(mask, q->info.si_signo)) {
749 list_del_init(&q->list);
756 static inline int is_si_special(const struct siginfo *info)
758 return info <= SEND_SIG_FORCED;
761 static inline bool si_fromuser(const struct siginfo *info)
763 return info == SEND_SIG_NOINFO ||
764 (!is_si_special(info) && SI_FROMUSER(info));
768 * called with RCU read lock from check_kill_permission()
770 static int kill_ok_by_cred(struct task_struct *t)
772 const struct cred *cred = current_cred();
773 const struct cred *tcred = __task_cred(t);
775 if (uid_eq(cred->euid, tcred->suid) ||
776 uid_eq(cred->euid, tcred->uid) ||
777 uid_eq(cred->uid, tcred->suid) ||
778 uid_eq(cred->uid, tcred->uid))
781 if (ns_capable(tcred->user_ns, CAP_KILL))
788 * Bad permissions for sending the signal
789 * - the caller must hold the RCU read lock
791 static int check_kill_permission(int sig, struct siginfo *info,
792 struct task_struct *t)
797 if (!valid_signal(sig))
800 if (!si_fromuser(info))
803 error = audit_signal_info(sig, t); /* Let audit system see the signal */
807 if (!same_thread_group(current, t) &&
808 !kill_ok_by_cred(t)) {
811 sid = task_session(t);
813 * We don't return the error if sid == NULL. The
814 * task was unhashed, the caller must notice this.
816 if (!sid || sid == task_session(current))
823 return security_task_kill(t, info, sig, 0);
827 * ptrace_trap_notify - schedule trap to notify ptracer
828 * @t: tracee wanting to notify tracer
830 * This function schedules sticky ptrace trap which is cleared on the next
831 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
834 * If @t is running, STOP trap will be taken. If trapped for STOP and
835 * ptracer is listening for events, tracee is woken up so that it can
836 * re-trap for the new event. If trapped otherwise, STOP trap will be
837 * eventually taken without returning to userland after the existing traps
838 * are finished by PTRACE_CONT.
841 * Must be called with @task->sighand->siglock held.
843 static void ptrace_trap_notify(struct task_struct *t)
845 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
846 assert_spin_locked(&t->sighand->siglock);
848 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
849 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
853 * Handle magic process-wide effects of stop/continue signals. Unlike
854 * the signal actions, these happen immediately at signal-generation
855 * time regardless of blocking, ignoring, or handling. This does the
856 * actual continuing for SIGCONT, but not the actual stopping for stop
857 * signals. The process stop is done as a signal action for SIG_DFL.
859 * Returns true if the signal should be actually delivered, otherwise
860 * it should be dropped.
862 static bool prepare_signal(int sig, struct task_struct *p, bool force)
864 struct signal_struct *signal = p->signal;
865 struct task_struct *t;
868 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
869 if (!(signal->flags & SIGNAL_GROUP_EXIT))
870 return sig == SIGKILL;
872 * The process is in the middle of dying, nothing to do.
874 } else if (sig_kernel_stop(sig)) {
876 * This is a stop signal. Remove SIGCONT from all queues.
878 siginitset(&flush, sigmask(SIGCONT));
879 flush_sigqueue_mask(&flush, &signal->shared_pending);
880 for_each_thread(p, t)
881 flush_sigqueue_mask(&flush, &t->pending);
882 } else if (sig == SIGCONT) {
885 * Remove all stop signals from all queues, wake all threads.
887 siginitset(&flush, SIG_KERNEL_STOP_MASK);
888 flush_sigqueue_mask(&flush, &signal->shared_pending);
889 for_each_thread(p, t) {
890 flush_sigqueue_mask(&flush, &t->pending);
891 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
892 if (likely(!(t->ptrace & PT_SEIZED)))
893 wake_up_state(t, __TASK_STOPPED);
895 ptrace_trap_notify(t);
899 * Notify the parent with CLD_CONTINUED if we were stopped.
901 * If we were in the middle of a group stop, we pretend it
902 * was already finished, and then continued. Since SIGCHLD
903 * doesn't queue we report only CLD_STOPPED, as if the next
904 * CLD_CONTINUED was dropped.
907 if (signal->flags & SIGNAL_STOP_STOPPED)
908 why |= SIGNAL_CLD_CONTINUED;
909 else if (signal->group_stop_count)
910 why |= SIGNAL_CLD_STOPPED;
914 * The first thread which returns from do_signal_stop()
915 * will take ->siglock, notice SIGNAL_CLD_MASK, and
916 * notify its parent. See get_signal_to_deliver().
918 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
919 signal->group_stop_count = 0;
920 signal->group_exit_code = 0;
924 return !sig_ignored(p, sig, force);
928 * Test if P wants to take SIG. After we've checked all threads with this,
929 * it's equivalent to finding no threads not blocking SIG. Any threads not
930 * blocking SIG were ruled out because they are not running and already
931 * have pending signals. Such threads will dequeue from the shared queue
932 * as soon as they're available, so putting the signal on the shared queue
933 * will be equivalent to sending it to one such thread.
935 static inline int wants_signal(int sig, struct task_struct *p)
937 if (sigismember(&p->blocked, sig))
939 if (p->flags & PF_EXITING)
943 if (task_is_stopped_or_traced(p))
945 return task_curr(p) || !signal_pending(p);
948 static void complete_signal(int sig, struct task_struct *p, int group)
950 struct signal_struct *signal = p->signal;
951 struct task_struct *t;
954 * Now find a thread we can wake up to take the signal off the queue.
956 * If the main thread wants the signal, it gets first crack.
957 * Probably the least surprising to the average bear.
959 if (wants_signal(sig, p))
961 else if (!group || thread_group_empty(p))
963 * There is just one thread and it does not need to be woken.
964 * It will dequeue unblocked signals before it runs again.
969 * Otherwise try to find a suitable thread.
971 t = signal->curr_target;
972 while (!wants_signal(sig, t)) {
974 if (t == signal->curr_target)
976 * No thread needs to be woken.
977 * Any eligible threads will see
978 * the signal in the queue soon.
982 signal->curr_target = t;
986 * Found a killable thread. If the signal will be fatal,
987 * then start taking the whole group down immediately.
989 if (sig_fatal(p, sig) &&
990 !(signal->flags & SIGNAL_GROUP_EXIT) &&
991 !sigismember(&t->real_blocked, sig) &&
992 (sig == SIGKILL || !p->ptrace)) {
994 * This signal will be fatal to the whole group.
996 if (!sig_kernel_coredump(sig)) {
998 * Start a group exit and wake everybody up.
999 * This way we don't have other threads
1000 * running and doing things after a slower
1001 * thread has the fatal signal pending.
1003 signal->flags = SIGNAL_GROUP_EXIT;
1004 signal->group_exit_code = sig;
1005 signal->group_stop_count = 0;
1008 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1009 sigaddset(&t->pending.signal, SIGKILL);
1010 signal_wake_up(t, 1);
1011 } while_each_thread(p, t);
1017 * The signal is already in the shared-pending queue.
1018 * Tell the chosen thread to wake up and dequeue it.
1020 signal_wake_up(t, sig == SIGKILL);
1024 static inline int legacy_queue(struct sigpending *signals, int sig)
1026 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1029 #ifdef CONFIG_USER_NS
1030 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1032 if (current_user_ns() == task_cred_xxx(t, user_ns))
1035 if (SI_FROMKERNEL(info))
1039 info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1040 make_kuid(current_user_ns(), info->si_uid));
1044 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1050 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1051 int group, int from_ancestor_ns)
1053 struct sigpending *pending;
1055 int override_rlimit;
1056 int ret = 0, result;
1058 assert_spin_locked(&t->sighand->siglock);
1060 result = TRACE_SIGNAL_IGNORED;
1061 if (!prepare_signal(sig, t,
1062 from_ancestor_ns || (info == SEND_SIG_PRIV) || (info == SEND_SIG_FORCED)))
1065 pending = group ? &t->signal->shared_pending : &t->pending;
1067 * Short-circuit ignored signals and support queuing
1068 * exactly one non-rt signal, so that we can get more
1069 * detailed information about the cause of the signal.
1071 result = TRACE_SIGNAL_ALREADY_PENDING;
1072 if (legacy_queue(pending, sig))
1075 result = TRACE_SIGNAL_DELIVERED;
1077 * fast-pathed signals for kernel-internal things like SIGSTOP
1080 if (info == SEND_SIG_FORCED)
1084 * Real-time signals must be queued if sent by sigqueue, or
1085 * some other real-time mechanism. It is implementation
1086 * defined whether kill() does so. We attempt to do so, on
1087 * the principle of least surprise, but since kill is not
1088 * allowed to fail with EAGAIN when low on memory we just
1089 * make sure at least one signal gets delivered and don't
1090 * pass on the info struct.
1093 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1095 override_rlimit = 0;
1097 q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
1099 list_add_tail(&q->list, &pending->list);
1100 switch ((unsigned long) info) {
1101 case (unsigned long) SEND_SIG_NOINFO:
1102 q->info.si_signo = sig;
1103 q->info.si_errno = 0;
1104 q->info.si_code = SI_USER;
1105 q->info.si_pid = task_tgid_nr_ns(current,
1106 task_active_pid_ns(t));
1107 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1109 case (unsigned long) SEND_SIG_PRIV:
1110 q->info.si_signo = sig;
1111 q->info.si_errno = 0;
1112 q->info.si_code = SI_KERNEL;
1117 copy_siginfo(&q->info, info);
1118 if (from_ancestor_ns)
1123 userns_fixup_signal_uid(&q->info, t);
1125 } else if (!is_si_special(info)) {
1126 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1128 * Queue overflow, abort. We may abort if the
1129 * signal was rt and sent by user using something
1130 * other than kill().
1132 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1137 * This is a silent loss of information. We still
1138 * send the signal, but the *info bits are lost.
1140 result = TRACE_SIGNAL_LOSE_INFO;
1145 signalfd_notify(t, sig);
1146 sigaddset(&pending->signal, sig);
1147 complete_signal(sig, t, group);
1149 trace_signal_generate(sig, info, t, group, result);
1153 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1156 int from_ancestor_ns = 0;
1158 #ifdef CONFIG_PID_NS
1159 from_ancestor_ns = si_fromuser(info) &&
1160 !task_pid_nr_ns(current, task_active_pid_ns(t));
1163 return __send_signal(sig, info, t, group, from_ancestor_ns);
1166 static void print_fatal_signal(int signr)
1168 struct pt_regs *regs = signal_pt_regs();
1169 pr_info("potentially unexpected fatal signal %d.\n", signr);
1171 #if defined(__i386__) && !defined(__arch_um__)
1172 pr_info("code at %08lx: ", regs->ip);
1175 for (i = 0; i < 16; i++) {
1178 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1180 pr_cont("%02x ", insn);
1190 static int __init setup_print_fatal_signals(char *str)
1192 get_option (&str, &print_fatal_signals);
1197 __setup("print-fatal-signals=", setup_print_fatal_signals);
1200 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1202 return send_signal(sig, info, p, 1);
1206 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1208 return send_signal(sig, info, t, 0);
1211 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1214 unsigned long flags;
1217 if (lock_task_sighand(p, &flags)) {
1218 ret = send_signal(sig, info, p, group);
1219 unlock_task_sighand(p, &flags);
1226 * Force a signal that the process can't ignore: if necessary
1227 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1229 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1230 * since we do not want to have a signal handler that was blocked
1231 * be invoked when user space had explicitly blocked it.
1233 * We don't want to have recursive SIGSEGV's etc, for example,
1234 * that is why we also clear SIGNAL_UNKILLABLE.
1237 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1239 unsigned long int flags;
1240 int ret, blocked, ignored;
1241 struct k_sigaction *action;
1243 spin_lock_irqsave(&t->sighand->siglock, flags);
1244 action = &t->sighand->action[sig-1];
1245 ignored = action->sa.sa_handler == SIG_IGN;
1246 blocked = sigismember(&t->blocked, sig);
1247 if (blocked || ignored) {
1248 action->sa.sa_handler = SIG_DFL;
1250 sigdelset(&t->blocked, sig);
1251 recalc_sigpending_and_wake(t);
1255 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
1256 * debugging to leave init killable.
1258 if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
1259 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1260 ret = specific_send_sig_info(sig, info, t);
1261 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1267 * Nuke all other threads in the group.
1269 int zap_other_threads(struct task_struct *p)
1271 struct task_struct *t = p;
1274 p->signal->group_stop_count = 0;
1276 while_each_thread(p, t) {
1277 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1280 /* Don't bother with already dead threads */
1283 sigaddset(&t->pending.signal, SIGKILL);
1284 signal_wake_up(t, 1);
1290 struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1291 unsigned long *flags)
1293 struct sighand_struct *sighand;
1297 * Disable interrupts early to avoid deadlocks.
1298 * See rcu_read_unlock() comment header for details.
1300 local_irq_save(*flags);
1302 sighand = rcu_dereference(tsk->sighand);
1303 if (unlikely(sighand == NULL)) {
1305 local_irq_restore(*flags);
1309 * This sighand can be already freed and even reused, but
1310 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
1311 * initializes ->siglock: this slab can't go away, it has
1312 * the same object type, ->siglock can't be reinitialized.
1314 * We need to ensure that tsk->sighand is still the same
1315 * after we take the lock, we can race with de_thread() or
1316 * __exit_signal(). In the latter case the next iteration
1317 * must see ->sighand == NULL.
1319 spin_lock(&sighand->siglock);
1320 if (likely(sighand == tsk->sighand)) {
1324 spin_unlock(&sighand->siglock);
1326 local_irq_restore(*flags);
1333 * send signal info to all the members of a group
1335 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1340 ret = check_kill_permission(sig, info, p);
1344 ret = do_send_sig_info(sig, info, p, true);
1350 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1351 * control characters do (^C, ^Z etc)
1352 * - the caller must hold at least a readlock on tasklist_lock
1354 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1356 struct task_struct *p = NULL;
1357 int retval, success;
1361 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1362 int err = group_send_sig_info(sig, info, p);
1365 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1366 return success ? 0 : retval;
1369 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1372 struct task_struct *p;
1376 p = pid_task(pid, PIDTYPE_PID);
1378 error = group_send_sig_info(sig, info, p);
1380 if (likely(!p || error != -ESRCH))
1384 * The task was unhashed in between, try again. If it
1385 * is dead, pid_task() will return NULL, if we race with
1386 * de_thread() it will find the new leader.
1391 static int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1395 error = kill_pid_info(sig, info, find_vpid(pid));
1400 static int kill_as_cred_perm(const struct cred *cred,
1401 struct task_struct *target)
1403 const struct cred *pcred = __task_cred(target);
1404 if (!uid_eq(cred->euid, pcred->suid) && !uid_eq(cred->euid, pcred->uid) &&
1405 !uid_eq(cred->uid, pcred->suid) && !uid_eq(cred->uid, pcred->uid))
1410 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1411 int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1412 const struct cred *cred, u32 secid)
1415 struct task_struct *p;
1416 unsigned long flags;
1418 if (!valid_signal(sig))
1422 p = pid_task(pid, PIDTYPE_PID);
1427 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
1431 ret = security_task_kill(p, info, sig, secid);
1436 if (lock_task_sighand(p, &flags)) {
1437 ret = __send_signal(sig, info, p, 1, 0);
1438 unlock_task_sighand(p, &flags);
1446 EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
1449 * kill_something_info() interprets pid in interesting ways just like kill(2).
1451 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1452 * is probably wrong. Should make it like BSD or SYSV.
1455 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1461 ret = kill_pid_info(sig, info, find_vpid(pid));
1466 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1470 read_lock(&tasklist_lock);
1472 ret = __kill_pgrp_info(sig, info,
1473 pid ? find_vpid(-pid) : task_pgrp(current));
1475 int retval = 0, count = 0;
1476 struct task_struct * p;
1478 for_each_process(p) {
1479 if (task_pid_vnr(p) > 1 &&
1480 !same_thread_group(p, current)) {
1481 int err = group_send_sig_info(sig, info, p);
1487 ret = count ? retval : -ESRCH;
1489 read_unlock(&tasklist_lock);
1495 * These are for backward compatibility with the rest of the kernel source.
1498 int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1501 * Make sure legacy kernel users don't send in bad values
1502 * (normal paths check this in check_kill_permission).
1504 if (!valid_signal(sig))
1507 return do_send_sig_info(sig, info, p, false);
1510 #define __si_special(priv) \
1511 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1514 send_sig(int sig, struct task_struct *p, int priv)
1516 return send_sig_info(sig, __si_special(priv), p);
1520 force_sig(int sig, struct task_struct *p)
1522 force_sig_info(sig, SEND_SIG_PRIV, p);
1526 * When things go south during signal handling, we
1527 * will force a SIGSEGV. And if the signal that caused
1528 * the problem was already a SIGSEGV, we'll want to
1529 * make sure we don't even try to deliver the signal..
1532 force_sigsegv(int sig, struct task_struct *p)
1534 if (sig == SIGSEGV) {
1535 unsigned long flags;
1536 spin_lock_irqsave(&p->sighand->siglock, flags);
1537 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1538 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1540 force_sig(SIGSEGV, p);
1544 int kill_pgrp(struct pid *pid, int sig, int priv)
1548 read_lock(&tasklist_lock);
1549 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1550 read_unlock(&tasklist_lock);
1554 EXPORT_SYMBOL(kill_pgrp);
1556 int kill_pid(struct pid *pid, int sig, int priv)
1558 return kill_pid_info(sig, __si_special(priv), pid);
1560 EXPORT_SYMBOL(kill_pid);
1563 * These functions support sending signals using preallocated sigqueue
1564 * structures. This is needed "because realtime applications cannot
1565 * afford to lose notifications of asynchronous events, like timer
1566 * expirations or I/O completions". In the case of POSIX Timers
1567 * we allocate the sigqueue structure from the timer_create. If this
1568 * allocation fails we are able to report the failure to the application
1569 * with an EAGAIN error.
1571 struct sigqueue *sigqueue_alloc(void)
1573 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1576 q->flags |= SIGQUEUE_PREALLOC;
1581 void sigqueue_free(struct sigqueue *q)
1583 unsigned long flags;
1584 spinlock_t *lock = ¤t->sighand->siglock;
1586 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1588 * We must hold ->siglock while testing q->list
1589 * to serialize with collect_signal() or with
1590 * __exit_signal()->flush_sigqueue().
1592 spin_lock_irqsave(lock, flags);
1593 q->flags &= ~SIGQUEUE_PREALLOC;
1595 * If it is queued it will be freed when dequeued,
1596 * like the "regular" sigqueue.
1598 if (!list_empty(&q->list))
1600 spin_unlock_irqrestore(lock, flags);
1606 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1608 int sig = q->info.si_signo;
1609 struct sigpending *pending;
1610 unsigned long flags;
1613 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1616 if (!likely(lock_task_sighand(t, &flags)))
1619 ret = 1; /* the signal is ignored */
1620 result = TRACE_SIGNAL_IGNORED;
1621 if (!prepare_signal(sig, t, false))
1625 if (unlikely(!list_empty(&q->list))) {
1627 * If an SI_TIMER entry is already queue just increment
1628 * the overrun count.
1630 BUG_ON(q->info.si_code != SI_TIMER);
1631 q->info.si_overrun++;
1632 result = TRACE_SIGNAL_ALREADY_PENDING;
1635 q->info.si_overrun = 0;
1637 signalfd_notify(t, sig);
1638 pending = group ? &t->signal->shared_pending : &t->pending;
1639 list_add_tail(&q->list, &pending->list);
1640 sigaddset(&pending->signal, sig);
1641 complete_signal(sig, t, group);
1642 result = TRACE_SIGNAL_DELIVERED;
1644 trace_signal_generate(sig, &q->info, t, group, result);
1645 unlock_task_sighand(t, &flags);
1651 * Let a parent know about the death of a child.
1652 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1654 * Returns true if our parent ignored us and so we've switched to
1657 bool do_notify_parent(struct task_struct *tsk, int sig)
1659 struct siginfo info;
1660 unsigned long flags;
1661 struct sighand_struct *psig;
1662 bool autoreap = false;
1667 /* do_notify_parent_cldstop should have been called instead. */
1668 BUG_ON(task_is_stopped_or_traced(tsk));
1670 BUG_ON(!tsk->ptrace &&
1671 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1673 if (sig != SIGCHLD) {
1675 * This is only possible if parent == real_parent.
1676 * Check if it has changed security domain.
1678 if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id))
1682 info.si_signo = sig;
1685 * We are under tasklist_lock here so our parent is tied to
1686 * us and cannot change.
1688 * task_active_pid_ns will always return the same pid namespace
1689 * until a task passes through release_task.
1691 * write_lock() currently calls preempt_disable() which is the
1692 * same as rcu_read_lock(), but according to Oleg, this is not
1693 * correct to rely on this
1696 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
1697 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1701 task_cputime(tsk, &utime, &stime);
1702 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
1703 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
1705 info.si_status = tsk->exit_code & 0x7f;
1706 if (tsk->exit_code & 0x80)
1707 info.si_code = CLD_DUMPED;
1708 else if (tsk->exit_code & 0x7f)
1709 info.si_code = CLD_KILLED;
1711 info.si_code = CLD_EXITED;
1712 info.si_status = tsk->exit_code >> 8;
1715 psig = tsk->parent->sighand;
1716 spin_lock_irqsave(&psig->siglock, flags);
1717 if (!tsk->ptrace && sig == SIGCHLD &&
1718 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1719 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1721 * We are exiting and our parent doesn't care. POSIX.1
1722 * defines special semantics for setting SIGCHLD to SIG_IGN
1723 * or setting the SA_NOCLDWAIT flag: we should be reaped
1724 * automatically and not left for our parent's wait4 call.
1725 * Rather than having the parent do it as a magic kind of
1726 * signal handler, we just set this to tell do_exit that we
1727 * can be cleaned up without becoming a zombie. Note that
1728 * we still call __wake_up_parent in this case, because a
1729 * blocked sys_wait4 might now return -ECHILD.
1731 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1732 * is implementation-defined: we do (if you don't want
1733 * it, just use SIG_IGN instead).
1736 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1739 if (valid_signal(sig) && sig)
1740 __group_send_sig_info(sig, &info, tsk->parent);
1741 __wake_up_parent(tsk, tsk->parent);
1742 spin_unlock_irqrestore(&psig->siglock, flags);
1748 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1749 * @tsk: task reporting the state change
1750 * @for_ptracer: the notification is for ptracer
1751 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1753 * Notify @tsk's parent that the stopped/continued state has changed. If
1754 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1755 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1758 * Must be called with tasklist_lock at least read locked.
1760 static void do_notify_parent_cldstop(struct task_struct *tsk,
1761 bool for_ptracer, int why)
1763 struct siginfo info;
1764 unsigned long flags;
1765 struct task_struct *parent;
1766 struct sighand_struct *sighand;
1770 parent = tsk->parent;
1772 tsk = tsk->group_leader;
1773 parent = tsk->real_parent;
1776 info.si_signo = SIGCHLD;
1779 * see comment in do_notify_parent() about the following 4 lines
1782 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
1783 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
1786 task_cputime(tsk, &utime, &stime);
1787 info.si_utime = nsec_to_clock_t(utime);
1788 info.si_stime = nsec_to_clock_t(stime);
1793 info.si_status = SIGCONT;
1796 info.si_status = tsk->signal->group_exit_code & 0x7f;
1799 info.si_status = tsk->exit_code & 0x7f;
1805 sighand = parent->sighand;
1806 spin_lock_irqsave(&sighand->siglock, flags);
1807 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1808 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1809 __group_send_sig_info(SIGCHLD, &info, parent);
1811 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1813 __wake_up_parent(tsk, parent);
1814 spin_unlock_irqrestore(&sighand->siglock, flags);
1817 static inline int may_ptrace_stop(void)
1819 if (!likely(current->ptrace))
1822 * Are we in the middle of do_coredump?
1823 * If so and our tracer is also part of the coredump stopping
1824 * is a deadlock situation, and pointless because our tracer
1825 * is dead so don't allow us to stop.
1826 * If SIGKILL was already sent before the caller unlocked
1827 * ->siglock we must see ->core_state != NULL. Otherwise it
1828 * is safe to enter schedule().
1830 * This is almost outdated, a task with the pending SIGKILL can't
1831 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1832 * after SIGKILL was already dequeued.
1834 if (unlikely(current->mm->core_state) &&
1835 unlikely(current->mm == current->parent->mm))
1842 * Return non-zero if there is a SIGKILL that should be waking us up.
1843 * Called with the siglock held.
1845 static int sigkill_pending(struct task_struct *tsk)
1847 return sigismember(&tsk->pending.signal, SIGKILL) ||
1848 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1852 * This must be called with current->sighand->siglock held.
1854 * This should be the path for all ptrace stops.
1855 * We always set current->last_siginfo while stopped here.
1856 * That makes it a way to test a stopped process for
1857 * being ptrace-stopped vs being job-control-stopped.
1859 * If we actually decide not to stop at all because the tracer
1860 * is gone, we keep current->exit_code unless clear_code.
1862 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1863 __releases(¤t->sighand->siglock)
1864 __acquires(¤t->sighand->siglock)
1866 bool gstop_done = false;
1868 if (arch_ptrace_stop_needed(exit_code, info)) {
1870 * The arch code has something special to do before a
1871 * ptrace stop. This is allowed to block, e.g. for faults
1872 * on user stack pages. We can't keep the siglock while
1873 * calling arch_ptrace_stop, so we must release it now.
1874 * To preserve proper semantics, we must do this before
1875 * any signal bookkeeping like checking group_stop_count.
1876 * Meanwhile, a SIGKILL could come in before we retake the
1877 * siglock. That must prevent us from sleeping in TASK_TRACED.
1878 * So after regaining the lock, we must check for SIGKILL.
1880 spin_unlock_irq(¤t->sighand->siglock);
1881 arch_ptrace_stop(exit_code, info);
1882 spin_lock_irq(¤t->sighand->siglock);
1883 if (sigkill_pending(current))
1887 set_special_state(TASK_TRACED);
1890 * We're committing to trapping. TRACED should be visible before
1891 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1892 * Also, transition to TRACED and updates to ->jobctl should be
1893 * atomic with respect to siglock and should be done after the arch
1894 * hook as siglock is released and regrabbed across it.
1899 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
1901 * set_current_state() smp_wmb();
1903 * wait_task_stopped()
1904 * task_stopped_code()
1905 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
1909 current->last_siginfo = info;
1910 current->exit_code = exit_code;
1913 * If @why is CLD_STOPPED, we're trapping to participate in a group
1914 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1915 * across siglock relocks since INTERRUPT was scheduled, PENDING
1916 * could be clear now. We act as if SIGCONT is received after
1917 * TASK_TRACED is entered - ignore it.
1919 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
1920 gstop_done = task_participate_group_stop(current);
1922 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
1923 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
1924 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1925 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
1927 /* entering a trap, clear TRAPPING */
1928 task_clear_jobctl_trapping(current);
1930 spin_unlock_irq(¤t->sighand->siglock);
1931 read_lock(&tasklist_lock);
1932 if (may_ptrace_stop()) {
1934 * Notify parents of the stop.
1936 * While ptraced, there are two parents - the ptracer and
1937 * the real_parent of the group_leader. The ptracer should
1938 * know about every stop while the real parent is only
1939 * interested in the completion of group stop. The states
1940 * for the two don't interact with each other. Notify
1941 * separately unless they're gonna be duplicates.
1943 do_notify_parent_cldstop(current, true, why);
1944 if (gstop_done && ptrace_reparented(current))
1945 do_notify_parent_cldstop(current, false, why);
1948 * Don't want to allow preemption here, because
1949 * sys_ptrace() needs this task to be inactive.
1951 * XXX: implement read_unlock_no_resched().
1954 read_unlock(&tasklist_lock);
1955 preempt_enable_no_resched();
1956 freezable_schedule();
1959 * By the time we got the lock, our tracer went away.
1960 * Don't drop the lock yet, another tracer may come.
1962 * If @gstop_done, the ptracer went away between group stop
1963 * completion and here. During detach, it would have set
1964 * JOBCTL_STOP_PENDING on us and we'll re-enter
1965 * TASK_STOPPED in do_signal_stop() on return, so notifying
1966 * the real parent of the group stop completion is enough.
1969 do_notify_parent_cldstop(current, false, why);
1971 /* tasklist protects us from ptrace_freeze_traced() */
1972 __set_current_state(TASK_RUNNING);
1974 current->exit_code = 0;
1975 read_unlock(&tasklist_lock);
1979 * We are back. Now reacquire the siglock before touching
1980 * last_siginfo, so that we are sure to have synchronized with
1981 * any signal-sending on another CPU that wants to examine it.
1983 spin_lock_irq(¤t->sighand->siglock);
1984 current->last_siginfo = NULL;
1986 /* LISTENING can be set only during STOP traps, clear it */
1987 current->jobctl &= ~JOBCTL_LISTENING;
1990 * Queued signals ignored us while we were stopped for tracing.
1991 * So check for any that we should take before resuming user mode.
1992 * This sets TIF_SIGPENDING, but never clears it.
1994 recalc_sigpending_tsk(current);
1997 static void ptrace_do_notify(int signr, int exit_code, int why)
2001 memset(&info, 0, sizeof info);
2002 info.si_signo = signr;
2003 info.si_code = exit_code;
2004 info.si_pid = task_pid_vnr(current);
2005 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2007 /* Let the debugger run. */
2008 ptrace_stop(exit_code, why, 1, &info);
2011 void ptrace_notify(int exit_code)
2013 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2014 if (unlikely(current->task_works))
2017 spin_lock_irq(¤t->sighand->siglock);
2018 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
2019 spin_unlock_irq(¤t->sighand->siglock);
2023 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2024 * @signr: signr causing group stop if initiating
2026 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2027 * and participate in it. If already set, participate in the existing
2028 * group stop. If participated in a group stop (and thus slept), %true is
2029 * returned with siglock released.
2031 * If ptraced, this function doesn't handle stop itself. Instead,
2032 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2033 * untouched. The caller must ensure that INTERRUPT trap handling takes
2034 * places afterwards.
2037 * Must be called with @current->sighand->siglock held, which is released
2041 * %false if group stop is already cancelled or ptrace trap is scheduled.
2042 * %true if participated in group stop.
2044 static bool do_signal_stop(int signr)
2045 __releases(¤t->sighand->siglock)
2047 struct signal_struct *sig = current->signal;
2049 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2050 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2051 struct task_struct *t;
2053 /* signr will be recorded in task->jobctl for retries */
2054 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2056 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2057 unlikely(signal_group_exit(sig)))
2060 * There is no group stop already in progress. We must
2063 * While ptraced, a task may be resumed while group stop is
2064 * still in effect and then receive a stop signal and
2065 * initiate another group stop. This deviates from the
2066 * usual behavior as two consecutive stop signals can't
2067 * cause two group stops when !ptraced. That is why we
2068 * also check !task_is_stopped(t) below.
2070 * The condition can be distinguished by testing whether
2071 * SIGNAL_STOP_STOPPED is already set. Don't generate
2072 * group_exit_code in such case.
2074 * This is not necessary for SIGNAL_STOP_CONTINUED because
2075 * an intervening stop signal is required to cause two
2076 * continued events regardless of ptrace.
2078 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2079 sig->group_exit_code = signr;
2081 sig->group_stop_count = 0;
2083 if (task_set_jobctl_pending(current, signr | gstop))
2084 sig->group_stop_count++;
2087 while_each_thread(current, t) {
2089 * Setting state to TASK_STOPPED for a group
2090 * stop is always done with the siglock held,
2091 * so this check has no races.
2093 if (!task_is_stopped(t) &&
2094 task_set_jobctl_pending(t, signr | gstop)) {
2095 sig->group_stop_count++;
2096 if (likely(!(t->ptrace & PT_SEIZED)))
2097 signal_wake_up(t, 0);
2099 ptrace_trap_notify(t);
2104 if (likely(!current->ptrace)) {
2108 * If there are no other threads in the group, or if there
2109 * is a group stop in progress and we are the last to stop,
2110 * report to the parent.
2112 if (task_participate_group_stop(current))
2113 notify = CLD_STOPPED;
2115 set_special_state(TASK_STOPPED);
2116 spin_unlock_irq(¤t->sighand->siglock);
2119 * Notify the parent of the group stop completion. Because
2120 * we're not holding either the siglock or tasklist_lock
2121 * here, ptracer may attach inbetween; however, this is for
2122 * group stop and should always be delivered to the real
2123 * parent of the group leader. The new ptracer will get
2124 * its notification when this task transitions into
2128 read_lock(&tasklist_lock);
2129 do_notify_parent_cldstop(current, false, notify);
2130 read_unlock(&tasklist_lock);
2133 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2134 freezable_schedule();
2138 * While ptraced, group stop is handled by STOP trap.
2139 * Schedule it and let the caller deal with it.
2141 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2147 * do_jobctl_trap - take care of ptrace jobctl traps
2149 * When PT_SEIZED, it's used for both group stop and explicit
2150 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2151 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2152 * the stop signal; otherwise, %SIGTRAP.
2154 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2155 * number as exit_code and no siginfo.
2158 * Must be called with @current->sighand->siglock held, which may be
2159 * released and re-acquired before returning with intervening sleep.
2161 static void do_jobctl_trap(void)
2163 struct signal_struct *signal = current->signal;
2164 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2166 if (current->ptrace & PT_SEIZED) {
2167 if (!signal->group_stop_count &&
2168 !(signal->flags & SIGNAL_STOP_STOPPED))
2170 WARN_ON_ONCE(!signr);
2171 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2174 WARN_ON_ONCE(!signr);
2175 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2176 current->exit_code = 0;
2180 static int ptrace_signal(int signr, siginfo_t *info)
2183 * We do not check sig_kernel_stop(signr) but set this marker
2184 * unconditionally because we do not know whether debugger will
2185 * change signr. This flag has no meaning unless we are going
2186 * to stop after return from ptrace_stop(). In this case it will
2187 * be checked in do_signal_stop(), we should only stop if it was
2188 * not cleared by SIGCONT while we were sleeping. See also the
2189 * comment in dequeue_signal().
2191 current->jobctl |= JOBCTL_STOP_DEQUEUED;
2192 ptrace_stop(signr, CLD_TRAPPED, 0, info);
2194 /* We're back. Did the debugger cancel the sig? */
2195 signr = current->exit_code;
2199 current->exit_code = 0;
2202 * Update the siginfo structure if the signal has
2203 * changed. If the debugger wanted something
2204 * specific in the siginfo structure then it should
2205 * have updated *info via PTRACE_SETSIGINFO.
2207 if (signr != info->si_signo) {
2208 info->si_signo = signr;
2210 info->si_code = SI_USER;
2212 info->si_pid = task_pid_vnr(current->parent);
2213 info->si_uid = from_kuid_munged(current_user_ns(),
2214 task_uid(current->parent));
2218 /* If the (new) signal is now blocked, requeue it. */
2219 if (sigismember(¤t->blocked, signr)) {
2220 specific_send_sig_info(signr, info, current);
2227 int get_signal(struct ksignal *ksig)
2229 struct sighand_struct *sighand = current->sighand;
2230 struct signal_struct *signal = current->signal;
2233 if (unlikely(current->task_works))
2236 if (unlikely(uprobe_deny_signal()))
2240 * Do this once, we can't return to user-mode if freezing() == T.
2241 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2242 * thus do not need another check after return.
2247 spin_lock_irq(&sighand->siglock);
2249 * Every stopped thread goes here after wakeup. Check to see if
2250 * we should notify the parent, prepare_signal(SIGCONT) encodes
2251 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2253 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2256 if (signal->flags & SIGNAL_CLD_CONTINUED)
2257 why = CLD_CONTINUED;
2261 signal->flags &= ~SIGNAL_CLD_MASK;
2263 spin_unlock_irq(&sighand->siglock);
2266 * Notify the parent that we're continuing. This event is
2267 * always per-process and doesn't make whole lot of sense
2268 * for ptracers, who shouldn't consume the state via
2269 * wait(2) either, but, for backward compatibility, notify
2270 * the ptracer of the group leader too unless it's gonna be
2273 read_lock(&tasklist_lock);
2274 do_notify_parent_cldstop(current, false, why);
2276 if (ptrace_reparented(current->group_leader))
2277 do_notify_parent_cldstop(current->group_leader,
2279 read_unlock(&tasklist_lock);
2284 /* Has this task already been marked for death? */
2285 if (signal_group_exit(signal)) {
2286 ksig->info.si_signo = signr = SIGKILL;
2287 sigdelset(¤t->pending.signal, SIGKILL);
2288 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2289 &sighand->action[SIGKILL - 1]);
2290 recalc_sigpending();
2295 struct k_sigaction *ka;
2297 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2301 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2303 spin_unlock_irq(&sighand->siglock);
2308 * Signals generated by the execution of an instruction
2309 * need to be delivered before any other pending signals
2310 * so that the instruction pointer in the signal stack
2311 * frame points to the faulting instruction.
2313 signr = dequeue_synchronous_signal(&ksig->info);
2315 signr = dequeue_signal(current, ¤t->blocked, &ksig->info);
2318 break; /* will return 0 */
2320 if (unlikely(current->ptrace) && signr != SIGKILL) {
2321 signr = ptrace_signal(signr, &ksig->info);
2326 ka = &sighand->action[signr-1];
2328 /* Trace actually delivered signals. */
2329 trace_signal_deliver(signr, &ksig->info, ka);
2331 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2333 if (ka->sa.sa_handler != SIG_DFL) {
2334 /* Run the handler. */
2337 if (ka->sa.sa_flags & SA_ONESHOT)
2338 ka->sa.sa_handler = SIG_DFL;
2340 break; /* will return non-zero "signr" value */
2344 * Now we are doing the default action for this signal.
2346 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2350 * Global init gets no signals it doesn't want.
2351 * Container-init gets no signals it doesn't want from same
2354 * Note that if global/container-init sees a sig_kernel_only()
2355 * signal here, the signal must have been generated internally
2356 * or must have come from an ancestor namespace. In either
2357 * case, the signal cannot be dropped.
2359 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2360 !sig_kernel_only(signr))
2363 if (sig_kernel_stop(signr)) {
2365 * The default action is to stop all threads in
2366 * the thread group. The job control signals
2367 * do nothing in an orphaned pgrp, but SIGSTOP
2368 * always works. Note that siglock needs to be
2369 * dropped during the call to is_orphaned_pgrp()
2370 * because of lock ordering with tasklist_lock.
2371 * This allows an intervening SIGCONT to be posted.
2372 * We need to check for that and bail out if necessary.
2374 if (signr != SIGSTOP) {
2375 spin_unlock_irq(&sighand->siglock);
2377 /* signals can be posted during this window */
2379 if (is_current_pgrp_orphaned())
2382 spin_lock_irq(&sighand->siglock);
2385 if (likely(do_signal_stop(ksig->info.si_signo))) {
2386 /* It released the siglock. */
2391 * We didn't actually stop, due to a race
2392 * with SIGCONT or something like that.
2398 spin_unlock_irq(&sighand->siglock);
2401 * Anything else is fatal, maybe with a core dump.
2403 current->flags |= PF_SIGNALED;
2405 if (sig_kernel_coredump(signr)) {
2406 if (print_fatal_signals)
2407 print_fatal_signal(ksig->info.si_signo);
2408 proc_coredump_connector(current);
2410 * If it was able to dump core, this kills all
2411 * other threads in the group and synchronizes with
2412 * their demise. If we lost the race with another
2413 * thread getting here, it set group_exit_code
2414 * first and our do_group_exit call below will use
2415 * that value and ignore the one we pass it.
2417 do_coredump(&ksig->info);
2421 * Death signals, no core dump.
2423 do_group_exit(ksig->info.si_signo);
2426 spin_unlock_irq(&sighand->siglock);
2429 return ksig->sig > 0;
2433 * signal_delivered -
2434 * @ksig: kernel signal struct
2435 * @stepping: nonzero if debugger single-step or block-step in use
2437 * This function should be called when a signal has successfully been
2438 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2439 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2440 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
2442 static void signal_delivered(struct ksignal *ksig, int stepping)
2446 /* A signal was successfully delivered, and the
2447 saved sigmask was stored on the signal frame,
2448 and will be restored by sigreturn. So we can
2449 simply clear the restore sigmask flag. */
2450 clear_restore_sigmask();
2452 sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
2453 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2454 sigaddset(&blocked, ksig->sig);
2455 set_current_blocked(&blocked);
2456 tracehook_signal_handler(stepping);
2459 void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2462 force_sigsegv(ksig->sig, current);
2464 signal_delivered(ksig, stepping);
2468 * It could be that complete_signal() picked us to notify about the
2469 * group-wide signal. Other threads should be notified now to take
2470 * the shared signals in @which since we will not.
2472 static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2475 struct task_struct *t;
2477 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2478 if (sigisemptyset(&retarget))
2482 while_each_thread(tsk, t) {
2483 if (t->flags & PF_EXITING)
2486 if (!has_pending_signals(&retarget, &t->blocked))
2488 /* Remove the signals this thread can handle. */
2489 sigandsets(&retarget, &retarget, &t->blocked);
2491 if (!signal_pending(t))
2492 signal_wake_up(t, 0);
2494 if (sigisemptyset(&retarget))
2499 void exit_signals(struct task_struct *tsk)
2505 * @tsk is about to have PF_EXITING set - lock out users which
2506 * expect stable threadgroup.
2508 cgroup_threadgroup_change_begin(tsk);
2510 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2511 tsk->flags |= PF_EXITING;
2512 cgroup_threadgroup_change_end(tsk);
2516 spin_lock_irq(&tsk->sighand->siglock);
2518 * From now this task is not visible for group-wide signals,
2519 * see wants_signal(), do_signal_stop().
2521 tsk->flags |= PF_EXITING;
2523 cgroup_threadgroup_change_end(tsk);
2525 if (!signal_pending(tsk))
2528 unblocked = tsk->blocked;
2529 signotset(&unblocked);
2530 retarget_shared_pending(tsk, &unblocked);
2532 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2533 task_participate_group_stop(tsk))
2534 group_stop = CLD_STOPPED;
2536 spin_unlock_irq(&tsk->sighand->siglock);
2539 * If group stop has completed, deliver the notification. This
2540 * should always go to the real parent of the group leader.
2542 if (unlikely(group_stop)) {
2543 read_lock(&tasklist_lock);
2544 do_notify_parent_cldstop(tsk, false, group_stop);
2545 read_unlock(&tasklist_lock);
2549 EXPORT_SYMBOL(recalc_sigpending);
2550 EXPORT_SYMBOL_GPL(dequeue_signal);
2551 EXPORT_SYMBOL(flush_signals);
2552 EXPORT_SYMBOL(force_sig);
2553 EXPORT_SYMBOL(send_sig);
2554 EXPORT_SYMBOL(send_sig_info);
2555 EXPORT_SYMBOL(sigprocmask);
2558 * System call entry points.
2562 * sys_restart_syscall - restart a system call
2564 SYSCALL_DEFINE0(restart_syscall)
2566 struct restart_block *restart = ¤t->restart_block;
2567 return restart->fn(restart);
2570 long do_no_restart_syscall(struct restart_block *param)
2575 static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2577 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2578 sigset_t newblocked;
2579 /* A set of now blocked but previously unblocked signals. */
2580 sigandnsets(&newblocked, newset, ¤t->blocked);
2581 retarget_shared_pending(tsk, &newblocked);
2583 tsk->blocked = *newset;
2584 recalc_sigpending();
2588 * set_current_blocked - change current->blocked mask
2591 * It is wrong to change ->blocked directly, this helper should be used
2592 * to ensure the process can't miss a shared signal we are going to block.
2594 void set_current_blocked(sigset_t *newset)
2596 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2597 __set_current_blocked(newset);
2600 void __set_current_blocked(const sigset_t *newset)
2602 struct task_struct *tsk = current;
2605 * In case the signal mask hasn't changed, there is nothing we need
2606 * to do. The current->blocked shouldn't be modified by other task.
2608 if (sigequalsets(&tsk->blocked, newset))
2611 spin_lock_irq(&tsk->sighand->siglock);
2612 __set_task_blocked(tsk, newset);
2613 spin_unlock_irq(&tsk->sighand->siglock);
2617 * This is also useful for kernel threads that want to temporarily
2618 * (or permanently) block certain signals.
2620 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2621 * interface happily blocks "unblockable" signals like SIGKILL
2624 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2626 struct task_struct *tsk = current;
2629 /* Lockless, only current can change ->blocked, never from irq */
2631 *oldset = tsk->blocked;
2635 sigorsets(&newset, &tsk->blocked, set);
2638 sigandnsets(&newset, &tsk->blocked, set);
2647 __set_current_blocked(&newset);
2652 * sys_rt_sigprocmask - change the list of currently blocked signals
2653 * @how: whether to add, remove, or set signals
2654 * @nset: stores pending signals
2655 * @oset: previous value of signal mask if non-null
2656 * @sigsetsize: size of sigset_t type
2658 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2659 sigset_t __user *, oset, size_t, sigsetsize)
2661 sigset_t old_set, new_set;
2664 /* XXX: Don't preclude handling different sized sigset_t's. */
2665 if (sigsetsize != sizeof(sigset_t))
2668 old_set = current->blocked;
2671 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2673 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2675 error = sigprocmask(how, &new_set, NULL);
2681 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2688 #ifdef CONFIG_COMPAT
2689 COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2690 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
2693 sigset_t old_set = current->blocked;
2695 /* XXX: Don't preclude handling different sized sigset_t's. */
2696 if (sigsetsize != sizeof(sigset_t))
2700 compat_sigset_t new32;
2703 if (copy_from_user(&new32, nset, sizeof(compat_sigset_t)))
2706 sigset_from_compat(&new_set, &new32);
2707 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2709 error = sigprocmask(how, &new_set, NULL);
2714 compat_sigset_t old32;
2715 sigset_to_compat(&old32, &old_set);
2716 if (copy_to_user(oset, &old32, sizeof(compat_sigset_t)))
2721 return sys_rt_sigprocmask(how, (sigset_t __user *)nset,
2722 (sigset_t __user *)oset, sigsetsize);
2727 static int do_sigpending(void *set, unsigned long sigsetsize)
2729 if (sigsetsize > sizeof(sigset_t))
2732 spin_lock_irq(¤t->sighand->siglock);
2733 sigorsets(set, ¤t->pending.signal,
2734 ¤t->signal->shared_pending.signal);
2735 spin_unlock_irq(¤t->sighand->siglock);
2737 /* Outside the lock because only this thread touches it. */
2738 sigandsets(set, ¤t->blocked, set);
2743 * sys_rt_sigpending - examine a pending signal that has been raised
2745 * @uset: stores pending signals
2746 * @sigsetsize: size of sigset_t type or larger
2748 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
2751 int err = do_sigpending(&set, sigsetsize);
2752 if (!err && copy_to_user(uset, &set, sigsetsize))
2757 #ifdef CONFIG_COMPAT
2758 COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2759 compat_size_t, sigsetsize)
2763 int err = do_sigpending(&set, sigsetsize);
2765 compat_sigset_t set32;
2766 sigset_to_compat(&set32, &set);
2767 /* we can get here only if sigsetsize <= sizeof(set) */
2768 if (copy_to_user(uset, &set32, sigsetsize))
2773 return sys_rt_sigpending((sigset_t __user *)uset, sigsetsize);
2778 enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
2780 enum siginfo_layout layout = SIL_KILL;
2781 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
2782 static const struct {
2783 unsigned char limit, layout;
2785 [SIGILL] = { NSIGILL, SIL_FAULT },
2786 [SIGFPE] = { NSIGFPE, SIL_FAULT },
2787 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
2788 [SIGBUS] = { NSIGBUS, SIL_FAULT },
2789 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
2790 #if defined(SIGEMT) && defined(NSIGEMT)
2791 [SIGEMT] = { NSIGEMT, SIL_FAULT },
2793 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
2794 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
2795 #ifdef __ARCH_SIGSYS
2796 [SIGSYS] = { NSIGSYS, SIL_SYS },
2799 if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit))
2800 layout = filter[sig].layout;
2801 else if (si_code <= NSIGPOLL)
2804 if (si_code == SI_TIMER)
2806 else if (si_code == SI_SIGIO)
2808 else if (si_code < 0)
2810 /* Tests to support buggy kernel ABIs */
2812 if ((sig == SIGTRAP) && (si_code == TRAP_FIXME))
2816 if ((sig == SIGFPE) && (si_code == FPE_FIXME))
2823 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2825 int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
2829 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2831 if (from->si_code < 0)
2832 return __copy_to_user(to, from, sizeof(siginfo_t))
2835 * If you change siginfo_t structure, please be sure
2836 * this code is fixed accordingly.
2837 * Please remember to update the signalfd_copyinfo() function
2838 * inside fs/signalfd.c too, in case siginfo_t changes.
2839 * It should never copy any pad contained in the structure
2840 * to avoid security leaks, but must copy the generic
2841 * 3 ints plus the relevant union member.
2843 err = __put_user(from->si_signo, &to->si_signo);
2844 err |= __put_user(from->si_errno, &to->si_errno);
2845 err |= __put_user(from->si_code, &to->si_code);
2846 switch (siginfo_layout(from->si_signo, from->si_code)) {
2848 err |= __put_user(from->si_pid, &to->si_pid);
2849 err |= __put_user(from->si_uid, &to->si_uid);
2852 /* Unreached SI_TIMER is negative */
2855 err |= __put_user(from->si_band, &to->si_band);
2856 err |= __put_user(from->si_fd, &to->si_fd);
2859 err |= __put_user(from->si_addr, &to->si_addr);
2860 #ifdef __ARCH_SI_TRAPNO
2861 err |= __put_user(from->si_trapno, &to->si_trapno);
2863 #ifdef BUS_MCEERR_AO
2865 * Other callers might not initialize the si_lsb field,
2866 * so check explicitly for the right codes here.
2868 if (from->si_signo == SIGBUS &&
2869 (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
2870 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2873 if (from->si_signo == SIGSEGV && from->si_code == SEGV_BNDERR) {
2874 err |= __put_user(from->si_lower, &to->si_lower);
2875 err |= __put_user(from->si_upper, &to->si_upper);
2879 if (from->si_signo == SIGSEGV && from->si_code == SEGV_PKUERR)
2880 err |= __put_user(from->si_pkey, &to->si_pkey);
2884 err |= __put_user(from->si_pid, &to->si_pid);
2885 err |= __put_user(from->si_uid, &to->si_uid);
2886 err |= __put_user(from->si_status, &to->si_status);
2887 err |= __put_user(from->si_utime, &to->si_utime);
2888 err |= __put_user(from->si_stime, &to->si_stime);
2891 err |= __put_user(from->si_pid, &to->si_pid);
2892 err |= __put_user(from->si_uid, &to->si_uid);
2893 err |= __put_user(from->si_ptr, &to->si_ptr);
2895 #ifdef __ARCH_SIGSYS
2897 err |= __put_user(from->si_call_addr, &to->si_call_addr);
2898 err |= __put_user(from->si_syscall, &to->si_syscall);
2899 err |= __put_user(from->si_arch, &to->si_arch);
2909 * do_sigtimedwait - wait for queued signals specified in @which
2910 * @which: queued signals to wait for
2911 * @info: if non-null, the signal's siginfo is returned here
2912 * @ts: upper bound on process time suspension
2914 static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2915 const struct timespec *ts)
2917 ktime_t *to = NULL, timeout = KTIME_MAX;
2918 struct task_struct *tsk = current;
2919 sigset_t mask = *which;
2923 if (!timespec_valid(ts))
2925 timeout = timespec_to_ktime(*ts);
2930 * Invert the set of allowed signals to get those we want to block.
2932 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2935 spin_lock_irq(&tsk->sighand->siglock);
2936 sig = dequeue_signal(tsk, &mask, info);
2937 if (!sig && timeout) {
2939 * None ready, temporarily unblock those we're interested
2940 * while we are sleeping in so that we'll be awakened when
2941 * they arrive. Unblocking is always fine, we can avoid
2942 * set_current_blocked().
2944 tsk->real_blocked = tsk->blocked;
2945 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2946 recalc_sigpending();
2947 spin_unlock_irq(&tsk->sighand->siglock);
2949 __set_current_state(TASK_INTERRUPTIBLE);
2950 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
2952 spin_lock_irq(&tsk->sighand->siglock);
2953 __set_task_blocked(tsk, &tsk->real_blocked);
2954 sigemptyset(&tsk->real_blocked);
2955 sig = dequeue_signal(tsk, &mask, info);
2957 spin_unlock_irq(&tsk->sighand->siglock);
2961 return ret ? -EINTR : -EAGAIN;
2965 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2967 * @uthese: queued signals to wait for
2968 * @uinfo: if non-null, the signal's siginfo is returned here
2969 * @uts: upper bound on process time suspension
2970 * @sigsetsize: size of sigset_t type
2972 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2973 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2981 /* XXX: Don't preclude handling different sized sigset_t's. */
2982 if (sigsetsize != sizeof(sigset_t))
2985 if (copy_from_user(&these, uthese, sizeof(these)))
2989 if (copy_from_user(&ts, uts, sizeof(ts)))
2993 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
2995 if (ret > 0 && uinfo) {
2996 if (copy_siginfo_to_user(uinfo, &info))
3003 #ifdef CONFIG_COMPAT
3004 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
3005 struct compat_siginfo __user *, uinfo,
3006 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
3008 compat_sigset_t s32;
3014 if (sigsetsize != sizeof(sigset_t))
3017 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
3019 sigset_from_compat(&s, &s32);
3022 if (compat_get_timespec(&t, uts))
3026 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3028 if (ret > 0 && uinfo) {
3029 if (copy_siginfo_to_user32(uinfo, &info))
3038 * sys_kill - send a signal to a process
3039 * @pid: the PID of the process
3040 * @sig: signal to be sent
3042 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
3044 struct siginfo info;
3046 info.si_signo = sig;
3048 info.si_code = SI_USER;
3049 info.si_pid = task_tgid_vnr(current);
3050 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3052 return kill_something_info(sig, &info, pid);
3056 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
3058 struct task_struct *p;
3062 p = find_task_by_vpid(pid);
3063 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
3064 error = check_kill_permission(sig, info, p);
3066 * The null signal is a permissions and process existence
3067 * probe. No signal is actually delivered.
3069 if (!error && sig) {
3070 error = do_send_sig_info(sig, info, p, false);
3072 * If lock_task_sighand() failed we pretend the task
3073 * dies after receiving the signal. The window is tiny,
3074 * and the signal is private anyway.
3076 if (unlikely(error == -ESRCH))
3085 static int do_tkill(pid_t tgid, pid_t pid, int sig)
3087 struct siginfo info = {};
3089 info.si_signo = sig;
3091 info.si_code = SI_TKILL;
3092 info.si_pid = task_tgid_vnr(current);
3093 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3095 return do_send_specific(tgid, pid, sig, &info);
3099 * sys_tgkill - send signal to one specific thread
3100 * @tgid: the thread group ID of the thread
3101 * @pid: the PID of the thread
3102 * @sig: signal to be sent
3104 * This syscall also checks the @tgid and returns -ESRCH even if the PID
3105 * exists but it's not belonging to the target process anymore. This
3106 * method solves the problem of threads exiting and PIDs getting reused.
3108 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3110 /* This is only valid for single tasks */
3111 if (pid <= 0 || tgid <= 0)
3114 return do_tkill(tgid, pid, sig);
3118 * sys_tkill - send signal to one specific task
3119 * @pid: the PID of the task
3120 * @sig: signal to be sent
3122 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3124 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
3126 /* This is only valid for single tasks */
3130 return do_tkill(0, pid, sig);
3133 static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
3135 /* Not even root can pretend to send signals from the kernel.
3136 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3138 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3139 (task_pid_vnr(current) != pid))
3142 info->si_signo = sig;
3144 /* POSIX.1b doesn't mention process groups. */
3145 return kill_proc_info(sig, info, pid);
3149 * sys_rt_sigqueueinfo - send signal information to a signal
3150 * @pid: the PID of the thread
3151 * @sig: signal to be sent
3152 * @uinfo: signal info to be sent
3154 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3155 siginfo_t __user *, uinfo)
3158 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3160 return do_rt_sigqueueinfo(pid, sig, &info);
3163 #ifdef CONFIG_COMPAT
3164 COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3167 struct compat_siginfo __user *, uinfo)
3169 siginfo_t info = {};
3170 int ret = copy_siginfo_from_user32(&info, uinfo);
3173 return do_rt_sigqueueinfo(pid, sig, &info);
3177 static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
3179 /* This is only valid for single tasks */
3180 if (pid <= 0 || tgid <= 0)
3183 /* Not even root can pretend to send signals from the kernel.
3184 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3186 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3187 (task_pid_vnr(current) != pid))
3190 info->si_signo = sig;
3192 return do_send_specific(tgid, pid, sig, info);
3195 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3196 siginfo_t __user *, uinfo)
3200 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3203 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3206 #ifdef CONFIG_COMPAT
3207 COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3211 struct compat_siginfo __user *, uinfo)
3213 siginfo_t info = {};
3215 if (copy_siginfo_from_user32(&info, uinfo))
3217 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3222 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3224 void kernel_sigaction(int sig, __sighandler_t action)
3226 spin_lock_irq(¤t->sighand->siglock);
3227 current->sighand->action[sig - 1].sa.sa_handler = action;
3228 if (action == SIG_IGN) {
3232 sigaddset(&mask, sig);
3234 flush_sigqueue_mask(&mask, ¤t->signal->shared_pending);
3235 flush_sigqueue_mask(&mask, ¤t->pending);
3236 recalc_sigpending();
3238 spin_unlock_irq(¤t->sighand->siglock);
3240 EXPORT_SYMBOL(kernel_sigaction);
3242 void __weak sigaction_compat_abi(struct k_sigaction *act,
3243 struct k_sigaction *oact)
3247 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
3249 struct task_struct *p = current, *t;
3250 struct k_sigaction *k;
3253 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
3256 k = &p->sighand->action[sig-1];
3258 spin_lock_irq(&p->sighand->siglock);
3262 sigaction_compat_abi(act, oact);
3265 sigdelsetmask(&act->sa.sa_mask,
3266 sigmask(SIGKILL) | sigmask(SIGSTOP));
3270 * "Setting a signal action to SIG_IGN for a signal that is
3271 * pending shall cause the pending signal to be discarded,
3272 * whether or not it is blocked."
3274 * "Setting a signal action to SIG_DFL for a signal that is
3275 * pending and whose default action is to ignore the signal
3276 * (for example, SIGCHLD), shall cause the pending signal to
3277 * be discarded, whether or not it is blocked"
3279 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3281 sigaddset(&mask, sig);
3282 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3283 for_each_thread(p, t)
3284 flush_sigqueue_mask(&mask, &t->pending);
3288 spin_unlock_irq(&p->sighand->siglock);
3293 do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
3296 struct task_struct *t = current;
3299 memset(oss, 0, sizeof(stack_t));
3300 oss->ss_sp = (void __user *) t->sas_ss_sp;
3301 oss->ss_size = t->sas_ss_size;
3302 oss->ss_flags = sas_ss_flags(sp) |
3303 (current->sas_ss_flags & SS_FLAG_BITS);
3307 void __user *ss_sp = ss->ss_sp;
3308 size_t ss_size = ss->ss_size;
3309 unsigned ss_flags = ss->ss_flags;
3312 if (unlikely(on_sig_stack(sp)))
3315 ss_mode = ss_flags & ~SS_FLAG_BITS;
3316 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3320 if (ss_mode == SS_DISABLE) {
3324 if (unlikely(ss_size < min_ss_size))
3328 t->sas_ss_sp = (unsigned long) ss_sp;
3329 t->sas_ss_size = ss_size;
3330 t->sas_ss_flags = ss_flags;
3335 SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3339 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
3341 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
3342 current_user_stack_pointer(),
3344 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
3349 int restore_altstack(const stack_t __user *uss)
3352 if (copy_from_user(&new, uss, sizeof(stack_t)))
3354 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
3356 /* squash all but EFAULT for now */
3360 int __save_altstack(stack_t __user *uss, unsigned long sp)
3362 struct task_struct *t = current;
3363 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3364 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3365 __put_user(t->sas_ss_size, &uss->ss_size);
3368 if (t->sas_ss_flags & SS_AUTODISARM)
3373 #ifdef CONFIG_COMPAT
3374 COMPAT_SYSCALL_DEFINE2(sigaltstack,
3375 const compat_stack_t __user *, uss_ptr,
3376 compat_stack_t __user *, uoss_ptr)
3382 compat_stack_t uss32;
3383 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3385 uss.ss_sp = compat_ptr(uss32.ss_sp);
3386 uss.ss_flags = uss32.ss_flags;
3387 uss.ss_size = uss32.ss_size;
3389 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
3390 compat_user_stack_pointer(),
3391 COMPAT_MINSIGSTKSZ);
3392 if (ret >= 0 && uoss_ptr) {
3394 memset(&old, 0, sizeof(old));
3395 old.ss_sp = ptr_to_compat(uoss.ss_sp);
3396 old.ss_flags = uoss.ss_flags;
3397 old.ss_size = uoss.ss_size;
3398 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
3404 int compat_restore_altstack(const compat_stack_t __user *uss)
3406 int err = compat_sys_sigaltstack(uss, NULL);
3407 /* squash all but -EFAULT for now */
3408 return err == -EFAULT ? err : 0;
3411 int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3414 struct task_struct *t = current;
3415 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3417 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3418 __put_user(t->sas_ss_size, &uss->ss_size);
3421 if (t->sas_ss_flags & SS_AUTODISARM)
3427 #ifdef __ARCH_WANT_SYS_SIGPENDING
3430 * sys_sigpending - examine pending signals
3431 * @set: where mask of pending signal is returned
3433 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
3435 return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
3438 #ifdef CONFIG_COMPAT
3439 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
3443 int err = do_sigpending(&set, sizeof(set.sig[0]));
3445 err = put_user(set.sig[0], set32);
3448 return sys_rt_sigpending((sigset_t __user *)set32, sizeof(*set32));
3455 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
3457 * sys_sigprocmask - examine and change blocked signals
3458 * @how: whether to add, remove, or set signals
3459 * @nset: signals to add or remove (if non-null)
3460 * @oset: previous value of signal mask if non-null
3462 * Some platforms have their own version with special arguments;
3463 * others support only sys_rt_sigprocmask.
3466 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
3467 old_sigset_t __user *, oset)
3469 old_sigset_t old_set, new_set;
3470 sigset_t new_blocked;
3472 old_set = current->blocked.sig[0];
3475 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3478 new_blocked = current->blocked;
3482 sigaddsetmask(&new_blocked, new_set);
3485 sigdelsetmask(&new_blocked, new_set);
3488 new_blocked.sig[0] = new_set;
3494 set_current_blocked(&new_blocked);
3498 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3504 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3506 #ifndef CONFIG_ODD_RT_SIGACTION
3508 * sys_rt_sigaction - alter an action taken by a process
3509 * @sig: signal to be sent
3510 * @act: new sigaction
3511 * @oact: used to save the previous sigaction
3512 * @sigsetsize: size of sigset_t type
3514 SYSCALL_DEFINE4(rt_sigaction, int, sig,
3515 const struct sigaction __user *, act,
3516 struct sigaction __user *, oact,
3519 struct k_sigaction new_sa, old_sa;
3522 /* XXX: Don't preclude handling different sized sigset_t's. */
3523 if (sigsetsize != sizeof(sigset_t))
3527 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3531 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3534 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3540 #ifdef CONFIG_COMPAT
3541 COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3542 const struct compat_sigaction __user *, act,
3543 struct compat_sigaction __user *, oact,
3544 compat_size_t, sigsetsize)
3546 struct k_sigaction new_ka, old_ka;
3547 compat_sigset_t mask;
3548 #ifdef __ARCH_HAS_SA_RESTORER
3549 compat_uptr_t restorer;
3553 /* XXX: Don't preclude handling different sized sigset_t's. */
3554 if (sigsetsize != sizeof(compat_sigset_t))
3558 compat_uptr_t handler;
3559 ret = get_user(handler, &act->sa_handler);
3560 new_ka.sa.sa_handler = compat_ptr(handler);
3561 #ifdef __ARCH_HAS_SA_RESTORER
3562 ret |= get_user(restorer, &act->sa_restorer);
3563 new_ka.sa.sa_restorer = compat_ptr(restorer);
3565 ret |= copy_from_user(&mask, &act->sa_mask, sizeof(mask));
3566 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
3569 sigset_from_compat(&new_ka.sa.sa_mask, &mask);
3572 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3574 sigset_to_compat(&mask, &old_ka.sa.sa_mask);
3575 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
3577 ret |= copy_to_user(&oact->sa_mask, &mask, sizeof(mask));
3578 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
3579 #ifdef __ARCH_HAS_SA_RESTORER
3580 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3581 &oact->sa_restorer);
3587 #endif /* !CONFIG_ODD_RT_SIGACTION */
3589 #ifdef CONFIG_OLD_SIGACTION
3590 SYSCALL_DEFINE3(sigaction, int, sig,
3591 const struct old_sigaction __user *, act,
3592 struct old_sigaction __user *, oact)
3594 struct k_sigaction new_ka, old_ka;
3599 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3600 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3601 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3602 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3603 __get_user(mask, &act->sa_mask))
3605 #ifdef __ARCH_HAS_KA_RESTORER
3606 new_ka.ka_restorer = NULL;
3608 siginitset(&new_ka.sa.sa_mask, mask);
3611 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3614 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3615 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3616 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3617 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3618 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3625 #ifdef CONFIG_COMPAT_OLD_SIGACTION
3626 COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3627 const struct compat_old_sigaction __user *, act,
3628 struct compat_old_sigaction __user *, oact)
3630 struct k_sigaction new_ka, old_ka;
3632 compat_old_sigset_t mask;
3633 compat_uptr_t handler, restorer;
3636 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3637 __get_user(handler, &act->sa_handler) ||
3638 __get_user(restorer, &act->sa_restorer) ||
3639 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3640 __get_user(mask, &act->sa_mask))
3643 #ifdef __ARCH_HAS_KA_RESTORER
3644 new_ka.ka_restorer = NULL;
3646 new_ka.sa.sa_handler = compat_ptr(handler);
3647 new_ka.sa.sa_restorer = compat_ptr(restorer);
3648 siginitset(&new_ka.sa.sa_mask, mask);
3651 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3654 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3655 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3656 &oact->sa_handler) ||
3657 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3658 &oact->sa_restorer) ||
3659 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3660 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3667 #ifdef CONFIG_SGETMASK_SYSCALL
3670 * For backwards compatibility. Functionality superseded by sigprocmask.
3672 SYSCALL_DEFINE0(sgetmask)
3675 return current->blocked.sig[0];
3678 SYSCALL_DEFINE1(ssetmask, int, newmask)
3680 int old = current->blocked.sig[0];
3683 siginitset(&newset, newmask);
3684 set_current_blocked(&newset);
3688 #endif /* CONFIG_SGETMASK_SYSCALL */
3690 #ifdef __ARCH_WANT_SYS_SIGNAL
3692 * For backwards compatibility. Functionality superseded by sigaction.
3694 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
3696 struct k_sigaction new_sa, old_sa;
3699 new_sa.sa.sa_handler = handler;
3700 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
3701 sigemptyset(&new_sa.sa.sa_mask);
3703 ret = do_sigaction(sig, &new_sa, &old_sa);
3705 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3707 #endif /* __ARCH_WANT_SYS_SIGNAL */
3709 #ifdef __ARCH_WANT_SYS_PAUSE
3711 SYSCALL_DEFINE0(pause)
3713 while (!signal_pending(current)) {
3714 __set_current_state(TASK_INTERRUPTIBLE);
3717 return -ERESTARTNOHAND;
3722 static int sigsuspend(sigset_t *set)
3724 current->saved_sigmask = current->blocked;
3725 set_current_blocked(set);
3727 while (!signal_pending(current)) {
3728 __set_current_state(TASK_INTERRUPTIBLE);
3731 set_restore_sigmask();
3732 return -ERESTARTNOHAND;
3736 * sys_rt_sigsuspend - replace the signal mask for a value with the
3737 * @unewset value until a signal is received
3738 * @unewset: new signal mask value
3739 * @sigsetsize: size of sigset_t type
3741 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3745 /* XXX: Don't preclude handling different sized sigset_t's. */
3746 if (sigsetsize != sizeof(sigset_t))
3749 if (copy_from_user(&newset, unewset, sizeof(newset)))
3751 return sigsuspend(&newset);
3754 #ifdef CONFIG_COMPAT
3755 COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
3759 compat_sigset_t newset32;
3761 /* XXX: Don't preclude handling different sized sigset_t's. */
3762 if (sigsetsize != sizeof(sigset_t))
3765 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
3767 sigset_from_compat(&newset, &newset32);
3768 return sigsuspend(&newset);
3770 /* on little-endian bitmaps don't care about granularity */
3771 return sys_rt_sigsuspend((sigset_t __user *)unewset, sigsetsize);
3776 #ifdef CONFIG_OLD_SIGSUSPEND
3777 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
3780 siginitset(&blocked, mask);
3781 return sigsuspend(&blocked);
3784 #ifdef CONFIG_OLD_SIGSUSPEND3
3785 SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
3788 siginitset(&blocked, mask);
3789 return sigsuspend(&blocked);
3793 __weak const char *arch_vma_name(struct vm_area_struct *vma)
3798 void __init signals_init(void)
3800 /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
3801 BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
3802 != offsetof(struct siginfo, _sifields._pad));
3804 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
3807 #ifdef CONFIG_KGDB_KDB
3808 #include <linux/kdb.h>
3810 * kdb_send_sig_info - Allows kdb to send signals without exposing
3811 * signal internals. This function checks if the required locks are
3812 * available before calling the main signal code, to avoid kdb
3816 kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3818 static struct task_struct *kdb_prev_t;
3820 if (!spin_trylock(&t->sighand->siglock)) {
3821 kdb_printf("Can't do kill command now.\n"
3822 "The sigmask lock is held somewhere else in "
3823 "kernel, try again later\n");
3826 spin_unlock(&t->sighand->siglock);
3827 new_t = kdb_prev_t != t;
3829 if (t->state != TASK_RUNNING && new_t) {
3830 kdb_printf("Process is not RUNNING, sending a signal from "
3831 "kdb risks deadlock\n"
3832 "on the run queue locks. "
3833 "The signal has _not_ been sent.\n"
3834 "Reissue the kill command if you want to risk "
3838 sig = info->si_signo;
3839 if (send_sig_info(sig, info, t))
3840 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3843 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3845 #endif /* CONFIG_KGDB_KDB */