Linux 2.6.32.47
[linux/fpc-iii.git] / kernel / signal.c
blob2494827e42acd3450380026a2a5144474f2c616e
1 /*
2 * linux/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <linux/fs.h>
18 #include <linux/tty.h>
19 #include <linux/binfmts.h>
20 #include <linux/security.h>
21 #include <linux/syscalls.h>
22 #include <linux/ptrace.h>
23 #include <linux/signal.h>
24 #include <linux/signalfd.h>
25 #include <linux/tracehook.h>
26 #include <linux/capability.h>
27 #include <linux/freezer.h>
28 #include <linux/pid_namespace.h>
29 #include <linux/nsproxy.h>
30 #include <trace/events/sched.h>
32 #include <asm/param.h>
33 #include <asm/uaccess.h>
34 #include <asm/unistd.h>
35 #include <asm/siginfo.h>
36 #include "audit.h" /* audit_signal_info() */
39 * SLAB caches for signal bits.
42 static struct kmem_cache *sigqueue_cachep;
44 static void __user *sig_handler(struct task_struct *t, int sig)
46 return t->sighand->action[sig - 1].sa.sa_handler;
49 static int sig_handler_ignored(void __user *handler, int sig)
51 /* Is it explicitly or implicitly ignored? */
52 return handler == SIG_IGN ||
53 (handler == SIG_DFL && sig_kernel_ignore(sig));
56 static int sig_task_ignored(struct task_struct *t, int sig,
57 int from_ancestor_ns)
59 void __user *handler;
61 handler = sig_handler(t, sig);
63 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
64 handler == SIG_DFL && !from_ancestor_ns)
65 return 1;
67 return sig_handler_ignored(handler, sig);
70 static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
73 * Blocked signals are never ignored, since the
74 * signal handler may change by the time it is
75 * unblocked.
77 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
78 return 0;
80 if (!sig_task_ignored(t, sig, from_ancestor_ns))
81 return 0;
84 * Tracers may want to know about even ignored signals.
86 return !tracehook_consider_ignored_signal(t, sig);
90 * Re-calculate pending state from the set of locally pending
91 * signals, globally pending signals, and blocked signals.
93 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
95 unsigned long ready;
96 long i;
98 switch (_NSIG_WORDS) {
99 default:
100 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
101 ready |= signal->sig[i] &~ blocked->sig[i];
102 break;
104 case 4: ready = signal->sig[3] &~ blocked->sig[3];
105 ready |= signal->sig[2] &~ blocked->sig[2];
106 ready |= signal->sig[1] &~ blocked->sig[1];
107 ready |= signal->sig[0] &~ blocked->sig[0];
108 break;
110 case 2: ready = signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
114 case 1: ready = signal->sig[0] &~ blocked->sig[0];
116 return ready != 0;
119 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
121 static int recalc_sigpending_tsk(struct task_struct *t)
123 if (t->signal->group_stop_count > 0 ||
124 PENDING(&t->pending, &t->blocked) ||
125 PENDING(&t->signal->shared_pending, &t->blocked)) {
126 set_tsk_thread_flag(t, TIF_SIGPENDING);
127 return 1;
130 * We must never clear the flag in another thread, or in current
131 * when it's possible the current syscall is returning -ERESTART*.
132 * So we don't clear it here, and only callers who know they should do.
134 return 0;
138 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
139 * This is superfluous when called on current, the wakeup is a harmless no-op.
141 void recalc_sigpending_and_wake(struct task_struct *t)
143 if (recalc_sigpending_tsk(t))
144 signal_wake_up(t, 0);
147 void recalc_sigpending(void)
149 if (unlikely(tracehook_force_sigpending()))
150 set_thread_flag(TIF_SIGPENDING);
151 else if (!recalc_sigpending_tsk(current) && !freezing(current))
152 clear_thread_flag(TIF_SIGPENDING);
156 /* Given the mask, find the first available signal that should be serviced. */
158 int next_signal(struct sigpending *pending, sigset_t *mask)
160 unsigned long i, *s, *m, x;
161 int sig = 0;
163 s = pending->signal.sig;
164 m = mask->sig;
165 switch (_NSIG_WORDS) {
166 default:
167 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
168 if ((x = *s &~ *m) != 0) {
169 sig = ffz(~x) + i*_NSIG_BPW + 1;
170 break;
172 break;
174 case 2: if ((x = s[0] &~ m[0]) != 0)
175 sig = 1;
176 else if ((x = s[1] &~ m[1]) != 0)
177 sig = _NSIG_BPW + 1;
178 else
179 break;
180 sig += ffz(~x);
181 break;
183 case 1: if ((x = *s &~ *m) != 0)
184 sig = ffz(~x) + 1;
185 break;
188 return sig;
192 * allocate a new signal queue record
193 * - this may be called without locks if and only if t == current, otherwise an
194 * appopriate lock must be held to stop the target task from exiting
196 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
197 int override_rlimit)
199 struct sigqueue *q = NULL;
200 struct user_struct *user;
203 * We won't get problems with the target's UID changing under us
204 * because changing it requires RCU be used, and if t != current, the
205 * caller must be holding the RCU readlock (by way of a spinlock) and
206 * we use RCU protection here
208 user = get_uid(__task_cred(t)->user);
209 atomic_inc(&user->sigpending);
210 if (override_rlimit ||
211 atomic_read(&user->sigpending) <=
212 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
213 q = kmem_cache_alloc(sigqueue_cachep, flags);
214 if (unlikely(q == NULL)) {
215 atomic_dec(&user->sigpending);
216 free_uid(user);
217 } else {
218 INIT_LIST_HEAD(&q->list);
219 q->flags = 0;
220 q->user = user;
223 return q;
226 static void __sigqueue_free(struct sigqueue *q)
228 if (q->flags & SIGQUEUE_PREALLOC)
229 return;
230 atomic_dec(&q->user->sigpending);
231 free_uid(q->user);
232 kmem_cache_free(sigqueue_cachep, q);
235 void flush_sigqueue(struct sigpending *queue)
237 struct sigqueue *q;
239 sigemptyset(&queue->signal);
240 while (!list_empty(&queue->list)) {
241 q = list_entry(queue->list.next, struct sigqueue , list);
242 list_del_init(&q->list);
243 __sigqueue_free(q);
248 * Flush all pending signals for a task.
250 void __flush_signals(struct task_struct *t)
252 clear_tsk_thread_flag(t, TIF_SIGPENDING);
253 flush_sigqueue(&t->pending);
254 flush_sigqueue(&t->signal->shared_pending);
257 void flush_signals(struct task_struct *t)
259 unsigned long flags;
261 spin_lock_irqsave(&t->sighand->siglock, flags);
262 __flush_signals(t);
263 spin_unlock_irqrestore(&t->sighand->siglock, flags);
266 static void __flush_itimer_signals(struct sigpending *pending)
268 sigset_t signal, retain;
269 struct sigqueue *q, *n;
271 signal = pending->signal;
272 sigemptyset(&retain);
274 list_for_each_entry_safe(q, n, &pending->list, list) {
275 int sig = q->info.si_signo;
277 if (likely(q->info.si_code != SI_TIMER)) {
278 sigaddset(&retain, sig);
279 } else {
280 sigdelset(&signal, sig);
281 list_del_init(&q->list);
282 __sigqueue_free(q);
286 sigorsets(&pending->signal, &signal, &retain);
289 void flush_itimer_signals(void)
291 struct task_struct *tsk = current;
292 unsigned long flags;
294 spin_lock_irqsave(&tsk->sighand->siglock, flags);
295 __flush_itimer_signals(&tsk->pending);
296 __flush_itimer_signals(&tsk->signal->shared_pending);
297 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
300 void ignore_signals(struct task_struct *t)
302 int i;
304 for (i = 0; i < _NSIG; ++i)
305 t->sighand->action[i].sa.sa_handler = SIG_IGN;
307 flush_signals(t);
311 * Flush all handlers for a task.
314 void
315 flush_signal_handlers(struct task_struct *t, int force_default)
317 int i;
318 struct k_sigaction *ka = &t->sighand->action[0];
319 for (i = _NSIG ; i != 0 ; i--) {
320 if (force_default || ka->sa.sa_handler != SIG_IGN)
321 ka->sa.sa_handler = SIG_DFL;
322 ka->sa.sa_flags = 0;
323 sigemptyset(&ka->sa.sa_mask);
324 ka++;
328 int unhandled_signal(struct task_struct *tsk, int sig)
330 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
331 if (is_global_init(tsk))
332 return 1;
333 if (handler != SIG_IGN && handler != SIG_DFL)
334 return 0;
335 return !tracehook_consider_fatal_signal(tsk, sig);
339 /* Notify the system that a driver wants to block all signals for this
340 * process, and wants to be notified if any signals at all were to be
341 * sent/acted upon. If the notifier routine returns non-zero, then the
342 * signal will be acted upon after all. If the notifier routine returns 0,
343 * then then signal will be blocked. Only one block per process is
344 * allowed. priv is a pointer to private data that the notifier routine
345 * can use to determine if the signal should be blocked or not. */
347 void
348 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
350 unsigned long flags;
352 spin_lock_irqsave(&current->sighand->siglock, flags);
353 current->notifier_mask = mask;
354 current->notifier_data = priv;
355 current->notifier = notifier;
356 spin_unlock_irqrestore(&current->sighand->siglock, flags);
359 /* Notify the system that blocking has ended. */
361 void
362 unblock_all_signals(void)
364 unsigned long flags;
366 spin_lock_irqsave(&current->sighand->siglock, flags);
367 current->notifier = NULL;
368 current->notifier_data = NULL;
369 recalc_sigpending();
370 spin_unlock_irqrestore(&current->sighand->siglock, flags);
373 static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
375 struct sigqueue *q, *first = NULL;
378 * Collect the siginfo appropriate to this signal. Check if
379 * there is another siginfo for the same signal.
381 list_for_each_entry(q, &list->list, list) {
382 if (q->info.si_signo == sig) {
383 if (first)
384 goto still_pending;
385 first = q;
389 sigdelset(&list->signal, sig);
391 if (first) {
392 still_pending:
393 list_del_init(&first->list);
394 copy_siginfo(info, &first->info);
395 __sigqueue_free(first);
396 } else {
397 /* Ok, it wasn't in the queue. This must be
398 a fast-pathed signal or we must have been
399 out of queue space. So zero out the info.
401 info->si_signo = sig;
402 info->si_errno = 0;
403 info->si_code = 0;
404 info->si_pid = 0;
405 info->si_uid = 0;
409 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
410 siginfo_t *info)
412 int sig = next_signal(pending, mask);
414 if (sig) {
415 if (current->notifier) {
416 if (sigismember(current->notifier_mask, sig)) {
417 if (!(current->notifier)(current->notifier_data)) {
418 clear_thread_flag(TIF_SIGPENDING);
419 return 0;
424 collect_signal(sig, pending, info);
427 return sig;
431 * Dequeue a signal and return the element to the caller, which is
432 * expected to free it.
434 * All callers have to hold the siglock.
436 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
438 int signr;
440 /* We only dequeue private signals from ourselves, we don't let
441 * signalfd steal them
443 signr = __dequeue_signal(&tsk->pending, mask, info);
444 if (!signr) {
445 signr = __dequeue_signal(&tsk->signal->shared_pending,
446 mask, info);
448 * itimer signal ?
450 * itimers are process shared and we restart periodic
451 * itimers in the signal delivery path to prevent DoS
452 * attacks in the high resolution timer case. This is
453 * compliant with the old way of self restarting
454 * itimers, as the SIGALRM is a legacy signal and only
455 * queued once. Changing the restart behaviour to
456 * restart the timer in the signal dequeue path is
457 * reducing the timer noise on heavy loaded !highres
458 * systems too.
460 if (unlikely(signr == SIGALRM)) {
461 struct hrtimer *tmr = &tsk->signal->real_timer;
463 if (!hrtimer_is_queued(tmr) &&
464 tsk->signal->it_real_incr.tv64 != 0) {
465 hrtimer_forward(tmr, tmr->base->get_time(),
466 tsk->signal->it_real_incr);
467 hrtimer_restart(tmr);
472 recalc_sigpending();
473 if (!signr)
474 return 0;
476 if (unlikely(sig_kernel_stop(signr))) {
478 * Set a marker that we have dequeued a stop signal. Our
479 * caller might release the siglock and then the pending
480 * stop signal it is about to process is no longer in the
481 * pending bitmasks, but must still be cleared by a SIGCONT
482 * (and overruled by a SIGKILL). So those cases clear this
483 * shared flag after we've set it. Note that this flag may
484 * remain set after the signal we return is ignored or
485 * handled. That doesn't matter because its only purpose
486 * is to alert stop-signal processing code when another
487 * processor has come along and cleared the flag.
489 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
491 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
493 * Release the siglock to ensure proper locking order
494 * of timer locks outside of siglocks. Note, we leave
495 * irqs disabled here, since the posix-timers code is
496 * about to disable them again anyway.
498 spin_unlock(&tsk->sighand->siglock);
499 do_schedule_next_timer(info);
500 spin_lock(&tsk->sighand->siglock);
502 return signr;
506 * Tell a process that it has a new active signal..
508 * NOTE! we rely on the previous spin_lock to
509 * lock interrupts for us! We can only be called with
510 * "siglock" held, and the local interrupt must
511 * have been disabled when that got acquired!
513 * No need to set need_resched since signal event passing
514 * goes through ->blocked
516 void signal_wake_up(struct task_struct *t, int resume)
518 unsigned int mask;
520 set_tsk_thread_flag(t, TIF_SIGPENDING);
523 * For SIGKILL, we want to wake it up in the stopped/traced/killable
524 * case. We don't check t->state here because there is a race with it
525 * executing another processor and just now entering stopped state.
526 * By using wake_up_state, we ensure the process will wake up and
527 * handle its death signal.
529 mask = TASK_INTERRUPTIBLE;
530 if (resume)
531 mask |= TASK_WAKEKILL;
532 if (!wake_up_state(t, mask))
533 kick_process(t);
537 * Remove signals in mask from the pending set and queue.
538 * Returns 1 if any signals were found.
540 * All callers must be holding the siglock.
542 * This version takes a sigset mask and looks at all signals,
543 * not just those in the first mask word.
545 static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
547 struct sigqueue *q, *n;
548 sigset_t m;
550 sigandsets(&m, mask, &s->signal);
551 if (sigisemptyset(&m))
552 return 0;
554 signandsets(&s->signal, &s->signal, mask);
555 list_for_each_entry_safe(q, n, &s->list, list) {
556 if (sigismember(mask, q->info.si_signo)) {
557 list_del_init(&q->list);
558 __sigqueue_free(q);
561 return 1;
564 * Remove signals in mask from the pending set and queue.
565 * Returns 1 if any signals were found.
567 * All callers must be holding the siglock.
569 static int rm_from_queue(unsigned long mask, struct sigpending *s)
571 struct sigqueue *q, *n;
573 if (!sigtestsetmask(&s->signal, mask))
574 return 0;
576 sigdelsetmask(&s->signal, mask);
577 list_for_each_entry_safe(q, n, &s->list, list) {
578 if (q->info.si_signo < SIGRTMIN &&
579 (mask & sigmask(q->info.si_signo))) {
580 list_del_init(&q->list);
581 __sigqueue_free(q);
584 return 1;
588 * Bad permissions for sending the signal
589 * - the caller must hold at least the RCU read lock
591 static int check_kill_permission(int sig, struct siginfo *info,
592 struct task_struct *t)
594 const struct cred *cred, *tcred;
595 struct pid *sid;
596 int error;
598 if (!valid_signal(sig))
599 return -EINVAL;
601 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
602 return 0;
604 error = audit_signal_info(sig, t); /* Let audit system see the signal */
605 if (error)
606 return error;
608 cred = current_cred();
609 tcred = __task_cred(t);
610 if (!same_thread_group(current, t) &&
611 (cred->euid ^ tcred->suid) &&
612 (cred->euid ^ tcred->uid) &&
613 (cred->uid ^ tcred->suid) &&
614 (cred->uid ^ tcred->uid) &&
615 !capable(CAP_KILL)) {
616 switch (sig) {
617 case SIGCONT:
618 sid = task_session(t);
620 * We don't return the error if sid == NULL. The
621 * task was unhashed, the caller must notice this.
623 if (!sid || sid == task_session(current))
624 break;
625 default:
626 return -EPERM;
630 return security_task_kill(t, info, sig, 0);
634 * Handle magic process-wide effects of stop/continue signals. Unlike
635 * the signal actions, these happen immediately at signal-generation
636 * time regardless of blocking, ignoring, or handling. This does the
637 * actual continuing for SIGCONT, but not the actual stopping for stop
638 * signals. The process stop is done as a signal action for SIG_DFL.
640 * Returns true if the signal should be actually delivered, otherwise
641 * it should be dropped.
643 static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
645 struct signal_struct *signal = p->signal;
646 struct task_struct *t;
648 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
650 * The process is in the middle of dying, nothing to do.
652 } else if (sig_kernel_stop(sig)) {
654 * This is a stop signal. Remove SIGCONT from all queues.
656 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
657 t = p;
658 do {
659 rm_from_queue(sigmask(SIGCONT), &t->pending);
660 } while_each_thread(p, t);
661 } else if (sig == SIGCONT) {
662 unsigned int why;
664 * Remove all stop signals from all queues,
665 * and wake all threads.
667 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
668 t = p;
669 do {
670 unsigned int state;
671 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
673 * If there is a handler for SIGCONT, we must make
674 * sure that no thread returns to user mode before
675 * we post the signal, in case it was the only
676 * thread eligible to run the signal handler--then
677 * it must not do anything between resuming and
678 * running the handler. With the TIF_SIGPENDING
679 * flag set, the thread will pause and acquire the
680 * siglock that we hold now and until we've queued
681 * the pending signal.
683 * Wake up the stopped thread _after_ setting
684 * TIF_SIGPENDING
686 state = __TASK_STOPPED;
687 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
688 set_tsk_thread_flag(t, TIF_SIGPENDING);
689 state |= TASK_INTERRUPTIBLE;
691 wake_up_state(t, state);
692 } while_each_thread(p, t);
695 * Notify the parent with CLD_CONTINUED if we were stopped.
697 * If we were in the middle of a group stop, we pretend it
698 * was already finished, and then continued. Since SIGCHLD
699 * doesn't queue we report only CLD_STOPPED, as if the next
700 * CLD_CONTINUED was dropped.
702 why = 0;
703 if (signal->flags & SIGNAL_STOP_STOPPED)
704 why |= SIGNAL_CLD_CONTINUED;
705 else if (signal->group_stop_count)
706 why |= SIGNAL_CLD_STOPPED;
708 if (why) {
710 * The first thread which returns from do_signal_stop()
711 * will take ->siglock, notice SIGNAL_CLD_MASK, and
712 * notify its parent. See get_signal_to_deliver().
714 signal->flags = why | SIGNAL_STOP_CONTINUED;
715 signal->group_stop_count = 0;
716 signal->group_exit_code = 0;
717 } else {
719 * We are not stopped, but there could be a stop
720 * signal in the middle of being processed after
721 * being removed from the queue. Clear that too.
723 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
727 return !sig_ignored(p, sig, from_ancestor_ns);
731 * Test if P wants to take SIG. After we've checked all threads with this,
732 * it's equivalent to finding no threads not blocking SIG. Any threads not
733 * blocking SIG were ruled out because they are not running and already
734 * have pending signals. Such threads will dequeue from the shared queue
735 * as soon as they're available, so putting the signal on the shared queue
736 * will be equivalent to sending it to one such thread.
738 static inline int wants_signal(int sig, struct task_struct *p)
740 if (sigismember(&p->blocked, sig))
741 return 0;
742 if (p->flags & PF_EXITING)
743 return 0;
744 if (sig == SIGKILL)
745 return 1;
746 if (task_is_stopped_or_traced(p))
747 return 0;
748 return task_curr(p) || !signal_pending(p);
751 static void complete_signal(int sig, struct task_struct *p, int group)
753 struct signal_struct *signal = p->signal;
754 struct task_struct *t;
757 * Now find a thread we can wake up to take the signal off the queue.
759 * If the main thread wants the signal, it gets first crack.
760 * Probably the least surprising to the average bear.
762 if (wants_signal(sig, p))
763 t = p;
764 else if (!group || thread_group_empty(p))
766 * There is just one thread and it does not need to be woken.
767 * It will dequeue unblocked signals before it runs again.
769 return;
770 else {
772 * Otherwise try to find a suitable thread.
774 t = signal->curr_target;
775 while (!wants_signal(sig, t)) {
776 t = next_thread(t);
777 if (t == signal->curr_target)
779 * No thread needs to be woken.
780 * Any eligible threads will see
781 * the signal in the queue soon.
783 return;
785 signal->curr_target = t;
789 * Found a killable thread. If the signal will be fatal,
790 * then start taking the whole group down immediately.
792 if (sig_fatal(p, sig) &&
793 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
794 !sigismember(&t->real_blocked, sig) &&
795 (sig == SIGKILL ||
796 !tracehook_consider_fatal_signal(t, sig))) {
798 * This signal will be fatal to the whole group.
800 if (!sig_kernel_coredump(sig)) {
802 * Start a group exit and wake everybody up.
803 * This way we don't have other threads
804 * running and doing things after a slower
805 * thread has the fatal signal pending.
807 signal->flags = SIGNAL_GROUP_EXIT;
808 signal->group_exit_code = sig;
809 signal->group_stop_count = 0;
810 t = p;
811 do {
812 sigaddset(&t->pending.signal, SIGKILL);
813 signal_wake_up(t, 1);
814 } while_each_thread(p, t);
815 return;
820 * The signal is already in the shared-pending queue.
821 * Tell the chosen thread to wake up and dequeue it.
823 signal_wake_up(t, sig == SIGKILL);
824 return;
827 static inline int legacy_queue(struct sigpending *signals, int sig)
829 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
832 static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
833 int group, int from_ancestor_ns)
835 struct sigpending *pending;
836 struct sigqueue *q;
837 int override_rlimit;
839 trace_sched_signal_send(sig, t);
841 assert_spin_locked(&t->sighand->siglock);
843 if (!prepare_signal(sig, t, from_ancestor_ns))
844 return 0;
846 pending = group ? &t->signal->shared_pending : &t->pending;
848 * Short-circuit ignored signals and support queuing
849 * exactly one non-rt signal, so that we can get more
850 * detailed information about the cause of the signal.
852 if (legacy_queue(pending, sig))
853 return 0;
855 * fast-pathed signals for kernel-internal things like SIGSTOP
856 * or SIGKILL.
858 if (info == SEND_SIG_FORCED)
859 goto out_set;
861 /* Real-time signals must be queued if sent by sigqueue, or
862 some other real-time mechanism. It is implementation
863 defined whether kill() does so. We attempt to do so, on
864 the principle of least surprise, but since kill is not
865 allowed to fail with EAGAIN when low on memory we just
866 make sure at least one signal gets delivered and don't
867 pass on the info struct. */
869 if (sig < SIGRTMIN)
870 override_rlimit = (is_si_special(info) || info->si_code >= 0);
871 else
872 override_rlimit = 0;
874 q = __sigqueue_alloc(t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
875 override_rlimit);
876 if (q) {
877 list_add_tail(&q->list, &pending->list);
878 switch ((unsigned long) info) {
879 case (unsigned long) SEND_SIG_NOINFO:
880 q->info.si_signo = sig;
881 q->info.si_errno = 0;
882 q->info.si_code = SI_USER;
883 q->info.si_pid = task_tgid_nr_ns(current,
884 task_active_pid_ns(t));
885 q->info.si_uid = current_uid();
886 break;
887 case (unsigned long) SEND_SIG_PRIV:
888 q->info.si_signo = sig;
889 q->info.si_errno = 0;
890 q->info.si_code = SI_KERNEL;
891 q->info.si_pid = 0;
892 q->info.si_uid = 0;
893 break;
894 default:
895 copy_siginfo(&q->info, info);
896 if (from_ancestor_ns)
897 q->info.si_pid = 0;
898 break;
900 } else if (!is_si_special(info)) {
901 if (sig >= SIGRTMIN && info->si_code != SI_USER)
903 * Queue overflow, abort. We may abort if the signal was rt
904 * and sent by user using something other than kill().
906 return -EAGAIN;
909 out_set:
910 signalfd_notify(t, sig);
911 sigaddset(&pending->signal, sig);
912 complete_signal(sig, t, group);
913 return 0;
916 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
917 int group)
919 int from_ancestor_ns = 0;
921 #ifdef CONFIG_PID_NS
922 if (!is_si_special(info) && SI_FROMUSER(info) &&
923 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
924 from_ancestor_ns = 1;
925 #endif
927 return __send_signal(sig, info, t, group, from_ancestor_ns);
930 int print_fatal_signals;
932 static void print_fatal_signal(struct pt_regs *regs, int signr)
934 printk("%s/%d: potentially unexpected fatal signal %d.\n",
935 current->comm, task_pid_nr(current), signr);
937 #if defined(__i386__) && !defined(__arch_um__)
938 printk("code at %08lx: ", regs->ip);
940 int i;
941 for (i = 0; i < 16; i++) {
942 unsigned char insn;
944 if (get_user(insn, (unsigned char *)(regs->ip + i)))
945 break;
946 printk("%02x ", insn);
949 #endif
950 printk("\n");
951 preempt_disable();
952 show_regs(regs);
953 preempt_enable();
956 static int __init setup_print_fatal_signals(char *str)
958 get_option (&str, &print_fatal_signals);
960 return 1;
963 __setup("print-fatal-signals=", setup_print_fatal_signals);
966 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
968 return send_signal(sig, info, p, 1);
971 static int
972 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
974 return send_signal(sig, info, t, 0);
977 int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
978 bool group)
980 unsigned long flags;
981 int ret = -ESRCH;
983 if (lock_task_sighand(p, &flags)) {
984 ret = send_signal(sig, info, p, group);
985 unlock_task_sighand(p, &flags);
988 return ret;
992 * Force a signal that the process can't ignore: if necessary
993 * we unblock the signal and change any SIG_IGN to SIG_DFL.
995 * Note: If we unblock the signal, we always reset it to SIG_DFL,
996 * since we do not want to have a signal handler that was blocked
997 * be invoked when user space had explicitly blocked it.
999 * We don't want to have recursive SIGSEGV's etc, for example,
1000 * that is why we also clear SIGNAL_UNKILLABLE.
1003 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1005 unsigned long int flags;
1006 int ret, blocked, ignored;
1007 struct k_sigaction *action;
1009 spin_lock_irqsave(&t->sighand->siglock, flags);
1010 action = &t->sighand->action[sig-1];
1011 ignored = action->sa.sa_handler == SIG_IGN;
1012 blocked = sigismember(&t->blocked, sig);
1013 if (blocked || ignored) {
1014 action->sa.sa_handler = SIG_DFL;
1015 if (blocked) {
1016 sigdelset(&t->blocked, sig);
1017 recalc_sigpending_and_wake(t);
1020 if (action->sa.sa_handler == SIG_DFL)
1021 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1022 ret = specific_send_sig_info(sig, info, t);
1023 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1025 return ret;
1028 void
1029 force_sig_specific(int sig, struct task_struct *t)
1031 force_sig_info(sig, SEND_SIG_FORCED, t);
1035 * Nuke all other threads in the group.
1037 void zap_other_threads(struct task_struct *p)
1039 struct task_struct *t;
1041 p->signal->group_stop_count = 0;
1043 for (t = next_thread(p); t != p; t = next_thread(t)) {
1045 * Don't bother with already dead threads
1047 if (t->exit_state)
1048 continue;
1050 /* SIGKILL will be handled before any pending SIGSTOP */
1051 sigaddset(&t->pending.signal, SIGKILL);
1052 signal_wake_up(t, 1);
1056 struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1058 struct sighand_struct *sighand;
1060 rcu_read_lock();
1061 for (;;) {
1062 sighand = rcu_dereference(tsk->sighand);
1063 if (unlikely(sighand == NULL))
1064 break;
1066 spin_lock_irqsave(&sighand->siglock, *flags);
1067 if (likely(sighand == tsk->sighand))
1068 break;
1069 spin_unlock_irqrestore(&sighand->siglock, *flags);
1071 rcu_read_unlock();
1073 return sighand;
1077 * send signal info to all the members of a group
1078 * - the caller must hold the RCU read lock at least
1080 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1082 int ret = check_kill_permission(sig, info, p);
1084 if (!ret && sig)
1085 ret = do_send_sig_info(sig, info, p, true);
1087 return ret;
1091 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1092 * control characters do (^C, ^Z etc)
1093 * - the caller must hold at least a readlock on tasklist_lock
1095 int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1097 struct task_struct *p = NULL;
1098 int retval, success;
1100 success = 0;
1101 retval = -ESRCH;
1102 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1103 int err = group_send_sig_info(sig, info, p);
1104 success |= !err;
1105 retval = err;
1106 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1107 return success ? 0 : retval;
1110 int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1112 int error = -ESRCH;
1113 struct task_struct *p;
1115 rcu_read_lock();
1116 retry:
1117 p = pid_task(pid, PIDTYPE_PID);
1118 if (p) {
1119 error = group_send_sig_info(sig, info, p);
1120 if (unlikely(error == -ESRCH))
1122 * The task was unhashed in between, try again.
1123 * If it is dead, pid_task() will return NULL,
1124 * if we race with de_thread() it will find the
1125 * new leader.
1127 goto retry;
1129 rcu_read_unlock();
1131 return error;
1135 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1137 int error;
1138 rcu_read_lock();
1139 error = kill_pid_info(sig, info, find_vpid(pid));
1140 rcu_read_unlock();
1141 return error;
1144 /* like kill_pid_info(), but doesn't use uid/euid of "current" */
1145 int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
1146 uid_t uid, uid_t euid, u32 secid)
1148 int ret = -EINVAL;
1149 struct task_struct *p;
1150 const struct cred *pcred;
1152 if (!valid_signal(sig))
1153 return ret;
1155 read_lock(&tasklist_lock);
1156 p = pid_task(pid, PIDTYPE_PID);
1157 if (!p) {
1158 ret = -ESRCH;
1159 goto out_unlock;
1161 pcred = __task_cred(p);
1162 if ((info == SEND_SIG_NOINFO ||
1163 (!is_si_special(info) && SI_FROMUSER(info))) &&
1164 euid != pcred->suid && euid != pcred->uid &&
1165 uid != pcred->suid && uid != pcred->uid) {
1166 ret = -EPERM;
1167 goto out_unlock;
1169 ret = security_task_kill(p, info, sig, secid);
1170 if (ret)
1171 goto out_unlock;
1172 if (sig && p->sighand) {
1173 unsigned long flags;
1174 spin_lock_irqsave(&p->sighand->siglock, flags);
1175 ret = __send_signal(sig, info, p, 1, 0);
1176 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1178 out_unlock:
1179 read_unlock(&tasklist_lock);
1180 return ret;
1182 EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1185 * kill_something_info() interprets pid in interesting ways just like kill(2).
1187 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1188 * is probably wrong. Should make it like BSD or SYSV.
1191 static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1193 int ret;
1195 if (pid > 0) {
1196 rcu_read_lock();
1197 ret = kill_pid_info(sig, info, find_vpid(pid));
1198 rcu_read_unlock();
1199 return ret;
1202 read_lock(&tasklist_lock);
1203 if (pid != -1) {
1204 ret = __kill_pgrp_info(sig, info,
1205 pid ? find_vpid(-pid) : task_pgrp(current));
1206 } else {
1207 int retval = 0, count = 0;
1208 struct task_struct * p;
1210 for_each_process(p) {
1211 if (task_pid_vnr(p) > 1 &&
1212 !same_thread_group(p, current)) {
1213 int err = group_send_sig_info(sig, info, p);
1214 ++count;
1215 if (err != -EPERM)
1216 retval = err;
1219 ret = count ? retval : -ESRCH;
1221 read_unlock(&tasklist_lock);
1223 return ret;
1227 * These are for backward compatibility with the rest of the kernel source.
1231 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1234 * Make sure legacy kernel users don't send in bad values
1235 * (normal paths check this in check_kill_permission).
1237 if (!valid_signal(sig))
1238 return -EINVAL;
1240 return do_send_sig_info(sig, info, p, false);
1243 #define __si_special(priv) \
1244 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1247 send_sig(int sig, struct task_struct *p, int priv)
1249 return send_sig_info(sig, __si_special(priv), p);
1252 void
1253 force_sig(int sig, struct task_struct *p)
1255 force_sig_info(sig, SEND_SIG_PRIV, p);
1259 * When things go south during signal handling, we
1260 * will force a SIGSEGV. And if the signal that caused
1261 * the problem was already a SIGSEGV, we'll want to
1262 * make sure we don't even try to deliver the signal..
1265 force_sigsegv(int sig, struct task_struct *p)
1267 if (sig == SIGSEGV) {
1268 unsigned long flags;
1269 spin_lock_irqsave(&p->sighand->siglock, flags);
1270 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1271 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1273 force_sig(SIGSEGV, p);
1274 return 0;
1277 int kill_pgrp(struct pid *pid, int sig, int priv)
1279 int ret;
1281 read_lock(&tasklist_lock);
1282 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1283 read_unlock(&tasklist_lock);
1285 return ret;
1287 EXPORT_SYMBOL(kill_pgrp);
1289 int kill_pid(struct pid *pid, int sig, int priv)
1291 return kill_pid_info(sig, __si_special(priv), pid);
1293 EXPORT_SYMBOL(kill_pid);
1296 * These functions support sending signals using preallocated sigqueue
1297 * structures. This is needed "because realtime applications cannot
1298 * afford to lose notifications of asynchronous events, like timer
1299 * expirations or I/O completions". In the case of Posix Timers
1300 * we allocate the sigqueue structure from the timer_create. If this
1301 * allocation fails we are able to report the failure to the application
1302 * with an EAGAIN error.
1305 struct sigqueue *sigqueue_alloc(void)
1307 struct sigqueue *q;
1309 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1310 q->flags |= SIGQUEUE_PREALLOC;
1311 return(q);
1314 void sigqueue_free(struct sigqueue *q)
1316 unsigned long flags;
1317 spinlock_t *lock = &current->sighand->siglock;
1319 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1321 * We must hold ->siglock while testing q->list
1322 * to serialize with collect_signal() or with
1323 * __exit_signal()->flush_sigqueue().
1325 spin_lock_irqsave(lock, flags);
1326 q->flags &= ~SIGQUEUE_PREALLOC;
1328 * If it is queued it will be freed when dequeued,
1329 * like the "regular" sigqueue.
1331 if (!list_empty(&q->list))
1332 q = NULL;
1333 spin_unlock_irqrestore(lock, flags);
1335 if (q)
1336 __sigqueue_free(q);
1339 int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
1341 int sig = q->info.si_signo;
1342 struct sigpending *pending;
1343 unsigned long flags;
1344 int ret;
1346 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1348 ret = -1;
1349 if (!likely(lock_task_sighand(t, &flags)))
1350 goto ret;
1352 ret = 1; /* the signal is ignored */
1353 if (!prepare_signal(sig, t, 0))
1354 goto out;
1356 ret = 0;
1357 if (unlikely(!list_empty(&q->list))) {
1359 * If an SI_TIMER entry is already queue just increment
1360 * the overrun count.
1362 BUG_ON(q->info.si_code != SI_TIMER);
1363 q->info.si_overrun++;
1364 goto out;
1366 q->info.si_overrun = 0;
1368 signalfd_notify(t, sig);
1369 pending = group ? &t->signal->shared_pending : &t->pending;
1370 list_add_tail(&q->list, &pending->list);
1371 sigaddset(&pending->signal, sig);
1372 complete_signal(sig, t, group);
1373 out:
1374 unlock_task_sighand(t, &flags);
1375 ret:
1376 return ret;
1380 * Let a parent know about the death of a child.
1381 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1383 * Returns -1 if our parent ignored us and so we've switched to
1384 * self-reaping, or else @sig.
1386 int do_notify_parent(struct task_struct *tsk, int sig)
1388 struct siginfo info;
1389 unsigned long flags;
1390 struct sighand_struct *psig;
1391 int ret = sig;
1393 BUG_ON(sig == -1);
1395 /* do_notify_parent_cldstop should have been called instead. */
1396 BUG_ON(task_is_stopped_or_traced(tsk));
1398 BUG_ON(!task_ptrace(tsk) &&
1399 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1401 info.si_signo = sig;
1402 info.si_errno = 0;
1404 * we are under tasklist_lock here so our parent is tied to
1405 * us and cannot exit and release its namespace.
1407 * the only it can is to switch its nsproxy with sys_unshare,
1408 * bu uncharing pid namespaces is not allowed, so we'll always
1409 * see relevant namespace
1411 * write_lock() currently calls preempt_disable() which is the
1412 * same as rcu_read_lock(), but according to Oleg, this is not
1413 * correct to rely on this
1415 rcu_read_lock();
1416 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1417 info.si_uid = __task_cred(tsk)->uid;
1418 rcu_read_unlock();
1420 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1421 tsk->signal->utime));
1422 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1423 tsk->signal->stime));
1425 info.si_status = tsk->exit_code & 0x7f;
1426 if (tsk->exit_code & 0x80)
1427 info.si_code = CLD_DUMPED;
1428 else if (tsk->exit_code & 0x7f)
1429 info.si_code = CLD_KILLED;
1430 else {
1431 info.si_code = CLD_EXITED;
1432 info.si_status = tsk->exit_code >> 8;
1435 psig = tsk->parent->sighand;
1436 spin_lock_irqsave(&psig->siglock, flags);
1437 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1438 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1439 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1441 * We are exiting and our parent doesn't care. POSIX.1
1442 * defines special semantics for setting SIGCHLD to SIG_IGN
1443 * or setting the SA_NOCLDWAIT flag: we should be reaped
1444 * automatically and not left for our parent's wait4 call.
1445 * Rather than having the parent do it as a magic kind of
1446 * signal handler, we just set this to tell do_exit that we
1447 * can be cleaned up without becoming a zombie. Note that
1448 * we still call __wake_up_parent in this case, because a
1449 * blocked sys_wait4 might now return -ECHILD.
1451 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1452 * is implementation-defined: we do (if you don't want
1453 * it, just use SIG_IGN instead).
1455 ret = tsk->exit_signal = -1;
1456 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1457 sig = -1;
1459 if (valid_signal(sig) && sig > 0)
1460 __group_send_sig_info(sig, &info, tsk->parent);
1461 __wake_up_parent(tsk, tsk->parent);
1462 spin_unlock_irqrestore(&psig->siglock, flags);
1464 return ret;
1467 static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1469 struct siginfo info;
1470 unsigned long flags;
1471 struct task_struct *parent;
1472 struct sighand_struct *sighand;
1474 if (task_ptrace(tsk))
1475 parent = tsk->parent;
1476 else {
1477 tsk = tsk->group_leader;
1478 parent = tsk->real_parent;
1481 info.si_signo = SIGCHLD;
1482 info.si_errno = 0;
1484 * see comment in do_notify_parent() abot the following 3 lines
1486 rcu_read_lock();
1487 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
1488 info.si_uid = __task_cred(tsk)->uid;
1489 rcu_read_unlock();
1491 info.si_utime = cputime_to_clock_t(tsk->utime);
1492 info.si_stime = cputime_to_clock_t(tsk->stime);
1494 info.si_code = why;
1495 switch (why) {
1496 case CLD_CONTINUED:
1497 info.si_status = SIGCONT;
1498 break;
1499 case CLD_STOPPED:
1500 info.si_status = tsk->signal->group_exit_code & 0x7f;
1501 break;
1502 case CLD_TRAPPED:
1503 info.si_status = tsk->exit_code & 0x7f;
1504 break;
1505 default:
1506 BUG();
1509 sighand = parent->sighand;
1510 spin_lock_irqsave(&sighand->siglock, flags);
1511 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1512 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1513 __group_send_sig_info(SIGCHLD, &info, parent);
1515 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1517 __wake_up_parent(tsk, parent);
1518 spin_unlock_irqrestore(&sighand->siglock, flags);
1521 static inline int may_ptrace_stop(void)
1523 if (!likely(task_ptrace(current)))
1524 return 0;
1526 * Are we in the middle of do_coredump?
1527 * If so and our tracer is also part of the coredump stopping
1528 * is a deadlock situation, and pointless because our tracer
1529 * is dead so don't allow us to stop.
1530 * If SIGKILL was already sent before the caller unlocked
1531 * ->siglock we must see ->core_state != NULL. Otherwise it
1532 * is safe to enter schedule().
1534 if (unlikely(current->mm->core_state) &&
1535 unlikely(current->mm == current->parent->mm))
1536 return 0;
1538 return 1;
1542 * Return nonzero if there is a SIGKILL that should be waking us up.
1543 * Called with the siglock held.
1545 static int sigkill_pending(struct task_struct *tsk)
1547 return sigismember(&tsk->pending.signal, SIGKILL) ||
1548 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1552 * This must be called with current->sighand->siglock held.
1554 * This should be the path for all ptrace stops.
1555 * We always set current->last_siginfo while stopped here.
1556 * That makes it a way to test a stopped process for
1557 * being ptrace-stopped vs being job-control-stopped.
1559 * If we actually decide not to stop at all because the tracer
1560 * is gone, we keep current->exit_code unless clear_code.
1562 static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1564 if (arch_ptrace_stop_needed(exit_code, info)) {
1566 * The arch code has something special to do before a
1567 * ptrace stop. This is allowed to block, e.g. for faults
1568 * on user stack pages. We can't keep the siglock while
1569 * calling arch_ptrace_stop, so we must release it now.
1570 * To preserve proper semantics, we must do this before
1571 * any signal bookkeeping like checking group_stop_count.
1572 * Meanwhile, a SIGKILL could come in before we retake the
1573 * siglock. That must prevent us from sleeping in TASK_TRACED.
1574 * So after regaining the lock, we must check for SIGKILL.
1576 spin_unlock_irq(&current->sighand->siglock);
1577 arch_ptrace_stop(exit_code, info);
1578 spin_lock_irq(&current->sighand->siglock);
1579 if (sigkill_pending(current))
1580 return;
1584 * If there is a group stop in progress,
1585 * we must participate in the bookkeeping.
1587 if (current->signal->group_stop_count > 0)
1588 --current->signal->group_stop_count;
1590 current->last_siginfo = info;
1591 current->exit_code = exit_code;
1593 /* Let the debugger run. */
1594 __set_current_state(TASK_TRACED);
1595 spin_unlock_irq(&current->sighand->siglock);
1596 read_lock(&tasklist_lock);
1597 if (may_ptrace_stop()) {
1598 do_notify_parent_cldstop(current, CLD_TRAPPED);
1600 * Don't want to allow preemption here, because
1601 * sys_ptrace() needs this task to be inactive.
1603 * XXX: implement read_unlock_no_resched().
1605 preempt_disable();
1606 read_unlock(&tasklist_lock);
1607 preempt_enable_no_resched();
1608 schedule();
1609 } else {
1611 * By the time we got the lock, our tracer went away.
1612 * Don't drop the lock yet, another tracer may come.
1614 __set_current_state(TASK_RUNNING);
1615 if (clear_code)
1616 current->exit_code = 0;
1617 read_unlock(&tasklist_lock);
1621 * While in TASK_TRACED, we were considered "frozen enough".
1622 * Now that we woke up, it's crucial if we're supposed to be
1623 * frozen that we freeze now before running anything substantial.
1625 try_to_freeze();
1628 * We are back. Now reacquire the siglock before touching
1629 * last_siginfo, so that we are sure to have synchronized with
1630 * any signal-sending on another CPU that wants to examine it.
1632 spin_lock_irq(&current->sighand->siglock);
1633 current->last_siginfo = NULL;
1636 * Queued signals ignored us while we were stopped for tracing.
1637 * So check for any that we should take before resuming user mode.
1638 * This sets TIF_SIGPENDING, but never clears it.
1640 recalc_sigpending_tsk(current);
1643 void ptrace_notify(int exit_code)
1645 siginfo_t info;
1647 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1649 memset(&info, 0, sizeof info);
1650 info.si_signo = SIGTRAP;
1651 info.si_code = exit_code;
1652 info.si_pid = task_pid_vnr(current);
1653 info.si_uid = current_uid();
1655 /* Let the debugger run. */
1656 spin_lock_irq(&current->sighand->siglock);
1657 ptrace_stop(exit_code, 1, &info);
1658 spin_unlock_irq(&current->sighand->siglock);
1662 * This performs the stopping for SIGSTOP and other stop signals.
1663 * We have to stop all threads in the thread group.
1664 * Returns nonzero if we've actually stopped and released the siglock.
1665 * Returns zero if we didn't stop and still hold the siglock.
1667 static int do_signal_stop(int signr)
1669 struct signal_struct *sig = current->signal;
1670 int notify;
1672 if (!sig->group_stop_count) {
1673 struct task_struct *t;
1675 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
1676 unlikely(signal_group_exit(sig)))
1677 return 0;
1679 * There is no group stop already in progress.
1680 * We must initiate one now.
1682 sig->group_exit_code = signr;
1684 sig->group_stop_count = 1;
1685 for (t = next_thread(current); t != current; t = next_thread(t))
1687 * Setting state to TASK_STOPPED for a group
1688 * stop is always done with the siglock held,
1689 * so this check has no races.
1691 if (!(t->flags & PF_EXITING) &&
1692 !task_is_stopped_or_traced(t)) {
1693 sig->group_stop_count++;
1694 signal_wake_up(t, 0);
1698 * If there are no other threads in the group, or if there is
1699 * a group stop in progress and we are the last to stop, report
1700 * to the parent. When ptraced, every thread reports itself.
1702 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1703 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1705 * tracehook_notify_jctl() can drop and reacquire siglock, so
1706 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1707 * or SIGKILL comes in between ->group_stop_count == 0.
1709 if (sig->group_stop_count) {
1710 if (!--sig->group_stop_count)
1711 sig->flags = SIGNAL_STOP_STOPPED;
1712 current->exit_code = sig->group_exit_code;
1713 __set_current_state(TASK_STOPPED);
1715 spin_unlock_irq(&current->sighand->siglock);
1717 if (notify) {
1718 read_lock(&tasklist_lock);
1719 do_notify_parent_cldstop(current, notify);
1720 read_unlock(&tasklist_lock);
1723 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1724 do {
1725 schedule();
1726 } while (try_to_freeze());
1728 tracehook_finish_jctl();
1729 current->exit_code = 0;
1731 return 1;
1734 static int ptrace_signal(int signr, siginfo_t *info,
1735 struct pt_regs *regs, void *cookie)
1737 if (!task_ptrace(current))
1738 return signr;
1740 ptrace_signal_deliver(regs, cookie);
1742 /* Let the debugger run. */
1743 ptrace_stop(signr, 0, info);
1745 /* We're back. Did the debugger cancel the sig? */
1746 signr = current->exit_code;
1747 if (signr == 0)
1748 return signr;
1750 current->exit_code = 0;
1752 /* Update the siginfo structure if the signal has
1753 changed. If the debugger wanted something
1754 specific in the siginfo structure then it should
1755 have updated *info via PTRACE_SETSIGINFO. */
1756 if (signr != info->si_signo) {
1757 info->si_signo = signr;
1758 info->si_errno = 0;
1759 info->si_code = SI_USER;
1760 info->si_pid = task_pid_vnr(current->parent);
1761 info->si_uid = task_uid(current->parent);
1764 /* If the (new) signal is now blocked, requeue it. */
1765 if (sigismember(&current->blocked, signr)) {
1766 specific_send_sig_info(signr, info, current);
1767 signr = 0;
1770 return signr;
1773 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1774 struct pt_regs *regs, void *cookie)
1776 struct sighand_struct *sighand = current->sighand;
1777 struct signal_struct *signal = current->signal;
1778 int signr;
1780 relock:
1782 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1783 * While in TASK_STOPPED, we were considered "frozen enough".
1784 * Now that we woke up, it's crucial if we're supposed to be
1785 * frozen that we freeze now before running anything substantial.
1787 try_to_freeze();
1789 spin_lock_irq(&sighand->siglock);
1791 * Every stopped thread goes here after wakeup. Check to see if
1792 * we should notify the parent, prepare_signal(SIGCONT) encodes
1793 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1795 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1796 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
1797 ? CLD_CONTINUED : CLD_STOPPED;
1798 signal->flags &= ~SIGNAL_CLD_MASK;
1800 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1801 spin_unlock_irq(&sighand->siglock);
1803 if (why) {
1804 read_lock(&tasklist_lock);
1805 do_notify_parent_cldstop(current->group_leader, why);
1806 read_unlock(&tasklist_lock);
1808 goto relock;
1811 for (;;) {
1812 struct k_sigaction *ka;
1814 if (unlikely(signal->group_stop_count > 0) &&
1815 do_signal_stop(0))
1816 goto relock;
1819 * Tracing can induce an artifical signal and choose sigaction.
1820 * The return value in @signr determines the default action,
1821 * but @info->si_signo is the signal number we will report.
1823 signr = tracehook_get_signal(current, regs, info, return_ka);
1824 if (unlikely(signr < 0))
1825 goto relock;
1826 if (unlikely(signr != 0))
1827 ka = return_ka;
1828 else {
1829 signr = dequeue_signal(current, &current->blocked,
1830 info);
1832 if (!signr)
1833 break; /* will return 0 */
1835 if (signr != SIGKILL) {
1836 signr = ptrace_signal(signr, info,
1837 regs, cookie);
1838 if (!signr)
1839 continue;
1842 ka = &sighand->action[signr-1];
1845 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1846 continue;
1847 if (ka->sa.sa_handler != SIG_DFL) {
1848 /* Run the handler. */
1849 *return_ka = *ka;
1851 if (ka->sa.sa_flags & SA_ONESHOT)
1852 ka->sa.sa_handler = SIG_DFL;
1854 break; /* will return non-zero "signr" value */
1858 * Now we are doing the default action for this signal.
1860 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1861 continue;
1864 * Global init gets no signals it doesn't want.
1865 * Container-init gets no signals it doesn't want from same
1866 * container.
1868 * Note that if global/container-init sees a sig_kernel_only()
1869 * signal here, the signal must have been generated internally
1870 * or must have come from an ancestor namespace. In either
1871 * case, the signal cannot be dropped.
1873 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
1874 !sig_kernel_only(signr))
1875 continue;
1877 if (sig_kernel_stop(signr)) {
1879 * The default action is to stop all threads in
1880 * the thread group. The job control signals
1881 * do nothing in an orphaned pgrp, but SIGSTOP
1882 * always works. Note that siglock needs to be
1883 * dropped during the call to is_orphaned_pgrp()
1884 * because of lock ordering with tasklist_lock.
1885 * This allows an intervening SIGCONT to be posted.
1886 * We need to check for that and bail out if necessary.
1888 if (signr != SIGSTOP) {
1889 spin_unlock_irq(&sighand->siglock);
1891 /* signals can be posted during this window */
1893 if (is_current_pgrp_orphaned())
1894 goto relock;
1896 spin_lock_irq(&sighand->siglock);
1899 if (likely(do_signal_stop(info->si_signo))) {
1900 /* It released the siglock. */
1901 goto relock;
1905 * We didn't actually stop, due to a race
1906 * with SIGCONT or something like that.
1908 continue;
1911 spin_unlock_irq(&sighand->siglock);
1914 * Anything else is fatal, maybe with a core dump.
1916 current->flags |= PF_SIGNALED;
1918 if (sig_kernel_coredump(signr)) {
1919 if (print_fatal_signals)
1920 print_fatal_signal(regs, info->si_signo);
1922 * If it was able to dump core, this kills all
1923 * other threads in the group and synchronizes with
1924 * their demise. If we lost the race with another
1925 * thread getting here, it set group_exit_code
1926 * first and our do_group_exit call below will use
1927 * that value and ignore the one we pass it.
1929 do_coredump(info->si_signo, info->si_signo, regs);
1933 * Death signals, no core dump.
1935 do_group_exit(info->si_signo);
1936 /* NOTREACHED */
1938 spin_unlock_irq(&sighand->siglock);
1939 return signr;
1942 void exit_signals(struct task_struct *tsk)
1944 int group_stop = 0;
1945 struct task_struct *t;
1947 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1948 tsk->flags |= PF_EXITING;
1949 return;
1952 spin_lock_irq(&tsk->sighand->siglock);
1954 * From now this task is not visible for group-wide signals,
1955 * see wants_signal(), do_signal_stop().
1957 tsk->flags |= PF_EXITING;
1958 if (!signal_pending(tsk))
1959 goto out;
1961 /* It could be that __group_complete_signal() choose us to
1962 * notify about group-wide signal. Another thread should be
1963 * woken now to take the signal since we will not.
1965 for (t = tsk; (t = next_thread(t)) != tsk; )
1966 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1967 recalc_sigpending_and_wake(t);
1969 if (unlikely(tsk->signal->group_stop_count) &&
1970 !--tsk->signal->group_stop_count) {
1971 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1972 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
1974 out:
1975 spin_unlock_irq(&tsk->sighand->siglock);
1977 if (unlikely(group_stop)) {
1978 read_lock(&tasklist_lock);
1979 do_notify_parent_cldstop(tsk, group_stop);
1980 read_unlock(&tasklist_lock);
1984 EXPORT_SYMBOL(recalc_sigpending);
1985 EXPORT_SYMBOL_GPL(dequeue_signal);
1986 EXPORT_SYMBOL(flush_signals);
1987 EXPORT_SYMBOL(force_sig);
1988 EXPORT_SYMBOL(send_sig);
1989 EXPORT_SYMBOL(send_sig_info);
1990 EXPORT_SYMBOL(sigprocmask);
1991 EXPORT_SYMBOL(block_all_signals);
1992 EXPORT_SYMBOL(unblock_all_signals);
1996 * System call entry points.
1999 SYSCALL_DEFINE0(restart_syscall)
2001 struct restart_block *restart = &current_thread_info()->restart_block;
2002 return restart->fn(restart);
2005 long do_no_restart_syscall(struct restart_block *param)
2007 return -EINTR;
2011 * We don't need to get the kernel lock - this is all local to this
2012 * particular thread.. (and that's good, because this is _heavily_
2013 * used by various programs)
2017 * This is also useful for kernel threads that want to temporarily
2018 * (or permanently) block certain signals.
2020 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2021 * interface happily blocks "unblockable" signals like SIGKILL
2022 * and friends.
2024 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2026 int error;
2028 spin_lock_irq(&current->sighand->siglock);
2029 if (oldset)
2030 *oldset = current->blocked;
2032 error = 0;
2033 switch (how) {
2034 case SIG_BLOCK:
2035 sigorsets(&current->blocked, &current->blocked, set);
2036 break;
2037 case SIG_UNBLOCK:
2038 signandsets(&current->blocked, &current->blocked, set);
2039 break;
2040 case SIG_SETMASK:
2041 current->blocked = *set;
2042 break;
2043 default:
2044 error = -EINVAL;
2046 recalc_sigpending();
2047 spin_unlock_irq(&current->sighand->siglock);
2049 return error;
2052 SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2053 sigset_t __user *, oset, size_t, sigsetsize)
2055 int error = -EINVAL;
2056 sigset_t old_set, new_set;
2058 /* XXX: Don't preclude handling different sized sigset_t's. */
2059 if (sigsetsize != sizeof(sigset_t))
2060 goto out;
2062 if (set) {
2063 error = -EFAULT;
2064 if (copy_from_user(&new_set, set, sizeof(*set)))
2065 goto out;
2066 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2068 error = sigprocmask(how, &new_set, &old_set);
2069 if (error)
2070 goto out;
2071 if (oset)
2072 goto set_old;
2073 } else if (oset) {
2074 spin_lock_irq(&current->sighand->siglock);
2075 old_set = current->blocked;
2076 spin_unlock_irq(&current->sighand->siglock);
2078 set_old:
2079 error = -EFAULT;
2080 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2081 goto out;
2083 error = 0;
2084 out:
2085 return error;
2088 long do_sigpending(void __user *set, unsigned long sigsetsize)
2090 long error = -EINVAL;
2091 sigset_t pending;
2093 if (sigsetsize > sizeof(sigset_t))
2094 goto out;
2096 spin_lock_irq(&current->sighand->siglock);
2097 sigorsets(&pending, &current->pending.signal,
2098 &current->signal->shared_pending.signal);
2099 spin_unlock_irq(&current->sighand->siglock);
2101 /* Outside the lock because only this thread touches it. */
2102 sigandsets(&pending, &current->blocked, &pending);
2104 error = -EFAULT;
2105 if (!copy_to_user(set, &pending, sigsetsize))
2106 error = 0;
2108 out:
2109 return error;
2112 SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
2114 return do_sigpending(set, sigsetsize);
2117 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2119 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2121 int err;
2123 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2124 return -EFAULT;
2125 if (from->si_code < 0)
2126 return __copy_to_user(to, from, sizeof(siginfo_t))
2127 ? -EFAULT : 0;
2129 * If you change siginfo_t structure, please be sure
2130 * this code is fixed accordingly.
2131 * Please remember to update the signalfd_copyinfo() function
2132 * inside fs/signalfd.c too, in case siginfo_t changes.
2133 * It should never copy any pad contained in the structure
2134 * to avoid security leaks, but must copy the generic
2135 * 3 ints plus the relevant union member.
2137 err = __put_user(from->si_signo, &to->si_signo);
2138 err |= __put_user(from->si_errno, &to->si_errno);
2139 err |= __put_user((short)from->si_code, &to->si_code);
2140 switch (from->si_code & __SI_MASK) {
2141 case __SI_KILL:
2142 err |= __put_user(from->si_pid, &to->si_pid);
2143 err |= __put_user(from->si_uid, &to->si_uid);
2144 break;
2145 case __SI_TIMER:
2146 err |= __put_user(from->si_tid, &to->si_tid);
2147 err |= __put_user(from->si_overrun, &to->si_overrun);
2148 err |= __put_user(from->si_ptr, &to->si_ptr);
2149 break;
2150 case __SI_POLL:
2151 err |= __put_user(from->si_band, &to->si_band);
2152 err |= __put_user(from->si_fd, &to->si_fd);
2153 break;
2154 case __SI_FAULT:
2155 err |= __put_user(from->si_addr, &to->si_addr);
2156 #ifdef __ARCH_SI_TRAPNO
2157 err |= __put_user(from->si_trapno, &to->si_trapno);
2158 #endif
2159 break;
2160 case __SI_CHLD:
2161 err |= __put_user(from->si_pid, &to->si_pid);
2162 err |= __put_user(from->si_uid, &to->si_uid);
2163 err |= __put_user(from->si_status, &to->si_status);
2164 err |= __put_user(from->si_utime, &to->si_utime);
2165 err |= __put_user(from->si_stime, &to->si_stime);
2166 break;
2167 case __SI_RT: /* This is not generated by the kernel as of now. */
2168 case __SI_MESGQ: /* But this is */
2169 err |= __put_user(from->si_pid, &to->si_pid);
2170 err |= __put_user(from->si_uid, &to->si_uid);
2171 err |= __put_user(from->si_ptr, &to->si_ptr);
2172 break;
2173 default: /* this is just in case for now ... */
2174 err |= __put_user(from->si_pid, &to->si_pid);
2175 err |= __put_user(from->si_uid, &to->si_uid);
2176 break;
2178 return err;
2181 #endif
2183 SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2184 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2185 size_t, sigsetsize)
2187 int ret, sig;
2188 sigset_t these;
2189 struct timespec ts;
2190 siginfo_t info;
2191 long timeout = 0;
2193 /* XXX: Don't preclude handling different sized sigset_t's. */
2194 if (sigsetsize != sizeof(sigset_t))
2195 return -EINVAL;
2197 if (copy_from_user(&these, uthese, sizeof(these)))
2198 return -EFAULT;
2201 * Invert the set of allowed signals to get those we
2202 * want to block.
2204 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2205 signotset(&these);
2207 if (uts) {
2208 if (copy_from_user(&ts, uts, sizeof(ts)))
2209 return -EFAULT;
2210 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2211 || ts.tv_sec < 0)
2212 return -EINVAL;
2215 spin_lock_irq(&current->sighand->siglock);
2216 sig = dequeue_signal(current, &these, &info);
2217 if (!sig) {
2218 timeout = MAX_SCHEDULE_TIMEOUT;
2219 if (uts)
2220 timeout = (timespec_to_jiffies(&ts)
2221 + (ts.tv_sec || ts.tv_nsec));
2223 if (timeout) {
2224 /* None ready -- temporarily unblock those we're
2225 * interested while we are sleeping in so that we'll
2226 * be awakened when they arrive. */
2227 current->real_blocked = current->blocked;
2228 sigandsets(&current->blocked, &current->blocked, &these);
2229 recalc_sigpending();
2230 spin_unlock_irq(&current->sighand->siglock);
2232 timeout = schedule_timeout_interruptible(timeout);
2234 spin_lock_irq(&current->sighand->siglock);
2235 sig = dequeue_signal(current, &these, &info);
2236 current->blocked = current->real_blocked;
2237 siginitset(&current->real_blocked, 0);
2238 recalc_sigpending();
2241 spin_unlock_irq(&current->sighand->siglock);
2243 if (sig) {
2244 ret = sig;
2245 if (uinfo) {
2246 if (copy_siginfo_to_user(uinfo, &info))
2247 ret = -EFAULT;
2249 } else {
2250 ret = -EAGAIN;
2251 if (timeout)
2252 ret = -EINTR;
2255 return ret;
2258 SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
2260 struct siginfo info;
2262 info.si_signo = sig;
2263 info.si_errno = 0;
2264 info.si_code = SI_USER;
2265 info.si_pid = task_tgid_vnr(current);
2266 info.si_uid = current_uid();
2268 return kill_something_info(sig, &info, pid);
2271 static int
2272 do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
2274 struct task_struct *p;
2275 int error = -ESRCH;
2277 rcu_read_lock();
2278 p = find_task_by_vpid(pid);
2279 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
2280 error = check_kill_permission(sig, info, p);
2282 * The null signal is a permissions and process existence
2283 * probe. No signal is actually delivered.
2285 if (!error && sig) {
2286 error = do_send_sig_info(sig, info, p, false);
2288 * If lock_task_sighand() failed we pretend the task
2289 * dies after receiving the signal. The window is tiny,
2290 * and the signal is private anyway.
2292 if (unlikely(error == -ESRCH))
2293 error = 0;
2296 rcu_read_unlock();
2298 return error;
2301 static int do_tkill(pid_t tgid, pid_t pid, int sig)
2303 struct siginfo info;
2305 info.si_signo = sig;
2306 info.si_errno = 0;
2307 info.si_code = SI_TKILL;
2308 info.si_pid = task_tgid_vnr(current);
2309 info.si_uid = current_uid();
2311 return do_send_specific(tgid, pid, sig, &info);
2315 * sys_tgkill - send signal to one specific thread
2316 * @tgid: the thread group ID of the thread
2317 * @pid: the PID of the thread
2318 * @sig: signal to be sent
2320 * This syscall also checks the @tgid and returns -ESRCH even if the PID
2321 * exists but it's not belonging to the target process anymore. This
2322 * method solves the problem of threads exiting and PIDs getting reused.
2324 SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
2326 /* This is only valid for single tasks */
2327 if (pid <= 0 || tgid <= 0)
2328 return -EINVAL;
2330 return do_tkill(tgid, pid, sig);
2334 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2336 SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
2338 /* This is only valid for single tasks */
2339 if (pid <= 0)
2340 return -EINVAL;
2342 return do_tkill(0, pid, sig);
2345 SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2346 siginfo_t __user *, uinfo)
2348 siginfo_t info;
2350 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2351 return -EFAULT;
2353 /* Not even root can pretend to send signals from the kernel.
2354 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2356 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
2357 /* We used to allow any < 0 si_code */
2358 WARN_ON_ONCE(info.si_code < 0);
2359 return -EPERM;
2361 info.si_signo = sig;
2363 /* POSIX.1b doesn't mention process groups. */
2364 return kill_proc_info(sig, &info, pid);
2367 long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2369 /* This is only valid for single tasks */
2370 if (pid <= 0 || tgid <= 0)
2371 return -EINVAL;
2373 /* Not even root can pretend to send signals from the kernel.
2374 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2376 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
2377 /* We used to allow any < 0 si_code */
2378 WARN_ON_ONCE(info->si_code < 0);
2379 return -EPERM;
2381 info->si_signo = sig;
2383 return do_send_specific(tgid, pid, sig, info);
2386 SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2387 siginfo_t __user *, uinfo)
2389 siginfo_t info;
2391 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2392 return -EFAULT;
2394 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2397 int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2399 struct task_struct *t = current;
2400 struct k_sigaction *k;
2401 sigset_t mask;
2403 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2404 return -EINVAL;
2406 k = &t->sighand->action[sig-1];
2408 spin_lock_irq(&current->sighand->siglock);
2409 if (oact)
2410 *oact = *k;
2412 if (act) {
2413 sigdelsetmask(&act->sa.sa_mask,
2414 sigmask(SIGKILL) | sigmask(SIGSTOP));
2415 *k = *act;
2417 * POSIX 3.3.1.3:
2418 * "Setting a signal action to SIG_IGN for a signal that is
2419 * pending shall cause the pending signal to be discarded,
2420 * whether or not it is blocked."
2422 * "Setting a signal action to SIG_DFL for a signal that is
2423 * pending and whose default action is to ignore the signal
2424 * (for example, SIGCHLD), shall cause the pending signal to
2425 * be discarded, whether or not it is blocked"
2427 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
2428 sigemptyset(&mask);
2429 sigaddset(&mask, sig);
2430 rm_from_queue_full(&mask, &t->signal->shared_pending);
2431 do {
2432 rm_from_queue_full(&mask, &t->pending);
2433 t = next_thread(t);
2434 } while (t != current);
2438 spin_unlock_irq(&current->sighand->siglock);
2439 return 0;
2442 int
2443 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2445 stack_t oss;
2446 int error;
2448 oss.ss_sp = (void __user *) current->sas_ss_sp;
2449 oss.ss_size = current->sas_ss_size;
2450 oss.ss_flags = sas_ss_flags(sp);
2452 if (uss) {
2453 void __user *ss_sp;
2454 size_t ss_size;
2455 int ss_flags;
2457 error = -EFAULT;
2458 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2459 goto out;
2460 error = __get_user(ss_sp, &uss->ss_sp) |
2461 __get_user(ss_flags, &uss->ss_flags) |
2462 __get_user(ss_size, &uss->ss_size);
2463 if (error)
2464 goto out;
2466 error = -EPERM;
2467 if (on_sig_stack(sp))
2468 goto out;
2470 error = -EINVAL;
2473 * Note - this code used to test ss_flags incorrectly
2474 * old code may have been written using ss_flags==0
2475 * to mean ss_flags==SS_ONSTACK (as this was the only
2476 * way that worked) - this fix preserves that older
2477 * mechanism
2479 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2480 goto out;
2482 if (ss_flags == SS_DISABLE) {
2483 ss_size = 0;
2484 ss_sp = NULL;
2485 } else {
2486 error = -ENOMEM;
2487 if (ss_size < MINSIGSTKSZ)
2488 goto out;
2491 current->sas_ss_sp = (unsigned long) ss_sp;
2492 current->sas_ss_size = ss_size;
2495 error = 0;
2496 if (uoss) {
2497 error = -EFAULT;
2498 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
2499 goto out;
2500 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2501 __put_user(oss.ss_size, &uoss->ss_size) |
2502 __put_user(oss.ss_flags, &uoss->ss_flags);
2505 out:
2506 return error;
2509 #ifdef __ARCH_WANT_SYS_SIGPENDING
2511 SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
2513 return do_sigpending(set, sizeof(*set));
2516 #endif
2518 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2519 /* Some platforms have their own version with special arguments others
2520 support only sys_rt_sigprocmask. */
2522 SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2523 old_sigset_t __user *, oset)
2525 int error;
2526 old_sigset_t old_set, new_set;
2528 if (set) {
2529 error = -EFAULT;
2530 if (copy_from_user(&new_set, set, sizeof(*set)))
2531 goto out;
2532 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2534 spin_lock_irq(&current->sighand->siglock);
2535 old_set = current->blocked.sig[0];
2537 error = 0;
2538 switch (how) {
2539 default:
2540 error = -EINVAL;
2541 break;
2542 case SIG_BLOCK:
2543 sigaddsetmask(&current->blocked, new_set);
2544 break;
2545 case SIG_UNBLOCK:
2546 sigdelsetmask(&current->blocked, new_set);
2547 break;
2548 case SIG_SETMASK:
2549 current->blocked.sig[0] = new_set;
2550 break;
2553 recalc_sigpending();
2554 spin_unlock_irq(&current->sighand->siglock);
2555 if (error)
2556 goto out;
2557 if (oset)
2558 goto set_old;
2559 } else if (oset) {
2560 old_set = current->blocked.sig[0];
2561 set_old:
2562 error = -EFAULT;
2563 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2564 goto out;
2566 error = 0;
2567 out:
2568 return error;
2570 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2572 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2573 SYSCALL_DEFINE4(rt_sigaction, int, sig,
2574 const struct sigaction __user *, act,
2575 struct sigaction __user *, oact,
2576 size_t, sigsetsize)
2578 struct k_sigaction new_sa, old_sa;
2579 int ret = -EINVAL;
2581 /* XXX: Don't preclude handling different sized sigset_t's. */
2582 if (sigsetsize != sizeof(sigset_t))
2583 goto out;
2585 if (act) {
2586 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2587 return -EFAULT;
2590 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2592 if (!ret && oact) {
2593 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2594 return -EFAULT;
2596 out:
2597 return ret;
2599 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2601 #ifdef __ARCH_WANT_SYS_SGETMASK
2604 * For backwards compatibility. Functionality superseded by sigprocmask.
2606 SYSCALL_DEFINE0(sgetmask)
2608 /* SMP safe */
2609 return current->blocked.sig[0];
2612 SYSCALL_DEFINE1(ssetmask, int, newmask)
2614 int old;
2616 spin_lock_irq(&current->sighand->siglock);
2617 old = current->blocked.sig[0];
2619 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2620 sigmask(SIGSTOP)));
2621 recalc_sigpending();
2622 spin_unlock_irq(&current->sighand->siglock);
2624 return old;
2626 #endif /* __ARCH_WANT_SGETMASK */
2628 #ifdef __ARCH_WANT_SYS_SIGNAL
2630 * For backwards compatibility. Functionality superseded by sigaction.
2632 SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
2634 struct k_sigaction new_sa, old_sa;
2635 int ret;
2637 new_sa.sa.sa_handler = handler;
2638 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2639 sigemptyset(&new_sa.sa.sa_mask);
2641 ret = do_sigaction(sig, &new_sa, &old_sa);
2643 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2645 #endif /* __ARCH_WANT_SYS_SIGNAL */
2647 #ifdef __ARCH_WANT_SYS_PAUSE
2649 SYSCALL_DEFINE0(pause)
2651 current->state = TASK_INTERRUPTIBLE;
2652 schedule();
2653 return -ERESTARTNOHAND;
2656 #endif
2658 #ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2659 SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
2661 sigset_t newset;
2663 /* XXX: Don't preclude handling different sized sigset_t's. */
2664 if (sigsetsize != sizeof(sigset_t))
2665 return -EINVAL;
2667 if (copy_from_user(&newset, unewset, sizeof(newset)))
2668 return -EFAULT;
2669 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2671 spin_lock_irq(&current->sighand->siglock);
2672 current->saved_sigmask = current->blocked;
2673 current->blocked = newset;
2674 recalc_sigpending();
2675 spin_unlock_irq(&current->sighand->siglock);
2677 current->state = TASK_INTERRUPTIBLE;
2678 schedule();
2679 set_restore_sigmask();
2680 return -ERESTARTNOHAND;
2682 #endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2684 __attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2686 return NULL;
2689 void __init signals_init(void)
2691 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);