blob: bb9200070ea07458775207b9962cd420f034a193 [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 Heo39efa3e2011-03-23 10:37:00 +0100127 if ((t->group_stop & GROUP_STOP_PENDING) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700129 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700131 return 1;
132 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700133 /*
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
137 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700138 return 0;
139}
140
141/*
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 */
145void recalc_sigpending_and_wake(struct task_struct *t)
146{
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void recalc_sigpending(void)
152{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700156 clear_thread_flag(TIF_SIGPENDING);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/* Given the mask, find the first available signal that should be serviced. */
161
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800162#define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
165
Davide Libenzifba2afa2007-05-10 22:23:13 -0700166int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned long i, *s, *m, x;
169 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 s = pending->signal.sig;
172 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800173
174 /*
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
177 */
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 switch (_NSIG_WORDS) {
187 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
200 break;
201 sig = ffz(~x) + _NSIG_BPW + 1;
202 break;
203
204 case 1:
205 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return sig;
210}
211
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900212static inline void print_dropped_signal(int sig)
213{
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
215
216 if (!print_fatal_signals)
217 return;
218
219 if (!__ratelimit(&ratelimit_state))
220 return;
221
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
224}
225
Tejun Heoe5c19022011-03-23 10:37:00 +0100226/**
Tejun Heod79fdd62011-03-23 10:37:00 +0100227 * task_clear_group_stop_trapping - clear group stop trapping bit
228 * @task: target task
229 *
230 * If GROUP_STOP_TRAPPING is set, a ptracer is waiting for us. Clear it
231 * and wake up the ptracer. Note that we don't need any further locking.
232 * @task->siglock guarantees that @task->parent points to the ptracer.
233 *
234 * CONTEXT:
235 * Must be called with @task->sighand->siglock held.
236 */
237static void task_clear_group_stop_trapping(struct task_struct *task)
238{
239 if (unlikely(task->group_stop & GROUP_STOP_TRAPPING)) {
240 task->group_stop &= ~GROUP_STOP_TRAPPING;
241 __wake_up_sync(&task->parent->signal->wait_chldexit,
242 TASK_UNINTERRUPTIBLE, 1);
243 }
244}
245
246/**
Tejun Heoe5c19022011-03-23 10:37:00 +0100247 * task_clear_group_stop_pending - clear pending group stop
248 * @task: target task
249 *
250 * Clear group stop states for @task.
251 *
252 * CONTEXT:
253 * Must be called with @task->sighand->siglock held.
254 */
Tejun Heo39efa3e2011-03-23 10:37:00 +0100255void task_clear_group_stop_pending(struct task_struct *task)
Tejun Heoe5c19022011-03-23 10:37:00 +0100256{
Oleg Nesterovee77f072011-04-01 20:12:38 +0200257 task->group_stop &= ~(GROUP_STOP_PENDING | GROUP_STOP_CONSUME |
258 GROUP_STOP_DEQUEUED);
Tejun Heoe5c19022011-03-23 10:37:00 +0100259}
260
261/**
262 * task_participate_group_stop - participate in a group stop
263 * @task: task participating in a group stop
264 *
Tejun Heo39efa3e2011-03-23 10:37:00 +0100265 * @task has GROUP_STOP_PENDING set and is participating in a group stop.
266 * Group stop states are cleared and the group stop count is consumed if
267 * %GROUP_STOP_CONSUME was set. If the consumption completes the group
268 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100269 *
270 * CONTEXT:
271 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100272 *
273 * RETURNS:
274 * %true if group stop completion should be notified to the parent, %false
275 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100276 */
277static bool task_participate_group_stop(struct task_struct *task)
278{
279 struct signal_struct *sig = task->signal;
280 bool consume = task->group_stop & GROUP_STOP_CONSUME;
281
Tejun Heo39efa3e2011-03-23 10:37:00 +0100282 WARN_ON_ONCE(!(task->group_stop & GROUP_STOP_PENDING));
283
Tejun Heoe5c19022011-03-23 10:37:00 +0100284 task_clear_group_stop_pending(task);
285
286 if (!consume)
287 return false;
288
289 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
290 sig->group_stop_count--;
291
Tejun Heo244056f2011-03-23 10:37:01 +0100292 /*
293 * Tell the caller to notify completion iff we are entering into a
294 * fresh group stop. Read comment in do_signal_stop() for details.
295 */
296 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100297 sig->flags = SIGNAL_STOP_STOPPED;
298 return true;
299 }
300 return false;
301}
302
David Howellsc69e8d92008-11-14 10:39:19 +1100303/*
304 * allocate a new signal queue record
305 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700306 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100307 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900308static struct sigqueue *
309__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800312 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800314 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000315 * Protect access to @t credentials. This can go away when all
316 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800317 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000318 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100319 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800320 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000321 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800324 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800325 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900327 } else {
328 print_dropped_signal(sig);
329 }
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800332 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100333 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 } else {
335 INIT_LIST_HEAD(&q->list);
336 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100337 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
David Howellsd84f4f92008-11-14 10:39:23 +1100339
340 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Andrew Morton514a01b2006-02-03 03:04:41 -0800343static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 if (q->flags & SIGQUEUE_PREALLOC)
346 return;
347 atomic_dec(&q->user->sigpending);
348 free_uid(q->user);
349 kmem_cache_free(sigqueue_cachep, q);
350}
351
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800352void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct sigqueue *q;
355
356 sigemptyset(&queue->signal);
357 while (!list_empty(&queue->list)) {
358 q = list_entry(queue->list.next, struct sigqueue , list);
359 list_del_init(&q->list);
360 __sigqueue_free(q);
361 }
362}
363
364/*
365 * Flush all pending signals for a task.
366 */
David Howells3bcac022009-04-29 13:45:05 +0100367void __flush_signals(struct task_struct *t)
368{
369 clear_tsk_thread_flag(t, TIF_SIGPENDING);
370 flush_sigqueue(&t->pending);
371 flush_sigqueue(&t->signal->shared_pending);
372}
373
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800374void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 unsigned long flags;
377
378 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100379 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock_irqrestore(&t->sighand->siglock, flags);
381}
382
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400383static void __flush_itimer_signals(struct sigpending *pending)
384{
385 sigset_t signal, retain;
386 struct sigqueue *q, *n;
387
388 signal = pending->signal;
389 sigemptyset(&retain);
390
391 list_for_each_entry_safe(q, n, &pending->list, list) {
392 int sig = q->info.si_signo;
393
394 if (likely(q->info.si_code != SI_TIMER)) {
395 sigaddset(&retain, sig);
396 } else {
397 sigdelset(&signal, sig);
398 list_del_init(&q->list);
399 __sigqueue_free(q);
400 }
401 }
402
403 sigorsets(&pending->signal, &signal, &retain);
404}
405
406void flush_itimer_signals(void)
407{
408 struct task_struct *tsk = current;
409 unsigned long flags;
410
411 spin_lock_irqsave(&tsk->sighand->siglock, flags);
412 __flush_itimer_signals(&tsk->pending);
413 __flush_itimer_signals(&tsk->signal->shared_pending);
414 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
415}
416
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700417void ignore_signals(struct task_struct *t)
418{
419 int i;
420
421 for (i = 0; i < _NSIG; ++i)
422 t->sighand->action[i].sa.sa_handler = SIG_IGN;
423
424 flush_signals(t);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 * Flush all handlers for a task.
429 */
430
431void
432flush_signal_handlers(struct task_struct *t, int force_default)
433{
434 int i;
435 struct k_sigaction *ka = &t->sighand->action[0];
436 for (i = _NSIG ; i != 0 ; i--) {
437 if (force_default || ka->sa.sa_handler != SIG_IGN)
438 ka->sa.sa_handler = SIG_DFL;
439 ka->sa.sa_flags = 0;
440 sigemptyset(&ka->sa.sa_mask);
441 ka++;
442 }
443}
444
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200445int unhandled_signal(struct task_struct *tsk, int sig)
446{
Roland McGrath445a91d2008-07-25 19:45:52 -0700447 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700448 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200449 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700450 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200451 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700452 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200453}
454
Randy Dunlap5aba0852011-04-04 14:59:31 -0700455/*
456 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * process, and wants to be notified if any signals at all were to be
458 * sent/acted upon. If the notifier routine returns non-zero, then the
459 * signal will be acted upon after all. If the notifier routine returns 0,
460 * then then signal will be blocked. Only one block per process is
461 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700462 * can use to determine if the signal should be blocked or not.
463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464void
465block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
466{
467 unsigned long flags;
468
469 spin_lock_irqsave(&current->sighand->siglock, flags);
470 current->notifier_mask = mask;
471 current->notifier_data = priv;
472 current->notifier = notifier;
473 spin_unlock_irqrestore(&current->sighand->siglock, flags);
474}
475
476/* Notify the system that blocking has ended. */
477
478void
479unblock_all_signals(void)
480{
481 unsigned long flags;
482
483 spin_lock_irqsave(&current->sighand->siglock, flags);
484 current->notifier = NULL;
485 current->notifier_data = NULL;
486 recalc_sigpending();
487 spin_unlock_irqrestore(&current->sighand->siglock, flags);
488}
489
Oleg Nesterov100360f2008-07-25 01:47:29 -0700490static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * Collect the siginfo appropriate to this signal. Check if
496 * there is another siginfo for the same signal.
497 */
498 list_for_each_entry(q, &list->list, list) {
499 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700500 if (first)
501 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 first = q;
503 }
504 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700505
506 sigdelset(&list->signal, sig);
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700509still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 list_del_init(&first->list);
511 copy_siginfo(info, &first->info);
512 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700514 /*
515 * Ok, it wasn't in the queue. This must be
516 * a fast-pathed signal or we must have been
517 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 info->si_signo = sig;
520 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800521 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 info->si_pid = 0;
523 info->si_uid = 0;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
528 siginfo_t *info)
529{
Roland McGrath27d91e02006-09-29 02:00:31 -0700530 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (sig) {
533 if (current->notifier) {
534 if (sigismember(current->notifier_mask, sig)) {
535 if (!(current->notifier)(current->notifier_data)) {
536 clear_thread_flag(TIF_SIGPENDING);
537 return 0;
538 }
539 }
540 }
541
Oleg Nesterov100360f2008-07-25 01:47:29 -0700542 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 return sig;
546}
547
548/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700549 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * expected to free it.
551 *
552 * All callers have to hold the siglock.
553 */
554int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
555{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700556 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000557
558 /* We only dequeue private signals from ourselves, we don't let
559 * signalfd steal them
560 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700561 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800562 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 signr = __dequeue_signal(&tsk->signal->shared_pending,
564 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800565 /*
566 * itimer signal ?
567 *
568 * itimers are process shared and we restart periodic
569 * itimers in the signal delivery path to prevent DoS
570 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700571 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800572 * itimers, as the SIGALRM is a legacy signal and only
573 * queued once. Changing the restart behaviour to
574 * restart the timer in the signal dequeue path is
575 * reducing the timer noise on heavy loaded !highres
576 * systems too.
577 */
578 if (unlikely(signr == SIGALRM)) {
579 struct hrtimer *tmr = &tsk->signal->real_timer;
580
581 if (!hrtimer_is_queued(tmr) &&
582 tsk->signal->it_real_incr.tv64 != 0) {
583 hrtimer_forward(tmr, tmr->base->get_time(),
584 tsk->signal->it_real_incr);
585 hrtimer_restart(tmr);
586 }
587 }
588 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700589
Davide Libenzib8fceee2007-09-20 12:40:16 -0700590 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700591 if (!signr)
592 return 0;
593
594 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800595 /*
596 * Set a marker that we have dequeued a stop signal. Our
597 * caller might release the siglock and then the pending
598 * stop signal it is about to process is no longer in the
599 * pending bitmasks, but must still be cleared by a SIGCONT
600 * (and overruled by a SIGKILL). So those cases clear this
601 * shared flag after we've set it. Note that this flag may
602 * remain set after the signal we return is ignored or
603 * handled. That doesn't matter because its only purpose
604 * is to alert stop-signal processing code when another
605 * processor has come along and cleared the flag.
606 */
Oleg Nesterovee77f072011-04-01 20:12:38 +0200607 current->group_stop |= GROUP_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800608 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700609 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /*
611 * Release the siglock to ensure proper locking order
612 * of timer locks outside of siglocks. Note, we leave
613 * irqs disabled here, since the posix-timers code is
614 * about to disable them again anyway.
615 */
616 spin_unlock(&tsk->sighand->siglock);
617 do_schedule_next_timer(info);
618 spin_lock(&tsk->sighand->siglock);
619 }
620 return signr;
621}
622
623/*
624 * Tell a process that it has a new active signal..
625 *
626 * NOTE! we rely on the previous spin_lock to
627 * lock interrupts for us! We can only be called with
628 * "siglock" held, and the local interrupt must
629 * have been disabled when that got acquired!
630 *
631 * No need to set need_resched since signal event passing
632 * goes through ->blocked
633 */
634void signal_wake_up(struct task_struct *t, int resume)
635{
636 unsigned int mask;
637
638 set_tsk_thread_flag(t, TIF_SIGPENDING);
639
640 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500641 * For SIGKILL, we want to wake it up in the stopped/traced/killable
642 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * executing another processor and just now entering stopped state.
644 * By using wake_up_state, we ensure the process will wake up and
645 * handle its death signal.
646 */
647 mask = TASK_INTERRUPTIBLE;
648 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500649 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!wake_up_state(t, mask))
651 kick_process(t);
652}
653
654/*
655 * Remove signals in mask from the pending set and queue.
656 * Returns 1 if any signals were found.
657 *
658 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800659 *
660 * This version takes a sigset mask and looks at all signals,
661 * not just those in the first mask word.
662 */
663static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
664{
665 struct sigqueue *q, *n;
666 sigset_t m;
667
668 sigandsets(&m, mask, &s->signal);
669 if (sigisemptyset(&m))
670 return 0;
671
672 signandsets(&s->signal, &s->signal, mask);
673 list_for_each_entry_safe(q, n, &s->list, list) {
674 if (sigismember(mask, q->info.si_signo)) {
675 list_del_init(&q->list);
676 __sigqueue_free(q);
677 }
678 }
679 return 1;
680}
681/*
682 * Remove signals in mask from the pending set and queue.
683 * Returns 1 if any signals were found.
684 *
685 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
687static int rm_from_queue(unsigned long mask, struct sigpending *s)
688{
689 struct sigqueue *q, *n;
690
691 if (!sigtestsetmask(&s->signal, mask))
692 return 0;
693
694 sigdelsetmask(&s->signal, mask);
695 list_for_each_entry_safe(q, n, &s->list, list) {
696 if (q->info.si_signo < SIGRTMIN &&
697 (mask & sigmask(q->info.si_signo))) {
698 list_del_init(&q->list);
699 __sigqueue_free(q);
700 }
701 }
702 return 1;
703}
704
Oleg Nesterov614c5172009-12-15 16:47:22 -0800705static inline int is_si_special(const struct siginfo *info)
706{
707 return info <= SEND_SIG_FORCED;
708}
709
710static inline bool si_fromuser(const struct siginfo *info)
711{
712 return info == SEND_SIG_NOINFO ||
713 (!is_si_special(info) && SI_FROMUSER(info));
714}
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700717 * called with RCU read lock from check_kill_permission()
718 */
719static int kill_ok_by_cred(struct task_struct *t)
720{
721 const struct cred *cred = current_cred();
722 const struct cred *tcred = __task_cred(t);
723
724 if (cred->user->user_ns == tcred->user->user_ns &&
725 (cred->euid == tcred->suid ||
726 cred->euid == tcred->uid ||
727 cred->uid == tcred->suid ||
728 cred->uid == tcred->uid))
729 return 1;
730
731 if (ns_capable(tcred->user->user_ns, CAP_KILL))
732 return 1;
733
734 return 0;
735}
736
737/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100739 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
741static int check_kill_permission(int sig, struct siginfo *info,
742 struct task_struct *t)
743{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700744 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700745 int error;
746
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700747 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700748 return -EINVAL;
749
Oleg Nesterov614c5172009-12-15 16:47:22 -0800750 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700751 return 0;
752
753 error = audit_signal_info(sig, t); /* Let audit system see the signal */
754 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400756
Oleg Nesterov065add32010-05-26 14:42:54 -0700757 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700758 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700759 switch (sig) {
760 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700761 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700762 /*
763 * We don't return the error if sid == NULL. The
764 * task was unhashed, the caller must notice this.
765 */
766 if (!sid || sid == task_session(current))
767 break;
768 default:
769 return -EPERM;
770 }
771 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100772
Amy Griffise54dc242007-03-29 18:01:04 -0400773 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700777 * Handle magic process-wide effects of stop/continue signals. Unlike
778 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 * time regardless of blocking, ignoring, or handling. This does the
780 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700781 * signals. The process stop is done as a signal action for SIG_DFL.
782 *
783 * Returns true if the signal should be actually delivered, otherwise
784 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700786static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700788 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct task_struct *t;
790
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700791 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700793 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700795 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 /*
797 * This is a stop signal. Remove SIGCONT from all queues.
798 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700799 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 t = p;
801 do {
802 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700803 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700805 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200807 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700809 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 t = p;
811 do {
Tejun Heo39efa3e2011-03-23 10:37:00 +0100812 task_clear_group_stop_pending(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Oleg Nesterov1deac632011-04-01 20:11:50 +0200814 wake_up_state(t, __TASK_STOPPED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700815 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700817 /*
818 * Notify the parent with CLD_CONTINUED if we were stopped.
819 *
820 * If we were in the middle of a group stop, we pretend it
821 * was already finished, and then continued. Since SIGCHLD
822 * doesn't queue we report only CLD_STOPPED, as if the next
823 * CLD_CONTINUED was dropped.
824 */
825 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700826 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700827 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700828 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700829 why |= SIGNAL_CLD_STOPPED;
830
831 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700832 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700833 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700834 * will take ->siglock, notice SIGNAL_CLD_MASK, and
835 * notify its parent. See get_signal_to_deliver().
836 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700837 signal->flags = why | SIGNAL_STOP_CONTINUED;
838 signal->group_stop_count = 0;
839 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700842
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700843 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700846/*
847 * Test if P wants to take SIG. After we've checked all threads with this,
848 * it's equivalent to finding no threads not blocking SIG. Any threads not
849 * blocking SIG were ruled out because they are not running and already
850 * have pending signals. Such threads will dequeue from the shared queue
851 * as soon as they're available, so putting the signal on the shared queue
852 * will be equivalent to sending it to one such thread.
853 */
854static inline int wants_signal(int sig, struct task_struct *p)
855{
856 if (sigismember(&p->blocked, sig))
857 return 0;
858 if (p->flags & PF_EXITING)
859 return 0;
860 if (sig == SIGKILL)
861 return 1;
862 if (task_is_stopped_or_traced(p))
863 return 0;
864 return task_curr(p) || !signal_pending(p);
865}
866
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700867static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700868{
869 struct signal_struct *signal = p->signal;
870 struct task_struct *t;
871
872 /*
873 * Now find a thread we can wake up to take the signal off the queue.
874 *
875 * If the main thread wants the signal, it gets first crack.
876 * Probably the least surprising to the average bear.
877 */
878 if (wants_signal(sig, p))
879 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700880 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700881 /*
882 * There is just one thread and it does not need to be woken.
883 * It will dequeue unblocked signals before it runs again.
884 */
885 return;
886 else {
887 /*
888 * Otherwise try to find a suitable thread.
889 */
890 t = signal->curr_target;
891 while (!wants_signal(sig, t)) {
892 t = next_thread(t);
893 if (t == signal->curr_target)
894 /*
895 * No thread needs to be woken.
896 * Any eligible threads will see
897 * the signal in the queue soon.
898 */
899 return;
900 }
901 signal->curr_target = t;
902 }
903
904 /*
905 * Found a killable thread. If the signal will be fatal,
906 * then start taking the whole group down immediately.
907 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700908 if (sig_fatal(p, sig) &&
909 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700910 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700911 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700912 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700913 /*
914 * This signal will be fatal to the whole group.
915 */
916 if (!sig_kernel_coredump(sig)) {
917 /*
918 * Start a group exit and wake everybody up.
919 * This way we don't have other threads
920 * running and doing things after a slower
921 * thread has the fatal signal pending.
922 */
923 signal->flags = SIGNAL_GROUP_EXIT;
924 signal->group_exit_code = sig;
925 signal->group_stop_count = 0;
926 t = p;
927 do {
Tejun Heo39efa3e2011-03-23 10:37:00 +0100928 task_clear_group_stop_pending(t);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700929 sigaddset(&t->pending.signal, SIGKILL);
930 signal_wake_up(t, 1);
931 } while_each_thread(p, t);
932 return;
933 }
934 }
935
936 /*
937 * The signal is already in the shared-pending queue.
938 * Tell the chosen thread to wake up and dequeue it.
939 */
940 signal_wake_up(t, sig == SIGKILL);
941 return;
942}
943
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700944static inline int legacy_queue(struct sigpending *signals, int sig)
945{
946 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
947}
948
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700949static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
950 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700952 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700953 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200954 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500956 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400957
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700958 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700959
960 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700961 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700962
963 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700965 * Short-circuit ignored signals and support queuing
966 * exactly one non-rt signal, so that we can get more
967 * detailed information about the cause of the signal.
968 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700969 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700970 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700971 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * fast-pathed signals for kernel-internal things like SIGSTOP
973 * or SIGKILL.
974 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800975 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 goto out_set;
977
Randy Dunlap5aba0852011-04-04 14:59:31 -0700978 /*
979 * Real-time signals must be queued if sent by sigqueue, or
980 * some other real-time mechanism. It is implementation
981 * defined whether kill() does so. We attempt to do so, on
982 * the principle of least surprise, but since kill is not
983 * allowed to fail with EAGAIN when low on memory we just
984 * make sure at least one signal gets delivered and don't
985 * pass on the info struct.
986 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200987 if (sig < SIGRTMIN)
988 override_rlimit = (is_si_special(info) || info->si_code >= 0);
989 else
990 override_rlimit = 0;
991
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900992 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200993 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700995 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800997 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 q->info.si_signo = sig;
999 q->info.si_errno = 0;
1000 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001001 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001002 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001003 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001005 case (unsigned long) SEND_SIG_PRIV:
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_KERNEL;
1009 q->info.si_pid = 0;
1010 q->info.si_uid = 0;
1011 break;
1012 default:
1013 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001014 if (from_ancestor_ns)
1015 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 }
Oleg Nesterov621d3122005-10-30 15:03:45 -08001018 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001019 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1020 /*
1021 * Queue overflow, abort. We may abort if the
1022 * signal was rt and sent by user using something
1023 * other than kill().
1024 */
1025 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001027 } else {
1028 /*
1029 * This is a silent loss of information. We still
1030 * send the signal, but the *info bits are lost.
1031 */
1032 trace_signal_lose_info(sig, group, info);
1033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035
1036out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001037 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001038 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001039 complete_signal(sig, t, group);
1040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001043static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1044 int group)
1045{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001046 int from_ancestor_ns = 0;
1047
1048#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001049 from_ancestor_ns = si_fromuser(info) &&
1050 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001051#endif
1052
1053 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001054}
1055
Ingo Molnar45807a12007-07-15 23:40:10 -07001056static void print_fatal_signal(struct pt_regs *regs, int signr)
1057{
1058 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001059 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001060
Al Viroca5cd872007-10-29 04:31:16 +00001061#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001062 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001063 {
1064 int i;
1065 for (i = 0; i < 16; i++) {
1066 unsigned char insn;
1067
Andi Kleenb45c6e72010-01-08 14:42:52 -08001068 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1069 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001070 printk("%02x ", insn);
1071 }
1072 }
1073#endif
1074 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001075 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001076 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001077 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001078}
1079
1080static int __init setup_print_fatal_signals(char *str)
1081{
1082 get_option (&str, &print_fatal_signals);
1083
1084 return 1;
1085}
1086
1087__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001089int
1090__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1091{
1092 return send_signal(sig, info, p, 1);
1093}
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095static int
1096specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1097{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001098 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001101int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1102 bool group)
1103{
1104 unsigned long flags;
1105 int ret = -ESRCH;
1106
1107 if (lock_task_sighand(p, &flags)) {
1108 ret = send_signal(sig, info, p, group);
1109 unlock_task_sighand(p, &flags);
1110 }
1111
1112 return ret;
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/*
1116 * Force a signal that the process can't ignore: if necessary
1117 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001118 *
1119 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1120 * since we do not want to have a signal handler that was blocked
1121 * be invoked when user space had explicitly blocked it.
1122 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001123 * We don't want to have recursive SIGSEGV's etc, for example,
1124 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126int
1127force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1128{
1129 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001130 int ret, blocked, ignored;
1131 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001134 action = &t->sighand->action[sig-1];
1135 ignored = action->sa.sa_handler == SIG_IGN;
1136 blocked = sigismember(&t->blocked, sig);
1137 if (blocked || ignored) {
1138 action->sa.sa_handler = SIG_DFL;
1139 if (blocked) {
1140 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001141 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001144 if (action->sa.sa_handler == SIG_DFL)
1145 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 ret = specific_send_sig_info(sig, info, t);
1147 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1148
1149 return ret;
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/*
1153 * Nuke all other threads in the group.
1154 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001155int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001157 struct task_struct *t = p;
1158 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 p->signal->group_stop_count = 0;
1161
Oleg Nesterov09faef12010-05-26 14:43:11 -07001162 while_each_thread(p, t) {
Tejun Heo39efa3e2011-03-23 10:37:00 +01001163 task_clear_group_stop_pending(t);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001164 count++;
1165
1166 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (t->exit_state)
1168 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 signal_wake_up(t, 1);
1171 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001172
1173 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001176struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1177 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001178{
1179 struct sighand_struct *sighand;
1180
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001181 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001182 for (;;) {
1183 sighand = rcu_dereference(tsk->sighand);
1184 if (unlikely(sighand == NULL))
1185 break;
1186
1187 spin_lock_irqsave(&sighand->siglock, *flags);
1188 if (likely(sighand == tsk->sighand))
1189 break;
1190 spin_unlock_irqrestore(&sighand->siglock, *flags);
1191 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001192 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001193
1194 return sighand;
1195}
1196
David Howellsc69e8d92008-11-14 10:39:19 +11001197/*
1198 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1201{
David Howells694f6902010-08-04 16:59:14 +01001202 int ret;
1203
1204 rcu_read_lock();
1205 ret = check_kill_permission(sig, info, p);
1206 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001208 if (!ret && sig)
1209 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 return ret;
1212}
1213
1214/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001215 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001217 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001219int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct task_struct *p = NULL;
1222 int retval, success;
1223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 success = 0;
1225 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001226 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int err = group_send_sig_info(sig, info, p);
1228 success |= !err;
1229 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001230 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return success ? 0 : retval;
1232}
1233
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001234int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001236 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 struct task_struct *p;
1238
Ingo Molnare56d0902006-01-08 01:01:37 -08001239 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001240retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001241 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001242 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001244 if (unlikely(error == -ESRCH))
1245 /*
1246 * The task was unhashed in between, try again.
1247 * If it is dead, pid_task() will return NULL,
1248 * if we race with de_thread() it will find the
1249 * new leader.
1250 */
1251 goto retry;
1252 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001253 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return error;
1256}
1257
Randy Dunlap5aba0852011-04-04 14:59:31 -07001258int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001259{
1260 int error;
1261 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001262 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001263 rcu_read_unlock();
1264 return error;
1265}
1266
Eric W. Biederman2425c082006-10-02 02:17:28 -07001267/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1268int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001269 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001270{
1271 int ret = -EINVAL;
1272 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001273 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001274 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001275
1276 if (!valid_signal(sig))
1277 return ret;
1278
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001279 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001280 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001281 if (!p) {
1282 ret = -ESRCH;
1283 goto out_unlock;
1284 }
David Howellsc69e8d92008-11-14 10:39:19 +11001285 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001286 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001287 euid != pcred->suid && euid != pcred->uid &&
1288 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001289 ret = -EPERM;
1290 goto out_unlock;
1291 }
David Quigley8f95dc52006-06-30 01:55:47 -07001292 ret = security_task_kill(p, info, sig, secid);
1293 if (ret)
1294 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001295
1296 if (sig) {
1297 if (lock_task_sighand(p, &flags)) {
1298 ret = __send_signal(sig, info, p, 1, 0);
1299 unlock_task_sighand(p, &flags);
1300 } else
1301 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001302 }
1303out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001304 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001305 return ret;
1306}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001307EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309/*
1310 * kill_something_info() interprets pid in interesting ways just like kill(2).
1311 *
1312 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1313 * is probably wrong. Should make it like BSD or SYSV.
1314 */
1315
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001316static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001318 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001319
1320 if (pid > 0) {
1321 rcu_read_lock();
1322 ret = kill_pid_info(sig, info, find_vpid(pid));
1323 rcu_read_unlock();
1324 return ret;
1325 }
1326
1327 read_lock(&tasklist_lock);
1328 if (pid != -1) {
1329 ret = __kill_pgrp_info(sig, info,
1330 pid ? find_vpid(-pid) : task_pgrp(current));
1331 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int retval = 0, count = 0;
1333 struct task_struct * p;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001336 if (task_pid_vnr(p) > 1 &&
1337 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int err = group_send_sig_info(sig, info, p);
1339 ++count;
1340 if (err != -EPERM)
1341 retval = err;
1342 }
1343 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001344 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001346 read_unlock(&tasklist_lock);
1347
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001348 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
1351/*
1352 * These are for backward compatibility with the rest of the kernel source.
1353 */
1354
Randy Dunlap5aba0852011-04-04 14:59:31 -07001355int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Make sure legacy kernel users don't send in bad values
1359 * (normal paths check this in check_kill_permission).
1360 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001361 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EINVAL;
1363
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001364 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001367#define __si_special(priv) \
1368 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1369
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370int
1371send_sig(int sig, struct task_struct *p, int priv)
1372{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001373 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376void
1377force_sig(int sig, struct task_struct *p)
1378{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001379 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382/*
1383 * When things go south during signal handling, we
1384 * will force a SIGSEGV. And if the signal that caused
1385 * the problem was already a SIGSEGV, we'll want to
1386 * make sure we don't even try to deliver the signal..
1387 */
1388int
1389force_sigsegv(int sig, struct task_struct *p)
1390{
1391 if (sig == SIGSEGV) {
1392 unsigned long flags;
1393 spin_lock_irqsave(&p->sighand->siglock, flags);
1394 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1395 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1396 }
1397 force_sig(SIGSEGV, p);
1398 return 0;
1399}
1400
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001401int kill_pgrp(struct pid *pid, int sig, int priv)
1402{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001403 int ret;
1404
1405 read_lock(&tasklist_lock);
1406 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1407 read_unlock(&tasklist_lock);
1408
1409 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001410}
1411EXPORT_SYMBOL(kill_pgrp);
1412
1413int kill_pid(struct pid *pid, int sig, int priv)
1414{
1415 return kill_pid_info(sig, __si_special(priv), pid);
1416}
1417EXPORT_SYMBOL(kill_pid);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419/*
1420 * These functions support sending signals using preallocated sigqueue
1421 * structures. This is needed "because realtime applications cannot
1422 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001423 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * we allocate the sigqueue structure from the timer_create. If this
1425 * allocation fails we are able to report the failure to the application
1426 * with an EAGAIN error.
1427 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428struct sigqueue *sigqueue_alloc(void)
1429{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001430 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001432 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001434
1435 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
1438void sigqueue_free(struct sigqueue *q)
1439{
1440 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001441 spinlock_t *lock = &current->sighand->siglock;
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1444 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001445 * We must hold ->siglock while testing q->list
1446 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001447 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001449 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001450 q->flags &= ~SIGQUEUE_PREALLOC;
1451 /*
1452 * If it is queued it will be freed when dequeued,
1453 * like the "regular" sigqueue.
1454 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001455 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001456 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001457 spin_unlock_irqrestore(lock, flags);
1458
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001459 if (q)
1460 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001463int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001464{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001465 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001466 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001467 unsigned long flags;
1468 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001469
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001470 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001471
1472 ret = -1;
1473 if (!likely(lock_task_sighand(t, &flags)))
1474 goto ret;
1475
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001476 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001477 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001478 goto out;
1479
1480 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001481 if (unlikely(!list_empty(&q->list))) {
1482 /*
1483 * If an SI_TIMER entry is already queue just increment
1484 * the overrun count.
1485 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001486 BUG_ON(q->info.si_code != SI_TIMER);
1487 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001488 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001489 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001490 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001491
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001492 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001493 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001494 list_add_tail(&q->list, &pending->list);
1495 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001496 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001497out:
1498 unlock_task_sighand(t, &flags);
1499ret:
1500 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001501}
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 * Let a parent know about the death of a child.
1505 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001506 *
1507 * Returns -1 if our parent ignored us and so we've switched to
1508 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001510int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct siginfo info;
1513 unsigned long flags;
1514 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001515 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 BUG_ON(sig == -1);
1518
1519 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001520 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001522 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1524
1525 info.si_signo = sig;
1526 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001527 /*
1528 * we are under tasklist_lock here so our parent is tied to
1529 * us and cannot exit and release its namespace.
1530 *
1531 * the only it can is to switch its nsproxy with sys_unshare,
1532 * bu uncharing pid namespaces is not allowed, so we'll always
1533 * see relevant namespace
1534 *
1535 * write_lock() currently calls preempt_disable() which is the
1536 * same as rcu_read_lock(), but according to Oleg, this is not
1537 * correct to rely on this
1538 */
1539 rcu_read_lock();
1540 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001541 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001542 rcu_read_unlock();
1543
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001544 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1545 tsk->signal->utime));
1546 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1547 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 info.si_status = tsk->exit_code & 0x7f;
1550 if (tsk->exit_code & 0x80)
1551 info.si_code = CLD_DUMPED;
1552 else if (tsk->exit_code & 0x7f)
1553 info.si_code = CLD_KILLED;
1554 else {
1555 info.si_code = CLD_EXITED;
1556 info.si_status = tsk->exit_code >> 8;
1557 }
1558
1559 psig = tsk->parent->sighand;
1560 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001561 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1563 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1564 /*
1565 * We are exiting and our parent doesn't care. POSIX.1
1566 * defines special semantics for setting SIGCHLD to SIG_IGN
1567 * or setting the SA_NOCLDWAIT flag: we should be reaped
1568 * automatically and not left for our parent's wait4 call.
1569 * Rather than having the parent do it as a magic kind of
1570 * signal handler, we just set this to tell do_exit that we
1571 * can be cleaned up without becoming a zombie. Note that
1572 * we still call __wake_up_parent in this case, because a
1573 * blocked sys_wait4 might now return -ECHILD.
1574 *
1575 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1576 * is implementation-defined: we do (if you don't want
1577 * it, just use SIG_IGN instead).
1578 */
Roland McGrath1b046242008-08-19 20:37:07 -07001579 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001581 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001583 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 __group_send_sig_info(sig, &info, tsk->parent);
1585 __wake_up_parent(tsk, tsk->parent);
1586 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001587
Roland McGrath1b046242008-08-19 20:37:07 -07001588 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
Tejun Heo75b95952011-03-23 10:37:01 +01001591/**
1592 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1593 * @tsk: task reporting the state change
1594 * @for_ptracer: the notification is for ptracer
1595 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1596 *
1597 * Notify @tsk's parent that the stopped/continued state has changed. If
1598 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1599 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1600 *
1601 * CONTEXT:
1602 * Must be called with tasklist_lock at least read locked.
1603 */
1604static void do_notify_parent_cldstop(struct task_struct *tsk,
1605 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 struct siginfo info;
1608 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001609 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct sighand_struct *sighand;
1611
Tejun Heo75b95952011-03-23 10:37:01 +01001612 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001613 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001614 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001615 tsk = tsk->group_leader;
1616 parent = tsk->real_parent;
1617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 info.si_signo = SIGCHLD;
1620 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001621 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001622 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001623 */
1624 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001625 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001626 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001627 rcu_read_unlock();
1628
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001629 info.si_utime = cputime_to_clock_t(tsk->utime);
1630 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 info.si_code = why;
1633 switch (why) {
1634 case CLD_CONTINUED:
1635 info.si_status = SIGCONT;
1636 break;
1637 case CLD_STOPPED:
1638 info.si_status = tsk->signal->group_exit_code & 0x7f;
1639 break;
1640 case CLD_TRAPPED:
1641 info.si_status = tsk->exit_code & 0x7f;
1642 break;
1643 default:
1644 BUG();
1645 }
1646
1647 sighand = parent->sighand;
1648 spin_lock_irqsave(&sighand->siglock, flags);
1649 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1650 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1651 __group_send_sig_info(SIGCHLD, &info, parent);
1652 /*
1653 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1654 */
1655 __wake_up_parent(tsk, parent);
1656 spin_unlock_irqrestore(&sighand->siglock, flags);
1657}
1658
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001659static inline int may_ptrace_stop(void)
1660{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001661 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001662 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001663 /*
1664 * Are we in the middle of do_coredump?
1665 * If so and our tracer is also part of the coredump stopping
1666 * is a deadlock situation, and pointless because our tracer
1667 * is dead so don't allow us to stop.
1668 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001669 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001670 * is safe to enter schedule().
1671 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001672 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001673 unlikely(current->mm == current->parent->mm))
1674 return 0;
1675
1676 return 1;
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001680 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001681 * Called with the siglock held.
1682 */
1683static int sigkill_pending(struct task_struct *tsk)
1684{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001685 return sigismember(&tsk->pending.signal, SIGKILL) ||
1686 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001687}
1688
1689/*
Tejun Heoceb6bd62011-03-23 10:37:01 +01001690 * Test whether the target task of the usual cldstop notification - the
1691 * real_parent of @child - is in the same group as the ptracer.
1692 */
1693static bool real_parent_is_ptracer(struct task_struct *child)
1694{
1695 return same_thread_group(child->parent, child->real_parent);
1696}
1697
1698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 * This must be called with current->sighand->siglock held.
1700 *
1701 * This should be the path for all ptrace stops.
1702 * We always set current->last_siginfo while stopped here.
1703 * That makes it a way to test a stopped process for
1704 * being ptrace-stopped vs being job-control-stopped.
1705 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001706 * If we actually decide not to stop at all because the tracer
1707 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001709static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001710 __releases(&current->sighand->siglock)
1711 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001713 bool gstop_done = false;
1714
Roland McGrath1a669c22008-02-06 01:37:37 -08001715 if (arch_ptrace_stop_needed(exit_code, info)) {
1716 /*
1717 * The arch code has something special to do before a
1718 * ptrace stop. This is allowed to block, e.g. for faults
1719 * on user stack pages. We can't keep the siglock while
1720 * calling arch_ptrace_stop, so we must release it now.
1721 * To preserve proper semantics, we must do this before
1722 * any signal bookkeeping like checking group_stop_count.
1723 * Meanwhile, a SIGKILL could come in before we retake the
1724 * siglock. That must prevent us from sleeping in TASK_TRACED.
1725 * So after regaining the lock, we must check for SIGKILL.
1726 */
1727 spin_unlock_irq(&current->sighand->siglock);
1728 arch_ptrace_stop(exit_code, info);
1729 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001730 if (sigkill_pending(current))
1731 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001732 }
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001735 * If @why is CLD_STOPPED, we're trapping to participate in a group
1736 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1737 * while siglock was released for the arch hook, PENDING could be
1738 * clear now. We act as if SIGCONT is received after TASK_TRACED
1739 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001741 if (why == CLD_STOPPED && (current->group_stop & GROUP_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001742 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 current->last_siginfo = info;
1745 current->exit_code = exit_code;
1746
Tejun Heod79fdd62011-03-23 10:37:00 +01001747 /*
1748 * TRACED should be visible before TRAPPING is cleared; otherwise,
1749 * the tracer might fail do_wait().
1750 */
1751 set_current_state(TASK_TRACED);
1752
1753 /*
1754 * We're committing to trapping. Clearing GROUP_STOP_TRAPPING and
1755 * transition to TASK_TRACED should be atomic with respect to
1756 * siglock. This hsould be done after the arch hook as siglock is
1757 * released and regrabbed across it.
1758 */
1759 task_clear_group_stop_trapping(current);
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 spin_unlock_irq(&current->sighand->siglock);
1762 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001763 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001764 /*
1765 * Notify parents of the stop.
1766 *
1767 * While ptraced, there are two parents - the ptracer and
1768 * the real_parent of the group_leader. The ptracer should
1769 * know about every stop while the real parent is only
1770 * interested in the completion of group stop. The states
1771 * for the two don't interact with each other. Notify
1772 * separately unless they're gonna be duplicates.
1773 */
1774 do_notify_parent_cldstop(current, true, why);
1775 if (gstop_done && !real_parent_is_ptracer(current))
1776 do_notify_parent_cldstop(current, false, why);
1777
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001778 /*
1779 * Don't want to allow preemption here, because
1780 * sys_ptrace() needs this task to be inactive.
1781 *
1782 * XXX: implement read_unlock_no_resched().
1783 */
1784 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001786 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 schedule();
1788 } else {
1789 /*
1790 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001791 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001792 *
1793 * If @gstop_done, the ptracer went away between group stop
1794 * completion and here. During detach, it would have set
1795 * GROUP_STOP_PENDING on us and we'll re-enter TASK_STOPPED
1796 * in do_signal_stop() on return, so notifying the real
1797 * parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001799 if (gstop_done)
1800 do_notify_parent_cldstop(current, false, why);
1801
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001802 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001803 if (clear_code)
1804 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001805 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
1808 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001809 * While in TASK_TRACED, we were considered "frozen enough".
1810 * Now that we woke up, it's crucial if we're supposed to be
1811 * frozen that we freeze now before running anything substantial.
1812 */
1813 try_to_freeze();
1814
1815 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * We are back. Now reacquire the siglock before touching
1817 * last_siginfo, so that we are sure to have synchronized with
1818 * any signal-sending on another CPU that wants to examine it.
1819 */
1820 spin_lock_irq(&current->sighand->siglock);
1821 current->last_siginfo = NULL;
1822
1823 /*
1824 * Queued signals ignored us while we were stopped for tracing.
1825 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001826 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001828 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829}
1830
1831void ptrace_notify(int exit_code)
1832{
1833 siginfo_t info;
1834
1835 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1836
1837 memset(&info, 0, sizeof info);
1838 info.si_signo = SIGTRAP;
1839 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001840 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001841 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 /* Let the debugger run. */
1844 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001845 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 spin_unlock_irq(&current->sighand->siglock);
1847}
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849/*
1850 * This performs the stopping for SIGSTOP and other stop signals.
1851 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001852 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * Returns zero if we didn't stop and still hold the siglock.
1854 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001855static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Tejun Heo39efa3e2011-03-23 10:37:00 +01001859 if (!(current->group_stop & GROUP_STOP_PENDING)) {
1860 unsigned int gstop = GROUP_STOP_PENDING | GROUP_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001861 struct task_struct *t;
1862
Tejun Heod79fdd62011-03-23 10:37:00 +01001863 /* signr will be recorded in task->group_stop for retries */
1864 WARN_ON_ONCE(signr & ~GROUP_STOP_SIGMASK);
1865
Oleg Nesterovee77f072011-04-01 20:12:38 +02001866 if (!likely(current->group_stop & GROUP_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001867 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001870 * There is no group stop already in progress. We must
1871 * initiate one now.
1872 *
1873 * While ptraced, a task may be resumed while group stop is
1874 * still in effect and then receive a stop signal and
1875 * initiate another group stop. This deviates from the
1876 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001877 * cause two group stops when !ptraced. That is why we
1878 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001879 *
1880 * The condition can be distinguished by testing whether
1881 * SIGNAL_STOP_STOPPED is already set. Don't generate
1882 * group_exit_code in such case.
1883 *
1884 * This is not necessary for SIGNAL_STOP_CONTINUED because
1885 * an intervening stop signal is required to cause two
1886 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001888 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1889 sig->group_exit_code = signr;
1890 else
1891 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001892
Tejun Heod79fdd62011-03-23 10:37:00 +01001893 current->group_stop &= ~GROUP_STOP_SIGMASK;
1894 current->group_stop |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001895 sig->group_stop_count = 1;
Tejun Heod79fdd62011-03-23 10:37:00 +01001896 for (t = next_thread(current); t != current;
1897 t = next_thread(t)) {
1898 t->group_stop &= ~GROUP_STOP_SIGMASK;
Oleg Nesterova122b342006-03-28 16:11:22 -08001899 /*
1900 * Setting state to TASK_STOPPED for a group
1901 * stop is always done with the siglock held,
1902 * so this check has no races.
1903 */
Tejun Heo39efa3e2011-03-23 10:37:00 +01001904 if (!(t->flags & PF_EXITING) && !task_is_stopped(t)) {
Tejun Heod79fdd62011-03-23 10:37:00 +01001905 t->group_stop |= signr | gstop;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001906 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001907 signal_wake_up(t, 0);
1908 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001909 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001910 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001911retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001912 if (likely(!task_ptrace(current))) {
1913 int notify = 0;
1914
1915 /*
1916 * If there are no other threads in the group, or if there
1917 * is a group stop in progress and we are the last to stop,
1918 * report to the parent.
1919 */
1920 if (task_participate_group_stop(current))
1921 notify = CLD_STOPPED;
1922
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001923 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001924 spin_unlock_irq(&current->sighand->siglock);
1925
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001926 /*
1927 * Notify the parent of the group stop completion. Because
1928 * we're not holding either the siglock or tasklist_lock
1929 * here, ptracer may attach inbetween; however, this is for
1930 * group stop and should always be delivered to the real
1931 * parent of the group leader. The new ptracer will get
1932 * its notification when this task transitions into
1933 * TASK_TRACED.
1934 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001935 if (notify) {
1936 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001937 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001938 read_unlock(&tasklist_lock);
1939 }
1940
1941 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1942 schedule();
1943
1944 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001945 } else {
1946 ptrace_stop(current->group_stop & GROUP_STOP_SIGMASK,
1947 CLD_STOPPED, 0, NULL);
1948 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001949 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001950
1951 /*
1952 * GROUP_STOP_PENDING could be set if another group stop has
1953 * started since being woken up or ptrace wants us to transit
1954 * between TASK_STOPPED and TRACED. Retry group stop.
1955 */
1956 if (current->group_stop & GROUP_STOP_PENDING) {
1957 WARN_ON_ONCE(!(current->group_stop & GROUP_STOP_SIGMASK));
1958 goto retry;
1959 }
1960
1961 /* PTRACE_ATTACH might have raced with task killing, clear trapping */
1962 task_clear_group_stop_trapping(current);
Tejun Heo5224fa32011-03-23 10:37:00 +01001963
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001964 spin_unlock_irq(&current->sighand->siglock);
1965
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001966 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 1;
1969}
1970
Roland McGrath18c98b62008-04-17 18:44:38 -07001971static int ptrace_signal(int signr, siginfo_t *info,
1972 struct pt_regs *regs, void *cookie)
1973{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001974 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001975 return signr;
1976
1977 ptrace_signal_deliver(regs, cookie);
1978
1979 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001980 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07001981
1982 /* We're back. Did the debugger cancel the sig? */
1983 signr = current->exit_code;
1984 if (signr == 0)
1985 return signr;
1986
1987 current->exit_code = 0;
1988
Randy Dunlap5aba0852011-04-04 14:59:31 -07001989 /*
1990 * Update the siginfo structure if the signal has
1991 * changed. If the debugger wanted something
1992 * specific in the siginfo structure then it should
1993 * have updated *info via PTRACE_SETSIGINFO.
1994 */
Roland McGrath18c98b62008-04-17 18:44:38 -07001995 if (signr != info->si_signo) {
1996 info->si_signo = signr;
1997 info->si_errno = 0;
1998 info->si_code = SI_USER;
1999 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11002000 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002001 }
2002
2003 /* If the (new) signal is now blocked, requeue it. */
2004 if (sigismember(&current->blocked, signr)) {
2005 specific_send_sig_info(signr, info, current);
2006 signr = 0;
2007 }
2008
2009 return signr;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2013 struct pt_regs *regs, void *cookie)
2014{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002015 struct sighand_struct *sighand = current->sighand;
2016 struct signal_struct *signal = current->signal;
2017 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002019relock:
2020 /*
2021 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2022 * While in TASK_STOPPED, we were considered "frozen enough".
2023 * Now that we woke up, it's crucial if we're supposed to be
2024 * frozen that we freeze now before running anything substantial.
2025 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002026 try_to_freeze();
2027
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002028 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002029 /*
2030 * Every stopped thread goes here after wakeup. Check to see if
2031 * we should notify the parent, prepare_signal(SIGCONT) encodes
2032 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2033 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002034 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002035 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002036 int why;
2037
2038 if (signal->flags & SIGNAL_CLD_CONTINUED)
2039 why = CLD_CONTINUED;
2040 else
2041 why = CLD_STOPPED;
2042
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002043 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002044
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002045 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002046
Tejun Heoceb6bd62011-03-23 10:37:01 +01002047 /*
2048 * Notify the parent that we're continuing. This event is
2049 * always per-process and doesn't make whole lot of sense
2050 * for ptracers, who shouldn't consume the state via
2051 * wait(2) either, but, for backward compatibility, notify
2052 * the ptracer of the group leader too unless it's gonna be
2053 * a duplicate.
2054 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002055 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002056
2057 do_notify_parent_cldstop(current, false, why);
2058
Tejun Heo75b95952011-03-23 10:37:01 +01002059 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002060 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2061 do_notify_parent_cldstop(leader, true, why);
2062
Tejun Heoedf2ed12011-03-23 10:37:00 +01002063 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002064
Oleg Nesterove4420552008-04-30 00:52:44 -07002065 goto relock;
2066 }
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 for (;;) {
2069 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002070 /*
2071 * Tracing can induce an artifical signal and choose sigaction.
2072 * The return value in @signr determines the default action,
2073 * but @info->si_signo is the signal number we will report.
2074 */
2075 signr = tracehook_get_signal(current, regs, info, return_ka);
2076 if (unlikely(signr < 0))
2077 goto relock;
2078 if (unlikely(signr != 0))
2079 ka = return_ka;
2080 else {
Tejun Heo39efa3e2011-03-23 10:37:00 +01002081 if (unlikely(current->group_stop &
2082 GROUP_STOP_PENDING) && do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002083 goto relock;
2084
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002085 signr = dequeue_signal(current, &current->blocked,
2086 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Roland McGrath18c98b62008-04-17 18:44:38 -07002088 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002089 break; /* will return 0 */
2090
2091 if (signr != SIGKILL) {
2092 signr = ptrace_signal(signr, info,
2093 regs, cookie);
2094 if (!signr)
2095 continue;
2096 }
2097
2098 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002101 /* Trace actually delivered signals. */
2102 trace_signal_deliver(signr, info, ka);
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2105 continue;
2106 if (ka->sa.sa_handler != SIG_DFL) {
2107 /* Run the handler. */
2108 *return_ka = *ka;
2109
2110 if (ka->sa.sa_flags & SA_ONESHOT)
2111 ka->sa.sa_handler = SIG_DFL;
2112
2113 break; /* will return non-zero "signr" value */
2114 }
2115
2116 /*
2117 * Now we are doing the default action for this signal.
2118 */
2119 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2120 continue;
2121
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002122 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002123 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002124 * Container-init gets no signals it doesn't want from same
2125 * container.
2126 *
2127 * Note that if global/container-init sees a sig_kernel_only()
2128 * signal here, the signal must have been generated internally
2129 * or must have come from an ancestor namespace. In either
2130 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002131 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002132 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002133 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 continue;
2135
2136 if (sig_kernel_stop(signr)) {
2137 /*
2138 * The default action is to stop all threads in
2139 * the thread group. The job control signals
2140 * do nothing in an orphaned pgrp, but SIGSTOP
2141 * always works. Note that siglock needs to be
2142 * dropped during the call to is_orphaned_pgrp()
2143 * because of lock ordering with tasklist_lock.
2144 * This allows an intervening SIGCONT to be posted.
2145 * We need to check for that and bail out if necessary.
2146 */
2147 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002148 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 /* signals can be posted during this window */
2151
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002152 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 goto relock;
2154
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002155 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002158 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 /* It released the siglock. */
2160 goto relock;
2161 }
2162
2163 /*
2164 * We didn't actually stop, due to a race
2165 * with SIGCONT or something like that.
2166 */
2167 continue;
2168 }
2169
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002170 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /*
2173 * Anything else is fatal, maybe with a core dump.
2174 */
2175 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002178 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002179 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /*
2181 * If it was able to dump core, this kills all
2182 * other threads in the group and synchronizes with
2183 * their demise. If we lost the race with another
2184 * thread getting here, it set group_exit_code
2185 * first and our do_group_exit call below will use
2186 * that value and ignore the one we pass it.
2187 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002188 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
2191 /*
2192 * Death signals, no core dump.
2193 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002194 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* NOTREACHED */
2196 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002197 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return signr;
2199}
2200
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002201/*
2202 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002203 * group-wide signal. Other threads should be notified now to take
2204 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002205 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002206static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002207{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002208 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002209 struct task_struct *t;
2210
Oleg Nesterovf646e222011-04-27 19:18:39 +02002211 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2212 if (sigisemptyset(&retarget))
2213 return;
2214
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002215 t = tsk;
2216 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002217 if (t->flags & PF_EXITING)
2218 continue;
2219
2220 if (!has_pending_signals(&retarget, &t->blocked))
2221 continue;
2222 /* Remove the signals this thread can handle. */
2223 sigandsets(&retarget, &retarget, &t->blocked);
2224
2225 if (!signal_pending(t))
2226 signal_wake_up(t, 0);
2227
2228 if (sigisemptyset(&retarget))
2229 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002230 }
2231}
2232
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002233void exit_signals(struct task_struct *tsk)
2234{
2235 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002236 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002237
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002238 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2239 tsk->flags |= PF_EXITING;
2240 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002241 }
2242
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002243 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002244 /*
2245 * From now this task is not visible for group-wide signals,
2246 * see wants_signal(), do_signal_stop().
2247 */
2248 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002249 if (!signal_pending(tsk))
2250 goto out;
2251
Oleg Nesterovf646e222011-04-27 19:18:39 +02002252 unblocked = tsk->blocked;
2253 signotset(&unblocked);
2254 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002255
Tejun Heo39efa3e2011-03-23 10:37:00 +01002256 if (unlikely(tsk->group_stop & GROUP_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002257 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002258 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002259out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002260 spin_unlock_irq(&tsk->sighand->siglock);
2261
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002262 /*
2263 * If group stop has completed, deliver the notification. This
2264 * should always go to the real parent of the group leader.
2265 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002266 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002267 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002268 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002269 read_unlock(&tasklist_lock);
2270 }
2271}
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273EXPORT_SYMBOL(recalc_sigpending);
2274EXPORT_SYMBOL_GPL(dequeue_signal);
2275EXPORT_SYMBOL(flush_signals);
2276EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277EXPORT_SYMBOL(send_sig);
2278EXPORT_SYMBOL(send_sig_info);
2279EXPORT_SYMBOL(sigprocmask);
2280EXPORT_SYMBOL(block_all_signals);
2281EXPORT_SYMBOL(unblock_all_signals);
2282
2283
2284/*
2285 * System call entry points.
2286 */
2287
Randy Dunlap41c57892011-04-04 15:00:26 -07002288/**
2289 * sys_restart_syscall - restart a system call
2290 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002291SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 struct restart_block *restart = &current_thread_info()->restart_block;
2294 return restart->fn(restart);
2295}
2296
2297long do_no_restart_syscall(struct restart_block *param)
2298{
2299 return -EINTR;
2300}
2301
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002302/**
2303 * set_current_blocked - change current->blocked mask
2304 * @newset: new mask
2305 *
2306 * It is wrong to change ->blocked directly, this helper should be used
2307 * to ensure the process can't miss a shared signal we are going to block.
2308 */
2309void set_current_blocked(const sigset_t *newset)
2310{
2311 struct task_struct *tsk = current;
2312
2313 spin_lock_irq(&tsk->sighand->siglock);
2314 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2315 sigset_t newblocked;
2316 /* A set of now blocked but previously unblocked signals. */
2317 signandsets(&newblocked, newset, &current->blocked);
2318 retarget_shared_pending(tsk, &newblocked);
2319 }
2320 tsk->blocked = *newset;
2321 recalc_sigpending();
2322 spin_unlock_irq(&tsk->sighand->siglock);
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 * This is also useful for kernel threads that want to temporarily
2327 * (or permanently) block certain signals.
2328 *
2329 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2330 * interface happily blocks "unblockable" signals like SIGKILL
2331 * and friends.
2332 */
2333int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2334{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002335 struct task_struct *tsk = current;
2336 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002338 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002339 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002340 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 switch (how) {
2343 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002344 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 break;
2346 case SIG_UNBLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002347 signandsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 break;
2349 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002350 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002355
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002356 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358}
2359
Randy Dunlap41c57892011-04-04 15:00:26 -07002360/**
2361 * sys_rt_sigprocmask - change the list of currently blocked signals
2362 * @how: whether to add, remove, or set signals
2363 * @set: stores pending signals
2364 * @oset: previous value of signal mask if non-null
2365 * @sigsetsize: size of sigset_t type
2366 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002367SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002368 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002371 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /* XXX: Don't preclude handling different sized sigset_t's. */
2374 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002375 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002377 old_set = current->blocked;
2378
2379 if (nset) {
2380 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2381 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2383
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002384 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002386 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002388
2389 if (oset) {
2390 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2391 return -EFAULT;
2392 }
2393
2394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
2397long do_sigpending(void __user *set, unsigned long sigsetsize)
2398{
2399 long error = -EINVAL;
2400 sigset_t pending;
2401
2402 if (sigsetsize > sizeof(sigset_t))
2403 goto out;
2404
2405 spin_lock_irq(&current->sighand->siglock);
2406 sigorsets(&pending, &current->pending.signal,
2407 &current->signal->shared_pending.signal);
2408 spin_unlock_irq(&current->sighand->siglock);
2409
2410 /* Outside the lock because only this thread touches it. */
2411 sigandsets(&pending, &current->blocked, &pending);
2412
2413 error = -EFAULT;
2414 if (!copy_to_user(set, &pending, sigsetsize))
2415 error = 0;
2416
2417out:
2418 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002419}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Randy Dunlap41c57892011-04-04 15:00:26 -07002421/**
2422 * sys_rt_sigpending - examine a pending signal that has been raised
2423 * while blocked
2424 * @set: stores pending signals
2425 * @sigsetsize: size of sigset_t type or larger
2426 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002427SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
2429 return do_sigpending(set, sigsetsize);
2430}
2431
2432#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2433
2434int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2435{
2436 int err;
2437
2438 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2439 return -EFAULT;
2440 if (from->si_code < 0)
2441 return __copy_to_user(to, from, sizeof(siginfo_t))
2442 ? -EFAULT : 0;
2443 /*
2444 * If you change siginfo_t structure, please be sure
2445 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002446 * Please remember to update the signalfd_copyinfo() function
2447 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 * It should never copy any pad contained in the structure
2449 * to avoid security leaks, but must copy the generic
2450 * 3 ints plus the relevant union member.
2451 */
2452 err = __put_user(from->si_signo, &to->si_signo);
2453 err |= __put_user(from->si_errno, &to->si_errno);
2454 err |= __put_user((short)from->si_code, &to->si_code);
2455 switch (from->si_code & __SI_MASK) {
2456 case __SI_KILL:
2457 err |= __put_user(from->si_pid, &to->si_pid);
2458 err |= __put_user(from->si_uid, &to->si_uid);
2459 break;
2460 case __SI_TIMER:
2461 err |= __put_user(from->si_tid, &to->si_tid);
2462 err |= __put_user(from->si_overrun, &to->si_overrun);
2463 err |= __put_user(from->si_ptr, &to->si_ptr);
2464 break;
2465 case __SI_POLL:
2466 err |= __put_user(from->si_band, &to->si_band);
2467 err |= __put_user(from->si_fd, &to->si_fd);
2468 break;
2469 case __SI_FAULT:
2470 err |= __put_user(from->si_addr, &to->si_addr);
2471#ifdef __ARCH_SI_TRAPNO
2472 err |= __put_user(from->si_trapno, &to->si_trapno);
2473#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002474#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002475 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002476 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002477 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002478 */
2479 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2480 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2481#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 break;
2483 case __SI_CHLD:
2484 err |= __put_user(from->si_pid, &to->si_pid);
2485 err |= __put_user(from->si_uid, &to->si_uid);
2486 err |= __put_user(from->si_status, &to->si_status);
2487 err |= __put_user(from->si_utime, &to->si_utime);
2488 err |= __put_user(from->si_stime, &to->si_stime);
2489 break;
2490 case __SI_RT: /* This is not generated by the kernel as of now. */
2491 case __SI_MESGQ: /* But this is */
2492 err |= __put_user(from->si_pid, &to->si_pid);
2493 err |= __put_user(from->si_uid, &to->si_uid);
2494 err |= __put_user(from->si_ptr, &to->si_ptr);
2495 break;
2496 default: /* this is just in case for now ... */
2497 err |= __put_user(from->si_pid, &to->si_pid);
2498 err |= __put_user(from->si_uid, &to->si_uid);
2499 break;
2500 }
2501 return err;
2502}
2503
2504#endif
2505
Randy Dunlap41c57892011-04-04 15:00:26 -07002506/**
2507 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2508 * in @uthese
2509 * @uthese: queued signals to wait for
2510 * @uinfo: if non-null, the signal's siginfo is returned here
2511 * @uts: upper bound on process time suspension
2512 * @sigsetsize: size of sigset_t type
2513 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002514SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2515 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2516 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 int ret, sig;
2519 sigset_t these;
2520 struct timespec ts;
2521 siginfo_t info;
2522 long timeout = 0;
2523
2524 /* XXX: Don't preclude handling different sized sigset_t's. */
2525 if (sigsetsize != sizeof(sigset_t))
2526 return -EINVAL;
2527
2528 if (copy_from_user(&these, uthese, sizeof(these)))
2529 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Invert the set of allowed signals to get those we
2533 * want to block.
2534 */
2535 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2536 signotset(&these);
2537
2538 if (uts) {
2539 if (copy_from_user(&ts, uts, sizeof(ts)))
2540 return -EFAULT;
2541 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2542 || ts.tv_sec < 0)
2543 return -EINVAL;
2544 }
2545
2546 spin_lock_irq(&current->sighand->siglock);
2547 sig = dequeue_signal(current, &these, &info);
2548 if (!sig) {
2549 timeout = MAX_SCHEDULE_TIMEOUT;
2550 if (uts)
2551 timeout = (timespec_to_jiffies(&ts)
2552 + (ts.tv_sec || ts.tv_nsec));
2553
2554 if (timeout) {
Randy Dunlap5aba0852011-04-04 14:59:31 -07002555 /*
2556 * None ready -- temporarily unblock those we're
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 * interested while we are sleeping in so that we'll
Randy Dunlap5aba0852011-04-04 14:59:31 -07002558 * be awakened when they arrive.
2559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 current->real_blocked = current->blocked;
2561 sigandsets(&current->blocked, &current->blocked, &these);
2562 recalc_sigpending();
2563 spin_unlock_irq(&current->sighand->siglock);
2564
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002565 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 spin_lock_irq(&current->sighand->siglock);
2568 sig = dequeue_signal(current, &these, &info);
2569 current->blocked = current->real_blocked;
2570 siginitset(&current->real_blocked, 0);
2571 recalc_sigpending();
2572 }
2573 }
2574 spin_unlock_irq(&current->sighand->siglock);
2575
2576 if (sig) {
2577 ret = sig;
2578 if (uinfo) {
2579 if (copy_siginfo_to_user(uinfo, &info))
2580 ret = -EFAULT;
2581 }
2582 } else {
2583 ret = -EAGAIN;
2584 if (timeout)
2585 ret = -EINTR;
2586 }
2587
2588 return ret;
2589}
2590
Randy Dunlap41c57892011-04-04 15:00:26 -07002591/**
2592 * sys_kill - send a signal to a process
2593 * @pid: the PID of the process
2594 * @sig: signal to be sent
2595 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002596SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
2598 struct siginfo info;
2599
2600 info.si_signo = sig;
2601 info.si_errno = 0;
2602 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002603 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002604 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
2606 return kill_something_info(sig, &info, pid);
2607}
2608
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002609static int
2610do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002611{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002612 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002613 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002614
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002615 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002616 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002617 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002618 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002619 /*
2620 * The null signal is a permissions and process existence
2621 * probe. No signal is actually delivered.
2622 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002623 if (!error && sig) {
2624 error = do_send_sig_info(sig, info, p, false);
2625 /*
2626 * If lock_task_sighand() failed we pretend the task
2627 * dies after receiving the signal. The window is tiny,
2628 * and the signal is private anyway.
2629 */
2630 if (unlikely(error == -ESRCH))
2631 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002632 }
2633 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002634 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002635
2636 return error;
2637}
2638
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002639static int do_tkill(pid_t tgid, pid_t pid, int sig)
2640{
2641 struct siginfo info;
2642
2643 info.si_signo = sig;
2644 info.si_errno = 0;
2645 info.si_code = SI_TKILL;
2646 info.si_pid = task_tgid_vnr(current);
2647 info.si_uid = current_uid();
2648
2649 return do_send_specific(tgid, pid, sig, &info);
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652/**
2653 * sys_tgkill - send signal to one specific thread
2654 * @tgid: the thread group ID of the thread
2655 * @pid: the PID of the thread
2656 * @sig: signal to be sent
2657 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002658 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 * exists but it's not belonging to the target process anymore. This
2660 * method solves the problem of threads exiting and PIDs getting reused.
2661 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002662SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 /* This is only valid for single tasks */
2665 if (pid <= 0 || tgid <= 0)
2666 return -EINVAL;
2667
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002668 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Randy Dunlap41c57892011-04-04 15:00:26 -07002671/**
2672 * sys_tkill - send signal to one specific task
2673 * @pid: the PID of the task
2674 * @sig: signal to be sent
2675 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2677 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002678SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* This is only valid for single tasks */
2681 if (pid <= 0)
2682 return -EINVAL;
2683
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002684 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Randy Dunlap41c57892011-04-04 15:00:26 -07002687/**
2688 * sys_rt_sigqueueinfo - send signal information to a signal
2689 * @pid: the PID of the thread
2690 * @sig: signal to be sent
2691 * @uinfo: signal info to be sent
2692 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002693SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2694 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
2696 siginfo_t info;
2697
2698 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2699 return -EFAULT;
2700
2701 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002702 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2703 */
Roland Dreier243b4222011-03-28 14:13:35 -07002704 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002705 /* We used to allow any < 0 si_code */
2706 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 info.si_signo = sig;
2710
2711 /* POSIX.1b doesn't mention process groups. */
2712 return kill_proc_info(sig, &info, pid);
2713}
2714
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002715long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2716{
2717 /* This is only valid for single tasks */
2718 if (pid <= 0 || tgid <= 0)
2719 return -EINVAL;
2720
2721 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002722 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2723 */
Roland Dreier243b4222011-03-28 14:13:35 -07002724 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002725 /* We used to allow any < 0 si_code */
2726 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002727 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002728 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002729 info->si_signo = sig;
2730
2731 return do_send_specific(tgid, pid, sig, info);
2732}
2733
2734SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2735 siginfo_t __user *, uinfo)
2736{
2737 siginfo_t info;
2738
2739 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2740 return -EFAULT;
2741
2742 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2743}
2744
Oleg Nesterov88531f72006-03-28 16:11:24 -08002745int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002747 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002749 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002751 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return -EINVAL;
2753
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002754 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (oact)
2758 *oact = *k;
2759
2760 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002761 sigdelsetmask(&act->sa.sa_mask,
2762 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002763 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /*
2765 * POSIX 3.3.1.3:
2766 * "Setting a signal action to SIG_IGN for a signal that is
2767 * pending shall cause the pending signal to be discarded,
2768 * whether or not it is blocked."
2769 *
2770 * "Setting a signal action to SIG_DFL for a signal that is
2771 * pending and whose default action is to ignore the signal
2772 * (for example, SIGCHLD), shall cause the pending signal to
2773 * be discarded, whether or not it is blocked"
2774 */
Roland McGrath35de2542008-07-25 19:45:51 -07002775 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002776 sigemptyset(&mask);
2777 sigaddset(&mask, sig);
2778 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002780 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 t = next_thread(t);
2782 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
2785
2786 spin_unlock_irq(&current->sighand->siglock);
2787 return 0;
2788}
2789
2790int
2791do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2792{
2793 stack_t oss;
2794 int error;
2795
Linus Torvalds0083fc22009-08-01 10:34:56 -07002796 oss.ss_sp = (void __user *) current->sas_ss_sp;
2797 oss.ss_size = current->sas_ss_size;
2798 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 if (uss) {
2801 void __user *ss_sp;
2802 size_t ss_size;
2803 int ss_flags;
2804
2805 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002806 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2807 goto out;
2808 error = __get_user(ss_sp, &uss->ss_sp) |
2809 __get_user(ss_flags, &uss->ss_flags) |
2810 __get_user(ss_size, &uss->ss_size);
2811 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 goto out;
2813
2814 error = -EPERM;
2815 if (on_sig_stack(sp))
2816 goto out;
2817
2818 error = -EINVAL;
2819 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002820 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 * old code may have been written using ss_flags==0
2822 * to mean ss_flags==SS_ONSTACK (as this was the only
2823 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002824 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 */
2826 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2827 goto out;
2828
2829 if (ss_flags == SS_DISABLE) {
2830 ss_size = 0;
2831 ss_sp = NULL;
2832 } else {
2833 error = -ENOMEM;
2834 if (ss_size < MINSIGSTKSZ)
2835 goto out;
2836 }
2837
2838 current->sas_ss_sp = (unsigned long) ss_sp;
2839 current->sas_ss_size = ss_size;
2840 }
2841
Linus Torvalds0083fc22009-08-01 10:34:56 -07002842 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 if (uoss) {
2844 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002845 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002847 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2848 __put_user(oss.ss_size, &uoss->ss_size) |
2849 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852out:
2853 return error;
2854}
2855
2856#ifdef __ARCH_WANT_SYS_SIGPENDING
2857
Randy Dunlap41c57892011-04-04 15:00:26 -07002858/**
2859 * sys_sigpending - examine pending signals
2860 * @set: where mask of pending signal is returned
2861 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002862SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 return do_sigpending(set, sizeof(*set));
2865}
2866
2867#endif
2868
2869#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002870/**
2871 * sys_sigprocmask - examine and change blocked signals
2872 * @how: whether to add, remove, or set signals
2873 * @set: signals to add or remove (if non-null)
2874 * @oset: previous value of signal mask if non-null
2875 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002876 * Some platforms have their own version with special arguments;
2877 * others support only sys_rt_sigprocmask.
2878 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002880SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2881 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
2883 int error;
2884 old_sigset_t old_set, new_set;
2885
2886 if (set) {
2887 error = -EFAULT;
2888 if (copy_from_user(&new_set, set, sizeof(*set)))
2889 goto out;
2890 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2891
2892 spin_lock_irq(&current->sighand->siglock);
2893 old_set = current->blocked.sig[0];
2894
2895 error = 0;
2896 switch (how) {
2897 default:
2898 error = -EINVAL;
2899 break;
2900 case SIG_BLOCK:
2901 sigaddsetmask(&current->blocked, new_set);
2902 break;
2903 case SIG_UNBLOCK:
2904 sigdelsetmask(&current->blocked, new_set);
2905 break;
2906 case SIG_SETMASK:
2907 current->blocked.sig[0] = new_set;
2908 break;
2909 }
2910
2911 recalc_sigpending();
2912 spin_unlock_irq(&current->sighand->siglock);
2913 if (error)
2914 goto out;
2915 if (oset)
2916 goto set_old;
2917 } else if (oset) {
2918 old_set = current->blocked.sig[0];
2919 set_old:
2920 error = -EFAULT;
2921 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2922 goto out;
2923 }
2924 error = 0;
2925out:
2926 return error;
2927}
2928#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2929
2930#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002931/**
2932 * sys_rt_sigaction - alter an action taken by a process
2933 * @sig: signal to be sent
2934 * @act: the thread group ID of the thread
2935 * @oact: the PID of the thread
2936 * @sigsetsize: size of sigset_t type
2937 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002938SYSCALL_DEFINE4(rt_sigaction, int, sig,
2939 const struct sigaction __user *, act,
2940 struct sigaction __user *, oact,
2941 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct k_sigaction new_sa, old_sa;
2944 int ret = -EINVAL;
2945
2946 /* XXX: Don't preclude handling different sized sigset_t's. */
2947 if (sigsetsize != sizeof(sigset_t))
2948 goto out;
2949
2950 if (act) {
2951 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2952 return -EFAULT;
2953 }
2954
2955 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2956
2957 if (!ret && oact) {
2958 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2959 return -EFAULT;
2960 }
2961out:
2962 return ret;
2963}
2964#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2965
2966#ifdef __ARCH_WANT_SYS_SGETMASK
2967
2968/*
2969 * For backwards compatibility. Functionality superseded by sigprocmask.
2970 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002971SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
2973 /* SMP safe */
2974 return current->blocked.sig[0];
2975}
2976
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002977SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978{
2979 int old;
2980
2981 spin_lock_irq(&current->sighand->siglock);
2982 old = current->blocked.sig[0];
2983
2984 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2985 sigmask(SIGSTOP)));
2986 recalc_sigpending();
2987 spin_unlock_irq(&current->sighand->siglock);
2988
2989 return old;
2990}
2991#endif /* __ARCH_WANT_SGETMASK */
2992
2993#ifdef __ARCH_WANT_SYS_SIGNAL
2994/*
2995 * For backwards compatibility. Functionality superseded by sigaction.
2996 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002997SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 struct k_sigaction new_sa, old_sa;
3000 int ret;
3001
3002 new_sa.sa.sa_handler = handler;
3003 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03003004 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 ret = do_sigaction(sig, &new_sa, &old_sa);
3007
3008 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3009}
3010#endif /* __ARCH_WANT_SYS_SIGNAL */
3011
3012#ifdef __ARCH_WANT_SYS_PAUSE
3013
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003014SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
3016 current->state = TASK_INTERRUPTIBLE;
3017 schedule();
3018 return -ERESTARTNOHAND;
3019}
3020
3021#endif
3022
David Woodhouse150256d2006-01-18 17:43:57 -08003023#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003024/**
3025 * sys_rt_sigsuspend - replace the signal mask for a value with the
3026 * @unewset value until a signal is received
3027 * @unewset: new signal mask value
3028 * @sigsetsize: size of sigset_t type
3029 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003030SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003031{
3032 sigset_t newset;
3033
3034 /* XXX: Don't preclude handling different sized sigset_t's. */
3035 if (sigsetsize != sizeof(sigset_t))
3036 return -EINVAL;
3037
3038 if (copy_from_user(&newset, unewset, sizeof(newset)))
3039 return -EFAULT;
3040 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3041
3042 spin_lock_irq(&current->sighand->siglock);
3043 current->saved_sigmask = current->blocked;
3044 current->blocked = newset;
3045 recalc_sigpending();
3046 spin_unlock_irq(&current->sighand->siglock);
3047
3048 current->state = TASK_INTERRUPTIBLE;
3049 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003050 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003051 return -ERESTARTNOHAND;
3052}
3053#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3054
David Howellsf269fdd2006-09-27 01:50:23 -07003055__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3056{
3057 return NULL;
3058}
3059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060void __init signals_init(void)
3061{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003062 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003064
3065#ifdef CONFIG_KGDB_KDB
3066#include <linux/kdb.h>
3067/*
3068 * kdb_send_sig_info - Allows kdb to send signals without exposing
3069 * signal internals. This function checks if the required locks are
3070 * available before calling the main signal code, to avoid kdb
3071 * deadlocks.
3072 */
3073void
3074kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3075{
3076 static struct task_struct *kdb_prev_t;
3077 int sig, new_t;
3078 if (!spin_trylock(&t->sighand->siglock)) {
3079 kdb_printf("Can't do kill command now.\n"
3080 "The sigmask lock is held somewhere else in "
3081 "kernel, try again later\n");
3082 return;
3083 }
3084 spin_unlock(&t->sighand->siglock);
3085 new_t = kdb_prev_t != t;
3086 kdb_prev_t = t;
3087 if (t->state != TASK_RUNNING && new_t) {
3088 kdb_printf("Process is not RUNNING, sending a signal from "
3089 "kdb risks deadlock\n"
3090 "on the run queue locks. "
3091 "The signal has _not_ been sent.\n"
3092 "Reissue the kill command if you want to risk "
3093 "the deadlock.\n");
3094 return;
3095 }
3096 sig = info->si_signo;
3097 if (send_sig_info(sig, info, t))
3098 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3099 sig, t->pid);
3100 else
3101 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3102}
3103#endif /* CONFIG_KGDB_KDB */