blob: ab6851c06461fbd79154639da76ece41273a760d [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{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200127 if ((t->jobctl & JOBCTL_STOP_PENDING) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 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/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200227 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100228 * @task: target task
229 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200230 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
231 * Clear it and wake up the ptracer. Note that we don't need any further
232 * locking. @task->siglock guarantees that @task->parent points to the
233 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100234 *
235 * CONTEXT:
236 * Must be called with @task->sighand->siglock held.
237 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200238static void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100239{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200240 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
241 task->jobctl &= ~JOBCTL_TRAPPING;
Tejun Heo40ae7172011-05-06 11:52:22 +0200242 __wake_up_sync_key(&task->parent->signal->wait_chldexit,
243 TASK_UNINTERRUPTIBLE, 1, task);
Tejun Heod79fdd62011-03-23 10:37:00 +0100244 }
245}
246
247/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200248 * task_clear_jobctl_stop_pending - clear pending group stop
Tejun Heoe5c19022011-03-23 10:37:00 +0100249 * @task: target task
250 *
251 * Clear group stop states for @task.
252 *
253 * CONTEXT:
254 * Must be called with @task->sighand->siglock held.
255 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200256void task_clear_jobctl_stop_pending(struct task_struct *task)
Tejun Heoe5c19022011-03-23 10:37:00 +0100257{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200258 task->jobctl &= ~(JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME |
259 JOBCTL_STOP_DEQUEUED);
Tejun Heoe5c19022011-03-23 10:37:00 +0100260}
261
262/**
263 * task_participate_group_stop - participate in a group stop
264 * @task: task participating in a group stop
265 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200266 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100267 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200268 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100269 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100270 *
271 * CONTEXT:
272 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100273 *
274 * RETURNS:
275 * %true if group stop completion should be notified to the parent, %false
276 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100277 */
278static bool task_participate_group_stop(struct task_struct *task)
279{
280 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200281 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100282
Tejun Heoa8f072c2011-06-02 11:13:59 +0200283 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100284
Tejun Heoa8f072c2011-06-02 11:13:59 +0200285 task_clear_jobctl_stop_pending(task);
Tejun Heoe5c19022011-03-23 10:37:00 +0100286
287 if (!consume)
288 return false;
289
290 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
291 sig->group_stop_count--;
292
Tejun Heo244056f2011-03-23 10:37:01 +0100293 /*
294 * Tell the caller to notify completion iff we are entering into a
295 * fresh group stop. Read comment in do_signal_stop() for details.
296 */
297 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100298 sig->flags = SIGNAL_STOP_STOPPED;
299 return true;
300 }
301 return false;
302}
303
David Howellsc69e8d92008-11-14 10:39:19 +1100304/*
305 * allocate a new signal queue record
306 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700307 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100308 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900309static struct sigqueue *
310__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
312 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800313 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800315 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000316 * Protect access to @t credentials. This can go away when all
317 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800318 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000319 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100320 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800321 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000322 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800325 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800326 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900328 } else {
329 print_dropped_signal(sig);
330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800333 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100334 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 } else {
336 INIT_LIST_HEAD(&q->list);
337 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100338 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
David Howellsd84f4f92008-11-14 10:39:23 +1100340
341 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Andrew Morton514a01b2006-02-03 03:04:41 -0800344static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 if (q->flags & SIGQUEUE_PREALLOC)
347 return;
348 atomic_dec(&q->user->sigpending);
349 free_uid(q->user);
350 kmem_cache_free(sigqueue_cachep, q);
351}
352
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800353void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sigqueue *q;
356
357 sigemptyset(&queue->signal);
358 while (!list_empty(&queue->list)) {
359 q = list_entry(queue->list.next, struct sigqueue , list);
360 list_del_init(&q->list);
361 __sigqueue_free(q);
362 }
363}
364
365/*
366 * Flush all pending signals for a task.
367 */
David Howells3bcac022009-04-29 13:45:05 +0100368void __flush_signals(struct task_struct *t)
369{
370 clear_tsk_thread_flag(t, TIF_SIGPENDING);
371 flush_sigqueue(&t->pending);
372 flush_sigqueue(&t->signal->shared_pending);
373}
374
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800375void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 unsigned long flags;
378
379 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100380 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_unlock_irqrestore(&t->sighand->siglock, flags);
382}
383
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400384static void __flush_itimer_signals(struct sigpending *pending)
385{
386 sigset_t signal, retain;
387 struct sigqueue *q, *n;
388
389 signal = pending->signal;
390 sigemptyset(&retain);
391
392 list_for_each_entry_safe(q, n, &pending->list, list) {
393 int sig = q->info.si_signo;
394
395 if (likely(q->info.si_code != SI_TIMER)) {
396 sigaddset(&retain, sig);
397 } else {
398 sigdelset(&signal, sig);
399 list_del_init(&q->list);
400 __sigqueue_free(q);
401 }
402 }
403
404 sigorsets(&pending->signal, &signal, &retain);
405}
406
407void flush_itimer_signals(void)
408{
409 struct task_struct *tsk = current;
410 unsigned long flags;
411
412 spin_lock_irqsave(&tsk->sighand->siglock, flags);
413 __flush_itimer_signals(&tsk->pending);
414 __flush_itimer_signals(&tsk->signal->shared_pending);
415 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
416}
417
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700418void ignore_signals(struct task_struct *t)
419{
420 int i;
421
422 for (i = 0; i < _NSIG; ++i)
423 t->sighand->action[i].sa.sa_handler = SIG_IGN;
424
425 flush_signals(t);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 * Flush all handlers for a task.
430 */
431
432void
433flush_signal_handlers(struct task_struct *t, int force_default)
434{
435 int i;
436 struct k_sigaction *ka = &t->sighand->action[0];
437 for (i = _NSIG ; i != 0 ; i--) {
438 if (force_default || ka->sa.sa_handler != SIG_IGN)
439 ka->sa.sa_handler = SIG_DFL;
440 ka->sa.sa_flags = 0;
441 sigemptyset(&ka->sa.sa_mask);
442 ka++;
443 }
444}
445
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200446int unhandled_signal(struct task_struct *tsk, int sig)
447{
Roland McGrath445a91d2008-07-25 19:45:52 -0700448 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700449 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200450 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700451 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200452 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700453 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200454}
455
Randy Dunlap5aba0852011-04-04 14:59:31 -0700456/*
457 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * process, and wants to be notified if any signals at all were to be
459 * sent/acted upon. If the notifier routine returns non-zero, then the
460 * signal will be acted upon after all. If the notifier routine returns 0,
461 * then then signal will be blocked. Only one block per process is
462 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700463 * can use to determine if the signal should be blocked or not.
464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465void
466block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
467{
468 unsigned long flags;
469
470 spin_lock_irqsave(&current->sighand->siglock, flags);
471 current->notifier_mask = mask;
472 current->notifier_data = priv;
473 current->notifier = notifier;
474 spin_unlock_irqrestore(&current->sighand->siglock, flags);
475}
476
477/* Notify the system that blocking has ended. */
478
479void
480unblock_all_signals(void)
481{
482 unsigned long flags;
483
484 spin_lock_irqsave(&current->sighand->siglock, flags);
485 current->notifier = NULL;
486 current->notifier_data = NULL;
487 recalc_sigpending();
488 spin_unlock_irqrestore(&current->sighand->siglock, flags);
489}
490
Oleg Nesterov100360f2008-07-25 01:47:29 -0700491static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
496 * Collect the siginfo appropriate to this signal. Check if
497 * there is another siginfo for the same signal.
498 */
499 list_for_each_entry(q, &list->list, list) {
500 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700501 if (first)
502 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 first = q;
504 }
505 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700506
507 sigdelset(&list->signal, sig);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700510still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 list_del_init(&first->list);
512 copy_siginfo(info, &first->info);
513 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700515 /*
516 * Ok, it wasn't in the queue. This must be
517 * a fast-pathed signal or we must have been
518 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 info->si_signo = sig;
521 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800522 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 info->si_pid = 0;
524 info->si_uid = 0;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
528static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
529 siginfo_t *info)
530{
Roland McGrath27d91e02006-09-29 02:00:31 -0700531 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (sig) {
534 if (current->notifier) {
535 if (sigismember(current->notifier_mask, sig)) {
536 if (!(current->notifier)(current->notifier_data)) {
537 clear_thread_flag(TIF_SIGPENDING);
538 return 0;
539 }
540 }
541 }
542
Oleg Nesterov100360f2008-07-25 01:47:29 -0700543 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return sig;
547}
548
549/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700550 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 * expected to free it.
552 *
553 * All callers have to hold the siglock.
554 */
555int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
556{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700557 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000558
559 /* We only dequeue private signals from ourselves, we don't let
560 * signalfd steal them
561 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700562 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800563 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 signr = __dequeue_signal(&tsk->signal->shared_pending,
565 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800566 /*
567 * itimer signal ?
568 *
569 * itimers are process shared and we restart periodic
570 * itimers in the signal delivery path to prevent DoS
571 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700572 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800573 * itimers, as the SIGALRM is a legacy signal and only
574 * queued once. Changing the restart behaviour to
575 * restart the timer in the signal dequeue path is
576 * reducing the timer noise on heavy loaded !highres
577 * systems too.
578 */
579 if (unlikely(signr == SIGALRM)) {
580 struct hrtimer *tmr = &tsk->signal->real_timer;
581
582 if (!hrtimer_is_queued(tmr) &&
583 tsk->signal->it_real_incr.tv64 != 0) {
584 hrtimer_forward(tmr, tmr->base->get_time(),
585 tsk->signal->it_real_incr);
586 hrtimer_restart(tmr);
587 }
588 }
589 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700590
Davide Libenzib8fceee2007-09-20 12:40:16 -0700591 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700592 if (!signr)
593 return 0;
594
595 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800596 /*
597 * Set a marker that we have dequeued a stop signal. Our
598 * caller might release the siglock and then the pending
599 * stop signal it is about to process is no longer in the
600 * pending bitmasks, but must still be cleared by a SIGCONT
601 * (and overruled by a SIGKILL). So those cases clear this
602 * shared flag after we've set it. Note that this flag may
603 * remain set after the signal we return is ignored or
604 * handled. That doesn't matter because its only purpose
605 * is to alert stop-signal processing code when another
606 * processor has come along and cleared the flag.
607 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200608 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800609 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700610 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Release the siglock to ensure proper locking order
613 * of timer locks outside of siglocks. Note, we leave
614 * irqs disabled here, since the posix-timers code is
615 * about to disable them again anyway.
616 */
617 spin_unlock(&tsk->sighand->siglock);
618 do_schedule_next_timer(info);
619 spin_lock(&tsk->sighand->siglock);
620 }
621 return signr;
622}
623
624/*
625 * Tell a process that it has a new active signal..
626 *
627 * NOTE! we rely on the previous spin_lock to
628 * lock interrupts for us! We can only be called with
629 * "siglock" held, and the local interrupt must
630 * have been disabled when that got acquired!
631 *
632 * No need to set need_resched since signal event passing
633 * goes through ->blocked
634 */
635void signal_wake_up(struct task_struct *t, int resume)
636{
637 unsigned int mask;
638
639 set_tsk_thread_flag(t, TIF_SIGPENDING);
640
641 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500642 * For SIGKILL, we want to wake it up in the stopped/traced/killable
643 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * executing another processor and just now entering stopped state.
645 * By using wake_up_state, we ensure the process will wake up and
646 * handle its death signal.
647 */
648 mask = TASK_INTERRUPTIBLE;
649 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500650 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!wake_up_state(t, mask))
652 kick_process(t);
653}
654
655/*
656 * Remove signals in mask from the pending set and queue.
657 * Returns 1 if any signals were found.
658 *
659 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800660 *
661 * This version takes a sigset mask and looks at all signals,
662 * not just those in the first mask word.
663 */
664static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
665{
666 struct sigqueue *q, *n;
667 sigset_t m;
668
669 sigandsets(&m, mask, &s->signal);
670 if (sigisemptyset(&m))
671 return 0;
672
Oleg Nesterov702a5072011-04-27 22:01:27 +0200673 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800674 list_for_each_entry_safe(q, n, &s->list, list) {
675 if (sigismember(mask, q->info.si_signo)) {
676 list_del_init(&q->list);
677 __sigqueue_free(q);
678 }
679 }
680 return 1;
681}
682/*
683 * Remove signals in mask from the pending set and queue.
684 * Returns 1 if any signals were found.
685 *
686 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
688static int rm_from_queue(unsigned long mask, struct sigpending *s)
689{
690 struct sigqueue *q, *n;
691
692 if (!sigtestsetmask(&s->signal, mask))
693 return 0;
694
695 sigdelsetmask(&s->signal, mask);
696 list_for_each_entry_safe(q, n, &s->list, list) {
697 if (q->info.si_signo < SIGRTMIN &&
698 (mask & sigmask(q->info.si_signo))) {
699 list_del_init(&q->list);
700 __sigqueue_free(q);
701 }
702 }
703 return 1;
704}
705
Oleg Nesterov614c5172009-12-15 16:47:22 -0800706static inline int is_si_special(const struct siginfo *info)
707{
708 return info <= SEND_SIG_FORCED;
709}
710
711static inline bool si_fromuser(const struct siginfo *info)
712{
713 return info == SEND_SIG_NOINFO ||
714 (!is_si_special(info) && SI_FROMUSER(info));
715}
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700718 * called with RCU read lock from check_kill_permission()
719 */
720static int kill_ok_by_cred(struct task_struct *t)
721{
722 const struct cred *cred = current_cred();
723 const struct cred *tcred = __task_cred(t);
724
725 if (cred->user->user_ns == tcred->user->user_ns &&
726 (cred->euid == tcred->suid ||
727 cred->euid == tcred->uid ||
728 cred->uid == tcred->suid ||
729 cred->uid == tcred->uid))
730 return 1;
731
732 if (ns_capable(tcred->user->user_ns, CAP_KILL))
733 return 1;
734
735 return 0;
736}
737
738/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100740 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
742static int check_kill_permission(int sig, struct siginfo *info,
743 struct task_struct *t)
744{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700745 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700746 int error;
747
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700748 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700749 return -EINVAL;
750
Oleg Nesterov614c5172009-12-15 16:47:22 -0800751 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700752 return 0;
753
754 error = audit_signal_info(sig, t); /* Let audit system see the signal */
755 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400757
Oleg Nesterov065add32010-05-26 14:42:54 -0700758 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700759 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700760 switch (sig) {
761 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700762 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700763 /*
764 * We don't return the error if sid == NULL. The
765 * task was unhashed, the caller must notice this.
766 */
767 if (!sid || sid == task_session(current))
768 break;
769 default:
770 return -EPERM;
771 }
772 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100773
Amy Griffise54dc242007-03-29 18:01:04 -0400774 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700778 * Handle magic process-wide effects of stop/continue signals. Unlike
779 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * time regardless of blocking, ignoring, or handling. This does the
781 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700782 * signals. The process stop is done as a signal action for SIG_DFL.
783 *
784 * Returns true if the signal should be actually delivered, otherwise
785 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700787static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Oleg Nesterovad16a462008-04-30 00:52:46 -0700789 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 struct task_struct *t;
791
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700792 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700794 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700796 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * This is a stop signal. Remove SIGCONT from all queues.
799 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700800 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 t = p;
802 do {
803 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700804 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700806 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200808 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700810 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 t = p;
812 do {
Tejun Heoa8f072c2011-06-02 11:13:59 +0200813 task_clear_jobctl_stop_pending(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Oleg Nesterov1deac632011-04-01 20:11:50 +0200815 wake_up_state(t, __TASK_STOPPED);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700816 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700818 /*
819 * Notify the parent with CLD_CONTINUED if we were stopped.
820 *
821 * If we were in the middle of a group stop, we pretend it
822 * was already finished, and then continued. Since SIGCHLD
823 * doesn't queue we report only CLD_STOPPED, as if the next
824 * CLD_CONTINUED was dropped.
825 */
826 why = 0;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700827 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700828 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700829 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700830 why |= SIGNAL_CLD_STOPPED;
831
832 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700833 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700834 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700835 * will take ->siglock, notice SIGNAL_CLD_MASK, and
836 * notify its parent. See get_signal_to_deliver().
837 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700838 signal->flags = why | SIGNAL_STOP_CONTINUED;
839 signal->group_stop_count = 0;
840 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700843
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700844 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700847/*
848 * Test if P wants to take SIG. After we've checked all threads with this,
849 * it's equivalent to finding no threads not blocking SIG. Any threads not
850 * blocking SIG were ruled out because they are not running and already
851 * have pending signals. Such threads will dequeue from the shared queue
852 * as soon as they're available, so putting the signal on the shared queue
853 * will be equivalent to sending it to one such thread.
854 */
855static inline int wants_signal(int sig, struct task_struct *p)
856{
857 if (sigismember(&p->blocked, sig))
858 return 0;
859 if (p->flags & PF_EXITING)
860 return 0;
861 if (sig == SIGKILL)
862 return 1;
863 if (task_is_stopped_or_traced(p))
864 return 0;
865 return task_curr(p) || !signal_pending(p);
866}
867
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700868static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700869{
870 struct signal_struct *signal = p->signal;
871 struct task_struct *t;
872
873 /*
874 * Now find a thread we can wake up to take the signal off the queue.
875 *
876 * If the main thread wants the signal, it gets first crack.
877 * Probably the least surprising to the average bear.
878 */
879 if (wants_signal(sig, p))
880 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700881 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700882 /*
883 * There is just one thread and it does not need to be woken.
884 * It will dequeue unblocked signals before it runs again.
885 */
886 return;
887 else {
888 /*
889 * Otherwise try to find a suitable thread.
890 */
891 t = signal->curr_target;
892 while (!wants_signal(sig, t)) {
893 t = next_thread(t);
894 if (t == signal->curr_target)
895 /*
896 * No thread needs to be woken.
897 * Any eligible threads will see
898 * the signal in the queue soon.
899 */
900 return;
901 }
902 signal->curr_target = t;
903 }
904
905 /*
906 * Found a killable thread. If the signal will be fatal,
907 * then start taking the whole group down immediately.
908 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700909 if (sig_fatal(p, sig) &&
910 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700911 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700912 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700913 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700914 /*
915 * This signal will be fatal to the whole group.
916 */
917 if (!sig_kernel_coredump(sig)) {
918 /*
919 * Start a group exit and wake everybody up.
920 * This way we don't have other threads
921 * running and doing things after a slower
922 * thread has the fatal signal pending.
923 */
924 signal->flags = SIGNAL_GROUP_EXIT;
925 signal->group_exit_code = sig;
926 signal->group_stop_count = 0;
927 t = p;
928 do {
Tejun Heoa8f072c2011-06-02 11:13:59 +0200929 task_clear_jobctl_stop_pending(t);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700930 sigaddset(&t->pending.signal, SIGKILL);
931 signal_wake_up(t, 1);
932 } while_each_thread(p, t);
933 return;
934 }
935 }
936
937 /*
938 * The signal is already in the shared-pending queue.
939 * Tell the chosen thread to wake up and dequeue it.
940 */
941 signal_wake_up(t, sig == SIGKILL);
942 return;
943}
944
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700945static inline int legacy_queue(struct sigpending *signals, int sig)
946{
947 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
948}
949
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700950static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
951 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700953 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700954 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200955 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500957 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400958
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700959 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700960
961 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700962 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700963
964 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700966 * Short-circuit ignored signals and support queuing
967 * exactly one non-rt signal, so that we can get more
968 * detailed information about the cause of the signal.
969 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700970 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700971 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700972 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 * fast-pathed signals for kernel-internal things like SIGSTOP
974 * or SIGKILL.
975 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800976 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 goto out_set;
978
Randy Dunlap5aba0852011-04-04 14:59:31 -0700979 /*
980 * Real-time signals must be queued if sent by sigqueue, or
981 * some other real-time mechanism. It is implementation
982 * defined whether kill() does so. We attempt to do so, on
983 * the principle of least surprise, but since kill is not
984 * allowed to fail with EAGAIN when low on memory we just
985 * make sure at least one signal gets delivered and don't
986 * pass on the info struct.
987 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200988 if (sig < SIGRTMIN)
989 override_rlimit = (is_si_special(info) || info->si_code >= 0);
990 else
991 override_rlimit = 0;
992
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900993 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200994 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700996 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800998 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 q->info.si_signo = sig;
1000 q->info.si_errno = 0;
1001 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001002 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001003 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001004 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001006 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 q->info.si_signo = sig;
1008 q->info.si_errno = 0;
1009 q->info.si_code = SI_KERNEL;
1010 q->info.si_pid = 0;
1011 q->info.si_uid = 0;
1012 break;
1013 default:
1014 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001015 if (from_ancestor_ns)
1016 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 break;
1018 }
Oleg Nesterov621d3122005-10-30 15:03:45 -08001019 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001020 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1021 /*
1022 * Queue overflow, abort. We may abort if the
1023 * signal was rt and sent by user using something
1024 * other than kill().
1025 */
1026 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001028 } else {
1029 /*
1030 * This is a silent loss of information. We still
1031 * send the signal, but the *info bits are lost.
1032 */
1033 trace_signal_lose_info(sig, group, info);
1034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001038 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001039 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001040 complete_signal(sig, t, group);
1041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001044static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1045 int group)
1046{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001047 int from_ancestor_ns = 0;
1048
1049#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001050 from_ancestor_ns = si_fromuser(info) &&
1051 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001052#endif
1053
1054 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001055}
1056
Ingo Molnar45807a12007-07-15 23:40:10 -07001057static void print_fatal_signal(struct pt_regs *regs, int signr)
1058{
1059 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001060 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001061
Al Viroca5cd872007-10-29 04:31:16 +00001062#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001063 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001064 {
1065 int i;
1066 for (i = 0; i < 16; i++) {
1067 unsigned char insn;
1068
Andi Kleenb45c6e72010-01-08 14:42:52 -08001069 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1070 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001071 printk("%02x ", insn);
1072 }
1073 }
1074#endif
1075 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001076 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001077 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001078 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001079}
1080
1081static int __init setup_print_fatal_signals(char *str)
1082{
1083 get_option (&str, &print_fatal_signals);
1084
1085 return 1;
1086}
1087
1088__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001090int
1091__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1092{
1093 return send_signal(sig, info, p, 1);
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096static int
1097specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1098{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001099 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001102int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1103 bool group)
1104{
1105 unsigned long flags;
1106 int ret = -ESRCH;
1107
1108 if (lock_task_sighand(p, &flags)) {
1109 ret = send_signal(sig, info, p, group);
1110 unlock_task_sighand(p, &flags);
1111 }
1112
1113 return ret;
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * Force a signal that the process can't ignore: if necessary
1118 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001119 *
1120 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1121 * since we do not want to have a signal handler that was blocked
1122 * be invoked when user space had explicitly blocked it.
1123 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001124 * We don't want to have recursive SIGSEGV's etc, for example,
1125 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127int
1128force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1129{
1130 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001131 int ret, blocked, ignored;
1132 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001135 action = &t->sighand->action[sig-1];
1136 ignored = action->sa.sa_handler == SIG_IGN;
1137 blocked = sigismember(&t->blocked, sig);
1138 if (blocked || ignored) {
1139 action->sa.sa_handler = SIG_DFL;
1140 if (blocked) {
1141 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001142 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001145 if (action->sa.sa_handler == SIG_DFL)
1146 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ret = specific_send_sig_info(sig, info, t);
1148 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1149
1150 return ret;
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/*
1154 * Nuke all other threads in the group.
1155 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001156int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001158 struct task_struct *t = p;
1159 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 p->signal->group_stop_count = 0;
1162
Oleg Nesterov09faef12010-05-26 14:43:11 -07001163 while_each_thread(p, t) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001164 task_clear_jobctl_stop_pending(t);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001165 count++;
1166
1167 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (t->exit_state)
1169 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 signal_wake_up(t, 1);
1172 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001173
1174 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001177struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1178 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001179{
1180 struct sighand_struct *sighand;
1181
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001182 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001183 for (;;) {
1184 sighand = rcu_dereference(tsk->sighand);
1185 if (unlikely(sighand == NULL))
1186 break;
1187
1188 spin_lock_irqsave(&sighand->siglock, *flags);
1189 if (likely(sighand == tsk->sighand))
1190 break;
1191 spin_unlock_irqrestore(&sighand->siglock, *flags);
1192 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001193 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001194
1195 return sighand;
1196}
1197
David Howellsc69e8d92008-11-14 10:39:19 +11001198/*
1199 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1202{
David Howells694f6902010-08-04 16:59:14 +01001203 int ret;
1204
1205 rcu_read_lock();
1206 ret = check_kill_permission(sig, info, p);
1207 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001209 if (!ret && sig)
1210 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 return ret;
1213}
1214
1215/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001216 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001218 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001220int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 struct task_struct *p = NULL;
1223 int retval, success;
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 success = 0;
1226 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001227 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int err = group_send_sig_info(sig, info, p);
1229 success |= !err;
1230 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001231 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return success ? 0 : retval;
1233}
1234
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001235int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001237 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 struct task_struct *p;
1239
Ingo Molnare56d0902006-01-08 01:01:37 -08001240 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001241retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001242 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001243 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001245 if (unlikely(error == -ESRCH))
1246 /*
1247 * The task was unhashed in between, try again.
1248 * If it is dead, pid_task() will return NULL,
1249 * if we race with de_thread() it will find the
1250 * new leader.
1251 */
1252 goto retry;
1253 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001254 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return error;
1257}
1258
Randy Dunlap5aba0852011-04-04 14:59:31 -07001259int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001260{
1261 int error;
1262 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001263 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001264 rcu_read_unlock();
1265 return error;
1266}
1267
Eric W. Biederman2425c082006-10-02 02:17:28 -07001268/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1269int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001270 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001271{
1272 int ret = -EINVAL;
1273 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001274 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001275 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001276
1277 if (!valid_signal(sig))
1278 return ret;
1279
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001280 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001281 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001282 if (!p) {
1283 ret = -ESRCH;
1284 goto out_unlock;
1285 }
David Howellsc69e8d92008-11-14 10:39:19 +11001286 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001287 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001288 euid != pcred->suid && euid != pcred->uid &&
1289 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001290 ret = -EPERM;
1291 goto out_unlock;
1292 }
David Quigley8f95dc52006-06-30 01:55:47 -07001293 ret = security_task_kill(p, info, sig, secid);
1294 if (ret)
1295 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001296
1297 if (sig) {
1298 if (lock_task_sighand(p, &flags)) {
1299 ret = __send_signal(sig, info, p, 1, 0);
1300 unlock_task_sighand(p, &flags);
1301 } else
1302 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001303 }
1304out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001305 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001306 return ret;
1307}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001308EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310/*
1311 * kill_something_info() interprets pid in interesting ways just like kill(2).
1312 *
1313 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1314 * is probably wrong. Should make it like BSD or SYSV.
1315 */
1316
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001317static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001319 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001320
1321 if (pid > 0) {
1322 rcu_read_lock();
1323 ret = kill_pid_info(sig, info, find_vpid(pid));
1324 rcu_read_unlock();
1325 return ret;
1326 }
1327
1328 read_lock(&tasklist_lock);
1329 if (pid != -1) {
1330 ret = __kill_pgrp_info(sig, info,
1331 pid ? find_vpid(-pid) : task_pgrp(current));
1332 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int retval = 0, count = 0;
1334 struct task_struct * p;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001337 if (task_pid_vnr(p) > 1 &&
1338 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 int err = group_send_sig_info(sig, info, p);
1340 ++count;
1341 if (err != -EPERM)
1342 retval = err;
1343 }
1344 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001345 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001347 read_unlock(&tasklist_lock);
1348
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352/*
1353 * These are for backward compatibility with the rest of the kernel source.
1354 */
1355
Randy Dunlap5aba0852011-04-04 14:59:31 -07001356int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /*
1359 * Make sure legacy kernel users don't send in bad values
1360 * (normal paths check this in check_kill_permission).
1361 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001362 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return -EINVAL;
1364
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001365 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001368#define __si_special(priv) \
1369 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371int
1372send_sig(int sig, struct task_struct *p, int priv)
1373{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001374 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377void
1378force_sig(int sig, struct task_struct *p)
1379{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001380 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383/*
1384 * When things go south during signal handling, we
1385 * will force a SIGSEGV. And if the signal that caused
1386 * the problem was already a SIGSEGV, we'll want to
1387 * make sure we don't even try to deliver the signal..
1388 */
1389int
1390force_sigsegv(int sig, struct task_struct *p)
1391{
1392 if (sig == SIGSEGV) {
1393 unsigned long flags;
1394 spin_lock_irqsave(&p->sighand->siglock, flags);
1395 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1396 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1397 }
1398 force_sig(SIGSEGV, p);
1399 return 0;
1400}
1401
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001402int kill_pgrp(struct pid *pid, int sig, int priv)
1403{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001404 int ret;
1405
1406 read_lock(&tasklist_lock);
1407 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1408 read_unlock(&tasklist_lock);
1409
1410 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001411}
1412EXPORT_SYMBOL(kill_pgrp);
1413
1414int kill_pid(struct pid *pid, int sig, int priv)
1415{
1416 return kill_pid_info(sig, __si_special(priv), pid);
1417}
1418EXPORT_SYMBOL(kill_pid);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/*
1421 * These functions support sending signals using preallocated sigqueue
1422 * structures. This is needed "because realtime applications cannot
1423 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001424 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * we allocate the sigqueue structure from the timer_create. If this
1426 * allocation fails we are able to report the failure to the application
1427 * with an EAGAIN error.
1428 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429struct sigqueue *sigqueue_alloc(void)
1430{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001431 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001433 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001435
1436 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439void sigqueue_free(struct sigqueue *q)
1440{
1441 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001442 spinlock_t *lock = &current->sighand->siglock;
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1445 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001446 * We must hold ->siglock while testing q->list
1447 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001448 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001450 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001451 q->flags &= ~SIGQUEUE_PREALLOC;
1452 /*
1453 * If it is queued it will be freed when dequeued,
1454 * like the "regular" sigqueue.
1455 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001456 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001457 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001458 spin_unlock_irqrestore(lock, flags);
1459
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001460 if (q)
1461 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001464int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001465{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001466 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001467 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001468 unsigned long flags;
1469 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001470
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001471 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001472
1473 ret = -1;
1474 if (!likely(lock_task_sighand(t, &flags)))
1475 goto ret;
1476
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001477 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001478 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001479 goto out;
1480
1481 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001482 if (unlikely(!list_empty(&q->list))) {
1483 /*
1484 * If an SI_TIMER entry is already queue just increment
1485 * the overrun count.
1486 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001487 BUG_ON(q->info.si_code != SI_TIMER);
1488 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001489 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001490 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001491 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001492
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001493 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001494 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001495 list_add_tail(&q->list, &pending->list);
1496 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001497 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001498out:
1499 unlock_task_sighand(t, &flags);
1500ret:
1501 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 * Let a parent know about the death of a child.
1506 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001507 *
1508 * Returns -1 if our parent ignored us and so we've switched to
1509 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001511int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 struct siginfo info;
1514 unsigned long flags;
1515 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001516 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 BUG_ON(sig == -1);
1519
1520 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001521 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001523 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1525
1526 info.si_signo = sig;
1527 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001528 /*
1529 * we are under tasklist_lock here so our parent is tied to
1530 * us and cannot exit and release its namespace.
1531 *
1532 * the only it can is to switch its nsproxy with sys_unshare,
1533 * bu uncharing pid namespaces is not allowed, so we'll always
1534 * see relevant namespace
1535 *
1536 * write_lock() currently calls preempt_disable() which is the
1537 * same as rcu_read_lock(), but according to Oleg, this is not
1538 * correct to rely on this
1539 */
1540 rcu_read_lock();
1541 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001542 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001543 rcu_read_unlock();
1544
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001545 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1546 tsk->signal->utime));
1547 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1548 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 info.si_status = tsk->exit_code & 0x7f;
1551 if (tsk->exit_code & 0x80)
1552 info.si_code = CLD_DUMPED;
1553 else if (tsk->exit_code & 0x7f)
1554 info.si_code = CLD_KILLED;
1555 else {
1556 info.si_code = CLD_EXITED;
1557 info.si_status = tsk->exit_code >> 8;
1558 }
1559
1560 psig = tsk->parent->sighand;
1561 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001562 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1564 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1565 /*
1566 * We are exiting and our parent doesn't care. POSIX.1
1567 * defines special semantics for setting SIGCHLD to SIG_IGN
1568 * or setting the SA_NOCLDWAIT flag: we should be reaped
1569 * automatically and not left for our parent's wait4 call.
1570 * Rather than having the parent do it as a magic kind of
1571 * signal handler, we just set this to tell do_exit that we
1572 * can be cleaned up without becoming a zombie. Note that
1573 * we still call __wake_up_parent in this case, because a
1574 * blocked sys_wait4 might now return -ECHILD.
1575 *
1576 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1577 * is implementation-defined: we do (if you don't want
1578 * it, just use SIG_IGN instead).
1579 */
Roland McGrath1b046242008-08-19 20:37:07 -07001580 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001582 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001584 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 __group_send_sig_info(sig, &info, tsk->parent);
1586 __wake_up_parent(tsk, tsk->parent);
1587 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001588
Roland McGrath1b046242008-08-19 20:37:07 -07001589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Tejun Heo75b95952011-03-23 10:37:01 +01001592/**
1593 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1594 * @tsk: task reporting the state change
1595 * @for_ptracer: the notification is for ptracer
1596 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1597 *
1598 * Notify @tsk's parent that the stopped/continued state has changed. If
1599 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1600 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1601 *
1602 * CONTEXT:
1603 * Must be called with tasklist_lock at least read locked.
1604 */
1605static void do_notify_parent_cldstop(struct task_struct *tsk,
1606 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
1608 struct siginfo info;
1609 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001610 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct sighand_struct *sighand;
1612
Tejun Heo75b95952011-03-23 10:37:01 +01001613 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001614 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001615 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001616 tsk = tsk->group_leader;
1617 parent = tsk->real_parent;
1618 }
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 info.si_signo = SIGCHLD;
1621 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001622 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001623 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001624 */
1625 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001626 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001627 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001628 rcu_read_unlock();
1629
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001630 info.si_utime = cputime_to_clock_t(tsk->utime);
1631 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 info.si_code = why;
1634 switch (why) {
1635 case CLD_CONTINUED:
1636 info.si_status = SIGCONT;
1637 break;
1638 case CLD_STOPPED:
1639 info.si_status = tsk->signal->group_exit_code & 0x7f;
1640 break;
1641 case CLD_TRAPPED:
1642 info.si_status = tsk->exit_code & 0x7f;
1643 break;
1644 default:
1645 BUG();
1646 }
1647
1648 sighand = parent->sighand;
1649 spin_lock_irqsave(&sighand->siglock, flags);
1650 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1651 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1652 __group_send_sig_info(SIGCHLD, &info, parent);
1653 /*
1654 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1655 */
1656 __wake_up_parent(tsk, parent);
1657 spin_unlock_irqrestore(&sighand->siglock, flags);
1658}
1659
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001660static inline int may_ptrace_stop(void)
1661{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001662 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001663 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001664 /*
1665 * Are we in the middle of do_coredump?
1666 * If so and our tracer is also part of the coredump stopping
1667 * is a deadlock situation, and pointless because our tracer
1668 * is dead so don't allow us to stop.
1669 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001670 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001671 * is safe to enter schedule().
1672 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001673 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001674 unlikely(current->mm == current->parent->mm))
1675 return 0;
1676
1677 return 1;
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001681 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001682 * Called with the siglock held.
1683 */
1684static int sigkill_pending(struct task_struct *tsk)
1685{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001686 return sigismember(&tsk->pending.signal, SIGKILL) ||
1687 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001688}
1689
1690/*
Tejun Heoceb6bd62011-03-23 10:37:01 +01001691 * Test whether the target task of the usual cldstop notification - the
1692 * real_parent of @child - is in the same group as the ptracer.
1693 */
1694static bool real_parent_is_ptracer(struct task_struct *child)
1695{
1696 return same_thread_group(child->parent, child->real_parent);
1697}
1698
1699/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 * This must be called with current->sighand->siglock held.
1701 *
1702 * This should be the path for all ptrace stops.
1703 * We always set current->last_siginfo while stopped here.
1704 * That makes it a way to test a stopped process for
1705 * being ptrace-stopped vs being job-control-stopped.
1706 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001707 * If we actually decide not to stop at all because the tracer
1708 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001710static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001711 __releases(&current->sighand->siglock)
1712 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001714 bool gstop_done = false;
1715
Roland McGrath1a669c22008-02-06 01:37:37 -08001716 if (arch_ptrace_stop_needed(exit_code, info)) {
1717 /*
1718 * The arch code has something special to do before a
1719 * ptrace stop. This is allowed to block, e.g. for faults
1720 * on user stack pages. We can't keep the siglock while
1721 * calling arch_ptrace_stop, so we must release it now.
1722 * To preserve proper semantics, we must do this before
1723 * any signal bookkeeping like checking group_stop_count.
1724 * Meanwhile, a SIGKILL could come in before we retake the
1725 * siglock. That must prevent us from sleeping in TASK_TRACED.
1726 * So after regaining the lock, we must check for SIGKILL.
1727 */
1728 spin_unlock_irq(&current->sighand->siglock);
1729 arch_ptrace_stop(exit_code, info);
1730 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001731 if (sigkill_pending(current))
1732 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001733 }
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001736 * If @why is CLD_STOPPED, we're trapping to participate in a group
1737 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1738 * while siglock was released for the arch hook, PENDING could be
1739 * clear now. We act as if SIGCONT is received after TASK_TRACED
1740 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001742 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001743 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 current->last_siginfo = info;
1746 current->exit_code = exit_code;
1747
Tejun Heod79fdd62011-03-23 10:37:00 +01001748 /*
1749 * TRACED should be visible before TRAPPING is cleared; otherwise,
1750 * the tracer might fail do_wait().
1751 */
1752 set_current_state(TASK_TRACED);
1753
1754 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001755 * We're committing to trapping. Clearing JOBCTL_TRAPPING and
Tejun Heod79fdd62011-03-23 10:37:00 +01001756 * transition to TASK_TRACED should be atomic with respect to
Tejun Heoa8f072c2011-06-02 11:13:59 +02001757 * siglock. This should be done after the arch hook as siglock is
Tejun Heod79fdd62011-03-23 10:37:00 +01001758 * released and regrabbed across it.
1759 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001760 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 spin_unlock_irq(&current->sighand->siglock);
1763 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001764 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001765 /*
1766 * Notify parents of the stop.
1767 *
1768 * While ptraced, there are two parents - the ptracer and
1769 * the real_parent of the group_leader. The ptracer should
1770 * know about every stop while the real parent is only
1771 * interested in the completion of group stop. The states
1772 * for the two don't interact with each other. Notify
1773 * separately unless they're gonna be duplicates.
1774 */
1775 do_notify_parent_cldstop(current, true, why);
1776 if (gstop_done && !real_parent_is_ptracer(current))
1777 do_notify_parent_cldstop(current, false, why);
1778
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001779 /*
1780 * Don't want to allow preemption here, because
1781 * sys_ptrace() needs this task to be inactive.
1782 *
1783 * XXX: implement read_unlock_no_resched().
1784 */
1785 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001787 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 schedule();
1789 } else {
1790 /*
1791 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001792 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001793 *
1794 * If @gstop_done, the ptracer went away between group stop
1795 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001796 * JOBCTL_STOP_PENDING on us and we'll re-enter
1797 * TASK_STOPPED in do_signal_stop() on return, so notifying
1798 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001800 if (gstop_done)
1801 do_notify_parent_cldstop(current, false, why);
1802
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001803 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001804 if (clear_code)
1805 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001806 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808
1809 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001810 * While in TASK_TRACED, we were considered "frozen enough".
1811 * Now that we woke up, it's crucial if we're supposed to be
1812 * frozen that we freeze now before running anything substantial.
1813 */
1814 try_to_freeze();
1815
1816 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * We are back. Now reacquire the siglock before touching
1818 * last_siginfo, so that we are sure to have synchronized with
1819 * any signal-sending on another CPU that wants to examine it.
1820 */
1821 spin_lock_irq(&current->sighand->siglock);
1822 current->last_siginfo = NULL;
1823
1824 /*
1825 * Queued signals ignored us while we were stopped for tracing.
1826 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001827 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001829 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832void ptrace_notify(int exit_code)
1833{
1834 siginfo_t info;
1835
1836 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1837
1838 memset(&info, 0, sizeof info);
1839 info.si_signo = SIGTRAP;
1840 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001841 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001842 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 /* Let the debugger run. */
1845 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001846 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 spin_unlock_irq(&current->sighand->siglock);
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/*
1851 * This performs the stopping for SIGSTOP and other stop signals.
1852 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001853 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 * Returns zero if we didn't stop and still hold the siglock.
1855 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001856static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
1858 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Tejun Heoa8f072c2011-06-02 11:13:59 +02001860 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1861 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001862 struct task_struct *t;
1863
Tejun Heoa8f072c2011-06-02 11:13:59 +02001864 /* signr will be recorded in task->jobctl for retries */
1865 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001866
Tejun Heoa8f072c2011-06-02 11:13:59 +02001867 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001868 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001869 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001871 * There is no group stop already in progress. We must
1872 * initiate one now.
1873 *
1874 * While ptraced, a task may be resumed while group stop is
1875 * still in effect and then receive a stop signal and
1876 * initiate another group stop. This deviates from the
1877 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001878 * cause two group stops when !ptraced. That is why we
1879 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001880 *
1881 * The condition can be distinguished by testing whether
1882 * SIGNAL_STOP_STOPPED is already set. Don't generate
1883 * group_exit_code in such case.
1884 *
1885 * This is not necessary for SIGNAL_STOP_CONTINUED because
1886 * an intervening stop signal is required to cause two
1887 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001889 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1890 sig->group_exit_code = signr;
1891 else
1892 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001893
Tejun Heoa8f072c2011-06-02 11:13:59 +02001894 current->jobctl &= ~JOBCTL_STOP_SIGMASK;
1895 current->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001896 sig->group_stop_count = 1;
Tejun Heod79fdd62011-03-23 10:37:00 +01001897 for (t = next_thread(current); t != current;
1898 t = next_thread(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001899 t->jobctl &= ~JOBCTL_STOP_SIGMASK;
Oleg Nesterova122b342006-03-28 16:11:22 -08001900 /*
1901 * Setting state to TASK_STOPPED for a group
1902 * stop is always done with the siglock held,
1903 * so this check has no races.
1904 */
Tejun Heo39efa3e2011-03-23 10:37:00 +01001905 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001906 t->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001907 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001908 signal_wake_up(t, 0);
1909 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001910 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001911 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001912retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001913 if (likely(!task_ptrace(current))) {
1914 int notify = 0;
1915
1916 /*
1917 * If there are no other threads in the group, or if there
1918 * is a group stop in progress and we are the last to stop,
1919 * report to the parent.
1920 */
1921 if (task_participate_group_stop(current))
1922 notify = CLD_STOPPED;
1923
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001924 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001925 spin_unlock_irq(&current->sighand->siglock);
1926
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001927 /*
1928 * Notify the parent of the group stop completion. Because
1929 * we're not holding either the siglock or tasklist_lock
1930 * here, ptracer may attach inbetween; however, this is for
1931 * group stop and should always be delivered to the real
1932 * parent of the group leader. The new ptracer will get
1933 * its notification when this task transitions into
1934 * TASK_TRACED.
1935 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001936 if (notify) {
1937 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001938 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001939 read_unlock(&tasklist_lock);
1940 }
1941
1942 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1943 schedule();
1944
1945 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001946 } else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001947 ptrace_stop(current->jobctl & JOBCTL_STOP_SIGMASK,
Tejun Heod79fdd62011-03-23 10:37:00 +01001948 CLD_STOPPED, 0, NULL);
1949 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001950 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001951
1952 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001953 * JOBCTL_STOP_PENDING could be set if another group stop has
Tejun Heod79fdd62011-03-23 10:37:00 +01001954 * started since being woken up or ptrace wants us to transit
1955 * between TASK_STOPPED and TRACED. Retry group stop.
1956 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001957 if (current->jobctl & JOBCTL_STOP_PENDING) {
1958 WARN_ON_ONCE(!(current->jobctl & JOBCTL_STOP_SIGMASK));
Tejun Heod79fdd62011-03-23 10:37:00 +01001959 goto retry;
1960 }
1961
1962 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001963 task_clear_jobctl_trapping(current);
Tejun Heo5224fa32011-03-23 10:37:00 +01001964
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001965 spin_unlock_irq(&current->sighand->siglock);
1966
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001967 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return 1;
1970}
1971
Roland McGrath18c98b62008-04-17 18:44:38 -07001972static int ptrace_signal(int signr, siginfo_t *info,
1973 struct pt_regs *regs, void *cookie)
1974{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001975 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001976 return signr;
1977
1978 ptrace_signal_deliver(regs, cookie);
1979
1980 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001981 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07001982
1983 /* We're back. Did the debugger cancel the sig? */
1984 signr = current->exit_code;
1985 if (signr == 0)
1986 return signr;
1987
1988 current->exit_code = 0;
1989
Randy Dunlap5aba0852011-04-04 14:59:31 -07001990 /*
1991 * Update the siginfo structure if the signal has
1992 * changed. If the debugger wanted something
1993 * specific in the siginfo structure then it should
1994 * have updated *info via PTRACE_SETSIGINFO.
1995 */
Roland McGrath18c98b62008-04-17 18:44:38 -07001996 if (signr != info->si_signo) {
1997 info->si_signo = signr;
1998 info->si_errno = 0;
1999 info->si_code = SI_USER;
2000 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11002001 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002002 }
2003
2004 /* If the (new) signal is now blocked, requeue it. */
2005 if (sigismember(&current->blocked, signr)) {
2006 specific_send_sig_info(signr, info, current);
2007 signr = 0;
2008 }
2009
2010 return signr;
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2014 struct pt_regs *regs, void *cookie)
2015{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002016 struct sighand_struct *sighand = current->sighand;
2017 struct signal_struct *signal = current->signal;
2018 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002020relock:
2021 /*
2022 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2023 * While in TASK_STOPPED, we were considered "frozen enough".
2024 * Now that we woke up, it's crucial if we're supposed to be
2025 * frozen that we freeze now before running anything substantial.
2026 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002027 try_to_freeze();
2028
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002029 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002030 /*
2031 * Every stopped thread goes here after wakeup. Check to see if
2032 * we should notify the parent, prepare_signal(SIGCONT) encodes
2033 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2034 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002035 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002036 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002037 int why;
2038
2039 if (signal->flags & SIGNAL_CLD_CONTINUED)
2040 why = CLD_CONTINUED;
2041 else
2042 why = CLD_STOPPED;
2043
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002044 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002045
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002046 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002047
Tejun Heoceb6bd62011-03-23 10:37:01 +01002048 /*
2049 * Notify the parent that we're continuing. This event is
2050 * always per-process and doesn't make whole lot of sense
2051 * for ptracers, who shouldn't consume the state via
2052 * wait(2) either, but, for backward compatibility, notify
2053 * the ptracer of the group leader too unless it's gonna be
2054 * a duplicate.
2055 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002056 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002057
2058 do_notify_parent_cldstop(current, false, why);
2059
Tejun Heo75b95952011-03-23 10:37:01 +01002060 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002061 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2062 do_notify_parent_cldstop(leader, true, why);
2063
Tejun Heoedf2ed12011-03-23 10:37:00 +01002064 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002065
Oleg Nesterove4420552008-04-30 00:52:44 -07002066 goto relock;
2067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 for (;;) {
2070 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002071 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002072 * Tracing can induce an artificial signal and choose sigaction.
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002073 * The return value in @signr determines the default action,
2074 * but @info->si_signo is the signal number we will report.
2075 */
2076 signr = tracehook_get_signal(current, regs, info, return_ka);
2077 if (unlikely(signr < 0))
2078 goto relock;
2079 if (unlikely(signr != 0))
2080 ka = return_ka;
2081 else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02002082 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2083 do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002084 goto relock;
2085
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002086 signr = dequeue_signal(current, &current->blocked,
2087 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Roland McGrath18c98b62008-04-17 18:44:38 -07002089 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002090 break; /* will return 0 */
2091
2092 if (signr != SIGKILL) {
2093 signr = ptrace_signal(signr, info,
2094 regs, cookie);
2095 if (!signr)
2096 continue;
2097 }
2098
2099 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002102 /* Trace actually delivered signals. */
2103 trace_signal_deliver(signr, info, ka);
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2106 continue;
2107 if (ka->sa.sa_handler != SIG_DFL) {
2108 /* Run the handler. */
2109 *return_ka = *ka;
2110
2111 if (ka->sa.sa_flags & SA_ONESHOT)
2112 ka->sa.sa_handler = SIG_DFL;
2113
2114 break; /* will return non-zero "signr" value */
2115 }
2116
2117 /*
2118 * Now we are doing the default action for this signal.
2119 */
2120 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2121 continue;
2122
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002123 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002124 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002125 * Container-init gets no signals it doesn't want from same
2126 * container.
2127 *
2128 * Note that if global/container-init sees a sig_kernel_only()
2129 * signal here, the signal must have been generated internally
2130 * or must have come from an ancestor namespace. In either
2131 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002132 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002133 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002134 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 continue;
2136
2137 if (sig_kernel_stop(signr)) {
2138 /*
2139 * The default action is to stop all threads in
2140 * the thread group. The job control signals
2141 * do nothing in an orphaned pgrp, but SIGSTOP
2142 * always works. Note that siglock needs to be
2143 * dropped during the call to is_orphaned_pgrp()
2144 * because of lock ordering with tasklist_lock.
2145 * This allows an intervening SIGCONT to be posted.
2146 * We need to check for that and bail out if necessary.
2147 */
2148 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002149 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 /* signals can be posted during this window */
2152
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002153 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 goto relock;
2155
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002156 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002159 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* It released the siglock. */
2161 goto relock;
2162 }
2163
2164 /*
2165 * We didn't actually stop, due to a race
2166 * with SIGCONT or something like that.
2167 */
2168 continue;
2169 }
2170
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002171 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 /*
2174 * Anything else is fatal, maybe with a core dump.
2175 */
2176 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002179 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002180 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * If it was able to dump core, this kills all
2183 * other threads in the group and synchronizes with
2184 * their demise. If we lost the race with another
2185 * thread getting here, it set group_exit_code
2186 * first and our do_group_exit call below will use
2187 * that value and ignore the one we pass it.
2188 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002189 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
2192 /*
2193 * Death signals, no core dump.
2194 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002195 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* NOTREACHED */
2197 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002198 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return signr;
2200}
2201
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002202/*
2203 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002204 * group-wide signal. Other threads should be notified now to take
2205 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002206 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002207static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002208{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002209 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002210 struct task_struct *t;
2211
Oleg Nesterovf646e222011-04-27 19:18:39 +02002212 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2213 if (sigisemptyset(&retarget))
2214 return;
2215
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002216 t = tsk;
2217 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002218 if (t->flags & PF_EXITING)
2219 continue;
2220
2221 if (!has_pending_signals(&retarget, &t->blocked))
2222 continue;
2223 /* Remove the signals this thread can handle. */
2224 sigandsets(&retarget, &retarget, &t->blocked);
2225
2226 if (!signal_pending(t))
2227 signal_wake_up(t, 0);
2228
2229 if (sigisemptyset(&retarget))
2230 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002231 }
2232}
2233
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002234void exit_signals(struct task_struct *tsk)
2235{
2236 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002237 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002238
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002239 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2240 tsk->flags |= PF_EXITING;
2241 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002242 }
2243
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002244 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002245 /*
2246 * From now this task is not visible for group-wide signals,
2247 * see wants_signal(), do_signal_stop().
2248 */
2249 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002250 if (!signal_pending(tsk))
2251 goto out;
2252
Oleg Nesterovf646e222011-04-27 19:18:39 +02002253 unblocked = tsk->blocked;
2254 signotset(&unblocked);
2255 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002256
Tejun Heoa8f072c2011-06-02 11:13:59 +02002257 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002258 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002259 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002260out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002261 spin_unlock_irq(&tsk->sighand->siglock);
2262
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002263 /*
2264 * If group stop has completed, deliver the notification. This
2265 * should always go to the real parent of the group leader.
2266 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002267 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002268 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002269 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002270 read_unlock(&tasklist_lock);
2271 }
2272}
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274EXPORT_SYMBOL(recalc_sigpending);
2275EXPORT_SYMBOL_GPL(dequeue_signal);
2276EXPORT_SYMBOL(flush_signals);
2277EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278EXPORT_SYMBOL(send_sig);
2279EXPORT_SYMBOL(send_sig_info);
2280EXPORT_SYMBOL(sigprocmask);
2281EXPORT_SYMBOL(block_all_signals);
2282EXPORT_SYMBOL(unblock_all_signals);
2283
2284
2285/*
2286 * System call entry points.
2287 */
2288
Randy Dunlap41c57892011-04-04 15:00:26 -07002289/**
2290 * sys_restart_syscall - restart a system call
2291 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002292SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
2294 struct restart_block *restart = &current_thread_info()->restart_block;
2295 return restart->fn(restart);
2296}
2297
2298long do_no_restart_syscall(struct restart_block *param)
2299{
2300 return -EINTR;
2301}
2302
Oleg Nesterovb1828012011-04-27 21:56:14 +02002303static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2304{
2305 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2306 sigset_t newblocked;
2307 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002308 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002309 retarget_shared_pending(tsk, &newblocked);
2310 }
2311 tsk->blocked = *newset;
2312 recalc_sigpending();
2313}
2314
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002315/**
2316 * set_current_blocked - change current->blocked mask
2317 * @newset: new mask
2318 *
2319 * It is wrong to change ->blocked directly, this helper should be used
2320 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002322void set_current_blocked(const sigset_t *newset)
2323{
2324 struct task_struct *tsk = current;
2325
2326 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002327 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002328 spin_unlock_irq(&tsk->sighand->siglock);
2329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331/*
2332 * This is also useful for kernel threads that want to temporarily
2333 * (or permanently) block certain signals.
2334 *
2335 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2336 * interface happily blocks "unblockable" signals like SIGKILL
2337 * and friends.
2338 */
2339int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2340{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002341 struct task_struct *tsk = current;
2342 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002344 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002345 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002346 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002347
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 switch (how) {
2349 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002350 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002353 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 break;
2355 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002356 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002361
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002362 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Randy Dunlap41c57892011-04-04 15:00:26 -07002366/**
2367 * sys_rt_sigprocmask - change the list of currently blocked signals
2368 * @how: whether to add, remove, or set signals
2369 * @set: stores pending signals
2370 * @oset: previous value of signal mask if non-null
2371 * @sigsetsize: size of sigset_t type
2372 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002373SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002374 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002377 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 /* XXX: Don't preclude handling different sized sigset_t's. */
2380 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002381 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002383 old_set = current->blocked;
2384
2385 if (nset) {
2386 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2387 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2389
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002390 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002392 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002394
2395 if (oset) {
2396 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2397 return -EFAULT;
2398 }
2399
2400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401}
2402
2403long do_sigpending(void __user *set, unsigned long sigsetsize)
2404{
2405 long error = -EINVAL;
2406 sigset_t pending;
2407
2408 if (sigsetsize > sizeof(sigset_t))
2409 goto out;
2410
2411 spin_lock_irq(&current->sighand->siglock);
2412 sigorsets(&pending, &current->pending.signal,
2413 &current->signal->shared_pending.signal);
2414 spin_unlock_irq(&current->sighand->siglock);
2415
2416 /* Outside the lock because only this thread touches it. */
2417 sigandsets(&pending, &current->blocked, &pending);
2418
2419 error = -EFAULT;
2420 if (!copy_to_user(set, &pending, sigsetsize))
2421 error = 0;
2422
2423out:
2424 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002425}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Randy Dunlap41c57892011-04-04 15:00:26 -07002427/**
2428 * sys_rt_sigpending - examine a pending signal that has been raised
2429 * while blocked
2430 * @set: stores pending signals
2431 * @sigsetsize: size of sigset_t type or larger
2432 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002433SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 return do_sigpending(set, sigsetsize);
2436}
2437
2438#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2439
2440int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2441{
2442 int err;
2443
2444 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2445 return -EFAULT;
2446 if (from->si_code < 0)
2447 return __copy_to_user(to, from, sizeof(siginfo_t))
2448 ? -EFAULT : 0;
2449 /*
2450 * If you change siginfo_t structure, please be sure
2451 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002452 * Please remember to update the signalfd_copyinfo() function
2453 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 * It should never copy any pad contained in the structure
2455 * to avoid security leaks, but must copy the generic
2456 * 3 ints plus the relevant union member.
2457 */
2458 err = __put_user(from->si_signo, &to->si_signo);
2459 err |= __put_user(from->si_errno, &to->si_errno);
2460 err |= __put_user((short)from->si_code, &to->si_code);
2461 switch (from->si_code & __SI_MASK) {
2462 case __SI_KILL:
2463 err |= __put_user(from->si_pid, &to->si_pid);
2464 err |= __put_user(from->si_uid, &to->si_uid);
2465 break;
2466 case __SI_TIMER:
2467 err |= __put_user(from->si_tid, &to->si_tid);
2468 err |= __put_user(from->si_overrun, &to->si_overrun);
2469 err |= __put_user(from->si_ptr, &to->si_ptr);
2470 break;
2471 case __SI_POLL:
2472 err |= __put_user(from->si_band, &to->si_band);
2473 err |= __put_user(from->si_fd, &to->si_fd);
2474 break;
2475 case __SI_FAULT:
2476 err |= __put_user(from->si_addr, &to->si_addr);
2477#ifdef __ARCH_SI_TRAPNO
2478 err |= __put_user(from->si_trapno, &to->si_trapno);
2479#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002480#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002481 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002482 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002483 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002484 */
2485 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2486 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 break;
2489 case __SI_CHLD:
2490 err |= __put_user(from->si_pid, &to->si_pid);
2491 err |= __put_user(from->si_uid, &to->si_uid);
2492 err |= __put_user(from->si_status, &to->si_status);
2493 err |= __put_user(from->si_utime, &to->si_utime);
2494 err |= __put_user(from->si_stime, &to->si_stime);
2495 break;
2496 case __SI_RT: /* This is not generated by the kernel as of now. */
2497 case __SI_MESGQ: /* But this is */
2498 err |= __put_user(from->si_pid, &to->si_pid);
2499 err |= __put_user(from->si_uid, &to->si_uid);
2500 err |= __put_user(from->si_ptr, &to->si_ptr);
2501 break;
2502 default: /* this is just in case for now ... */
2503 err |= __put_user(from->si_pid, &to->si_pid);
2504 err |= __put_user(from->si_uid, &to->si_uid);
2505 break;
2506 }
2507 return err;
2508}
2509
2510#endif
2511
Randy Dunlap41c57892011-04-04 15:00:26 -07002512/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002513 * do_sigtimedwait - wait for queued signals specified in @which
2514 * @which: queued signals to wait for
2515 * @info: if non-null, the signal's siginfo is returned here
2516 * @ts: upper bound on process time suspension
2517 */
2518int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2519 const struct timespec *ts)
2520{
2521 struct task_struct *tsk = current;
2522 long timeout = MAX_SCHEDULE_TIMEOUT;
2523 sigset_t mask = *which;
2524 int sig;
2525
2526 if (ts) {
2527 if (!timespec_valid(ts))
2528 return -EINVAL;
2529 timeout = timespec_to_jiffies(ts);
2530 /*
2531 * We can be close to the next tick, add another one
2532 * to ensure we will wait at least the time asked for.
2533 */
2534 if (ts->tv_sec || ts->tv_nsec)
2535 timeout++;
2536 }
2537
2538 /*
2539 * Invert the set of allowed signals to get those we want to block.
2540 */
2541 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2542 signotset(&mask);
2543
2544 spin_lock_irq(&tsk->sighand->siglock);
2545 sig = dequeue_signal(tsk, &mask, info);
2546 if (!sig && timeout) {
2547 /*
2548 * None ready, temporarily unblock those we're interested
2549 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002550 * they arrive. Unblocking is always fine, we can avoid
2551 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002552 */
2553 tsk->real_blocked = tsk->blocked;
2554 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2555 recalc_sigpending();
2556 spin_unlock_irq(&tsk->sighand->siglock);
2557
2558 timeout = schedule_timeout_interruptible(timeout);
2559
2560 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002561 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002562 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002563 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002564 }
2565 spin_unlock_irq(&tsk->sighand->siglock);
2566
2567 if (sig)
2568 return sig;
2569 return timeout ? -EINTR : -EAGAIN;
2570}
2571
2572/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002573 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2574 * in @uthese
2575 * @uthese: queued signals to wait for
2576 * @uinfo: if non-null, the signal's siginfo is returned here
2577 * @uts: upper bound on process time suspension
2578 * @sigsetsize: size of sigset_t type
2579 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002580SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2581 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2582 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 sigset_t these;
2585 struct timespec ts;
2586 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002587 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /* XXX: Don't preclude handling different sized sigset_t's. */
2590 if (sigsetsize != sizeof(sigset_t))
2591 return -EINVAL;
2592
2593 if (copy_from_user(&these, uthese, sizeof(these)))
2594 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (uts) {
2597 if (copy_from_user(&ts, uts, sizeof(ts)))
2598 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
2600
Oleg Nesterov943df142011-04-27 21:44:14 +02002601 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Oleg Nesterov943df142011-04-27 21:44:14 +02002603 if (ret > 0 && uinfo) {
2604 if (copy_siginfo_to_user(uinfo, &info))
2605 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 }
2607
2608 return ret;
2609}
2610
Randy Dunlap41c57892011-04-04 15:00:26 -07002611/**
2612 * sys_kill - send a signal to a process
2613 * @pid: the PID of the process
2614 * @sig: signal to be sent
2615 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002616SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 struct siginfo info;
2619
2620 info.si_signo = sig;
2621 info.si_errno = 0;
2622 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002623 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002624 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 return kill_something_info(sig, &info, pid);
2627}
2628
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002629static int
2630do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002631{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002632 struct task_struct *p;
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002633 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002634
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002635 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002636 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002637 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002638 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002639 /*
2640 * The null signal is a permissions and process existence
2641 * probe. No signal is actually delivered.
2642 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002643 if (!error && sig) {
2644 error = do_send_sig_info(sig, info, p, false);
2645 /*
2646 * If lock_task_sighand() failed we pretend the task
2647 * dies after receiving the signal. The window is tiny,
2648 * and the signal is private anyway.
2649 */
2650 if (unlikely(error == -ESRCH))
2651 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002652 }
2653 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002654 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002655
2656 return error;
2657}
2658
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002659static int do_tkill(pid_t tgid, pid_t pid, int sig)
2660{
2661 struct siginfo info;
2662
2663 info.si_signo = sig;
2664 info.si_errno = 0;
2665 info.si_code = SI_TKILL;
2666 info.si_pid = task_tgid_vnr(current);
2667 info.si_uid = current_uid();
2668
2669 return do_send_specific(tgid, pid, sig, &info);
2670}
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672/**
2673 * sys_tgkill - send signal to one specific thread
2674 * @tgid: the thread group ID of the thread
2675 * @pid: the PID of the thread
2676 * @sig: signal to be sent
2677 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002678 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * exists but it's not belonging to the target process anymore. This
2680 * method solves the problem of threads exiting and PIDs getting reused.
2681 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002682SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /* This is only valid for single tasks */
2685 if (pid <= 0 || tgid <= 0)
2686 return -EINVAL;
2687
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002688 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Randy Dunlap41c57892011-04-04 15:00:26 -07002691/**
2692 * sys_tkill - send signal to one specific task
2693 * @pid: the PID of the task
2694 * @sig: signal to be sent
2695 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2697 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002698SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* This is only valid for single tasks */
2701 if (pid <= 0)
2702 return -EINVAL;
2703
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002704 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Randy Dunlap41c57892011-04-04 15:00:26 -07002707/**
2708 * sys_rt_sigqueueinfo - send signal information to a signal
2709 * @pid: the PID of the thread
2710 * @sig: signal to be sent
2711 * @uinfo: signal info to be sent
2712 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002713SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2714 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 siginfo_t info;
2717
2718 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2719 return -EFAULT;
2720
2721 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002722 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2723 */
Roland Dreier243b4222011-03-28 14:13:35 -07002724 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002725 /* We used to allow any < 0 si_code */
2726 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 info.si_signo = sig;
2730
2731 /* POSIX.1b doesn't mention process groups. */
2732 return kill_proc_info(sig, &info, pid);
2733}
2734
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002735long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2736{
2737 /* This is only valid for single tasks */
2738 if (pid <= 0 || tgid <= 0)
2739 return -EINVAL;
2740
2741 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002742 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2743 */
Roland Dreier243b4222011-03-28 14:13:35 -07002744 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002745 /* We used to allow any < 0 si_code */
2746 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002747 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002748 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002749 info->si_signo = sig;
2750
2751 return do_send_specific(tgid, pid, sig, info);
2752}
2753
2754SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2755 siginfo_t __user *, uinfo)
2756{
2757 siginfo_t info;
2758
2759 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2760 return -EFAULT;
2761
2762 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2763}
2764
Oleg Nesterov88531f72006-03-28 16:11:24 -08002765int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002767 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002769 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002771 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return -EINVAL;
2773
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002774 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (oact)
2778 *oact = *k;
2779
2780 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002781 sigdelsetmask(&act->sa.sa_mask,
2782 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002783 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /*
2785 * POSIX 3.3.1.3:
2786 * "Setting a signal action to SIG_IGN for a signal that is
2787 * pending shall cause the pending signal to be discarded,
2788 * whether or not it is blocked."
2789 *
2790 * "Setting a signal action to SIG_DFL for a signal that is
2791 * pending and whose default action is to ignore the signal
2792 * (for example, SIGCHLD), shall cause the pending signal to
2793 * be discarded, whether or not it is blocked"
2794 */
Roland McGrath35de2542008-07-25 19:45:51 -07002795 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002796 sigemptyset(&mask);
2797 sigaddset(&mask, sig);
2798 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002800 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 t = next_thread(t);
2802 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805
2806 spin_unlock_irq(&current->sighand->siglock);
2807 return 0;
2808}
2809
2810int
2811do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2812{
2813 stack_t oss;
2814 int error;
2815
Linus Torvalds0083fc22009-08-01 10:34:56 -07002816 oss.ss_sp = (void __user *) current->sas_ss_sp;
2817 oss.ss_size = current->sas_ss_size;
2818 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 if (uss) {
2821 void __user *ss_sp;
2822 size_t ss_size;
2823 int ss_flags;
2824
2825 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002826 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2827 goto out;
2828 error = __get_user(ss_sp, &uss->ss_sp) |
2829 __get_user(ss_flags, &uss->ss_flags) |
2830 __get_user(ss_size, &uss->ss_size);
2831 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 goto out;
2833
2834 error = -EPERM;
2835 if (on_sig_stack(sp))
2836 goto out;
2837
2838 error = -EINVAL;
2839 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002840 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 * old code may have been written using ss_flags==0
2842 * to mean ss_flags==SS_ONSTACK (as this was the only
2843 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002844 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
2846 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2847 goto out;
2848
2849 if (ss_flags == SS_DISABLE) {
2850 ss_size = 0;
2851 ss_sp = NULL;
2852 } else {
2853 error = -ENOMEM;
2854 if (ss_size < MINSIGSTKSZ)
2855 goto out;
2856 }
2857
2858 current->sas_ss_sp = (unsigned long) ss_sp;
2859 current->sas_ss_size = ss_size;
2860 }
2861
Linus Torvalds0083fc22009-08-01 10:34:56 -07002862 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (uoss) {
2864 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002865 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002867 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2868 __put_user(oss.ss_size, &uoss->ss_size) |
2869 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872out:
2873 return error;
2874}
2875
2876#ifdef __ARCH_WANT_SYS_SIGPENDING
2877
Randy Dunlap41c57892011-04-04 15:00:26 -07002878/**
2879 * sys_sigpending - examine pending signals
2880 * @set: where mask of pending signal is returned
2881 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002882SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 return do_sigpending(set, sizeof(*set));
2885}
2886
2887#endif
2888
2889#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002890/**
2891 * sys_sigprocmask - examine and change blocked signals
2892 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02002893 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07002894 * @oset: previous value of signal mask if non-null
2895 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002896 * Some platforms have their own version with special arguments;
2897 * others support only sys_rt_sigprocmask.
2898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Oleg Nesterovb013c392011-04-28 11:36:20 +02002900SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002901 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002904 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Oleg Nesterovb013c392011-04-28 11:36:20 +02002906 old_set = current->blocked.sig[0];
2907
2908 if (nset) {
2909 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2910 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2912
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002913 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002917 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 break;
2919 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002920 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 break;
2922 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002923 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002925 default:
2926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
2928
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002929 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02002931
2932 if (oset) {
2933 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2934 return -EFAULT;
2935 }
2936
2937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938}
2939#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2940
2941#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002942/**
2943 * sys_rt_sigaction - alter an action taken by a process
2944 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07002945 * @act: new sigaction
2946 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07002947 * @sigsetsize: size of sigset_t type
2948 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002949SYSCALL_DEFINE4(rt_sigaction, int, sig,
2950 const struct sigaction __user *, act,
2951 struct sigaction __user *, oact,
2952 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 struct k_sigaction new_sa, old_sa;
2955 int ret = -EINVAL;
2956
2957 /* XXX: Don't preclude handling different sized sigset_t's. */
2958 if (sigsetsize != sizeof(sigset_t))
2959 goto out;
2960
2961 if (act) {
2962 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2963 return -EFAULT;
2964 }
2965
2966 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2967
2968 if (!ret && oact) {
2969 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2970 return -EFAULT;
2971 }
2972out:
2973 return ret;
2974}
2975#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2976
2977#ifdef __ARCH_WANT_SYS_SGETMASK
2978
2979/*
2980 * For backwards compatibility. Functionality superseded by sigprocmask.
2981 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002982SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983{
2984 /* SMP safe */
2985 return current->blocked.sig[0];
2986}
2987
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002988SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989{
2990 int old;
2991
2992 spin_lock_irq(&current->sighand->siglock);
2993 old = current->blocked.sig[0];
2994
2995 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2996 sigmask(SIGSTOP)));
2997 recalc_sigpending();
2998 spin_unlock_irq(&current->sighand->siglock);
2999
3000 return old;
3001}
3002#endif /* __ARCH_WANT_SGETMASK */
3003
3004#ifdef __ARCH_WANT_SYS_SIGNAL
3005/*
3006 * For backwards compatibility. Functionality superseded by sigaction.
3007 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003008SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 struct k_sigaction new_sa, old_sa;
3011 int ret;
3012
3013 new_sa.sa.sa_handler = handler;
3014 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03003015 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017 ret = do_sigaction(sig, &new_sa, &old_sa);
3018
3019 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3020}
3021#endif /* __ARCH_WANT_SYS_SIGNAL */
3022
3023#ifdef __ARCH_WANT_SYS_PAUSE
3024
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003025SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003027 while (!signal_pending(current)) {
3028 current->state = TASK_INTERRUPTIBLE;
3029 schedule();
3030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return -ERESTARTNOHAND;
3032}
3033
3034#endif
3035
David Woodhouse150256d2006-01-18 17:43:57 -08003036#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003037/**
3038 * sys_rt_sigsuspend - replace the signal mask for a value with the
3039 * @unewset value until a signal is received
3040 * @unewset: new signal mask value
3041 * @sigsetsize: size of sigset_t type
3042 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003043SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003044{
3045 sigset_t newset;
3046
3047 /* XXX: Don't preclude handling different sized sigset_t's. */
3048 if (sigsetsize != sizeof(sigset_t))
3049 return -EINVAL;
3050
3051 if (copy_from_user(&newset, unewset, sizeof(newset)))
3052 return -EFAULT;
3053 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3054
3055 spin_lock_irq(&current->sighand->siglock);
3056 current->saved_sigmask = current->blocked;
3057 current->blocked = newset;
3058 recalc_sigpending();
3059 spin_unlock_irq(&current->sighand->siglock);
3060
3061 current->state = TASK_INTERRUPTIBLE;
3062 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003063 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003064 return -ERESTARTNOHAND;
3065}
3066#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3067
David Howellsf269fdd2006-09-27 01:50:23 -07003068__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3069{
3070 return NULL;
3071}
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073void __init signals_init(void)
3074{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003075 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003077
3078#ifdef CONFIG_KGDB_KDB
3079#include <linux/kdb.h>
3080/*
3081 * kdb_send_sig_info - Allows kdb to send signals without exposing
3082 * signal internals. This function checks if the required locks are
3083 * available before calling the main signal code, to avoid kdb
3084 * deadlocks.
3085 */
3086void
3087kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3088{
3089 static struct task_struct *kdb_prev_t;
3090 int sig, new_t;
3091 if (!spin_trylock(&t->sighand->siglock)) {
3092 kdb_printf("Can't do kill command now.\n"
3093 "The sigmask lock is held somewhere else in "
3094 "kernel, try again later\n");
3095 return;
3096 }
3097 spin_unlock(&t->sighand->siglock);
3098 new_t = kdb_prev_t != t;
3099 kdb_prev_t = t;
3100 if (t->state != TASK_RUNNING && new_t) {
3101 kdb_printf("Process is not RUNNING, sending a signal from "
3102 "kdb risks deadlock\n"
3103 "on the run queue locks. "
3104 "The signal has _not_ been sent.\n"
3105 "Reissue the kill command if you want to risk "
3106 "the deadlock.\n");
3107 return;
3108 }
3109 sig = info->si_signo;
3110 if (send_sig_info(sig, info, t))
3111 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3112 sig, t->pid);
3113 else
3114 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3115}
3116#endif /* CONFIG_KGDB_KDB */