blob: ba3da25f0eea2e980863960da8bdcbfe5466290a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070024#include <linux/signalfd.h>
Roland McGrath35de2542008-07-25 19:45:51 -070025#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080028#include <linux/pid_namespace.h>
29#include <linux/nsproxy.h>
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -040030#include <trace/sched.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/param.h>
33#include <asm/uaccess.h>
34#include <asm/unistd.h>
35#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040036#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * SLAB caches for signal bits.
40 */
41
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Mathieu Desnoyers7e066fb2008-11-14 17:47:47 -050044DEFINE_TRACE(sched_signal_send);
45
Roland McGrath35de2542008-07-25 19:45:51 -070046static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070047{
Roland McGrath35de2542008-07-25 19:45:51 -070048 return t->sighand->action[sig - 1].sa.sa_handler;
49}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070050
Roland McGrath35de2542008-07-25 19:45:51 -070051static int sig_handler_ignored(void __user *handler, int sig)
52{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070053 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070054 return handler == SIG_IGN ||
55 (handler == SIG_DFL && sig_kernel_ignore(sig));
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070058static int sig_task_ignored(struct task_struct *t, int sig,
59 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Roland McGrath35de2542008-07-25 19:45:51 -070061 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Oleg Nesterovf008faf2009-04-02 16:58:02 -070063 handler = sig_handler(t, sig);
64
65 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070066 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070067 return 1;
68
69 return sig_handler_ignored(handler, sig);
70}
71
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070072static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070073{
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /*
75 * Blocked signals are never ignored, since the
76 * signal handler may change by the time it is
77 * unblocked.
78 */
Roland McGrath325d22d2007-11-12 15:41:55 -080079 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 return 0;
81
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070082 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070083 return 0;
84
85 /*
86 * Tracers may want to know about even ignored signals.
87 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070088 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/*
92 * Re-calculate pending state from the set of locally pending
93 * signals, globally pending signals, and blocked signals.
94 */
95static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
96{
97 unsigned long ready;
98 long i;
99
100 switch (_NSIG_WORDS) {
101 default:
102 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
103 ready |= signal->sig[i] &~ blocked->sig[i];
104 break;
105
106 case 4: ready = signal->sig[3] &~ blocked->sig[3];
107 ready |= signal->sig[2] &~ blocked->sig[2];
108 ready |= signal->sig[1] &~ blocked->sig[1];
109 ready |= signal->sig[0] &~ blocked->sig[0];
110 break;
111
112 case 2: ready = signal->sig[1] &~ blocked->sig[1];
113 ready |= signal->sig[0] &~ blocked->sig[0];
114 break;
115
116 case 1: ready = signal->sig[0] &~ blocked->sig[0];
117 }
118 return ready != 0;
119}
120
121#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
122
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700123static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 if (t->signal->group_stop_count > 0 ||
126 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700127 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700129 return 1;
130 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700131 /*
132 * We must never clear the flag in another thread, or in current
133 * when it's possible the current syscall is returning -ERESTART*.
134 * So we don't clear it here, and only callers who know they should do.
135 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700136 return 0;
137}
138
139/*
140 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
141 * This is superfluous when called on current, the wakeup is a harmless no-op.
142 */
143void recalc_sigpending_and_wake(struct task_struct *t)
144{
145 if (recalc_sigpending_tsk(t))
146 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149void recalc_sigpending(void)
150{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700151 if (unlikely(tracehook_force_sigpending()))
152 set_thread_flag(TIF_SIGPENDING);
153 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700154 clear_thread_flag(TIF_SIGPENDING);
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/* Given the mask, find the first available signal that should be serviced. */
159
Davide Libenzifba2afa2007-05-10 22:23:13 -0700160int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 unsigned long i, *s, *m, x;
163 int sig = 0;
164
165 s = pending->signal.sig;
166 m = mask->sig;
167 switch (_NSIG_WORDS) {
168 default:
169 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
170 if ((x = *s &~ *m) != 0) {
171 sig = ffz(~x) + i*_NSIG_BPW + 1;
172 break;
173 }
174 break;
175
176 case 2: if ((x = s[0] &~ m[0]) != 0)
177 sig = 1;
178 else if ((x = s[1] &~ m[1]) != 0)
179 sig = _NSIG_BPW + 1;
180 else
181 break;
182 sig += ffz(~x);
183 break;
184
185 case 1: if ((x = *s &~ *m) != 0)
186 sig = ffz(~x) + 1;
187 break;
188 }
189
190 return sig;
191}
192
David Howellsc69e8d92008-11-14 10:39:19 +1100193/*
194 * allocate a new signal queue record
195 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100196 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100197 */
Al Virodd0fc662005-10-07 07:46:04 +0100198static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int override_rlimit)
200{
201 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800202 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800204 /*
David Howellsc69e8d92008-11-14 10:39:19 +1100205 * We won't get problems with the target's UID changing under us
206 * because changing it requires RCU be used, and if t != current, the
207 * caller must be holding the RCU readlock (by way of a spinlock) and
208 * we use RCU protection here
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800209 */
David Howellsd84f4f92008-11-14 10:39:23 +1100210 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800211 atomic_inc(&user->sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800213 atomic_read(&user->sigpending) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
215 q = kmem_cache_alloc(sigqueue_cachep, flags);
216 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800217 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100218 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 } else {
220 INIT_LIST_HEAD(&q->list);
221 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100222 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
David Howellsd84f4f92008-11-14 10:39:23 +1100224
225 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Andrew Morton514a01b2006-02-03 03:04:41 -0800228static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (q->flags & SIGQUEUE_PREALLOC)
231 return;
232 atomic_dec(&q->user->sigpending);
233 free_uid(q->user);
234 kmem_cache_free(sigqueue_cachep, q);
235}
236
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800237void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct sigqueue *q;
240
241 sigemptyset(&queue->signal);
242 while (!list_empty(&queue->list)) {
243 q = list_entry(queue->list.next, struct sigqueue , list);
244 list_del_init(&q->list);
245 __sigqueue_free(q);
246 }
247}
248
249/*
250 * Flush all pending signals for a task.
251 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800252void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 unsigned long flags;
255
256 spin_lock_irqsave(&t->sighand->siglock, flags);
Pavel Machekf5264482008-04-21 22:15:06 +0000257 clear_tsk_thread_flag(t, TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 flush_sigqueue(&t->pending);
259 flush_sigqueue(&t->signal->shared_pending);
260 spin_unlock_irqrestore(&t->sighand->siglock, flags);
261}
262
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400263static void __flush_itimer_signals(struct sigpending *pending)
264{
265 sigset_t signal, retain;
266 struct sigqueue *q, *n;
267
268 signal = pending->signal;
269 sigemptyset(&retain);
270
271 list_for_each_entry_safe(q, n, &pending->list, list) {
272 int sig = q->info.si_signo;
273
274 if (likely(q->info.si_code != SI_TIMER)) {
275 sigaddset(&retain, sig);
276 } else {
277 sigdelset(&signal, sig);
278 list_del_init(&q->list);
279 __sigqueue_free(q);
280 }
281 }
282
283 sigorsets(&pending->signal, &signal, &retain);
284}
285
286void flush_itimer_signals(void)
287{
288 struct task_struct *tsk = current;
289 unsigned long flags;
290
291 spin_lock_irqsave(&tsk->sighand->siglock, flags);
292 __flush_itimer_signals(&tsk->pending);
293 __flush_itimer_signals(&tsk->signal->shared_pending);
294 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
295}
296
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700297void ignore_signals(struct task_struct *t)
298{
299 int i;
300
301 for (i = 0; i < _NSIG; ++i)
302 t->sighand->action[i].sa.sa_handler = SIG_IGN;
303
304 flush_signals(t);
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * Flush all handlers for a task.
309 */
310
311void
312flush_signal_handlers(struct task_struct *t, int force_default)
313{
314 int i;
315 struct k_sigaction *ka = &t->sighand->action[0];
316 for (i = _NSIG ; i != 0 ; i--) {
317 if (force_default || ka->sa.sa_handler != SIG_IGN)
318 ka->sa.sa_handler = SIG_DFL;
319 ka->sa.sa_flags = 0;
320 sigemptyset(&ka->sa.sa_mask);
321 ka++;
322 }
323}
324
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200325int unhandled_signal(struct task_struct *tsk, int sig)
326{
Roland McGrath445a91d2008-07-25 19:45:52 -0700327 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700328 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200329 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700330 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200331 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700332 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336/* Notify the system that a driver wants to block all signals for this
337 * process, and wants to be notified if any signals at all were to be
338 * sent/acted upon. If the notifier routine returns non-zero, then the
339 * signal will be acted upon after all. If the notifier routine returns 0,
340 * then then signal will be blocked. Only one block per process is
341 * allowed. priv is a pointer to private data that the notifier routine
342 * can use to determine if the signal should be blocked or not. */
343
344void
345block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
346{
347 unsigned long flags;
348
349 spin_lock_irqsave(&current->sighand->siglock, flags);
350 current->notifier_mask = mask;
351 current->notifier_data = priv;
352 current->notifier = notifier;
353 spin_unlock_irqrestore(&current->sighand->siglock, flags);
354}
355
356/* Notify the system that blocking has ended. */
357
358void
359unblock_all_signals(void)
360{
361 unsigned long flags;
362
363 spin_lock_irqsave(&current->sighand->siglock, flags);
364 current->notifier = NULL;
365 current->notifier_data = NULL;
366 recalc_sigpending();
367 spin_unlock_irqrestore(&current->sighand->siglock, flags);
368}
369
Oleg Nesterov100360f2008-07-25 01:47:29 -0700370static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * Collect the siginfo appropriate to this signal. Check if
376 * there is another siginfo for the same signal.
377 */
378 list_for_each_entry(q, &list->list, list) {
379 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700380 if (first)
381 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 first = q;
383 }
384 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700385
386 sigdelset(&list->signal, sig);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700389still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 list_del_init(&first->list);
391 copy_siginfo(info, &first->info);
392 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* Ok, it wasn't in the queue. This must be
395 a fast-pathed signal or we must have been
396 out of queue space. So zero out the info.
397 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 info->si_signo = sig;
399 info->si_errno = 0;
400 info->si_code = 0;
401 info->si_pid = 0;
402 info->si_uid = 0;
403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
407 siginfo_t *info)
408{
Roland McGrath27d91e02006-09-29 02:00:31 -0700409 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (sig) {
412 if (current->notifier) {
413 if (sigismember(current->notifier_mask, sig)) {
414 if (!(current->notifier)(current->notifier_data)) {
415 clear_thread_flag(TIF_SIGPENDING);
416 return 0;
417 }
418 }
419 }
420
Oleg Nesterov100360f2008-07-25 01:47:29 -0700421 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 return sig;
425}
426
427/*
428 * Dequeue a signal and return the element to the caller, which is
429 * expected to free it.
430 *
431 * All callers have to hold the siglock.
432 */
433int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
434{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700435 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000436
437 /* We only dequeue private signals from ourselves, we don't let
438 * signalfd steal them
439 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700440 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800441 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 signr = __dequeue_signal(&tsk->signal->shared_pending,
443 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800444 /*
445 * itimer signal ?
446 *
447 * itimers are process shared and we restart periodic
448 * itimers in the signal delivery path to prevent DoS
449 * attacks in the high resolution timer case. This is
450 * compliant with the old way of self restarting
451 * itimers, as the SIGALRM is a legacy signal and only
452 * queued once. Changing the restart behaviour to
453 * restart the timer in the signal dequeue path is
454 * reducing the timer noise on heavy loaded !highres
455 * systems too.
456 */
457 if (unlikely(signr == SIGALRM)) {
458 struct hrtimer *tmr = &tsk->signal->real_timer;
459
460 if (!hrtimer_is_queued(tmr) &&
461 tsk->signal->it_real_incr.tv64 != 0) {
462 hrtimer_forward(tmr, tmr->base->get_time(),
463 tsk->signal->it_real_incr);
464 hrtimer_restart(tmr);
465 }
466 }
467 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700468
Davide Libenzib8fceee2007-09-20 12:40:16 -0700469 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700470 if (!signr)
471 return 0;
472
473 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800474 /*
475 * Set a marker that we have dequeued a stop signal. Our
476 * caller might release the siglock and then the pending
477 * stop signal it is about to process is no longer in the
478 * pending bitmasks, but must still be cleared by a SIGCONT
479 * (and overruled by a SIGKILL). So those cases clear this
480 * shared flag after we've set it. Note that this flag may
481 * remain set after the signal we return is ignored or
482 * handled. That doesn't matter because its only purpose
483 * is to alert stop-signal processing code when another
484 * processor has come along and cleared the flag.
485 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700486 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800487 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700488 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /*
490 * Release the siglock to ensure proper locking order
491 * of timer locks outside of siglocks. Note, we leave
492 * irqs disabled here, since the posix-timers code is
493 * about to disable them again anyway.
494 */
495 spin_unlock(&tsk->sighand->siglock);
496 do_schedule_next_timer(info);
497 spin_lock(&tsk->sighand->siglock);
498 }
499 return signr;
500}
501
502/*
503 * Tell a process that it has a new active signal..
504 *
505 * NOTE! we rely on the previous spin_lock to
506 * lock interrupts for us! We can only be called with
507 * "siglock" held, and the local interrupt must
508 * have been disabled when that got acquired!
509 *
510 * No need to set need_resched since signal event passing
511 * goes through ->blocked
512 */
513void signal_wake_up(struct task_struct *t, int resume)
514{
515 unsigned int mask;
516
517 set_tsk_thread_flag(t, TIF_SIGPENDING);
518
519 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500520 * For SIGKILL, we want to wake it up in the stopped/traced/killable
521 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 * executing another processor and just now entering stopped state.
523 * By using wake_up_state, we ensure the process will wake up and
524 * handle its death signal.
525 */
526 mask = TASK_INTERRUPTIBLE;
527 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500528 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (!wake_up_state(t, mask))
530 kick_process(t);
531}
532
533/*
534 * Remove signals in mask from the pending set and queue.
535 * Returns 1 if any signals were found.
536 *
537 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800538 *
539 * This version takes a sigset mask and looks at all signals,
540 * not just those in the first mask word.
541 */
542static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
543{
544 struct sigqueue *q, *n;
545 sigset_t m;
546
547 sigandsets(&m, mask, &s->signal);
548 if (sigisemptyset(&m))
549 return 0;
550
551 signandsets(&s->signal, &s->signal, mask);
552 list_for_each_entry_safe(q, n, &s->list, list) {
553 if (sigismember(mask, q->info.si_signo)) {
554 list_del_init(&q->list);
555 __sigqueue_free(q);
556 }
557 }
558 return 1;
559}
560/*
561 * Remove signals in mask from the pending set and queue.
562 * Returns 1 if any signals were found.
563 *
564 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
566static int rm_from_queue(unsigned long mask, struct sigpending *s)
567{
568 struct sigqueue *q, *n;
569
570 if (!sigtestsetmask(&s->signal, mask))
571 return 0;
572
573 sigdelsetmask(&s->signal, mask);
574 list_for_each_entry_safe(q, n, &s->list, list) {
575 if (q->info.si_signo < SIGRTMIN &&
576 (mask & sigmask(q->info.si_signo))) {
577 list_del_init(&q->list);
578 __sigqueue_free(q);
579 }
580 }
581 return 1;
582}
583
584/*
585 * Bad permissions for sending the signal
David Howellsc69e8d92008-11-14 10:39:19 +1100586 * - the caller must hold at least the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
588static int check_kill_permission(int sig, struct siginfo *info,
589 struct task_struct *t)
590{
David Howellsc69e8d92008-11-14 10:39:19 +1100591 const struct cred *cred = current_cred(), *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700592 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700593 int error;
594
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700595 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700596 return -EINVAL;
597
598 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
599 return 0;
600
601 error = audit_signal_info(sig, t); /* Let audit system see the signal */
602 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400604
David Howellsc69e8d92008-11-14 10:39:19 +1100605 tcred = __task_cred(t);
606 if ((cred->euid ^ tcred->suid) &&
607 (cred->euid ^ tcred->uid) &&
608 (cred->uid ^ tcred->suid) &&
609 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700610 !capable(CAP_KILL)) {
611 switch (sig) {
612 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700613 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700614 /*
615 * We don't return the error if sid == NULL. The
616 * task was unhashed, the caller must notice this.
617 */
618 if (!sid || sid == task_session(current))
619 break;
620 default:
621 return -EPERM;
622 }
623 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100624
Amy Griffise54dc242007-03-29 18:01:04 -0400625 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700629 * Handle magic process-wide effects of stop/continue signals. Unlike
630 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * time regardless of blocking, ignoring, or handling. This does the
632 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700633 * signals. The process stop is done as a signal action for SIG_DFL.
634 *
635 * Returns true if the signal should be actually delivered, otherwise
636 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700638static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700640 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct task_struct *t;
642
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700643 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700645 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700647 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
649 * This is a stop signal. Remove SIGCONT from all queues.
650 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700651 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 t = p;
653 do {
654 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700655 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700657 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /*
659 * Remove all stop signals from all queues,
660 * and wake all threads.
661 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700662 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 t = p;
664 do {
665 unsigned int state;
666 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /*
668 * If there is a handler for SIGCONT, we must make
669 * sure that no thread returns to user mode before
670 * we post the signal, in case it was the only
671 * thread eligible to run the signal handler--then
672 * it must not do anything between resuming and
673 * running the handler. With the TIF_SIGPENDING
674 * flag set, the thread will pause and acquire the
675 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700676 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 *
678 * Wake up the stopped thread _after_ setting
679 * TIF_SIGPENDING
680 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500681 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
683 set_tsk_thread_flag(t, TIF_SIGPENDING);
684 state |= TASK_INTERRUPTIBLE;
685 }
686 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700687 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700689 /*
690 * Notify the parent with CLD_CONTINUED if we were stopped.
691 *
692 * If we were in the middle of a group stop, we pretend it
693 * was already finished, and then continued. Since SIGCHLD
694 * doesn't queue we report only CLD_STOPPED, as if the next
695 * CLD_CONTINUED was dropped.
696 */
697 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700698 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700699 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700700 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700701 why |= SIGNAL_CLD_STOPPED;
702
703 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700704 /*
705 * The first thread which returns from finish_stop()
706 * will take ->siglock, notice SIGNAL_CLD_MASK, and
707 * notify its parent. See get_signal_to_deliver().
708 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700709 signal->flags = why | SIGNAL_STOP_CONTINUED;
710 signal->group_stop_count = 0;
711 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else {
713 /*
714 * We are not stopped, but there could be a stop
715 * signal in the middle of being processed after
716 * being removed from the queue. Clear that too.
717 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700718 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700721
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700722 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700725/*
726 * Test if P wants to take SIG. After we've checked all threads with this,
727 * it's equivalent to finding no threads not blocking SIG. Any threads not
728 * blocking SIG were ruled out because they are not running and already
729 * have pending signals. Such threads will dequeue from the shared queue
730 * as soon as they're available, so putting the signal on the shared queue
731 * will be equivalent to sending it to one such thread.
732 */
733static inline int wants_signal(int sig, struct task_struct *p)
734{
735 if (sigismember(&p->blocked, sig))
736 return 0;
737 if (p->flags & PF_EXITING)
738 return 0;
739 if (sig == SIGKILL)
740 return 1;
741 if (task_is_stopped_or_traced(p))
742 return 0;
743 return task_curr(p) || !signal_pending(p);
744}
745
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700746static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700747{
748 struct signal_struct *signal = p->signal;
749 struct task_struct *t;
750
751 /*
752 * Now find a thread we can wake up to take the signal off the queue.
753 *
754 * If the main thread wants the signal, it gets first crack.
755 * Probably the least surprising to the average bear.
756 */
757 if (wants_signal(sig, p))
758 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700759 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700760 /*
761 * There is just one thread and it does not need to be woken.
762 * It will dequeue unblocked signals before it runs again.
763 */
764 return;
765 else {
766 /*
767 * Otherwise try to find a suitable thread.
768 */
769 t = signal->curr_target;
770 while (!wants_signal(sig, t)) {
771 t = next_thread(t);
772 if (t == signal->curr_target)
773 /*
774 * No thread needs to be woken.
775 * Any eligible threads will see
776 * the signal in the queue soon.
777 */
778 return;
779 }
780 signal->curr_target = t;
781 }
782
783 /*
784 * Found a killable thread. If the signal will be fatal,
785 * then start taking the whole group down immediately.
786 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700787 if (sig_fatal(p, sig) &&
788 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700789 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700790 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700791 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700792 /*
793 * This signal will be fatal to the whole group.
794 */
795 if (!sig_kernel_coredump(sig)) {
796 /*
797 * Start a group exit and wake everybody up.
798 * This way we don't have other threads
799 * running and doing things after a slower
800 * thread has the fatal signal pending.
801 */
802 signal->flags = SIGNAL_GROUP_EXIT;
803 signal->group_exit_code = sig;
804 signal->group_stop_count = 0;
805 t = p;
806 do {
807 sigaddset(&t->pending.signal, SIGKILL);
808 signal_wake_up(t, 1);
809 } while_each_thread(p, t);
810 return;
811 }
812 }
813
814 /*
815 * The signal is already in the shared-pending queue.
816 * Tell the chosen thread to wake up and dequeue it.
817 */
818 signal_wake_up(t, sig == SIGKILL);
819 return;
820}
821
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700822static inline int legacy_queue(struct sigpending *signals, int sig)
823{
824 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
825}
826
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700827static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
828 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700830 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700831 struct sigqueue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400833 trace_sched_signal_send(sig, t);
834
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700835 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700836
837 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700838 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700839
840 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700842 * Short-circuit ignored signals and support queuing
843 * exactly one non-rt signal, so that we can get more
844 * detailed information about the cause of the signal.
845 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700846 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700847 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * fast-pathed signals for kernel-internal things like SIGSTOP
850 * or SIGKILL.
851 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800852 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto out_set;
854
855 /* Real-time signals must be queued if sent by sigqueue, or
856 some other real-time mechanism. It is implementation
857 defined whether kill() does so. We attempt to do so, on
858 the principle of least surprise, but since kill is not
859 allowed to fail with EAGAIN when low on memory we just
860 make sure at least one signal gets delivered and don't
861 pass on the info struct. */
862
863 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
Oleg Nesterov621d3122005-10-30 15:03:45 -0800864 (is_si_special(info) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 info->si_code >= 0)));
866 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700867 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800869 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 q->info.si_signo = sig;
871 q->info.si_errno = 0;
872 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800873 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800874 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100875 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800877 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 q->info.si_signo = sig;
879 q->info.si_errno = 0;
880 q->info.si_code = SI_KERNEL;
881 q->info.si_pid = 0;
882 q->info.si_uid = 0;
883 break;
884 default:
885 copy_siginfo(&q->info, info);
886 break;
887 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800888 } else if (!is_si_special(info)) {
889 if (sig >= SIGRTMIN && info->si_code != SI_USER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /*
891 * Queue overflow, abort. We may abort if the signal was rt
892 * and sent by user using something other than kill().
893 */
894 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896
897out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700898 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700899 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700900 complete_signal(sig, t, group);
901 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700904static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
905 int group)
906{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700907 int from_ancestor_ns = 0;
908
909#ifdef CONFIG_PID_NS
910 if (!is_si_special(info) && SI_FROMUSER(info) &&
911 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
912 from_ancestor_ns = 1;
913#endif
914
915 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700916}
917
Ingo Molnar45807a12007-07-15 23:40:10 -0700918int print_fatal_signals;
919
920static void print_fatal_signal(struct pt_regs *regs, int signr)
921{
922 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700923 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700924
Al Viroca5cd872007-10-29 04:31:16 +0000925#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100926 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700927 {
928 int i;
929 for (i = 0; i < 16; i++) {
930 unsigned char insn;
931
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100932 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700933 printk("%02x ", insn);
934 }
935 }
936#endif
937 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800938 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700939 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800940 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700941}
942
943static int __init setup_print_fatal_signals(char *str)
944{
945 get_option (&str, &print_fatal_signals);
946
947 return 1;
948}
949
950__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700952int
953__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
954{
955 return send_signal(sig, info, p, 1);
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958static int
959specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
960{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700961 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*
965 * Force a signal that the process can't ignore: if necessary
966 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700967 *
968 * Note: If we unblock the signal, we always reset it to SIG_DFL,
969 * since we do not want to have a signal handler that was blocked
970 * be invoked when user space had explicitly blocked it.
971 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -0700972 * We don't want to have recursive SIGSEGV's etc, for example,
973 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975int
976force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
977{
978 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700979 int ret, blocked, ignored;
980 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700983 action = &t->sighand->action[sig-1];
984 ignored = action->sa.sa_handler == SIG_IGN;
985 blocked = sigismember(&t->blocked, sig);
986 if (blocked || ignored) {
987 action->sa.sa_handler = SIG_DFL;
988 if (blocked) {
989 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700990 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -0700993 if (action->sa.sa_handler == SIG_DFL)
994 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 ret = specific_send_sig_info(sig, info, t);
996 spin_unlock_irqrestore(&t->sighand->siglock, flags);
997
998 return ret;
999}
1000
1001void
1002force_sig_specific(int sig, struct task_struct *t)
1003{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001004 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007/*
1008 * Nuke all other threads in the group.
1009 */
1010void zap_other_threads(struct task_struct *p)
1011{
1012 struct task_struct *t;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 p->signal->group_stop_count = 0;
1015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 for (t = next_thread(p); t != p; t = next_thread(t)) {
1017 /*
1018 * Don't bother with already dead threads
1019 */
1020 if (t->exit_state)
1021 continue;
1022
Andrea Arcangeli30e0fca2005-10-30 15:02:38 -08001023 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 signal_wake_up(t, 1);
1026 }
1027}
1028
Harvey Harrisonb5606c22008-02-13 15:03:16 -08001029int __fatal_signal_pending(struct task_struct *tsk)
Matthew Wilcoxf776d122007-12-06 11:15:50 -05001030{
1031 return sigismember(&tsk->pending.signal, SIGKILL);
1032}
Trond Myklebust13f09b92008-01-31 20:40:29 -05001033EXPORT_SYMBOL(__fatal_signal_pending);
Matthew Wilcoxf776d122007-12-06 11:15:50 -05001034
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001035struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1036{
1037 struct sighand_struct *sighand;
1038
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001039 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001040 for (;;) {
1041 sighand = rcu_dereference(tsk->sighand);
1042 if (unlikely(sighand == NULL))
1043 break;
1044
1045 spin_lock_irqsave(&sighand->siglock, *flags);
1046 if (likely(sighand == tsk->sighand))
1047 break;
1048 spin_unlock_irqrestore(&sighand->siglock, *flags);
1049 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001050 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001051
1052 return sighand;
1053}
1054
David Howellsc69e8d92008-11-14 10:39:19 +11001055/*
1056 * send signal info to all the members of a group
1057 * - the caller must hold the RCU read lock at least
1058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1060{
1061 unsigned long flags;
1062 int ret;
1063
1064 ret = check_kill_permission(sig, info, p);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001065
1066 if (!ret && sig) {
1067 ret = -ESRCH;
1068 if (lock_task_sighand(p, &flags)) {
1069 ret = __group_send_sig_info(sig, info, p);
1070 unlock_task_sighand(p, &flags);
Ingo Molnare56d0902006-01-08 01:01:37 -08001071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 return ret;
1075}
1076
1077/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001078 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001080 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001082int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct task_struct *p = NULL;
1085 int retval, success;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 success = 0;
1088 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001089 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int err = group_send_sig_info(sig, info, p);
1091 success |= !err;
1092 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001093 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return success ? 0 : retval;
1095}
1096
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001097int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001099 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct task_struct *p;
1101
Ingo Molnare56d0902006-01-08 01:01:37 -08001102 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001103retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001104 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001105 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001107 if (unlikely(error == -ESRCH))
1108 /*
1109 * The task was unhashed in between, try again.
1110 * If it is dead, pid_task() will return NULL,
1111 * if we race with de_thread() it will find the
1112 * new leader.
1113 */
1114 goto retry;
1115 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001116 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return error;
1119}
1120
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001121int
1122kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001123{
1124 int error;
1125 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001126 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001127 rcu_read_unlock();
1128 return error;
1129}
1130
Eric W. Biederman2425c082006-10-02 02:17:28 -07001131/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1132int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001133 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001134{
1135 int ret = -EINVAL;
1136 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001137 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001138
1139 if (!valid_signal(sig))
1140 return ret;
1141
1142 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001143 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001144 if (!p) {
1145 ret = -ESRCH;
1146 goto out_unlock;
1147 }
David Howellsc69e8d92008-11-14 10:39:19 +11001148 pcred = __task_cred(p);
1149 if ((info == SEND_SIG_NOINFO ||
1150 (!is_si_special(info) && SI_FROMUSER(info))) &&
1151 euid != pcred->suid && euid != pcred->uid &&
1152 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001153 ret = -EPERM;
1154 goto out_unlock;
1155 }
David Quigley8f95dc52006-06-30 01:55:47 -07001156 ret = security_task_kill(p, info, sig, secid);
1157 if (ret)
1158 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001159 if (sig && p->sighand) {
1160 unsigned long flags;
1161 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001162 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001163 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1164 }
1165out_unlock:
1166 read_unlock(&tasklist_lock);
1167 return ret;
1168}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001169EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171/*
1172 * kill_something_info() interprets pid in interesting ways just like kill(2).
1173 *
1174 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1175 * is probably wrong. Should make it like BSD or SYSV.
1176 */
1177
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001178static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001180 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001181
1182 if (pid > 0) {
1183 rcu_read_lock();
1184 ret = kill_pid_info(sig, info, find_vpid(pid));
1185 rcu_read_unlock();
1186 return ret;
1187 }
1188
1189 read_lock(&tasklist_lock);
1190 if (pid != -1) {
1191 ret = __kill_pgrp_info(sig, info,
1192 pid ? find_vpid(-pid) : task_pgrp(current));
1193 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 int retval = 0, count = 0;
1195 struct task_struct * p;
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001198 if (task_pid_vnr(p) > 1 &&
1199 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 int err = group_send_sig_info(sig, info, p);
1201 ++count;
1202 if (err != -EPERM)
1203 retval = err;
1204 }
1205 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001206 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001208 read_unlock(&tasklist_lock);
1209
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001210 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/*
1214 * These are for backward compatibility with the rest of the kernel source.
1215 */
1216
1217/*
Oleg Nesterov08d2c302008-04-30 00:52:51 -07001218 * The caller must ensure the task can't exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
1220int
1221send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1222{
1223 int ret;
1224 unsigned long flags;
1225
1226 /*
1227 * Make sure legacy kernel users don't send in bad values
1228 * (normal paths check this in check_kill_permission).
1229 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001230 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return -EINVAL;
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 spin_lock_irqsave(&p->sighand->siglock, flags);
1234 ret = specific_send_sig_info(sig, info, p);
1235 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return ret;
1237}
1238
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001239#define __si_special(priv) \
1240 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242int
1243send_sig(int sig, struct task_struct *p, int priv)
1244{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001245 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248void
1249force_sig(int sig, struct task_struct *p)
1250{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001251 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254/*
1255 * When things go south during signal handling, we
1256 * will force a SIGSEGV. And if the signal that caused
1257 * the problem was already a SIGSEGV, we'll want to
1258 * make sure we don't even try to deliver the signal..
1259 */
1260int
1261force_sigsegv(int sig, struct task_struct *p)
1262{
1263 if (sig == SIGSEGV) {
1264 unsigned long flags;
1265 spin_lock_irqsave(&p->sighand->siglock, flags);
1266 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1267 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1268 }
1269 force_sig(SIGSEGV, p);
1270 return 0;
1271}
1272
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001273int kill_pgrp(struct pid *pid, int sig, int priv)
1274{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001275 int ret;
1276
1277 read_lock(&tasklist_lock);
1278 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1279 read_unlock(&tasklist_lock);
1280
1281 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001282}
1283EXPORT_SYMBOL(kill_pgrp);
1284
1285int kill_pid(struct pid *pid, int sig, int priv)
1286{
1287 return kill_pid_info(sig, __si_special(priv), pid);
1288}
1289EXPORT_SYMBOL(kill_pid);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291/*
1292 * These functions support sending signals using preallocated sigqueue
1293 * structures. This is needed "because realtime applications cannot
1294 * afford to lose notifications of asynchronous events, like timer
1295 * expirations or I/O completions". In the case of Posix Timers
1296 * we allocate the sigqueue structure from the timer_create. If this
1297 * allocation fails we are able to report the failure to the application
1298 * with an EAGAIN error.
1299 */
1300
1301struct sigqueue *sigqueue_alloc(void)
1302{
1303 struct sigqueue *q;
1304
1305 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1306 q->flags |= SIGQUEUE_PREALLOC;
1307 return(q);
1308}
1309
1310void sigqueue_free(struct sigqueue *q)
1311{
1312 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001313 spinlock_t *lock = &current->sighand->siglock;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1316 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001317 * We must hold ->siglock while testing q->list
1318 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001319 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001321 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001322 q->flags &= ~SIGQUEUE_PREALLOC;
1323 /*
1324 * If it is queued it will be freed when dequeued,
1325 * like the "regular" sigqueue.
1326 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001327 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001328 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001329 spin_unlock_irqrestore(lock, flags);
1330
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001331 if (q)
1332 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333}
1334
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001335int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001336{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001337 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001338 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001339 unsigned long flags;
1340 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001341
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001342 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001343
1344 ret = -1;
1345 if (!likely(lock_task_sighand(t, &flags)))
1346 goto ret;
1347
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001348 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001349 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001350 goto out;
1351
1352 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001353 if (unlikely(!list_empty(&q->list))) {
1354 /*
1355 * If an SI_TIMER entry is already queue just increment
1356 * the overrun count.
1357 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001358 BUG_ON(q->info.si_code != SI_TIMER);
1359 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001360 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001361 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001362 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001363
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001364 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001365 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001366 list_add_tail(&q->list, &pending->list);
1367 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001368 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001369out:
1370 unlock_task_sighand(t, &flags);
1371ret:
1372 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/*
1376 * Wake up any threads in the parent blocked in wait* syscalls.
1377 */
1378static inline void __wake_up_parent(struct task_struct *p,
1379 struct task_struct *parent)
1380{
1381 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1382}
1383
1384/*
1385 * Let a parent know about the death of a child.
1386 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001387 *
1388 * Returns -1 if our parent ignored us and so we've switched to
1389 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001391int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct siginfo info;
1394 unsigned long flags;
1395 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001396 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 BUG_ON(sig == -1);
1399
1400 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001401 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 BUG_ON(!tsk->ptrace &&
1404 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1405
1406 info.si_signo = sig;
1407 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001408 /*
1409 * we are under tasklist_lock here so our parent is tied to
1410 * us and cannot exit and release its namespace.
1411 *
1412 * the only it can is to switch its nsproxy with sys_unshare,
1413 * bu uncharing pid namespaces is not allowed, so we'll always
1414 * see relevant namespace
1415 *
1416 * write_lock() currently calls preempt_disable() which is the
1417 * same as rcu_read_lock(), but according to Oleg, this is not
1418 * correct to rely on this
1419 */
1420 rcu_read_lock();
1421 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001422 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001423 rcu_read_unlock();
1424
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001425 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1426 tsk->signal->utime));
1427 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1428 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
1430 info.si_status = tsk->exit_code & 0x7f;
1431 if (tsk->exit_code & 0x80)
1432 info.si_code = CLD_DUMPED;
1433 else if (tsk->exit_code & 0x7f)
1434 info.si_code = CLD_KILLED;
1435 else {
1436 info.si_code = CLD_EXITED;
1437 info.si_status = tsk->exit_code >> 8;
1438 }
1439
1440 psig = tsk->parent->sighand;
1441 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov7ed01752005-11-10 17:22:18 +03001442 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1444 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1445 /*
1446 * We are exiting and our parent doesn't care. POSIX.1
1447 * defines special semantics for setting SIGCHLD to SIG_IGN
1448 * or setting the SA_NOCLDWAIT flag: we should be reaped
1449 * automatically and not left for our parent's wait4 call.
1450 * Rather than having the parent do it as a magic kind of
1451 * signal handler, we just set this to tell do_exit that we
1452 * can be cleaned up without becoming a zombie. Note that
1453 * we still call __wake_up_parent in this case, because a
1454 * blocked sys_wait4 might now return -ECHILD.
1455 *
1456 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1457 * is implementation-defined: we do (if you don't want
1458 * it, just use SIG_IGN instead).
1459 */
Roland McGrath1b046242008-08-19 20:37:07 -07001460 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001462 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001464 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 __group_send_sig_info(sig, &info, tsk->parent);
1466 __wake_up_parent(tsk, tsk->parent);
1467 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001468
Roland McGrath1b046242008-08-19 20:37:07 -07001469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001472static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
1474 struct siginfo info;
1475 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001476 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 struct sighand_struct *sighand;
1478
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001479 if (tsk->ptrace & PT_PTRACED)
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001480 parent = tsk->parent;
1481 else {
1482 tsk = tsk->group_leader;
1483 parent = tsk->real_parent;
1484 }
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 info.si_signo = SIGCHLD;
1487 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001488 /*
1489 * see comment in do_notify_parent() abot the following 3 lines
1490 */
1491 rcu_read_lock();
1492 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001493 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001494 rcu_read_unlock();
1495
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001496 info.si_utime = cputime_to_clock_t(tsk->utime);
1497 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 info.si_code = why;
1500 switch (why) {
1501 case CLD_CONTINUED:
1502 info.si_status = SIGCONT;
1503 break;
1504 case CLD_STOPPED:
1505 info.si_status = tsk->signal->group_exit_code & 0x7f;
1506 break;
1507 case CLD_TRAPPED:
1508 info.si_status = tsk->exit_code & 0x7f;
1509 break;
1510 default:
1511 BUG();
1512 }
1513
1514 sighand = parent->sighand;
1515 spin_lock_irqsave(&sighand->siglock, flags);
1516 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1517 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1518 __group_send_sig_info(SIGCHLD, &info, parent);
1519 /*
1520 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1521 */
1522 __wake_up_parent(tsk, parent);
1523 spin_unlock_irqrestore(&sighand->siglock, flags);
1524}
1525
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001526static inline int may_ptrace_stop(void)
1527{
1528 if (!likely(current->ptrace & PT_PTRACED))
1529 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001530 /*
1531 * Are we in the middle of do_coredump?
1532 * If so and our tracer is also part of the coredump stopping
1533 * is a deadlock situation, and pointless because our tracer
1534 * is dead so don't allow us to stop.
1535 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001536 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001537 * is safe to enter schedule().
1538 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001539 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001540 unlikely(current->mm == current->parent->mm))
1541 return 0;
1542
1543 return 1;
1544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001547 * Return nonzero if there is a SIGKILL that should be waking us up.
1548 * Called with the siglock held.
1549 */
1550static int sigkill_pending(struct task_struct *tsk)
1551{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001552 return sigismember(&tsk->pending.signal, SIGKILL) ||
1553 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001554}
1555
1556/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 * This must be called with current->sighand->siglock held.
1558 *
1559 * This should be the path for all ptrace stops.
1560 * We always set current->last_siginfo while stopped here.
1561 * That makes it a way to test a stopped process for
1562 * being ptrace-stopped vs being job-control-stopped.
1563 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001564 * If we actually decide not to stop at all because the tracer
1565 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001567static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Roland McGrath1a669c22008-02-06 01:37:37 -08001569 if (arch_ptrace_stop_needed(exit_code, info)) {
1570 /*
1571 * The arch code has something special to do before a
1572 * ptrace stop. This is allowed to block, e.g. for faults
1573 * on user stack pages. We can't keep the siglock while
1574 * calling arch_ptrace_stop, so we must release it now.
1575 * To preserve proper semantics, we must do this before
1576 * any signal bookkeeping like checking group_stop_count.
1577 * Meanwhile, a SIGKILL could come in before we retake the
1578 * siglock. That must prevent us from sleeping in TASK_TRACED.
1579 * So after regaining the lock, we must check for SIGKILL.
1580 */
1581 spin_unlock_irq(&current->sighand->siglock);
1582 arch_ptrace_stop(exit_code, info);
1583 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001584 if (sigkill_pending(current))
1585 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001586 }
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /*
1589 * If there is a group stop in progress,
1590 * we must participate in the bookkeeping.
1591 */
1592 if (current->signal->group_stop_count > 0)
1593 --current->signal->group_stop_count;
1594
1595 current->last_siginfo = info;
1596 current->exit_code = exit_code;
1597
1598 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001599 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 spin_unlock_irq(&current->sighand->siglock);
1601 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001602 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001603 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001604 /*
1605 * Don't want to allow preemption here, because
1606 * sys_ptrace() needs this task to be inactive.
1607 *
1608 * XXX: implement read_unlock_no_resched().
1609 */
1610 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001612 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 schedule();
1614 } else {
1615 /*
1616 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001617 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001619 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001620 if (clear_code)
1621 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001622 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001626 * While in TASK_TRACED, we were considered "frozen enough".
1627 * Now that we woke up, it's crucial if we're supposed to be
1628 * frozen that we freeze now before running anything substantial.
1629 */
1630 try_to_freeze();
1631
1632 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * We are back. Now reacquire the siglock before touching
1634 * last_siginfo, so that we are sure to have synchronized with
1635 * any signal-sending on another CPU that wants to examine it.
1636 */
1637 spin_lock_irq(&current->sighand->siglock);
1638 current->last_siginfo = NULL;
1639
1640 /*
1641 * Queued signals ignored us while we were stopped for tracing.
1642 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001643 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001645 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646}
1647
1648void ptrace_notify(int exit_code)
1649{
1650 siginfo_t info;
1651
1652 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1653
1654 memset(&info, 0, sizeof info);
1655 info.si_signo = SIGTRAP;
1656 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001657 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001658 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 /* Let the debugger run. */
1661 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001662 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 spin_unlock_irq(&current->sighand->siglock);
1664}
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666static void
1667finish_stop(int stop_count)
1668{
1669 /*
1670 * If there are no other threads in the group, or if there is
1671 * a group stop in progress and we are the last to stop,
1672 * report to the parent. When ptraced, every thread reports itself.
1673 */
Roland McGrathfa00b802008-07-25 19:45:54 -07001674 if (tracehook_notify_jctl(stop_count == 0, CLD_STOPPED)) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001675 read_lock(&tasklist_lock);
1676 do_notify_parent_cldstop(current, CLD_STOPPED);
1677 read_unlock(&tasklist_lock);
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -08001680 do {
1681 schedule();
1682 } while (try_to_freeze());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * Now we don't run again until continued.
1685 */
1686 current->exit_code = 0;
1687}
1688
1689/*
1690 * This performs the stopping for SIGSTOP and other stop signals.
1691 * We have to stop all threads in the thread group.
1692 * Returns nonzero if we've actually stopped and released the siglock.
1693 * Returns zero if we didn't stop and still hold the siglock.
1694 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001695static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct signal_struct *sig = current->signal;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001698 int stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (sig->group_stop_count > 0) {
1701 /*
1702 * There is a group stop in progress. We don't need to
1703 * start another one.
1704 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 stop_count = --sig->group_stop_count;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001706 } else {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001707 struct task_struct *t;
1708
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001709 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001710 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001714 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001716 sig->group_exit_code = signr;
1717
1718 stop_count = 0;
1719 for (t = next_thread(current); t != current; t = next_thread(t))
1720 /*
1721 * Setting state to TASK_STOPPED for a group
1722 * stop is always done with the siglock held,
1723 * so this check has no races.
1724 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001725 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001726 !task_is_stopped_or_traced(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08001727 stop_count++;
1728 signal_wake_up(t, 0);
1729 }
1730 sig->group_stop_count = stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001733 if (stop_count == 0)
1734 sig->flags = SIGNAL_STOP_STOPPED;
1735 current->exit_code = sig->group_exit_code;
1736 __set_current_state(TASK_STOPPED);
1737
1738 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 finish_stop(stop_count);
1740 return 1;
1741}
1742
Roland McGrath18c98b62008-04-17 18:44:38 -07001743static int ptrace_signal(int signr, siginfo_t *info,
1744 struct pt_regs *regs, void *cookie)
1745{
1746 if (!(current->ptrace & PT_PTRACED))
1747 return signr;
1748
1749 ptrace_signal_deliver(regs, cookie);
1750
1751 /* Let the debugger run. */
1752 ptrace_stop(signr, 0, info);
1753
1754 /* We're back. Did the debugger cancel the sig? */
1755 signr = current->exit_code;
1756 if (signr == 0)
1757 return signr;
1758
1759 current->exit_code = 0;
1760
1761 /* Update the siginfo structure if the signal has
1762 changed. If the debugger wanted something
1763 specific in the siginfo structure then it should
1764 have updated *info via PTRACE_SETSIGINFO. */
1765 if (signr != info->si_signo) {
1766 info->si_signo = signr;
1767 info->si_errno = 0;
1768 info->si_code = SI_USER;
1769 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001770 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001771 }
1772
1773 /* If the (new) signal is now blocked, requeue it. */
1774 if (sigismember(&current->blocked, signr)) {
1775 specific_send_sig_info(signr, info, current);
1776 signr = 0;
1777 }
1778
1779 return signr;
1780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1783 struct pt_regs *regs, void *cookie)
1784{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001785 struct sighand_struct *sighand = current->sighand;
1786 struct signal_struct *signal = current->signal;
1787 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001789relock:
1790 /*
1791 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1792 * While in TASK_STOPPED, we were considered "frozen enough".
1793 * Now that we woke up, it's crucial if we're supposed to be
1794 * frozen that we freeze now before running anything substantial.
1795 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001796 try_to_freeze();
1797
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001798 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001799 /*
1800 * Every stopped thread goes here after wakeup. Check to see if
1801 * we should notify the parent, prepare_signal(SIGCONT) encodes
1802 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1803 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001804 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1805 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001806 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001807 signal->flags &= ~SIGNAL_CLD_MASK;
1808 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001809
Roland McGrathfa00b802008-07-25 19:45:54 -07001810 if (unlikely(!tracehook_notify_jctl(1, why)))
1811 goto relock;
1812
Oleg Nesterove4420552008-04-30 00:52:44 -07001813 read_lock(&tasklist_lock);
1814 do_notify_parent_cldstop(current->group_leader, why);
1815 read_unlock(&tasklist_lock);
1816 goto relock;
1817 }
1818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 for (;;) {
1820 struct k_sigaction *ka;
1821
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001822 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001823 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto relock;
1825
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001826 /*
1827 * Tracing can induce an artifical signal and choose sigaction.
1828 * The return value in @signr determines the default action,
1829 * but @info->si_signo is the signal number we will report.
1830 */
1831 signr = tracehook_get_signal(current, regs, info, return_ka);
1832 if (unlikely(signr < 0))
1833 goto relock;
1834 if (unlikely(signr != 0))
1835 ka = return_ka;
1836 else {
1837 signr = dequeue_signal(current, &current->blocked,
1838 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Roland McGrath18c98b62008-04-17 18:44:38 -07001840 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001841 break; /* will return 0 */
1842
1843 if (signr != SIGKILL) {
1844 signr = ptrace_signal(signr, info,
1845 regs, cookie);
1846 if (!signr)
1847 continue;
1848 }
1849
1850 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1854 continue;
1855 if (ka->sa.sa_handler != SIG_DFL) {
1856 /* Run the handler. */
1857 *return_ka = *ka;
1858
1859 if (ka->sa.sa_flags & SA_ONESHOT)
1860 ka->sa.sa_handler = SIG_DFL;
1861
1862 break; /* will return non-zero "signr" value */
1863 }
1864
1865 /*
1866 * Now we are doing the default action for this signal.
1867 */
1868 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1869 continue;
1870
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001871 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001872 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001873 * Container-init gets no signals it doesn't want from same
1874 * container.
1875 *
1876 * Note that if global/container-init sees a sig_kernel_only()
1877 * signal here, the signal must have been generated internally
1878 * or must have come from an ancestor namespace. In either
1879 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001880 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001881 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001882 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 continue;
1884
1885 if (sig_kernel_stop(signr)) {
1886 /*
1887 * The default action is to stop all threads in
1888 * the thread group. The job control signals
1889 * do nothing in an orphaned pgrp, but SIGSTOP
1890 * always works. Note that siglock needs to be
1891 * dropped during the call to is_orphaned_pgrp()
1892 * because of lock ordering with tasklist_lock.
1893 * This allows an intervening SIGCONT to be posted.
1894 * We need to check for that and bail out if necessary.
1895 */
1896 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001897 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /* signals can be posted during this window */
1900
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001901 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto relock;
1903
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001904 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
1906
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001907 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* It released the siglock. */
1909 goto relock;
1910 }
1911
1912 /*
1913 * We didn't actually stop, due to a race
1914 * with SIGCONT or something like that.
1915 */
1916 continue;
1917 }
1918
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001919 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /*
1922 * Anything else is fatal, maybe with a core dump.
1923 */
1924 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001927 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001928 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /*
1930 * If it was able to dump core, this kills all
1931 * other threads in the group and synchronizes with
1932 * their demise. If we lost the race with another
1933 * thread getting here, it set group_exit_code
1934 * first and our do_group_exit call below will use
1935 * that value and ignore the one we pass it.
1936 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001937 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939
1940 /*
1941 * Death signals, no core dump.
1942 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001943 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /* NOTREACHED */
1945 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001946 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return signr;
1948}
1949
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001950void exit_signals(struct task_struct *tsk)
1951{
1952 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001953 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001954
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001955 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1956 tsk->flags |= PF_EXITING;
1957 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001958 }
1959
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001960 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001961 /*
1962 * From now this task is not visible for group-wide signals,
1963 * see wants_signal(), do_signal_stop().
1964 */
1965 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001966 if (!signal_pending(tsk))
1967 goto out;
1968
1969 /* It could be that __group_complete_signal() choose us to
1970 * notify about group-wide signal. Another thread should be
1971 * woken now to take the signal since we will not.
1972 */
1973 for (t = tsk; (t = next_thread(t)) != tsk; )
1974 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1975 recalc_sigpending_and_wake(t);
1976
1977 if (unlikely(tsk->signal->group_stop_count) &&
1978 !--tsk->signal->group_stop_count) {
1979 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1980 group_stop = 1;
1981 }
1982out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001983 spin_unlock_irq(&tsk->sighand->siglock);
1984
Roland McGrathfa00b802008-07-25 19:45:54 -07001985 if (unlikely(group_stop) && tracehook_notify_jctl(1, CLD_STOPPED)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001986 read_lock(&tasklist_lock);
1987 do_notify_parent_cldstop(tsk, CLD_STOPPED);
1988 read_unlock(&tasklist_lock);
1989 }
1990}
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992EXPORT_SYMBOL(recalc_sigpending);
1993EXPORT_SYMBOL_GPL(dequeue_signal);
1994EXPORT_SYMBOL(flush_signals);
1995EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996EXPORT_SYMBOL(send_sig);
1997EXPORT_SYMBOL(send_sig_info);
1998EXPORT_SYMBOL(sigprocmask);
1999EXPORT_SYMBOL(block_all_signals);
2000EXPORT_SYMBOL(unblock_all_signals);
2001
2002
2003/*
2004 * System call entry points.
2005 */
2006
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002007SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
2009 struct restart_block *restart = &current_thread_info()->restart_block;
2010 return restart->fn(restart);
2011}
2012
2013long do_no_restart_syscall(struct restart_block *param)
2014{
2015 return -EINTR;
2016}
2017
2018/*
2019 * We don't need to get the kernel lock - this is all local to this
2020 * particular thread.. (and that's good, because this is _heavily_
2021 * used by various programs)
2022 */
2023
2024/*
2025 * This is also useful for kernel threads that want to temporarily
2026 * (or permanently) block certain signals.
2027 *
2028 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2029 * interface happily blocks "unblockable" signals like SIGKILL
2030 * and friends.
2031 */
2032int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2033{
2034 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002037 if (oldset)
2038 *oldset = current->blocked;
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 error = 0;
2041 switch (how) {
2042 case SIG_BLOCK:
2043 sigorsets(&current->blocked, &current->blocked, set);
2044 break;
2045 case SIG_UNBLOCK:
2046 signandsets(&current->blocked, &current->blocked, set);
2047 break;
2048 case SIG_SETMASK:
2049 current->blocked = *set;
2050 break;
2051 default:
2052 error = -EINVAL;
2053 }
2054 recalc_sigpending();
2055 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 return error;
2058}
2059
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002060SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2061 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
2063 int error = -EINVAL;
2064 sigset_t old_set, new_set;
2065
2066 /* XXX: Don't preclude handling different sized sigset_t's. */
2067 if (sigsetsize != sizeof(sigset_t))
2068 goto out;
2069
2070 if (set) {
2071 error = -EFAULT;
2072 if (copy_from_user(&new_set, set, sizeof(*set)))
2073 goto out;
2074 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2075
2076 error = sigprocmask(how, &new_set, &old_set);
2077 if (error)
2078 goto out;
2079 if (oset)
2080 goto set_old;
2081 } else if (oset) {
2082 spin_lock_irq(&current->sighand->siglock);
2083 old_set = current->blocked;
2084 spin_unlock_irq(&current->sighand->siglock);
2085
2086 set_old:
2087 error = -EFAULT;
2088 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2089 goto out;
2090 }
2091 error = 0;
2092out:
2093 return error;
2094}
2095
2096long do_sigpending(void __user *set, unsigned long sigsetsize)
2097{
2098 long error = -EINVAL;
2099 sigset_t pending;
2100
2101 if (sigsetsize > sizeof(sigset_t))
2102 goto out;
2103
2104 spin_lock_irq(&current->sighand->siglock);
2105 sigorsets(&pending, &current->pending.signal,
2106 &current->signal->shared_pending.signal);
2107 spin_unlock_irq(&current->sighand->siglock);
2108
2109 /* Outside the lock because only this thread touches it. */
2110 sigandsets(&pending, &current->blocked, &pending);
2111
2112 error = -EFAULT;
2113 if (!copy_to_user(set, &pending, sigsetsize))
2114 error = 0;
2115
2116out:
2117 return error;
2118}
2119
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002120SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 return do_sigpending(set, sigsetsize);
2123}
2124
2125#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2126
2127int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2128{
2129 int err;
2130
2131 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2132 return -EFAULT;
2133 if (from->si_code < 0)
2134 return __copy_to_user(to, from, sizeof(siginfo_t))
2135 ? -EFAULT : 0;
2136 /*
2137 * If you change siginfo_t structure, please be sure
2138 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002139 * Please remember to update the signalfd_copyinfo() function
2140 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 * It should never copy any pad contained in the structure
2142 * to avoid security leaks, but must copy the generic
2143 * 3 ints plus the relevant union member.
2144 */
2145 err = __put_user(from->si_signo, &to->si_signo);
2146 err |= __put_user(from->si_errno, &to->si_errno);
2147 err |= __put_user((short)from->si_code, &to->si_code);
2148 switch (from->si_code & __SI_MASK) {
2149 case __SI_KILL:
2150 err |= __put_user(from->si_pid, &to->si_pid);
2151 err |= __put_user(from->si_uid, &to->si_uid);
2152 break;
2153 case __SI_TIMER:
2154 err |= __put_user(from->si_tid, &to->si_tid);
2155 err |= __put_user(from->si_overrun, &to->si_overrun);
2156 err |= __put_user(from->si_ptr, &to->si_ptr);
2157 break;
2158 case __SI_POLL:
2159 err |= __put_user(from->si_band, &to->si_band);
2160 err |= __put_user(from->si_fd, &to->si_fd);
2161 break;
2162 case __SI_FAULT:
2163 err |= __put_user(from->si_addr, &to->si_addr);
2164#ifdef __ARCH_SI_TRAPNO
2165 err |= __put_user(from->si_trapno, &to->si_trapno);
2166#endif
2167 break;
2168 case __SI_CHLD:
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_status, &to->si_status);
2172 err |= __put_user(from->si_utime, &to->si_utime);
2173 err |= __put_user(from->si_stime, &to->si_stime);
2174 break;
2175 case __SI_RT: /* This is not generated by the kernel as of now. */
2176 case __SI_MESGQ: /* But this is */
2177 err |= __put_user(from->si_pid, &to->si_pid);
2178 err |= __put_user(from->si_uid, &to->si_uid);
2179 err |= __put_user(from->si_ptr, &to->si_ptr);
2180 break;
2181 default: /* this is just in case for now ... */
2182 err |= __put_user(from->si_pid, &to->si_pid);
2183 err |= __put_user(from->si_uid, &to->si_uid);
2184 break;
2185 }
2186 return err;
2187}
2188
2189#endif
2190
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002191SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2192 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2193 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 int ret, sig;
2196 sigset_t these;
2197 struct timespec ts;
2198 siginfo_t info;
2199 long timeout = 0;
2200
2201 /* XXX: Don't preclude handling different sized sigset_t's. */
2202 if (sigsetsize != sizeof(sigset_t))
2203 return -EINVAL;
2204
2205 if (copy_from_user(&these, uthese, sizeof(these)))
2206 return -EFAULT;
2207
2208 /*
2209 * Invert the set of allowed signals to get those we
2210 * want to block.
2211 */
2212 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2213 signotset(&these);
2214
2215 if (uts) {
2216 if (copy_from_user(&ts, uts, sizeof(ts)))
2217 return -EFAULT;
2218 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2219 || ts.tv_sec < 0)
2220 return -EINVAL;
2221 }
2222
2223 spin_lock_irq(&current->sighand->siglock);
2224 sig = dequeue_signal(current, &these, &info);
2225 if (!sig) {
2226 timeout = MAX_SCHEDULE_TIMEOUT;
2227 if (uts)
2228 timeout = (timespec_to_jiffies(&ts)
2229 + (ts.tv_sec || ts.tv_nsec));
2230
2231 if (timeout) {
2232 /* None ready -- temporarily unblock those we're
2233 * interested while we are sleeping in so that we'll
2234 * be awakened when they arrive. */
2235 current->real_blocked = current->blocked;
2236 sigandsets(&current->blocked, &current->blocked, &these);
2237 recalc_sigpending();
2238 spin_unlock_irq(&current->sighand->siglock);
2239
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002240 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 spin_lock_irq(&current->sighand->siglock);
2243 sig = dequeue_signal(current, &these, &info);
2244 current->blocked = current->real_blocked;
2245 siginitset(&current->real_blocked, 0);
2246 recalc_sigpending();
2247 }
2248 }
2249 spin_unlock_irq(&current->sighand->siglock);
2250
2251 if (sig) {
2252 ret = sig;
2253 if (uinfo) {
2254 if (copy_siginfo_to_user(uinfo, &info))
2255 ret = -EFAULT;
2256 }
2257 } else {
2258 ret = -EAGAIN;
2259 if (timeout)
2260 ret = -EINTR;
2261 }
2262
2263 return ret;
2264}
2265
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002266SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 struct siginfo info;
2269
2270 info.si_signo = sig;
2271 info.si_errno = 0;
2272 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002273 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002274 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
2276 return kill_something_info(sig, &info, pid);
2277}
2278
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07002279static int do_tkill(pid_t tgid, pid_t pid, int sig)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002280{
2281 int error;
2282 struct siginfo info;
2283 struct task_struct *p;
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002284 unsigned long flags;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002285
2286 error = -ESRCH;
2287 info.si_signo = sig;
2288 info.si_errno = 0;
2289 info.si_code = SI_TKILL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002290 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002291 info.si_uid = current_uid();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002292
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002293 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002294 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002295 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002296 error = check_kill_permission(sig, &info, p);
2297 /*
2298 * The null signal is a permissions and process existence
2299 * probe. No signal is actually delivered.
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002300 *
2301 * If lock_task_sighand() fails we pretend the task dies
2302 * after receiving the signal. The window is tiny, and the
2303 * signal is private anyway.
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002304 */
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002305 if (!error && sig && lock_task_sighand(p, &flags)) {
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002306 error = specific_send_sig_info(sig, &info, p);
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002307 unlock_task_sighand(p, &flags);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002308 }
2309 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002310 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002311
2312 return error;
2313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315/**
2316 * sys_tgkill - send signal to one specific thread
2317 * @tgid: the thread group ID of the thread
2318 * @pid: the PID of the thread
2319 * @sig: signal to be sent
2320 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002321 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * exists but it's not belonging to the target process anymore. This
2323 * method solves the problem of threads exiting and PIDs getting reused.
2324 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002325SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* This is only valid for single tasks */
2328 if (pid <= 0 || tgid <= 0)
2329 return -EINVAL;
2330
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002331 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334/*
2335 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2336 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002337SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* This is only valid for single tasks */
2340 if (pid <= 0)
2341 return -EINVAL;
2342
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002343 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002346SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2347 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 siginfo_t info;
2350
2351 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2352 return -EFAULT;
2353
2354 /* Not even root can pretend to send signals from the kernel.
2355 Nor can they impersonate a kill(), which adds source info. */
2356 if (info.si_code >= 0)
2357 return -EPERM;
2358 info.si_signo = sig;
2359
2360 /* POSIX.1b doesn't mention process groups. */
2361 return kill_proc_info(sig, &info, pid);
2362}
2363
Oleg Nesterov88531f72006-03-28 16:11:24 -08002364int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002366 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002368 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002370 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return -EINVAL;
2372
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002373 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (oact)
2377 *oact = *k;
2378
2379 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002380 sigdelsetmask(&act->sa.sa_mask,
2381 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002382 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * POSIX 3.3.1.3:
2385 * "Setting a signal action to SIG_IGN for a signal that is
2386 * pending shall cause the pending signal to be discarded,
2387 * whether or not it is blocked."
2388 *
2389 * "Setting a signal action to SIG_DFL for a signal that is
2390 * pending and whose default action is to ignore the signal
2391 * (for example, SIGCHLD), shall cause the pending signal to
2392 * be discarded, whether or not it is blocked"
2393 */
Roland McGrath35de2542008-07-25 19:45:51 -07002394 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002395 sigemptyset(&mask);
2396 sigaddset(&mask, sig);
2397 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002399 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 t = next_thread(t);
2401 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
2405 spin_unlock_irq(&current->sighand->siglock);
2406 return 0;
2407}
2408
2409int
2410do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2411{
2412 stack_t oss;
2413 int error;
2414
2415 if (uoss) {
2416 oss.ss_sp = (void __user *) current->sas_ss_sp;
2417 oss.ss_size = current->sas_ss_size;
2418 oss.ss_flags = sas_ss_flags(sp);
2419 }
2420
2421 if (uss) {
2422 void __user *ss_sp;
2423 size_t ss_size;
2424 int ss_flags;
2425
2426 error = -EFAULT;
2427 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2428 || __get_user(ss_sp, &uss->ss_sp)
2429 || __get_user(ss_flags, &uss->ss_flags)
2430 || __get_user(ss_size, &uss->ss_size))
2431 goto out;
2432
2433 error = -EPERM;
2434 if (on_sig_stack(sp))
2435 goto out;
2436
2437 error = -EINVAL;
2438 /*
2439 *
2440 * Note - this code used to test ss_flags incorrectly
2441 * old code may have been written using ss_flags==0
2442 * to mean ss_flags==SS_ONSTACK (as this was the only
2443 * way that worked) - this fix preserves that older
2444 * mechanism
2445 */
2446 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2447 goto out;
2448
2449 if (ss_flags == SS_DISABLE) {
2450 ss_size = 0;
2451 ss_sp = NULL;
2452 } else {
2453 error = -ENOMEM;
2454 if (ss_size < MINSIGSTKSZ)
2455 goto out;
2456 }
2457
2458 current->sas_ss_sp = (unsigned long) ss_sp;
2459 current->sas_ss_size = ss_size;
2460 }
2461
2462 if (uoss) {
2463 error = -EFAULT;
2464 if (copy_to_user(uoss, &oss, sizeof(oss)))
2465 goto out;
2466 }
2467
2468 error = 0;
2469out:
2470 return error;
2471}
2472
2473#ifdef __ARCH_WANT_SYS_SIGPENDING
2474
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002475SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 return do_sigpending(set, sizeof(*set));
2478}
2479
2480#endif
2481
2482#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2483/* Some platforms have their own version with special arguments others
2484 support only sys_rt_sigprocmask. */
2485
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002486SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2487 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
2489 int error;
2490 old_sigset_t old_set, new_set;
2491
2492 if (set) {
2493 error = -EFAULT;
2494 if (copy_from_user(&new_set, set, sizeof(*set)))
2495 goto out;
2496 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2497
2498 spin_lock_irq(&current->sighand->siglock);
2499 old_set = current->blocked.sig[0];
2500
2501 error = 0;
2502 switch (how) {
2503 default:
2504 error = -EINVAL;
2505 break;
2506 case SIG_BLOCK:
2507 sigaddsetmask(&current->blocked, new_set);
2508 break;
2509 case SIG_UNBLOCK:
2510 sigdelsetmask(&current->blocked, new_set);
2511 break;
2512 case SIG_SETMASK:
2513 current->blocked.sig[0] = new_set;
2514 break;
2515 }
2516
2517 recalc_sigpending();
2518 spin_unlock_irq(&current->sighand->siglock);
2519 if (error)
2520 goto out;
2521 if (oset)
2522 goto set_old;
2523 } else if (oset) {
2524 old_set = current->blocked.sig[0];
2525 set_old:
2526 error = -EFAULT;
2527 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2528 goto out;
2529 }
2530 error = 0;
2531out:
2532 return error;
2533}
2534#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2535
2536#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002537SYSCALL_DEFINE4(rt_sigaction, int, sig,
2538 const struct sigaction __user *, act,
2539 struct sigaction __user *, oact,
2540 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
2542 struct k_sigaction new_sa, old_sa;
2543 int ret = -EINVAL;
2544
2545 /* XXX: Don't preclude handling different sized sigset_t's. */
2546 if (sigsetsize != sizeof(sigset_t))
2547 goto out;
2548
2549 if (act) {
2550 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2551 return -EFAULT;
2552 }
2553
2554 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2555
2556 if (!ret && oact) {
2557 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2558 return -EFAULT;
2559 }
2560out:
2561 return ret;
2562}
2563#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2564
2565#ifdef __ARCH_WANT_SYS_SGETMASK
2566
2567/*
2568 * For backwards compatibility. Functionality superseded by sigprocmask.
2569 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002570SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 /* SMP safe */
2573 return current->blocked.sig[0];
2574}
2575
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002576SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 int old;
2579
2580 spin_lock_irq(&current->sighand->siglock);
2581 old = current->blocked.sig[0];
2582
2583 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2584 sigmask(SIGSTOP)));
2585 recalc_sigpending();
2586 spin_unlock_irq(&current->sighand->siglock);
2587
2588 return old;
2589}
2590#endif /* __ARCH_WANT_SGETMASK */
2591
2592#ifdef __ARCH_WANT_SYS_SIGNAL
2593/*
2594 * For backwards compatibility. Functionality superseded by sigaction.
2595 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002596SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 struct k_sigaction new_sa, old_sa;
2599 int ret;
2600
2601 new_sa.sa.sa_handler = handler;
2602 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002603 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 ret = do_sigaction(sig, &new_sa, &old_sa);
2606
2607 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2608}
2609#endif /* __ARCH_WANT_SYS_SIGNAL */
2610
2611#ifdef __ARCH_WANT_SYS_PAUSE
2612
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002613SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 current->state = TASK_INTERRUPTIBLE;
2616 schedule();
2617 return -ERESTARTNOHAND;
2618}
2619
2620#endif
2621
David Woodhouse150256d2006-01-18 17:43:57 -08002622#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002623SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002624{
2625 sigset_t newset;
2626
2627 /* XXX: Don't preclude handling different sized sigset_t's. */
2628 if (sigsetsize != sizeof(sigset_t))
2629 return -EINVAL;
2630
2631 if (copy_from_user(&newset, unewset, sizeof(newset)))
2632 return -EFAULT;
2633 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2634
2635 spin_lock_irq(&current->sighand->siglock);
2636 current->saved_sigmask = current->blocked;
2637 current->blocked = newset;
2638 recalc_sigpending();
2639 spin_unlock_irq(&current->sighand->siglock);
2640
2641 current->state = TASK_INTERRUPTIBLE;
2642 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002643 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002644 return -ERESTARTNOHAND;
2645}
2646#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2647
David Howellsf269fdd2006-09-27 01:50:23 -07002648__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2649{
2650 return NULL;
2651}
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653void __init signals_init(void)
2654{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002655 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}