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;
1665 WARN_ON_ONCE(sig == -1);
1667 /* do_notify_parent_cldstop should have been called instead. */
1668 WARN_ON_ONCE(task_is_stopped_or_traced(tsk));
1670 WARN_ON_ONCE(!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 * This must be called with current->sighand->siglock held.
1844 * This should be the path for all ptrace stops.
1845 * We always set current->last_siginfo while stopped here.
1846 * That makes it a way to test a stopped process for
1847 * being ptrace-stopped vs being job-control-stopped.
1849 * If we actually decide not to stop at all because the tracer
1850 * is gone, we keep current->exit_code unless clear_code.
1852 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1853 __releases(¤t->sighand->siglock)
1854 __acquires(¤t->sighand->siglock)
1856 bool gstop_done = false;
1858 if (arch_ptrace_stop_needed(exit_code, info)) {
1860 * The arch code has something special to do before a
1861 * ptrace stop. This is allowed to block, e.g. for faults
1862 * on user stack pages. We can't keep the siglock while
1863 * calling arch_ptrace_stop, so we must release it now.
1864 * To preserve proper semantics, we must do this before
1865 * any signal bookkeeping like checking group_stop_count.
1867 spin_unlock_irq(¤t->sighand->siglock);
1868 arch_ptrace_stop(exit_code, info);
1869 spin_lock_irq(¤t->sighand->siglock);
1873 * schedule() will not sleep if there is a pending signal that
1874 * can awaken the task.
1876 set_special_state(TASK_TRACED);
1879 * We're committing to trapping. TRACED should be visible before
1880 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1881 * Also, transition to TRACED and updates to ->jobctl should be
1882 * atomic with respect to siglock and should be done after the arch
1883 * hook as siglock is released and regrabbed across it.
1888 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
1890 * set_current_state() smp_wmb();
1892 * wait_task_stopped()
1893 * task_stopped_code()
1894 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
1898 current->last_siginfo = info;
1899 current->exit_code = exit_code;
1902 * If @why is CLD_STOPPED, we're trapping to participate in a group
1903 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1904 * across siglock relocks since INTERRUPT was scheduled, PENDING
1905 * could be clear now. We act as if SIGCONT is received after
1906 * TASK_TRACED is entered - ignore it.
1908 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
1909 gstop_done = task_participate_group_stop(current);
1911 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
1912 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
1913 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1914 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
1916 /* entering a trap, clear TRAPPING */
1917 task_clear_jobctl_trapping(current);
1919 spin_unlock_irq(¤t->sighand->siglock);
1920 read_lock(&tasklist_lock);
1921 if (may_ptrace_stop()) {
1923 * Notify parents of the stop.
1925 * While ptraced, there are two parents - the ptracer and
1926 * the real_parent of the group_leader. The ptracer should
1927 * know about every stop while the real parent is only
1928 * interested in the completion of group stop. The states
1929 * for the two don't interact with each other. Notify
1930 * separately unless they're gonna be duplicates.
1932 do_notify_parent_cldstop(current, true, why);
1933 if (gstop_done && ptrace_reparented(current))
1934 do_notify_parent_cldstop(current, false, why);
1937 * Don't want to allow preemption here, because
1938 * sys_ptrace() needs this task to be inactive.
1940 * XXX: implement read_unlock_no_resched().
1943 read_unlock(&tasklist_lock);
1944 preempt_enable_no_resched();
1945 freezable_schedule();
1948 * By the time we got the lock, our tracer went away.
1949 * Don't drop the lock yet, another tracer may come.
1951 * If @gstop_done, the ptracer went away between group stop
1952 * completion and here. During detach, it would have set
1953 * JOBCTL_STOP_PENDING on us and we'll re-enter
1954 * TASK_STOPPED in do_signal_stop() on return, so notifying
1955 * the real parent of the group stop completion is enough.
1958 do_notify_parent_cldstop(current, false, why);
1960 /* tasklist protects us from ptrace_freeze_traced() */
1961 __set_current_state(TASK_RUNNING);
1963 current->exit_code = 0;
1964 read_unlock(&tasklist_lock);
1968 * We are back. Now reacquire the siglock before touching
1969 * last_siginfo, so that we are sure to have synchronized with
1970 * any signal-sending on another CPU that wants to examine it.
1972 spin_lock_irq(¤t->sighand->siglock);
1973 current->last_siginfo = NULL;
1975 /* LISTENING can be set only during STOP traps, clear it */
1976 current->jobctl &= ~JOBCTL_LISTENING;
1979 * Queued signals ignored us while we were stopped for tracing.
1980 * So check for any that we should take before resuming user mode.
1981 * This sets TIF_SIGPENDING, but never clears it.
1983 recalc_sigpending_tsk(current);
1986 static void ptrace_do_notify(int signr, int exit_code, int why)
1990 memset(&info, 0, sizeof info);
1991 info.si_signo = signr;
1992 info.si_code = exit_code;
1993 info.si_pid = task_pid_vnr(current);
1994 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1996 /* Let the debugger run. */
1997 ptrace_stop(exit_code, why, 1, &info);
2000 void ptrace_notify(int exit_code)
2002 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
2003 if (unlikely(current->task_works))
2006 spin_lock_irq(¤t->sighand->siglock);
2007 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
2008 spin_unlock_irq(¤t->sighand->siglock);
2012 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2013 * @signr: signr causing group stop if initiating
2015 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2016 * and participate in it. If already set, participate in the existing
2017 * group stop. If participated in a group stop (and thus slept), %true is
2018 * returned with siglock released.
2020 * If ptraced, this function doesn't handle stop itself. Instead,
2021 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2022 * untouched. The caller must ensure that INTERRUPT trap handling takes
2023 * places afterwards.
2026 * Must be called with @current->sighand->siglock held, which is released
2030 * %false if group stop is already cancelled or ptrace trap is scheduled.
2031 * %true if participated in group stop.
2033 static bool do_signal_stop(int signr)
2034 __releases(¤t->sighand->siglock)
2036 struct signal_struct *sig = current->signal;
2038 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
2039 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
2040 struct task_struct *t;
2042 /* signr will be recorded in task->jobctl for retries */
2043 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
2045 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
2046 unlikely(signal_group_exit(sig)))
2049 * There is no group stop already in progress. We must
2052 * While ptraced, a task may be resumed while group stop is
2053 * still in effect and then receive a stop signal and
2054 * initiate another group stop. This deviates from the
2055 * usual behavior as two consecutive stop signals can't
2056 * cause two group stops when !ptraced. That is why we
2057 * also check !task_is_stopped(t) below.
2059 * The condition can be distinguished by testing whether
2060 * SIGNAL_STOP_STOPPED is already set. Don't generate
2061 * group_exit_code in such case.
2063 * This is not necessary for SIGNAL_STOP_CONTINUED because
2064 * an intervening stop signal is required to cause two
2065 * continued events regardless of ptrace.
2067 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2068 sig->group_exit_code = signr;
2070 sig->group_stop_count = 0;
2072 if (task_set_jobctl_pending(current, signr | gstop))
2073 sig->group_stop_count++;
2076 while_each_thread(current, t) {
2078 * Setting state to TASK_STOPPED for a group
2079 * stop is always done with the siglock held,
2080 * so this check has no races.
2082 if (!task_is_stopped(t) &&
2083 task_set_jobctl_pending(t, signr | gstop)) {
2084 sig->group_stop_count++;
2085 if (likely(!(t->ptrace & PT_SEIZED)))
2086 signal_wake_up(t, 0);
2088 ptrace_trap_notify(t);
2093 if (likely(!current->ptrace)) {
2097 * If there are no other threads in the group, or if there
2098 * is a group stop in progress and we are the last to stop,
2099 * report to the parent.
2101 if (task_participate_group_stop(current))
2102 notify = CLD_STOPPED;
2104 set_special_state(TASK_STOPPED);
2105 spin_unlock_irq(¤t->sighand->siglock);
2108 * Notify the parent of the group stop completion. Because
2109 * we're not holding either the siglock or tasklist_lock
2110 * here, ptracer may attach inbetween; however, this is for
2111 * group stop and should always be delivered to the real
2112 * parent of the group leader. The new ptracer will get
2113 * its notification when this task transitions into
2117 read_lock(&tasklist_lock);
2118 do_notify_parent_cldstop(current, false, notify);
2119 read_unlock(&tasklist_lock);
2122 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2123 freezable_schedule();
2127 * While ptraced, group stop is handled by STOP trap.
2128 * Schedule it and let the caller deal with it.
2130 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2136 * do_jobctl_trap - take care of ptrace jobctl traps
2138 * When PT_SEIZED, it's used for both group stop and explicit
2139 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2140 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2141 * the stop signal; otherwise, %SIGTRAP.
2143 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2144 * number as exit_code and no siginfo.
2147 * Must be called with @current->sighand->siglock held, which may be
2148 * released and re-acquired before returning with intervening sleep.
2150 static void do_jobctl_trap(void)
2152 struct signal_struct *signal = current->signal;
2153 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2155 if (current->ptrace & PT_SEIZED) {
2156 if (!signal->group_stop_count &&
2157 !(signal->flags & SIGNAL_STOP_STOPPED))
2159 WARN_ON_ONCE(!signr);
2160 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2163 WARN_ON_ONCE(!signr);
2164 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2165 current->exit_code = 0;
2169 static int ptrace_signal(int signr, siginfo_t *info)
2172 * We do not check sig_kernel_stop(signr) but set this marker
2173 * unconditionally because we do not know whether debugger will
2174 * change signr. This flag has no meaning unless we are going
2175 * to stop after return from ptrace_stop(). In this case it will
2176 * be checked in do_signal_stop(), we should only stop if it was
2177 * not cleared by SIGCONT while we were sleeping. See also the
2178 * comment in dequeue_signal().
2180 current->jobctl |= JOBCTL_STOP_DEQUEUED;
2181 ptrace_stop(signr, CLD_TRAPPED, 0, info);
2183 /* We're back. Did the debugger cancel the sig? */
2184 signr = current->exit_code;
2188 current->exit_code = 0;
2191 * Update the siginfo structure if the signal has
2192 * changed. If the debugger wanted something
2193 * specific in the siginfo structure then it should
2194 * have updated *info via PTRACE_SETSIGINFO.
2196 if (signr != info->si_signo) {
2197 info->si_signo = signr;
2199 info->si_code = SI_USER;
2201 info->si_pid = task_pid_vnr(current->parent);
2202 info->si_uid = from_kuid_munged(current_user_ns(),
2203 task_uid(current->parent));
2207 /* If the (new) signal is now blocked, requeue it. */
2208 if (sigismember(¤t->blocked, signr)) {
2209 specific_send_sig_info(signr, info, current);
2216 int get_signal(struct ksignal *ksig)
2218 struct sighand_struct *sighand = current->sighand;
2219 struct signal_struct *signal = current->signal;
2222 if (unlikely(current->task_works))
2225 if (unlikely(uprobe_deny_signal()))
2229 * Do this once, we can't return to user-mode if freezing() == T.
2230 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2231 * thus do not need another check after return.
2236 spin_lock_irq(&sighand->siglock);
2238 * Every stopped thread goes here after wakeup. Check to see if
2239 * we should notify the parent, prepare_signal(SIGCONT) encodes
2240 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2242 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2245 if (signal->flags & SIGNAL_CLD_CONTINUED)
2246 why = CLD_CONTINUED;
2250 signal->flags &= ~SIGNAL_CLD_MASK;
2252 spin_unlock_irq(&sighand->siglock);
2255 * Notify the parent that we're continuing. This event is
2256 * always per-process and doesn't make whole lot of sense
2257 * for ptracers, who shouldn't consume the state via
2258 * wait(2) either, but, for backward compatibility, notify
2259 * the ptracer of the group leader too unless it's gonna be
2262 read_lock(&tasklist_lock);
2263 do_notify_parent_cldstop(current, false, why);
2265 if (ptrace_reparented(current->group_leader))
2266 do_notify_parent_cldstop(current->group_leader,
2268 read_unlock(&tasklist_lock);
2273 /* Has this task already been marked for death? */
2274 if (signal_group_exit(signal)) {
2275 ksig->info.si_signo = signr = SIGKILL;
2276 sigdelset(¤t->pending.signal, SIGKILL);
2277 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2278 &sighand->action[SIGKILL - 1]);
2279 recalc_sigpending();
2284 struct k_sigaction *ka;
2286 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2290 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2292 spin_unlock_irq(&sighand->siglock);
2297 * Signals generated by the execution of an instruction
2298 * need to be delivered before any other pending signals
2299 * so that the instruction pointer in the signal stack
2300 * frame points to the faulting instruction.
2302 signr = dequeue_synchronous_signal(&ksig->info);
2304 signr = dequeue_signal(current, ¤t->blocked, &ksig->info);
2307 break; /* will return 0 */
2309 if (unlikely(current->ptrace) && signr != SIGKILL) {
2310 signr = ptrace_signal(signr, &ksig->info);
2315 ka = &sighand->action[signr-1];
2317 /* Trace actually delivered signals. */
2318 trace_signal_deliver(signr, &ksig->info, ka);
2320 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2322 if (ka->sa.sa_handler != SIG_DFL) {
2323 /* Run the handler. */
2326 if (ka->sa.sa_flags & SA_ONESHOT)
2327 ka->sa.sa_handler = SIG_DFL;
2329 break; /* will return non-zero "signr" value */
2333 * Now we are doing the default action for this signal.
2335 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2339 * Global init gets no signals it doesn't want.
2340 * Container-init gets no signals it doesn't want from same
2343 * Note that if global/container-init sees a sig_kernel_only()
2344 * signal here, the signal must have been generated internally
2345 * or must have come from an ancestor namespace. In either
2346 * case, the signal cannot be dropped.
2348 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2349 !sig_kernel_only(signr))
2352 if (sig_kernel_stop(signr)) {
2354 * The default action is to stop all threads in
2355 * the thread group. The job control signals
2356 * do nothing in an orphaned pgrp, but SIGSTOP
2357 * always works. Note that siglock needs to be
2358 * dropped during the call to is_orphaned_pgrp()
2359 * because of lock ordering with tasklist_lock.
2360 * This allows an intervening SIGCONT to be posted.
2361 * We need to check for that and bail out if necessary.
2363 if (signr != SIGSTOP) {
2364 spin_unlock_irq(&sighand->siglock);
2366 /* signals can be posted during this window */
2368 if (is_current_pgrp_orphaned())
2371 spin_lock_irq(&sighand->siglock);
2374 if (likely(do_signal_stop(ksig->info.si_signo))) {
2375 /* It released the siglock. */
2380 * We didn't actually stop, due to a race
2381 * with SIGCONT or something like that.
2387 spin_unlock_irq(&sighand->siglock);
2390 * Anything else is fatal, maybe with a core dump.
2392 current->flags |= PF_SIGNALED;
2394 if (sig_kernel_coredump(signr)) {
2395 if (print_fatal_signals)
2396 print_fatal_signal(ksig->info.si_signo);
2397 proc_coredump_connector(current);
2399 * If it was able to dump core, this kills all
2400 * other threads in the group and synchronizes with
2401 * their demise. If we lost the race with another
2402 * thread getting here, it set group_exit_code
2403 * first and our do_group_exit call below will use
2404 * that value and ignore the one we pass it.
2406 do_coredump(&ksig->info);
2410 * Death signals, no core dump.
2412 do_group_exit(ksig->info.si_signo);
2415 spin_unlock_irq(&sighand->siglock);
2418 return ksig->sig > 0;
2422 * signal_delivered -
2423 * @ksig: kernel signal struct
2424 * @stepping: nonzero if debugger single-step or block-step in use
2426 * This function should be called when a signal has successfully been
2427 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2428 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2429 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
2431 static void signal_delivered(struct ksignal *ksig, int stepping)
2435 /* A signal was successfully delivered, and the
2436 saved sigmask was stored on the signal frame,
2437 and will be restored by sigreturn. So we can
2438 simply clear the restore sigmask flag. */
2439 clear_restore_sigmask();
2441 sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
2442 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2443 sigaddset(&blocked, ksig->sig);
2444 set_current_blocked(&blocked);
2445 tracehook_signal_handler(stepping);
2448 void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2451 force_sigsegv(ksig->sig, current);
2453 signal_delivered(ksig, stepping);
2457 * It could be that complete_signal() picked us to notify about the
2458 * group-wide signal. Other threads should be notified now to take
2459 * the shared signals in @which since we will not.
2461 static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2464 struct task_struct *t;
2466 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2467 if (sigisemptyset(&retarget))
2471 while_each_thread(tsk, t) {
2472 if (t->flags & PF_EXITING)
2475 if (!has_pending_signals(&retarget, &t->blocked))
2477 /* Remove the signals this thread can handle. */
2478 sigandsets(&retarget, &retarget, &t->blocked);
2480 if (!signal_pending(t))
2481 signal_wake_up(t, 0);
2483 if (sigisemptyset(&retarget))
2488 void exit_signals(struct task_struct *tsk)
2494 * @tsk is about to have PF_EXITING set - lock out users which
2495 * expect stable threadgroup.
2497 cgroup_threadgroup_change_begin(tsk);
2499 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2500 tsk->flags |= PF_EXITING;
2501 cgroup_threadgroup_change_end(tsk);
2505 spin_lock_irq(&tsk->sighand->siglock);
2507 * From now this task is not visible for group-wide signals,
2508 * see wants_signal(), do_signal_stop().
2510 tsk->flags |= PF_EXITING;
2512 cgroup_threadgroup_change_end(tsk);
2514 if (!signal_pending(tsk))
2517 unblocked = tsk->blocked;
2518 signotset(&unblocked);
2519 retarget_shared_pending(tsk, &unblocked);
2521 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2522 task_participate_group_stop(tsk))
2523 group_stop = CLD_STOPPED;
2525 spin_unlock_irq(&tsk->sighand->siglock);
2528 * If group stop has completed, deliver the notification. This
2529 * should always go to the real parent of the group leader.
2531 if (unlikely(group_stop)) {
2532 read_lock(&tasklist_lock);
2533 do_notify_parent_cldstop(tsk, false, group_stop);
2534 read_unlock(&tasklist_lock);
2538 EXPORT_SYMBOL(recalc_sigpending);
2539 EXPORT_SYMBOL_GPL(dequeue_signal);
2540 EXPORT_SYMBOL(flush_signals);
2541 EXPORT_SYMBOL(force_sig);
2542 EXPORT_SYMBOL(send_sig);
2543 EXPORT_SYMBOL(send_sig_info);
2544 EXPORT_SYMBOL(sigprocmask);
2547 * System call entry points.
2551 * sys_restart_syscall - restart a system call
2553 SYSCALL_DEFINE0(restart_syscall)
2555 struct restart_block *restart = ¤t->restart_block;
2556 return restart->fn(restart);
2559 long do_no_restart_syscall(struct restart_block *param)
2564 static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2566 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2567 sigset_t newblocked;
2568 /* A set of now blocked but previously unblocked signals. */
2569 sigandnsets(&newblocked, newset, ¤t->blocked);
2570 retarget_shared_pending(tsk, &newblocked);
2572 tsk->blocked = *newset;
2573 recalc_sigpending();
2577 * set_current_blocked - change current->blocked mask
2580 * It is wrong to change ->blocked directly, this helper should be used
2581 * to ensure the process can't miss a shared signal we are going to block.
2583 void set_current_blocked(sigset_t *newset)
2585 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2586 __set_current_blocked(newset);
2589 void __set_current_blocked(const sigset_t *newset)
2591 struct task_struct *tsk = current;
2594 * In case the signal mask hasn't changed, there is nothing we need
2595 * to do. The current->blocked shouldn't be modified by other task.
2597 if (sigequalsets(&tsk->blocked, newset))
2600 spin_lock_irq(&tsk->sighand->siglock);
2601 __set_task_blocked(tsk, newset);
2602 spin_unlock_irq(&tsk->sighand->siglock);
2606 * This is also useful for kernel threads that want to temporarily
2607 * (or permanently) block certain signals.
2609 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2610 * interface happily blocks "unblockable" signals like SIGKILL
2613 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2615 struct task_struct *tsk = current;
2618 /* Lockless, only current can change ->blocked, never from irq */
2620 *oldset = tsk->blocked;
2624 sigorsets(&newset, &tsk->blocked, set);
2627 sigandnsets(&newset, &tsk->blocked, set);
2636 __set_current_blocked(&newset);
2641 * sys_rt_sigprocmask - change the list of currently blocked signals
2642 * @how: whether to add, remove, or set signals
2643 * @nset: stores pending signals
2644 * @oset: previous value of signal mask if non-null
2645 * @sigsetsize: size of sigset_t type
2647 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2648 sigset_t __user *, oset, size_t, sigsetsize)
2650 sigset_t old_set, new_set;
2653 /* XXX: Don't preclude handling different sized sigset_t's. */
2654 if (sigsetsize != sizeof(sigset_t))
2657 old_set = current->blocked;
2660 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2662 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2664 error = sigprocmask(how, &new_set, NULL);
2670 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2677 #ifdef CONFIG_COMPAT
2678 COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2679 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
2682 sigset_t old_set = current->blocked;
2684 /* XXX: Don't preclude handling different sized sigset_t's. */
2685 if (sigsetsize != sizeof(sigset_t))
2689 compat_sigset_t new32;
2692 if (copy_from_user(&new32, nset, sizeof(compat_sigset_t)))
2695 sigset_from_compat(&new_set, &new32);
2696 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2698 error = sigprocmask(how, &new_set, NULL);
2703 compat_sigset_t old32;
2704 sigset_to_compat(&old32, &old_set);
2705 if (copy_to_user(oset, &old32, sizeof(compat_sigset_t)))
2710 return sys_rt_sigprocmask(how, (sigset_t __user *)nset,
2711 (sigset_t __user *)oset, sigsetsize);
2716 static int do_sigpending(void *set, unsigned long sigsetsize)
2718 if (sigsetsize > sizeof(sigset_t))
2721 spin_lock_irq(¤t->sighand->siglock);
2722 sigorsets(set, ¤t->pending.signal,
2723 ¤t->signal->shared_pending.signal);
2724 spin_unlock_irq(¤t->sighand->siglock);
2726 /* Outside the lock because only this thread touches it. */
2727 sigandsets(set, ¤t->blocked, set);
2732 * sys_rt_sigpending - examine a pending signal that has been raised
2734 * @uset: stores pending signals
2735 * @sigsetsize: size of sigset_t type or larger
2737 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
2740 int err = do_sigpending(&set, sigsetsize);
2741 if (!err && copy_to_user(uset, &set, sigsetsize))
2746 #ifdef CONFIG_COMPAT
2747 COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2748 compat_size_t, sigsetsize)
2752 int err = do_sigpending(&set, sigsetsize);
2754 compat_sigset_t set32;
2755 sigset_to_compat(&set32, &set);
2756 /* we can get here only if sigsetsize <= sizeof(set) */
2757 if (copy_to_user(uset, &set32, sigsetsize))
2762 return sys_rt_sigpending((sigset_t __user *)uset, sigsetsize);
2767 enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
2769 enum siginfo_layout layout = SIL_KILL;
2770 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
2771 static const struct {
2772 unsigned char limit, layout;
2774 [SIGILL] = { NSIGILL, SIL_FAULT },
2775 [SIGFPE] = { NSIGFPE, SIL_FAULT },
2776 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
2777 [SIGBUS] = { NSIGBUS, SIL_FAULT },
2778 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
2779 #if defined(SIGEMT) && defined(NSIGEMT)
2780 [SIGEMT] = { NSIGEMT, SIL_FAULT },
2782 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
2783 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
2784 #ifdef __ARCH_SIGSYS
2785 [SIGSYS] = { NSIGSYS, SIL_SYS },
2788 if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit))
2789 layout = filter[sig].layout;
2790 else if (si_code <= NSIGPOLL)
2793 if (si_code == SI_TIMER)
2795 else if (si_code == SI_SIGIO)
2797 else if (si_code < 0)
2799 /* Tests to support buggy kernel ABIs */
2801 if ((sig == SIGTRAP) && (si_code == TRAP_FIXME))
2805 if ((sig == SIGFPE) && (si_code == FPE_FIXME))
2812 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2814 int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
2818 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2820 if (from->si_code < 0)
2821 return __copy_to_user(to, from, sizeof(siginfo_t))
2824 * If you change siginfo_t structure, please be sure
2825 * this code is fixed accordingly.
2826 * Please remember to update the signalfd_copyinfo() function
2827 * inside fs/signalfd.c too, in case siginfo_t changes.
2828 * It should never copy any pad contained in the structure
2829 * to avoid security leaks, but must copy the generic
2830 * 3 ints plus the relevant union member.
2832 err = __put_user(from->si_signo, &to->si_signo);
2833 err |= __put_user(from->si_errno, &to->si_errno);
2834 err |= __put_user(from->si_code, &to->si_code);
2835 switch (siginfo_layout(from->si_signo, from->si_code)) {
2837 err |= __put_user(from->si_pid, &to->si_pid);
2838 err |= __put_user(from->si_uid, &to->si_uid);
2841 /* Unreached SI_TIMER is negative */
2844 err |= __put_user(from->si_band, &to->si_band);
2845 err |= __put_user(from->si_fd, &to->si_fd);
2848 err |= __put_user(from->si_addr, &to->si_addr);
2849 #ifdef __ARCH_SI_TRAPNO
2850 err |= __put_user(from->si_trapno, &to->si_trapno);
2852 #ifdef BUS_MCEERR_AO
2854 * Other callers might not initialize the si_lsb field,
2855 * so check explicitly for the right codes here.
2857 if (from->si_signo == SIGBUS &&
2858 (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
2859 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2862 if (from->si_signo == SIGSEGV && from->si_code == SEGV_BNDERR) {
2863 err |= __put_user(from->si_lower, &to->si_lower);
2864 err |= __put_user(from->si_upper, &to->si_upper);
2868 if (from->si_signo == SIGSEGV && from->si_code == SEGV_PKUERR)
2869 err |= __put_user(from->si_pkey, &to->si_pkey);
2873 err |= __put_user(from->si_pid, &to->si_pid);
2874 err |= __put_user(from->si_uid, &to->si_uid);
2875 err |= __put_user(from->si_status, &to->si_status);
2876 err |= __put_user(from->si_utime, &to->si_utime);
2877 err |= __put_user(from->si_stime, &to->si_stime);
2880 err |= __put_user(from->si_pid, &to->si_pid);
2881 err |= __put_user(from->si_uid, &to->si_uid);
2882 err |= __put_user(from->si_ptr, &to->si_ptr);
2884 #ifdef __ARCH_SIGSYS
2886 err |= __put_user(from->si_call_addr, &to->si_call_addr);
2887 err |= __put_user(from->si_syscall, &to->si_syscall);
2888 err |= __put_user(from->si_arch, &to->si_arch);
2898 * do_sigtimedwait - wait for queued signals specified in @which
2899 * @which: queued signals to wait for
2900 * @info: if non-null, the signal's siginfo is returned here
2901 * @ts: upper bound on process time suspension
2903 static int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2904 const struct timespec *ts)
2906 ktime_t *to = NULL, timeout = KTIME_MAX;
2907 struct task_struct *tsk = current;
2908 sigset_t mask = *which;
2912 if (!timespec_valid(ts))
2914 timeout = timespec_to_ktime(*ts);
2919 * Invert the set of allowed signals to get those we want to block.
2921 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2924 spin_lock_irq(&tsk->sighand->siglock);
2925 sig = dequeue_signal(tsk, &mask, info);
2926 if (!sig && timeout) {
2928 * None ready, temporarily unblock those we're interested
2929 * while we are sleeping in so that we'll be awakened when
2930 * they arrive. Unblocking is always fine, we can avoid
2931 * set_current_blocked().
2933 tsk->real_blocked = tsk->blocked;
2934 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2935 recalc_sigpending();
2936 spin_unlock_irq(&tsk->sighand->siglock);
2938 __set_current_state(TASK_INTERRUPTIBLE);
2939 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
2941 spin_lock_irq(&tsk->sighand->siglock);
2942 __set_task_blocked(tsk, &tsk->real_blocked);
2943 sigemptyset(&tsk->real_blocked);
2944 sig = dequeue_signal(tsk, &mask, info);
2946 spin_unlock_irq(&tsk->sighand->siglock);
2950 return ret ? -EINTR : -EAGAIN;
2954 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2956 * @uthese: queued signals to wait for
2957 * @uinfo: if non-null, the signal's siginfo is returned here
2958 * @uts: upper bound on process time suspension
2959 * @sigsetsize: size of sigset_t type
2961 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2962 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2970 /* XXX: Don't preclude handling different sized sigset_t's. */
2971 if (sigsetsize != sizeof(sigset_t))
2974 if (copy_from_user(&these, uthese, sizeof(these)))
2978 if (copy_from_user(&ts, uts, sizeof(ts)))
2982 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
2984 if (ret > 0 && uinfo) {
2985 if (copy_siginfo_to_user(uinfo, &info))
2992 #ifdef CONFIG_COMPAT
2993 COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese,
2994 struct compat_siginfo __user *, uinfo,
2995 struct compat_timespec __user *, uts, compat_size_t, sigsetsize)
2997 compat_sigset_t s32;
3003 if (sigsetsize != sizeof(sigset_t))
3006 if (copy_from_user(&s32, uthese, sizeof(compat_sigset_t)))
3008 sigset_from_compat(&s, &s32);
3011 if (compat_get_timespec(&t, uts))
3015 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3017 if (ret > 0 && uinfo) {
3018 if (copy_siginfo_to_user32(uinfo, &info))
3027 * sys_kill - send a signal to a process
3028 * @pid: the PID of the process
3029 * @sig: signal to be sent
3031 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
3033 struct siginfo info;
3035 info.si_signo = sig;
3037 info.si_code = SI_USER;
3038 info.si_pid = task_tgid_vnr(current);
3039 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3041 return kill_something_info(sig, &info, pid);
3045 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
3047 struct task_struct *p;
3051 p = find_task_by_vpid(pid);
3052 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
3053 error = check_kill_permission(sig, info, p);
3055 * The null signal is a permissions and process existence
3056 * probe. No signal is actually delivered.
3058 if (!error && sig) {
3059 error = do_send_sig_info(sig, info, p, false);
3061 * If lock_task_sighand() failed we pretend the task
3062 * dies after receiving the signal. The window is tiny,
3063 * and the signal is private anyway.
3065 if (unlikely(error == -ESRCH))
3074 static int do_tkill(pid_t tgid, pid_t pid, int sig)
3076 struct siginfo info = {};
3078 info.si_signo = sig;
3080 info.si_code = SI_TKILL;
3081 info.si_pid = task_tgid_vnr(current);
3082 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
3084 return do_send_specific(tgid, pid, sig, &info);
3088 * sys_tgkill - send signal to one specific thread
3089 * @tgid: the thread group ID of the thread
3090 * @pid: the PID of the thread
3091 * @sig: signal to be sent
3093 * This syscall also checks the @tgid and returns -ESRCH even if the PID
3094 * exists but it's not belonging to the target process anymore. This
3095 * method solves the problem of threads exiting and PIDs getting reused.
3097 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
3099 /* This is only valid for single tasks */
3100 if (pid <= 0 || tgid <= 0)
3103 return do_tkill(tgid, pid, sig);
3107 * sys_tkill - send signal to one specific task
3108 * @pid: the PID of the task
3109 * @sig: signal to be sent
3111 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3113 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
3115 /* This is only valid for single tasks */
3119 return do_tkill(0, pid, sig);
3122 static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
3124 /* Not even root can pretend to send signals from the kernel.
3125 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3127 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3128 (task_pid_vnr(current) != pid))
3131 info->si_signo = sig;
3133 /* POSIX.1b doesn't mention process groups. */
3134 return kill_proc_info(sig, info, pid);
3138 * sys_rt_sigqueueinfo - send signal information to a signal
3139 * @pid: the PID of the thread
3140 * @sig: signal to be sent
3141 * @uinfo: signal info to be sent
3143 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3144 siginfo_t __user *, uinfo)
3147 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3149 return do_rt_sigqueueinfo(pid, sig, &info);
3152 #ifdef CONFIG_COMPAT
3153 COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3156 struct compat_siginfo __user *, uinfo)
3158 siginfo_t info = {};
3159 int ret = copy_siginfo_from_user32(&info, uinfo);
3162 return do_rt_sigqueueinfo(pid, sig, &info);
3166 static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
3168 /* This is only valid for single tasks */
3169 if (pid <= 0 || tgid <= 0)
3172 /* Not even root can pretend to send signals from the kernel.
3173 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3175 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3176 (task_pid_vnr(current) != pid))
3179 info->si_signo = sig;
3181 return do_send_specific(tgid, pid, sig, info);
3184 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3185 siginfo_t __user *, uinfo)
3189 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3192 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3195 #ifdef CONFIG_COMPAT
3196 COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3200 struct compat_siginfo __user *, uinfo)
3202 siginfo_t info = {};
3204 if (copy_siginfo_from_user32(&info, uinfo))
3206 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3211 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3213 void kernel_sigaction(int sig, __sighandler_t action)
3215 spin_lock_irq(¤t->sighand->siglock);
3216 current->sighand->action[sig - 1].sa.sa_handler = action;
3217 if (action == SIG_IGN) {
3221 sigaddset(&mask, sig);
3223 flush_sigqueue_mask(&mask, ¤t->signal->shared_pending);
3224 flush_sigqueue_mask(&mask, ¤t->pending);
3225 recalc_sigpending();
3227 spin_unlock_irq(¤t->sighand->siglock);
3229 EXPORT_SYMBOL(kernel_sigaction);
3231 void __weak sigaction_compat_abi(struct k_sigaction *act,
3232 struct k_sigaction *oact)
3236 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
3238 struct task_struct *p = current, *t;
3239 struct k_sigaction *k;
3242 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
3245 k = &p->sighand->action[sig-1];
3247 spin_lock_irq(&p->sighand->siglock);
3251 sigaction_compat_abi(act, oact);
3254 sigdelsetmask(&act->sa.sa_mask,
3255 sigmask(SIGKILL) | sigmask(SIGSTOP));
3259 * "Setting a signal action to SIG_IGN for a signal that is
3260 * pending shall cause the pending signal to be discarded,
3261 * whether or not it is blocked."
3263 * "Setting a signal action to SIG_DFL for a signal that is
3264 * pending and whose default action is to ignore the signal
3265 * (for example, SIGCHLD), shall cause the pending signal to
3266 * be discarded, whether or not it is blocked"
3268 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3270 sigaddset(&mask, sig);
3271 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3272 for_each_thread(p, t)
3273 flush_sigqueue_mask(&mask, &t->pending);
3277 spin_unlock_irq(&p->sighand->siglock);
3282 do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
3285 struct task_struct *t = current;
3288 memset(oss, 0, sizeof(stack_t));
3289 oss->ss_sp = (void __user *) t->sas_ss_sp;
3290 oss->ss_size = t->sas_ss_size;
3291 oss->ss_flags = sas_ss_flags(sp) |
3292 (current->sas_ss_flags & SS_FLAG_BITS);
3296 void __user *ss_sp = ss->ss_sp;
3297 size_t ss_size = ss->ss_size;
3298 unsigned ss_flags = ss->ss_flags;
3301 if (unlikely(on_sig_stack(sp)))
3304 ss_mode = ss_flags & ~SS_FLAG_BITS;
3305 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3309 if (ss_mode == SS_DISABLE) {
3313 if (unlikely(ss_size < min_ss_size))
3317 t->sas_ss_sp = (unsigned long) ss_sp;
3318 t->sas_ss_size = ss_size;
3319 t->sas_ss_flags = ss_flags;
3324 SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3328 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
3330 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
3331 current_user_stack_pointer(),
3333 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
3338 int restore_altstack(const stack_t __user *uss)
3341 if (copy_from_user(&new, uss, sizeof(stack_t)))
3343 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
3345 /* squash all but EFAULT for now */
3349 int __save_altstack(stack_t __user *uss, unsigned long sp)
3351 struct task_struct *t = current;
3352 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3353 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3354 __put_user(t->sas_ss_size, &uss->ss_size);
3357 if (t->sas_ss_flags & SS_AUTODISARM)
3362 #ifdef CONFIG_COMPAT
3363 COMPAT_SYSCALL_DEFINE2(sigaltstack,
3364 const compat_stack_t __user *, uss_ptr,
3365 compat_stack_t __user *, uoss_ptr)
3371 compat_stack_t uss32;
3372 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3374 uss.ss_sp = compat_ptr(uss32.ss_sp);
3375 uss.ss_flags = uss32.ss_flags;
3376 uss.ss_size = uss32.ss_size;
3378 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
3379 compat_user_stack_pointer(),
3380 COMPAT_MINSIGSTKSZ);
3381 if (ret >= 0 && uoss_ptr) {
3383 memset(&old, 0, sizeof(old));
3384 old.ss_sp = ptr_to_compat(uoss.ss_sp);
3385 old.ss_flags = uoss.ss_flags;
3386 old.ss_size = uoss.ss_size;
3387 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
3393 int compat_restore_altstack(const compat_stack_t __user *uss)
3395 int err = compat_sys_sigaltstack(uss, NULL);
3396 /* squash all but -EFAULT for now */
3397 return err == -EFAULT ? err : 0;
3400 int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3403 struct task_struct *t = current;
3404 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3406 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3407 __put_user(t->sas_ss_size, &uss->ss_size);
3410 if (t->sas_ss_flags & SS_AUTODISARM)
3416 #ifdef __ARCH_WANT_SYS_SIGPENDING
3419 * sys_sigpending - examine pending signals
3420 * @set: where mask of pending signal is returned
3422 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
3424 return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
3427 #ifdef CONFIG_COMPAT
3428 COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
3432 int err = do_sigpending(&set, sizeof(set.sig[0]));
3434 err = put_user(set.sig[0], set32);
3437 return sys_rt_sigpending((sigset_t __user *)set32, sizeof(*set32));
3444 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
3446 * sys_sigprocmask - examine and change blocked signals
3447 * @how: whether to add, remove, or set signals
3448 * @nset: signals to add or remove (if non-null)
3449 * @oset: previous value of signal mask if non-null
3451 * Some platforms have their own version with special arguments;
3452 * others support only sys_rt_sigprocmask.
3455 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
3456 old_sigset_t __user *, oset)
3458 old_sigset_t old_set, new_set;
3459 sigset_t new_blocked;
3461 old_set = current->blocked.sig[0];
3464 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3467 new_blocked = current->blocked;
3471 sigaddsetmask(&new_blocked, new_set);
3474 sigdelsetmask(&new_blocked, new_set);
3477 new_blocked.sig[0] = new_set;
3483 set_current_blocked(&new_blocked);
3487 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3493 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3495 #ifndef CONFIG_ODD_RT_SIGACTION
3497 * sys_rt_sigaction - alter an action taken by a process
3498 * @sig: signal to be sent
3499 * @act: new sigaction
3500 * @oact: used to save the previous sigaction
3501 * @sigsetsize: size of sigset_t type
3503 SYSCALL_DEFINE4(rt_sigaction, int, sig,
3504 const struct sigaction __user *, act,
3505 struct sigaction __user *, oact,
3508 struct k_sigaction new_sa, old_sa;
3511 /* XXX: Don't preclude handling different sized sigset_t's. */
3512 if (sigsetsize != sizeof(sigset_t))
3516 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3520 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3523 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3529 #ifdef CONFIG_COMPAT
3530 COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3531 const struct compat_sigaction __user *, act,
3532 struct compat_sigaction __user *, oact,
3533 compat_size_t, sigsetsize)
3535 struct k_sigaction new_ka, old_ka;
3536 compat_sigset_t mask;
3537 #ifdef __ARCH_HAS_SA_RESTORER
3538 compat_uptr_t restorer;
3542 /* XXX: Don't preclude handling different sized sigset_t's. */
3543 if (sigsetsize != sizeof(compat_sigset_t))
3547 compat_uptr_t handler;
3548 ret = get_user(handler, &act->sa_handler);
3549 new_ka.sa.sa_handler = compat_ptr(handler);
3550 #ifdef __ARCH_HAS_SA_RESTORER
3551 ret |= get_user(restorer, &act->sa_restorer);
3552 new_ka.sa.sa_restorer = compat_ptr(restorer);
3554 ret |= copy_from_user(&mask, &act->sa_mask, sizeof(mask));
3555 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
3558 sigset_from_compat(&new_ka.sa.sa_mask, &mask);
3561 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3563 sigset_to_compat(&mask, &old_ka.sa.sa_mask);
3564 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
3566 ret |= copy_to_user(&oact->sa_mask, &mask, sizeof(mask));
3567 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
3568 #ifdef __ARCH_HAS_SA_RESTORER
3569 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3570 &oact->sa_restorer);
3576 #endif /* !CONFIG_ODD_RT_SIGACTION */
3578 #ifdef CONFIG_OLD_SIGACTION
3579 SYSCALL_DEFINE3(sigaction, int, sig,
3580 const struct old_sigaction __user *, act,
3581 struct old_sigaction __user *, oact)
3583 struct k_sigaction new_ka, old_ka;
3588 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3589 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3590 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3591 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3592 __get_user(mask, &act->sa_mask))
3594 #ifdef __ARCH_HAS_KA_RESTORER
3595 new_ka.ka_restorer = NULL;
3597 siginitset(&new_ka.sa.sa_mask, mask);
3600 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3603 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3604 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3605 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3606 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3607 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3614 #ifdef CONFIG_COMPAT_OLD_SIGACTION
3615 COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3616 const struct compat_old_sigaction __user *, act,
3617 struct compat_old_sigaction __user *, oact)
3619 struct k_sigaction new_ka, old_ka;
3621 compat_old_sigset_t mask;
3622 compat_uptr_t handler, restorer;
3625 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3626 __get_user(handler, &act->sa_handler) ||
3627 __get_user(restorer, &act->sa_restorer) ||
3628 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3629 __get_user(mask, &act->sa_mask))
3632 #ifdef __ARCH_HAS_KA_RESTORER
3633 new_ka.ka_restorer = NULL;
3635 new_ka.sa.sa_handler = compat_ptr(handler);
3636 new_ka.sa.sa_restorer = compat_ptr(restorer);
3637 siginitset(&new_ka.sa.sa_mask, mask);
3640 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3643 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3644 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3645 &oact->sa_handler) ||
3646 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3647 &oact->sa_restorer) ||
3648 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3649 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3656 #ifdef CONFIG_SGETMASK_SYSCALL
3659 * For backwards compatibility. Functionality superseded by sigprocmask.
3661 SYSCALL_DEFINE0(sgetmask)
3664 return current->blocked.sig[0];
3667 SYSCALL_DEFINE1(ssetmask, int, newmask)
3669 int old = current->blocked.sig[0];
3672 siginitset(&newset, newmask);
3673 set_current_blocked(&newset);
3677 #endif /* CONFIG_SGETMASK_SYSCALL */
3679 #ifdef __ARCH_WANT_SYS_SIGNAL
3681 * For backwards compatibility. Functionality superseded by sigaction.
3683 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
3685 struct k_sigaction new_sa, old_sa;
3688 new_sa.sa.sa_handler = handler;
3689 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
3690 sigemptyset(&new_sa.sa.sa_mask);
3692 ret = do_sigaction(sig, &new_sa, &old_sa);
3694 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3696 #endif /* __ARCH_WANT_SYS_SIGNAL */
3698 #ifdef __ARCH_WANT_SYS_PAUSE
3700 SYSCALL_DEFINE0(pause)
3702 while (!signal_pending(current)) {
3703 __set_current_state(TASK_INTERRUPTIBLE);
3706 return -ERESTARTNOHAND;
3711 static int sigsuspend(sigset_t *set)
3713 current->saved_sigmask = current->blocked;
3714 set_current_blocked(set);
3716 while (!signal_pending(current)) {
3717 __set_current_state(TASK_INTERRUPTIBLE);
3720 set_restore_sigmask();
3721 return -ERESTARTNOHAND;
3725 * sys_rt_sigsuspend - replace the signal mask for a value with the
3726 * @unewset value until a signal is received
3727 * @unewset: new signal mask value
3728 * @sigsetsize: size of sigset_t type
3730 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3734 /* XXX: Don't preclude handling different sized sigset_t's. */
3735 if (sigsetsize != sizeof(sigset_t))
3738 if (copy_from_user(&newset, unewset, sizeof(newset)))
3740 return sigsuspend(&newset);
3743 #ifdef CONFIG_COMPAT
3744 COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
3748 compat_sigset_t newset32;
3750 /* XXX: Don't preclude handling different sized sigset_t's. */
3751 if (sigsetsize != sizeof(sigset_t))
3754 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
3756 sigset_from_compat(&newset, &newset32);
3757 return sigsuspend(&newset);
3759 /* on little-endian bitmaps don't care about granularity */
3760 return sys_rt_sigsuspend((sigset_t __user *)unewset, sigsetsize);
3765 #ifdef CONFIG_OLD_SIGSUSPEND
3766 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
3769 siginitset(&blocked, mask);
3770 return sigsuspend(&blocked);
3773 #ifdef CONFIG_OLD_SIGSUSPEND3
3774 SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
3777 siginitset(&blocked, mask);
3778 return sigsuspend(&blocked);
3782 __weak const char *arch_vma_name(struct vm_area_struct *vma)
3787 void __init signals_init(void)
3789 /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
3790 BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
3791 != offsetof(struct siginfo, _sifields._pad));
3793 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
3796 #ifdef CONFIG_KGDB_KDB
3797 #include <linux/kdb.h>
3799 * kdb_send_sig_info - Allows kdb to send signals without exposing
3800 * signal internals. This function checks if the required locks are
3801 * available before calling the main signal code, to avoid kdb
3805 kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3807 static struct task_struct *kdb_prev_t;
3809 if (!spin_trylock(&t->sighand->siglock)) {
3810 kdb_printf("Can't do kill command now.\n"
3811 "The sigmask lock is held somewhere else in "
3812 "kernel, try again later\n");
3815 spin_unlock(&t->sighand->siglock);
3816 new_t = kdb_prev_t != t;
3818 if (t->state != TASK_RUNNING && new_t) {
3819 kdb_printf("Process is not RUNNING, sending a signal from "
3820 "kdb risks deadlock\n"
3821 "on the run queue locks. "
3822 "The signal has _not_ been sent.\n"
3823 "Reissue the kill command if you want to risk "
3827 sig = info->si_signo;
3828 if (send_sig_info(sig, info, t))
3829 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3832 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3834 #endif /* CONFIG_KGDB_KDB */