blob: 172a4c79f12c10f286c7a9b47513d4834ba948e1 [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 Heo62c124f2011-06-02 11:14:00 +0200275 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100276 }
277}
278
279/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200280 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c19022011-03-23 10:37:00 +0100281 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200282 * @mask: pending bits to clear
Tejun Heoe5c19022011-03-23 10:37:00 +0100283 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200284 * Clear @mask from @task->jobctl. @mask must be subset of
285 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
286 * STOP bits are cleared together.
Tejun Heoe5c19022011-03-23 10:37:00 +0100287 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200288 * If clearing of @mask leaves no stop or trap pending, this function calls
289 * task_clear_jobctl_trapping().
290 *
Tejun Heoe5c19022011-03-23 10:37:00 +0100291 * CONTEXT:
292 * Must be called with @task->sighand->siglock held.
293 */
Tejun Heo3759a0d2011-06-02 11:14:00 +0200294void task_clear_jobctl_pending(struct task_struct *task, unsigned int mask)
Tejun Heoe5c19022011-03-23 10:37:00 +0100295{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200296 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
297
298 if (mask & JOBCTL_STOP_PENDING)
299 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
300
301 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200302
303 if (!(task->jobctl & JOBCTL_PENDING_MASK))
304 task_clear_jobctl_trapping(task);
Tejun Heoe5c19022011-03-23 10:37:00 +0100305}
306
307/**
308 * task_participate_group_stop - participate in a group stop
309 * @task: task participating in a group stop
310 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200311 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100312 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200313 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100314 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100315 *
316 * CONTEXT:
317 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100318 *
319 * RETURNS:
320 * %true if group stop completion should be notified to the parent, %false
321 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100322 */
323static bool task_participate_group_stop(struct task_struct *task)
324{
325 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200326 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100327
Tejun Heoa8f072c2011-06-02 11:13:59 +0200328 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100329
Tejun Heo3759a0d2011-06-02 11:14:00 +0200330 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c19022011-03-23 10:37:00 +0100331
332 if (!consume)
333 return false;
334
335 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
336 sig->group_stop_count--;
337
Tejun Heo244056f2011-03-23 10:37:01 +0100338 /*
339 * Tell the caller to notify completion iff we are entering into a
340 * fresh group stop. Read comment in do_signal_stop() for details.
341 */
342 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Tejun Heoe5c19022011-03-23 10:37:00 +0100343 sig->flags = SIGNAL_STOP_STOPPED;
344 return true;
345 }
346 return false;
347}
348
David Howellsc69e8d92008-11-14 10:39:19 +1100349/*
350 * allocate a new signal queue record
351 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700352 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100353 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900354static struct sigqueue *
355__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800358 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800360 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000361 * Protect access to @t credentials. This can go away when all
362 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800363 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000364 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100365 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800366 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000367 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800370 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800371 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900373 } else {
374 print_dropped_signal(sig);
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800378 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100379 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 } else {
381 INIT_LIST_HEAD(&q->list);
382 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100383 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
David Howellsd84f4f92008-11-14 10:39:23 +1100385
386 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Andrew Morton514a01b2006-02-03 03:04:41 -0800389static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 if (q->flags & SIGQUEUE_PREALLOC)
392 return;
393 atomic_dec(&q->user->sigpending);
394 free_uid(q->user);
395 kmem_cache_free(sigqueue_cachep, q);
396}
397
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800398void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct sigqueue *q;
401
402 sigemptyset(&queue->signal);
403 while (!list_empty(&queue->list)) {
404 q = list_entry(queue->list.next, struct sigqueue , list);
405 list_del_init(&q->list);
406 __sigqueue_free(q);
407 }
408}
409
410/*
411 * Flush all pending signals for a task.
412 */
David Howells3bcac022009-04-29 13:45:05 +0100413void __flush_signals(struct task_struct *t)
414{
415 clear_tsk_thread_flag(t, TIF_SIGPENDING);
416 flush_sigqueue(&t->pending);
417 flush_sigqueue(&t->signal->shared_pending);
418}
419
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800420void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 unsigned long flags;
423
424 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100425 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 spin_unlock_irqrestore(&t->sighand->siglock, flags);
427}
428
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400429static void __flush_itimer_signals(struct sigpending *pending)
430{
431 sigset_t signal, retain;
432 struct sigqueue *q, *n;
433
434 signal = pending->signal;
435 sigemptyset(&retain);
436
437 list_for_each_entry_safe(q, n, &pending->list, list) {
438 int sig = q->info.si_signo;
439
440 if (likely(q->info.si_code != SI_TIMER)) {
441 sigaddset(&retain, sig);
442 } else {
443 sigdelset(&signal, sig);
444 list_del_init(&q->list);
445 __sigqueue_free(q);
446 }
447 }
448
449 sigorsets(&pending->signal, &signal, &retain);
450}
451
452void flush_itimer_signals(void)
453{
454 struct task_struct *tsk = current;
455 unsigned long flags;
456
457 spin_lock_irqsave(&tsk->sighand->siglock, flags);
458 __flush_itimer_signals(&tsk->pending);
459 __flush_itimer_signals(&tsk->signal->shared_pending);
460 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
461}
462
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700463void ignore_signals(struct task_struct *t)
464{
465 int i;
466
467 for (i = 0; i < _NSIG; ++i)
468 t->sighand->action[i].sa.sa_handler = SIG_IGN;
469
470 flush_signals(t);
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * Flush all handlers for a task.
475 */
476
477void
478flush_signal_handlers(struct task_struct *t, int force_default)
479{
480 int i;
481 struct k_sigaction *ka = &t->sighand->action[0];
482 for (i = _NSIG ; i != 0 ; i--) {
483 if (force_default || ka->sa.sa_handler != SIG_IGN)
484 ka->sa.sa_handler = SIG_DFL;
485 ka->sa.sa_flags = 0;
486 sigemptyset(&ka->sa.sa_mask);
487 ka++;
488 }
489}
490
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200491int unhandled_signal(struct task_struct *tsk, int sig)
492{
Roland McGrath445a91d2008-07-25 19:45:52 -0700493 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700494 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200495 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700496 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200497 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700498 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200499}
500
Randy Dunlap5aba0852011-04-04 14:59:31 -0700501/*
502 * Notify the system that a driver wants to block all signals for this
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * process, and wants to be notified if any signals at all were to be
504 * sent/acted upon. If the notifier routine returns non-zero, then the
505 * signal will be acted upon after all. If the notifier routine returns 0,
506 * then then signal will be blocked. Only one block per process is
507 * allowed. priv is a pointer to private data that the notifier routine
Randy Dunlap5aba0852011-04-04 14:59:31 -0700508 * can use to determine if the signal should be blocked or not.
509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510void
511block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
512{
513 unsigned long flags;
514
515 spin_lock_irqsave(&current->sighand->siglock, flags);
516 current->notifier_mask = mask;
517 current->notifier_data = priv;
518 current->notifier = notifier;
519 spin_unlock_irqrestore(&current->sighand->siglock, flags);
520}
521
522/* Notify the system that blocking has ended. */
523
524void
525unblock_all_signals(void)
526{
527 unsigned long flags;
528
529 spin_lock_irqsave(&current->sighand->siglock, flags);
530 current->notifier = NULL;
531 current->notifier_data = NULL;
532 recalc_sigpending();
533 spin_unlock_irqrestore(&current->sighand->siglock, flags);
534}
535
Oleg Nesterov100360f2008-07-25 01:47:29 -0700536static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /*
541 * Collect the siginfo appropriate to this signal. Check if
542 * there is another siginfo for the same signal.
543 */
544 list_for_each_entry(q, &list->list, list) {
545 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700546 if (first)
547 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 first = q;
549 }
550 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700551
552 sigdelset(&list->signal, sig);
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700555still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 list_del_init(&first->list);
557 copy_siginfo(info, &first->info);
558 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700560 /*
561 * Ok, it wasn't in the queue. This must be
562 * a fast-pathed signal or we must have been
563 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 info->si_signo = sig;
566 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800567 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 info->si_pid = 0;
569 info->si_uid = 0;
570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
574 siginfo_t *info)
575{
Roland McGrath27d91e02006-09-29 02:00:31 -0700576 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (sig) {
579 if (current->notifier) {
580 if (sigismember(current->notifier_mask, sig)) {
581 if (!(current->notifier)(current->notifier_data)) {
582 clear_thread_flag(TIF_SIGPENDING);
583 return 0;
584 }
585 }
586 }
587
Oleg Nesterov100360f2008-07-25 01:47:29 -0700588 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 return sig;
592}
593
594/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700595 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 * expected to free it.
597 *
598 * All callers have to hold the siglock.
599 */
600int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
601{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700602 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000603
604 /* We only dequeue private signals from ourselves, we don't let
605 * signalfd steal them
606 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700607 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800608 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 signr = __dequeue_signal(&tsk->signal->shared_pending,
610 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800611 /*
612 * itimer signal ?
613 *
614 * itimers are process shared and we restart periodic
615 * itimers in the signal delivery path to prevent DoS
616 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700617 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800618 * itimers, as the SIGALRM is a legacy signal and only
619 * queued once. Changing the restart behaviour to
620 * restart the timer in the signal dequeue path is
621 * reducing the timer noise on heavy loaded !highres
622 * systems too.
623 */
624 if (unlikely(signr == SIGALRM)) {
625 struct hrtimer *tmr = &tsk->signal->real_timer;
626
627 if (!hrtimer_is_queued(tmr) &&
628 tsk->signal->it_real_incr.tv64 != 0) {
629 hrtimer_forward(tmr, tmr->base->get_time(),
630 tsk->signal->it_real_incr);
631 hrtimer_restart(tmr);
632 }
633 }
634 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700635
Davide Libenzib8fceee2007-09-20 12:40:16 -0700636 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700637 if (!signr)
638 return 0;
639
640 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800641 /*
642 * Set a marker that we have dequeued a stop signal. Our
643 * caller might release the siglock and then the pending
644 * stop signal it is about to process is no longer in the
645 * pending bitmasks, but must still be cleared by a SIGCONT
646 * (and overruled by a SIGKILL). So those cases clear this
647 * shared flag after we've set it. Note that this flag may
648 * remain set after the signal we return is ignored or
649 * handled. That doesn't matter because its only purpose
650 * is to alert stop-signal processing code when another
651 * processor has come along and cleared the flag.
652 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200653 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800654 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700655 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Release the siglock to ensure proper locking order
658 * of timer locks outside of siglocks. Note, we leave
659 * irqs disabled here, since the posix-timers code is
660 * about to disable them again anyway.
661 */
662 spin_unlock(&tsk->sighand->siglock);
663 do_schedule_next_timer(info);
664 spin_lock(&tsk->sighand->siglock);
665 }
666 return signr;
667}
668
669/*
670 * Tell a process that it has a new active signal..
671 *
672 * NOTE! we rely on the previous spin_lock to
673 * lock interrupts for us! We can only be called with
674 * "siglock" held, and the local interrupt must
675 * have been disabled when that got acquired!
676 *
677 * No need to set need_resched since signal event passing
678 * goes through ->blocked
679 */
680void signal_wake_up(struct task_struct *t, int resume)
681{
682 unsigned int mask;
683
684 set_tsk_thread_flag(t, TIF_SIGPENDING);
685
686 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500687 * For SIGKILL, we want to wake it up in the stopped/traced/killable
688 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * executing another processor and just now entering stopped state.
690 * By using wake_up_state, we ensure the process will wake up and
691 * handle its death signal.
692 */
693 mask = TASK_INTERRUPTIBLE;
694 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500695 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!wake_up_state(t, mask))
697 kick_process(t);
698}
699
700/*
701 * Remove signals in mask from the pending set and queue.
702 * Returns 1 if any signals were found.
703 *
704 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800705 *
706 * This version takes a sigset mask and looks at all signals,
707 * not just those in the first mask word.
708 */
709static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
710{
711 struct sigqueue *q, *n;
712 sigset_t m;
713
714 sigandsets(&m, mask, &s->signal);
715 if (sigisemptyset(&m))
716 return 0;
717
Oleg Nesterov702a5072011-04-27 22:01:27 +0200718 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800719 list_for_each_entry_safe(q, n, &s->list, list) {
720 if (sigismember(mask, q->info.si_signo)) {
721 list_del_init(&q->list);
722 __sigqueue_free(q);
723 }
724 }
725 return 1;
726}
727/*
728 * Remove signals in mask from the pending set and queue.
729 * Returns 1 if any signals were found.
730 *
731 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733static int rm_from_queue(unsigned long mask, struct sigpending *s)
734{
735 struct sigqueue *q, *n;
736
737 if (!sigtestsetmask(&s->signal, mask))
738 return 0;
739
740 sigdelsetmask(&s->signal, mask);
741 list_for_each_entry_safe(q, n, &s->list, list) {
742 if (q->info.si_signo < SIGRTMIN &&
743 (mask & sigmask(q->info.si_signo))) {
744 list_del_init(&q->list);
745 __sigqueue_free(q);
746 }
747 }
748 return 1;
749}
750
Oleg Nesterov614c5172009-12-15 16:47:22 -0800751static inline int is_si_special(const struct siginfo *info)
752{
753 return info <= SEND_SIG_FORCED;
754}
755
756static inline bool si_fromuser(const struct siginfo *info)
757{
758 return info == SEND_SIG_NOINFO ||
759 (!is_si_special(info) && SI_FROMUSER(info));
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700763 * called with RCU read lock from check_kill_permission()
764 */
765static int kill_ok_by_cred(struct task_struct *t)
766{
767 const struct cred *cred = current_cred();
768 const struct cred *tcred = __task_cred(t);
769
770 if (cred->user->user_ns == tcred->user->user_ns &&
771 (cred->euid == tcred->suid ||
772 cred->euid == tcred->uid ||
773 cred->uid == tcred->suid ||
774 cred->uid == tcred->uid))
775 return 1;
776
777 if (ns_capable(tcred->user->user_ns, CAP_KILL))
778 return 1;
779
780 return 0;
781}
782
783/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100785 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787static int check_kill_permission(int sig, struct siginfo *info,
788 struct task_struct *t)
789{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700790 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700791 int error;
792
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700793 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700794 return -EINVAL;
795
Oleg Nesterov614c5172009-12-15 16:47:22 -0800796 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700797 return 0;
798
799 error = audit_signal_info(sig, t); /* Let audit system see the signal */
800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400802
Oleg Nesterov065add32010-05-26 14:42:54 -0700803 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700804 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700805 switch (sig) {
806 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700807 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700808 /*
809 * We don't return the error if sid == NULL. The
810 * task was unhashed, the caller must notice this.
811 */
812 if (!sid || sid == task_session(current))
813 break;
814 default:
815 return -EPERM;
816 }
817 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100818
Amy Griffise54dc242007-03-29 18:01:04 -0400819 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700823 * Handle magic process-wide effects of stop/continue signals. Unlike
824 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * time regardless of blocking, ignoring, or handling. This does the
826 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700827 * signals. The process stop is done as a signal action for SIG_DFL.
828 *
829 * Returns true if the signal should be actually delivered, otherwise
830 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700832static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700834 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 struct task_struct *t;
836
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700837 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700839 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700841 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 /*
843 * This is a stop signal. Remove SIGCONT from all queues.
844 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700845 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 t = p;
847 do {
848 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700849 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700851 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200853 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700855 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 t = p;
857 do {
Tejun Heo3759a0d2011-06-02 11:14:00 +0200858 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Oleg Nesterov1deac632011-04-01 20:11:50 +0200860 wake_up_state(t, __TASK_STOPPED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700861 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700863 /*
864 * Notify the parent with CLD_CONTINUED if we were stopped.
865 *
866 * If we were in the middle of a group stop, we pretend it
867 * was already finished, and then continued. Since SIGCHLD
868 * doesn't queue we report only CLD_STOPPED, as if the next
869 * CLD_CONTINUED was dropped.
870 */
871 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700872 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700873 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700874 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700875 why |= SIGNAL_CLD_STOPPED;
876
877 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700878 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700879 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700880 * will take ->siglock, notice SIGNAL_CLD_MASK, and
881 * notify its parent. See get_signal_to_deliver().
882 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700883 signal->flags = why | SIGNAL_STOP_CONTINUED;
884 signal->group_stop_count = 0;
885 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700888
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700889 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700892/*
893 * Test if P wants to take SIG. After we've checked all threads with this,
894 * it's equivalent to finding no threads not blocking SIG. Any threads not
895 * blocking SIG were ruled out because they are not running and already
896 * have pending signals. Such threads will dequeue from the shared queue
897 * as soon as they're available, so putting the signal on the shared queue
898 * will be equivalent to sending it to one such thread.
899 */
900static inline int wants_signal(int sig, struct task_struct *p)
901{
902 if (sigismember(&p->blocked, sig))
903 return 0;
904 if (p->flags & PF_EXITING)
905 return 0;
906 if (sig == SIGKILL)
907 return 1;
908 if (task_is_stopped_or_traced(p))
909 return 0;
910 return task_curr(p) || !signal_pending(p);
911}
912
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700913static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700914{
915 struct signal_struct *signal = p->signal;
916 struct task_struct *t;
917
918 /*
919 * Now find a thread we can wake up to take the signal off the queue.
920 *
921 * If the main thread wants the signal, it gets first crack.
922 * Probably the least surprising to the average bear.
923 */
924 if (wants_signal(sig, p))
925 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700926 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700927 /*
928 * There is just one thread and it does not need to be woken.
929 * It will dequeue unblocked signals before it runs again.
930 */
931 return;
932 else {
933 /*
934 * Otherwise try to find a suitable thread.
935 */
936 t = signal->curr_target;
937 while (!wants_signal(sig, t)) {
938 t = next_thread(t);
939 if (t == signal->curr_target)
940 /*
941 * No thread needs to be woken.
942 * Any eligible threads will see
943 * the signal in the queue soon.
944 */
945 return;
946 }
947 signal->curr_target = t;
948 }
949
950 /*
951 * Found a killable thread. If the signal will be fatal,
952 * then start taking the whole group down immediately.
953 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700954 if (sig_fatal(p, sig) &&
955 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700956 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700957 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700958 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700959 /*
960 * This signal will be fatal to the whole group.
961 */
962 if (!sig_kernel_coredump(sig)) {
963 /*
964 * Start a group exit and wake everybody up.
965 * This way we don't have other threads
966 * running and doing things after a slower
967 * thread has the fatal signal pending.
968 */
969 signal->flags = SIGNAL_GROUP_EXIT;
970 signal->group_exit_code = sig;
971 signal->group_stop_count = 0;
972 t = p;
973 do {
Tejun Heo6dfca322011-06-02 11:14:00 +0200974 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700975 sigaddset(&t->pending.signal, SIGKILL);
976 signal_wake_up(t, 1);
977 } while_each_thread(p, t);
978 return;
979 }
980 }
981
982 /*
983 * The signal is already in the shared-pending queue.
984 * Tell the chosen thread to wake up and dequeue it.
985 */
986 signal_wake_up(t, sig == SIGKILL);
987 return;
988}
989
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700990static inline int legacy_queue(struct sigpending *signals, int sig)
991{
992 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
993}
994
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700995static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
996 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700998 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700999 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001000 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Masami Hiramatsud1eb6502009-11-24 16:56:45 -05001002 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001003
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001004 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001005
1006 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001007 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001008
1009 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001011 * Short-circuit ignored signals and support queuing
1012 * exactly one non-rt signal, so that we can get more
1013 * detailed information about the cause of the signal.
1014 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001015 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001016 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001017 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * fast-pathed signals for kernel-internal things like SIGSTOP
1019 * or SIGKILL.
1020 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001021 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto out_set;
1023
Randy Dunlap5aba0852011-04-04 14:59:31 -07001024 /*
1025 * Real-time signals must be queued if sent by sigqueue, or
1026 * some other real-time mechanism. It is implementation
1027 * defined whether kill() does so. We attempt to do so, on
1028 * the principle of least surprise, but since kill is not
1029 * allowed to fail with EAGAIN when low on memory we just
1030 * make sure at least one signal gets delivered and don't
1031 * pass on the info struct.
1032 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001033 if (sig < SIGRTMIN)
1034 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1035 else
1036 override_rlimit = 0;
1037
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001038 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001039 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001041 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001043 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 q->info.si_signo = sig;
1045 q->info.si_errno = 0;
1046 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001047 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001048 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +11001049 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001051 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 q->info.si_signo = sig;
1053 q->info.si_errno = 0;
1054 q->info.si_code = SI_KERNEL;
1055 q->info.si_pid = 0;
1056 q->info.si_uid = 0;
1057 break;
1058 default:
1059 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001060 if (from_ancestor_ns)
1061 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 break;
1063 }
Oleg Nesterov621d3122005-10-30 15:03:45 -08001064 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001065 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1066 /*
1067 * Queue overflow, abort. We may abort if the
1068 * signal was rt and sent by user using something
1069 * other than kill().
1070 */
1071 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001073 } else {
1074 /*
1075 * This is a silent loss of information. We still
1076 * send the signal, but the *info bits are lost.
1077 */
1078 trace_signal_lose_info(sig, group, info);
1079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
1082out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001083 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001084 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001085 complete_signal(sig, t, group);
1086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001089static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1090 int group)
1091{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001092 int from_ancestor_ns = 0;
1093
1094#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001095 from_ancestor_ns = si_fromuser(info) &&
1096 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001097#endif
1098
1099 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001100}
1101
Ingo Molnar45807a12007-07-15 23:40:10 -07001102static void print_fatal_signal(struct pt_regs *regs, int signr)
1103{
1104 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001105 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001106
Al Viroca5cd872007-10-29 04:31:16 +00001107#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +01001108 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001109 {
1110 int i;
1111 for (i = 0; i < 16; i++) {
1112 unsigned char insn;
1113
Andi Kleenb45c6e72010-01-08 14:42:52 -08001114 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1115 break;
Ingo Molnar45807a12007-07-15 23:40:10 -07001116 printk("%02x ", insn);
1117 }
1118 }
1119#endif
1120 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001121 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001122 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001123 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001124}
1125
1126static int __init setup_print_fatal_signals(char *str)
1127{
1128 get_option (&str, &print_fatal_signals);
1129
1130 return 1;
1131}
1132
1133__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001135int
1136__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1137{
1138 return send_signal(sig, info, p, 1);
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static int
1142specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1143{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001144 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001147int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1148 bool group)
1149{
1150 unsigned long flags;
1151 int ret = -ESRCH;
1152
1153 if (lock_task_sighand(p, &flags)) {
1154 ret = send_signal(sig, info, p, group);
1155 unlock_task_sighand(p, &flags);
1156 }
1157
1158 return ret;
1159}
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161/*
1162 * Force a signal that the process can't ignore: if necessary
1163 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001164 *
1165 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1166 * since we do not want to have a signal handler that was blocked
1167 * be invoked when user space had explicitly blocked it.
1168 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001169 * We don't want to have recursive SIGSEGV's etc, for example,
1170 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172int
1173force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1174{
1175 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001176 int ret, blocked, ignored;
1177 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001180 action = &t->sighand->action[sig-1];
1181 ignored = action->sa.sa_handler == SIG_IGN;
1182 blocked = sigismember(&t->blocked, sig);
1183 if (blocked || ignored) {
1184 action->sa.sa_handler = SIG_DFL;
1185 if (blocked) {
1186 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001187 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001190 if (action->sa.sa_handler == SIG_DFL)
1191 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 ret = specific_send_sig_info(sig, info, t);
1193 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1194
1195 return ret;
1196}
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/*
1199 * Nuke all other threads in the group.
1200 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001201int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001203 struct task_struct *t = p;
1204 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 p->signal->group_stop_count = 0;
1207
Oleg Nesterov09faef12010-05-26 14:43:11 -07001208 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001209 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001210 count++;
1211
1212 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (t->exit_state)
1214 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 signal_wake_up(t, 1);
1217 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001218
1219 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
1221
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001222struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1223 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001224{
1225 struct sighand_struct *sighand;
1226
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001227 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001228 for (;;) {
1229 sighand = rcu_dereference(tsk->sighand);
1230 if (unlikely(sighand == NULL))
1231 break;
1232
1233 spin_lock_irqsave(&sighand->siglock, *flags);
1234 if (likely(sighand == tsk->sighand))
1235 break;
1236 spin_unlock_irqrestore(&sighand->siglock, *flags);
1237 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001238 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001239
1240 return sighand;
1241}
1242
David Howellsc69e8d92008-11-14 10:39:19 +11001243/*
1244 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1247{
David Howells694f6902010-08-04 16:59:14 +01001248 int ret;
1249
1250 rcu_read_lock();
1251 ret = check_kill_permission(sig, info, p);
1252 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001254 if (!ret && sig)
1255 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 return ret;
1258}
1259
1260/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001261 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001263 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001265int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct task_struct *p = NULL;
1268 int retval, success;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 success = 0;
1271 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001272 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int err = group_send_sig_info(sig, info, p);
1274 success |= !err;
1275 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001276 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return success ? 0 : retval;
1278}
1279
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001280int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001282 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct task_struct *p;
1284
Ingo Molnare56d0902006-01-08 01:01:37 -08001285 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001286retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001287 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001288 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001290 if (unlikely(error == -ESRCH))
1291 /*
1292 * The task was unhashed in between, try again.
1293 * If it is dead, pid_task() will return NULL,
1294 * if we race with de_thread() it will find the
1295 * new leader.
1296 */
1297 goto retry;
1298 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001299 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return error;
1302}
1303
Randy Dunlap5aba0852011-04-04 14:59:31 -07001304int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001305{
1306 int error;
1307 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001308 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001309 rcu_read_unlock();
1310 return error;
1311}
1312
Eric W. Biederman2425c082006-10-02 02:17:28 -07001313/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1314int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001315 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001316{
1317 int ret = -EINVAL;
1318 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001319 const struct cred *pcred;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001320 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001321
1322 if (!valid_signal(sig))
1323 return ret;
1324
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001325 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001326 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001327 if (!p) {
1328 ret = -ESRCH;
1329 goto out_unlock;
1330 }
David Howellsc69e8d92008-11-14 10:39:19 +11001331 pcred = __task_cred(p);
Oleg Nesterov614c5172009-12-15 16:47:22 -08001332 if (si_fromuser(info) &&
David Howellsc69e8d92008-11-14 10:39:19 +11001333 euid != pcred->suid && euid != pcred->uid &&
1334 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001335 ret = -EPERM;
1336 goto out_unlock;
1337 }
David Quigley8f95dc52006-06-30 01:55:47 -07001338 ret = security_task_kill(p, info, sig, secid);
1339 if (ret)
1340 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001341
1342 if (sig) {
1343 if (lock_task_sighand(p, &flags)) {
1344 ret = __send_signal(sig, info, p, 1, 0);
1345 unlock_task_sighand(p, &flags);
1346 } else
1347 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001348 }
1349out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001350 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001351 return ret;
1352}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001353EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355/*
1356 * kill_something_info() interprets pid in interesting ways just like kill(2).
1357 *
1358 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1359 * is probably wrong. Should make it like BSD or SYSV.
1360 */
1361
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001362static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001364 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001365
1366 if (pid > 0) {
1367 rcu_read_lock();
1368 ret = kill_pid_info(sig, info, find_vpid(pid));
1369 rcu_read_unlock();
1370 return ret;
1371 }
1372
1373 read_lock(&tasklist_lock);
1374 if (pid != -1) {
1375 ret = __kill_pgrp_info(sig, info,
1376 pid ? find_vpid(-pid) : task_pgrp(current));
1377 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int retval = 0, count = 0;
1379 struct task_struct * p;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001382 if (task_pid_vnr(p) > 1 &&
1383 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int err = group_send_sig_info(sig, info, p);
1385 ++count;
1386 if (err != -EPERM)
1387 retval = err;
1388 }
1389 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001390 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001392 read_unlock(&tasklist_lock);
1393
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001394 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395}
1396
1397/*
1398 * These are for backward compatibility with the rest of the kernel source.
1399 */
1400
Randy Dunlap5aba0852011-04-04 14:59:31 -07001401int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 /*
1404 * Make sure legacy kernel users don't send in bad values
1405 * (normal paths check this in check_kill_permission).
1406 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001407 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -EINVAL;
1409
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001410 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411}
1412
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001413#define __si_special(priv) \
1414 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416int
1417send_sig(int sig, struct task_struct *p, int priv)
1418{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001419 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422void
1423force_sig(int sig, struct task_struct *p)
1424{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001425 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
1428/*
1429 * When things go south during signal handling, we
1430 * will force a SIGSEGV. And if the signal that caused
1431 * the problem was already a SIGSEGV, we'll want to
1432 * make sure we don't even try to deliver the signal..
1433 */
1434int
1435force_sigsegv(int sig, struct task_struct *p)
1436{
1437 if (sig == SIGSEGV) {
1438 unsigned long flags;
1439 spin_lock_irqsave(&p->sighand->siglock, flags);
1440 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1441 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1442 }
1443 force_sig(SIGSEGV, p);
1444 return 0;
1445}
1446
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001447int kill_pgrp(struct pid *pid, int sig, int priv)
1448{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001449 int ret;
1450
1451 read_lock(&tasklist_lock);
1452 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1453 read_unlock(&tasklist_lock);
1454
1455 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001456}
1457EXPORT_SYMBOL(kill_pgrp);
1458
1459int kill_pid(struct pid *pid, int sig, int priv)
1460{
1461 return kill_pid_info(sig, __si_special(priv), pid);
1462}
1463EXPORT_SYMBOL(kill_pid);
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/*
1466 * These functions support sending signals using preallocated sigqueue
1467 * structures. This is needed "because realtime applications cannot
1468 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001469 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 * we allocate the sigqueue structure from the timer_create. If this
1471 * allocation fails we are able to report the failure to the application
1472 * with an EAGAIN error.
1473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474struct sigqueue *sigqueue_alloc(void)
1475{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001476 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001478 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001480
1481 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484void sigqueue_free(struct sigqueue *q)
1485{
1486 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001487 spinlock_t *lock = &current->sighand->siglock;
1488
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1490 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001491 * We must hold ->siglock while testing q->list
1492 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001493 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001495 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001496 q->flags &= ~SIGQUEUE_PREALLOC;
1497 /*
1498 * If it is queued it will be freed when dequeued,
1499 * like the "regular" sigqueue.
1500 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001501 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001502 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001503 spin_unlock_irqrestore(lock, flags);
1504
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001505 if (q)
1506 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001509int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001510{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001511 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001512 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001513 unsigned long flags;
1514 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001515
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001516 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001517
1518 ret = -1;
1519 if (!likely(lock_task_sighand(t, &flags)))
1520 goto ret;
1521
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001522 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001523 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001524 goto out;
1525
1526 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001527 if (unlikely(!list_empty(&q->list))) {
1528 /*
1529 * If an SI_TIMER entry is already queue just increment
1530 * the overrun count.
1531 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001532 BUG_ON(q->info.si_code != SI_TIMER);
1533 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001534 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001535 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001536 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001537
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001538 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001539 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001540 list_add_tail(&q->list, &pending->list);
1541 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001542 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001543out:
1544 unlock_task_sighand(t, &flags);
1545ret:
1546 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * Let a parent know about the death of a child.
1551 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001552 *
1553 * Returns -1 if our parent ignored us and so we've switched to
1554 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001556int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
1558 struct siginfo info;
1559 unsigned long flags;
1560 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001561 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 BUG_ON(sig == -1);
1564
1565 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001566 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001568 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1570
1571 info.si_signo = sig;
1572 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001573 /*
1574 * we are under tasklist_lock here so our parent is tied to
1575 * us and cannot exit and release its namespace.
1576 *
1577 * the only it can is to switch its nsproxy with sys_unshare,
1578 * bu uncharing pid namespaces is not allowed, so we'll always
1579 * see relevant namespace
1580 *
1581 * write_lock() currently calls preempt_disable() which is the
1582 * same as rcu_read_lock(), but according to Oleg, this is not
1583 * correct to rely on this
1584 */
1585 rcu_read_lock();
1586 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001587 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001588 rcu_read_unlock();
1589
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001590 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1591 tsk->signal->utime));
1592 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1593 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 info.si_status = tsk->exit_code & 0x7f;
1596 if (tsk->exit_code & 0x80)
1597 info.si_code = CLD_DUMPED;
1598 else if (tsk->exit_code & 0x7f)
1599 info.si_code = CLD_KILLED;
1600 else {
1601 info.si_code = CLD_EXITED;
1602 info.si_status = tsk->exit_code >> 8;
1603 }
1604
1605 psig = tsk->parent->sighand;
1606 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001607 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1609 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1610 /*
1611 * We are exiting and our parent doesn't care. POSIX.1
1612 * defines special semantics for setting SIGCHLD to SIG_IGN
1613 * or setting the SA_NOCLDWAIT flag: we should be reaped
1614 * automatically and not left for our parent's wait4 call.
1615 * Rather than having the parent do it as a magic kind of
1616 * signal handler, we just set this to tell do_exit that we
1617 * can be cleaned up without becoming a zombie. Note that
1618 * we still call __wake_up_parent in this case, because a
1619 * blocked sys_wait4 might now return -ECHILD.
1620 *
1621 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1622 * is implementation-defined: we do (if you don't want
1623 * it, just use SIG_IGN instead).
1624 */
Roland McGrath1b046242008-08-19 20:37:07 -07001625 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001627 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001629 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 __group_send_sig_info(sig, &info, tsk->parent);
1631 __wake_up_parent(tsk, tsk->parent);
1632 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001633
Roland McGrath1b046242008-08-19 20:37:07 -07001634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Tejun Heo75b95952011-03-23 10:37:01 +01001637/**
1638 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1639 * @tsk: task reporting the state change
1640 * @for_ptracer: the notification is for ptracer
1641 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1642 *
1643 * Notify @tsk's parent that the stopped/continued state has changed. If
1644 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1645 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1646 *
1647 * CONTEXT:
1648 * Must be called with tasklist_lock at least read locked.
1649 */
1650static void do_notify_parent_cldstop(struct task_struct *tsk,
1651 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
1653 struct siginfo info;
1654 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001655 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct sighand_struct *sighand;
1657
Tejun Heo75b95952011-03-23 10:37:01 +01001658 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001659 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001660 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001661 tsk = tsk->group_leader;
1662 parent = tsk->real_parent;
1663 }
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 info.si_signo = SIGCHLD;
1666 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001667 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001668 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001669 */
1670 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001671 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001672 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001673 rcu_read_unlock();
1674
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001675 info.si_utime = cputime_to_clock_t(tsk->utime);
1676 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 info.si_code = why;
1679 switch (why) {
1680 case CLD_CONTINUED:
1681 info.si_status = SIGCONT;
1682 break;
1683 case CLD_STOPPED:
1684 info.si_status = tsk->signal->group_exit_code & 0x7f;
1685 break;
1686 case CLD_TRAPPED:
1687 info.si_status = tsk->exit_code & 0x7f;
1688 break;
1689 default:
1690 BUG();
1691 }
1692
1693 sighand = parent->sighand;
1694 spin_lock_irqsave(&sighand->siglock, flags);
1695 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1696 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1697 __group_send_sig_info(SIGCHLD, &info, parent);
1698 /*
1699 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1700 */
1701 __wake_up_parent(tsk, parent);
1702 spin_unlock_irqrestore(&sighand->siglock, flags);
1703}
1704
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001705static inline int may_ptrace_stop(void)
1706{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001707 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001708 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001709 /*
1710 * Are we in the middle of do_coredump?
1711 * If so and our tracer is also part of the coredump stopping
1712 * is a deadlock situation, and pointless because our tracer
1713 * is dead so don't allow us to stop.
1714 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001715 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001716 * is safe to enter schedule().
1717 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001718 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001719 unlikely(current->mm == current->parent->mm))
1720 return 0;
1721
1722 return 1;
1723}
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001726 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001727 * Called with the siglock held.
1728 */
1729static int sigkill_pending(struct task_struct *tsk)
1730{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001731 return sigismember(&tsk->pending.signal, SIGKILL) ||
1732 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001733}
1734
1735/*
Tejun Heoceb6bd62011-03-23 10:37:01 +01001736 * Test whether the target task of the usual cldstop notification - the
1737 * real_parent of @child - is in the same group as the ptracer.
1738 */
1739static bool real_parent_is_ptracer(struct task_struct *child)
1740{
1741 return same_thread_group(child->parent, child->real_parent);
1742}
1743
1744/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 * This must be called with current->sighand->siglock held.
1746 *
1747 * This should be the path for all ptrace stops.
1748 * We always set current->last_siginfo while stopped here.
1749 * That makes it a way to test a stopped process for
1750 * being ptrace-stopped vs being job-control-stopped.
1751 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001752 * If we actually decide not to stop at all because the tracer
1753 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001755static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001756 __releases(&current->sighand->siglock)
1757 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001759 bool gstop_done = false;
1760
Roland McGrath1a669c22008-02-06 01:37:37 -08001761 if (arch_ptrace_stop_needed(exit_code, info)) {
1762 /*
1763 * The arch code has something special to do before a
1764 * ptrace stop. This is allowed to block, e.g. for faults
1765 * on user stack pages. We can't keep the siglock while
1766 * calling arch_ptrace_stop, so we must release it now.
1767 * To preserve proper semantics, we must do this before
1768 * any signal bookkeeping like checking group_stop_count.
1769 * Meanwhile, a SIGKILL could come in before we retake the
1770 * siglock. That must prevent us from sleeping in TASK_TRACED.
1771 * So after regaining the lock, we must check for SIGKILL.
1772 */
1773 spin_unlock_irq(&current->sighand->siglock);
1774 arch_ptrace_stop(exit_code, info);
1775 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001776 if (sigkill_pending(current))
1777 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001778 }
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001781 * We're committing to trapping. TRACED should be visible before
1782 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1783 * Also, transition to TRACED and updates to ->jobctl should be
1784 * atomic with respect to siglock and should be done after the arch
1785 * hook as siglock is released and regrabbed across it.
1786 */
1787 set_current_state(TASK_TRACED);
1788
1789 current->last_siginfo = info;
1790 current->exit_code = exit_code;
1791
1792 /*
Tejun Heo0ae8ce12011-03-23 10:37:00 +01001793 * If @why is CLD_STOPPED, we're trapping to participate in a group
1794 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
1795 * while siglock was released for the arch hook, PENDING could be
1796 * clear now. We act as if SIGCONT is received after TASK_TRACED
1797 * is entered - ignore it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001799 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001800 gstop_done = task_participate_group_stop(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Tejun Heo81be24b2011-06-02 11:13:59 +02001802 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001803 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 spin_unlock_irq(&current->sighand->siglock);
1806 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001807 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001808 /*
1809 * Notify parents of the stop.
1810 *
1811 * While ptraced, there are two parents - the ptracer and
1812 * the real_parent of the group_leader. The ptracer should
1813 * know about every stop while the real parent is only
1814 * interested in the completion of group stop. The states
1815 * for the two don't interact with each other. Notify
1816 * separately unless they're gonna be duplicates.
1817 */
1818 do_notify_parent_cldstop(current, true, why);
1819 if (gstop_done && !real_parent_is_ptracer(current))
1820 do_notify_parent_cldstop(current, false, why);
1821
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001822 /*
1823 * Don't want to allow preemption here, because
1824 * sys_ptrace() needs this task to be inactive.
1825 *
1826 * XXX: implement read_unlock_no_resched().
1827 */
1828 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001830 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 schedule();
1832 } else {
1833 /*
1834 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001835 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001836 *
1837 * If @gstop_done, the ptracer went away between group stop
1838 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001839 * JOBCTL_STOP_PENDING on us and we'll re-enter
1840 * TASK_STOPPED in do_signal_stop() on return, so notifying
1841 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001843 if (gstop_done)
1844 do_notify_parent_cldstop(current, false, why);
1845
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001846 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001847 if (clear_code)
1848 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001849 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
1852 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001853 * While in TASK_TRACED, we were considered "frozen enough".
1854 * Now that we woke up, it's crucial if we're supposed to be
1855 * frozen that we freeze now before running anything substantial.
1856 */
1857 try_to_freeze();
1858
1859 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 * We are back. Now reacquire the siglock before touching
1861 * last_siginfo, so that we are sure to have synchronized with
1862 * any signal-sending on another CPU that wants to examine it.
1863 */
1864 spin_lock_irq(&current->sighand->siglock);
1865 current->last_siginfo = NULL;
1866
1867 /*
1868 * Queued signals ignored us while we were stopped for tracing.
1869 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001870 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001872 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
1875void ptrace_notify(int exit_code)
1876{
1877 siginfo_t info;
1878
1879 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1880
1881 memset(&info, 0, sizeof info);
1882 info.si_signo = SIGTRAP;
1883 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001884 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001885 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 /* Let the debugger run. */
1888 spin_lock_irq(&current->sighand->siglock);
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001889 ptrace_stop(exit_code, CLD_TRAPPED, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 spin_unlock_irq(&current->sighand->siglock);
1891}
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893/*
1894 * This performs the stopping for SIGSTOP and other stop signals.
1895 * We have to stop all threads in the thread group.
Randy Dunlap5aba0852011-04-04 14:59:31 -07001896 * Returns non-zero if we've actually stopped and released the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 * Returns zero if we didn't stop and still hold the siglock.
1898 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001899static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Tejun Heoa8f072c2011-06-02 11:13:59 +02001903 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
1904 unsigned int gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001905 struct task_struct *t;
1906
Tejun Heoa8f072c2011-06-02 11:13:59 +02001907 /* signr will be recorded in task->jobctl for retries */
1908 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01001909
Tejun Heoa8f072c2011-06-02 11:13:59 +02001910 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001911 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01001914 * There is no group stop already in progress. We must
1915 * initiate one now.
1916 *
1917 * While ptraced, a task may be resumed while group stop is
1918 * still in effect and then receive a stop signal and
1919 * initiate another group stop. This deviates from the
1920 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02001921 * cause two group stops when !ptraced. That is why we
1922 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01001923 *
1924 * The condition can be distinguished by testing whether
1925 * SIGNAL_STOP_STOPPED is already set. Don't generate
1926 * group_exit_code in such case.
1927 *
1928 * This is not necessary for SIGNAL_STOP_CONTINUED because
1929 * an intervening stop signal is required to cause two
1930 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
Tejun Heo408a37d2011-03-23 10:37:01 +01001932 if (!(sig->flags & SIGNAL_STOP_STOPPED))
1933 sig->group_exit_code = signr;
1934 else
1935 WARN_ON_ONCE(!task_ptrace(current));
Oleg Nesterova122b342006-03-28 16:11:22 -08001936
Tejun Heo7dd3db52011-06-02 11:14:00 +02001937 sig->group_stop_count = 0;
1938
1939 if (task_set_jobctl_pending(current, signr | gstop))
1940 sig->group_stop_count++;
1941
Tejun Heod79fdd62011-03-23 10:37:00 +01001942 for (t = next_thread(current); t != current;
1943 t = next_thread(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08001944 /*
1945 * Setting state to TASK_STOPPED for a group
1946 * stop is always done with the siglock held,
1947 * so this check has no races.
1948 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02001949 if (!task_is_stopped(t) &&
1950 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001951 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001952 signal_wake_up(t, 0);
1953 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001954 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001955 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001956retry:
Tejun Heo5224fa32011-03-23 10:37:00 +01001957 if (likely(!task_ptrace(current))) {
1958 int notify = 0;
1959
1960 /*
1961 * If there are no other threads in the group, or if there
1962 * is a group stop in progress and we are the last to stop,
1963 * report to the parent.
1964 */
1965 if (task_participate_group_stop(current))
1966 notify = CLD_STOPPED;
1967
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001968 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01001969 spin_unlock_irq(&current->sighand->siglock);
1970
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001971 /*
1972 * Notify the parent of the group stop completion. Because
1973 * we're not holding either the siglock or tasklist_lock
1974 * here, ptracer may attach inbetween; however, this is for
1975 * group stop and should always be delivered to the real
1976 * parent of the group leader. The new ptracer will get
1977 * its notification when this task transitions into
1978 * TASK_TRACED.
1979 */
Tejun Heo5224fa32011-03-23 10:37:00 +01001980 if (notify) {
1981 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01001982 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01001983 read_unlock(&tasklist_lock);
1984 }
1985
1986 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1987 schedule();
1988
1989 spin_lock_irq(&current->sighand->siglock);
Tejun Heod79fdd62011-03-23 10:37:00 +01001990 } else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02001991 ptrace_stop(current->jobctl & JOBCTL_STOP_SIGMASK,
Tejun Heod79fdd62011-03-23 10:37:00 +01001992 CLD_STOPPED, 0, NULL);
1993 current->exit_code = 0;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001994 }
Tejun Heod79fdd62011-03-23 10:37:00 +01001995
1996 /*
Tejun Heoa8f072c2011-06-02 11:13:59 +02001997 * JOBCTL_STOP_PENDING could be set if another group stop has
Tejun Heod79fdd62011-03-23 10:37:00 +01001998 * started since being woken up or ptrace wants us to transit
1999 * between TASK_STOPPED and TRACED. Retry group stop.
2000 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002001 if (current->jobctl & JOBCTL_STOP_PENDING) {
2002 WARN_ON_ONCE(!(current->jobctl & JOBCTL_STOP_SIGMASK));
Tejun Heod79fdd62011-03-23 10:37:00 +01002003 goto retry;
2004 }
2005
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002006 spin_unlock_irq(&current->sighand->siglock);
2007
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002008 tracehook_finish_jctl();
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 return 1;
2011}
2012
Roland McGrath18c98b62008-04-17 18:44:38 -07002013static int ptrace_signal(int signr, siginfo_t *info,
2014 struct pt_regs *regs, void *cookie)
2015{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07002016 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07002017 return signr;
2018
2019 ptrace_signal_deliver(regs, cookie);
2020
2021 /* Let the debugger run. */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01002022 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002023
2024 /* We're back. Did the debugger cancel the sig? */
2025 signr = current->exit_code;
2026 if (signr == 0)
2027 return signr;
2028
2029 current->exit_code = 0;
2030
Randy Dunlap5aba0852011-04-04 14:59:31 -07002031 /*
2032 * Update the siginfo structure if the signal has
2033 * changed. If the debugger wanted something
2034 * specific in the siginfo structure then it should
2035 * have updated *info via PTRACE_SETSIGINFO.
2036 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002037 if (signr != info->si_signo) {
2038 info->si_signo = signr;
2039 info->si_errno = 0;
2040 info->si_code = SI_USER;
2041 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11002042 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07002043 }
2044
2045 /* If the (new) signal is now blocked, requeue it. */
2046 if (sigismember(&current->blocked, signr)) {
2047 specific_send_sig_info(signr, info, current);
2048 signr = 0;
2049 }
2050
2051 return signr;
2052}
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
2055 struct pt_regs *regs, void *cookie)
2056{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002057 struct sighand_struct *sighand = current->sighand;
2058 struct signal_struct *signal = current->signal;
2059 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002061relock:
2062 /*
2063 * We'll jump back here after any time we were stopped in TASK_STOPPED.
2064 * While in TASK_STOPPED, we were considered "frozen enough".
2065 * Now that we woke up, it's crucial if we're supposed to be
2066 * frozen that we freeze now before running anything substantial.
2067 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002068 try_to_freeze();
2069
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002070 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002071 /*
2072 * Every stopped thread goes here after wakeup. Check to see if
2073 * we should notify the parent, prepare_signal(SIGCONT) encodes
2074 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2075 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002076 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heo75b95952011-03-23 10:37:01 +01002077 struct task_struct *leader;
Tejun Heoc672af32011-03-23 10:36:59 +01002078 int why;
2079
2080 if (signal->flags & SIGNAL_CLD_CONTINUED)
2081 why = CLD_CONTINUED;
2082 else
2083 why = CLD_STOPPED;
2084
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002085 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002086
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002087 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002088
Tejun Heoceb6bd62011-03-23 10:37:01 +01002089 /*
2090 * Notify the parent that we're continuing. This event is
2091 * always per-process and doesn't make whole lot of sense
2092 * for ptracers, who shouldn't consume the state via
2093 * wait(2) either, but, for backward compatibility, notify
2094 * the ptracer of the group leader too unless it's gonna be
2095 * a duplicate.
2096 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002097 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002098
2099 do_notify_parent_cldstop(current, false, why);
2100
Tejun Heo75b95952011-03-23 10:37:01 +01002101 leader = current->group_leader;
Tejun Heoceb6bd62011-03-23 10:37:01 +01002102 if (task_ptrace(leader) && !real_parent_is_ptracer(leader))
2103 do_notify_parent_cldstop(leader, true, why);
2104
Tejun Heoedf2ed12011-03-23 10:37:00 +01002105 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002106
Oleg Nesterove4420552008-04-30 00:52:44 -07002107 goto relock;
2108 }
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 for (;;) {
2111 struct k_sigaction *ka;
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002112 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002113 * Tracing can induce an artificial signal and choose sigaction.
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002114 * The return value in @signr determines the default action,
2115 * but @info->si_signo is the signal number we will report.
2116 */
2117 signr = tracehook_get_signal(current, regs, info, return_ka);
2118 if (unlikely(signr < 0))
2119 goto relock;
2120 if (unlikely(signr != 0))
2121 ka = return_ka;
2122 else {
Tejun Heoa8f072c2011-06-02 11:13:59 +02002123 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2124 do_signal_stop(0))
Oleg Nesterov1be53962009-12-15 16:47:26 -08002125 goto relock;
2126
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002127 signr = dequeue_signal(current, &current->blocked,
2128 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Roland McGrath18c98b62008-04-17 18:44:38 -07002130 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002131 break; /* will return 0 */
2132
2133 if (signr != SIGKILL) {
2134 signr = ptrace_signal(signr, info,
2135 regs, cookie);
2136 if (!signr)
2137 continue;
2138 }
2139
2140 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
2142
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002143 /* Trace actually delivered signals. */
2144 trace_signal_deliver(signr, info, ka);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2147 continue;
2148 if (ka->sa.sa_handler != SIG_DFL) {
2149 /* Run the handler. */
2150 *return_ka = *ka;
2151
2152 if (ka->sa.sa_flags & SA_ONESHOT)
2153 ka->sa.sa_handler = SIG_DFL;
2154
2155 break; /* will return non-zero "signr" value */
2156 }
2157
2158 /*
2159 * Now we are doing the default action for this signal.
2160 */
2161 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2162 continue;
2163
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002164 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002165 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002166 * Container-init gets no signals it doesn't want from same
2167 * container.
2168 *
2169 * Note that if global/container-init sees a sig_kernel_only()
2170 * signal here, the signal must have been generated internally
2171 * or must have come from an ancestor namespace. In either
2172 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002173 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002174 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002175 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 continue;
2177
2178 if (sig_kernel_stop(signr)) {
2179 /*
2180 * The default action is to stop all threads in
2181 * the thread group. The job control signals
2182 * do nothing in an orphaned pgrp, but SIGSTOP
2183 * always works. Note that siglock needs to be
2184 * dropped during the call to is_orphaned_pgrp()
2185 * because of lock ordering with tasklist_lock.
2186 * This allows an intervening SIGCONT to be posted.
2187 * We need to check for that and bail out if necessary.
2188 */
2189 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002190 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 /* signals can be posted during this window */
2193
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002194 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 goto relock;
2196
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002197 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002200 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 /* It released the siglock. */
2202 goto relock;
2203 }
2204
2205 /*
2206 * We didn't actually stop, due to a race
2207 * with SIGCONT or something like that.
2208 */
2209 continue;
2210 }
2211
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002212 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /*
2215 * Anything else is fatal, maybe with a core dump.
2216 */
2217 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002220 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002221 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
2223 * If it was able to dump core, this kills all
2224 * other threads in the group and synchronizes with
2225 * their demise. If we lost the race with another
2226 * thread getting here, it set group_exit_code
2227 * first and our do_group_exit call below will use
2228 * that value and ignore the one we pass it.
2229 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002230 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 }
2232
2233 /*
2234 * Death signals, no core dump.
2235 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002236 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* NOTREACHED */
2238 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002239 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return signr;
2241}
2242
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002243/*
2244 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002245 * group-wide signal. Other threads should be notified now to take
2246 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002247 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002248static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002249{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002250 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002251 struct task_struct *t;
2252
Oleg Nesterovf646e222011-04-27 19:18:39 +02002253 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2254 if (sigisemptyset(&retarget))
2255 return;
2256
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002257 t = tsk;
2258 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002259 if (t->flags & PF_EXITING)
2260 continue;
2261
2262 if (!has_pending_signals(&retarget, &t->blocked))
2263 continue;
2264 /* Remove the signals this thread can handle. */
2265 sigandsets(&retarget, &retarget, &t->blocked);
2266
2267 if (!signal_pending(t))
2268 signal_wake_up(t, 0);
2269
2270 if (sigisemptyset(&retarget))
2271 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002272 }
2273}
2274
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002275void exit_signals(struct task_struct *tsk)
2276{
2277 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002278 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002279
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002280 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2281 tsk->flags |= PF_EXITING;
2282 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002283 }
2284
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002285 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002286 /*
2287 * From now this task is not visible for group-wide signals,
2288 * see wants_signal(), do_signal_stop().
2289 */
2290 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002291 if (!signal_pending(tsk))
2292 goto out;
2293
Oleg Nesterovf646e222011-04-27 19:18:39 +02002294 unblocked = tsk->blocked;
2295 signotset(&unblocked);
2296 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002297
Tejun Heoa8f072c2011-06-02 11:13:59 +02002298 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002299 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002300 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002301out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002302 spin_unlock_irq(&tsk->sighand->siglock);
2303
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002304 /*
2305 * If group stop has completed, deliver the notification. This
2306 * should always go to the real parent of the group leader.
2307 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002308 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002309 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002310 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002311 read_unlock(&tasklist_lock);
2312 }
2313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315EXPORT_SYMBOL(recalc_sigpending);
2316EXPORT_SYMBOL_GPL(dequeue_signal);
2317EXPORT_SYMBOL(flush_signals);
2318EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319EXPORT_SYMBOL(send_sig);
2320EXPORT_SYMBOL(send_sig_info);
2321EXPORT_SYMBOL(sigprocmask);
2322EXPORT_SYMBOL(block_all_signals);
2323EXPORT_SYMBOL(unblock_all_signals);
2324
2325
2326/*
2327 * System call entry points.
2328 */
2329
Randy Dunlap41c57892011-04-04 15:00:26 -07002330/**
2331 * sys_restart_syscall - restart a system call
2332 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002333SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334{
2335 struct restart_block *restart = &current_thread_info()->restart_block;
2336 return restart->fn(restart);
2337}
2338
2339long do_no_restart_syscall(struct restart_block *param)
2340{
2341 return -EINTR;
2342}
2343
Oleg Nesterovb1828012011-04-27 21:56:14 +02002344static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2345{
2346 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2347 sigset_t newblocked;
2348 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002349 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002350 retarget_shared_pending(tsk, &newblocked);
2351 }
2352 tsk->blocked = *newset;
2353 recalc_sigpending();
2354}
2355
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002356/**
2357 * set_current_blocked - change current->blocked mask
2358 * @newset: new mask
2359 *
2360 * It is wrong to change ->blocked directly, this helper should be used
2361 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 */
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002363void set_current_blocked(const sigset_t *newset)
2364{
2365 struct task_struct *tsk = current;
2366
2367 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002368 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002369 spin_unlock_irq(&tsk->sighand->siglock);
2370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372/*
2373 * This is also useful for kernel threads that want to temporarily
2374 * (or permanently) block certain signals.
2375 *
2376 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2377 * interface happily blocks "unblockable" signals like SIGKILL
2378 * and friends.
2379 */
2380int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2381{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002382 struct task_struct *tsk = current;
2383 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002385 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002386 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002387 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 switch (how) {
2390 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002391 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
2393 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002394 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 break;
2396 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002397 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 break;
2399 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002400 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002402
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002403 set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Randy Dunlap41c57892011-04-04 15:00:26 -07002407/**
2408 * sys_rt_sigprocmask - change the list of currently blocked signals
2409 * @how: whether to add, remove, or set signals
2410 * @set: stores pending signals
2411 * @oset: previous value of signal mask if non-null
2412 * @sigsetsize: size of sigset_t type
2413 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002414SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002415 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002418 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 /* XXX: Don't preclude handling different sized sigset_t's. */
2421 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002422 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002424 old_set = current->blocked;
2425
2426 if (nset) {
2427 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2428 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2430
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002431 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002433 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002435
2436 if (oset) {
2437 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2438 return -EFAULT;
2439 }
2440
2441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444long do_sigpending(void __user *set, unsigned long sigsetsize)
2445{
2446 long error = -EINVAL;
2447 sigset_t pending;
2448
2449 if (sigsetsize > sizeof(sigset_t))
2450 goto out;
2451
2452 spin_lock_irq(&current->sighand->siglock);
2453 sigorsets(&pending, &current->pending.signal,
2454 &current->signal->shared_pending.signal);
2455 spin_unlock_irq(&current->sighand->siglock);
2456
2457 /* Outside the lock because only this thread touches it. */
2458 sigandsets(&pending, &current->blocked, &pending);
2459
2460 error = -EFAULT;
2461 if (!copy_to_user(set, &pending, sigsetsize))
2462 error = 0;
2463
2464out:
2465 return error;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002466}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Randy Dunlap41c57892011-04-04 15:00:26 -07002468/**
2469 * sys_rt_sigpending - examine a pending signal that has been raised
2470 * while blocked
2471 * @set: stores pending signals
2472 * @sigsetsize: size of sigset_t type or larger
2473 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002474SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
2476 return do_sigpending(set, sigsetsize);
2477}
2478
2479#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2480
2481int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2482{
2483 int err;
2484
2485 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2486 return -EFAULT;
2487 if (from->si_code < 0)
2488 return __copy_to_user(to, from, sizeof(siginfo_t))
2489 ? -EFAULT : 0;
2490 /*
2491 * If you change siginfo_t structure, please be sure
2492 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002493 * Please remember to update the signalfd_copyinfo() function
2494 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 * It should never copy any pad contained in the structure
2496 * to avoid security leaks, but must copy the generic
2497 * 3 ints plus the relevant union member.
2498 */
2499 err = __put_user(from->si_signo, &to->si_signo);
2500 err |= __put_user(from->si_errno, &to->si_errno);
2501 err |= __put_user((short)from->si_code, &to->si_code);
2502 switch (from->si_code & __SI_MASK) {
2503 case __SI_KILL:
2504 err |= __put_user(from->si_pid, &to->si_pid);
2505 err |= __put_user(from->si_uid, &to->si_uid);
2506 break;
2507 case __SI_TIMER:
2508 err |= __put_user(from->si_tid, &to->si_tid);
2509 err |= __put_user(from->si_overrun, &to->si_overrun);
2510 err |= __put_user(from->si_ptr, &to->si_ptr);
2511 break;
2512 case __SI_POLL:
2513 err |= __put_user(from->si_band, &to->si_band);
2514 err |= __put_user(from->si_fd, &to->si_fd);
2515 break;
2516 case __SI_FAULT:
2517 err |= __put_user(from->si_addr, &to->si_addr);
2518#ifdef __ARCH_SI_TRAPNO
2519 err |= __put_user(from->si_trapno, &to->si_trapno);
2520#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002521#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002522 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002523 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002524 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002525 */
2526 if (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO)
2527 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2528#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 break;
2530 case __SI_CHLD:
2531 err |= __put_user(from->si_pid, &to->si_pid);
2532 err |= __put_user(from->si_uid, &to->si_uid);
2533 err |= __put_user(from->si_status, &to->si_status);
2534 err |= __put_user(from->si_utime, &to->si_utime);
2535 err |= __put_user(from->si_stime, &to->si_stime);
2536 break;
2537 case __SI_RT: /* This is not generated by the kernel as of now. */
2538 case __SI_MESGQ: /* But this is */
2539 err |= __put_user(from->si_pid, &to->si_pid);
2540 err |= __put_user(from->si_uid, &to->si_uid);
2541 err |= __put_user(from->si_ptr, &to->si_ptr);
2542 break;
2543 default: /* this is just in case for now ... */
2544 err |= __put_user(from->si_pid, &to->si_pid);
2545 err |= __put_user(from->si_uid, &to->si_uid);
2546 break;
2547 }
2548 return err;
2549}
2550
2551#endif
2552
Randy Dunlap41c57892011-04-04 15:00:26 -07002553/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002554 * do_sigtimedwait - wait for queued signals specified in @which
2555 * @which: queued signals to wait for
2556 * @info: if non-null, the signal's siginfo is returned here
2557 * @ts: upper bound on process time suspension
2558 */
2559int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
2560 const struct timespec *ts)
2561{
2562 struct task_struct *tsk = current;
2563 long timeout = MAX_SCHEDULE_TIMEOUT;
2564 sigset_t mask = *which;
2565 int sig;
2566
2567 if (ts) {
2568 if (!timespec_valid(ts))
2569 return -EINVAL;
2570 timeout = timespec_to_jiffies(ts);
2571 /*
2572 * We can be close to the next tick, add another one
2573 * to ensure we will wait at least the time asked for.
2574 */
2575 if (ts->tv_sec || ts->tv_nsec)
2576 timeout++;
2577 }
2578
2579 /*
2580 * Invert the set of allowed signals to get those we want to block.
2581 */
2582 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2583 signotset(&mask);
2584
2585 spin_lock_irq(&tsk->sighand->siglock);
2586 sig = dequeue_signal(tsk, &mask, info);
2587 if (!sig && timeout) {
2588 /*
2589 * None ready, temporarily unblock those we're interested
2590 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002591 * they arrive. Unblocking is always fine, we can avoid
2592 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002593 */
2594 tsk->real_blocked = tsk->blocked;
2595 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2596 recalc_sigpending();
2597 spin_unlock_irq(&tsk->sighand->siglock);
2598
2599 timeout = schedule_timeout_interruptible(timeout);
2600
2601 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002602 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov943df142011-04-27 21:44:14 +02002603 siginitset(&tsk->real_blocked, 0);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002604 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002605 }
2606 spin_unlock_irq(&tsk->sighand->siglock);
2607
2608 if (sig)
2609 return sig;
2610 return timeout ? -EINTR : -EAGAIN;
2611}
2612
2613/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002614 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2615 * in @uthese
2616 * @uthese: queued signals to wait for
2617 * @uinfo: if non-null, the signal's siginfo is returned here
2618 * @uts: upper bound on process time suspension
2619 * @sigsetsize: size of sigset_t type
2620 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002621SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2622 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2623 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 sigset_t these;
2626 struct timespec ts;
2627 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002628 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630 /* XXX: Don't preclude handling different sized sigset_t's. */
2631 if (sigsetsize != sizeof(sigset_t))
2632 return -EINVAL;
2633
2634 if (copy_from_user(&these, uthese, sizeof(these)))
2635 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (uts) {
2638 if (copy_from_user(&ts, uts, sizeof(ts)))
2639 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641
Oleg Nesterov943df142011-04-27 21:44:14 +02002642 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Oleg Nesterov943df142011-04-27 21:44:14 +02002644 if (ret > 0 && uinfo) {
2645 if (copy_siginfo_to_user(uinfo, &info))
2646 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
2649 return ret;
2650}
2651
Randy Dunlap41c57892011-04-04 15:00:26 -07002652/**
2653 * sys_kill - send a signal to a process
2654 * @pid: the PID of the process
2655 * @sig: signal to be sent
2656 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002657SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 struct siginfo info;
2660
2661 info.si_signo = sig;
2662 info.si_errno = 0;
2663 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002664 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002665 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 return kill_something_info(sig, &info, pid);
2668}
2669
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002670static int
2671do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002672{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002673 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002674 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002675
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002676 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002677 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002678 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002679 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002680 /*
2681 * The null signal is a permissions and process existence
2682 * probe. No signal is actually delivered.
2683 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002684 if (!error && sig) {
2685 error = do_send_sig_info(sig, info, p, false);
2686 /*
2687 * If lock_task_sighand() failed we pretend the task
2688 * dies after receiving the signal. The window is tiny,
2689 * and the signal is private anyway.
2690 */
2691 if (unlikely(error == -ESRCH))
2692 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002693 }
2694 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002695 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002696
2697 return error;
2698}
2699
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002700static int do_tkill(pid_t tgid, pid_t pid, int sig)
2701{
2702 struct siginfo info;
2703
2704 info.si_signo = sig;
2705 info.si_errno = 0;
2706 info.si_code = SI_TKILL;
2707 info.si_pid = task_tgid_vnr(current);
2708 info.si_uid = current_uid();
2709
2710 return do_send_specific(tgid, pid, sig, &info);
2711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713/**
2714 * sys_tgkill - send signal to one specific thread
2715 * @tgid: the thread group ID of the thread
2716 * @pid: the PID of the thread
2717 * @sig: signal to be sent
2718 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002719 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * exists but it's not belonging to the target process anymore. This
2721 * method solves the problem of threads exiting and PIDs getting reused.
2722 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002723SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* This is only valid for single tasks */
2726 if (pid <= 0 || tgid <= 0)
2727 return -EINVAL;
2728
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002729 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Randy Dunlap41c57892011-04-04 15:00:26 -07002732/**
2733 * sys_tkill - send signal to one specific task
2734 * @pid: the PID of the task
2735 * @sig: signal to be sent
2736 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2738 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002739SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /* This is only valid for single tasks */
2742 if (pid <= 0)
2743 return -EINVAL;
2744
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002745 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746}
2747
Randy Dunlap41c57892011-04-04 15:00:26 -07002748/**
2749 * sys_rt_sigqueueinfo - send signal information to a signal
2750 * @pid: the PID of the thread
2751 * @sig: signal to be sent
2752 * @uinfo: signal info to be sent
2753 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002754SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2755 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 siginfo_t info;
2758
2759 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2760 return -EFAULT;
2761
2762 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002763 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2764 */
Roland Dreier243b4222011-03-28 14:13:35 -07002765 if (info.si_code >= 0 || info.si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002766 /* We used to allow any < 0 si_code */
2767 WARN_ON_ONCE(info.si_code < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 info.si_signo = sig;
2771
2772 /* POSIX.1b doesn't mention process groups. */
2773 return kill_proc_info(sig, &info, pid);
2774}
2775
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002776long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2777{
2778 /* This is only valid for single tasks */
2779 if (pid <= 0 || tgid <= 0)
2780 return -EINVAL;
2781
2782 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07002783 * Nor can they impersonate a kill()/tgkill(), which adds source info.
2784 */
Roland Dreier243b4222011-03-28 14:13:35 -07002785 if (info->si_code >= 0 || info->si_code == SI_TKILL) {
Julien Tinnesda485242011-03-18 15:05:21 -07002786 /* We used to allow any < 0 si_code */
2787 WARN_ON_ONCE(info->si_code < 0);
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002788 return -EPERM;
Julien Tinnesda485242011-03-18 15:05:21 -07002789 }
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002790 info->si_signo = sig;
2791
2792 return do_send_specific(tgid, pid, sig, info);
2793}
2794
2795SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2796 siginfo_t __user *, uinfo)
2797{
2798 siginfo_t info;
2799
2800 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2801 return -EFAULT;
2802
2803 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2804}
2805
Oleg Nesterov88531f72006-03-28 16:11:24 -08002806int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002808 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002810 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002812 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return -EINVAL;
2814
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002815 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 if (oact)
2819 *oact = *k;
2820
2821 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002822 sigdelsetmask(&act->sa.sa_mask,
2823 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002824 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * POSIX 3.3.1.3:
2827 * "Setting a signal action to SIG_IGN for a signal that is
2828 * pending shall cause the pending signal to be discarded,
2829 * whether or not it is blocked."
2830 *
2831 * "Setting a signal action to SIG_DFL for a signal that is
2832 * pending and whose default action is to ignore the signal
2833 * (for example, SIGCHLD), shall cause the pending signal to
2834 * be discarded, whether or not it is blocked"
2835 */
Roland McGrath35de2542008-07-25 19:45:51 -07002836 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002837 sigemptyset(&mask);
2838 sigaddset(&mask, sig);
2839 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002841 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 t = next_thread(t);
2843 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
2847 spin_unlock_irq(&current->sighand->siglock);
2848 return 0;
2849}
2850
2851int
2852do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2853{
2854 stack_t oss;
2855 int error;
2856
Linus Torvalds0083fc22009-08-01 10:34:56 -07002857 oss.ss_sp = (void __user *) current->sas_ss_sp;
2858 oss.ss_size = current->sas_ss_size;
2859 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 if (uss) {
2862 void __user *ss_sp;
2863 size_t ss_size;
2864 int ss_flags;
2865
2866 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002867 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2868 goto out;
2869 error = __get_user(ss_sp, &uss->ss_sp) |
2870 __get_user(ss_flags, &uss->ss_flags) |
2871 __get_user(ss_size, &uss->ss_size);
2872 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 goto out;
2874
2875 error = -EPERM;
2876 if (on_sig_stack(sp))
2877 goto out;
2878
2879 error = -EINVAL;
2880 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002881 * Note - this code used to test ss_flags incorrectly:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 * old code may have been written using ss_flags==0
2883 * to mean ss_flags==SS_ONSTACK (as this was the only
2884 * way that worked) - this fix preserves that older
Randy Dunlap5aba0852011-04-04 14:59:31 -07002885 * mechanism.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 */
2887 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2888 goto out;
2889
2890 if (ss_flags == SS_DISABLE) {
2891 ss_size = 0;
2892 ss_sp = NULL;
2893 } else {
2894 error = -ENOMEM;
2895 if (ss_size < MINSIGSTKSZ)
2896 goto out;
2897 }
2898
2899 current->sas_ss_sp = (unsigned long) ss_sp;
2900 current->sas_ss_size = ss_size;
2901 }
2902
Linus Torvalds0083fc22009-08-01 10:34:56 -07002903 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (uoss) {
2905 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002906 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002908 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2909 __put_user(oss.ss_size, &uoss->ss_size) |
2910 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913out:
2914 return error;
2915}
2916
2917#ifdef __ARCH_WANT_SYS_SIGPENDING
2918
Randy Dunlap41c57892011-04-04 15:00:26 -07002919/**
2920 * sys_sigpending - examine pending signals
2921 * @set: where mask of pending signal is returned
2922 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002923SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
2925 return do_sigpending(set, sizeof(*set));
2926}
2927
2928#endif
2929
2930#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07002931/**
2932 * sys_sigprocmask - examine and change blocked signals
2933 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02002934 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07002935 * @oset: previous value of signal mask if non-null
2936 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07002937 * Some platforms have their own version with special arguments;
2938 * others support only sys_rt_sigprocmask.
2939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Oleg Nesterovb013c392011-04-28 11:36:20 +02002941SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002942 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002945 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Oleg Nesterovb013c392011-04-28 11:36:20 +02002947 old_set = current->blocked.sig[0];
2948
2949 if (nset) {
2950 if (copy_from_user(&new_set, nset, sizeof(*nset)))
2951 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2953
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002954 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002958 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 break;
2960 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002961 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 break;
2963 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002964 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002966 default:
2967 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02002970 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02002972
2973 if (oset) {
2974 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2975 return -EFAULT;
2976 }
2977
2978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2981
2982#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07002983/**
2984 * sys_rt_sigaction - alter an action taken by a process
2985 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07002986 * @act: new sigaction
2987 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07002988 * @sigsetsize: size of sigset_t type
2989 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01002990SYSCALL_DEFINE4(rt_sigaction, int, sig,
2991 const struct sigaction __user *, act,
2992 struct sigaction __user *, oact,
2993 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct k_sigaction new_sa, old_sa;
2996 int ret = -EINVAL;
2997
2998 /* XXX: Don't preclude handling different sized sigset_t's. */
2999 if (sigsetsize != sizeof(sigset_t))
3000 goto out;
3001
3002 if (act) {
3003 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3004 return -EFAULT;
3005 }
3006
3007 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3008
3009 if (!ret && oact) {
3010 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3011 return -EFAULT;
3012 }
3013out:
3014 return ret;
3015}
3016#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
3017
3018#ifdef __ARCH_WANT_SYS_SGETMASK
3019
3020/*
3021 * For backwards compatibility. Functionality superseded by sigprocmask.
3022 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003023SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024{
3025 /* SMP safe */
3026 return current->blocked.sig[0];
3027}
3028
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003029SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 int old;
3032
3033 spin_lock_irq(&current->sighand->siglock);
3034 old = current->blocked.sig[0];
3035
3036 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
3037 sigmask(SIGSTOP)));
3038 recalc_sigpending();
3039 spin_unlock_irq(&current->sighand->siglock);
3040
3041 return old;
3042}
3043#endif /* __ARCH_WANT_SGETMASK */
3044
3045#ifdef __ARCH_WANT_SYS_SIGNAL
3046/*
3047 * For backwards compatibility. Functionality superseded by sigaction.
3048 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003049SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 struct k_sigaction new_sa, old_sa;
3052 int ret;
3053
3054 new_sa.sa.sa_handler = handler;
3055 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03003056 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 ret = do_sigaction(sig, &new_sa, &old_sa);
3059
3060 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3061}
3062#endif /* __ARCH_WANT_SYS_SIGNAL */
3063
3064#ifdef __ARCH_WANT_SYS_PAUSE
3065
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003066SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003068 while (!signal_pending(current)) {
3069 current->state = TASK_INTERRUPTIBLE;
3070 schedule();
3071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return -ERESTARTNOHAND;
3073}
3074
3075#endif
3076
David Woodhouse150256d2006-01-18 17:43:57 -08003077#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Randy Dunlap41c57892011-04-04 15:00:26 -07003078/**
3079 * sys_rt_sigsuspend - replace the signal mask for a value with the
3080 * @unewset value until a signal is received
3081 * @unewset: new signal mask value
3082 * @sigsetsize: size of sigset_t type
3083 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003084SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003085{
3086 sigset_t newset;
3087
3088 /* XXX: Don't preclude handling different sized sigset_t's. */
3089 if (sigsetsize != sizeof(sigset_t))
3090 return -EINVAL;
3091
3092 if (copy_from_user(&newset, unewset, sizeof(newset)))
3093 return -EFAULT;
3094 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3095
3096 spin_lock_irq(&current->sighand->siglock);
3097 current->saved_sigmask = current->blocked;
3098 current->blocked = newset;
3099 recalc_sigpending();
3100 spin_unlock_irq(&current->sighand->siglock);
3101
3102 current->state = TASK_INTERRUPTIBLE;
3103 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07003104 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08003105 return -ERESTARTNOHAND;
3106}
3107#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
3108
David Howellsf269fdd2006-09-27 01:50:23 -07003109__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
3110{
3111 return NULL;
3112}
3113
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114void __init signals_init(void)
3115{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003116 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003118
3119#ifdef CONFIG_KGDB_KDB
3120#include <linux/kdb.h>
3121/*
3122 * kdb_send_sig_info - Allows kdb to send signals without exposing
3123 * signal internals. This function checks if the required locks are
3124 * available before calling the main signal code, to avoid kdb
3125 * deadlocks.
3126 */
3127void
3128kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3129{
3130 static struct task_struct *kdb_prev_t;
3131 int sig, new_t;
3132 if (!spin_trylock(&t->sighand->siglock)) {
3133 kdb_printf("Can't do kill command now.\n"
3134 "The sigmask lock is held somewhere else in "
3135 "kernel, try again later\n");
3136 return;
3137 }
3138 spin_unlock(&t->sighand->siglock);
3139 new_t = kdb_prev_t != t;
3140 kdb_prev_t = t;
3141 if (t->state != TASK_RUNNING && new_t) {
3142 kdb_printf("Process is not RUNNING, sending a signal from "
3143 "kdb risks deadlock\n"
3144 "on the run queue locks. "
3145 "The signal has _not_ been sent.\n"
3146 "Reissue the kill command if you want to risk "
3147 "the deadlock.\n");
3148 return;
3149 }
3150 sig = info->si_signo;
3151 if (send_sig_info(sig, info, t))
3152 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3153 sig, t->pid);
3154 else
3155 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3156}
3157#endif /* CONFIG_KGDB_KDB */