blob: 9ab91c516c3f9b86cca519d98a167b515cfa8084 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/sched.h>
17#include <linux/fs.h>
18#include <linux/tty.h>
19#include <linux/binfmts.h>
20#include <linux/security.h>
21#include <linux/syscalls.h>
22#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070024#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090025#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070026#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080029#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050031#define CREATE_TRACE_POINTS
32#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/param.h>
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040038#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/*
41 * SLAB caches for signal bits.
42 */
43
Christoph Lametere18b8902006-12-06 20:33:20 -080044static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090046int print_fatal_signals __read_mostly;
47
Roland McGrath35de2542008-07-25 19:45:51 -070048static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070049{
Roland McGrath35de2542008-07-25 19:45:51 -070050 return t->sighand->action[sig - 1].sa.sa_handler;
51}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070052
Roland McGrath35de2542008-07-25 19:45:51 -070053static int sig_handler_ignored(void __user *handler, int sig)
54{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070055 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070056 return handler == SIG_IGN ||
57 (handler == SIG_DFL && sig_kernel_ignore(sig));
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070060static int sig_task_ignored(struct task_struct *t, int sig,
61 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Roland McGrath35de2542008-07-25 19:45:51 -070063 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Oleg Nesterovf008faf2009-04-02 16:58:02 -070065 handler = sig_handler(t, sig);
66
67 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070068 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070069 return 1;
70
71 return sig_handler_ignored(handler, sig);
72}
73
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070074static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070075{
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 /*
77 * Blocked signals are never ignored, since the
78 * signal handler may change by the time it is
79 * unblocked.
80 */
Roland McGrath325d22d2007-11-12 15:41:55 -080081 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return 0;
83
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070084 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070085 return 0;
86
87 /*
88 * Tracers may want to know about even ignored signals.
89 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070090 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * Re-calculate pending state from the set of locally pending
95 * signals, globally pending signals, and blocked signals.
96 */
97static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
98{
99 unsigned long ready;
100 long i;
101
102 switch (_NSIG_WORDS) {
103 default:
104 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
105 ready |= signal->sig[i] &~ blocked->sig[i];
106 break;
107
108 case 4: ready = signal->sig[3] &~ blocked->sig[3];
109 ready |= signal->sig[2] &~ blocked->sig[2];
110 ready |= signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
113
114 case 2: ready = signal->sig[1] &~ blocked->sig[1];
115 ready |= signal->sig[0] &~ blocked->sig[0];
116 break;
117
118 case 1: ready = signal->sig[0] &~ blocked->sig[0];
119 }
120 return ready != 0;
121}
122
123#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
124
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700125static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200127 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700129 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700131 return 1;
132 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700133 /*
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
137 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700138 return 0;
139}
140
141/*
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 */
145void recalc_sigpending_and_wake(struct task_struct *t)
146{
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151void recalc_sigpending(void)
152{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700156 clear_thread_flag(TIF_SIGPENDING);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160/* Given the mask, find the first available signal that should be serviced. */
161
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800162#define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
165
Davide Libenzifba2afa2007-05-10 22:23:13 -0700166int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 unsigned long i, *s, *m, x;
169 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 s = pending->signal.sig;
172 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800173
174 /*
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
177 */
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
184 }
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 switch (_NSIG_WORDS) {
187 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
200 break;
201 sig = ffz(~x) + _NSIG_BPW + 1;
202 break;
203
204 case 1:
205 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 break;
207 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return sig;
210}
211
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900212static inline void print_dropped_signal(int sig)
213{
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
215
216 if (!print_fatal_signals)
217 return;
218
219 if (!__ratelimit(&ratelimit_state))
220 return;
221
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
224}
225
Tejun Heoe5c19022011-03-23 10:37:00 +0100226/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200227 * task_set_jobctl_pending - set jobctl pending bits
228 * @task: target task
229 * @mask: pending bits to set
230 *
231 * Clear @mask from @task->jobctl. @mask must be subset of
232 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
233 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
234 * cleared. If @task is already being killed or exiting, this function
235 * becomes noop.
236 *
237 * CONTEXT:
238 * Must be called with @task->sighand->siglock held.
239 *
240 * RETURNS:
241 * %true if @mask is set, %false if made noop because @task was dying.
242 */
243bool task_set_jobctl_pending(struct task_struct *task, unsigned int mask)
244{
245 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
246 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
247 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
248
249 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
250 return false;
251
252 if (mask & JOBCTL_STOP_SIGMASK)
253 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
254
255 task->jobctl |= mask;
256 return true;
257}
258
259/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200260 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100261 * @task: target task
262 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200263 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
264 * Clear it and wake up the ptracer. Note that we don't need any further
265 * locking. @task->siglock guarantees that @task->parent points to the
266 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100267 *
268 * CONTEXT:
269 * Must be called with @task->sighand->siglock held.
270 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200271static void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100272{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200273 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
274 task->jobctl &= ~JOBCTL_TRAPPING;
Tejun Heo40ae7172011-05-06 11:52:22 +0200275 __wake_up_sync_key(&task->parent->signal->wait_chldexit,
276 TASK_UNINTERRUPTIBLE, 1, task);
Tejun Heod79fdd62011-03-23 10:37:00 +0100277 }
278}
279
280/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200281 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c19022011-03-23 10:37:00 +0100282 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200283 * @mask: pending bits to clear
Tejun Heoe5c19022011-03-23 10:37:00 +0100284 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200285 * Clear @mask from @task->jobctl. @mask must be subset of
286 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
287 * STOP bits are cleared together.
Tejun Heoe5c19022011-03-23 10:37:00 +0100288 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200289 * If clearing of @mask leaves no stop or trap pending, this function calls
290 * task_clear_jobctl_trapping().
291 *
Tejun Heoe5c19022011-03-23 10:37:00 +0100292 * CONTEXT:
293 * Must be called with @task->sighand->siglock held.
294 */
Tejun Heo3759a0d2011-06-02 11:14:00 +0200295void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
Tejun Heoe5c19022011-03-23 10:37:00 +0100296{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200297 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
298
299 if (mask & JOBCTL_STOP_PENDING)
300 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
301
302 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200303
304 if (!(task->jobctl & JOBCTL_PENDING_MASK))
305 task_clear_jobctl_trapping(task);
Tejun Heoe5c19022011-03-23 10:37:00 +0100306}
307
308/**
309 * task_participate_group_stop - participate in a group stop
310 * @task: task participating in a group stop
311 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200312 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100313 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200314 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100315 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100316 *
317 * CONTEXT:
318 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100319 *
320 * RETURNS:
321 * %true if group stop completion should be notified to the parent, %false
322 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100323 */
324static bool task_participate_group_stop(struct task_struct *task)
325{
326 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200327 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100328
Tejun Heoa8f072c2011-06-02 11:13:59 +0200329 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100330
Tejun Heo3759a0d2011-06-02 11:14:00 +0200331 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c19022011-03-23 10:37:00 +0100332
333 if (!consume)
334 return false;
335
336 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
337 sig->group_stop_count--;
338
Tejun Heo244056f2011-03-23 10:37:01 +0100339 /*
340 * Tell the caller to notify completion iff we are entering into a
341 * fresh group stop. Read comment in do_signal_stop() for details.
342 */
343 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100344 sig->flags = SIGNAL_STOP_STOPPED;
345 return true;
346 }
347 return false;
348}
349
David Howellsc69e8d92008-11-14 10:39:19 +1100350/*
351 * allocate a new signal queue record
352 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700353 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100354 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900355static struct sigqueue *
356__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800359 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800361 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000362 * Protect access to @t credentials. This can go away when all
363 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800364 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000365 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100366 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800367 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000368 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800371 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800372 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900374 } else {
375 print_dropped_signal(sig);
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800379 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100380 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 } else {
382 INIT_LIST_HEAD(&q->list);
383 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100384 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
David Howellsd84f4f92008-11-14 10:39:23 +1100386
387 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Andrew Morton514a01b2006-02-03 03:04:41 -0800390static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 if (q->flags & SIGQUEUE_PREALLOC)
393 return;
394 atomic_dec(&q->user->sigpending);
395 free_uid(q->user);
396 kmem_cache_free(sigqueue_cachep, q);
397}
398
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800399void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct sigqueue *q;
402
403 sigemptyset(&queue->signal);
404 while (!list_empty(&queue->list)) {
405 q = list_entry(queue->list.next, struct sigqueue , list);
406 list_del_init(&q->list);
407 __sigqueue_free(q);
408 }
409}
410
411/*
412 * Flush all pending signals for a task.
413 */
David Howells3bcac022009-04-29 13:45:05 +0100414void __flush_signals(struct task_struct *t)
415{
416 clear_tsk_thread_flag(t, TIF_SIGPENDING);
417 flush_sigqueue(&t->pending);
418 flush_sigqueue(&t->signal->shared_pending);
419}
420
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800421void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100426 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spin_unlock_irqrestore(&t->sighand->siglock, flags);
428}
429
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400430static void __flush_itimer_signals(struct sigpending *pending)
431{
432 sigset_t signal, retain;
433 struct sigqueue *q, *n;
434
435 signal = pending->signal;
436 sigemptyset(&retain);
437
438 list_for_each_entry_safe(q, n, &pending->list, list) {
439 int sig = q->info.si_signo;
440
441 if (likely(q->info.si_code != SI_TIMER)) {
442 sigaddset(&retain, sig);
443 } else {
444 sigdelset(&signal, sig);
445 list_del_init(&q->list);
446 __sigqueue_free(q);
447 }
448 }
449
450 sigorsets(&pending->signal, &signal, &retain);
451}
452
453void flush_itimer_signals(void)
454{
455 struct task_struct *tsk = current;
456 unsigned long flags;
457
458 spin_lock_irqsave(&tsk->sighand->siglock, flags);
459 __flush_itimer_signals(&tsk->pending);
460 __flush_itimer_signals(&tsk->signal->shared_pending);
461 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
462}
463
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700464void ignore_signals(struct task_struct *t)
465{
466 int i;
467
468 for (i = 0; i < _NSIG; ++i)
469 t->sighand->action[i].sa.sa_handler = SIG_IGN;
470
471 flush_signals(t);
472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 * Flush all handlers for a task.
476 */
477
478void
479flush_signal_handlers(struct task_struct *t, int force_default)
480{
481 int i;
482 struct k_sigaction *ka = &t->sighand->action[0];
483 for (i = _NSIG ; i != 0 ; i--) {
484 if (force_default || ka->sa.sa_handler != SIG_IGN)
485 ka->sa.sa_handler = SIG_DFL;
486 ka->sa.sa_flags = 0;
487 sigemptyset(&ka->sa.sa_mask);
488 ka++;
489 }
490}
491
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200492int unhandled_signal(struct task_struct *tsk, int sig)
493{
Roland McGrath445a91d2008-07-25 19:45:52 -0700494 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700495 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200496 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700497 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200498 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700499 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200500}
501
Randy Dunlap5aba0852011-04-04 14:59:31 -0700502/*
503 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * process, and wants to be notified if any signals at all were to be
505 * sent/acted upon. If the notifier routine returns non-zero, then the
506 * signal will be acted upon after all. If the notifier routine returns 0,
507 * then then signal will be blocked. Only one block per process is
508 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700509 * can use to determine if the signal should be blocked or not.
510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511void
512block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
513{
514 unsigned long flags;
515
516 spin_lock_irqsave(&current->sighand->siglock, flags);
517 current->notifier_mask = mask;
518 current->notifier_data = priv;
519 current->notifier = notifier;
520 spin_unlock_irqrestore(&current->sighand->siglock, flags);
521}
522
523/* Notify the system that blocking has ended. */
524
525void
526unblock_all_signals(void)
527{
528 unsigned long flags;
529
530 spin_lock_irqsave(&current->sighand->siglock, flags);
531 current->notifier = NULL;
532 current->notifier_data = NULL;
533 recalc_sigpending();
534 spin_unlock_irqrestore(&current->sighand->siglock, flags);
535}
536
Oleg Nesterov100360f2008-07-25 01:47:29 -0700537static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /*
542 * Collect the siginfo appropriate to this signal. Check if
543 * there is another siginfo for the same signal.
544 */
545 list_for_each_entry(q, &list->list, list) {
546 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700547 if (first)
548 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 first = q;
550 }
551 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700552
553 sigdelset(&list->signal, sig);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700556still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 list_del_init(&first->list);
558 copy_siginfo(info, &first->info);
559 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700561 /*
562 * Ok, it wasn't in the queue. This must be
563 * a fast-pathed signal or we must have been
564 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 info->si_signo = sig;
567 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800568 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 info->si_pid = 0;
570 info->si_uid = 0;
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
574static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
575 siginfo_t *info)
576{
Roland McGrath27d91e02006-09-29 02:00:31 -0700577 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 if (sig) {
580 if (current->notifier) {
581 if (sigismember(current->notifier_mask, sig)) {
582 if (!(current->notifier)(current->notifier_data)) {
583 clear_thread_flag(TIF_SIGPENDING);
584 return 0;
585 }
586 }
587 }
588
Oleg Nesterov100360f2008-07-25 01:47:29 -0700589 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 return sig;
593}
594
595/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700596 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 * expected to free it.
598 *
599 * All callers have to hold the siglock.
600 */
601int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
602{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700603 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000604
605 /* We only dequeue private signals from ourselves, we don't let
606 * signalfd steal them
607 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700608 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800609 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 signr = __dequeue_signal(&tsk->signal->shared_pending,
611 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800612 /*
613 * itimer signal ?
614 *
615 * itimers are process shared and we restart periodic
616 * itimers in the signal delivery path to prevent DoS
617 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700618 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800619 * itimers, as the SIGALRM is a legacy signal and only
620 * queued once. Changing the restart behaviour to
621 * restart the timer in the signal dequeue path is
622 * reducing the timer noise on heavy loaded !highres
623 * systems too.
624 */
625 if (unlikely(signr == SIGALRM)) {
626 struct hrtimer *tmr = &tsk->signal->real_timer;
627
628 if (!hrtimer_is_queued(tmr) &&
629 tsk->signal->it_real_incr.tv64 != 0) {
630 hrtimer_forward(tmr, tmr->base->get_time(),
631 tsk->signal->it_real_incr);
632 hrtimer_restart(tmr);
633 }
634 }
635 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700636
Davide Libenzib8fceee2007-09-20 12:40:16 -0700637 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700638 if (!signr)
639 return 0;
640
641 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800642 /*
643 * Set a marker that we have dequeued a stop signal. Our
644 * caller might release the siglock and then the pending
645 * stop signal it is about to process is no longer in the
646 * pending bitmasks, but must still be cleared by a SIGCONT
647 * (and overruled by a SIGKILL). So those cases clear this
648 * shared flag after we've set it. Note that this flag may
649 * remain set after the signal we return is ignored or
650 * handled. That doesn't matter because its only purpose
651 * is to alert stop-signal processing code when another
652 * processor has come along and cleared the flag.
653 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200654 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800655 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700656 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Release the siglock to ensure proper locking order
659 * of timer locks outside of siglocks. Note, we leave
660 * irqs disabled here, since the posix-timers code is
661 * about to disable them again anyway.
662 */
663 spin_unlock(&tsk->sighand->siglock);
664 do_schedule_next_timer(info);
665 spin_lock(&tsk->sighand->siglock);
666 }
667 return signr;
668}
669
670/*
671 * Tell a process that it has a new active signal..
672 *
673 * NOTE! we rely on the previous spin_lock to
674 * lock interrupts for us! We can only be called with
675 * "siglock" held, and the local interrupt must
676 * have been disabled when that got acquired!
677 *
678 * No need to set need_resched since signal event passing
679 * goes through ->blocked
680 */
681void signal_wake_up(struct task_struct *t, int resume)
682{
683 unsigned int mask;
684
685 set_tsk_thread_flag(t, TIF_SIGPENDING);
686
687 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500688 * For SIGKILL, we want to wake it up in the stopped/traced/killable
689 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * executing another processor and just now entering stopped state.
691 * By using wake_up_state, we ensure the process will wake up and
692 * handle its death signal.
693 */
694 mask = TASK_INTERRUPTIBLE;
695 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500696 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!wake_up_state(t, mask))
698 kick_process(t);
699}
700
701/*
702 * Remove signals in mask from the pending set and queue.
703 * Returns 1 if any signals were found.
704 *
705 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800706 *
707 * This version takes a sigset mask and looks at all signals,
708 * not just those in the first mask word.
709 */
710static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
711{
712 struct sigqueue *q, *n;
713 sigset_t m;
714
715 sigandsets(&m, mask, &s->signal);
716 if (sigisemptyset(&m))
717 return 0;
718
Oleg Nesterov702a5072011-04-27 22:01:27 +0200719 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800720 list_for_each_entry_safe(q, n, &s->list, list) {
721 if (sigismember(mask, q->info.si_signo)) {
722 list_del_init(&q->list);
723 __sigqueue_free(q);
724 }
725 }
726 return 1;
727}
728/*
729 * Remove signals in mask from the pending set and queue.
730 * Returns 1 if any signals were found.
731 *
732 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
734static int rm_from_queue(unsigned long mask, struct sigpending *s)
735{
736 struct sigqueue *q, *n;
737
738 if (!sigtestsetmask(&s->signal, mask))
739 return 0;
740
741 sigdelsetmask(&s->signal, mask);
742 list_for_each_entry_safe(q, n, &s->list, list) {
743 if (q->info.si_signo < SIGRTMIN &&
744 (mask & sigmask(q->info.si_signo))) {
745 list_del_init(&q->list);
746 __sigqueue_free(q);
747 }
748 }
749 return 1;
750}
751
Oleg Nesterov614c5172009-12-15 16:47:22 -0800752static inline int is_si_special(const struct siginfo *info)
753{
754 return info <= SEND_SIG_FORCED;
755}
756
757static inline bool si_fromuser(const struct siginfo *info)
758{
759 return info == SEND_SIG_NOINFO ||
760 (!is_si_special(info) && SI_FROMUSER(info));
761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700764 * called with RCU read lock from check_kill_permission()
765 */
766static int kill_ok_by_cred(struct task_struct *t)
767{
768 const struct cred *cred = current_cred();
769 const struct cred *tcred = __task_cred(t);
770
771 if (cred->user->user_ns == tcred->user->user_ns &&
772 (cred->euid == tcred->suid ||
773 cred->euid == tcred->uid ||
774 cred->uid == tcred->suid ||
775 cred->uid == tcred->uid))
776 return 1;
777
778 if (ns_capable(tcred->user->user_ns, CAP_KILL))
779 return 1;
780
781 return 0;
782}
783
784/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100786 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
788static int check_kill_permission(int sig, struct siginfo *info,
789 struct task_struct *t)
790{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700791 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700792 int error;
793
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700794 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700795 return -EINVAL;
796
Oleg Nesterov614c5172009-12-15 16:47:22 -0800797 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700798 return 0;
799
800 error = audit_signal_info(sig, t); /* Let audit system see the signal */
801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400803
Oleg Nesterov065add32010-05-26 14:42:54 -0700804 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700805 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700806 switch (sig) {
807 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700808 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700809 /*
810 * We don't return the error if sid == NULL. The
811 * task was unhashed, the caller must notice this.
812 */
813 if (!sid || sid == task_session(current))
814 break;
815 default:
816 return -EPERM;
817 }
818 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100819
Amy Griffise54dc242007-03-29 18:01:04 -0400820 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700824 * Handle magic process-wide effects of stop/continue signals. Unlike
825 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * time regardless of blocking, ignoring, or handling. This does the
827 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700828 * signals. The process stop is done as a signal action for SIG_DFL.
829 *
830 * Returns true if the signal should be actually delivered, otherwise
831 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700833static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700835 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct task_struct *t;
837
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700838 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700840 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700842 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 /*
844 * This is a stop signal. Remove SIGCONT from all queues.
845 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700846 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 t = p;
848 do {
849 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700850 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700852 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200854 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700856 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 t = p;
858 do {
Tejun Heo3759a0d2011-06-02 11:14:00 +0200859 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Oleg Nesterov1deac632011-04-01 20:11:50 +0200861 wake_up_state(t, __TASK_STOPPED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700862 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700864 /*
865 * Notify the parent with CLD_CONTINUED if we were stopped.
866 *
867 * If we were in the middle of a group stop, we pretend it
868 * was already finished, and then continued. Since SIGCHLD
869 * doesn't queue we report only CLD_STOPPED, as if the next
870 * CLD_CONTINUED was dropped.
871 */
872 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700873 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700874 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700875 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700876 why |= SIGNAL_CLD_STOPPED;
877
878 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700879 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700880 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700881 * will take ->siglock, notice SIGNAL_CLD_MASK, and
882 * notify its parent. See get_signal_to_deliver().
883 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700884 signal->flags = why | SIGNAL_STOP_CONTINUED;
885 signal->group_stop_count = 0;
886 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700889
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700890 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700893/*
894 * Test if P wants to take SIG. After we've checked all threads with this,
895 * it's equivalent to finding no threads not blocking SIG. Any threads not
896 * blocking SIG were ruled out because they are not running and already
897 * have pending signals. Such threads will dequeue from the shared queue
898 * as soon as they're available, so putting the signal on the shared queue
899 * will be equivalent to sending it to one such thread.
900 */
901static inline int wants_signal(int sig, struct task_struct *p)
902{
903 if (sigismember(&p->blocked, sig))
904 return 0;
905 if (p->flags & PF_EXITING)
906 return 0;
907 if (sig == SIGKILL)
908 return 1;
909 if (task_is_stopped_or_traced(p))
910 return 0;
911 return task_curr(p) || !signal_pending(p);
912}
913
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700914static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700915{
916 struct signal_struct *signal = p->signal;
917 struct task_struct *t;
918
919 /*
920 * Now find a thread we can wake up to take the signal off the queue.
921 *
922 * If the main thread wants the signal, it gets first crack.
923 * Probably the least surprising to the average bear.
924 */
925 if (wants_signal(sig, p))
926 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700927 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700928 /*
929 * There is just one thread and it does not need to be woken.
930 * It will dequeue unblocked signals before it runs again.
931 */
932 return;
933 else {
934 /*
935 * Otherwise try to find a suitable thread.
936 */
937 t = signal->curr_target;
938 while (!wants_signal(sig, t)) {
939 t = next_thread(t);
940 if (t == signal->curr_target)
941 /*
942 * No thread needs to be woken.
943 * Any eligible threads will see
944 * the signal in the queue soon.
945 */
946 return;
947 }
948 signal->curr_target = t;
949 }
950
951 /*
952 * Found a killable thread. If the signal will be fatal,
953 * then start taking the whole group down immediately.
954 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700955 if (sig_fatal(p, sig) &&
956 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700957 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700958 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700959 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700960 /*
961 * This signal will be fatal to the whole group.
962 */
963 if (!sig_kernel_coredump(sig)) {
964 /*
965 * Start a group exit and wake everybody up.
966 * This way we don't have other threads
967 * running and doing things after a slower
968 * thread has the fatal signal pending.
969 */
970 signal->flags = SIGNAL_GROUP_EXIT;
971 signal->group_exit_code = sig;
972 signal->group_stop_count = 0;
973 t = p;
974 do {
Tejun Heo6dfca322011-06-02 11:14:00 +0200975 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700976 sigaddset(&t->pending.signal, SIGKILL);
977 signal_wake_up(t, 1);
978 } while_each_thread(p, t);
979 return;
980 }
981 }
982
983 /*
984 * The signal is already in the shared-pending queue.
985 * Tell the chosen thread to wake up and dequeue it.
986 */
987 signal_wake_up(t, sig == SIGKILL);
988 return;
989}
990
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700991static inline int legacy_queue(struct sigpending *signals, int sig)
992{
993 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
994}
995
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700996static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
997 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700999 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001000 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001001 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Masami Hiramatsud1eb6502009-11-24 16:56:45 -05001003 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001004
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001005 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001006
1007 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001008 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001009
1010 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001012 * Short-circuit ignored signals and support queuing
1013 * exactly one non-rt signal, so that we can get more
1014 * detailed information about the cause of the signal.
1015 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001016 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001017 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001018 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * fast-pathed signals for kernel-internal things like SIGSTOP
1020 * or SIGKILL.
1021 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001022 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 goto out_set;
1024
Randy Dunlap5aba0852011-04-04 14:59:31 -07001025 /*
1026 * Real-time signals must be queued if sent by sigqueue, or
1027 * some other real-time mechanism. It is implementation
1028 * defined whether kill() does so. We attempt to do so, on
1029 * the principle of least surprise, but since kill is not
1030 * allowed to fail with EAGAIN when low on memory we just
1031 * make sure at least one signal gets delivered and don't
1032 * pass on the info struct.
1033 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001034 if (sig < SIGRTMIN)
1035 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1036 else
1037 override_rlimit = 0;
1038
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001039 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001040 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001042 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001044 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 q->info.si_signo = sig;
1046 q->info.si_errno = 0;
1047 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001048 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001049 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001050 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001052 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 q->info.si_signo = sig;
1054 q->info.si_errno = 0;
1055 q->info.si_code = SI_KERNEL;
1056 q->info.si_pid = 0;
1057 q->info.si_uid = 0;
1058 break;
1059 default:
1060 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001061 if (from_ancestor_ns)
1062 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 }
Oleg Nesterov621d3122005-10-30 15:03:45 -08001065 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001066 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1067 /*
1068 * Queue overflow, abort. We may abort if the
1069 * signal was rt and sent by user using something
1070 * other than kill().
1071 */
1072 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001074 } else {
1075 /*
1076 * This is a silent loss of information. We still
1077 * send the signal, but the *info bits are lost.
1078 */
1079 trace_signal_lose_info(sig, group, info);
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082
1083out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001084 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001085 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001086 complete_signal(sig, t, group);
1087 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001090static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1091 int group)
1092{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001093 int from_ancestor_ns = 0;
1094
1095#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001096 from_ancestor_ns = si_fromuser(info) &&
1097 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001098#endif
1099
1100 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001101}
1102
Ingo Molnar45807a12007-07-15 23:40:10 -07001103static void print_fatal_signal(struct pt_regs *regs, int signr)
1104{
1105 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001106 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001107
Al Viroca5cd872007-10-29 04:31:16 +00001108#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001109 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001110 {
1111 int i;
1112 for (i = 0; i < 16; i++) {
1113 unsigned char insn;
1114
Andi Kleenb45c6e72010-01-08 14:42:52 -08001115 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1116 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001117 printk("%02x ", insn);
1118 }
1119 }
1120#endif
1121 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001122 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001123 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001124 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001125}
1126
1127static int __init setup_print_fatal_signals(char *str)
1128{
1129 get_option (&str, &print_fatal_signals);
1130
1131 return 1;
1132}
1133
1134__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001136int
1137__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1138{
1139 return send_signal(sig, info, p, 1);
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142static int
1143specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1144{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001145 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001148int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1149 bool group)
1150{
1151 unsigned long flags;
1152 int ret = -ESRCH;
1153
1154 if (lock_task_sighand(p, &flags)) {
1155 ret = send_signal(sig, info, p, group);
1156 unlock_task_sighand(p, &flags);
1157 }
1158
1159 return ret;
1160}
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162/*
1163 * Force a signal that the process can't ignore: if necessary
1164 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001165 *
1166 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1167 * since we do not want to have a signal handler that was blocked
1168 * be invoked when user space had explicitly blocked it.
1169 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001170 * We don't want to have recursive SIGSEGV's etc, for example,
1171 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173int
1174force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1175{
1176 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001177 int ret, blocked, ignored;
1178 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001181 action = &t->sighand->action[sig-1];
1182 ignored = action->sa.sa_handler == SIG_IGN;
1183 blocked = sigismember(&t->blocked, sig);
1184 if (blocked || ignored) {
1185 action->sa.sa_handler = SIG_DFL;
1186 if (blocked) {
1187 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001188 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001191 if (action->sa.sa_handler == SIG_DFL)
1192 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 ret = specific_send_sig_info(sig, info, t);
1194 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1195
1196 return ret;
1197}
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199/*
1200 * Nuke all other threads in the group.
1201 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001202int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001204 struct task_struct *t = p;
1205 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 p->signal->group_stop_count = 0;
1208
Oleg Nesterov09faef12010-05-26 14:43:11 -07001209 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001210 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001211 count++;
1212
1213 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (t->exit_state)
1215 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 signal_wake_up(t, 1);
1218 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001219
1220 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001223struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1224 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001225{
1226 struct sighand_struct *sighand;
1227
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001228 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001229 for (;;) {
1230 sighand = rcu_dereference(tsk->sighand);
1231 if (unlikely(sighand == NULL))
1232 break;
1233
1234 spin_lock_irqsave(&sighand->siglock, *flags);
1235 if (likely(sighand == tsk->sighand))
1236 break;
1237 spin_unlock_irqrestore(&sighand->siglock, *flags);
1238 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001239 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001240
1241 return sighand;
1242}
1243
David Howellsc69e8d92008-11-14 10:39:19 +11001244/*
1245 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1248{
David Howells694f6902010-08-04 16:59:14 +01001249 int ret;
1250
1251 rcu_read_lock();
1252 ret = check_kill_permission(sig, info, p);
1253 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001255 if (!ret && sig)
1256 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 return ret;
1259}
1260
1261/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001262 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001264 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001266int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct task_struct *p = NULL;
1269 int retval, success;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 success = 0;
1272 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001273 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 int err = group_send_sig_info(sig, info, p);
1275 success |= !err;
1276 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001277 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return success ? 0 : retval;
1279}
1280
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001281int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001283 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 struct task_struct *p;
1285
Ingo Molnare56d0902006-01-08 01:01:37 -08001286 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001287retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001288 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001289 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001291 if (unlikely(error == -ESRCH))
1292 /*
1293 * The task was unhashed in between, try again.
1294 * If it is dead, pid_task() will return NULL,
1295 * if we race with de_thread() it will find the
1296 * new leader.
1297 */
1298 goto retry;
1299 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001300 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 return error;
1303}
1304
Randy Dunlap5aba0852011-04-04 14:59:31 -07001305int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001306{
1307 int error;
1308 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001309 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001310 rcu_read_unlock();
1311 return error;
1312}
1313
Eric W. Biederman2425c082006-10-02 02:17:28 -07001314/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1315int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001316 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001317{
1318 int ret = -EINVAL;
1319 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001320 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001321 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001322
1323 if (!valid_signal(sig))
1324 return ret;
1325
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001326 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001327 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001328 if (!p) {
1329 ret = -ESRCH;
1330 goto out_unlock;
1331 }
David Howellsc69e8d92008-11-14 10:39:19 +11001332 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001333 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001334 euid != pcred->suid && euid != pcred->uid &&
1335 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001336 ret = -EPERM;
1337 goto out_unlock;
1338 }
David Quigley8f95dc52006-06-30 01:55:47 -07001339 ret = security_task_kill(p, info, sig, secid);
1340 if (ret)
1341 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001342
1343 if (sig) {
1344 if (lock_task_sighand(p, &flags)) {
1345 ret = __send_signal(sig, info, p, 1, 0);
1346 unlock_task_sighand(p, &flags);
1347 } else
1348 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001349 }
1350out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001351 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001352 return ret;
1353}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001354EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356/*
1357 * kill_something_info() interprets pid in interesting ways just like kill(2).
1358 *
1359 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1360 * is probably wrong. Should make it like BSD or SYSV.
1361 */
1362
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001363static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001365 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001366
1367 if (pid > 0) {
1368 rcu_read_lock();
1369 ret = kill_pid_info(sig, info, find_vpid(pid));
1370 rcu_read_unlock();
1371 return ret;
1372 }
1373
1374 read_lock(&tasklist_lock);
1375 if (pid != -1) {
1376 ret = __kill_pgrp_info(sig, info,
1377 pid ? find_vpid(-pid) : task_pgrp(current));
1378 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int retval = 0, count = 0;
1380 struct task_struct * p;
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001383 if (task_pid_vnr(p) > 1 &&
1384 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 int err = group_send_sig_info(sig, info, p);
1386 ++count;
1387 if (err != -EPERM)
1388 retval = err;
1389 }
1390 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001391 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001393 read_unlock(&tasklist_lock);
1394
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398/*
1399 * These are for backward compatibility with the rest of the kernel source.
1400 */
1401
Randy Dunlap5aba0852011-04-04 14:59:31 -07001402int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 /*
1405 * Make sure legacy kernel users don't send in bad values
1406 * (normal paths check this in check_kill_permission).
1407 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001408 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
1410
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001411 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412}
1413
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001414#define __si_special(priv) \
1415 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417int
1418send_sig(int sig, struct task_struct *p, int priv)
1419{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001420 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423void
1424force_sig(int sig, struct task_struct *p)
1425{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001426 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429/*
1430 * When things go south during signal handling, we
1431 * will force a SIGSEGV. And if the signal that caused
1432 * the problem was already a SIGSEGV, we'll want to
1433 * make sure we don't even try to deliver the signal..
1434 */
1435int
1436force_sigsegv(int sig, struct task_struct *p)
1437{
1438 if (sig == SIGSEGV) {
1439 unsigned long flags;
1440 spin_lock_irqsave(&p->sighand->siglock, flags);
1441 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1442 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1443 }
1444 force_sig(SIGSEGV, p);
1445 return 0;
1446}
1447
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001448int kill_pgrp(struct pid *pid, int sig, int priv)
1449{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001450 int ret;
1451
1452 read_lock(&tasklist_lock);
1453 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1454 read_unlock(&tasklist_lock);
1455
1456 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001457}
1458EXPORT_SYMBOL(kill_pgrp);
1459
1460int kill_pid(struct pid *pid, int sig, int priv)
1461{
1462 return kill_pid_info(sig, __si_special(priv), pid);
1463}
1464EXPORT_SYMBOL(kill_pid);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466/*
1467 * These functions support sending signals using preallocated sigqueue
1468 * structures. This is needed "because realtime applications cannot
1469 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001470 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 * we allocate the sigqueue structure from the timer_create. If this
1472 * allocation fails we are able to report the failure to the application
1473 * with an EAGAIN error.
1474 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475struct sigqueue *sigqueue_alloc(void)
1476{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001477 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001479 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001481
1482 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484
1485void sigqueue_free(struct sigqueue *q)
1486{
1487 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001488 spinlock_t *lock = &current->sighand->siglock;
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1491 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001492 * We must hold ->siglock while testing q->list
1493 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001494 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001496 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001497 q->flags &= ~SIGQUEUE_PREALLOC;
1498 /*
1499 * If it is queued it will be freed when dequeued,
1500 * like the "regular" sigqueue.
1501 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001502 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001503 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001504 spin_unlock_irqrestore(lock, flags);
1505
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001506 if (q)
1507 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001510int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001511{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001512 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001513 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001514 unsigned long flags;
1515 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001516
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001517 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001518
1519 ret = -1;
1520 if (!likely(lock_task_sighand(t, &flags)))
1521 goto ret;
1522
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001523 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001524 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001525 goto out;
1526
1527 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001528 if (unlikely(!list_empty(&q->list))) {
1529 /*
1530 * If an SI_TIMER entry is already queue just increment
1531 * the overrun count.
1532 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001533 BUG_ON(q->info.si_code != SI_TIMER);
1534 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001535 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001536 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001537 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001538
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001539 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001540 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001541 list_add_tail(&q->list, &pending->list);
1542 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001543 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001544out:
1545 unlock_task_sighand(t, &flags);
1546ret:
1547 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001548}
1549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 * Let a parent know about the death of a child.
1552 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001553 *
1554 * Returns -1 if our parent ignored us and so we've switched to
1555 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001557int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
1559 struct siginfo info;
1560 unsigned long flags;
1561 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001562 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
1564 BUG_ON(sig == -1);
1565
1566 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001567 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001569 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1571
1572 info.si_signo = sig;
1573 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001574 /*
1575 * we are under tasklist_lock here so our parent is tied to
1576 * us and cannot exit and release its namespace.
1577 *
1578 * the only it can is to switch its nsproxy with sys_unshare,
1579 * bu uncharing pid namespaces is not allowed, so we'll always
1580 * see relevant namespace
1581 *
1582 * write_lock() currently calls preempt_disable() which is the
1583 * same as rcu_read_lock(), but according to Oleg, this is not
1584 * correct to rely on this
1585 */
1586 rcu_read_lock();
1587 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001588 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001589 rcu_read_unlock();
1590
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001591 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1592 tsk->signal->utime));
1593 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1594 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 info.si_status = tsk->exit_code & 0x7f;
1597 if (tsk->exit_code & 0x80)
1598 info.si_code = CLD_DUMPED;
1599 else if (tsk->exit_code & 0x7f)
1600 info.si_code = CLD_KILLED;
1601 else {
1602 info.si_code = CLD_EXITED;
1603 info.si_status = tsk->exit_code >> 8;
1604 }
1605
1606 psig = tsk->parent->sighand;
1607 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001608 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1610 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1611 /*
1612 * We are exiting and our parent doesn't care. POSIX.1
1613 * defines special semantics for setting SIGCHLD to SIG_IGN
1614 * or setting the SA_NOCLDWAIT flag: we should be reaped
1615 * automatically and not left for our parent's wait4 call.
1616 * Rather than having the parent do it as a magic kind of
1617 * signal handler, we just set this to tell do_exit that we
1618 * can be cleaned up without becoming a zombie. Note that
1619 * we still call __wake_up_parent in this case, because a
1620 * blocked sys_wait4 might now return -ECHILD.
1621 *
1622 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1623 * is implementation-defined: we do (if you don't want
1624 * it, just use SIG_IGN instead).
1625 */
Roland McGrath1b046242008-08-19 20:37:07 -07001626 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001628 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001630 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 __group_send_sig_info(sig, &info, tsk->parent);
1632 __wake_up_parent(tsk, tsk->parent);
1633 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001634
Roland McGrath1b046242008-08-19 20:37:07 -07001635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Tejun Heo75b95952011-03-23 10:37:01 +01001638/**
1639 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1640 * @tsk: task reporting the state change
1641 * @for_ptracer: the notification is for ptracer
1642 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1643 *
1644 * Notify @tsk's parent that the stopped/continued state has changed. If
1645 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1646 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1647 *
1648 * CONTEXT:
1649 * Must be called with tasklist_lock at least read locked.
1650 */
1651static void do_notify_parent_cldstop(struct task_struct *tsk,
1652 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct siginfo info;
1655 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001656 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct sighand_struct *sighand;
1658
Tejun Heo75b95952011-03-23 10:37:01 +01001659 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001660 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001661 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001662 tsk = tsk->group_leader;
1663 parent = tsk->real_parent;
1664 }
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 info.si_signo = SIGCHLD;
1667 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001668 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001669 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001670 */
1671 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001672 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001673 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001674 rcu_read_unlock();
1675
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001676 info.si_utime = cputime_to_clock_t(tsk->utime);
1677 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 info.si_code = why;
1680 switch (why) {
1681 case CLD_CONTINUED:
1682 info.si_status = SIGCONT;
1683 break;
1684 case CLD_STOPPED:
1685 info.si_status = tsk->signal->group_exit_code & 0x7f;
1686 break;
1687 case CLD_TRAPPED:
1688 info.si_status = tsk->exit_code & 0x7f;
1689 break;
1690 default:
1691 BUG();
1692 }
1693
1694 sighand = parent->sighand;
1695 spin_lock_irqsave(&sighand->siglock, flags);
1696 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1697 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1698 __group_send_sig_info(SIGCHLD, &info, parent);
1699 /*
1700 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1701 */
1702 __wake_up_parent(tsk, parent);
1703 spin_unlock_irqrestore(&sighand->siglock, flags);
1704}
1705
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001706static inline int may_ptrace_stop(void)
1707{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001708 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001709 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001710 /*
1711 * Are we in the middle of do_coredump?
1712 * If so and our tracer is also part of the coredump stopping
1713 * is a deadlock situation, and pointless because our tracer
1714 * is dead so don't allow us to stop.
1715 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001716 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001717 * is safe to enter schedule().
1718 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001719 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001720 unlikely(current->mm == current->parent->mm))
1721 return 0;
1722
1723 return 1;
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001727 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001728 * Called with the siglock held.
1729 */
1730static int sigkill_pending(struct task_struct *tsk)
1731{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001732 return sigismember(&tsk->pending.signal, SIGKILL) ||
1733 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001734}
1735
1736/*
Tejun Heoceb6bd62011-03-23 10:37:01 +01001737 * Test whether the target task of the usual cldstop notification - the
1738 * real_parent of @child - is in the same group as the ptracer.
1739 */
1740static bool real_parent_is_ptracer(struct task_struct *child)
1741{
1742 return same_thread_group(child->parent, child->real_parent);
1743}
1744
1745/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * This must be called with current->sighand->siglock held.
1747 *
1748 * This should be the path for all ptrace stops.
1749 * We always set current->last_siginfo while stopped here.
1750 * That makes it a way to test a stopped process for
1751 * being ptrace-stopped vs being job-control-stopped.
1752 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001753 * If we actually decide not to stop at all because the tracer
1754 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001756static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001757 __releases(&current->sighand->siglock)
1758 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001760 bool gstop_done = false;
1761
Roland McGrath1a669c22008-02-06 01:37:37 -08001762 if (arch_ptrace_stop_needed(exit_code, info)) {
1763 /*
1764 * The arch code has something special to do before a
1765 * ptrace stop. This is allowed to block, e.g. for faults
1766 * on user stack pages. We can't keep the siglock while
1767 * calling arch_ptrace_stop, so we must release it now.
1768 * To preserve proper semantics, we must do this before
1769 * any signal bookkeeping like checking group_stop_count.
1770 * Meanwhile, a SIGKILL could come in before we retake the
1771 * siglock. That must prevent us from sleeping in TASK_TRACED.
1772 * So after regaining the lock, we must check for SIGKILL.
1773 */
1774 spin_unlock_irq(&current->sighand->siglock);
1775 arch_ptrace_stop(exit_code, info);
1776 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001777 if (sigkill_pending(current))
1778 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001779 }
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001782 * We're committing to trapping. TRACED should be visible before
1783 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1784 * Also, transition to TRACED and updates to ->jobctl should be
1785 * atomic with respect to siglock and should be done after the arch
1786 * hook as siglock is released and regrabbed across it.
1787 */
1788 set_current_state(TASK_TRACED);
1789
1790 current->last_siginfo = info;
1791 current->exit_code = exit_code;
1792
1793 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001794 * If @why is CLD_STOPPED, we're trapping to participate in a group
1795 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1796 * while siglock was released for the arch hook, PENDING could be
1797 * clear now. We act as if SIGCONT is received after TASK_TRACED
1798 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001800 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001801 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Tejun Heo81be24b2011-06-02 11:13:59 +02001803 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001804 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 spin_unlock_irq(&current->sighand->siglock);
1807 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001808 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001809 /*
1810 * Notify parents of the stop.
1811 *
1812 * While ptraced, there are two parents - the ptracer and
1813 * the real_parent of the group_leader. The ptracer should
1814 * know about every stop while the real parent is only
1815 * interested in the completion of group stop. The states
1816 * for the two don't interact with each other. Notify
1817 * separately unless they're gonna be duplicates.
1818 */
1819 do_notify_parent_cldstop(current, true, why);
1820 if (gstop_done && !real_parent_is_ptracer(current))
1821 do_notify_parent_cldstop(current, false, why);
1822
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001823 /*
1824 * Don't want to allow preemption here, because
1825 * sys_ptrace() needs this task to be inactive.
1826 *
1827 * XXX: implement read_unlock_no_resched().
1828 */
1829 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001831 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 schedule();
1833 } else {
1834 /*
1835 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001836 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001837 *
1838 * If @gstop_done, the ptracer went away between group stop
1839 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001840 * JOBCTL_STOP_PENDING on us and we'll re-enter
1841 * TASK_STOPPED in do_signal_stop() on return, so notifying
1842 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001844 if (gstop_done)
1845 do_notify_parent_cldstop(current, false, why);
1846
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001847 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001848 if (clear_code)
1849 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001850 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852
1853 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001854 * While in TASK_TRACED, we were considered "frozen enough".
1855 * Now that we woke up, it's crucial if we're supposed to be
1856 * frozen that we freeze now before running anything substantial.
1857 */
1858 try_to_freeze();
1859
1860 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * We are back. Now reacquire the siglock before touching
1862 * last_siginfo, so that we are sure to have synchronized with
1863 * any signal-sending on another CPU that wants to examine it.
1864 */
1865 spin_lock_irq(&current->sighand->siglock);
1866 current->last_siginfo = NULL;
1867
1868 /*
1869 * Queued signals ignored us while we were stopped for tracing.
1870 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001871 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001873 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
1876void ptrace_notify(int exit_code)
1877{
1878 siginfo_t info;
1879
1880 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1881
1882 memset(&info, 0, sizeof info);
1883 info.si_signo = SIGTRAP;
1884 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001885 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001886 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* Let the debugger run. */
1889 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001890 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 spin_unlock_irq(&current->sighand->siglock);
1892}
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894/*
1895 * This performs the stopping for SIGSTOP and other stop signals.
1896 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001897 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 * Returns zero if we didn't stop and still hold the siglock.
1899 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001900static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Tejun Heoa8f072c2011-06-02 11:13:59 +02001904 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1905 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001906 struct task_struct *t;
1907
Tejun Heoa8f072c2011-06-02 11:13:59 +02001908 /* signr will be recorded in task->jobctl for retries */
1909 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001910
Tejun Heoa8f072c2011-06-02 11:13:59 +02001911 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001912 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001915 * There is no group stop already in progress. We must
1916 * initiate one now.
1917 *
1918 * While ptraced, a task may be resumed while group stop is
1919 * still in effect and then receive a stop signal and
1920 * initiate another group stop. This deviates from the
1921 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001922 * cause two group stops when !ptraced. That is why we
1923 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001924 *
1925 * The condition can be distinguished by testing whether
1926 * SIGNAL_STOP_STOPPED is already set. Don't generate
1927 * group_exit_code in such case.
1928 *
1929 * This is not necessary for SIGNAL_STOP_CONTINUED because
1930 * an intervening stop signal is required to cause two
1931 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001933 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1934 sig->group_exit_code = signr;
1935 else
1936 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001937
Tejun Heo7dd3db52011-06-02 11:14:00 +02001938 sig->group_stop_count = 0;
1939
1940 if (task_set_jobctl_pending(current, signr | gstop))
1941 sig->group_stop_count++;
1942
Tejun Heod79fdd62011-03-23 10:37:00 +01001943 for (t = next_thread(current); t != current;
1944 t = next_thread(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08001945 /*
1946 * Setting state to TASK_STOPPED for a group
1947 * stop is always done with the siglock held,
1948 * so this check has no races.
1949 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02001950 if (!task_is_stopped(t) &&
1951 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001952 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001953 signal_wake_up(t, 0);
1954 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001955 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001956 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001957retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001958 if (likely(!task_ptrace(current))) {
1959 int notify = 0;
1960
1961 /*
1962 * If there are no other threads in the group, or if there
1963 * is a group stop in progress and we are the last to stop,
1964 * report to the parent.
1965 */
1966 if (task_participate_group_stop(current))
1967 notify = CLD_STOPPED;
1968
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001969 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001970 spin_unlock_irq(&current->sighand->siglock);
1971
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001972 /*
1973 * Notify the parent of the group stop completion. Because
1974 * we're not holding either the siglock or tasklist_lock
1975 * here, ptracer may attach inbetween; however, this is for
1976 * group stop and should always be delivered to the real
1977 * parent of the group leader. The new ptracer will get
1978 * its notification when this task transitions into
1979 * TASK_TRACED.
1980 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001981 if (notify) {
1982 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001983 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001984 read_unlock(&tasklist_lock);
1985 }
1986
1987 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1988 schedule();
1989
1990 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001991 } else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001992 ptrace_stop(current->jobctl & JOBCTL_STOP_SIGMASK,
Tejun Heod79fdd62011-03-23 10:37:00 +01001993 CLD_STOPPED, 0, NULL);
1994 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001995 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001996
1997 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001998 * JOBCTL_STOP_PENDING could be set if another group stop has
Tejun Heod79fdd62011-03-23 10:37:00 +01001999 * started since being woken up or ptrace wants us to transit
2000 * between TASK_STOPPED and TRACED. Retry group stop.
2001 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002002 if (current->jobctl & JOBCTL_STOP_PENDING) {
2003 WARN_ON_ONCE(!(current->jobctl & JOBCTL_STOP_SIGMASK));
Tejun Heod79fdd62011-03-23 10:37:00 +01002004 goto retry;
2005 }
2006
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002007 spin_unlock_irq(&current->sighand->siglock);
2008
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002009 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002010
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 return 1;
2012}
2013
Roland McGrath18c98b62008-04-17 18:44:38 -07002014static int ptrace_signal(int signr, siginfo_t *info,
2015 struct pt_regs *regs, void *cookie)
2016{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07002017 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07002018 return signr;
2019
2020 ptrace_signal_deliver(regs, cookie);
2021
2022 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01002023 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002024
2025 /* We're back. Did the debugger cancel the sig? */
2026 signr = current->exit_code;
2027 if (signr == 0)
2028 return signr;
2029
2030 current->exit_code = 0;
2031
Randy Dunlap5aba0852011-04-04 14:59:31 -07002032 /*
2033 * Update the siginfo structure if the signal has
2034 * changed. If the debugger wanted something
2035 * specific in the siginfo structure then it should
2036 * have updated *info via PTRACE_SETSIGINFO.
2037 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002038 if (signr != info->si_signo) {
2039 info->si_signo = signr;
2040 info->si_errno = 0;
2041 info->si_code = SI_USER;
2042 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11002043 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002044 }
2045
2046 /* If the (new) signal is now blocked, requeue it. */
2047 if (sigismember(&current->blocked, signr)) {
2048 specific_send_sig_info(signr, info, current);
2049 signr = 0;
2050 }
2051
2052 return signr;
2053}
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2056 struct pt_regs *regs, void *cookie)
2057{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002058 struct sighand_struct *sighand = current->sighand;
2059 struct signal_struct *signal = current->signal;
2060 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002062relock:
2063 /*
2064 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2065 * While in TASK_STOPPED, we were considered "frozen enough".
2066 * Now that we woke up, it's crucial if we're supposed to be
2067 * frozen that we freeze now before running anything substantial.
2068 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002069 try_to_freeze();
2070
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002071 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002072 /*
2073 * Every stopped thread goes here after wakeup. Check to see if
2074 * we should notify the parent, prepare_signal(SIGCONT) encodes
2075 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2076 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002077 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002078 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002079 int why;
2080
2081 if (signal->flags & SIGNAL_CLD_CONTINUED)
2082 why = CLD_CONTINUED;
2083 else
2084 why = CLD_STOPPED;
2085
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002086 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002087
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002088 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002089
Tejun Heoceb6bd62011-03-23 10:37:01 +01002090 /*
2091 * Notify the parent that we're continuing. This event is
2092 * always per-process and doesn't make whole lot of sense
2093 * for ptracers, who shouldn't consume the state via
2094 * wait(2) either, but, for backward compatibility, notify
2095 * the ptracer of the group leader too unless it's gonna be
2096 * a duplicate.
2097 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002098 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002099
2100 do_notify_parent_cldstop(current, false, why);
2101
Tejun Heo75b95952011-03-23 10:37:01 +01002102 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002103 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2104 do_notify_parent_cldstop(leader, true, why);
2105
Tejun Heoedf2ed12011-03-23 10:37:00 +01002106 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002107
Oleg Nesterove4420552008-04-30 00:52:44 -07002108 goto relock;
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 for (;;) {
2112 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002113 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002114 * Tracing can induce an artificial signal and choose sigaction.
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002115 * The return value in @signr determines the default action,
2116 * but @info->si_signo is the signal number we will report.
2117 */
2118 signr = tracehook_get_signal(current, regs, info, return_ka);
2119 if (unlikely(signr < 0))
2120 goto relock;
2121 if (unlikely(signr != 0))
2122 ka = return_ka;
2123 else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02002124 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2125 do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002126 goto relock;
2127
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002128 signr = dequeue_signal(current, &current->blocked,
2129 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Roland McGrath18c98b62008-04-17 18:44:38 -07002131 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002132 break; /* will return 0 */
2133
2134 if (signr != SIGKILL) {
2135 signr = ptrace_signal(signr, info,
2136 regs, cookie);
2137 if (!signr)
2138 continue;
2139 }
2140
2141 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002144 /* Trace actually delivered signals. */
2145 trace_signal_deliver(signr, info, ka);
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2148 continue;
2149 if (ka->sa.sa_handler != SIG_DFL) {
2150 /* Run the handler. */
2151 *return_ka = *ka;
2152
2153 if (ka->sa.sa_flags & SA_ONESHOT)
2154 ka->sa.sa_handler = SIG_DFL;
2155
2156 break; /* will return non-zero "signr" value */
2157 }
2158
2159 /*
2160 * Now we are doing the default action for this signal.
2161 */
2162 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2163 continue;
2164
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002165 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002166 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002167 * Container-init gets no signals it doesn't want from same
2168 * container.
2169 *
2170 * Note that if global/container-init sees a sig_kernel_only()
2171 * signal here, the signal must have been generated internally
2172 * or must have come from an ancestor namespace. In either
2173 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002174 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002175 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002176 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 continue;
2178
2179 if (sig_kernel_stop(signr)) {
2180 /*
2181 * The default action is to stop all threads in
2182 * the thread group. The job control signals
2183 * do nothing in an orphaned pgrp, but SIGSTOP
2184 * always works. Note that siglock needs to be
2185 * dropped during the call to is_orphaned_pgrp()
2186 * because of lock ordering with tasklist_lock.
2187 * This allows an intervening SIGCONT to be posted.
2188 * We need to check for that and bail out if necessary.
2189 */
2190 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002191 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 /* signals can be posted during this window */
2194
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002195 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto relock;
2197
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002198 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
2200
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002201 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /* It released the siglock. */
2203 goto relock;
2204 }
2205
2206 /*
2207 * We didn't actually stop, due to a race
2208 * with SIGCONT or something like that.
2209 */
2210 continue;
2211 }
2212
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002213 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /*
2216 * Anything else is fatal, maybe with a core dump.
2217 */
2218 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002221 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002222 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /*
2224 * If it was able to dump core, this kills all
2225 * other threads in the group and synchronizes with
2226 * their demise. If we lost the race with another
2227 * thread getting here, it set group_exit_code
2228 * first and our do_group_exit call below will use
2229 * that value and ignore the one we pass it.
2230 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002231 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233
2234 /*
2235 * Death signals, no core dump.
2236 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002237 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /* NOTREACHED */
2239 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002240 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return signr;
2242}
2243
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002244/*
2245 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002246 * group-wide signal. Other threads should be notified now to take
2247 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002248 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002249static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002250{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002251 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002252 struct task_struct *t;
2253
Oleg Nesterovf646e222011-04-27 19:18:39 +02002254 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2255 if (sigisemptyset(&retarget))
2256 return;
2257
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002258 t = tsk;
2259 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002260 if (t->flags & PF_EXITING)
2261 continue;
2262
2263 if (!has_pending_signals(&retarget, &t->blocked))
2264 continue;
2265 /* Remove the signals this thread can handle. */
2266 sigandsets(&retarget, &retarget, &t->blocked);
2267
2268 if (!signal_pending(t))
2269 signal_wake_up(t, 0);
2270
2271 if (sigisemptyset(&retarget))
2272 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002273 }
2274}
2275
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002276void exit_signals(struct task_struct *tsk)
2277{
2278 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002279 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002280
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002281 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2282 tsk->flags |= PF_EXITING;
2283 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002284 }
2285
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002286 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002287 /*
2288 * From now this task is not visible for group-wide signals,
2289 * see wants_signal(), do_signal_stop().
2290 */
2291 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002292 if (!signal_pending(tsk))
2293 goto out;
2294
Oleg Nesterovf646e222011-04-27 19:18:39 +02002295 unblocked = tsk->blocked;
2296 signotset(&unblocked);
2297 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002298
Tejun Heoa8f072c2011-06-02 11:13:59 +02002299 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002300 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002301 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002302out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002303 spin_unlock_irq(&tsk->sighand->siglock);
2304
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002305 /*
2306 * If group stop has completed, deliver the notification. This
2307 * should always go to the real parent of the group leader.
2308 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002309 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002310 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002311 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002312 read_unlock(&tasklist_lock);
2313 }
2314}
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316EXPORT_SYMBOL(recalc_sigpending);
2317EXPORT_SYMBOL_GPL(dequeue_signal);
2318EXPORT_SYMBOL(flush_signals);
2319EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320EXPORT_SYMBOL(send_sig);
2321EXPORT_SYMBOL(send_sig_info);
2322EXPORT_SYMBOL(sigprocmask);
2323EXPORT_SYMBOL(block_all_signals);
2324EXPORT_SYMBOL(unblock_all_signals);
2325
2326
2327/*
2328 * System call entry points.
2329 */
2330
Randy Dunlap41c57892011-04-04 15:00:26 -07002331/**
2332 * sys_restart_syscall - restart a system call
2333 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002334SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 struct restart_block *restart = &current_thread_info()->restart_block;
2337 return restart->fn(restart);
2338}
2339
2340long do_no_restart_syscall(struct restart_block *param)
2341{
2342 return -EINTR;
2343}
2344
Oleg Nesterovb1828012011-04-27 21:56:14 +02002345static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2346{
2347 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2348 sigset_t newblocked;
2349 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002350 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002351 retarget_shared_pending(tsk, &newblocked);
2352 }
2353 tsk->blocked = *newset;
2354 recalc_sigpending();
2355}
2356
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002357/**
2358 * set_current_blocked - change current->blocked mask
2359 * @newset: new mask
2360 *
2361 * It is wrong to change ->blocked directly, this helper should be used
2362 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002364void set_current_blocked(const sigset_t *newset)
2365{
2366 struct task_struct *tsk = current;
2367
2368 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002369 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002370 spin_unlock_irq(&tsk->sighand->siglock);
2371}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373/*
2374 * This is also useful for kernel threads that want to temporarily
2375 * (or permanently) block certain signals.
2376 *
2377 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2378 * interface happily blocks "unblockable" signals like SIGKILL
2379 * and friends.
2380 */
2381int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2382{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002383 struct task_struct *tsk = current;
2384 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002386 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002387 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002388 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002389
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 switch (how) {
2391 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002392 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 break;
2394 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002395 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 break;
2397 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002398 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 break;
2400 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002403
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002404 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
Randy Dunlap41c57892011-04-04 15:00:26 -07002408/**
2409 * sys_rt_sigprocmask - change the list of currently blocked signals
2410 * @how: whether to add, remove, or set signals
2411 * @set: stores pending signals
2412 * @oset: previous value of signal mask if non-null
2413 * @sigsetsize: size of sigset_t type
2414 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002415SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002416 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002419 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 /* XXX: Don't preclude handling different sized sigset_t's. */
2422 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002425 old_set = current->blocked;
2426
2427 if (nset) {
2428 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2429 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2431
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002432 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002434 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002436
2437 if (oset) {
2438 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2439 return -EFAULT;
2440 }
2441
2442 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445long do_sigpending(void __user *set, unsigned long sigsetsize)
2446{
2447 long error = -EINVAL;
2448 sigset_t pending;
2449
2450 if (sigsetsize > sizeof(sigset_t))
2451 goto out;
2452
2453 spin_lock_irq(&current->sighand->siglock);
2454 sigorsets(&pending, &current->pending.signal,
2455 &current->signal->shared_pending.signal);
2456 spin_unlock_irq(&current->sighand->siglock);
2457
2458 /* Outside the lock because only this thread touches it. */
2459 sigandsets(&pending, &current->blocked, &pending);
2460
2461 error = -EFAULT;
2462 if (!copy_to_user(set, &pending, sigsetsize))
2463 error = 0;
2464
2465out:
2466 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Randy Dunlap41c57892011-04-04 15:00:26 -07002469/**
2470 * sys_rt_sigpending - examine a pending signal that has been raised
2471 * while blocked
2472 * @set: stores pending signals
2473 * @sigsetsize: size of sigset_t type or larger
2474 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002475SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477 return do_sigpending(set, sigsetsize);
2478}
2479
2480#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2481
2482int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2483{
2484 int err;
2485
2486 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2487 return -EFAULT;
2488 if (from->si_code < 0)
2489 return __copy_to_user(to, from, sizeof(siginfo_t))
2490 ? -EFAULT : 0;
2491 /*
2492 * If you change siginfo_t structure, please be sure
2493 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002494 * Please remember to update the signalfd_copyinfo() function
2495 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 * It should never copy any pad contained in the structure
2497 * to avoid security leaks, but must copy the generic
2498 * 3 ints plus the relevant union member.
2499 */
2500 err = __put_user(from->si_signo, &to->si_signo);
2501 err |= __put_user(from->si_errno, &to->si_errno);
2502 err |= __put_user((short)from->si_code, &to->si_code);
2503 switch (from->si_code & __SI_MASK) {
2504 case __SI_KILL:
2505 err |= __put_user(from->si_pid, &to->si_pid);
2506 err |= __put_user(from->si_uid, &to->si_uid);
2507 break;
2508 case __SI_TIMER:
2509 err |= __put_user(from->si_tid, &to->si_tid);
2510 err |= __put_user(from->si_overrun, &to->si_overrun);
2511 err |= __put_user(from->si_ptr, &to->si_ptr);
2512 break;
2513 case __SI_POLL:
2514 err |= __put_user(from->si_band, &to->si_band);
2515 err |= __put_user(from->si_fd, &to->si_fd);
2516 break;
2517 case __SI_FAULT:
2518 err |= __put_user(from->si_addr, &to->si_addr);
2519#ifdef __ARCH_SI_TRAPNO
2520 err |= __put_user(from->si_trapno, &to->si_trapno);
2521#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002522#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002523 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002524 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002525 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002526 */
2527 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2528 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2529#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 break;
2531 case __SI_CHLD:
2532 err |= __put_user(from->si_pid, &to->si_pid);
2533 err |= __put_user(from->si_uid, &to->si_uid);
2534 err |= __put_user(from->si_status, &to->si_status);
2535 err |= __put_user(from->si_utime, &to->si_utime);
2536 err |= __put_user(from->si_stime, &to->si_stime);
2537 break;
2538 case __SI_RT: /* This is not generated by the kernel as of now. */
2539 case __SI_MESGQ: /* But this is */
2540 err |= __put_user(from->si_pid, &to->si_pid);
2541 err |= __put_user(from->si_uid, &to->si_uid);
2542 err |= __put_user(from->si_ptr, &to->si_ptr);
2543 break;
2544 default: /* this is just in case for now ... */
2545 err |= __put_user(from->si_pid, &to->si_pid);
2546 err |= __put_user(from->si_uid, &to->si_uid);
2547 break;
2548 }
2549 return err;
2550}
2551
2552#endif
2553
Randy Dunlap41c57892011-04-04 15:00:26 -07002554/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002555 * do_sigtimedwait - wait for queued signals specified in @which
2556 * @which: queued signals to wait for
2557 * @info: if non-null, the signal's siginfo is returned here
2558 * @ts: upper bound on process time suspension
2559 */
2560int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2561 const struct timespec *ts)
2562{
2563 struct task_struct *tsk = current;
2564 long timeout = MAX_SCHEDULE_TIMEOUT;
2565 sigset_t mask = *which;
2566 int sig;
2567
2568 if (ts) {
2569 if (!timespec_valid(ts))
2570 return -EINVAL;
2571 timeout = timespec_to_jiffies(ts);
2572 /*
2573 * We can be close to the next tick, add another one
2574 * to ensure we will wait at least the time asked for.
2575 */
2576 if (ts->tv_sec || ts->tv_nsec)
2577 timeout++;
2578 }
2579
2580 /*
2581 * Invert the set of allowed signals to get those we want to block.
2582 */
2583 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2584 signotset(&mask);
2585
2586 spin_lock_irq(&tsk->sighand->siglock);
2587 sig = dequeue_signal(tsk, &mask, info);
2588 if (!sig && timeout) {
2589 /*
2590 * None ready, temporarily unblock those we're interested
2591 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002592 * they arrive. Unblocking is always fine, we can avoid
2593 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002594 */
2595 tsk->real_blocked = tsk->blocked;
2596 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2597 recalc_sigpending();
2598 spin_unlock_irq(&tsk->sighand->siglock);
2599
2600 timeout = schedule_timeout_interruptible(timeout);
2601
2602 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002603 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002604 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002605 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002606 }
2607 spin_unlock_irq(&tsk->sighand->siglock);
2608
2609 if (sig)
2610 return sig;
2611 return timeout ? -EINTR : -EAGAIN;
2612}
2613
2614/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002615 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2616 * in @uthese
2617 * @uthese: queued signals to wait for
2618 * @uinfo: if non-null, the signal's siginfo is returned here
2619 * @uts: upper bound on process time suspension
2620 * @sigsetsize: size of sigset_t type
2621 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002622SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2623 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2624 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 sigset_t these;
2627 struct timespec ts;
2628 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002629 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 /* XXX: Don't preclude handling different sized sigset_t's. */
2632 if (sigsetsize != sizeof(sigset_t))
2633 return -EINVAL;
2634
2635 if (copy_from_user(&these, uthese, sizeof(these)))
2636 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (uts) {
2639 if (copy_from_user(&ts, uts, sizeof(ts)))
2640 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
2642
Oleg Nesterov943df142011-04-27 21:44:14 +02002643 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
Oleg Nesterov943df142011-04-27 21:44:14 +02002645 if (ret > 0 && uinfo) {
2646 if (copy_siginfo_to_user(uinfo, &info))
2647 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649
2650 return ret;
2651}
2652
Randy Dunlap41c57892011-04-04 15:00:26 -07002653/**
2654 * sys_kill - send a signal to a process
2655 * @pid: the PID of the process
2656 * @sig: signal to be sent
2657 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002658SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 struct siginfo info;
2661
2662 info.si_signo = sig;
2663 info.si_errno = 0;
2664 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002665 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002666 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 return kill_something_info(sig, &info, pid);
2669}
2670
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002671static int
2672do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002673{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002674 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002675 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002676
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002677 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002678 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002679 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002680 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002681 /*
2682 * The null signal is a permissions and process existence
2683 * probe. No signal is actually delivered.
2684 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002685 if (!error && sig) {
2686 error = do_send_sig_info(sig, info, p, false);
2687 /*
2688 * If lock_task_sighand() failed we pretend the task
2689 * dies after receiving the signal. The window is tiny,
2690 * and the signal is private anyway.
2691 */
2692 if (unlikely(error == -ESRCH))
2693 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002694 }
2695 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002696 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002697
2698 return error;
2699}
2700
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002701static int do_tkill(pid_t tgid, pid_t pid, int sig)
2702{
2703 struct siginfo info;
2704
2705 info.si_signo = sig;
2706 info.si_errno = 0;
2707 info.si_code = SI_TKILL;
2708 info.si_pid = task_tgid_vnr(current);
2709 info.si_uid = current_uid();
2710
2711 return do_send_specific(tgid, pid, sig, &info);
2712}
2713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714/**
2715 * sys_tgkill - send signal to one specific thread
2716 * @tgid: the thread group ID of the thread
2717 * @pid: the PID of the thread
2718 * @sig: signal to be sent
2719 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002720 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 * exists but it's not belonging to the target process anymore. This
2722 * method solves the problem of threads exiting and PIDs getting reused.
2723 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002724SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /* This is only valid for single tasks */
2727 if (pid <= 0 || tgid <= 0)
2728 return -EINVAL;
2729
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002730 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Randy Dunlap41c57892011-04-04 15:00:26 -07002733/**
2734 * sys_tkill - send signal to one specific task
2735 * @pid: the PID of the task
2736 * @sig: signal to be sent
2737 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2739 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002740SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* This is only valid for single tasks */
2743 if (pid <= 0)
2744 return -EINVAL;
2745
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002746 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
Randy Dunlap41c57892011-04-04 15:00:26 -07002749/**
2750 * sys_rt_sigqueueinfo - send signal information to a signal
2751 * @pid: the PID of the thread
2752 * @sig: signal to be sent
2753 * @uinfo: signal info to be sent
2754 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002755SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2756 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
2758 siginfo_t info;
2759
2760 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2761 return -EFAULT;
2762
2763 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002764 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2765 */
Roland Dreier243b4222011-03-28 14:13:35 -07002766 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002767 /* We used to allow any < 0 si_code */
2768 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 info.si_signo = sig;
2772
2773 /* POSIX.1b doesn't mention process groups. */
2774 return kill_proc_info(sig, &info, pid);
2775}
2776
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002777long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2778{
2779 /* This is only valid for single tasks */
2780 if (pid <= 0 || tgid <= 0)
2781 return -EINVAL;
2782
2783 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002784 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2785 */
Roland Dreier243b4222011-03-28 14:13:35 -07002786 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002787 /* We used to allow any < 0 si_code */
2788 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002789 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002790 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002791 info->si_signo = sig;
2792
2793 return do_send_specific(tgid, pid, sig, info);
2794}
2795
2796SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2797 siginfo_t __user *, uinfo)
2798{
2799 siginfo_t info;
2800
2801 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2802 return -EFAULT;
2803
2804 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2805}
2806
Oleg Nesterov88531f72006-03-28 16:11:24 -08002807int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002809 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002811 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002813 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return -EINVAL;
2815
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002816 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (oact)
2820 *oact = *k;
2821
2822 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002823 sigdelsetmask(&act->sa.sa_mask,
2824 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002825 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /*
2827 * POSIX 3.3.1.3:
2828 * "Setting a signal action to SIG_IGN for a signal that is
2829 * pending shall cause the pending signal to be discarded,
2830 * whether or not it is blocked."
2831 *
2832 * "Setting a signal action to SIG_DFL for a signal that is
2833 * pending and whose default action is to ignore the signal
2834 * (for example, SIGCHLD), shall cause the pending signal to
2835 * be discarded, whether or not it is blocked"
2836 */
Roland McGrath35de2542008-07-25 19:45:51 -07002837 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002838 sigemptyset(&mask);
2839 sigaddset(&mask, sig);
2840 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002842 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 t = next_thread(t);
2844 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
2848 spin_unlock_irq(&current->sighand->siglock);
2849 return 0;
2850}
2851
2852int
2853do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2854{
2855 stack_t oss;
2856 int error;
2857
Linus Torvalds0083fc22009-08-01 10:34:56 -07002858 oss.ss_sp = (void __user *) current->sas_ss_sp;
2859 oss.ss_size = current->sas_ss_size;
2860 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 if (uss) {
2863 void __user *ss_sp;
2864 size_t ss_size;
2865 int ss_flags;
2866
2867 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002868 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2869 goto out;
2870 error = __get_user(ss_sp, &uss->ss_sp) |
2871 __get_user(ss_flags, &uss->ss_flags) |
2872 __get_user(ss_size, &uss->ss_size);
2873 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto out;
2875
2876 error = -EPERM;
2877 if (on_sig_stack(sp))
2878 goto out;
2879
2880 error = -EINVAL;
2881 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002882 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 * old code may have been written using ss_flags==0
2884 * to mean ss_flags==SS_ONSTACK (as this was the only
2885 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002886 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 */
2888 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2889 goto out;
2890
2891 if (ss_flags == SS_DISABLE) {
2892 ss_size = 0;
2893 ss_sp = NULL;
2894 } else {
2895 error = -ENOMEM;
2896 if (ss_size < MINSIGSTKSZ)
2897 goto out;
2898 }
2899
2900 current->sas_ss_sp = (unsigned long) ss_sp;
2901 current->sas_ss_size = ss_size;
2902 }
2903
Linus Torvalds0083fc22009-08-01 10:34:56 -07002904 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (uoss) {
2906 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002907 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002909 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2910 __put_user(oss.ss_size, &uoss->ss_size) |
2911 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914out:
2915 return error;
2916}
2917
2918#ifdef __ARCH_WANT_SYS_SIGPENDING
2919
Randy Dunlap41c57892011-04-04 15:00:26 -07002920/**
2921 * sys_sigpending - examine pending signals
2922 * @set: where mask of pending signal is returned
2923 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002924SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
2926 return do_sigpending(set, sizeof(*set));
2927}
2928
2929#endif
2930
2931#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002932/**
2933 * sys_sigprocmask - examine and change blocked signals
2934 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02002935 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07002936 * @oset: previous value of signal mask if non-null
2937 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002938 * Some platforms have their own version with special arguments;
2939 * others support only sys_rt_sigprocmask.
2940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Oleg Nesterovb013c392011-04-28 11:36:20 +02002942SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002943 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002946 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Oleg Nesterovb013c392011-04-28 11:36:20 +02002948 old_set = current->blocked.sig[0];
2949
2950 if (nset) {
2951 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2952 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2954
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002955 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002959 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 break;
2961 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002962 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 break;
2964 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002965 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002967 default:
2968 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002971 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02002973
2974 if (oset) {
2975 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2976 return -EFAULT;
2977 }
2978
2979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2982
2983#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002984/**
2985 * sys_rt_sigaction - alter an action taken by a process
2986 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07002987 * @act: new sigaction
2988 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07002989 * @sigsetsize: size of sigset_t type
2990 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002991SYSCALL_DEFINE4(rt_sigaction, int, sig,
2992 const struct sigaction __user *, act,
2993 struct sigaction __user *, oact,
2994 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
2996 struct k_sigaction new_sa, old_sa;
2997 int ret = -EINVAL;
2998
2999 /* XXX: Don't preclude handling different sized sigset_t's. */
3000 if (sigsetsize != sizeof(sigset_t))
3001 goto out;
3002
3003 if (act) {
3004 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3005 return -EFAULT;
3006 }
3007
3008 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3009
3010 if (!ret && oact) {
3011 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3012 return -EFAULT;
3013 }
3014out:
3015 return ret;
3016}
3017#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
3018
3019#ifdef __ARCH_WANT_SYS_SGETMASK
3020
3021/*
3022 * For backwards compatibility. Functionality superseded by sigprocmask.
3023 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003024SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
3026 /* SMP safe */
3027 return current->blocked.sig[0];
3028}
3029
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003030SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 int old;
3033
3034 spin_lock_irq(&current->sighand->siglock);
3035 old = current->blocked.sig[0];
3036
3037 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
3038 sigmask(SIGSTOP)));
3039 recalc_sigpending();
3040 spin_unlock_irq(&current->sighand->siglock);
3041
3042 return old;
3043}
3044#endif /* __ARCH_WANT_SGETMASK */
3045
3046#ifdef __ARCH_WANT_SYS_SIGNAL
3047/*
3048 * For backwards compatibility. Functionality superseded by sigaction.
3049 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003050SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
3052 struct k_sigaction new_sa, old_sa;
3053 int ret;
3054
3055 new_sa.sa.sa_handler = handler;
3056 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03003057 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 ret = do_sigaction(sig, &new_sa, &old_sa);
3060
3061 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3062}
3063#endif /* __ARCH_WANT_SYS_SIGNAL */
3064
3065#ifdef __ARCH_WANT_SYS_PAUSE
3066
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003067SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003069 while (!signal_pending(current)) {
3070 current->state = TASK_INTERRUPTIBLE;
3071 schedule();
3072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return -ERESTARTNOHAND;
3074}
3075
3076#endif
3077
David Woodhouse150256d2006-01-18 17:43:57 -08003078#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003079/**
3080 * sys_rt_sigsuspend - replace the signal mask for a value with the
3081 * @unewset value until a signal is received
3082 * @unewset: new signal mask value
3083 * @sigsetsize: size of sigset_t type
3084 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003085SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003086{
3087 sigset_t newset;
3088
3089 /* XXX: Don't preclude handling different sized sigset_t's. */
3090 if (sigsetsize != sizeof(sigset_t))
3091 return -EINVAL;
3092
3093 if (copy_from_user(&newset, unewset, sizeof(newset)))
3094 return -EFAULT;
3095 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3096
3097 spin_lock_irq(&current->sighand->siglock);
3098 current->saved_sigmask = current->blocked;
3099 current->blocked = newset;
3100 recalc_sigpending();
3101 spin_unlock_irq(&current->sighand->siglock);
3102
3103 current->state = TASK_INTERRUPTIBLE;
3104 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003105 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003106 return -ERESTARTNOHAND;
3107}
3108#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3109
David Howellsf269fdd2006-09-27 01:50:23 -07003110__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3111{
3112 return NULL;
3113}
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115void __init signals_init(void)
3116{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003117 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003119
3120#ifdef CONFIG_KGDB_KDB
3121#include <linux/kdb.h>
3122/*
3123 * kdb_send_sig_info - Allows kdb to send signals without exposing
3124 * signal internals. This function checks if the required locks are
3125 * available before calling the main signal code, to avoid kdb
3126 * deadlocks.
3127 */
3128void
3129kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3130{
3131 static struct task_struct *kdb_prev_t;
3132 int sig, new_t;
3133 if (!spin_trylock(&t->sighand->siglock)) {
3134 kdb_printf("Can't do kill command now.\n"
3135 "The sigmask lock is held somewhere else in "
3136 "kernel, try again later\n");
3137 return;
3138 }
3139 spin_unlock(&t->sighand->siglock);
3140 new_t = kdb_prev_t != t;
3141 kdb_prev_t = t;
3142 if (t->state != TASK_RUNNING && new_t) {
3143 kdb_printf("Process is not RUNNING, sending a signal from "
3144 "kdb risks deadlock\n"
3145 "on the run queue locks. "
3146 "The signal has _not_ been sent.\n"
3147 "Reissue the kill command if you want to risk "
3148 "the deadlock.\n");
3149 return;
3150 }
3151 sig = info->si_signo;
3152 if (send_sig_info(sig, info, t))
3153 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3154 sig, t->pid);
3155 else
3156 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3157}
3158#endif /* CONFIG_KGDB_KDB */