signals: SEND_SIG_NOINFO should be considered as SI_FROMUSER()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / signal.c
bloba0ba428954b610987463f719d5626b156e0d06d5
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 int next_signal(struct sigpending *pending, sigset_t *mask)
164 unsigned long i, *s, *m, x;
165 int sig = 0;
167 s = pending->signal.sig;
168 m = mask->sig;
169 switch (_NSIG_WORDS) {
170 default:
171 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
172 if ((x = *s &~ *m) != 0) {
173 sig = ffz(~x) + i*_NSIG_BPW + 1;
174 break;
176 break;
178 case 2: if ((x = s[0] &~ m[0]) != 0)
179 sig = 1;
180 else if ((x = s[1] &~ m[1]) != 0)
181 sig = _NSIG_BPW + 1;
182 else
183 break;
184 sig += ffz(~x);
185 break;
187 case 1: if ((x = *s &~ *m) != 0)
188 sig = ffz(~x) + 1;
189 break;
192 return sig;
195 static inline void print_dropped_signal(int sig)
197 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
199 if (!print_fatal_signals)
200 return;
202 if (!__ratelimit(&ratelimit_state))
203 return;
205 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
206 current->comm, current->pid, sig);
210 * allocate a new signal queue record
211 * - this may be called without locks if and only if t == current, otherwise an
212 * appopriate lock must be held to stop the target task from exiting
214 static struct sigqueue *
215 __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
217 struct sigqueue *q = NULL;
218 struct user_struct *user;
221 * We won't get problems with the target's UID changing under us
222 * because changing it requires RCU be used, and if t != current, the
223 * caller must be holding the RCU readlock (by way of a spinlock) and
224 * we use RCU protection here
226 user = get_uid(__task_cred(t)->user);
227 atomic_inc(&user->sigpending);
229 if (override_rlimit ||
230 atomic_read(&user->sigpending) <=
231 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) {
232 q = kmem_cache_alloc(sigqueue_cachep, flags);
233 } else {
234 print_dropped_signal(sig);
237 if (unlikely(q == NULL)) {
238 atomic_dec(&user->sigpending);
239 free_uid(user);
240 } else {
241 INIT_LIST_HEAD(&q->list);
242 q->flags = 0;
243 q->user = user;
246 return q;
249 static void __sigqueue_free(struct sigqueue *q)
251 if (q->flags & SIGQUEUE_PREALLOC)
252 return;
253 atomic_dec(&q->user->sigpending);
254 free_uid(q->user);
255 kmem_cache_free(sigqueue_cachep, q);
258 void flush_sigqueue(struct sigpending *queue)
260 struct sigqueue *q;
262 sigemptyset(&queue->signal);
263 while (!list_empty(&queue->list)) {
264 q = list_entry(queue->list.next, struct sigqueue , list);
265 list_del_init(&q->list);
266 __sigqueue_free(q);
271 * Flush all pending signals for a task.
273 void __flush_signals(struct task_struct *t)
275 clear_tsk_thread_flag(t, TIF_SIGPENDING);
276 flush_sigqueue(&t->pending);
277 flush_sigqueue(&t->signal->shared_pending);
280 void flush_signals(struct task_struct *t)
282 unsigned long flags;
284 spin_lock_irqsave(&t->sighand->siglock, flags);
285 __flush_signals(t);
286 spin_unlock_irqrestore(&t->sighand->siglock, flags);
289 static void __flush_itimer_signals(struct sigpending *pending)
291 sigset_t signal, retain;
292 struct sigqueue *q, *n;
294 signal = pending->signal;
295 sigemptyset(&retain);
297 list_for_each_entry_safe(q, n, &pending->list, list) {
298 int sig = q->info.si_signo;
300 if (likely(q->info.si_code != SI_TIMER)) {
301 sigaddset(&retain, sig);
302 } else {
303 sigdelset(&signal, sig);
304 list_del_init(&q->list);
305 __sigqueue_free(q);
309 sigorsets(&pending->signal, &signal, &retain);
312 void flush_itimer_signals(void)
314 struct task_struct *tsk = current;
315 unsigned long flags;
317 spin_lock_irqsave(&tsk->sighand->siglock, flags);
318 __flush_itimer_signals(&tsk->pending);
319 __flush_itimer_signals(&tsk->signal->shared_pending);
320 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
323 void ignore_signals(struct task_struct *t)
325 int i;
327 for (i = 0; i < _NSIG; ++i)
328 t->sighand->action[i].sa.sa_handler = SIG_IGN;
330 flush_signals(t);
334 * Flush all handlers for a task.
337 void
338 flush_signal_handlers(struct task_struct *t, int force_default)
340 int i;
341 struct k_sigaction *ka = &t->sighand->action[0];
342 for (i = _NSIG ; i != 0 ; i--) {
343 if (force_default || ka->sa.sa_handler != SIG_IGN)
344 ka->sa.sa_handler = SIG_DFL;
345 ka->sa.sa_flags = 0;
346 sigemptyset(&ka->sa.sa_mask);
347 ka++;
351 int unhandled_signal(struct task_struct *tsk, int sig)
353 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
354 if (is_global_init(tsk))
355 return 1;
356 if (handler != SIG_IGN && handler != SIG_DFL)
357 return 0;
358 return !tracehook_consider_fatal_signal(tsk, sig);
362 /* Notify the system that a driver wants to block all signals for this
363 * process, and wants to be notified if any signals at all were to be
364 * sent/acted upon. If the notifier routine returns non-zero, then the
365 * signal will be acted upon after all. If the notifier routine returns 0,
366 * then then signal will be blocked. Only one block per process is
367 * allowed. priv is a pointer to private data that the notifier routine
368 * can use to determine if the signal should be blocked or not. */
370 void
371 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
373 unsigned long flags;
375 spin_lock_irqsave(&current->sighand->siglock, flags);
376 current->notifier_mask = mask;
377 current->notifier_data = priv;
378 current->notifier = notifier;
379 spin_unlock_irqrestore(&current->sighand->siglock, flags);
382 /* Notify the system that blocking has ended. */
384 void
385 unblock_all_signals(void)
387 unsigned long flags;
389 spin_lock_irqsave(&current->sighand->siglock, flags);
390 current->notifier = NULL;
391 current->notifier_data = NULL;
392 recalc_sigpending();
393 spin_unlock_irqrestore(&current->sighand->siglock, flags);
396 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
398 struct sigqueue *q, *first = NULL;
401 * Collect the siginfo appropriate to this signal. Check if
402 * there is another siginfo for the same signal.
404 list_for_each_entry(q, &list->list, list) {
405 if (q->info.si_signo == sig) {
406 if (first)
407 goto still_pending;
408 first = q;
412 sigdelset(&list->signal, sig);
414 if (first) {
415 still_pending:
416 list_del_init(&first->list);
417 copy_siginfo(info, &first->info);
418 __sigqueue_free(first);
419 } else {
420 /* Ok, it wasn't in the queue. This must be
421 a fast-pathed signal or we must have been
422 out of queue space. So zero out the info.
424 info->si_signo = sig;
425 info->si_errno = 0;
426 info->si_code = 0;
427 info->si_pid = 0;
428 info->si_uid = 0;
432 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
433 siginfo_t *info)
435 int sig = next_signal(pending, mask);
437 if (sig) {
438 if (current->notifier) {
439 if (sigismember(current->notifier_mask, sig)) {
440 if (!(current->notifier)(current->notifier_data)) {
441 clear_thread_flag(TIF_SIGPENDING);
442 return 0;
447 collect_signal(sig, pending, info);
450 return sig;
454 * Dequeue a signal and return the element to the caller, which is
455 * expected to free it.
457 * All callers have to hold the siglock.
459 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
461 int signr;
463 /* We only dequeue private signals from ourselves, we don't let
464 * signalfd steal them
466 signr = __dequeue_signal(&tsk->pending, mask, info);
467 if (!signr) {
468 signr = __dequeue_signal(&tsk->signal->shared_pending,
469 mask, info);
471 * itimer signal ?
473 * itimers are process shared and we restart periodic
474 * itimers in the signal delivery path to prevent DoS
475 * attacks in the high resolution timer case. This is
476 * compliant with the old way of self restarting
477 * itimers, as the SIGALRM is a legacy signal and only
478 * queued once. Changing the restart behaviour to
479 * restart the timer in the signal dequeue path is
480 * reducing the timer noise on heavy loaded !highres
481 * systems too.
483 if (unlikely(signr == SIGALRM)) {
484 struct hrtimer *tmr = &tsk->signal->real_timer;
486 if (!hrtimer_is_queued(tmr) &&
487 tsk->signal->it_real_incr.tv64 != 0) {
488 hrtimer_forward(tmr, tmr->base->get_time(),
489 tsk->signal->it_real_incr);
490 hrtimer_restart(tmr);
495 recalc_sigpending();
496 if (!signr)
497 return 0;
499 if (unlikely(sig_kernel_stop(signr))) {
501 * Set a marker that we have dequeued a stop signal. Our
502 * caller might release the siglock and then the pending
503 * stop signal it is about to process is no longer in the
504 * pending bitmasks, but must still be cleared by a SIGCONT
505 * (and overruled by a SIGKILL). So those cases clear this
506 * shared flag after we've set it. Note that this flag may
507 * remain set after the signal we return is ignored or
508 * handled. That doesn't matter because its only purpose
509 * is to alert stop-signal processing code when another
510 * processor has come along and cleared the flag.
512 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
514 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
516 * Release the siglock to ensure proper locking order
517 * of timer locks outside of siglocks. Note, we leave
518 * irqs disabled here, since the posix-timers code is
519 * about to disable them again anyway.
521 spin_unlock(&tsk->sighand->siglock);
522 do_schedule_next_timer(info);
523 spin_lock(&tsk->sighand->siglock);
525 return signr;
529 * Tell a process that it has a new active signal..
531 * NOTE! we rely on the previous spin_lock to
532 * lock interrupts for us! We can only be called with
533 * "siglock" held, and the local interrupt must
534 * have been disabled when that got acquired!
536 * No need to set need_resched since signal event passing
537 * goes through ->blocked
539 void signal_wake_up(struct task_struct *t, int resume)
541 unsigned int mask;
543 set_tsk_thread_flag(t, TIF_SIGPENDING);
546 * For SIGKILL, we want to wake it up in the stopped/traced/killable
547 * case. We don't check t->state here because there is a race with it
548 * executing another processor and just now entering stopped state.
549 * By using wake_up_state, we ensure the process will wake up and
550 * handle its death signal.
552 mask = TASK_INTERRUPTIBLE;
553 if (resume)
554 mask |= TASK_WAKEKILL;
555 if (!wake_up_state(t, mask))
556 kick_process(t);
560 * Remove signals in mask from the pending set and queue.
561 * Returns 1 if any signals were found.
563 * All callers must be holding the siglock.
565 * This version takes a sigset mask and looks at all signals,
566 * not just those in the first mask word.
568 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
570 struct sigqueue *q, *n;
571 sigset_t m;
573 sigandsets(&m, mask, &s->signal);
574 if (sigisemptyset(&m))
575 return 0;
577 signandsets(&s->signal, &s->signal, mask);
578 list_for_each_entry_safe(q, n, &s->list, list) {
579 if (sigismember(mask, q->info.si_signo)) {
580 list_del_init(&q->list);
581 __sigqueue_free(q);
584 return 1;
587 * Remove signals in mask from the pending set and queue.
588 * Returns 1 if any signals were found.
590 * All callers must be holding the siglock.
592 static int rm_from_queue(unsigned long mask, struct sigpending *s)
594 struct sigqueue *q, *n;
596 if (!sigtestsetmask(&s->signal, mask))
597 return 0;
599 sigdelsetmask(&s->signal, mask);
600 list_for_each_entry_safe(q, n, &s->list, list) {
601 if (q->info.si_signo < SIGRTMIN &&
602 (mask & sigmask(q->info.si_signo))) {
603 list_del_init(&q->list);
604 __sigqueue_free(q);
607 return 1;
610 static inline int is_si_special(const struct siginfo *info)
612 return info <= SEND_SIG_FORCED;
615 static inline bool si_fromuser(const struct siginfo *info)
617 return info == SEND_SIG_NOINFO ||
618 (!is_si_special(info) && SI_FROMUSER(info));
622 * Bad permissions for sending the signal
623 * - the caller must hold at least the RCU read lock
625 static int check_kill_permission(int sig, struct siginfo *info,
626 struct task_struct *t)
628 const struct cred *cred = current_cred(), *tcred;
629 struct pid *sid;
630 int error;
632 if (!valid_signal(sig))
633 return -EINVAL;
635 if (!si_fromuser(info))
636 return 0;
638 error = audit_signal_info(sig, t); /* Let audit system see the signal */
639 if (error)
640 return error;
642 tcred = __task_cred(t);
643 if ((cred->euid ^ tcred->suid) &&
644 (cred->euid ^ tcred->uid) &&
645 (cred->uid ^ tcred->suid) &&
646 (cred->uid ^ tcred->uid) &&
647 !capable(CAP_KILL)) {
648 switch (sig) {
649 case SIGCONT:
650 sid = task_session(t);
652 * We don't return the error if sid == NULL. The
653 * task was unhashed, the caller must notice this.
655 if (!sid || sid == task_session(current))
656 break;
657 default:
658 return -EPERM;
662 return security_task_kill(t, info, sig, 0);
666 * Handle magic process-wide effects of stop/continue signals. Unlike
667 * the signal actions, these happen immediately at signal-generation
668 * time regardless of blocking, ignoring, or handling. This does the
669 * actual continuing for SIGCONT, but not the actual stopping for stop
670 * signals. The process stop is done as a signal action for SIG_DFL.
672 * Returns true if the signal should be actually delivered, otherwise
673 * it should be dropped.
675 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
677 struct signal_struct *signal = p->signal;
678 struct task_struct *t;
680 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
682 * The process is in the middle of dying, nothing to do.
684 } else if (sig_kernel_stop(sig)) {
686 * This is a stop signal. Remove SIGCONT from all queues.
688 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
689 t = p;
690 do {
691 rm_from_queue(sigmask(SIGCONT), &t->pending);
692 } while_each_thread(p, t);
693 } else if (sig == SIGCONT) {
694 unsigned int why;
696 * Remove all stop signals from all queues,
697 * and wake all threads.
699 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
700 t = p;
701 do {
702 unsigned int state;
703 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
705 * If there is a handler for SIGCONT, we must make
706 * sure that no thread returns to user mode before
707 * we post the signal, in case it was the only
708 * thread eligible to run the signal handler--then
709 * it must not do anything between resuming and
710 * running the handler. With the TIF_SIGPENDING
711 * flag set, the thread will pause and acquire the
712 * siglock that we hold now and until we've queued
713 * the pending signal.
715 * Wake up the stopped thread _after_ setting
716 * TIF_SIGPENDING
718 state = __TASK_STOPPED;
719 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
720 set_tsk_thread_flag(t, TIF_SIGPENDING);
721 state |= TASK_INTERRUPTIBLE;
723 wake_up_state(t, state);
724 } while_each_thread(p, t);
727 * Notify the parent with CLD_CONTINUED if we were stopped.
729 * If we were in the middle of a group stop, we pretend it
730 * was already finished, and then continued. Since SIGCHLD
731 * doesn't queue we report only CLD_STOPPED, as if the next
732 * CLD_CONTINUED was dropped.
734 why = 0;
735 if (signal->flags & SIGNAL_STOP_STOPPED)
736 why |= SIGNAL_CLD_CONTINUED;
737 else if (signal->group_stop_count)
738 why |= SIGNAL_CLD_STOPPED;
740 if (why) {
742 * The first thread which returns from do_signal_stop()
743 * will take ->siglock, notice SIGNAL_CLD_MASK, and
744 * notify its parent. See get_signal_to_deliver().
746 signal->flags = why | SIGNAL_STOP_CONTINUED;
747 signal->group_stop_count = 0;
748 signal->group_exit_code = 0;
749 } else {
751 * We are not stopped, but there could be a stop
752 * signal in the middle of being processed after
753 * being removed from the queue. Clear that too.
755 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
759 return !sig_ignored(p, sig, from_ancestor_ns);
763 * Test if P wants to take SIG. After we've checked all threads with this,
764 * it's equivalent to finding no threads not blocking SIG. Any threads not
765 * blocking SIG were ruled out because they are not running and already
766 * have pending signals. Such threads will dequeue from the shared queue
767 * as soon as they're available, so putting the signal on the shared queue
768 * will be equivalent to sending it to one such thread.
770 static inline int wants_signal(int sig, struct task_struct *p)
772 if (sigismember(&p->blocked, sig))
773 return 0;
774 if (p->flags & PF_EXITING)
775 return 0;
776 if (sig == SIGKILL)
777 return 1;
778 if (task_is_stopped_or_traced(p))
779 return 0;
780 return task_curr(p) || !signal_pending(p);
783 static void complete_signal(int sig, struct task_struct *p, int group)
785 struct signal_struct *signal = p->signal;
786 struct task_struct *t;
789 * Now find a thread we can wake up to take the signal off the queue.
791 * If the main thread wants the signal, it gets first crack.
792 * Probably the least surprising to the average bear.
794 if (wants_signal(sig, p))
795 t = p;
796 else if (!group || thread_group_empty(p))
798 * There is just one thread and it does not need to be woken.
799 * It will dequeue unblocked signals before it runs again.
801 return;
802 else {
804 * Otherwise try to find a suitable thread.
806 t = signal->curr_target;
807 while (!wants_signal(sig, t)) {
808 t = next_thread(t);
809 if (t == signal->curr_target)
811 * No thread needs to be woken.
812 * Any eligible threads will see
813 * the signal in the queue soon.
815 return;
817 signal->curr_target = t;
821 * Found a killable thread. If the signal will be fatal,
822 * then start taking the whole group down immediately.
824 if (sig_fatal(p, sig) &&
825 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
826 !sigismember(&t->real_blocked, sig) &&
827 (sig == SIGKILL ||
828 !tracehook_consider_fatal_signal(t, sig))) {
830 * This signal will be fatal to the whole group.
832 if (!sig_kernel_coredump(sig)) {
834 * Start a group exit and wake everybody up.
835 * This way we don't have other threads
836 * running and doing things after a slower
837 * thread has the fatal signal pending.
839 signal->flags = SIGNAL_GROUP_EXIT;
840 signal->group_exit_code = sig;
841 signal->group_stop_count = 0;
842 t = p;
843 do {
844 sigaddset(&t->pending.signal, SIGKILL);
845 signal_wake_up(t, 1);
846 } while_each_thread(p, t);
847 return;
852 * The signal is already in the shared-pending queue.
853 * Tell the chosen thread to wake up and dequeue it.
855 signal_wake_up(t, sig == SIGKILL);
856 return;
859 static inline int legacy_queue(struct sigpending *signals, int sig)
861 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
864 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
865 int group, int from_ancestor_ns)
867 struct sigpending *pending;
868 struct sigqueue *q;
869 int override_rlimit;
871 trace_signal_generate(sig, info, t);
873 assert_spin_locked(&t->sighand->siglock);
875 if (!prepare_signal(sig, t, from_ancestor_ns))
876 return 0;
878 pending = group ? &t->signal->shared_pending : &t->pending;
880 * Short-circuit ignored signals and support queuing
881 * exactly one non-rt signal, so that we can get more
882 * detailed information about the cause of the signal.
884 if (legacy_queue(pending, sig))
885 return 0;
887 * fast-pathed signals for kernel-internal things like SIGSTOP
888 * or SIGKILL.
890 if (info == SEND_SIG_FORCED)
891 goto out_set;
893 /* Real-time signals must be queued if sent by sigqueue, or
894 some other real-time mechanism. It is implementation
895 defined whether kill() does so. We attempt to do so, on
896 the principle of least surprise, but since kill is not
897 allowed to fail with EAGAIN when low on memory we just
898 make sure at least one signal gets delivered and don't
899 pass on the info struct. */
901 if (sig < SIGRTMIN)
902 override_rlimit = (is_si_special(info) || info->si_code >= 0);
903 else
904 override_rlimit = 0;
906 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
907 override_rlimit);
908 if (q) {
909 list_add_tail(&q->list, &pending->list);
910 switch ((unsigned long) info) {
911 case (unsigned long) SEND_SIG_NOINFO:
912 q->info.si_signo = sig;
913 q->info.si_errno = 0;
914 q->info.si_code = SI_USER;
915 q->info.si_pid = task_tgid_nr_ns(current,
916 task_active_pid_ns(t));
917 q->info.si_uid = current_uid();
918 break;
919 case (unsigned long) SEND_SIG_PRIV:
920 q->info.si_signo = sig;
921 q->info.si_errno = 0;
922 q->info.si_code = SI_KERNEL;
923 q->info.si_pid = 0;
924 q->info.si_uid = 0;
925 break;
926 default:
927 copy_siginfo(&q->info, info);
928 if (from_ancestor_ns)
929 q->info.si_pid = 0;
930 break;
932 } else if (!is_si_special(info)) {
933 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
935 * Queue overflow, abort. We may abort if the
936 * signal was rt and sent by user using something
937 * other than kill().
939 trace_signal_overflow_fail(sig, group, info);
940 return -EAGAIN;
941 } else {
943 * This is a silent loss of information. We still
944 * send the signal, but the *info bits are lost.
946 trace_signal_lose_info(sig, group, info);
950 out_set:
951 signalfd_notify(t, sig);
952 sigaddset(&pending->signal, sig);
953 complete_signal(sig, t, group);
954 return 0;
957 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
958 int group)
960 int from_ancestor_ns = 0;
962 #ifdef CONFIG_PID_NS
963 if (!is_si_special(info) && SI_FROMUSER(info) &&
964 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
965 from_ancestor_ns = 1;
966 #endif
968 return __send_signal(sig, info, t, group, from_ancestor_ns);
971 static void print_fatal_signal(struct pt_regs *regs, int signr)
973 printk("%s/%d: potentially unexpected fatal signal %d.\n",
974 current->comm, task_pid_nr(current), signr);
976 #if defined(__i386__) && !defined(__arch_um__)
977 printk("code at %08lx: ", regs->ip);
979 int i;
980 for (i = 0; i < 16; i++) {
981 unsigned char insn;
983 __get_user(insn, (unsigned char *)(regs->ip + i));
984 printk("%02x ", insn);
987 #endif
988 printk("\n");
989 preempt_disable();
990 show_regs(regs);
991 preempt_enable();
994 static int __init setup_print_fatal_signals(char *str)
996 get_option (&str, &print_fatal_signals);
998 return 1;
1001 __setup("print-fatal-signals=", setup_print_fatal_signals);
1004 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1006 return send_signal(sig, info, p, 1);
1009 static int
1010 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1012 return send_signal(sig, info, t, 0);
1015 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1016 bool group)
1018 unsigned long flags;
1019 int ret = -ESRCH;
1021 if (lock_task_sighand(p, &flags)) {
1022 ret = send_signal(sig, info, p, group);
1023 unlock_task_sighand(p, &flags);
1026 return ret;
1030 * Force a signal that the process can't ignore: if necessary
1031 * we unblock the signal and change any SIG_IGN to SIG_DFL.
1033 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1034 * since we do not want to have a signal handler that was blocked
1035 * be invoked when user space had explicitly blocked it.
1037 * We don't want to have recursive SIGSEGV's etc, for example,
1038 * that is why we also clear SIGNAL_UNKILLABLE.
1041 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1043 unsigned long int flags;
1044 int ret, blocked, ignored;
1045 struct k_sigaction *action;
1047 spin_lock_irqsave(&t->sighand->siglock, flags);
1048 action = &t->sighand->action[sig-1];
1049 ignored = action->sa.sa_handler == SIG_IGN;
1050 blocked = sigismember(&t->blocked, sig);
1051 if (blocked || ignored) {
1052 action->sa.sa_handler = SIG_DFL;
1053 if (blocked) {
1054 sigdelset(&t->blocked, sig);
1055 recalc_sigpending_and_wake(t);
1058 if (action->sa.sa_handler == SIG_DFL)
1059 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1060 ret = specific_send_sig_info(sig, info, t);
1061 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1063 return ret;
1066 void
1067 force_sig_specific(int sig, struct task_struct *t)
1069 force_sig_info(sig, SEND_SIG_FORCED, t);
1073 * Nuke all other threads in the group.
1075 void zap_other_threads(struct task_struct *p)
1077 struct task_struct *t;
1079 p->signal->group_stop_count = 0;
1081 for (t = next_thread(p); t != p; t = next_thread(t)) {
1083 * Don't bother with already dead threads
1085 if (t->exit_state)
1086 continue;
1088 /* SIGKILL will be handled before any pending SIGSTOP */
1089 sigaddset(&t->pending.signal, SIGKILL);
1090 signal_wake_up(t, 1);
1094 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1096 struct sighand_struct *sighand;
1098 rcu_read_lock();
1099 for (;;) {
1100 sighand = rcu_dereference(tsk->sighand);
1101 if (unlikely(sighand == NULL))
1102 break;
1104 spin_lock_irqsave(&sighand->siglock, *flags);
1105 if (likely(sighand == tsk->sighand))
1106 break;
1107 spin_unlock_irqrestore(&sighand->siglock, *flags);
1109 rcu_read_unlock();
1111 return sighand;
1115 * send signal info to all the members of a group
1116 * - the caller must hold the RCU read lock at least
1118 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1120 int ret = check_kill_permission(sig, info, p);
1122 if (!ret && sig)
1123 ret = do_send_sig_info(sig, info, p, true);
1125 return ret;
1129 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1130 * control characters do (^C, ^Z etc)
1131 * - the caller must hold at least a readlock on tasklist_lock
1133 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1135 struct task_struct *p = NULL;
1136 int retval, success;
1138 success = 0;
1139 retval = -ESRCH;
1140 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1141 int err = group_send_sig_info(sig, info, p);
1142 success |= !err;
1143 retval = err;
1144 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1145 return success ? 0 : retval;
1148 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1150 int error = -ESRCH;
1151 struct task_struct *p;
1153 rcu_read_lock();
1154 retry:
1155 p = pid_task(pid, PIDTYPE_PID);
1156 if (p) {
1157 error = group_send_sig_info(sig, info, p);
1158 if (unlikely(error == -ESRCH))
1160 * The task was unhashed in between, try again.
1161 * If it is dead, pid_task() will return NULL,
1162 * if we race with de_thread() it will find the
1163 * new leader.
1165 goto retry;
1167 rcu_read_unlock();
1169 return error;
1173 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1175 int error;
1176 rcu_read_lock();
1177 error = kill_pid_info(sig, info, find_vpid(pid));
1178 rcu_read_unlock();
1179 return error;
1182 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1183 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1184 uid_t uid, uid_t euid, u32 secid)
1186 int ret = -EINVAL;
1187 struct task_struct *p;
1188 const struct cred *pcred;
1190 if (!valid_signal(sig))
1191 return ret;
1193 read_lock(&tasklist_lock);
1194 p = pid_task(pid, PIDTYPE_PID);
1195 if (!p) {
1196 ret = -ESRCH;
1197 goto out_unlock;
1199 pcred = __task_cred(p);
1200 if (si_fromuser(info) &&
1201 euid != pcred->suid && euid != pcred->uid &&
1202 uid != pcred->suid && uid != pcred->uid) {
1203 ret = -EPERM;
1204 goto out_unlock;
1206 ret = security_task_kill(p, info, sig, secid);
1207 if (ret)
1208 goto out_unlock;
1209 if (sig && p->sighand) {
1210 unsigned long flags;
1211 spin_lock_irqsave(&p->sighand->siglock, flags);
1212 ret = __send_signal(sig, info, p, 1, 0);
1213 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1215 out_unlock:
1216 read_unlock(&tasklist_lock);
1217 return ret;
1219 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1222 * kill_something_info() interprets pid in interesting ways just like kill(2).
1224 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1225 * is probably wrong. Should make it like BSD or SYSV.
1228 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1230 int ret;
1232 if (pid > 0) {
1233 rcu_read_lock();
1234 ret = kill_pid_info(sig, info, find_vpid(pid));
1235 rcu_read_unlock();
1236 return ret;
1239 read_lock(&tasklist_lock);
1240 if (pid != -1) {
1241 ret = __kill_pgrp_info(sig, info,
1242 pid ? find_vpid(-pid) : task_pgrp(current));
1243 } else {
1244 int retval = 0, count = 0;
1245 struct task_struct * p;
1247 for_each_process(p) {
1248 if (task_pid_vnr(p) > 1 &&
1249 !same_thread_group(p, current)) {
1250 int err = group_send_sig_info(sig, info, p);
1251 ++count;
1252 if (err != -EPERM)
1253 retval = err;
1256 ret = count ? retval : -ESRCH;
1258 read_unlock(&tasklist_lock);
1260 return ret;
1264 * These are for backward compatibility with the rest of the kernel source.
1268 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1271 * Make sure legacy kernel users don't send in bad values
1272 * (normal paths check this in check_kill_permission).
1274 if (!valid_signal(sig))
1275 return -EINVAL;
1277 return do_send_sig_info(sig, info, p, false);
1280 #define __si_special(priv) \
1281 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1284 send_sig(int sig, struct task_struct *p, int priv)
1286 return send_sig_info(sig, __si_special(priv), p);
1289 void
1290 force_sig(int sig, struct task_struct *p)
1292 force_sig_info(sig, SEND_SIG_PRIV, p);
1296 * When things go south during signal handling, we
1297 * will force a SIGSEGV. And if the signal that caused
1298 * the problem was already a SIGSEGV, we'll want to
1299 * make sure we don't even try to deliver the signal..
1302 force_sigsegv(int sig, struct task_struct *p)
1304 if (sig == SIGSEGV) {
1305 unsigned long flags;
1306 spin_lock_irqsave(&p->sighand->siglock, flags);
1307 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1308 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1310 force_sig(SIGSEGV, p);
1311 return 0;
1314 int kill_pgrp(struct pid *pid, int sig, int priv)
1316 int ret;
1318 read_lock(&tasklist_lock);
1319 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1320 read_unlock(&tasklist_lock);
1322 return ret;
1324 EXPORT_SYMBOL(kill_pgrp);
1326 int kill_pid(struct pid *pid, int sig, int priv)
1328 return kill_pid_info(sig, __si_special(priv), pid);
1330 EXPORT_SYMBOL(kill_pid);
1333 * These functions support sending signals using preallocated sigqueue
1334 * structures. This is needed "because realtime applications cannot
1335 * afford to lose notifications of asynchronous events, like timer
1336 * expirations or I/O completions". In the case of Posix Timers
1337 * we allocate the sigqueue structure from the timer_create. If this
1338 * allocation fails we are able to report the failure to the application
1339 * with an EAGAIN error.
1341 struct sigqueue *sigqueue_alloc(void)
1343 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1345 if (q)
1346 q->flags |= SIGQUEUE_PREALLOC;
1348 return q;
1351 void sigqueue_free(struct sigqueue *q)
1353 unsigned long flags;
1354 spinlock_t *lock = &current->sighand->siglock;
1356 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1358 * We must hold ->siglock while testing q->list
1359 * to serialize with collect_signal() or with
1360 * __exit_signal()->flush_sigqueue().
1362 spin_lock_irqsave(lock, flags);
1363 q->flags &= ~SIGQUEUE_PREALLOC;
1365 * If it is queued it will be freed when dequeued,
1366 * like the "regular" sigqueue.
1368 if (!list_empty(&q->list))
1369 q = NULL;
1370 spin_unlock_irqrestore(lock, flags);
1372 if (q)
1373 __sigqueue_free(q);
1376 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1378 int sig = q->info.si_signo;
1379 struct sigpending *pending;
1380 unsigned long flags;
1381 int ret;
1383 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1385 ret = -1;
1386 if (!likely(lock_task_sighand(t, &flags)))
1387 goto ret;
1389 ret = 1; /* the signal is ignored */
1390 if (!prepare_signal(sig, t, 0))
1391 goto out;
1393 ret = 0;
1394 if (unlikely(!list_empty(&q->list))) {
1396 * If an SI_TIMER entry is already queue just increment
1397 * the overrun count.
1399 BUG_ON(q->info.si_code != SI_TIMER);
1400 q->info.si_overrun++;
1401 goto out;
1403 q->info.si_overrun = 0;
1405 signalfd_notify(t, sig);
1406 pending = group ? &t->signal->shared_pending : &t->pending;
1407 list_add_tail(&q->list, &pending->list);
1408 sigaddset(&pending->signal, sig);
1409 complete_signal(sig, t, group);
1410 out:
1411 unlock_task_sighand(t, &flags);
1412 ret:
1413 return ret;
1417 * Let a parent know about the death of a child.
1418 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1420 * Returns -1 if our parent ignored us and so we've switched to
1421 * self-reaping, or else @sig.
1423 int do_notify_parent(struct task_struct *tsk, int sig)
1425 struct siginfo info;
1426 unsigned long flags;
1427 struct sighand_struct *psig;
1428 int ret = sig;
1430 BUG_ON(sig == -1);
1432 /* do_notify_parent_cldstop should have been called instead. */
1433 BUG_ON(task_is_stopped_or_traced(tsk));
1435 BUG_ON(!task_ptrace(tsk) &&
1436 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1438 info.si_signo = sig;
1439 info.si_errno = 0;
1441 * we are under tasklist_lock here so our parent is tied to
1442 * us and cannot exit and release its namespace.
1444 * the only it can is to switch its nsproxy with sys_unshare,
1445 * bu uncharing pid namespaces is not allowed, so we'll always
1446 * see relevant namespace
1448 * write_lock() currently calls preempt_disable() which is the
1449 * same as rcu_read_lock(), but according to Oleg, this is not
1450 * correct to rely on this
1452 rcu_read_lock();
1453 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1454 info.si_uid = __task_cred(tsk)->uid;
1455 rcu_read_unlock();
1457 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1458 tsk->signal->utime));
1459 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1460 tsk->signal->stime));
1462 info.si_status = tsk->exit_code & 0x7f;
1463 if (tsk->exit_code & 0x80)
1464 info.si_code = CLD_DUMPED;
1465 else if (tsk->exit_code & 0x7f)
1466 info.si_code = CLD_KILLED;
1467 else {
1468 info.si_code = CLD_EXITED;
1469 info.si_status = tsk->exit_code >> 8;
1472 psig = tsk->parent->sighand;
1473 spin_lock_irqsave(&psig->siglock, flags);
1474 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1475 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1476 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1478 * We are exiting and our parent doesn't care. POSIX.1
1479 * defines special semantics for setting SIGCHLD to SIG_IGN
1480 * or setting the SA_NOCLDWAIT flag: we should be reaped
1481 * automatically and not left for our parent's wait4 call.
1482 * Rather than having the parent do it as a magic kind of
1483 * signal handler, we just set this to tell do_exit that we
1484 * can be cleaned up without becoming a zombie. Note that
1485 * we still call __wake_up_parent in this case, because a
1486 * blocked sys_wait4 might now return -ECHILD.
1488 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1489 * is implementation-defined: we do (if you don't want
1490 * it, just use SIG_IGN instead).
1492 ret = tsk->exit_signal = -1;
1493 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1494 sig = -1;
1496 if (valid_signal(sig) && sig > 0)
1497 __group_send_sig_info(sig, &info, tsk->parent);
1498 __wake_up_parent(tsk, tsk->parent);
1499 spin_unlock_irqrestore(&psig->siglock, flags);
1501 return ret;
1504 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1506 struct siginfo info;
1507 unsigned long flags;
1508 struct task_struct *parent;
1509 struct sighand_struct *sighand;
1511 if (task_ptrace(tsk))
1512 parent = tsk->parent;
1513 else {
1514 tsk = tsk->group_leader;
1515 parent = tsk->real_parent;
1518 info.si_signo = SIGCHLD;
1519 info.si_errno = 0;
1521 * see comment in do_notify_parent() abot the following 3 lines
1523 rcu_read_lock();
1524 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1525 info.si_uid = __task_cred(tsk)->uid;
1526 rcu_read_unlock();
1528 info.si_utime = cputime_to_clock_t(tsk->utime);
1529 info.si_stime = cputime_to_clock_t(tsk->stime);
1531 info.si_code = why;
1532 switch (why) {
1533 case CLD_CONTINUED:
1534 info.si_status = SIGCONT;
1535 break;
1536 case CLD_STOPPED:
1537 info.si_status = tsk->signal->group_exit_code & 0x7f;
1538 break;
1539 case CLD_TRAPPED:
1540 info.si_status = tsk->exit_code & 0x7f;
1541 break;
1542 default:
1543 BUG();
1546 sighand = parent->sighand;
1547 spin_lock_irqsave(&sighand->siglock, flags);
1548 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1549 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1550 __group_send_sig_info(SIGCHLD, &info, parent);
1552 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1554 __wake_up_parent(tsk, parent);
1555 spin_unlock_irqrestore(&sighand->siglock, flags);
1558 static inline int may_ptrace_stop(void)
1560 if (!likely(task_ptrace(current)))
1561 return 0;
1563 * Are we in the middle of do_coredump?
1564 * If so and our tracer is also part of the coredump stopping
1565 * is a deadlock situation, and pointless because our tracer
1566 * is dead so don't allow us to stop.
1567 * If SIGKILL was already sent before the caller unlocked
1568 * ->siglock we must see ->core_state != NULL. Otherwise it
1569 * is safe to enter schedule().
1571 if (unlikely(current->mm->core_state) &&
1572 unlikely(current->mm == current->parent->mm))
1573 return 0;
1575 return 1;
1579 * Return nonzero if there is a SIGKILL that should be waking us up.
1580 * Called with the siglock held.
1582 static int sigkill_pending(struct task_struct *tsk)
1584 return sigismember(&tsk->pending.signal, SIGKILL) ||
1585 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1589 * This must be called with current->sighand->siglock held.
1591 * This should be the path for all ptrace stops.
1592 * We always set current->last_siginfo while stopped here.
1593 * That makes it a way to test a stopped process for
1594 * being ptrace-stopped vs being job-control-stopped.
1596 * If we actually decide not to stop at all because the tracer
1597 * is gone, we keep current->exit_code unless clear_code.
1599 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1601 if (arch_ptrace_stop_needed(exit_code, info)) {
1603 * The arch code has something special to do before a
1604 * ptrace stop. This is allowed to block, e.g. for faults
1605 * on user stack pages. We can't keep the siglock while
1606 * calling arch_ptrace_stop, so we must release it now.
1607 * To preserve proper semantics, we must do this before
1608 * any signal bookkeeping like checking group_stop_count.
1609 * Meanwhile, a SIGKILL could come in before we retake the
1610 * siglock. That must prevent us from sleeping in TASK_TRACED.
1611 * So after regaining the lock, we must check for SIGKILL.
1613 spin_unlock_irq(&current->sighand->siglock);
1614 arch_ptrace_stop(exit_code, info);
1615 spin_lock_irq(&current->sighand->siglock);
1616 if (sigkill_pending(current))
1617 return;
1621 * If there is a group stop in progress,
1622 * we must participate in the bookkeeping.
1624 if (current->signal->group_stop_count > 0)
1625 --current->signal->group_stop_count;
1627 current->last_siginfo = info;
1628 current->exit_code = exit_code;
1630 /* Let the debugger run. */
1631 __set_current_state(TASK_TRACED);
1632 spin_unlock_irq(&current->sighand->siglock);
1633 read_lock(&tasklist_lock);
1634 if (may_ptrace_stop()) {
1635 do_notify_parent_cldstop(current, CLD_TRAPPED);
1637 * Don't want to allow preemption here, because
1638 * sys_ptrace() needs this task to be inactive.
1640 * XXX: implement read_unlock_no_resched().
1642 preempt_disable();
1643 read_unlock(&tasklist_lock);
1644 preempt_enable_no_resched();
1645 schedule();
1646 } else {
1648 * By the time we got the lock, our tracer went away.
1649 * Don't drop the lock yet, another tracer may come.
1651 __set_current_state(TASK_RUNNING);
1652 if (clear_code)
1653 current->exit_code = 0;
1654 read_unlock(&tasklist_lock);
1658 * While in TASK_TRACED, we were considered "frozen enough".
1659 * Now that we woke up, it's crucial if we're supposed to be
1660 * frozen that we freeze now before running anything substantial.
1662 try_to_freeze();
1665 * We are back. Now reacquire the siglock before touching
1666 * last_siginfo, so that we are sure to have synchronized with
1667 * any signal-sending on another CPU that wants to examine it.
1669 spin_lock_irq(&current->sighand->siglock);
1670 current->last_siginfo = NULL;
1673 * Queued signals ignored us while we were stopped for tracing.
1674 * So check for any that we should take before resuming user mode.
1675 * This sets TIF_SIGPENDING, but never clears it.
1677 recalc_sigpending_tsk(current);
1680 void ptrace_notify(int exit_code)
1682 siginfo_t info;
1684 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1686 memset(&info, 0, sizeof info);
1687 info.si_signo = SIGTRAP;
1688 info.si_code = exit_code;
1689 info.si_pid = task_pid_vnr(current);
1690 info.si_uid = current_uid();
1692 /* Let the debugger run. */
1693 spin_lock_irq(&current->sighand->siglock);
1694 ptrace_stop(exit_code, 1, &info);
1695 spin_unlock_irq(&current->sighand->siglock);
1699 * This performs the stopping for SIGSTOP and other stop signals.
1700 * We have to stop all threads in the thread group.
1701 * Returns nonzero if we've actually stopped and released the siglock.
1702 * Returns zero if we didn't stop and still hold the siglock.
1704 static int do_signal_stop(int signr)
1706 struct signal_struct *sig = current->signal;
1707 int notify;
1709 if (!sig->group_stop_count) {
1710 struct task_struct *t;
1712 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1713 unlikely(signal_group_exit(sig)))
1714 return 0;
1716 * There is no group stop already in progress.
1717 * We must initiate one now.
1719 sig->group_exit_code = signr;
1721 sig->group_stop_count = 1;
1722 for (t = next_thread(current); t != current; t = next_thread(t))
1724 * Setting state to TASK_STOPPED for a group
1725 * stop is always done with the siglock held,
1726 * so this check has no races.
1728 if (!(t->flags & PF_EXITING) &&
1729 !task_is_stopped_or_traced(t)) {
1730 sig->group_stop_count++;
1731 signal_wake_up(t, 0);
1735 * If there are no other threads in the group, or if there is
1736 * a group stop in progress and we are the last to stop, report
1737 * to the parent. When ptraced, every thread reports itself.
1739 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1740 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1742 * tracehook_notify_jctl() can drop and reacquire siglock, so
1743 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1744 * or SIGKILL comes in between ->group_stop_count == 0.
1746 if (sig->group_stop_count) {
1747 if (!--sig->group_stop_count)
1748 sig->flags = SIGNAL_STOP_STOPPED;
1749 current->exit_code = sig->group_exit_code;
1750 __set_current_state(TASK_STOPPED);
1752 spin_unlock_irq(&current->sighand->siglock);
1754 if (notify) {
1755 read_lock(&tasklist_lock);
1756 do_notify_parent_cldstop(current, notify);
1757 read_unlock(&tasklist_lock);
1760 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1761 do {
1762 schedule();
1763 } while (try_to_freeze());
1765 tracehook_finish_jctl();
1766 current->exit_code = 0;
1768 return 1;
1771 static int ptrace_signal(int signr, siginfo_t *info,
1772 struct pt_regs *regs, void *cookie)
1774 if (!task_ptrace(current))
1775 return signr;
1777 ptrace_signal_deliver(regs, cookie);
1779 /* Let the debugger run. */
1780 ptrace_stop(signr, 0, info);
1782 /* We're back. Did the debugger cancel the sig? */
1783 signr = current->exit_code;
1784 if (signr == 0)
1785 return signr;
1787 current->exit_code = 0;
1789 /* Update the siginfo structure if the signal has
1790 changed. If the debugger wanted something
1791 specific in the siginfo structure then it should
1792 have updated *info via PTRACE_SETSIGINFO. */
1793 if (signr != info->si_signo) {
1794 info->si_signo = signr;
1795 info->si_errno = 0;
1796 info->si_code = SI_USER;
1797 info->si_pid = task_pid_vnr(current->parent);
1798 info->si_uid = task_uid(current->parent);
1801 /* If the (new) signal is now blocked, requeue it. */
1802 if (sigismember(&current->blocked, signr)) {
1803 specific_send_sig_info(signr, info, current);
1804 signr = 0;
1807 return signr;
1810 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1811 struct pt_regs *regs, void *cookie)
1813 struct sighand_struct *sighand = current->sighand;
1814 struct signal_struct *signal = current->signal;
1815 int signr;
1817 relock:
1819 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1820 * While in TASK_STOPPED, we were considered "frozen enough".
1821 * Now that we woke up, it's crucial if we're supposed to be
1822 * frozen that we freeze now before running anything substantial.
1824 try_to_freeze();
1826 spin_lock_irq(&sighand->siglock);
1828 * Every stopped thread goes here after wakeup. Check to see if
1829 * we should notify the parent, prepare_signal(SIGCONT) encodes
1830 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1832 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1833 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1834 ? CLD_CONTINUED : CLD_STOPPED;
1835 signal->flags &= ~SIGNAL_CLD_MASK;
1837 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1838 spin_unlock_irq(&sighand->siglock);
1840 if (why) {
1841 read_lock(&tasklist_lock);
1842 do_notify_parent_cldstop(current->group_leader, why);
1843 read_unlock(&tasklist_lock);
1845 goto relock;
1848 for (;;) {
1849 struct k_sigaction *ka;
1851 if (unlikely(signal->group_stop_count > 0) &&
1852 do_signal_stop(0))
1853 goto relock;
1856 * Tracing can induce an artifical signal and choose sigaction.
1857 * The return value in @signr determines the default action,
1858 * but @info->si_signo is the signal number we will report.
1860 signr = tracehook_get_signal(current, regs, info, return_ka);
1861 if (unlikely(signr < 0))
1862 goto relock;
1863 if (unlikely(signr != 0))
1864 ka = return_ka;
1865 else {
1866 signr = dequeue_signal(current, &current->blocked,
1867 info);
1869 if (!signr)
1870 break; /* will return 0 */
1872 if (signr != SIGKILL) {
1873 signr = ptrace_signal(signr, info,
1874 regs, cookie);
1875 if (!signr)
1876 continue;
1879 ka = &sighand->action[signr-1];
1882 /* Trace actually delivered signals. */
1883 trace_signal_deliver(signr, info, ka);
1885 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1886 continue;
1887 if (ka->sa.sa_handler != SIG_DFL) {
1888 /* Run the handler. */
1889 *return_ka = *ka;
1891 if (ka->sa.sa_flags & SA_ONESHOT)
1892 ka->sa.sa_handler = SIG_DFL;
1894 break; /* will return non-zero "signr" value */
1898 * Now we are doing the default action for this signal.
1900 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1901 continue;
1904 * Global init gets no signals it doesn't want.
1905 * Container-init gets no signals it doesn't want from same
1906 * container.
1908 * Note that if global/container-init sees a sig_kernel_only()
1909 * signal here, the signal must have been generated internally
1910 * or must have come from an ancestor namespace. In either
1911 * case, the signal cannot be dropped.
1913 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1914 !sig_kernel_only(signr))
1915 continue;
1917 if (sig_kernel_stop(signr)) {
1919 * The default action is to stop all threads in
1920 * the thread group. The job control signals
1921 * do nothing in an orphaned pgrp, but SIGSTOP
1922 * always works. Note that siglock needs to be
1923 * dropped during the call to is_orphaned_pgrp()
1924 * because of lock ordering with tasklist_lock.
1925 * This allows an intervening SIGCONT to be posted.
1926 * We need to check for that and bail out if necessary.
1928 if (signr != SIGSTOP) {
1929 spin_unlock_irq(&sighand->siglock);
1931 /* signals can be posted during this window */
1933 if (is_current_pgrp_orphaned())
1934 goto relock;
1936 spin_lock_irq(&sighand->siglock);
1939 if (likely(do_signal_stop(info->si_signo))) {
1940 /* It released the siglock. */
1941 goto relock;
1945 * We didn't actually stop, due to a race
1946 * with SIGCONT or something like that.
1948 continue;
1951 spin_unlock_irq(&sighand->siglock);
1954 * Anything else is fatal, maybe with a core dump.
1956 current->flags |= PF_SIGNALED;
1958 if (sig_kernel_coredump(signr)) {
1959 if (print_fatal_signals)
1960 print_fatal_signal(regs, info->si_signo);
1962 * If it was able to dump core, this kills all
1963 * other threads in the group and synchronizes with
1964 * their demise. If we lost the race with another
1965 * thread getting here, it set group_exit_code
1966 * first and our do_group_exit call below will use
1967 * that value and ignore the one we pass it.
1969 do_coredump(info->si_signo, info->si_signo, regs);
1973 * Death signals, no core dump.
1975 do_group_exit(info->si_signo);
1976 /* NOTREACHED */
1978 spin_unlock_irq(&sighand->siglock);
1979 return signr;
1982 void exit_signals(struct task_struct *tsk)
1984 int group_stop = 0;
1985 struct task_struct *t;
1987 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1988 tsk->flags |= PF_EXITING;
1989 return;
1992 spin_lock_irq(&tsk->sighand->siglock);
1994 * From now this task is not visible for group-wide signals,
1995 * see wants_signal(), do_signal_stop().
1997 tsk->flags |= PF_EXITING;
1998 if (!signal_pending(tsk))
1999 goto out;
2001 /* It could be that __group_complete_signal() choose us to
2002 * notify about group-wide signal. Another thread should be
2003 * woken now to take the signal since we will not.
2005 for (t = tsk; (t = next_thread(t)) != tsk; )
2006 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2007 recalc_sigpending_and_wake(t);
2009 if (unlikely(tsk->signal->group_stop_count) &&
2010 !--tsk->signal->group_stop_count) {
2011 tsk->signal->flags = SIGNAL_STOP_STOPPED;
2012 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
2014 out:
2015 spin_unlock_irq(&tsk->sighand->siglock);
2017 if (unlikely(group_stop)) {
2018 read_lock(&tasklist_lock);
2019 do_notify_parent_cldstop(tsk, group_stop);
2020 read_unlock(&tasklist_lock);
2024 EXPORT_SYMBOL(recalc_sigpending);
2025 EXPORT_SYMBOL_GPL(dequeue_signal);
2026 EXPORT_SYMBOL(flush_signals);
2027 EXPORT_SYMBOL(force_sig);
2028 EXPORT_SYMBOL(send_sig);
2029 EXPORT_SYMBOL(send_sig_info);
2030 EXPORT_SYMBOL(sigprocmask);
2031 EXPORT_SYMBOL(block_all_signals);
2032 EXPORT_SYMBOL(unblock_all_signals);
2036 * System call entry points.
2039 SYSCALL_DEFINE0(restart_syscall)
2041 struct restart_block *restart = &current_thread_info()->restart_block;
2042 return restart->fn(restart);
2045 long do_no_restart_syscall(struct restart_block *param)
2047 return -EINTR;
2051 * We don't need to get the kernel lock - this is all local to this
2052 * particular thread.. (and that's good, because this is _heavily_
2053 * used by various programs)
2057 * This is also useful for kernel threads that want to temporarily
2058 * (or permanently) block certain signals.
2060 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2061 * interface happily blocks "unblockable" signals like SIGKILL
2062 * and friends.
2064 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2066 int error;
2068 spin_lock_irq(&current->sighand->siglock);
2069 if (oldset)
2070 *oldset = current->blocked;
2072 error = 0;
2073 switch (how) {
2074 case SIG_BLOCK:
2075 sigorsets(&current->blocked, &current->blocked, set);
2076 break;
2077 case SIG_UNBLOCK:
2078 signandsets(&current->blocked, &current->blocked, set);
2079 break;
2080 case SIG_SETMASK:
2081 current->blocked = *set;
2082 break;
2083 default:
2084 error = -EINVAL;
2086 recalc_sigpending();
2087 spin_unlock_irq(&current->sighand->siglock);
2089 return error;
2092 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2093 sigset_t __user *, oset, size_t, sigsetsize)
2095 int error = -EINVAL;
2096 sigset_t old_set, new_set;
2098 /* XXX: Don't preclude handling different sized sigset_t's. */
2099 if (sigsetsize != sizeof(sigset_t))
2100 goto out;
2102 if (set) {
2103 error = -EFAULT;
2104 if (copy_from_user(&new_set, set, sizeof(*set)))
2105 goto out;
2106 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2108 error = sigprocmask(how, &new_set, &old_set);
2109 if (error)
2110 goto out;
2111 if (oset)
2112 goto set_old;
2113 } else if (oset) {
2114 spin_lock_irq(&current->sighand->siglock);
2115 old_set = current->blocked;
2116 spin_unlock_irq(&current->sighand->siglock);
2118 set_old:
2119 error = -EFAULT;
2120 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2121 goto out;
2123 error = 0;
2124 out:
2125 return error;
2128 long do_sigpending(void __user *set, unsigned long sigsetsize)
2130 long error = -EINVAL;
2131 sigset_t pending;
2133 if (sigsetsize > sizeof(sigset_t))
2134 goto out;
2136 spin_lock_irq(&current->sighand->siglock);
2137 sigorsets(&pending, &current->pending.signal,
2138 &current->signal->shared_pending.signal);
2139 spin_unlock_irq(&current->sighand->siglock);
2141 /* Outside the lock because only this thread touches it. */
2142 sigandsets(&pending, &current->blocked, &pending);
2144 error = -EFAULT;
2145 if (!copy_to_user(set, &pending, sigsetsize))
2146 error = 0;
2148 out:
2149 return error;
2152 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2154 return do_sigpending(set, sigsetsize);
2157 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2159 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2161 int err;
2163 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2164 return -EFAULT;
2165 if (from->si_code < 0)
2166 return __copy_to_user(to, from, sizeof(siginfo_t))
2167 ? -EFAULT : 0;
2169 * If you change siginfo_t structure, please be sure
2170 * this code is fixed accordingly.
2171 * Please remember to update the signalfd_copyinfo() function
2172 * inside fs/signalfd.c too, in case siginfo_t changes.
2173 * It should never copy any pad contained in the structure
2174 * to avoid security leaks, but must copy the generic
2175 * 3 ints plus the relevant union member.
2177 err = __put_user(from->si_signo, &to->si_signo);
2178 err |= __put_user(from->si_errno, &to->si_errno);
2179 err |= __put_user((short)from->si_code, &to->si_code);
2180 switch (from->si_code & __SI_MASK) {
2181 case __SI_KILL:
2182 err |= __put_user(from->si_pid, &to->si_pid);
2183 err |= __put_user(from->si_uid, &to->si_uid);
2184 break;
2185 case __SI_TIMER:
2186 err |= __put_user(from->si_tid, &to->si_tid);
2187 err |= __put_user(from->si_overrun, &to->si_overrun);
2188 err |= __put_user(from->si_ptr, &to->si_ptr);
2189 break;
2190 case __SI_POLL:
2191 err |= __put_user(from->si_band, &to->si_band);
2192 err |= __put_user(from->si_fd, &to->si_fd);
2193 break;
2194 case __SI_FAULT:
2195 err |= __put_user(from->si_addr, &to->si_addr);
2196 #ifdef __ARCH_SI_TRAPNO
2197 err |= __put_user(from->si_trapno, &to->si_trapno);
2198 #endif
2199 break;
2200 case __SI_CHLD:
2201 err |= __put_user(from->si_pid, &to->si_pid);
2202 err |= __put_user(from->si_uid, &to->si_uid);
2203 err |= __put_user(from->si_status, &to->si_status);
2204 err |= __put_user(from->si_utime, &to->si_utime);
2205 err |= __put_user(from->si_stime, &to->si_stime);
2206 break;
2207 case __SI_RT: /* This is not generated by the kernel as of now. */
2208 case __SI_MESGQ: /* But this is */
2209 err |= __put_user(from->si_pid, &to->si_pid);
2210 err |= __put_user(from->si_uid, &to->si_uid);
2211 err |= __put_user(from->si_ptr, &to->si_ptr);
2212 break;
2213 default: /* this is just in case for now ... */
2214 err |= __put_user(from->si_pid, &to->si_pid);
2215 err |= __put_user(from->si_uid, &to->si_uid);
2216 break;
2218 return err;
2221 #endif
2223 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2224 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2225 size_t, sigsetsize)
2227 int ret, sig;
2228 sigset_t these;
2229 struct timespec ts;
2230 siginfo_t info;
2231 long timeout = 0;
2233 /* XXX: Don't preclude handling different sized sigset_t's. */
2234 if (sigsetsize != sizeof(sigset_t))
2235 return -EINVAL;
2237 if (copy_from_user(&these, uthese, sizeof(these)))
2238 return -EFAULT;
2241 * Invert the set of allowed signals to get those we
2242 * want to block.
2244 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2245 signotset(&these);
2247 if (uts) {
2248 if (copy_from_user(&ts, uts, sizeof(ts)))
2249 return -EFAULT;
2250 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2251 || ts.tv_sec < 0)
2252 return -EINVAL;
2255 spin_lock_irq(&current->sighand->siglock);
2256 sig = dequeue_signal(current, &these, &info);
2257 if (!sig) {
2258 timeout = MAX_SCHEDULE_TIMEOUT;
2259 if (uts)
2260 timeout = (timespec_to_jiffies(&ts)
2261 + (ts.tv_sec || ts.tv_nsec));
2263 if (timeout) {
2264 /* None ready -- temporarily unblock those we're
2265 * interested while we are sleeping in so that we'll
2266 * be awakened when they arrive. */
2267 current->real_blocked = current->blocked;
2268 sigandsets(&current->blocked, &current->blocked, &these);
2269 recalc_sigpending();
2270 spin_unlock_irq(&current->sighand->siglock);
2272 timeout = schedule_timeout_interruptible(timeout);
2274 spin_lock_irq(&current->sighand->siglock);
2275 sig = dequeue_signal(current, &these, &info);
2276 current->blocked = current->real_blocked;
2277 siginitset(&current->real_blocked, 0);
2278 recalc_sigpending();
2281 spin_unlock_irq(&current->sighand->siglock);
2283 if (sig) {
2284 ret = sig;
2285 if (uinfo) {
2286 if (copy_siginfo_to_user(uinfo, &info))
2287 ret = -EFAULT;
2289 } else {
2290 ret = -EAGAIN;
2291 if (timeout)
2292 ret = -EINTR;
2295 return ret;
2298 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2300 struct siginfo info;
2302 info.si_signo = sig;
2303 info.si_errno = 0;
2304 info.si_code = SI_USER;
2305 info.si_pid = task_tgid_vnr(current);
2306 info.si_uid = current_uid();
2308 return kill_something_info(sig, &info, pid);
2311 static int
2312 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2314 struct task_struct *p;
2315 int error = -ESRCH;
2317 rcu_read_lock();
2318 p = find_task_by_vpid(pid);
2319 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2320 error = check_kill_permission(sig, info, p);
2322 * The null signal is a permissions and process existence
2323 * probe. No signal is actually delivered.
2325 if (!error && sig) {
2326 error = do_send_sig_info(sig, info, p, false);
2328 * If lock_task_sighand() failed we pretend the task
2329 * dies after receiving the signal. The window is tiny,
2330 * and the signal is private anyway.
2332 if (unlikely(error == -ESRCH))
2333 error = 0;
2336 rcu_read_unlock();
2338 return error;
2341 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2343 struct siginfo info;
2345 info.si_signo = sig;
2346 info.si_errno = 0;
2347 info.si_code = SI_TKILL;
2348 info.si_pid = task_tgid_vnr(current);
2349 info.si_uid = current_uid();
2351 return do_send_specific(tgid, pid, sig, &info);
2355 * sys_tgkill - send signal to one specific thread
2356 * @tgid: the thread group ID of the thread
2357 * @pid: the PID of the thread
2358 * @sig: signal to be sent
2360 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2361 * exists but it's not belonging to the target process anymore. This
2362 * method solves the problem of threads exiting and PIDs getting reused.
2364 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2366 /* This is only valid for single tasks */
2367 if (pid <= 0 || tgid <= 0)
2368 return -EINVAL;
2370 return do_tkill(tgid, pid, sig);
2374 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2376 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2378 /* This is only valid for single tasks */
2379 if (pid <= 0)
2380 return -EINVAL;
2382 return do_tkill(0, pid, sig);
2385 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2386 siginfo_t __user *, uinfo)
2388 siginfo_t info;
2390 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2391 return -EFAULT;
2393 /* Not even root can pretend to send signals from the kernel.
2394 Nor can they impersonate a kill(), which adds source info. */
2395 if (info.si_code >= 0)
2396 return -EPERM;
2397 info.si_signo = sig;
2399 /* POSIX.1b doesn't mention process groups. */
2400 return kill_proc_info(sig, &info, pid);
2403 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2405 /* This is only valid for single tasks */
2406 if (pid <= 0 || tgid <= 0)
2407 return -EINVAL;
2409 /* Not even root can pretend to send signals from the kernel.
2410 Nor can they impersonate a kill(), which adds source info. */
2411 if (info->si_code >= 0)
2412 return -EPERM;
2413 info->si_signo = sig;
2415 return do_send_specific(tgid, pid, sig, info);
2418 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2419 siginfo_t __user *, uinfo)
2421 siginfo_t info;
2423 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2424 return -EFAULT;
2426 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2429 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2431 struct task_struct *t = current;
2432 struct k_sigaction *k;
2433 sigset_t mask;
2435 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2436 return -EINVAL;
2438 k = &t->sighand->action[sig-1];
2440 spin_lock_irq(&current->sighand->siglock);
2441 if (oact)
2442 *oact = *k;
2444 if (act) {
2445 sigdelsetmask(&act->sa.sa_mask,
2446 sigmask(SIGKILL) | sigmask(SIGSTOP));
2447 *k = *act;
2449 * POSIX 3.3.1.3:
2450 * "Setting a signal action to SIG_IGN for a signal that is
2451 * pending shall cause the pending signal to be discarded,
2452 * whether or not it is blocked."
2454 * "Setting a signal action to SIG_DFL for a signal that is
2455 * pending and whose default action is to ignore the signal
2456 * (for example, SIGCHLD), shall cause the pending signal to
2457 * be discarded, whether or not it is blocked"
2459 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2460 sigemptyset(&mask);
2461 sigaddset(&mask, sig);
2462 rm_from_queue_full(&mask, &t->signal->shared_pending);
2463 do {
2464 rm_from_queue_full(&mask, &t->pending);
2465 t = next_thread(t);
2466 } while (t != current);
2470 spin_unlock_irq(&current->sighand->siglock);
2471 return 0;
2474 int
2475 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2477 stack_t oss;
2478 int error;
2480 oss.ss_sp = (void __user *) current->sas_ss_sp;
2481 oss.ss_size = current->sas_ss_size;
2482 oss.ss_flags = sas_ss_flags(sp);
2484 if (uss) {
2485 void __user *ss_sp;
2486 size_t ss_size;
2487 int ss_flags;
2489 error = -EFAULT;
2490 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2491 goto out;
2492 error = __get_user(ss_sp, &uss->ss_sp) |
2493 __get_user(ss_flags, &uss->ss_flags) |
2494 __get_user(ss_size, &uss->ss_size);
2495 if (error)
2496 goto out;
2498 error = -EPERM;
2499 if (on_sig_stack(sp))
2500 goto out;
2502 error = -EINVAL;
2505 * Note - this code used to test ss_flags incorrectly
2506 * old code may have been written using ss_flags==0
2507 * to mean ss_flags==SS_ONSTACK (as this was the only
2508 * way that worked) - this fix preserves that older
2509 * mechanism
2511 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2512 goto out;
2514 if (ss_flags == SS_DISABLE) {
2515 ss_size = 0;
2516 ss_sp = NULL;
2517 } else {
2518 error = -ENOMEM;
2519 if (ss_size < MINSIGSTKSZ)
2520 goto out;
2523 current->sas_ss_sp = (unsigned long) ss_sp;
2524 current->sas_ss_size = ss_size;
2527 error = 0;
2528 if (uoss) {
2529 error = -EFAULT;
2530 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2531 goto out;
2532 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2533 __put_user(oss.ss_size, &uoss->ss_size) |
2534 __put_user(oss.ss_flags, &uoss->ss_flags);
2537 out:
2538 return error;
2541 #ifdef __ARCH_WANT_SYS_SIGPENDING
2543 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2545 return do_sigpending(set, sizeof(*set));
2548 #endif
2550 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2551 /* Some platforms have their own version with special arguments others
2552 support only sys_rt_sigprocmask. */
2554 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2555 old_sigset_t __user *, oset)
2557 int error;
2558 old_sigset_t old_set, new_set;
2560 if (set) {
2561 error = -EFAULT;
2562 if (copy_from_user(&new_set, set, sizeof(*set)))
2563 goto out;
2564 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2566 spin_lock_irq(&current->sighand->siglock);
2567 old_set = current->blocked.sig[0];
2569 error = 0;
2570 switch (how) {
2571 default:
2572 error = -EINVAL;
2573 break;
2574 case SIG_BLOCK:
2575 sigaddsetmask(&current->blocked, new_set);
2576 break;
2577 case SIG_UNBLOCK:
2578 sigdelsetmask(&current->blocked, new_set);
2579 break;
2580 case SIG_SETMASK:
2581 current->blocked.sig[0] = new_set;
2582 break;
2585 recalc_sigpending();
2586 spin_unlock_irq(&current->sighand->siglock);
2587 if (error)
2588 goto out;
2589 if (oset)
2590 goto set_old;
2591 } else if (oset) {
2592 old_set = current->blocked.sig[0];
2593 set_old:
2594 error = -EFAULT;
2595 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2596 goto out;
2598 error = 0;
2599 out:
2600 return error;
2602 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2604 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2605 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2606 const struct sigaction __user *, act,
2607 struct sigaction __user *, oact,
2608 size_t, sigsetsize)
2610 struct k_sigaction new_sa, old_sa;
2611 int ret = -EINVAL;
2613 /* XXX: Don't preclude handling different sized sigset_t's. */
2614 if (sigsetsize != sizeof(sigset_t))
2615 goto out;
2617 if (act) {
2618 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2619 return -EFAULT;
2622 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2624 if (!ret && oact) {
2625 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2626 return -EFAULT;
2628 out:
2629 return ret;
2631 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2633 #ifdef __ARCH_WANT_SYS_SGETMASK
2636 * For backwards compatibility. Functionality superseded by sigprocmask.
2638 SYSCALL_DEFINE0(sgetmask)
2640 /* SMP safe */
2641 return current->blocked.sig[0];
2644 SYSCALL_DEFINE1(ssetmask, int, newmask)
2646 int old;
2648 spin_lock_irq(&current->sighand->siglock);
2649 old = current->blocked.sig[0];
2651 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2652 sigmask(SIGSTOP)));
2653 recalc_sigpending();
2654 spin_unlock_irq(&current->sighand->siglock);
2656 return old;
2658 #endif /* __ARCH_WANT_SGETMASK */
2660 #ifdef __ARCH_WANT_SYS_SIGNAL
2662 * For backwards compatibility. Functionality superseded by sigaction.
2664 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2666 struct k_sigaction new_sa, old_sa;
2667 int ret;
2669 new_sa.sa.sa_handler = handler;
2670 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2671 sigemptyset(&new_sa.sa.sa_mask);
2673 ret = do_sigaction(sig, &new_sa, &old_sa);
2675 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2677 #endif /* __ARCH_WANT_SYS_SIGNAL */
2679 #ifdef __ARCH_WANT_SYS_PAUSE
2681 SYSCALL_DEFINE0(pause)
2683 current->state = TASK_INTERRUPTIBLE;
2684 schedule();
2685 return -ERESTARTNOHAND;
2688 #endif
2690 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2691 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2693 sigset_t newset;
2695 /* XXX: Don't preclude handling different sized sigset_t's. */
2696 if (sigsetsize != sizeof(sigset_t))
2697 return -EINVAL;
2699 if (copy_from_user(&newset, unewset, sizeof(newset)))
2700 return -EFAULT;
2701 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2703 spin_lock_irq(&current->sighand->siglock);
2704 current->saved_sigmask = current->blocked;
2705 current->blocked = newset;
2706 recalc_sigpending();
2707 spin_unlock_irq(&current->sighand->siglock);
2709 current->state = TASK_INTERRUPTIBLE;
2710 schedule();
2711 set_restore_sigmask();
2712 return -ERESTARTNOHAND;
2714 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2716 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2718 return NULL;
2721 void __init signals_init(void)
2723 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);