blob: b65879d4e08fc04e5f88605e1468492a0bf325a3 [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>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090025#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070026#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080029#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050031#define CREATE_TRACE_POINTS
32#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/param.h>
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040038#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * SLAB caches for signal bits.
42 */
43
Christoph Lametere18b8902006-12-06 20:33:20 -080044static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090046int print_fatal_signals __read_mostly;
47
Roland McGrath35de2542008-07-25 19:45:51 -070048static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070049{
Roland McGrath35de2542008-07-25 19:45:51 -070050 return t->sighand->action[sig - 1].sa.sa_handler;
51}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070052
Roland McGrath35de2542008-07-25 19:45:51 -070053static int sig_handler_ignored(void __user *handler, int sig)
54{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070055 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070056 return handler == SIG_IGN ||
57 (handler == SIG_DFL && sig_kernel_ignore(sig));
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070060static int sig_task_ignored(struct task_struct *t, int sig,
61 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Roland McGrath35de2542008-07-25 19:45:51 -070063 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Oleg Nesterovf008faf2009-04-02 16:58:02 -070065 handler = sig_handler(t, sig);
66
67 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070068 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070069 return 1;
70
71 return sig_handler_ignored(handler, sig);
72}
73
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070074static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070075{
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /*
77 * Blocked signals are never ignored, since the
78 * signal handler may change by the time it is
79 * unblocked.
80 */
Roland McGrath325d22d2007-11-12 15:41:55 -080081 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070084 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070085 return 0;
86
87 /*
88 * Tracers may want to know about even ignored signals.
89 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070090 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * Re-calculate pending state from the set of locally pending
95 * signals, globally pending signals, and blocked signals.
96 */
97static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
98{
99 unsigned long ready;
100 long i;
101
102 switch (_NSIG_WORDS) {
103 default:
104 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
105 ready |= signal->sig[i] &~ blocked->sig[i];
106 break;
107
108 case 4: ready = signal->sig[3] &~ blocked->sig[3];
109 ready |= signal->sig[2] &~ blocked->sig[2];
110 ready |= signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
113
114 case 2: ready = signal->sig[1] &~ blocked->sig[1];
115 ready |= signal->sig[0] &~ blocked->sig[0];
116 break;
117
118 case 1: ready = signal->sig[0] &~ blocked->sig[0];
119 }
120 return ready != 0;
121}
122
123#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
124
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700125static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 if (t->signal->group_stop_count > 0 ||
128 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700129 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700131 return 1;
132 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700133 /*
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
137 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700138 return 0;
139}
140
141/*
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 */
145void recalc_sigpending_and_wake(struct task_struct *t)
146{
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void recalc_sigpending(void)
152{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700156 clear_thread_flag(TIF_SIGPENDING);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/* Given the mask, find the first available signal that should be serviced. */
161
Davide Libenzifba2afa2007-05-10 22:23:13 -0700162int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 unsigned long i, *s, *m, x;
165 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 s = pending->signal.sig;
168 m = mask->sig;
169 switch (_NSIG_WORDS) {
170 default:
171 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
172 if ((x = *s &~ *m) != 0) {
173 sig = ffz(~x) + i*_NSIG_BPW + 1;
174 break;
175 }
176 break;
177
178 case 2: if ((x = s[0] &~ m[0]) != 0)
179 sig = 1;
180 else if ((x = s[1] &~ m[1]) != 0)
181 sig = _NSIG_BPW + 1;
182 else
183 break;
184 sig += ffz(~x);
185 break;
186
187 case 1: if ((x = *s &~ *m) != 0)
188 sig = ffz(~x) + 1;
189 break;
190 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return sig;
193}
194
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900195static inline void print_dropped_signal(int sig)
196{
197 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
198
199 if (!print_fatal_signals)
200 return;
201
202 if (!__ratelimit(&ratelimit_state))
203 return;
204
205 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
206 current->comm, current->pid, sig);
207}
208
David Howellsc69e8d92008-11-14 10:39:19 +1100209/*
210 * allocate a new signal queue record
211 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100212 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100213 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900214static struct sigqueue *
215__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800218 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800220 /*
David Howellsc69e8d92008-11-14 10:39:19 +1100221 * We won't get problems with the target's UID changing under us
222 * because changing it requires RCU be used, and if t != current, the
223 * caller must be holding the RCU readlock (by way of a spinlock) and
224 * we use RCU protection here
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800225 */
David Howellsd84f4f92008-11-14 10:39:23 +1100226 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800227 atomic_inc(&user->sigpending);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800230 atomic_read(&user->sigpending) <=
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900231 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900233 } else {
234 print_dropped_signal(sig);
235 }
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800238 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100239 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 } else {
241 INIT_LIST_HEAD(&q->list);
242 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100243 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
David Howellsd84f4f92008-11-14 10:39:23 +1100245
246 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Andrew Morton514a01b2006-02-03 03:04:41 -0800249static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 if (q->flags & SIGQUEUE_PREALLOC)
252 return;
253 atomic_dec(&q->user->sigpending);
254 free_uid(q->user);
255 kmem_cache_free(sigqueue_cachep, q);
256}
257
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800258void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct sigqueue *q;
261
262 sigemptyset(&queue->signal);
263 while (!list_empty(&queue->list)) {
264 q = list_entry(queue->list.next, struct sigqueue , list);
265 list_del_init(&q->list);
266 __sigqueue_free(q);
267 }
268}
269
270/*
271 * Flush all pending signals for a task.
272 */
David Howells3bcac022009-04-29 13:45:05 +0100273void __flush_signals(struct task_struct *t)
274{
275 clear_tsk_thread_flag(t, TIF_SIGPENDING);
276 flush_sigqueue(&t->pending);
277 flush_sigqueue(&t->signal->shared_pending);
278}
279
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800280void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 unsigned long flags;
283
284 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100285 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 spin_unlock_irqrestore(&t->sighand->siglock, flags);
287}
288
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400289static void __flush_itimer_signals(struct sigpending *pending)
290{
291 sigset_t signal, retain;
292 struct sigqueue *q, *n;
293
294 signal = pending->signal;
295 sigemptyset(&retain);
296
297 list_for_each_entry_safe(q, n, &pending->list, list) {
298 int sig = q->info.si_signo;
299
300 if (likely(q->info.si_code != SI_TIMER)) {
301 sigaddset(&retain, sig);
302 } else {
303 sigdelset(&signal, sig);
304 list_del_init(&q->list);
305 __sigqueue_free(q);
306 }
307 }
308
309 sigorsets(&pending->signal, &signal, &retain);
310}
311
312void flush_itimer_signals(void)
313{
314 struct task_struct *tsk = current;
315 unsigned long flags;
316
317 spin_lock_irqsave(&tsk->sighand->siglock, flags);
318 __flush_itimer_signals(&tsk->pending);
319 __flush_itimer_signals(&tsk->signal->shared_pending);
320 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
321}
322
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700323void ignore_signals(struct task_struct *t)
324{
325 int i;
326
327 for (i = 0; i < _NSIG; ++i)
328 t->sighand->action[i].sa.sa_handler = SIG_IGN;
329
330 flush_signals(t);
331}
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * Flush all handlers for a task.
335 */
336
337void
338flush_signal_handlers(struct task_struct *t, int force_default)
339{
340 int i;
341 struct k_sigaction *ka = &t->sighand->action[0];
342 for (i = _NSIG ; i != 0 ; i--) {
343 if (force_default || ka->sa.sa_handler != SIG_IGN)
344 ka->sa.sa_handler = SIG_DFL;
345 ka->sa.sa_flags = 0;
346 sigemptyset(&ka->sa.sa_mask);
347 ka++;
348 }
349}
350
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200351int unhandled_signal(struct task_struct *tsk, int sig)
352{
Roland McGrath445a91d2008-07-25 19:45:52 -0700353 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700354 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200355 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700356 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200357 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700358 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/* Notify the system that a driver wants to block all signals for this
363 * process, and wants to be notified if any signals at all were to be
364 * sent/acted upon. If the notifier routine returns non-zero, then the
365 * signal will be acted upon after all. If the notifier routine returns 0,
366 * then then signal will be blocked. Only one block per process is
367 * allowed. priv is a pointer to private data that the notifier routine
368 * can use to determine if the signal should be blocked or not. */
369
370void
371block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
372{
373 unsigned long flags;
374
375 spin_lock_irqsave(&current->sighand->siglock, flags);
376 current->notifier_mask = mask;
377 current->notifier_data = priv;
378 current->notifier = notifier;
379 spin_unlock_irqrestore(&current->sighand->siglock, flags);
380}
381
382/* Notify the system that blocking has ended. */
383
384void
385unblock_all_signals(void)
386{
387 unsigned long flags;
388
389 spin_lock_irqsave(&current->sighand->siglock, flags);
390 current->notifier = NULL;
391 current->notifier_data = NULL;
392 recalc_sigpending();
393 spin_unlock_irqrestore(&current->sighand->siglock, flags);
394}
395
Oleg Nesterov100360f2008-07-25 01:47:29 -0700396static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Collect the siginfo appropriate to this signal. Check if
402 * there is another siginfo for the same signal.
403 */
404 list_for_each_entry(q, &list->list, list) {
405 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700406 if (first)
407 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 first = q;
409 }
410 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700411
412 sigdelset(&list->signal, sig);
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700415still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 list_del_init(&first->list);
417 copy_siginfo(info, &first->info);
418 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Ok, it wasn't in the queue. This must be
421 a fast-pathed signal or we must have been
422 out of queue space. So zero out the info.
423 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 info->si_signo = sig;
425 info->si_errno = 0;
426 info->si_code = 0;
427 info->si_pid = 0;
428 info->si_uid = 0;
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
433 siginfo_t *info)
434{
Roland McGrath27d91e02006-09-29 02:00:31 -0700435 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (sig) {
438 if (current->notifier) {
439 if (sigismember(current->notifier_mask, sig)) {
440 if (!(current->notifier)(current->notifier_data)) {
441 clear_thread_flag(TIF_SIGPENDING);
442 return 0;
443 }
444 }
445 }
446
Oleg Nesterov100360f2008-07-25 01:47:29 -0700447 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 return sig;
451}
452
453/*
454 * Dequeue a signal and return the element to the caller, which is
455 * expected to free it.
456 *
457 * All callers have to hold the siglock.
458 */
459int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
460{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700461 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000462
463 /* We only dequeue private signals from ourselves, we don't let
464 * signalfd steal them
465 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700466 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800467 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 signr = __dequeue_signal(&tsk->signal->shared_pending,
469 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800470 /*
471 * itimer signal ?
472 *
473 * itimers are process shared and we restart periodic
474 * itimers in the signal delivery path to prevent DoS
475 * attacks in the high resolution timer case. This is
476 * compliant with the old way of self restarting
477 * itimers, as the SIGALRM is a legacy signal and only
478 * queued once. Changing the restart behaviour to
479 * restart the timer in the signal dequeue path is
480 * reducing the timer noise on heavy loaded !highres
481 * systems too.
482 */
483 if (unlikely(signr == SIGALRM)) {
484 struct hrtimer *tmr = &tsk->signal->real_timer;
485
486 if (!hrtimer_is_queued(tmr) &&
487 tsk->signal->it_real_incr.tv64 != 0) {
488 hrtimer_forward(tmr, tmr->base->get_time(),
489 tsk->signal->it_real_incr);
490 hrtimer_restart(tmr);
491 }
492 }
493 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700494
Davide Libenzib8fceee2007-09-20 12:40:16 -0700495 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700496 if (!signr)
497 return 0;
498
499 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800500 /*
501 * Set a marker that we have dequeued a stop signal. Our
502 * caller might release the siglock and then the pending
503 * stop signal it is about to process is no longer in the
504 * pending bitmasks, but must still be cleared by a SIGCONT
505 * (and overruled by a SIGKILL). So those cases clear this
506 * shared flag after we've set it. Note that this flag may
507 * remain set after the signal we return is ignored or
508 * handled. That doesn't matter because its only purpose
509 * is to alert stop-signal processing code when another
510 * processor has come along and cleared the flag.
511 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700512 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800513 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700514 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /*
516 * Release the siglock to ensure proper locking order
517 * of timer locks outside of siglocks. Note, we leave
518 * irqs disabled here, since the posix-timers code is
519 * about to disable them again anyway.
520 */
521 spin_unlock(&tsk->sighand->siglock);
522 do_schedule_next_timer(info);
523 spin_lock(&tsk->sighand->siglock);
524 }
525 return signr;
526}
527
528/*
529 * Tell a process that it has a new active signal..
530 *
531 * NOTE! we rely on the previous spin_lock to
532 * lock interrupts for us! We can only be called with
533 * "siglock" held, and the local interrupt must
534 * have been disabled when that got acquired!
535 *
536 * No need to set need_resched since signal event passing
537 * goes through ->blocked
538 */
539void signal_wake_up(struct task_struct *t, int resume)
540{
541 unsigned int mask;
542
543 set_tsk_thread_flag(t, TIF_SIGPENDING);
544
545 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500546 * For SIGKILL, we want to wake it up in the stopped/traced/killable
547 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 * executing another processor and just now entering stopped state.
549 * By using wake_up_state, we ensure the process will wake up and
550 * handle its death signal.
551 */
552 mask = TASK_INTERRUPTIBLE;
553 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500554 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!wake_up_state(t, mask))
556 kick_process(t);
557}
558
559/*
560 * Remove signals in mask from the pending set and queue.
561 * Returns 1 if any signals were found.
562 *
563 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800564 *
565 * This version takes a sigset mask and looks at all signals,
566 * not just those in the first mask word.
567 */
568static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
569{
570 struct sigqueue *q, *n;
571 sigset_t m;
572
573 sigandsets(&m, mask, &s->signal);
574 if (sigisemptyset(&m))
575 return 0;
576
577 signandsets(&s->signal, &s->signal, mask);
578 list_for_each_entry_safe(q, n, &s->list, list) {
579 if (sigismember(mask, q->info.si_signo)) {
580 list_del_init(&q->list);
581 __sigqueue_free(q);
582 }
583 }
584 return 1;
585}
586/*
587 * Remove signals in mask from the pending set and queue.
588 * Returns 1 if any signals were found.
589 *
590 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
592static int rm_from_queue(unsigned long mask, struct sigpending *s)
593{
594 struct sigqueue *q, *n;
595
596 if (!sigtestsetmask(&s->signal, mask))
597 return 0;
598
599 sigdelsetmask(&s->signal, mask);
600 list_for_each_entry_safe(q, n, &s->list, list) {
601 if (q->info.si_signo < SIGRTMIN &&
602 (mask & sigmask(q->info.si_signo))) {
603 list_del_init(&q->list);
604 __sigqueue_free(q);
605 }
606 }
607 return 1;
608}
609
Oleg Nesterov614c5172009-12-15 16:47:22 -0800610static inline int is_si_special(const struct siginfo *info)
611{
612 return info <= SEND_SIG_FORCED;
613}
614
615static inline bool si_fromuser(const struct siginfo *info)
616{
617 return info == SEND_SIG_NOINFO ||
618 (!is_si_special(info) && SI_FROMUSER(info));
619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * Bad permissions for sending the signal
David Howellsc69e8d92008-11-14 10:39:19 +1100623 * - the caller must hold at least the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625static int check_kill_permission(int sig, struct siginfo *info,
626 struct task_struct *t)
627{
David Howellsc69e8d92008-11-14 10:39:19 +1100628 const struct cred *cred = current_cred(), *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700629 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700630 int error;
631
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700632 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700633 return -EINVAL;
634
Oleg Nesterov614c5172009-12-15 16:47:22 -0800635 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700636 return 0;
637
638 error = audit_signal_info(sig, t); /* Let audit system see the signal */
639 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400641
David Howellsc69e8d92008-11-14 10:39:19 +1100642 tcred = __task_cred(t);
643 if ((cred->euid ^ tcred->suid) &&
644 (cred->euid ^ tcred->uid) &&
645 (cred->uid ^ tcred->suid) &&
646 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700647 !capable(CAP_KILL)) {
648 switch (sig) {
649 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700650 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700651 /*
652 * We don't return the error if sid == NULL. The
653 * task was unhashed, the caller must notice this.
654 */
655 if (!sid || sid == task_session(current))
656 break;
657 default:
658 return -EPERM;
659 }
660 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100661
Amy Griffise54dc242007-03-29 18:01:04 -0400662 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700666 * Handle magic process-wide effects of stop/continue signals. Unlike
667 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * time regardless of blocking, ignoring, or handling. This does the
669 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700670 * signals. The process stop is done as a signal action for SIG_DFL.
671 *
672 * Returns true if the signal should be actually delivered, otherwise
673 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700675static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700677 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct task_struct *t;
679
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700680 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700682 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700684 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
686 * This is a stop signal. Remove SIGCONT from all queues.
687 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700688 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 t = p;
690 do {
691 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700692 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700694 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * Remove all stop signals from all queues,
697 * and wake all threads.
698 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700699 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 t = p;
701 do {
702 unsigned int state;
703 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * If there is a handler for SIGCONT, we must make
706 * sure that no thread returns to user mode before
707 * we post the signal, in case it was the only
708 * thread eligible to run the signal handler--then
709 * it must not do anything between resuming and
710 * running the handler. With the TIF_SIGPENDING
711 * flag set, the thread will pause and acquire the
712 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700713 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *
715 * Wake up the stopped thread _after_ setting
716 * TIF_SIGPENDING
717 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500718 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
720 set_tsk_thread_flag(t, TIF_SIGPENDING);
721 state |= TASK_INTERRUPTIBLE;
722 }
723 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700724 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700726 /*
727 * Notify the parent with CLD_CONTINUED if we were stopped.
728 *
729 * If we were in the middle of a group stop, we pretend it
730 * was already finished, and then continued. Since SIGCHLD
731 * doesn't queue we report only CLD_STOPPED, as if the next
732 * CLD_CONTINUED was dropped.
733 */
734 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700735 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700736 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700737 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700738 why |= SIGNAL_CLD_STOPPED;
739
740 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700741 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700742 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700743 * will take ->siglock, notice SIGNAL_CLD_MASK, and
744 * notify its parent. See get_signal_to_deliver().
745 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700746 signal->flags = why | SIGNAL_STOP_CONTINUED;
747 signal->group_stop_count = 0;
748 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 } else {
750 /*
751 * We are not stopped, but there could be a stop
752 * signal in the middle of being processed after
753 * being removed from the queue. Clear that too.
754 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700755 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700758
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700759 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700762/*
763 * Test if P wants to take SIG. After we've checked all threads with this,
764 * it's equivalent to finding no threads not blocking SIG. Any threads not
765 * blocking SIG were ruled out because they are not running and already
766 * have pending signals. Such threads will dequeue from the shared queue
767 * as soon as they're available, so putting the signal on the shared queue
768 * will be equivalent to sending it to one such thread.
769 */
770static inline int wants_signal(int sig, struct task_struct *p)
771{
772 if (sigismember(&p->blocked, sig))
773 return 0;
774 if (p->flags & PF_EXITING)
775 return 0;
776 if (sig == SIGKILL)
777 return 1;
778 if (task_is_stopped_or_traced(p))
779 return 0;
780 return task_curr(p) || !signal_pending(p);
781}
782
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700783static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700784{
785 struct signal_struct *signal = p->signal;
786 struct task_struct *t;
787
788 /*
789 * Now find a thread we can wake up to take the signal off the queue.
790 *
791 * If the main thread wants the signal, it gets first crack.
792 * Probably the least surprising to the average bear.
793 */
794 if (wants_signal(sig, p))
795 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700796 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700797 /*
798 * There is just one thread and it does not need to be woken.
799 * It will dequeue unblocked signals before it runs again.
800 */
801 return;
802 else {
803 /*
804 * Otherwise try to find a suitable thread.
805 */
806 t = signal->curr_target;
807 while (!wants_signal(sig, t)) {
808 t = next_thread(t);
809 if (t == signal->curr_target)
810 /*
811 * No thread needs to be woken.
812 * Any eligible threads will see
813 * the signal in the queue soon.
814 */
815 return;
816 }
817 signal->curr_target = t;
818 }
819
820 /*
821 * Found a killable thread. If the signal will be fatal,
822 * then start taking the whole group down immediately.
823 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700824 if (sig_fatal(p, sig) &&
825 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700826 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700827 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700828 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700829 /*
830 * This signal will be fatal to the whole group.
831 */
832 if (!sig_kernel_coredump(sig)) {
833 /*
834 * Start a group exit and wake everybody up.
835 * This way we don't have other threads
836 * running and doing things after a slower
837 * thread has the fatal signal pending.
838 */
839 signal->flags = SIGNAL_GROUP_EXIT;
840 signal->group_exit_code = sig;
841 signal->group_stop_count = 0;
842 t = p;
843 do {
844 sigaddset(&t->pending.signal, SIGKILL);
845 signal_wake_up(t, 1);
846 } while_each_thread(p, t);
847 return;
848 }
849 }
850
851 /*
852 * The signal is already in the shared-pending queue.
853 * Tell the chosen thread to wake up and dequeue it.
854 */
855 signal_wake_up(t, sig == SIGKILL);
856 return;
857}
858
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700859static inline int legacy_queue(struct sigpending *signals, int sig)
860{
861 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
862}
863
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700864static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
865 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700867 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700868 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200869 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500871 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400872
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700873 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700874
875 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700876 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700877
878 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700880 * Short-circuit ignored signals and support queuing
881 * exactly one non-rt signal, so that we can get more
882 * detailed information about the cause of the signal.
883 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700884 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700885 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700886 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * fast-pathed signals for kernel-internal things like SIGSTOP
888 * or SIGKILL.
889 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800890 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto out_set;
892
893 /* Real-time signals must be queued if sent by sigqueue, or
894 some other real-time mechanism. It is implementation
895 defined whether kill() does so. We attempt to do so, on
896 the principle of least surprise, but since kill is not
897 allowed to fail with EAGAIN when low on memory we just
898 make sure at least one signal gets delivered and don't
899 pass on the info struct. */
900
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200901 if (sig < SIGRTMIN)
902 override_rlimit = (is_si_special(info) || info->si_code >= 0);
903 else
904 override_rlimit = 0;
905
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900906 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200907 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700909 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800911 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 q->info.si_signo = sig;
913 q->info.si_errno = 0;
914 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800915 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800916 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100917 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800919 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 q->info.si_signo = sig;
921 q->info.si_errno = 0;
922 q->info.si_code = SI_KERNEL;
923 q->info.si_pid = 0;
924 q->info.si_uid = 0;
925 break;
926 default:
927 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700928 if (from_ancestor_ns)
929 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 break;
931 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800932 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500933 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
934 /*
935 * Queue overflow, abort. We may abort if the
936 * signal was rt and sent by user using something
937 * other than kill().
938 */
939 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500941 } else {
942 /*
943 * This is a silent loss of information. We still
944 * send the signal, but the *info bits are lost.
945 */
946 trace_signal_lose_info(sig, group, info);
947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
949
950out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700951 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700952 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700953 complete_signal(sig, t, group);
954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700957static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
958 int group)
959{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700960 int from_ancestor_ns = 0;
961
962#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -0800963 from_ancestor_ns = si_fromuser(info) &&
964 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700965#endif
966
967 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700968}
969
Ingo Molnar45807a12007-07-15 23:40:10 -0700970static void print_fatal_signal(struct pt_regs *regs, int signr)
971{
972 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700973 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700974
Al Viroca5cd872007-10-29 04:31:16 +0000975#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100976 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700977 {
978 int i;
979 for (i = 0; i < 16; i++) {
980 unsigned char insn;
981
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100982 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700983 printk("%02x ", insn);
984 }
985 }
986#endif
987 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800988 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700989 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800990 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700991}
992
993static int __init setup_print_fatal_signals(char *str)
994{
995 get_option (&str, &print_fatal_signals);
996
997 return 1;
998}
999
1000__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001002int
1003__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1004{
1005 return send_signal(sig, info, p, 1);
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static int
1009specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1010{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001011 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001014int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1015 bool group)
1016{
1017 unsigned long flags;
1018 int ret = -ESRCH;
1019
1020 if (lock_task_sighand(p, &flags)) {
1021 ret = send_signal(sig, info, p, group);
1022 unlock_task_sighand(p, &flags);
1023 }
1024
1025 return ret;
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028/*
1029 * Force a signal that the process can't ignore: if necessary
1030 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001031 *
1032 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1033 * since we do not want to have a signal handler that was blocked
1034 * be invoked when user space had explicitly blocked it.
1035 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001036 * We don't want to have recursive SIGSEGV's etc, for example,
1037 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039int
1040force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1041{
1042 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001043 int ret, blocked, ignored;
1044 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001047 action = &t->sighand->action[sig-1];
1048 ignored = action->sa.sa_handler == SIG_IGN;
1049 blocked = sigismember(&t->blocked, sig);
1050 if (blocked || ignored) {
1051 action->sa.sa_handler = SIG_DFL;
1052 if (blocked) {
1053 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001054 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001057 if (action->sa.sa_handler == SIG_DFL)
1058 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ret = specific_send_sig_info(sig, info, t);
1060 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1061
1062 return ret;
1063}
1064
1065void
1066force_sig_specific(int sig, struct task_struct *t)
1067{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001068 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/*
1072 * Nuke all other threads in the group.
1073 */
1074void zap_other_threads(struct task_struct *p)
1075{
1076 struct task_struct *t;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 p->signal->group_stop_count = 0;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 for (t = next_thread(p); t != p; t = next_thread(t)) {
1081 /*
1082 * Don't bother with already dead threads
1083 */
1084 if (t->exit_state)
1085 continue;
1086
Andrea Arcangeli30e0fca62005-10-30 15:02:38 -08001087 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 signal_wake_up(t, 1);
1090 }
1091}
1092
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001093struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1094{
1095 struct sighand_struct *sighand;
1096
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001097 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001098 for (;;) {
1099 sighand = rcu_dereference(tsk->sighand);
1100 if (unlikely(sighand == NULL))
1101 break;
1102
1103 spin_lock_irqsave(&sighand->siglock, *flags);
1104 if (likely(sighand == tsk->sighand))
1105 break;
1106 spin_unlock_irqrestore(&sighand->siglock, *flags);
1107 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001108 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001109
1110 return sighand;
1111}
1112
David Howellsc69e8d92008-11-14 10:39:19 +11001113/*
1114 * send signal info to all the members of a group
1115 * - the caller must hold the RCU read lock at least
1116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1118{
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001119 int ret = check_kill_permission(sig, info, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001121 if (!ret && sig)
1122 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 return ret;
1125}
1126
1127/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001128 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001130 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001132int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 struct task_struct *p = NULL;
1135 int retval, success;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 success = 0;
1138 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001139 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 int err = group_send_sig_info(sig, info, p);
1141 success |= !err;
1142 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001143 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return success ? 0 : retval;
1145}
1146
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001147int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001149 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct task_struct *p;
1151
Ingo Molnare56d0902006-01-08 01:01:37 -08001152 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001153retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001154 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001155 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001157 if (unlikely(error == -ESRCH))
1158 /*
1159 * The task was unhashed in between, try again.
1160 * If it is dead, pid_task() will return NULL,
1161 * if we race with de_thread() it will find the
1162 * new leader.
1163 */
1164 goto retry;
1165 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001166 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return error;
1169}
1170
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001171int
1172kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001173{
1174 int error;
1175 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001176 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001177 rcu_read_unlock();
1178 return error;
1179}
1180
Eric W. Biederman2425c082006-10-02 02:17:28 -07001181/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1182int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001183 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001184{
1185 int ret = -EINVAL;
1186 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001187 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001188
1189 if (!valid_signal(sig))
1190 return ret;
1191
1192 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001193 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001194 if (!p) {
1195 ret = -ESRCH;
1196 goto out_unlock;
1197 }
David Howellsc69e8d92008-11-14 10:39:19 +11001198 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001199 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001200 euid != pcred->suid && euid != pcred->uid &&
1201 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001202 ret = -EPERM;
1203 goto out_unlock;
1204 }
David Quigley8f95dc52006-06-30 01:55:47 -07001205 ret = security_task_kill(p, info, sig, secid);
1206 if (ret)
1207 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001208 if (sig && p->sighand) {
1209 unsigned long flags;
1210 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001211 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001212 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1213 }
1214out_unlock:
1215 read_unlock(&tasklist_lock);
1216 return ret;
1217}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001218EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220/*
1221 * kill_something_info() interprets pid in interesting ways just like kill(2).
1222 *
1223 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1224 * is probably wrong. Should make it like BSD or SYSV.
1225 */
1226
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001227static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001229 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001230
1231 if (pid > 0) {
1232 rcu_read_lock();
1233 ret = kill_pid_info(sig, info, find_vpid(pid));
1234 rcu_read_unlock();
1235 return ret;
1236 }
1237
1238 read_lock(&tasklist_lock);
1239 if (pid != -1) {
1240 ret = __kill_pgrp_info(sig, info,
1241 pid ? find_vpid(-pid) : task_pgrp(current));
1242 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int retval = 0, count = 0;
1244 struct task_struct * p;
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001247 if (task_pid_vnr(p) > 1 &&
1248 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int err = group_send_sig_info(sig, info, p);
1250 ++count;
1251 if (err != -EPERM)
1252 retval = err;
1253 }
1254 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001255 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001257 read_unlock(&tasklist_lock);
1258
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262/*
1263 * These are for backward compatibility with the rest of the kernel source.
1264 */
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266int
1267send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1268{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /*
1270 * Make sure legacy kernel users don't send in bad values
1271 * (normal paths check this in check_kill_permission).
1272 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001273 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -EINVAL;
1275
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001276 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
1278
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001279#define __si_special(priv) \
1280 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282int
1283send_sig(int sig, struct task_struct *p, int priv)
1284{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001285 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288void
1289force_sig(int sig, struct task_struct *p)
1290{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001291 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
1295 * When things go south during signal handling, we
1296 * will force a SIGSEGV. And if the signal that caused
1297 * the problem was already a SIGSEGV, we'll want to
1298 * make sure we don't even try to deliver the signal..
1299 */
1300int
1301force_sigsegv(int sig, struct task_struct *p)
1302{
1303 if (sig == SIGSEGV) {
1304 unsigned long flags;
1305 spin_lock_irqsave(&p->sighand->siglock, flags);
1306 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1307 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1308 }
1309 force_sig(SIGSEGV, p);
1310 return 0;
1311}
1312
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001313int kill_pgrp(struct pid *pid, int sig, int priv)
1314{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001315 int ret;
1316
1317 read_lock(&tasklist_lock);
1318 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1319 read_unlock(&tasklist_lock);
1320
1321 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001322}
1323EXPORT_SYMBOL(kill_pgrp);
1324
1325int kill_pid(struct pid *pid, int sig, int priv)
1326{
1327 return kill_pid_info(sig, __si_special(priv), pid);
1328}
1329EXPORT_SYMBOL(kill_pid);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331/*
1332 * These functions support sending signals using preallocated sigqueue
1333 * structures. This is needed "because realtime applications cannot
1334 * afford to lose notifications of asynchronous events, like timer
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001335 * expirations or I/O completions". In the case of Posix Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * we allocate the sigqueue structure from the timer_create. If this
1337 * allocation fails we are able to report the failure to the application
1338 * with an EAGAIN error.
1339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340struct sigqueue *sigqueue_alloc(void)
1341{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001342 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001344 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001346
1347 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350void sigqueue_free(struct sigqueue *q)
1351{
1352 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001353 spinlock_t *lock = &current->sighand->siglock;
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1356 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001357 * We must hold ->siglock while testing q->list
1358 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001359 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001361 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001362 q->flags &= ~SIGQUEUE_PREALLOC;
1363 /*
1364 * If it is queued it will be freed when dequeued,
1365 * like the "regular" sigqueue.
1366 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001367 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001368 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001369 spin_unlock_irqrestore(lock, flags);
1370
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001371 if (q)
1372 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001375int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001376{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001377 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001378 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001379 unsigned long flags;
1380 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001381
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001382 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001383
1384 ret = -1;
1385 if (!likely(lock_task_sighand(t, &flags)))
1386 goto ret;
1387
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001388 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001389 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001390 goto out;
1391
1392 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001393 if (unlikely(!list_empty(&q->list))) {
1394 /*
1395 * If an SI_TIMER entry is already queue just increment
1396 * the overrun count.
1397 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001398 BUG_ON(q->info.si_code != SI_TIMER);
1399 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001400 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001401 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001402 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001403
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001404 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001405 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001406 list_add_tail(&q->list, &pending->list);
1407 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001408 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001409out:
1410 unlock_task_sighand(t, &flags);
1411ret:
1412 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001413}
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * Let a parent know about the death of a child.
1417 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001418 *
1419 * Returns -1 if our parent ignored us and so we've switched to
1420 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001422int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
1424 struct siginfo info;
1425 unsigned long flags;
1426 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001427 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
1429 BUG_ON(sig == -1);
1430
1431 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001432 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001434 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1436
1437 info.si_signo = sig;
1438 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001439 /*
1440 * we are under tasklist_lock here so our parent is tied to
1441 * us and cannot exit and release its namespace.
1442 *
1443 * the only it can is to switch its nsproxy with sys_unshare,
1444 * bu uncharing pid namespaces is not allowed, so we'll always
1445 * see relevant namespace
1446 *
1447 * write_lock() currently calls preempt_disable() which is the
1448 * same as rcu_read_lock(), but according to Oleg, this is not
1449 * correct to rely on this
1450 */
1451 rcu_read_lock();
1452 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001453 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001454 rcu_read_unlock();
1455
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001456 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1457 tsk->signal->utime));
1458 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1459 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 info.si_status = tsk->exit_code & 0x7f;
1462 if (tsk->exit_code & 0x80)
1463 info.si_code = CLD_DUMPED;
1464 else if (tsk->exit_code & 0x7f)
1465 info.si_code = CLD_KILLED;
1466 else {
1467 info.si_code = CLD_EXITED;
1468 info.si_status = tsk->exit_code >> 8;
1469 }
1470
1471 psig = tsk->parent->sighand;
1472 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001473 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1475 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1476 /*
1477 * We are exiting and our parent doesn't care. POSIX.1
1478 * defines special semantics for setting SIGCHLD to SIG_IGN
1479 * or setting the SA_NOCLDWAIT flag: we should be reaped
1480 * automatically and not left for our parent's wait4 call.
1481 * Rather than having the parent do it as a magic kind of
1482 * signal handler, we just set this to tell do_exit that we
1483 * can be cleaned up without becoming a zombie. Note that
1484 * we still call __wake_up_parent in this case, because a
1485 * blocked sys_wait4 might now return -ECHILD.
1486 *
1487 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1488 * is implementation-defined: we do (if you don't want
1489 * it, just use SIG_IGN instead).
1490 */
Roland McGrath1b046242008-08-19 20:37:07 -07001491 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001493 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001495 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 __group_send_sig_info(sig, &info, tsk->parent);
1497 __wake_up_parent(tsk, tsk->parent);
1498 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001499
Roland McGrath1b046242008-08-19 20:37:07 -07001500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001503static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct siginfo info;
1506 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001507 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct sighand_struct *sighand;
1509
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001510 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001511 parent = tsk->parent;
1512 else {
1513 tsk = tsk->group_leader;
1514 parent = tsk->real_parent;
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 info.si_signo = SIGCHLD;
1518 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001519 /*
1520 * see comment in do_notify_parent() abot the following 3 lines
1521 */
1522 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001523 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001524 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001525 rcu_read_unlock();
1526
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001527 info.si_utime = cputime_to_clock_t(tsk->utime);
1528 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
1530 info.si_code = why;
1531 switch (why) {
1532 case CLD_CONTINUED:
1533 info.si_status = SIGCONT;
1534 break;
1535 case CLD_STOPPED:
1536 info.si_status = tsk->signal->group_exit_code & 0x7f;
1537 break;
1538 case CLD_TRAPPED:
1539 info.si_status = tsk->exit_code & 0x7f;
1540 break;
1541 default:
1542 BUG();
1543 }
1544
1545 sighand = parent->sighand;
1546 spin_lock_irqsave(&sighand->siglock, flags);
1547 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1548 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1549 __group_send_sig_info(SIGCHLD, &info, parent);
1550 /*
1551 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1552 */
1553 __wake_up_parent(tsk, parent);
1554 spin_unlock_irqrestore(&sighand->siglock, flags);
1555}
1556
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001557static inline int may_ptrace_stop(void)
1558{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001559 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001560 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001561 /*
1562 * Are we in the middle of do_coredump?
1563 * If so and our tracer is also part of the coredump stopping
1564 * is a deadlock situation, and pointless because our tracer
1565 * is dead so don't allow us to stop.
1566 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001567 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001568 * is safe to enter schedule().
1569 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001570 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001571 unlikely(current->mm == current->parent->mm))
1572 return 0;
1573
1574 return 1;
1575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001578 * Return nonzero if there is a SIGKILL that should be waking us up.
1579 * Called with the siglock held.
1580 */
1581static int sigkill_pending(struct task_struct *tsk)
1582{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001583 return sigismember(&tsk->pending.signal, SIGKILL) ||
1584 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001585}
1586
1587/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 * This must be called with current->sighand->siglock held.
1589 *
1590 * This should be the path for all ptrace stops.
1591 * We always set current->last_siginfo while stopped here.
1592 * That makes it a way to test a stopped process for
1593 * being ptrace-stopped vs being job-control-stopped.
1594 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001595 * If we actually decide not to stop at all because the tracer
1596 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001598static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Roland McGrath1a669c22008-02-06 01:37:37 -08001600 if (arch_ptrace_stop_needed(exit_code, info)) {
1601 /*
1602 * The arch code has something special to do before a
1603 * ptrace stop. This is allowed to block, e.g. for faults
1604 * on user stack pages. We can't keep the siglock while
1605 * calling arch_ptrace_stop, so we must release it now.
1606 * To preserve proper semantics, we must do this before
1607 * any signal bookkeeping like checking group_stop_count.
1608 * Meanwhile, a SIGKILL could come in before we retake the
1609 * siglock. That must prevent us from sleeping in TASK_TRACED.
1610 * So after regaining the lock, we must check for SIGKILL.
1611 */
1612 spin_unlock_irq(&current->sighand->siglock);
1613 arch_ptrace_stop(exit_code, info);
1614 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001615 if (sigkill_pending(current))
1616 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 /*
1620 * If there is a group stop in progress,
1621 * we must participate in the bookkeeping.
1622 */
1623 if (current->signal->group_stop_count > 0)
1624 --current->signal->group_stop_count;
1625
1626 current->last_siginfo = info;
1627 current->exit_code = exit_code;
1628
1629 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001630 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 spin_unlock_irq(&current->sighand->siglock);
1632 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001633 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001634 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001635 /*
1636 * Don't want to allow preemption here, because
1637 * sys_ptrace() needs this task to be inactive.
1638 *
1639 * XXX: implement read_unlock_no_resched().
1640 */
1641 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001643 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 schedule();
1645 } else {
1646 /*
1647 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001648 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001650 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001651 if (clear_code)
1652 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001653 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655
1656 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001657 * While in TASK_TRACED, we were considered "frozen enough".
1658 * Now that we woke up, it's crucial if we're supposed to be
1659 * frozen that we freeze now before running anything substantial.
1660 */
1661 try_to_freeze();
1662
1663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 * We are back. Now reacquire the siglock before touching
1665 * last_siginfo, so that we are sure to have synchronized with
1666 * any signal-sending on another CPU that wants to examine it.
1667 */
1668 spin_lock_irq(&current->sighand->siglock);
1669 current->last_siginfo = NULL;
1670
1671 /*
1672 * Queued signals ignored us while we were stopped for tracing.
1673 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001674 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001676 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
1679void ptrace_notify(int exit_code)
1680{
1681 siginfo_t info;
1682
1683 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1684
1685 memset(&info, 0, sizeof info);
1686 info.si_signo = SIGTRAP;
1687 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001688 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001689 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /* Let the debugger run. */
1692 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001693 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 spin_unlock_irq(&current->sighand->siglock);
1695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/*
1698 * This performs the stopping for SIGSTOP and other stop signals.
1699 * We have to stop all threads in the thread group.
1700 * Returns nonzero if we've actually stopped and released the siglock.
1701 * Returns zero if we didn't stop and still hold the siglock.
1702 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001703static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704{
1705 struct signal_struct *sig = current->signal;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001706 int notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001708 if (!sig->group_stop_count) {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001709 struct task_struct *t;
1710
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001711 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001712 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001713 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001716 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001718 sig->group_exit_code = signr;
1719
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001720 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001721 for (t = next_thread(current); t != current; t = next_thread(t))
1722 /*
1723 * Setting state to TASK_STOPPED for a group
1724 * stop is always done with the siglock held,
1725 * so this check has no races.
1726 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001727 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001728 !task_is_stopped_or_traced(t)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001729 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001730 signal_wake_up(t, 0);
1731 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001732 }
1733 /*
1734 * If there are no other threads in the group, or if there is
1735 * a group stop in progress and we are the last to stop, report
1736 * to the parent. When ptraced, every thread reports itself.
1737 */
1738 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1739 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1740 /*
1741 * tracehook_notify_jctl() can drop and reacquire siglock, so
1742 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1743 * or SIGKILL comes in between ->group_stop_count == 0.
1744 */
1745 if (sig->group_stop_count) {
1746 if (!--sig->group_stop_count)
1747 sig->flags = SIGNAL_STOP_STOPPED;
1748 current->exit_code = sig->group_exit_code;
1749 __set_current_state(TASK_STOPPED);
1750 }
1751 spin_unlock_irq(&current->sighand->siglock);
1752
1753 if (notify) {
1754 read_lock(&tasklist_lock);
1755 do_notify_parent_cldstop(current, notify);
1756 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001759 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1760 do {
1761 schedule();
1762 } while (try_to_freeze());
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001763
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001764 tracehook_finish_jctl();
1765 current->exit_code = 0;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 1;
1768}
1769
Roland McGrath18c98b62008-04-17 18:44:38 -07001770static int ptrace_signal(int signr, siginfo_t *info,
1771 struct pt_regs *regs, void *cookie)
1772{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001773 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001774 return signr;
1775
1776 ptrace_signal_deliver(regs, cookie);
1777
1778 /* Let the debugger run. */
1779 ptrace_stop(signr, 0, info);
1780
1781 /* We're back. Did the debugger cancel the sig? */
1782 signr = current->exit_code;
1783 if (signr == 0)
1784 return signr;
1785
1786 current->exit_code = 0;
1787
1788 /* Update the siginfo structure if the signal has
1789 changed. If the debugger wanted something
1790 specific in the siginfo structure then it should
1791 have updated *info via PTRACE_SETSIGINFO. */
1792 if (signr != info->si_signo) {
1793 info->si_signo = signr;
1794 info->si_errno = 0;
1795 info->si_code = SI_USER;
1796 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001797 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001798 }
1799
1800 /* If the (new) signal is now blocked, requeue it. */
1801 if (sigismember(&current->blocked, signr)) {
1802 specific_send_sig_info(signr, info, current);
1803 signr = 0;
1804 }
1805
1806 return signr;
1807}
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1810 struct pt_regs *regs, void *cookie)
1811{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001812 struct sighand_struct *sighand = current->sighand;
1813 struct signal_struct *signal = current->signal;
1814 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001816relock:
1817 /*
1818 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1819 * While in TASK_STOPPED, we were considered "frozen enough".
1820 * Now that we woke up, it's crucial if we're supposed to be
1821 * frozen that we freeze now before running anything substantial.
1822 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001823 try_to_freeze();
1824
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001825 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001826 /*
1827 * Every stopped thread goes here after wakeup. Check to see if
1828 * we should notify the parent, prepare_signal(SIGCONT) encodes
1829 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1830 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001831 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1832 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001833 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001834 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001835
1836 why = tracehook_notify_jctl(why, CLD_CONTINUED);
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001837 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001838
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001839 if (why) {
1840 read_lock(&tasklist_lock);
1841 do_notify_parent_cldstop(current->group_leader, why);
1842 read_unlock(&tasklist_lock);
1843 }
Oleg Nesterove4420552008-04-30 00:52:44 -07001844 goto relock;
1845 }
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 for (;;) {
1848 struct k_sigaction *ka;
1849
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001850 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001851 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto relock;
1853
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001854 /*
1855 * Tracing can induce an artifical signal and choose sigaction.
1856 * The return value in @signr determines the default action,
1857 * but @info->si_signo is the signal number we will report.
1858 */
1859 signr = tracehook_get_signal(current, regs, info, return_ka);
1860 if (unlikely(signr < 0))
1861 goto relock;
1862 if (unlikely(signr != 0))
1863 ka = return_ka;
1864 else {
1865 signr = dequeue_signal(current, &current->blocked,
1866 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Roland McGrath18c98b62008-04-17 18:44:38 -07001868 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001869 break; /* will return 0 */
1870
1871 if (signr != SIGKILL) {
1872 signr = ptrace_signal(signr, info,
1873 regs, cookie);
1874 if (!signr)
1875 continue;
1876 }
1877
1878 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
1880
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05001881 /* Trace actually delivered signals. */
1882 trace_signal_deliver(signr, info, ka);
1883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1885 continue;
1886 if (ka->sa.sa_handler != SIG_DFL) {
1887 /* Run the handler. */
1888 *return_ka = *ka;
1889
1890 if (ka->sa.sa_flags & SA_ONESHOT)
1891 ka->sa.sa_handler = SIG_DFL;
1892
1893 break; /* will return non-zero "signr" value */
1894 }
1895
1896 /*
1897 * Now we are doing the default action for this signal.
1898 */
1899 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1900 continue;
1901
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001902 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001903 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001904 * Container-init gets no signals it doesn't want from same
1905 * container.
1906 *
1907 * Note that if global/container-init sees a sig_kernel_only()
1908 * signal here, the signal must have been generated internally
1909 * or must have come from an ancestor namespace. In either
1910 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001911 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001912 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001913 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 continue;
1915
1916 if (sig_kernel_stop(signr)) {
1917 /*
1918 * The default action is to stop all threads in
1919 * the thread group. The job control signals
1920 * do nothing in an orphaned pgrp, but SIGSTOP
1921 * always works. Note that siglock needs to be
1922 * dropped during the call to is_orphaned_pgrp()
1923 * because of lock ordering with tasklist_lock.
1924 * This allows an intervening SIGCONT to be posted.
1925 * We need to check for that and bail out if necessary.
1926 */
1927 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001928 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* signals can be posted during this window */
1931
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001932 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto relock;
1934
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001935 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001938 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /* It released the siglock. */
1940 goto relock;
1941 }
1942
1943 /*
1944 * We didn't actually stop, due to a race
1945 * with SIGCONT or something like that.
1946 */
1947 continue;
1948 }
1949
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001950 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /*
1953 * Anything else is fatal, maybe with a core dump.
1954 */
1955 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001958 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001959 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * If it was able to dump core, this kills all
1962 * other threads in the group and synchronizes with
1963 * their demise. If we lost the race with another
1964 * thread getting here, it set group_exit_code
1965 * first and our do_group_exit call below will use
1966 * that value and ignore the one we pass it.
1967 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001968 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970
1971 /*
1972 * Death signals, no core dump.
1973 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001974 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* NOTREACHED */
1976 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001977 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return signr;
1979}
1980
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001981void exit_signals(struct task_struct *tsk)
1982{
1983 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001984 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001985
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001986 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1987 tsk->flags |= PF_EXITING;
1988 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001989 }
1990
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001991 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001992 /*
1993 * From now this task is not visible for group-wide signals,
1994 * see wants_signal(), do_signal_stop().
1995 */
1996 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001997 if (!signal_pending(tsk))
1998 goto out;
1999
2000 /* It could be that __group_complete_signal() choose us to
2001 * notify about group-wide signal. Another thread should be
2002 * woken now to take the signal since we will not.
2003 */
2004 for (t = tsk; (t = next_thread(t)) != tsk; )
2005 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2006 recalc_sigpending_and_wake(t);
2007
2008 if (unlikely(tsk->signal->group_stop_count) &&
2009 !--tsk->signal->group_stop_count) {
2010 tsk->signal->flags = SIGNAL_STOP_STOPPED;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002011 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002012 }
2013out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002014 spin_unlock_irq(&tsk->sighand->siglock);
2015
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002016 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002017 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002018 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002019 read_unlock(&tasklist_lock);
2020 }
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023EXPORT_SYMBOL(recalc_sigpending);
2024EXPORT_SYMBOL_GPL(dequeue_signal);
2025EXPORT_SYMBOL(flush_signals);
2026EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027EXPORT_SYMBOL(send_sig);
2028EXPORT_SYMBOL(send_sig_info);
2029EXPORT_SYMBOL(sigprocmask);
2030EXPORT_SYMBOL(block_all_signals);
2031EXPORT_SYMBOL(unblock_all_signals);
2032
2033
2034/*
2035 * System call entry points.
2036 */
2037
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002038SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
2040 struct restart_block *restart = &current_thread_info()->restart_block;
2041 return restart->fn(restart);
2042}
2043
2044long do_no_restart_syscall(struct restart_block *param)
2045{
2046 return -EINTR;
2047}
2048
2049/*
2050 * We don't need to get the kernel lock - this is all local to this
2051 * particular thread.. (and that's good, because this is _heavily_
2052 * used by various programs)
2053 */
2054
2055/*
2056 * This is also useful for kernel threads that want to temporarily
2057 * (or permanently) block certain signals.
2058 *
2059 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2060 * interface happily blocks "unblockable" signals like SIGKILL
2061 * and friends.
2062 */
2063int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2064{
2065 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002068 if (oldset)
2069 *oldset = current->blocked;
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 error = 0;
2072 switch (how) {
2073 case SIG_BLOCK:
2074 sigorsets(&current->blocked, &current->blocked, set);
2075 break;
2076 case SIG_UNBLOCK:
2077 signandsets(&current->blocked, &current->blocked, set);
2078 break;
2079 case SIG_SETMASK:
2080 current->blocked = *set;
2081 break;
2082 default:
2083 error = -EINVAL;
2084 }
2085 recalc_sigpending();
2086 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return error;
2089}
2090
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002091SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2092 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
2094 int error = -EINVAL;
2095 sigset_t old_set, new_set;
2096
2097 /* XXX: Don't preclude handling different sized sigset_t's. */
2098 if (sigsetsize != sizeof(sigset_t))
2099 goto out;
2100
2101 if (set) {
2102 error = -EFAULT;
2103 if (copy_from_user(&new_set, set, sizeof(*set)))
2104 goto out;
2105 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2106
2107 error = sigprocmask(how, &new_set, &old_set);
2108 if (error)
2109 goto out;
2110 if (oset)
2111 goto set_old;
2112 } else if (oset) {
2113 spin_lock_irq(&current->sighand->siglock);
2114 old_set = current->blocked;
2115 spin_unlock_irq(&current->sighand->siglock);
2116
2117 set_old:
2118 error = -EFAULT;
2119 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2120 goto out;
2121 }
2122 error = 0;
2123out:
2124 return error;
2125}
2126
2127long do_sigpending(void __user *set, unsigned long sigsetsize)
2128{
2129 long error = -EINVAL;
2130 sigset_t pending;
2131
2132 if (sigsetsize > sizeof(sigset_t))
2133 goto out;
2134
2135 spin_lock_irq(&current->sighand->siglock);
2136 sigorsets(&pending, &current->pending.signal,
2137 &current->signal->shared_pending.signal);
2138 spin_unlock_irq(&current->sighand->siglock);
2139
2140 /* Outside the lock because only this thread touches it. */
2141 sigandsets(&pending, &current->blocked, &pending);
2142
2143 error = -EFAULT;
2144 if (!copy_to_user(set, &pending, sigsetsize))
2145 error = 0;
2146
2147out:
2148 return error;
2149}
2150
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002151SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 return do_sigpending(set, sigsetsize);
2154}
2155
2156#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2157
2158int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2159{
2160 int err;
2161
2162 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2163 return -EFAULT;
2164 if (from->si_code < 0)
2165 return __copy_to_user(to, from, sizeof(siginfo_t))
2166 ? -EFAULT : 0;
2167 /*
2168 * If you change siginfo_t structure, please be sure
2169 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002170 * Please remember to update the signalfd_copyinfo() function
2171 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * It should never copy any pad contained in the structure
2173 * to avoid security leaks, but must copy the generic
2174 * 3 ints plus the relevant union member.
2175 */
2176 err = __put_user(from->si_signo, &to->si_signo);
2177 err |= __put_user(from->si_errno, &to->si_errno);
2178 err |= __put_user((short)from->si_code, &to->si_code);
2179 switch (from->si_code & __SI_MASK) {
2180 case __SI_KILL:
2181 err |= __put_user(from->si_pid, &to->si_pid);
2182 err |= __put_user(from->si_uid, &to->si_uid);
2183 break;
2184 case __SI_TIMER:
2185 err |= __put_user(from->si_tid, &to->si_tid);
2186 err |= __put_user(from->si_overrun, &to->si_overrun);
2187 err |= __put_user(from->si_ptr, &to->si_ptr);
2188 break;
2189 case __SI_POLL:
2190 err |= __put_user(from->si_band, &to->si_band);
2191 err |= __put_user(from->si_fd, &to->si_fd);
2192 break;
2193 case __SI_FAULT:
2194 err |= __put_user(from->si_addr, &to->si_addr);
2195#ifdef __ARCH_SI_TRAPNO
2196 err |= __put_user(from->si_trapno, &to->si_trapno);
2197#endif
2198 break;
2199 case __SI_CHLD:
2200 err |= __put_user(from->si_pid, &to->si_pid);
2201 err |= __put_user(from->si_uid, &to->si_uid);
2202 err |= __put_user(from->si_status, &to->si_status);
2203 err |= __put_user(from->si_utime, &to->si_utime);
2204 err |= __put_user(from->si_stime, &to->si_stime);
2205 break;
2206 case __SI_RT: /* This is not generated by the kernel as of now. */
2207 case __SI_MESGQ: /* But this is */
2208 err |= __put_user(from->si_pid, &to->si_pid);
2209 err |= __put_user(from->si_uid, &to->si_uid);
2210 err |= __put_user(from->si_ptr, &to->si_ptr);
2211 break;
2212 default: /* this is just in case for now ... */
2213 err |= __put_user(from->si_pid, &to->si_pid);
2214 err |= __put_user(from->si_uid, &to->si_uid);
2215 break;
2216 }
2217 return err;
2218}
2219
2220#endif
2221
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002222SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2223 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2224 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
2226 int ret, sig;
2227 sigset_t these;
2228 struct timespec ts;
2229 siginfo_t info;
2230 long timeout = 0;
2231
2232 /* XXX: Don't preclude handling different sized sigset_t's. */
2233 if (sigsetsize != sizeof(sigset_t))
2234 return -EINVAL;
2235
2236 if (copy_from_user(&these, uthese, sizeof(these)))
2237 return -EFAULT;
2238
2239 /*
2240 * Invert the set of allowed signals to get those we
2241 * want to block.
2242 */
2243 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2244 signotset(&these);
2245
2246 if (uts) {
2247 if (copy_from_user(&ts, uts, sizeof(ts)))
2248 return -EFAULT;
2249 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2250 || ts.tv_sec < 0)
2251 return -EINVAL;
2252 }
2253
2254 spin_lock_irq(&current->sighand->siglock);
2255 sig = dequeue_signal(current, &these, &info);
2256 if (!sig) {
2257 timeout = MAX_SCHEDULE_TIMEOUT;
2258 if (uts)
2259 timeout = (timespec_to_jiffies(&ts)
2260 + (ts.tv_sec || ts.tv_nsec));
2261
2262 if (timeout) {
2263 /* None ready -- temporarily unblock those we're
2264 * interested while we are sleeping in so that we'll
2265 * be awakened when they arrive. */
2266 current->real_blocked = current->blocked;
2267 sigandsets(&current->blocked, &current->blocked, &these);
2268 recalc_sigpending();
2269 spin_unlock_irq(&current->sighand->siglock);
2270
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002271 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 spin_lock_irq(&current->sighand->siglock);
2274 sig = dequeue_signal(current, &these, &info);
2275 current->blocked = current->real_blocked;
2276 siginitset(&current->real_blocked, 0);
2277 recalc_sigpending();
2278 }
2279 }
2280 spin_unlock_irq(&current->sighand->siglock);
2281
2282 if (sig) {
2283 ret = sig;
2284 if (uinfo) {
2285 if (copy_siginfo_to_user(uinfo, &info))
2286 ret = -EFAULT;
2287 }
2288 } else {
2289 ret = -EAGAIN;
2290 if (timeout)
2291 ret = -EINTR;
2292 }
2293
2294 return ret;
2295}
2296
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002297SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct siginfo info;
2300
2301 info.si_signo = sig;
2302 info.si_errno = 0;
2303 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002304 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002305 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
2307 return kill_something_info(sig, &info, pid);
2308}
2309
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002310static int
2311do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002312{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002313 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002314 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002315
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002316 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002317 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002318 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002319 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002320 /*
2321 * The null signal is a permissions and process existence
2322 * probe. No signal is actually delivered.
2323 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002324 if (!error && sig) {
2325 error = do_send_sig_info(sig, info, p, false);
2326 /*
2327 * If lock_task_sighand() failed we pretend the task
2328 * dies after receiving the signal. The window is tiny,
2329 * and the signal is private anyway.
2330 */
2331 if (unlikely(error == -ESRCH))
2332 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002333 }
2334 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002335 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002336
2337 return error;
2338}
2339
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002340static int do_tkill(pid_t tgid, pid_t pid, int sig)
2341{
2342 struct siginfo info;
2343
2344 info.si_signo = sig;
2345 info.si_errno = 0;
2346 info.si_code = SI_TKILL;
2347 info.si_pid = task_tgid_vnr(current);
2348 info.si_uid = current_uid();
2349
2350 return do_send_specific(tgid, pid, sig, &info);
2351}
2352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353/**
2354 * sys_tgkill - send signal to one specific thread
2355 * @tgid: the thread group ID of the thread
2356 * @pid: the PID of the thread
2357 * @sig: signal to be sent
2358 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002359 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 * exists but it's not belonging to the target process anymore. This
2361 * method solves the problem of threads exiting and PIDs getting reused.
2362 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002363SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /* This is only valid for single tasks */
2366 if (pid <= 0 || tgid <= 0)
2367 return -EINVAL;
2368
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002369 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371
2372/*
2373 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2374 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002375SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* This is only valid for single tasks */
2378 if (pid <= 0)
2379 return -EINVAL;
2380
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002381 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
2383
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002384SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2385 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386{
2387 siginfo_t info;
2388
2389 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2390 return -EFAULT;
2391
2392 /* Not even root can pretend to send signals from the kernel.
2393 Nor can they impersonate a kill(), which adds source info. */
2394 if (info.si_code >= 0)
2395 return -EPERM;
2396 info.si_signo = sig;
2397
2398 /* POSIX.1b doesn't mention process groups. */
2399 return kill_proc_info(sig, &info, pid);
2400}
2401
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002402long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2403{
2404 /* This is only valid for single tasks */
2405 if (pid <= 0 || tgid <= 0)
2406 return -EINVAL;
2407
2408 /* Not even root can pretend to send signals from the kernel.
2409 Nor can they impersonate a kill(), which adds source info. */
2410 if (info->si_code >= 0)
2411 return -EPERM;
2412 info->si_signo = sig;
2413
2414 return do_send_specific(tgid, pid, sig, info);
2415}
2416
2417SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2418 siginfo_t __user *, uinfo)
2419{
2420 siginfo_t info;
2421
2422 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2423 return -EFAULT;
2424
2425 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2426}
2427
Oleg Nesterov88531f72006-03-28 16:11:24 -08002428int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002430 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002432 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002434 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return -EINVAL;
2436
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002437 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (oact)
2441 *oact = *k;
2442
2443 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002444 sigdelsetmask(&act->sa.sa_mask,
2445 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002446 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /*
2448 * POSIX 3.3.1.3:
2449 * "Setting a signal action to SIG_IGN for a signal that is
2450 * pending shall cause the pending signal to be discarded,
2451 * whether or not it is blocked."
2452 *
2453 * "Setting a signal action to SIG_DFL for a signal that is
2454 * pending and whose default action is to ignore the signal
2455 * (for example, SIGCHLD), shall cause the pending signal to
2456 * be discarded, whether or not it is blocked"
2457 */
Roland McGrath35de2542008-07-25 19:45:51 -07002458 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002459 sigemptyset(&mask);
2460 sigaddset(&mask, sig);
2461 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002463 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 t = next_thread(t);
2465 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
2469 spin_unlock_irq(&current->sighand->siglock);
2470 return 0;
2471}
2472
2473int
2474do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2475{
2476 stack_t oss;
2477 int error;
2478
Linus Torvalds0083fc22009-08-01 10:34:56 -07002479 oss.ss_sp = (void __user *) current->sas_ss_sp;
2480 oss.ss_size = current->sas_ss_size;
2481 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 if (uss) {
2484 void __user *ss_sp;
2485 size_t ss_size;
2486 int ss_flags;
2487
2488 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002489 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2490 goto out;
2491 error = __get_user(ss_sp, &uss->ss_sp) |
2492 __get_user(ss_flags, &uss->ss_flags) |
2493 __get_user(ss_size, &uss->ss_size);
2494 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 goto out;
2496
2497 error = -EPERM;
2498 if (on_sig_stack(sp))
2499 goto out;
2500
2501 error = -EINVAL;
2502 /*
2503 *
2504 * Note - this code used to test ss_flags incorrectly
2505 * old code may have been written using ss_flags==0
2506 * to mean ss_flags==SS_ONSTACK (as this was the only
2507 * way that worked) - this fix preserves that older
2508 * mechanism
2509 */
2510 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2511 goto out;
2512
2513 if (ss_flags == SS_DISABLE) {
2514 ss_size = 0;
2515 ss_sp = NULL;
2516 } else {
2517 error = -ENOMEM;
2518 if (ss_size < MINSIGSTKSZ)
2519 goto out;
2520 }
2521
2522 current->sas_ss_sp = (unsigned long) ss_sp;
2523 current->sas_ss_size = ss_size;
2524 }
2525
Linus Torvalds0083fc22009-08-01 10:34:56 -07002526 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (uoss) {
2528 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002529 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002531 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2532 __put_user(oss.ss_size, &uoss->ss_size) |
2533 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536out:
2537 return error;
2538}
2539
2540#ifdef __ARCH_WANT_SYS_SIGPENDING
2541
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002542SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
2544 return do_sigpending(set, sizeof(*set));
2545}
2546
2547#endif
2548
2549#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2550/* Some platforms have their own version with special arguments others
2551 support only sys_rt_sigprocmask. */
2552
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002553SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2554 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 int error;
2557 old_sigset_t old_set, new_set;
2558
2559 if (set) {
2560 error = -EFAULT;
2561 if (copy_from_user(&new_set, set, sizeof(*set)))
2562 goto out;
2563 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2564
2565 spin_lock_irq(&current->sighand->siglock);
2566 old_set = current->blocked.sig[0];
2567
2568 error = 0;
2569 switch (how) {
2570 default:
2571 error = -EINVAL;
2572 break;
2573 case SIG_BLOCK:
2574 sigaddsetmask(&current->blocked, new_set);
2575 break;
2576 case SIG_UNBLOCK:
2577 sigdelsetmask(&current->blocked, new_set);
2578 break;
2579 case SIG_SETMASK:
2580 current->blocked.sig[0] = new_set;
2581 break;
2582 }
2583
2584 recalc_sigpending();
2585 spin_unlock_irq(&current->sighand->siglock);
2586 if (error)
2587 goto out;
2588 if (oset)
2589 goto set_old;
2590 } else if (oset) {
2591 old_set = current->blocked.sig[0];
2592 set_old:
2593 error = -EFAULT;
2594 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2595 goto out;
2596 }
2597 error = 0;
2598out:
2599 return error;
2600}
2601#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2602
2603#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002604SYSCALL_DEFINE4(rt_sigaction, int, sig,
2605 const struct sigaction __user *, act,
2606 struct sigaction __user *, oact,
2607 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
2609 struct k_sigaction new_sa, old_sa;
2610 int ret = -EINVAL;
2611
2612 /* XXX: Don't preclude handling different sized sigset_t's. */
2613 if (sigsetsize != sizeof(sigset_t))
2614 goto out;
2615
2616 if (act) {
2617 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2618 return -EFAULT;
2619 }
2620
2621 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2622
2623 if (!ret && oact) {
2624 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2625 return -EFAULT;
2626 }
2627out:
2628 return ret;
2629}
2630#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2631
2632#ifdef __ARCH_WANT_SYS_SGETMASK
2633
2634/*
2635 * For backwards compatibility. Functionality superseded by sigprocmask.
2636 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002637SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 /* SMP safe */
2640 return current->blocked.sig[0];
2641}
2642
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002643SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
2645 int old;
2646
2647 spin_lock_irq(&current->sighand->siglock);
2648 old = current->blocked.sig[0];
2649
2650 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2651 sigmask(SIGSTOP)));
2652 recalc_sigpending();
2653 spin_unlock_irq(&current->sighand->siglock);
2654
2655 return old;
2656}
2657#endif /* __ARCH_WANT_SGETMASK */
2658
2659#ifdef __ARCH_WANT_SYS_SIGNAL
2660/*
2661 * For backwards compatibility. Functionality superseded by sigaction.
2662 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002663SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
2665 struct k_sigaction new_sa, old_sa;
2666 int ret;
2667
2668 new_sa.sa.sa_handler = handler;
2669 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002670 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 ret = do_sigaction(sig, &new_sa, &old_sa);
2673
2674 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2675}
2676#endif /* __ARCH_WANT_SYS_SIGNAL */
2677
2678#ifdef __ARCH_WANT_SYS_PAUSE
2679
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002680SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
2682 current->state = TASK_INTERRUPTIBLE;
2683 schedule();
2684 return -ERESTARTNOHAND;
2685}
2686
2687#endif
2688
David Woodhouse150256d2006-01-18 17:43:57 -08002689#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002690SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002691{
2692 sigset_t newset;
2693
2694 /* XXX: Don't preclude handling different sized sigset_t's. */
2695 if (sigsetsize != sizeof(sigset_t))
2696 return -EINVAL;
2697
2698 if (copy_from_user(&newset, unewset, sizeof(newset)))
2699 return -EFAULT;
2700 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2701
2702 spin_lock_irq(&current->sighand->siglock);
2703 current->saved_sigmask = current->blocked;
2704 current->blocked = newset;
2705 recalc_sigpending();
2706 spin_unlock_irq(&current->sighand->siglock);
2707
2708 current->state = TASK_INTERRUPTIBLE;
2709 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002710 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002711 return -ERESTARTNOHAND;
2712}
2713#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2714
David Howellsf269fdd2006-09-27 01:50:23 -07002715__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2716{
2717 return NULL;
2718}
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720void __init signals_init(void)
2721{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002722 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}