blob: 534ea81cde4759811a107855d0f73fd221489d15 [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>
Roland McGrath35de2542008-07-25 19:45:51 -070025#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080026#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080027#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080028#include <linux/pid_namespace.h>
29#include <linux/nsproxy.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040030#include <trace/events/sched.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/param.h>
33#include <asm/uaccess.h>
34#include <asm/unistd.h>
35#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040036#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/*
39 * SLAB caches for signal bits.
40 */
41
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Roland McGrath35de2542008-07-25 19:45:51 -070044static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070045{
Roland McGrath35de2542008-07-25 19:45:51 -070046 return t->sighand->action[sig - 1].sa.sa_handler;
47}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070048
Roland McGrath35de2542008-07-25 19:45:51 -070049static int sig_handler_ignored(void __user *handler, int sig)
50{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070051 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070052 return handler == SIG_IGN ||
53 (handler == SIG_DFL && sig_kernel_ignore(sig));
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070056static int sig_task_ignored(struct task_struct *t, int sig,
57 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Roland McGrath35de2542008-07-25 19:45:51 -070059 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Oleg Nesterovf008faf2009-04-02 16:58:02 -070061 handler = sig_handler(t, sig);
62
63 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070064 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070065 return 1;
66
67 return sig_handler_ignored(handler, sig);
68}
69
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070070static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /*
73 * Blocked signals are never ignored, since the
74 * signal handler may change by the time it is
75 * unblocked.
76 */
Roland McGrath325d22d2007-11-12 15:41:55 -080077 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070080 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070081 return 0;
82
83 /*
84 * Tracers may want to know about even ignored signals.
85 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070086 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89/*
90 * Re-calculate pending state from the set of locally pending
91 * signals, globally pending signals, and blocked signals.
92 */
93static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
94{
95 unsigned long ready;
96 long i;
97
98 switch (_NSIG_WORDS) {
99 default:
100 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
101 ready |= signal->sig[i] &~ blocked->sig[i];
102 break;
103
104 case 4: ready = signal->sig[3] &~ blocked->sig[3];
105 ready |= signal->sig[2] &~ blocked->sig[2];
106 ready |= signal->sig[1] &~ blocked->sig[1];
107 ready |= signal->sig[0] &~ blocked->sig[0];
108 break;
109
110 case 2: ready = signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
113
114 case 1: ready = signal->sig[0] &~ blocked->sig[0];
115 }
116 return ready != 0;
117}
118
119#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
120
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700121static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 if (t->signal->group_stop_count > 0 ||
124 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700125 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700127 return 1;
128 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700129 /*
130 * We must never clear the flag in another thread, or in current
131 * when it's possible the current syscall is returning -ERESTART*.
132 * So we don't clear it here, and only callers who know they should do.
133 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700134 return 0;
135}
136
137/*
138 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
139 * This is superfluous when called on current, the wakeup is a harmless no-op.
140 */
141void recalc_sigpending_and_wake(struct task_struct *t)
142{
143 if (recalc_sigpending_tsk(t))
144 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147void recalc_sigpending(void)
148{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700149 if (unlikely(tracehook_force_sigpending()))
150 set_thread_flag(TIF_SIGPENDING);
151 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700152 clear_thread_flag(TIF_SIGPENDING);
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/* Given the mask, find the first available signal that should be serviced. */
157
Davide Libenzifba2afa2007-05-10 22:23:13 -0700158int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 unsigned long i, *s, *m, x;
161 int sig = 0;
162
163 s = pending->signal.sig;
164 m = mask->sig;
165 switch (_NSIG_WORDS) {
166 default:
167 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
168 if ((x = *s &~ *m) != 0) {
169 sig = ffz(~x) + i*_NSIG_BPW + 1;
170 break;
171 }
172 break;
173
174 case 2: if ((x = s[0] &~ m[0]) != 0)
175 sig = 1;
176 else if ((x = s[1] &~ m[1]) != 0)
177 sig = _NSIG_BPW + 1;
178 else
179 break;
180 sig += ffz(~x);
181 break;
182
183 case 1: if ((x = *s &~ *m) != 0)
184 sig = ffz(~x) + 1;
185 break;
186 }
187
188 return sig;
189}
190
David Howellsc69e8d92008-11-14 10:39:19 +1100191/*
192 * allocate a new signal queue record
193 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100194 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100195 */
Al Virodd0fc662005-10-07 07:46:04 +0100196static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int override_rlimit)
198{
199 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800200 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800202 /*
David Howellsc69e8d92008-11-14 10:39:19 +1100203 * We won't get problems with the target's UID changing under us
204 * because changing it requires RCU be used, and if t != current, the
205 * caller must be holding the RCU readlock (by way of a spinlock) and
206 * we use RCU protection here
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800207 */
David Howellsd84f4f92008-11-14 10:39:23 +1100208 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800209 atomic_inc(&user->sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800211 atomic_read(&user->sigpending) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
213 q = kmem_cache_alloc(sigqueue_cachep, flags);
214 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800215 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100216 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else {
218 INIT_LIST_HEAD(&q->list);
219 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100220 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
David Howellsd84f4f92008-11-14 10:39:23 +1100222
223 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Andrew Morton514a01b2006-02-03 03:04:41 -0800226static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 if (q->flags & SIGQUEUE_PREALLOC)
229 return;
230 atomic_dec(&q->user->sigpending);
231 free_uid(q->user);
232 kmem_cache_free(sigqueue_cachep, q);
233}
234
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800235void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 struct sigqueue *q;
238
239 sigemptyset(&queue->signal);
240 while (!list_empty(&queue->list)) {
241 q = list_entry(queue->list.next, struct sigqueue , list);
242 list_del_init(&q->list);
243 __sigqueue_free(q);
244 }
245}
246
247/*
248 * Flush all pending signals for a task.
249 */
David Howells3bcac022009-04-29 13:45:05 +0100250void __flush_signals(struct task_struct *t)
251{
252 clear_tsk_thread_flag(t, TIF_SIGPENDING);
253 flush_sigqueue(&t->pending);
254 flush_sigqueue(&t->signal->shared_pending);
255}
256
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800257void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 unsigned long flags;
260
261 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100262 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_unlock_irqrestore(&t->sighand->siglock, flags);
264}
265
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400266static void __flush_itimer_signals(struct sigpending *pending)
267{
268 sigset_t signal, retain;
269 struct sigqueue *q, *n;
270
271 signal = pending->signal;
272 sigemptyset(&retain);
273
274 list_for_each_entry_safe(q, n, &pending->list, list) {
275 int sig = q->info.si_signo;
276
277 if (likely(q->info.si_code != SI_TIMER)) {
278 sigaddset(&retain, sig);
279 } else {
280 sigdelset(&signal, sig);
281 list_del_init(&q->list);
282 __sigqueue_free(q);
283 }
284 }
285
286 sigorsets(&pending->signal, &signal, &retain);
287}
288
289void flush_itimer_signals(void)
290{
291 struct task_struct *tsk = current;
292 unsigned long flags;
293
294 spin_lock_irqsave(&tsk->sighand->siglock, flags);
295 __flush_itimer_signals(&tsk->pending);
296 __flush_itimer_signals(&tsk->signal->shared_pending);
297 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
298}
299
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700300void ignore_signals(struct task_struct *t)
301{
302 int i;
303
304 for (i = 0; i < _NSIG; ++i)
305 t->sighand->action[i].sa.sa_handler = SIG_IGN;
306
307 flush_signals(t);
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * Flush all handlers for a task.
312 */
313
314void
315flush_signal_handlers(struct task_struct *t, int force_default)
316{
317 int i;
318 struct k_sigaction *ka = &t->sighand->action[0];
319 for (i = _NSIG ; i != 0 ; i--) {
320 if (force_default || ka->sa.sa_handler != SIG_IGN)
321 ka->sa.sa_handler = SIG_DFL;
322 ka->sa.sa_flags = 0;
323 sigemptyset(&ka->sa.sa_mask);
324 ka++;
325 }
326}
327
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200328int unhandled_signal(struct task_struct *tsk, int sig)
329{
Roland McGrath445a91d2008-07-25 19:45:52 -0700330 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700331 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200332 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700333 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200334 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700335 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200336}
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/* Notify the system that a driver wants to block all signals for this
340 * process, and wants to be notified if any signals at all were to be
341 * sent/acted upon. If the notifier routine returns non-zero, then the
342 * signal will be acted upon after all. If the notifier routine returns 0,
343 * then then signal will be blocked. Only one block per process is
344 * allowed. priv is a pointer to private data that the notifier routine
345 * can use to determine if the signal should be blocked or not. */
346
347void
348block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
349{
350 unsigned long flags;
351
352 spin_lock_irqsave(&current->sighand->siglock, flags);
353 current->notifier_mask = mask;
354 current->notifier_data = priv;
355 current->notifier = notifier;
356 spin_unlock_irqrestore(&current->sighand->siglock, flags);
357}
358
359/* Notify the system that blocking has ended. */
360
361void
362unblock_all_signals(void)
363{
364 unsigned long flags;
365
366 spin_lock_irqsave(&current->sighand->siglock, flags);
367 current->notifier = NULL;
368 current->notifier_data = NULL;
369 recalc_sigpending();
370 spin_unlock_irqrestore(&current->sighand->siglock, flags);
371}
372
Oleg Nesterov100360f2008-07-25 01:47:29 -0700373static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /*
378 * Collect the siginfo appropriate to this signal. Check if
379 * there is another siginfo for the same signal.
380 */
381 list_for_each_entry(q, &list->list, list) {
382 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700383 if (first)
384 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 first = q;
386 }
387 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700388
389 sigdelset(&list->signal, sig);
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700392still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 list_del_init(&first->list);
394 copy_siginfo(info, &first->info);
395 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Ok, it wasn't in the queue. This must be
398 a fast-pathed signal or we must have been
399 out of queue space. So zero out the info.
400 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 info->si_signo = sig;
402 info->si_errno = 0;
403 info->si_code = 0;
404 info->si_pid = 0;
405 info->si_uid = 0;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
410 siginfo_t *info)
411{
Roland McGrath27d91e02006-09-29 02:00:31 -0700412 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (sig) {
415 if (current->notifier) {
416 if (sigismember(current->notifier_mask, sig)) {
417 if (!(current->notifier)(current->notifier_data)) {
418 clear_thread_flag(TIF_SIGPENDING);
419 return 0;
420 }
421 }
422 }
423
Oleg Nesterov100360f2008-07-25 01:47:29 -0700424 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427 return sig;
428}
429
430/*
431 * Dequeue a signal and return the element to the caller, which is
432 * expected to free it.
433 *
434 * All callers have to hold the siglock.
435 */
436int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
437{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700438 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000439
440 /* We only dequeue private signals from ourselves, we don't let
441 * signalfd steal them
442 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700443 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800444 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 signr = __dequeue_signal(&tsk->signal->shared_pending,
446 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800447 /*
448 * itimer signal ?
449 *
450 * itimers are process shared and we restart periodic
451 * itimers in the signal delivery path to prevent DoS
452 * attacks in the high resolution timer case. This is
453 * compliant with the old way of self restarting
454 * itimers, as the SIGALRM is a legacy signal and only
455 * queued once. Changing the restart behaviour to
456 * restart the timer in the signal dequeue path is
457 * reducing the timer noise on heavy loaded !highres
458 * systems too.
459 */
460 if (unlikely(signr == SIGALRM)) {
461 struct hrtimer *tmr = &tsk->signal->real_timer;
462
463 if (!hrtimer_is_queued(tmr) &&
464 tsk->signal->it_real_incr.tv64 != 0) {
465 hrtimer_forward(tmr, tmr->base->get_time(),
466 tsk->signal->it_real_incr);
467 hrtimer_restart(tmr);
468 }
469 }
470 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700471
Davide Libenzib8fceee2007-09-20 12:40:16 -0700472 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700473 if (!signr)
474 return 0;
475
476 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800477 /*
478 * Set a marker that we have dequeued a stop signal. Our
479 * caller might release the siglock and then the pending
480 * stop signal it is about to process is no longer in the
481 * pending bitmasks, but must still be cleared by a SIGCONT
482 * (and overruled by a SIGKILL). So those cases clear this
483 * shared flag after we've set it. Note that this flag may
484 * remain set after the signal we return is ignored or
485 * handled. That doesn't matter because its only purpose
486 * is to alert stop-signal processing code when another
487 * processor has come along and cleared the flag.
488 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700489 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800490 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700491 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /*
493 * Release the siglock to ensure proper locking order
494 * of timer locks outside of siglocks. Note, we leave
495 * irqs disabled here, since the posix-timers code is
496 * about to disable them again anyway.
497 */
498 spin_unlock(&tsk->sighand->siglock);
499 do_schedule_next_timer(info);
500 spin_lock(&tsk->sighand->siglock);
501 }
502 return signr;
503}
504
505/*
506 * Tell a process that it has a new active signal..
507 *
508 * NOTE! we rely on the previous spin_lock to
509 * lock interrupts for us! We can only be called with
510 * "siglock" held, and the local interrupt must
511 * have been disabled when that got acquired!
512 *
513 * No need to set need_resched since signal event passing
514 * goes through ->blocked
515 */
516void signal_wake_up(struct task_struct *t, int resume)
517{
518 unsigned int mask;
519
520 set_tsk_thread_flag(t, TIF_SIGPENDING);
521
522 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500523 * For SIGKILL, we want to wake it up in the stopped/traced/killable
524 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 * executing another processor and just now entering stopped state.
526 * By using wake_up_state, we ensure the process will wake up and
527 * handle its death signal.
528 */
529 mask = TASK_INTERRUPTIBLE;
530 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500531 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!wake_up_state(t, mask))
533 kick_process(t);
534}
535
536/*
537 * Remove signals in mask from the pending set and queue.
538 * Returns 1 if any signals were found.
539 *
540 * All callers must be holding the siglock.
George Anzinger71fabd5e2006-01-08 01:02:48 -0800541 *
542 * This version takes a sigset mask and looks at all signals,
543 * not just those in the first mask word.
544 */
545static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
546{
547 struct sigqueue *q, *n;
548 sigset_t m;
549
550 sigandsets(&m, mask, &s->signal);
551 if (sigisemptyset(&m))
552 return 0;
553
554 signandsets(&s->signal, &s->signal, mask);
555 list_for_each_entry_safe(q, n, &s->list, list) {
556 if (sigismember(mask, q->info.si_signo)) {
557 list_del_init(&q->list);
558 __sigqueue_free(q);
559 }
560 }
561 return 1;
562}
563/*
564 * Remove signals in mask from the pending set and queue.
565 * Returns 1 if any signals were found.
566 *
567 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
569static int rm_from_queue(unsigned long mask, struct sigpending *s)
570{
571 struct sigqueue *q, *n;
572
573 if (!sigtestsetmask(&s->signal, mask))
574 return 0;
575
576 sigdelsetmask(&s->signal, mask);
577 list_for_each_entry_safe(q, n, &s->list, list) {
578 if (q->info.si_signo < SIGRTMIN &&
579 (mask & sigmask(q->info.si_signo))) {
580 list_del_init(&q->list);
581 __sigqueue_free(q);
582 }
583 }
584 return 1;
585}
586
587/*
588 * Bad permissions for sending the signal
David Howellsc69e8d92008-11-14 10:39:19 +1100589 * - the caller must hold at least the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591static int check_kill_permission(int sig, struct siginfo *info,
592 struct task_struct *t)
593{
David Howellsc69e8d92008-11-14 10:39:19 +1100594 const struct cred *cred = current_cred(), *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700595 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700596 int error;
597
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700598 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700599 return -EINVAL;
600
601 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
602 return 0;
603
604 error = audit_signal_info(sig, t); /* Let audit system see the signal */
605 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400607
David Howellsc69e8d92008-11-14 10:39:19 +1100608 tcred = __task_cred(t);
609 if ((cred->euid ^ tcred->suid) &&
610 (cred->euid ^ tcred->uid) &&
611 (cred->uid ^ tcred->suid) &&
612 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700613 !capable(CAP_KILL)) {
614 switch (sig) {
615 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700616 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700617 /*
618 * We don't return the error if sid == NULL. The
619 * task was unhashed, the caller must notice this.
620 */
621 if (!sid || sid == task_session(current))
622 break;
623 default:
624 return -EPERM;
625 }
626 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100627
Amy Griffise54dc242007-03-29 18:01:04 -0400628 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700632 * Handle magic process-wide effects of stop/continue signals. Unlike
633 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 * time regardless of blocking, ignoring, or handling. This does the
635 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700636 * signals. The process stop is done as a signal action for SIG_DFL.
637 *
638 * Returns true if the signal should be actually delivered, otherwise
639 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700641static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700643 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct task_struct *t;
645
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700646 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700648 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700650 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
652 * This is a stop signal. Remove SIGCONT from all queues.
653 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700654 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 t = p;
656 do {
657 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700658 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700660 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Remove all stop signals from all queues,
663 * and wake all threads.
664 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700665 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 t = p;
667 do {
668 unsigned int state;
669 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 /*
671 * If there is a handler for SIGCONT, we must make
672 * sure that no thread returns to user mode before
673 * we post the signal, in case it was the only
674 * thread eligible to run the signal handler--then
675 * it must not do anything between resuming and
676 * running the handler. With the TIF_SIGPENDING
677 * flag set, the thread will pause and acquire the
678 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700679 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 *
681 * Wake up the stopped thread _after_ setting
682 * TIF_SIGPENDING
683 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500684 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
686 set_tsk_thread_flag(t, TIF_SIGPENDING);
687 state |= TASK_INTERRUPTIBLE;
688 }
689 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700690 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700692 /*
693 * Notify the parent with CLD_CONTINUED if we were stopped.
694 *
695 * If we were in the middle of a group stop, we pretend it
696 * was already finished, and then continued. Since SIGCHLD
697 * doesn't queue we report only CLD_STOPPED, as if the next
698 * CLD_CONTINUED was dropped.
699 */
700 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700701 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700702 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700703 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700704 why |= SIGNAL_CLD_STOPPED;
705
706 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700707 /*
708 * The first thread which returns from finish_stop()
709 * will take ->siglock, notice SIGNAL_CLD_MASK, and
710 * notify its parent. See get_signal_to_deliver().
711 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700712 signal->flags = why | SIGNAL_STOP_CONTINUED;
713 signal->group_stop_count = 0;
714 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 } else {
716 /*
717 * We are not stopped, but there could be a stop
718 * signal in the middle of being processed after
719 * being removed from the queue. Clear that too.
720 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700721 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700724
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700725 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700728/*
729 * Test if P wants to take SIG. After we've checked all threads with this,
730 * it's equivalent to finding no threads not blocking SIG. Any threads not
731 * blocking SIG were ruled out because they are not running and already
732 * have pending signals. Such threads will dequeue from the shared queue
733 * as soon as they're available, so putting the signal on the shared queue
734 * will be equivalent to sending it to one such thread.
735 */
736static inline int wants_signal(int sig, struct task_struct *p)
737{
738 if (sigismember(&p->blocked, sig))
739 return 0;
740 if (p->flags & PF_EXITING)
741 return 0;
742 if (sig == SIGKILL)
743 return 1;
744 if (task_is_stopped_or_traced(p))
745 return 0;
746 return task_curr(p) || !signal_pending(p);
747}
748
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700749static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700750{
751 struct signal_struct *signal = p->signal;
752 struct task_struct *t;
753
754 /*
755 * Now find a thread we can wake up to take the signal off the queue.
756 *
757 * If the main thread wants the signal, it gets first crack.
758 * Probably the least surprising to the average bear.
759 */
760 if (wants_signal(sig, p))
761 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700762 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700763 /*
764 * There is just one thread and it does not need to be woken.
765 * It will dequeue unblocked signals before it runs again.
766 */
767 return;
768 else {
769 /*
770 * Otherwise try to find a suitable thread.
771 */
772 t = signal->curr_target;
773 while (!wants_signal(sig, t)) {
774 t = next_thread(t);
775 if (t == signal->curr_target)
776 /*
777 * No thread needs to be woken.
778 * Any eligible threads will see
779 * the signal in the queue soon.
780 */
781 return;
782 }
783 signal->curr_target = t;
784 }
785
786 /*
787 * Found a killable thread. If the signal will be fatal,
788 * then start taking the whole group down immediately.
789 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700790 if (sig_fatal(p, sig) &&
791 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700792 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700793 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700794 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700795 /*
796 * This signal will be fatal to the whole group.
797 */
798 if (!sig_kernel_coredump(sig)) {
799 /*
800 * Start a group exit and wake everybody up.
801 * This way we don't have other threads
802 * running and doing things after a slower
803 * thread has the fatal signal pending.
804 */
805 signal->flags = SIGNAL_GROUP_EXIT;
806 signal->group_exit_code = sig;
807 signal->group_stop_count = 0;
808 t = p;
809 do {
810 sigaddset(&t->pending.signal, SIGKILL);
811 signal_wake_up(t, 1);
812 } while_each_thread(p, t);
813 return;
814 }
815 }
816
817 /*
818 * The signal is already in the shared-pending queue.
819 * Tell the chosen thread to wake up and dequeue it.
820 */
821 signal_wake_up(t, sig == SIGKILL);
822 return;
823}
824
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700825static inline int legacy_queue(struct sigpending *signals, int sig)
826{
827 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
828}
829
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700830static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
831 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700833 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700834 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200835 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400837 trace_sched_signal_send(sig, t);
838
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700839 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700840
841 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700842 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700843
844 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700846 * Short-circuit ignored signals and support queuing
847 * exactly one non-rt signal, so that we can get more
848 * detailed information about the cause of the signal.
849 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700850 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700851 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700852 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * fast-pathed signals for kernel-internal things like SIGSTOP
854 * or SIGKILL.
855 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800856 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 goto out_set;
858
859 /* Real-time signals must be queued if sent by sigqueue, or
860 some other real-time mechanism. It is implementation
861 defined whether kill() does so. We attempt to do so, on
862 the principle of least surprise, but since kill is not
863 allowed to fail with EAGAIN when low on memory we just
864 make sure at least one signal gets delivered and don't
865 pass on the info struct. */
866
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200867 if (sig < SIGRTMIN)
868 override_rlimit = (is_si_special(info) || info->si_code >= 0);
869 else
870 override_rlimit = 0;
871
872 q = __sigqueue_alloc(t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
873 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700875 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800877 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 q->info.si_signo = sig;
879 q->info.si_errno = 0;
880 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800881 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800882 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100883 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800885 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 q->info.si_signo = sig;
887 q->info.si_errno = 0;
888 q->info.si_code = SI_KERNEL;
889 q->info.si_pid = 0;
890 q->info.si_uid = 0;
891 break;
892 default:
893 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700894 if (from_ancestor_ns)
895 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 break;
897 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800898 } else if (!is_si_special(info)) {
899 if (sig >= SIGRTMIN && info->si_code != SI_USER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Queue overflow, abort. We may abort if the signal was rt
902 * and sent by user using something other than kill().
903 */
904 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
907out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700908 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700909 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700910 complete_signal(sig, t, group);
911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700914static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
915 int group)
916{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700917 int from_ancestor_ns = 0;
918
919#ifdef CONFIG_PID_NS
920 if (!is_si_special(info) && SI_FROMUSER(info) &&
921 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
922 from_ancestor_ns = 1;
923#endif
924
925 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700926}
927
Ingo Molnar45807a12007-07-15 23:40:10 -0700928int print_fatal_signals;
929
930static void print_fatal_signal(struct pt_regs *regs, int signr)
931{
932 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700933 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700934
Al Viroca5cd872007-10-29 04:31:16 +0000935#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100936 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700937 {
938 int i;
939 for (i = 0; i < 16; i++) {
940 unsigned char insn;
941
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100942 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700943 printk("%02x ", insn);
944 }
945 }
946#endif
947 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800948 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700949 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800950 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700951}
952
953static int __init setup_print_fatal_signals(char *str)
954{
955 get_option (&str, &print_fatal_signals);
956
957 return 1;
958}
959
960__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700962int
963__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
964{
965 return send_signal(sig, info, p, 1);
966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static int
969specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
970{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700971 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Force a signal that the process can't ignore: if necessary
976 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700977 *
978 * Note: If we unblock the signal, we always reset it to SIG_DFL,
979 * since we do not want to have a signal handler that was blocked
980 * be invoked when user space had explicitly blocked it.
981 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -0700982 * We don't want to have recursive SIGSEGV's etc, for example,
983 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985int
986force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
987{
988 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700989 int ret, blocked, ignored;
990 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700993 action = &t->sighand->action[sig-1];
994 ignored = action->sa.sa_handler == SIG_IGN;
995 blocked = sigismember(&t->blocked, sig);
996 if (blocked || ignored) {
997 action->sa.sa_handler = SIG_DFL;
998 if (blocked) {
999 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001000 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001003 if (action->sa.sa_handler == SIG_DFL)
1004 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 ret = specific_send_sig_info(sig, info, t);
1006 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1007
1008 return ret;
1009}
1010
1011void
1012force_sig_specific(int sig, struct task_struct *t)
1013{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001014 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/*
1018 * Nuke all other threads in the group.
1019 */
1020void zap_other_threads(struct task_struct *p)
1021{
1022 struct task_struct *t;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 p->signal->group_stop_count = 0;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 for (t = next_thread(p); t != p; t = next_thread(t)) {
1027 /*
1028 * Don't bother with already dead threads
1029 */
1030 if (t->exit_state)
1031 continue;
1032
Andrea Arcangeli30e0fca62005-10-30 15:02:38 -08001033 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 signal_wake_up(t, 1);
1036 }
1037}
1038
Harvey Harrisonb5606c22008-02-13 15:03:16 -08001039int __fatal_signal_pending(struct task_struct *tsk)
Matthew Wilcoxf776d122007-12-06 11:15:50 -05001040{
1041 return sigismember(&tsk->pending.signal, SIGKILL);
1042}
Trond Myklebust13f09b92008-01-31 20:40:29 -05001043EXPORT_SYMBOL(__fatal_signal_pending);
Matthew Wilcoxf776d122007-12-06 11:15:50 -05001044
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001045struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1046{
1047 struct sighand_struct *sighand;
1048
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001049 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001050 for (;;) {
1051 sighand = rcu_dereference(tsk->sighand);
1052 if (unlikely(sighand == NULL))
1053 break;
1054
1055 spin_lock_irqsave(&sighand->siglock, *flags);
1056 if (likely(sighand == tsk->sighand))
1057 break;
1058 spin_unlock_irqrestore(&sighand->siglock, *flags);
1059 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001060 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001061
1062 return sighand;
1063}
1064
David Howellsc69e8d92008-11-14 10:39:19 +11001065/*
1066 * send signal info to all the members of a group
1067 * - the caller must hold the RCU read lock at least
1068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1070{
1071 unsigned long flags;
1072 int ret;
1073
1074 ret = check_kill_permission(sig, info, p);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001075
1076 if (!ret && sig) {
1077 ret = -ESRCH;
1078 if (lock_task_sighand(p, &flags)) {
1079 ret = __group_send_sig_info(sig, info, p);
1080 unlock_task_sighand(p, &flags);
Ingo Molnare56d0902006-01-08 01:01:37 -08001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
1084 return ret;
1085}
1086
1087/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001088 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001090 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001092int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct task_struct *p = NULL;
1095 int retval, success;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 success = 0;
1098 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001099 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int err = group_send_sig_info(sig, info, p);
1101 success |= !err;
1102 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001103 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return success ? 0 : retval;
1105}
1106
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001107int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001109 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 struct task_struct *p;
1111
Ingo Molnare56d0902006-01-08 01:01:37 -08001112 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001113retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001114 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001115 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001117 if (unlikely(error == -ESRCH))
1118 /*
1119 * The task was unhashed in between, try again.
1120 * If it is dead, pid_task() will return NULL,
1121 * if we race with de_thread() it will find the
1122 * new leader.
1123 */
1124 goto retry;
1125 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001126 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 return error;
1129}
1130
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001131int
1132kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001133{
1134 int error;
1135 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001136 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001137 rcu_read_unlock();
1138 return error;
1139}
1140
Eric W. Biederman2425c082006-10-02 02:17:28 -07001141/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1142int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001143 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001144{
1145 int ret = -EINVAL;
1146 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001147 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001148
1149 if (!valid_signal(sig))
1150 return ret;
1151
1152 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001153 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001154 if (!p) {
1155 ret = -ESRCH;
1156 goto out_unlock;
1157 }
David Howellsc69e8d92008-11-14 10:39:19 +11001158 pcred = __task_cred(p);
1159 if ((info == SEND_SIG_NOINFO ||
1160 (!is_si_special(info) && SI_FROMUSER(info))) &&
1161 euid != pcred->suid && euid != pcred->uid &&
1162 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001163 ret = -EPERM;
1164 goto out_unlock;
1165 }
David Quigley8f95dc52006-06-30 01:55:47 -07001166 ret = security_task_kill(p, info, sig, secid);
1167 if (ret)
1168 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001169 if (sig && p->sighand) {
1170 unsigned long flags;
1171 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001172 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001173 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1174 }
1175out_unlock:
1176 read_unlock(&tasklist_lock);
1177 return ret;
1178}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001179EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181/*
1182 * kill_something_info() interprets pid in interesting ways just like kill(2).
1183 *
1184 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1185 * is probably wrong. Should make it like BSD or SYSV.
1186 */
1187
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001188static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001190 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001191
1192 if (pid > 0) {
1193 rcu_read_lock();
1194 ret = kill_pid_info(sig, info, find_vpid(pid));
1195 rcu_read_unlock();
1196 return ret;
1197 }
1198
1199 read_lock(&tasklist_lock);
1200 if (pid != -1) {
1201 ret = __kill_pgrp_info(sig, info,
1202 pid ? find_vpid(-pid) : task_pgrp(current));
1203 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int retval = 0, count = 0;
1205 struct task_struct * p;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001208 if (task_pid_vnr(p) > 1 &&
1209 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int err = group_send_sig_info(sig, info, p);
1211 ++count;
1212 if (err != -EPERM)
1213 retval = err;
1214 }
1215 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001216 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001218 read_unlock(&tasklist_lock);
1219
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001220 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * These are for backward compatibility with the rest of the kernel source.
1225 */
1226
1227/*
Oleg Nesterov08d2c302008-04-30 00:52:51 -07001228 * The caller must ensure the task can't exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
1230int
1231send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1232{
1233 int ret;
1234 unsigned long flags;
1235
1236 /*
1237 * Make sure legacy kernel users don't send in bad values
1238 * (normal paths check this in check_kill_permission).
1239 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001240 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return -EINVAL;
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 spin_lock_irqsave(&p->sighand->siglock, flags);
1244 ret = specific_send_sig_info(sig, info, p);
1245 spin_unlock_irqrestore(&p->sighand->siglock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return ret;
1247}
1248
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001249#define __si_special(priv) \
1250 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252int
1253send_sig(int sig, struct task_struct *p, int priv)
1254{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001255 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258void
1259force_sig(int sig, struct task_struct *p)
1260{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001261 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264/*
1265 * When things go south during signal handling, we
1266 * will force a SIGSEGV. And if the signal that caused
1267 * the problem was already a SIGSEGV, we'll want to
1268 * make sure we don't even try to deliver the signal..
1269 */
1270int
1271force_sigsegv(int sig, struct task_struct *p)
1272{
1273 if (sig == SIGSEGV) {
1274 unsigned long flags;
1275 spin_lock_irqsave(&p->sighand->siglock, flags);
1276 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1277 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1278 }
1279 force_sig(SIGSEGV, p);
1280 return 0;
1281}
1282
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001283int kill_pgrp(struct pid *pid, int sig, int priv)
1284{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001285 int ret;
1286
1287 read_lock(&tasklist_lock);
1288 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1289 read_unlock(&tasklist_lock);
1290
1291 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001292}
1293EXPORT_SYMBOL(kill_pgrp);
1294
1295int kill_pid(struct pid *pid, int sig, int priv)
1296{
1297 return kill_pid_info(sig, __si_special(priv), pid);
1298}
1299EXPORT_SYMBOL(kill_pid);
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301/*
1302 * These functions support sending signals using preallocated sigqueue
1303 * structures. This is needed "because realtime applications cannot
1304 * afford to lose notifications of asynchronous events, like timer
1305 * expirations or I/O completions". In the case of Posix Timers
1306 * we allocate the sigqueue structure from the timer_create. If this
1307 * allocation fails we are able to report the failure to the application
1308 * with an EAGAIN error.
1309 */
1310
1311struct sigqueue *sigqueue_alloc(void)
1312{
1313 struct sigqueue *q;
1314
1315 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1316 q->flags |= SIGQUEUE_PREALLOC;
1317 return(q);
1318}
1319
1320void sigqueue_free(struct sigqueue *q)
1321{
1322 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001323 spinlock_t *lock = &current->sighand->siglock;
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1326 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001327 * We must hold ->siglock while testing q->list
1328 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001329 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001331 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001332 q->flags &= ~SIGQUEUE_PREALLOC;
1333 /*
1334 * If it is queued it will be freed when dequeued,
1335 * like the "regular" sigqueue.
1336 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001337 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001338 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001339 spin_unlock_irqrestore(lock, flags);
1340
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001341 if (q)
1342 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001345int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001346{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001347 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001348 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001349 unsigned long flags;
1350 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001351
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001352 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001353
1354 ret = -1;
1355 if (!likely(lock_task_sighand(t, &flags)))
1356 goto ret;
1357
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001358 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001359 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001360 goto out;
1361
1362 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001363 if (unlikely(!list_empty(&q->list))) {
1364 /*
1365 * If an SI_TIMER entry is already queue just increment
1366 * the overrun count.
1367 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001368 BUG_ON(q->info.si_code != SI_TIMER);
1369 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001370 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001371 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001372 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001373
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001374 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001375 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001376 list_add_tail(&q->list, &pending->list);
1377 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001378 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001379out:
1380 unlock_task_sighand(t, &flags);
1381ret:
1382 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001383}
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * Let a parent know about the death of a child.
1387 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001388 *
1389 * Returns -1 if our parent ignored us and so we've switched to
1390 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001392int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 struct siginfo info;
1395 unsigned long flags;
1396 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001397 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 BUG_ON(sig == -1);
1400
1401 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001402 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001404 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1406
1407 info.si_signo = sig;
1408 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001409 /*
1410 * we are under tasklist_lock here so our parent is tied to
1411 * us and cannot exit and release its namespace.
1412 *
1413 * the only it can is to switch its nsproxy with sys_unshare,
1414 * bu uncharing pid namespaces is not allowed, so we'll always
1415 * see relevant namespace
1416 *
1417 * write_lock() currently calls preempt_disable() which is the
1418 * same as rcu_read_lock(), but according to Oleg, this is not
1419 * correct to rely on this
1420 */
1421 rcu_read_lock();
1422 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001423 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001424 rcu_read_unlock();
1425
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001426 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1427 tsk->signal->utime));
1428 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1429 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 info.si_status = tsk->exit_code & 0x7f;
1432 if (tsk->exit_code & 0x80)
1433 info.si_code = CLD_DUMPED;
1434 else if (tsk->exit_code & 0x7f)
1435 info.si_code = CLD_KILLED;
1436 else {
1437 info.si_code = CLD_EXITED;
1438 info.si_status = tsk->exit_code >> 8;
1439 }
1440
1441 psig = tsk->parent->sighand;
1442 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001443 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1445 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1446 /*
1447 * We are exiting and our parent doesn't care. POSIX.1
1448 * defines special semantics for setting SIGCHLD to SIG_IGN
1449 * or setting the SA_NOCLDWAIT flag: we should be reaped
1450 * automatically and not left for our parent's wait4 call.
1451 * Rather than having the parent do it as a magic kind of
1452 * signal handler, we just set this to tell do_exit that we
1453 * can be cleaned up without becoming a zombie. Note that
1454 * we still call __wake_up_parent in this case, because a
1455 * blocked sys_wait4 might now return -ECHILD.
1456 *
1457 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1458 * is implementation-defined: we do (if you don't want
1459 * it, just use SIG_IGN instead).
1460 */
Roland McGrath1b046242008-08-19 20:37:07 -07001461 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001463 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001465 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 __group_send_sig_info(sig, &info, tsk->parent);
1467 __wake_up_parent(tsk, tsk->parent);
1468 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001469
Roland McGrath1b046242008-08-19 20:37:07 -07001470 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001473static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
1475 struct siginfo info;
1476 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001477 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct sighand_struct *sighand;
1479
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001480 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001481 parent = tsk->parent;
1482 else {
1483 tsk = tsk->group_leader;
1484 parent = tsk->real_parent;
1485 }
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 info.si_signo = SIGCHLD;
1488 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001489 /*
1490 * see comment in do_notify_parent() abot the following 3 lines
1491 */
1492 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001493 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001494 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001495 rcu_read_unlock();
1496
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001497 info.si_utime = cputime_to_clock_t(tsk->utime);
1498 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 info.si_code = why;
1501 switch (why) {
1502 case CLD_CONTINUED:
1503 info.si_status = SIGCONT;
1504 break;
1505 case CLD_STOPPED:
1506 info.si_status = tsk->signal->group_exit_code & 0x7f;
1507 break;
1508 case CLD_TRAPPED:
1509 info.si_status = tsk->exit_code & 0x7f;
1510 break;
1511 default:
1512 BUG();
1513 }
1514
1515 sighand = parent->sighand;
1516 spin_lock_irqsave(&sighand->siglock, flags);
1517 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1518 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1519 __group_send_sig_info(SIGCHLD, &info, parent);
1520 /*
1521 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1522 */
1523 __wake_up_parent(tsk, parent);
1524 spin_unlock_irqrestore(&sighand->siglock, flags);
1525}
1526
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001527static inline int may_ptrace_stop(void)
1528{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001529 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001530 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001531 /*
1532 * Are we in the middle of do_coredump?
1533 * If so and our tracer is also part of the coredump stopping
1534 * is a deadlock situation, and pointless because our tracer
1535 * is dead so don't allow us to stop.
1536 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001537 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001538 * is safe to enter schedule().
1539 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001540 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001541 unlikely(current->mm == current->parent->mm))
1542 return 0;
1543
1544 return 1;
1545}
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001548 * Return nonzero if there is a SIGKILL that should be waking us up.
1549 * Called with the siglock held.
1550 */
1551static int sigkill_pending(struct task_struct *tsk)
1552{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001553 return sigismember(&tsk->pending.signal, SIGKILL) ||
1554 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001555}
1556
1557/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 * This must be called with current->sighand->siglock held.
1559 *
1560 * This should be the path for all ptrace stops.
1561 * We always set current->last_siginfo while stopped here.
1562 * That makes it a way to test a stopped process for
1563 * being ptrace-stopped vs being job-control-stopped.
1564 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001565 * If we actually decide not to stop at all because the tracer
1566 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001568static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569{
Roland McGrath1a669c22008-02-06 01:37:37 -08001570 if (arch_ptrace_stop_needed(exit_code, info)) {
1571 /*
1572 * The arch code has something special to do before a
1573 * ptrace stop. This is allowed to block, e.g. for faults
1574 * on user stack pages. We can't keep the siglock while
1575 * calling arch_ptrace_stop, so we must release it now.
1576 * To preserve proper semantics, we must do this before
1577 * any signal bookkeeping like checking group_stop_count.
1578 * Meanwhile, a SIGKILL could come in before we retake the
1579 * siglock. That must prevent us from sleeping in TASK_TRACED.
1580 * So after regaining the lock, we must check for SIGKILL.
1581 */
1582 spin_unlock_irq(&current->sighand->siglock);
1583 arch_ptrace_stop(exit_code, info);
1584 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001585 if (sigkill_pending(current))
1586 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001587 }
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /*
1590 * If there is a group stop in progress,
1591 * we must participate in the bookkeeping.
1592 */
1593 if (current->signal->group_stop_count > 0)
1594 --current->signal->group_stop_count;
1595
1596 current->last_siginfo = info;
1597 current->exit_code = exit_code;
1598
1599 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001600 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 spin_unlock_irq(&current->sighand->siglock);
1602 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001603 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001604 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001605 /*
1606 * Don't want to allow preemption here, because
1607 * sys_ptrace() needs this task to be inactive.
1608 *
1609 * XXX: implement read_unlock_no_resched().
1610 */
1611 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001613 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 schedule();
1615 } else {
1616 /*
1617 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001618 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001620 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001621 if (clear_code)
1622 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001623 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001627 * While in TASK_TRACED, we were considered "frozen enough".
1628 * Now that we woke up, it's crucial if we're supposed to be
1629 * frozen that we freeze now before running anything substantial.
1630 */
1631 try_to_freeze();
1632
1633 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * We are back. Now reacquire the siglock before touching
1635 * last_siginfo, so that we are sure to have synchronized with
1636 * any signal-sending on another CPU that wants to examine it.
1637 */
1638 spin_lock_irq(&current->sighand->siglock);
1639 current->last_siginfo = NULL;
1640
1641 /*
1642 * Queued signals ignored us while we were stopped for tracing.
1643 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001644 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001646 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649void ptrace_notify(int exit_code)
1650{
1651 siginfo_t info;
1652
1653 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1654
1655 memset(&info, 0, sizeof info);
1656 info.si_signo = SIGTRAP;
1657 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001658 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001659 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Let the debugger run. */
1662 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001663 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 spin_unlock_irq(&current->sighand->siglock);
1665}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667static void
1668finish_stop(int stop_count)
1669{
1670 /*
1671 * If there are no other threads in the group, or if there is
1672 * a group stop in progress and we are the last to stop,
1673 * report to the parent. When ptraced, every thread reports itself.
1674 */
Roland McGrathfa00b802008-07-25 19:45:54 -07001675 if (tracehook_notify_jctl(stop_count == 0, CLD_STOPPED)) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001676 read_lock(&tasklist_lock);
1677 do_notify_parent_cldstop(current, CLD_STOPPED);
1678 read_unlock(&tasklist_lock);
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -08001681 do {
1682 schedule();
1683 } while (try_to_freeze());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /*
1685 * Now we don't run again until continued.
1686 */
1687 current->exit_code = 0;
1688}
1689
1690/*
1691 * This performs the stopping for SIGSTOP and other stop signals.
1692 * We have to stop all threads in the thread group.
1693 * Returns nonzero if we've actually stopped and released the siglock.
1694 * Returns zero if we didn't stop and still hold the siglock.
1695 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001696static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697{
1698 struct signal_struct *sig = current->signal;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001699 int stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (sig->group_stop_count > 0) {
1702 /*
1703 * There is a group stop in progress. We don't need to
1704 * start another one.
1705 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 stop_count = --sig->group_stop_count;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001707 } else {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001708 struct task_struct *t;
1709
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001710 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001711 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001712 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001715 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001717 sig->group_exit_code = signr;
1718
1719 stop_count = 0;
1720 for (t = next_thread(current); t != current; t = next_thread(t))
1721 /*
1722 * Setting state to TASK_STOPPED for a group
1723 * stop is always done with the siglock held,
1724 * so this check has no races.
1725 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001726 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001727 !task_is_stopped_or_traced(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08001728 stop_count++;
1729 signal_wake_up(t, 0);
1730 }
1731 sig->group_stop_count = stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001734 if (stop_count == 0)
1735 sig->flags = SIGNAL_STOP_STOPPED;
1736 current->exit_code = sig->group_exit_code;
1737 __set_current_state(TASK_STOPPED);
1738
1739 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 finish_stop(stop_count);
1741 return 1;
1742}
1743
Roland McGrath18c98b62008-04-17 18:44:38 -07001744static int ptrace_signal(int signr, siginfo_t *info,
1745 struct pt_regs *regs, void *cookie)
1746{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001747 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001748 return signr;
1749
1750 ptrace_signal_deliver(regs, cookie);
1751
1752 /* Let the debugger run. */
1753 ptrace_stop(signr, 0, info);
1754
1755 /* We're back. Did the debugger cancel the sig? */
1756 signr = current->exit_code;
1757 if (signr == 0)
1758 return signr;
1759
1760 current->exit_code = 0;
1761
1762 /* Update the siginfo structure if the signal has
1763 changed. If the debugger wanted something
1764 specific in the siginfo structure then it should
1765 have updated *info via PTRACE_SETSIGINFO. */
1766 if (signr != info->si_signo) {
1767 info->si_signo = signr;
1768 info->si_errno = 0;
1769 info->si_code = SI_USER;
1770 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001771 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001772 }
1773
1774 /* If the (new) signal is now blocked, requeue it. */
1775 if (sigismember(&current->blocked, signr)) {
1776 specific_send_sig_info(signr, info, current);
1777 signr = 0;
1778 }
1779
1780 return signr;
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1784 struct pt_regs *regs, void *cookie)
1785{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001786 struct sighand_struct *sighand = current->sighand;
1787 struct signal_struct *signal = current->signal;
1788 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001790relock:
1791 /*
1792 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1793 * While in TASK_STOPPED, we were considered "frozen enough".
1794 * Now that we woke up, it's crucial if we're supposed to be
1795 * frozen that we freeze now before running anything substantial.
1796 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001797 try_to_freeze();
1798
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001799 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001800 /*
1801 * Every stopped thread goes here after wakeup. Check to see if
1802 * we should notify the parent, prepare_signal(SIGCONT) encodes
1803 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1804 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001805 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1806 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001807 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001808 signal->flags &= ~SIGNAL_CLD_MASK;
1809 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001810
Roland McGrathfa00b802008-07-25 19:45:54 -07001811 if (unlikely(!tracehook_notify_jctl(1, why)))
1812 goto relock;
1813
Oleg Nesterove4420552008-04-30 00:52:44 -07001814 read_lock(&tasklist_lock);
1815 do_notify_parent_cldstop(current->group_leader, why);
1816 read_unlock(&tasklist_lock);
1817 goto relock;
1818 }
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 for (;;) {
1821 struct k_sigaction *ka;
1822
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001823 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001824 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto relock;
1826
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001827 /*
1828 * Tracing can induce an artifical signal and choose sigaction.
1829 * The return value in @signr determines the default action,
1830 * but @info->si_signo is the signal number we will report.
1831 */
1832 signr = tracehook_get_signal(current, regs, info, return_ka);
1833 if (unlikely(signr < 0))
1834 goto relock;
1835 if (unlikely(signr != 0))
1836 ka = return_ka;
1837 else {
1838 signr = dequeue_signal(current, &current->blocked,
1839 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Roland McGrath18c98b62008-04-17 18:44:38 -07001841 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001842 break; /* will return 0 */
1843
1844 if (signr != SIGKILL) {
1845 signr = ptrace_signal(signr, info,
1846 regs, cookie);
1847 if (!signr)
1848 continue;
1849 }
1850
1851 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1855 continue;
1856 if (ka->sa.sa_handler != SIG_DFL) {
1857 /* Run the handler. */
1858 *return_ka = *ka;
1859
1860 if (ka->sa.sa_flags & SA_ONESHOT)
1861 ka->sa.sa_handler = SIG_DFL;
1862
1863 break; /* will return non-zero "signr" value */
1864 }
1865
1866 /*
1867 * Now we are doing the default action for this signal.
1868 */
1869 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1870 continue;
1871
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001872 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001873 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001874 * Container-init gets no signals it doesn't want from same
1875 * container.
1876 *
1877 * Note that if global/container-init sees a sig_kernel_only()
1878 * signal here, the signal must have been generated internally
1879 * or must have come from an ancestor namespace. In either
1880 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001881 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001882 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001883 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 continue;
1885
1886 if (sig_kernel_stop(signr)) {
1887 /*
1888 * The default action is to stop all threads in
1889 * the thread group. The job control signals
1890 * do nothing in an orphaned pgrp, but SIGSTOP
1891 * always works. Note that siglock needs to be
1892 * dropped during the call to is_orphaned_pgrp()
1893 * because of lock ordering with tasklist_lock.
1894 * This allows an intervening SIGCONT to be posted.
1895 * We need to check for that and bail out if necessary.
1896 */
1897 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001898 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 /* signals can be posted during this window */
1901
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001902 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 goto relock;
1904
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001905 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001908 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* It released the siglock. */
1910 goto relock;
1911 }
1912
1913 /*
1914 * We didn't actually stop, due to a race
1915 * with SIGCONT or something like that.
1916 */
1917 continue;
1918 }
1919
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001920 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /*
1923 * Anything else is fatal, maybe with a core dump.
1924 */
1925 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001928 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001929 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * If it was able to dump core, this kills all
1932 * other threads in the group and synchronizes with
1933 * their demise. If we lost the race with another
1934 * thread getting here, it set group_exit_code
1935 * first and our do_group_exit call below will use
1936 * that value and ignore the one we pass it.
1937 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001938 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940
1941 /*
1942 * Death signals, no core dump.
1943 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001944 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* NOTREACHED */
1946 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001947 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return signr;
1949}
1950
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001951void exit_signals(struct task_struct *tsk)
1952{
1953 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001954 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001955
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001956 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1957 tsk->flags |= PF_EXITING;
1958 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001959 }
1960
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001961 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001962 /*
1963 * From now this task is not visible for group-wide signals,
1964 * see wants_signal(), do_signal_stop().
1965 */
1966 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001967 if (!signal_pending(tsk))
1968 goto out;
1969
1970 /* It could be that __group_complete_signal() choose us to
1971 * notify about group-wide signal. Another thread should be
1972 * woken now to take the signal since we will not.
1973 */
1974 for (t = tsk; (t = next_thread(t)) != tsk; )
1975 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1976 recalc_sigpending_and_wake(t);
1977
1978 if (unlikely(tsk->signal->group_stop_count) &&
1979 !--tsk->signal->group_stop_count) {
1980 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1981 group_stop = 1;
1982 }
1983out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001984 spin_unlock_irq(&tsk->sighand->siglock);
1985
Roland McGrathfa00b802008-07-25 19:45:54 -07001986 if (unlikely(group_stop) && tracehook_notify_jctl(1, CLD_STOPPED)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001987 read_lock(&tasklist_lock);
1988 do_notify_parent_cldstop(tsk, CLD_STOPPED);
1989 read_unlock(&tasklist_lock);
1990 }
1991}
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993EXPORT_SYMBOL(recalc_sigpending);
1994EXPORT_SYMBOL_GPL(dequeue_signal);
1995EXPORT_SYMBOL(flush_signals);
1996EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997EXPORT_SYMBOL(send_sig);
1998EXPORT_SYMBOL(send_sig_info);
1999EXPORT_SYMBOL(sigprocmask);
2000EXPORT_SYMBOL(block_all_signals);
2001EXPORT_SYMBOL(unblock_all_signals);
2002
2003
2004/*
2005 * System call entry points.
2006 */
2007
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002008SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
2010 struct restart_block *restart = &current_thread_info()->restart_block;
2011 return restart->fn(restart);
2012}
2013
2014long do_no_restart_syscall(struct restart_block *param)
2015{
2016 return -EINTR;
2017}
2018
2019/*
2020 * We don't need to get the kernel lock - this is all local to this
2021 * particular thread.. (and that's good, because this is _heavily_
2022 * used by various programs)
2023 */
2024
2025/*
2026 * This is also useful for kernel threads that want to temporarily
2027 * (or permanently) block certain signals.
2028 *
2029 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2030 * interface happily blocks "unblockable" signals like SIGKILL
2031 * and friends.
2032 */
2033int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2034{
2035 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002038 if (oldset)
2039 *oldset = current->blocked;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 error = 0;
2042 switch (how) {
2043 case SIG_BLOCK:
2044 sigorsets(&current->blocked, &current->blocked, set);
2045 break;
2046 case SIG_UNBLOCK:
2047 signandsets(&current->blocked, &current->blocked, set);
2048 break;
2049 case SIG_SETMASK:
2050 current->blocked = *set;
2051 break;
2052 default:
2053 error = -EINVAL;
2054 }
2055 recalc_sigpending();
2056 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return error;
2059}
2060
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002061SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2062 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
2064 int error = -EINVAL;
2065 sigset_t old_set, new_set;
2066
2067 /* XXX: Don't preclude handling different sized sigset_t's. */
2068 if (sigsetsize != sizeof(sigset_t))
2069 goto out;
2070
2071 if (set) {
2072 error = -EFAULT;
2073 if (copy_from_user(&new_set, set, sizeof(*set)))
2074 goto out;
2075 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2076
2077 error = sigprocmask(how, &new_set, &old_set);
2078 if (error)
2079 goto out;
2080 if (oset)
2081 goto set_old;
2082 } else if (oset) {
2083 spin_lock_irq(&current->sighand->siglock);
2084 old_set = current->blocked;
2085 spin_unlock_irq(&current->sighand->siglock);
2086
2087 set_old:
2088 error = -EFAULT;
2089 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2090 goto out;
2091 }
2092 error = 0;
2093out:
2094 return error;
2095}
2096
2097long do_sigpending(void __user *set, unsigned long sigsetsize)
2098{
2099 long error = -EINVAL;
2100 sigset_t pending;
2101
2102 if (sigsetsize > sizeof(sigset_t))
2103 goto out;
2104
2105 spin_lock_irq(&current->sighand->siglock);
2106 sigorsets(&pending, &current->pending.signal,
2107 &current->signal->shared_pending.signal);
2108 spin_unlock_irq(&current->sighand->siglock);
2109
2110 /* Outside the lock because only this thread touches it. */
2111 sigandsets(&pending, &current->blocked, &pending);
2112
2113 error = -EFAULT;
2114 if (!copy_to_user(set, &pending, sigsetsize))
2115 error = 0;
2116
2117out:
2118 return error;
2119}
2120
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002121SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
2123 return do_sigpending(set, sigsetsize);
2124}
2125
2126#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2127
2128int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2129{
2130 int err;
2131
2132 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2133 return -EFAULT;
2134 if (from->si_code < 0)
2135 return __copy_to_user(to, from, sizeof(siginfo_t))
2136 ? -EFAULT : 0;
2137 /*
2138 * If you change siginfo_t structure, please be sure
2139 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002140 * Please remember to update the signalfd_copyinfo() function
2141 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 * It should never copy any pad contained in the structure
2143 * to avoid security leaks, but must copy the generic
2144 * 3 ints plus the relevant union member.
2145 */
2146 err = __put_user(from->si_signo, &to->si_signo);
2147 err |= __put_user(from->si_errno, &to->si_errno);
2148 err |= __put_user((short)from->si_code, &to->si_code);
2149 switch (from->si_code & __SI_MASK) {
2150 case __SI_KILL:
2151 err |= __put_user(from->si_pid, &to->si_pid);
2152 err |= __put_user(from->si_uid, &to->si_uid);
2153 break;
2154 case __SI_TIMER:
2155 err |= __put_user(from->si_tid, &to->si_tid);
2156 err |= __put_user(from->si_overrun, &to->si_overrun);
2157 err |= __put_user(from->si_ptr, &to->si_ptr);
2158 break;
2159 case __SI_POLL:
2160 err |= __put_user(from->si_band, &to->si_band);
2161 err |= __put_user(from->si_fd, &to->si_fd);
2162 break;
2163 case __SI_FAULT:
2164 err |= __put_user(from->si_addr, &to->si_addr);
2165#ifdef __ARCH_SI_TRAPNO
2166 err |= __put_user(from->si_trapno, &to->si_trapno);
2167#endif
2168 break;
2169 case __SI_CHLD:
2170 err |= __put_user(from->si_pid, &to->si_pid);
2171 err |= __put_user(from->si_uid, &to->si_uid);
2172 err |= __put_user(from->si_status, &to->si_status);
2173 err |= __put_user(from->si_utime, &to->si_utime);
2174 err |= __put_user(from->si_stime, &to->si_stime);
2175 break;
2176 case __SI_RT: /* This is not generated by the kernel as of now. */
2177 case __SI_MESGQ: /* But this is */
2178 err |= __put_user(from->si_pid, &to->si_pid);
2179 err |= __put_user(from->si_uid, &to->si_uid);
2180 err |= __put_user(from->si_ptr, &to->si_ptr);
2181 break;
2182 default: /* this is just in case for now ... */
2183 err |= __put_user(from->si_pid, &to->si_pid);
2184 err |= __put_user(from->si_uid, &to->si_uid);
2185 break;
2186 }
2187 return err;
2188}
2189
2190#endif
2191
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002192SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2193 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2194 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 int ret, sig;
2197 sigset_t these;
2198 struct timespec ts;
2199 siginfo_t info;
2200 long timeout = 0;
2201
2202 /* XXX: Don't preclude handling different sized sigset_t's. */
2203 if (sigsetsize != sizeof(sigset_t))
2204 return -EINVAL;
2205
2206 if (copy_from_user(&these, uthese, sizeof(these)))
2207 return -EFAULT;
2208
2209 /*
2210 * Invert the set of allowed signals to get those we
2211 * want to block.
2212 */
2213 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2214 signotset(&these);
2215
2216 if (uts) {
2217 if (copy_from_user(&ts, uts, sizeof(ts)))
2218 return -EFAULT;
2219 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2220 || ts.tv_sec < 0)
2221 return -EINVAL;
2222 }
2223
2224 spin_lock_irq(&current->sighand->siglock);
2225 sig = dequeue_signal(current, &these, &info);
2226 if (!sig) {
2227 timeout = MAX_SCHEDULE_TIMEOUT;
2228 if (uts)
2229 timeout = (timespec_to_jiffies(&ts)
2230 + (ts.tv_sec || ts.tv_nsec));
2231
2232 if (timeout) {
2233 /* None ready -- temporarily unblock those we're
2234 * interested while we are sleeping in so that we'll
2235 * be awakened when they arrive. */
2236 current->real_blocked = current->blocked;
2237 sigandsets(&current->blocked, &current->blocked, &these);
2238 recalc_sigpending();
2239 spin_unlock_irq(&current->sighand->siglock);
2240
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002241 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 spin_lock_irq(&current->sighand->siglock);
2244 sig = dequeue_signal(current, &these, &info);
2245 current->blocked = current->real_blocked;
2246 siginitset(&current->real_blocked, 0);
2247 recalc_sigpending();
2248 }
2249 }
2250 spin_unlock_irq(&current->sighand->siglock);
2251
2252 if (sig) {
2253 ret = sig;
2254 if (uinfo) {
2255 if (copy_siginfo_to_user(uinfo, &info))
2256 ret = -EFAULT;
2257 }
2258 } else {
2259 ret = -EAGAIN;
2260 if (timeout)
2261 ret = -EINTR;
2262 }
2263
2264 return ret;
2265}
2266
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002267SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 struct siginfo info;
2270
2271 info.si_signo = sig;
2272 info.si_errno = 0;
2273 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002274 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002275 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 return kill_something_info(sig, &info, pid);
2278}
2279
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002280static int
2281do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002282{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002283 struct task_struct *p;
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002284 unsigned long flags;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002285 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002286
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002287 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002288 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002289 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002290 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002291 /*
2292 * The null signal is a permissions and process existence
2293 * probe. No signal is actually delivered.
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002294 *
2295 * If lock_task_sighand() fails we pretend the task dies
2296 * after receiving the signal. The window is tiny, and the
2297 * signal is private anyway.
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002298 */
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002299 if (!error && sig && lock_task_sighand(p, &flags)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002300 error = specific_send_sig_info(sig, info, p);
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002301 unlock_task_sighand(p, &flags);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002302 }
2303 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002304 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002305
2306 return error;
2307}
2308
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002309static int do_tkill(pid_t tgid, pid_t pid, int sig)
2310{
2311 struct siginfo info;
2312
2313 info.si_signo = sig;
2314 info.si_errno = 0;
2315 info.si_code = SI_TKILL;
2316 info.si_pid = task_tgid_vnr(current);
2317 info.si_uid = current_uid();
2318
2319 return do_send_specific(tgid, pid, sig, &info);
2320}
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/**
2323 * sys_tgkill - send signal to one specific thread
2324 * @tgid: the thread group ID of the thread
2325 * @pid: the PID of the thread
2326 * @sig: signal to be sent
2327 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002328 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 * exists but it's not belonging to the target process anymore. This
2330 * method solves the problem of threads exiting and PIDs getting reused.
2331 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002332SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /* This is only valid for single tasks */
2335 if (pid <= 0 || tgid <= 0)
2336 return -EINVAL;
2337
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002338 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339}
2340
2341/*
2342 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2343 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002344SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* This is only valid for single tasks */
2347 if (pid <= 0)
2348 return -EINVAL;
2349
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002350 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351}
2352
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002353SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2354 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
2356 siginfo_t info;
2357
2358 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2359 return -EFAULT;
2360
2361 /* Not even root can pretend to send signals from the kernel.
2362 Nor can they impersonate a kill(), which adds source info. */
2363 if (info.si_code >= 0)
2364 return -EPERM;
2365 info.si_signo = sig;
2366
2367 /* POSIX.1b doesn't mention process groups. */
2368 return kill_proc_info(sig, &info, pid);
2369}
2370
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002371long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2372{
2373 /* This is only valid for single tasks */
2374 if (pid <= 0 || tgid <= 0)
2375 return -EINVAL;
2376
2377 /* Not even root can pretend to send signals from the kernel.
2378 Nor can they impersonate a kill(), which adds source info. */
2379 if (info->si_code >= 0)
2380 return -EPERM;
2381 info->si_signo = sig;
2382
2383 return do_send_specific(tgid, pid, sig, info);
2384}
2385
2386SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2387 siginfo_t __user *, uinfo)
2388{
2389 siginfo_t info;
2390
2391 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2392 return -EFAULT;
2393
2394 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2395}
2396
Oleg Nesterov88531f72006-03-28 16:11:24 -08002397int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002399 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 struct k_sigaction *k;
George Anzinger71fabd5e2006-01-08 01:02:48 -08002401 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002403 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return -EINVAL;
2405
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002406 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (oact)
2410 *oact = *k;
2411
2412 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002413 sigdelsetmask(&act->sa.sa_mask,
2414 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002415 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 /*
2417 * POSIX 3.3.1.3:
2418 * "Setting a signal action to SIG_IGN for a signal that is
2419 * pending shall cause the pending signal to be discarded,
2420 * whether or not it is blocked."
2421 *
2422 * "Setting a signal action to SIG_DFL for a signal that is
2423 * pending and whose default action is to ignore the signal
2424 * (for example, SIGCHLD), shall cause the pending signal to
2425 * be discarded, whether or not it is blocked"
2426 */
Roland McGrath35de2542008-07-25 19:45:51 -07002427 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd5e2006-01-08 01:02:48 -08002428 sigemptyset(&mask);
2429 sigaddset(&mask, sig);
2430 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 do {
George Anzinger71fabd5e2006-01-08 01:02:48 -08002432 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 t = next_thread(t);
2434 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437
2438 spin_unlock_irq(&current->sighand->siglock);
2439 return 0;
2440}
2441
2442int
2443do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2444{
2445 stack_t oss;
2446 int error;
2447
Linus Torvalds0083fc22009-08-01 10:34:56 -07002448 oss.ss_sp = (void __user *) current->sas_ss_sp;
2449 oss.ss_size = current->sas_ss_size;
2450 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
2452 if (uss) {
2453 void __user *ss_sp;
2454 size_t ss_size;
2455 int ss_flags;
2456
2457 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002458 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2459 goto out;
2460 error = __get_user(ss_sp, &uss->ss_sp) |
2461 __get_user(ss_flags, &uss->ss_flags) |
2462 __get_user(ss_size, &uss->ss_size);
2463 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto out;
2465
2466 error = -EPERM;
2467 if (on_sig_stack(sp))
2468 goto out;
2469
2470 error = -EINVAL;
2471 /*
2472 *
2473 * Note - this code used to test ss_flags incorrectly
2474 * old code may have been written using ss_flags==0
2475 * to mean ss_flags==SS_ONSTACK (as this was the only
2476 * way that worked) - this fix preserves that older
2477 * mechanism
2478 */
2479 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2480 goto out;
2481
2482 if (ss_flags == SS_DISABLE) {
2483 ss_size = 0;
2484 ss_sp = NULL;
2485 } else {
2486 error = -ENOMEM;
2487 if (ss_size < MINSIGSTKSZ)
2488 goto out;
2489 }
2490
2491 current->sas_ss_sp = (unsigned long) ss_sp;
2492 current->sas_ss_size = ss_size;
2493 }
2494
Linus Torvalds0083fc22009-08-01 10:34:56 -07002495 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (uoss) {
2497 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002498 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002500 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2501 __put_user(oss.ss_size, &uoss->ss_size) |
2502 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505out:
2506 return error;
2507}
2508
2509#ifdef __ARCH_WANT_SYS_SIGPENDING
2510
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002511SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
2513 return do_sigpending(set, sizeof(*set));
2514}
2515
2516#endif
2517
2518#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2519/* Some platforms have their own version with special arguments others
2520 support only sys_rt_sigprocmask. */
2521
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002522SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2523 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524{
2525 int error;
2526 old_sigset_t old_set, new_set;
2527
2528 if (set) {
2529 error = -EFAULT;
2530 if (copy_from_user(&new_set, set, sizeof(*set)))
2531 goto out;
2532 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2533
2534 spin_lock_irq(&current->sighand->siglock);
2535 old_set = current->blocked.sig[0];
2536
2537 error = 0;
2538 switch (how) {
2539 default:
2540 error = -EINVAL;
2541 break;
2542 case SIG_BLOCK:
2543 sigaddsetmask(&current->blocked, new_set);
2544 break;
2545 case SIG_UNBLOCK:
2546 sigdelsetmask(&current->blocked, new_set);
2547 break;
2548 case SIG_SETMASK:
2549 current->blocked.sig[0] = new_set;
2550 break;
2551 }
2552
2553 recalc_sigpending();
2554 spin_unlock_irq(&current->sighand->siglock);
2555 if (error)
2556 goto out;
2557 if (oset)
2558 goto set_old;
2559 } else if (oset) {
2560 old_set = current->blocked.sig[0];
2561 set_old:
2562 error = -EFAULT;
2563 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2564 goto out;
2565 }
2566 error = 0;
2567out:
2568 return error;
2569}
2570#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2571
2572#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002573SYSCALL_DEFINE4(rt_sigaction, int, sig,
2574 const struct sigaction __user *, act,
2575 struct sigaction __user *, oact,
2576 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577{
2578 struct k_sigaction new_sa, old_sa;
2579 int ret = -EINVAL;
2580
2581 /* XXX: Don't preclude handling different sized sigset_t's. */
2582 if (sigsetsize != sizeof(sigset_t))
2583 goto out;
2584
2585 if (act) {
2586 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2587 return -EFAULT;
2588 }
2589
2590 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2591
2592 if (!ret && oact) {
2593 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2594 return -EFAULT;
2595 }
2596out:
2597 return ret;
2598}
2599#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2600
2601#ifdef __ARCH_WANT_SYS_SGETMASK
2602
2603/*
2604 * For backwards compatibility. Functionality superseded by sigprocmask.
2605 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002606SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
2608 /* SMP safe */
2609 return current->blocked.sig[0];
2610}
2611
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002612SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 int old;
2615
2616 spin_lock_irq(&current->sighand->siglock);
2617 old = current->blocked.sig[0];
2618
2619 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2620 sigmask(SIGSTOP)));
2621 recalc_sigpending();
2622 spin_unlock_irq(&current->sighand->siglock);
2623
2624 return old;
2625}
2626#endif /* __ARCH_WANT_SGETMASK */
2627
2628#ifdef __ARCH_WANT_SYS_SIGNAL
2629/*
2630 * For backwards compatibility. Functionality superseded by sigaction.
2631 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002632SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
2634 struct k_sigaction new_sa, old_sa;
2635 int ret;
2636
2637 new_sa.sa.sa_handler = handler;
2638 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002639 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 ret = do_sigaction(sig, &new_sa, &old_sa);
2642
2643 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2644}
2645#endif /* __ARCH_WANT_SYS_SIGNAL */
2646
2647#ifdef __ARCH_WANT_SYS_PAUSE
2648
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002649SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 current->state = TASK_INTERRUPTIBLE;
2652 schedule();
2653 return -ERESTARTNOHAND;
2654}
2655
2656#endif
2657
David Woodhouse150256d2006-01-18 17:43:57 -08002658#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002659SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002660{
2661 sigset_t newset;
2662
2663 /* XXX: Don't preclude handling different sized sigset_t's. */
2664 if (sigsetsize != sizeof(sigset_t))
2665 return -EINVAL;
2666
2667 if (copy_from_user(&newset, unewset, sizeof(newset)))
2668 return -EFAULT;
2669 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2670
2671 spin_lock_irq(&current->sighand->siglock);
2672 current->saved_sigmask = current->blocked;
2673 current->blocked = newset;
2674 recalc_sigpending();
2675 spin_unlock_irq(&current->sighand->siglock);
2676
2677 current->state = TASK_INTERRUPTIBLE;
2678 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002679 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002680 return -ERESTARTNOHAND;
2681}
2682#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2683
David Howellsf269fdd2006-09-27 01:50:23 -07002684__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2685{
2686 return NULL;
2687}
2688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689void __init signals_init(void)
2690{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002691 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}