blob: 349d44937406561478648dc5fb0935aefbb019f4 [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>
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050030#define CREATE_TRACE_POINTS
31#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/param.h>
34#include <asm/uaccess.h>
35#include <asm/unistd.h>
36#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040037#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/*
40 * SLAB caches for signal bits.
41 */
42
Christoph Lametere18b8902006-12-06 20:33:20 -080043static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Roland McGrath35de2542008-07-25 19:45:51 -070045static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070046{
Roland McGrath35de2542008-07-25 19:45:51 -070047 return t->sighand->action[sig - 1].sa.sa_handler;
48}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070049
Roland McGrath35de2542008-07-25 19:45:51 -070050static int sig_handler_ignored(void __user *handler, int sig)
51{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070052 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070053 return handler == SIG_IGN ||
54 (handler == SIG_DFL && sig_kernel_ignore(sig));
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070057static int sig_task_ignored(struct task_struct *t, int sig,
58 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Roland McGrath35de2542008-07-25 19:45:51 -070060 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Oleg Nesterovf008faf2009-04-02 16:58:02 -070062 handler = sig_handler(t, sig);
63
64 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070065 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070066 return 1;
67
68 return sig_handler_ignored(handler, sig);
69}
70
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070071static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 /*
74 * Blocked signals are never ignored, since the
75 * signal handler may change by the time it is
76 * unblocked.
77 */
Roland McGrath325d22d2007-11-12 15:41:55 -080078 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070081 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070082 return 0;
83
84 /*
85 * Tracers may want to know about even ignored signals.
86 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070087 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
90/*
91 * Re-calculate pending state from the set of locally pending
92 * signals, globally pending signals, and blocked signals.
93 */
94static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
95{
96 unsigned long ready;
97 long i;
98
99 switch (_NSIG_WORDS) {
100 default:
101 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
102 ready |= signal->sig[i] &~ blocked->sig[i];
103 break;
104
105 case 4: ready = signal->sig[3] &~ blocked->sig[3];
106 ready |= signal->sig[2] &~ blocked->sig[2];
107 ready |= signal->sig[1] &~ blocked->sig[1];
108 ready |= signal->sig[0] &~ blocked->sig[0];
109 break;
110
111 case 2: ready = signal->sig[1] &~ blocked->sig[1];
112 ready |= signal->sig[0] &~ blocked->sig[0];
113 break;
114
115 case 1: ready = signal->sig[0] &~ blocked->sig[0];
116 }
117 return ready != 0;
118}
119
120#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
121
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700122static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 if (t->signal->group_stop_count > 0 ||
125 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700126 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700128 return 1;
129 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700130 /*
131 * We must never clear the flag in another thread, or in current
132 * when it's possible the current syscall is returning -ERESTART*.
133 * So we don't clear it here, and only callers who know they should do.
134 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700135 return 0;
136}
137
138/*
139 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
140 * This is superfluous when called on current, the wakeup is a harmless no-op.
141 */
142void recalc_sigpending_and_wake(struct task_struct *t)
143{
144 if (recalc_sigpending_tsk(t))
145 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148void recalc_sigpending(void)
149{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700150 if (unlikely(tracehook_force_sigpending()))
151 set_thread_flag(TIF_SIGPENDING);
152 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700153 clear_thread_flag(TIF_SIGPENDING);
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/* Given the mask, find the first available signal that should be serviced. */
158
Davide Libenzifba2afa2007-05-10 22:23:13 -0700159int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 unsigned long i, *s, *m, x;
162 int sig = 0;
163
164 s = pending->signal.sig;
165 m = mask->sig;
166 switch (_NSIG_WORDS) {
167 default:
168 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
169 if ((x = *s &~ *m) != 0) {
170 sig = ffz(~x) + i*_NSIG_BPW + 1;
171 break;
172 }
173 break;
174
175 case 2: if ((x = s[0] &~ m[0]) != 0)
176 sig = 1;
177 else if ((x = s[1] &~ m[1]) != 0)
178 sig = _NSIG_BPW + 1;
179 else
180 break;
181 sig += ffz(~x);
182 break;
183
184 case 1: if ((x = *s &~ *m) != 0)
185 sig = ffz(~x) + 1;
186 break;
187 }
188
189 return sig;
190}
191
David Howellsc69e8d92008-11-14 10:39:19 +1100192/*
193 * allocate a new signal queue record
194 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100195 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100196 */
Al Virodd0fc662005-10-07 07:46:04 +0100197static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 int override_rlimit)
199{
200 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800201 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800203 /*
David Howellsc69e8d92008-11-14 10:39:19 +1100204 * We won't get problems with the target's UID changing under us
205 * because changing it requires RCU be used, and if t != current, the
206 * caller must be holding the RCU readlock (by way of a spinlock) and
207 * we use RCU protection here
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800208 */
David Howellsd84f4f92008-11-14 10:39:23 +1100209 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800210 atomic_inc(&user->sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800212 atomic_read(&user->sigpending) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
214 q = kmem_cache_alloc(sigqueue_cachep, flags);
215 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800216 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100217 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else {
219 INIT_LIST_HEAD(&q->list);
220 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100221 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
David Howellsd84f4f92008-11-14 10:39:23 +1100223
224 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Andrew Morton514a01b2006-02-03 03:04:41 -0800227static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 if (q->flags & SIGQUEUE_PREALLOC)
230 return;
231 atomic_dec(&q->user->sigpending);
232 free_uid(q->user);
233 kmem_cache_free(sigqueue_cachep, q);
234}
235
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800236void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct sigqueue *q;
239
240 sigemptyset(&queue->signal);
241 while (!list_empty(&queue->list)) {
242 q = list_entry(queue->list.next, struct sigqueue , list);
243 list_del_init(&q->list);
244 __sigqueue_free(q);
245 }
246}
247
248/*
249 * Flush all pending signals for a task.
250 */
David Howells3bcac022009-04-29 13:45:05 +0100251void __flush_signals(struct task_struct *t)
252{
253 clear_tsk_thread_flag(t, TIF_SIGPENDING);
254 flush_sigqueue(&t->pending);
255 flush_sigqueue(&t->signal->shared_pending);
256}
257
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800258void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 unsigned long flags;
261
262 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100263 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_unlock_irqrestore(&t->sighand->siglock, flags);
265}
266
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400267static void __flush_itimer_signals(struct sigpending *pending)
268{
269 sigset_t signal, retain;
270 struct sigqueue *q, *n;
271
272 signal = pending->signal;
273 sigemptyset(&retain);
274
275 list_for_each_entry_safe(q, n, &pending->list, list) {
276 int sig = q->info.si_signo;
277
278 if (likely(q->info.si_code != SI_TIMER)) {
279 sigaddset(&retain, sig);
280 } else {
281 sigdelset(&signal, sig);
282 list_del_init(&q->list);
283 __sigqueue_free(q);
284 }
285 }
286
287 sigorsets(&pending->signal, &signal, &retain);
288}
289
290void flush_itimer_signals(void)
291{
292 struct task_struct *tsk = current;
293 unsigned long flags;
294
295 spin_lock_irqsave(&tsk->sighand->siglock, flags);
296 __flush_itimer_signals(&tsk->pending);
297 __flush_itimer_signals(&tsk->signal->shared_pending);
298 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
299}
300
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700301void ignore_signals(struct task_struct *t)
302{
303 int i;
304
305 for (i = 0; i < _NSIG; ++i)
306 t->sighand->action[i].sa.sa_handler = SIG_IGN;
307
308 flush_signals(t);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * Flush all handlers for a task.
313 */
314
315void
316flush_signal_handlers(struct task_struct *t, int force_default)
317{
318 int i;
319 struct k_sigaction *ka = &t->sighand->action[0];
320 for (i = _NSIG ; i != 0 ; i--) {
321 if (force_default || ka->sa.sa_handler != SIG_IGN)
322 ka->sa.sa_handler = SIG_DFL;
323 ka->sa.sa_flags = 0;
324 sigemptyset(&ka->sa.sa_mask);
325 ka++;
326 }
327}
328
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200329int unhandled_signal(struct task_struct *tsk, int sig)
330{
Roland McGrath445a91d2008-07-25 19:45:52 -0700331 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700332 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200333 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700334 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200335 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700336 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340/* Notify the system that a driver wants to block all signals for this
341 * process, and wants to be notified if any signals at all were to be
342 * sent/acted upon. If the notifier routine returns non-zero, then the
343 * signal will be acted upon after all. If the notifier routine returns 0,
344 * then then signal will be blocked. Only one block per process is
345 * allowed. priv is a pointer to private data that the notifier routine
346 * can use to determine if the signal should be blocked or not. */
347
348void
349block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
350{
351 unsigned long flags;
352
353 spin_lock_irqsave(&current->sighand->siglock, flags);
354 current->notifier_mask = mask;
355 current->notifier_data = priv;
356 current->notifier = notifier;
357 spin_unlock_irqrestore(&current->sighand->siglock, flags);
358}
359
360/* Notify the system that blocking has ended. */
361
362void
363unblock_all_signals(void)
364{
365 unsigned long flags;
366
367 spin_lock_irqsave(&current->sighand->siglock, flags);
368 current->notifier = NULL;
369 current->notifier_data = NULL;
370 recalc_sigpending();
371 spin_unlock_irqrestore(&current->sighand->siglock, flags);
372}
373
Oleg Nesterov100360f2008-07-25 01:47:29 -0700374static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /*
379 * Collect the siginfo appropriate to this signal. Check if
380 * there is another siginfo for the same signal.
381 */
382 list_for_each_entry(q, &list->list, list) {
383 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700384 if (first)
385 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 first = q;
387 }
388 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700389
390 sigdelset(&list->signal, sig);
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700393still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 list_del_init(&first->list);
395 copy_siginfo(info, &first->info);
396 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Ok, it wasn't in the queue. This must be
399 a fast-pathed signal or we must have been
400 out of queue space. So zero out the info.
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 info->si_signo = sig;
403 info->si_errno = 0;
404 info->si_code = 0;
405 info->si_pid = 0;
406 info->si_uid = 0;
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
411 siginfo_t *info)
412{
Roland McGrath27d91e02006-09-29 02:00:31 -0700413 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (sig) {
416 if (current->notifier) {
417 if (sigismember(current->notifier_mask, sig)) {
418 if (!(current->notifier)(current->notifier_data)) {
419 clear_thread_flag(TIF_SIGPENDING);
420 return 0;
421 }
422 }
423 }
424
Oleg Nesterov100360f2008-07-25 01:47:29 -0700425 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 return sig;
429}
430
431/*
432 * Dequeue a signal and return the element to the caller, which is
433 * expected to free it.
434 *
435 * All callers have to hold the siglock.
436 */
437int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
438{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700439 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000440
441 /* We only dequeue private signals from ourselves, we don't let
442 * signalfd steal them
443 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700444 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800445 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 signr = __dequeue_signal(&tsk->signal->shared_pending,
447 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800448 /*
449 * itimer signal ?
450 *
451 * itimers are process shared and we restart periodic
452 * itimers in the signal delivery path to prevent DoS
453 * attacks in the high resolution timer case. This is
454 * compliant with the old way of self restarting
455 * itimers, as the SIGALRM is a legacy signal and only
456 * queued once. Changing the restart behaviour to
457 * restart the timer in the signal dequeue path is
458 * reducing the timer noise on heavy loaded !highres
459 * systems too.
460 */
461 if (unlikely(signr == SIGALRM)) {
462 struct hrtimer *tmr = &tsk->signal->real_timer;
463
464 if (!hrtimer_is_queued(tmr) &&
465 tsk->signal->it_real_incr.tv64 != 0) {
466 hrtimer_forward(tmr, tmr->base->get_time(),
467 tsk->signal->it_real_incr);
468 hrtimer_restart(tmr);
469 }
470 }
471 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700472
Davide Libenzib8fceee2007-09-20 12:40:16 -0700473 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700474 if (!signr)
475 return 0;
476
477 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800478 /*
479 * Set a marker that we have dequeued a stop signal. Our
480 * caller might release the siglock and then the pending
481 * stop signal it is about to process is no longer in the
482 * pending bitmasks, but must still be cleared by a SIGCONT
483 * (and overruled by a SIGKILL). So those cases clear this
484 * shared flag after we've set it. Note that this flag may
485 * remain set after the signal we return is ignored or
486 * handled. That doesn't matter because its only purpose
487 * is to alert stop-signal processing code when another
488 * processor has come along and cleared the flag.
489 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700490 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800491 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700492 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Release the siglock to ensure proper locking order
495 * of timer locks outside of siglocks. Note, we leave
496 * irqs disabled here, since the posix-timers code is
497 * about to disable them again anyway.
498 */
499 spin_unlock(&tsk->sighand->siglock);
500 do_schedule_next_timer(info);
501 spin_lock(&tsk->sighand->siglock);
502 }
503 return signr;
504}
505
506/*
507 * Tell a process that it has a new active signal..
508 *
509 * NOTE! we rely on the previous spin_lock to
510 * lock interrupts for us! We can only be called with
511 * "siglock" held, and the local interrupt must
512 * have been disabled when that got acquired!
513 *
514 * No need to set need_resched since signal event passing
515 * goes through ->blocked
516 */
517void signal_wake_up(struct task_struct *t, int resume)
518{
519 unsigned int mask;
520
521 set_tsk_thread_flag(t, TIF_SIGPENDING);
522
523 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500524 * For SIGKILL, we want to wake it up in the stopped/traced/killable
525 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * executing another processor and just now entering stopped state.
527 * By using wake_up_state, we ensure the process will wake up and
528 * handle its death signal.
529 */
530 mask = TASK_INTERRUPTIBLE;
531 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500532 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!wake_up_state(t, mask))
534 kick_process(t);
535}
536
537/*
538 * Remove signals in mask from the pending set and queue.
539 * Returns 1 if any signals were found.
540 *
541 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800542 *
543 * This version takes a sigset mask and looks at all signals,
544 * not just those in the first mask word.
545 */
546static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
547{
548 struct sigqueue *q, *n;
549 sigset_t m;
550
551 sigandsets(&m, mask, &s->signal);
552 if (sigisemptyset(&m))
553 return 0;
554
555 signandsets(&s->signal, &s->signal, mask);
556 list_for_each_entry_safe(q, n, &s->list, list) {
557 if (sigismember(mask, q->info.si_signo)) {
558 list_del_init(&q->list);
559 __sigqueue_free(q);
560 }
561 }
562 return 1;
563}
564/*
565 * Remove signals in mask from the pending set and queue.
566 * Returns 1 if any signals were found.
567 *
568 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
570static int rm_from_queue(unsigned long mask, struct sigpending *s)
571{
572 struct sigqueue *q, *n;
573
574 if (!sigtestsetmask(&s->signal, mask))
575 return 0;
576
577 sigdelsetmask(&s->signal, mask);
578 list_for_each_entry_safe(q, n, &s->list, list) {
579 if (q->info.si_signo < SIGRTMIN &&
580 (mask & sigmask(q->info.si_signo))) {
581 list_del_init(&q->list);
582 __sigqueue_free(q);
583 }
584 }
585 return 1;
586}
587
588/*
589 * Bad permissions for sending the signal
David Howellsc69e8d92008-11-14 10:39:19 +1100590 * - the caller must hold at least the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 */
592static int check_kill_permission(int sig, struct siginfo *info,
593 struct task_struct *t)
594{
David Howellsc69e8d92008-11-14 10:39:19 +1100595 const struct cred *cred = current_cred(), *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700596 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700597 int error;
598
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700599 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700600 return -EINVAL;
601
602 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
603 return 0;
604
605 error = audit_signal_info(sig, t); /* Let audit system see the signal */
606 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400608
David Howellsc69e8d92008-11-14 10:39:19 +1100609 tcred = __task_cred(t);
610 if ((cred->euid ^ tcred->suid) &&
611 (cred->euid ^ tcred->uid) &&
612 (cred->uid ^ tcred->suid) &&
613 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700614 !capable(CAP_KILL)) {
615 switch (sig) {
616 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700617 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700618 /*
619 * We don't return the error if sid == NULL. The
620 * task was unhashed, the caller must notice this.
621 */
622 if (!sid || sid == task_session(current))
623 break;
624 default:
625 return -EPERM;
626 }
627 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100628
Amy Griffise54dc242007-03-29 18:01:04 -0400629 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700633 * Handle magic process-wide effects of stop/continue signals. Unlike
634 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 * time regardless of blocking, ignoring, or handling. This does the
636 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700637 * signals. The process stop is done as a signal action for SIG_DFL.
638 *
639 * Returns true if the signal should be actually delivered, otherwise
640 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700642static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700644 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct task_struct *t;
646
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700647 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700649 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700651 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 /*
653 * This is a stop signal. Remove SIGCONT from all queues.
654 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700655 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 t = p;
657 do {
658 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700659 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700661 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Remove all stop signals from all queues,
664 * and wake all threads.
665 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700666 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 t = p;
668 do {
669 unsigned int state;
670 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /*
672 * If there is a handler for SIGCONT, we must make
673 * sure that no thread returns to user mode before
674 * we post the signal, in case it was the only
675 * thread eligible to run the signal handler--then
676 * it must not do anything between resuming and
677 * running the handler. With the TIF_SIGPENDING
678 * flag set, the thread will pause and acquire the
679 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700680 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 *
682 * Wake up the stopped thread _after_ setting
683 * TIF_SIGPENDING
684 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500685 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
687 set_tsk_thread_flag(t, TIF_SIGPENDING);
688 state |= TASK_INTERRUPTIBLE;
689 }
690 wake_up_state(t, state);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700691 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700693 /*
694 * Notify the parent with CLD_CONTINUED if we were stopped.
695 *
696 * If we were in the middle of a group stop, we pretend it
697 * was already finished, and then continued. Since SIGCHLD
698 * doesn't queue we report only CLD_STOPPED, as if the next
699 * CLD_CONTINUED was dropped.
700 */
701 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700702 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700703 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700704 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700705 why |= SIGNAL_CLD_STOPPED;
706
707 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700708 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700709 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700710 * will take ->siglock, notice SIGNAL_CLD_MASK, and
711 * notify its parent. See get_signal_to_deliver().
712 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700713 signal->flags = why | SIGNAL_STOP_CONTINUED;
714 signal->group_stop_count = 0;
715 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
717 /*
718 * We are not stopped, but there could be a stop
719 * signal in the middle of being processed after
720 * being removed from the queue. Clear that too.
721 */
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700722 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700725
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700726 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700729/*
730 * Test if P wants to take SIG. After we've checked all threads with this,
731 * it's equivalent to finding no threads not blocking SIG. Any threads not
732 * blocking SIG were ruled out because they are not running and already
733 * have pending signals. Such threads will dequeue from the shared queue
734 * as soon as they're available, so putting the signal on the shared queue
735 * will be equivalent to sending it to one such thread.
736 */
737static inline int wants_signal(int sig, struct task_struct *p)
738{
739 if (sigismember(&p->blocked, sig))
740 return 0;
741 if (p->flags & PF_EXITING)
742 return 0;
743 if (sig == SIGKILL)
744 return 1;
745 if (task_is_stopped_or_traced(p))
746 return 0;
747 return task_curr(p) || !signal_pending(p);
748}
749
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700750static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700751{
752 struct signal_struct *signal = p->signal;
753 struct task_struct *t;
754
755 /*
756 * Now find a thread we can wake up to take the signal off the queue.
757 *
758 * If the main thread wants the signal, it gets first crack.
759 * Probably the least surprising to the average bear.
760 */
761 if (wants_signal(sig, p))
762 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700763 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700764 /*
765 * There is just one thread and it does not need to be woken.
766 * It will dequeue unblocked signals before it runs again.
767 */
768 return;
769 else {
770 /*
771 * Otherwise try to find a suitable thread.
772 */
773 t = signal->curr_target;
774 while (!wants_signal(sig, t)) {
775 t = next_thread(t);
776 if (t == signal->curr_target)
777 /*
778 * No thread needs to be woken.
779 * Any eligible threads will see
780 * the signal in the queue soon.
781 */
782 return;
783 }
784 signal->curr_target = t;
785 }
786
787 /*
788 * Found a killable thread. If the signal will be fatal,
789 * then start taking the whole group down immediately.
790 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700791 if (sig_fatal(p, sig) &&
792 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700793 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700794 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700795 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700796 /*
797 * This signal will be fatal to the whole group.
798 */
799 if (!sig_kernel_coredump(sig)) {
800 /*
801 * Start a group exit and wake everybody up.
802 * This way we don't have other threads
803 * running and doing things after a slower
804 * thread has the fatal signal pending.
805 */
806 signal->flags = SIGNAL_GROUP_EXIT;
807 signal->group_exit_code = sig;
808 signal->group_stop_count = 0;
809 t = p;
810 do {
811 sigaddset(&t->pending.signal, SIGKILL);
812 signal_wake_up(t, 1);
813 } while_each_thread(p, t);
814 return;
815 }
816 }
817
818 /*
819 * The signal is already in the shared-pending queue.
820 * Tell the chosen thread to wake up and dequeue it.
821 */
822 signal_wake_up(t, sig == SIGKILL);
823 return;
824}
825
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700826static inline int legacy_queue(struct sigpending *signals, int sig)
827{
828 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
829}
830
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700831static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
832 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700834 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700835 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200836 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Masami Hiramatsud1eb6502009-11-24 16:56:45 -0500838 trace_signal_generate(sig, info, t);
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400839
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700840 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700841
842 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700843 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700844
845 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700847 * Short-circuit ignored signals and support queuing
848 * exactly one non-rt signal, so that we can get more
849 * detailed information about the cause of the signal.
850 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700851 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700852 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700853 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * fast-pathed signals for kernel-internal things like SIGSTOP
855 * or SIGKILL.
856 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800857 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto out_set;
859
860 /* Real-time signals must be queued if sent by sigqueue, or
861 some other real-time mechanism. It is implementation
862 defined whether kill() does so. We attempt to do so, on
863 the principle of least surprise, but since kill is not
864 allowed to fail with EAGAIN when low on memory we just
865 make sure at least one signal gets delivered and don't
866 pass on the info struct. */
867
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200868 if (sig < SIGRTMIN)
869 override_rlimit = (is_si_special(info) || info->si_code >= 0);
870 else
871 override_rlimit = 0;
872
873 q = __sigqueue_alloc(t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
874 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700876 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800878 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 q->info.si_signo = sig;
880 q->info.si_errno = 0;
881 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800882 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800883 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100884 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800886 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 q->info.si_signo = sig;
888 q->info.si_errno = 0;
889 q->info.si_code = SI_KERNEL;
890 q->info.si_pid = 0;
891 q->info.si_uid = 0;
892 break;
893 default:
894 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700895 if (from_ancestor_ns)
896 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800899 } else if (!is_si_special(info)) {
900 if (sig >= SIGRTMIN && info->si_code != SI_USER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /*
902 * Queue overflow, abort. We may abort if the signal was rt
903 * and sent by user using something other than kill().
904 */
905 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907
908out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700909 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700910 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700911 complete_signal(sig, t, group);
912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700915static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
916 int group)
917{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700918 int from_ancestor_ns = 0;
919
920#ifdef CONFIG_PID_NS
921 if (!is_si_special(info) && SI_FROMUSER(info) &&
922 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
923 from_ancestor_ns = 1;
924#endif
925
926 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700927}
928
Ingo Molnar45807a12007-07-15 23:40:10 -0700929int print_fatal_signals;
930
931static void print_fatal_signal(struct pt_regs *regs, int signr)
932{
933 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700934 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700935
Al Viroca5cd872007-10-29 04:31:16 +0000936#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100937 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700938 {
939 int i;
940 for (i = 0; i < 16; i++) {
941 unsigned char insn;
942
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100943 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700944 printk("%02x ", insn);
945 }
946 }
947#endif
948 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800949 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700950 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800951 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700952}
953
954static int __init setup_print_fatal_signals(char *str)
955{
956 get_option (&str, &print_fatal_signals);
957
958 return 1;
959}
960
961__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700963int
964__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
965{
966 return send_signal(sig, info, p, 1);
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969static int
970specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
971{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700972 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700975int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
976 bool group)
977{
978 unsigned long flags;
979 int ret = -ESRCH;
980
981 if (lock_task_sighand(p, &flags)) {
982 ret = send_signal(sig, info, p, group);
983 unlock_task_sighand(p, &flags);
984 }
985
986 return ret;
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/*
990 * Force a signal that the process can't ignore: if necessary
991 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700992 *
993 * Note: If we unblock the signal, we always reset it to SIG_DFL,
994 * since we do not want to have a signal handler that was blocked
995 * be invoked when user space had explicitly blocked it.
996 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -0700997 * We don't want to have recursive SIGSEGV's etc, for example,
998 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000int
1001force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1002{
1003 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001004 int ret, blocked, ignored;
1005 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001008 action = &t->sighand->action[sig-1];
1009 ignored = action->sa.sa_handler == SIG_IGN;
1010 blocked = sigismember(&t->blocked, sig);
1011 if (blocked || ignored) {
1012 action->sa.sa_handler = SIG_DFL;
1013 if (blocked) {
1014 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001015 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001018 if (action->sa.sa_handler == SIG_DFL)
1019 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 ret = specific_send_sig_info(sig, info, t);
1021 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1022
1023 return ret;
1024}
1025
1026void
1027force_sig_specific(int sig, struct task_struct *t)
1028{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001029 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/*
1033 * Nuke all other threads in the group.
1034 */
1035void zap_other_threads(struct task_struct *p)
1036{
1037 struct task_struct *t;
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 p->signal->group_stop_count = 0;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 for (t = next_thread(p); t != p; t = next_thread(t)) {
1042 /*
1043 * Don't bother with already dead threads
1044 */
1045 if (t->exit_state)
1046 continue;
1047
Andrea Arcangeli30e0fca62005-10-30 15:02:38 -08001048 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 signal_wake_up(t, 1);
1051 }
1052}
1053
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001054struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1055{
1056 struct sighand_struct *sighand;
1057
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001058 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001059 for (;;) {
1060 sighand = rcu_dereference(tsk->sighand);
1061 if (unlikely(sighand == NULL))
1062 break;
1063
1064 spin_lock_irqsave(&sighand->siglock, *flags);
1065 if (likely(sighand == tsk->sighand))
1066 break;
1067 spin_unlock_irqrestore(&sighand->siglock, *flags);
1068 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001069 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001070
1071 return sighand;
1072}
1073
David Howellsc69e8d92008-11-14 10:39:19 +11001074/*
1075 * send signal info to all the members of a group
1076 * - the caller must hold the RCU read lock at least
1077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1079{
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001080 int ret = check_kill_permission(sig, info, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001082 if (!ret && sig)
1083 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 return ret;
1086}
1087
1088/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001089 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001091 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001093int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 struct task_struct *p = NULL;
1096 int retval, success;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 success = 0;
1099 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001100 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int err = group_send_sig_info(sig, info, p);
1102 success |= !err;
1103 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001104 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return success ? 0 : retval;
1106}
1107
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001108int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001110 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct task_struct *p;
1112
Ingo Molnare56d0902006-01-08 01:01:37 -08001113 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001114retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001115 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001116 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001118 if (unlikely(error == -ESRCH))
1119 /*
1120 * The task was unhashed in between, try again.
1121 * If it is dead, pid_task() will return NULL,
1122 * if we race with de_thread() it will find the
1123 * new leader.
1124 */
1125 goto retry;
1126 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001127 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return error;
1130}
1131
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001132int
1133kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001134{
1135 int error;
1136 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001137 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001138 rcu_read_unlock();
1139 return error;
1140}
1141
Eric W. Biederman2425c082006-10-02 02:17:28 -07001142/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1143int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001144 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001145{
1146 int ret = -EINVAL;
1147 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001148 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001149
1150 if (!valid_signal(sig))
1151 return ret;
1152
1153 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001154 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001155 if (!p) {
1156 ret = -ESRCH;
1157 goto out_unlock;
1158 }
David Howellsc69e8d92008-11-14 10:39:19 +11001159 pcred = __task_cred(p);
1160 if ((info == SEND_SIG_NOINFO ||
1161 (!is_si_special(info) && SI_FROMUSER(info))) &&
1162 euid != pcred->suid && euid != pcred->uid &&
1163 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001164 ret = -EPERM;
1165 goto out_unlock;
1166 }
David Quigley8f95dc52006-06-30 01:55:47 -07001167 ret = security_task_kill(p, info, sig, secid);
1168 if (ret)
1169 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001170 if (sig && p->sighand) {
1171 unsigned long flags;
1172 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001173 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001174 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1175 }
1176out_unlock:
1177 read_unlock(&tasklist_lock);
1178 return ret;
1179}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001180EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182/*
1183 * kill_something_info() interprets pid in interesting ways just like kill(2).
1184 *
1185 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1186 * is probably wrong. Should make it like BSD or SYSV.
1187 */
1188
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001189static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001191 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001192
1193 if (pid > 0) {
1194 rcu_read_lock();
1195 ret = kill_pid_info(sig, info, find_vpid(pid));
1196 rcu_read_unlock();
1197 return ret;
1198 }
1199
1200 read_lock(&tasklist_lock);
1201 if (pid != -1) {
1202 ret = __kill_pgrp_info(sig, info,
1203 pid ? find_vpid(-pid) : task_pgrp(current));
1204 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int retval = 0, count = 0;
1206 struct task_struct * p;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001209 if (task_pid_vnr(p) > 1 &&
1210 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int err = group_send_sig_info(sig, info, p);
1212 ++count;
1213 if (err != -EPERM)
1214 retval = err;
1215 }
1216 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001217 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001219 read_unlock(&tasklist_lock);
1220
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224/*
1225 * These are for backward compatibility with the rest of the kernel source.
1226 */
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228int
1229send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * Make sure legacy kernel users don't send in bad values
1233 * (normal paths check this in check_kill_permission).
1234 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001235 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return -EINVAL;
1237
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001238 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001241#define __si_special(priv) \
1242 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244int
1245send_sig(int sig, struct task_struct *p, int priv)
1246{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001247 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250void
1251force_sig(int sig, struct task_struct *p)
1252{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001253 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
1256/*
1257 * When things go south during signal handling, we
1258 * will force a SIGSEGV. And if the signal that caused
1259 * the problem was already a SIGSEGV, we'll want to
1260 * make sure we don't even try to deliver the signal..
1261 */
1262int
1263force_sigsegv(int sig, struct task_struct *p)
1264{
1265 if (sig == SIGSEGV) {
1266 unsigned long flags;
1267 spin_lock_irqsave(&p->sighand->siglock, flags);
1268 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1269 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1270 }
1271 force_sig(SIGSEGV, p);
1272 return 0;
1273}
1274
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001275int kill_pgrp(struct pid *pid, int sig, int priv)
1276{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001277 int ret;
1278
1279 read_lock(&tasklist_lock);
1280 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1281 read_unlock(&tasklist_lock);
1282
1283 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001284}
1285EXPORT_SYMBOL(kill_pgrp);
1286
1287int kill_pid(struct pid *pid, int sig, int priv)
1288{
1289 return kill_pid_info(sig, __si_special(priv), pid);
1290}
1291EXPORT_SYMBOL(kill_pid);
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293/*
1294 * These functions support sending signals using preallocated sigqueue
1295 * structures. This is needed "because realtime applications cannot
1296 * afford to lose notifications of asynchronous events, like timer
1297 * expirations or I/O completions". In the case of Posix Timers
1298 * we allocate the sigqueue structure from the timer_create. If this
1299 * allocation fails we are able to report the failure to the application
1300 * with an EAGAIN error.
1301 */
1302
1303struct sigqueue *sigqueue_alloc(void)
1304{
1305 struct sigqueue *q;
1306
1307 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1308 q->flags |= SIGQUEUE_PREALLOC;
1309 return(q);
1310}
1311
1312void sigqueue_free(struct sigqueue *q)
1313{
1314 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001315 spinlock_t *lock = &current->sighand->siglock;
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1318 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001319 * We must hold ->siglock while testing q->list
1320 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001321 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001323 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001324 q->flags &= ~SIGQUEUE_PREALLOC;
1325 /*
1326 * If it is queued it will be freed when dequeued,
1327 * like the "regular" sigqueue.
1328 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001329 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001330 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001331 spin_unlock_irqrestore(lock, flags);
1332
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001333 if (q)
1334 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001337int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001338{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001339 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001340 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001341 unsigned long flags;
1342 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001343
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001344 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001345
1346 ret = -1;
1347 if (!likely(lock_task_sighand(t, &flags)))
1348 goto ret;
1349
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001350 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001351 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001352 goto out;
1353
1354 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001355 if (unlikely(!list_empty(&q->list))) {
1356 /*
1357 * If an SI_TIMER entry is already queue just increment
1358 * the overrun count.
1359 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001360 BUG_ON(q->info.si_code != SI_TIMER);
1361 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001362 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001363 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001364 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001365
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001366 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001367 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001368 list_add_tail(&q->list, &pending->list);
1369 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001370 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001371out:
1372 unlock_task_sighand(t, &flags);
1373ret:
1374 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001375}
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * Let a parent know about the death of a child.
1379 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001380 *
1381 * Returns -1 if our parent ignored us and so we've switched to
1382 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001384int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
1386 struct siginfo info;
1387 unsigned long flags;
1388 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001389 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 BUG_ON(sig == -1);
1392
1393 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001394 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001396 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1398
1399 info.si_signo = sig;
1400 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001401 /*
1402 * we are under tasklist_lock here so our parent is tied to
1403 * us and cannot exit and release its namespace.
1404 *
1405 * the only it can is to switch its nsproxy with sys_unshare,
1406 * bu uncharing pid namespaces is not allowed, so we'll always
1407 * see relevant namespace
1408 *
1409 * write_lock() currently calls preempt_disable() which is the
1410 * same as rcu_read_lock(), but according to Oleg, this is not
1411 * correct to rely on this
1412 */
1413 rcu_read_lock();
1414 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001415 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001416 rcu_read_unlock();
1417
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001418 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1419 tsk->signal->utime));
1420 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1421 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 info.si_status = tsk->exit_code & 0x7f;
1424 if (tsk->exit_code & 0x80)
1425 info.si_code = CLD_DUMPED;
1426 else if (tsk->exit_code & 0x7f)
1427 info.si_code = CLD_KILLED;
1428 else {
1429 info.si_code = CLD_EXITED;
1430 info.si_status = tsk->exit_code >> 8;
1431 }
1432
1433 psig = tsk->parent->sighand;
1434 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001435 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1437 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1438 /*
1439 * We are exiting and our parent doesn't care. POSIX.1
1440 * defines special semantics for setting SIGCHLD to SIG_IGN
1441 * or setting the SA_NOCLDWAIT flag: we should be reaped
1442 * automatically and not left for our parent's wait4 call.
1443 * Rather than having the parent do it as a magic kind of
1444 * signal handler, we just set this to tell do_exit that we
1445 * can be cleaned up without becoming a zombie. Note that
1446 * we still call __wake_up_parent in this case, because a
1447 * blocked sys_wait4 might now return -ECHILD.
1448 *
1449 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1450 * is implementation-defined: we do (if you don't want
1451 * it, just use SIG_IGN instead).
1452 */
Roland McGrath1b046242008-08-19 20:37:07 -07001453 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001455 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001457 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 __group_send_sig_info(sig, &info, tsk->parent);
1459 __wake_up_parent(tsk, tsk->parent);
1460 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001461
Roland McGrath1b046242008-08-19 20:37:07 -07001462 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001465static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
1467 struct siginfo info;
1468 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001469 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 struct sighand_struct *sighand;
1471
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001472 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001473 parent = tsk->parent;
1474 else {
1475 tsk = tsk->group_leader;
1476 parent = tsk->real_parent;
1477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 info.si_signo = SIGCHLD;
1480 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001481 /*
1482 * see comment in do_notify_parent() abot the following 3 lines
1483 */
1484 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001485 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001486 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001487 rcu_read_unlock();
1488
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001489 info.si_utime = cputime_to_clock_t(tsk->utime);
1490 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 info.si_code = why;
1493 switch (why) {
1494 case CLD_CONTINUED:
1495 info.si_status = SIGCONT;
1496 break;
1497 case CLD_STOPPED:
1498 info.si_status = tsk->signal->group_exit_code & 0x7f;
1499 break;
1500 case CLD_TRAPPED:
1501 info.si_status = tsk->exit_code & 0x7f;
1502 break;
1503 default:
1504 BUG();
1505 }
1506
1507 sighand = parent->sighand;
1508 spin_lock_irqsave(&sighand->siglock, flags);
1509 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1510 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1511 __group_send_sig_info(SIGCHLD, &info, parent);
1512 /*
1513 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1514 */
1515 __wake_up_parent(tsk, parent);
1516 spin_unlock_irqrestore(&sighand->siglock, flags);
1517}
1518
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001519static inline int may_ptrace_stop(void)
1520{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001521 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001522 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001523 /*
1524 * Are we in the middle of do_coredump?
1525 * If so and our tracer is also part of the coredump stopping
1526 * is a deadlock situation, and pointless because our tracer
1527 * is dead so don't allow us to stop.
1528 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001529 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001530 * is safe to enter schedule().
1531 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001532 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001533 unlikely(current->mm == current->parent->mm))
1534 return 0;
1535
1536 return 1;
1537}
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001540 * Return nonzero if there is a SIGKILL that should be waking us up.
1541 * Called with the siglock held.
1542 */
1543static int sigkill_pending(struct task_struct *tsk)
1544{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001545 return sigismember(&tsk->pending.signal, SIGKILL) ||
1546 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001547}
1548
1549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * This must be called with current->sighand->siglock held.
1551 *
1552 * This should be the path for all ptrace stops.
1553 * We always set current->last_siginfo while stopped here.
1554 * That makes it a way to test a stopped process for
1555 * being ptrace-stopped vs being job-control-stopped.
1556 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001557 * If we actually decide not to stop at all because the tracer
1558 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001560static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Roland McGrath1a669c22008-02-06 01:37:37 -08001562 if (arch_ptrace_stop_needed(exit_code, info)) {
1563 /*
1564 * The arch code has something special to do before a
1565 * ptrace stop. This is allowed to block, e.g. for faults
1566 * on user stack pages. We can't keep the siglock while
1567 * calling arch_ptrace_stop, so we must release it now.
1568 * To preserve proper semantics, we must do this before
1569 * any signal bookkeeping like checking group_stop_count.
1570 * Meanwhile, a SIGKILL could come in before we retake the
1571 * siglock. That must prevent us from sleeping in TASK_TRACED.
1572 * So after regaining the lock, we must check for SIGKILL.
1573 */
1574 spin_unlock_irq(&current->sighand->siglock);
1575 arch_ptrace_stop(exit_code, info);
1576 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001577 if (sigkill_pending(current))
1578 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001579 }
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /*
1582 * If there is a group stop in progress,
1583 * we must participate in the bookkeeping.
1584 */
1585 if (current->signal->group_stop_count > 0)
1586 --current->signal->group_stop_count;
1587
1588 current->last_siginfo = info;
1589 current->exit_code = exit_code;
1590
1591 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001592 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 spin_unlock_irq(&current->sighand->siglock);
1594 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001595 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001596 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001597 /*
1598 * Don't want to allow preemption here, because
1599 * sys_ptrace() needs this task to be inactive.
1600 *
1601 * XXX: implement read_unlock_no_resched().
1602 */
1603 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001605 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 schedule();
1607 } else {
1608 /*
1609 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001610 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001612 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001613 if (clear_code)
1614 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001615 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617
1618 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001619 * While in TASK_TRACED, we were considered "frozen enough".
1620 * Now that we woke up, it's crucial if we're supposed to be
1621 * frozen that we freeze now before running anything substantial.
1622 */
1623 try_to_freeze();
1624
1625 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * We are back. Now reacquire the siglock before touching
1627 * last_siginfo, so that we are sure to have synchronized with
1628 * any signal-sending on another CPU that wants to examine it.
1629 */
1630 spin_lock_irq(&current->sighand->siglock);
1631 current->last_siginfo = NULL;
1632
1633 /*
1634 * Queued signals ignored us while we were stopped for tracing.
1635 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001636 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001638 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641void ptrace_notify(int exit_code)
1642{
1643 siginfo_t info;
1644
1645 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1646
1647 memset(&info, 0, sizeof info);
1648 info.si_signo = SIGTRAP;
1649 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001650 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001651 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /* Let the debugger run. */
1654 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001655 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 spin_unlock_irq(&current->sighand->siglock);
1657}
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659/*
1660 * This performs the stopping for SIGSTOP and other stop signals.
1661 * We have to stop all threads in the thread group.
1662 * Returns nonzero if we've actually stopped and released the siglock.
1663 * Returns zero if we didn't stop and still hold the siglock.
1664 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001665static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 struct signal_struct *sig = current->signal;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001668 int notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001670 if (!sig->group_stop_count) {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001671 struct task_struct *t;
1672
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001673 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001674 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001678 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001680 sig->group_exit_code = signr;
1681
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001682 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001683 for (t = next_thread(current); t != current; t = next_thread(t))
1684 /*
1685 * Setting state to TASK_STOPPED for a group
1686 * stop is always done with the siglock held,
1687 * so this check has no races.
1688 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001689 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001690 !task_is_stopped_or_traced(t)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001691 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001692 signal_wake_up(t, 0);
1693 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001694 }
1695 /*
1696 * If there are no other threads in the group, or if there is
1697 * a group stop in progress and we are the last to stop, report
1698 * to the parent. When ptraced, every thread reports itself.
1699 */
1700 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1701 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1702 /*
1703 * tracehook_notify_jctl() can drop and reacquire siglock, so
1704 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1705 * or SIGKILL comes in between ->group_stop_count == 0.
1706 */
1707 if (sig->group_stop_count) {
1708 if (!--sig->group_stop_count)
1709 sig->flags = SIGNAL_STOP_STOPPED;
1710 current->exit_code = sig->group_exit_code;
1711 __set_current_state(TASK_STOPPED);
1712 }
1713 spin_unlock_irq(&current->sighand->siglock);
1714
1715 if (notify) {
1716 read_lock(&tasklist_lock);
1717 do_notify_parent_cldstop(current, notify);
1718 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001721 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1722 do {
1723 schedule();
1724 } while (try_to_freeze());
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001725
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001726 tracehook_finish_jctl();
1727 current->exit_code = 0;
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return 1;
1730}
1731
Roland McGrath18c98b62008-04-17 18:44:38 -07001732static int ptrace_signal(int signr, siginfo_t *info,
1733 struct pt_regs *regs, void *cookie)
1734{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001735 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001736 return signr;
1737
1738 ptrace_signal_deliver(regs, cookie);
1739
1740 /* Let the debugger run. */
1741 ptrace_stop(signr, 0, info);
1742
1743 /* We're back. Did the debugger cancel the sig? */
1744 signr = current->exit_code;
1745 if (signr == 0)
1746 return signr;
1747
1748 current->exit_code = 0;
1749
1750 /* Update the siginfo structure if the signal has
1751 changed. If the debugger wanted something
1752 specific in the siginfo structure then it should
1753 have updated *info via PTRACE_SETSIGINFO. */
1754 if (signr != info->si_signo) {
1755 info->si_signo = signr;
1756 info->si_errno = 0;
1757 info->si_code = SI_USER;
1758 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001759 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001760 }
1761
1762 /* If the (new) signal is now blocked, requeue it. */
1763 if (sigismember(&current->blocked, signr)) {
1764 specific_send_sig_info(signr, info, current);
1765 signr = 0;
1766 }
1767
1768 return signr;
1769}
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1772 struct pt_regs *regs, void *cookie)
1773{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001774 struct sighand_struct *sighand = current->sighand;
1775 struct signal_struct *signal = current->signal;
1776 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001778relock:
1779 /*
1780 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1781 * While in TASK_STOPPED, we were considered "frozen enough".
1782 * Now that we woke up, it's crucial if we're supposed to be
1783 * frozen that we freeze now before running anything substantial.
1784 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001785 try_to_freeze();
1786
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001787 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001788 /*
1789 * Every stopped thread goes here after wakeup. Check to see if
1790 * we should notify the parent, prepare_signal(SIGCONT) encodes
1791 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1792 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001793 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1794 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001795 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001796 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001797
1798 why = tracehook_notify_jctl(why, CLD_CONTINUED);
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001799 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001800
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001801 if (why) {
1802 read_lock(&tasklist_lock);
1803 do_notify_parent_cldstop(current->group_leader, why);
1804 read_unlock(&tasklist_lock);
1805 }
Oleg Nesterove4420552008-04-30 00:52:44 -07001806 goto relock;
1807 }
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 for (;;) {
1810 struct k_sigaction *ka;
1811
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001812 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001813 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto relock;
1815
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001816 /*
1817 * Tracing can induce an artifical signal and choose sigaction.
1818 * The return value in @signr determines the default action,
1819 * but @info->si_signo is the signal number we will report.
1820 */
1821 signr = tracehook_get_signal(current, regs, info, return_ka);
1822 if (unlikely(signr < 0))
1823 goto relock;
1824 if (unlikely(signr != 0))
1825 ka = return_ka;
1826 else {
1827 signr = dequeue_signal(current, &current->blocked,
1828 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Roland McGrath18c98b62008-04-17 18:44:38 -07001830 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001831 break; /* will return 0 */
1832
1833 if (signr != SIGKILL) {
1834 signr = ptrace_signal(signr, info,
1835 regs, cookie);
1836 if (!signr)
1837 continue;
1838 }
1839
1840 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 }
1842
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05001843 /* Trace actually delivered signals. */
1844 trace_signal_deliver(signr, info, ka);
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1847 continue;
1848 if (ka->sa.sa_handler != SIG_DFL) {
1849 /* Run the handler. */
1850 *return_ka = *ka;
1851
1852 if (ka->sa.sa_flags & SA_ONESHOT)
1853 ka->sa.sa_handler = SIG_DFL;
1854
1855 break; /* will return non-zero "signr" value */
1856 }
1857
1858 /*
1859 * Now we are doing the default action for this signal.
1860 */
1861 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1862 continue;
1863
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001864 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001865 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001866 * Container-init gets no signals it doesn't want from same
1867 * container.
1868 *
1869 * Note that if global/container-init sees a sig_kernel_only()
1870 * signal here, the signal must have been generated internally
1871 * or must have come from an ancestor namespace. In either
1872 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001873 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001874 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001875 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 continue;
1877
1878 if (sig_kernel_stop(signr)) {
1879 /*
1880 * The default action is to stop all threads in
1881 * the thread group. The job control signals
1882 * do nothing in an orphaned pgrp, but SIGSTOP
1883 * always works. Note that siglock needs to be
1884 * dropped during the call to is_orphaned_pgrp()
1885 * because of lock ordering with tasklist_lock.
1886 * This allows an intervening SIGCONT to be posted.
1887 * We need to check for that and bail out if necessary.
1888 */
1889 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001890 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* signals can be posted during this window */
1893
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001894 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto relock;
1896
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001897 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001900 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* It released the siglock. */
1902 goto relock;
1903 }
1904
1905 /*
1906 * We didn't actually stop, due to a race
1907 * with SIGCONT or something like that.
1908 */
1909 continue;
1910 }
1911
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001912 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /*
1915 * Anything else is fatal, maybe with a core dump.
1916 */
1917 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001920 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001921 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /*
1923 * If it was able to dump core, this kills all
1924 * other threads in the group and synchronizes with
1925 * their demise. If we lost the race with another
1926 * thread getting here, it set group_exit_code
1927 * first and our do_group_exit call below will use
1928 * that value and ignore the one we pass it.
1929 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001930 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 }
1932
1933 /*
1934 * Death signals, no core dump.
1935 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001936 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* NOTREACHED */
1938 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001939 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return signr;
1941}
1942
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001943void exit_signals(struct task_struct *tsk)
1944{
1945 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001946 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001947
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001948 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1949 tsk->flags |= PF_EXITING;
1950 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001951 }
1952
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001953 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001954 /*
1955 * From now this task is not visible for group-wide signals,
1956 * see wants_signal(), do_signal_stop().
1957 */
1958 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001959 if (!signal_pending(tsk))
1960 goto out;
1961
1962 /* It could be that __group_complete_signal() choose us to
1963 * notify about group-wide signal. Another thread should be
1964 * woken now to take the signal since we will not.
1965 */
1966 for (t = tsk; (t = next_thread(t)) != tsk; )
1967 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1968 recalc_sigpending_and_wake(t);
1969
1970 if (unlikely(tsk->signal->group_stop_count) &&
1971 !--tsk->signal->group_stop_count) {
1972 tsk->signal->flags = SIGNAL_STOP_STOPPED;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001973 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001974 }
1975out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001976 spin_unlock_irq(&tsk->sighand->siglock);
1977
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001978 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001979 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001980 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001981 read_unlock(&tasklist_lock);
1982 }
1983}
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985EXPORT_SYMBOL(recalc_sigpending);
1986EXPORT_SYMBOL_GPL(dequeue_signal);
1987EXPORT_SYMBOL(flush_signals);
1988EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989EXPORT_SYMBOL(send_sig);
1990EXPORT_SYMBOL(send_sig_info);
1991EXPORT_SYMBOL(sigprocmask);
1992EXPORT_SYMBOL(block_all_signals);
1993EXPORT_SYMBOL(unblock_all_signals);
1994
1995
1996/*
1997 * System call entry points.
1998 */
1999
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002000SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct restart_block *restart = &current_thread_info()->restart_block;
2003 return restart->fn(restart);
2004}
2005
2006long do_no_restart_syscall(struct restart_block *param)
2007{
2008 return -EINTR;
2009}
2010
2011/*
2012 * We don't need to get the kernel lock - this is all local to this
2013 * particular thread.. (and that's good, because this is _heavily_
2014 * used by various programs)
2015 */
2016
2017/*
2018 * This is also useful for kernel threads that want to temporarily
2019 * (or permanently) block certain signals.
2020 *
2021 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2022 * interface happily blocks "unblockable" signals like SIGKILL
2023 * and friends.
2024 */
2025int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2026{
2027 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002030 if (oldset)
2031 *oldset = current->blocked;
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 error = 0;
2034 switch (how) {
2035 case SIG_BLOCK:
2036 sigorsets(&current->blocked, &current->blocked, set);
2037 break;
2038 case SIG_UNBLOCK:
2039 signandsets(&current->blocked, &current->blocked, set);
2040 break;
2041 case SIG_SETMASK:
2042 current->blocked = *set;
2043 break;
2044 default:
2045 error = -EINVAL;
2046 }
2047 recalc_sigpending();
2048 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 return error;
2051}
2052
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002053SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2054 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
2056 int error = -EINVAL;
2057 sigset_t old_set, new_set;
2058
2059 /* XXX: Don't preclude handling different sized sigset_t's. */
2060 if (sigsetsize != sizeof(sigset_t))
2061 goto out;
2062
2063 if (set) {
2064 error = -EFAULT;
2065 if (copy_from_user(&new_set, set, sizeof(*set)))
2066 goto out;
2067 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2068
2069 error = sigprocmask(how, &new_set, &old_set);
2070 if (error)
2071 goto out;
2072 if (oset)
2073 goto set_old;
2074 } else if (oset) {
2075 spin_lock_irq(&current->sighand->siglock);
2076 old_set = current->blocked;
2077 spin_unlock_irq(&current->sighand->siglock);
2078
2079 set_old:
2080 error = -EFAULT;
2081 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2082 goto out;
2083 }
2084 error = 0;
2085out:
2086 return error;
2087}
2088
2089long do_sigpending(void __user *set, unsigned long sigsetsize)
2090{
2091 long error = -EINVAL;
2092 sigset_t pending;
2093
2094 if (sigsetsize > sizeof(sigset_t))
2095 goto out;
2096
2097 spin_lock_irq(&current->sighand->siglock);
2098 sigorsets(&pending, &current->pending.signal,
2099 &current->signal->shared_pending.signal);
2100 spin_unlock_irq(&current->sighand->siglock);
2101
2102 /* Outside the lock because only this thread touches it. */
2103 sigandsets(&pending, &current->blocked, &pending);
2104
2105 error = -EFAULT;
2106 if (!copy_to_user(set, &pending, sigsetsize))
2107 error = 0;
2108
2109out:
2110 return error;
2111}
2112
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002113SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 return do_sigpending(set, sigsetsize);
2116}
2117
2118#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2119
2120int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2121{
2122 int err;
2123
2124 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2125 return -EFAULT;
2126 if (from->si_code < 0)
2127 return __copy_to_user(to, from, sizeof(siginfo_t))
2128 ? -EFAULT : 0;
2129 /*
2130 * If you change siginfo_t structure, please be sure
2131 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002132 * Please remember to update the signalfd_copyinfo() function
2133 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 * It should never copy any pad contained in the structure
2135 * to avoid security leaks, but must copy the generic
2136 * 3 ints plus the relevant union member.
2137 */
2138 err = __put_user(from->si_signo, &to->si_signo);
2139 err |= __put_user(from->si_errno, &to->si_errno);
2140 err |= __put_user((short)from->si_code, &to->si_code);
2141 switch (from->si_code & __SI_MASK) {
2142 case __SI_KILL:
2143 err |= __put_user(from->si_pid, &to->si_pid);
2144 err |= __put_user(from->si_uid, &to->si_uid);
2145 break;
2146 case __SI_TIMER:
2147 err |= __put_user(from->si_tid, &to->si_tid);
2148 err |= __put_user(from->si_overrun, &to->si_overrun);
2149 err |= __put_user(from->si_ptr, &to->si_ptr);
2150 break;
2151 case __SI_POLL:
2152 err |= __put_user(from->si_band, &to->si_band);
2153 err |= __put_user(from->si_fd, &to->si_fd);
2154 break;
2155 case __SI_FAULT:
2156 err |= __put_user(from->si_addr, &to->si_addr);
2157#ifdef __ARCH_SI_TRAPNO
2158 err |= __put_user(from->si_trapno, &to->si_trapno);
2159#endif
2160 break;
2161 case __SI_CHLD:
2162 err |= __put_user(from->si_pid, &to->si_pid);
2163 err |= __put_user(from->si_uid, &to->si_uid);
2164 err |= __put_user(from->si_status, &to->si_status);
2165 err |= __put_user(from->si_utime, &to->si_utime);
2166 err |= __put_user(from->si_stime, &to->si_stime);
2167 break;
2168 case __SI_RT: /* This is not generated by the kernel as of now. */
2169 case __SI_MESGQ: /* But this is */
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_ptr, &to->si_ptr);
2173 break;
2174 default: /* this is just in case for now ... */
2175 err |= __put_user(from->si_pid, &to->si_pid);
2176 err |= __put_user(from->si_uid, &to->si_uid);
2177 break;
2178 }
2179 return err;
2180}
2181
2182#endif
2183
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002184SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2185 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2186 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int ret, sig;
2189 sigset_t these;
2190 struct timespec ts;
2191 siginfo_t info;
2192 long timeout = 0;
2193
2194 /* XXX: Don't preclude handling different sized sigset_t's. */
2195 if (sigsetsize != sizeof(sigset_t))
2196 return -EINVAL;
2197
2198 if (copy_from_user(&these, uthese, sizeof(these)))
2199 return -EFAULT;
2200
2201 /*
2202 * Invert the set of allowed signals to get those we
2203 * want to block.
2204 */
2205 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2206 signotset(&these);
2207
2208 if (uts) {
2209 if (copy_from_user(&ts, uts, sizeof(ts)))
2210 return -EFAULT;
2211 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2212 || ts.tv_sec < 0)
2213 return -EINVAL;
2214 }
2215
2216 spin_lock_irq(&current->sighand->siglock);
2217 sig = dequeue_signal(current, &these, &info);
2218 if (!sig) {
2219 timeout = MAX_SCHEDULE_TIMEOUT;
2220 if (uts)
2221 timeout = (timespec_to_jiffies(&ts)
2222 + (ts.tv_sec || ts.tv_nsec));
2223
2224 if (timeout) {
2225 /* None ready -- temporarily unblock those we're
2226 * interested while we are sleeping in so that we'll
2227 * be awakened when they arrive. */
2228 current->real_blocked = current->blocked;
2229 sigandsets(&current->blocked, &current->blocked, &these);
2230 recalc_sigpending();
2231 spin_unlock_irq(&current->sighand->siglock);
2232
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002233 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 spin_lock_irq(&current->sighand->siglock);
2236 sig = dequeue_signal(current, &these, &info);
2237 current->blocked = current->real_blocked;
2238 siginitset(&current->real_blocked, 0);
2239 recalc_sigpending();
2240 }
2241 }
2242 spin_unlock_irq(&current->sighand->siglock);
2243
2244 if (sig) {
2245 ret = sig;
2246 if (uinfo) {
2247 if (copy_siginfo_to_user(uinfo, &info))
2248 ret = -EFAULT;
2249 }
2250 } else {
2251 ret = -EAGAIN;
2252 if (timeout)
2253 ret = -EINTR;
2254 }
2255
2256 return ret;
2257}
2258
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002259SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
2261 struct siginfo info;
2262
2263 info.si_signo = sig;
2264 info.si_errno = 0;
2265 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002266 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002267 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 return kill_something_info(sig, &info, pid);
2270}
2271
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002272static int
2273do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002274{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002275 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002276 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002277
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002278 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002279 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002280 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002281 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002282 /*
2283 * The null signal is a permissions and process existence
2284 * probe. No signal is actually delivered.
2285 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002286 if (!error && sig) {
2287 error = do_send_sig_info(sig, info, p, false);
2288 /*
2289 * If lock_task_sighand() failed we pretend the task
2290 * dies after receiving the signal. The window is tiny,
2291 * and the signal is private anyway.
2292 */
2293 if (unlikely(error == -ESRCH))
2294 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002295 }
2296 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002297 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002298
2299 return error;
2300}
2301
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00002302static int do_tkill(pid_t tgid, pid_t pid, int sig)
2303{
2304 struct siginfo info;
2305
2306 info.si_signo = sig;
2307 info.si_errno = 0;
2308 info.si_code = SI_TKILL;
2309 info.si_pid = task_tgid_vnr(current);
2310 info.si_uid = current_uid();
2311
2312 return do_send_specific(tgid, pid, sig, &info);
2313}
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315/**
2316 * sys_tgkill - send signal to one specific thread
2317 * @tgid: the thread group ID of the thread
2318 * @pid: the PID of the thread
2319 * @sig: signal to be sent
2320 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002321 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * exists but it's not belonging to the target process anymore. This
2323 * method solves the problem of threads exiting and PIDs getting reused.
2324 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002325SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /* This is only valid for single tasks */
2328 if (pid <= 0 || tgid <= 0)
2329 return -EINVAL;
2330
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002331 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
2334/*
2335 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2336 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002337SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 /* This is only valid for single tasks */
2340 if (pid <= 0)
2341 return -EINVAL;
2342
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002343 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002346SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2347 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 siginfo_t info;
2350
2351 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2352 return -EFAULT;
2353
2354 /* Not even root can pretend to send signals from the kernel.
2355 Nor can they impersonate a kill(), which adds source info. */
2356 if (info.si_code >= 0)
2357 return -EPERM;
2358 info.si_signo = sig;
2359
2360 /* POSIX.1b doesn't mention process groups. */
2361 return kill_proc_info(sig, &info, pid);
2362}
2363
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002364long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2365{
2366 /* This is only valid for single tasks */
2367 if (pid <= 0 || tgid <= 0)
2368 return -EINVAL;
2369
2370 /* Not even root can pretend to send signals from the kernel.
2371 Nor can they impersonate a kill(), which adds source info. */
2372 if (info->si_code >= 0)
2373 return -EPERM;
2374 info->si_signo = sig;
2375
2376 return do_send_specific(tgid, pid, sig, info);
2377}
2378
2379SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2380 siginfo_t __user *, uinfo)
2381{
2382 siginfo_t info;
2383
2384 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2385 return -EFAULT;
2386
2387 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2388}
2389
Oleg Nesterov88531f72006-03-28 16:11:24 -08002390int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002392 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002394 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002396 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return -EINVAL;
2398
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002399 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (oact)
2403 *oact = *k;
2404
2405 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002406 sigdelsetmask(&act->sa.sa_mask,
2407 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002408 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 /*
2410 * POSIX 3.3.1.3:
2411 * "Setting a signal action to SIG_IGN for a signal that is
2412 * pending shall cause the pending signal to be discarded,
2413 * whether or not it is blocked."
2414 *
2415 * "Setting a signal action to SIG_DFL for a signal that is
2416 * pending and whose default action is to ignore the signal
2417 * (for example, SIGCHLD), shall cause the pending signal to
2418 * be discarded, whether or not it is blocked"
2419 */
Roland McGrath35de2542008-07-25 19:45:51 -07002420 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002421 sigemptyset(&mask);
2422 sigaddset(&mask, sig);
2423 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002425 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 t = next_thread(t);
2427 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
2431 spin_unlock_irq(&current->sighand->siglock);
2432 return 0;
2433}
2434
2435int
2436do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2437{
2438 stack_t oss;
2439 int error;
2440
Linus Torvalds0083fc22009-08-01 10:34:56 -07002441 oss.ss_sp = (void __user *) current->sas_ss_sp;
2442 oss.ss_size = current->sas_ss_size;
2443 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 if (uss) {
2446 void __user *ss_sp;
2447 size_t ss_size;
2448 int ss_flags;
2449
2450 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002451 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2452 goto out;
2453 error = __get_user(ss_sp, &uss->ss_sp) |
2454 __get_user(ss_flags, &uss->ss_flags) |
2455 __get_user(ss_size, &uss->ss_size);
2456 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 goto out;
2458
2459 error = -EPERM;
2460 if (on_sig_stack(sp))
2461 goto out;
2462
2463 error = -EINVAL;
2464 /*
2465 *
2466 * Note - this code used to test ss_flags incorrectly
2467 * old code may have been written using ss_flags==0
2468 * to mean ss_flags==SS_ONSTACK (as this was the only
2469 * way that worked) - this fix preserves that older
2470 * mechanism
2471 */
2472 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2473 goto out;
2474
2475 if (ss_flags == SS_DISABLE) {
2476 ss_size = 0;
2477 ss_sp = NULL;
2478 } else {
2479 error = -ENOMEM;
2480 if (ss_size < MINSIGSTKSZ)
2481 goto out;
2482 }
2483
2484 current->sas_ss_sp = (unsigned long) ss_sp;
2485 current->sas_ss_size = ss_size;
2486 }
2487
Linus Torvalds0083fc22009-08-01 10:34:56 -07002488 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (uoss) {
2490 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002491 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002493 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2494 __put_user(oss.ss_size, &uoss->ss_size) |
2495 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
2497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498out:
2499 return error;
2500}
2501
2502#ifdef __ARCH_WANT_SYS_SIGPENDING
2503
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002504SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 return do_sigpending(set, sizeof(*set));
2507}
2508
2509#endif
2510
2511#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2512/* Some platforms have their own version with special arguments others
2513 support only sys_rt_sigprocmask. */
2514
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002515SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2516 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 int error;
2519 old_sigset_t old_set, new_set;
2520
2521 if (set) {
2522 error = -EFAULT;
2523 if (copy_from_user(&new_set, set, sizeof(*set)))
2524 goto out;
2525 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2526
2527 spin_lock_irq(&current->sighand->siglock);
2528 old_set = current->blocked.sig[0];
2529
2530 error = 0;
2531 switch (how) {
2532 default:
2533 error = -EINVAL;
2534 break;
2535 case SIG_BLOCK:
2536 sigaddsetmask(&current->blocked, new_set);
2537 break;
2538 case SIG_UNBLOCK:
2539 sigdelsetmask(&current->blocked, new_set);
2540 break;
2541 case SIG_SETMASK:
2542 current->blocked.sig[0] = new_set;
2543 break;
2544 }
2545
2546 recalc_sigpending();
2547 spin_unlock_irq(&current->sighand->siglock);
2548 if (error)
2549 goto out;
2550 if (oset)
2551 goto set_old;
2552 } else if (oset) {
2553 old_set = current->blocked.sig[0];
2554 set_old:
2555 error = -EFAULT;
2556 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2557 goto out;
2558 }
2559 error = 0;
2560out:
2561 return error;
2562}
2563#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2564
2565#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002566SYSCALL_DEFINE4(rt_sigaction, int, sig,
2567 const struct sigaction __user *, act,
2568 struct sigaction __user *, oact,
2569 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 struct k_sigaction new_sa, old_sa;
2572 int ret = -EINVAL;
2573
2574 /* XXX: Don't preclude handling different sized sigset_t's. */
2575 if (sigsetsize != sizeof(sigset_t))
2576 goto out;
2577
2578 if (act) {
2579 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2580 return -EFAULT;
2581 }
2582
2583 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2584
2585 if (!ret && oact) {
2586 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2587 return -EFAULT;
2588 }
2589out:
2590 return ret;
2591}
2592#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2593
2594#ifdef __ARCH_WANT_SYS_SGETMASK
2595
2596/*
2597 * For backwards compatibility. Functionality superseded by sigprocmask.
2598 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002599SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
2601 /* SMP safe */
2602 return current->blocked.sig[0];
2603}
2604
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002605SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 int old;
2608
2609 spin_lock_irq(&current->sighand->siglock);
2610 old = current->blocked.sig[0];
2611
2612 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2613 sigmask(SIGSTOP)));
2614 recalc_sigpending();
2615 spin_unlock_irq(&current->sighand->siglock);
2616
2617 return old;
2618}
2619#endif /* __ARCH_WANT_SGETMASK */
2620
2621#ifdef __ARCH_WANT_SYS_SIGNAL
2622/*
2623 * For backwards compatibility. Functionality superseded by sigaction.
2624 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002625SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 struct k_sigaction new_sa, old_sa;
2628 int ret;
2629
2630 new_sa.sa.sa_handler = handler;
2631 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03002632 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 ret = do_sigaction(sig, &new_sa, &old_sa);
2635
2636 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2637}
2638#endif /* __ARCH_WANT_SYS_SIGNAL */
2639
2640#ifdef __ARCH_WANT_SYS_PAUSE
2641
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002642SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643{
2644 current->state = TASK_INTERRUPTIBLE;
2645 schedule();
2646 return -ERESTARTNOHAND;
2647}
2648
2649#endif
2650
David Woodhouse150256d2006-01-18 17:43:57 -08002651#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002652SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002653{
2654 sigset_t newset;
2655
2656 /* XXX: Don't preclude handling different sized sigset_t's. */
2657 if (sigsetsize != sizeof(sigset_t))
2658 return -EINVAL;
2659
2660 if (copy_from_user(&newset, unewset, sizeof(newset)))
2661 return -EFAULT;
2662 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2663
2664 spin_lock_irq(&current->sighand->siglock);
2665 current->saved_sigmask = current->blocked;
2666 current->blocked = newset;
2667 recalc_sigpending();
2668 spin_unlock_irq(&current->sighand->siglock);
2669
2670 current->state = TASK_INTERRUPTIBLE;
2671 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002672 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002673 return -ERESTARTNOHAND;
2674}
2675#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2676
David Howellsf269fdd2006-09-27 01:50:23 -07002677__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2678{
2679 return NULL;
2680}
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682void __init signals_init(void)
2683{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002684 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}