blob: 288d952fa3b8885557363b877b49e896ee5cee35 [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 Heo3759a0d2011-06-02 11:14:00 +0200127 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
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 Heo3759a0d2011-06-02 11:14:00 +0200248 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c19022011-03-23 10:37:00 +0100249 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200250 * @mask: pending bits to clear
Tejun Heoe5c19022011-03-23 10:37:00 +0100251 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200252 * Clear @mask from @task->jobctl. @mask must be subset of
253 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
254 * STOP bits are cleared together.
Tejun Heoe5c19022011-03-23 10:37:00 +0100255 *
256 * CONTEXT:
257 * Must be called with @task->sighand->siglock held.
258 */
Tejun Heo3759a0d2011-06-02 11:14:00 +0200259void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
Tejun Heoe5c19022011-03-23 10:37:00 +0100260{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200261 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
262
263 if (mask & JOBCTL_STOP_PENDING)
264 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
265
266 task->jobctl &= ~mask;
Tejun Heoe5c19022011-03-23 10:37:00 +0100267}
268
269/**
270 * task_participate_group_stop - participate in a group stop
271 * @task: task participating in a group stop
272 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200273 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100274 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200275 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100276 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100277 *
278 * CONTEXT:
279 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100280 *
281 * RETURNS:
282 * %true if group stop completion should be notified to the parent, %false
283 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100284 */
285static bool task_participate_group_stop(struct task_struct *task)
286{
287 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200288 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100289
Tejun Heoa8f072c2011-06-02 11:13:59 +0200290 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100291
Tejun Heo3759a0d2011-06-02 11:14:00 +0200292 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c19022011-03-23 10:37:00 +0100293
294 if (!consume)
295 return false;
296
297 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
298 sig->group_stop_count--;
299
Tejun Heo244056f2011-03-23 10:37:01 +0100300 /*
301 * Tell the caller to notify completion iff we are entering into a
302 * fresh group stop. Read comment in do_signal_stop() for details.
303 */
304 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100305 sig->flags = SIGNAL_STOP_STOPPED;
306 return true;
307 }
308 return false;
309}
310
David Howellsc69e8d92008-11-14 10:39:19 +1100311/*
312 * allocate a new signal queue record
313 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700314 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100315 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900316static struct sigqueue *
317__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800320 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800322 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000323 * Protect access to @t credentials. This can go away when all
324 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800325 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000326 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100327 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800328 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000329 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800332 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800333 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900335 } else {
336 print_dropped_signal(sig);
337 }
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800340 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100341 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 } else {
343 INIT_LIST_HEAD(&q->list);
344 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100345 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
David Howellsd84f4f92008-11-14 10:39:23 +1100347
348 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Andrew Morton514a01b2006-02-03 03:04:41 -0800351static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 if (q->flags & SIGQUEUE_PREALLOC)
354 return;
355 atomic_dec(&q->user->sigpending);
356 free_uid(q->user);
357 kmem_cache_free(sigqueue_cachep, q);
358}
359
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800360void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 struct sigqueue *q;
363
364 sigemptyset(&queue->signal);
365 while (!list_empty(&queue->list)) {
366 q = list_entry(queue->list.next, struct sigqueue , list);
367 list_del_init(&q->list);
368 __sigqueue_free(q);
369 }
370}
371
372/*
373 * Flush all pending signals for a task.
374 */
David Howells3bcac022009-04-29 13:45:05 +0100375void __flush_signals(struct task_struct *t)
376{
377 clear_tsk_thread_flag(t, TIF_SIGPENDING);
378 flush_sigqueue(&t->pending);
379 flush_sigqueue(&t->signal->shared_pending);
380}
381
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800382void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
384 unsigned long flags;
385
386 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100387 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock_irqrestore(&t->sighand->siglock, flags);
389}
390
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400391static void __flush_itimer_signals(struct sigpending *pending)
392{
393 sigset_t signal, retain;
394 struct sigqueue *q, *n;
395
396 signal = pending->signal;
397 sigemptyset(&retain);
398
399 list_for_each_entry_safe(q, n, &pending->list, list) {
400 int sig = q->info.si_signo;
401
402 if (likely(q->info.si_code != SI_TIMER)) {
403 sigaddset(&retain, sig);
404 } else {
405 sigdelset(&signal, sig);
406 list_del_init(&q->list);
407 __sigqueue_free(q);
408 }
409 }
410
411 sigorsets(&pending->signal, &signal, &retain);
412}
413
414void flush_itimer_signals(void)
415{
416 struct task_struct *tsk = current;
417 unsigned long flags;
418
419 spin_lock_irqsave(&tsk->sighand->siglock, flags);
420 __flush_itimer_signals(&tsk->pending);
421 __flush_itimer_signals(&tsk->signal->shared_pending);
422 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
423}
424
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700425void ignore_signals(struct task_struct *t)
426{
427 int i;
428
429 for (i = 0; i < _NSIG; ++i)
430 t->sighand->action[i].sa.sa_handler = SIG_IGN;
431
432 flush_signals(t);
433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * Flush all handlers for a task.
437 */
438
439void
440flush_signal_handlers(struct task_struct *t, int force_default)
441{
442 int i;
443 struct k_sigaction *ka = &t->sighand->action[0];
444 for (i = _NSIG ; i != 0 ; i--) {
445 if (force_default || ka->sa.sa_handler != SIG_IGN)
446 ka->sa.sa_handler = SIG_DFL;
447 ka->sa.sa_flags = 0;
448 sigemptyset(&ka->sa.sa_mask);
449 ka++;
450 }
451}
452
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200453int unhandled_signal(struct task_struct *tsk, int sig)
454{
Roland McGrath445a91d2008-07-25 19:45:52 -0700455 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700456 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200457 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700458 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200459 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700460 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200461}
462
Randy Dunlap5aba0852011-04-04 14:59:31 -0700463/*
464 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * process, and wants to be notified if any signals at all were to be
466 * sent/acted upon. If the notifier routine returns non-zero, then the
467 * signal will be acted upon after all. If the notifier routine returns 0,
468 * then then signal will be blocked. Only one block per process is
469 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700470 * can use to determine if the signal should be blocked or not.
471 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472void
473block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
474{
475 unsigned long flags;
476
477 spin_lock_irqsave(&current->sighand->siglock, flags);
478 current->notifier_mask = mask;
479 current->notifier_data = priv;
480 current->notifier = notifier;
481 spin_unlock_irqrestore(&current->sighand->siglock, flags);
482}
483
484/* Notify the system that blocking has ended. */
485
486void
487unblock_all_signals(void)
488{
489 unsigned long flags;
490
491 spin_lock_irqsave(&current->sighand->siglock, flags);
492 current->notifier = NULL;
493 current->notifier_data = NULL;
494 recalc_sigpending();
495 spin_unlock_irqrestore(&current->sighand->siglock, flags);
496}
497
Oleg Nesterov100360f2008-07-25 01:47:29 -0700498static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Collect the siginfo appropriate to this signal. Check if
504 * there is another siginfo for the same signal.
505 */
506 list_for_each_entry(q, &list->list, list) {
507 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700508 if (first)
509 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 first = q;
511 }
512 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700513
514 sigdelset(&list->signal, sig);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700517still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 list_del_init(&first->list);
519 copy_siginfo(info, &first->info);
520 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700522 /*
523 * Ok, it wasn't in the queue. This must be
524 * a fast-pathed signal or we must have been
525 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 info->si_signo = sig;
528 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800529 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 info->si_pid = 0;
531 info->si_uid = 0;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
536 siginfo_t *info)
537{
Roland McGrath27d91e02006-09-29 02:00:31 -0700538 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (sig) {
541 if (current->notifier) {
542 if (sigismember(current->notifier_mask, sig)) {
543 if (!(current->notifier)(current->notifier_data)) {
544 clear_thread_flag(TIF_SIGPENDING);
545 return 0;
546 }
547 }
548 }
549
Oleg Nesterov100360f2008-07-25 01:47:29 -0700550 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return sig;
554}
555
556/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700557 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 * expected to free it.
559 *
560 * All callers have to hold the siglock.
561 */
562int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
563{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700564 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000565
566 /* We only dequeue private signals from ourselves, we don't let
567 * signalfd steal them
568 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700569 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800570 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 signr = __dequeue_signal(&tsk->signal->shared_pending,
572 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800573 /*
574 * itimer signal ?
575 *
576 * itimers are process shared and we restart periodic
577 * itimers in the signal delivery path to prevent DoS
578 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700579 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800580 * itimers, as the SIGALRM is a legacy signal and only
581 * queued once. Changing the restart behaviour to
582 * restart the timer in the signal dequeue path is
583 * reducing the timer noise on heavy loaded !highres
584 * systems too.
585 */
586 if (unlikely(signr == SIGALRM)) {
587 struct hrtimer *tmr = &tsk->signal->real_timer;
588
589 if (!hrtimer_is_queued(tmr) &&
590 tsk->signal->it_real_incr.tv64 != 0) {
591 hrtimer_forward(tmr, tmr->base->get_time(),
592 tsk->signal->it_real_incr);
593 hrtimer_restart(tmr);
594 }
595 }
596 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700597
Davide Libenzib8fceee2007-09-20 12:40:16 -0700598 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700599 if (!signr)
600 return 0;
601
602 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800603 /*
604 * Set a marker that we have dequeued a stop signal. Our
605 * caller might release the siglock and then the pending
606 * stop signal it is about to process is no longer in the
607 * pending bitmasks, but must still be cleared by a SIGCONT
608 * (and overruled by a SIGKILL). So those cases clear this
609 * shared flag after we've set it. Note that this flag may
610 * remain set after the signal we return is ignored or
611 * handled. That doesn't matter because its only purpose
612 * is to alert stop-signal processing code when another
613 * processor has come along and cleared the flag.
614 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200615 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800616 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700617 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /*
619 * Release the siglock to ensure proper locking order
620 * of timer locks outside of siglocks. Note, we leave
621 * irqs disabled here, since the posix-timers code is
622 * about to disable them again anyway.
623 */
624 spin_unlock(&tsk->sighand->siglock);
625 do_schedule_next_timer(info);
626 spin_lock(&tsk->sighand->siglock);
627 }
628 return signr;
629}
630
631/*
632 * Tell a process that it has a new active signal..
633 *
634 * NOTE! we rely on the previous spin_lock to
635 * lock interrupts for us! We can only be called with
636 * "siglock" held, and the local interrupt must
637 * have been disabled when that got acquired!
638 *
639 * No need to set need_resched since signal event passing
640 * goes through ->blocked
641 */
642void signal_wake_up(struct task_struct *t, int resume)
643{
644 unsigned int mask;
645
646 set_tsk_thread_flag(t, TIF_SIGPENDING);
647
648 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500649 * For SIGKILL, we want to wake it up in the stopped/traced/killable
650 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * executing another processor and just now entering stopped state.
652 * By using wake_up_state, we ensure the process will wake up and
653 * handle its death signal.
654 */
655 mask = TASK_INTERRUPTIBLE;
656 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500657 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (!wake_up_state(t, mask))
659 kick_process(t);
660}
661
662/*
663 * Remove signals in mask from the pending set and queue.
664 * Returns 1 if any signals were found.
665 *
666 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800667 *
668 * This version takes a sigset mask and looks at all signals,
669 * not just those in the first mask word.
670 */
671static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
672{
673 struct sigqueue *q, *n;
674 sigset_t m;
675
676 sigandsets(&m, mask, &s->signal);
677 if (sigisemptyset(&m))
678 return 0;
679
Oleg Nesterov702a5072011-04-27 22:01:27 +0200680 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800681 list_for_each_entry_safe(q, n, &s->list, list) {
682 if (sigismember(mask, q->info.si_signo)) {
683 list_del_init(&q->list);
684 __sigqueue_free(q);
685 }
686 }
687 return 1;
688}
689/*
690 * Remove signals in mask from the pending set and queue.
691 * Returns 1 if any signals were found.
692 *
693 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 */
695static int rm_from_queue(unsigned long mask, struct sigpending *s)
696{
697 struct sigqueue *q, *n;
698
699 if (!sigtestsetmask(&s->signal, mask))
700 return 0;
701
702 sigdelsetmask(&s->signal, mask);
703 list_for_each_entry_safe(q, n, &s->list, list) {
704 if (q->info.si_signo < SIGRTMIN &&
705 (mask & sigmask(q->info.si_signo))) {
706 list_del_init(&q->list);
707 __sigqueue_free(q);
708 }
709 }
710 return 1;
711}
712
Oleg Nesterov614c5172009-12-15 16:47:22 -0800713static inline int is_si_special(const struct siginfo *info)
714{
715 return info <= SEND_SIG_FORCED;
716}
717
718static inline bool si_fromuser(const struct siginfo *info)
719{
720 return info == SEND_SIG_NOINFO ||
721 (!is_si_special(info) && SI_FROMUSER(info));
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700725 * called with RCU read lock from check_kill_permission()
726 */
727static int kill_ok_by_cred(struct task_struct *t)
728{
729 const struct cred *cred = current_cred();
730 const struct cred *tcred = __task_cred(t);
731
732 if (cred->user->user_ns == tcred->user->user_ns &&
733 (cred->euid == tcred->suid ||
734 cred->euid == tcred->uid ||
735 cred->uid == tcred->suid ||
736 cred->uid == tcred->uid))
737 return 1;
738
739 if (ns_capable(tcred->user->user_ns, CAP_KILL))
740 return 1;
741
742 return 0;
743}
744
745/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100747 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 */
749static int check_kill_permission(int sig, struct siginfo *info,
750 struct task_struct *t)
751{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700752 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700753 int error;
754
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700755 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700756 return -EINVAL;
757
Oleg Nesterov614c5172009-12-15 16:47:22 -0800758 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700759 return 0;
760
761 error = audit_signal_info(sig, t); /* Let audit system see the signal */
762 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400764
Oleg Nesterov065add32010-05-26 14:42:54 -0700765 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700766 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700767 switch (sig) {
768 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700769 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700770 /*
771 * We don't return the error if sid == NULL. The
772 * task was unhashed, the caller must notice this.
773 */
774 if (!sid || sid == task_session(current))
775 break;
776 default:
777 return -EPERM;
778 }
779 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100780
Amy Griffise54dc242007-03-29 18:01:04 -0400781 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700785 * Handle magic process-wide effects of stop/continue signals. Unlike
786 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * time regardless of blocking, ignoring, or handling. This does the
788 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700789 * signals. The process stop is done as a signal action for SIG_DFL.
790 *
791 * Returns true if the signal should be actually delivered, otherwise
792 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700794static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Oleg Nesterovad16a462008-04-30 00:52:46 -0700796 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct task_struct *t;
798
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700799 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700801 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700803 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
805 * This is a stop signal. Remove SIGCONT from all queues.
806 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700807 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 t = p;
809 do {
810 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700811 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700813 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200815 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700817 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 t = p;
819 do {
Tejun Heo3759a0d2011-06-02 11:14:00 +0200820 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Oleg Nesterov1deac632011-04-01 20:11:50 +0200822 wake_up_state(t, __TASK_STOPPED);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700823 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700825 /*
826 * Notify the parent with CLD_CONTINUED if we were stopped.
827 *
828 * If we were in the middle of a group stop, we pretend it
829 * was already finished, and then continued. Since SIGCHLD
830 * doesn't queue we report only CLD_STOPPED, as if the next
831 * CLD_CONTINUED was dropped.
832 */
833 why = 0;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700834 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700835 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700836 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700837 why |= SIGNAL_CLD_STOPPED;
838
839 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700840 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700841 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700842 * will take ->siglock, notice SIGNAL_CLD_MASK, and
843 * notify its parent. See get_signal_to_deliver().
844 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700845 signal->flags = why | SIGNAL_STOP_CONTINUED;
846 signal->group_stop_count = 0;
847 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700850
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700851 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700854/*
855 * Test if P wants to take SIG. After we've checked all threads with this,
856 * it's equivalent to finding no threads not blocking SIG. Any threads not
857 * blocking SIG were ruled out because they are not running and already
858 * have pending signals. Such threads will dequeue from the shared queue
859 * as soon as they're available, so putting the signal on the shared queue
860 * will be equivalent to sending it to one such thread.
861 */
862static inline int wants_signal(int sig, struct task_struct *p)
863{
864 if (sigismember(&p->blocked, sig))
865 return 0;
866 if (p->flags & PF_EXITING)
867 return 0;
868 if (sig == SIGKILL)
869 return 1;
870 if (task_is_stopped_or_traced(p))
871 return 0;
872 return task_curr(p) || !signal_pending(p);
873}
874
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700875static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700876{
877 struct signal_struct *signal = p->signal;
878 struct task_struct *t;
879
880 /*
881 * Now find a thread we can wake up to take the signal off the queue.
882 *
883 * If the main thread wants the signal, it gets first crack.
884 * Probably the least surprising to the average bear.
885 */
886 if (wants_signal(sig, p))
887 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700888 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700889 /*
890 * There is just one thread and it does not need to be woken.
891 * It will dequeue unblocked signals before it runs again.
892 */
893 return;
894 else {
895 /*
896 * Otherwise try to find a suitable thread.
897 */
898 t = signal->curr_target;
899 while (!wants_signal(sig, t)) {
900 t = next_thread(t);
901 if (t == signal->curr_target)
902 /*
903 * No thread needs to be woken.
904 * Any eligible threads will see
905 * the signal in the queue soon.
906 */
907 return;
908 }
909 signal->curr_target = t;
910 }
911
912 /*
913 * Found a killable thread. If the signal will be fatal,
914 * then start taking the whole group down immediately.
915 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700916 if (sig_fatal(p, sig) &&
917 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700918 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700919 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700920 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700921 /*
922 * This signal will be fatal to the whole group.
923 */
924 if (!sig_kernel_coredump(sig)) {
925 /*
926 * Start a group exit and wake everybody up.
927 * This way we don't have other threads
928 * running and doing things after a slower
929 * thread has the fatal signal pending.
930 */
931 signal->flags = SIGNAL_GROUP_EXIT;
932 signal->group_exit_code = sig;
933 signal->group_stop_count = 0;
934 t = p;
935 do {
Tejun Heo3759a0d2011-06-02 11:14:00 +0200936 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700937 sigaddset(&t->pending.signal, SIGKILL);
938 signal_wake_up(t, 1);
939 } while_each_thread(p, t);
940 return;
941 }
942 }
943
944 /*
945 * The signal is already in the shared-pending queue.
946 * Tell the chosen thread to wake up and dequeue it.
947 */
948 signal_wake_up(t, sig == SIGKILL);
949 return;
950}
951
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700952static inline int legacy_queue(struct sigpending *signals, int sig)
953{
954 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
955}
956
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700957static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
958 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700960 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700961 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200962 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500964 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400965
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700966 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700967
968 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700969 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700970
971 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700973 * Short-circuit ignored signals and support queuing
974 * exactly one non-rt signal, so that we can get more
975 * detailed information about the cause of the signal.
976 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700977 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700978 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700979 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * fast-pathed signals for kernel-internal things like SIGSTOP
981 * or SIGKILL.
982 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800983 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out_set;
985
Randy Dunlap5aba0852011-04-04 14:59:31 -0700986 /*
987 * Real-time signals must be queued if sent by sigqueue, or
988 * some other real-time mechanism. It is implementation
989 * defined whether kill() does so. We attempt to do so, on
990 * the principle of least surprise, but since kill is not
991 * allowed to fail with EAGAIN when low on memory we just
992 * make sure at least one signal gets delivered and don't
993 * pass on the info struct.
994 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200995 if (sig < SIGRTMIN)
996 override_rlimit = (is_si_special(info) || info->si_code >= 0);
997 else
998 override_rlimit = 0;
999
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001000 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001001 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001003 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001005 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 q->info.si_signo = sig;
1007 q->info.si_errno = 0;
1008 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001009 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001010 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001011 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001013 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 q->info.si_signo = sig;
1015 q->info.si_errno = 0;
1016 q->info.si_code = SI_KERNEL;
1017 q->info.si_pid = 0;
1018 q->info.si_uid = 0;
1019 break;
1020 default:
1021 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001022 if (from_ancestor_ns)
1023 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 break;
1025 }
Oleg Nesterov621d3122005-10-30 15:03:45 -08001026 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001027 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1028 /*
1029 * Queue overflow, abort. We may abort if the
1030 * signal was rt and sent by user using something
1031 * other than kill().
1032 */
1033 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001035 } else {
1036 /*
1037 * This is a silent loss of information. We still
1038 * send the signal, but the *info bits are lost.
1039 */
1040 trace_signal_lose_info(sig, group, info);
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001045 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001046 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001047 complete_signal(sig, t, group);
1048 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001051static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1052 int group)
1053{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001054 int from_ancestor_ns = 0;
1055
1056#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001057 from_ancestor_ns = si_fromuser(info) &&
1058 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001059#endif
1060
1061 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001062}
1063
Ingo Molnar45807a12007-07-15 23:40:10 -07001064static void print_fatal_signal(struct pt_regs *regs, int signr)
1065{
1066 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001067 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001068
Al Viroca5cd872007-10-29 04:31:16 +00001069#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001070 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001071 {
1072 int i;
1073 for (i = 0; i < 16; i++) {
1074 unsigned char insn;
1075
Andi Kleenb45c6e72010-01-08 14:42:52 -08001076 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1077 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001078 printk("%02x ", insn);
1079 }
1080 }
1081#endif
1082 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001083 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001084 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001085 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001086}
1087
1088static int __init setup_print_fatal_signals(char *str)
1089{
1090 get_option (&str, &print_fatal_signals);
1091
1092 return 1;
1093}
1094
1095__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001097int
1098__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1099{
1100 return send_signal(sig, info, p, 1);
1101}
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103static int
1104specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1105{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001106 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001109int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1110 bool group)
1111{
1112 unsigned long flags;
1113 int ret = -ESRCH;
1114
1115 if (lock_task_sighand(p, &flags)) {
1116 ret = send_signal(sig, info, p, group);
1117 unlock_task_sighand(p, &flags);
1118 }
1119
1120 return ret;
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
1124 * Force a signal that the process can't ignore: if necessary
1125 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001126 *
1127 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1128 * since we do not want to have a signal handler that was blocked
1129 * be invoked when user space had explicitly blocked it.
1130 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001131 * We don't want to have recursive SIGSEGV's etc, for example,
1132 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134int
1135force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1136{
1137 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001138 int ret, blocked, ignored;
1139 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001142 action = &t->sighand->action[sig-1];
1143 ignored = action->sa.sa_handler == SIG_IGN;
1144 blocked = sigismember(&t->blocked, sig);
1145 if (blocked || ignored) {
1146 action->sa.sa_handler = SIG_DFL;
1147 if (blocked) {
1148 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001149 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001152 if (action->sa.sa_handler == SIG_DFL)
1153 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ret = specific_send_sig_info(sig, info, t);
1155 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1156
1157 return ret;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
1161 * Nuke all other threads in the group.
1162 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001163int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001165 struct task_struct *t = p;
1166 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 p->signal->group_stop_count = 0;
1169
Oleg Nesterov09faef12010-05-26 14:43:11 -07001170 while_each_thread(p, t) {
Tejun Heo3759a0d2011-06-02 11:14:00 +02001171 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001172 count++;
1173
1174 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (t->exit_state)
1176 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 signal_wake_up(t, 1);
1179 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001180
1181 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001184struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1185 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001186{
1187 struct sighand_struct *sighand;
1188
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001189 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001190 for (;;) {
1191 sighand = rcu_dereference(tsk->sighand);
1192 if (unlikely(sighand == NULL))
1193 break;
1194
1195 spin_lock_irqsave(&sighand->siglock, *flags);
1196 if (likely(sighand == tsk->sighand))
1197 break;
1198 spin_unlock_irqrestore(&sighand->siglock, *flags);
1199 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001200 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001201
1202 return sighand;
1203}
1204
David Howellsc69e8d92008-11-14 10:39:19 +11001205/*
1206 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1209{
David Howells694f6902010-08-04 16:59:14 +01001210 int ret;
1211
1212 rcu_read_lock();
1213 ret = check_kill_permission(sig, info, p);
1214 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001216 if (!ret && sig)
1217 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 return ret;
1220}
1221
1222/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001223 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001225 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001227int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct task_struct *p = NULL;
1230 int retval, success;
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 success = 0;
1233 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001234 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int err = group_send_sig_info(sig, info, p);
1236 success |= !err;
1237 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001238 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return success ? 0 : retval;
1240}
1241
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001242int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001244 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 struct task_struct *p;
1246
Ingo Molnare56d0902006-01-08 01:01:37 -08001247 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001248retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001249 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001250 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001252 if (unlikely(error == -ESRCH))
1253 /*
1254 * The task was unhashed in between, try again.
1255 * If it is dead, pid_task() will return NULL,
1256 * if we race with de_thread() it will find the
1257 * new leader.
1258 */
1259 goto retry;
1260 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001261 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return error;
1264}
1265
Randy Dunlap5aba0852011-04-04 14:59:31 -07001266int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001267{
1268 int error;
1269 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001270 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001271 rcu_read_unlock();
1272 return error;
1273}
1274
Eric W. Biederman2425c082006-10-02 02:17:28 -07001275/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1276int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001277 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001278{
1279 int ret = -EINVAL;
1280 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001281 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001282 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001283
1284 if (!valid_signal(sig))
1285 return ret;
1286
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001287 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001288 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001289 if (!p) {
1290 ret = -ESRCH;
1291 goto out_unlock;
1292 }
David Howellsc69e8d92008-11-14 10:39:19 +11001293 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001294 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001295 euid != pcred->suid && euid != pcred->uid &&
1296 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001297 ret = -EPERM;
1298 goto out_unlock;
1299 }
David Quigley8f95dc52006-06-30 01:55:47 -07001300 ret = security_task_kill(p, info, sig, secid);
1301 if (ret)
1302 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001303
1304 if (sig) {
1305 if (lock_task_sighand(p, &flags)) {
1306 ret = __send_signal(sig, info, p, 1, 0);
1307 unlock_task_sighand(p, &flags);
1308 } else
1309 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001310 }
1311out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001312 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001313 return ret;
1314}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001315EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317/*
1318 * kill_something_info() interprets pid in interesting ways just like kill(2).
1319 *
1320 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1321 * is probably wrong. Should make it like BSD or SYSV.
1322 */
1323
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001324static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001326 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001327
1328 if (pid > 0) {
1329 rcu_read_lock();
1330 ret = kill_pid_info(sig, info, find_vpid(pid));
1331 rcu_read_unlock();
1332 return ret;
1333 }
1334
1335 read_lock(&tasklist_lock);
1336 if (pid != -1) {
1337 ret = __kill_pgrp_info(sig, info,
1338 pid ? find_vpid(-pid) : task_pgrp(current));
1339 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 int retval = 0, count = 0;
1341 struct task_struct * p;
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001344 if (task_pid_vnr(p) > 1 &&
1345 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int err = group_send_sig_info(sig, info, p);
1347 ++count;
1348 if (err != -EPERM)
1349 retval = err;
1350 }
1351 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001352 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001354 read_unlock(&tasklist_lock);
1355
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001356 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
1359/*
1360 * These are for backward compatibility with the rest of the kernel source.
1361 */
1362
Randy Dunlap5aba0852011-04-04 14:59:31 -07001363int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 /*
1366 * Make sure legacy kernel users don't send in bad values
1367 * (normal paths check this in check_kill_permission).
1368 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001369 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 return -EINVAL;
1371
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001372 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001375#define __si_special(priv) \
1376 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378int
1379send_sig(int sig, struct task_struct *p, int priv)
1380{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001381 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384void
1385force_sig(int sig, struct task_struct *p)
1386{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001387 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
1390/*
1391 * When things go south during signal handling, we
1392 * will force a SIGSEGV. And if the signal that caused
1393 * the problem was already a SIGSEGV, we'll want to
1394 * make sure we don't even try to deliver the signal..
1395 */
1396int
1397force_sigsegv(int sig, struct task_struct *p)
1398{
1399 if (sig == SIGSEGV) {
1400 unsigned long flags;
1401 spin_lock_irqsave(&p->sighand->siglock, flags);
1402 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1403 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1404 }
1405 force_sig(SIGSEGV, p);
1406 return 0;
1407}
1408
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001409int kill_pgrp(struct pid *pid, int sig, int priv)
1410{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001411 int ret;
1412
1413 read_lock(&tasklist_lock);
1414 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1415 read_unlock(&tasklist_lock);
1416
1417 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001418}
1419EXPORT_SYMBOL(kill_pgrp);
1420
1421int kill_pid(struct pid *pid, int sig, int priv)
1422{
1423 return kill_pid_info(sig, __si_special(priv), pid);
1424}
1425EXPORT_SYMBOL(kill_pid);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
1428 * These functions support sending signals using preallocated sigqueue
1429 * structures. This is needed "because realtime applications cannot
1430 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001431 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 * we allocate the sigqueue structure from the timer_create. If this
1433 * allocation fails we are able to report the failure to the application
1434 * with an EAGAIN error.
1435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436struct sigqueue *sigqueue_alloc(void)
1437{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001438 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001440 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001442
1443 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446void sigqueue_free(struct sigqueue *q)
1447{
1448 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001449 spinlock_t *lock = &current->sighand->siglock;
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1452 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001453 * We must hold ->siglock while testing q->list
1454 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001455 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001457 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001458 q->flags &= ~SIGQUEUE_PREALLOC;
1459 /*
1460 * If it is queued it will be freed when dequeued,
1461 * like the "regular" sigqueue.
1462 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001463 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001464 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001465 spin_unlock_irqrestore(lock, flags);
1466
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001467 if (q)
1468 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001471int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001472{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001473 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001474 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001475 unsigned long flags;
1476 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001477
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001478 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001479
1480 ret = -1;
1481 if (!likely(lock_task_sighand(t, &flags)))
1482 goto ret;
1483
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001484 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001485 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001486 goto out;
1487
1488 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001489 if (unlikely(!list_empty(&q->list))) {
1490 /*
1491 * If an SI_TIMER entry is already queue just increment
1492 * the overrun count.
1493 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001494 BUG_ON(q->info.si_code != SI_TIMER);
1495 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001496 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001497 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001498 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001499
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001500 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001501 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001502 list_add_tail(&q->list, &pending->list);
1503 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001504 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001505out:
1506 unlock_task_sighand(t, &flags);
1507ret:
1508 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 * Let a parent know about the death of a child.
1513 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001514 *
1515 * Returns -1 if our parent ignored us and so we've switched to
1516 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001518int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct siginfo info;
1521 unsigned long flags;
1522 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001523 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 BUG_ON(sig == -1);
1526
1527 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001528 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001530 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1532
1533 info.si_signo = sig;
1534 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001535 /*
1536 * we are under tasklist_lock here so our parent is tied to
1537 * us and cannot exit and release its namespace.
1538 *
1539 * the only it can is to switch its nsproxy with sys_unshare,
1540 * bu uncharing pid namespaces is not allowed, so we'll always
1541 * see relevant namespace
1542 *
1543 * write_lock() currently calls preempt_disable() which is the
1544 * same as rcu_read_lock(), but according to Oleg, this is not
1545 * correct to rely on this
1546 */
1547 rcu_read_lock();
1548 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001549 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001550 rcu_read_unlock();
1551
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001552 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1553 tsk->signal->utime));
1554 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1555 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 info.si_status = tsk->exit_code & 0x7f;
1558 if (tsk->exit_code & 0x80)
1559 info.si_code = CLD_DUMPED;
1560 else if (tsk->exit_code & 0x7f)
1561 info.si_code = CLD_KILLED;
1562 else {
1563 info.si_code = CLD_EXITED;
1564 info.si_status = tsk->exit_code >> 8;
1565 }
1566
1567 psig = tsk->parent->sighand;
1568 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001569 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1571 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1572 /*
1573 * We are exiting and our parent doesn't care. POSIX.1
1574 * defines special semantics for setting SIGCHLD to SIG_IGN
1575 * or setting the SA_NOCLDWAIT flag: we should be reaped
1576 * automatically and not left for our parent's wait4 call.
1577 * Rather than having the parent do it as a magic kind of
1578 * signal handler, we just set this to tell do_exit that we
1579 * can be cleaned up without becoming a zombie. Note that
1580 * we still call __wake_up_parent in this case, because a
1581 * blocked sys_wait4 might now return -ECHILD.
1582 *
1583 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1584 * is implementation-defined: we do (if you don't want
1585 * it, just use SIG_IGN instead).
1586 */
Roland McGrath1b046242008-08-19 20:37:07 -07001587 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001589 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001591 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 __group_send_sig_info(sig, &info, tsk->parent);
1593 __wake_up_parent(tsk, tsk->parent);
1594 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001595
Roland McGrath1b046242008-08-19 20:37:07 -07001596 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Tejun Heo75b95952011-03-23 10:37:01 +01001599/**
1600 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1601 * @tsk: task reporting the state change
1602 * @for_ptracer: the notification is for ptracer
1603 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1604 *
1605 * Notify @tsk's parent that the stopped/continued state has changed. If
1606 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1607 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1608 *
1609 * CONTEXT:
1610 * Must be called with tasklist_lock at least read locked.
1611 */
1612static void do_notify_parent_cldstop(struct task_struct *tsk,
1613 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
1615 struct siginfo info;
1616 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001617 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 struct sighand_struct *sighand;
1619
Tejun Heo75b95952011-03-23 10:37:01 +01001620 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001621 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001622 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001623 tsk = tsk->group_leader;
1624 parent = tsk->real_parent;
1625 }
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 info.si_signo = SIGCHLD;
1628 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001629 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001630 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001631 */
1632 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001633 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001634 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001635 rcu_read_unlock();
1636
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001637 info.si_utime = cputime_to_clock_t(tsk->utime);
1638 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 info.si_code = why;
1641 switch (why) {
1642 case CLD_CONTINUED:
1643 info.si_status = SIGCONT;
1644 break;
1645 case CLD_STOPPED:
1646 info.si_status = tsk->signal->group_exit_code & 0x7f;
1647 break;
1648 case CLD_TRAPPED:
1649 info.si_status = tsk->exit_code & 0x7f;
1650 break;
1651 default:
1652 BUG();
1653 }
1654
1655 sighand = parent->sighand;
1656 spin_lock_irqsave(&sighand->siglock, flags);
1657 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1658 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1659 __group_send_sig_info(SIGCHLD, &info, parent);
1660 /*
1661 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1662 */
1663 __wake_up_parent(tsk, parent);
1664 spin_unlock_irqrestore(&sighand->siglock, flags);
1665}
1666
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001667static inline int may_ptrace_stop(void)
1668{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001669 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001670 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001671 /*
1672 * Are we in the middle of do_coredump?
1673 * If so and our tracer is also part of the coredump stopping
1674 * is a deadlock situation, and pointless because our tracer
1675 * is dead so don't allow us to stop.
1676 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001677 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001678 * is safe to enter schedule().
1679 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001680 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001681 unlikely(current->mm == current->parent->mm))
1682 return 0;
1683
1684 return 1;
1685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001688 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001689 * Called with the siglock held.
1690 */
1691static int sigkill_pending(struct task_struct *tsk)
1692{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001693 return sigismember(&tsk->pending.signal, SIGKILL) ||
1694 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001695}
1696
1697/*
Tejun Heoceb6bd62011-03-23 10:37:01 +01001698 * Test whether the target task of the usual cldstop notification - the
1699 * real_parent of @child - is in the same group as the ptracer.
1700 */
1701static bool real_parent_is_ptracer(struct task_struct *child)
1702{
1703 return same_thread_group(child->parent, child->real_parent);
1704}
1705
1706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * This must be called with current->sighand->siglock held.
1708 *
1709 * This should be the path for all ptrace stops.
1710 * We always set current->last_siginfo while stopped here.
1711 * That makes it a way to test a stopped process for
1712 * being ptrace-stopped vs being job-control-stopped.
1713 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001714 * If we actually decide not to stop at all because the tracer
1715 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001717static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001718 __releases(&current->sighand->siglock)
1719 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001721 bool gstop_done = false;
1722
Roland McGrath1a669c22008-02-06 01:37:37 -08001723 if (arch_ptrace_stop_needed(exit_code, info)) {
1724 /*
1725 * The arch code has something special to do before a
1726 * ptrace stop. This is allowed to block, e.g. for faults
1727 * on user stack pages. We can't keep the siglock while
1728 * calling arch_ptrace_stop, so we must release it now.
1729 * To preserve proper semantics, we must do this before
1730 * any signal bookkeeping like checking group_stop_count.
1731 * Meanwhile, a SIGKILL could come in before we retake the
1732 * siglock. That must prevent us from sleeping in TASK_TRACED.
1733 * So after regaining the lock, we must check for SIGKILL.
1734 */
1735 spin_unlock_irq(&current->sighand->siglock);
1736 arch_ptrace_stop(exit_code, info);
1737 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001738 if (sigkill_pending(current))
1739 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001740 }
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001743 * We're committing to trapping. TRACED should be visible before
1744 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1745 * Also, transition to TRACED and updates to ->jobctl should be
1746 * atomic with respect to siglock and should be done after the arch
1747 * hook as siglock is released and regrabbed across it.
1748 */
1749 set_current_state(TASK_TRACED);
1750
1751 current->last_siginfo = info;
1752 current->exit_code = exit_code;
1753
1754 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001755 * If @why is CLD_STOPPED, we're trapping to participate in a group
1756 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1757 * while siglock was released for the arch hook, PENDING could be
1758 * clear now. We act as if SIGCONT is received after TASK_TRACED
1759 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001761 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001762 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Tejun Heo81be24b2011-06-02 11:13:59 +02001764 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001765 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 spin_unlock_irq(&current->sighand->siglock);
1768 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001769 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001770 /*
1771 * Notify parents of the stop.
1772 *
1773 * While ptraced, there are two parents - the ptracer and
1774 * the real_parent of the group_leader. The ptracer should
1775 * know about every stop while the real parent is only
1776 * interested in the completion of group stop. The states
1777 * for the two don't interact with each other. Notify
1778 * separately unless they're gonna be duplicates.
1779 */
1780 do_notify_parent_cldstop(current, true, why);
1781 if (gstop_done && !real_parent_is_ptracer(current))
1782 do_notify_parent_cldstop(current, false, why);
1783
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001784 /*
1785 * Don't want to allow preemption here, because
1786 * sys_ptrace() needs this task to be inactive.
1787 *
1788 * XXX: implement read_unlock_no_resched().
1789 */
1790 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001792 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 schedule();
1794 } else {
1795 /*
1796 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001797 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001798 *
1799 * If @gstop_done, the ptracer went away between group stop
1800 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001801 * JOBCTL_STOP_PENDING on us and we'll re-enter
1802 * TASK_STOPPED in do_signal_stop() on return, so notifying
1803 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001805 if (gstop_done)
1806 do_notify_parent_cldstop(current, false, why);
1807
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001808 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001809 if (clear_code)
1810 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001811 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
1814 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001815 * While in TASK_TRACED, we were considered "frozen enough".
1816 * Now that we woke up, it's crucial if we're supposed to be
1817 * frozen that we freeze now before running anything substantial.
1818 */
1819 try_to_freeze();
1820
1821 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * We are back. Now reacquire the siglock before touching
1823 * last_siginfo, so that we are sure to have synchronized with
1824 * any signal-sending on another CPU that wants to examine it.
1825 */
1826 spin_lock_irq(&current->sighand->siglock);
1827 current->last_siginfo = NULL;
1828
1829 /*
1830 * Queued signals ignored us while we were stopped for tracing.
1831 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001832 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001834 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
1837void ptrace_notify(int exit_code)
1838{
1839 siginfo_t info;
1840
1841 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1842
1843 memset(&info, 0, sizeof info);
1844 info.si_signo = SIGTRAP;
1845 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001846 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001847 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* Let the debugger run. */
1850 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001851 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 spin_unlock_irq(&current->sighand->siglock);
1853}
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * This performs the stopping for SIGSTOP and other stop signals.
1857 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001858 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * Returns zero if we didn't stop and still hold the siglock.
1860 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001861static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Tejun Heoa8f072c2011-06-02 11:13:59 +02001865 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1866 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001867 struct task_struct *t;
1868
Tejun Heoa8f072c2011-06-02 11:13:59 +02001869 /* signr will be recorded in task->jobctl for retries */
1870 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001871
Tejun Heoa8f072c2011-06-02 11:13:59 +02001872 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001873 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001876 * There is no group stop already in progress. We must
1877 * initiate one now.
1878 *
1879 * While ptraced, a task may be resumed while group stop is
1880 * still in effect and then receive a stop signal and
1881 * initiate another group stop. This deviates from the
1882 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001883 * cause two group stops when !ptraced. That is why we
1884 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001885 *
1886 * The condition can be distinguished by testing whether
1887 * SIGNAL_STOP_STOPPED is already set. Don't generate
1888 * group_exit_code in such case.
1889 *
1890 * This is not necessary for SIGNAL_STOP_CONTINUED because
1891 * an intervening stop signal is required to cause two
1892 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001894 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1895 sig->group_exit_code = signr;
1896 else
1897 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001898
Tejun Heoa8f072c2011-06-02 11:13:59 +02001899 current->jobctl &= ~JOBCTL_STOP_SIGMASK;
1900 current->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001901 sig->group_stop_count = 1;
Tejun Heod79fdd62011-03-23 10:37:00 +01001902 for (t = next_thread(current); t != current;
1903 t = next_thread(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001904 t->jobctl &= ~JOBCTL_STOP_SIGMASK;
Oleg Nesterova122b342006-03-28 16:11:22 -08001905 /*
1906 * Setting state to TASK_STOPPED for a group
1907 * stop is always done with the siglock held,
1908 * so this check has no races.
1909 */
Tejun Heo39efa3e2011-03-23 10:37:00 +01001910 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001911 t->jobctl |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001912 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001913 signal_wake_up(t, 0);
1914 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001915 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001916 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001917retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001918 if (likely(!task_ptrace(current))) {
1919 int notify = 0;
1920
1921 /*
1922 * If there are no other threads in the group, or if there
1923 * is a group stop in progress and we are the last to stop,
1924 * report to the parent.
1925 */
1926 if (task_participate_group_stop(current))
1927 notify = CLD_STOPPED;
1928
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001929 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001930 spin_unlock_irq(&current->sighand->siglock);
1931
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001932 /*
1933 * Notify the parent of the group stop completion. Because
1934 * we're not holding either the siglock or tasklist_lock
1935 * here, ptracer may attach inbetween; however, this is for
1936 * group stop and should always be delivered to the real
1937 * parent of the group leader. The new ptracer will get
1938 * its notification when this task transitions into
1939 * TASK_TRACED.
1940 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001941 if (notify) {
1942 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001943 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001944 read_unlock(&tasklist_lock);
1945 }
1946
1947 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1948 schedule();
1949
1950 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001951 } else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001952 ptrace_stop(current->jobctl & JOBCTL_STOP_SIGMASK,
Tejun Heod79fdd62011-03-23 10:37:00 +01001953 CLD_STOPPED, 0, NULL);
1954 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001955 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001956
1957 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001958 * JOBCTL_STOP_PENDING could be set if another group stop has
Tejun Heod79fdd62011-03-23 10:37:00 +01001959 * started since being woken up or ptrace wants us to transit
1960 * between TASK_STOPPED and TRACED. Retry group stop.
1961 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001962 if (current->jobctl & JOBCTL_STOP_PENDING) {
1963 WARN_ON_ONCE(!(current->jobctl & JOBCTL_STOP_SIGMASK));
Tejun Heod79fdd62011-03-23 10:37:00 +01001964 goto retry;
1965 }
1966
1967 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001968 task_clear_jobctl_trapping(current);
Tejun Heo5224fa32011-03-23 10:37:00 +01001969
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001970 spin_unlock_irq(&current->sighand->siglock);
1971
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001972 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return 1;
1975}
1976
Roland McGrath18c98b62008-04-17 18:44:38 -07001977static int ptrace_signal(int signr, siginfo_t *info,
1978 struct pt_regs *regs, void *cookie)
1979{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001980 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001981 return signr;
1982
1983 ptrace_signal_deliver(regs, cookie);
1984
1985 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001986 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07001987
1988 /* We're back. Did the debugger cancel the sig? */
1989 signr = current->exit_code;
1990 if (signr == 0)
1991 return signr;
1992
1993 current->exit_code = 0;
1994
Randy Dunlap5aba0852011-04-04 14:59:31 -07001995 /*
1996 * Update the siginfo structure if the signal has
1997 * changed. If the debugger wanted something
1998 * specific in the siginfo structure then it should
1999 * have updated *info via PTRACE_SETSIGINFO.
2000 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002001 if (signr != info->si_signo) {
2002 info->si_signo = signr;
2003 info->si_errno = 0;
2004 info->si_code = SI_USER;
2005 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11002006 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002007 }
2008
2009 /* If the (new) signal is now blocked, requeue it. */
2010 if (sigismember(&current->blocked, signr)) {
2011 specific_send_sig_info(signr, info, current);
2012 signr = 0;
2013 }
2014
2015 return signr;
2016}
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2019 struct pt_regs *regs, void *cookie)
2020{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002021 struct sighand_struct *sighand = current->sighand;
2022 struct signal_struct *signal = current->signal;
2023 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002025relock:
2026 /*
2027 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2028 * While in TASK_STOPPED, we were considered "frozen enough".
2029 * Now that we woke up, it's crucial if we're supposed to be
2030 * frozen that we freeze now before running anything substantial.
2031 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002032 try_to_freeze();
2033
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002034 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002035 /*
2036 * Every stopped thread goes here after wakeup. Check to see if
2037 * we should notify the parent, prepare_signal(SIGCONT) encodes
2038 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2039 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002040 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002041 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002042 int why;
2043
2044 if (signal->flags & SIGNAL_CLD_CONTINUED)
2045 why = CLD_CONTINUED;
2046 else
2047 why = CLD_STOPPED;
2048
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002049 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002050
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002051 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002052
Tejun Heoceb6bd62011-03-23 10:37:01 +01002053 /*
2054 * Notify the parent that we're continuing. This event is
2055 * always per-process and doesn't make whole lot of sense
2056 * for ptracers, who shouldn't consume the state via
2057 * wait(2) either, but, for backward compatibility, notify
2058 * the ptracer of the group leader too unless it's gonna be
2059 * a duplicate.
2060 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002061 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002062
2063 do_notify_parent_cldstop(current, false, why);
2064
Tejun Heo75b95952011-03-23 10:37:01 +01002065 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002066 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2067 do_notify_parent_cldstop(leader, true, why);
2068
Tejun Heoedf2ed12011-03-23 10:37:00 +01002069 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002070
Oleg Nesterove4420552008-04-30 00:52:44 -07002071 goto relock;
2072 }
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 for (;;) {
2075 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002076 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002077 * Tracing can induce an artificial signal and choose sigaction.
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002078 * The return value in @signr determines the default action,
2079 * but @info->si_signo is the signal number we will report.
2080 */
2081 signr = tracehook_get_signal(current, regs, info, return_ka);
2082 if (unlikely(signr < 0))
2083 goto relock;
2084 if (unlikely(signr != 0))
2085 ka = return_ka;
2086 else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02002087 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2088 do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002089 goto relock;
2090
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002091 signr = dequeue_signal(current, &current->blocked,
2092 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Roland McGrath18c98b62008-04-17 18:44:38 -07002094 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002095 break; /* will return 0 */
2096
2097 if (signr != SIGKILL) {
2098 signr = ptrace_signal(signr, info,
2099 regs, cookie);
2100 if (!signr)
2101 continue;
2102 }
2103
2104 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
2106
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002107 /* Trace actually delivered signals. */
2108 trace_signal_deliver(signr, info, ka);
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2111 continue;
2112 if (ka->sa.sa_handler != SIG_DFL) {
2113 /* Run the handler. */
2114 *return_ka = *ka;
2115
2116 if (ka->sa.sa_flags & SA_ONESHOT)
2117 ka->sa.sa_handler = SIG_DFL;
2118
2119 break; /* will return non-zero "signr" value */
2120 }
2121
2122 /*
2123 * Now we are doing the default action for this signal.
2124 */
2125 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2126 continue;
2127
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002128 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002129 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002130 * Container-init gets no signals it doesn't want from same
2131 * container.
2132 *
2133 * Note that if global/container-init sees a sig_kernel_only()
2134 * signal here, the signal must have been generated internally
2135 * or must have come from an ancestor namespace. In either
2136 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002137 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002138 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002139 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 continue;
2141
2142 if (sig_kernel_stop(signr)) {
2143 /*
2144 * The default action is to stop all threads in
2145 * the thread group. The job control signals
2146 * do nothing in an orphaned pgrp, but SIGSTOP
2147 * always works. Note that siglock needs to be
2148 * dropped during the call to is_orphaned_pgrp()
2149 * because of lock ordering with tasklist_lock.
2150 * This allows an intervening SIGCONT to be posted.
2151 * We need to check for that and bail out if necessary.
2152 */
2153 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002154 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* signals can be posted during this window */
2157
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002158 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 goto relock;
2160
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002161 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002164 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* It released the siglock. */
2166 goto relock;
2167 }
2168
2169 /*
2170 * We didn't actually stop, due to a race
2171 * with SIGCONT or something like that.
2172 */
2173 continue;
2174 }
2175
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002176 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /*
2179 * Anything else is fatal, maybe with a core dump.
2180 */
2181 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002184 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002185 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /*
2187 * If it was able to dump core, this kills all
2188 * other threads in the group and synchronizes with
2189 * their demise. If we lost the race with another
2190 * thread getting here, it set group_exit_code
2191 * first and our do_group_exit call below will use
2192 * that value and ignore the one we pass it.
2193 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002194 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196
2197 /*
2198 * Death signals, no core dump.
2199 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002200 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* NOTREACHED */
2202 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002203 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return signr;
2205}
2206
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002207/*
2208 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002209 * group-wide signal. Other threads should be notified now to take
2210 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002211 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002212static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002213{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002214 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002215 struct task_struct *t;
2216
Oleg Nesterovf646e222011-04-27 19:18:39 +02002217 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2218 if (sigisemptyset(&retarget))
2219 return;
2220
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002221 t = tsk;
2222 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002223 if (t->flags & PF_EXITING)
2224 continue;
2225
2226 if (!has_pending_signals(&retarget, &t->blocked))
2227 continue;
2228 /* Remove the signals this thread can handle. */
2229 sigandsets(&retarget, &retarget, &t->blocked);
2230
2231 if (!signal_pending(t))
2232 signal_wake_up(t, 0);
2233
2234 if (sigisemptyset(&retarget))
2235 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002236 }
2237}
2238
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002239void exit_signals(struct task_struct *tsk)
2240{
2241 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002242 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002243
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002244 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2245 tsk->flags |= PF_EXITING;
2246 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002247 }
2248
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002249 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002250 /*
2251 * From now this task is not visible for group-wide signals,
2252 * see wants_signal(), do_signal_stop().
2253 */
2254 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002255 if (!signal_pending(tsk))
2256 goto out;
2257
Oleg Nesterovf646e222011-04-27 19:18:39 +02002258 unblocked = tsk->blocked;
2259 signotset(&unblocked);
2260 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002261
Tejun Heoa8f072c2011-06-02 11:13:59 +02002262 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002263 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002264 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002265out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002266 spin_unlock_irq(&tsk->sighand->siglock);
2267
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002268 /*
2269 * If group stop has completed, deliver the notification. This
2270 * should always go to the real parent of the group leader.
2271 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002272 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002273 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002274 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002275 read_unlock(&tasklist_lock);
2276 }
2277}
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279EXPORT_SYMBOL(recalc_sigpending);
2280EXPORT_SYMBOL_GPL(dequeue_signal);
2281EXPORT_SYMBOL(flush_signals);
2282EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283EXPORT_SYMBOL(send_sig);
2284EXPORT_SYMBOL(send_sig_info);
2285EXPORT_SYMBOL(sigprocmask);
2286EXPORT_SYMBOL(block_all_signals);
2287EXPORT_SYMBOL(unblock_all_signals);
2288
2289
2290/*
2291 * System call entry points.
2292 */
2293
Randy Dunlap41c57892011-04-04 15:00:26 -07002294/**
2295 * sys_restart_syscall - restart a system call
2296 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002297SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 struct restart_block *restart = &current_thread_info()->restart_block;
2300 return restart->fn(restart);
2301}
2302
2303long do_no_restart_syscall(struct restart_block *param)
2304{
2305 return -EINTR;
2306}
2307
Oleg Nesterovb1828012011-04-27 21:56:14 +02002308static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2309{
2310 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2311 sigset_t newblocked;
2312 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002313 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002314 retarget_shared_pending(tsk, &newblocked);
2315 }
2316 tsk->blocked = *newset;
2317 recalc_sigpending();
2318}
2319
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002320/**
2321 * set_current_blocked - change current->blocked mask
2322 * @newset: new mask
2323 *
2324 * It is wrong to change ->blocked directly, this helper should be used
2325 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002327void set_current_blocked(const sigset_t *newset)
2328{
2329 struct task_struct *tsk = current;
2330
2331 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002332 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002333 spin_unlock_irq(&tsk->sighand->siglock);
2334}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336/*
2337 * This is also useful for kernel threads that want to temporarily
2338 * (or permanently) block certain signals.
2339 *
2340 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2341 * interface happily blocks "unblockable" signals like SIGKILL
2342 * and friends.
2343 */
2344int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2345{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002346 struct task_struct *tsk = current;
2347 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002349 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002350 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002351 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 switch (how) {
2354 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002355 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 break;
2357 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002358 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
2360 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002361 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 break;
2363 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002366
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002367 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
Randy Dunlap41c57892011-04-04 15:00:26 -07002371/**
2372 * sys_rt_sigprocmask - change the list of currently blocked signals
2373 * @how: whether to add, remove, or set signals
2374 * @set: stores pending signals
2375 * @oset: previous value of signal mask if non-null
2376 * @sigsetsize: size of sigset_t type
2377 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002378SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002379 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002382 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /* XXX: Don't preclude handling different sized sigset_t's. */
2385 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002386 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002388 old_set = current->blocked;
2389
2390 if (nset) {
2391 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2392 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2394
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002395 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002397 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002399
2400 if (oset) {
2401 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2402 return -EFAULT;
2403 }
2404
2405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
2408long do_sigpending(void __user *set, unsigned long sigsetsize)
2409{
2410 long error = -EINVAL;
2411 sigset_t pending;
2412
2413 if (sigsetsize > sizeof(sigset_t))
2414 goto out;
2415
2416 spin_lock_irq(&current->sighand->siglock);
2417 sigorsets(&pending, &current->pending.signal,
2418 &current->signal->shared_pending.signal);
2419 spin_unlock_irq(&current->sighand->siglock);
2420
2421 /* Outside the lock because only this thread touches it. */
2422 sigandsets(&pending, &current->blocked, &pending);
2423
2424 error = -EFAULT;
2425 if (!copy_to_user(set, &pending, sigsetsize))
2426 error = 0;
2427
2428out:
2429 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Randy Dunlap41c57892011-04-04 15:00:26 -07002432/**
2433 * sys_rt_sigpending - examine a pending signal that has been raised
2434 * while blocked
2435 * @set: stores pending signals
2436 * @sigsetsize: size of sigset_t type or larger
2437 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002438SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
2440 return do_sigpending(set, sigsetsize);
2441}
2442
2443#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2444
2445int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2446{
2447 int err;
2448
2449 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2450 return -EFAULT;
2451 if (from->si_code < 0)
2452 return __copy_to_user(to, from, sizeof(siginfo_t))
2453 ? -EFAULT : 0;
2454 /*
2455 * If you change siginfo_t structure, please be sure
2456 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002457 * Please remember to update the signalfd_copyinfo() function
2458 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * It should never copy any pad contained in the structure
2460 * to avoid security leaks, but must copy the generic
2461 * 3 ints plus the relevant union member.
2462 */
2463 err = __put_user(from->si_signo, &to->si_signo);
2464 err |= __put_user(from->si_errno, &to->si_errno);
2465 err |= __put_user((short)from->si_code, &to->si_code);
2466 switch (from->si_code & __SI_MASK) {
2467 case __SI_KILL:
2468 err |= __put_user(from->si_pid, &to->si_pid);
2469 err |= __put_user(from->si_uid, &to->si_uid);
2470 break;
2471 case __SI_TIMER:
2472 err |= __put_user(from->si_tid, &to->si_tid);
2473 err |= __put_user(from->si_overrun, &to->si_overrun);
2474 err |= __put_user(from->si_ptr, &to->si_ptr);
2475 break;
2476 case __SI_POLL:
2477 err |= __put_user(from->si_band, &to->si_band);
2478 err |= __put_user(from->si_fd, &to->si_fd);
2479 break;
2480 case __SI_FAULT:
2481 err |= __put_user(from->si_addr, &to->si_addr);
2482#ifdef __ARCH_SI_TRAPNO
2483 err |= __put_user(from->si_trapno, &to->si_trapno);
2484#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002485#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002486 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002487 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002488 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002489 */
2490 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2491 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2492#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 break;
2494 case __SI_CHLD:
2495 err |= __put_user(from->si_pid, &to->si_pid);
2496 err |= __put_user(from->si_uid, &to->si_uid);
2497 err |= __put_user(from->si_status, &to->si_status);
2498 err |= __put_user(from->si_utime, &to->si_utime);
2499 err |= __put_user(from->si_stime, &to->si_stime);
2500 break;
2501 case __SI_RT: /* This is not generated by the kernel as of now. */
2502 case __SI_MESGQ: /* But this is */
2503 err |= __put_user(from->si_pid, &to->si_pid);
2504 err |= __put_user(from->si_uid, &to->si_uid);
2505 err |= __put_user(from->si_ptr, &to->si_ptr);
2506 break;
2507 default: /* this is just in case for now ... */
2508 err |= __put_user(from->si_pid, &to->si_pid);
2509 err |= __put_user(from->si_uid, &to->si_uid);
2510 break;
2511 }
2512 return err;
2513}
2514
2515#endif
2516
Randy Dunlap41c57892011-04-04 15:00:26 -07002517/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002518 * do_sigtimedwait - wait for queued signals specified in @which
2519 * @which: queued signals to wait for
2520 * @info: if non-null, the signal's siginfo is returned here
2521 * @ts: upper bound on process time suspension
2522 */
2523int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2524 const struct timespec *ts)
2525{
2526 struct task_struct *tsk = current;
2527 long timeout = MAX_SCHEDULE_TIMEOUT;
2528 sigset_t mask = *which;
2529 int sig;
2530
2531 if (ts) {
2532 if (!timespec_valid(ts))
2533 return -EINVAL;
2534 timeout = timespec_to_jiffies(ts);
2535 /*
2536 * We can be close to the next tick, add another one
2537 * to ensure we will wait at least the time asked for.
2538 */
2539 if (ts->tv_sec || ts->tv_nsec)
2540 timeout++;
2541 }
2542
2543 /*
2544 * Invert the set of allowed signals to get those we want to block.
2545 */
2546 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2547 signotset(&mask);
2548
2549 spin_lock_irq(&tsk->sighand->siglock);
2550 sig = dequeue_signal(tsk, &mask, info);
2551 if (!sig && timeout) {
2552 /*
2553 * None ready, temporarily unblock those we're interested
2554 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002555 * they arrive. Unblocking is always fine, we can avoid
2556 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002557 */
2558 tsk->real_blocked = tsk->blocked;
2559 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2560 recalc_sigpending();
2561 spin_unlock_irq(&tsk->sighand->siglock);
2562
2563 timeout = schedule_timeout_interruptible(timeout);
2564
2565 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002566 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002567 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002568 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002569 }
2570 spin_unlock_irq(&tsk->sighand->siglock);
2571
2572 if (sig)
2573 return sig;
2574 return timeout ? -EINTR : -EAGAIN;
2575}
2576
2577/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002578 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2579 * in @uthese
2580 * @uthese: queued signals to wait for
2581 * @uinfo: if non-null, the signal's siginfo is returned here
2582 * @uts: upper bound on process time suspension
2583 * @sigsetsize: size of sigset_t type
2584 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002585SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2586 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2587 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 sigset_t these;
2590 struct timespec ts;
2591 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002592 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
2594 /* XXX: Don't preclude handling different sized sigset_t's. */
2595 if (sigsetsize != sizeof(sigset_t))
2596 return -EINVAL;
2597
2598 if (copy_from_user(&these, uthese, sizeof(these)))
2599 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if (uts) {
2602 if (copy_from_user(&ts, uts, sizeof(ts)))
2603 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
2605
Oleg Nesterov943df142011-04-27 21:44:14 +02002606 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Oleg Nesterov943df142011-04-27 21:44:14 +02002608 if (ret > 0 && uinfo) {
2609 if (copy_siginfo_to_user(uinfo, &info))
2610 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
2612
2613 return ret;
2614}
2615
Randy Dunlap41c57892011-04-04 15:00:26 -07002616/**
2617 * sys_kill - send a signal to a process
2618 * @pid: the PID of the process
2619 * @sig: signal to be sent
2620 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002621SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 struct siginfo info;
2624
2625 info.si_signo = sig;
2626 info.si_errno = 0;
2627 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002628 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002629 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 return kill_something_info(sig, &info, pid);
2632}
2633
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002634static int
2635do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002636{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002637 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002638 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002639
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002640 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002641 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002642 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002643 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002644 /*
2645 * The null signal is a permissions and process existence
2646 * probe. No signal is actually delivered.
2647 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002648 if (!error && sig) {
2649 error = do_send_sig_info(sig, info, p, false);
2650 /*
2651 * If lock_task_sighand() failed we pretend the task
2652 * dies after receiving the signal. The window is tiny,
2653 * and the signal is private anyway.
2654 */
2655 if (unlikely(error == -ESRCH))
2656 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002657 }
2658 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002659 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002660
2661 return error;
2662}
2663
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002664static int do_tkill(pid_t tgid, pid_t pid, int sig)
2665{
2666 struct siginfo info;
2667
2668 info.si_signo = sig;
2669 info.si_errno = 0;
2670 info.si_code = SI_TKILL;
2671 info.si_pid = task_tgid_vnr(current);
2672 info.si_uid = current_uid();
2673
2674 return do_send_specific(tgid, pid, sig, &info);
2675}
2676
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677/**
2678 * sys_tgkill - send signal to one specific thread
2679 * @tgid: the thread group ID of the thread
2680 * @pid: the PID of the thread
2681 * @sig: signal to be sent
2682 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002683 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 * exists but it's not belonging to the target process anymore. This
2685 * method solves the problem of threads exiting and PIDs getting reused.
2686 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002687SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /* This is only valid for single tasks */
2690 if (pid <= 0 || tgid <= 0)
2691 return -EINVAL;
2692
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002693 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Randy Dunlap41c57892011-04-04 15:00:26 -07002696/**
2697 * sys_tkill - send signal to one specific task
2698 * @pid: the PID of the task
2699 * @sig: signal to be sent
2700 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2702 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002703SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /* This is only valid for single tasks */
2706 if (pid <= 0)
2707 return -EINVAL;
2708
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002709 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Randy Dunlap41c57892011-04-04 15:00:26 -07002712/**
2713 * sys_rt_sigqueueinfo - send signal information to a signal
2714 * @pid: the PID of the thread
2715 * @sig: signal to be sent
2716 * @uinfo: signal info to be sent
2717 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002718SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2719 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 siginfo_t info;
2722
2723 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2724 return -EFAULT;
2725
2726 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002727 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2728 */
Roland Dreier243b4222011-03-28 14:13:35 -07002729 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002730 /* We used to allow any < 0 si_code */
2731 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 info.si_signo = sig;
2735
2736 /* POSIX.1b doesn't mention process groups. */
2737 return kill_proc_info(sig, &info, pid);
2738}
2739
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002740long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2741{
2742 /* This is only valid for single tasks */
2743 if (pid <= 0 || tgid <= 0)
2744 return -EINVAL;
2745
2746 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002747 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2748 */
Roland Dreier243b4222011-03-28 14:13:35 -07002749 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002750 /* We used to allow any < 0 si_code */
2751 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002752 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002753 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002754 info->si_signo = sig;
2755
2756 return do_send_specific(tgid, pid, sig, info);
2757}
2758
2759SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2760 siginfo_t __user *, uinfo)
2761{
2762 siginfo_t info;
2763
2764 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2765 return -EFAULT;
2766
2767 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2768}
2769
Oleg Nesterov88531f72006-03-28 16:11:24 -08002770int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002772 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002774 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002776 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return -EINVAL;
2778
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002779 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (oact)
2783 *oact = *k;
2784
2785 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002786 sigdelsetmask(&act->sa.sa_mask,
2787 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002788 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * POSIX 3.3.1.3:
2791 * "Setting a signal action to SIG_IGN for a signal that is
2792 * pending shall cause the pending signal to be discarded,
2793 * whether or not it is blocked."
2794 *
2795 * "Setting a signal action to SIG_DFL for a signal that is
2796 * pending and whose default action is to ignore the signal
2797 * (for example, SIGCHLD), shall cause the pending signal to
2798 * be discarded, whether or not it is blocked"
2799 */
Roland McGrath35de2542008-07-25 19:45:51 -07002800 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002801 sigemptyset(&mask);
2802 sigaddset(&mask, sig);
2803 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002805 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 t = next_thread(t);
2807 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 }
2810
2811 spin_unlock_irq(&current->sighand->siglock);
2812 return 0;
2813}
2814
2815int
2816do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2817{
2818 stack_t oss;
2819 int error;
2820
Linus Torvalds0083fc22009-08-01 10:34:56 -07002821 oss.ss_sp = (void __user *) current->sas_ss_sp;
2822 oss.ss_size = current->sas_ss_size;
2823 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 if (uss) {
2826 void __user *ss_sp;
2827 size_t ss_size;
2828 int ss_flags;
2829
2830 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002831 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2832 goto out;
2833 error = __get_user(ss_sp, &uss->ss_sp) |
2834 __get_user(ss_flags, &uss->ss_flags) |
2835 __get_user(ss_size, &uss->ss_size);
2836 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 goto out;
2838
2839 error = -EPERM;
2840 if (on_sig_stack(sp))
2841 goto out;
2842
2843 error = -EINVAL;
2844 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002845 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 * old code may have been written using ss_flags==0
2847 * to mean ss_flags==SS_ONSTACK (as this was the only
2848 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002849 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 */
2851 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2852 goto out;
2853
2854 if (ss_flags == SS_DISABLE) {
2855 ss_size = 0;
2856 ss_sp = NULL;
2857 } else {
2858 error = -ENOMEM;
2859 if (ss_size < MINSIGSTKSZ)
2860 goto out;
2861 }
2862
2863 current->sas_ss_sp = (unsigned long) ss_sp;
2864 current->sas_ss_size = ss_size;
2865 }
2866
Linus Torvalds0083fc22009-08-01 10:34:56 -07002867 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (uoss) {
2869 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002870 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002872 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2873 __put_user(oss.ss_size, &uoss->ss_size) |
2874 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877out:
2878 return error;
2879}
2880
2881#ifdef __ARCH_WANT_SYS_SIGPENDING
2882
Randy Dunlap41c57892011-04-04 15:00:26 -07002883/**
2884 * sys_sigpending - examine pending signals
2885 * @set: where mask of pending signal is returned
2886 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002887SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 return do_sigpending(set, sizeof(*set));
2890}
2891
2892#endif
2893
2894#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002895/**
2896 * sys_sigprocmask - examine and change blocked signals
2897 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02002898 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07002899 * @oset: previous value of signal mask if non-null
2900 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002901 * Some platforms have their own version with special arguments;
2902 * others support only sys_rt_sigprocmask.
2903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Oleg Nesterovb013c392011-04-28 11:36:20 +02002905SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002906 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002909 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Oleg Nesterovb013c392011-04-28 11:36:20 +02002911 old_set = current->blocked.sig[0];
2912
2913 if (nset) {
2914 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2915 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2917
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002918 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002922 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 break;
2924 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002925 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 break;
2927 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002928 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002930 default:
2931 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
2933
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002934 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02002936
2937 if (oset) {
2938 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2939 return -EFAULT;
2940 }
2941
2942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943}
2944#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2945
2946#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002947/**
2948 * sys_rt_sigaction - alter an action taken by a process
2949 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07002950 * @act: new sigaction
2951 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07002952 * @sigsetsize: size of sigset_t type
2953 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002954SYSCALL_DEFINE4(rt_sigaction, int, sig,
2955 const struct sigaction __user *, act,
2956 struct sigaction __user *, oact,
2957 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 struct k_sigaction new_sa, old_sa;
2960 int ret = -EINVAL;
2961
2962 /* XXX: Don't preclude handling different sized sigset_t's. */
2963 if (sigsetsize != sizeof(sigset_t))
2964 goto out;
2965
2966 if (act) {
2967 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2968 return -EFAULT;
2969 }
2970
2971 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2972
2973 if (!ret && oact) {
2974 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2975 return -EFAULT;
2976 }
2977out:
2978 return ret;
2979}
2980#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2981
2982#ifdef __ARCH_WANT_SYS_SGETMASK
2983
2984/*
2985 * For backwards compatibility. Functionality superseded by sigprocmask.
2986 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002987SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 /* SMP safe */
2990 return current->blocked.sig[0];
2991}
2992
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002993SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 int old;
2996
2997 spin_lock_irq(&current->sighand->siglock);
2998 old = current->blocked.sig[0];
2999
3000 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
3001 sigmask(SIGSTOP)));
3002 recalc_sigpending();
3003 spin_unlock_irq(&current->sighand->siglock);
3004
3005 return old;
3006}
3007#endif /* __ARCH_WANT_SGETMASK */
3008
3009#ifdef __ARCH_WANT_SYS_SIGNAL
3010/*
3011 * For backwards compatibility. Functionality superseded by sigaction.
3012 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003013SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 struct k_sigaction new_sa, old_sa;
3016 int ret;
3017
3018 new_sa.sa.sa_handler = handler;
3019 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03003020 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 ret = do_sigaction(sig, &new_sa, &old_sa);
3023
3024 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3025}
3026#endif /* __ARCH_WANT_SYS_SIGNAL */
3027
3028#ifdef __ARCH_WANT_SYS_PAUSE
3029
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003030SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003032 while (!signal_pending(current)) {
3033 current->state = TASK_INTERRUPTIBLE;
3034 schedule();
3035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 return -ERESTARTNOHAND;
3037}
3038
3039#endif
3040
David Woodhouse150256d2006-01-18 17:43:57 -08003041#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003042/**
3043 * sys_rt_sigsuspend - replace the signal mask for a value with the
3044 * @unewset value until a signal is received
3045 * @unewset: new signal mask value
3046 * @sigsetsize: size of sigset_t type
3047 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003048SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003049{
3050 sigset_t newset;
3051
3052 /* XXX: Don't preclude handling different sized sigset_t's. */
3053 if (sigsetsize != sizeof(sigset_t))
3054 return -EINVAL;
3055
3056 if (copy_from_user(&newset, unewset, sizeof(newset)))
3057 return -EFAULT;
3058 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3059
3060 spin_lock_irq(&current->sighand->siglock);
3061 current->saved_sigmask = current->blocked;
3062 current->blocked = newset;
3063 recalc_sigpending();
3064 spin_unlock_irq(&current->sighand->siglock);
3065
3066 current->state = TASK_INTERRUPTIBLE;
3067 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003068 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003069 return -ERESTARTNOHAND;
3070}
3071#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3072
David Howellsf269fdd2006-09-27 01:50:23 -07003073__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3074{
3075 return NULL;
3076}
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078void __init signals_init(void)
3079{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003080 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003082
3083#ifdef CONFIG_KGDB_KDB
3084#include <linux/kdb.h>
3085/*
3086 * kdb_send_sig_info - Allows kdb to send signals without exposing
3087 * signal internals. This function checks if the required locks are
3088 * available before calling the main signal code, to avoid kdb
3089 * deadlocks.
3090 */
3091void
3092kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3093{
3094 static struct task_struct *kdb_prev_t;
3095 int sig, new_t;
3096 if (!spin_trylock(&t->sighand->siglock)) {
3097 kdb_printf("Can't do kill command now.\n"
3098 "The sigmask lock is held somewhere else in "
3099 "kernel, try again later\n");
3100 return;
3101 }
3102 spin_unlock(&t->sighand->siglock);
3103 new_t = kdb_prev_t != t;
3104 kdb_prev_t = t;
3105 if (t->state != TASK_RUNNING && new_t) {
3106 kdb_printf("Process is not RUNNING, sending a signal from "
3107 "kdb risks deadlock\n"
3108 "on the run queue locks. "
3109 "The signal has _not_ been sent.\n"
3110 "Reissue the kill command if you want to risk "
3111 "the deadlock.\n");
3112 return;
3113 }
3114 sig = info->si_signo;
3115 if (send_sig_info(sig, info, t))
3116 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3117 sig, t->pid);
3118 else
3119 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3120}
3121#endif /* CONFIG_KGDB_KDB */