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>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/signal.h>
46 #include <asm/param.h>
47 #include <linux/uaccess.h>
48 #include <asm/unistd.h>
49 #include <asm/siginfo.h>
50 #include <asm/cacheflush.h>
51 #include "audit.h" /* audit_signal_info() */
54 * SLAB caches for signal bits.
57 static struct kmem_cache *sigqueue_cachep;
59 int print_fatal_signals __read_mostly;
61 static void __user *sig_handler(struct task_struct *t, int sig)
63 return t->sighand->action[sig - 1].sa.sa_handler;
66 static int sig_handler_ignored(void __user *handler, int sig)
68 /* Is it explicitly or implicitly ignored? */
69 return handler == SIG_IGN ||
70 (handler == SIG_DFL && sig_kernel_ignore(sig));
73 static int sig_task_ignored(struct task_struct *t, int sig, bool force)
77 handler = sig_handler(t, sig);
79 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
80 handler == SIG_DFL && !force)
83 return sig_handler_ignored(handler, sig);
86 static int sig_ignored(struct task_struct *t, int sig, bool force)
89 * Blocked signals are never ignored, since the
90 * signal handler may change by the time it is
93 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
96 if (!sig_task_ignored(t, sig, force))
100 * Tracers may want to know about even ignored signals.
106 * Re-calculate pending state from the set of locally pending
107 * signals, globally pending signals, and blocked signals.
109 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
114 switch (_NSIG_WORDS) {
116 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
117 ready |= signal->sig[i] &~ blocked->sig[i];
120 case 4: ready = signal->sig[3] &~ blocked->sig[3];
121 ready |= signal->sig[2] &~ blocked->sig[2];
122 ready |= signal->sig[1] &~ blocked->sig[1];
123 ready |= signal->sig[0] &~ blocked->sig[0];
126 case 2: ready = signal->sig[1] &~ blocked->sig[1];
127 ready |= signal->sig[0] &~ blocked->sig[0];
130 case 1: ready = signal->sig[0] &~ blocked->sig[0];
135 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
137 static int recalc_sigpending_tsk(struct task_struct *t)
139 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
140 PENDING(&t->pending, &t->blocked) ||
141 PENDING(&t->signal->shared_pending, &t->blocked)) {
142 set_tsk_thread_flag(t, TIF_SIGPENDING);
146 * We must never clear the flag in another thread, or in current
147 * when it's possible the current syscall is returning -ERESTART*.
148 * So we don't clear it here, and only callers who know they should do.
154 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
155 * This is superfluous when called on current, the wakeup is a harmless no-op.
157 void recalc_sigpending_and_wake(struct task_struct *t)
159 if (recalc_sigpending_tsk(t))
160 signal_wake_up(t, 0);
163 void recalc_sigpending(void)
165 if (!recalc_sigpending_tsk(current) && !freezing(current))
166 clear_thread_flag(TIF_SIGPENDING);
170 /* Given the mask, find the first available signal that should be serviced. */
172 #define SYNCHRONOUS_MASK \
173 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
174 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
176 int next_signal(struct sigpending *pending, sigset_t *mask)
178 unsigned long i, *s, *m, x;
181 s = pending->signal.sig;
185 * Handle the first word specially: it contains the
186 * synchronous signals that need to be dequeued first.
190 if (x & SYNCHRONOUS_MASK)
191 x &= SYNCHRONOUS_MASK;
196 switch (_NSIG_WORDS) {
198 for (i = 1; i < _NSIG_WORDS; ++i) {
202 sig = ffz(~x) + i*_NSIG_BPW + 1;
211 sig = ffz(~x) + _NSIG_BPW + 1;
222 static inline void print_dropped_signal(int sig)
224 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
226 if (!print_fatal_signals)
229 if (!__ratelimit(&ratelimit_state))
232 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
233 current->comm, current->pid, sig);
237 * task_set_jobctl_pending - set jobctl pending bits
239 * @mask: pending bits to set
241 * Clear @mask from @task->jobctl. @mask must be subset of
242 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
243 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
244 * cleared. If @task is already being killed or exiting, this function
248 * Must be called with @task->sighand->siglock held.
251 * %true if @mask is set, %false if made noop because @task was dying.
253 bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
255 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
256 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
257 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
259 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
262 if (mask & JOBCTL_STOP_SIGMASK)
263 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
265 task->jobctl |= mask;
270 * task_clear_jobctl_trapping - clear jobctl trapping bit
273 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
274 * Clear it and wake up the ptracer. Note that we don't need any further
275 * locking. @task->siglock guarantees that @task->parent points to the
279 * Must be called with @task->sighand->siglock held.
281 void task_clear_jobctl_trapping(struct task_struct *task)
283 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
284 task->jobctl &= ~JOBCTL_TRAPPING;
285 smp_mb(); /* advised by wake_up_bit() */
286 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
291 * task_clear_jobctl_pending - clear jobctl pending bits
293 * @mask: pending bits to clear
295 * Clear @mask from @task->jobctl. @mask must be subset of
296 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
297 * STOP bits are cleared together.
299 * If clearing of @mask leaves no stop or trap pending, this function calls
300 * task_clear_jobctl_trapping().
303 * Must be called with @task->sighand->siglock held.
305 void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
307 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
309 if (mask & JOBCTL_STOP_PENDING)
310 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
312 task->jobctl &= ~mask;
314 if (!(task->jobctl & JOBCTL_PENDING_MASK))
315 task_clear_jobctl_trapping(task);
319 * task_participate_group_stop - participate in a group stop
320 * @task: task participating in a group stop
322 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
323 * Group stop states are cleared and the group stop count is consumed if
324 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
325 * stop, the appropriate %SIGNAL_* flags are set.
328 * Must be called with @task->sighand->siglock held.
331 * %true if group stop completion should be notified to the parent, %false
334 static bool task_participate_group_stop(struct task_struct *task)
336 struct signal_struct *sig = task->signal;
337 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
339 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
341 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
346 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
347 sig->group_stop_count--;
350 * Tell the caller to notify completion iff we are entering into a
351 * fresh group stop. Read comment in do_signal_stop() for details.
353 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
354 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
361 * allocate a new signal queue record
362 * - this may be called without locks if and only if t == current, otherwise an
363 * appropriate lock must be held to stop the target task from exiting
365 static struct sigqueue *
366 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
368 struct sigqueue *q = NULL;
369 struct user_struct *user;
372 * Protect access to @t credentials. This can go away when all
373 * callers hold rcu read lock.
376 user = get_uid(__task_cred(t)->user);
377 atomic_inc(&user->sigpending);
380 if (override_rlimit ||
381 atomic_read(&user->sigpending) <=
382 task_rlimit(t, RLIMIT_SIGPENDING)) {
383 q = kmem_cache_alloc(sigqueue_cachep, flags);
385 print_dropped_signal(sig);
388 if (unlikely(q == NULL)) {
389 atomic_dec(&user->sigpending);
392 INIT_LIST_HEAD(&q->list);
400 static void __sigqueue_free(struct sigqueue *q)
402 if (q->flags & SIGQUEUE_PREALLOC)
404 atomic_dec(&q->user->sigpending);
406 kmem_cache_free(sigqueue_cachep, q);
409 void flush_sigqueue(struct sigpending *queue)
413 sigemptyset(&queue->signal);
414 while (!list_empty(&queue->list)) {
415 q = list_entry(queue->list.next, struct sigqueue , list);
416 list_del_init(&q->list);
422 * Flush all pending signals for this kthread.
424 void flush_signals(struct task_struct *t)
428 spin_lock_irqsave(&t->sighand->siglock, flags);
429 clear_tsk_thread_flag(t, TIF_SIGPENDING);
430 flush_sigqueue(&t->pending);
431 flush_sigqueue(&t->signal->shared_pending);
432 spin_unlock_irqrestore(&t->sighand->siglock, flags);
435 #ifdef CONFIG_POSIX_TIMERS
436 static void __flush_itimer_signals(struct sigpending *pending)
438 sigset_t signal, retain;
439 struct sigqueue *q, *n;
441 signal = pending->signal;
442 sigemptyset(&retain);
444 list_for_each_entry_safe(q, n, &pending->list, list) {
445 int sig = q->info.si_signo;
447 if (likely(q->info.si_code != SI_TIMER)) {
448 sigaddset(&retain, sig);
450 sigdelset(&signal, sig);
451 list_del_init(&q->list);
456 sigorsets(&pending->signal, &signal, &retain);
459 void flush_itimer_signals(void)
461 struct task_struct *tsk = current;
464 spin_lock_irqsave(&tsk->sighand->siglock, flags);
465 __flush_itimer_signals(&tsk->pending);
466 __flush_itimer_signals(&tsk->signal->shared_pending);
467 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
471 void ignore_signals(struct task_struct *t)
475 for (i = 0; i < _NSIG; ++i)
476 t->sighand->action[i].sa.sa_handler = SIG_IGN;
482 * Flush all handlers for a task.
486 flush_signal_handlers(struct task_struct *t, int force_default)
489 struct k_sigaction *ka = &t->sighand->action[0];
490 for (i = _NSIG ; i != 0 ; i--) {
491 if (force_default || ka->sa.sa_handler != SIG_IGN)
492 ka->sa.sa_handler = SIG_DFL;
494 #ifdef __ARCH_HAS_SA_RESTORER
495 ka->sa.sa_restorer = NULL;
497 sigemptyset(&ka->sa.sa_mask);
502 int unhandled_signal(struct task_struct *tsk, int sig)
504 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
505 if (is_global_init(tsk))
507 if (handler != SIG_IGN && handler != SIG_DFL)
509 /* if ptraced, let the tracer determine */
513 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
515 struct sigqueue *q, *first = NULL;
518 * Collect the siginfo appropriate to this signal. Check if
519 * there is another siginfo for the same signal.
521 list_for_each_entry(q, &list->list, list) {
522 if (q->info.si_signo == sig) {
529 sigdelset(&list->signal, sig);
533 list_del_init(&first->list);
534 copy_siginfo(info, &first->info);
535 __sigqueue_free(first);
538 * Ok, it wasn't in the queue. This must be
539 * a fast-pathed signal or we must have been
540 * out of queue space. So zero out the info.
542 info->si_signo = sig;
544 info->si_code = SI_USER;
550 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
553 int sig = next_signal(pending, mask);
556 collect_signal(sig, pending, info);
561 * Dequeue a signal and return the element to the caller, which is
562 * expected to free it.
564 * All callers have to hold the siglock.
566 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
570 /* We only dequeue private signals from ourselves, we don't let
571 * signalfd steal them
573 signr = __dequeue_signal(&tsk->pending, mask, info);
575 signr = __dequeue_signal(&tsk->signal->shared_pending,
577 #ifdef CONFIG_POSIX_TIMERS
581 * itimers are process shared and we restart periodic
582 * itimers in the signal delivery path to prevent DoS
583 * attacks in the high resolution timer case. This is
584 * compliant with the old way of self-restarting
585 * itimers, as the SIGALRM is a legacy signal and only
586 * queued once. Changing the restart behaviour to
587 * restart the timer in the signal dequeue path is
588 * reducing the timer noise on heavy loaded !highres
591 if (unlikely(signr == SIGALRM)) {
592 struct hrtimer *tmr = &tsk->signal->real_timer;
594 if (!hrtimer_is_queued(tmr) &&
595 tsk->signal->it_real_incr != 0) {
596 hrtimer_forward(tmr, tmr->base->get_time(),
597 tsk->signal->it_real_incr);
598 hrtimer_restart(tmr);
608 if (unlikely(sig_kernel_stop(signr))) {
610 * Set a marker that we have dequeued a stop signal. Our
611 * caller might release the siglock and then the pending
612 * stop signal it is about to process is no longer in the
613 * pending bitmasks, but must still be cleared by a SIGCONT
614 * (and overruled by a SIGKILL). So those cases clear this
615 * shared flag after we've set it. Note that this flag may
616 * remain set after the signal we return is ignored or
617 * handled. That doesn't matter because its only purpose
618 * is to alert stop-signal processing code when another
619 * processor has come along and cleared the flag.
621 current->jobctl |= JOBCTL_STOP_DEQUEUED;
623 #ifdef CONFIG_POSIX_TIMERS
624 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
626 * Release the siglock to ensure proper locking order
627 * of timer locks outside of siglocks. Note, we leave
628 * irqs disabled here, since the posix-timers code is
629 * about to disable them again anyway.
631 spin_unlock(&tsk->sighand->siglock);
632 do_schedule_next_timer(info);
633 spin_lock(&tsk->sighand->siglock);
640 * Tell a process that it has a new active signal..
642 * NOTE! we rely on the previous spin_lock to
643 * lock interrupts for us! We can only be called with
644 * "siglock" held, and the local interrupt must
645 * have been disabled when that got acquired!
647 * No need to set need_resched since signal event passing
648 * goes through ->blocked
650 void signal_wake_up_state(struct task_struct *t, unsigned int state)
652 set_tsk_thread_flag(t, TIF_SIGPENDING);
654 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
655 * case. We don't check t->state here because there is a race with it
656 * executing another processor and just now entering stopped state.
657 * By using wake_up_state, we ensure the process will wake up and
658 * handle its death signal.
660 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
665 * Remove signals in mask from the pending set and queue.
666 * Returns 1 if any signals were found.
668 * All callers must be holding the siglock.
670 static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
672 struct sigqueue *q, *n;
675 sigandsets(&m, mask, &s->signal);
676 if (sigisemptyset(&m))
679 sigandnsets(&s->signal, &s->signal, mask);
680 list_for_each_entry_safe(q, n, &s->list, list) {
681 if (sigismember(mask, q->info.si_signo)) {
682 list_del_init(&q->list);
689 static inline int is_si_special(const struct siginfo *info)
691 return info <= SEND_SIG_FORCED;
694 static inline bool si_fromuser(const struct siginfo *info)
696 return info == SEND_SIG_NOINFO ||
697 (!is_si_special(info) && SI_FROMUSER(info));
701 * called with RCU read lock from check_kill_permission()
703 static int kill_ok_by_cred(struct task_struct *t)
705 const struct cred *cred = current_cred();
706 const struct cred *tcred = __task_cred(t);
708 if (uid_eq(cred->euid, tcred->suid) ||
709 uid_eq(cred->euid, tcred->uid) ||
710 uid_eq(cred->uid, tcred->suid) ||
711 uid_eq(cred->uid, tcred->uid))
714 if (ns_capable(tcred->user_ns, CAP_KILL))
721 * Bad permissions for sending the signal
722 * - the caller must hold the RCU read lock
724 static int check_kill_permission(int sig, struct siginfo *info,
725 struct task_struct *t)
730 if (!valid_signal(sig))
733 if (!si_fromuser(info))
736 error = audit_signal_info(sig, t); /* Let audit system see the signal */
740 if (!same_thread_group(current, t) &&
741 !kill_ok_by_cred(t)) {
744 sid = task_session(t);
746 * We don't return the error if sid == NULL. The
747 * task was unhashed, the caller must notice this.
749 if (!sid || sid == task_session(current))
756 return security_task_kill(t, info, sig, 0);
760 * ptrace_trap_notify - schedule trap to notify ptracer
761 * @t: tracee wanting to notify tracer
763 * This function schedules sticky ptrace trap which is cleared on the next
764 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
767 * If @t is running, STOP trap will be taken. If trapped for STOP and
768 * ptracer is listening for events, tracee is woken up so that it can
769 * re-trap for the new event. If trapped otherwise, STOP trap will be
770 * eventually taken without returning to userland after the existing traps
771 * are finished by PTRACE_CONT.
774 * Must be called with @task->sighand->siglock held.
776 static void ptrace_trap_notify(struct task_struct *t)
778 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
779 assert_spin_locked(&t->sighand->siglock);
781 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
782 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
786 * Handle magic process-wide effects of stop/continue signals. Unlike
787 * the signal actions, these happen immediately at signal-generation
788 * time regardless of blocking, ignoring, or handling. This does the
789 * actual continuing for SIGCONT, but not the actual stopping for stop
790 * signals. The process stop is done as a signal action for SIG_DFL.
792 * Returns true if the signal should be actually delivered, otherwise
793 * it should be dropped.
795 static bool prepare_signal(int sig, struct task_struct *p, bool force)
797 struct signal_struct *signal = p->signal;
798 struct task_struct *t;
801 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
802 if (!(signal->flags & SIGNAL_GROUP_EXIT))
803 return sig == SIGKILL;
805 * The process is in the middle of dying, nothing to do.
807 } else if (sig_kernel_stop(sig)) {
809 * This is a stop signal. Remove SIGCONT from all queues.
811 siginitset(&flush, sigmask(SIGCONT));
812 flush_sigqueue_mask(&flush, &signal->shared_pending);
813 for_each_thread(p, t)
814 flush_sigqueue_mask(&flush, &t->pending);
815 } else if (sig == SIGCONT) {
818 * Remove all stop signals from all queues, wake all threads.
820 siginitset(&flush, SIG_KERNEL_STOP_MASK);
821 flush_sigqueue_mask(&flush, &signal->shared_pending);
822 for_each_thread(p, t) {
823 flush_sigqueue_mask(&flush, &t->pending);
824 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
825 if (likely(!(t->ptrace & PT_SEIZED)))
826 wake_up_state(t, __TASK_STOPPED);
828 ptrace_trap_notify(t);
832 * Notify the parent with CLD_CONTINUED if we were stopped.
834 * If we were in the middle of a group stop, we pretend it
835 * was already finished, and then continued. Since SIGCHLD
836 * doesn't queue we report only CLD_STOPPED, as if the next
837 * CLD_CONTINUED was dropped.
840 if (signal->flags & SIGNAL_STOP_STOPPED)
841 why |= SIGNAL_CLD_CONTINUED;
842 else if (signal->group_stop_count)
843 why |= SIGNAL_CLD_STOPPED;
847 * The first thread which returns from do_signal_stop()
848 * will take ->siglock, notice SIGNAL_CLD_MASK, and
849 * notify its parent. See get_signal_to_deliver().
851 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
852 signal->group_stop_count = 0;
853 signal->group_exit_code = 0;
857 return !sig_ignored(p, sig, force);
861 * Test if P wants to take SIG. After we've checked all threads with this,
862 * it's equivalent to finding no threads not blocking SIG. Any threads not
863 * blocking SIG were ruled out because they are not running and already
864 * have pending signals. Such threads will dequeue from the shared queue
865 * as soon as they're available, so putting the signal on the shared queue
866 * will be equivalent to sending it to one such thread.
868 static inline int wants_signal(int sig, struct task_struct *p)
870 if (sigismember(&p->blocked, sig))
872 if (p->flags & PF_EXITING)
876 if (task_is_stopped_or_traced(p))
878 return task_curr(p) || !signal_pending(p);
881 static void complete_signal(int sig, struct task_struct *p, int group)
883 struct signal_struct *signal = p->signal;
884 struct task_struct *t;
887 * Now find a thread we can wake up to take the signal off the queue.
889 * If the main thread wants the signal, it gets first crack.
890 * Probably the least surprising to the average bear.
892 if (wants_signal(sig, p))
894 else if (!group || thread_group_empty(p))
896 * There is just one thread and it does not need to be woken.
897 * It will dequeue unblocked signals before it runs again.
902 * Otherwise try to find a suitable thread.
904 t = signal->curr_target;
905 while (!wants_signal(sig, t)) {
907 if (t == signal->curr_target)
909 * No thread needs to be woken.
910 * Any eligible threads will see
911 * the signal in the queue soon.
915 signal->curr_target = t;
919 * Found a killable thread. If the signal will be fatal,
920 * then start taking the whole group down immediately.
922 if (sig_fatal(p, sig) &&
923 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
924 !sigismember(&t->real_blocked, sig) &&
925 (sig == SIGKILL || !t->ptrace)) {
927 * This signal will be fatal to the whole group.
929 if (!sig_kernel_coredump(sig)) {
931 * Start a group exit and wake everybody up.
932 * This way we don't have other threads
933 * running and doing things after a slower
934 * thread has the fatal signal pending.
936 signal->flags = SIGNAL_GROUP_EXIT;
937 signal->group_exit_code = sig;
938 signal->group_stop_count = 0;
941 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
942 sigaddset(&t->pending.signal, SIGKILL);
943 signal_wake_up(t, 1);
944 } while_each_thread(p, t);
950 * The signal is already in the shared-pending queue.
951 * Tell the chosen thread to wake up and dequeue it.
953 signal_wake_up(t, sig == SIGKILL);
957 static inline int legacy_queue(struct sigpending *signals, int sig)
959 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
962 #ifdef CONFIG_USER_NS
963 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
965 if (current_user_ns() == task_cred_xxx(t, user_ns))
968 if (SI_FROMKERNEL(info))
972 info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
973 make_kuid(current_user_ns(), info->si_uid));
977 static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
983 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
984 int group, int from_ancestor_ns)
986 struct sigpending *pending;
991 assert_spin_locked(&t->sighand->siglock);
993 result = TRACE_SIGNAL_IGNORED;
994 if (!prepare_signal(sig, t,
995 from_ancestor_ns || (info == SEND_SIG_FORCED)))
998 pending = group ? &t->signal->shared_pending : &t->pending;
1000 * Short-circuit ignored signals and support queuing
1001 * exactly one non-rt signal, so that we can get more
1002 * detailed information about the cause of the signal.
1004 result = TRACE_SIGNAL_ALREADY_PENDING;
1005 if (legacy_queue(pending, sig))
1008 result = TRACE_SIGNAL_DELIVERED;
1010 * fast-pathed signals for kernel-internal things like SIGSTOP
1013 if (info == SEND_SIG_FORCED)
1017 * Real-time signals must be queued if sent by sigqueue, or
1018 * some other real-time mechanism. It is implementation
1019 * defined whether kill() does so. We attempt to do so, on
1020 * the principle of least surprise, but since kill is not
1021 * allowed to fail with EAGAIN when low on memory we just
1022 * make sure at least one signal gets delivered and don't
1023 * pass on the info struct.
1026 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1028 override_rlimit = 0;
1030 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
1033 list_add_tail(&q->list, &pending->list);
1034 switch ((unsigned long) info) {
1035 case (unsigned long) SEND_SIG_NOINFO:
1036 q->info.si_signo = sig;
1037 q->info.si_errno = 0;
1038 q->info.si_code = SI_USER;
1039 q->info.si_pid = task_tgid_nr_ns(current,
1040 task_active_pid_ns(t));
1041 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1043 case (unsigned long) SEND_SIG_PRIV:
1044 q->info.si_signo = sig;
1045 q->info.si_errno = 0;
1046 q->info.si_code = SI_KERNEL;
1051 copy_siginfo(&q->info, info);
1052 if (from_ancestor_ns)
1057 userns_fixup_signal_uid(&q->info, t);
1059 } else if (!is_si_special(info)) {
1060 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1062 * Queue overflow, abort. We may abort if the
1063 * signal was rt and sent by user using something
1064 * other than kill().
1066 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1071 * This is a silent loss of information. We still
1072 * send the signal, but the *info bits are lost.
1074 result = TRACE_SIGNAL_LOSE_INFO;
1079 signalfd_notify(t, sig);
1080 sigaddset(&pending->signal, sig);
1081 complete_signal(sig, t, group);
1083 trace_signal_generate(sig, info, t, group, result);
1087 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1090 int from_ancestor_ns = 0;
1092 #ifdef CONFIG_PID_NS
1093 from_ancestor_ns = si_fromuser(info) &&
1094 !task_pid_nr_ns(current, task_active_pid_ns(t));
1097 return __send_signal(sig, info, t, group, from_ancestor_ns);
1100 static void print_fatal_signal(int signr)
1102 struct pt_regs *regs = signal_pt_regs();
1103 pr_info("potentially unexpected fatal signal %d.\n", signr);
1105 #if defined(__i386__) && !defined(__arch_um__)
1106 pr_info("code at %08lx: ", regs->ip);
1109 for (i = 0; i < 16; i++) {
1112 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1114 pr_cont("%02x ", insn);
1124 static int __init setup_print_fatal_signals(char *str)
1126 get_option (&str, &print_fatal_signals);
1131 __setup("print-fatal-signals=", setup_print_fatal_signals);
1134 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1136 return send_signal(sig, info, p, 1);
1140 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1142 return send_signal(sig, info, t, 0);
1145 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1148 unsigned long flags;
1151 if (lock_task_sighand(p, &flags)) {
1152 ret = send_signal(sig, info, p, group);
1153 unlock_task_sighand(p, &flags);
1160 * Force a signal that the process can't ignore: if necessary
1161 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1163 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1164 * since we do not want to have a signal handler that was blocked
1165 * be invoked when user space had explicitly blocked it.
1167 * We don't want to have recursive SIGSEGV's etc, for example,
1168 * that is why we also clear SIGNAL_UNKILLABLE.
1171 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1173 unsigned long int flags;
1174 int ret, blocked, ignored;
1175 struct k_sigaction *action;
1177 spin_lock_irqsave(&t->sighand->siglock, flags);
1178 action = &t->sighand->action[sig-1];
1179 ignored = action->sa.sa_handler == SIG_IGN;
1180 blocked = sigismember(&t->blocked, sig);
1181 if (blocked || ignored) {
1182 action->sa.sa_handler = SIG_DFL;
1184 sigdelset(&t->blocked, sig);
1185 recalc_sigpending_and_wake(t);
1188 if (action->sa.sa_handler == SIG_DFL)
1189 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1190 ret = specific_send_sig_info(sig, info, t);
1191 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1197 * Nuke all other threads in the group.
1199 int zap_other_threads(struct task_struct *p)
1201 struct task_struct *t = p;
1204 p->signal->group_stop_count = 0;
1206 while_each_thread(p, t) {
1207 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
1210 /* Don't bother with already dead threads */
1213 sigaddset(&t->pending.signal, SIGKILL);
1214 signal_wake_up(t, 1);
1220 struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1221 unsigned long *flags)
1223 struct sighand_struct *sighand;
1227 * Disable interrupts early to avoid deadlocks.
1228 * See rcu_read_unlock() comment header for details.
1230 local_irq_save(*flags);
1232 sighand = rcu_dereference(tsk->sighand);
1233 if (unlikely(sighand == NULL)) {
1235 local_irq_restore(*flags);
1239 * This sighand can be already freed and even reused, but
1240 * we rely on SLAB_DESTROY_BY_RCU and sighand_ctor() which
1241 * initializes ->siglock: this slab can't go away, it has
1242 * the same object type, ->siglock can't be reinitialized.
1244 * We need to ensure that tsk->sighand is still the same
1245 * after we take the lock, we can race with de_thread() or
1246 * __exit_signal(). In the latter case the next iteration
1247 * must see ->sighand == NULL.
1249 spin_lock(&sighand->siglock);
1250 if (likely(sighand == tsk->sighand)) {
1254 spin_unlock(&sighand->siglock);
1256 local_irq_restore(*flags);
1263 * send signal info to all the members of a group
1265 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1270 ret = check_kill_permission(sig, info, p);
1274 ret = do_send_sig_info(sig, info, p, true);
1280 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1281 * control characters do (^C, ^Z etc)
1282 * - the caller must hold at least a readlock on tasklist_lock
1284 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1286 struct task_struct *p = NULL;
1287 int retval, success;
1291 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1292 int err = group_send_sig_info(sig, info, p);
1295 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1296 return success ? 0 : retval;
1299 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1302 struct task_struct *p;
1306 p = pid_task(pid, PIDTYPE_PID);
1308 error = group_send_sig_info(sig, info, p);
1310 if (likely(!p || error != -ESRCH))
1314 * The task was unhashed in between, try again. If it
1315 * is dead, pid_task() will return NULL, if we race with
1316 * de_thread() it will find the new leader.
1321 int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1325 error = kill_pid_info(sig, info, find_vpid(pid));
1330 static int kill_as_cred_perm(const struct cred *cred,
1331 struct task_struct *target)
1333 const struct cred *pcred = __task_cred(target);
1334 if (!uid_eq(cred->euid, pcred->suid) && !uid_eq(cred->euid, pcred->uid) &&
1335 !uid_eq(cred->uid, pcred->suid) && !uid_eq(cred->uid, pcred->uid))
1340 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1341 int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1342 const struct cred *cred, u32 secid)
1345 struct task_struct *p;
1346 unsigned long flags;
1348 if (!valid_signal(sig))
1352 p = pid_task(pid, PIDTYPE_PID);
1357 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
1361 ret = security_task_kill(p, info, sig, secid);
1366 if (lock_task_sighand(p, &flags)) {
1367 ret = __send_signal(sig, info, p, 1, 0);
1368 unlock_task_sighand(p, &flags);
1376 EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
1379 * kill_something_info() interprets pid in interesting ways just like kill(2).
1381 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1382 * is probably wrong. Should make it like BSD or SYSV.
1385 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1391 ret = kill_pid_info(sig, info, find_vpid(pid));
1396 read_lock(&tasklist_lock);
1398 ret = __kill_pgrp_info(sig, info,
1399 pid ? find_vpid(-pid) : task_pgrp(current));
1401 int retval = 0, count = 0;
1402 struct task_struct * p;
1404 for_each_process(p) {
1405 if (task_pid_vnr(p) > 1 &&
1406 !same_thread_group(p, current)) {
1407 int err = group_send_sig_info(sig, info, p);
1413 ret = count ? retval : -ESRCH;
1415 read_unlock(&tasklist_lock);
1421 * These are for backward compatibility with the rest of the kernel source.
1424 int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1427 * Make sure legacy kernel users don't send in bad values
1428 * (normal paths check this in check_kill_permission).
1430 if (!valid_signal(sig))
1433 return do_send_sig_info(sig, info, p, false);
1436 #define __si_special(priv) \
1437 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1440 send_sig(int sig, struct task_struct *p, int priv)
1442 return send_sig_info(sig, __si_special(priv), p);
1446 force_sig(int sig, struct task_struct *p)
1448 force_sig_info(sig, SEND_SIG_PRIV, p);
1452 * When things go south during signal handling, we
1453 * will force a SIGSEGV. And if the signal that caused
1454 * the problem was already a SIGSEGV, we'll want to
1455 * make sure we don't even try to deliver the signal..
1458 force_sigsegv(int sig, struct task_struct *p)
1460 if (sig == SIGSEGV) {
1461 unsigned long flags;
1462 spin_lock_irqsave(&p->sighand->siglock, flags);
1463 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1464 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1466 force_sig(SIGSEGV, p);
1470 int kill_pgrp(struct pid *pid, int sig, int priv)
1474 read_lock(&tasklist_lock);
1475 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1476 read_unlock(&tasklist_lock);
1480 EXPORT_SYMBOL(kill_pgrp);
1482 int kill_pid(struct pid *pid, int sig, int priv)
1484 return kill_pid_info(sig, __si_special(priv), pid);
1486 EXPORT_SYMBOL(kill_pid);
1489 * These functions support sending signals using preallocated sigqueue
1490 * structures. This is needed "because realtime applications cannot
1491 * afford to lose notifications of asynchronous events, like timer
1492 * expirations or I/O completions". In the case of POSIX Timers
1493 * we allocate the sigqueue structure from the timer_create. If this
1494 * allocation fails we are able to report the failure to the application
1495 * with an EAGAIN error.
1497 struct sigqueue *sigqueue_alloc(void)
1499 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1502 q->flags |= SIGQUEUE_PREALLOC;
1507 void sigqueue_free(struct sigqueue *q)
1509 unsigned long flags;
1510 spinlock_t *lock = ¤t->sighand->siglock;
1512 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1514 * We must hold ->siglock while testing q->list
1515 * to serialize with collect_signal() or with
1516 * __exit_signal()->flush_sigqueue().
1518 spin_lock_irqsave(lock, flags);
1519 q->flags &= ~SIGQUEUE_PREALLOC;
1521 * If it is queued it will be freed when dequeued,
1522 * like the "regular" sigqueue.
1524 if (!list_empty(&q->list))
1526 spin_unlock_irqrestore(lock, flags);
1532 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1534 int sig = q->info.si_signo;
1535 struct sigpending *pending;
1536 unsigned long flags;
1539 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1542 if (!likely(lock_task_sighand(t, &flags)))
1545 ret = 1; /* the signal is ignored */
1546 result = TRACE_SIGNAL_IGNORED;
1547 if (!prepare_signal(sig, t, false))
1551 if (unlikely(!list_empty(&q->list))) {
1553 * If an SI_TIMER entry is already queue just increment
1554 * the overrun count.
1556 BUG_ON(q->info.si_code != SI_TIMER);
1557 q->info.si_overrun++;
1558 result = TRACE_SIGNAL_ALREADY_PENDING;
1561 q->info.si_overrun = 0;
1563 signalfd_notify(t, sig);
1564 pending = group ? &t->signal->shared_pending : &t->pending;
1565 list_add_tail(&q->list, &pending->list);
1566 sigaddset(&pending->signal, sig);
1567 complete_signal(sig, t, group);
1568 result = TRACE_SIGNAL_DELIVERED;
1570 trace_signal_generate(sig, &q->info, t, group, result);
1571 unlock_task_sighand(t, &flags);
1577 * Let a parent know about the death of a child.
1578 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1580 * Returns true if our parent ignored us and so we've switched to
1583 bool do_notify_parent(struct task_struct *tsk, int sig)
1585 struct siginfo info;
1586 unsigned long flags;
1587 struct sighand_struct *psig;
1588 bool autoreap = false;
1593 /* do_notify_parent_cldstop should have been called instead. */
1594 BUG_ON(task_is_stopped_or_traced(tsk));
1596 BUG_ON(!tsk->ptrace &&
1597 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1599 if (sig != SIGCHLD) {
1601 * This is only possible if parent == real_parent.
1602 * Check if it has changed security domain.
1604 if (tsk->parent_exec_id != tsk->parent->self_exec_id)
1608 info.si_signo = sig;
1611 * We are under tasklist_lock here so our parent is tied to
1612 * us and cannot change.
1614 * task_active_pid_ns will always return the same pid namespace
1615 * until a task passes through release_task.
1617 * write_lock() currently calls preempt_disable() which is the
1618 * same as rcu_read_lock(), but according to Oleg, this is not
1619 * correct to rely on this
1622 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
1623 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1627 task_cputime(tsk, &utime, &stime);
1628 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
1629 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
1631 info.si_status = tsk->exit_code & 0x7f;
1632 if (tsk->exit_code & 0x80)
1633 info.si_code = CLD_DUMPED;
1634 else if (tsk->exit_code & 0x7f)
1635 info.si_code = CLD_KILLED;
1637 info.si_code = CLD_EXITED;
1638 info.si_status = tsk->exit_code >> 8;
1641 psig = tsk->parent->sighand;
1642 spin_lock_irqsave(&psig->siglock, flags);
1643 if (!tsk->ptrace && sig == SIGCHLD &&
1644 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1645 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1647 * We are exiting and our parent doesn't care. POSIX.1
1648 * defines special semantics for setting SIGCHLD to SIG_IGN
1649 * or setting the SA_NOCLDWAIT flag: we should be reaped
1650 * automatically and not left for our parent's wait4 call.
1651 * Rather than having the parent do it as a magic kind of
1652 * signal handler, we just set this to tell do_exit that we
1653 * can be cleaned up without becoming a zombie. Note that
1654 * we still call __wake_up_parent in this case, because a
1655 * blocked sys_wait4 might now return -ECHILD.
1657 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1658 * is implementation-defined: we do (if you don't want
1659 * it, just use SIG_IGN instead).
1662 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1665 if (valid_signal(sig) && sig)
1666 __group_send_sig_info(sig, &info, tsk->parent);
1667 __wake_up_parent(tsk, tsk->parent);
1668 spin_unlock_irqrestore(&psig->siglock, flags);
1674 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1675 * @tsk: task reporting the state change
1676 * @for_ptracer: the notification is for ptracer
1677 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1679 * Notify @tsk's parent that the stopped/continued state has changed. If
1680 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1681 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1684 * Must be called with tasklist_lock at least read locked.
1686 static void do_notify_parent_cldstop(struct task_struct *tsk,
1687 bool for_ptracer, int why)
1689 struct siginfo info;
1690 unsigned long flags;
1691 struct task_struct *parent;
1692 struct sighand_struct *sighand;
1696 parent = tsk->parent;
1698 tsk = tsk->group_leader;
1699 parent = tsk->real_parent;
1702 info.si_signo = SIGCHLD;
1705 * see comment in do_notify_parent() about the following 4 lines
1708 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
1709 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
1712 task_cputime(tsk, &utime, &stime);
1713 info.si_utime = nsec_to_clock_t(utime);
1714 info.si_stime = nsec_to_clock_t(stime);
1719 info.si_status = SIGCONT;
1722 info.si_status = tsk->signal->group_exit_code & 0x7f;
1725 info.si_status = tsk->exit_code & 0x7f;
1731 sighand = parent->sighand;
1732 spin_lock_irqsave(&sighand->siglock, flags);
1733 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1734 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1735 __group_send_sig_info(SIGCHLD, &info, parent);
1737 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1739 __wake_up_parent(tsk, parent);
1740 spin_unlock_irqrestore(&sighand->siglock, flags);
1743 static inline int may_ptrace_stop(void)
1745 if (!likely(current->ptrace))
1748 * Are we in the middle of do_coredump?
1749 * If so and our tracer is also part of the coredump stopping
1750 * is a deadlock situation, and pointless because our tracer
1751 * is dead so don't allow us to stop.
1752 * If SIGKILL was already sent before the caller unlocked
1753 * ->siglock we must see ->core_state != NULL. Otherwise it
1754 * is safe to enter schedule().
1756 * This is almost outdated, a task with the pending SIGKILL can't
1757 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1758 * after SIGKILL was already dequeued.
1760 if (unlikely(current->mm->core_state) &&
1761 unlikely(current->mm == current->parent->mm))
1768 * Return non-zero if there is a SIGKILL that should be waking us up.
1769 * Called with the siglock held.
1771 static int sigkill_pending(struct task_struct *tsk)
1773 return sigismember(&tsk->pending.signal, SIGKILL) ||
1774 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1778 * This must be called with current->sighand->siglock held.
1780 * This should be the path for all ptrace stops.
1781 * We always set current->last_siginfo while stopped here.
1782 * That makes it a way to test a stopped process for
1783 * being ptrace-stopped vs being job-control-stopped.
1785 * If we actually decide not to stop at all because the tracer
1786 * is gone, we keep current->exit_code unless clear_code.
1788 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1789 __releases(¤t->sighand->siglock)
1790 __acquires(¤t->sighand->siglock)
1792 bool gstop_done = false;
1794 if (arch_ptrace_stop_needed(exit_code, info)) {
1796 * The arch code has something special to do before a
1797 * ptrace stop. This is allowed to block, e.g. for faults
1798 * on user stack pages. We can't keep the siglock while
1799 * calling arch_ptrace_stop, so we must release it now.
1800 * To preserve proper semantics, we must do this before
1801 * any signal bookkeeping like checking group_stop_count.
1802 * Meanwhile, a SIGKILL could come in before we retake the
1803 * siglock. That must prevent us from sleeping in TASK_TRACED.
1804 * So after regaining the lock, we must check for SIGKILL.
1806 spin_unlock_irq(¤t->sighand->siglock);
1807 arch_ptrace_stop(exit_code, info);
1808 spin_lock_irq(¤t->sighand->siglock);
1809 if (sigkill_pending(current))
1814 * We're committing to trapping. TRACED should be visible before
1815 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1816 * Also, transition to TRACED and updates to ->jobctl should be
1817 * atomic with respect to siglock and should be done after the arch
1818 * hook as siglock is released and regrabbed across it.
1820 set_current_state(TASK_TRACED);
1822 current->last_siginfo = info;
1823 current->exit_code = exit_code;
1826 * If @why is CLD_STOPPED, we're trapping to participate in a group
1827 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1828 * across siglock relocks since INTERRUPT was scheduled, PENDING
1829 * could be clear now. We act as if SIGCONT is received after
1830 * TASK_TRACED is entered - ignore it.
1832 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
1833 gstop_done = task_participate_group_stop(current);
1835 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
1836 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
1837 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1838 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
1840 /* entering a trap, clear TRAPPING */
1841 task_clear_jobctl_trapping(current);
1843 spin_unlock_irq(¤t->sighand->siglock);
1844 read_lock(&tasklist_lock);
1845 if (may_ptrace_stop()) {
1847 * Notify parents of the stop.
1849 * While ptraced, there are two parents - the ptracer and
1850 * the real_parent of the group_leader. The ptracer should
1851 * know about every stop while the real parent is only
1852 * interested in the completion of group stop. The states
1853 * for the two don't interact with each other. Notify
1854 * separately unless they're gonna be duplicates.
1856 do_notify_parent_cldstop(current, true, why);
1857 if (gstop_done && ptrace_reparented(current))
1858 do_notify_parent_cldstop(current, false, why);
1861 * Don't want to allow preemption here, because
1862 * sys_ptrace() needs this task to be inactive.
1864 * XXX: implement read_unlock_no_resched().
1867 read_unlock(&tasklist_lock);
1868 preempt_enable_no_resched();
1869 freezable_schedule();
1872 * By the time we got the lock, our tracer went away.
1873 * Don't drop the lock yet, another tracer may come.
1875 * If @gstop_done, the ptracer went away between group stop
1876 * completion and here. During detach, it would have set
1877 * JOBCTL_STOP_PENDING on us and we'll re-enter
1878 * TASK_STOPPED in do_signal_stop() on return, so notifying
1879 * the real parent of the group stop completion is enough.
1882 do_notify_parent_cldstop(current, false, why);
1884 /* tasklist protects us from ptrace_freeze_traced() */
1885 __set_current_state(TASK_RUNNING);
1887 current->exit_code = 0;
1888 read_unlock(&tasklist_lock);
1892 * We are back. Now reacquire the siglock before touching
1893 * last_siginfo, so that we are sure to have synchronized with
1894 * any signal-sending on another CPU that wants to examine it.
1896 spin_lock_irq(¤t->sighand->siglock);
1897 current->last_siginfo = NULL;
1899 /* LISTENING can be set only during STOP traps, clear it */
1900 current->jobctl &= ~JOBCTL_LISTENING;
1903 * Queued signals ignored us while we were stopped for tracing.
1904 * So check for any that we should take before resuming user mode.
1905 * This sets TIF_SIGPENDING, but never clears it.
1907 recalc_sigpending_tsk(current);
1910 static void ptrace_do_notify(int signr, int exit_code, int why)
1914 memset(&info, 0, sizeof info);
1915 info.si_signo = signr;
1916 info.si_code = exit_code;
1917 info.si_pid = task_pid_vnr(current);
1918 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
1920 /* Let the debugger run. */
1921 ptrace_stop(exit_code, why, 1, &info);
1924 void ptrace_notify(int exit_code)
1926 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1927 if (unlikely(current->task_works))
1930 spin_lock_irq(¤t->sighand->siglock);
1931 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
1932 spin_unlock_irq(¤t->sighand->siglock);
1936 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
1937 * @signr: signr causing group stop if initiating
1939 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
1940 * and participate in it. If already set, participate in the existing
1941 * group stop. If participated in a group stop (and thus slept), %true is
1942 * returned with siglock released.
1944 * If ptraced, this function doesn't handle stop itself. Instead,
1945 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
1946 * untouched. The caller must ensure that INTERRUPT trap handling takes
1947 * places afterwards.
1950 * Must be called with @current->sighand->siglock held, which is released
1954 * %false if group stop is already cancelled or ptrace trap is scheduled.
1955 * %true if participated in group stop.
1957 static bool do_signal_stop(int signr)
1958 __releases(¤t->sighand->siglock)
1960 struct signal_struct *sig = current->signal;
1962 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1963 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
1964 struct task_struct *t;
1966 /* signr will be recorded in task->jobctl for retries */
1967 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
1969 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
1970 unlikely(signal_group_exit(sig)))
1973 * There is no group stop already in progress. We must
1976 * While ptraced, a task may be resumed while group stop is
1977 * still in effect and then receive a stop signal and
1978 * initiate another group stop. This deviates from the
1979 * usual behavior as two consecutive stop signals can't
1980 * cause two group stops when !ptraced. That is why we
1981 * also check !task_is_stopped(t) below.
1983 * The condition can be distinguished by testing whether
1984 * SIGNAL_STOP_STOPPED is already set. Don't generate
1985 * group_exit_code in such case.
1987 * This is not necessary for SIGNAL_STOP_CONTINUED because
1988 * an intervening stop signal is required to cause two
1989 * continued events regardless of ptrace.
1991 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1992 sig->group_exit_code = signr;
1994 sig->group_stop_count = 0;
1996 if (task_set_jobctl_pending(current, signr | gstop))
1997 sig->group_stop_count++;
2000 while_each_thread(current, t) {
2002 * Setting state to TASK_STOPPED for a group
2003 * stop is always done with the siglock held,
2004 * so this check has no races.
2006 if (!task_is_stopped(t) &&
2007 task_set_jobctl_pending(t, signr | gstop)) {
2008 sig->group_stop_count++;
2009 if (likely(!(t->ptrace & PT_SEIZED)))
2010 signal_wake_up(t, 0);
2012 ptrace_trap_notify(t);
2017 if (likely(!current->ptrace)) {
2021 * If there are no other threads in the group, or if there
2022 * is a group stop in progress and we are the last to stop,
2023 * report to the parent.
2025 if (task_participate_group_stop(current))
2026 notify = CLD_STOPPED;
2028 __set_current_state(TASK_STOPPED);
2029 spin_unlock_irq(¤t->sighand->siglock);
2032 * Notify the parent of the group stop completion. Because
2033 * we're not holding either the siglock or tasklist_lock
2034 * here, ptracer may attach inbetween; however, this is for
2035 * group stop and should always be delivered to the real
2036 * parent of the group leader. The new ptracer will get
2037 * its notification when this task transitions into
2041 read_lock(&tasklist_lock);
2042 do_notify_parent_cldstop(current, false, notify);
2043 read_unlock(&tasklist_lock);
2046 /* Now we don't run again until woken by SIGCONT or SIGKILL */
2047 freezable_schedule();
2051 * While ptraced, group stop is handled by STOP trap.
2052 * Schedule it and let the caller deal with it.
2054 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2060 * do_jobctl_trap - take care of ptrace jobctl traps
2062 * When PT_SEIZED, it's used for both group stop and explicit
2063 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2064 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2065 * the stop signal; otherwise, %SIGTRAP.
2067 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2068 * number as exit_code and no siginfo.
2071 * Must be called with @current->sighand->siglock held, which may be
2072 * released and re-acquired before returning with intervening sleep.
2074 static void do_jobctl_trap(void)
2076 struct signal_struct *signal = current->signal;
2077 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
2079 if (current->ptrace & PT_SEIZED) {
2080 if (!signal->group_stop_count &&
2081 !(signal->flags & SIGNAL_STOP_STOPPED))
2083 WARN_ON_ONCE(!signr);
2084 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2087 WARN_ON_ONCE(!signr);
2088 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
2089 current->exit_code = 0;
2093 static int ptrace_signal(int signr, siginfo_t *info)
2095 ptrace_signal_deliver();
2097 * We do not check sig_kernel_stop(signr) but set this marker
2098 * unconditionally because we do not know whether debugger will
2099 * change signr. This flag has no meaning unless we are going
2100 * to stop after return from ptrace_stop(). In this case it will
2101 * be checked in do_signal_stop(), we should only stop if it was
2102 * not cleared by SIGCONT while we were sleeping. See also the
2103 * comment in dequeue_signal().
2105 current->jobctl |= JOBCTL_STOP_DEQUEUED;
2106 ptrace_stop(signr, CLD_TRAPPED, 0, info);
2108 /* We're back. Did the debugger cancel the sig? */
2109 signr = current->exit_code;
2113 current->exit_code = 0;
2116 * Update the siginfo structure if the signal has
2117 * changed. If the debugger wanted something
2118 * specific in the siginfo structure then it should
2119 * have updated *info via PTRACE_SETSIGINFO.
2121 if (signr != info->si_signo) {
2122 info->si_signo = signr;
2124 info->si_code = SI_USER;
2126 info->si_pid = task_pid_vnr(current->parent);
2127 info->si_uid = from_kuid_munged(current_user_ns(),
2128 task_uid(current->parent));
2132 /* If the (new) signal is now blocked, requeue it. */
2133 if (sigismember(¤t->blocked, signr)) {
2134 specific_send_sig_info(signr, info, current);
2141 int get_signal(struct ksignal *ksig)
2143 struct sighand_struct *sighand = current->sighand;
2144 struct signal_struct *signal = current->signal;
2147 if (unlikely(current->task_works))
2150 if (unlikely(uprobe_deny_signal()))
2154 * Do this once, we can't return to user-mode if freezing() == T.
2155 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2156 * thus do not need another check after return.
2161 spin_lock_irq(&sighand->siglock);
2163 * Every stopped thread goes here after wakeup. Check to see if
2164 * we should notify the parent, prepare_signal(SIGCONT) encodes
2165 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2167 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
2170 if (signal->flags & SIGNAL_CLD_CONTINUED)
2171 why = CLD_CONTINUED;
2175 signal->flags &= ~SIGNAL_CLD_MASK;
2177 spin_unlock_irq(&sighand->siglock);
2180 * Notify the parent that we're continuing. This event is
2181 * always per-process and doesn't make whole lot of sense
2182 * for ptracers, who shouldn't consume the state via
2183 * wait(2) either, but, for backward compatibility, notify
2184 * the ptracer of the group leader too unless it's gonna be
2187 read_lock(&tasklist_lock);
2188 do_notify_parent_cldstop(current, false, why);
2190 if (ptrace_reparented(current->group_leader))
2191 do_notify_parent_cldstop(current->group_leader,
2193 read_unlock(&tasklist_lock);
2199 struct k_sigaction *ka;
2201 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2205 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2207 spin_unlock_irq(&sighand->siglock);
2211 signr = dequeue_signal(current, ¤t->blocked, &ksig->info);
2214 break; /* will return 0 */
2216 if (unlikely(current->ptrace) && signr != SIGKILL) {
2217 signr = ptrace_signal(signr, &ksig->info);
2222 ka = &sighand->action[signr-1];
2224 /* Trace actually delivered signals. */
2225 trace_signal_deliver(signr, &ksig->info, ka);
2227 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2229 if (ka->sa.sa_handler != SIG_DFL) {
2230 /* Run the handler. */
2233 if (ka->sa.sa_flags & SA_ONESHOT)
2234 ka->sa.sa_handler = SIG_DFL;
2236 break; /* will return non-zero "signr" value */
2240 * Now we are doing the default action for this signal.
2242 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2246 * Global init gets no signals it doesn't want.
2247 * Container-init gets no signals it doesn't want from same
2250 * Note that if global/container-init sees a sig_kernel_only()
2251 * signal here, the signal must have been generated internally
2252 * or must have come from an ancestor namespace. In either
2253 * case, the signal cannot be dropped.
2255 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
2256 !sig_kernel_only(signr))
2259 if (sig_kernel_stop(signr)) {
2261 * The default action is to stop all threads in
2262 * the thread group. The job control signals
2263 * do nothing in an orphaned pgrp, but SIGSTOP
2264 * always works. Note that siglock needs to be
2265 * dropped during the call to is_orphaned_pgrp()
2266 * because of lock ordering with tasklist_lock.
2267 * This allows an intervening SIGCONT to be posted.
2268 * We need to check for that and bail out if necessary.
2270 if (signr != SIGSTOP) {
2271 spin_unlock_irq(&sighand->siglock);
2273 /* signals can be posted during this window */
2275 if (is_current_pgrp_orphaned())
2278 spin_lock_irq(&sighand->siglock);
2281 if (likely(do_signal_stop(ksig->info.si_signo))) {
2282 /* It released the siglock. */
2287 * We didn't actually stop, due to a race
2288 * with SIGCONT or something like that.
2293 spin_unlock_irq(&sighand->siglock);
2296 * Anything else is fatal, maybe with a core dump.
2298 current->flags |= PF_SIGNALED;
2300 if (sig_kernel_coredump(signr)) {
2301 if (print_fatal_signals)
2302 print_fatal_signal(ksig->info.si_signo);
2303 proc_coredump_connector(current);
2305 * If it was able to dump core, this kills all
2306 * other threads in the group and synchronizes with
2307 * their demise. If we lost the race with another
2308 * thread getting here, it set group_exit_code
2309 * first and our do_group_exit call below will use
2310 * that value and ignore the one we pass it.
2312 do_coredump(&ksig->info);
2316 * Death signals, no core dump.
2318 do_group_exit(ksig->info.si_signo);
2321 spin_unlock_irq(&sighand->siglock);
2324 return ksig->sig > 0;
2328 * signal_delivered -
2329 * @ksig: kernel signal struct
2330 * @stepping: nonzero if debugger single-step or block-step in use
2332 * This function should be called when a signal has successfully been
2333 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
2334 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
2335 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
2337 static void signal_delivered(struct ksignal *ksig, int stepping)
2341 /* A signal was successfully delivered, and the
2342 saved sigmask was stored on the signal frame,
2343 and will be restored by sigreturn. So we can
2344 simply clear the restore sigmask flag. */
2345 clear_restore_sigmask();
2347 sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask);
2348 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2349 sigaddset(&blocked, ksig->sig);
2350 set_current_blocked(&blocked);
2351 tracehook_signal_handler(stepping);
2354 void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2357 force_sigsegv(ksig->sig, current);
2359 signal_delivered(ksig, stepping);
2363 * It could be that complete_signal() picked us to notify about the
2364 * group-wide signal. Other threads should be notified now to take
2365 * the shared signals in @which since we will not.
2367 static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
2370 struct task_struct *t;
2372 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2373 if (sigisemptyset(&retarget))
2377 while_each_thread(tsk, t) {
2378 if (t->flags & PF_EXITING)
2381 if (!has_pending_signals(&retarget, &t->blocked))
2383 /* Remove the signals this thread can handle. */
2384 sigandsets(&retarget, &retarget, &t->blocked);
2386 if (!signal_pending(t))
2387 signal_wake_up(t, 0);
2389 if (sigisemptyset(&retarget))
2394 void exit_signals(struct task_struct *tsk)
2400 * @tsk is about to have PF_EXITING set - lock out users which
2401 * expect stable threadgroup.
2403 cgroup_threadgroup_change_begin(tsk);
2405 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2406 tsk->flags |= PF_EXITING;
2407 cgroup_threadgroup_change_end(tsk);
2411 spin_lock_irq(&tsk->sighand->siglock);
2413 * From now this task is not visible for group-wide signals,
2414 * see wants_signal(), do_signal_stop().
2416 tsk->flags |= PF_EXITING;
2418 cgroup_threadgroup_change_end(tsk);
2420 if (!signal_pending(tsk))
2423 unblocked = tsk->blocked;
2424 signotset(&unblocked);
2425 retarget_shared_pending(tsk, &unblocked);
2427 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
2428 task_participate_group_stop(tsk))
2429 group_stop = CLD_STOPPED;
2431 spin_unlock_irq(&tsk->sighand->siglock);
2434 * If group stop has completed, deliver the notification. This
2435 * should always go to the real parent of the group leader.
2437 if (unlikely(group_stop)) {
2438 read_lock(&tasklist_lock);
2439 do_notify_parent_cldstop(tsk, false, group_stop);
2440 read_unlock(&tasklist_lock);
2444 EXPORT_SYMBOL(recalc_sigpending);
2445 EXPORT_SYMBOL_GPL(dequeue_signal);
2446 EXPORT_SYMBOL(flush_signals);
2447 EXPORT_SYMBOL(force_sig);
2448 EXPORT_SYMBOL(send_sig);
2449 EXPORT_SYMBOL(send_sig_info);
2450 EXPORT_SYMBOL(sigprocmask);
2453 * System call entry points.
2457 * sys_restart_syscall - restart a system call
2459 SYSCALL_DEFINE0(restart_syscall)
2461 struct restart_block *restart = ¤t->restart_block;
2462 return restart->fn(restart);
2465 long do_no_restart_syscall(struct restart_block *param)
2470 static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2472 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2473 sigset_t newblocked;
2474 /* A set of now blocked but previously unblocked signals. */
2475 sigandnsets(&newblocked, newset, ¤t->blocked);
2476 retarget_shared_pending(tsk, &newblocked);
2478 tsk->blocked = *newset;
2479 recalc_sigpending();
2483 * set_current_blocked - change current->blocked mask
2486 * It is wrong to change ->blocked directly, this helper should be used
2487 * to ensure the process can't miss a shared signal we are going to block.
2489 void set_current_blocked(sigset_t *newset)
2491 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
2492 __set_current_blocked(newset);
2495 void __set_current_blocked(const sigset_t *newset)
2497 struct task_struct *tsk = current;
2500 * In case the signal mask hasn't changed, there is nothing we need
2501 * to do. The current->blocked shouldn't be modified by other task.
2503 if (sigequalsets(&tsk->blocked, newset))
2506 spin_lock_irq(&tsk->sighand->siglock);
2507 __set_task_blocked(tsk, newset);
2508 spin_unlock_irq(&tsk->sighand->siglock);
2512 * This is also useful for kernel threads that want to temporarily
2513 * (or permanently) block certain signals.
2515 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2516 * interface happily blocks "unblockable" signals like SIGKILL
2519 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2521 struct task_struct *tsk = current;
2524 /* Lockless, only current can change ->blocked, never from irq */
2526 *oldset = tsk->blocked;
2530 sigorsets(&newset, &tsk->blocked, set);
2533 sigandnsets(&newset, &tsk->blocked, set);
2542 __set_current_blocked(&newset);
2547 * sys_rt_sigprocmask - change the list of currently blocked signals
2548 * @how: whether to add, remove, or set signals
2549 * @nset: stores pending signals
2550 * @oset: previous value of signal mask if non-null
2551 * @sigsetsize: size of sigset_t type
2553 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
2554 sigset_t __user *, oset, size_t, sigsetsize)
2556 sigset_t old_set, new_set;
2559 /* XXX: Don't preclude handling different sized sigset_t's. */
2560 if (sigsetsize != sizeof(sigset_t))
2563 old_set = current->blocked;
2566 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2568 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2570 error = sigprocmask(how, &new_set, NULL);
2576 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2583 #ifdef CONFIG_COMPAT
2584 COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2585 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
2588 sigset_t old_set = current->blocked;
2590 /* XXX: Don't preclude handling different sized sigset_t's. */
2591 if (sigsetsize != sizeof(sigset_t))
2595 compat_sigset_t new32;
2598 if (copy_from_user(&new32, nset, sizeof(compat_sigset_t)))
2601 sigset_from_compat(&new_set, &new32);
2602 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2604 error = sigprocmask(how, &new_set, NULL);
2609 compat_sigset_t old32;
2610 sigset_to_compat(&old32, &old_set);
2611 if (copy_to_user(oset, &old32, sizeof(compat_sigset_t)))
2616 return sys_rt_sigprocmask(how, (sigset_t __user *)nset,
2617 (sigset_t __user *)oset, sigsetsize);
2622 static int do_sigpending(void *set, unsigned long sigsetsize)
2624 if (sigsetsize > sizeof(sigset_t))
2627 spin_lock_irq(¤t->sighand->siglock);
2628 sigorsets(set, ¤t->pending.signal,
2629 ¤t->signal->shared_pending.signal);
2630 spin_unlock_irq(¤t->sighand->siglock);
2632 /* Outside the lock because only this thread touches it. */
2633 sigandsets(set, ¤t->blocked, set);
2638 * sys_rt_sigpending - examine a pending signal that has been raised
2640 * @uset: stores pending signals
2641 * @sigsetsize: size of sigset_t type or larger
2643 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
2646 int err = do_sigpending(&set, sigsetsize);
2647 if (!err && copy_to_user(uset, &set, sigsetsize))
2652 #ifdef CONFIG_COMPAT
2653 COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2654 compat_size_t, sigsetsize)
2658 int err = do_sigpending(&set, sigsetsize);
2660 compat_sigset_t set32;
2661 sigset_to_compat(&set32, &set);
2662 /* we can get here only if sigsetsize <= sizeof(set) */
2663 if (copy_to_user(uset, &set32, sigsetsize))
2668 return sys_rt_sigpending((sigset_t __user *)uset, sigsetsize);
2673 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2675 int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
2679 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2681 if (from->si_code < 0)
2682 return __copy_to_user(to, from, sizeof(siginfo_t))
2685 * If you change siginfo_t structure, please be sure
2686 * this code is fixed accordingly.
2687 * Please remember to update the signalfd_copyinfo() function
2688 * inside fs/signalfd.c too, in case siginfo_t changes.
2689 * It should never copy any pad contained in the structure
2690 * to avoid security leaks, but must copy the generic
2691 * 3 ints plus the relevant union member.
2693 err = __put_user(from->si_signo, &to->si_signo);
2694 err |= __put_user(from->si_errno, &to->si_errno);
2695 err |= __put_user((short)from->si_code, &to->si_code);
2696 switch (from->si_code & __SI_MASK) {
2698 err |= __put_user(from->si_pid, &to->si_pid);
2699 err |= __put_user(from->si_uid, &to->si_uid);
2702 err |= __put_user(from->si_tid, &to->si_tid);
2703 err |= __put_user(from->si_overrun, &to->si_overrun);
2704 err |= __put_user(from->si_ptr, &to->si_ptr);
2707 err |= __put_user(from->si_band, &to->si_band);
2708 err |= __put_user(from->si_fd, &to->si_fd);
2711 err |= __put_user(from->si_addr, &to->si_addr);
2712 #ifdef __ARCH_SI_TRAPNO
2713 err |= __put_user(from->si_trapno, &to->si_trapno);
2715 #ifdef BUS_MCEERR_AO
2717 * Other callers might not initialize the si_lsb field,
2718 * so check explicitly for the right codes here.
2720 if (from->si_signo == SIGBUS &&
2721 (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
2722 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2725 if (from->si_signo == SIGSEGV && from->si_code == SEGV_BNDERR) {
2726 err |= __put_user(from->si_lower, &to->si_lower);
2727 err |= __put_user(from->si_upper, &to->si_upper);
2731 if (from->si_signo == SIGSEGV && from->si_code == SEGV_PKUERR)
2732 err |= __put_user(from->si_pkey, &to->si_pkey);
2736 err |= __put_user(from->si_pid, &to->si_pid);
2737 err |= __put_user(from->si_uid, &to->si_uid);
2738 err |= __put_user(from->si_status, &to->si_status);
2739 err |= __put_user(from->si_utime, &to->si_utime);
2740 err |= __put_user(from->si_stime, &to->si_stime);
2742 case __SI_RT: /* This is not generated by the kernel as of now. */
2743 case __SI_MESGQ: /* But this is */
2744 err |= __put_user(from->si_pid, &to->si_pid);
2745 err |= __put_user(from->si_uid, &to->si_uid);
2746 err |= __put_user(from->si_ptr, &to->si_ptr);
2748 #ifdef __ARCH_SIGSYS
2750 err |= __put_user(from->si_call_addr, &to->si_call_addr);
2751 err |= __put_user(from->si_syscall, &to->si_syscall);
2752 err |= __put_user(from->si_arch, &to->si_arch);
2755 default: /* this is just in case for now ... */
2756 err |= __put_user(from->si_pid, &to->si_pid);
2757 err |= __put_user(from->si_uid, &to->si_uid);
2766 * do_sigtimedwait - wait for queued signals specified in @which
2767 * @which: queued signals to wait for
2768 * @info: if non-null, the signal's siginfo is returned here
2769 * @ts: upper bound on process time suspension
2771 int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2772 const struct timespec *ts)
2774 ktime_t *to = NULL, timeout = KTIME_MAX;
2775 struct task_struct *tsk = current;
2776 sigset_t mask = *which;
2780 if (!timespec_valid(ts))
2782 timeout = timespec_to_ktime(*ts);
2787 * Invert the set of allowed signals to get those we want to block.
2789 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2792 spin_lock_irq(&tsk->sighand->siglock);
2793 sig = dequeue_signal(tsk, &mask, info);
2794 if (!sig && timeout) {
2796 * None ready, temporarily unblock those we're interested
2797 * while we are sleeping in so that we'll be awakened when
2798 * they arrive. Unblocking is always fine, we can avoid
2799 * set_current_blocked().
2801 tsk->real_blocked = tsk->blocked;
2802 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2803 recalc_sigpending();
2804 spin_unlock_irq(&tsk->sighand->siglock);
2806 __set_current_state(TASK_INTERRUPTIBLE);
2807 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
2809 spin_lock_irq(&tsk->sighand->siglock);
2810 __set_task_blocked(tsk, &tsk->real_blocked);
2811 sigemptyset(&tsk->real_blocked);
2812 sig = dequeue_signal(tsk, &mask, info);
2814 spin_unlock_irq(&tsk->sighand->siglock);
2818 return ret ? -EINTR : -EAGAIN;
2822 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2824 * @uthese: queued signals to wait for
2825 * @uinfo: if non-null, the signal's siginfo is returned here
2826 * @uts: upper bound on process time suspension
2827 * @sigsetsize: size of sigset_t type
2829 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2830 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2838 /* XXX: Don't preclude handling different sized sigset_t's. */
2839 if (sigsetsize != sizeof(sigset_t))
2842 if (copy_from_user(&these, uthese, sizeof(these)))
2846 if (copy_from_user(&ts, uts, sizeof(ts)))
2850 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
2852 if (ret > 0 && uinfo) {
2853 if (copy_siginfo_to_user(uinfo, &info))
2861 * sys_kill - send a signal to a process
2862 * @pid: the PID of the process
2863 * @sig: signal to be sent
2865 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2867 struct siginfo info;
2869 info.si_signo = sig;
2871 info.si_code = SI_USER;
2872 info.si_pid = task_tgid_vnr(current);
2873 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2875 return kill_something_info(sig, &info, pid);
2879 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2881 struct task_struct *p;
2885 p = find_task_by_vpid(pid);
2886 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2887 error = check_kill_permission(sig, info, p);
2889 * The null signal is a permissions and process existence
2890 * probe. No signal is actually delivered.
2892 if (!error && sig) {
2893 error = do_send_sig_info(sig, info, p, false);
2895 * If lock_task_sighand() failed we pretend the task
2896 * dies after receiving the signal. The window is tiny,
2897 * and the signal is private anyway.
2899 if (unlikely(error == -ESRCH))
2908 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2910 struct siginfo info = {};
2912 info.si_signo = sig;
2914 info.si_code = SI_TKILL;
2915 info.si_pid = task_tgid_vnr(current);
2916 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
2918 return do_send_specific(tgid, pid, sig, &info);
2922 * sys_tgkill - send signal to one specific thread
2923 * @tgid: the thread group ID of the thread
2924 * @pid: the PID of the thread
2925 * @sig: signal to be sent
2927 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2928 * exists but it's not belonging to the target process anymore. This
2929 * method solves the problem of threads exiting and PIDs getting reused.
2931 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2933 /* This is only valid for single tasks */
2934 if (pid <= 0 || tgid <= 0)
2937 return do_tkill(tgid, pid, sig);
2941 * sys_tkill - send signal to one specific task
2942 * @pid: the PID of the task
2943 * @sig: signal to be sent
2945 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2947 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2949 /* This is only valid for single tasks */
2953 return do_tkill(0, pid, sig);
2956 static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
2958 /* Not even root can pretend to send signals from the kernel.
2959 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2961 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
2962 (task_pid_vnr(current) != pid))
2965 info->si_signo = sig;
2967 /* POSIX.1b doesn't mention process groups. */
2968 return kill_proc_info(sig, info, pid);
2972 * sys_rt_sigqueueinfo - send signal information to a signal
2973 * @pid: the PID of the thread
2974 * @sig: signal to be sent
2975 * @uinfo: signal info to be sent
2977 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2978 siginfo_t __user *, uinfo)
2981 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2983 return do_rt_sigqueueinfo(pid, sig, &info);
2986 #ifdef CONFIG_COMPAT
2987 COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
2990 struct compat_siginfo __user *, uinfo)
2992 siginfo_t info = {};
2993 int ret = copy_siginfo_from_user32(&info, uinfo);
2996 return do_rt_sigqueueinfo(pid, sig, &info);
3000 static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
3002 /* This is only valid for single tasks */
3003 if (pid <= 0 || tgid <= 0)
3006 /* Not even root can pretend to send signals from the kernel.
3007 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3009 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3010 (task_pid_vnr(current) != pid))
3013 info->si_signo = sig;
3015 return do_send_specific(tgid, pid, sig, info);
3018 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3019 siginfo_t __user *, uinfo)
3023 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3026 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3029 #ifdef CONFIG_COMPAT
3030 COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3034 struct compat_siginfo __user *, uinfo)
3036 siginfo_t info = {};
3038 if (copy_siginfo_from_user32(&info, uinfo))
3040 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3045 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
3047 void kernel_sigaction(int sig, __sighandler_t action)
3049 spin_lock_irq(¤t->sighand->siglock);
3050 current->sighand->action[sig - 1].sa.sa_handler = action;
3051 if (action == SIG_IGN) {
3055 sigaddset(&mask, sig);
3057 flush_sigqueue_mask(&mask, ¤t->signal->shared_pending);
3058 flush_sigqueue_mask(&mask, ¤t->pending);
3059 recalc_sigpending();
3061 spin_unlock_irq(¤t->sighand->siglock);
3063 EXPORT_SYMBOL(kernel_sigaction);
3065 void __weak sigaction_compat_abi(struct k_sigaction *act,
3066 struct k_sigaction *oact)
3070 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
3072 struct task_struct *p = current, *t;
3073 struct k_sigaction *k;
3076 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
3079 k = &p->sighand->action[sig-1];
3081 spin_lock_irq(&p->sighand->siglock);
3085 sigaction_compat_abi(act, oact);
3088 sigdelsetmask(&act->sa.sa_mask,
3089 sigmask(SIGKILL) | sigmask(SIGSTOP));
3093 * "Setting a signal action to SIG_IGN for a signal that is
3094 * pending shall cause the pending signal to be discarded,
3095 * whether or not it is blocked."
3097 * "Setting a signal action to SIG_DFL for a signal that is
3098 * pending and whose default action is to ignore the signal
3099 * (for example, SIGCHLD), shall cause the pending signal to
3100 * be discarded, whether or not it is blocked"
3102 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
3104 sigaddset(&mask, sig);
3105 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3106 for_each_thread(p, t)
3107 flush_sigqueue_mask(&mask, &t->pending);
3111 spin_unlock_irq(&p->sighand->siglock);
3116 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
3121 oss.ss_sp = (void __user *) current->sas_ss_sp;
3122 oss.ss_size = current->sas_ss_size;
3123 oss.ss_flags = sas_ss_flags(sp) |
3124 (current->sas_ss_flags & SS_FLAG_BITS);
3133 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
3135 error = __get_user(ss_sp, &uss->ss_sp) |
3136 __get_user(ss_flags, &uss->ss_flags) |
3137 __get_user(ss_size, &uss->ss_size);
3142 if (on_sig_stack(sp))
3145 ss_mode = ss_flags & ~SS_FLAG_BITS;
3147 if (ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3151 if (ss_mode == SS_DISABLE) {
3156 if (ss_size < MINSIGSTKSZ)
3160 current->sas_ss_sp = (unsigned long) ss_sp;
3161 current->sas_ss_size = ss_size;
3162 current->sas_ss_flags = ss_flags;
3168 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
3170 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
3171 __put_user(oss.ss_size, &uoss->ss_size) |
3172 __put_user(oss.ss_flags, &uoss->ss_flags);
3178 SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3180 return do_sigaltstack(uss, uoss, current_user_stack_pointer());
3183 int restore_altstack(const stack_t __user *uss)
3185 int err = do_sigaltstack(uss, NULL, current_user_stack_pointer());
3186 /* squash all but EFAULT for now */
3187 return err == -EFAULT ? err : 0;
3190 int __save_altstack(stack_t __user *uss, unsigned long sp)
3192 struct task_struct *t = current;
3193 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3194 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3195 __put_user(t->sas_ss_size, &uss->ss_size);
3198 if (t->sas_ss_flags & SS_AUTODISARM)
3203 #ifdef CONFIG_COMPAT
3204 COMPAT_SYSCALL_DEFINE2(sigaltstack,
3205 const compat_stack_t __user *, uss_ptr,
3206 compat_stack_t __user *, uoss_ptr)
3213 compat_stack_t uss32;
3215 memset(&uss, 0, sizeof(stack_t));
3216 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3218 uss.ss_sp = compat_ptr(uss32.ss_sp);
3219 uss.ss_flags = uss32.ss_flags;
3220 uss.ss_size = uss32.ss_size;
3224 ret = do_sigaltstack((stack_t __force __user *) (uss_ptr ? &uss : NULL),
3225 (stack_t __force __user *) &uoss,
3226 compat_user_stack_pointer());
3228 if (ret >= 0 && uoss_ptr) {
3229 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(compat_stack_t)) ||
3230 __put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||
3231 __put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||
3232 __put_user(uoss.ss_size, &uoss_ptr->ss_size))
3238 int compat_restore_altstack(const compat_stack_t __user *uss)
3240 int err = compat_sys_sigaltstack(uss, NULL);
3241 /* squash all but -EFAULT for now */
3242 return err == -EFAULT ? err : 0;
3245 int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3248 struct task_struct *t = current;
3249 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3251 __put_user(t->sas_ss_flags, &uss->ss_flags) |
3252 __put_user(t->sas_ss_size, &uss->ss_size);
3255 if (t->sas_ss_flags & SS_AUTODISARM)
3261 #ifdef __ARCH_WANT_SYS_SIGPENDING
3264 * sys_sigpending - examine pending signals
3265 * @set: where mask of pending signal is returned
3267 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
3269 return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
3274 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
3276 * sys_sigprocmask - examine and change blocked signals
3277 * @how: whether to add, remove, or set signals
3278 * @nset: signals to add or remove (if non-null)
3279 * @oset: previous value of signal mask if non-null
3281 * Some platforms have their own version with special arguments;
3282 * others support only sys_rt_sigprocmask.
3285 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
3286 old_sigset_t __user *, oset)
3288 old_sigset_t old_set, new_set;
3289 sigset_t new_blocked;
3291 old_set = current->blocked.sig[0];
3294 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3297 new_blocked = current->blocked;
3301 sigaddsetmask(&new_blocked, new_set);
3304 sigdelsetmask(&new_blocked, new_set);
3307 new_blocked.sig[0] = new_set;
3313 set_current_blocked(&new_blocked);
3317 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3323 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3325 #ifndef CONFIG_ODD_RT_SIGACTION
3327 * sys_rt_sigaction - alter an action taken by a process
3328 * @sig: signal to be sent
3329 * @act: new sigaction
3330 * @oact: used to save the previous sigaction
3331 * @sigsetsize: size of sigset_t type
3333 SYSCALL_DEFINE4(rt_sigaction, int, sig,
3334 const struct sigaction __user *, act,
3335 struct sigaction __user *, oact,
3338 struct k_sigaction new_sa, old_sa;
3341 /* XXX: Don't preclude handling different sized sigset_t's. */
3342 if (sigsetsize != sizeof(sigset_t))
3346 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3350 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3353 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3359 #ifdef CONFIG_COMPAT
3360 COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3361 const struct compat_sigaction __user *, act,
3362 struct compat_sigaction __user *, oact,
3363 compat_size_t, sigsetsize)
3365 struct k_sigaction new_ka, old_ka;
3366 compat_sigset_t mask;
3367 #ifdef __ARCH_HAS_SA_RESTORER
3368 compat_uptr_t restorer;
3372 /* XXX: Don't preclude handling different sized sigset_t's. */
3373 if (sigsetsize != sizeof(compat_sigset_t))
3377 compat_uptr_t handler;
3378 ret = get_user(handler, &act->sa_handler);
3379 new_ka.sa.sa_handler = compat_ptr(handler);
3380 #ifdef __ARCH_HAS_SA_RESTORER
3381 ret |= get_user(restorer, &act->sa_restorer);
3382 new_ka.sa.sa_restorer = compat_ptr(restorer);
3384 ret |= copy_from_user(&mask, &act->sa_mask, sizeof(mask));
3385 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
3388 sigset_from_compat(&new_ka.sa.sa_mask, &mask);
3391 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3393 sigset_to_compat(&mask, &old_ka.sa.sa_mask);
3394 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
3396 ret |= copy_to_user(&oact->sa_mask, &mask, sizeof(mask));
3397 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
3398 #ifdef __ARCH_HAS_SA_RESTORER
3399 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3400 &oact->sa_restorer);
3406 #endif /* !CONFIG_ODD_RT_SIGACTION */
3408 #ifdef CONFIG_OLD_SIGACTION
3409 SYSCALL_DEFINE3(sigaction, int, sig,
3410 const struct old_sigaction __user *, act,
3411 struct old_sigaction __user *, oact)
3413 struct k_sigaction new_ka, old_ka;
3418 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3419 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3420 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3421 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3422 __get_user(mask, &act->sa_mask))
3424 #ifdef __ARCH_HAS_KA_RESTORER
3425 new_ka.ka_restorer = NULL;
3427 siginitset(&new_ka.sa.sa_mask, mask);
3430 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3433 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3434 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3435 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3436 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3437 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3444 #ifdef CONFIG_COMPAT_OLD_SIGACTION
3445 COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3446 const struct compat_old_sigaction __user *, act,
3447 struct compat_old_sigaction __user *, oact)
3449 struct k_sigaction new_ka, old_ka;
3451 compat_old_sigset_t mask;
3452 compat_uptr_t handler, restorer;
3455 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3456 __get_user(handler, &act->sa_handler) ||
3457 __get_user(restorer, &act->sa_restorer) ||
3458 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3459 __get_user(mask, &act->sa_mask))
3462 #ifdef __ARCH_HAS_KA_RESTORER
3463 new_ka.ka_restorer = NULL;
3465 new_ka.sa.sa_handler = compat_ptr(handler);
3466 new_ka.sa.sa_restorer = compat_ptr(restorer);
3467 siginitset(&new_ka.sa.sa_mask, mask);
3470 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3473 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3474 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3475 &oact->sa_handler) ||
3476 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3477 &oact->sa_restorer) ||
3478 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3479 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3486 #ifdef CONFIG_SGETMASK_SYSCALL
3489 * For backwards compatibility. Functionality superseded by sigprocmask.
3491 SYSCALL_DEFINE0(sgetmask)
3494 return current->blocked.sig[0];
3497 SYSCALL_DEFINE1(ssetmask, int, newmask)
3499 int old = current->blocked.sig[0];
3502 siginitset(&newset, newmask);
3503 set_current_blocked(&newset);
3507 #endif /* CONFIG_SGETMASK_SYSCALL */
3509 #ifdef __ARCH_WANT_SYS_SIGNAL
3511 * For backwards compatibility. Functionality superseded by sigaction.
3513 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
3515 struct k_sigaction new_sa, old_sa;
3518 new_sa.sa.sa_handler = handler;
3519 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
3520 sigemptyset(&new_sa.sa.sa_mask);
3522 ret = do_sigaction(sig, &new_sa, &old_sa);
3524 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3526 #endif /* __ARCH_WANT_SYS_SIGNAL */
3528 #ifdef __ARCH_WANT_SYS_PAUSE
3530 SYSCALL_DEFINE0(pause)
3532 while (!signal_pending(current)) {
3533 __set_current_state(TASK_INTERRUPTIBLE);
3536 return -ERESTARTNOHAND;
3541 static int sigsuspend(sigset_t *set)
3543 current->saved_sigmask = current->blocked;
3544 set_current_blocked(set);
3546 while (!signal_pending(current)) {
3547 __set_current_state(TASK_INTERRUPTIBLE);
3550 set_restore_sigmask();
3551 return -ERESTARTNOHAND;
3555 * sys_rt_sigsuspend - replace the signal mask for a value with the
3556 * @unewset value until a signal is received
3557 * @unewset: new signal mask value
3558 * @sigsetsize: size of sigset_t type
3560 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
3564 /* XXX: Don't preclude handling different sized sigset_t's. */
3565 if (sigsetsize != sizeof(sigset_t))
3568 if (copy_from_user(&newset, unewset, sizeof(newset)))
3570 return sigsuspend(&newset);
3573 #ifdef CONFIG_COMPAT
3574 COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
3578 compat_sigset_t newset32;
3580 /* XXX: Don't preclude handling different sized sigset_t's. */
3581 if (sigsetsize != sizeof(sigset_t))
3584 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
3586 sigset_from_compat(&newset, &newset32);
3587 return sigsuspend(&newset);
3589 /* on little-endian bitmaps don't care about granularity */
3590 return sys_rt_sigsuspend((sigset_t __user *)unewset, sigsetsize);
3595 #ifdef CONFIG_OLD_SIGSUSPEND
3596 SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
3599 siginitset(&blocked, mask);
3600 return sigsuspend(&blocked);
3603 #ifdef CONFIG_OLD_SIGSUSPEND3
3604 SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
3607 siginitset(&blocked, mask);
3608 return sigsuspend(&blocked);
3612 __weak const char *arch_vma_name(struct vm_area_struct *vma)
3617 void __init signals_init(void)
3619 /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
3620 BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
3621 != offsetof(struct siginfo, _sifields._pad));
3623 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
3626 #ifdef CONFIG_KGDB_KDB
3627 #include <linux/kdb.h>
3629 * kdb_send_sig_info - Allows kdb to send signals without exposing
3630 * signal internals. This function checks if the required locks are
3631 * available before calling the main signal code, to avoid kdb
3635 kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3637 static struct task_struct *kdb_prev_t;
3639 if (!spin_trylock(&t->sighand->siglock)) {
3640 kdb_printf("Can't do kill command now.\n"
3641 "The sigmask lock is held somewhere else in "
3642 "kernel, try again later\n");
3645 spin_unlock(&t->sighand->siglock);
3646 new_t = kdb_prev_t != t;
3648 if (t->state != TASK_RUNNING && new_t) {
3649 kdb_printf("Process is not RUNNING, sending a signal from "
3650 "kdb risks deadlock\n"
3651 "on the run queue locks. "
3652 "The signal has _not_ been sent.\n"
3653 "Reissue the kill command if you want to risk "
3657 sig = info->si_signo;
3658 if (send_sig_info(sig, info, t))
3659 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3662 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3664 #endif /* CONFIG_KGDB_KDB */