code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.34.14-moxart.git] / kernel / signal.c
blob137a3333b444d4dea1f9b946e7ca74cbf4b8865b
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 * allocate a new signal queue record
228 * - this may be called without locks if and only if t == current, otherwise an
229 * appopriate lock must be held to stop the target task from exiting
231 static struct sigqueue *
232 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
234 struct sigqueue *q = NULL;
235 struct user_struct *user;
238 * Protect access to @t credentials. This can go away when all
239 * callers hold rcu read lock.
241 rcu_read_lock();
242 user = get_uid(__task_cred(t)->user);
243 atomic_inc(&user->sigpending);
244 rcu_read_unlock();
246 if (override_rlimit ||
247 atomic_read(&user->sigpending) <=
248 task_rlimit(t, RLIMIT_SIGPENDING)) {
249 q = kmem_cache_alloc(sigqueue_cachep, flags);
250 } else {
251 print_dropped_signal(sig);
254 if (unlikely(q == NULL)) {
255 atomic_dec(&user->sigpending);
256 free_uid(user);
257 } else {
258 INIT_LIST_HEAD(&q->list);
259 q->flags = 0;
260 q->user = user;
263 return q;
266 static void __sigqueue_free(struct sigqueue *q)
268 if (q->flags & SIGQUEUE_PREALLOC)
269 return;
270 atomic_dec(&q->user->sigpending);
271 free_uid(q->user);
272 kmem_cache_free(sigqueue_cachep, q);
275 void flush_sigqueue(struct sigpending *queue)
277 struct sigqueue *q;
279 sigemptyset(&queue->signal);
280 while (!list_empty(&queue->list)) {
281 q = list_entry(queue->list.next, struct sigqueue , list);
282 list_del_init(&q->list);
283 __sigqueue_free(q);
288 * Flush all pending signals for a task.
290 void __flush_signals(struct task_struct *t)
292 clear_tsk_thread_flag(t, TIF_SIGPENDING);
293 flush_sigqueue(&t->pending);
294 flush_sigqueue(&t->signal->shared_pending);
297 void flush_signals(struct task_struct *t)
299 unsigned long flags;
301 spin_lock_irqsave(&t->sighand->siglock, flags);
302 __flush_signals(t);
303 spin_unlock_irqrestore(&t->sighand->siglock, flags);
306 static void __flush_itimer_signals(struct sigpending *pending)
308 sigset_t signal, retain;
309 struct sigqueue *q, *n;
311 signal = pending->signal;
312 sigemptyset(&retain);
314 list_for_each_entry_safe(q, n, &pending->list, list) {
315 int sig = q->info.si_signo;
317 if (likely(q->info.si_code != SI_TIMER)) {
318 sigaddset(&retain, sig);
319 } else {
320 sigdelset(&signal, sig);
321 list_del_init(&q->list);
322 __sigqueue_free(q);
326 sigorsets(&pending->signal, &signal, &retain);
329 void flush_itimer_signals(void)
331 struct task_struct *tsk = current;
332 unsigned long flags;
334 spin_lock_irqsave(&tsk->sighand->siglock, flags);
335 __flush_itimer_signals(&tsk->pending);
336 __flush_itimer_signals(&tsk->signal->shared_pending);
337 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
340 void ignore_signals(struct task_struct *t)
342 int i;
344 for (i = 0; i < _NSIG; ++i)
345 t->sighand->action[i].sa.sa_handler = SIG_IGN;
347 flush_signals(t);
351 * Flush all handlers for a task.
354 void
355 flush_signal_handlers(struct task_struct *t, int force_default)
357 int i;
358 struct k_sigaction *ka = &t->sighand->action[0];
359 for (i = _NSIG ; i != 0 ; i--) {
360 if (force_default || ka->sa.sa_handler != SIG_IGN)
361 ka->sa.sa_handler = SIG_DFL;
362 ka->sa.sa_flags = 0;
363 sigemptyset(&ka->sa.sa_mask);
364 ka++;
368 int unhandled_signal(struct task_struct *tsk, int sig)
370 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
371 if (is_global_init(tsk))
372 return 1;
373 if (handler != SIG_IGN && handler != SIG_DFL)
374 return 0;
375 return !tracehook_consider_fatal_signal(tsk, sig);
379 /* Notify the system that a driver wants to block all signals for this
380 * process, and wants to be notified if any signals at all were to be
381 * sent/acted upon. If the notifier routine returns non-zero, then the
382 * signal will be acted upon after all. If the notifier routine returns 0,
383 * then then signal will be blocked. Only one block per process is
384 * allowed. priv is a pointer to private data that the notifier routine
385 * can use to determine if the signal should be blocked or not. */
387 void
388 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
390 unsigned long flags;
392 spin_lock_irqsave(&current->sighand->siglock, flags);
393 current->notifier_mask = mask;
394 current->notifier_data = priv;
395 current->notifier = notifier;
396 spin_unlock_irqrestore(&current->sighand->siglock, flags);
399 /* Notify the system that blocking has ended. */
401 void
402 unblock_all_signals(void)
404 unsigned long flags;
406 spin_lock_irqsave(&current->sighand->siglock, flags);
407 current->notifier = NULL;
408 current->notifier_data = NULL;
409 recalc_sigpending();
410 spin_unlock_irqrestore(&current->sighand->siglock, flags);
413 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
415 struct sigqueue *q, *first = NULL;
418 * Collect the siginfo appropriate to this signal. Check if
419 * there is another siginfo for the same signal.
421 list_for_each_entry(q, &list->list, list) {
422 if (q->info.si_signo == sig) {
423 if (first)
424 goto still_pending;
425 first = q;
429 sigdelset(&list->signal, sig);
431 if (first) {
432 still_pending:
433 list_del_init(&first->list);
434 copy_siginfo(info, &first->info);
435 __sigqueue_free(first);
436 } else {
437 /* Ok, it wasn't in the queue. This must be
438 a fast-pathed signal or we must have been
439 out of queue space. So zero out the info.
441 info->si_signo = sig;
442 info->si_errno = 0;
443 info->si_code = SI_USER;
444 info->si_pid = 0;
445 info->si_uid = 0;
449 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
450 siginfo_t *info)
452 int sig = next_signal(pending, mask);
454 if (sig) {
455 if (current->notifier) {
456 if (sigismember(current->notifier_mask, sig)) {
457 if (!(current->notifier)(current->notifier_data)) {
458 clear_thread_flag(TIF_SIGPENDING);
459 return 0;
464 collect_signal(sig, pending, info);
467 return sig;
471 * Dequeue a signal and return the element to the caller, which is
472 * expected to free it.
474 * All callers have to hold the siglock.
476 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
478 int signr;
480 /* We only dequeue private signals from ourselves, we don't let
481 * signalfd steal them
483 signr = __dequeue_signal(&tsk->pending, mask, info);
484 if (!signr) {
485 signr = __dequeue_signal(&tsk->signal->shared_pending,
486 mask, info);
488 * itimer signal ?
490 * itimers are process shared and we restart periodic
491 * itimers in the signal delivery path to prevent DoS
492 * attacks in the high resolution timer case. This is
493 * compliant with the old way of self restarting
494 * itimers, as the SIGALRM is a legacy signal and only
495 * queued once. Changing the restart behaviour to
496 * restart the timer in the signal dequeue path is
497 * reducing the timer noise on heavy loaded !highres
498 * systems too.
500 if (unlikely(signr == SIGALRM)) {
501 struct hrtimer *tmr = &tsk->signal->real_timer;
503 if (!hrtimer_is_queued(tmr) &&
504 tsk->signal->it_real_incr.tv64 != 0) {
505 hrtimer_forward(tmr, tmr->base->get_time(),
506 tsk->signal->it_real_incr);
507 hrtimer_restart(tmr);
512 recalc_sigpending();
513 if (!signr)
514 return 0;
516 if (unlikely(sig_kernel_stop(signr))) {
518 * Set a marker that we have dequeued a stop signal. Our
519 * caller might release the siglock and then the pending
520 * stop signal it is about to process is no longer in the
521 * pending bitmasks, but must still be cleared by a SIGCONT
522 * (and overruled by a SIGKILL). So those cases clear this
523 * shared flag after we've set it. Note that this flag may
524 * remain set after the signal we return is ignored or
525 * handled. That doesn't matter because its only purpose
526 * is to alert stop-signal processing code when another
527 * processor has come along and cleared the flag.
529 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
531 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
533 * Release the siglock to ensure proper locking order
534 * of timer locks outside of siglocks. Note, we leave
535 * irqs disabled here, since the posix-timers code is
536 * about to disable them again anyway.
538 spin_unlock(&tsk->sighand->siglock);
539 do_schedule_next_timer(info);
540 spin_lock(&tsk->sighand->siglock);
542 return signr;
546 * Tell a process that it has a new active signal..
548 * NOTE! we rely on the previous spin_lock to
549 * lock interrupts for us! We can only be called with
550 * "siglock" held, and the local interrupt must
551 * have been disabled when that got acquired!
553 * No need to set need_resched since signal event passing
554 * goes through ->blocked
556 void signal_wake_up(struct task_struct *t, int resume)
558 unsigned int mask;
560 set_tsk_thread_flag(t, TIF_SIGPENDING);
563 * For SIGKILL, we want to wake it up in the stopped/traced/killable
564 * case. We don't check t->state here because there is a race with it
565 * executing another processor and just now entering stopped state.
566 * By using wake_up_state, we ensure the process will wake up and
567 * handle its death signal.
569 mask = TASK_INTERRUPTIBLE;
570 if (resume)
571 mask |= TASK_WAKEKILL;
572 if (!wake_up_state(t, mask))
573 kick_process(t);
577 * Remove signals in mask from the pending set and queue.
578 * Returns 1 if any signals were found.
580 * All callers must be holding the siglock.
582 * This version takes a sigset mask and looks at all signals,
583 * not just those in the first mask word.
585 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
587 struct sigqueue *q, *n;
588 sigset_t m;
590 sigandsets(&m, mask, &s->signal);
591 if (sigisemptyset(&m))
592 return 0;
594 signandsets(&s->signal, &s->signal, mask);
595 list_for_each_entry_safe(q, n, &s->list, list) {
596 if (sigismember(mask, q->info.si_signo)) {
597 list_del_init(&q->list);
598 __sigqueue_free(q);
601 return 1;
604 * Remove signals in mask from the pending set and queue.
605 * Returns 1 if any signals were found.
607 * All callers must be holding the siglock.
609 static int rm_from_queue(unsigned long mask, struct sigpending *s)
611 struct sigqueue *q, *n;
613 if (!sigtestsetmask(&s->signal, mask))
614 return 0;
616 sigdelsetmask(&s->signal, mask);
617 list_for_each_entry_safe(q, n, &s->list, list) {
618 if (q->info.si_signo < SIGRTMIN &&
619 (mask & sigmask(q->info.si_signo))) {
620 list_del_init(&q->list);
621 __sigqueue_free(q);
624 return 1;
627 static inline int is_si_special(const struct siginfo *info)
629 return info <= SEND_SIG_FORCED;
632 static inline bool si_fromuser(const struct siginfo *info)
634 return info == SEND_SIG_NOINFO ||
635 (!is_si_special(info) && SI_FROMUSER(info));
639 * Bad permissions for sending the signal
640 * - the caller must hold the RCU read lock
642 static int check_kill_permission(int sig, struct siginfo *info,
643 struct task_struct *t)
645 const struct cred *cred, *tcred;
646 struct pid *sid;
647 int error;
649 if (!valid_signal(sig))
650 return -EINVAL;
652 if (!si_fromuser(info))
653 return 0;
655 error = audit_signal_info(sig, t); /* Let audit system see the signal */
656 if (error)
657 return error;
659 cred = current_cred();
660 tcred = __task_cred(t);
661 if (!same_thread_group(current, t) &&
662 (cred->euid ^ tcred->suid) &&
663 (cred->euid ^ tcred->uid) &&
664 (cred->uid ^ tcred->suid) &&
665 (cred->uid ^ tcred->uid) &&
666 !capable(CAP_KILL)) {
667 switch (sig) {
668 case SIGCONT:
669 sid = task_session(t);
671 * We don't return the error if sid == NULL. The
672 * task was unhashed, the caller must notice this.
674 if (!sid || sid == task_session(current))
675 break;
676 default:
677 return -EPERM;
681 return security_task_kill(t, info, sig, 0);
685 * Handle magic process-wide effects of stop/continue signals. Unlike
686 * the signal actions, these happen immediately at signal-generation
687 * time regardless of blocking, ignoring, or handling. This does the
688 * actual continuing for SIGCONT, but not the actual stopping for stop
689 * signals. The process stop is done as a signal action for SIG_DFL.
691 * Returns true if the signal should be actually delivered, otherwise
692 * it should be dropped.
694 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
696 struct signal_struct *signal = p->signal;
697 struct task_struct *t;
699 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
701 * The process is in the middle of dying, nothing to do.
703 } else if (sig_kernel_stop(sig)) {
705 * This is a stop signal. Remove SIGCONT from all queues.
707 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
708 t = p;
709 do {
710 rm_from_queue(sigmask(SIGCONT), &t->pending);
711 } while_each_thread(p, t);
712 } else if (sig == SIGCONT) {
713 unsigned int why;
715 * Remove all stop signals from all queues,
716 * and wake all threads.
718 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
719 t = p;
720 do {
721 unsigned int state;
722 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
724 * If there is a handler for SIGCONT, we must make
725 * sure that no thread returns to user mode before
726 * we post the signal, in case it was the only
727 * thread eligible to run the signal handler--then
728 * it must not do anything between resuming and
729 * running the handler. With the TIF_SIGPENDING
730 * flag set, the thread will pause and acquire the
731 * siglock that we hold now and until we've queued
732 * the pending signal.
734 * Wake up the stopped thread _after_ setting
735 * TIF_SIGPENDING
737 state = __TASK_STOPPED;
738 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
739 set_tsk_thread_flag(t, TIF_SIGPENDING);
740 state |= TASK_INTERRUPTIBLE;
742 wake_up_state(t, state);
743 } while_each_thread(p, t);
746 * Notify the parent with CLD_CONTINUED if we were stopped.
748 * If we were in the middle of a group stop, we pretend it
749 * was already finished, and then continued. Since SIGCHLD
750 * doesn't queue we report only CLD_STOPPED, as if the next
751 * CLD_CONTINUED was dropped.
753 why = 0;
754 if (signal->flags & SIGNAL_STOP_STOPPED)
755 why |= SIGNAL_CLD_CONTINUED;
756 else if (signal->group_stop_count)
757 why |= SIGNAL_CLD_STOPPED;
759 if (why) {
761 * The first thread which returns from do_signal_stop()
762 * will take ->siglock, notice SIGNAL_CLD_MASK, and
763 * notify its parent. See get_signal_to_deliver().
765 signal->flags = why | SIGNAL_STOP_CONTINUED;
766 signal->group_stop_count = 0;
767 signal->group_exit_code = 0;
768 } else {
770 * We are not stopped, but there could be a stop
771 * signal in the middle of being processed after
772 * being removed from the queue. Clear that too.
774 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
778 return !sig_ignored(p, sig, from_ancestor_ns);
782 * Test if P wants to take SIG. After we've checked all threads with this,
783 * it's equivalent to finding no threads not blocking SIG. Any threads not
784 * blocking SIG were ruled out because they are not running and already
785 * have pending signals. Such threads will dequeue from the shared queue
786 * as soon as they're available, so putting the signal on the shared queue
787 * will be equivalent to sending it to one such thread.
789 static inline int wants_signal(int sig, struct task_struct *p)
791 if (sigismember(&p->blocked, sig))
792 return 0;
793 if (p->flags & PF_EXITING)
794 return 0;
795 if (sig == SIGKILL)
796 return 1;
797 if (task_is_stopped_or_traced(p))
798 return 0;
799 return task_curr(p) || !signal_pending(p);
802 static void complete_signal(int sig, struct task_struct *p, int group)
804 struct signal_struct *signal = p->signal;
805 struct task_struct *t;
808 * Now find a thread we can wake up to take the signal off the queue.
810 * If the main thread wants the signal, it gets first crack.
811 * Probably the least surprising to the average bear.
813 if (wants_signal(sig, p))
814 t = p;
815 else if (!group || thread_group_empty(p))
817 * There is just one thread and it does not need to be woken.
818 * It will dequeue unblocked signals before it runs again.
820 return;
821 else {
823 * Otherwise try to find a suitable thread.
825 t = signal->curr_target;
826 while (!wants_signal(sig, t)) {
827 t = next_thread(t);
828 if (t == signal->curr_target)
830 * No thread needs to be woken.
831 * Any eligible threads will see
832 * the signal in the queue soon.
834 return;
836 signal->curr_target = t;
840 * Found a killable thread. If the signal will be fatal,
841 * then start taking the whole group down immediately.
843 if (sig_fatal(p, sig) &&
844 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
845 !sigismember(&t->real_blocked, sig) &&
846 (sig == SIGKILL ||
847 !tracehook_consider_fatal_signal(t, sig))) {
849 * This signal will be fatal to the whole group.
851 if (!sig_kernel_coredump(sig)) {
853 * Start a group exit and wake everybody up.
854 * This way we don't have other threads
855 * running and doing things after a slower
856 * thread has the fatal signal pending.
858 signal->flags = SIGNAL_GROUP_EXIT;
859 signal->group_exit_code = sig;
860 signal->group_stop_count = 0;
861 t = p;
862 do {
863 sigaddset(&t->pending.signal, SIGKILL);
864 signal_wake_up(t, 1);
865 } while_each_thread(p, t);
866 return;
871 * The signal is already in the shared-pending queue.
872 * Tell the chosen thread to wake up and dequeue it.
874 signal_wake_up(t, sig == SIGKILL);
875 return;
878 static inline int legacy_queue(struct sigpending *signals, int sig)
880 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
883 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
884 int group, int from_ancestor_ns)
886 struct sigpending *pending;
887 struct sigqueue *q;
888 int override_rlimit;
890 trace_signal_generate(sig, info, t);
892 assert_spin_locked(&t->sighand->siglock);
894 if (!prepare_signal(sig, t, from_ancestor_ns))
895 return 0;
897 pending = group ? &t->signal->shared_pending : &t->pending;
899 * Short-circuit ignored signals and support queuing
900 * exactly one non-rt signal, so that we can get more
901 * detailed information about the cause of the signal.
903 if (legacy_queue(pending, sig))
904 return 0;
906 * fast-pathed signals for kernel-internal things like SIGSTOP
907 * or SIGKILL.
909 if (info == SEND_SIG_FORCED)
910 goto out_set;
912 /* Real-time signals must be queued if sent by sigqueue, or
913 some other real-time mechanism. It is implementation
914 defined whether kill() does so. We attempt to do so, on
915 the principle of least surprise, but since kill is not
916 allowed to fail with EAGAIN when low on memory we just
917 make sure at least one signal gets delivered and don't
918 pass on the info struct. */
920 if (sig < SIGRTMIN)
921 override_rlimit = (is_si_special(info) || info->si_code >= 0);
922 else
923 override_rlimit = 0;
925 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
926 override_rlimit);
927 if (q) {
928 list_add_tail(&q->list, &pending->list);
929 switch ((unsigned long) info) {
930 case (unsigned long) SEND_SIG_NOINFO:
931 q->info.si_signo = sig;
932 q->info.si_errno = 0;
933 q->info.si_code = SI_USER;
934 q->info.si_pid = task_tgid_nr_ns(current,
935 task_active_pid_ns(t));
936 q->info.si_uid = current_uid();
937 break;
938 case (unsigned long) SEND_SIG_PRIV:
939 q->info.si_signo = sig;
940 q->info.si_errno = 0;
941 q->info.si_code = SI_KERNEL;
942 q->info.si_pid = 0;
943 q->info.si_uid = 0;
944 break;
945 default:
946 copy_siginfo(&q->info, info);
947 if (from_ancestor_ns)
948 q->info.si_pid = 0;
949 break;
951 } else if (!is_si_special(info)) {
952 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
954 * Queue overflow, abort. We may abort if the
955 * signal was rt and sent by user using something
956 * other than kill().
958 trace_signal_overflow_fail(sig, group, info);
959 return -EAGAIN;
960 } else {
962 * This is a silent loss of information. We still
963 * send the signal, but the *info bits are lost.
965 trace_signal_lose_info(sig, group, info);
969 out_set:
970 signalfd_notify(t, sig);
971 sigaddset(&pending->signal, sig);
972 complete_signal(sig, t, group);
973 return 0;
976 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
977 int group)
979 int from_ancestor_ns = 0;
981 #ifdef CONFIG_PID_NS
982 from_ancestor_ns = si_fromuser(info) &&
983 !task_pid_nr_ns(current, task_active_pid_ns(t));
984 #endif
986 return __send_signal(sig, info, t, group, from_ancestor_ns);
989 static void print_fatal_signal(struct pt_regs *regs, int signr)
991 printk("%s/%d: potentially unexpected fatal signal %d.\n",
992 current->comm, task_pid_nr(current), signr);
994 #if defined(__i386__) && !defined(__arch_um__)
995 printk("code at %08lx: ", regs->ip);
997 int i;
998 for (i = 0; i < 16; i++) {
999 unsigned char insn;
1001 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1002 break;
1003 printk("%02x ", insn);
1006 #endif
1007 printk("\n");
1008 preempt_disable();
1009 show_regs(regs);
1010 preempt_enable();
1013 static int __init setup_print_fatal_signals(char *str)
1015 get_option (&str, &print_fatal_signals);
1017 return 1;
1020 __setup("print-fatal-signals=", setup_print_fatal_signals);
1023 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1025 return send_signal(sig, info, p, 1);
1028 static int
1029 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1031 return send_signal(sig, info, t, 0);
1034 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1035 bool group)
1037 unsigned long flags;
1038 int ret = -ESRCH;
1040 if (lock_task_sighand(p, &flags)) {
1041 ret = send_signal(sig, info, p, group);
1042 unlock_task_sighand(p, &flags);
1045 return ret;
1049 * Force a signal that the process can't ignore: if necessary
1050 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1052 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1053 * since we do not want to have a signal handler that was blocked
1054 * be invoked when user space had explicitly blocked it.
1056 * We don't want to have recursive SIGSEGV's etc, for example,
1057 * that is why we also clear SIGNAL_UNKILLABLE.
1060 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1062 unsigned long int flags;
1063 int ret, blocked, ignored;
1064 struct k_sigaction *action;
1066 spin_lock_irqsave(&t->sighand->siglock, flags);
1067 action = &t->sighand->action[sig-1];
1068 ignored = action->sa.sa_handler == SIG_IGN;
1069 blocked = sigismember(&t->blocked, sig);
1070 if (blocked || ignored) {
1071 action->sa.sa_handler = SIG_DFL;
1072 if (blocked) {
1073 sigdelset(&t->blocked, sig);
1074 recalc_sigpending_and_wake(t);
1077 if (action->sa.sa_handler == SIG_DFL)
1078 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1079 ret = specific_send_sig_info(sig, info, t);
1080 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1082 return ret;
1086 * Nuke all other threads in the group.
1088 void zap_other_threads(struct task_struct *p)
1090 struct task_struct *t;
1092 p->signal->group_stop_count = 0;
1094 for (t = next_thread(p); t != p; t = next_thread(t)) {
1096 * Don't bother with already dead threads
1098 if (t->exit_state)
1099 continue;
1101 /* SIGKILL will be handled before any pending SIGSTOP */
1102 sigaddset(&t->pending.signal, SIGKILL);
1103 signal_wake_up(t, 1);
1107 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1109 struct sighand_struct *sighand;
1111 rcu_read_lock();
1112 for (;;) {
1113 sighand = rcu_dereference(tsk->sighand);
1114 if (unlikely(sighand == NULL))
1115 break;
1117 spin_lock_irqsave(&sighand->siglock, *flags);
1118 if (likely(sighand == tsk->sighand))
1119 break;
1120 spin_unlock_irqrestore(&sighand->siglock, *flags);
1122 rcu_read_unlock();
1124 return sighand;
1128 * send signal info to all the members of a group
1130 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1132 int ret;
1134 rcu_read_lock();
1135 ret = check_kill_permission(sig, info, p);
1136 rcu_read_unlock();
1138 if (!ret && sig)
1139 ret = do_send_sig_info(sig, info, p, true);
1141 return ret;
1145 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1146 * control characters do (^C, ^Z etc)
1147 * - the caller must hold at least a readlock on tasklist_lock
1149 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1151 struct task_struct *p = NULL;
1152 int retval, success;
1154 success = 0;
1155 retval = -ESRCH;
1156 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1157 int err = group_send_sig_info(sig, info, p);
1158 success |= !err;
1159 retval = err;
1160 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1161 return success ? 0 : retval;
1164 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1166 int error = -ESRCH;
1167 struct task_struct *p;
1169 rcu_read_lock();
1170 retry:
1171 p = pid_task(pid, PIDTYPE_PID);
1172 if (p) {
1173 error = group_send_sig_info(sig, info, p);
1174 if (unlikely(error == -ESRCH))
1176 * The task was unhashed in between, try again.
1177 * If it is dead, pid_task() will return NULL,
1178 * if we race with de_thread() it will find the
1179 * new leader.
1181 goto retry;
1183 rcu_read_unlock();
1185 return error;
1189 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1191 int error;
1192 rcu_read_lock();
1193 error = kill_pid_info(sig, info, find_vpid(pid));
1194 rcu_read_unlock();
1195 return error;
1198 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1199 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1200 uid_t uid, uid_t euid, u32 secid)
1202 int ret = -EINVAL;
1203 struct task_struct *p;
1204 const struct cred *pcred;
1205 unsigned long flags;
1207 if (!valid_signal(sig))
1208 return ret;
1210 rcu_read_lock();
1211 p = pid_task(pid, PIDTYPE_PID);
1212 if (!p) {
1213 ret = -ESRCH;
1214 goto out_unlock;
1216 pcred = __task_cred(p);
1217 if (si_fromuser(info) &&
1218 euid != pcred->suid && euid != pcred->uid &&
1219 uid != pcred->suid && uid != pcred->uid) {
1220 ret = -EPERM;
1221 goto out_unlock;
1223 ret = security_task_kill(p, info, sig, secid);
1224 if (ret)
1225 goto out_unlock;
1227 if (sig) {
1228 if (lock_task_sighand(p, &flags)) {
1229 ret = __send_signal(sig, info, p, 1, 0);
1230 unlock_task_sighand(p, &flags);
1231 } else
1232 ret = -ESRCH;
1234 out_unlock:
1235 rcu_read_unlock();
1236 return ret;
1238 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1241 * kill_something_info() interprets pid in interesting ways just like kill(2).
1243 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1244 * is probably wrong. Should make it like BSD or SYSV.
1247 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1249 int ret;
1251 if (pid > 0) {
1252 rcu_read_lock();
1253 ret = kill_pid_info(sig, info, find_vpid(pid));
1254 rcu_read_unlock();
1255 return ret;
1258 read_lock(&tasklist_lock);
1259 if (pid != -1) {
1260 ret = __kill_pgrp_info(sig, info,
1261 pid ? find_vpid(-pid) : task_pgrp(current));
1262 } else {
1263 int retval = 0, count = 0;
1264 struct task_struct * p;
1266 for_each_process(p) {
1267 if (task_pid_vnr(p) > 1 &&
1268 !same_thread_group(p, current)) {
1269 int err = group_send_sig_info(sig, info, p);
1270 ++count;
1271 if (err != -EPERM)
1272 retval = err;
1275 ret = count ? retval : -ESRCH;
1277 read_unlock(&tasklist_lock);
1279 return ret;
1283 * These are for backward compatibility with the rest of the kernel source.
1287 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1290 * Make sure legacy kernel users don't send in bad values
1291 * (normal paths check this in check_kill_permission).
1293 if (!valid_signal(sig))
1294 return -EINVAL;
1296 return do_send_sig_info(sig, info, p, false);
1299 #define __si_special(priv) \
1300 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1303 send_sig(int sig, struct task_struct *p, int priv)
1305 return send_sig_info(sig, __si_special(priv), p);
1308 void
1309 force_sig(int sig, struct task_struct *p)
1311 force_sig_info(sig, SEND_SIG_PRIV, p);
1315 * When things go south during signal handling, we
1316 * will force a SIGSEGV. And if the signal that caused
1317 * the problem was already a SIGSEGV, we'll want to
1318 * make sure we don't even try to deliver the signal..
1321 force_sigsegv(int sig, struct task_struct *p)
1323 if (sig == SIGSEGV) {
1324 unsigned long flags;
1325 spin_lock_irqsave(&p->sighand->siglock, flags);
1326 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1327 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1329 force_sig(SIGSEGV, p);
1330 return 0;
1333 int kill_pgrp(struct pid *pid, int sig, int priv)
1335 int ret;
1337 read_lock(&tasklist_lock);
1338 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1339 read_unlock(&tasklist_lock);
1341 return ret;
1343 EXPORT_SYMBOL(kill_pgrp);
1345 int kill_pid(struct pid *pid, int sig, int priv)
1347 return kill_pid_info(sig, __si_special(priv), pid);
1349 EXPORT_SYMBOL(kill_pid);
1352 * These functions support sending signals using preallocated sigqueue
1353 * structures. This is needed "because realtime applications cannot
1354 * afford to lose notifications of asynchronous events, like timer
1355 * expirations or I/O completions". In the case of Posix Timers
1356 * we allocate the sigqueue structure from the timer_create. If this
1357 * allocation fails we are able to report the failure to the application
1358 * with an EAGAIN error.
1360 struct sigqueue *sigqueue_alloc(void)
1362 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1364 if (q)
1365 q->flags |= SIGQUEUE_PREALLOC;
1367 return q;
1370 void sigqueue_free(struct sigqueue *q)
1372 unsigned long flags;
1373 spinlock_t *lock = &current->sighand->siglock;
1375 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1377 * We must hold ->siglock while testing q->list
1378 * to serialize with collect_signal() or with
1379 * __exit_signal()->flush_sigqueue().
1381 spin_lock_irqsave(lock, flags);
1382 q->flags &= ~SIGQUEUE_PREALLOC;
1384 * If it is queued it will be freed when dequeued,
1385 * like the "regular" sigqueue.
1387 if (!list_empty(&q->list))
1388 q = NULL;
1389 spin_unlock_irqrestore(lock, flags);
1391 if (q)
1392 __sigqueue_free(q);
1395 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1397 int sig = q->info.si_signo;
1398 struct sigpending *pending;
1399 unsigned long flags;
1400 int ret;
1402 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1404 ret = -1;
1405 if (!likely(lock_task_sighand(t, &flags)))
1406 goto ret;
1408 ret = 1; /* the signal is ignored */
1409 if (!prepare_signal(sig, t, 0))
1410 goto out;
1412 ret = 0;
1413 if (unlikely(!list_empty(&q->list))) {
1415 * If an SI_TIMER entry is already queue just increment
1416 * the overrun count.
1418 BUG_ON(q->info.si_code != SI_TIMER);
1419 q->info.si_overrun++;
1420 goto out;
1422 q->info.si_overrun = 0;
1424 signalfd_notify(t, sig);
1425 pending = group ? &t->signal->shared_pending : &t->pending;
1426 list_add_tail(&q->list, &pending->list);
1427 sigaddset(&pending->signal, sig);
1428 complete_signal(sig, t, group);
1429 out:
1430 unlock_task_sighand(t, &flags);
1431 ret:
1432 return ret;
1436 * Let a parent know about the death of a child.
1437 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1439 * Returns -1 if our parent ignored us and so we've switched to
1440 * self-reaping, or else @sig.
1442 int do_notify_parent(struct task_struct *tsk, int sig)
1444 struct siginfo info;
1445 unsigned long flags;
1446 struct sighand_struct *psig;
1447 int ret = sig;
1449 BUG_ON(sig == -1);
1451 /* do_notify_parent_cldstop should have been called instead. */
1452 BUG_ON(task_is_stopped_or_traced(tsk));
1454 BUG_ON(!task_ptrace(tsk) &&
1455 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1457 info.si_signo = sig;
1458 info.si_errno = 0;
1460 * we are under tasklist_lock here so our parent is tied to
1461 * us and cannot exit and release its namespace.
1463 * the only it can is to switch its nsproxy with sys_unshare,
1464 * bu uncharing pid namespaces is not allowed, so we'll always
1465 * see relevant namespace
1467 * write_lock() currently calls preempt_disable() which is the
1468 * same as rcu_read_lock(), but according to Oleg, this is not
1469 * correct to rely on this
1471 rcu_read_lock();
1472 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1473 info.si_uid = __task_cred(tsk)->uid;
1474 rcu_read_unlock();
1476 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1477 tsk->signal->utime));
1478 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1479 tsk->signal->stime));
1481 info.si_status = tsk->exit_code & 0x7f;
1482 if (tsk->exit_code & 0x80)
1483 info.si_code = CLD_DUMPED;
1484 else if (tsk->exit_code & 0x7f)
1485 info.si_code = CLD_KILLED;
1486 else {
1487 info.si_code = CLD_EXITED;
1488 info.si_status = tsk->exit_code >> 8;
1491 psig = tsk->parent->sighand;
1492 spin_lock_irqsave(&psig->siglock, flags);
1493 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1494 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1495 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1497 * We are exiting and our parent doesn't care. POSIX.1
1498 * defines special semantics for setting SIGCHLD to SIG_IGN
1499 * or setting the SA_NOCLDWAIT flag: we should be reaped
1500 * automatically and not left for our parent's wait4 call.
1501 * Rather than having the parent do it as a magic kind of
1502 * signal handler, we just set this to tell do_exit that we
1503 * can be cleaned up without becoming a zombie. Note that
1504 * we still call __wake_up_parent in this case, because a
1505 * blocked sys_wait4 might now return -ECHILD.
1507 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1508 * is implementation-defined: we do (if you don't want
1509 * it, just use SIG_IGN instead).
1511 ret = tsk->exit_signal = -1;
1512 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1513 sig = -1;
1515 if (valid_signal(sig) && sig > 0)
1516 __group_send_sig_info(sig, &info, tsk->parent);
1517 __wake_up_parent(tsk, tsk->parent);
1518 spin_unlock_irqrestore(&psig->siglock, flags);
1520 return ret;
1523 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1525 struct siginfo info;
1526 unsigned long flags;
1527 struct task_struct *parent;
1528 struct sighand_struct *sighand;
1530 if (task_ptrace(tsk))
1531 parent = tsk->parent;
1532 else {
1533 tsk = tsk->group_leader;
1534 parent = tsk->real_parent;
1537 info.si_signo = SIGCHLD;
1538 info.si_errno = 0;
1540 * see comment in do_notify_parent() abot the following 3 lines
1542 rcu_read_lock();
1543 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1544 info.si_uid = __task_cred(tsk)->uid;
1545 rcu_read_unlock();
1547 info.si_utime = cputime_to_clock_t(tsk->utime);
1548 info.si_stime = cputime_to_clock_t(tsk->stime);
1550 info.si_code = why;
1551 switch (why) {
1552 case CLD_CONTINUED:
1553 info.si_status = SIGCONT;
1554 break;
1555 case CLD_STOPPED:
1556 info.si_status = tsk->signal->group_exit_code & 0x7f;
1557 break;
1558 case CLD_TRAPPED:
1559 info.si_status = tsk->exit_code & 0x7f;
1560 break;
1561 default:
1562 BUG();
1565 sighand = parent->sighand;
1566 spin_lock_irqsave(&sighand->siglock, flags);
1567 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1568 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1569 __group_send_sig_info(SIGCHLD, &info, parent);
1571 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1573 __wake_up_parent(tsk, parent);
1574 spin_unlock_irqrestore(&sighand->siglock, flags);
1577 static inline int may_ptrace_stop(void)
1579 if (!likely(task_ptrace(current)))
1580 return 0;
1582 * Are we in the middle of do_coredump?
1583 * If so and our tracer is also part of the coredump stopping
1584 * is a deadlock situation, and pointless because our tracer
1585 * is dead so don't allow us to stop.
1586 * If SIGKILL was already sent before the caller unlocked
1587 * ->siglock we must see ->core_state != NULL. Otherwise it
1588 * is safe to enter schedule().
1590 if (unlikely(current->mm->core_state) &&
1591 unlikely(current->mm == current->parent->mm))
1592 return 0;
1594 return 1;
1598 * Return nonzero if there is a SIGKILL that should be waking us up.
1599 * Called with the siglock held.
1601 static int sigkill_pending(struct task_struct *tsk)
1603 return sigismember(&tsk->pending.signal, SIGKILL) ||
1604 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1608 * This must be called with current->sighand->siglock held.
1610 * This should be the path for all ptrace stops.
1611 * We always set current->last_siginfo while stopped here.
1612 * That makes it a way to test a stopped process for
1613 * being ptrace-stopped vs being job-control-stopped.
1615 * If we actually decide not to stop at all because the tracer
1616 * is gone, we keep current->exit_code unless clear_code.
1618 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1620 if (arch_ptrace_stop_needed(exit_code, info)) {
1622 * The arch code has something special to do before a
1623 * ptrace stop. This is allowed to block, e.g. for faults
1624 * on user stack pages. We can't keep the siglock while
1625 * calling arch_ptrace_stop, so we must release it now.
1626 * To preserve proper semantics, we must do this before
1627 * any signal bookkeeping like checking group_stop_count.
1628 * Meanwhile, a SIGKILL could come in before we retake the
1629 * siglock. That must prevent us from sleeping in TASK_TRACED.
1630 * So after regaining the lock, we must check for SIGKILL.
1632 spin_unlock_irq(&current->sighand->siglock);
1633 arch_ptrace_stop(exit_code, info);
1634 spin_lock_irq(&current->sighand->siglock);
1635 if (sigkill_pending(current))
1636 return;
1640 * If there is a group stop in progress,
1641 * we must participate in the bookkeeping.
1643 if (current->signal->group_stop_count > 0)
1644 --current->signal->group_stop_count;
1646 current->last_siginfo = info;
1647 current->exit_code = exit_code;
1649 /* Let the debugger run. */
1650 __set_current_state(TASK_TRACED);
1651 spin_unlock_irq(&current->sighand->siglock);
1652 read_lock(&tasklist_lock);
1653 if (may_ptrace_stop()) {
1654 do_notify_parent_cldstop(current, CLD_TRAPPED);
1656 * Don't want to allow preemption here, because
1657 * sys_ptrace() needs this task to be inactive.
1659 * XXX: implement read_unlock_no_resched().
1661 preempt_disable();
1662 read_unlock(&tasklist_lock);
1663 preempt_enable_no_resched();
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);
1718 * This performs the stopping for SIGSTOP and other stop signals.
1719 * We have to stop all threads in the thread group.
1720 * Returns nonzero if we've actually stopped and released the siglock.
1721 * Returns zero if we didn't stop and still hold the siglock.
1723 static int do_signal_stop(int signr)
1725 struct signal_struct *sig = current->signal;
1726 int notify;
1728 if (!sig->group_stop_count) {
1729 struct task_struct *t;
1731 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1732 unlikely(signal_group_exit(sig)))
1733 return 0;
1735 * There is no group stop already in progress.
1736 * We must initiate one now.
1738 sig->group_exit_code = signr;
1740 sig->group_stop_count = 1;
1741 for (t = next_thread(current); t != current; t = next_thread(t))
1743 * Setting state to TASK_STOPPED for a group
1744 * stop is always done with the siglock held,
1745 * so this check has no races.
1747 if (!(t->flags & PF_EXITING) &&
1748 !task_is_stopped_or_traced(t)) {
1749 sig->group_stop_count++;
1750 signal_wake_up(t, 0);
1754 * If there are no other threads in the group, or if there is
1755 * a group stop in progress and we are the last to stop, report
1756 * to the parent. When ptraced, every thread reports itself.
1758 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1759 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1761 * tracehook_notify_jctl() can drop and reacquire siglock, so
1762 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1763 * or SIGKILL comes in between ->group_stop_count == 0.
1765 if (sig->group_stop_count) {
1766 if (!--sig->group_stop_count)
1767 sig->flags = SIGNAL_STOP_STOPPED;
1768 current->exit_code = sig->group_exit_code;
1769 __set_current_state(TASK_STOPPED);
1771 spin_unlock_irq(&current->sighand->siglock);
1773 if (notify) {
1774 read_lock(&tasklist_lock);
1775 do_notify_parent_cldstop(current, notify);
1776 read_unlock(&tasklist_lock);
1779 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1780 do {
1781 schedule();
1782 } while (try_to_freeze());
1784 tracehook_finish_jctl();
1785 current->exit_code = 0;
1787 return 1;
1790 static int ptrace_signal(int signr, siginfo_t *info,
1791 struct pt_regs *regs, void *cookie)
1793 if (!task_ptrace(current))
1794 return signr;
1796 ptrace_signal_deliver(regs, cookie);
1798 /* Let the debugger run. */
1799 ptrace_stop(signr, 0, info);
1801 /* We're back. Did the debugger cancel the sig? */
1802 signr = current->exit_code;
1803 if (signr == 0)
1804 return signr;
1806 current->exit_code = 0;
1808 /* Update the siginfo structure if the signal has
1809 changed. If the debugger wanted something
1810 specific in the siginfo structure then it should
1811 have updated *info via PTRACE_SETSIGINFO. */
1812 if (signr != info->si_signo) {
1813 info->si_signo = signr;
1814 info->si_errno = 0;
1815 info->si_code = SI_USER;
1816 info->si_pid = task_pid_vnr(current->parent);
1817 info->si_uid = task_uid(current->parent);
1820 /* If the (new) signal is now blocked, requeue it. */
1821 if (sigismember(&current->blocked, signr)) {
1822 specific_send_sig_info(signr, info, current);
1823 signr = 0;
1826 return signr;
1829 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1830 struct pt_regs *regs, void *cookie)
1832 struct sighand_struct *sighand = current->sighand;
1833 struct signal_struct *signal = current->signal;
1834 int signr;
1836 relock:
1838 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1839 * While in TASK_STOPPED, we were considered "frozen enough".
1840 * Now that we woke up, it's crucial if we're supposed to be
1841 * frozen that we freeze now before running anything substantial.
1843 try_to_freeze();
1845 spin_lock_irq(&sighand->siglock);
1847 * Every stopped thread goes here after wakeup. Check to see if
1848 * we should notify the parent, prepare_signal(SIGCONT) encodes
1849 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1851 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1852 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1853 ? CLD_CONTINUED : CLD_STOPPED;
1854 signal->flags &= ~SIGNAL_CLD_MASK;
1856 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1857 spin_unlock_irq(&sighand->siglock);
1859 if (why) {
1860 read_lock(&tasklist_lock);
1861 do_notify_parent_cldstop(current->group_leader, why);
1862 read_unlock(&tasklist_lock);
1864 goto relock;
1867 for (;;) {
1868 struct k_sigaction *ka;
1870 * Tracing can induce an artifical signal and choose sigaction.
1871 * The return value in @signr determines the default action,
1872 * but @info->si_signo is the signal number we will report.
1874 signr = tracehook_get_signal(current, regs, info, return_ka);
1875 if (unlikely(signr < 0))
1876 goto relock;
1877 if (unlikely(signr != 0))
1878 ka = return_ka;
1879 else {
1880 if (unlikely(signal->group_stop_count > 0) &&
1881 do_signal_stop(0))
1882 goto relock;
1884 signr = dequeue_signal(current, &current->blocked,
1885 info);
1887 if (!signr)
1888 break; /* will return 0 */
1890 if (signr != SIGKILL) {
1891 signr = ptrace_signal(signr, info,
1892 regs, cookie);
1893 if (!signr)
1894 continue;
1897 ka = &sighand->action[signr-1];
1900 /* Trace actually delivered signals. */
1901 trace_signal_deliver(signr, info, ka);
1903 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1904 continue;
1905 if (ka->sa.sa_handler != SIG_DFL) {
1906 /* Run the handler. */
1907 *return_ka = *ka;
1909 if (ka->sa.sa_flags & SA_ONESHOT)
1910 ka->sa.sa_handler = SIG_DFL;
1912 break; /* will return non-zero "signr" value */
1916 * Now we are doing the default action for this signal.
1918 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1919 continue;
1922 * Global init gets no signals it doesn't want.
1923 * Container-init gets no signals it doesn't want from same
1924 * container.
1926 * Note that if global/container-init sees a sig_kernel_only()
1927 * signal here, the signal must have been generated internally
1928 * or must have come from an ancestor namespace. In either
1929 * case, the signal cannot be dropped.
1931 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1932 !sig_kernel_only(signr))
1933 continue;
1935 if (sig_kernel_stop(signr)) {
1937 * The default action is to stop all threads in
1938 * the thread group. The job control signals
1939 * do nothing in an orphaned pgrp, but SIGSTOP
1940 * always works. Note that siglock needs to be
1941 * dropped during the call to is_orphaned_pgrp()
1942 * because of lock ordering with tasklist_lock.
1943 * This allows an intervening SIGCONT to be posted.
1944 * We need to check for that and bail out if necessary.
1946 if (signr != SIGSTOP) {
1947 spin_unlock_irq(&sighand->siglock);
1949 /* signals can be posted during this window */
1951 if (is_current_pgrp_orphaned())
1952 goto relock;
1954 spin_lock_irq(&sighand->siglock);
1957 if (likely(do_signal_stop(info->si_signo))) {
1958 /* It released the siglock. */
1959 goto relock;
1963 * We didn't actually stop, due to a race
1964 * with SIGCONT or something like that.
1966 continue;
1969 spin_unlock_irq(&sighand->siglock);
1972 * Anything else is fatal, maybe with a core dump.
1974 current->flags |= PF_SIGNALED;
1976 if (sig_kernel_coredump(signr)) {
1977 if (print_fatal_signals)
1978 print_fatal_signal(regs, info->si_signo);
1980 * If it was able to dump core, this kills all
1981 * other threads in the group and synchronizes with
1982 * their demise. If we lost the race with another
1983 * thread getting here, it set group_exit_code
1984 * first and our do_group_exit call below will use
1985 * that value and ignore the one we pass it.
1987 do_coredump(info->si_signo, info->si_signo, regs);
1991 * Death signals, no core dump.
1993 do_group_exit(info->si_signo);
1994 /* NOTREACHED */
1996 spin_unlock_irq(&sighand->siglock);
1997 return signr;
2000 void exit_signals(struct task_struct *tsk)
2002 int group_stop = 0;
2003 struct task_struct *t;
2005 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2006 tsk->flags |= PF_EXITING;
2007 return;
2010 spin_lock_irq(&tsk->sighand->siglock);
2012 * From now this task is not visible for group-wide signals,
2013 * see wants_signal(), do_signal_stop().
2015 tsk->flags |= PF_EXITING;
2016 if (!signal_pending(tsk))
2017 goto out;
2019 /* It could be that __group_complete_signal() choose us to
2020 * notify about group-wide signal. Another thread should be
2021 * woken now to take the signal since we will not.
2023 for (t = tsk; (t = next_thread(t)) != tsk; )
2024 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2025 recalc_sigpending_and_wake(t);
2027 if (unlikely(tsk->signal->group_stop_count) &&
2028 !--tsk->signal->group_stop_count) {
2029 tsk->signal->flags = SIGNAL_STOP_STOPPED;
2030 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
2032 out:
2033 spin_unlock_irq(&tsk->sighand->siglock);
2035 if (unlikely(group_stop)) {
2036 read_lock(&tasklist_lock);
2037 do_notify_parent_cldstop(tsk, group_stop);
2038 read_unlock(&tasklist_lock);
2042 EXPORT_SYMBOL(recalc_sigpending);
2043 EXPORT_SYMBOL_GPL(dequeue_signal);
2044 EXPORT_SYMBOL(flush_signals);
2045 EXPORT_SYMBOL(force_sig);
2046 EXPORT_SYMBOL(send_sig);
2047 EXPORT_SYMBOL(send_sig_info);
2048 EXPORT_SYMBOL(sigprocmask);
2049 EXPORT_SYMBOL(block_all_signals);
2050 EXPORT_SYMBOL(unblock_all_signals);
2054 * System call entry points.
2057 SYSCALL_DEFINE0(restart_syscall)
2059 struct restart_block *restart = &current_thread_info()->restart_block;
2060 return restart->fn(restart);
2063 long do_no_restart_syscall(struct restart_block *param)
2065 return -EINTR;
2069 * We don't need to get the kernel lock - this is all local to this
2070 * particular thread.. (and that's good, because this is _heavily_
2071 * used by various programs)
2075 * This is also useful for kernel threads that want to temporarily
2076 * (or permanently) block certain signals.
2078 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2079 * interface happily blocks "unblockable" signals like SIGKILL
2080 * and friends.
2082 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2084 int error;
2086 spin_lock_irq(&current->sighand->siglock);
2087 if (oldset)
2088 *oldset = current->blocked;
2090 error = 0;
2091 switch (how) {
2092 case SIG_BLOCK:
2093 sigorsets(&current->blocked, &current->blocked, set);
2094 break;
2095 case SIG_UNBLOCK:
2096 signandsets(&current->blocked, &current->blocked, set);
2097 break;
2098 case SIG_SETMASK:
2099 current->blocked = *set;
2100 break;
2101 default:
2102 error = -EINVAL;
2104 recalc_sigpending();
2105 spin_unlock_irq(&current->sighand->siglock);
2107 return error;
2110 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2111 sigset_t __user *, oset, size_t, sigsetsize)
2113 int error = -EINVAL;
2114 sigset_t old_set, new_set;
2116 /* XXX: Don't preclude handling different sized sigset_t's. */
2117 if (sigsetsize != sizeof(sigset_t))
2118 goto out;
2120 if (set) {
2121 error = -EFAULT;
2122 if (copy_from_user(&new_set, set, sizeof(*set)))
2123 goto out;
2124 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2126 error = sigprocmask(how, &new_set, &old_set);
2127 if (error)
2128 goto out;
2129 if (oset)
2130 goto set_old;
2131 } else if (oset) {
2132 spin_lock_irq(&current->sighand->siglock);
2133 old_set = current->blocked;
2134 spin_unlock_irq(&current->sighand->siglock);
2136 set_old:
2137 error = -EFAULT;
2138 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2139 goto out;
2141 error = 0;
2142 out:
2143 return error;
2146 long do_sigpending(void __user *set, unsigned long sigsetsize)
2148 long error = -EINVAL;
2149 sigset_t pending;
2151 if (sigsetsize > sizeof(sigset_t))
2152 goto out;
2154 spin_lock_irq(&current->sighand->siglock);
2155 sigorsets(&pending, &current->pending.signal,
2156 &current->signal->shared_pending.signal);
2157 spin_unlock_irq(&current->sighand->siglock);
2159 /* Outside the lock because only this thread touches it. */
2160 sigandsets(&pending, &current->blocked, &pending);
2162 error = -EFAULT;
2163 if (!copy_to_user(set, &pending, sigsetsize))
2164 error = 0;
2166 out:
2167 return error;
2170 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2172 return do_sigpending(set, sigsetsize);
2175 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2177 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2179 int err;
2181 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2182 return -EFAULT;
2183 if (from->si_code < 0)
2184 return __copy_to_user(to, from, sizeof(siginfo_t))
2185 ? -EFAULT : 0;
2187 * If you change siginfo_t structure, please be sure
2188 * this code is fixed accordingly.
2189 * Please remember to update the signalfd_copyinfo() function
2190 * inside fs/signalfd.c too, in case siginfo_t changes.
2191 * It should never copy any pad contained in the structure
2192 * to avoid security leaks, but must copy the generic
2193 * 3 ints plus the relevant union member.
2195 err = __put_user(from->si_signo, &to->si_signo);
2196 err |= __put_user(from->si_errno, &to->si_errno);
2197 err |= __put_user((short)from->si_code, &to->si_code);
2198 switch (from->si_code & __SI_MASK) {
2199 case __SI_KILL:
2200 err |= __put_user(from->si_pid, &to->si_pid);
2201 err |= __put_user(from->si_uid, &to->si_uid);
2202 break;
2203 case __SI_TIMER:
2204 err |= __put_user(from->si_tid, &to->si_tid);
2205 err |= __put_user(from->si_overrun, &to->si_overrun);
2206 err |= __put_user(from->si_ptr, &to->si_ptr);
2207 break;
2208 case __SI_POLL:
2209 err |= __put_user(from->si_band, &to->si_band);
2210 err |= __put_user(from->si_fd, &to->si_fd);
2211 break;
2212 case __SI_FAULT:
2213 err |= __put_user(from->si_addr, &to->si_addr);
2214 #ifdef __ARCH_SI_TRAPNO
2215 err |= __put_user(from->si_trapno, &to->si_trapno);
2216 #endif
2217 break;
2218 case __SI_CHLD:
2219 err |= __put_user(from->si_pid, &to->si_pid);
2220 err |= __put_user(from->si_uid, &to->si_uid);
2221 err |= __put_user(from->si_status, &to->si_status);
2222 err |= __put_user(from->si_utime, &to->si_utime);
2223 err |= __put_user(from->si_stime, &to->si_stime);
2224 break;
2225 case __SI_RT: /* This is not generated by the kernel as of now. */
2226 case __SI_MESGQ: /* But this is */
2227 err |= __put_user(from->si_pid, &to->si_pid);
2228 err |= __put_user(from->si_uid, &to->si_uid);
2229 err |= __put_user(from->si_ptr, &to->si_ptr);
2230 break;
2231 default: /* this is just in case for now ... */
2232 err |= __put_user(from->si_pid, &to->si_pid);
2233 err |= __put_user(from->si_uid, &to->si_uid);
2234 break;
2236 return err;
2239 #endif
2241 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2242 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2243 size_t, sigsetsize)
2245 int ret, sig;
2246 sigset_t these;
2247 struct timespec ts;
2248 siginfo_t info;
2249 long timeout = 0;
2251 /* XXX: Don't preclude handling different sized sigset_t's. */
2252 if (sigsetsize != sizeof(sigset_t))
2253 return -EINVAL;
2255 if (copy_from_user(&these, uthese, sizeof(these)))
2256 return -EFAULT;
2259 * Invert the set of allowed signals to get those we
2260 * want to block.
2262 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2263 signotset(&these);
2265 if (uts) {
2266 if (copy_from_user(&ts, uts, sizeof(ts)))
2267 return -EFAULT;
2268 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2269 || ts.tv_sec < 0)
2270 return -EINVAL;
2273 spin_lock_irq(&current->sighand->siglock);
2274 sig = dequeue_signal(current, &these, &info);
2275 if (!sig) {
2276 timeout = MAX_SCHEDULE_TIMEOUT;
2277 if (uts)
2278 timeout = (timespec_to_jiffies(&ts)
2279 + (ts.tv_sec || ts.tv_nsec));
2281 if (timeout) {
2282 /* None ready -- temporarily unblock those we're
2283 * interested while we are sleeping in so that we'll
2284 * be awakened when they arrive. */
2285 current->real_blocked = current->blocked;
2286 sigandsets(&current->blocked, &current->blocked, &these);
2287 recalc_sigpending();
2288 spin_unlock_irq(&current->sighand->siglock);
2290 timeout = schedule_timeout_interruptible(timeout);
2292 spin_lock_irq(&current->sighand->siglock);
2293 sig = dequeue_signal(current, &these, &info);
2294 current->blocked = current->real_blocked;
2295 siginitset(&current->real_blocked, 0);
2296 recalc_sigpending();
2299 spin_unlock_irq(&current->sighand->siglock);
2301 if (sig) {
2302 ret = sig;
2303 if (uinfo) {
2304 if (copy_siginfo_to_user(uinfo, &info))
2305 ret = -EFAULT;
2307 } else {
2308 ret = -EAGAIN;
2309 if (timeout)
2310 ret = -EINTR;
2313 return ret;
2316 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2318 struct siginfo info;
2320 info.si_signo = sig;
2321 info.si_errno = 0;
2322 info.si_code = SI_USER;
2323 info.si_pid = task_tgid_vnr(current);
2324 info.si_uid = current_uid();
2326 return kill_something_info(sig, &info, pid);
2329 static int
2330 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2332 struct task_struct *p;
2333 int error = -ESRCH;
2335 rcu_read_lock();
2336 p = find_task_by_vpid(pid);
2337 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2338 error = check_kill_permission(sig, info, p);
2340 * The null signal is a permissions and process existence
2341 * probe. No signal is actually delivered.
2343 if (!error && sig) {
2344 error = do_send_sig_info(sig, info, p, false);
2346 * If lock_task_sighand() failed we pretend the task
2347 * dies after receiving the signal. The window is tiny,
2348 * and the signal is private anyway.
2350 if (unlikely(error == -ESRCH))
2351 error = 0;
2354 rcu_read_unlock();
2356 return error;
2359 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2361 struct siginfo info;
2363 info.si_signo = sig;
2364 info.si_errno = 0;
2365 info.si_code = SI_TKILL;
2366 info.si_pid = task_tgid_vnr(current);
2367 info.si_uid = current_uid();
2369 return do_send_specific(tgid, pid, sig, &info);
2373 * sys_tgkill - send signal to one specific thread
2374 * @tgid: the thread group ID of the thread
2375 * @pid: the PID of the thread
2376 * @sig: signal to be sent
2378 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2379 * exists but it's not belonging to the target process anymore. This
2380 * method solves the problem of threads exiting and PIDs getting reused.
2382 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2384 /* This is only valid for single tasks */
2385 if (pid <= 0 || tgid <= 0)
2386 return -EINVAL;
2388 return do_tkill(tgid, pid, sig);
2392 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2394 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2396 /* This is only valid for single tasks */
2397 if (pid <= 0)
2398 return -EINVAL;
2400 return do_tkill(0, pid, sig);
2403 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2404 siginfo_t __user *, uinfo)
2406 siginfo_t info;
2408 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2409 return -EFAULT;
2411 /* Not even root can pretend to send signals from the kernel.
2412 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2414 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
2415 /* We used to allow any < 0 si_code */
2416 WARN_ON_ONCE(info.si_code < 0);
2417 return -EPERM;
2419 info.si_signo = sig;
2421 /* POSIX.1b doesn't mention process groups. */
2422 return kill_proc_info(sig, &info, pid);
2425 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2427 /* This is only valid for single tasks */
2428 if (pid <= 0 || tgid <= 0)
2429 return -EINVAL;
2431 /* Not even root can pretend to send signals from the kernel.
2432 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2434 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
2435 /* We used to allow any < 0 si_code */
2436 WARN_ON_ONCE(info->si_code < 0);
2437 return -EPERM;
2439 info->si_signo = sig;
2441 return do_send_specific(tgid, pid, sig, info);
2444 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2445 siginfo_t __user *, uinfo)
2447 siginfo_t info;
2449 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2450 return -EFAULT;
2452 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2455 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2457 struct task_struct *t = current;
2458 struct k_sigaction *k;
2459 sigset_t mask;
2461 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2462 return -EINVAL;
2464 k = &t->sighand->action[sig-1];
2466 spin_lock_irq(&current->sighand->siglock);
2467 if (oact)
2468 *oact = *k;
2470 if (act) {
2471 sigdelsetmask(&act->sa.sa_mask,
2472 sigmask(SIGKILL) | sigmask(SIGSTOP));
2473 *k = *act;
2475 * POSIX 3.3.1.3:
2476 * "Setting a signal action to SIG_IGN for a signal that is
2477 * pending shall cause the pending signal to be discarded,
2478 * whether or not it is blocked."
2480 * "Setting a signal action to SIG_DFL for a signal that is
2481 * pending and whose default action is to ignore the signal
2482 * (for example, SIGCHLD), shall cause the pending signal to
2483 * be discarded, whether or not it is blocked"
2485 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2486 sigemptyset(&mask);
2487 sigaddset(&mask, sig);
2488 rm_from_queue_full(&mask, &t->signal->shared_pending);
2489 do {
2490 rm_from_queue_full(&mask, &t->pending);
2491 t = next_thread(t);
2492 } while (t != current);
2496 spin_unlock_irq(&current->sighand->siglock);
2497 return 0;
2500 int
2501 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2503 stack_t oss;
2504 int error;
2506 oss.ss_sp = (void __user *) current->sas_ss_sp;
2507 oss.ss_size = current->sas_ss_size;
2508 oss.ss_flags = sas_ss_flags(sp);
2510 if (uss) {
2511 void __user *ss_sp;
2512 size_t ss_size;
2513 int ss_flags;
2515 error = -EFAULT;
2516 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2517 goto out;
2518 error = __get_user(ss_sp, &uss->ss_sp) |
2519 __get_user(ss_flags, &uss->ss_flags) |
2520 __get_user(ss_size, &uss->ss_size);
2521 if (error)
2522 goto out;
2524 error = -EPERM;
2525 if (on_sig_stack(sp))
2526 goto out;
2528 error = -EINVAL;
2531 * Note - this code used to test ss_flags incorrectly
2532 * old code may have been written using ss_flags==0
2533 * to mean ss_flags==SS_ONSTACK (as this was the only
2534 * way that worked) - this fix preserves that older
2535 * mechanism
2537 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2538 goto out;
2540 if (ss_flags == SS_DISABLE) {
2541 ss_size = 0;
2542 ss_sp = NULL;
2543 } else {
2544 error = -ENOMEM;
2545 if (ss_size < MINSIGSTKSZ)
2546 goto out;
2549 current->sas_ss_sp = (unsigned long) ss_sp;
2550 current->sas_ss_size = ss_size;
2553 error = 0;
2554 if (uoss) {
2555 error = -EFAULT;
2556 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2557 goto out;
2558 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2559 __put_user(oss.ss_size, &uoss->ss_size) |
2560 __put_user(oss.ss_flags, &uoss->ss_flags);
2563 out:
2564 return error;
2567 #ifdef __ARCH_WANT_SYS_SIGPENDING
2569 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2571 return do_sigpending(set, sizeof(*set));
2574 #endif
2576 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2577 /* Some platforms have their own version with special arguments others
2578 support only sys_rt_sigprocmask. */
2580 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2581 old_sigset_t __user *, oset)
2583 int error;
2584 old_sigset_t old_set, new_set;
2586 if (set) {
2587 error = -EFAULT;
2588 if (copy_from_user(&new_set, set, sizeof(*set)))
2589 goto out;
2590 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2592 spin_lock_irq(&current->sighand->siglock);
2593 old_set = current->blocked.sig[0];
2595 error = 0;
2596 switch (how) {
2597 default:
2598 error = -EINVAL;
2599 break;
2600 case SIG_BLOCK:
2601 sigaddsetmask(&current->blocked, new_set);
2602 break;
2603 case SIG_UNBLOCK:
2604 sigdelsetmask(&current->blocked, new_set);
2605 break;
2606 case SIG_SETMASK:
2607 current->blocked.sig[0] = new_set;
2608 break;
2611 recalc_sigpending();
2612 spin_unlock_irq(&current->sighand->siglock);
2613 if (error)
2614 goto out;
2615 if (oset)
2616 goto set_old;
2617 } else if (oset) {
2618 old_set = current->blocked.sig[0];
2619 set_old:
2620 error = -EFAULT;
2621 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2622 goto out;
2624 error = 0;
2625 out:
2626 return error;
2628 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2630 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2631 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2632 const struct sigaction __user *, act,
2633 struct sigaction __user *, oact,
2634 size_t, sigsetsize)
2636 struct k_sigaction new_sa, old_sa;
2637 int ret = -EINVAL;
2639 /* XXX: Don't preclude handling different sized sigset_t's. */
2640 if (sigsetsize != sizeof(sigset_t))
2641 goto out;
2643 if (act) {
2644 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2645 return -EFAULT;
2648 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2650 if (!ret && oact) {
2651 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2652 return -EFAULT;
2654 out:
2655 return ret;
2657 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2659 #ifdef __ARCH_WANT_SYS_SGETMASK
2662 * For backwards compatibility. Functionality superseded by sigprocmask.
2664 SYSCALL_DEFINE0(sgetmask)
2666 /* SMP safe */
2667 return current->blocked.sig[0];
2670 SYSCALL_DEFINE1(ssetmask, int, newmask)
2672 int old;
2674 spin_lock_irq(&current->sighand->siglock);
2675 old = current->blocked.sig[0];
2677 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2678 sigmask(SIGSTOP)));
2679 recalc_sigpending();
2680 spin_unlock_irq(&current->sighand->siglock);
2682 return old;
2684 #endif /* __ARCH_WANT_SGETMASK */
2686 #ifdef __ARCH_WANT_SYS_SIGNAL
2688 * For backwards compatibility. Functionality superseded by sigaction.
2690 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2692 struct k_sigaction new_sa, old_sa;
2693 int ret;
2695 new_sa.sa.sa_handler = handler;
2696 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2697 sigemptyset(&new_sa.sa.sa_mask);
2699 ret = do_sigaction(sig, &new_sa, &old_sa);
2701 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2703 #endif /* __ARCH_WANT_SYS_SIGNAL */
2705 #ifdef __ARCH_WANT_SYS_PAUSE
2707 SYSCALL_DEFINE0(pause)
2709 current->state = TASK_INTERRUPTIBLE;
2710 schedule();
2711 return -ERESTARTNOHAND;
2714 #endif
2716 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2717 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2719 sigset_t newset;
2721 /* XXX: Don't preclude handling different sized sigset_t's. */
2722 if (sigsetsize != sizeof(sigset_t))
2723 return -EINVAL;
2725 if (copy_from_user(&newset, unewset, sizeof(newset)))
2726 return -EFAULT;
2727 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2729 spin_lock_irq(&current->sighand->siglock);
2730 current->saved_sigmask = current->blocked;
2731 current->blocked = newset;
2732 recalc_sigpending();
2733 spin_unlock_irq(&current->sighand->siglock);
2735 current->state = TASK_INTERRUPTIBLE;
2736 schedule();
2737 set_restore_sigmask();
2738 return -ERESTARTNOHAND;
2740 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2742 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2744 return NULL;
2747 void __init signals_init(void)
2749 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);