blob: 62a6c3bb9f0d9a839ce8efa4540e9069d0134fa4 [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 Heo81be24b2011-06-02 11:13:59 +02001736 * We're committing to trapping. TRACED should be visible before
1737 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1738 * Also, transition to TRACED and updates to ->jobctl should be
1739 * atomic with respect to siglock and should be done after the arch
1740 * hook as siglock is released and regrabbed across it.
1741 */
1742 set_current_state(TASK_TRACED);
1743
1744 current->last_siginfo = info;
1745 current->exit_code = exit_code;
1746
1747 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001748 * If @why is CLD_STOPPED, we're trapping to participate in a group
1749 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1750 * while siglock was released for the arch hook, PENDING could be
1751 * clear now. We act as if SIGCONT is received after TASK_TRACED
1752 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001754 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001755 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Tejun Heo81be24b2011-06-02 11:13:59 +02001757 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001758 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 spin_unlock_irq(&current->sighand->siglock);
1761 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001762 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001763 /*
1764 * Notify parents of the stop.
1765 *
1766 * While ptraced, there are two parents - the ptracer and
1767 * the real_parent of the group_leader. The ptracer should
1768 * know about every stop while the real parent is only
1769 * interested in the completion of group stop. The states
1770 * for the two don't interact with each other. Notify
1771 * separately unless they're gonna be duplicates.
1772 */
1773 do_notify_parent_cldstop(current, true, why);
1774 if (gstop_done && !real_parent_is_ptracer(current))
1775 do_notify_parent_cldstop(current, false, why);
1776
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001777 /*
1778 * Don't want to allow preemption here, because
1779 * sys_ptrace() needs this task to be inactive.
1780 *
1781 * XXX: implement read_unlock_no_resched().
1782 */
1783 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001785 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 schedule();
1787 } else {
1788 /*
1789 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001790 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001791 *
1792 * If @gstop_done, the ptracer went away between group stop
1793 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001794 * JOBCTL_STOP_PENDING on us and we'll re-enter
1795 * TASK_STOPPED in do_signal_stop() on return, so notifying
1796 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001798 if (gstop_done)
1799 do_notify_parent_cldstop(current, false, why);
1800
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001801 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001802 if (clear_code)
1803 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001804 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
1806
1807 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001808 * While in TASK_TRACED, we were considered "frozen enough".
1809 * Now that we woke up, it's crucial if we're supposed to be
1810 * frozen that we freeze now before running anything substantial.
1811 */
1812 try_to_freeze();
1813
1814 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * We are back. Now reacquire the siglock before touching
1816 * last_siginfo, so that we are sure to have synchronized with
1817 * any signal-sending on another CPU that wants to examine it.
1818 */
1819 spin_lock_irq(&current->sighand->siglock);
1820 current->last_siginfo = NULL;
1821
1822 /*
1823 * Queued signals ignored us while we were stopped for tracing.
1824 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001825 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001827 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830void ptrace_notify(int exit_code)
1831{
1832 siginfo_t info;
1833
1834 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1835
1836 memset(&info, 0, sizeof info);
1837 info.si_signo = SIGTRAP;
1838 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001839 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001840 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 /* Let the debugger run. */
1843 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001844 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 spin_unlock_irq(&current->sighand->siglock);
1846}
1847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848/*
1849 * This performs the stopping for SIGSTOP and other stop signals.
1850 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001851 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * Returns zero if we didn't stop and still hold the siglock.
1853 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001854static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
1856 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Tejun Heoa8f072c2011-06-02 11:13:59 +02001858 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1859 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001860 struct task_struct *t;
1861
Tejun Heoa8f072c2011-06-02 11:13:59 +02001862 /* signr will be recorded in task->jobctl for retries */
1863 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001864
Tejun Heoa8f072c2011-06-02 11:13:59 +02001865 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001866 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001869 * There is no group stop already in progress. We must
1870 * initiate one now.
1871 *
1872 * While ptraced, a task may be resumed while group stop is
1873 * still in effect and then receive a stop signal and
1874 * initiate another group stop. This deviates from the
1875 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001876 * cause two group stops when !ptraced. That is why we
1877 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001878 *
1879 * The condition can be distinguished by testing whether
1880 * SIGNAL_STOP_STOPPED is already set. Don't generate
1881 * group_exit_code in such case.
1882 *
1883 * This is not necessary for SIGNAL_STOP_CONTINUED because
1884 * an intervening stop signal is required to cause two
1885 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001887 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1888 sig->group_exit_code = signr;
1889 else
1890 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001891
Tejun Heoa8f072c2011-06-02 11:13:59 +02001892 current->jobctl &= ~JOBCTL_STOP_SIGMASK;
1893 current->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001894 sig->group_stop_count = 1;
Tejun Heod79fdd62011-03-23 10:37:00 +01001895 for (t = next_thread(current); t != current;
1896 t = next_thread(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001897 t->jobctl &= ~JOBCTL_STOP_SIGMASK;
Oleg Nesterova122b342006-03-28 16:11:22 -08001898 /*
1899 * Setting state to TASK_STOPPED for a group
1900 * stop is always done with the siglock held,
1901 * so this check has no races.
1902 */
Tejun Heo39efa3e2011-03-23 10:37:00 +01001903 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001904 t->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001905 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001906 signal_wake_up(t, 0);
1907 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001908 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001909 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001910retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001911 if (likely(!task_ptrace(current))) {
1912 int notify = 0;
1913
1914 /*
1915 * If there are no other threads in the group, or if there
1916 * is a group stop in progress and we are the last to stop,
1917 * report to the parent.
1918 */
1919 if (task_participate_group_stop(current))
1920 notify = CLD_STOPPED;
1921
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001922 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001923 spin_unlock_irq(&current->sighand->siglock);
1924
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001925 /*
1926 * Notify the parent of the group stop completion. Because
1927 * we're not holding either the siglock or tasklist_lock
1928 * here, ptracer may attach inbetween; however, this is for
1929 * group stop and should always be delivered to the real
1930 * parent of the group leader. The new ptracer will get
1931 * its notification when this task transitions into
1932 * TASK_TRACED.
1933 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001934 if (notify) {
1935 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001936 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001937 read_unlock(&tasklist_lock);
1938 }
1939
1940 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1941 schedule();
1942
1943 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001944 } else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001945 ptrace_stop(current->jobctl & JOBCTL_STOP_SIGMASK,
Tejun Heod79fdd62011-03-23 10:37:00 +01001946 CLD_STOPPED, 0, NULL);
1947 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001948 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001949
1950 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001951 * JOBCTL_STOP_PENDING could be set if another group stop has
Tejun Heod79fdd62011-03-23 10:37:00 +01001952 * started since being woken up or ptrace wants us to transit
1953 * between TASK_STOPPED and TRACED. Retry group stop.
1954 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001955 if (current->jobctl & JOBCTL_STOP_PENDING) {
1956 WARN_ON_ONCE(!(current->jobctl & JOBCTL_STOP_SIGMASK));
Tejun Heod79fdd62011-03-23 10:37:00 +01001957 goto retry;
1958 }
1959
1960 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001961 task_clear_jobctl_trapping(current);
Tejun Heo5224fa32011-03-23 10:37:00 +01001962
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001963 spin_unlock_irq(&current->sighand->siglock);
1964
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001965 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 return 1;
1968}
1969
Roland McGrath18c98b62008-04-17 18:44:38 -07001970static int ptrace_signal(int signr, siginfo_t *info,
1971 struct pt_regs *regs, void *cookie)
1972{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001973 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001974 return signr;
1975
1976 ptrace_signal_deliver(regs, cookie);
1977
1978 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001979 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07001980
1981 /* We're back. Did the debugger cancel the sig? */
1982 signr = current->exit_code;
1983 if (signr == 0)
1984 return signr;
1985
1986 current->exit_code = 0;
1987
Randy Dunlap5aba0852011-04-04 14:59:31 -07001988 /*
1989 * Update the siginfo structure if the signal has
1990 * changed. If the debugger wanted something
1991 * specific in the siginfo structure then it should
1992 * have updated *info via PTRACE_SETSIGINFO.
1993 */
Roland McGrath18c98b62008-04-17 18:44:38 -07001994 if (signr != info->si_signo) {
1995 info->si_signo = signr;
1996 info->si_errno = 0;
1997 info->si_code = SI_USER;
1998 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001999 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002000 }
2001
2002 /* If the (new) signal is now blocked, requeue it. */
2003 if (sigismember(&current->blocked, signr)) {
2004 specific_send_sig_info(signr, info, current);
2005 signr = 0;
2006 }
2007
2008 return signr;
2009}
2010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2012 struct pt_regs *regs, void *cookie)
2013{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002014 struct sighand_struct *sighand = current->sighand;
2015 struct signal_struct *signal = current->signal;
2016 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002018relock:
2019 /*
2020 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2021 * While in TASK_STOPPED, we were considered "frozen enough".
2022 * Now that we woke up, it's crucial if we're supposed to be
2023 * frozen that we freeze now before running anything substantial.
2024 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002025 try_to_freeze();
2026
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002027 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002028 /*
2029 * Every stopped thread goes here after wakeup. Check to see if
2030 * we should notify the parent, prepare_signal(SIGCONT) encodes
2031 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2032 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002033 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002034 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002035 int why;
2036
2037 if (signal->flags & SIGNAL_CLD_CONTINUED)
2038 why = CLD_CONTINUED;
2039 else
2040 why = CLD_STOPPED;
2041
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002042 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002043
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002044 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002045
Tejun Heoceb6bd62011-03-23 10:37:01 +01002046 /*
2047 * Notify the parent that we're continuing. This event is
2048 * always per-process and doesn't make whole lot of sense
2049 * for ptracers, who shouldn't consume the state via
2050 * wait(2) either, but, for backward compatibility, notify
2051 * the ptracer of the group leader too unless it's gonna be
2052 * a duplicate.
2053 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002054 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002055
2056 do_notify_parent_cldstop(current, false, why);
2057
Tejun Heo75b95952011-03-23 10:37:01 +01002058 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002059 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2060 do_notify_parent_cldstop(leader, true, why);
2061
Tejun Heoedf2ed12011-03-23 10:37:00 +01002062 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002063
Oleg Nesterove4420552008-04-30 00:52:44 -07002064 goto relock;
2065 }
2066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 for (;;) {
2068 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002069 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002070 * Tracing can induce an artificial signal and choose sigaction.
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002071 * The return value in @signr determines the default action,
2072 * but @info->si_signo is the signal number we will report.
2073 */
2074 signr = tracehook_get_signal(current, regs, info, return_ka);
2075 if (unlikely(signr < 0))
2076 goto relock;
2077 if (unlikely(signr != 0))
2078 ka = return_ka;
2079 else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02002080 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2081 do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002082 goto relock;
2083
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002084 signr = dequeue_signal(current, &current->blocked,
2085 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Roland McGrath18c98b62008-04-17 18:44:38 -07002087 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002088 break; /* will return 0 */
2089
2090 if (signr != SIGKILL) {
2091 signr = ptrace_signal(signr, info,
2092 regs, cookie);
2093 if (!signr)
2094 continue;
2095 }
2096
2097 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002100 /* Trace actually delivered signals. */
2101 trace_signal_deliver(signr, info, ka);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2104 continue;
2105 if (ka->sa.sa_handler != SIG_DFL) {
2106 /* Run the handler. */
2107 *return_ka = *ka;
2108
2109 if (ka->sa.sa_flags & SA_ONESHOT)
2110 ka->sa.sa_handler = SIG_DFL;
2111
2112 break; /* will return non-zero "signr" value */
2113 }
2114
2115 /*
2116 * Now we are doing the default action for this signal.
2117 */
2118 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2119 continue;
2120
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002121 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002122 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002123 * Container-init gets no signals it doesn't want from same
2124 * container.
2125 *
2126 * Note that if global/container-init sees a sig_kernel_only()
2127 * signal here, the signal must have been generated internally
2128 * or must have come from an ancestor namespace. In either
2129 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002130 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002131 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002132 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 continue;
2134
2135 if (sig_kernel_stop(signr)) {
2136 /*
2137 * The default action is to stop all threads in
2138 * the thread group. The job control signals
2139 * do nothing in an orphaned pgrp, but SIGSTOP
2140 * always works. Note that siglock needs to be
2141 * dropped during the call to is_orphaned_pgrp()
2142 * because of lock ordering with tasklist_lock.
2143 * This allows an intervening SIGCONT to be posted.
2144 * We need to check for that and bail out if necessary.
2145 */
2146 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002147 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* signals can be posted during this window */
2150
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002151 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 goto relock;
2153
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002154 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002157 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 /* It released the siglock. */
2159 goto relock;
2160 }
2161
2162 /*
2163 * We didn't actually stop, due to a race
2164 * with SIGCONT or something like that.
2165 */
2166 continue;
2167 }
2168
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002169 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /*
2172 * Anything else is fatal, maybe with a core dump.
2173 */
2174 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002177 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002178 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * If it was able to dump core, this kills all
2181 * other threads in the group and synchronizes with
2182 * their demise. If we lost the race with another
2183 * thread getting here, it set group_exit_code
2184 * first and our do_group_exit call below will use
2185 * that value and ignore the one we pass it.
2186 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002187 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189
2190 /*
2191 * Death signals, no core dump.
2192 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002193 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 /* NOTREACHED */
2195 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002196 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return signr;
2198}
2199
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002200/*
2201 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002202 * group-wide signal. Other threads should be notified now to take
2203 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002204 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002205static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002206{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002207 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002208 struct task_struct *t;
2209
Oleg Nesterovf646e222011-04-27 19:18:39 +02002210 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2211 if (sigisemptyset(&retarget))
2212 return;
2213
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002214 t = tsk;
2215 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002216 if (t->flags & PF_EXITING)
2217 continue;
2218
2219 if (!has_pending_signals(&retarget, &t->blocked))
2220 continue;
2221 /* Remove the signals this thread can handle. */
2222 sigandsets(&retarget, &retarget, &t->blocked);
2223
2224 if (!signal_pending(t))
2225 signal_wake_up(t, 0);
2226
2227 if (sigisemptyset(&retarget))
2228 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002229 }
2230}
2231
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002232void exit_signals(struct task_struct *tsk)
2233{
2234 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002235 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002236
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002237 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2238 tsk->flags |= PF_EXITING;
2239 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002240 }
2241
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002242 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002243 /*
2244 * From now this task is not visible for group-wide signals,
2245 * see wants_signal(), do_signal_stop().
2246 */
2247 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002248 if (!signal_pending(tsk))
2249 goto out;
2250
Oleg Nesterovf646e222011-04-27 19:18:39 +02002251 unblocked = tsk->blocked;
2252 signotset(&unblocked);
2253 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002254
Tejun Heoa8f072c2011-06-02 11:13:59 +02002255 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002256 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002257 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002258out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002259 spin_unlock_irq(&tsk->sighand->siglock);
2260
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002261 /*
2262 * If group stop has completed, deliver the notification. This
2263 * should always go to the real parent of the group leader.
2264 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002265 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002266 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002267 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002268 read_unlock(&tasklist_lock);
2269 }
2270}
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272EXPORT_SYMBOL(recalc_sigpending);
2273EXPORT_SYMBOL_GPL(dequeue_signal);
2274EXPORT_SYMBOL(flush_signals);
2275EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276EXPORT_SYMBOL(send_sig);
2277EXPORT_SYMBOL(send_sig_info);
2278EXPORT_SYMBOL(sigprocmask);
2279EXPORT_SYMBOL(block_all_signals);
2280EXPORT_SYMBOL(unblock_all_signals);
2281
2282
2283/*
2284 * System call entry points.
2285 */
2286
Randy Dunlap41c57892011-04-04 15:00:26 -07002287/**
2288 * sys_restart_syscall - restart a system call
2289 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002290SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 struct restart_block *restart = &current_thread_info()->restart_block;
2293 return restart->fn(restart);
2294}
2295
2296long do_no_restart_syscall(struct restart_block *param)
2297{
2298 return -EINTR;
2299}
2300
Oleg Nesterovb1828012011-04-27 21:56:14 +02002301static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2302{
2303 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2304 sigset_t newblocked;
2305 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002306 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002307 retarget_shared_pending(tsk, &newblocked);
2308 }
2309 tsk->blocked = *newset;
2310 recalc_sigpending();
2311}
2312
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002313/**
2314 * set_current_blocked - change current->blocked mask
2315 * @newset: new mask
2316 *
2317 * It is wrong to change ->blocked directly, this helper should be used
2318 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002320void set_current_blocked(const sigset_t *newset)
2321{
2322 struct task_struct *tsk = current;
2323
2324 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002325 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002326 spin_unlock_irq(&tsk->sighand->siglock);
2327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329/*
2330 * This is also useful for kernel threads that want to temporarily
2331 * (or permanently) block certain signals.
2332 *
2333 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2334 * interface happily blocks "unblockable" signals like SIGKILL
2335 * and friends.
2336 */
2337int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2338{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002339 struct task_struct *tsk = current;
2340 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002342 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002343 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002344 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 switch (how) {
2347 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002348 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 break;
2350 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002351 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002354 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 break;
2356 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002357 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002359
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002360 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362}
2363
Randy Dunlap41c57892011-04-04 15:00:26 -07002364/**
2365 * sys_rt_sigprocmask - change the list of currently blocked signals
2366 * @how: whether to add, remove, or set signals
2367 * @set: stores pending signals
2368 * @oset: previous value of signal mask if non-null
2369 * @sigsetsize: size of sigset_t type
2370 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002371SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002372 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002375 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /* XXX: Don't preclude handling different sized sigset_t's. */
2378 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002381 old_set = current->blocked;
2382
2383 if (nset) {
2384 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2385 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2387
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002388 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002390 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002392
2393 if (oset) {
2394 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2395 return -EFAULT;
2396 }
2397
2398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399}
2400
2401long do_sigpending(void __user *set, unsigned long sigsetsize)
2402{
2403 long error = -EINVAL;
2404 sigset_t pending;
2405
2406 if (sigsetsize > sizeof(sigset_t))
2407 goto out;
2408
2409 spin_lock_irq(&current->sighand->siglock);
2410 sigorsets(&pending, &current->pending.signal,
2411 &current->signal->shared_pending.signal);
2412 spin_unlock_irq(&current->sighand->siglock);
2413
2414 /* Outside the lock because only this thread touches it. */
2415 sigandsets(&pending, &current->blocked, &pending);
2416
2417 error = -EFAULT;
2418 if (!copy_to_user(set, &pending, sigsetsize))
2419 error = 0;
2420
2421out:
2422 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002423}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Randy Dunlap41c57892011-04-04 15:00:26 -07002425/**
2426 * sys_rt_sigpending - examine a pending signal that has been raised
2427 * while blocked
2428 * @set: stores pending signals
2429 * @sigsetsize: size of sigset_t type or larger
2430 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002431SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 return do_sigpending(set, sigsetsize);
2434}
2435
2436#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2437
2438int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2439{
2440 int err;
2441
2442 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2443 return -EFAULT;
2444 if (from->si_code < 0)
2445 return __copy_to_user(to, from, sizeof(siginfo_t))
2446 ? -EFAULT : 0;
2447 /*
2448 * If you change siginfo_t structure, please be sure
2449 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002450 * Please remember to update the signalfd_copyinfo() function
2451 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 * It should never copy any pad contained in the structure
2453 * to avoid security leaks, but must copy the generic
2454 * 3 ints plus the relevant union member.
2455 */
2456 err = __put_user(from->si_signo, &to->si_signo);
2457 err |= __put_user(from->si_errno, &to->si_errno);
2458 err |= __put_user((short)from->si_code, &to->si_code);
2459 switch (from->si_code & __SI_MASK) {
2460 case __SI_KILL:
2461 err |= __put_user(from->si_pid, &to->si_pid);
2462 err |= __put_user(from->si_uid, &to->si_uid);
2463 break;
2464 case __SI_TIMER:
2465 err |= __put_user(from->si_tid, &to->si_tid);
2466 err |= __put_user(from->si_overrun, &to->si_overrun);
2467 err |= __put_user(from->si_ptr, &to->si_ptr);
2468 break;
2469 case __SI_POLL:
2470 err |= __put_user(from->si_band, &to->si_band);
2471 err |= __put_user(from->si_fd, &to->si_fd);
2472 break;
2473 case __SI_FAULT:
2474 err |= __put_user(from->si_addr, &to->si_addr);
2475#ifdef __ARCH_SI_TRAPNO
2476 err |= __put_user(from->si_trapno, &to->si_trapno);
2477#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002478#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002479 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002480 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002481 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002482 */
2483 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2484 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 break;
2487 case __SI_CHLD:
2488 err |= __put_user(from->si_pid, &to->si_pid);
2489 err |= __put_user(from->si_uid, &to->si_uid);
2490 err |= __put_user(from->si_status, &to->si_status);
2491 err |= __put_user(from->si_utime, &to->si_utime);
2492 err |= __put_user(from->si_stime, &to->si_stime);
2493 break;
2494 case __SI_RT: /* This is not generated by the kernel as of now. */
2495 case __SI_MESGQ: /* But this is */
2496 err |= __put_user(from->si_pid, &to->si_pid);
2497 err |= __put_user(from->si_uid, &to->si_uid);
2498 err |= __put_user(from->si_ptr, &to->si_ptr);
2499 break;
2500 default: /* this is just in case for now ... */
2501 err |= __put_user(from->si_pid, &to->si_pid);
2502 err |= __put_user(from->si_uid, &to->si_uid);
2503 break;
2504 }
2505 return err;
2506}
2507
2508#endif
2509
Randy Dunlap41c57892011-04-04 15:00:26 -07002510/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002511 * do_sigtimedwait - wait for queued signals specified in @which
2512 * @which: queued signals to wait for
2513 * @info: if non-null, the signal's siginfo is returned here
2514 * @ts: upper bound on process time suspension
2515 */
2516int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2517 const struct timespec *ts)
2518{
2519 struct task_struct *tsk = current;
2520 long timeout = MAX_SCHEDULE_TIMEOUT;
2521 sigset_t mask = *which;
2522 int sig;
2523
2524 if (ts) {
2525 if (!timespec_valid(ts))
2526 return -EINVAL;
2527 timeout = timespec_to_jiffies(ts);
2528 /*
2529 * We can be close to the next tick, add another one
2530 * to ensure we will wait at least the time asked for.
2531 */
2532 if (ts->tv_sec || ts->tv_nsec)
2533 timeout++;
2534 }
2535
2536 /*
2537 * Invert the set of allowed signals to get those we want to block.
2538 */
2539 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2540 signotset(&mask);
2541
2542 spin_lock_irq(&tsk->sighand->siglock);
2543 sig = dequeue_signal(tsk, &mask, info);
2544 if (!sig && timeout) {
2545 /*
2546 * None ready, temporarily unblock those we're interested
2547 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002548 * they arrive. Unblocking is always fine, we can avoid
2549 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002550 */
2551 tsk->real_blocked = tsk->blocked;
2552 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2553 recalc_sigpending();
2554 spin_unlock_irq(&tsk->sighand->siglock);
2555
2556 timeout = schedule_timeout_interruptible(timeout);
2557
2558 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002559 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002560 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002561 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002562 }
2563 spin_unlock_irq(&tsk->sighand->siglock);
2564
2565 if (sig)
2566 return sig;
2567 return timeout ? -EINTR : -EAGAIN;
2568}
2569
2570/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002571 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2572 * in @uthese
2573 * @uthese: queued signals to wait for
2574 * @uinfo: if non-null, the signal's siginfo is returned here
2575 * @uts: upper bound on process time suspension
2576 * @sigsetsize: size of sigset_t type
2577 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002578SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2579 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2580 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 sigset_t these;
2583 struct timespec ts;
2584 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002585 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /* XXX: Don't preclude handling different sized sigset_t's. */
2588 if (sigsetsize != sizeof(sigset_t))
2589 return -EINVAL;
2590
2591 if (copy_from_user(&these, uthese, sizeof(these)))
2592 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (uts) {
2595 if (copy_from_user(&ts, uts, sizeof(ts)))
2596 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598
Oleg Nesterov943df142011-04-27 21:44:14 +02002599 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Oleg Nesterov943df142011-04-27 21:44:14 +02002601 if (ret > 0 && uinfo) {
2602 if (copy_siginfo_to_user(uinfo, &info))
2603 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605
2606 return ret;
2607}
2608
Randy Dunlap41c57892011-04-04 15:00:26 -07002609/**
2610 * sys_kill - send a signal to a process
2611 * @pid: the PID of the process
2612 * @sig: signal to be sent
2613 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002614SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 struct siginfo info;
2617
2618 info.si_signo = sig;
2619 info.si_errno = 0;
2620 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002621 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002622 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 return kill_something_info(sig, &info, pid);
2625}
2626
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002627static int
2628do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002629{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002630 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002631 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002632
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002633 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002634 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002635 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002636 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002637 /*
2638 * The null signal is a permissions and process existence
2639 * probe. No signal is actually delivered.
2640 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002641 if (!error && sig) {
2642 error = do_send_sig_info(sig, info, p, false);
2643 /*
2644 * If lock_task_sighand() failed we pretend the task
2645 * dies after receiving the signal. The window is tiny,
2646 * and the signal is private anyway.
2647 */
2648 if (unlikely(error == -ESRCH))
2649 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002650 }
2651 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002652 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002653
2654 return error;
2655}
2656
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002657static int do_tkill(pid_t tgid, pid_t pid, int sig)
2658{
2659 struct siginfo info;
2660
2661 info.si_signo = sig;
2662 info.si_errno = 0;
2663 info.si_code = SI_TKILL;
2664 info.si_pid = task_tgid_vnr(current);
2665 info.si_uid = current_uid();
2666
2667 return do_send_specific(tgid, pid, sig, &info);
2668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670/**
2671 * sys_tgkill - send signal to one specific thread
2672 * @tgid: the thread group ID of the thread
2673 * @pid: the PID of the thread
2674 * @sig: signal to be sent
2675 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002676 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 * exists but it's not belonging to the target process anymore. This
2678 * method solves the problem of threads exiting and PIDs getting reused.
2679 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002680SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /* This is only valid for single tasks */
2683 if (pid <= 0 || tgid <= 0)
2684 return -EINVAL;
2685
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002686 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688
Randy Dunlap41c57892011-04-04 15:00:26 -07002689/**
2690 * sys_tkill - send signal to one specific task
2691 * @pid: the PID of the task
2692 * @sig: signal to be sent
2693 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2695 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002696SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /* This is only valid for single tasks */
2699 if (pid <= 0)
2700 return -EINVAL;
2701
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002702 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Randy Dunlap41c57892011-04-04 15:00:26 -07002705/**
2706 * sys_rt_sigqueueinfo - send signal information to a signal
2707 * @pid: the PID of the thread
2708 * @sig: signal to be sent
2709 * @uinfo: signal info to be sent
2710 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002711SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2712 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
2714 siginfo_t info;
2715
2716 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2717 return -EFAULT;
2718
2719 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002720 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2721 */
Roland Dreier243b4222011-03-28 14:13:35 -07002722 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002723 /* We used to allow any < 0 si_code */
2724 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 info.si_signo = sig;
2728
2729 /* POSIX.1b doesn't mention process groups. */
2730 return kill_proc_info(sig, &info, pid);
2731}
2732
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002733long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2734{
2735 /* This is only valid for single tasks */
2736 if (pid <= 0 || tgid <= 0)
2737 return -EINVAL;
2738
2739 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002740 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2741 */
Roland Dreier243b4222011-03-28 14:13:35 -07002742 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002743 /* We used to allow any < 0 si_code */
2744 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002745 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002746 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002747 info->si_signo = sig;
2748
2749 return do_send_specific(tgid, pid, sig, info);
2750}
2751
2752SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2753 siginfo_t __user *, uinfo)
2754{
2755 siginfo_t info;
2756
2757 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2758 return -EFAULT;
2759
2760 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2761}
2762
Oleg Nesterov88531f72006-03-28 16:11:24 -08002763int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002765 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002767 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002769 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return -EINVAL;
2771
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002772 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (oact)
2776 *oact = *k;
2777
2778 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002779 sigdelsetmask(&act->sa.sa_mask,
2780 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002781 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /*
2783 * POSIX 3.3.1.3:
2784 * "Setting a signal action to SIG_IGN for a signal that is
2785 * pending shall cause the pending signal to be discarded,
2786 * whether or not it is blocked."
2787 *
2788 * "Setting a signal action to SIG_DFL for a signal that is
2789 * pending and whose default action is to ignore the signal
2790 * (for example, SIGCHLD), shall cause the pending signal to
2791 * be discarded, whether or not it is blocked"
2792 */
Roland McGrath35de2542008-07-25 19:45:51 -07002793 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002794 sigemptyset(&mask);
2795 sigaddset(&mask, sig);
2796 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002798 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 t = next_thread(t);
2800 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803
2804 spin_unlock_irq(&current->sighand->siglock);
2805 return 0;
2806}
2807
2808int
2809do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2810{
2811 stack_t oss;
2812 int error;
2813
Linus Torvalds0083fc22009-08-01 10:34:56 -07002814 oss.ss_sp = (void __user *) current->sas_ss_sp;
2815 oss.ss_size = current->sas_ss_size;
2816 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 if (uss) {
2819 void __user *ss_sp;
2820 size_t ss_size;
2821 int ss_flags;
2822
2823 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002824 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2825 goto out;
2826 error = __get_user(ss_sp, &uss->ss_sp) |
2827 __get_user(ss_flags, &uss->ss_flags) |
2828 __get_user(ss_size, &uss->ss_size);
2829 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 goto out;
2831
2832 error = -EPERM;
2833 if (on_sig_stack(sp))
2834 goto out;
2835
2836 error = -EINVAL;
2837 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002838 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 * old code may have been written using ss_flags==0
2840 * to mean ss_flags==SS_ONSTACK (as this was the only
2841 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002842 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
2844 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2845 goto out;
2846
2847 if (ss_flags == SS_DISABLE) {
2848 ss_size = 0;
2849 ss_sp = NULL;
2850 } else {
2851 error = -ENOMEM;
2852 if (ss_size < MINSIGSTKSZ)
2853 goto out;
2854 }
2855
2856 current->sas_ss_sp = (unsigned long) ss_sp;
2857 current->sas_ss_size = ss_size;
2858 }
2859
Linus Torvalds0083fc22009-08-01 10:34:56 -07002860 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (uoss) {
2862 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002863 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002865 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2866 __put_user(oss.ss_size, &uoss->ss_size) |
2867 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870out:
2871 return error;
2872}
2873
2874#ifdef __ARCH_WANT_SYS_SIGPENDING
2875
Randy Dunlap41c57892011-04-04 15:00:26 -07002876/**
2877 * sys_sigpending - examine pending signals
2878 * @set: where mask of pending signal is returned
2879 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002880SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 return do_sigpending(set, sizeof(*set));
2883}
2884
2885#endif
2886
2887#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002888/**
2889 * sys_sigprocmask - examine and change blocked signals
2890 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02002891 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07002892 * @oset: previous value of signal mask if non-null
2893 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002894 * Some platforms have their own version with special arguments;
2895 * others support only sys_rt_sigprocmask.
2896 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Oleg Nesterovb013c392011-04-28 11:36:20 +02002898SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002899 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002902 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Oleg Nesterovb013c392011-04-28 11:36:20 +02002904 old_set = current->blocked.sig[0];
2905
2906 if (nset) {
2907 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2908 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2910
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002911 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002915 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 break;
2917 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002918 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 break;
2920 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002921 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002923 default:
2924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
2926
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002927 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02002929
2930 if (oset) {
2931 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2932 return -EFAULT;
2933 }
2934
2935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936}
2937#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2938
2939#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002940/**
2941 * sys_rt_sigaction - alter an action taken by a process
2942 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07002943 * @act: new sigaction
2944 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07002945 * @sigsetsize: size of sigset_t type
2946 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002947SYSCALL_DEFINE4(rt_sigaction, int, sig,
2948 const struct sigaction __user *, act,
2949 struct sigaction __user *, oact,
2950 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 struct k_sigaction new_sa, old_sa;
2953 int ret = -EINVAL;
2954
2955 /* XXX: Don't preclude handling different sized sigset_t's. */
2956 if (sigsetsize != sizeof(sigset_t))
2957 goto out;
2958
2959 if (act) {
2960 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2961 return -EFAULT;
2962 }
2963
2964 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2965
2966 if (!ret && oact) {
2967 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2968 return -EFAULT;
2969 }
2970out:
2971 return ret;
2972}
2973#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2974
2975#ifdef __ARCH_WANT_SYS_SGETMASK
2976
2977/*
2978 * For backwards compatibility. Functionality superseded by sigprocmask.
2979 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002980SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 /* SMP safe */
2983 return current->blocked.sig[0];
2984}
2985
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002986SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 int old;
2989
2990 spin_lock_irq(&current->sighand->siglock);
2991 old = current->blocked.sig[0];
2992
2993 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2994 sigmask(SIGSTOP)));
2995 recalc_sigpending();
2996 spin_unlock_irq(&current->sighand->siglock);
2997
2998 return old;
2999}
3000#endif /* __ARCH_WANT_SGETMASK */
3001
3002#ifdef __ARCH_WANT_SYS_SIGNAL
3003/*
3004 * For backwards compatibility. Functionality superseded by sigaction.
3005 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003006SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007{
3008 struct k_sigaction new_sa, old_sa;
3009 int ret;
3010
3011 new_sa.sa.sa_handler = handler;
3012 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03003013 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 ret = do_sigaction(sig, &new_sa, &old_sa);
3016
3017 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3018}
3019#endif /* __ARCH_WANT_SYS_SIGNAL */
3020
3021#ifdef __ARCH_WANT_SYS_PAUSE
3022
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003023SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003025 while (!signal_pending(current)) {
3026 current->state = TASK_INTERRUPTIBLE;
3027 schedule();
3028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return -ERESTARTNOHAND;
3030}
3031
3032#endif
3033
David Woodhouse150256d2006-01-18 17:43:57 -08003034#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003035/**
3036 * sys_rt_sigsuspend - replace the signal mask for a value with the
3037 * @unewset value until a signal is received
3038 * @unewset: new signal mask value
3039 * @sigsetsize: size of sigset_t type
3040 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003041SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003042{
3043 sigset_t newset;
3044
3045 /* XXX: Don't preclude handling different sized sigset_t's. */
3046 if (sigsetsize != sizeof(sigset_t))
3047 return -EINVAL;
3048
3049 if (copy_from_user(&newset, unewset, sizeof(newset)))
3050 return -EFAULT;
3051 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3052
3053 spin_lock_irq(&current->sighand->siglock);
3054 current->saved_sigmask = current->blocked;
3055 current->blocked = newset;
3056 recalc_sigpending();
3057 spin_unlock_irq(&current->sighand->siglock);
3058
3059 current->state = TASK_INTERRUPTIBLE;
3060 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003061 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003062 return -ERESTARTNOHAND;
3063}
3064#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3065
David Howellsf269fdd2006-09-27 01:50:23 -07003066__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3067{
3068 return NULL;
3069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071void __init signals_init(void)
3072{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003073 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003075
3076#ifdef CONFIG_KGDB_KDB
3077#include <linux/kdb.h>
3078/*
3079 * kdb_send_sig_info - Allows kdb to send signals without exposing
3080 * signal internals. This function checks if the required locks are
3081 * available before calling the main signal code, to avoid kdb
3082 * deadlocks.
3083 */
3084void
3085kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3086{
3087 static struct task_struct *kdb_prev_t;
3088 int sig, new_t;
3089 if (!spin_trylock(&t->sighand->siglock)) {
3090 kdb_printf("Can't do kill command now.\n"
3091 "The sigmask lock is held somewhere else in "
3092 "kernel, try again later\n");
3093 return;
3094 }
3095 spin_unlock(&t->sighand->siglock);
3096 new_t = kdb_prev_t != t;
3097 kdb_prev_t = t;
3098 if (t->state != TASK_RUNNING && new_t) {
3099 kdb_printf("Process is not RUNNING, sending a signal from "
3100 "kdb risks deadlock\n"
3101 "on the run queue locks. "
3102 "The signal has _not_ been sent.\n"
3103 "Reissue the kill command if you want to risk "
3104 "the deadlock.\n");
3105 return;
3106 }
3107 sig = info->si_signo;
3108 if (send_sig_info(sig, info, t))
3109 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3110 sig, t->pid);
3111 else
3112 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3113}
3114#endif /* CONFIG_KGDB_KDB */