blob: ecb20089eaffb8baef7e7add27a25ccb0cec5384 [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
Tejun Heoe5c19022011-03-23 10:37:00 +0100226/**
227 * task_clear_group_stop_pending - clear pending group stop
228 * @task: target task
229 *
230 * Clear group stop states for @task.
231 *
232 * CONTEXT:
233 * Must be called with @task->sighand->siglock held.
234 */
235static void task_clear_group_stop_pending(struct task_struct *task)
236{
237 task->group_stop &= ~GROUP_STOP_CONSUME;
238}
239
240/**
241 * task_participate_group_stop - participate in a group stop
242 * @task: task participating in a group stop
243 *
244 * @task is participating in a group stop. Group stop states are cleared
245 * and the group stop count is consumed if %GROUP_STOP_CONSUME was set. If
246 * the consumption completes the group stop, the appropriate %SIGNAL_*
247 * flags are set.
248 *
249 * CONTEXT:
250 * Must be called with @task->sighand->siglock held.
251 */
252static bool task_participate_group_stop(struct task_struct *task)
253{
254 struct signal_struct *sig = task->signal;
255 bool consume = task->group_stop & GROUP_STOP_CONSUME;
256
257 task_clear_group_stop_pending(task);
258
259 if (!consume)
260 return false;
261
262 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
263 sig->group_stop_count--;
264
265 if (!sig->group_stop_count) {
266 sig->flags = SIGNAL_STOP_STOPPED;
267 return true;
268 }
269 return false;
270}
271
David Howellsc69e8d92008-11-14 10:39:19 +1100272/*
273 * allocate a new signal queue record
274 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100275 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100276 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900277static struct sigqueue *
278__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800281 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800283 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000284 * Protect access to @t credentials. This can go away when all
285 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800286 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000287 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100288 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800289 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000290 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800293 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800294 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900296 } else {
297 print_dropped_signal(sig);
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800301 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100302 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 } else {
304 INIT_LIST_HEAD(&q->list);
305 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100306 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
David Howellsd84f4f92008-11-14 10:39:23 +1100308
309 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Andrew Morton514a01b2006-02-03 03:04:41 -0800312static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 if (q->flags & SIGQUEUE_PREALLOC)
315 return;
316 atomic_dec(&q->user->sigpending);
317 free_uid(q->user);
318 kmem_cache_free(sigqueue_cachep, q);
319}
320
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800321void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct sigqueue *q;
324
325 sigemptyset(&queue->signal);
326 while (!list_empty(&queue->list)) {
327 q = list_entry(queue->list.next, struct sigqueue , list);
328 list_del_init(&q->list);
329 __sigqueue_free(q);
330 }
331}
332
333/*
334 * Flush all pending signals for a task.
335 */
David Howells3bcac022009-04-29 13:45:05 +0100336void __flush_signals(struct task_struct *t)
337{
338 clear_tsk_thread_flag(t, TIF_SIGPENDING);
339 flush_sigqueue(&t->pending);
340 flush_sigqueue(&t->signal->shared_pending);
341}
342
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800343void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 unsigned long flags;
346
347 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100348 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_unlock_irqrestore(&t->sighand->siglock, flags);
350}
351
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400352static void __flush_itimer_signals(struct sigpending *pending)
353{
354 sigset_t signal, retain;
355 struct sigqueue *q, *n;
356
357 signal = pending->signal;
358 sigemptyset(&retain);
359
360 list_for_each_entry_safe(q, n, &pending->list, list) {
361 int sig = q->info.si_signo;
362
363 if (likely(q->info.si_code != SI_TIMER)) {
364 sigaddset(&retain, sig);
365 } else {
366 sigdelset(&signal, sig);
367 list_del_init(&q->list);
368 __sigqueue_free(q);
369 }
370 }
371
372 sigorsets(&pending->signal, &signal, &retain);
373}
374
375void flush_itimer_signals(void)
376{
377 struct task_struct *tsk = current;
378 unsigned long flags;
379
380 spin_lock_irqsave(&tsk->sighand->siglock, flags);
381 __flush_itimer_signals(&tsk->pending);
382 __flush_itimer_signals(&tsk->signal->shared_pending);
383 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
384}
385
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700386void ignore_signals(struct task_struct *t)
387{
388 int i;
389
390 for (i = 0; i < _NSIG; ++i)
391 t->sighand->action[i].sa.sa_handler = SIG_IGN;
392
393 flush_signals(t);
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * Flush all handlers for a task.
398 */
399
400void
401flush_signal_handlers(struct task_struct *t, int force_default)
402{
403 int i;
404 struct k_sigaction *ka = &t->sighand->action[0];
405 for (i = _NSIG ; i != 0 ; i--) {
406 if (force_default || ka->sa.sa_handler != SIG_IGN)
407 ka->sa.sa_handler = SIG_DFL;
408 ka->sa.sa_flags = 0;
409 sigemptyset(&ka->sa.sa_mask);
410 ka++;
411 }
412}
413
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200414int unhandled_signal(struct task_struct *tsk, int sig)
415{
Roland McGrath445a91d2008-07-25 19:45:52 -0700416 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700417 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200418 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700419 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200420 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700421 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425/* Notify the system that a driver wants to block all signals for this
426 * process, and wants to be notified if any signals at all were to be
427 * sent/acted upon. If the notifier routine returns non-zero, then the
428 * signal will be acted upon after all. If the notifier routine returns 0,
429 * then then signal will be blocked. Only one block per process is
430 * allowed. priv is a pointer to private data that the notifier routine
431 * can use to determine if the signal should be blocked or not. */
432
433void
434block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
435{
436 unsigned long flags;
437
438 spin_lock_irqsave(&current->sighand->siglock, flags);
439 current->notifier_mask = mask;
440 current->notifier_data = priv;
441 current->notifier = notifier;
442 spin_unlock_irqrestore(&current->sighand->siglock, flags);
443}
444
445/* Notify the system that blocking has ended. */
446
447void
448unblock_all_signals(void)
449{
450 unsigned long flags;
451
452 spin_lock_irqsave(&current->sighand->siglock, flags);
453 current->notifier = NULL;
454 current->notifier_data = NULL;
455 recalc_sigpending();
456 spin_unlock_irqrestore(&current->sighand->siglock, flags);
457}
458
Oleg Nesterov100360f2008-07-25 01:47:29 -0700459static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Collect the siginfo appropriate to this signal. Check if
465 * there is another siginfo for the same signal.
466 */
467 list_for_each_entry(q, &list->list, list) {
468 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700469 if (first)
470 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 first = q;
472 }
473 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700474
475 sigdelset(&list->signal, sig);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700478still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 list_del_init(&first->list);
480 copy_siginfo(info, &first->info);
481 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /* Ok, it wasn't in the queue. This must be
484 a fast-pathed signal or we must have been
485 out of queue space. So zero out the info.
486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 info->si_signo = sig;
488 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800489 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 info->si_pid = 0;
491 info->si_uid = 0;
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
496 siginfo_t *info)
497{
Roland McGrath27d91e02006-09-29 02:00:31 -0700498 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (sig) {
501 if (current->notifier) {
502 if (sigismember(current->notifier_mask, sig)) {
503 if (!(current->notifier)(current->notifier_data)) {
504 clear_thread_flag(TIF_SIGPENDING);
505 return 0;
506 }
507 }
508 }
509
Oleg Nesterov100360f2008-07-25 01:47:29 -0700510 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 return sig;
514}
515
516/*
517 * Dequeue a signal and return the element to the caller, which is
518 * expected to free it.
519 *
520 * All callers have to hold the siglock.
521 */
522int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
523{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700524 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000525
526 /* We only dequeue private signals from ourselves, we don't let
527 * signalfd steal them
528 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700529 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800530 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 signr = __dequeue_signal(&tsk->signal->shared_pending,
532 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800533 /*
534 * itimer signal ?
535 *
536 * itimers are process shared and we restart periodic
537 * itimers in the signal delivery path to prevent DoS
538 * attacks in the high resolution timer case. This is
539 * compliant with the old way of self restarting
540 * itimers, as the SIGALRM is a legacy signal and only
541 * queued once. Changing the restart behaviour to
542 * restart the timer in the signal dequeue path is
543 * reducing the timer noise on heavy loaded !highres
544 * systems too.
545 */
546 if (unlikely(signr == SIGALRM)) {
547 struct hrtimer *tmr = &tsk->signal->real_timer;
548
549 if (!hrtimer_is_queued(tmr) &&
550 tsk->signal->it_real_incr.tv64 != 0) {
551 hrtimer_forward(tmr, tmr->base->get_time(),
552 tsk->signal->it_real_incr);
553 hrtimer_restart(tmr);
554 }
555 }
556 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700557
Davide Libenzib8fceee2007-09-20 12:40:16 -0700558 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700559 if (!signr)
560 return 0;
561
562 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800563 /*
564 * Set a marker that we have dequeued a stop signal. Our
565 * caller might release the siglock and then the pending
566 * stop signal it is about to process is no longer in the
567 * pending bitmasks, but must still be cleared by a SIGCONT
568 * (and overruled by a SIGKILL). So those cases clear this
569 * shared flag after we've set it. Note that this flag may
570 * remain set after the signal we return is ignored or
571 * handled. That doesn't matter because its only purpose
572 * is to alert stop-signal processing code when another
573 * processor has come along and cleared the flag.
574 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700575 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800576 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700577 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /*
579 * Release the siglock to ensure proper locking order
580 * of timer locks outside of siglocks. Note, we leave
581 * irqs disabled here, since the posix-timers code is
582 * about to disable them again anyway.
583 */
584 spin_unlock(&tsk->sighand->siglock);
585 do_schedule_next_timer(info);
586 spin_lock(&tsk->sighand->siglock);
587 }
588 return signr;
589}
590
591/*
592 * Tell a process that it has a new active signal..
593 *
594 * NOTE! we rely on the previous spin_lock to
595 * lock interrupts for us! We can only be called with
596 * "siglock" held, and the local interrupt must
597 * have been disabled when that got acquired!
598 *
599 * No need to set need_resched since signal event passing
600 * goes through ->blocked
601 */
602void signal_wake_up(struct task_struct *t, int resume)
603{
604 unsigned int mask;
605
606 set_tsk_thread_flag(t, TIF_SIGPENDING);
607
608 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500609 * For SIGKILL, we want to wake it up in the stopped/traced/killable
610 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * executing another processor and just now entering stopped state.
612 * By using wake_up_state, we ensure the process will wake up and
613 * handle its death signal.
614 */
615 mask = TASK_INTERRUPTIBLE;
616 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500617 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (!wake_up_state(t, mask))
619 kick_process(t);
620}
621
622/*
623 * Remove signals in mask from the pending set and queue.
624 * Returns 1 if any signals were found.
625 *
626 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800627 *
628 * This version takes a sigset mask and looks at all signals,
629 * not just those in the first mask word.
630 */
631static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
632{
633 struct sigqueue *q, *n;
634 sigset_t m;
635
636 sigandsets(&m, mask, &s->signal);
637 if (sigisemptyset(&m))
638 return 0;
639
640 signandsets(&s->signal, &s->signal, mask);
641 list_for_each_entry_safe(q, n, &s->list, list) {
642 if (sigismember(mask, q->info.si_signo)) {
643 list_del_init(&q->list);
644 __sigqueue_free(q);
645 }
646 }
647 return 1;
648}
649/*
650 * Remove signals in mask from the pending set and queue.
651 * Returns 1 if any signals were found.
652 *
653 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
655static int rm_from_queue(unsigned long mask, struct sigpending *s)
656{
657 struct sigqueue *q, *n;
658
659 if (!sigtestsetmask(&s->signal, mask))
660 return 0;
661
662 sigdelsetmask(&s->signal, mask);
663 list_for_each_entry_safe(q, n, &s->list, list) {
664 if (q->info.si_signo < SIGRTMIN &&
665 (mask & sigmask(q->info.si_signo))) {
666 list_del_init(&q->list);
667 __sigqueue_free(q);
668 }
669 }
670 return 1;
671}
672
Oleg Nesterov614c5172009-12-15 16:47:22 -0800673static inline int is_si_special(const struct siginfo *info)
674{
675 return info <= SEND_SIG_FORCED;
676}
677
678static inline bool si_fromuser(const struct siginfo *info)
679{
680 return info == SEND_SIG_NOINFO ||
681 (!is_si_special(info) && SI_FROMUSER(info));
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/*
685 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100686 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
688static int check_kill_permission(int sig, struct siginfo *info,
689 struct task_struct *t)
690{
Oleg Nesterov065add32010-05-26 14:42:54 -0700691 const struct cred *cred, *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700692 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700693 int error;
694
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700695 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700696 return -EINVAL;
697
Oleg Nesterov614c5172009-12-15 16:47:22 -0800698 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700699 return 0;
700
701 error = audit_signal_info(sig, t); /* Let audit system see the signal */
702 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400704
Oleg Nesterov065add32010-05-26 14:42:54 -0700705 cred = current_cred();
David Howellsc69e8d92008-11-14 10:39:19 +1100706 tcred = __task_cred(t);
Oleg Nesterov065add32010-05-26 14:42:54 -0700707 if (!same_thread_group(current, t) &&
708 (cred->euid ^ tcred->suid) &&
David Howellsc69e8d92008-11-14 10:39:19 +1100709 (cred->euid ^ tcred->uid) &&
710 (cred->uid ^ tcred->suid) &&
711 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700712 !capable(CAP_KILL)) {
713 switch (sig) {
714 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700715 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700716 /*
717 * We don't return the error if sid == NULL. The
718 * task was unhashed, the caller must notice this.
719 */
720 if (!sid || sid == task_session(current))
721 break;
722 default:
723 return -EPERM;
724 }
725 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100726
Amy Griffise54dc242007-03-29 18:01:04 -0400727 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700731 * Handle magic process-wide effects of stop/continue signals. Unlike
732 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * time regardless of blocking, ignoring, or handling. This does the
734 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700735 * signals. The process stop is done as a signal action for SIG_DFL.
736 *
737 * Returns true if the signal should be actually delivered, otherwise
738 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700740static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700742 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 struct task_struct *t;
744
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700745 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700747 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700749 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 /*
751 * This is a stop signal. Remove SIGCONT from all queues.
752 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700753 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 t = p;
755 do {
756 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700757 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700759 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 /*
761 * Remove all stop signals from all queues,
762 * and wake all threads.
763 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700764 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 t = p;
766 do {
767 unsigned int state;
768 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /*
770 * If there is a handler for SIGCONT, we must make
771 * sure that no thread returns to user mode before
772 * we post the signal, in case it was the only
773 * thread eligible to run the signal handler--then
774 * it must not do anything between resuming and
775 * running the handler. With the TIF_SIGPENDING
776 * flag set, the thread will pause and acquire the
777 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700778 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
780 * Wake up the stopped thread _after_ setting
781 * TIF_SIGPENDING
782 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500783 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
785 set_tsk_thread_flag(t, TIF_SIGPENDING);
786 state |= TASK_INTERRUPTIBLE;
787 }
788 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700789 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700791 /*
792 * Notify the parent with CLD_CONTINUED if we were stopped.
793 *
794 * If we were in the middle of a group stop, we pretend it
795 * was already finished, and then continued. Since SIGCHLD
796 * doesn't queue we report only CLD_STOPPED, as if the next
797 * CLD_CONTINUED was dropped.
798 */
799 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700800 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700801 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700802 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700803 why |= SIGNAL_CLD_STOPPED;
804
805 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700806 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700807 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700808 * will take ->siglock, notice SIGNAL_CLD_MASK, and
809 * notify its parent. See get_signal_to_deliver().
810 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700811 signal->flags = why | SIGNAL_STOP_CONTINUED;
812 signal->group_stop_count = 0;
813 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 } else {
815 /*
816 * We are not stopped, but there could be a stop
817 * signal in the middle of being processed after
818 * being removed from the queue. Clear that too.
819 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700820 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700823
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700824 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700827/*
828 * Test if P wants to take SIG. After we've checked all threads with this,
829 * it's equivalent to finding no threads not blocking SIG. Any threads not
830 * blocking SIG were ruled out because they are not running and already
831 * have pending signals. Such threads will dequeue from the shared queue
832 * as soon as they're available, so putting the signal on the shared queue
833 * will be equivalent to sending it to one such thread.
834 */
835static inline int wants_signal(int sig, struct task_struct *p)
836{
837 if (sigismember(&p->blocked, sig))
838 return 0;
839 if (p->flags & PF_EXITING)
840 return 0;
841 if (sig == SIGKILL)
842 return 1;
843 if (task_is_stopped_or_traced(p))
844 return 0;
845 return task_curr(p) || !signal_pending(p);
846}
847
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700848static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700849{
850 struct signal_struct *signal = p->signal;
851 struct task_struct *t;
852
853 /*
854 * Now find a thread we can wake up to take the signal off the queue.
855 *
856 * If the main thread wants the signal, it gets first crack.
857 * Probably the least surprising to the average bear.
858 */
859 if (wants_signal(sig, p))
860 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700861 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700862 /*
863 * There is just one thread and it does not need to be woken.
864 * It will dequeue unblocked signals before it runs again.
865 */
866 return;
867 else {
868 /*
869 * Otherwise try to find a suitable thread.
870 */
871 t = signal->curr_target;
872 while (!wants_signal(sig, t)) {
873 t = next_thread(t);
874 if (t == signal->curr_target)
875 /*
876 * No thread needs to be woken.
877 * Any eligible threads will see
878 * the signal in the queue soon.
879 */
880 return;
881 }
882 signal->curr_target = t;
883 }
884
885 /*
886 * Found a killable thread. If the signal will be fatal,
887 * then start taking the whole group down immediately.
888 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700889 if (sig_fatal(p, sig) &&
890 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700891 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700892 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700893 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700894 /*
895 * This signal will be fatal to the whole group.
896 */
897 if (!sig_kernel_coredump(sig)) {
898 /*
899 * Start a group exit and wake everybody up.
900 * This way we don't have other threads
901 * running and doing things after a slower
902 * thread has the fatal signal pending.
903 */
904 signal->flags = SIGNAL_GROUP_EXIT;
905 signal->group_exit_code = sig;
906 signal->group_stop_count = 0;
907 t = p;
908 do {
909 sigaddset(&t->pending.signal, SIGKILL);
910 signal_wake_up(t, 1);
911 } while_each_thread(p, t);
912 return;
913 }
914 }
915
916 /*
917 * The signal is already in the shared-pending queue.
918 * Tell the chosen thread to wake up and dequeue it.
919 */
920 signal_wake_up(t, sig == SIGKILL);
921 return;
922}
923
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700924static inline int legacy_queue(struct sigpending *signals, int sig)
925{
926 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
927}
928
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700929static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
930 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700932 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700933 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200934 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500936 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400937
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700938 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700939
940 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700941 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700942
943 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700945 * Short-circuit ignored signals and support queuing
946 * exactly one non-rt signal, so that we can get more
947 * detailed information about the cause of the signal.
948 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700949 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700950 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700951 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 * fast-pathed signals for kernel-internal things like SIGSTOP
953 * or SIGKILL.
954 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800955 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 goto out_set;
957
958 /* Real-time signals must be queued if sent by sigqueue, or
959 some other real-time mechanism. It is implementation
960 defined whether kill() does so. We attempt to do so, on
961 the principle of least surprise, but since kill is not
962 allowed to fail with EAGAIN when low on memory we just
963 make sure at least one signal gets delivered and don't
964 pass on the info struct. */
965
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200966 if (sig < SIGRTMIN)
967 override_rlimit = (is_si_special(info) || info->si_code >= 0);
968 else
969 override_rlimit = 0;
970
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900971 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200972 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700974 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800976 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 q->info.si_signo = sig;
978 q->info.si_errno = 0;
979 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800980 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800981 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100982 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800984 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 q->info.si_signo = sig;
986 q->info.si_errno = 0;
987 q->info.si_code = SI_KERNEL;
988 q->info.si_pid = 0;
989 q->info.si_uid = 0;
990 break;
991 default:
992 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700993 if (from_ancestor_ns)
994 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 break;
996 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800997 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500998 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
999 /*
1000 * Queue overflow, abort. We may abort if the
1001 * signal was rt and sent by user using something
1002 * other than kill().
1003 */
1004 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001006 } else {
1007 /*
1008 * This is a silent loss of information. We still
1009 * send the signal, but the *info bits are lost.
1010 */
1011 trace_signal_lose_info(sig, group, info);
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
1015out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001016 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001017 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001018 complete_signal(sig, t, group);
1019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001022static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1023 int group)
1024{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001025 int from_ancestor_ns = 0;
1026
1027#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001028 from_ancestor_ns = si_fromuser(info) &&
1029 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001030#endif
1031
1032 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001033}
1034
Ingo Molnar45807a12007-07-15 23:40:10 -07001035static void print_fatal_signal(struct pt_regs *regs, int signr)
1036{
1037 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001038 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001039
Al Viroca5cd872007-10-29 04:31:16 +00001040#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001041 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001042 {
1043 int i;
1044 for (i = 0; i < 16; i++) {
1045 unsigned char insn;
1046
Andi Kleenb45c6e72010-01-08 14:42:52 -08001047 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1048 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001049 printk("%02x ", insn);
1050 }
1051 }
1052#endif
1053 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001054 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001055 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001056 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001057}
1058
1059static int __init setup_print_fatal_signals(char *str)
1060{
1061 get_option (&str, &print_fatal_signals);
1062
1063 return 1;
1064}
1065
1066__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001068int
1069__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1070{
1071 return send_signal(sig, info, p, 1);
1072}
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074static int
1075specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1076{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001077 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001080int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1081 bool group)
1082{
1083 unsigned long flags;
1084 int ret = -ESRCH;
1085
1086 if (lock_task_sighand(p, &flags)) {
1087 ret = send_signal(sig, info, p, group);
1088 unlock_task_sighand(p, &flags);
1089 }
1090
1091 return ret;
1092}
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094/*
1095 * Force a signal that the process can't ignore: if necessary
1096 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001097 *
1098 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1099 * since we do not want to have a signal handler that was blocked
1100 * be invoked when user space had explicitly blocked it.
1101 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001102 * We don't want to have recursive SIGSEGV's etc, for example,
1103 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105int
1106force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1107{
1108 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001109 int ret, blocked, ignored;
1110 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001113 action = &t->sighand->action[sig-1];
1114 ignored = action->sa.sa_handler == SIG_IGN;
1115 blocked = sigismember(&t->blocked, sig);
1116 if (blocked || ignored) {
1117 action->sa.sa_handler = SIG_DFL;
1118 if (blocked) {
1119 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001120 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001123 if (action->sa.sa_handler == SIG_DFL)
1124 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ret = specific_send_sig_info(sig, info, t);
1126 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1127
1128 return ret;
1129}
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/*
1132 * Nuke all other threads in the group.
1133 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001134int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001136 struct task_struct *t = p;
1137 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 p->signal->group_stop_count = 0;
1140
Oleg Nesterov09faef12010-05-26 14:43:11 -07001141 while_each_thread(p, t) {
1142 count++;
1143
1144 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 if (t->exit_state)
1146 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 signal_wake_up(t, 1);
1149 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001150
1151 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001154struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1155 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001156{
1157 struct sighand_struct *sighand;
1158
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001159 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001160 for (;;) {
1161 sighand = rcu_dereference(tsk->sighand);
1162 if (unlikely(sighand == NULL))
1163 break;
1164
1165 spin_lock_irqsave(&sighand->siglock, *flags);
1166 if (likely(sighand == tsk->sighand))
1167 break;
1168 spin_unlock_irqrestore(&sighand->siglock, *flags);
1169 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001170 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001171
1172 return sighand;
1173}
1174
David Howellsc69e8d92008-11-14 10:39:19 +11001175/*
1176 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001177 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1179{
David Howells694f6902010-08-04 16:59:14 +01001180 int ret;
1181
1182 rcu_read_lock();
1183 ret = check_kill_permission(sig, info, p);
1184 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001186 if (!ret && sig)
1187 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 return ret;
1190}
1191
1192/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001193 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001195 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001197int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 struct task_struct *p = NULL;
1200 int retval, success;
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 success = 0;
1203 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001204 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err = group_send_sig_info(sig, info, p);
1206 success |= !err;
1207 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001208 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return success ? 0 : retval;
1210}
1211
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001212int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001214 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 struct task_struct *p;
1216
Ingo Molnare56d0902006-01-08 01:01:37 -08001217 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001218retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001219 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001220 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001222 if (unlikely(error == -ESRCH))
1223 /*
1224 * The task was unhashed in between, try again.
1225 * If it is dead, pid_task() will return NULL,
1226 * if we race with de_thread() it will find the
1227 * new leader.
1228 */
1229 goto retry;
1230 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001231 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return error;
1234}
1235
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001236int
1237kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001238{
1239 int error;
1240 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001241 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001242 rcu_read_unlock();
1243 return error;
1244}
1245
Eric W. Biederman2425c082006-10-02 02:17:28 -07001246/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1247int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001248 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001249{
1250 int ret = -EINVAL;
1251 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001252 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001253 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001254
1255 if (!valid_signal(sig))
1256 return ret;
1257
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001258 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001259 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001260 if (!p) {
1261 ret = -ESRCH;
1262 goto out_unlock;
1263 }
David Howellsc69e8d92008-11-14 10:39:19 +11001264 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001265 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001266 euid != pcred->suid && euid != pcred->uid &&
1267 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001268 ret = -EPERM;
1269 goto out_unlock;
1270 }
David Quigley8f95dc52006-06-30 01:55:47 -07001271 ret = security_task_kill(p, info, sig, secid);
1272 if (ret)
1273 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001274
1275 if (sig) {
1276 if (lock_task_sighand(p, &flags)) {
1277 ret = __send_signal(sig, info, p, 1, 0);
1278 unlock_task_sighand(p, &flags);
1279 } else
1280 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001281 }
1282out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001283 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001284 return ret;
1285}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001286EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288/*
1289 * kill_something_info() interprets pid in interesting ways just like kill(2).
1290 *
1291 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1292 * is probably wrong. Should make it like BSD or SYSV.
1293 */
1294
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001295static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001297 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001298
1299 if (pid > 0) {
1300 rcu_read_lock();
1301 ret = kill_pid_info(sig, info, find_vpid(pid));
1302 rcu_read_unlock();
1303 return ret;
1304 }
1305
1306 read_lock(&tasklist_lock);
1307 if (pid != -1) {
1308 ret = __kill_pgrp_info(sig, info,
1309 pid ? find_vpid(-pid) : task_pgrp(current));
1310 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 int retval = 0, count = 0;
1312 struct task_struct * p;
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001315 if (task_pid_vnr(p) > 1 &&
1316 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 int err = group_send_sig_info(sig, info, p);
1318 ++count;
1319 if (err != -EPERM)
1320 retval = err;
1321 }
1322 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001323 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001325 read_unlock(&tasklist_lock);
1326
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330/*
1331 * These are for backward compatibility with the rest of the kernel source.
1332 */
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334int
1335send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 /*
1338 * Make sure legacy kernel users don't send in bad values
1339 * (normal paths check this in check_kill_permission).
1340 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001341 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return -EINVAL;
1343
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001344 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001347#define __si_special(priv) \
1348 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350int
1351send_sig(int sig, struct task_struct *p, int priv)
1352{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001353 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356void
1357force_sig(int sig, struct task_struct *p)
1358{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001359 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362/*
1363 * When things go south during signal handling, we
1364 * will force a SIGSEGV. And if the signal that caused
1365 * the problem was already a SIGSEGV, we'll want to
1366 * make sure we don't even try to deliver the signal..
1367 */
1368int
1369force_sigsegv(int sig, struct task_struct *p)
1370{
1371 if (sig == SIGSEGV) {
1372 unsigned long flags;
1373 spin_lock_irqsave(&p->sighand->siglock, flags);
1374 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1375 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1376 }
1377 force_sig(SIGSEGV, p);
1378 return 0;
1379}
1380
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001381int kill_pgrp(struct pid *pid, int sig, int priv)
1382{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001383 int ret;
1384
1385 read_lock(&tasklist_lock);
1386 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1387 read_unlock(&tasklist_lock);
1388
1389 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001390}
1391EXPORT_SYMBOL(kill_pgrp);
1392
1393int kill_pid(struct pid *pid, int sig, int priv)
1394{
1395 return kill_pid_info(sig, __si_special(priv), pid);
1396}
1397EXPORT_SYMBOL(kill_pid);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/*
1400 * These functions support sending signals using preallocated sigqueue
1401 * structures. This is needed "because realtime applications cannot
1402 * afford to lose notifications of asynchronous events, like timer
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001403 * expirations or I/O completions". In the case of Posix Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * we allocate the sigqueue structure from the timer_create. If this
1405 * allocation fails we are able to report the failure to the application
1406 * with an EAGAIN error.
1407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408struct sigqueue *sigqueue_alloc(void)
1409{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001410 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001412 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001414
1415 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416}
1417
1418void sigqueue_free(struct sigqueue *q)
1419{
1420 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001421 spinlock_t *lock = &current->sighand->siglock;
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1424 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001425 * We must hold ->siglock while testing q->list
1426 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001427 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001429 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001430 q->flags &= ~SIGQUEUE_PREALLOC;
1431 /*
1432 * If it is queued it will be freed when dequeued,
1433 * like the "regular" sigqueue.
1434 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001435 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001436 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001437 spin_unlock_irqrestore(lock, flags);
1438
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001439 if (q)
1440 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001443int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001444{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001445 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001446 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001447 unsigned long flags;
1448 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001449
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001450 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001451
1452 ret = -1;
1453 if (!likely(lock_task_sighand(t, &flags)))
1454 goto ret;
1455
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001456 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001457 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001458 goto out;
1459
1460 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001461 if (unlikely(!list_empty(&q->list))) {
1462 /*
1463 * If an SI_TIMER entry is already queue just increment
1464 * the overrun count.
1465 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001466 BUG_ON(q->info.si_code != SI_TIMER);
1467 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001468 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001469 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001470 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001471
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001472 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001473 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001474 list_add_tail(&q->list, &pending->list);
1475 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001476 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001477out:
1478 unlock_task_sighand(t, &flags);
1479ret:
1480 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 * Let a parent know about the death of a child.
1485 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001486 *
1487 * Returns -1 if our parent ignored us and so we've switched to
1488 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001490int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct siginfo info;
1493 unsigned long flags;
1494 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001495 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 BUG_ON(sig == -1);
1498
1499 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001500 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001502 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1504
1505 info.si_signo = sig;
1506 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001507 /*
1508 * we are under tasklist_lock here so our parent is tied to
1509 * us and cannot exit and release its namespace.
1510 *
1511 * the only it can is to switch its nsproxy with sys_unshare,
1512 * bu uncharing pid namespaces is not allowed, so we'll always
1513 * see relevant namespace
1514 *
1515 * write_lock() currently calls preempt_disable() which is the
1516 * same as rcu_read_lock(), but according to Oleg, this is not
1517 * correct to rely on this
1518 */
1519 rcu_read_lock();
1520 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001521 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001522 rcu_read_unlock();
1523
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001524 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1525 tsk->signal->utime));
1526 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1527 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
1529 info.si_status = tsk->exit_code & 0x7f;
1530 if (tsk->exit_code & 0x80)
1531 info.si_code = CLD_DUMPED;
1532 else if (tsk->exit_code & 0x7f)
1533 info.si_code = CLD_KILLED;
1534 else {
1535 info.si_code = CLD_EXITED;
1536 info.si_status = tsk->exit_code >> 8;
1537 }
1538
1539 psig = tsk->parent->sighand;
1540 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001541 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1543 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1544 /*
1545 * We are exiting and our parent doesn't care. POSIX.1
1546 * defines special semantics for setting SIGCHLD to SIG_IGN
1547 * or setting the SA_NOCLDWAIT flag: we should be reaped
1548 * automatically and not left for our parent's wait4 call.
1549 * Rather than having the parent do it as a magic kind of
1550 * signal handler, we just set this to tell do_exit that we
1551 * can be cleaned up without becoming a zombie. Note that
1552 * we still call __wake_up_parent in this case, because a
1553 * blocked sys_wait4 might now return -ECHILD.
1554 *
1555 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1556 * is implementation-defined: we do (if you don't want
1557 * it, just use SIG_IGN instead).
1558 */
Roland McGrath1b046242008-08-19 20:37:07 -07001559 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001561 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001563 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 __group_send_sig_info(sig, &info, tsk->parent);
1565 __wake_up_parent(tsk, tsk->parent);
1566 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001567
Roland McGrath1b046242008-08-19 20:37:07 -07001568 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001571static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct siginfo info;
1574 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001575 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 struct sighand_struct *sighand;
1577
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001578 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001579 parent = tsk->parent;
1580 else {
1581 tsk = tsk->group_leader;
1582 parent = tsk->real_parent;
1583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 info.si_signo = SIGCHLD;
1586 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001587 /*
1588 * see comment in do_notify_parent() abot the following 3 lines
1589 */
1590 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001591 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001592 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001593 rcu_read_unlock();
1594
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001595 info.si_utime = cputime_to_clock_t(tsk->utime);
1596 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 info.si_code = why;
1599 switch (why) {
1600 case CLD_CONTINUED:
1601 info.si_status = SIGCONT;
1602 break;
1603 case CLD_STOPPED:
1604 info.si_status = tsk->signal->group_exit_code & 0x7f;
1605 break;
1606 case CLD_TRAPPED:
1607 info.si_status = tsk->exit_code & 0x7f;
1608 break;
1609 default:
1610 BUG();
1611 }
1612
1613 sighand = parent->sighand;
1614 spin_lock_irqsave(&sighand->siglock, flags);
1615 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1616 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1617 __group_send_sig_info(SIGCHLD, &info, parent);
1618 /*
1619 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1620 */
1621 __wake_up_parent(tsk, parent);
1622 spin_unlock_irqrestore(&sighand->siglock, flags);
1623}
1624
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001625static inline int may_ptrace_stop(void)
1626{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001627 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001628 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001629 /*
1630 * Are we in the middle of do_coredump?
1631 * If so and our tracer is also part of the coredump stopping
1632 * is a deadlock situation, and pointless because our tracer
1633 * is dead so don't allow us to stop.
1634 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001635 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001636 * is safe to enter schedule().
1637 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001638 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001639 unlikely(current->mm == current->parent->mm))
1640 return 0;
1641
1642 return 1;
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001646 * Return nonzero if there is a SIGKILL that should be waking us up.
1647 * Called with the siglock held.
1648 */
1649static int sigkill_pending(struct task_struct *tsk)
1650{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001651 return sigismember(&tsk->pending.signal, SIGKILL) ||
1652 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001653}
1654
1655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * This must be called with current->sighand->siglock held.
1657 *
1658 * This should be the path for all ptrace stops.
1659 * We always set current->last_siginfo while stopped here.
1660 * That makes it a way to test a stopped process for
1661 * being ptrace-stopped vs being job-control-stopped.
1662 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001663 * If we actually decide not to stop at all because the tracer
1664 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001666static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001667 __releases(&current->sighand->siglock)
1668 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Roland McGrath1a669c22008-02-06 01:37:37 -08001670 if (arch_ptrace_stop_needed(exit_code, info)) {
1671 /*
1672 * The arch code has something special to do before a
1673 * ptrace stop. This is allowed to block, e.g. for faults
1674 * on user stack pages. We can't keep the siglock while
1675 * calling arch_ptrace_stop, so we must release it now.
1676 * To preserve proper semantics, we must do this before
1677 * any signal bookkeeping like checking group_stop_count.
1678 * Meanwhile, a SIGKILL could come in before we retake the
1679 * siglock. That must prevent us from sleeping in TASK_TRACED.
1680 * So after regaining the lock, we must check for SIGKILL.
1681 */
1682 spin_unlock_irq(&current->sighand->siglock);
1683 arch_ptrace_stop(exit_code, info);
1684 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001685 if (sigkill_pending(current))
1686 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001687 }
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 /*
1690 * If there is a group stop in progress,
1691 * we must participate in the bookkeeping.
1692 */
1693 if (current->signal->group_stop_count > 0)
Tejun Heoe5c19022011-03-23 10:37:00 +01001694 task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 current->last_siginfo = info;
1697 current->exit_code = exit_code;
1698
1699 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001700 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 spin_unlock_irq(&current->sighand->siglock);
1702 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001703 if (may_ptrace_stop()) {
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001704 do_notify_parent_cldstop(current, why);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001705 /*
1706 * Don't want to allow preemption here, because
1707 * sys_ptrace() needs this task to be inactive.
1708 *
1709 * XXX: implement read_unlock_no_resched().
1710 */
1711 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001713 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 schedule();
1715 } else {
1716 /*
1717 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001718 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001720 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001721 if (clear_code)
1722 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001723 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
1726 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001727 * While in TASK_TRACED, we were considered "frozen enough".
1728 * Now that we woke up, it's crucial if we're supposed to be
1729 * frozen that we freeze now before running anything substantial.
1730 */
1731 try_to_freeze();
1732
1733 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * We are back. Now reacquire the siglock before touching
1735 * last_siginfo, so that we are sure to have synchronized with
1736 * any signal-sending on another CPU that wants to examine it.
1737 */
1738 spin_lock_irq(&current->sighand->siglock);
1739 current->last_siginfo = NULL;
1740
1741 /*
1742 * Queued signals ignored us while we were stopped for tracing.
1743 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001744 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001746 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747}
1748
1749void ptrace_notify(int exit_code)
1750{
1751 siginfo_t info;
1752
1753 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1754
1755 memset(&info, 0, sizeof info);
1756 info.si_signo = SIGTRAP;
1757 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001758 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001759 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* Let the debugger run. */
1762 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001763 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 spin_unlock_irq(&current->sighand->siglock);
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * This performs the stopping for SIGSTOP and other stop signals.
1769 * We have to stop all threads in the thread group.
1770 * Returns nonzero if we've actually stopped and released the siglock.
1771 * Returns zero if we didn't stop and still hold the siglock.
1772 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001773static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct signal_struct *sig = current->signal;
Tejun Heoedf2ed12011-03-23 10:37:00 +01001776 int notify = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001778 if (!sig->group_stop_count) {
Tejun Heoe5c19022011-03-23 10:37:00 +01001779 unsigned int gstop = GROUP_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001780 struct task_struct *t;
1781
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001782 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001783 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001787 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001789 sig->group_exit_code = signr;
1790
Tejun Heoe5c19022011-03-23 10:37:00 +01001791 current->group_stop = gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001792 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001793 for (t = next_thread(current); t != current; t = next_thread(t))
1794 /*
1795 * Setting state to TASK_STOPPED for a group
1796 * stop is always done with the siglock held,
1797 * so this check has no races.
1798 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001799 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001800 !task_is_stopped_or_traced(t)) {
Tejun Heoe5c19022011-03-23 10:37:00 +01001801 t->group_stop = gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001802 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001803 signal_wake_up(t, 0);
Tejun Heoe5c19022011-03-23 10:37:00 +01001804 } else
1805 task_clear_group_stop_pending(t);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001806 }
1807 /*
1808 * If there are no other threads in the group, or if there is
1809 * a group stop in progress and we are the last to stop, report
1810 * to the parent. When ptraced, every thread reports itself.
1811 */
Tejun Heoe5c19022011-03-23 10:37:00 +01001812 if (task_participate_group_stop(current))
Tejun Heoedf2ed12011-03-23 10:37:00 +01001813 notify = CLD_STOPPED;
Tejun Heoedf2ed12011-03-23 10:37:00 +01001814 if (task_ptrace(current))
1815 notify = CLD_STOPPED;
1816
1817 current->exit_code = sig->group_exit_code;
1818 __set_current_state(TASK_STOPPED);
1819
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001820 spin_unlock_irq(&current->sighand->siglock);
1821
1822 if (notify) {
1823 read_lock(&tasklist_lock);
1824 do_notify_parent_cldstop(current, notify);
1825 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 }
1827
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001828 /* Now we don't run again until woken by SIGCONT or SIGKILL */
Tejun Heo71db5eb2011-03-23 10:37:00 +01001829 schedule();
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001830
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001831 tracehook_finish_jctl();
1832 current->exit_code = 0;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 1;
1835}
1836
Roland McGrath18c98b62008-04-17 18:44:38 -07001837static int ptrace_signal(int signr, siginfo_t *info,
1838 struct pt_regs *regs, void *cookie)
1839{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001840 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001841 return signr;
1842
1843 ptrace_signal_deliver(regs, cookie);
1844
1845 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001846 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07001847
1848 /* We're back. Did the debugger cancel the sig? */
1849 signr = current->exit_code;
1850 if (signr == 0)
1851 return signr;
1852
1853 current->exit_code = 0;
1854
1855 /* Update the siginfo structure if the signal has
1856 changed. If the debugger wanted something
1857 specific in the siginfo structure then it should
1858 have updated *info via PTRACE_SETSIGINFO. */
1859 if (signr != info->si_signo) {
1860 info->si_signo = signr;
1861 info->si_errno = 0;
1862 info->si_code = SI_USER;
1863 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001864 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001865 }
1866
1867 /* If the (new) signal is now blocked, requeue it. */
1868 if (sigismember(&current->blocked, signr)) {
1869 specific_send_sig_info(signr, info, current);
1870 signr = 0;
1871 }
1872
1873 return signr;
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1877 struct pt_regs *regs, void *cookie)
1878{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001879 struct sighand_struct *sighand = current->sighand;
1880 struct signal_struct *signal = current->signal;
1881 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001883relock:
1884 /*
1885 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1886 * While in TASK_STOPPED, we were considered "frozen enough".
1887 * Now that we woke up, it's crucial if we're supposed to be
1888 * frozen that we freeze now before running anything substantial.
1889 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001890 try_to_freeze();
1891
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001892 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001893 /*
1894 * Every stopped thread goes here after wakeup. Check to see if
1895 * we should notify the parent, prepare_signal(SIGCONT) encodes
1896 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1897 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001898 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01001899 int why;
1900
1901 if (signal->flags & SIGNAL_CLD_CONTINUED)
1902 why = CLD_CONTINUED;
1903 else
1904 why = CLD_STOPPED;
1905
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001906 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001907
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001908 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001909
Tejun Heoedf2ed12011-03-23 10:37:00 +01001910 read_lock(&tasklist_lock);
1911 do_notify_parent_cldstop(current->group_leader, why);
1912 read_unlock(&tasklist_lock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001913 goto relock;
1914 }
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 for (;;) {
1917 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001918 /*
1919 * Tracing can induce an artifical signal and choose sigaction.
1920 * The return value in @signr determines the default action,
1921 * but @info->si_signo is the signal number we will report.
1922 */
1923 signr = tracehook_get_signal(current, regs, info, return_ka);
1924 if (unlikely(signr < 0))
1925 goto relock;
1926 if (unlikely(signr != 0))
1927 ka = return_ka;
1928 else {
Oleg Nesterov1be53962009-12-15 16:47:26 -08001929 if (unlikely(signal->group_stop_count > 0) &&
1930 do_signal_stop(0))
1931 goto relock;
1932
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001933 signr = dequeue_signal(current, &current->blocked,
1934 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Roland McGrath18c98b62008-04-17 18:44:38 -07001936 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001937 break; /* will return 0 */
1938
1939 if (signr != SIGKILL) {
1940 signr = ptrace_signal(signr, info,
1941 regs, cookie);
1942 if (!signr)
1943 continue;
1944 }
1945
1946 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05001949 /* Trace actually delivered signals. */
1950 trace_signal_deliver(signr, info, ka);
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1953 continue;
1954 if (ka->sa.sa_handler != SIG_DFL) {
1955 /* Run the handler. */
1956 *return_ka = *ka;
1957
1958 if (ka->sa.sa_flags & SA_ONESHOT)
1959 ka->sa.sa_handler = SIG_DFL;
1960
1961 break; /* will return non-zero "signr" value */
1962 }
1963
1964 /*
1965 * Now we are doing the default action for this signal.
1966 */
1967 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1968 continue;
1969
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001970 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001971 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001972 * Container-init gets no signals it doesn't want from same
1973 * container.
1974 *
1975 * Note that if global/container-init sees a sig_kernel_only()
1976 * signal here, the signal must have been generated internally
1977 * or must have come from an ancestor namespace. In either
1978 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001979 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001980 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001981 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 continue;
1983
1984 if (sig_kernel_stop(signr)) {
1985 /*
1986 * The default action is to stop all threads in
1987 * the thread group. The job control signals
1988 * do nothing in an orphaned pgrp, but SIGSTOP
1989 * always works. Note that siglock needs to be
1990 * dropped during the call to is_orphaned_pgrp()
1991 * because of lock ordering with tasklist_lock.
1992 * This allows an intervening SIGCONT to be posted.
1993 * We need to check for that and bail out if necessary.
1994 */
1995 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001996 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* signals can be posted during this window */
1999
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002000 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto relock;
2002
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002003 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
2005
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002006 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* It released the siglock. */
2008 goto relock;
2009 }
2010
2011 /*
2012 * We didn't actually stop, due to a race
2013 * with SIGCONT or something like that.
2014 */
2015 continue;
2016 }
2017
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002018 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 /*
2021 * Anything else is fatal, maybe with a core dump.
2022 */
2023 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002026 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002027 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 /*
2029 * If it was able to dump core, this kills all
2030 * other threads in the group and synchronizes with
2031 * their demise. If we lost the race with another
2032 * thread getting here, it set group_exit_code
2033 * first and our do_group_exit call below will use
2034 * that value and ignore the one we pass it.
2035 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002036 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038
2039 /*
2040 * Death signals, no core dump.
2041 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002042 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* NOTREACHED */
2044 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002045 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return signr;
2047}
2048
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002049void exit_signals(struct task_struct *tsk)
2050{
2051 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002052 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002053
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002054 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2055 tsk->flags |= PF_EXITING;
2056 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002057 }
2058
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002059 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002060 /*
2061 * From now this task is not visible for group-wide signals,
2062 * see wants_signal(), do_signal_stop().
2063 */
2064 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002065 if (!signal_pending(tsk))
2066 goto out;
2067
2068 /* It could be that __group_complete_signal() choose us to
2069 * notify about group-wide signal. Another thread should be
2070 * woken now to take the signal since we will not.
2071 */
2072 for (t = tsk; (t = next_thread(t)) != tsk; )
2073 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2074 recalc_sigpending_and_wake(t);
2075
2076 if (unlikely(tsk->signal->group_stop_count) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002077 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002078 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002079out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002080 spin_unlock_irq(&tsk->sighand->siglock);
2081
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002082 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002083 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002084 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002085 read_unlock(&tasklist_lock);
2086 }
2087}
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089EXPORT_SYMBOL(recalc_sigpending);
2090EXPORT_SYMBOL_GPL(dequeue_signal);
2091EXPORT_SYMBOL(flush_signals);
2092EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093EXPORT_SYMBOL(send_sig);
2094EXPORT_SYMBOL(send_sig_info);
2095EXPORT_SYMBOL(sigprocmask);
2096EXPORT_SYMBOL(block_all_signals);
2097EXPORT_SYMBOL(unblock_all_signals);
2098
2099
2100/*
2101 * System call entry points.
2102 */
2103
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002104SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
2106 struct restart_block *restart = &current_thread_info()->restart_block;
2107 return restart->fn(restart);
2108}
2109
2110long do_no_restart_syscall(struct restart_block *param)
2111{
2112 return -EINTR;
2113}
2114
2115/*
2116 * We don't need to get the kernel lock - this is all local to this
2117 * particular thread.. (and that's good, because this is _heavily_
2118 * used by various programs)
2119 */
2120
2121/*
2122 * This is also useful for kernel threads that want to temporarily
2123 * (or permanently) block certain signals.
2124 *
2125 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2126 * interface happily blocks "unblockable" signals like SIGKILL
2127 * and friends.
2128 */
2129int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2130{
2131 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002134 if (oldset)
2135 *oldset = current->blocked;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 error = 0;
2138 switch (how) {
2139 case SIG_BLOCK:
2140 sigorsets(&current->blocked, &current->blocked, set);
2141 break;
2142 case SIG_UNBLOCK:
2143 signandsets(&current->blocked, &current->blocked, set);
2144 break;
2145 case SIG_SETMASK:
2146 current->blocked = *set;
2147 break;
2148 default:
2149 error = -EINVAL;
2150 }
2151 recalc_sigpending();
2152 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return error;
2155}
2156
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002157SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2158 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 int error = -EINVAL;
2161 sigset_t old_set, new_set;
2162
2163 /* XXX: Don't preclude handling different sized sigset_t's. */
2164 if (sigsetsize != sizeof(sigset_t))
2165 goto out;
2166
2167 if (set) {
2168 error = -EFAULT;
2169 if (copy_from_user(&new_set, set, sizeof(*set)))
2170 goto out;
2171 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2172
2173 error = sigprocmask(how, &new_set, &old_set);
2174 if (error)
2175 goto out;
2176 if (oset)
2177 goto set_old;
2178 } else if (oset) {
2179 spin_lock_irq(&current->sighand->siglock);
2180 old_set = current->blocked;
2181 spin_unlock_irq(&current->sighand->siglock);
2182
2183 set_old:
2184 error = -EFAULT;
2185 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2186 goto out;
2187 }
2188 error = 0;
2189out:
2190 return error;
2191}
2192
2193long do_sigpending(void __user *set, unsigned long sigsetsize)
2194{
2195 long error = -EINVAL;
2196 sigset_t pending;
2197
2198 if (sigsetsize > sizeof(sigset_t))
2199 goto out;
2200
2201 spin_lock_irq(&current->sighand->siglock);
2202 sigorsets(&pending, &current->pending.signal,
2203 &current->signal->shared_pending.signal);
2204 spin_unlock_irq(&current->sighand->siglock);
2205
2206 /* Outside the lock because only this thread touches it. */
2207 sigandsets(&pending, &current->blocked, &pending);
2208
2209 error = -EFAULT;
2210 if (!copy_to_user(set, &pending, sigsetsize))
2211 error = 0;
2212
2213out:
2214 return error;
2215}
2216
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002217SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218{
2219 return do_sigpending(set, sigsetsize);
2220}
2221
2222#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2223
2224int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2225{
2226 int err;
2227
2228 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2229 return -EFAULT;
2230 if (from->si_code < 0)
2231 return __copy_to_user(to, from, sizeof(siginfo_t))
2232 ? -EFAULT : 0;
2233 /*
2234 * If you change siginfo_t structure, please be sure
2235 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002236 * Please remember to update the signalfd_copyinfo() function
2237 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 * It should never copy any pad contained in the structure
2239 * to avoid security leaks, but must copy the generic
2240 * 3 ints plus the relevant union member.
2241 */
2242 err = __put_user(from->si_signo, &to->si_signo);
2243 err |= __put_user(from->si_errno, &to->si_errno);
2244 err |= __put_user((short)from->si_code, &to->si_code);
2245 switch (from->si_code & __SI_MASK) {
2246 case __SI_KILL:
2247 err |= __put_user(from->si_pid, &to->si_pid);
2248 err |= __put_user(from->si_uid, &to->si_uid);
2249 break;
2250 case __SI_TIMER:
2251 err |= __put_user(from->si_tid, &to->si_tid);
2252 err |= __put_user(from->si_overrun, &to->si_overrun);
2253 err |= __put_user(from->si_ptr, &to->si_ptr);
2254 break;
2255 case __SI_POLL:
2256 err |= __put_user(from->si_band, &to->si_band);
2257 err |= __put_user(from->si_fd, &to->si_fd);
2258 break;
2259 case __SI_FAULT:
2260 err |= __put_user(from->si_addr, &to->si_addr);
2261#ifdef __ARCH_SI_TRAPNO
2262 err |= __put_user(from->si_trapno, &to->si_trapno);
2263#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002264#ifdef BUS_MCEERR_AO
2265 /*
2266 * Other callers might not initialize the si_lsb field,
2267 * so check explicitely for the right codes here.
2268 */
2269 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2270 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2271#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
2273 case __SI_CHLD:
2274 err |= __put_user(from->si_pid, &to->si_pid);
2275 err |= __put_user(from->si_uid, &to->si_uid);
2276 err |= __put_user(from->si_status, &to->si_status);
2277 err |= __put_user(from->si_utime, &to->si_utime);
2278 err |= __put_user(from->si_stime, &to->si_stime);
2279 break;
2280 case __SI_RT: /* This is not generated by the kernel as of now. */
2281 case __SI_MESGQ: /* But this is */
2282 err |= __put_user(from->si_pid, &to->si_pid);
2283 err |= __put_user(from->si_uid, &to->si_uid);
2284 err |= __put_user(from->si_ptr, &to->si_ptr);
2285 break;
2286 default: /* this is just in case for now ... */
2287 err |= __put_user(from->si_pid, &to->si_pid);
2288 err |= __put_user(from->si_uid, &to->si_uid);
2289 break;
2290 }
2291 return err;
2292}
2293
2294#endif
2295
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002296SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2297 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2298 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
2300 int ret, sig;
2301 sigset_t these;
2302 struct timespec ts;
2303 siginfo_t info;
2304 long timeout = 0;
2305
2306 /* XXX: Don't preclude handling different sized sigset_t's. */
2307 if (sigsetsize != sizeof(sigset_t))
2308 return -EINVAL;
2309
2310 if (copy_from_user(&these, uthese, sizeof(these)))
2311 return -EFAULT;
2312
2313 /*
2314 * Invert the set of allowed signals to get those we
2315 * want to block.
2316 */
2317 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2318 signotset(&these);
2319
2320 if (uts) {
2321 if (copy_from_user(&ts, uts, sizeof(ts)))
2322 return -EFAULT;
2323 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2324 || ts.tv_sec < 0)
2325 return -EINVAL;
2326 }
2327
2328 spin_lock_irq(&current->sighand->siglock);
2329 sig = dequeue_signal(current, &these, &info);
2330 if (!sig) {
2331 timeout = MAX_SCHEDULE_TIMEOUT;
2332 if (uts)
2333 timeout = (timespec_to_jiffies(&ts)
2334 + (ts.tv_sec || ts.tv_nsec));
2335
2336 if (timeout) {
2337 /* None ready -- temporarily unblock those we're
2338 * interested while we are sleeping in so that we'll
2339 * be awakened when they arrive. */
2340 current->real_blocked = current->blocked;
2341 sigandsets(&current->blocked, &current->blocked, &these);
2342 recalc_sigpending();
2343 spin_unlock_irq(&current->sighand->siglock);
2344
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002345 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 spin_lock_irq(&current->sighand->siglock);
2348 sig = dequeue_signal(current, &these, &info);
2349 current->blocked = current->real_blocked;
2350 siginitset(&current->real_blocked, 0);
2351 recalc_sigpending();
2352 }
2353 }
2354 spin_unlock_irq(&current->sighand->siglock);
2355
2356 if (sig) {
2357 ret = sig;
2358 if (uinfo) {
2359 if (copy_siginfo_to_user(uinfo, &info))
2360 ret = -EFAULT;
2361 }
2362 } else {
2363 ret = -EAGAIN;
2364 if (timeout)
2365 ret = -EINTR;
2366 }
2367
2368 return ret;
2369}
2370
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002371SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 struct siginfo info;
2374
2375 info.si_signo = sig;
2376 info.si_errno = 0;
2377 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002378 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002379 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
2381 return kill_something_info(sig, &info, pid);
2382}
2383
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002384static int
2385do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002386{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002387 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002388 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002389
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002390 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002391 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002392 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002393 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002394 /*
2395 * The null signal is a permissions and process existence
2396 * probe. No signal is actually delivered.
2397 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002398 if (!error && sig) {
2399 error = do_send_sig_info(sig, info, p, false);
2400 /*
2401 * If lock_task_sighand() failed we pretend the task
2402 * dies after receiving the signal. The window is tiny,
2403 * and the signal is private anyway.
2404 */
2405 if (unlikely(error == -ESRCH))
2406 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002407 }
2408 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002409 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002410
2411 return error;
2412}
2413
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002414static int do_tkill(pid_t tgid, pid_t pid, int sig)
2415{
2416 struct siginfo info;
2417
2418 info.si_signo = sig;
2419 info.si_errno = 0;
2420 info.si_code = SI_TKILL;
2421 info.si_pid = task_tgid_vnr(current);
2422 info.si_uid = current_uid();
2423
2424 return do_send_specific(tgid, pid, sig, &info);
2425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427/**
2428 * sys_tgkill - send signal to one specific thread
2429 * @tgid: the thread group ID of the thread
2430 * @pid: the PID of the thread
2431 * @sig: signal to be sent
2432 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002433 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * exists but it's not belonging to the target process anymore. This
2435 * method solves the problem of threads exiting and PIDs getting reused.
2436 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002437SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /* This is only valid for single tasks */
2440 if (pid <= 0 || tgid <= 0)
2441 return -EINVAL;
2442
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002443 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446/*
2447 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2448 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002449SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /* This is only valid for single tasks */
2452 if (pid <= 0)
2453 return -EINVAL;
2454
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002455 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002458SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2459 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 siginfo_t info;
2462
2463 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2464 return -EFAULT;
2465
2466 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002467 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2468 */
2469 if (info.si_code != SI_QUEUE) {
2470 /* We used to allow any < 0 si_code */
2471 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 info.si_signo = sig;
2475
2476 /* POSIX.1b doesn't mention process groups. */
2477 return kill_proc_info(sig, &info, pid);
2478}
2479
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002480long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2481{
2482 /* This is only valid for single tasks */
2483 if (pid <= 0 || tgid <= 0)
2484 return -EINVAL;
2485
2486 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002487 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2488 */
2489 if (info->si_code != SI_QUEUE) {
2490 /* We used to allow any < 0 si_code */
2491 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002492 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002493 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002494 info->si_signo = sig;
2495
2496 return do_send_specific(tgid, pid, sig, info);
2497}
2498
2499SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2500 siginfo_t __user *, uinfo)
2501{
2502 siginfo_t info;
2503
2504 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2505 return -EFAULT;
2506
2507 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2508}
2509
Oleg Nesterov88531f72006-03-28 16:11:24 -08002510int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002512 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002514 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002516 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return -EINVAL;
2518
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002519 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (oact)
2523 *oact = *k;
2524
2525 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002526 sigdelsetmask(&act->sa.sa_mask,
2527 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002528 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * POSIX 3.3.1.3:
2531 * "Setting a signal action to SIG_IGN for a signal that is
2532 * pending shall cause the pending signal to be discarded,
2533 * whether or not it is blocked."
2534 *
2535 * "Setting a signal action to SIG_DFL for a signal that is
2536 * pending and whose default action is to ignore the signal
2537 * (for example, SIGCHLD), shall cause the pending signal to
2538 * be discarded, whether or not it is blocked"
2539 */
Roland McGrath35de2542008-07-25 19:45:51 -07002540 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002541 sigemptyset(&mask);
2542 sigaddset(&mask, sig);
2543 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002545 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 t = next_thread(t);
2547 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550
2551 spin_unlock_irq(&current->sighand->siglock);
2552 return 0;
2553}
2554
2555int
2556do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2557{
2558 stack_t oss;
2559 int error;
2560
Linus Torvalds0083fc22009-08-01 10:34:56 -07002561 oss.ss_sp = (void __user *) current->sas_ss_sp;
2562 oss.ss_size = current->sas_ss_size;
2563 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
2565 if (uss) {
2566 void __user *ss_sp;
2567 size_t ss_size;
2568 int ss_flags;
2569
2570 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002571 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2572 goto out;
2573 error = __get_user(ss_sp, &uss->ss_sp) |
2574 __get_user(ss_flags, &uss->ss_flags) |
2575 __get_user(ss_size, &uss->ss_size);
2576 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 goto out;
2578
2579 error = -EPERM;
2580 if (on_sig_stack(sp))
2581 goto out;
2582
2583 error = -EINVAL;
2584 /*
2585 *
2586 * Note - this code used to test ss_flags incorrectly
2587 * old code may have been written using ss_flags==0
2588 * to mean ss_flags==SS_ONSTACK (as this was the only
2589 * way that worked) - this fix preserves that older
2590 * mechanism
2591 */
2592 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2593 goto out;
2594
2595 if (ss_flags == SS_DISABLE) {
2596 ss_size = 0;
2597 ss_sp = NULL;
2598 } else {
2599 error = -ENOMEM;
2600 if (ss_size < MINSIGSTKSZ)
2601 goto out;
2602 }
2603
2604 current->sas_ss_sp = (unsigned long) ss_sp;
2605 current->sas_ss_size = ss_size;
2606 }
2607
Linus Torvalds0083fc22009-08-01 10:34:56 -07002608 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (uoss) {
2610 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002611 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002613 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2614 __put_user(oss.ss_size, &uoss->ss_size) |
2615 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618out:
2619 return error;
2620}
2621
2622#ifdef __ARCH_WANT_SYS_SIGPENDING
2623
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002624SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
2626 return do_sigpending(set, sizeof(*set));
2627}
2628
2629#endif
2630
2631#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2632/* Some platforms have their own version with special arguments others
2633 support only sys_rt_sigprocmask. */
2634
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002635SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2636 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
2638 int error;
2639 old_sigset_t old_set, new_set;
2640
2641 if (set) {
2642 error = -EFAULT;
2643 if (copy_from_user(&new_set, set, sizeof(*set)))
2644 goto out;
2645 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2646
2647 spin_lock_irq(&current->sighand->siglock);
2648 old_set = current->blocked.sig[0];
2649
2650 error = 0;
2651 switch (how) {
2652 default:
2653 error = -EINVAL;
2654 break;
2655 case SIG_BLOCK:
2656 sigaddsetmask(&current->blocked, new_set);
2657 break;
2658 case SIG_UNBLOCK:
2659 sigdelsetmask(&current->blocked, new_set);
2660 break;
2661 case SIG_SETMASK:
2662 current->blocked.sig[0] = new_set;
2663 break;
2664 }
2665
2666 recalc_sigpending();
2667 spin_unlock_irq(&current->sighand->siglock);
2668 if (error)
2669 goto out;
2670 if (oset)
2671 goto set_old;
2672 } else if (oset) {
2673 old_set = current->blocked.sig[0];
2674 set_old:
2675 error = -EFAULT;
2676 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2677 goto out;
2678 }
2679 error = 0;
2680out:
2681 return error;
2682}
2683#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2684
2685#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002686SYSCALL_DEFINE4(rt_sigaction, int, sig,
2687 const struct sigaction __user *, act,
2688 struct sigaction __user *, oact,
2689 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct k_sigaction new_sa, old_sa;
2692 int ret = -EINVAL;
2693
2694 /* XXX: Don't preclude handling different sized sigset_t's. */
2695 if (sigsetsize != sizeof(sigset_t))
2696 goto out;
2697
2698 if (act) {
2699 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2700 return -EFAULT;
2701 }
2702
2703 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2704
2705 if (!ret && oact) {
2706 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2707 return -EFAULT;
2708 }
2709out:
2710 return ret;
2711}
2712#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2713
2714#ifdef __ARCH_WANT_SYS_SGETMASK
2715
2716/*
2717 * For backwards compatibility. Functionality superseded by sigprocmask.
2718 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002719SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 /* SMP safe */
2722 return current->blocked.sig[0];
2723}
2724
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002725SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
2727 int old;
2728
2729 spin_lock_irq(&current->sighand->siglock);
2730 old = current->blocked.sig[0];
2731
2732 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2733 sigmask(SIGSTOP)));
2734 recalc_sigpending();
2735 spin_unlock_irq(&current->sighand->siglock);
2736
2737 return old;
2738}
2739#endif /* __ARCH_WANT_SGETMASK */
2740
2741#ifdef __ARCH_WANT_SYS_SIGNAL
2742/*
2743 * For backwards compatibility. Functionality superseded by sigaction.
2744 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002745SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 struct k_sigaction new_sa, old_sa;
2748 int ret;
2749
2750 new_sa.sa.sa_handler = handler;
2751 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002752 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 ret = do_sigaction(sig, &new_sa, &old_sa);
2755
2756 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2757}
2758#endif /* __ARCH_WANT_SYS_SIGNAL */
2759
2760#ifdef __ARCH_WANT_SYS_PAUSE
2761
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002762SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
2764 current->state = TASK_INTERRUPTIBLE;
2765 schedule();
2766 return -ERESTARTNOHAND;
2767}
2768
2769#endif
2770
David Woodhouse150256d2006-01-18 17:43:57 -08002771#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002772SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002773{
2774 sigset_t newset;
2775
2776 /* XXX: Don't preclude handling different sized sigset_t's. */
2777 if (sigsetsize != sizeof(sigset_t))
2778 return -EINVAL;
2779
2780 if (copy_from_user(&newset, unewset, sizeof(newset)))
2781 return -EFAULT;
2782 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2783
2784 spin_lock_irq(&current->sighand->siglock);
2785 current->saved_sigmask = current->blocked;
2786 current->blocked = newset;
2787 recalc_sigpending();
2788 spin_unlock_irq(&current->sighand->siglock);
2789
2790 current->state = TASK_INTERRUPTIBLE;
2791 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002792 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002793 return -ERESTARTNOHAND;
2794}
2795#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2796
David Howellsf269fdd2006-09-27 01:50:23 -07002797__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2798{
2799 return NULL;
2800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802void __init signals_init(void)
2803{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002804 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
Jason Wessel67fc4e02010-05-20 21:04:21 -05002806
2807#ifdef CONFIG_KGDB_KDB
2808#include <linux/kdb.h>
2809/*
2810 * kdb_send_sig_info - Allows kdb to send signals without exposing
2811 * signal internals. This function checks if the required locks are
2812 * available before calling the main signal code, to avoid kdb
2813 * deadlocks.
2814 */
2815void
2816kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
2817{
2818 static struct task_struct *kdb_prev_t;
2819 int sig, new_t;
2820 if (!spin_trylock(&t->sighand->siglock)) {
2821 kdb_printf("Can't do kill command now.\n"
2822 "The sigmask lock is held somewhere else in "
2823 "kernel, try again later\n");
2824 return;
2825 }
2826 spin_unlock(&t->sighand->siglock);
2827 new_t = kdb_prev_t != t;
2828 kdb_prev_t = t;
2829 if (t->state != TASK_RUNNING && new_t) {
2830 kdb_printf("Process is not RUNNING, sending a signal from "
2831 "kdb risks deadlock\n"
2832 "on the run queue locks. "
2833 "The signal has _not_ been sent.\n"
2834 "Reissue the kill command if you want to risk "
2835 "the deadlock.\n");
2836 return;
2837 }
2838 sig = info->si_signo;
2839 if (send_sig_info(sig, info, t))
2840 kdb_printf("Fail to deliver Signal %d to process %d.\n",
2841 sig, t->pid);
2842 else
2843 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
2844}
2845#endif /* CONFIG_KGDB_KDB */