Now it works.
[cbs-scheduler.git] / kernel / signal.c
blob21e77f543ccea5c6af3f7bcf29c1fc02b6b54e3a
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/tracehook.h>
26 #include <linux/capability.h>
27 #include <linux/freezer.h>
28 #include <linux/pid_namespace.h>
29 #include <linux/nsproxy.h>
30 #include <trace/sched.h>
32 #include <asm/param.h>
33 #include <asm/uaccess.h>
34 #include <asm/unistd.h>
35 #include <asm/siginfo.h>
36 #include "audit.h" /* audit_signal_info() */
39 * SLAB caches for signal bits.
42 static struct kmem_cache *sigqueue_cachep;
44 DEFINE_TRACE(sched_signal_send);
46 static void __user *sig_handler(struct task_struct *t, int sig)
48 return t->sighand->action[sig - 1].sa.sa_handler;
51 static int sig_handler_ignored(void __user *handler, int sig)
53 /* Is it explicitly or implicitly ignored? */
54 return handler == SIG_IGN ||
55 (handler == SIG_DFL && sig_kernel_ignore(sig));
58 static int sig_ignored(struct task_struct *t, int sig)
60 void __user *handler;
63 * Blocked signals are never ignored, since the
64 * signal handler may change by the time it is
65 * unblocked.
67 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
68 return 0;
70 handler = sig_handler(t, sig);
71 if (!sig_handler_ignored(handler, sig))
72 return 0;
75 * Tracers may want to know about even ignored signals.
77 return !tracehook_consider_ignored_signal(t, sig, handler);
81 * Re-calculate pending state from the set of locally pending
82 * signals, globally pending signals, and blocked signals.
84 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
86 unsigned long ready;
87 long i;
89 switch (_NSIG_WORDS) {
90 default:
91 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
92 ready |= signal->sig[i] &~ blocked->sig[i];
93 break;
95 case 4: ready = signal->sig[3] &~ blocked->sig[3];
96 ready |= signal->sig[2] &~ blocked->sig[2];
97 ready |= signal->sig[1] &~ blocked->sig[1];
98 ready |= signal->sig[0] &~ blocked->sig[0];
99 break;
101 case 2: ready = signal->sig[1] &~ blocked->sig[1];
102 ready |= signal->sig[0] &~ blocked->sig[0];
103 break;
105 case 1: ready = signal->sig[0] &~ blocked->sig[0];
107 return ready != 0;
110 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
112 static int recalc_sigpending_tsk(struct task_struct *t)
114 if (t->signal->group_stop_count > 0 ||
115 PENDING(&t->pending, &t->blocked) ||
116 PENDING(&t->signal->shared_pending, &t->blocked)) {
117 set_tsk_thread_flag(t, TIF_SIGPENDING);
118 return 1;
121 * We must never clear the flag in another thread, or in current
122 * when it's possible the current syscall is returning -ERESTART*.
123 * So we don't clear it here, and only callers who know they should do.
125 return 0;
129 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
130 * This is superfluous when called on current, the wakeup is a harmless no-op.
132 void recalc_sigpending_and_wake(struct task_struct *t)
134 if (recalc_sigpending_tsk(t))
135 signal_wake_up(t, 0);
138 void recalc_sigpending(void)
140 if (unlikely(tracehook_force_sigpending()))
141 set_thread_flag(TIF_SIGPENDING);
142 else if (!recalc_sigpending_tsk(current) && !freezing(current))
143 clear_thread_flag(TIF_SIGPENDING);
147 /* Given the mask, find the first available signal that should be serviced. */
149 int next_signal(struct sigpending *pending, sigset_t *mask)
151 unsigned long i, *s, *m, x;
152 int sig = 0;
154 s = pending->signal.sig;
155 m = mask->sig;
156 switch (_NSIG_WORDS) {
157 default:
158 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
159 if ((x = *s &~ *m) != 0) {
160 sig = ffz(~x) + i*_NSIG_BPW + 1;
161 break;
163 break;
165 case 2: if ((x = s[0] &~ m[0]) != 0)
166 sig = 1;
167 else if ((x = s[1] &~ m[1]) != 0)
168 sig = _NSIG_BPW + 1;
169 else
170 break;
171 sig += ffz(~x);
172 break;
174 case 1: if ((x = *s &~ *m) != 0)
175 sig = ffz(~x) + 1;
176 break;
179 return sig;
182 #ifdef __HAVE_ARCH_CMPXCHG
183 static inline struct sigqueue *get_task_cache(struct task_struct *t)
185 struct sigqueue *q = t->sigqueue_cache;
187 if (cmpxchg(&t->sigqueue_cache, q, NULL) != q)
188 return NULL;
190 return q;
193 static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
195 if (cmpxchg(&t->sigqueue_cache, NULL, q) == NULL)
196 return 0;
198 return 1;
201 #else
203 static inline struct sigqueue *get_task_cache(struct task_struct *t)
205 return NULL;
208 static inline int put_task_cache(struct task_struct *t, struct sigqueue *q)
210 return 1;
213 #endif
216 * allocate a new signal queue record
217 * - this may be called without locks if and only if t == current, otherwise an
218 * appopriate lock must be held to stop the target task from exiting
220 static struct sigqueue *__sigqueue_do_alloc(struct task_struct *t, gfp_t flags,
221 int override_rlimit, int fromslab)
223 struct sigqueue *q = NULL;
224 struct user_struct *user;
227 * We won't get problems with the target's UID changing under us
228 * because changing it requires RCU be used, and if t != current, the
229 * caller must be holding the RCU readlock (by way of a spinlock) and
230 * we use RCU protection here
232 user = get_uid(__task_cred(t)->user);
233 atomic_inc(&user->sigpending);
234 if (override_rlimit ||
235 atomic_read(&user->sigpending) <=
236 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) {
238 if (!fromslab)
239 q = get_task_cache(t);
240 if (!q)
241 q = kmem_cache_alloc(sigqueue_cachep, flags);
244 if (unlikely(q == NULL)) {
245 atomic_dec(&user->sigpending);
246 free_uid(user);
247 } else {
248 INIT_LIST_HEAD(&q->list);
249 q->flags = 0;
250 q->user = user;
253 return q;
256 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
257 int override_rlimit)
259 return __sigqueue_do_alloc(t, flags, override_rlimit, 0);
262 static void __sigqueue_free(struct sigqueue *q)
264 if (q->flags & SIGQUEUE_PREALLOC)
265 return;
266 atomic_dec(&q->user->sigpending);
267 free_uid(q->user);
268 kmem_cache_free(sigqueue_cachep, q);
271 static void sigqueue_free_current(struct sigqueue *q)
273 struct user_struct *up;
275 if (q->flags & SIGQUEUE_PREALLOC)
276 return;
278 up = q->user;
279 if (rt_prio(current->normal_prio) && !put_task_cache(current, q)) {
280 atomic_dec(&up->sigpending);
281 free_uid(up);
282 } else
283 __sigqueue_free(q);
286 void flush_sigqueue(struct sigpending *queue)
288 struct sigqueue *q;
290 sigemptyset(&queue->signal);
291 while (!list_empty(&queue->list)) {
292 q = list_entry(queue->list.next, struct sigqueue , list);
293 list_del_init(&q->list);
294 __sigqueue_free(q);
299 * Called from __exit_signal. Flush tsk->pending and
300 * tsk->sigqueue_cache
302 void flush_task_sigqueue(struct task_struct *tsk)
304 struct sigqueue *q;
306 flush_sigqueue(&tsk->pending);
308 q = get_task_cache(tsk);
309 if (q)
310 kmem_cache_free(sigqueue_cachep, q);
314 * Flush all pending signals for a task.
316 void flush_signals(struct task_struct *t)
318 unsigned long flags;
320 spin_lock_irqsave(&t->sighand->siglock, flags);
321 clear_tsk_thread_flag(t, TIF_SIGPENDING);
322 flush_sigqueue(&t->pending);
323 flush_sigqueue(&t->signal->shared_pending);
324 spin_unlock_irqrestore(&t->sighand->siglock, flags);
327 static void __flush_itimer_signals(struct sigpending *pending)
329 sigset_t signal, retain;
330 struct sigqueue *q, *n;
332 signal = pending->signal;
333 sigemptyset(&retain);
335 list_for_each_entry_safe(q, n, &pending->list, list) {
336 int sig = q->info.si_signo;
338 if (likely(q->info.si_code != SI_TIMER)) {
339 sigaddset(&retain, sig);
340 } else {
341 sigdelset(&signal, sig);
342 list_del_init(&q->list);
343 __sigqueue_free(q);
347 sigorsets(&pending->signal, &signal, &retain);
350 void flush_itimer_signals(void)
352 struct task_struct *tsk = current;
353 unsigned long flags;
355 spin_lock_irqsave(&tsk->sighand->siglock, flags);
356 __flush_itimer_signals(&tsk->pending);
357 __flush_itimer_signals(&tsk->signal->shared_pending);
358 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
361 void ignore_signals(struct task_struct *t)
363 int i;
365 for (i = 0; i < _NSIG; ++i)
366 t->sighand->action[i].sa.sa_handler = SIG_IGN;
368 flush_signals(t);
372 * Flush all handlers for a task.
375 void
376 flush_signal_handlers(struct task_struct *t, int force_default)
378 int i;
379 struct k_sigaction *ka = &t->sighand->action[0];
380 for (i = _NSIG ; i != 0 ; i--) {
381 if (force_default || ka->sa.sa_handler != SIG_IGN)
382 ka->sa.sa_handler = SIG_DFL;
383 ka->sa.sa_flags = 0;
384 sigemptyset(&ka->sa.sa_mask);
385 ka++;
389 int unhandled_signal(struct task_struct *tsk, int sig)
391 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
392 if (is_global_init(tsk))
393 return 1;
394 if (handler != SIG_IGN && handler != SIG_DFL)
395 return 0;
396 return !tracehook_consider_fatal_signal(tsk, sig, handler);
400 /* Notify the system that a driver wants to block all signals for this
401 * process, and wants to be notified if any signals at all were to be
402 * sent/acted upon. If the notifier routine returns non-zero, then the
403 * signal will be acted upon after all. If the notifier routine returns 0,
404 * then then signal will be blocked. Only one block per process is
405 * allowed. priv is a pointer to private data that the notifier routine
406 * can use to determine if the signal should be blocked or not. */
408 void
409 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
411 unsigned long flags;
413 spin_lock_irqsave(&current->sighand->siglock, flags);
414 current->notifier_mask = mask;
415 current->notifier_data = priv;
416 current->notifier = notifier;
417 spin_unlock_irqrestore(&current->sighand->siglock, flags);
420 /* Notify the system that blocking has ended. */
422 void
423 unblock_all_signals(void)
425 unsigned long flags;
427 spin_lock_irqsave(&current->sighand->siglock, flags);
428 current->notifier = NULL;
429 current->notifier_data = NULL;
430 recalc_sigpending();
431 spin_unlock_irqrestore(&current->sighand->siglock, flags);
434 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
436 struct sigqueue *q, *first = NULL;
439 * Collect the siginfo appropriate to this signal. Check if
440 * there is another siginfo for the same signal.
442 list_for_each_entry(q, &list->list, list) {
443 if (q->info.si_signo == sig) {
444 if (first)
445 goto still_pending;
446 first = q;
450 sigdelset(&list->signal, sig);
452 if (first) {
453 still_pending:
454 list_del_init(&first->list);
455 copy_siginfo(info, &first->info);
456 sigqueue_free_current(first);
457 } else {
458 /* Ok, it wasn't in the queue. This must be
459 a fast-pathed signal or we must have been
460 out of queue space. So zero out the info.
462 info->si_signo = sig;
463 info->si_errno = 0;
464 info->si_code = 0;
465 info->si_pid = 0;
466 info->si_uid = 0;
470 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
471 siginfo_t *info)
473 int sig = next_signal(pending, mask);
475 if (sig) {
476 if (current->notifier) {
477 if (sigismember(current->notifier_mask, sig)) {
478 if (!(current->notifier)(current->notifier_data)) {
479 clear_thread_flag(TIF_SIGPENDING);
480 return 0;
485 collect_signal(sig, pending, info);
488 return sig;
492 * Dequeue a signal and return the element to the caller, which is
493 * expected to free it.
495 * All callers have to hold the siglock.
497 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
499 int signr;
501 WARN_ON_ONCE(tsk != current);
503 /* We only dequeue private signals from ourselves, we don't let
504 * signalfd steal them
506 signr = __dequeue_signal(&tsk->pending, mask, info);
507 if (!signr) {
508 signr = __dequeue_signal(&tsk->signal->shared_pending,
509 mask, info);
511 * itimer signal ?
513 * itimers are process shared and we restart periodic
514 * itimers in the signal delivery path to prevent DoS
515 * attacks in the high resolution timer case. This is
516 * compliant with the old way of self restarting
517 * itimers, as the SIGALRM is a legacy signal and only
518 * queued once. Changing the restart behaviour to
519 * restart the timer in the signal dequeue path is
520 * reducing the timer noise on heavy loaded !highres
521 * systems too.
523 if (unlikely(signr == SIGALRM)) {
524 struct hrtimer *tmr = &tsk->signal->real_timer;
526 if (!hrtimer_is_queued(tmr) &&
527 tsk->signal->it_real_incr.tv64 != 0) {
528 hrtimer_forward(tmr, tmr->base->get_time(),
529 tsk->signal->it_real_incr);
530 hrtimer_restart(tmr);
535 recalc_sigpending();
536 if (!signr)
537 return 0;
539 if (unlikely(sig_kernel_stop(signr))) {
541 * Set a marker that we have dequeued a stop signal. Our
542 * caller might release the siglock and then the pending
543 * stop signal it is about to process is no longer in the
544 * pending bitmasks, but must still be cleared by a SIGCONT
545 * (and overruled by a SIGKILL). So those cases clear this
546 * shared flag after we've set it. Note that this flag may
547 * remain set after the signal we return is ignored or
548 * handled. That doesn't matter because its only purpose
549 * is to alert stop-signal processing code when another
550 * processor has come along and cleared the flag.
552 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
554 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
556 * Release the siglock to ensure proper locking order
557 * of timer locks outside of siglocks. Note, we leave
558 * irqs disabled here, since the posix-timers code is
559 * about to disable them again anyway.
561 spin_unlock(&tsk->sighand->siglock);
562 do_schedule_next_timer(info);
563 spin_lock(&tsk->sighand->siglock);
565 return signr;
569 * Tell a process that it has a new active signal..
571 * NOTE! we rely on the previous spin_lock to
572 * lock interrupts for us! We can only be called with
573 * "siglock" held, and the local interrupt must
574 * have been disabled when that got acquired!
576 * No need to set need_resched since signal event passing
577 * goes through ->blocked
579 void signal_wake_up(struct task_struct *t, int resume)
581 unsigned int mask;
583 set_tsk_thread_flag(t, TIF_SIGPENDING);
585 if (unlikely(t == current))
586 return;
589 * For SIGKILL, we want to wake it up in the stopped/traced/killable
590 * case. We don't check t->state here because there is a race with it
591 * executing another processor and just now entering stopped state.
592 * By using wake_up_state, we ensure the process will wake up and
593 * handle its death signal.
595 mask = TASK_INTERRUPTIBLE;
596 if (resume)
597 mask |= TASK_WAKEKILL;
598 if (!wake_up_state(t, mask))
599 kick_process(t);
603 * Remove signals in mask from the pending set and queue.
604 * Returns 1 if any signals were found.
606 * All callers must be holding the siglock.
608 * This version takes a sigset mask and looks at all signals,
609 * not just those in the first mask word.
611 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
613 struct sigqueue *q, *n;
614 sigset_t m;
616 sigandsets(&m, mask, &s->signal);
617 if (sigisemptyset(&m))
618 return 0;
620 signandsets(&s->signal, &s->signal, mask);
621 list_for_each_entry_safe(q, n, &s->list, list) {
622 if (sigismember(mask, q->info.si_signo)) {
623 list_del_init(&q->list);
624 __sigqueue_free(q);
627 return 1;
630 * Remove signals in mask from the pending set and queue.
631 * Returns 1 if any signals were found.
633 * All callers must be holding the siglock.
635 static int rm_from_queue(unsigned long mask, struct sigpending *s)
637 struct sigqueue *q, *n;
639 if (!sigtestsetmask(&s->signal, mask))
640 return 0;
642 sigdelsetmask(&s->signal, mask);
643 list_for_each_entry_safe(q, n, &s->list, list) {
644 if (q->info.si_signo < SIGRTMIN &&
645 (mask & sigmask(q->info.si_signo))) {
646 list_del_init(&q->list);
647 __sigqueue_free(q);
650 return 1;
654 * Bad permissions for sending the signal
655 * - the caller must hold at least the RCU read lock
657 static int check_kill_permission(int sig, struct siginfo *info,
658 struct task_struct *t)
660 const struct cred *cred = current_cred(), *tcred;
661 struct pid *sid;
662 int error;
664 if (!valid_signal(sig))
665 return -EINVAL;
667 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
668 return 0;
670 error = audit_signal_info(sig, t); /* Let audit system see the signal */
671 if (error)
672 return error;
674 tcred = __task_cred(t);
675 if ((cred->euid ^ tcred->suid) &&
676 (cred->euid ^ tcred->uid) &&
677 (cred->uid ^ tcred->suid) &&
678 (cred->uid ^ tcred->uid) &&
679 !capable(CAP_KILL)) {
680 switch (sig) {
681 case SIGCONT:
682 sid = task_session(t);
684 * We don't return the error if sid == NULL. The
685 * task was unhashed, the caller must notice this.
687 if (!sid || sid == task_session(current))
688 break;
689 default:
690 return -EPERM;
694 return security_task_kill(t, info, sig, 0);
698 * Handle magic process-wide effects of stop/continue signals. Unlike
699 * the signal actions, these happen immediately at signal-generation
700 * time regardless of blocking, ignoring, or handling. This does the
701 * actual continuing for SIGCONT, but not the actual stopping for stop
702 * signals. The process stop is done as a signal action for SIG_DFL.
704 * Returns true if the signal should be actually delivered, otherwise
705 * it should be dropped.
707 static int prepare_signal(int sig, struct task_struct *p)
709 struct signal_struct *signal = p->signal;
710 struct task_struct *t;
712 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
714 * The process is in the middle of dying, nothing to do.
716 } else if (sig_kernel_stop(sig)) {
718 * This is a stop signal. Remove SIGCONT from all queues.
720 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
721 t = p;
722 do {
723 rm_from_queue(sigmask(SIGCONT), &t->pending);
724 } while_each_thread(p, t);
725 } else if (sig == SIGCONT) {
726 unsigned int why;
728 * Remove all stop signals from all queues,
729 * and wake all threads.
731 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
732 t = p;
733 do {
734 unsigned int state;
735 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
737 * If there is a handler for SIGCONT, we must make
738 * sure that no thread returns to user mode before
739 * we post the signal, in case it was the only
740 * thread eligible to run the signal handler--then
741 * it must not do anything between resuming and
742 * running the handler. With the TIF_SIGPENDING
743 * flag set, the thread will pause and acquire the
744 * siglock that we hold now and until we've queued
745 * the pending signal.
747 * Wake up the stopped thread _after_ setting
748 * TIF_SIGPENDING
750 state = __TASK_STOPPED;
751 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
752 set_tsk_thread_flag(t, TIF_SIGPENDING);
753 state |= TASK_INTERRUPTIBLE;
755 wake_up_state(t, state);
756 } while_each_thread(p, t);
759 * Notify the parent with CLD_CONTINUED if we were stopped.
761 * If we were in the middle of a group stop, we pretend it
762 * was already finished, and then continued. Since SIGCHLD
763 * doesn't queue we report only CLD_STOPPED, as if the next
764 * CLD_CONTINUED was dropped.
766 why = 0;
767 if (signal->flags & SIGNAL_STOP_STOPPED)
768 why |= SIGNAL_CLD_CONTINUED;
769 else if (signal->group_stop_count)
770 why |= SIGNAL_CLD_STOPPED;
772 if (why) {
774 * The first thread which returns from finish_stop()
775 * will take ->siglock, notice SIGNAL_CLD_MASK, and
776 * notify its parent. See get_signal_to_deliver().
778 signal->flags = why | SIGNAL_STOP_CONTINUED;
779 signal->group_stop_count = 0;
780 signal->group_exit_code = 0;
781 } else {
783 * We are not stopped, but there could be a stop
784 * signal in the middle of being processed after
785 * being removed from the queue. Clear that too.
787 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
791 return !sig_ignored(p, sig);
795 * Test if P wants to take SIG. After we've checked all threads with this,
796 * it's equivalent to finding no threads not blocking SIG. Any threads not
797 * blocking SIG were ruled out because they are not running and already
798 * have pending signals. Such threads will dequeue from the shared queue
799 * as soon as they're available, so putting the signal on the shared queue
800 * will be equivalent to sending it to one such thread.
802 static inline int wants_signal(int sig, struct task_struct *p)
804 if (sigismember(&p->blocked, sig))
805 return 0;
806 if (p->flags & PF_EXITING)
807 return 0;
808 if (sig == SIGKILL)
809 return 1;
810 if (task_is_stopped_or_traced(p))
811 return 0;
812 return task_curr(p) || !signal_pending(p);
815 static void complete_signal(int sig, struct task_struct *p, int group)
817 struct signal_struct *signal = p->signal;
818 struct task_struct *t;
821 * Now find a thread we can wake up to take the signal off the queue.
823 * If the main thread wants the signal, it gets first crack.
824 * Probably the least surprising to the average bear.
826 if (wants_signal(sig, p))
827 t = p;
828 else if (!group || thread_group_empty(p))
830 * There is just one thread and it does not need to be woken.
831 * It will dequeue unblocked signals before it runs again.
833 return;
834 else {
836 * Otherwise try to find a suitable thread.
838 t = signal->curr_target;
839 while (!wants_signal(sig, t)) {
840 t = next_thread(t);
841 if (t == signal->curr_target)
843 * No thread needs to be woken.
844 * Any eligible threads will see
845 * the signal in the queue soon.
847 return;
849 signal->curr_target = t;
853 * Found a killable thread. If the signal will be fatal,
854 * then start taking the whole group down immediately.
856 if (sig_fatal(p, sig) &&
857 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
858 !sigismember(&t->real_blocked, sig) &&
859 (sig == SIGKILL ||
860 !tracehook_consider_fatal_signal(t, sig, SIG_DFL))) {
862 * This signal will be fatal to the whole group.
864 if (!sig_kernel_coredump(sig)) {
866 * Start a group exit and wake everybody up.
867 * This way we don't have other threads
868 * running and doing things after a slower
869 * thread has the fatal signal pending.
871 signal->flags = SIGNAL_GROUP_EXIT;
872 signal->group_exit_code = sig;
873 signal->group_stop_count = 0;
874 t = p;
875 do {
876 sigaddset(&t->pending.signal, SIGKILL);
877 signal_wake_up(t, 1);
878 } while_each_thread(p, t);
879 return;
884 * The signal is already in the shared-pending queue.
885 * Tell the chosen thread to wake up and dequeue it.
887 signal_wake_up(t, sig == SIGKILL);
888 return;
891 static inline int legacy_queue(struct sigpending *signals, int sig)
893 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
896 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
897 int group)
899 struct sigpending *pending;
900 struct sigqueue *q;
902 trace_sched_signal_send(sig, t);
904 #ifdef CONFIG_SMP
905 assert_spin_locked(&t->sighand->siglock);
906 #endif
907 if (!prepare_signal(sig, t))
908 return 0;
910 pending = group ? &t->signal->shared_pending : &t->pending;
912 * Short-circuit ignored signals and support queuing
913 * exactly one non-rt signal, so that we can get more
914 * detailed information about the cause of the signal.
916 if (legacy_queue(pending, sig))
917 return 0;
919 * fast-pathed signals for kernel-internal things like SIGSTOP
920 * or SIGKILL.
922 if (info == SEND_SIG_FORCED)
923 goto out_set;
925 /* Real-time signals must be queued if sent by sigqueue, or
926 some other real-time mechanism. It is implementation
927 defined whether kill() does so. We attempt to do so, on
928 the principle of least surprise, but since kill is not
929 allowed to fail with EAGAIN when low on memory we just
930 make sure at least one signal gets delivered and don't
931 pass on the info struct. */
933 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
934 (is_si_special(info) ||
935 info->si_code >= 0)));
936 if (q) {
937 list_add_tail(&q->list, &pending->list);
938 switch ((unsigned long) info) {
939 case (unsigned long) SEND_SIG_NOINFO:
940 q->info.si_signo = sig;
941 q->info.si_errno = 0;
942 q->info.si_code = SI_USER;
943 q->info.si_pid = task_tgid_nr_ns(current,
944 task_active_pid_ns(t));
945 q->info.si_uid = current_uid();
946 break;
947 case (unsigned long) SEND_SIG_PRIV:
948 q->info.si_signo = sig;
949 q->info.si_errno = 0;
950 q->info.si_code = SI_KERNEL;
951 q->info.si_pid = 0;
952 q->info.si_uid = 0;
953 break;
954 default:
955 copy_siginfo(&q->info, info);
956 break;
958 } else if (!is_si_special(info)) {
959 if (sig >= SIGRTMIN && info->si_code != SI_USER)
961 * Queue overflow, abort. We may abort if the signal was rt
962 * and sent by user using something other than kill().
964 return -EAGAIN;
967 out_set:
968 signalfd_notify(t, sig);
969 sigaddset(&pending->signal, sig);
970 complete_signal(sig, t, group);
971 return 0;
974 int print_fatal_signals;
976 static void print_fatal_signal(struct pt_regs *regs, int signr)
978 printk("%s/%d: potentially unexpected fatal signal %d.\n",
979 current->comm, task_pid_nr(current), signr);
981 #if defined(__i386__) && !defined(__arch_um__)
982 printk("code at %08lx: ", regs->ip);
984 int i;
985 for (i = 0; i < 16; i++) {
986 unsigned char insn;
988 __get_user(insn, (unsigned char *)(regs->ip + i));
989 printk("%02x ", insn);
992 #endif
993 printk("\n");
994 preempt_disable();
995 show_regs(regs);
996 preempt_enable();
999 static int __init setup_print_fatal_signals(char *str)
1001 get_option (&str, &print_fatal_signals);
1003 return 1;
1006 __setup("print-fatal-signals=", setup_print_fatal_signals);
1009 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1011 return send_signal(sig, info, p, 1);
1014 static int
1015 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1017 return send_signal(sig, info, t, 0);
1021 * Force a signal that the process can't ignore: if necessary
1022 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1024 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1025 * since we do not want to have a signal handler that was blocked
1026 * be invoked when user space had explicitly blocked it.
1028 * We don't want to have recursive SIGSEGV's etc, for example,
1029 * that is why we also clear SIGNAL_UNKILLABLE.
1032 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1034 unsigned long int flags;
1035 int ret, blocked, ignored;
1036 struct k_sigaction *action;
1038 spin_lock_irqsave(&t->sighand->siglock, flags);
1039 action = &t->sighand->action[sig-1];
1040 ignored = action->sa.sa_handler == SIG_IGN;
1041 blocked = sigismember(&t->blocked, sig);
1042 if (blocked || ignored) {
1043 action->sa.sa_handler = SIG_DFL;
1044 if (blocked) {
1045 sigdelset(&t->blocked, sig);
1046 recalc_sigpending_and_wake(t);
1049 if (action->sa.sa_handler == SIG_DFL)
1050 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1051 ret = specific_send_sig_info(sig, info, t);
1052 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1054 return ret;
1057 void
1058 force_sig_specific(int sig, struct task_struct *t)
1060 force_sig_info(sig, SEND_SIG_FORCED, t);
1064 * Nuke all other threads in the group.
1066 void zap_other_threads(struct task_struct *p)
1068 struct task_struct *t;
1070 p->signal->group_stop_count = 0;
1072 for (t = next_thread(p); t != p; t = next_thread(t)) {
1074 * Don't bother with already dead threads
1076 if (t->exit_state)
1077 continue;
1079 /* SIGKILL will be handled before any pending SIGSTOP */
1080 sigaddset(&t->pending.signal, SIGKILL);
1081 signal_wake_up(t, 1);
1085 int __fatal_signal_pending(struct task_struct *tsk)
1087 return sigismember(&tsk->pending.signal, SIGKILL);
1089 EXPORT_SYMBOL(__fatal_signal_pending);
1091 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1093 struct sighand_struct *sighand;
1095 rcu_read_lock();
1096 for (;;) {
1097 sighand = rcu_dereference(tsk->sighand);
1098 if (unlikely(sighand == NULL))
1099 break;
1101 spin_lock_irqsave(&sighand->siglock, *flags);
1102 if (likely(sighand == tsk->sighand))
1103 break;
1104 spin_unlock_irqrestore(&sighand->siglock, *flags);
1106 rcu_read_unlock();
1108 return sighand;
1112 * send signal info to all the members of a group
1113 * - the caller must hold the RCU read lock at least
1115 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1117 unsigned long flags;
1118 int ret;
1120 ret = check_kill_permission(sig, info, p);
1122 if (!ret && sig) {
1123 ret = -ESRCH;
1124 if (lock_task_sighand(p, &flags)) {
1125 ret = __group_send_sig_info(sig, info, p);
1126 unlock_task_sighand(p, &flags);
1130 return ret;
1134 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1135 * control characters do (^C, ^Z etc)
1136 * - the caller must hold at least a readlock on tasklist_lock
1138 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1140 struct task_struct *p = NULL;
1141 int retval, success;
1143 success = 0;
1144 retval = -ESRCH;
1145 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1146 int err = group_send_sig_info(sig, info, p);
1147 success |= !err;
1148 retval = err;
1149 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1150 return success ? 0 : retval;
1153 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1155 int error = -ESRCH;
1156 struct task_struct *p;
1158 rcu_read_lock();
1159 retry:
1160 p = pid_task(pid, PIDTYPE_PID);
1161 if (p) {
1162 error = group_send_sig_info(sig, info, p);
1163 if (unlikely(error == -ESRCH))
1165 * The task was unhashed in between, try again.
1166 * If it is dead, pid_task() will return NULL,
1167 * if we race with de_thread() it will find the
1168 * new leader.
1170 goto retry;
1172 rcu_read_unlock();
1174 return error;
1178 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1180 int error;
1181 rcu_read_lock();
1182 error = kill_pid_info(sig, info, find_vpid(pid));
1183 rcu_read_unlock();
1184 return error;
1187 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1188 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1189 uid_t uid, uid_t euid, u32 secid)
1191 int ret = -EINVAL;
1192 struct task_struct *p;
1193 const struct cred *pcred;
1195 if (!valid_signal(sig))
1196 return ret;
1198 read_lock(&tasklist_lock);
1199 p = pid_task(pid, PIDTYPE_PID);
1200 if (!p) {
1201 ret = -ESRCH;
1202 goto out_unlock;
1204 pcred = __task_cred(p);
1205 if ((info == SEND_SIG_NOINFO ||
1206 (!is_si_special(info) && SI_FROMUSER(info))) &&
1207 euid != pcred->suid && euid != pcred->uid &&
1208 uid != pcred->suid && uid != pcred->uid) {
1209 ret = -EPERM;
1210 goto out_unlock;
1212 ret = security_task_kill(p, info, sig, secid);
1213 if (ret)
1214 goto out_unlock;
1215 if (sig && p->sighand) {
1216 unsigned long flags;
1217 spin_lock_irqsave(&p->sighand->siglock, flags);
1218 ret = __group_send_sig_info(sig, info, p);
1219 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1221 out_unlock:
1222 read_unlock(&tasklist_lock);
1223 return ret;
1225 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1228 * kill_something_info() interprets pid in interesting ways just like kill(2).
1230 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1231 * is probably wrong. Should make it like BSD or SYSV.
1234 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1236 int ret;
1238 if (pid > 0) {
1239 rcu_read_lock();
1240 ret = kill_pid_info(sig, info, find_vpid(pid));
1241 rcu_read_unlock();
1242 return ret;
1245 read_lock(&tasklist_lock);
1246 if (pid != -1) {
1247 ret = __kill_pgrp_info(sig, info,
1248 pid ? find_vpid(-pid) : task_pgrp(current));
1249 } else {
1250 int retval = 0, count = 0;
1251 struct task_struct * p;
1253 for_each_process(p) {
1254 if (task_pid_vnr(p) > 1 &&
1255 !same_thread_group(p, current)) {
1256 int err = group_send_sig_info(sig, info, p);
1257 ++count;
1258 if (err != -EPERM)
1259 retval = err;
1262 ret = count ? retval : -ESRCH;
1264 read_unlock(&tasklist_lock);
1266 return ret;
1270 * These are for backward compatibility with the rest of the kernel source.
1274 * The caller must ensure the task can't exit.
1277 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1279 int ret;
1280 unsigned long flags;
1283 * Make sure legacy kernel users don't send in bad values
1284 * (normal paths check this in check_kill_permission).
1286 if (!valid_signal(sig))
1287 return -EINVAL;
1289 spin_lock_irqsave(&p->sighand->siglock, flags);
1290 ret = specific_send_sig_info(sig, info, p);
1291 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1292 return ret;
1295 #define __si_special(priv) \
1296 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1299 send_sig(int sig, struct task_struct *p, int priv)
1301 return send_sig_info(sig, __si_special(priv), p);
1304 void
1305 force_sig(int sig, struct task_struct *p)
1307 force_sig_info(sig, SEND_SIG_PRIV, p);
1311 * When things go south during signal handling, we
1312 * will force a SIGSEGV. And if the signal that caused
1313 * the problem was already a SIGSEGV, we'll want to
1314 * make sure we don't even try to deliver the signal..
1317 force_sigsegv(int sig, struct task_struct *p)
1319 if (sig == SIGSEGV) {
1320 unsigned long flags;
1321 spin_lock_irqsave(&p->sighand->siglock, flags);
1322 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1323 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1325 force_sig(SIGSEGV, p);
1326 return 0;
1329 int kill_pgrp(struct pid *pid, int sig, int priv)
1331 int ret;
1333 read_lock(&tasklist_lock);
1334 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1335 read_unlock(&tasklist_lock);
1337 return ret;
1339 EXPORT_SYMBOL(kill_pgrp);
1341 int kill_pid(struct pid *pid, int sig, int priv)
1343 return kill_pid_info(sig, __si_special(priv), pid);
1345 EXPORT_SYMBOL(kill_pid);
1348 * These functions support sending signals using preallocated sigqueue
1349 * structures. This is needed "because realtime applications cannot
1350 * afford to lose notifications of asynchronous events, like timer
1351 * expirations or I/O completions". In the case of Posix Timers
1352 * we allocate the sigqueue structure from the timer_create. If this
1353 * allocation fails we are able to report the failure to the application
1354 * with an EAGAIN error.
1357 struct sigqueue *sigqueue_alloc(void)
1359 struct sigqueue *q;
1361 /* Preallocated sigqueue objects always from the slabcache ! */
1362 if ((q = __sigqueue_do_alloc(current, GFP_KERNEL, 0, 1)))
1363 q->flags |= SIGQUEUE_PREALLOC;
1364 return(q);
1367 void sigqueue_free(struct sigqueue *q)
1369 unsigned long flags;
1370 spinlock_t *lock = &current->sighand->siglock;
1372 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1374 * We must hold ->siglock while testing q->list
1375 * to serialize with collect_signal() or with
1376 * __exit_signal()->flush_sigqueue().
1378 spin_lock_irqsave(lock, flags);
1379 q->flags &= ~SIGQUEUE_PREALLOC;
1381 * If it is queued it will be freed when dequeued,
1382 * like the "regular" sigqueue.
1384 if (!list_empty(&q->list))
1385 q = NULL;
1386 spin_unlock_irqrestore(lock, flags);
1388 if (q)
1389 __sigqueue_free(q);
1392 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1394 int sig = q->info.si_signo;
1395 struct sigpending *pending;
1396 unsigned long flags;
1397 int ret;
1399 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1401 ret = -1;
1402 if (!likely(lock_task_sighand(t, &flags)))
1403 goto ret;
1405 ret = 1; /* the signal is ignored */
1406 if (!prepare_signal(sig, t))
1407 goto out;
1409 ret = 0;
1410 if (unlikely(!list_empty(&q->list))) {
1412 * If an SI_TIMER entry is already queue just increment
1413 * the overrun count.
1415 BUG_ON(q->info.si_code != SI_TIMER);
1416 q->info.si_overrun++;
1417 goto out;
1419 q->info.si_overrun = 0;
1421 signalfd_notify(t, sig);
1422 pending = group ? &t->signal->shared_pending : &t->pending;
1423 list_add_tail(&q->list, &pending->list);
1424 sigaddset(&pending->signal, sig);
1425 complete_signal(sig, t, group);
1426 out:
1427 unlock_task_sighand(t, &flags);
1428 ret:
1429 return ret;
1433 * Wake up any threads in the parent blocked in wait* syscalls.
1435 static inline void __wake_up_parent(struct task_struct *p,
1436 struct task_struct *parent)
1438 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1442 * Let a parent know about the death of a child.
1443 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1445 * Returns -1 if our parent ignored us and so we've switched to
1446 * self-reaping, or else @sig.
1448 int do_notify_parent(struct task_struct *tsk, int sig)
1450 struct siginfo info;
1451 unsigned long flags;
1452 struct sighand_struct *psig;
1453 int ret = sig;
1455 BUG_ON(sig == -1);
1457 /* do_notify_parent_cldstop should have been called instead. */
1458 BUG_ON(task_is_stopped_or_traced(tsk));
1460 BUG_ON(!tsk->ptrace &&
1461 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1463 info.si_signo = sig;
1464 info.si_errno = 0;
1466 * we are under tasklist_lock here so our parent is tied to
1467 * us and cannot exit and release its namespace.
1469 * the only it can is to switch its nsproxy with sys_unshare,
1470 * bu uncharing pid namespaces is not allowed, so we'll always
1471 * see relevant namespace
1473 * write_lock() currently calls preempt_disable() which is the
1474 * same as rcu_read_lock(), but according to Oleg, this is not
1475 * correct to rely on this
1477 rcu_read_lock();
1478 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1479 info.si_uid = __task_cred(tsk)->uid;
1480 rcu_read_unlock();
1482 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1483 tsk->signal->utime));
1484 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1485 tsk->signal->stime));
1487 info.si_status = tsk->exit_code & 0x7f;
1488 if (tsk->exit_code & 0x80)
1489 info.si_code = CLD_DUMPED;
1490 else if (tsk->exit_code & 0x7f)
1491 info.si_code = CLD_KILLED;
1492 else {
1493 info.si_code = CLD_EXITED;
1494 info.si_status = tsk->exit_code >> 8;
1497 psig = tsk->parent->sighand;
1498 spin_lock_irqsave(&psig->siglock, flags);
1499 if (!tsk->ptrace && sig == SIGCHLD &&
1500 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1501 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1503 * We are exiting and our parent doesn't care. POSIX.1
1504 * defines special semantics for setting SIGCHLD to SIG_IGN
1505 * or setting the SA_NOCLDWAIT flag: we should be reaped
1506 * automatically and not left for our parent's wait4 call.
1507 * Rather than having the parent do it as a magic kind of
1508 * signal handler, we just set this to tell do_exit that we
1509 * can be cleaned up without becoming a zombie. Note that
1510 * we still call __wake_up_parent in this case, because a
1511 * blocked sys_wait4 might now return -ECHILD.
1513 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1514 * is implementation-defined: we do (if you don't want
1515 * it, just use SIG_IGN instead).
1517 ret = tsk->exit_signal = -1;
1518 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1519 sig = -1;
1521 if (valid_signal(sig) && sig > 0)
1522 __group_send_sig_info(sig, &info, tsk->parent);
1523 __wake_up_parent(tsk, tsk->parent);
1524 spin_unlock_irqrestore(&psig->siglock, flags);
1526 return ret;
1529 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1531 struct siginfo info;
1532 unsigned long flags;
1533 struct task_struct *parent;
1534 struct sighand_struct *sighand;
1536 if (tsk->ptrace & PT_PTRACED)
1537 parent = tsk->parent;
1538 else {
1539 tsk = tsk->group_leader;
1540 parent = tsk->real_parent;
1543 info.si_signo = SIGCHLD;
1544 info.si_errno = 0;
1546 * see comment in do_notify_parent() abot the following 3 lines
1548 rcu_read_lock();
1549 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1550 info.si_uid = __task_cred(tsk)->uid;
1551 rcu_read_unlock();
1553 info.si_utime = cputime_to_clock_t(tsk->utime);
1554 info.si_stime = cputime_to_clock_t(tsk->stime);
1556 info.si_code = why;
1557 switch (why) {
1558 case CLD_CONTINUED:
1559 info.si_status = SIGCONT;
1560 break;
1561 case CLD_STOPPED:
1562 info.si_status = tsk->signal->group_exit_code & 0x7f;
1563 break;
1564 case CLD_TRAPPED:
1565 info.si_status = tsk->exit_code & 0x7f;
1566 break;
1567 default:
1568 BUG();
1571 sighand = parent->sighand;
1572 spin_lock_irqsave(&sighand->siglock, flags);
1573 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1574 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1575 __group_send_sig_info(SIGCHLD, &info, parent);
1577 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1579 __wake_up_parent(tsk, parent);
1580 spin_unlock_irqrestore(&sighand->siglock, flags);
1583 static inline int may_ptrace_stop(void)
1585 if (!likely(current->ptrace & PT_PTRACED))
1586 return 0;
1588 * Are we in the middle of do_coredump?
1589 * If so and our tracer is also part of the coredump stopping
1590 * is a deadlock situation, and pointless because our tracer
1591 * is dead so don't allow us to stop.
1592 * If SIGKILL was already sent before the caller unlocked
1593 * ->siglock we must see ->core_state != NULL. Otherwise it
1594 * is safe to enter schedule().
1596 if (unlikely(current->mm->core_state) &&
1597 unlikely(current->mm == current->parent->mm))
1598 return 0;
1600 return 1;
1604 * Return nonzero if there is a SIGKILL that should be waking us up.
1605 * Called with the siglock held.
1607 static int sigkill_pending(struct task_struct *tsk)
1609 return sigismember(&tsk->pending.signal, SIGKILL) ||
1610 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1614 * This must be called with current->sighand->siglock held.
1616 * This should be the path for all ptrace stops.
1617 * We always set current->last_siginfo while stopped here.
1618 * That makes it a way to test a stopped process for
1619 * being ptrace-stopped vs being job-control-stopped.
1621 * If we actually decide not to stop at all because the tracer
1622 * is gone, we keep current->exit_code unless clear_code.
1624 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1626 if (arch_ptrace_stop_needed(exit_code, info)) {
1628 * The arch code has something special to do before a
1629 * ptrace stop. This is allowed to block, e.g. for faults
1630 * on user stack pages. We can't keep the siglock while
1631 * calling arch_ptrace_stop, so we must release it now.
1632 * To preserve proper semantics, we must do this before
1633 * any signal bookkeeping like checking group_stop_count.
1634 * Meanwhile, a SIGKILL could come in before we retake the
1635 * siglock. That must prevent us from sleeping in TASK_TRACED.
1636 * So after regaining the lock, we must check for SIGKILL.
1638 spin_unlock_irq(&current->sighand->siglock);
1639 arch_ptrace_stop(exit_code, info);
1640 spin_lock_irq(&current->sighand->siglock);
1641 if (sigkill_pending(current))
1642 return;
1646 * If there is a group stop in progress,
1647 * we must participate in the bookkeeping.
1649 if (current->signal->group_stop_count > 0)
1650 --current->signal->group_stop_count;
1652 current->last_siginfo = info;
1653 current->exit_code = exit_code;
1655 /* Let the debugger run. */
1656 __set_current_state(TASK_TRACED);
1657 spin_unlock_irq(&current->sighand->siglock);
1658 read_lock(&tasklist_lock);
1659 if (may_ptrace_stop()) {
1660 do_notify_parent_cldstop(current, CLD_TRAPPED);
1662 current->flags &= ~PF_NOSCHED;
1663 read_unlock(&tasklist_lock);
1664 schedule();
1665 } else {
1667 * By the time we got the lock, our tracer went away.
1668 * Don't drop the lock yet, another tracer may come.
1670 __set_current_state(TASK_RUNNING);
1671 if (clear_code)
1672 current->exit_code = 0;
1673 read_unlock(&tasklist_lock);
1677 * While in TASK_TRACED, we were considered "frozen enough".
1678 * Now that we woke up, it's crucial if we're supposed to be
1679 * frozen that we freeze now before running anything substantial.
1681 try_to_freeze();
1684 * We are back. Now reacquire the siglock before touching
1685 * last_siginfo, so that we are sure to have synchronized with
1686 * any signal-sending on another CPU that wants to examine it.
1688 spin_lock_irq(&current->sighand->siglock);
1689 current->last_siginfo = NULL;
1692 * Queued signals ignored us while we were stopped for tracing.
1693 * So check for any that we should take before resuming user mode.
1694 * This sets TIF_SIGPENDING, but never clears it.
1696 recalc_sigpending_tsk(current);
1699 void ptrace_notify(int exit_code)
1701 siginfo_t info;
1703 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1705 memset(&info, 0, sizeof info);
1706 info.si_signo = SIGTRAP;
1707 info.si_code = exit_code;
1708 info.si_pid = task_pid_vnr(current);
1709 info.si_uid = current_uid();
1711 /* Let the debugger run. */
1712 spin_lock_irq(&current->sighand->siglock);
1713 ptrace_stop(exit_code, 1, &info);
1714 spin_unlock_irq(&current->sighand->siglock);
1717 static void
1718 finish_stop(int stop_count)
1721 * If there are no other threads in the group, or if there is
1722 * a group stop in progress and we are the last to stop,
1723 * report to the parent. When ptraced, every thread reports itself.
1725 if (tracehook_notify_jctl(stop_count == 0, CLD_STOPPED)) {
1726 read_lock(&tasklist_lock);
1727 do_notify_parent_cldstop(current, CLD_STOPPED);
1728 read_unlock(&tasklist_lock);
1731 do {
1732 current->flags &= ~PF_NOSCHED;
1733 schedule();
1734 } while (try_to_freeze());
1736 * Now we don't run again until continued.
1738 current->exit_code = 0;
1742 * This performs the stopping for SIGSTOP and other stop signals.
1743 * We have to stop all threads in the thread group.
1744 * Returns nonzero if we've actually stopped and released the siglock.
1745 * Returns zero if we didn't stop and still hold the siglock.
1747 static int do_signal_stop(int signr)
1749 struct signal_struct *sig = current->signal;
1750 int stop_count;
1752 if (sig->group_stop_count > 0) {
1754 * There is a group stop in progress. We don't need to
1755 * start another one.
1757 stop_count = --sig->group_stop_count;
1758 } else {
1759 struct task_struct *t;
1761 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1762 unlikely(signal_group_exit(sig)))
1763 return 0;
1765 * There is no group stop already in progress.
1766 * We must initiate one now.
1768 sig->group_exit_code = signr;
1770 stop_count = 0;
1771 for (t = next_thread(current); t != current; t = next_thread(t))
1773 * Setting state to TASK_STOPPED for a group
1774 * stop is always done with the siglock held,
1775 * so this check has no races.
1777 if (!(t->flags & PF_EXITING) &&
1778 !task_is_stopped_or_traced(t)) {
1779 stop_count++;
1780 signal_wake_up(t, 0);
1782 sig->group_stop_count = stop_count;
1785 if (stop_count == 0)
1786 sig->flags = SIGNAL_STOP_STOPPED;
1787 current->exit_code = sig->group_exit_code;
1788 __set_current_state(TASK_STOPPED);
1790 spin_unlock_irq(&current->sighand->siglock);
1791 finish_stop(stop_count);
1792 return 1;
1795 static int ptrace_signal(int signr, siginfo_t *info,
1796 struct pt_regs *regs, void *cookie)
1798 if (!(current->ptrace & PT_PTRACED))
1799 return signr;
1801 ptrace_signal_deliver(regs, cookie);
1803 /* Let the debugger run. */
1804 ptrace_stop(signr, 0, info);
1806 /* We're back. Did the debugger cancel the sig? */
1807 signr = current->exit_code;
1808 if (signr == 0)
1809 return signr;
1811 current->exit_code = 0;
1813 /* Update the siginfo structure if the signal has
1814 changed. If the debugger wanted something
1815 specific in the siginfo structure then it should
1816 have updated *info via PTRACE_SETSIGINFO. */
1817 if (signr != info->si_signo) {
1818 info->si_signo = signr;
1819 info->si_errno = 0;
1820 info->si_code = SI_USER;
1821 info->si_pid = task_pid_vnr(current->parent);
1822 info->si_uid = task_uid(current->parent);
1825 /* If the (new) signal is now blocked, requeue it. */
1826 if (sigismember(&current->blocked, signr)) {
1827 specific_send_sig_info(signr, info, current);
1828 signr = 0;
1831 return signr;
1834 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1835 struct pt_regs *regs, void *cookie)
1837 struct sighand_struct *sighand = current->sighand;
1838 struct signal_struct *signal = current->signal;
1839 int signr;
1841 relock:
1843 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1844 * While in TASK_STOPPED, we were considered "frozen enough".
1845 * Now that we woke up, it's crucial if we're supposed to be
1846 * frozen that we freeze now before running anything substantial.
1848 try_to_freeze();
1850 spin_lock_irq(&sighand->siglock);
1852 * Every stopped thread goes here after wakeup. Check to see if
1853 * we should notify the parent, prepare_signal(SIGCONT) encodes
1854 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1856 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1857 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1858 ? CLD_CONTINUED : CLD_STOPPED;
1859 signal->flags &= ~SIGNAL_CLD_MASK;
1860 spin_unlock_irq(&sighand->siglock);
1862 if (unlikely(!tracehook_notify_jctl(1, why)))
1863 goto relock;
1865 read_lock(&tasklist_lock);
1866 do_notify_parent_cldstop(current->group_leader, why);
1867 read_unlock(&tasklist_lock);
1868 goto relock;
1871 for (;;) {
1872 struct k_sigaction *ka;
1874 if (unlikely(signal->group_stop_count > 0) &&
1875 do_signal_stop(0))
1876 goto relock;
1879 * Tracing can induce an artifical signal and choose sigaction.
1880 * The return value in @signr determines the default action,
1881 * but @info->si_signo is the signal number we will report.
1883 signr = tracehook_get_signal(current, regs, info, return_ka);
1884 if (unlikely(signr < 0))
1885 goto relock;
1886 if (unlikely(signr != 0))
1887 ka = return_ka;
1888 else {
1889 signr = dequeue_signal(current, &current->blocked,
1890 info);
1892 if (!signr)
1893 break; /* will return 0 */
1895 if (signr != SIGKILL) {
1896 signr = ptrace_signal(signr, info,
1897 regs, cookie);
1898 if (!signr)
1899 continue;
1902 ka = &sighand->action[signr-1];
1905 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1906 continue;
1907 if (ka->sa.sa_handler != SIG_DFL) {
1908 /* Run the handler. */
1909 *return_ka = *ka;
1911 if (ka->sa.sa_flags & SA_ONESHOT)
1912 ka->sa.sa_handler = SIG_DFL;
1914 break; /* will return non-zero "signr" value */
1918 * Now we are doing the default action for this signal.
1920 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1921 continue;
1924 * Global init gets no signals it doesn't want.
1926 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1927 !signal_group_exit(signal))
1928 continue;
1930 if (sig_kernel_stop(signr)) {
1932 * The default action is to stop all threads in
1933 * the thread group. The job control signals
1934 * do nothing in an orphaned pgrp, but SIGSTOP
1935 * always works. Note that siglock needs to be
1936 * dropped during the call to is_orphaned_pgrp()
1937 * because of lock ordering with tasklist_lock.
1938 * This allows an intervening SIGCONT to be posted.
1939 * We need to check for that and bail out if necessary.
1941 if (signr != SIGSTOP) {
1942 spin_unlock_irq(&sighand->siglock);
1944 /* signals can be posted during this window */
1946 if (is_current_pgrp_orphaned())
1947 goto relock;
1949 spin_lock_irq(&sighand->siglock);
1952 if (likely(do_signal_stop(info->si_signo))) {
1953 /* It released the siglock. */
1954 goto relock;
1958 * We didn't actually stop, due to a race
1959 * with SIGCONT or something like that.
1961 continue;
1964 spin_unlock_irq(&sighand->siglock);
1967 * Anything else is fatal, maybe with a core dump.
1969 current->flags |= PF_SIGNALED;
1971 if (sig_kernel_coredump(signr)) {
1972 if (print_fatal_signals)
1973 print_fatal_signal(regs, info->si_signo);
1975 * If it was able to dump core, this kills all
1976 * other threads in the group and synchronizes with
1977 * their demise. If we lost the race with another
1978 * thread getting here, it set group_exit_code
1979 * first and our do_group_exit call below will use
1980 * that value and ignore the one we pass it.
1982 do_coredump(info->si_signo, info->si_signo, regs);
1986 * Death signals, no core dump.
1988 do_group_exit(info->si_signo);
1989 /* NOTREACHED */
1991 spin_unlock_irq(&sighand->siglock);
1992 return signr;
1995 void exit_signals(struct task_struct *tsk)
1997 int group_stop = 0;
1998 struct task_struct *t;
2000 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2001 tsk->flags |= PF_EXITING;
2002 return;
2005 spin_lock_irq(&tsk->sighand->siglock);
2007 * From now this task is not visible for group-wide signals,
2008 * see wants_signal(), do_signal_stop().
2010 tsk->flags |= PF_EXITING;
2011 if (!signal_pending(tsk))
2012 goto out;
2014 /* It could be that __group_complete_signal() choose us to
2015 * notify about group-wide signal. Another thread should be
2016 * woken now to take the signal since we will not.
2018 for (t = tsk; (t = next_thread(t)) != tsk; )
2019 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2020 recalc_sigpending_and_wake(t);
2022 if (unlikely(tsk->signal->group_stop_count) &&
2023 !--tsk->signal->group_stop_count) {
2024 tsk->signal->flags = SIGNAL_STOP_STOPPED;
2025 group_stop = 1;
2027 out:
2028 spin_unlock_irq(&tsk->sighand->siglock);
2030 if (unlikely(group_stop) && tracehook_notify_jctl(1, CLD_STOPPED)) {
2031 read_lock(&tasklist_lock);
2032 do_notify_parent_cldstop(tsk, CLD_STOPPED);
2033 read_unlock(&tasklist_lock);
2037 EXPORT_SYMBOL(recalc_sigpending);
2038 EXPORT_SYMBOL_GPL(dequeue_signal);
2039 EXPORT_SYMBOL(flush_signals);
2040 EXPORT_SYMBOL(force_sig);
2041 EXPORT_SYMBOL(send_sig);
2042 EXPORT_SYMBOL(send_sig_info);
2043 EXPORT_SYMBOL(sigprocmask);
2044 EXPORT_SYMBOL(block_all_signals);
2045 EXPORT_SYMBOL(unblock_all_signals);
2049 * System call entry points.
2052 SYSCALL_DEFINE0(restart_syscall)
2054 struct restart_block *restart = &current_thread_info()->restart_block;
2055 return restart->fn(restart);
2058 long do_no_restart_syscall(struct restart_block *param)
2060 return -EINTR;
2064 * We don't need to get the kernel lock - this is all local to this
2065 * particular thread.. (and that's good, because this is _heavily_
2066 * used by various programs)
2070 * This is also useful for kernel threads that want to temporarily
2071 * (or permanently) block certain signals.
2073 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2074 * interface happily blocks "unblockable" signals like SIGKILL
2075 * and friends.
2077 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2079 int error;
2081 spin_lock_irq(&current->sighand->siglock);
2082 if (oldset)
2083 *oldset = current->blocked;
2085 error = 0;
2086 switch (how) {
2087 case SIG_BLOCK:
2088 sigorsets(&current->blocked, &current->blocked, set);
2089 break;
2090 case SIG_UNBLOCK:
2091 signandsets(&current->blocked, &current->blocked, set);
2092 break;
2093 case SIG_SETMASK:
2094 current->blocked = *set;
2095 break;
2096 default:
2097 error = -EINVAL;
2099 recalc_sigpending();
2100 spin_unlock_irq(&current->sighand->siglock);
2102 return error;
2105 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2106 sigset_t __user *, oset, size_t, sigsetsize)
2108 int error = -EINVAL;
2109 sigset_t old_set, new_set;
2111 /* XXX: Don't preclude handling different sized sigset_t's. */
2112 if (sigsetsize != sizeof(sigset_t))
2113 goto out;
2115 if (set) {
2116 error = -EFAULT;
2117 if (copy_from_user(&new_set, set, sizeof(*set)))
2118 goto out;
2119 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2121 error = sigprocmask(how, &new_set, &old_set);
2122 if (error)
2123 goto out;
2124 if (oset)
2125 goto set_old;
2126 } else if (oset) {
2127 spin_lock_irq(&current->sighand->siglock);
2128 old_set = current->blocked;
2129 spin_unlock_irq(&current->sighand->siglock);
2131 set_old:
2132 error = -EFAULT;
2133 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2134 goto out;
2136 error = 0;
2137 out:
2138 return error;
2141 long do_sigpending(void __user *set, unsigned long sigsetsize)
2143 long error = -EINVAL;
2144 sigset_t pending;
2146 if (sigsetsize > sizeof(sigset_t))
2147 goto out;
2149 spin_lock_irq(&current->sighand->siglock);
2150 sigorsets(&pending, &current->pending.signal,
2151 &current->signal->shared_pending.signal);
2152 spin_unlock_irq(&current->sighand->siglock);
2154 /* Outside the lock because only this thread touches it. */
2155 sigandsets(&pending, &current->blocked, &pending);
2157 error = -EFAULT;
2158 if (!copy_to_user(set, &pending, sigsetsize))
2159 error = 0;
2161 out:
2162 return error;
2165 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2167 return do_sigpending(set, sigsetsize);
2170 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2172 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2174 int err;
2176 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2177 return -EFAULT;
2178 if (from->si_code < 0)
2179 return __copy_to_user(to, from, sizeof(siginfo_t))
2180 ? -EFAULT : 0;
2182 * If you change siginfo_t structure, please be sure
2183 * this code is fixed accordingly.
2184 * Please remember to update the signalfd_copyinfo() function
2185 * inside fs/signalfd.c too, in case siginfo_t changes.
2186 * It should never copy any pad contained in the structure
2187 * to avoid security leaks, but must copy the generic
2188 * 3 ints plus the relevant union member.
2190 err = __put_user(from->si_signo, &to->si_signo);
2191 err |= __put_user(from->si_errno, &to->si_errno);
2192 err |= __put_user((short)from->si_code, &to->si_code);
2193 switch (from->si_code & __SI_MASK) {
2194 case __SI_KILL:
2195 err |= __put_user(from->si_pid, &to->si_pid);
2196 err |= __put_user(from->si_uid, &to->si_uid);
2197 break;
2198 case __SI_TIMER:
2199 err |= __put_user(from->si_tid, &to->si_tid);
2200 err |= __put_user(from->si_overrun, &to->si_overrun);
2201 err |= __put_user(from->si_ptr, &to->si_ptr);
2202 break;
2203 case __SI_POLL:
2204 err |= __put_user(from->si_band, &to->si_band);
2205 err |= __put_user(from->si_fd, &to->si_fd);
2206 break;
2207 case __SI_FAULT:
2208 err |= __put_user(from->si_addr, &to->si_addr);
2209 #ifdef __ARCH_SI_TRAPNO
2210 err |= __put_user(from->si_trapno, &to->si_trapno);
2211 #endif
2212 break;
2213 case __SI_CHLD:
2214 err |= __put_user(from->si_pid, &to->si_pid);
2215 err |= __put_user(from->si_uid, &to->si_uid);
2216 err |= __put_user(from->si_status, &to->si_status);
2217 err |= __put_user(from->si_utime, &to->si_utime);
2218 err |= __put_user(from->si_stime, &to->si_stime);
2219 break;
2220 case __SI_RT: /* This is not generated by the kernel as of now. */
2221 case __SI_MESGQ: /* But this is */
2222 err |= __put_user(from->si_pid, &to->si_pid);
2223 err |= __put_user(from->si_uid, &to->si_uid);
2224 err |= __put_user(from->si_ptr, &to->si_ptr);
2225 break;
2226 default: /* this is just in case for now ... */
2227 err |= __put_user(from->si_pid, &to->si_pid);
2228 err |= __put_user(from->si_uid, &to->si_uid);
2229 break;
2231 return err;
2234 #endif
2236 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2237 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2238 size_t, sigsetsize)
2240 int ret, sig;
2241 sigset_t these;
2242 struct timespec ts;
2243 siginfo_t info;
2244 long timeout = 0;
2246 /* XXX: Don't preclude handling different sized sigset_t's. */
2247 if (sigsetsize != sizeof(sigset_t))
2248 return -EINVAL;
2250 if (copy_from_user(&these, uthese, sizeof(these)))
2251 return -EFAULT;
2254 * Invert the set of allowed signals to get those we
2255 * want to block.
2257 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2258 signotset(&these);
2260 if (uts) {
2261 if (copy_from_user(&ts, uts, sizeof(ts)))
2262 return -EFAULT;
2263 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2264 || ts.tv_sec < 0)
2265 return -EINVAL;
2268 spin_lock_irq(&current->sighand->siglock);
2269 sig = dequeue_signal(current, &these, &info);
2270 if (!sig) {
2271 timeout = MAX_SCHEDULE_TIMEOUT;
2272 if (uts)
2273 timeout = (timespec_to_jiffies(&ts)
2274 + (ts.tv_sec || ts.tv_nsec));
2276 if (timeout) {
2277 /* None ready -- temporarily unblock those we're
2278 * interested while we are sleeping in so that we'll
2279 * be awakened when they arrive. */
2280 current->real_blocked = current->blocked;
2281 sigandsets(&current->blocked, &current->blocked, &these);
2282 recalc_sigpending();
2283 spin_unlock_irq(&current->sighand->siglock);
2285 timeout = schedule_timeout_interruptible(timeout);
2287 spin_lock_irq(&current->sighand->siglock);
2288 sig = dequeue_signal(current, &these, &info);
2289 current->blocked = current->real_blocked;
2290 siginitset(&current->real_blocked, 0);
2291 recalc_sigpending();
2294 spin_unlock_irq(&current->sighand->siglock);
2296 if (sig) {
2297 ret = sig;
2298 if (uinfo) {
2299 if (copy_siginfo_to_user(uinfo, &info))
2300 ret = -EFAULT;
2302 } else {
2303 ret = -EAGAIN;
2304 if (timeout)
2305 ret = -EINTR;
2308 return ret;
2311 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2313 struct siginfo info;
2315 info.si_signo = sig;
2316 info.si_errno = 0;
2317 info.si_code = SI_USER;
2318 info.si_pid = task_tgid_vnr(current);
2319 info.si_uid = current_uid();
2321 return kill_something_info(sig, &info, pid);
2324 static int
2325 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2327 struct task_struct *p;
2328 unsigned long flags;
2329 int error = -ESRCH;
2331 rcu_read_lock();
2332 p = find_task_by_vpid(pid);
2333 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2334 error = check_kill_permission(sig, info, p);
2336 * The null signal is a permissions and process existence
2337 * probe. No signal is actually delivered.
2339 * If lock_task_sighand() fails we pretend the task dies
2340 * after receiving the signal. The window is tiny, and the
2341 * signal is private anyway.
2343 if (!error && sig && lock_task_sighand(p, &flags)) {
2344 error = specific_send_sig_info(sig, info, p);
2345 unlock_task_sighand(p, &flags);
2348 rcu_read_unlock();
2350 return error;
2353 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2355 struct siginfo info;
2357 info.si_signo = sig;
2358 info.si_errno = 0;
2359 info.si_code = SI_TKILL;
2360 info.si_pid = task_tgid_vnr(current);
2361 info.si_uid = current_uid();
2363 return do_send_specific(tgid, pid, sig, &info);
2367 * sys_tgkill - send signal to one specific thread
2368 * @tgid: the thread group ID of the thread
2369 * @pid: the PID of the thread
2370 * @sig: signal to be sent
2372 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2373 * exists but it's not belonging to the target process anymore. This
2374 * method solves the problem of threads exiting and PIDs getting reused.
2376 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2378 /* This is only valid for single tasks */
2379 if (pid <= 0 || tgid <= 0)
2380 return -EINVAL;
2382 return do_tkill(tgid, pid, sig);
2386 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2388 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2390 /* This is only valid for single tasks */
2391 if (pid <= 0)
2392 return -EINVAL;
2394 return do_tkill(0, pid, sig);
2397 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2398 siginfo_t __user *, uinfo)
2400 siginfo_t info;
2402 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2403 return -EFAULT;
2405 /* Not even root can pretend to send signals from the kernel.
2406 Nor can they impersonate a kill(), which adds source info. */
2407 if (info.si_code >= 0)
2408 return -EPERM;
2409 info.si_signo = sig;
2411 /* POSIX.1b doesn't mention process groups. */
2412 return kill_proc_info(sig, &info, pid);
2415 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2417 /* This is only valid for single tasks */
2418 if (pid <= 0 || tgid <= 0)
2419 return -EINVAL;
2421 /* Not even root can pretend to send signals from the kernel.
2422 Nor can they impersonate a kill(), which adds source info. */
2423 if (info->si_code >= 0)
2424 return -EPERM;
2425 info->si_signo = sig;
2427 return do_send_specific(tgid, pid, sig, info);
2430 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2431 siginfo_t __user *, uinfo)
2433 siginfo_t info;
2435 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2436 return -EFAULT;
2438 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2441 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2443 struct task_struct *t = current;
2444 struct k_sigaction *k;
2445 sigset_t mask;
2447 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2448 return -EINVAL;
2450 k = &t->sighand->action[sig-1];
2452 spin_lock_irq(&current->sighand->siglock);
2453 if (oact)
2454 *oact = *k;
2456 if (act) {
2457 sigdelsetmask(&act->sa.sa_mask,
2458 sigmask(SIGKILL) | sigmask(SIGSTOP));
2459 *k = *act;
2461 * POSIX 3.3.1.3:
2462 * "Setting a signal action to SIG_IGN for a signal that is
2463 * pending shall cause the pending signal to be discarded,
2464 * whether or not it is blocked."
2466 * "Setting a signal action to SIG_DFL for a signal that is
2467 * pending and whose default action is to ignore the signal
2468 * (for example, SIGCHLD), shall cause the pending signal to
2469 * be discarded, whether or not it is blocked"
2471 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2472 sigemptyset(&mask);
2473 sigaddset(&mask, sig);
2474 rm_from_queue_full(&mask, &t->signal->shared_pending);
2475 do {
2476 rm_from_queue_full(&mask, &t->pending);
2477 t = next_thread(t);
2478 } while (t != current);
2482 spin_unlock_irq(&current->sighand->siglock);
2483 return 0;
2486 int
2487 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2489 stack_t oss;
2490 int error;
2492 if (uoss) {
2493 oss.ss_sp = (void __user *) current->sas_ss_sp;
2494 oss.ss_size = current->sas_ss_size;
2495 oss.ss_flags = sas_ss_flags(sp);
2498 if (uss) {
2499 void __user *ss_sp;
2500 size_t ss_size;
2501 int ss_flags;
2503 error = -EFAULT;
2504 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2505 || __get_user(ss_sp, &uss->ss_sp)
2506 || __get_user(ss_flags, &uss->ss_flags)
2507 || __get_user(ss_size, &uss->ss_size))
2508 goto out;
2510 error = -EPERM;
2511 if (on_sig_stack(sp))
2512 goto out;
2514 error = -EINVAL;
2517 * Note - this code used to test ss_flags incorrectly
2518 * old code may have been written using ss_flags==0
2519 * to mean ss_flags==SS_ONSTACK (as this was the only
2520 * way that worked) - this fix preserves that older
2521 * mechanism
2523 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2524 goto out;
2526 if (ss_flags == SS_DISABLE) {
2527 ss_size = 0;
2528 ss_sp = NULL;
2529 } else {
2530 error = -ENOMEM;
2531 if (ss_size < MINSIGSTKSZ)
2532 goto out;
2535 current->sas_ss_sp = (unsigned long) ss_sp;
2536 current->sas_ss_size = ss_size;
2539 if (uoss) {
2540 error = -EFAULT;
2541 if (copy_to_user(uoss, &oss, sizeof(oss)))
2542 goto out;
2545 error = 0;
2546 out:
2547 return error;
2550 #ifdef __ARCH_WANT_SYS_SIGPENDING
2552 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2554 return do_sigpending(set, sizeof(*set));
2557 #endif
2559 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2560 /* Some platforms have their own version with special arguments others
2561 support only sys_rt_sigprocmask. */
2563 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2564 old_sigset_t __user *, oset)
2566 int error;
2567 old_sigset_t old_set, new_set;
2569 if (set) {
2570 error = -EFAULT;
2571 if (copy_from_user(&new_set, set, sizeof(*set)))
2572 goto out;
2573 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2575 spin_lock_irq(&current->sighand->siglock);
2576 old_set = current->blocked.sig[0];
2578 error = 0;
2579 switch (how) {
2580 default:
2581 error = -EINVAL;
2582 break;
2583 case SIG_BLOCK:
2584 sigaddsetmask(&current->blocked, new_set);
2585 break;
2586 case SIG_UNBLOCK:
2587 sigdelsetmask(&current->blocked, new_set);
2588 break;
2589 case SIG_SETMASK:
2590 current->blocked.sig[0] = new_set;
2591 break;
2594 recalc_sigpending();
2595 spin_unlock_irq(&current->sighand->siglock);
2596 if (error)
2597 goto out;
2598 if (oset)
2599 goto set_old;
2600 } else if (oset) {
2601 old_set = current->blocked.sig[0];
2602 set_old:
2603 error = -EFAULT;
2604 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2605 goto out;
2607 error = 0;
2608 out:
2609 return error;
2611 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2613 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2614 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2615 const struct sigaction __user *, act,
2616 struct sigaction __user *, oact,
2617 size_t, sigsetsize)
2619 struct k_sigaction new_sa, old_sa;
2620 int ret = -EINVAL;
2622 /* XXX: Don't preclude handling different sized sigset_t's. */
2623 if (sigsetsize != sizeof(sigset_t))
2624 goto out;
2626 if (act) {
2627 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2628 return -EFAULT;
2631 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2633 if (!ret && oact) {
2634 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2635 return -EFAULT;
2637 out:
2638 return ret;
2640 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2642 #ifdef __ARCH_WANT_SYS_SGETMASK
2645 * For backwards compatibility. Functionality superseded by sigprocmask.
2647 SYSCALL_DEFINE0(sgetmask)
2649 /* SMP safe */
2650 return current->blocked.sig[0];
2653 SYSCALL_DEFINE1(ssetmask, int, newmask)
2655 int old;
2657 spin_lock_irq(&current->sighand->siglock);
2658 old = current->blocked.sig[0];
2660 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2661 sigmask(SIGSTOP)));
2662 recalc_sigpending();
2663 spin_unlock_irq(&current->sighand->siglock);
2665 return old;
2667 #endif /* __ARCH_WANT_SGETMASK */
2669 #ifdef __ARCH_WANT_SYS_SIGNAL
2671 * For backwards compatibility. Functionality superseded by sigaction.
2673 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2675 struct k_sigaction new_sa, old_sa;
2676 int ret;
2678 new_sa.sa.sa_handler = handler;
2679 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2680 sigemptyset(&new_sa.sa.sa_mask);
2682 ret = do_sigaction(sig, &new_sa, &old_sa);
2684 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2686 #endif /* __ARCH_WANT_SYS_SIGNAL */
2688 #ifdef __ARCH_WANT_SYS_PAUSE
2690 SYSCALL_DEFINE0(pause)
2692 current->state = TASK_INTERRUPTIBLE;
2693 schedule();
2694 return -ERESTARTNOHAND;
2697 #endif
2699 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2700 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2702 sigset_t newset;
2704 /* XXX: Don't preclude handling different sized sigset_t's. */
2705 if (sigsetsize != sizeof(sigset_t))
2706 return -EINVAL;
2708 if (copy_from_user(&newset, unewset, sizeof(newset)))
2709 return -EFAULT;
2710 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2712 spin_lock_irq(&current->sighand->siglock);
2713 current->saved_sigmask = current->blocked;
2714 current->blocked = newset;
2715 recalc_sigpending();
2716 spin_unlock_irq(&current->sighand->siglock);
2718 current->state = TASK_INTERRUPTIBLE;
2719 schedule();
2720 set_restore_sigmask();
2721 return -ERESTARTNOHAND;
2723 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2725 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2727 return NULL;
2730 void __init signals_init(void)
2732 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);