signal: Fix premature completion of group stop when interfered by ptrace
[linux-2.6.git] / kernel / signal.c
blobecb20089eaffb8baef7e7add27a25ccb0cec5384
1 /*
2 * linux/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/fs.h>
18 #include <linux/tty.h>
19 #include <linux/binfmts.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/ptrace.h>
23 #include <linux/signal.h>
24 #include <linux/signalfd.h>
25 #include <linux/ratelimit.h>
26 #include <linux/tracehook.h>
27 #include <linux/capability.h>
28 #include <linux/freezer.h>
29 #include <linux/pid_namespace.h>
30 #include <linux/nsproxy.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/signal.h>
34 #include <asm/param.h>
35 #include <asm/uaccess.h>
36 #include <asm/unistd.h>
37 #include <asm/siginfo.h>
38 #include "audit.h" /* audit_signal_info() */
41 * SLAB caches for signal bits.
44 static struct kmem_cache *sigqueue_cachep;
46 int print_fatal_signals __read_mostly;
48 static void __user *sig_handler(struct task_struct *t, int sig)
50 return t->sighand->action[sig - 1].sa.sa_handler;
53 static int sig_handler_ignored(void __user *handler, int sig)
55 /* Is it explicitly or implicitly ignored? */
56 return handler == SIG_IGN ||
57 (handler == SIG_DFL && sig_kernel_ignore(sig));
60 static int sig_task_ignored(struct task_struct *t, int sig,
61 int from_ancestor_ns)
63 void __user *handler;
65 handler = sig_handler(t, sig);
67 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
68 handler == SIG_DFL && !from_ancestor_ns)
69 return 1;
71 return sig_handler_ignored(handler, sig);
74 static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
77 * Blocked signals are never ignored, since the
78 * signal handler may change by the time it is
79 * unblocked.
81 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
82 return 0;
84 if (!sig_task_ignored(t, sig, from_ancestor_ns))
85 return 0;
88 * Tracers may want to know about even ignored signals.
90 return !tracehook_consider_ignored_signal(t, sig);
94 * Re-calculate pending state from the set of locally pending
95 * signals, globally pending signals, and blocked signals.
97 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
99 unsigned long ready;
100 long i;
102 switch (_NSIG_WORDS) {
103 default:
104 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
105 ready |= signal->sig[i] &~ blocked->sig[i];
106 break;
108 case 4: ready = signal->sig[3] &~ blocked->sig[3];
109 ready |= signal->sig[2] &~ blocked->sig[2];
110 ready |= signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
114 case 2: ready = signal->sig[1] &~ blocked->sig[1];
115 ready |= signal->sig[0] &~ blocked->sig[0];
116 break;
118 case 1: ready = signal->sig[0] &~ blocked->sig[0];
120 return ready != 0;
123 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
125 static int recalc_sigpending_tsk(struct task_struct *t)
127 if (t->signal->group_stop_count > 0 ||
128 PENDING(&t->pending, &t->blocked) ||
129 PENDING(&t->signal->shared_pending, &t->blocked)) {
130 set_tsk_thread_flag(t, TIF_SIGPENDING);
131 return 1;
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
138 return 0;
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
145 void recalc_sigpending_and_wake(struct task_struct *t)
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
151 void recalc_sigpending(void)
153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
156 clear_thread_flag(TIF_SIGPENDING);
160 /* Given the mask, find the first available signal that should be serviced. */
162 #define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
166 int next_signal(struct sigpending *pending, sigset_t *mask)
168 unsigned long i, *s, *m, x;
169 int sig = 0;
171 s = pending->signal.sig;
172 m = mask->sig;
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
186 switch (_NSIG_WORDS) {
187 default:
188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
193 break;
195 break;
197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
200 break;
201 sig = ffz(~x) + _NSIG_BPW + 1;
202 break;
204 case 1:
205 /* Nothing to do */
206 break;
209 return sig;
212 static inline void print_dropped_signal(int sig)
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
216 if (!print_fatal_signals)
217 return;
219 if (!__ratelimit(&ratelimit_state))
220 return;
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
227 * task_clear_group_stop_pending - clear pending group stop
228 * @task: target task
230 * Clear group stop states for @task.
232 * CONTEXT:
233 * Must be called with @task->sighand->siglock held.
235 static void task_clear_group_stop_pending(struct task_struct *task)
237 task->group_stop &= ~GROUP_STOP_CONSUME;
241 * task_participate_group_stop - participate in a group stop
242 * @task: task participating in a group stop
244 * @task is participating in a group stop. Group stop states are cleared
245 * and the group stop count is consumed if %GROUP_STOP_CONSUME was set. If
246 * the consumption completes the group stop, the appropriate %SIGNAL_*
247 * flags are set.
249 * CONTEXT:
250 * Must be called with @task->sighand->siglock held.
252 static bool task_participate_group_stop(struct task_struct *task)
254 struct signal_struct *sig = task->signal;
255 bool consume = task->group_stop & GROUP_STOP_CONSUME;
257 task_clear_group_stop_pending(task);
259 if (!consume)
260 return false;
262 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
263 sig->group_stop_count--;
265 if (!sig->group_stop_count) {
266 sig->flags = SIGNAL_STOP_STOPPED;
267 return true;
269 return false;
273 * allocate a new signal queue record
274 * - this may be called without locks if and only if t == current, otherwise an
275 * appopriate lock must be held to stop the target task from exiting
277 static struct sigqueue *
278 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
280 struct sigqueue *q = NULL;
281 struct user_struct *user;
284 * Protect access to @t credentials. This can go away when all
285 * callers hold rcu read lock.
287 rcu_read_lock();
288 user = get_uid(__task_cred(t)->user);
289 atomic_inc(&user->sigpending);
290 rcu_read_unlock();
292 if (override_rlimit ||
293 atomic_read(&user->sigpending) <=
294 task_rlimit(t, RLIMIT_SIGPENDING)) {
295 q = kmem_cache_alloc(sigqueue_cachep, flags);
296 } else {
297 print_dropped_signal(sig);
300 if (unlikely(q == NULL)) {
301 atomic_dec(&user->sigpending);
302 free_uid(user);
303 } else {
304 INIT_LIST_HEAD(&q->list);
305 q->flags = 0;
306 q->user = user;
309 return q;
312 static void __sigqueue_free(struct sigqueue *q)
314 if (q->flags & SIGQUEUE_PREALLOC)
315 return;
316 atomic_dec(&q->user->sigpending);
317 free_uid(q->user);
318 kmem_cache_free(sigqueue_cachep, q);
321 void flush_sigqueue(struct sigpending *queue)
323 struct sigqueue *q;
325 sigemptyset(&queue->signal);
326 while (!list_empty(&queue->list)) {
327 q = list_entry(queue->list.next, struct sigqueue , list);
328 list_del_init(&q->list);
329 __sigqueue_free(q);
334 * Flush all pending signals for a task.
336 void __flush_signals(struct task_struct *t)
338 clear_tsk_thread_flag(t, TIF_SIGPENDING);
339 flush_sigqueue(&t->pending);
340 flush_sigqueue(&t->signal->shared_pending);
343 void flush_signals(struct task_struct *t)
345 unsigned long flags;
347 spin_lock_irqsave(&t->sighand->siglock, flags);
348 __flush_signals(t);
349 spin_unlock_irqrestore(&t->sighand->siglock, flags);
352 static void __flush_itimer_signals(struct sigpending *pending)
354 sigset_t signal, retain;
355 struct sigqueue *q, *n;
357 signal = pending->signal;
358 sigemptyset(&retain);
360 list_for_each_entry_safe(q, n, &pending->list, list) {
361 int sig = q->info.si_signo;
363 if (likely(q->info.si_code != SI_TIMER)) {
364 sigaddset(&retain, sig);
365 } else {
366 sigdelset(&signal, sig);
367 list_del_init(&q->list);
368 __sigqueue_free(q);
372 sigorsets(&pending->signal, &signal, &retain);
375 void flush_itimer_signals(void)
377 struct task_struct *tsk = current;
378 unsigned long flags;
380 spin_lock_irqsave(&tsk->sighand->siglock, flags);
381 __flush_itimer_signals(&tsk->pending);
382 __flush_itimer_signals(&tsk->signal->shared_pending);
383 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
386 void ignore_signals(struct task_struct *t)
388 int i;
390 for (i = 0; i < _NSIG; ++i)
391 t->sighand->action[i].sa.sa_handler = SIG_IGN;
393 flush_signals(t);
397 * Flush all handlers for a task.
400 void
401 flush_signal_handlers(struct task_struct *t, int force_default)
403 int i;
404 struct k_sigaction *ka = &t->sighand->action[0];
405 for (i = _NSIG ; i != 0 ; i--) {
406 if (force_default || ka->sa.sa_handler != SIG_IGN)
407 ka->sa.sa_handler = SIG_DFL;
408 ka->sa.sa_flags = 0;
409 sigemptyset(&ka->sa.sa_mask);
410 ka++;
414 int unhandled_signal(struct task_struct *tsk, int sig)
416 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
417 if (is_global_init(tsk))
418 return 1;
419 if (handler != SIG_IGN && handler != SIG_DFL)
420 return 0;
421 return !tracehook_consider_fatal_signal(tsk, sig);
425 /* Notify the system that a driver wants to block all signals for this
426 * process, and wants to be notified if any signals at all were to be
427 * sent/acted upon. If the notifier routine returns non-zero, then the
428 * signal will be acted upon after all. If the notifier routine returns 0,
429 * then then signal will be blocked. Only one block per process is
430 * allowed. priv is a pointer to private data that the notifier routine
431 * can use to determine if the signal should be blocked or not. */
433 void
434 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
436 unsigned long flags;
438 spin_lock_irqsave(&current->sighand->siglock, flags);
439 current->notifier_mask = mask;
440 current->notifier_data = priv;
441 current->notifier = notifier;
442 spin_unlock_irqrestore(&current->sighand->siglock, flags);
445 /* Notify the system that blocking has ended. */
447 void
448 unblock_all_signals(void)
450 unsigned long flags;
452 spin_lock_irqsave(&current->sighand->siglock, flags);
453 current->notifier = NULL;
454 current->notifier_data = NULL;
455 recalc_sigpending();
456 spin_unlock_irqrestore(&current->sighand->siglock, flags);
459 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
461 struct sigqueue *q, *first = NULL;
464 * Collect the siginfo appropriate to this signal. Check if
465 * there is another siginfo for the same signal.
467 list_for_each_entry(q, &list->list, list) {
468 if (q->info.si_signo == sig) {
469 if (first)
470 goto still_pending;
471 first = q;
475 sigdelset(&list->signal, sig);
477 if (first) {
478 still_pending:
479 list_del_init(&first->list);
480 copy_siginfo(info, &first->info);
481 __sigqueue_free(first);
482 } else {
483 /* Ok, it wasn't in the queue. This must be
484 a fast-pathed signal or we must have been
485 out of queue space. So zero out the info.
487 info->si_signo = sig;
488 info->si_errno = 0;
489 info->si_code = SI_USER;
490 info->si_pid = 0;
491 info->si_uid = 0;
495 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
496 siginfo_t *info)
498 int sig = next_signal(pending, mask);
500 if (sig) {
501 if (current->notifier) {
502 if (sigismember(current->notifier_mask, sig)) {
503 if (!(current->notifier)(current->notifier_data)) {
504 clear_thread_flag(TIF_SIGPENDING);
505 return 0;
510 collect_signal(sig, pending, info);
513 return sig;
517 * Dequeue a signal and return the element to the caller, which is
518 * expected to free it.
520 * All callers have to hold the siglock.
522 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
524 int signr;
526 /* We only dequeue private signals from ourselves, we don't let
527 * signalfd steal them
529 signr = __dequeue_signal(&tsk->pending, mask, info);
530 if (!signr) {
531 signr = __dequeue_signal(&tsk->signal->shared_pending,
532 mask, info);
534 * itimer signal ?
536 * itimers are process shared and we restart periodic
537 * itimers in the signal delivery path to prevent DoS
538 * attacks in the high resolution timer case. This is
539 * compliant with the old way of self restarting
540 * itimers, as the SIGALRM is a legacy signal and only
541 * queued once. Changing the restart behaviour to
542 * restart the timer in the signal dequeue path is
543 * reducing the timer noise on heavy loaded !highres
544 * systems too.
546 if (unlikely(signr == SIGALRM)) {
547 struct hrtimer *tmr = &tsk->signal->real_timer;
549 if (!hrtimer_is_queued(tmr) &&
550 tsk->signal->it_real_incr.tv64 != 0) {
551 hrtimer_forward(tmr, tmr->base->get_time(),
552 tsk->signal->it_real_incr);
553 hrtimer_restart(tmr);
558 recalc_sigpending();
559 if (!signr)
560 return 0;
562 if (unlikely(sig_kernel_stop(signr))) {
564 * Set a marker that we have dequeued a stop signal. Our
565 * caller might release the siglock and then the pending
566 * stop signal it is about to process is no longer in the
567 * pending bitmasks, but must still be cleared by a SIGCONT
568 * (and overruled by a SIGKILL). So those cases clear this
569 * shared flag after we've set it. Note that this flag may
570 * remain set after the signal we return is ignored or
571 * handled. That doesn't matter because its only purpose
572 * is to alert stop-signal processing code when another
573 * processor has come along and cleared the flag.
575 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
577 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
579 * Release the siglock to ensure proper locking order
580 * of timer locks outside of siglocks. Note, we leave
581 * irqs disabled here, since the posix-timers code is
582 * about to disable them again anyway.
584 spin_unlock(&tsk->sighand->siglock);
585 do_schedule_next_timer(info);
586 spin_lock(&tsk->sighand->siglock);
588 return signr;
592 * Tell a process that it has a new active signal..
594 * NOTE! we rely on the previous spin_lock to
595 * lock interrupts for us! We can only be called with
596 * "siglock" held, and the local interrupt must
597 * have been disabled when that got acquired!
599 * No need to set need_resched since signal event passing
600 * goes through ->blocked
602 void signal_wake_up(struct task_struct *t, int resume)
604 unsigned int mask;
606 set_tsk_thread_flag(t, TIF_SIGPENDING);
609 * For SIGKILL, we want to wake it up in the stopped/traced/killable
610 * case. We don't check t->state here because there is a race with it
611 * executing another processor and just now entering stopped state.
612 * By using wake_up_state, we ensure the process will wake up and
613 * handle its death signal.
615 mask = TASK_INTERRUPTIBLE;
616 if (resume)
617 mask |= TASK_WAKEKILL;
618 if (!wake_up_state(t, mask))
619 kick_process(t);
623 * Remove signals in mask from the pending set and queue.
624 * Returns 1 if any signals were found.
626 * All callers must be holding the siglock.
628 * This version takes a sigset mask and looks at all signals,
629 * not just those in the first mask word.
631 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
633 struct sigqueue *q, *n;
634 sigset_t m;
636 sigandsets(&m, mask, &s->signal);
637 if (sigisemptyset(&m))
638 return 0;
640 signandsets(&s->signal, &s->signal, mask);
641 list_for_each_entry_safe(q, n, &s->list, list) {
642 if (sigismember(mask, q->info.si_signo)) {
643 list_del_init(&q->list);
644 __sigqueue_free(q);
647 return 1;
650 * Remove signals in mask from the pending set and queue.
651 * Returns 1 if any signals were found.
653 * All callers must be holding the siglock.
655 static int rm_from_queue(unsigned long mask, struct sigpending *s)
657 struct sigqueue *q, *n;
659 if (!sigtestsetmask(&s->signal, mask))
660 return 0;
662 sigdelsetmask(&s->signal, mask);
663 list_for_each_entry_safe(q, n, &s->list, list) {
664 if (q->info.si_signo < SIGRTMIN &&
665 (mask & sigmask(q->info.si_signo))) {
666 list_del_init(&q->list);
667 __sigqueue_free(q);
670 return 1;
673 static inline int is_si_special(const struct siginfo *info)
675 return info <= SEND_SIG_FORCED;
678 static inline bool si_fromuser(const struct siginfo *info)
680 return info == SEND_SIG_NOINFO ||
681 (!is_si_special(info) && SI_FROMUSER(info));
685 * Bad permissions for sending the signal
686 * - the caller must hold the RCU read lock
688 static int check_kill_permission(int sig, struct siginfo *info,
689 struct task_struct *t)
691 const struct cred *cred, *tcred;
692 struct pid *sid;
693 int error;
695 if (!valid_signal(sig))
696 return -EINVAL;
698 if (!si_fromuser(info))
699 return 0;
701 error = audit_signal_info(sig, t); /* Let audit system see the signal */
702 if (error)
703 return error;
705 cred = current_cred();
706 tcred = __task_cred(t);
707 if (!same_thread_group(current, t) &&
708 (cred->euid ^ tcred->suid) &&
709 (cred->euid ^ tcred->uid) &&
710 (cred->uid ^ tcred->suid) &&
711 (cred->uid ^ tcred->uid) &&
712 !capable(CAP_KILL)) {
713 switch (sig) {
714 case SIGCONT:
715 sid = task_session(t);
717 * We don't return the error if sid == NULL. The
718 * task was unhashed, the caller must notice this.
720 if (!sid || sid == task_session(current))
721 break;
722 default:
723 return -EPERM;
727 return security_task_kill(t, info, sig, 0);
731 * Handle magic process-wide effects of stop/continue signals. Unlike
732 * the signal actions, these happen immediately at signal-generation
733 * time regardless of blocking, ignoring, or handling. This does the
734 * actual continuing for SIGCONT, but not the actual stopping for stop
735 * signals. The process stop is done as a signal action for SIG_DFL.
737 * Returns true if the signal should be actually delivered, otherwise
738 * it should be dropped.
740 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
742 struct signal_struct *signal = p->signal;
743 struct task_struct *t;
745 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
747 * The process is in the middle of dying, nothing to do.
749 } else if (sig_kernel_stop(sig)) {
751 * This is a stop signal. Remove SIGCONT from all queues.
753 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
754 t = p;
755 do {
756 rm_from_queue(sigmask(SIGCONT), &t->pending);
757 } while_each_thread(p, t);
758 } else if (sig == SIGCONT) {
759 unsigned int why;
761 * Remove all stop signals from all queues,
762 * and wake all threads.
764 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
765 t = p;
766 do {
767 unsigned int state;
768 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
770 * If there is a handler for SIGCONT, we must make
771 * sure that no thread returns to user mode before
772 * we post the signal, in case it was the only
773 * thread eligible to run the signal handler--then
774 * it must not do anything between resuming and
775 * running the handler. With the TIF_SIGPENDING
776 * flag set, the thread will pause and acquire the
777 * siglock that we hold now and until we've queued
778 * the pending signal.
780 * Wake up the stopped thread _after_ setting
781 * TIF_SIGPENDING
783 state = __TASK_STOPPED;
784 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
785 set_tsk_thread_flag(t, TIF_SIGPENDING);
786 state |= TASK_INTERRUPTIBLE;
788 wake_up_state(t, state);
789 } while_each_thread(p, t);
792 * Notify the parent with CLD_CONTINUED if we were stopped.
794 * If we were in the middle of a group stop, we pretend it
795 * was already finished, and then continued. Since SIGCHLD
796 * doesn't queue we report only CLD_STOPPED, as if the next
797 * CLD_CONTINUED was dropped.
799 why = 0;
800 if (signal->flags & SIGNAL_STOP_STOPPED)
801 why |= SIGNAL_CLD_CONTINUED;
802 else if (signal->group_stop_count)
803 why |= SIGNAL_CLD_STOPPED;
805 if (why) {
807 * The first thread which returns from do_signal_stop()
808 * will take ->siglock, notice SIGNAL_CLD_MASK, and
809 * notify its parent. See get_signal_to_deliver().
811 signal->flags = why | SIGNAL_STOP_CONTINUED;
812 signal->group_stop_count = 0;
813 signal->group_exit_code = 0;
814 } else {
816 * We are not stopped, but there could be a stop
817 * signal in the middle of being processed after
818 * being removed from the queue. Clear that too.
820 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
824 return !sig_ignored(p, sig, from_ancestor_ns);
828 * Test if P wants to take SIG. After we've checked all threads with this,
829 * it's equivalent to finding no threads not blocking SIG. Any threads not
830 * blocking SIG were ruled out because they are not running and already
831 * have pending signals. Such threads will dequeue from the shared queue
832 * as soon as they're available, so putting the signal on the shared queue
833 * will be equivalent to sending it to one such thread.
835 static inline int wants_signal(int sig, struct task_struct *p)
837 if (sigismember(&p->blocked, sig))
838 return 0;
839 if (p->flags & PF_EXITING)
840 return 0;
841 if (sig == SIGKILL)
842 return 1;
843 if (task_is_stopped_or_traced(p))
844 return 0;
845 return task_curr(p) || !signal_pending(p);
848 static void complete_signal(int sig, struct task_struct *p, int group)
850 struct signal_struct *signal = p->signal;
851 struct task_struct *t;
854 * Now find a thread we can wake up to take the signal off the queue.
856 * If the main thread wants the signal, it gets first crack.
857 * Probably the least surprising to the average bear.
859 if (wants_signal(sig, p))
860 t = p;
861 else if (!group || thread_group_empty(p))
863 * There is just one thread and it does not need to be woken.
864 * It will dequeue unblocked signals before it runs again.
866 return;
867 else {
869 * Otherwise try to find a suitable thread.
871 t = signal->curr_target;
872 while (!wants_signal(sig, t)) {
873 t = next_thread(t);
874 if (t == signal->curr_target)
876 * No thread needs to be woken.
877 * Any eligible threads will see
878 * the signal in the queue soon.
880 return;
882 signal->curr_target = t;
886 * Found a killable thread. If the signal will be fatal,
887 * then start taking the whole group down immediately.
889 if (sig_fatal(p, sig) &&
890 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
891 !sigismember(&t->real_blocked, sig) &&
892 (sig == SIGKILL ||
893 !tracehook_consider_fatal_signal(t, sig))) {
895 * This signal will be fatal to the whole group.
897 if (!sig_kernel_coredump(sig)) {
899 * Start a group exit and wake everybody up.
900 * This way we don't have other threads
901 * running and doing things after a slower
902 * thread has the fatal signal pending.
904 signal->flags = SIGNAL_GROUP_EXIT;
905 signal->group_exit_code = sig;
906 signal->group_stop_count = 0;
907 t = p;
908 do {
909 sigaddset(&t->pending.signal, SIGKILL);
910 signal_wake_up(t, 1);
911 } while_each_thread(p, t);
912 return;
917 * The signal is already in the shared-pending queue.
918 * Tell the chosen thread to wake up and dequeue it.
920 signal_wake_up(t, sig == SIGKILL);
921 return;
924 static inline int legacy_queue(struct sigpending *signals, int sig)
926 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
929 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
930 int group, int from_ancestor_ns)
932 struct sigpending *pending;
933 struct sigqueue *q;
934 int override_rlimit;
936 trace_signal_generate(sig, info, t);
938 assert_spin_locked(&t->sighand->siglock);
940 if (!prepare_signal(sig, t, from_ancestor_ns))
941 return 0;
943 pending = group ? &t->signal->shared_pending : &t->pending;
945 * Short-circuit ignored signals and support queuing
946 * exactly one non-rt signal, so that we can get more
947 * detailed information about the cause of the signal.
949 if (legacy_queue(pending, sig))
950 return 0;
952 * fast-pathed signals for kernel-internal things like SIGSTOP
953 * or SIGKILL.
955 if (info == SEND_SIG_FORCED)
956 goto out_set;
958 /* Real-time signals must be queued if sent by sigqueue, or
959 some other real-time mechanism. It is implementation
960 defined whether kill() does so. We attempt to do so, on
961 the principle of least surprise, but since kill is not
962 allowed to fail with EAGAIN when low on memory we just
963 make sure at least one signal gets delivered and don't
964 pass on the info struct. */
966 if (sig < SIGRTMIN)
967 override_rlimit = (is_si_special(info) || info->si_code >= 0);
968 else
969 override_rlimit = 0;
971 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
972 override_rlimit);
973 if (q) {
974 list_add_tail(&q->list, &pending->list);
975 switch ((unsigned long) info) {
976 case (unsigned long) SEND_SIG_NOINFO:
977 q->info.si_signo = sig;
978 q->info.si_errno = 0;
979 q->info.si_code = SI_USER;
980 q->info.si_pid = task_tgid_nr_ns(current,
981 task_active_pid_ns(t));
982 q->info.si_uid = current_uid();
983 break;
984 case (unsigned long) SEND_SIG_PRIV:
985 q->info.si_signo = sig;
986 q->info.si_errno = 0;
987 q->info.si_code = SI_KERNEL;
988 q->info.si_pid = 0;
989 q->info.si_uid = 0;
990 break;
991 default:
992 copy_siginfo(&q->info, info);
993 if (from_ancestor_ns)
994 q->info.si_pid = 0;
995 break;
997 } else if (!is_si_special(info)) {
998 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1000 * Queue overflow, abort. We may abort if the
1001 * signal was rt and sent by user using something
1002 * other than kill().
1004 trace_signal_overflow_fail(sig, group, info);
1005 return -EAGAIN;
1006 } else {
1008 * This is a silent loss of information. We still
1009 * send the signal, but the *info bits are lost.
1011 trace_signal_lose_info(sig, group, info);
1015 out_set:
1016 signalfd_notify(t, sig);
1017 sigaddset(&pending->signal, sig);
1018 complete_signal(sig, t, group);
1019 return 0;
1022 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1023 int group)
1025 int from_ancestor_ns = 0;
1027 #ifdef CONFIG_PID_NS
1028 from_ancestor_ns = si_fromuser(info) &&
1029 !task_pid_nr_ns(current, task_active_pid_ns(t));
1030 #endif
1032 return __send_signal(sig, info, t, group, from_ancestor_ns);
1035 static void print_fatal_signal(struct pt_regs *regs, int signr)
1037 printk("%s/%d: potentially unexpected fatal signal %d.\n",
1038 current->comm, task_pid_nr(current), signr);
1040 #if defined(__i386__) && !defined(__arch_um__)
1041 printk("code at %08lx: ", regs->ip);
1043 int i;
1044 for (i = 0; i < 16; i++) {
1045 unsigned char insn;
1047 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1048 break;
1049 printk("%02x ", insn);
1052 #endif
1053 printk("\n");
1054 preempt_disable();
1055 show_regs(regs);
1056 preempt_enable();
1059 static int __init setup_print_fatal_signals(char *str)
1061 get_option (&str, &print_fatal_signals);
1063 return 1;
1066 __setup("print-fatal-signals=", setup_print_fatal_signals);
1069 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1071 return send_signal(sig, info, p, 1);
1074 static int
1075 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1077 return send_signal(sig, info, t, 0);
1080 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1081 bool group)
1083 unsigned long flags;
1084 int ret = -ESRCH;
1086 if (lock_task_sighand(p, &flags)) {
1087 ret = send_signal(sig, info, p, group);
1088 unlock_task_sighand(p, &flags);
1091 return ret;
1095 * Force a signal that the process can't ignore: if necessary
1096 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1098 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1099 * since we do not want to have a signal handler that was blocked
1100 * be invoked when user space had explicitly blocked it.
1102 * We don't want to have recursive SIGSEGV's etc, for example,
1103 * that is why we also clear SIGNAL_UNKILLABLE.
1106 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1108 unsigned long int flags;
1109 int ret, blocked, ignored;
1110 struct k_sigaction *action;
1112 spin_lock_irqsave(&t->sighand->siglock, flags);
1113 action = &t->sighand->action[sig-1];
1114 ignored = action->sa.sa_handler == SIG_IGN;
1115 blocked = sigismember(&t->blocked, sig);
1116 if (blocked || ignored) {
1117 action->sa.sa_handler = SIG_DFL;
1118 if (blocked) {
1119 sigdelset(&t->blocked, sig);
1120 recalc_sigpending_and_wake(t);
1123 if (action->sa.sa_handler == SIG_DFL)
1124 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1125 ret = specific_send_sig_info(sig, info, t);
1126 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1128 return ret;
1132 * Nuke all other threads in the group.
1134 int zap_other_threads(struct task_struct *p)
1136 struct task_struct *t = p;
1137 int count = 0;
1139 p->signal->group_stop_count = 0;
1141 while_each_thread(p, t) {
1142 count++;
1144 /* Don't bother with already dead threads */
1145 if (t->exit_state)
1146 continue;
1147 sigaddset(&t->pending.signal, SIGKILL);
1148 signal_wake_up(t, 1);
1151 return count;
1154 struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1155 unsigned long *flags)
1157 struct sighand_struct *sighand;
1159 rcu_read_lock();
1160 for (;;) {
1161 sighand = rcu_dereference(tsk->sighand);
1162 if (unlikely(sighand == NULL))
1163 break;
1165 spin_lock_irqsave(&sighand->siglock, *flags);
1166 if (likely(sighand == tsk->sighand))
1167 break;
1168 spin_unlock_irqrestore(&sighand->siglock, *flags);
1170 rcu_read_unlock();
1172 return sighand;
1176 * send signal info to all the members of a group
1178 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1180 int ret;
1182 rcu_read_lock();
1183 ret = check_kill_permission(sig, info, p);
1184 rcu_read_unlock();
1186 if (!ret && sig)
1187 ret = do_send_sig_info(sig, info, p, true);
1189 return ret;
1193 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1194 * control characters do (^C, ^Z etc)
1195 * - the caller must hold at least a readlock on tasklist_lock
1197 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1199 struct task_struct *p = NULL;
1200 int retval, success;
1202 success = 0;
1203 retval = -ESRCH;
1204 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1205 int err = group_send_sig_info(sig, info, p);
1206 success |= !err;
1207 retval = err;
1208 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1209 return success ? 0 : retval;
1212 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1214 int error = -ESRCH;
1215 struct task_struct *p;
1217 rcu_read_lock();
1218 retry:
1219 p = pid_task(pid, PIDTYPE_PID);
1220 if (p) {
1221 error = group_send_sig_info(sig, info, p);
1222 if (unlikely(error == -ESRCH))
1224 * The task was unhashed in between, try again.
1225 * If it is dead, pid_task() will return NULL,
1226 * if we race with de_thread() it will find the
1227 * new leader.
1229 goto retry;
1231 rcu_read_unlock();
1233 return error;
1237 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1239 int error;
1240 rcu_read_lock();
1241 error = kill_pid_info(sig, info, find_vpid(pid));
1242 rcu_read_unlock();
1243 return error;
1246 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1247 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1248 uid_t uid, uid_t euid, u32 secid)
1250 int ret = -EINVAL;
1251 struct task_struct *p;
1252 const struct cred *pcred;
1253 unsigned long flags;
1255 if (!valid_signal(sig))
1256 return ret;
1258 rcu_read_lock();
1259 p = pid_task(pid, PIDTYPE_PID);
1260 if (!p) {
1261 ret = -ESRCH;
1262 goto out_unlock;
1264 pcred = __task_cred(p);
1265 if (si_fromuser(info) &&
1266 euid != pcred->suid && euid != pcred->uid &&
1267 uid != pcred->suid && uid != pcred->uid) {
1268 ret = -EPERM;
1269 goto out_unlock;
1271 ret = security_task_kill(p, info, sig, secid);
1272 if (ret)
1273 goto out_unlock;
1275 if (sig) {
1276 if (lock_task_sighand(p, &flags)) {
1277 ret = __send_signal(sig, info, p, 1, 0);
1278 unlock_task_sighand(p, &flags);
1279 } else
1280 ret = -ESRCH;
1282 out_unlock:
1283 rcu_read_unlock();
1284 return ret;
1286 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1289 * kill_something_info() interprets pid in interesting ways just like kill(2).
1291 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1292 * is probably wrong. Should make it like BSD or SYSV.
1295 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1297 int ret;
1299 if (pid > 0) {
1300 rcu_read_lock();
1301 ret = kill_pid_info(sig, info, find_vpid(pid));
1302 rcu_read_unlock();
1303 return ret;
1306 read_lock(&tasklist_lock);
1307 if (pid != -1) {
1308 ret = __kill_pgrp_info(sig, info,
1309 pid ? find_vpid(-pid) : task_pgrp(current));
1310 } else {
1311 int retval = 0, count = 0;
1312 struct task_struct * p;
1314 for_each_process(p) {
1315 if (task_pid_vnr(p) > 1 &&
1316 !same_thread_group(p, current)) {
1317 int err = group_send_sig_info(sig, info, p);
1318 ++count;
1319 if (err != -EPERM)
1320 retval = err;
1323 ret = count ? retval : -ESRCH;
1325 read_unlock(&tasklist_lock);
1327 return ret;
1331 * These are for backward compatibility with the rest of the kernel source.
1335 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1338 * Make sure legacy kernel users don't send in bad values
1339 * (normal paths check this in check_kill_permission).
1341 if (!valid_signal(sig))
1342 return -EINVAL;
1344 return do_send_sig_info(sig, info, p, false);
1347 #define __si_special(priv) \
1348 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1351 send_sig(int sig, struct task_struct *p, int priv)
1353 return send_sig_info(sig, __si_special(priv), p);
1356 void
1357 force_sig(int sig, struct task_struct *p)
1359 force_sig_info(sig, SEND_SIG_PRIV, p);
1363 * When things go south during signal handling, we
1364 * will force a SIGSEGV. And if the signal that caused
1365 * the problem was already a SIGSEGV, we'll want to
1366 * make sure we don't even try to deliver the signal..
1369 force_sigsegv(int sig, struct task_struct *p)
1371 if (sig == SIGSEGV) {
1372 unsigned long flags;
1373 spin_lock_irqsave(&p->sighand->siglock, flags);
1374 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1375 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1377 force_sig(SIGSEGV, p);
1378 return 0;
1381 int kill_pgrp(struct pid *pid, int sig, int priv)
1383 int ret;
1385 read_lock(&tasklist_lock);
1386 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1387 read_unlock(&tasklist_lock);
1389 return ret;
1391 EXPORT_SYMBOL(kill_pgrp);
1393 int kill_pid(struct pid *pid, int sig, int priv)
1395 return kill_pid_info(sig, __si_special(priv), pid);
1397 EXPORT_SYMBOL(kill_pid);
1400 * These functions support sending signals using preallocated sigqueue
1401 * structures. This is needed "because realtime applications cannot
1402 * afford to lose notifications of asynchronous events, like timer
1403 * expirations or I/O completions". In the case of Posix Timers
1404 * we allocate the sigqueue structure from the timer_create. If this
1405 * allocation fails we are able to report the failure to the application
1406 * with an EAGAIN error.
1408 struct sigqueue *sigqueue_alloc(void)
1410 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1412 if (q)
1413 q->flags |= SIGQUEUE_PREALLOC;
1415 return q;
1418 void sigqueue_free(struct sigqueue *q)
1420 unsigned long flags;
1421 spinlock_t *lock = &current->sighand->siglock;
1423 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1425 * We must hold ->siglock while testing q->list
1426 * to serialize with collect_signal() or with
1427 * __exit_signal()->flush_sigqueue().
1429 spin_lock_irqsave(lock, flags);
1430 q->flags &= ~SIGQUEUE_PREALLOC;
1432 * If it is queued it will be freed when dequeued,
1433 * like the "regular" sigqueue.
1435 if (!list_empty(&q->list))
1436 q = NULL;
1437 spin_unlock_irqrestore(lock, flags);
1439 if (q)
1440 __sigqueue_free(q);
1443 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1445 int sig = q->info.si_signo;
1446 struct sigpending *pending;
1447 unsigned long flags;
1448 int ret;
1450 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1452 ret = -1;
1453 if (!likely(lock_task_sighand(t, &flags)))
1454 goto ret;
1456 ret = 1; /* the signal is ignored */
1457 if (!prepare_signal(sig, t, 0))
1458 goto out;
1460 ret = 0;
1461 if (unlikely(!list_empty(&q->list))) {
1463 * If an SI_TIMER entry is already queue just increment
1464 * the overrun count.
1466 BUG_ON(q->info.si_code != SI_TIMER);
1467 q->info.si_overrun++;
1468 goto out;
1470 q->info.si_overrun = 0;
1472 signalfd_notify(t, sig);
1473 pending = group ? &t->signal->shared_pending : &t->pending;
1474 list_add_tail(&q->list, &pending->list);
1475 sigaddset(&pending->signal, sig);
1476 complete_signal(sig, t, group);
1477 out:
1478 unlock_task_sighand(t, &flags);
1479 ret:
1480 return ret;
1484 * Let a parent know about the death of a child.
1485 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1487 * Returns -1 if our parent ignored us and so we've switched to
1488 * self-reaping, or else @sig.
1490 int do_notify_parent(struct task_struct *tsk, int sig)
1492 struct siginfo info;
1493 unsigned long flags;
1494 struct sighand_struct *psig;
1495 int ret = sig;
1497 BUG_ON(sig == -1);
1499 /* do_notify_parent_cldstop should have been called instead. */
1500 BUG_ON(task_is_stopped_or_traced(tsk));
1502 BUG_ON(!task_ptrace(tsk) &&
1503 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1505 info.si_signo = sig;
1506 info.si_errno = 0;
1508 * we are under tasklist_lock here so our parent is tied to
1509 * us and cannot exit and release its namespace.
1511 * the only it can is to switch its nsproxy with sys_unshare,
1512 * bu uncharing pid namespaces is not allowed, so we'll always
1513 * see relevant namespace
1515 * write_lock() currently calls preempt_disable() which is the
1516 * same as rcu_read_lock(), but according to Oleg, this is not
1517 * correct to rely on this
1519 rcu_read_lock();
1520 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1521 info.si_uid = __task_cred(tsk)->uid;
1522 rcu_read_unlock();
1524 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1525 tsk->signal->utime));
1526 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1527 tsk->signal->stime));
1529 info.si_status = tsk->exit_code & 0x7f;
1530 if (tsk->exit_code & 0x80)
1531 info.si_code = CLD_DUMPED;
1532 else if (tsk->exit_code & 0x7f)
1533 info.si_code = CLD_KILLED;
1534 else {
1535 info.si_code = CLD_EXITED;
1536 info.si_status = tsk->exit_code >> 8;
1539 psig = tsk->parent->sighand;
1540 spin_lock_irqsave(&psig->siglock, flags);
1541 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1542 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1543 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1545 * We are exiting and our parent doesn't care. POSIX.1
1546 * defines special semantics for setting SIGCHLD to SIG_IGN
1547 * or setting the SA_NOCLDWAIT flag: we should be reaped
1548 * automatically and not left for our parent's wait4 call.
1549 * Rather than having the parent do it as a magic kind of
1550 * signal handler, we just set this to tell do_exit that we
1551 * can be cleaned up without becoming a zombie. Note that
1552 * we still call __wake_up_parent in this case, because a
1553 * blocked sys_wait4 might now return -ECHILD.
1555 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1556 * is implementation-defined: we do (if you don't want
1557 * it, just use SIG_IGN instead).
1559 ret = tsk->exit_signal = -1;
1560 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1561 sig = -1;
1563 if (valid_signal(sig) && sig > 0)
1564 __group_send_sig_info(sig, &info, tsk->parent);
1565 __wake_up_parent(tsk, tsk->parent);
1566 spin_unlock_irqrestore(&psig->siglock, flags);
1568 return ret;
1571 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1573 struct siginfo info;
1574 unsigned long flags;
1575 struct task_struct *parent;
1576 struct sighand_struct *sighand;
1578 if (task_ptrace(tsk))
1579 parent = tsk->parent;
1580 else {
1581 tsk = tsk->group_leader;
1582 parent = tsk->real_parent;
1585 info.si_signo = SIGCHLD;
1586 info.si_errno = 0;
1588 * see comment in do_notify_parent() abot the following 3 lines
1590 rcu_read_lock();
1591 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1592 info.si_uid = __task_cred(tsk)->uid;
1593 rcu_read_unlock();
1595 info.si_utime = cputime_to_clock_t(tsk->utime);
1596 info.si_stime = cputime_to_clock_t(tsk->stime);
1598 info.si_code = why;
1599 switch (why) {
1600 case CLD_CONTINUED:
1601 info.si_status = SIGCONT;
1602 break;
1603 case CLD_STOPPED:
1604 info.si_status = tsk->signal->group_exit_code & 0x7f;
1605 break;
1606 case CLD_TRAPPED:
1607 info.si_status = tsk->exit_code & 0x7f;
1608 break;
1609 default:
1610 BUG();
1613 sighand = parent->sighand;
1614 spin_lock_irqsave(&sighand->siglock, flags);
1615 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1616 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1617 __group_send_sig_info(SIGCHLD, &info, parent);
1619 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1621 __wake_up_parent(tsk, parent);
1622 spin_unlock_irqrestore(&sighand->siglock, flags);
1625 static inline int may_ptrace_stop(void)
1627 if (!likely(task_ptrace(current)))
1628 return 0;
1630 * Are we in the middle of do_coredump?
1631 * If so and our tracer is also part of the coredump stopping
1632 * is a deadlock situation, and pointless because our tracer
1633 * is dead so don't allow us to stop.
1634 * If SIGKILL was already sent before the caller unlocked
1635 * ->siglock we must see ->core_state != NULL. Otherwise it
1636 * is safe to enter schedule().
1638 if (unlikely(current->mm->core_state) &&
1639 unlikely(current->mm == current->parent->mm))
1640 return 0;
1642 return 1;
1646 * Return nonzero if there is a SIGKILL that should be waking us up.
1647 * Called with the siglock held.
1649 static int sigkill_pending(struct task_struct *tsk)
1651 return sigismember(&tsk->pending.signal, SIGKILL) ||
1652 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1656 * This must be called with current->sighand->siglock held.
1658 * This should be the path for all ptrace stops.
1659 * We always set current->last_siginfo while stopped here.
1660 * That makes it a way to test a stopped process for
1661 * being ptrace-stopped vs being job-control-stopped.
1663 * If we actually decide not to stop at all because the tracer
1664 * is gone, we keep current->exit_code unless clear_code.
1666 static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
1667 __releases(&current->sighand->siglock)
1668 __acquires(&current->sighand->siglock)
1670 if (arch_ptrace_stop_needed(exit_code, info)) {
1672 * The arch code has something special to do before a
1673 * ptrace stop. This is allowed to block, e.g. for faults
1674 * on user stack pages. We can't keep the siglock while
1675 * calling arch_ptrace_stop, so we must release it now.
1676 * To preserve proper semantics, we must do this before
1677 * any signal bookkeeping like checking group_stop_count.
1678 * Meanwhile, a SIGKILL could come in before we retake the
1679 * siglock. That must prevent us from sleeping in TASK_TRACED.
1680 * So after regaining the lock, we must check for SIGKILL.
1682 spin_unlock_irq(&current->sighand->siglock);
1683 arch_ptrace_stop(exit_code, info);
1684 spin_lock_irq(&current->sighand->siglock);
1685 if (sigkill_pending(current))
1686 return;
1690 * If there is a group stop in progress,
1691 * we must participate in the bookkeeping.
1693 if (current->signal->group_stop_count > 0)
1694 task_participate_group_stop(current);
1696 current->last_siginfo = info;
1697 current->exit_code = exit_code;
1699 /* Let the debugger run. */
1700 __set_current_state(TASK_TRACED);
1701 spin_unlock_irq(&current->sighand->siglock);
1702 read_lock(&tasklist_lock);
1703 if (may_ptrace_stop()) {
1704 do_notify_parent_cldstop(current, why);
1706 * Don't want to allow preemption here, because
1707 * sys_ptrace() needs this task to be inactive.
1709 * XXX: implement read_unlock_no_resched().
1711 preempt_disable();
1712 read_unlock(&tasklist_lock);
1713 preempt_enable_no_resched();
1714 schedule();
1715 } else {
1717 * By the time we got the lock, our tracer went away.
1718 * Don't drop the lock yet, another tracer may come.
1720 __set_current_state(TASK_RUNNING);
1721 if (clear_code)
1722 current->exit_code = 0;
1723 read_unlock(&tasklist_lock);
1727 * While in TASK_TRACED, we were considered "frozen enough".
1728 * Now that we woke up, it's crucial if we're supposed to be
1729 * frozen that we freeze now before running anything substantial.
1731 try_to_freeze();
1734 * We are back. Now reacquire the siglock before touching
1735 * last_siginfo, so that we are sure to have synchronized with
1736 * any signal-sending on another CPU that wants to examine it.
1738 spin_lock_irq(&current->sighand->siglock);
1739 current->last_siginfo = NULL;
1742 * Queued signals ignored us while we were stopped for tracing.
1743 * So check for any that we should take before resuming user mode.
1744 * This sets TIF_SIGPENDING, but never clears it.
1746 recalc_sigpending_tsk(current);
1749 void ptrace_notify(int exit_code)
1751 siginfo_t info;
1753 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1755 memset(&info, 0, sizeof info);
1756 info.si_signo = SIGTRAP;
1757 info.si_code = exit_code;
1758 info.si_pid = task_pid_vnr(current);
1759 info.si_uid = current_uid();
1761 /* Let the debugger run. */
1762 spin_lock_irq(&current->sighand->siglock);
1763 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
1764 spin_unlock_irq(&current->sighand->siglock);
1768 * This performs the stopping for SIGSTOP and other stop signals.
1769 * We have to stop all threads in the thread group.
1770 * Returns nonzero if we've actually stopped and released the siglock.
1771 * Returns zero if we didn't stop and still hold the siglock.
1773 static int do_signal_stop(int signr)
1775 struct signal_struct *sig = current->signal;
1776 int notify = 0;
1778 if (!sig->group_stop_count) {
1779 unsigned int gstop = GROUP_STOP_CONSUME;
1780 struct task_struct *t;
1782 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1783 unlikely(signal_group_exit(sig)))
1784 return 0;
1786 * There is no group stop already in progress.
1787 * We must initiate one now.
1789 sig->group_exit_code = signr;
1791 current->group_stop = gstop;
1792 sig->group_stop_count = 1;
1793 for (t = next_thread(current); t != current; t = next_thread(t))
1795 * Setting state to TASK_STOPPED for a group
1796 * stop is always done with the siglock held,
1797 * so this check has no races.
1799 if (!(t->flags & PF_EXITING) &&
1800 !task_is_stopped_or_traced(t)) {
1801 t->group_stop = gstop;
1802 sig->group_stop_count++;
1803 signal_wake_up(t, 0);
1804 } else
1805 task_clear_group_stop_pending(t);
1808 * If there are no other threads in the group, or if there is
1809 * a group stop in progress and we are the last to stop, report
1810 * to the parent. When ptraced, every thread reports itself.
1812 if (task_participate_group_stop(current))
1813 notify = CLD_STOPPED;
1814 if (task_ptrace(current))
1815 notify = CLD_STOPPED;
1817 current->exit_code = sig->group_exit_code;
1818 __set_current_state(TASK_STOPPED);
1820 spin_unlock_irq(&current->sighand->siglock);
1822 if (notify) {
1823 read_lock(&tasklist_lock);
1824 do_notify_parent_cldstop(current, notify);
1825 read_unlock(&tasklist_lock);
1828 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1829 schedule();
1831 tracehook_finish_jctl();
1832 current->exit_code = 0;
1834 return 1;
1837 static int ptrace_signal(int signr, siginfo_t *info,
1838 struct pt_regs *regs, void *cookie)
1840 if (!task_ptrace(current))
1841 return signr;
1843 ptrace_signal_deliver(regs, cookie);
1845 /* Let the debugger run. */
1846 ptrace_stop(signr, CLD_TRAPPED, 0, info);
1848 /* We're back. Did the debugger cancel the sig? */
1849 signr = current->exit_code;
1850 if (signr == 0)
1851 return signr;
1853 current->exit_code = 0;
1855 /* Update the siginfo structure if the signal has
1856 changed. If the debugger wanted something
1857 specific in the siginfo structure then it should
1858 have updated *info via PTRACE_SETSIGINFO. */
1859 if (signr != info->si_signo) {
1860 info->si_signo = signr;
1861 info->si_errno = 0;
1862 info->si_code = SI_USER;
1863 info->si_pid = task_pid_vnr(current->parent);
1864 info->si_uid = task_uid(current->parent);
1867 /* If the (new) signal is now blocked, requeue it. */
1868 if (sigismember(&current->blocked, signr)) {
1869 specific_send_sig_info(signr, info, current);
1870 signr = 0;
1873 return signr;
1876 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1877 struct pt_regs *regs, void *cookie)
1879 struct sighand_struct *sighand = current->sighand;
1880 struct signal_struct *signal = current->signal;
1881 int signr;
1883 relock:
1885 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1886 * While in TASK_STOPPED, we were considered "frozen enough".
1887 * Now that we woke up, it's crucial if we're supposed to be
1888 * frozen that we freeze now before running anything substantial.
1890 try_to_freeze();
1892 spin_lock_irq(&sighand->siglock);
1894 * Every stopped thread goes here after wakeup. Check to see if
1895 * we should notify the parent, prepare_signal(SIGCONT) encodes
1896 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1898 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1899 int why;
1901 if (signal->flags & SIGNAL_CLD_CONTINUED)
1902 why = CLD_CONTINUED;
1903 else
1904 why = CLD_STOPPED;
1906 signal->flags &= ~SIGNAL_CLD_MASK;
1908 spin_unlock_irq(&sighand->siglock);
1910 read_lock(&tasklist_lock);
1911 do_notify_parent_cldstop(current->group_leader, why);
1912 read_unlock(&tasklist_lock);
1913 goto relock;
1916 for (;;) {
1917 struct k_sigaction *ka;
1919 * Tracing can induce an artifical signal and choose sigaction.
1920 * The return value in @signr determines the default action,
1921 * but @info->si_signo is the signal number we will report.
1923 signr = tracehook_get_signal(current, regs, info, return_ka);
1924 if (unlikely(signr < 0))
1925 goto relock;
1926 if (unlikely(signr != 0))
1927 ka = return_ka;
1928 else {
1929 if (unlikely(signal->group_stop_count > 0) &&
1930 do_signal_stop(0))
1931 goto relock;
1933 signr = dequeue_signal(current, &current->blocked,
1934 info);
1936 if (!signr)
1937 break; /* will return 0 */
1939 if (signr != SIGKILL) {
1940 signr = ptrace_signal(signr, info,
1941 regs, cookie);
1942 if (!signr)
1943 continue;
1946 ka = &sighand->action[signr-1];
1949 /* Trace actually delivered signals. */
1950 trace_signal_deliver(signr, info, ka);
1952 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1953 continue;
1954 if (ka->sa.sa_handler != SIG_DFL) {
1955 /* Run the handler. */
1956 *return_ka = *ka;
1958 if (ka->sa.sa_flags & SA_ONESHOT)
1959 ka->sa.sa_handler = SIG_DFL;
1961 break; /* will return non-zero "signr" value */
1965 * Now we are doing the default action for this signal.
1967 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1968 continue;
1971 * Global init gets no signals it doesn't want.
1972 * Container-init gets no signals it doesn't want from same
1973 * container.
1975 * Note that if global/container-init sees a sig_kernel_only()
1976 * signal here, the signal must have been generated internally
1977 * or must have come from an ancestor namespace. In either
1978 * case, the signal cannot be dropped.
1980 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1981 !sig_kernel_only(signr))
1982 continue;
1984 if (sig_kernel_stop(signr)) {
1986 * The default action is to stop all threads in
1987 * the thread group. The job control signals
1988 * do nothing in an orphaned pgrp, but SIGSTOP
1989 * always works. Note that siglock needs to be
1990 * dropped during the call to is_orphaned_pgrp()
1991 * because of lock ordering with tasklist_lock.
1992 * This allows an intervening SIGCONT to be posted.
1993 * We need to check for that and bail out if necessary.
1995 if (signr != SIGSTOP) {
1996 spin_unlock_irq(&sighand->siglock);
1998 /* signals can be posted during this window */
2000 if (is_current_pgrp_orphaned())
2001 goto relock;
2003 spin_lock_irq(&sighand->siglock);
2006 if (likely(do_signal_stop(info->si_signo))) {
2007 /* It released the siglock. */
2008 goto relock;
2012 * We didn't actually stop, due to a race
2013 * with SIGCONT or something like that.
2015 continue;
2018 spin_unlock_irq(&sighand->siglock);
2021 * Anything else is fatal, maybe with a core dump.
2023 current->flags |= PF_SIGNALED;
2025 if (sig_kernel_coredump(signr)) {
2026 if (print_fatal_signals)
2027 print_fatal_signal(regs, info->si_signo);
2029 * If it was able to dump core, this kills all
2030 * other threads in the group and synchronizes with
2031 * their demise. If we lost the race with another
2032 * thread getting here, it set group_exit_code
2033 * first and our do_group_exit call below will use
2034 * that value and ignore the one we pass it.
2036 do_coredump(info->si_signo, info->si_signo, regs);
2040 * Death signals, no core dump.
2042 do_group_exit(info->si_signo);
2043 /* NOTREACHED */
2045 spin_unlock_irq(&sighand->siglock);
2046 return signr;
2049 void exit_signals(struct task_struct *tsk)
2051 int group_stop = 0;
2052 struct task_struct *t;
2054 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2055 tsk->flags |= PF_EXITING;
2056 return;
2059 spin_lock_irq(&tsk->sighand->siglock);
2061 * From now this task is not visible for group-wide signals,
2062 * see wants_signal(), do_signal_stop().
2064 tsk->flags |= PF_EXITING;
2065 if (!signal_pending(tsk))
2066 goto out;
2068 /* It could be that __group_complete_signal() choose us to
2069 * notify about group-wide signal. Another thread should be
2070 * woken now to take the signal since we will not.
2072 for (t = tsk; (t = next_thread(t)) != tsk; )
2073 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2074 recalc_sigpending_and_wake(t);
2076 if (unlikely(tsk->signal->group_stop_count) &&
2077 task_participate_group_stop(tsk))
2078 group_stop = CLD_STOPPED;
2079 out:
2080 spin_unlock_irq(&tsk->sighand->siglock);
2082 if (unlikely(group_stop)) {
2083 read_lock(&tasklist_lock);
2084 do_notify_parent_cldstop(tsk, group_stop);
2085 read_unlock(&tasklist_lock);
2089 EXPORT_SYMBOL(recalc_sigpending);
2090 EXPORT_SYMBOL_GPL(dequeue_signal);
2091 EXPORT_SYMBOL(flush_signals);
2092 EXPORT_SYMBOL(force_sig);
2093 EXPORT_SYMBOL(send_sig);
2094 EXPORT_SYMBOL(send_sig_info);
2095 EXPORT_SYMBOL(sigprocmask);
2096 EXPORT_SYMBOL(block_all_signals);
2097 EXPORT_SYMBOL(unblock_all_signals);
2101 * System call entry points.
2104 SYSCALL_DEFINE0(restart_syscall)
2106 struct restart_block *restart = &current_thread_info()->restart_block;
2107 return restart->fn(restart);
2110 long do_no_restart_syscall(struct restart_block *param)
2112 return -EINTR;
2116 * We don't need to get the kernel lock - this is all local to this
2117 * particular thread.. (and that's good, because this is _heavily_
2118 * used by various programs)
2122 * This is also useful for kernel threads that want to temporarily
2123 * (or permanently) block certain signals.
2125 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2126 * interface happily blocks "unblockable" signals like SIGKILL
2127 * and friends.
2129 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2131 int error;
2133 spin_lock_irq(&current->sighand->siglock);
2134 if (oldset)
2135 *oldset = current->blocked;
2137 error = 0;
2138 switch (how) {
2139 case SIG_BLOCK:
2140 sigorsets(&current->blocked, &current->blocked, set);
2141 break;
2142 case SIG_UNBLOCK:
2143 signandsets(&current->blocked, &current->blocked, set);
2144 break;
2145 case SIG_SETMASK:
2146 current->blocked = *set;
2147 break;
2148 default:
2149 error = -EINVAL;
2151 recalc_sigpending();
2152 spin_unlock_irq(&current->sighand->siglock);
2154 return error;
2157 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2158 sigset_t __user *, oset, size_t, sigsetsize)
2160 int error = -EINVAL;
2161 sigset_t old_set, new_set;
2163 /* XXX: Don't preclude handling different sized sigset_t's. */
2164 if (sigsetsize != sizeof(sigset_t))
2165 goto out;
2167 if (set) {
2168 error = -EFAULT;
2169 if (copy_from_user(&new_set, set, sizeof(*set)))
2170 goto out;
2171 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2173 error = sigprocmask(how, &new_set, &old_set);
2174 if (error)
2175 goto out;
2176 if (oset)
2177 goto set_old;
2178 } else if (oset) {
2179 spin_lock_irq(&current->sighand->siglock);
2180 old_set = current->blocked;
2181 spin_unlock_irq(&current->sighand->siglock);
2183 set_old:
2184 error = -EFAULT;
2185 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2186 goto out;
2188 error = 0;
2189 out:
2190 return error;
2193 long do_sigpending(void __user *set, unsigned long sigsetsize)
2195 long error = -EINVAL;
2196 sigset_t pending;
2198 if (sigsetsize > sizeof(sigset_t))
2199 goto out;
2201 spin_lock_irq(&current->sighand->siglock);
2202 sigorsets(&pending, &current->pending.signal,
2203 &current->signal->shared_pending.signal);
2204 spin_unlock_irq(&current->sighand->siglock);
2206 /* Outside the lock because only this thread touches it. */
2207 sigandsets(&pending, &current->blocked, &pending);
2209 error = -EFAULT;
2210 if (!copy_to_user(set, &pending, sigsetsize))
2211 error = 0;
2213 out:
2214 return error;
2217 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2219 return do_sigpending(set, sigsetsize);
2222 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2224 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2226 int err;
2228 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2229 return -EFAULT;
2230 if (from->si_code < 0)
2231 return __copy_to_user(to, from, sizeof(siginfo_t))
2232 ? -EFAULT : 0;
2234 * If you change siginfo_t structure, please be sure
2235 * this code is fixed accordingly.
2236 * Please remember to update the signalfd_copyinfo() function
2237 * inside fs/signalfd.c too, in case siginfo_t changes.
2238 * It should never copy any pad contained in the structure
2239 * to avoid security leaks, but must copy the generic
2240 * 3 ints plus the relevant union member.
2242 err = __put_user(from->si_signo, &to->si_signo);
2243 err |= __put_user(from->si_errno, &to->si_errno);
2244 err |= __put_user((short)from->si_code, &to->si_code);
2245 switch (from->si_code & __SI_MASK) {
2246 case __SI_KILL:
2247 err |= __put_user(from->si_pid, &to->si_pid);
2248 err |= __put_user(from->si_uid, &to->si_uid);
2249 break;
2250 case __SI_TIMER:
2251 err |= __put_user(from->si_tid, &to->si_tid);
2252 err |= __put_user(from->si_overrun, &to->si_overrun);
2253 err |= __put_user(from->si_ptr, &to->si_ptr);
2254 break;
2255 case __SI_POLL:
2256 err |= __put_user(from->si_band, &to->si_band);
2257 err |= __put_user(from->si_fd, &to->si_fd);
2258 break;
2259 case __SI_FAULT:
2260 err |= __put_user(from->si_addr, &to->si_addr);
2261 #ifdef __ARCH_SI_TRAPNO
2262 err |= __put_user(from->si_trapno, &to->si_trapno);
2263 #endif
2264 #ifdef BUS_MCEERR_AO
2266 * Other callers might not initialize the si_lsb field,
2267 * so check explicitely for the right codes here.
2269 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2270 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2271 #endif
2272 break;
2273 case __SI_CHLD:
2274 err |= __put_user(from->si_pid, &to->si_pid);
2275 err |= __put_user(from->si_uid, &to->si_uid);
2276 err |= __put_user(from->si_status, &to->si_status);
2277 err |= __put_user(from->si_utime, &to->si_utime);
2278 err |= __put_user(from->si_stime, &to->si_stime);
2279 break;
2280 case __SI_RT: /* This is not generated by the kernel as of now. */
2281 case __SI_MESGQ: /* But this is */
2282 err |= __put_user(from->si_pid, &to->si_pid);
2283 err |= __put_user(from->si_uid, &to->si_uid);
2284 err |= __put_user(from->si_ptr, &to->si_ptr);
2285 break;
2286 default: /* this is just in case for now ... */
2287 err |= __put_user(from->si_pid, &to->si_pid);
2288 err |= __put_user(from->si_uid, &to->si_uid);
2289 break;
2291 return err;
2294 #endif
2296 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2297 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2298 size_t, sigsetsize)
2300 int ret, sig;
2301 sigset_t these;
2302 struct timespec ts;
2303 siginfo_t info;
2304 long timeout = 0;
2306 /* XXX: Don't preclude handling different sized sigset_t's. */
2307 if (sigsetsize != sizeof(sigset_t))
2308 return -EINVAL;
2310 if (copy_from_user(&these, uthese, sizeof(these)))
2311 return -EFAULT;
2314 * Invert the set of allowed signals to get those we
2315 * want to block.
2317 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2318 signotset(&these);
2320 if (uts) {
2321 if (copy_from_user(&ts, uts, sizeof(ts)))
2322 return -EFAULT;
2323 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2324 || ts.tv_sec < 0)
2325 return -EINVAL;
2328 spin_lock_irq(&current->sighand->siglock);
2329 sig = dequeue_signal(current, &these, &info);
2330 if (!sig) {
2331 timeout = MAX_SCHEDULE_TIMEOUT;
2332 if (uts)
2333 timeout = (timespec_to_jiffies(&ts)
2334 + (ts.tv_sec || ts.tv_nsec));
2336 if (timeout) {
2337 /* None ready -- temporarily unblock those we're
2338 * interested while we are sleeping in so that we'll
2339 * be awakened when they arrive. */
2340 current->real_blocked = current->blocked;
2341 sigandsets(&current->blocked, &current->blocked, &these);
2342 recalc_sigpending();
2343 spin_unlock_irq(&current->sighand->siglock);
2345 timeout = schedule_timeout_interruptible(timeout);
2347 spin_lock_irq(&current->sighand->siglock);
2348 sig = dequeue_signal(current, &these, &info);
2349 current->blocked = current->real_blocked;
2350 siginitset(&current->real_blocked, 0);
2351 recalc_sigpending();
2354 spin_unlock_irq(&current->sighand->siglock);
2356 if (sig) {
2357 ret = sig;
2358 if (uinfo) {
2359 if (copy_siginfo_to_user(uinfo, &info))
2360 ret = -EFAULT;
2362 } else {
2363 ret = -EAGAIN;
2364 if (timeout)
2365 ret = -EINTR;
2368 return ret;
2371 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2373 struct siginfo info;
2375 info.si_signo = sig;
2376 info.si_errno = 0;
2377 info.si_code = SI_USER;
2378 info.si_pid = task_tgid_vnr(current);
2379 info.si_uid = current_uid();
2381 return kill_something_info(sig, &info, pid);
2384 static int
2385 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2387 struct task_struct *p;
2388 int error = -ESRCH;
2390 rcu_read_lock();
2391 p = find_task_by_vpid(pid);
2392 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2393 error = check_kill_permission(sig, info, p);
2395 * The null signal is a permissions and process existence
2396 * probe. No signal is actually delivered.
2398 if (!error && sig) {
2399 error = do_send_sig_info(sig, info, p, false);
2401 * If lock_task_sighand() failed we pretend the task
2402 * dies after receiving the signal. The window is tiny,
2403 * and the signal is private anyway.
2405 if (unlikely(error == -ESRCH))
2406 error = 0;
2409 rcu_read_unlock();
2411 return error;
2414 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2416 struct siginfo info;
2418 info.si_signo = sig;
2419 info.si_errno = 0;
2420 info.si_code = SI_TKILL;
2421 info.si_pid = task_tgid_vnr(current);
2422 info.si_uid = current_uid();
2424 return do_send_specific(tgid, pid, sig, &info);
2428 * sys_tgkill - send signal to one specific thread
2429 * @tgid: the thread group ID of the thread
2430 * @pid: the PID of the thread
2431 * @sig: signal to be sent
2433 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2434 * exists but it's not belonging to the target process anymore. This
2435 * method solves the problem of threads exiting and PIDs getting reused.
2437 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2439 /* This is only valid for single tasks */
2440 if (pid <= 0 || tgid <= 0)
2441 return -EINVAL;
2443 return do_tkill(tgid, pid, sig);
2447 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2449 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2451 /* This is only valid for single tasks */
2452 if (pid <= 0)
2453 return -EINVAL;
2455 return do_tkill(0, pid, sig);
2458 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2459 siginfo_t __user *, uinfo)
2461 siginfo_t info;
2463 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2464 return -EFAULT;
2466 /* Not even root can pretend to send signals from the kernel.
2467 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2469 if (info.si_code != SI_QUEUE) {
2470 /* We used to allow any < 0 si_code */
2471 WARN_ON_ONCE(info.si_code < 0);
2472 return -EPERM;
2474 info.si_signo = sig;
2476 /* POSIX.1b doesn't mention process groups. */
2477 return kill_proc_info(sig, &info, pid);
2480 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2482 /* This is only valid for single tasks */
2483 if (pid <= 0 || tgid <= 0)
2484 return -EINVAL;
2486 /* Not even root can pretend to send signals from the kernel.
2487 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2489 if (info->si_code != SI_QUEUE) {
2490 /* We used to allow any < 0 si_code */
2491 WARN_ON_ONCE(info->si_code < 0);
2492 return -EPERM;
2494 info->si_signo = sig;
2496 return do_send_specific(tgid, pid, sig, info);
2499 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2500 siginfo_t __user *, uinfo)
2502 siginfo_t info;
2504 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2505 return -EFAULT;
2507 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2510 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2512 struct task_struct *t = current;
2513 struct k_sigaction *k;
2514 sigset_t mask;
2516 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2517 return -EINVAL;
2519 k = &t->sighand->action[sig-1];
2521 spin_lock_irq(&current->sighand->siglock);
2522 if (oact)
2523 *oact = *k;
2525 if (act) {
2526 sigdelsetmask(&act->sa.sa_mask,
2527 sigmask(SIGKILL) | sigmask(SIGSTOP));
2528 *k = *act;
2530 * POSIX 3.3.1.3:
2531 * "Setting a signal action to SIG_IGN for a signal that is
2532 * pending shall cause the pending signal to be discarded,
2533 * whether or not it is blocked."
2535 * "Setting a signal action to SIG_DFL for a signal that is
2536 * pending and whose default action is to ignore the signal
2537 * (for example, SIGCHLD), shall cause the pending signal to
2538 * be discarded, whether or not it is blocked"
2540 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2541 sigemptyset(&mask);
2542 sigaddset(&mask, sig);
2543 rm_from_queue_full(&mask, &t->signal->shared_pending);
2544 do {
2545 rm_from_queue_full(&mask, &t->pending);
2546 t = next_thread(t);
2547 } while (t != current);
2551 spin_unlock_irq(&current->sighand->siglock);
2552 return 0;
2555 int
2556 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2558 stack_t oss;
2559 int error;
2561 oss.ss_sp = (void __user *) current->sas_ss_sp;
2562 oss.ss_size = current->sas_ss_size;
2563 oss.ss_flags = sas_ss_flags(sp);
2565 if (uss) {
2566 void __user *ss_sp;
2567 size_t ss_size;
2568 int ss_flags;
2570 error = -EFAULT;
2571 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2572 goto out;
2573 error = __get_user(ss_sp, &uss->ss_sp) |
2574 __get_user(ss_flags, &uss->ss_flags) |
2575 __get_user(ss_size, &uss->ss_size);
2576 if (error)
2577 goto out;
2579 error = -EPERM;
2580 if (on_sig_stack(sp))
2581 goto out;
2583 error = -EINVAL;
2586 * Note - this code used to test ss_flags incorrectly
2587 * old code may have been written using ss_flags==0
2588 * to mean ss_flags==SS_ONSTACK (as this was the only
2589 * way that worked) - this fix preserves that older
2590 * mechanism
2592 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2593 goto out;
2595 if (ss_flags == SS_DISABLE) {
2596 ss_size = 0;
2597 ss_sp = NULL;
2598 } else {
2599 error = -ENOMEM;
2600 if (ss_size < MINSIGSTKSZ)
2601 goto out;
2604 current->sas_ss_sp = (unsigned long) ss_sp;
2605 current->sas_ss_size = ss_size;
2608 error = 0;
2609 if (uoss) {
2610 error = -EFAULT;
2611 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2612 goto out;
2613 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2614 __put_user(oss.ss_size, &uoss->ss_size) |
2615 __put_user(oss.ss_flags, &uoss->ss_flags);
2618 out:
2619 return error;
2622 #ifdef __ARCH_WANT_SYS_SIGPENDING
2624 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2626 return do_sigpending(set, sizeof(*set));
2629 #endif
2631 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2632 /* Some platforms have their own version with special arguments others
2633 support only sys_rt_sigprocmask. */
2635 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2636 old_sigset_t __user *, oset)
2638 int error;
2639 old_sigset_t old_set, new_set;
2641 if (set) {
2642 error = -EFAULT;
2643 if (copy_from_user(&new_set, set, sizeof(*set)))
2644 goto out;
2645 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2647 spin_lock_irq(&current->sighand->siglock);
2648 old_set = current->blocked.sig[0];
2650 error = 0;
2651 switch (how) {
2652 default:
2653 error = -EINVAL;
2654 break;
2655 case SIG_BLOCK:
2656 sigaddsetmask(&current->blocked, new_set);
2657 break;
2658 case SIG_UNBLOCK:
2659 sigdelsetmask(&current->blocked, new_set);
2660 break;
2661 case SIG_SETMASK:
2662 current->blocked.sig[0] = new_set;
2663 break;
2666 recalc_sigpending();
2667 spin_unlock_irq(&current->sighand->siglock);
2668 if (error)
2669 goto out;
2670 if (oset)
2671 goto set_old;
2672 } else if (oset) {
2673 old_set = current->blocked.sig[0];
2674 set_old:
2675 error = -EFAULT;
2676 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2677 goto out;
2679 error = 0;
2680 out:
2681 return error;
2683 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2685 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2686 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2687 const struct sigaction __user *, act,
2688 struct sigaction __user *, oact,
2689 size_t, sigsetsize)
2691 struct k_sigaction new_sa, old_sa;
2692 int ret = -EINVAL;
2694 /* XXX: Don't preclude handling different sized sigset_t's. */
2695 if (sigsetsize != sizeof(sigset_t))
2696 goto out;
2698 if (act) {
2699 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2700 return -EFAULT;
2703 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2705 if (!ret && oact) {
2706 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2707 return -EFAULT;
2709 out:
2710 return ret;
2712 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2714 #ifdef __ARCH_WANT_SYS_SGETMASK
2717 * For backwards compatibility. Functionality superseded by sigprocmask.
2719 SYSCALL_DEFINE0(sgetmask)
2721 /* SMP safe */
2722 return current->blocked.sig[0];
2725 SYSCALL_DEFINE1(ssetmask, int, newmask)
2727 int old;
2729 spin_lock_irq(&current->sighand->siglock);
2730 old = current->blocked.sig[0];
2732 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2733 sigmask(SIGSTOP)));
2734 recalc_sigpending();
2735 spin_unlock_irq(&current->sighand->siglock);
2737 return old;
2739 #endif /* __ARCH_WANT_SGETMASK */
2741 #ifdef __ARCH_WANT_SYS_SIGNAL
2743 * For backwards compatibility. Functionality superseded by sigaction.
2745 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2747 struct k_sigaction new_sa, old_sa;
2748 int ret;
2750 new_sa.sa.sa_handler = handler;
2751 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2752 sigemptyset(&new_sa.sa.sa_mask);
2754 ret = do_sigaction(sig, &new_sa, &old_sa);
2756 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2758 #endif /* __ARCH_WANT_SYS_SIGNAL */
2760 #ifdef __ARCH_WANT_SYS_PAUSE
2762 SYSCALL_DEFINE0(pause)
2764 current->state = TASK_INTERRUPTIBLE;
2765 schedule();
2766 return -ERESTARTNOHAND;
2769 #endif
2771 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2772 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2774 sigset_t newset;
2776 /* XXX: Don't preclude handling different sized sigset_t's. */
2777 if (sigsetsize != sizeof(sigset_t))
2778 return -EINVAL;
2780 if (copy_from_user(&newset, unewset, sizeof(newset)))
2781 return -EFAULT;
2782 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2784 spin_lock_irq(&current->sighand->siglock);
2785 current->saved_sigmask = current->blocked;
2786 current->blocked = newset;
2787 recalc_sigpending();
2788 spin_unlock_irq(&current->sighand->siglock);
2790 current->state = TASK_INTERRUPTIBLE;
2791 schedule();
2792 set_restore_sigmask();
2793 return -ERESTARTNOHAND;
2795 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2797 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2799 return NULL;
2802 void __init signals_init(void)
2804 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
2807 #ifdef CONFIG_KGDB_KDB
2808 #include <linux/kdb.h>
2810 * kdb_send_sig_info - Allows kdb to send signals without exposing
2811 * signal internals. This function checks if the required locks are
2812 * available before calling the main signal code, to avoid kdb
2813 * deadlocks.
2815 void
2816 kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
2818 static struct task_struct *kdb_prev_t;
2819 int sig, new_t;
2820 if (!spin_trylock(&t->sighand->siglock)) {
2821 kdb_printf("Can't do kill command now.\n"
2822 "The sigmask lock is held somewhere else in "
2823 "kernel, try again later\n");
2824 return;
2826 spin_unlock(&t->sighand->siglock);
2827 new_t = kdb_prev_t != t;
2828 kdb_prev_t = t;
2829 if (t->state != TASK_RUNNING && new_t) {
2830 kdb_printf("Process is not RUNNING, sending a signal from "
2831 "kdb risks deadlock\n"
2832 "on the run queue locks. "
2833 "The signal has _not_ been sent.\n"
2834 "Reissue the kill command if you want to risk "
2835 "the deadlock.\n");
2836 return;
2838 sig = info->si_signo;
2839 if (send_sig_info(sig, info, t))
2840 kdb_printf("Fail to deliver Signal %d to process %d.\n",
2841 sig, t->pid);
2842 else
2843 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
2845 #endif /* CONFIG_KGDB_KDB */