blob: e921409b85a9e4aeef3599cdce1484ac475d021e [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
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800162#define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
165
Davide Libenzifba2afa2007-05-10 22:23:13 -0700166int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned long i, *s, *m, x;
169 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 s = pending->signal.sig;
172 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800173
174 /*
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
177 */
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 switch (_NSIG_WORDS) {
187 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
200 break;
201 sig = ffz(~x) + _NSIG_BPW + 1;
202 break;
203
204 case 1:
205 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return sig;
210}
211
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900212static inline void print_dropped_signal(int sig)
213{
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
215
216 if (!print_fatal_signals)
217 return;
218
219 if (!__ratelimit(&ratelimit_state))
220 return;
221
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
224}
225
David Howellsc69e8d92008-11-14 10:39:19 +1100226/*
227 * allocate a new signal queue record
228 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100229 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100230 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900231static struct sigqueue *
232__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800235 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800237 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000238 * Protect access to @t credentials. This can go away when all
239 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800240 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000241 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100242 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800243 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000244 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800247 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800248 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900250 } else {
251 print_dropped_signal(sig);
252 }
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800255 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100256 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 } else {
258 INIT_LIST_HEAD(&q->list);
259 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100260 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
David Howellsd84f4f92008-11-14 10:39:23 +1100262
263 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Andrew Morton514a01b2006-02-03 03:04:41 -0800266static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 if (q->flags & SIGQUEUE_PREALLOC)
269 return;
270 atomic_dec(&q->user->sigpending);
271 free_uid(q->user);
272 kmem_cache_free(sigqueue_cachep, q);
273}
274
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800275void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct sigqueue *q;
278
279 sigemptyset(&queue->signal);
280 while (!list_empty(&queue->list)) {
281 q = list_entry(queue->list.next, struct sigqueue , list);
282 list_del_init(&q->list);
283 __sigqueue_free(q);
284 }
285}
286
287/*
288 * Flush all pending signals for a task.
289 */
David Howells3bcac022009-04-29 13:45:05 +0100290void __flush_signals(struct task_struct *t)
291{
292 clear_tsk_thread_flag(t, TIF_SIGPENDING);
293 flush_sigqueue(&t->pending);
294 flush_sigqueue(&t->signal->shared_pending);
295}
296
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800297void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100302 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 spin_unlock_irqrestore(&t->sighand->siglock, flags);
304}
305
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400306static void __flush_itimer_signals(struct sigpending *pending)
307{
308 sigset_t signal, retain;
309 struct sigqueue *q, *n;
310
311 signal = pending->signal;
312 sigemptyset(&retain);
313
314 list_for_each_entry_safe(q, n, &pending->list, list) {
315 int sig = q->info.si_signo;
316
317 if (likely(q->info.si_code != SI_TIMER)) {
318 sigaddset(&retain, sig);
319 } else {
320 sigdelset(&signal, sig);
321 list_del_init(&q->list);
322 __sigqueue_free(q);
323 }
324 }
325
326 sigorsets(&pending->signal, &signal, &retain);
327}
328
329void flush_itimer_signals(void)
330{
331 struct task_struct *tsk = current;
332 unsigned long flags;
333
334 spin_lock_irqsave(&tsk->sighand->siglock, flags);
335 __flush_itimer_signals(&tsk->pending);
336 __flush_itimer_signals(&tsk->signal->shared_pending);
337 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
338}
339
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700340void ignore_signals(struct task_struct *t)
341{
342 int i;
343
344 for (i = 0; i < _NSIG; ++i)
345 t->sighand->action[i].sa.sa_handler = SIG_IGN;
346
347 flush_signals(t);
348}
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 * Flush all handlers for a task.
352 */
353
354void
355flush_signal_handlers(struct task_struct *t, int force_default)
356{
357 int i;
358 struct k_sigaction *ka = &t->sighand->action[0];
359 for (i = _NSIG ; i != 0 ; i--) {
360 if (force_default || ka->sa.sa_handler != SIG_IGN)
361 ka->sa.sa_handler = SIG_DFL;
362 ka->sa.sa_flags = 0;
363 sigemptyset(&ka->sa.sa_mask);
364 ka++;
365 }
366}
367
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200368int unhandled_signal(struct task_struct *tsk, int sig)
369{
Roland McGrath445a91d2008-07-25 19:45:52 -0700370 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700371 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200372 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700373 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200374 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700375 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/* Notify the system that a driver wants to block all signals for this
380 * process, and wants to be notified if any signals at all were to be
381 * sent/acted upon. If the notifier routine returns non-zero, then the
382 * signal will be acted upon after all. If the notifier routine returns 0,
383 * then then signal will be blocked. Only one block per process is
384 * allowed. priv is a pointer to private data that the notifier routine
385 * can use to determine if the signal should be blocked or not. */
386
387void
388block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
389{
390 unsigned long flags;
391
392 spin_lock_irqsave(&current->sighand->siglock, flags);
393 current->notifier_mask = mask;
394 current->notifier_data = priv;
395 current->notifier = notifier;
396 spin_unlock_irqrestore(&current->sighand->siglock, flags);
397}
398
399/* Notify the system that blocking has ended. */
400
401void
402unblock_all_signals(void)
403{
404 unsigned long flags;
405
406 spin_lock_irqsave(&current->sighand->siglock, flags);
407 current->notifier = NULL;
408 current->notifier_data = NULL;
409 recalc_sigpending();
410 spin_unlock_irqrestore(&current->sighand->siglock, flags);
411}
412
Oleg Nesterov100360f2008-07-25 01:47:29 -0700413static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Collect the siginfo appropriate to this signal. Check if
419 * there is another siginfo for the same signal.
420 */
421 list_for_each_entry(q, &list->list, list) {
422 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700423 if (first)
424 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 first = q;
426 }
427 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700428
429 sigdelset(&list->signal, sig);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700432still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 list_del_init(&first->list);
434 copy_siginfo(info, &first->info);
435 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* Ok, it wasn't in the queue. This must be
438 a fast-pathed signal or we must have been
439 out of queue space. So zero out the info.
440 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 info->si_signo = sig;
442 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800443 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 info->si_pid = 0;
445 info->si_uid = 0;
446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
450 siginfo_t *info)
451{
Roland McGrath27d91e02006-09-29 02:00:31 -0700452 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (sig) {
455 if (current->notifier) {
456 if (sigismember(current->notifier_mask, sig)) {
457 if (!(current->notifier)(current->notifier_data)) {
458 clear_thread_flag(TIF_SIGPENDING);
459 return 0;
460 }
461 }
462 }
463
Oleg Nesterov100360f2008-07-25 01:47:29 -0700464 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return sig;
468}
469
470/*
471 * Dequeue a signal and return the element to the caller, which is
472 * expected to free it.
473 *
474 * All callers have to hold the siglock.
475 */
476int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
477{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700478 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000479
480 /* We only dequeue private signals from ourselves, we don't let
481 * signalfd steal them
482 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700483 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800484 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 signr = __dequeue_signal(&tsk->signal->shared_pending,
486 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800487 /*
488 * itimer signal ?
489 *
490 * itimers are process shared and we restart periodic
491 * itimers in the signal delivery path to prevent DoS
492 * attacks in the high resolution timer case. This is
493 * compliant with the old way of self restarting
494 * itimers, as the SIGALRM is a legacy signal and only
495 * queued once. Changing the restart behaviour to
496 * restart the timer in the signal dequeue path is
497 * reducing the timer noise on heavy loaded !highres
498 * systems too.
499 */
500 if (unlikely(signr == SIGALRM)) {
501 struct hrtimer *tmr = &tsk->signal->real_timer;
502
503 if (!hrtimer_is_queued(tmr) &&
504 tsk->signal->it_real_incr.tv64 != 0) {
505 hrtimer_forward(tmr, tmr->base->get_time(),
506 tsk->signal->it_real_incr);
507 hrtimer_restart(tmr);
508 }
509 }
510 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700511
Davide Libenzib8fceee2007-09-20 12:40:16 -0700512 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700513 if (!signr)
514 return 0;
515
516 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800517 /*
518 * Set a marker that we have dequeued a stop signal. Our
519 * caller might release the siglock and then the pending
520 * stop signal it is about to process is no longer in the
521 * pending bitmasks, but must still be cleared by a SIGCONT
522 * (and overruled by a SIGKILL). So those cases clear this
523 * shared flag after we've set it. Note that this flag may
524 * remain set after the signal we return is ignored or
525 * handled. That doesn't matter because its only purpose
526 * is to alert stop-signal processing code when another
527 * processor has come along and cleared the flag.
528 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700529 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800530 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700531 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /*
533 * Release the siglock to ensure proper locking order
534 * of timer locks outside of siglocks. Note, we leave
535 * irqs disabled here, since the posix-timers code is
536 * about to disable them again anyway.
537 */
538 spin_unlock(&tsk->sighand->siglock);
539 do_schedule_next_timer(info);
540 spin_lock(&tsk->sighand->siglock);
541 }
542 return signr;
543}
544
545/*
546 * Tell a process that it has a new active signal..
547 *
548 * NOTE! we rely on the previous spin_lock to
549 * lock interrupts for us! We can only be called with
550 * "siglock" held, and the local interrupt must
551 * have been disabled when that got acquired!
552 *
553 * No need to set need_resched since signal event passing
554 * goes through ->blocked
555 */
556void signal_wake_up(struct task_struct *t, int resume)
557{
558 unsigned int mask;
559
560 set_tsk_thread_flag(t, TIF_SIGPENDING);
561
562 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500563 * For SIGKILL, we want to wake it up in the stopped/traced/killable
564 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 * executing another processor and just now entering stopped state.
566 * By using wake_up_state, we ensure the process will wake up and
567 * handle its death signal.
568 */
569 mask = TASK_INTERRUPTIBLE;
570 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500571 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!wake_up_state(t, mask))
573 kick_process(t);
574}
575
576/*
577 * Remove signals in mask from the pending set and queue.
578 * Returns 1 if any signals were found.
579 *
580 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800581 *
582 * This version takes a sigset mask and looks at all signals,
583 * not just those in the first mask word.
584 */
585static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
586{
587 struct sigqueue *q, *n;
588 sigset_t m;
589
590 sigandsets(&m, mask, &s->signal);
591 if (sigisemptyset(&m))
592 return 0;
593
594 signandsets(&s->signal, &s->signal, mask);
595 list_for_each_entry_safe(q, n, &s->list, list) {
596 if (sigismember(mask, q->info.si_signo)) {
597 list_del_init(&q->list);
598 __sigqueue_free(q);
599 }
600 }
601 return 1;
602}
603/*
604 * Remove signals in mask from the pending set and queue.
605 * Returns 1 if any signals were found.
606 *
607 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 */
609static int rm_from_queue(unsigned long mask, struct sigpending *s)
610{
611 struct sigqueue *q, *n;
612
613 if (!sigtestsetmask(&s->signal, mask))
614 return 0;
615
616 sigdelsetmask(&s->signal, mask);
617 list_for_each_entry_safe(q, n, &s->list, list) {
618 if (q->info.si_signo < SIGRTMIN &&
619 (mask & sigmask(q->info.si_signo))) {
620 list_del_init(&q->list);
621 __sigqueue_free(q);
622 }
623 }
624 return 1;
625}
626
Oleg Nesterov614c5172009-12-15 16:47:22 -0800627static inline int is_si_special(const struct siginfo *info)
628{
629 return info <= SEND_SIG_FORCED;
630}
631
632static inline bool si_fromuser(const struct siginfo *info)
633{
634 return info == SEND_SIG_NOINFO ||
635 (!is_si_special(info) && SI_FROMUSER(info));
636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/*
639 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100640 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
642static int check_kill_permission(int sig, struct siginfo *info,
643 struct task_struct *t)
644{
Oleg Nesterov065add32010-05-26 14:42:54 -0700645 const struct cred *cred, *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700646 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700647 int error;
648
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700649 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700650 return -EINVAL;
651
Oleg Nesterov614c5172009-12-15 16:47:22 -0800652 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700653 return 0;
654
655 error = audit_signal_info(sig, t); /* Let audit system see the signal */
656 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400658
Oleg Nesterov065add32010-05-26 14:42:54 -0700659 cred = current_cred();
David Howellsc69e8d92008-11-14 10:39:19 +1100660 tcred = __task_cred(t);
Oleg Nesterov065add32010-05-26 14:42:54 -0700661 if (!same_thread_group(current, t) &&
662 (cred->euid ^ tcred->suid) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100663 (cred->euid ^ tcred->uid) &&
664 (cred->uid ^ tcred->suid) &&
665 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700666 !capable(CAP_KILL)) {
667 switch (sig) {
668 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700669 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700670 /*
671 * We don't return the error if sid == NULL. The
672 * task was unhashed, the caller must notice this.
673 */
674 if (!sid || sid == task_session(current))
675 break;
676 default:
677 return -EPERM;
678 }
679 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100680
Amy Griffise54dc242007-03-29 18:01:04 -0400681 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700685 * Handle magic process-wide effects of stop/continue signals. Unlike
686 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 * time regardless of blocking, ignoring, or handling. This does the
688 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700689 * signals. The process stop is done as a signal action for SIG_DFL.
690 *
691 * Returns true if the signal should be actually delivered, otherwise
692 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700694static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700696 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct task_struct *t;
698
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700699 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700701 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700703 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 /*
705 * This is a stop signal. Remove SIGCONT from all queues.
706 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700707 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 t = p;
709 do {
710 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700711 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700713 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Remove all stop signals from all queues,
716 * and wake all threads.
717 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700718 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 t = p;
720 do {
721 unsigned int state;
722 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /*
724 * If there is a handler for SIGCONT, we must make
725 * sure that no thread returns to user mode before
726 * we post the signal, in case it was the only
727 * thread eligible to run the signal handler--then
728 * it must not do anything between resuming and
729 * running the handler. With the TIF_SIGPENDING
730 * flag set, the thread will pause and acquire the
731 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700732 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * Wake up the stopped thread _after_ setting
735 * TIF_SIGPENDING
736 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500737 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
739 set_tsk_thread_flag(t, TIF_SIGPENDING);
740 state |= TASK_INTERRUPTIBLE;
741 }
742 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700743 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700745 /*
746 * Notify the parent with CLD_CONTINUED if we were stopped.
747 *
748 * If we were in the middle of a group stop, we pretend it
749 * was already finished, and then continued. Since SIGCHLD
750 * doesn't queue we report only CLD_STOPPED, as if the next
751 * CLD_CONTINUED was dropped.
752 */
753 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700754 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700755 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700756 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700757 why |= SIGNAL_CLD_STOPPED;
758
759 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700760 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700761 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700762 * will take ->siglock, notice SIGNAL_CLD_MASK, and
763 * notify its parent. See get_signal_to_deliver().
764 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700765 signal->flags = why | SIGNAL_STOP_CONTINUED;
766 signal->group_stop_count = 0;
767 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 } else {
769 /*
770 * We are not stopped, but there could be a stop
771 * signal in the middle of being processed after
772 * being removed from the queue. Clear that too.
773 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700774 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700777
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700778 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700781/*
782 * Test if P wants to take SIG. After we've checked all threads with this,
783 * it's equivalent to finding no threads not blocking SIG. Any threads not
784 * blocking SIG were ruled out because they are not running and already
785 * have pending signals. Such threads will dequeue from the shared queue
786 * as soon as they're available, so putting the signal on the shared queue
787 * will be equivalent to sending it to one such thread.
788 */
789static inline int wants_signal(int sig, struct task_struct *p)
790{
791 if (sigismember(&p->blocked, sig))
792 return 0;
793 if (p->flags & PF_EXITING)
794 return 0;
795 if (sig == SIGKILL)
796 return 1;
797 if (task_is_stopped_or_traced(p))
798 return 0;
799 return task_curr(p) || !signal_pending(p);
800}
801
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700802static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700803{
804 struct signal_struct *signal = p->signal;
805 struct task_struct *t;
806
807 /*
808 * Now find a thread we can wake up to take the signal off the queue.
809 *
810 * If the main thread wants the signal, it gets first crack.
811 * Probably the least surprising to the average bear.
812 */
813 if (wants_signal(sig, p))
814 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700815 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700816 /*
817 * There is just one thread and it does not need to be woken.
818 * It will dequeue unblocked signals before it runs again.
819 */
820 return;
821 else {
822 /*
823 * Otherwise try to find a suitable thread.
824 */
825 t = signal->curr_target;
826 while (!wants_signal(sig, t)) {
827 t = next_thread(t);
828 if (t == signal->curr_target)
829 /*
830 * No thread needs to be woken.
831 * Any eligible threads will see
832 * the signal in the queue soon.
833 */
834 return;
835 }
836 signal->curr_target = t;
837 }
838
839 /*
840 * Found a killable thread. If the signal will be fatal,
841 * then start taking the whole group down immediately.
842 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700843 if (sig_fatal(p, sig) &&
844 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700845 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700846 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700847 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700848 /*
849 * This signal will be fatal to the whole group.
850 */
851 if (!sig_kernel_coredump(sig)) {
852 /*
853 * Start a group exit and wake everybody up.
854 * This way we don't have other threads
855 * running and doing things after a slower
856 * thread has the fatal signal pending.
857 */
858 signal->flags = SIGNAL_GROUP_EXIT;
859 signal->group_exit_code = sig;
860 signal->group_stop_count = 0;
861 t = p;
862 do {
863 sigaddset(&t->pending.signal, SIGKILL);
864 signal_wake_up(t, 1);
865 } while_each_thread(p, t);
866 return;
867 }
868 }
869
870 /*
871 * The signal is already in the shared-pending queue.
872 * Tell the chosen thread to wake up and dequeue it.
873 */
874 signal_wake_up(t, sig == SIGKILL);
875 return;
876}
877
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700878static inline int legacy_queue(struct sigpending *signals, int sig)
879{
880 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
881}
882
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700883static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
884 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700886 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700887 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200888 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500890 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400891
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700892 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700893
894 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700895 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700896
897 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700899 * Short-circuit ignored signals and support queuing
900 * exactly one non-rt signal, so that we can get more
901 * detailed information about the cause of the signal.
902 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700903 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700904 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700905 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * fast-pathed signals for kernel-internal things like SIGSTOP
907 * or SIGKILL.
908 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800909 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto out_set;
911
912 /* Real-time signals must be queued if sent by sigqueue, or
913 some other real-time mechanism. It is implementation
914 defined whether kill() does so. We attempt to do so, on
915 the principle of least surprise, but since kill is not
916 allowed to fail with EAGAIN when low on memory we just
917 make sure at least one signal gets delivered and don't
918 pass on the info struct. */
919
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200920 if (sig < SIGRTMIN)
921 override_rlimit = (is_si_special(info) || info->si_code >= 0);
922 else
923 override_rlimit = 0;
924
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900925 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200926 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700928 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800930 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 q->info.si_signo = sig;
932 q->info.si_errno = 0;
933 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800934 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800935 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100936 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800938 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 q->info.si_signo = sig;
940 q->info.si_errno = 0;
941 q->info.si_code = SI_KERNEL;
942 q->info.si_pid = 0;
943 q->info.si_uid = 0;
944 break;
945 default:
946 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700947 if (from_ancestor_ns)
948 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 break;
950 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800951 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500952 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
953 /*
954 * Queue overflow, abort. We may abort if the
955 * signal was rt and sent by user using something
956 * other than kill().
957 */
958 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500960 } else {
961 /*
962 * This is a silent loss of information. We still
963 * send the signal, but the *info bits are lost.
964 */
965 trace_signal_lose_info(sig, group, info);
966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
969out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700970 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700971 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700972 complete_signal(sig, t, group);
973 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700976static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
977 int group)
978{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700979 int from_ancestor_ns = 0;
980
981#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -0800982 from_ancestor_ns = si_fromuser(info) &&
983 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700984#endif
985
986 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700987}
988
Ingo Molnar45807a12007-07-15 23:40:10 -0700989static void print_fatal_signal(struct pt_regs *regs, int signr)
990{
991 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700992 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700993
Al Viroca5cd872007-10-29 04:31:16 +0000994#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100995 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700996 {
997 int i;
998 for (i = 0; i < 16; i++) {
999 unsigned char insn;
1000
Andi Kleenb45c6e72010-01-08 14:42:52 -08001001 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1002 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001003 printk("%02x ", insn);
1004 }
1005 }
1006#endif
1007 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001008 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001009 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001010 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001011}
1012
1013static int __init setup_print_fatal_signals(char *str)
1014{
1015 get_option (&str, &print_fatal_signals);
1016
1017 return 1;
1018}
1019
1020__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001022int
1023__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1024{
1025 return send_signal(sig, info, p, 1);
1026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static int
1029specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1030{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001031 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001034int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1035 bool group)
1036{
1037 unsigned long flags;
1038 int ret = -ESRCH;
1039
1040 if (lock_task_sighand(p, &flags)) {
1041 ret = send_signal(sig, info, p, group);
1042 unlock_task_sighand(p, &flags);
1043 }
1044
1045 return ret;
1046}
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * Force a signal that the process can't ignore: if necessary
1050 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001051 *
1052 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1053 * since we do not want to have a signal handler that was blocked
1054 * be invoked when user space had explicitly blocked it.
1055 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001056 * We don't want to have recursive SIGSEGV's etc, for example,
1057 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059int
1060force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1061{
1062 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001063 int ret, blocked, ignored;
1064 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001067 action = &t->sighand->action[sig-1];
1068 ignored = action->sa.sa_handler == SIG_IGN;
1069 blocked = sigismember(&t->blocked, sig);
1070 if (blocked || ignored) {
1071 action->sa.sa_handler = SIG_DFL;
1072 if (blocked) {
1073 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001074 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001077 if (action->sa.sa_handler == SIG_DFL)
1078 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ret = specific_send_sig_info(sig, info, t);
1080 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1081
1082 return ret;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085/*
1086 * Nuke all other threads in the group.
1087 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001088int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001090 struct task_struct *t = p;
1091 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 p->signal->group_stop_count = 0;
1094
Oleg Nesterov09faef12010-05-26 14:43:11 -07001095 while_each_thread(p, t) {
1096 count++;
1097
1098 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (t->exit_state)
1100 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 signal_wake_up(t, 1);
1103 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001104
1105 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001108struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1109 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001110{
1111 struct sighand_struct *sighand;
1112
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001113 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001114 for (;;) {
1115 sighand = rcu_dereference(tsk->sighand);
1116 if (unlikely(sighand == NULL))
1117 break;
1118
1119 spin_lock_irqsave(&sighand->siglock, *flags);
1120 if (likely(sighand == tsk->sighand))
1121 break;
1122 spin_unlock_irqrestore(&sighand->siglock, *flags);
1123 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001124 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001125
1126 return sighand;
1127}
1128
David Howellsc69e8d92008-11-14 10:39:19 +11001129/*
1130 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1133{
David Howells694f6902010-08-04 16:59:14 +01001134 int ret;
1135
1136 rcu_read_lock();
1137 ret = check_kill_permission(sig, info, p);
1138 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001140 if (!ret && sig)
1141 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 return ret;
1144}
1145
1146/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001147 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001149 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001151int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct task_struct *p = NULL;
1154 int retval, success;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 success = 0;
1157 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001158 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 int err = group_send_sig_info(sig, info, p);
1160 success |= !err;
1161 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001162 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return success ? 0 : retval;
1164}
1165
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001166int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001168 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 struct task_struct *p;
1170
Ingo Molnare56d0902006-01-08 01:01:37 -08001171 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001172retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001173 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001174 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001176 if (unlikely(error == -ESRCH))
1177 /*
1178 * The task was unhashed in between, try again.
1179 * If it is dead, pid_task() will return NULL,
1180 * if we race with de_thread() it will find the
1181 * new leader.
1182 */
1183 goto retry;
1184 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001185 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return error;
1188}
1189
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001190int
1191kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001192{
1193 int error;
1194 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001195 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001196 rcu_read_unlock();
1197 return error;
1198}
1199
Eric W. Biederman2425c082006-10-02 02:17:28 -07001200/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1201int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001202 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001203{
1204 int ret = -EINVAL;
1205 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001206 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001207 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001208
1209 if (!valid_signal(sig))
1210 return ret;
1211
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001212 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001213 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001214 if (!p) {
1215 ret = -ESRCH;
1216 goto out_unlock;
1217 }
David Howellsc69e8d92008-11-14 10:39:19 +11001218 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001219 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001220 euid != pcred->suid && euid != pcred->uid &&
1221 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001222 ret = -EPERM;
1223 goto out_unlock;
1224 }
David Quigley8f95dc52006-06-30 01:55:47 -07001225 ret = security_task_kill(p, info, sig, secid);
1226 if (ret)
1227 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001228
1229 if (sig) {
1230 if (lock_task_sighand(p, &flags)) {
1231 ret = __send_signal(sig, info, p, 1, 0);
1232 unlock_task_sighand(p, &flags);
1233 } else
1234 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001235 }
1236out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001237 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001238 return ret;
1239}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001240EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242/*
1243 * kill_something_info() interprets pid in interesting ways just like kill(2).
1244 *
1245 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1246 * is probably wrong. Should make it like BSD or SYSV.
1247 */
1248
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001249static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001251 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001252
1253 if (pid > 0) {
1254 rcu_read_lock();
1255 ret = kill_pid_info(sig, info, find_vpid(pid));
1256 rcu_read_unlock();
1257 return ret;
1258 }
1259
1260 read_lock(&tasklist_lock);
1261 if (pid != -1) {
1262 ret = __kill_pgrp_info(sig, info,
1263 pid ? find_vpid(-pid) : task_pgrp(current));
1264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int retval = 0, count = 0;
1266 struct task_struct * p;
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001269 if (task_pid_vnr(p) > 1 &&
1270 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int err = group_send_sig_info(sig, info, p);
1272 ++count;
1273 if (err != -EPERM)
1274 retval = err;
1275 }
1276 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001277 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001279 read_unlock(&tasklist_lock);
1280
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284/*
1285 * These are for backward compatibility with the rest of the kernel source.
1286 */
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288int
1289send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1290{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 /*
1292 * Make sure legacy kernel users don't send in bad values
1293 * (normal paths check this in check_kill_permission).
1294 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001295 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return -EINVAL;
1297
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001298 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001301#define __si_special(priv) \
1302 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304int
1305send_sig(int sig, struct task_struct *p, int priv)
1306{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001307 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310void
1311force_sig(int sig, struct task_struct *p)
1312{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001313 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
1316/*
1317 * When things go south during signal handling, we
1318 * will force a SIGSEGV. And if the signal that caused
1319 * the problem was already a SIGSEGV, we'll want to
1320 * make sure we don't even try to deliver the signal..
1321 */
1322int
1323force_sigsegv(int sig, struct task_struct *p)
1324{
1325 if (sig == SIGSEGV) {
1326 unsigned long flags;
1327 spin_lock_irqsave(&p->sighand->siglock, flags);
1328 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1329 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1330 }
1331 force_sig(SIGSEGV, p);
1332 return 0;
1333}
1334
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001335int kill_pgrp(struct pid *pid, int sig, int priv)
1336{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001337 int ret;
1338
1339 read_lock(&tasklist_lock);
1340 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1341 read_unlock(&tasklist_lock);
1342
1343 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001344}
1345EXPORT_SYMBOL(kill_pgrp);
1346
1347int kill_pid(struct pid *pid, int sig, int priv)
1348{
1349 return kill_pid_info(sig, __si_special(priv), pid);
1350}
1351EXPORT_SYMBOL(kill_pid);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353/*
1354 * These functions support sending signals using preallocated sigqueue
1355 * structures. This is needed "because realtime applications cannot
1356 * afford to lose notifications of asynchronous events, like timer
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001357 * expirations or I/O completions". In the case of Posix Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * we allocate the sigqueue structure from the timer_create. If this
1359 * allocation fails we are able to report the failure to the application
1360 * with an EAGAIN error.
1361 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362struct sigqueue *sigqueue_alloc(void)
1363{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001364 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001366 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001368
1369 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
1372void sigqueue_free(struct sigqueue *q)
1373{
1374 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001375 spinlock_t *lock = &current->sighand->siglock;
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1378 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001379 * We must hold ->siglock while testing q->list
1380 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001381 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001383 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001384 q->flags &= ~SIGQUEUE_PREALLOC;
1385 /*
1386 * If it is queued it will be freed when dequeued,
1387 * like the "regular" sigqueue.
1388 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001389 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001390 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001391 spin_unlock_irqrestore(lock, flags);
1392
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001393 if (q)
1394 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001397int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001398{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001399 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001400 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001401 unsigned long flags;
1402 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001403
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001404 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001405
1406 ret = -1;
1407 if (!likely(lock_task_sighand(t, &flags)))
1408 goto ret;
1409
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001410 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001411 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001412 goto out;
1413
1414 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001415 if (unlikely(!list_empty(&q->list))) {
1416 /*
1417 * If an SI_TIMER entry is already queue just increment
1418 * the overrun count.
1419 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001420 BUG_ON(q->info.si_code != SI_TIMER);
1421 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001422 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001423 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001424 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001425
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001426 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001427 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001428 list_add_tail(&q->list, &pending->list);
1429 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001430 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001431out:
1432 unlock_task_sighand(t, &flags);
1433ret:
1434 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * Let a parent know about the death of a child.
1439 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001440 *
1441 * Returns -1 if our parent ignored us and so we've switched to
1442 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001444int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 struct siginfo info;
1447 unsigned long flags;
1448 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001449 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 BUG_ON(sig == -1);
1452
1453 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001454 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001456 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1458
1459 info.si_signo = sig;
1460 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001461 /*
1462 * we are under tasklist_lock here so our parent is tied to
1463 * us and cannot exit and release its namespace.
1464 *
1465 * the only it can is to switch its nsproxy with sys_unshare,
1466 * bu uncharing pid namespaces is not allowed, so we'll always
1467 * see relevant namespace
1468 *
1469 * write_lock() currently calls preempt_disable() which is the
1470 * same as rcu_read_lock(), but according to Oleg, this is not
1471 * correct to rely on this
1472 */
1473 rcu_read_lock();
1474 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001475 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001476 rcu_read_unlock();
1477
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001478 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1479 tsk->signal->utime));
1480 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1481 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 info.si_status = tsk->exit_code & 0x7f;
1484 if (tsk->exit_code & 0x80)
1485 info.si_code = CLD_DUMPED;
1486 else if (tsk->exit_code & 0x7f)
1487 info.si_code = CLD_KILLED;
1488 else {
1489 info.si_code = CLD_EXITED;
1490 info.si_status = tsk->exit_code >> 8;
1491 }
1492
1493 psig = tsk->parent->sighand;
1494 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001495 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1497 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1498 /*
1499 * We are exiting and our parent doesn't care. POSIX.1
1500 * defines special semantics for setting SIGCHLD to SIG_IGN
1501 * or setting the SA_NOCLDWAIT flag: we should be reaped
1502 * automatically and not left for our parent's wait4 call.
1503 * Rather than having the parent do it as a magic kind of
1504 * signal handler, we just set this to tell do_exit that we
1505 * can be cleaned up without becoming a zombie. Note that
1506 * we still call __wake_up_parent in this case, because a
1507 * blocked sys_wait4 might now return -ECHILD.
1508 *
1509 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1510 * is implementation-defined: we do (if you don't want
1511 * it, just use SIG_IGN instead).
1512 */
Roland McGrath1b046242008-08-19 20:37:07 -07001513 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001515 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001517 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 __group_send_sig_info(sig, &info, tsk->parent);
1519 __wake_up_parent(tsk, tsk->parent);
1520 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001521
Roland McGrath1b046242008-08-19 20:37:07 -07001522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001525static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct siginfo info;
1528 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001529 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 struct sighand_struct *sighand;
1531
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001532 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001533 parent = tsk->parent;
1534 else {
1535 tsk = tsk->group_leader;
1536 parent = tsk->real_parent;
1537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 info.si_signo = SIGCHLD;
1540 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001541 /*
1542 * see comment in do_notify_parent() abot the following 3 lines
1543 */
1544 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001545 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001546 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001547 rcu_read_unlock();
1548
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001549 info.si_utime = cputime_to_clock_t(tsk->utime);
1550 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 info.si_code = why;
1553 switch (why) {
1554 case CLD_CONTINUED:
1555 info.si_status = SIGCONT;
1556 break;
1557 case CLD_STOPPED:
1558 info.si_status = tsk->signal->group_exit_code & 0x7f;
1559 break;
1560 case CLD_TRAPPED:
1561 info.si_status = tsk->exit_code & 0x7f;
1562 break;
1563 default:
1564 BUG();
1565 }
1566
1567 sighand = parent->sighand;
1568 spin_lock_irqsave(&sighand->siglock, flags);
1569 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1570 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1571 __group_send_sig_info(SIGCHLD, &info, parent);
1572 /*
1573 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1574 */
1575 __wake_up_parent(tsk, parent);
1576 spin_unlock_irqrestore(&sighand->siglock, flags);
1577}
1578
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001579static inline int may_ptrace_stop(void)
1580{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001581 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001582 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001583 /*
1584 * Are we in the middle of do_coredump?
1585 * If so and our tracer is also part of the coredump stopping
1586 * is a deadlock situation, and pointless because our tracer
1587 * is dead so don't allow us to stop.
1588 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001589 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001590 * is safe to enter schedule().
1591 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001592 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001593 unlikely(current->mm == current->parent->mm))
1594 return 0;
1595
1596 return 1;
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001600 * Return nonzero if there is a SIGKILL that should be waking us up.
1601 * Called with the siglock held.
1602 */
1603static int sigkill_pending(struct task_struct *tsk)
1604{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001605 return sigismember(&tsk->pending.signal, SIGKILL) ||
1606 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001607}
1608
1609/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * This must be called with current->sighand->siglock held.
1611 *
1612 * This should be the path for all ptrace stops.
1613 * We always set current->last_siginfo while stopped here.
1614 * That makes it a way to test a stopped process for
1615 * being ptrace-stopped vs being job-control-stopped.
1616 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001617 * If we actually decide not to stop at all because the tracer
1618 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001620static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Roland McGrath1a669c22008-02-06 01:37:37 -08001622 if (arch_ptrace_stop_needed(exit_code, info)) {
1623 /*
1624 * The arch code has something special to do before a
1625 * ptrace stop. This is allowed to block, e.g. for faults
1626 * on user stack pages. We can't keep the siglock while
1627 * calling arch_ptrace_stop, so we must release it now.
1628 * To preserve proper semantics, we must do this before
1629 * any signal bookkeeping like checking group_stop_count.
1630 * Meanwhile, a SIGKILL could come in before we retake the
1631 * siglock. That must prevent us from sleeping in TASK_TRACED.
1632 * So after regaining the lock, we must check for SIGKILL.
1633 */
1634 spin_unlock_irq(&current->sighand->siglock);
1635 arch_ptrace_stop(exit_code, info);
1636 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001637 if (sigkill_pending(current))
1638 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001639 }
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /*
1642 * If there is a group stop in progress,
1643 * we must participate in the bookkeeping.
1644 */
1645 if (current->signal->group_stop_count > 0)
1646 --current->signal->group_stop_count;
1647
1648 current->last_siginfo = info;
1649 current->exit_code = exit_code;
1650
1651 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001652 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 spin_unlock_irq(&current->sighand->siglock);
1654 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001655 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001656 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001657 /*
1658 * Don't want to allow preemption here, because
1659 * sys_ptrace() needs this task to be inactive.
1660 *
1661 * XXX: implement read_unlock_no_resched().
1662 */
1663 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001665 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 schedule();
1667 } else {
1668 /*
1669 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001670 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001672 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001673 if (clear_code)
1674 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001675 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
1678 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001679 * While in TASK_TRACED, we were considered "frozen enough".
1680 * Now that we woke up, it's crucial if we're supposed to be
1681 * frozen that we freeze now before running anything substantial.
1682 */
1683 try_to_freeze();
1684
1685 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * We are back. Now reacquire the siglock before touching
1687 * last_siginfo, so that we are sure to have synchronized with
1688 * any signal-sending on another CPU that wants to examine it.
1689 */
1690 spin_lock_irq(&current->sighand->siglock);
1691 current->last_siginfo = NULL;
1692
1693 /*
1694 * Queued signals ignored us while we were stopped for tracing.
1695 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001696 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001698 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
1701void ptrace_notify(int exit_code)
1702{
1703 siginfo_t info;
1704
1705 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1706
1707 memset(&info, 0, sizeof info);
1708 info.si_signo = SIGTRAP;
1709 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001710 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001711 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* Let the debugger run. */
1714 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001715 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 spin_unlock_irq(&current->sighand->siglock);
1717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/*
1720 * This performs the stopping for SIGSTOP and other stop signals.
1721 * We have to stop all threads in the thread group.
1722 * Returns nonzero if we've actually stopped and released the siglock.
1723 * Returns zero if we didn't stop and still hold the siglock.
1724 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001725static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
1727 struct signal_struct *sig = current->signal;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001728 int notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001730 if (!sig->group_stop_count) {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001731 struct task_struct *t;
1732
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001733 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001734 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001735 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001738 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001740 sig->group_exit_code = signr;
1741
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001742 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001743 for (t = next_thread(current); t != current; t = next_thread(t))
1744 /*
1745 * Setting state to TASK_STOPPED for a group
1746 * stop is always done with the siglock held,
1747 * so this check has no races.
1748 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001749 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001750 !task_is_stopped_or_traced(t)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001751 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001752 signal_wake_up(t, 0);
1753 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001754 }
1755 /*
1756 * If there are no other threads in the group, or if there is
1757 * a group stop in progress and we are the last to stop, report
1758 * to the parent. When ptraced, every thread reports itself.
1759 */
1760 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1761 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1762 /*
1763 * tracehook_notify_jctl() can drop and reacquire siglock, so
1764 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1765 * or SIGKILL comes in between ->group_stop_count == 0.
1766 */
1767 if (sig->group_stop_count) {
1768 if (!--sig->group_stop_count)
1769 sig->flags = SIGNAL_STOP_STOPPED;
1770 current->exit_code = sig->group_exit_code;
1771 __set_current_state(TASK_STOPPED);
1772 }
1773 spin_unlock_irq(&current->sighand->siglock);
1774
1775 if (notify) {
1776 read_lock(&tasklist_lock);
1777 do_notify_parent_cldstop(current, notify);
1778 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001781 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1782 do {
1783 schedule();
1784 } while (try_to_freeze());
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001785
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001786 tracehook_finish_jctl();
1787 current->exit_code = 0;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 return 1;
1790}
1791
Roland McGrath18c98b62008-04-17 18:44:38 -07001792static int ptrace_signal(int signr, siginfo_t *info,
1793 struct pt_regs *regs, void *cookie)
1794{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001795 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001796 return signr;
1797
1798 ptrace_signal_deliver(regs, cookie);
1799
1800 /* Let the debugger run. */
1801 ptrace_stop(signr, 0, info);
1802
1803 /* We're back. Did the debugger cancel the sig? */
1804 signr = current->exit_code;
1805 if (signr == 0)
1806 return signr;
1807
1808 current->exit_code = 0;
1809
1810 /* Update the siginfo structure if the signal has
1811 changed. If the debugger wanted something
1812 specific in the siginfo structure then it should
1813 have updated *info via PTRACE_SETSIGINFO. */
1814 if (signr != info->si_signo) {
1815 info->si_signo = signr;
1816 info->si_errno = 0;
1817 info->si_code = SI_USER;
1818 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001819 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001820 }
1821
1822 /* If the (new) signal is now blocked, requeue it. */
1823 if (sigismember(&current->blocked, signr)) {
1824 specific_send_sig_info(signr, info, current);
1825 signr = 0;
1826 }
1827
1828 return signr;
1829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1832 struct pt_regs *regs, void *cookie)
1833{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001834 struct sighand_struct *sighand = current->sighand;
1835 struct signal_struct *signal = current->signal;
1836 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001838relock:
1839 /*
1840 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1841 * While in TASK_STOPPED, we were considered "frozen enough".
1842 * Now that we woke up, it's crucial if we're supposed to be
1843 * frozen that we freeze now before running anything substantial.
1844 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001845 try_to_freeze();
1846
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001847 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001848 /*
1849 * Every stopped thread goes here after wakeup. Check to see if
1850 * we should notify the parent, prepare_signal(SIGCONT) encodes
1851 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1852 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001853 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1854 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001855 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001856 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001857
1858 why = tracehook_notify_jctl(why, CLD_CONTINUED);
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001859 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001860
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001861 if (why) {
1862 read_lock(&tasklist_lock);
1863 do_notify_parent_cldstop(current->group_leader, why);
1864 read_unlock(&tasklist_lock);
1865 }
Oleg Nesterove4420552008-04-30 00:52:44 -07001866 goto relock;
1867 }
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 for (;;) {
1870 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001871 /*
1872 * Tracing can induce an artifical signal and choose sigaction.
1873 * The return value in @signr determines the default action,
1874 * but @info->si_signo is the signal number we will report.
1875 */
1876 signr = tracehook_get_signal(current, regs, info, return_ka);
1877 if (unlikely(signr < 0))
1878 goto relock;
1879 if (unlikely(signr != 0))
1880 ka = return_ka;
1881 else {
Oleg Nesterov1be53962009-12-15 16:47:26 -08001882 if (unlikely(signal->group_stop_count > 0) &&
1883 do_signal_stop(0))
1884 goto relock;
1885
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001886 signr = dequeue_signal(current, &current->blocked,
1887 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Roland McGrath18c98b62008-04-17 18:44:38 -07001889 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001890 break; /* will return 0 */
1891
1892 if (signr != SIGKILL) {
1893 signr = ptrace_signal(signr, info,
1894 regs, cookie);
1895 if (!signr)
1896 continue;
1897 }
1898
1899 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05001902 /* Trace actually delivered signals. */
1903 trace_signal_deliver(signr, info, ka);
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1906 continue;
1907 if (ka->sa.sa_handler != SIG_DFL) {
1908 /* Run the handler. */
1909 *return_ka = *ka;
1910
1911 if (ka->sa.sa_flags & SA_ONESHOT)
1912 ka->sa.sa_handler = SIG_DFL;
1913
1914 break; /* will return non-zero "signr" value */
1915 }
1916
1917 /*
1918 * Now we are doing the default action for this signal.
1919 */
1920 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1921 continue;
1922
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001923 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001924 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001925 * Container-init gets no signals it doesn't want from same
1926 * container.
1927 *
1928 * Note that if global/container-init sees a sig_kernel_only()
1929 * signal here, the signal must have been generated internally
1930 * or must have come from an ancestor namespace. In either
1931 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001932 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001933 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001934 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 continue;
1936
1937 if (sig_kernel_stop(signr)) {
1938 /*
1939 * The default action is to stop all threads in
1940 * the thread group. The job control signals
1941 * do nothing in an orphaned pgrp, but SIGSTOP
1942 * always works. Note that siglock needs to be
1943 * dropped during the call to is_orphaned_pgrp()
1944 * because of lock ordering with tasklist_lock.
1945 * This allows an intervening SIGCONT to be posted.
1946 * We need to check for that and bail out if necessary.
1947 */
1948 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001949 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* signals can be posted during this window */
1952
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001953 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto relock;
1955
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001956 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001959 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /* It released the siglock. */
1961 goto relock;
1962 }
1963
1964 /*
1965 * We didn't actually stop, due to a race
1966 * with SIGCONT or something like that.
1967 */
1968 continue;
1969 }
1970
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001971 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 /*
1974 * Anything else is fatal, maybe with a core dump.
1975 */
1976 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001979 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001980 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * If it was able to dump core, this kills all
1983 * other threads in the group and synchronizes with
1984 * their demise. If we lost the race with another
1985 * thread getting here, it set group_exit_code
1986 * first and our do_group_exit call below will use
1987 * that value and ignore the one we pass it.
1988 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001989 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
1991
1992 /*
1993 * Death signals, no core dump.
1994 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001995 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /* NOTREACHED */
1997 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001998 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return signr;
2000}
2001
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002002void exit_signals(struct task_struct *tsk)
2003{
2004 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002005 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002006
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002007 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2008 tsk->flags |= PF_EXITING;
2009 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002010 }
2011
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002012 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002013 /*
2014 * From now this task is not visible for group-wide signals,
2015 * see wants_signal(), do_signal_stop().
2016 */
2017 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002018 if (!signal_pending(tsk))
2019 goto out;
2020
2021 /* It could be that __group_complete_signal() choose us to
2022 * notify about group-wide signal. Another thread should be
2023 * woken now to take the signal since we will not.
2024 */
2025 for (t = tsk; (t = next_thread(t)) != tsk; )
2026 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2027 recalc_sigpending_and_wake(t);
2028
2029 if (unlikely(tsk->signal->group_stop_count) &&
2030 !--tsk->signal->group_stop_count) {
2031 tsk->signal->flags = SIGNAL_STOP_STOPPED;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002032 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002033 }
2034out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002035 spin_unlock_irq(&tsk->sighand->siglock);
2036
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002037 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002038 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002039 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002040 read_unlock(&tasklist_lock);
2041 }
2042}
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044EXPORT_SYMBOL(recalc_sigpending);
2045EXPORT_SYMBOL_GPL(dequeue_signal);
2046EXPORT_SYMBOL(flush_signals);
2047EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048EXPORT_SYMBOL(send_sig);
2049EXPORT_SYMBOL(send_sig_info);
2050EXPORT_SYMBOL(sigprocmask);
2051EXPORT_SYMBOL(block_all_signals);
2052EXPORT_SYMBOL(unblock_all_signals);
2053
2054
2055/*
2056 * System call entry points.
2057 */
2058
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002059SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 struct restart_block *restart = &current_thread_info()->restart_block;
2062 return restart->fn(restart);
2063}
2064
2065long do_no_restart_syscall(struct restart_block *param)
2066{
2067 return -EINTR;
2068}
2069
2070/*
2071 * We don't need to get the kernel lock - this is all local to this
2072 * particular thread.. (and that's good, because this is _heavily_
2073 * used by various programs)
2074 */
2075
2076/*
2077 * This is also useful for kernel threads that want to temporarily
2078 * (or permanently) block certain signals.
2079 *
2080 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2081 * interface happily blocks "unblockable" signals like SIGKILL
2082 * and friends.
2083 */
2084int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2085{
2086 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002089 if (oldset)
2090 *oldset = current->blocked;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 error = 0;
2093 switch (how) {
2094 case SIG_BLOCK:
2095 sigorsets(&current->blocked, &current->blocked, set);
2096 break;
2097 case SIG_UNBLOCK:
2098 signandsets(&current->blocked, &current->blocked, set);
2099 break;
2100 case SIG_SETMASK:
2101 current->blocked = *set;
2102 break;
2103 default:
2104 error = -EINVAL;
2105 }
2106 recalc_sigpending();
2107 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return error;
2110}
2111
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002112SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2113 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 int error = -EINVAL;
2116 sigset_t old_set, new_set;
2117
2118 /* XXX: Don't preclude handling different sized sigset_t's. */
2119 if (sigsetsize != sizeof(sigset_t))
2120 goto out;
2121
2122 if (set) {
2123 error = -EFAULT;
2124 if (copy_from_user(&new_set, set, sizeof(*set)))
2125 goto out;
2126 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2127
2128 error = sigprocmask(how, &new_set, &old_set);
2129 if (error)
2130 goto out;
2131 if (oset)
2132 goto set_old;
2133 } else if (oset) {
2134 spin_lock_irq(&current->sighand->siglock);
2135 old_set = current->blocked;
2136 spin_unlock_irq(&current->sighand->siglock);
2137
2138 set_old:
2139 error = -EFAULT;
2140 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2141 goto out;
2142 }
2143 error = 0;
2144out:
2145 return error;
2146}
2147
2148long do_sigpending(void __user *set, unsigned long sigsetsize)
2149{
2150 long error = -EINVAL;
2151 sigset_t pending;
2152
2153 if (sigsetsize > sizeof(sigset_t))
2154 goto out;
2155
2156 spin_lock_irq(&current->sighand->siglock);
2157 sigorsets(&pending, &current->pending.signal,
2158 &current->signal->shared_pending.signal);
2159 spin_unlock_irq(&current->sighand->siglock);
2160
2161 /* Outside the lock because only this thread touches it. */
2162 sigandsets(&pending, &current->blocked, &pending);
2163
2164 error = -EFAULT;
2165 if (!copy_to_user(set, &pending, sigsetsize))
2166 error = 0;
2167
2168out:
2169 return error;
2170}
2171
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002172SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 return do_sigpending(set, sigsetsize);
2175}
2176
2177#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2178
2179int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2180{
2181 int err;
2182
2183 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2184 return -EFAULT;
2185 if (from->si_code < 0)
2186 return __copy_to_user(to, from, sizeof(siginfo_t))
2187 ? -EFAULT : 0;
2188 /*
2189 * If you change siginfo_t structure, please be sure
2190 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002191 * Please remember to update the signalfd_copyinfo() function
2192 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * It should never copy any pad contained in the structure
2194 * to avoid security leaks, but must copy the generic
2195 * 3 ints plus the relevant union member.
2196 */
2197 err = __put_user(from->si_signo, &to->si_signo);
2198 err |= __put_user(from->si_errno, &to->si_errno);
2199 err |= __put_user((short)from->si_code, &to->si_code);
2200 switch (from->si_code & __SI_MASK) {
2201 case __SI_KILL:
2202 err |= __put_user(from->si_pid, &to->si_pid);
2203 err |= __put_user(from->si_uid, &to->si_uid);
2204 break;
2205 case __SI_TIMER:
2206 err |= __put_user(from->si_tid, &to->si_tid);
2207 err |= __put_user(from->si_overrun, &to->si_overrun);
2208 err |= __put_user(from->si_ptr, &to->si_ptr);
2209 break;
2210 case __SI_POLL:
2211 err |= __put_user(from->si_band, &to->si_band);
2212 err |= __put_user(from->si_fd, &to->si_fd);
2213 break;
2214 case __SI_FAULT:
2215 err |= __put_user(from->si_addr, &to->si_addr);
2216#ifdef __ARCH_SI_TRAPNO
2217 err |= __put_user(from->si_trapno, &to->si_trapno);
2218#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002219#ifdef BUS_MCEERR_AO
2220 /*
2221 * Other callers might not initialize the si_lsb field,
2222 * so check explicitely for the right codes here.
2223 */
2224 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2225 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2226#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 break;
2228 case __SI_CHLD:
2229 err |= __put_user(from->si_pid, &to->si_pid);
2230 err |= __put_user(from->si_uid, &to->si_uid);
2231 err |= __put_user(from->si_status, &to->si_status);
2232 err |= __put_user(from->si_utime, &to->si_utime);
2233 err |= __put_user(from->si_stime, &to->si_stime);
2234 break;
2235 case __SI_RT: /* This is not generated by the kernel as of now. */
2236 case __SI_MESGQ: /* But this is */
2237 err |= __put_user(from->si_pid, &to->si_pid);
2238 err |= __put_user(from->si_uid, &to->si_uid);
2239 err |= __put_user(from->si_ptr, &to->si_ptr);
2240 break;
2241 default: /* this is just in case for now ... */
2242 err |= __put_user(from->si_pid, &to->si_pid);
2243 err |= __put_user(from->si_uid, &to->si_uid);
2244 break;
2245 }
2246 return err;
2247}
2248
2249#endif
2250
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002251SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2252 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2253 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 int ret, sig;
2256 sigset_t these;
2257 struct timespec ts;
2258 siginfo_t info;
2259 long timeout = 0;
2260
2261 /* XXX: Don't preclude handling different sized sigset_t's. */
2262 if (sigsetsize != sizeof(sigset_t))
2263 return -EINVAL;
2264
2265 if (copy_from_user(&these, uthese, sizeof(these)))
2266 return -EFAULT;
2267
2268 /*
2269 * Invert the set of allowed signals to get those we
2270 * want to block.
2271 */
2272 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2273 signotset(&these);
2274
2275 if (uts) {
2276 if (copy_from_user(&ts, uts, sizeof(ts)))
2277 return -EFAULT;
2278 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2279 || ts.tv_sec < 0)
2280 return -EINVAL;
2281 }
2282
2283 spin_lock_irq(&current->sighand->siglock);
2284 sig = dequeue_signal(current, &these, &info);
2285 if (!sig) {
2286 timeout = MAX_SCHEDULE_TIMEOUT;
2287 if (uts)
2288 timeout = (timespec_to_jiffies(&ts)
2289 + (ts.tv_sec || ts.tv_nsec));
2290
2291 if (timeout) {
2292 /* None ready -- temporarily unblock those we're
2293 * interested while we are sleeping in so that we'll
2294 * be awakened when they arrive. */
2295 current->real_blocked = current->blocked;
2296 sigandsets(&current->blocked, &current->blocked, &these);
2297 recalc_sigpending();
2298 spin_unlock_irq(&current->sighand->siglock);
2299
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002300 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 spin_lock_irq(&current->sighand->siglock);
2303 sig = dequeue_signal(current, &these, &info);
2304 current->blocked = current->real_blocked;
2305 siginitset(&current->real_blocked, 0);
2306 recalc_sigpending();
2307 }
2308 }
2309 spin_unlock_irq(&current->sighand->siglock);
2310
2311 if (sig) {
2312 ret = sig;
2313 if (uinfo) {
2314 if (copy_siginfo_to_user(uinfo, &info))
2315 ret = -EFAULT;
2316 }
2317 } else {
2318 ret = -EAGAIN;
2319 if (timeout)
2320 ret = -EINTR;
2321 }
2322
2323 return ret;
2324}
2325
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002326SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct siginfo info;
2329
2330 info.si_signo = sig;
2331 info.si_errno = 0;
2332 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002333 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002334 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336 return kill_something_info(sig, &info, pid);
2337}
2338
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002339static int
2340do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002341{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002342 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002343 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002344
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002345 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002346 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002347 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002348 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002349 /*
2350 * The null signal is a permissions and process existence
2351 * probe. No signal is actually delivered.
2352 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002353 if (!error && sig) {
2354 error = do_send_sig_info(sig, info, p, false);
2355 /*
2356 * If lock_task_sighand() failed we pretend the task
2357 * dies after receiving the signal. The window is tiny,
2358 * and the signal is private anyway.
2359 */
2360 if (unlikely(error == -ESRCH))
2361 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002362 }
2363 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002364 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002365
2366 return error;
2367}
2368
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002369static int do_tkill(pid_t tgid, pid_t pid, int sig)
2370{
2371 struct siginfo info;
2372
2373 info.si_signo = sig;
2374 info.si_errno = 0;
2375 info.si_code = SI_TKILL;
2376 info.si_pid = task_tgid_vnr(current);
2377 info.si_uid = current_uid();
2378
2379 return do_send_specific(tgid, pid, sig, &info);
2380}
2381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382/**
2383 * sys_tgkill - send signal to one specific thread
2384 * @tgid: the thread group ID of the thread
2385 * @pid: the PID of the thread
2386 * @sig: signal to be sent
2387 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002388 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * exists but it's not belonging to the target process anymore. This
2390 * method solves the problem of threads exiting and PIDs getting reused.
2391 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002392SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /* This is only valid for single tasks */
2395 if (pid <= 0 || tgid <= 0)
2396 return -EINVAL;
2397
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002398 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
2401/*
2402 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2403 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002404SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* This is only valid for single tasks */
2407 if (pid <= 0)
2408 return -EINVAL;
2409
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002410 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002413SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2414 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
2416 siginfo_t info;
2417
2418 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2419 return -EFAULT;
2420
2421 /* Not even root can pretend to send signals from the kernel.
2422 Nor can they impersonate a kill(), which adds source info. */
2423 if (info.si_code >= 0)
2424 return -EPERM;
2425 info.si_signo = sig;
2426
2427 /* POSIX.1b doesn't mention process groups. */
2428 return kill_proc_info(sig, &info, pid);
2429}
2430
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002431long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2432{
2433 /* This is only valid for single tasks */
2434 if (pid <= 0 || tgid <= 0)
2435 return -EINVAL;
2436
2437 /* Not even root can pretend to send signals from the kernel.
2438 Nor can they impersonate a kill(), which adds source info. */
2439 if (info->si_code >= 0)
2440 return -EPERM;
2441 info->si_signo = sig;
2442
2443 return do_send_specific(tgid, pid, sig, info);
2444}
2445
2446SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2447 siginfo_t __user *, uinfo)
2448{
2449 siginfo_t info;
2450
2451 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2452 return -EFAULT;
2453
2454 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2455}
2456
Oleg Nesterov88531f72006-03-28 16:11:24 -08002457int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002459 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002461 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002463 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return -EINVAL;
2465
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002466 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
2468 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (oact)
2470 *oact = *k;
2471
2472 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002473 sigdelsetmask(&act->sa.sa_mask,
2474 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002475 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 /*
2477 * POSIX 3.3.1.3:
2478 * "Setting a signal action to SIG_IGN for a signal that is
2479 * pending shall cause the pending signal to be discarded,
2480 * whether or not it is blocked."
2481 *
2482 * "Setting a signal action to SIG_DFL for a signal that is
2483 * pending and whose default action is to ignore the signal
2484 * (for example, SIGCHLD), shall cause the pending signal to
2485 * be discarded, whether or not it is blocked"
2486 */
Roland McGrath35de2542008-07-25 19:45:51 -07002487 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002488 sigemptyset(&mask);
2489 sigaddset(&mask, sig);
2490 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002492 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 t = next_thread(t);
2494 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497
2498 spin_unlock_irq(&current->sighand->siglock);
2499 return 0;
2500}
2501
2502int
2503do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2504{
2505 stack_t oss;
2506 int error;
2507
Linus Torvalds0083fc22009-08-01 10:34:56 -07002508 oss.ss_sp = (void __user *) current->sas_ss_sp;
2509 oss.ss_size = current->sas_ss_size;
2510 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 if (uss) {
2513 void __user *ss_sp;
2514 size_t ss_size;
2515 int ss_flags;
2516
2517 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002518 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2519 goto out;
2520 error = __get_user(ss_sp, &uss->ss_sp) |
2521 __get_user(ss_flags, &uss->ss_flags) |
2522 __get_user(ss_size, &uss->ss_size);
2523 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 goto out;
2525
2526 error = -EPERM;
2527 if (on_sig_stack(sp))
2528 goto out;
2529
2530 error = -EINVAL;
2531 /*
2532 *
2533 * Note - this code used to test ss_flags incorrectly
2534 * old code may have been written using ss_flags==0
2535 * to mean ss_flags==SS_ONSTACK (as this was the only
2536 * way that worked) - this fix preserves that older
2537 * mechanism
2538 */
2539 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2540 goto out;
2541
2542 if (ss_flags == SS_DISABLE) {
2543 ss_size = 0;
2544 ss_sp = NULL;
2545 } else {
2546 error = -ENOMEM;
2547 if (ss_size < MINSIGSTKSZ)
2548 goto out;
2549 }
2550
2551 current->sas_ss_sp = (unsigned long) ss_sp;
2552 current->sas_ss_size = ss_size;
2553 }
2554
Linus Torvalds0083fc22009-08-01 10:34:56 -07002555 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (uoss) {
2557 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002558 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002560 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2561 __put_user(oss.ss_size, &uoss->ss_size) |
2562 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565out:
2566 return error;
2567}
2568
2569#ifdef __ARCH_WANT_SYS_SIGPENDING
2570
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002571SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 return do_sigpending(set, sizeof(*set));
2574}
2575
2576#endif
2577
2578#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2579/* Some platforms have their own version with special arguments others
2580 support only sys_rt_sigprocmask. */
2581
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002582SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2583 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 int error;
2586 old_sigset_t old_set, new_set;
2587
2588 if (set) {
2589 error = -EFAULT;
2590 if (copy_from_user(&new_set, set, sizeof(*set)))
2591 goto out;
2592 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2593
2594 spin_lock_irq(&current->sighand->siglock);
2595 old_set = current->blocked.sig[0];
2596
2597 error = 0;
2598 switch (how) {
2599 default:
2600 error = -EINVAL;
2601 break;
2602 case SIG_BLOCK:
2603 sigaddsetmask(&current->blocked, new_set);
2604 break;
2605 case SIG_UNBLOCK:
2606 sigdelsetmask(&current->blocked, new_set);
2607 break;
2608 case SIG_SETMASK:
2609 current->blocked.sig[0] = new_set;
2610 break;
2611 }
2612
2613 recalc_sigpending();
2614 spin_unlock_irq(&current->sighand->siglock);
2615 if (error)
2616 goto out;
2617 if (oset)
2618 goto set_old;
2619 } else if (oset) {
2620 old_set = current->blocked.sig[0];
2621 set_old:
2622 error = -EFAULT;
2623 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2624 goto out;
2625 }
2626 error = 0;
2627out:
2628 return error;
2629}
2630#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2631
2632#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002633SYSCALL_DEFINE4(rt_sigaction, int, sig,
2634 const struct sigaction __user *, act,
2635 struct sigaction __user *, oact,
2636 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 struct k_sigaction new_sa, old_sa;
2639 int ret = -EINVAL;
2640
2641 /* XXX: Don't preclude handling different sized sigset_t's. */
2642 if (sigsetsize != sizeof(sigset_t))
2643 goto out;
2644
2645 if (act) {
2646 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2647 return -EFAULT;
2648 }
2649
2650 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2651
2652 if (!ret && oact) {
2653 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2654 return -EFAULT;
2655 }
2656out:
2657 return ret;
2658}
2659#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2660
2661#ifdef __ARCH_WANT_SYS_SGETMASK
2662
2663/*
2664 * For backwards compatibility. Functionality superseded by sigprocmask.
2665 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002666SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
2668 /* SMP safe */
2669 return current->blocked.sig[0];
2670}
2671
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002672SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
2674 int old;
2675
2676 spin_lock_irq(&current->sighand->siglock);
2677 old = current->blocked.sig[0];
2678
2679 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2680 sigmask(SIGSTOP)));
2681 recalc_sigpending();
2682 spin_unlock_irq(&current->sighand->siglock);
2683
2684 return old;
2685}
2686#endif /* __ARCH_WANT_SGETMASK */
2687
2688#ifdef __ARCH_WANT_SYS_SIGNAL
2689/*
2690 * For backwards compatibility. Functionality superseded by sigaction.
2691 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002692SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
2694 struct k_sigaction new_sa, old_sa;
2695 int ret;
2696
2697 new_sa.sa.sa_handler = handler;
2698 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002699 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 ret = do_sigaction(sig, &new_sa, &old_sa);
2702
2703 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2704}
2705#endif /* __ARCH_WANT_SYS_SIGNAL */
2706
2707#ifdef __ARCH_WANT_SYS_PAUSE
2708
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002709SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 current->state = TASK_INTERRUPTIBLE;
2712 schedule();
2713 return -ERESTARTNOHAND;
2714}
2715
2716#endif
2717
David Woodhouse150256d2006-01-18 17:43:57 -08002718#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002719SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002720{
2721 sigset_t newset;
2722
2723 /* XXX: Don't preclude handling different sized sigset_t's. */
2724 if (sigsetsize != sizeof(sigset_t))
2725 return -EINVAL;
2726
2727 if (copy_from_user(&newset, unewset, sizeof(newset)))
2728 return -EFAULT;
2729 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2730
2731 spin_lock_irq(&current->sighand->siglock);
2732 current->saved_sigmask = current->blocked;
2733 current->blocked = newset;
2734 recalc_sigpending();
2735 spin_unlock_irq(&current->sighand->siglock);
2736
2737 current->state = TASK_INTERRUPTIBLE;
2738 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002739 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002740 return -ERESTARTNOHAND;
2741}
2742#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2743
David Howellsf269fdd2006-09-27 01:50:23 -07002744__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2745{
2746 return NULL;
2747}
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749void __init signals_init(void)
2750{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002751 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752}
Jason Wessel67fc4e02010-05-20 21:04:21 -05002753
2754#ifdef CONFIG_KGDB_KDB
2755#include <linux/kdb.h>
2756/*
2757 * kdb_send_sig_info - Allows kdb to send signals without exposing
2758 * signal internals. This function checks if the required locks are
2759 * available before calling the main signal code, to avoid kdb
2760 * deadlocks.
2761 */
2762void
2763kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
2764{
2765 static struct task_struct *kdb_prev_t;
2766 int sig, new_t;
2767 if (!spin_trylock(&t->sighand->siglock)) {
2768 kdb_printf("Can't do kill command now.\n"
2769 "The sigmask lock is held somewhere else in "
2770 "kernel, try again later\n");
2771 return;
2772 }
2773 spin_unlock(&t->sighand->siglock);
2774 new_t = kdb_prev_t != t;
2775 kdb_prev_t = t;
2776 if (t->state != TASK_RUNNING && new_t) {
2777 kdb_printf("Process is not RUNNING, sending a signal from "
2778 "kdb risks deadlock\n"
2779 "on the run queue locks. "
2780 "The signal has _not_ been sent.\n"
2781 "Reissue the kill command if you want to risk "
2782 "the deadlock.\n");
2783 return;
2784 }
2785 sig = info->si_signo;
2786 if (send_sig_info(sig, info, t))
2787 kdb_printf("Fail to deliver Signal %d to process %d.\n",
2788 sig, t->pid);
2789 else
2790 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
2791}
2792#endif /* CONFIG_KGDB_KDB */