blob: 93e72e5feae63bc0e9b6bb9e6da3ba3d57b8fcb4 [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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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 Nesterovad16a462008-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)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500900 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
901 /*
902 * Queue overflow, abort. We may abort if the
903 * signal was rt and sent by user using something
904 * other than kill().
905 */
906 trace_signal_overflow_fail(sig, group, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -EAGAIN;
Masami Hiramatsuba005e12009-11-24 16:56:58 -0500908 } else {
909 /*
910 * This is a silent loss of information. We still
911 * send the signal, but the *info bits are lost.
912 */
913 trace_signal_lose_info(sig, group, info);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
917out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700918 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700919 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700920 complete_signal(sig, t, group);
921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700924static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
925 int group)
926{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700927 int from_ancestor_ns = 0;
928
929#ifdef CONFIG_PID_NS
930 if (!is_si_special(info) && SI_FROMUSER(info) &&
931 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
932 from_ancestor_ns = 1;
933#endif
934
935 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700936}
937
Ingo Molnar45807a12007-07-15 23:40:10 -0700938int print_fatal_signals;
939
940static void print_fatal_signal(struct pt_regs *regs, int signr)
941{
942 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700943 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700944
Al Viroca5cd872007-10-29 04:31:16 +0000945#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100946 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700947 {
948 int i;
949 for (i = 0; i < 16; i++) {
950 unsigned char insn;
951
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100952 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700953 printk("%02x ", insn);
954 }
955 }
956#endif
957 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800958 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700959 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800960 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700961}
962
963static int __init setup_print_fatal_signals(char *str)
964{
965 get_option (&str, &print_fatal_signals);
966
967 return 1;
968}
969
970__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700972int
973__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
974{
975 return send_signal(sig, info, p, 1);
976}
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978static int
979specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
980{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700981 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700984int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
985 bool group)
986{
987 unsigned long flags;
988 int ret = -ESRCH;
989
990 if (lock_task_sighand(p, &flags)) {
991 ret = send_signal(sig, info, p, group);
992 unlock_task_sighand(p, &flags);
993 }
994
995 return ret;
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * Force a signal that the process can't ignore: if necessary
1000 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001001 *
1002 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1003 * since we do not want to have a signal handler that was blocked
1004 * be invoked when user space had explicitly blocked it.
1005 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001006 * We don't want to have recursive SIGSEGV's etc, for example,
1007 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009int
1010force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1011{
1012 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001013 int ret, blocked, ignored;
1014 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001017 action = &t->sighand->action[sig-1];
1018 ignored = action->sa.sa_handler == SIG_IGN;
1019 blocked = sigismember(&t->blocked, sig);
1020 if (blocked || ignored) {
1021 action->sa.sa_handler = SIG_DFL;
1022 if (blocked) {
1023 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001024 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001027 if (action->sa.sa_handler == SIG_DFL)
1028 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 ret = specific_send_sig_info(sig, info, t);
1030 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1031
1032 return ret;
1033}
1034
1035void
1036force_sig_specific(int sig, struct task_struct *t)
1037{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001038 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
1042 * Nuke all other threads in the group.
1043 */
1044void zap_other_threads(struct task_struct *p)
1045{
1046 struct task_struct *t;
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 p->signal->group_stop_count = 0;
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 for (t = next_thread(p); t != p; t = next_thread(t)) {
1051 /*
1052 * Don't bother with already dead threads
1053 */
1054 if (t->exit_state)
1055 continue;
1056
Andrea Arcangeli30e0fca2005-10-30 15:02:38 -08001057 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 signal_wake_up(t, 1);
1060 }
1061}
1062
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001063struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1064{
1065 struct sighand_struct *sighand;
1066
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001067 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001068 for (;;) {
1069 sighand = rcu_dereference(tsk->sighand);
1070 if (unlikely(sighand == NULL))
1071 break;
1072
1073 spin_lock_irqsave(&sighand->siglock, *flags);
1074 if (likely(sighand == tsk->sighand))
1075 break;
1076 spin_unlock_irqrestore(&sighand->siglock, *flags);
1077 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001078 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001079
1080 return sighand;
1081}
1082
David Howellsc69e8d92008-11-14 10:39:19 +11001083/*
1084 * send signal info to all the members of a group
1085 * - the caller must hold the RCU read lock at least
1086 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1088{
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001089 int ret = check_kill_permission(sig, info, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001091 if (!ret && sig)
1092 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 return ret;
1095}
1096
1097/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001098 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001100 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001102int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
1104 struct task_struct *p = NULL;
1105 int retval, success;
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 success = 0;
1108 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001109 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int err = group_send_sig_info(sig, info, p);
1111 success |= !err;
1112 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001113 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return success ? 0 : retval;
1115}
1116
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001117int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001119 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 struct task_struct *p;
1121
Ingo Molnare56d0902006-01-08 01:01:37 -08001122 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001123retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001124 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001125 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001127 if (unlikely(error == -ESRCH))
1128 /*
1129 * The task was unhashed in between, try again.
1130 * If it is dead, pid_task() will return NULL,
1131 * if we race with de_thread() it will find the
1132 * new leader.
1133 */
1134 goto retry;
1135 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001136 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return error;
1139}
1140
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001141int
1142kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001143{
1144 int error;
1145 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001146 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001147 rcu_read_unlock();
1148 return error;
1149}
1150
Eric W. Biederman2425c082006-10-02 02:17:28 -07001151/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1152int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001153 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001154{
1155 int ret = -EINVAL;
1156 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001157 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001158
1159 if (!valid_signal(sig))
1160 return ret;
1161
1162 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001163 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001164 if (!p) {
1165 ret = -ESRCH;
1166 goto out_unlock;
1167 }
David Howellsc69e8d92008-11-14 10:39:19 +11001168 pcred = __task_cred(p);
1169 if ((info == SEND_SIG_NOINFO ||
1170 (!is_si_special(info) && SI_FROMUSER(info))) &&
1171 euid != pcred->suid && euid != pcred->uid &&
1172 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001173 ret = -EPERM;
1174 goto out_unlock;
1175 }
David Quigley8f95dc52006-06-30 01:55:47 -07001176 ret = security_task_kill(p, info, sig, secid);
1177 if (ret)
1178 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001179 if (sig && p->sighand) {
1180 unsigned long flags;
1181 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001182 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001183 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1184 }
1185out_unlock:
1186 read_unlock(&tasklist_lock);
1187 return ret;
1188}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001189EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191/*
1192 * kill_something_info() interprets pid in interesting ways just like kill(2).
1193 *
1194 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1195 * is probably wrong. Should make it like BSD or SYSV.
1196 */
1197
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001198static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001200 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001201
1202 if (pid > 0) {
1203 rcu_read_lock();
1204 ret = kill_pid_info(sig, info, find_vpid(pid));
1205 rcu_read_unlock();
1206 return ret;
1207 }
1208
1209 read_lock(&tasklist_lock);
1210 if (pid != -1) {
1211 ret = __kill_pgrp_info(sig, info,
1212 pid ? find_vpid(-pid) : task_pgrp(current));
1213 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int retval = 0, count = 0;
1215 struct task_struct * p;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001218 if (task_pid_vnr(p) > 1 &&
1219 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int err = group_send_sig_info(sig, info, p);
1221 ++count;
1222 if (err != -EPERM)
1223 retval = err;
1224 }
1225 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001226 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001228 read_unlock(&tasklist_lock);
1229
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001230 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233/*
1234 * These are for backward compatibility with the rest of the kernel source.
1235 */
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237int
1238send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /*
1241 * Make sure legacy kernel users don't send in bad values
1242 * (normal paths check this in check_kill_permission).
1243 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001244 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return -EINVAL;
1246
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001247 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001250#define __si_special(priv) \
1251 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253int
1254send_sig(int sig, struct task_struct *p, int priv)
1255{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001256 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259void
1260force_sig(int sig, struct task_struct *p)
1261{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001262 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/*
1266 * When things go south during signal handling, we
1267 * will force a SIGSEGV. And if the signal that caused
1268 * the problem was already a SIGSEGV, we'll want to
1269 * make sure we don't even try to deliver the signal..
1270 */
1271int
1272force_sigsegv(int sig, struct task_struct *p)
1273{
1274 if (sig == SIGSEGV) {
1275 unsigned long flags;
1276 spin_lock_irqsave(&p->sighand->siglock, flags);
1277 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1278 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1279 }
1280 force_sig(SIGSEGV, p);
1281 return 0;
1282}
1283
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001284int kill_pgrp(struct pid *pid, int sig, int priv)
1285{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001286 int ret;
1287
1288 read_lock(&tasklist_lock);
1289 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1290 read_unlock(&tasklist_lock);
1291
1292 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001293}
1294EXPORT_SYMBOL(kill_pgrp);
1295
1296int kill_pid(struct pid *pid, int sig, int priv)
1297{
1298 return kill_pid_info(sig, __si_special(priv), pid);
1299}
1300EXPORT_SYMBOL(kill_pid);
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302/*
1303 * These functions support sending signals using preallocated sigqueue
1304 * structures. This is needed "because realtime applications cannot
1305 * afford to lose notifications of asynchronous events, like timer
1306 * expirations or I/O completions". In the case of Posix Timers
1307 * we allocate the sigqueue structure from the timer_create. If this
1308 * allocation fails we are able to report the failure to the application
1309 * with an EAGAIN error.
1310 */
1311
1312struct sigqueue *sigqueue_alloc(void)
1313{
1314 struct sigqueue *q;
1315
1316 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1317 q->flags |= SIGQUEUE_PREALLOC;
1318 return(q);
1319}
1320
1321void sigqueue_free(struct sigqueue *q)
1322{
1323 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001324 spinlock_t *lock = &current->sighand->siglock;
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1327 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001328 * We must hold ->siglock while testing q->list
1329 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001330 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001332 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001333 q->flags &= ~SIGQUEUE_PREALLOC;
1334 /*
1335 * If it is queued it will be freed when dequeued,
1336 * like the "regular" sigqueue.
1337 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001338 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001339 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001340 spin_unlock_irqrestore(lock, flags);
1341
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001342 if (q)
1343 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001346int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001347{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001348 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001349 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001350 unsigned long flags;
1351 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001352
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001353 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001354
1355 ret = -1;
1356 if (!likely(lock_task_sighand(t, &flags)))
1357 goto ret;
1358
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001359 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001360 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001361 goto out;
1362
1363 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001364 if (unlikely(!list_empty(&q->list))) {
1365 /*
1366 * If an SI_TIMER entry is already queue just increment
1367 * the overrun count.
1368 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001369 BUG_ON(q->info.si_code != SI_TIMER);
1370 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001371 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001372 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001373 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001374
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001375 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001376 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001377 list_add_tail(&q->list, &pending->list);
1378 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001379 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001380out:
1381 unlock_task_sighand(t, &flags);
1382ret:
1383 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * Let a parent know about the death of a child.
1388 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001389 *
1390 * Returns -1 if our parent ignored us and so we've switched to
1391 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001393int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394{
1395 struct siginfo info;
1396 unsigned long flags;
1397 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001398 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 BUG_ON(sig == -1);
1401
1402 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001403 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001405 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1407
1408 info.si_signo = sig;
1409 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001410 /*
1411 * we are under tasklist_lock here so our parent is tied to
1412 * us and cannot exit and release its namespace.
1413 *
1414 * the only it can is to switch its nsproxy with sys_unshare,
1415 * bu uncharing pid namespaces is not allowed, so we'll always
1416 * see relevant namespace
1417 *
1418 * write_lock() currently calls preempt_disable() which is the
1419 * same as rcu_read_lock(), but according to Oleg, this is not
1420 * correct to rely on this
1421 */
1422 rcu_read_lock();
1423 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001424 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001425 rcu_read_unlock();
1426
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001427 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1428 tsk->signal->utime));
1429 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1430 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 info.si_status = tsk->exit_code & 0x7f;
1433 if (tsk->exit_code & 0x80)
1434 info.si_code = CLD_DUMPED;
1435 else if (tsk->exit_code & 0x7f)
1436 info.si_code = CLD_KILLED;
1437 else {
1438 info.si_code = CLD_EXITED;
1439 info.si_status = tsk->exit_code >> 8;
1440 }
1441
1442 psig = tsk->parent->sighand;
1443 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001444 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1446 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1447 /*
1448 * We are exiting and our parent doesn't care. POSIX.1
1449 * defines special semantics for setting SIGCHLD to SIG_IGN
1450 * or setting the SA_NOCLDWAIT flag: we should be reaped
1451 * automatically and not left for our parent's wait4 call.
1452 * Rather than having the parent do it as a magic kind of
1453 * signal handler, we just set this to tell do_exit that we
1454 * can be cleaned up without becoming a zombie. Note that
1455 * we still call __wake_up_parent in this case, because a
1456 * blocked sys_wait4 might now return -ECHILD.
1457 *
1458 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1459 * is implementation-defined: we do (if you don't want
1460 * it, just use SIG_IGN instead).
1461 */
Roland McGrath1b046242008-08-19 20:37:07 -07001462 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001464 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001466 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 __group_send_sig_info(sig, &info, tsk->parent);
1468 __wake_up_parent(tsk, tsk->parent);
1469 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001470
Roland McGrath1b046242008-08-19 20:37:07 -07001471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001474static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 struct siginfo info;
1477 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001478 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 struct sighand_struct *sighand;
1480
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001481 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001482 parent = tsk->parent;
1483 else {
1484 tsk = tsk->group_leader;
1485 parent = tsk->real_parent;
1486 }
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 info.si_signo = SIGCHLD;
1489 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001490 /*
1491 * see comment in do_notify_parent() abot the following 3 lines
1492 */
1493 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001494 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001495 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001496 rcu_read_unlock();
1497
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001498 info.si_utime = cputime_to_clock_t(tsk->utime);
1499 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 info.si_code = why;
1502 switch (why) {
1503 case CLD_CONTINUED:
1504 info.si_status = SIGCONT;
1505 break;
1506 case CLD_STOPPED:
1507 info.si_status = tsk->signal->group_exit_code & 0x7f;
1508 break;
1509 case CLD_TRAPPED:
1510 info.si_status = tsk->exit_code & 0x7f;
1511 break;
1512 default:
1513 BUG();
1514 }
1515
1516 sighand = parent->sighand;
1517 spin_lock_irqsave(&sighand->siglock, flags);
1518 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1519 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1520 __group_send_sig_info(SIGCHLD, &info, parent);
1521 /*
1522 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1523 */
1524 __wake_up_parent(tsk, parent);
1525 spin_unlock_irqrestore(&sighand->siglock, flags);
1526}
1527
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001528static inline int may_ptrace_stop(void)
1529{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001530 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001531 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001532 /*
1533 * Are we in the middle of do_coredump?
1534 * If so and our tracer is also part of the coredump stopping
1535 * is a deadlock situation, and pointless because our tracer
1536 * is dead so don't allow us to stop.
1537 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001538 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001539 * is safe to enter schedule().
1540 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001541 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001542 unlikely(current->mm == current->parent->mm))
1543 return 0;
1544
1545 return 1;
1546}
1547
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001549 * Return nonzero if there is a SIGKILL that should be waking us up.
1550 * Called with the siglock held.
1551 */
1552static int sigkill_pending(struct task_struct *tsk)
1553{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001554 return sigismember(&tsk->pending.signal, SIGKILL) ||
1555 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001556}
1557
1558/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 * This must be called with current->sighand->siglock held.
1560 *
1561 * This should be the path for all ptrace stops.
1562 * We always set current->last_siginfo while stopped here.
1563 * That makes it a way to test a stopped process for
1564 * being ptrace-stopped vs being job-control-stopped.
1565 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001566 * If we actually decide not to stop at all because the tracer
1567 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001569static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Roland McGrath1a669c22008-02-06 01:37:37 -08001571 if (arch_ptrace_stop_needed(exit_code, info)) {
1572 /*
1573 * The arch code has something special to do before a
1574 * ptrace stop. This is allowed to block, e.g. for faults
1575 * on user stack pages. We can't keep the siglock while
1576 * calling arch_ptrace_stop, so we must release it now.
1577 * To preserve proper semantics, we must do this before
1578 * any signal bookkeeping like checking group_stop_count.
1579 * Meanwhile, a SIGKILL could come in before we retake the
1580 * siglock. That must prevent us from sleeping in TASK_TRACED.
1581 * So after regaining the lock, we must check for SIGKILL.
1582 */
1583 spin_unlock_irq(&current->sighand->siglock);
1584 arch_ptrace_stop(exit_code, info);
1585 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001586 if (sigkill_pending(current))
1587 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001588 }
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 /*
1591 * If there is a group stop in progress,
1592 * we must participate in the bookkeeping.
1593 */
1594 if (current->signal->group_stop_count > 0)
1595 --current->signal->group_stop_count;
1596
1597 current->last_siginfo = info;
1598 current->exit_code = exit_code;
1599
1600 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001601 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 spin_unlock_irq(&current->sighand->siglock);
1603 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001604 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001605 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001606 /*
1607 * Don't want to allow preemption here, because
1608 * sys_ptrace() needs this task to be inactive.
1609 *
1610 * XXX: implement read_unlock_no_resched().
1611 */
1612 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001614 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 schedule();
1616 } else {
1617 /*
1618 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001619 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001621 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001622 if (clear_code)
1623 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001624 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
1627 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001628 * While in TASK_TRACED, we were considered "frozen enough".
1629 * Now that we woke up, it's crucial if we're supposed to be
1630 * frozen that we freeze now before running anything substantial.
1631 */
1632 try_to_freeze();
1633
1634 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * We are back. Now reacquire the siglock before touching
1636 * last_siginfo, so that we are sure to have synchronized with
1637 * any signal-sending on another CPU that wants to examine it.
1638 */
1639 spin_lock_irq(&current->sighand->siglock);
1640 current->last_siginfo = NULL;
1641
1642 /*
1643 * Queued signals ignored us while we were stopped for tracing.
1644 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001645 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001647 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650void ptrace_notify(int exit_code)
1651{
1652 siginfo_t info;
1653
1654 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1655
1656 memset(&info, 0, sizeof info);
1657 info.si_signo = SIGTRAP;
1658 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001659 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001660 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /* Let the debugger run. */
1663 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001664 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 spin_unlock_irq(&current->sighand->siglock);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668/*
1669 * This performs the stopping for SIGSTOP and other stop signals.
1670 * We have to stop all threads in the thread group.
1671 * Returns nonzero if we've actually stopped and released the siglock.
1672 * Returns zero if we didn't stop and still hold the siglock.
1673 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001674static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 struct signal_struct *sig = current->signal;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001677 int notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001679 if (!sig->group_stop_count) {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001680 struct task_struct *t;
1681
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001682 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001683 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001687 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001689 sig->group_exit_code = signr;
1690
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001691 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001692 for (t = next_thread(current); t != current; t = next_thread(t))
1693 /*
1694 * Setting state to TASK_STOPPED for a group
1695 * stop is always done with the siglock held,
1696 * so this check has no races.
1697 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001698 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001699 !task_is_stopped_or_traced(t)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001700 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001701 signal_wake_up(t, 0);
1702 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001703 }
1704 /*
1705 * If there are no other threads in the group, or if there is
1706 * a group stop in progress and we are the last to stop, report
1707 * to the parent. When ptraced, every thread reports itself.
1708 */
1709 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1710 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1711 /*
1712 * tracehook_notify_jctl() can drop and reacquire siglock, so
1713 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1714 * or SIGKILL comes in between ->group_stop_count == 0.
1715 */
1716 if (sig->group_stop_count) {
1717 if (!--sig->group_stop_count)
1718 sig->flags = SIGNAL_STOP_STOPPED;
1719 current->exit_code = sig->group_exit_code;
1720 __set_current_state(TASK_STOPPED);
1721 }
1722 spin_unlock_irq(&current->sighand->siglock);
1723
1724 if (notify) {
1725 read_lock(&tasklist_lock);
1726 do_notify_parent_cldstop(current, notify);
1727 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001730 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1731 do {
1732 schedule();
1733 } while (try_to_freeze());
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001734
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001735 tracehook_finish_jctl();
1736 current->exit_code = 0;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 return 1;
1739}
1740
Roland McGrath18c98b62008-04-17 18:44:38 -07001741static int ptrace_signal(int signr, siginfo_t *info,
1742 struct pt_regs *regs, void *cookie)
1743{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001744 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001745 return signr;
1746
1747 ptrace_signal_deliver(regs, cookie);
1748
1749 /* Let the debugger run. */
1750 ptrace_stop(signr, 0, info);
1751
1752 /* We're back. Did the debugger cancel the sig? */
1753 signr = current->exit_code;
1754 if (signr == 0)
1755 return signr;
1756
1757 current->exit_code = 0;
1758
1759 /* Update the siginfo structure if the signal has
1760 changed. If the debugger wanted something
1761 specific in the siginfo structure then it should
1762 have updated *info via PTRACE_SETSIGINFO. */
1763 if (signr != info->si_signo) {
1764 info->si_signo = signr;
1765 info->si_errno = 0;
1766 info->si_code = SI_USER;
1767 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001768 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001769 }
1770
1771 /* If the (new) signal is now blocked, requeue it. */
1772 if (sigismember(&current->blocked, signr)) {
1773 specific_send_sig_info(signr, info, current);
1774 signr = 0;
1775 }
1776
1777 return signr;
1778}
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1781 struct pt_regs *regs, void *cookie)
1782{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001783 struct sighand_struct *sighand = current->sighand;
1784 struct signal_struct *signal = current->signal;
1785 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001787relock:
1788 /*
1789 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1790 * While in TASK_STOPPED, we were considered "frozen enough".
1791 * Now that we woke up, it's crucial if we're supposed to be
1792 * frozen that we freeze now before running anything substantial.
1793 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001794 try_to_freeze();
1795
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001796 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001797 /*
1798 * Every stopped thread goes here after wakeup. Check to see if
1799 * we should notify the parent, prepare_signal(SIGCONT) encodes
1800 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1801 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001802 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1803 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001804 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001805 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001806
1807 why = tracehook_notify_jctl(why, CLD_CONTINUED);
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001808 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001809
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001810 if (why) {
1811 read_lock(&tasklist_lock);
1812 do_notify_parent_cldstop(current->group_leader, why);
1813 read_unlock(&tasklist_lock);
1814 }
Oleg Nesterove4420552008-04-30 00:52:44 -07001815 goto relock;
1816 }
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 for (;;) {
1819 struct k_sigaction *ka;
1820
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001821 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001822 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 goto relock;
1824
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001825 /*
1826 * Tracing can induce an artifical signal and choose sigaction.
1827 * The return value in @signr determines the default action,
1828 * but @info->si_signo is the signal number we will report.
1829 */
1830 signr = tracehook_get_signal(current, regs, info, return_ka);
1831 if (unlikely(signr < 0))
1832 goto relock;
1833 if (unlikely(signr != 0))
1834 ka = return_ka;
1835 else {
1836 signr = dequeue_signal(current, &current->blocked,
1837 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Roland McGrath18c98b62008-04-17 18:44:38 -07001839 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001840 break; /* will return 0 */
1841
1842 if (signr != SIGKILL) {
1843 signr = ptrace_signal(signr, info,
1844 regs, cookie);
1845 if (!signr)
1846 continue;
1847 }
1848
1849 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05001852 /* Trace actually delivered signals. */
1853 trace_signal_deliver(signr, info, ka);
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1856 continue;
1857 if (ka->sa.sa_handler != SIG_DFL) {
1858 /* Run the handler. */
1859 *return_ka = *ka;
1860
1861 if (ka->sa.sa_flags & SA_ONESHOT)
1862 ka->sa.sa_handler = SIG_DFL;
1863
1864 break; /* will return non-zero "signr" value */
1865 }
1866
1867 /*
1868 * Now we are doing the default action for this signal.
1869 */
1870 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1871 continue;
1872
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001873 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001874 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001875 * Container-init gets no signals it doesn't want from same
1876 * container.
1877 *
1878 * Note that if global/container-init sees a sig_kernel_only()
1879 * signal here, the signal must have been generated internally
1880 * or must have come from an ancestor namespace. In either
1881 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001882 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001883 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001884 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 continue;
1886
1887 if (sig_kernel_stop(signr)) {
1888 /*
1889 * The default action is to stop all threads in
1890 * the thread group. The job control signals
1891 * do nothing in an orphaned pgrp, but SIGSTOP
1892 * always works. Note that siglock needs to be
1893 * dropped during the call to is_orphaned_pgrp()
1894 * because of lock ordering with tasklist_lock.
1895 * This allows an intervening SIGCONT to be posted.
1896 * We need to check for that and bail out if necessary.
1897 */
1898 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001899 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* signals can be posted during this window */
1902
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001903 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto relock;
1905
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001906 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001909 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* It released the siglock. */
1911 goto relock;
1912 }
1913
1914 /*
1915 * We didn't actually stop, due to a race
1916 * with SIGCONT or something like that.
1917 */
1918 continue;
1919 }
1920
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001921 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 /*
1924 * Anything else is fatal, maybe with a core dump.
1925 */
1926 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001929 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001930 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
1932 * If it was able to dump core, this kills all
1933 * other threads in the group and synchronizes with
1934 * their demise. If we lost the race with another
1935 * thread getting here, it set group_exit_code
1936 * first and our do_group_exit call below will use
1937 * that value and ignore the one we pass it.
1938 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001939 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941
1942 /*
1943 * Death signals, no core dump.
1944 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001945 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /* NOTREACHED */
1947 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001948 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return signr;
1950}
1951
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001952void exit_signals(struct task_struct *tsk)
1953{
1954 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001955 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001956
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001957 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1958 tsk->flags |= PF_EXITING;
1959 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001960 }
1961
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001962 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001963 /*
1964 * From now this task is not visible for group-wide signals,
1965 * see wants_signal(), do_signal_stop().
1966 */
1967 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001968 if (!signal_pending(tsk))
1969 goto out;
1970
1971 /* It could be that __group_complete_signal() choose us to
1972 * notify about group-wide signal. Another thread should be
1973 * woken now to take the signal since we will not.
1974 */
1975 for (t = tsk; (t = next_thread(t)) != tsk; )
1976 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1977 recalc_sigpending_and_wake(t);
1978
1979 if (unlikely(tsk->signal->group_stop_count) &&
1980 !--tsk->signal->group_stop_count) {
1981 tsk->signal->flags = SIGNAL_STOP_STOPPED;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001982 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001983 }
1984out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001985 spin_unlock_irq(&tsk->sighand->siglock);
1986
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001987 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001988 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001989 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001990 read_unlock(&tasklist_lock);
1991 }
1992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994EXPORT_SYMBOL(recalc_sigpending);
1995EXPORT_SYMBOL_GPL(dequeue_signal);
1996EXPORT_SYMBOL(flush_signals);
1997EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998EXPORT_SYMBOL(send_sig);
1999EXPORT_SYMBOL(send_sig_info);
2000EXPORT_SYMBOL(sigprocmask);
2001EXPORT_SYMBOL(block_all_signals);
2002EXPORT_SYMBOL(unblock_all_signals);
2003
2004
2005/*
2006 * System call entry points.
2007 */
2008
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002009SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010{
2011 struct restart_block *restart = &current_thread_info()->restart_block;
2012 return restart->fn(restart);
2013}
2014
2015long do_no_restart_syscall(struct restart_block *param)
2016{
2017 return -EINTR;
2018}
2019
2020/*
2021 * We don't need to get the kernel lock - this is all local to this
2022 * particular thread.. (and that's good, because this is _heavily_
2023 * used by various programs)
2024 */
2025
2026/*
2027 * This is also useful for kernel threads that want to temporarily
2028 * (or permanently) block certain signals.
2029 *
2030 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2031 * interface happily blocks "unblockable" signals like SIGKILL
2032 * and friends.
2033 */
2034int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2035{
2036 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002039 if (oldset)
2040 *oldset = current->blocked;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 error = 0;
2043 switch (how) {
2044 case SIG_BLOCK:
2045 sigorsets(&current->blocked, &current->blocked, set);
2046 break;
2047 case SIG_UNBLOCK:
2048 signandsets(&current->blocked, &current->blocked, set);
2049 break;
2050 case SIG_SETMASK:
2051 current->blocked = *set;
2052 break;
2053 default:
2054 error = -EINVAL;
2055 }
2056 recalc_sigpending();
2057 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 return error;
2060}
2061
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002062SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2063 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 int error = -EINVAL;
2066 sigset_t old_set, new_set;
2067
2068 /* XXX: Don't preclude handling different sized sigset_t's. */
2069 if (sigsetsize != sizeof(sigset_t))
2070 goto out;
2071
2072 if (set) {
2073 error = -EFAULT;
2074 if (copy_from_user(&new_set, set, sizeof(*set)))
2075 goto out;
2076 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2077
2078 error = sigprocmask(how, &new_set, &old_set);
2079 if (error)
2080 goto out;
2081 if (oset)
2082 goto set_old;
2083 } else if (oset) {
2084 spin_lock_irq(&current->sighand->siglock);
2085 old_set = current->blocked;
2086 spin_unlock_irq(&current->sighand->siglock);
2087
2088 set_old:
2089 error = -EFAULT;
2090 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2091 goto out;
2092 }
2093 error = 0;
2094out:
2095 return error;
2096}
2097
2098long do_sigpending(void __user *set, unsigned long sigsetsize)
2099{
2100 long error = -EINVAL;
2101 sigset_t pending;
2102
2103 if (sigsetsize > sizeof(sigset_t))
2104 goto out;
2105
2106 spin_lock_irq(&current->sighand->siglock);
2107 sigorsets(&pending, &current->pending.signal,
2108 &current->signal->shared_pending.signal);
2109 spin_unlock_irq(&current->sighand->siglock);
2110
2111 /* Outside the lock because only this thread touches it. */
2112 sigandsets(&pending, &current->blocked, &pending);
2113
2114 error = -EFAULT;
2115 if (!copy_to_user(set, &pending, sigsetsize))
2116 error = 0;
2117
2118out:
2119 return error;
2120}
2121
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002122SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 return do_sigpending(set, sigsetsize);
2125}
2126
2127#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2128
2129int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2130{
2131 int err;
2132
2133 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2134 return -EFAULT;
2135 if (from->si_code < 0)
2136 return __copy_to_user(to, from, sizeof(siginfo_t))
2137 ? -EFAULT : 0;
2138 /*
2139 * If you change siginfo_t structure, please be sure
2140 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002141 * Please remember to update the signalfd_copyinfo() function
2142 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * It should never copy any pad contained in the structure
2144 * to avoid security leaks, but must copy the generic
2145 * 3 ints plus the relevant union member.
2146 */
2147 err = __put_user(from->si_signo, &to->si_signo);
2148 err |= __put_user(from->si_errno, &to->si_errno);
2149 err |= __put_user((short)from->si_code, &to->si_code);
2150 switch (from->si_code & __SI_MASK) {
2151 case __SI_KILL:
2152 err |= __put_user(from->si_pid, &to->si_pid);
2153 err |= __put_user(from->si_uid, &to->si_uid);
2154 break;
2155 case __SI_TIMER:
2156 err |= __put_user(from->si_tid, &to->si_tid);
2157 err |= __put_user(from->si_overrun, &to->si_overrun);
2158 err |= __put_user(from->si_ptr, &to->si_ptr);
2159 break;
2160 case __SI_POLL:
2161 err |= __put_user(from->si_band, &to->si_band);
2162 err |= __put_user(from->si_fd, &to->si_fd);
2163 break;
2164 case __SI_FAULT:
2165 err |= __put_user(from->si_addr, &to->si_addr);
2166#ifdef __ARCH_SI_TRAPNO
2167 err |= __put_user(from->si_trapno, &to->si_trapno);
2168#endif
2169 break;
2170 case __SI_CHLD:
2171 err |= __put_user(from->si_pid, &to->si_pid);
2172 err |= __put_user(from->si_uid, &to->si_uid);
2173 err |= __put_user(from->si_status, &to->si_status);
2174 err |= __put_user(from->si_utime, &to->si_utime);
2175 err |= __put_user(from->si_stime, &to->si_stime);
2176 break;
2177 case __SI_RT: /* This is not generated by the kernel as of now. */
2178 case __SI_MESGQ: /* But this is */
2179 err |= __put_user(from->si_pid, &to->si_pid);
2180 err |= __put_user(from->si_uid, &to->si_uid);
2181 err |= __put_user(from->si_ptr, &to->si_ptr);
2182 break;
2183 default: /* this is just in case for now ... */
2184 err |= __put_user(from->si_pid, &to->si_pid);
2185 err |= __put_user(from->si_uid, &to->si_uid);
2186 break;
2187 }
2188 return err;
2189}
2190
2191#endif
2192
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002193SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2194 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2195 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
2197 int ret, sig;
2198 sigset_t these;
2199 struct timespec ts;
2200 siginfo_t info;
2201 long timeout = 0;
2202
2203 /* XXX: Don't preclude handling different sized sigset_t's. */
2204 if (sigsetsize != sizeof(sigset_t))
2205 return -EINVAL;
2206
2207 if (copy_from_user(&these, uthese, sizeof(these)))
2208 return -EFAULT;
2209
2210 /*
2211 * Invert the set of allowed signals to get those we
2212 * want to block.
2213 */
2214 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2215 signotset(&these);
2216
2217 if (uts) {
2218 if (copy_from_user(&ts, uts, sizeof(ts)))
2219 return -EFAULT;
2220 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2221 || ts.tv_sec < 0)
2222 return -EINVAL;
2223 }
2224
2225 spin_lock_irq(&current->sighand->siglock);
2226 sig = dequeue_signal(current, &these, &info);
2227 if (!sig) {
2228 timeout = MAX_SCHEDULE_TIMEOUT;
2229 if (uts)
2230 timeout = (timespec_to_jiffies(&ts)
2231 + (ts.tv_sec || ts.tv_nsec));
2232
2233 if (timeout) {
2234 /* None ready -- temporarily unblock those we're
2235 * interested while we are sleeping in so that we'll
2236 * be awakened when they arrive. */
2237 current->real_blocked = current->blocked;
2238 sigandsets(&current->blocked, &current->blocked, &these);
2239 recalc_sigpending();
2240 spin_unlock_irq(&current->sighand->siglock);
2241
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002242 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 spin_lock_irq(&current->sighand->siglock);
2245 sig = dequeue_signal(current, &these, &info);
2246 current->blocked = current->real_blocked;
2247 siginitset(&current->real_blocked, 0);
2248 recalc_sigpending();
2249 }
2250 }
2251 spin_unlock_irq(&current->sighand->siglock);
2252
2253 if (sig) {
2254 ret = sig;
2255 if (uinfo) {
2256 if (copy_siginfo_to_user(uinfo, &info))
2257 ret = -EFAULT;
2258 }
2259 } else {
2260 ret = -EAGAIN;
2261 if (timeout)
2262 ret = -EINTR;
2263 }
2264
2265 return ret;
2266}
2267
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002268SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 struct siginfo info;
2271
2272 info.si_signo = sig;
2273 info.si_errno = 0;
2274 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002275 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002276 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 return kill_something_info(sig, &info, pid);
2279}
2280
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002281static int
2282do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002283{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002284 struct task_struct *p;
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002285 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002286
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002287 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002288 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002289 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002290 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002291 /*
2292 * The null signal is a permissions and process existence
2293 * probe. No signal is actually delivered.
2294 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002295 if (!error && sig) {
2296 error = do_send_sig_info(sig, info, p, false);
2297 /*
2298 * If lock_task_sighand() failed we pretend the task
2299 * dies after receiving the signal. The window is tiny,
2300 * and the signal is private anyway.
2301 */
2302 if (unlikely(error == -ESRCH))
2303 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002304 }
2305 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002306 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002307
2308 return error;
2309}
2310
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002311static int do_tkill(pid_t tgid, pid_t pid, int sig)
2312{
2313 struct siginfo info;
2314
2315 info.si_signo = sig;
2316 info.si_errno = 0;
2317 info.si_code = SI_TKILL;
2318 info.si_pid = task_tgid_vnr(current);
2319 info.si_uid = current_uid();
2320
2321 return do_send_specific(tgid, pid, sig, &info);
2322}
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324/**
2325 * sys_tgkill - send signal to one specific thread
2326 * @tgid: the thread group ID of the thread
2327 * @pid: the PID of the thread
2328 * @sig: signal to be sent
2329 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002330 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * exists but it's not belonging to the target process anymore. This
2332 * method solves the problem of threads exiting and PIDs getting reused.
2333 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002334SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /* This is only valid for single tasks */
2337 if (pid <= 0 || tgid <= 0)
2338 return -EINVAL;
2339
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002340 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
2343/*
2344 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2345 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002346SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /* This is only valid for single tasks */
2349 if (pid <= 0)
2350 return -EINVAL;
2351
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002352 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353}
2354
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002355SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2356 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
2358 siginfo_t info;
2359
2360 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2361 return -EFAULT;
2362
2363 /* Not even root can pretend to send signals from the kernel.
2364 Nor can they impersonate a kill(), which adds source info. */
2365 if (info.si_code >= 0)
2366 return -EPERM;
2367 info.si_signo = sig;
2368
2369 /* POSIX.1b doesn't mention process groups. */
2370 return kill_proc_info(sig, &info, pid);
2371}
2372
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002373long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2374{
2375 /* This is only valid for single tasks */
2376 if (pid <= 0 || tgid <= 0)
2377 return -EINVAL;
2378
2379 /* Not even root can pretend to send signals from the kernel.
2380 Nor can they impersonate a kill(), which adds source info. */
2381 if (info->si_code >= 0)
2382 return -EPERM;
2383 info->si_signo = sig;
2384
2385 return do_send_specific(tgid, pid, sig, info);
2386}
2387
2388SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2389 siginfo_t __user *, uinfo)
2390{
2391 siginfo_t info;
2392
2393 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2394 return -EFAULT;
2395
2396 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2397}
2398
Oleg Nesterov88531f72006-03-28 16:11:24 -08002399int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002401 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002403 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002405 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return -EINVAL;
2407
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002408 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if (oact)
2412 *oact = *k;
2413
2414 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002415 sigdelsetmask(&act->sa.sa_mask,
2416 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002417 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /*
2419 * POSIX 3.3.1.3:
2420 * "Setting a signal action to SIG_IGN for a signal that is
2421 * pending shall cause the pending signal to be discarded,
2422 * whether or not it is blocked."
2423 *
2424 * "Setting a signal action to SIG_DFL for a signal that is
2425 * pending and whose default action is to ignore the signal
2426 * (for example, SIGCHLD), shall cause the pending signal to
2427 * be discarded, whether or not it is blocked"
2428 */
Roland McGrath35de2542008-07-25 19:45:51 -07002429 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002430 sigemptyset(&mask);
2431 sigaddset(&mask, sig);
2432 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002434 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 t = next_thread(t);
2436 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 }
2439
2440 spin_unlock_irq(&current->sighand->siglock);
2441 return 0;
2442}
2443
2444int
2445do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2446{
2447 stack_t oss;
2448 int error;
2449
Linus Torvalds0083fc22009-08-01 10:34:56 -07002450 oss.ss_sp = (void __user *) current->sas_ss_sp;
2451 oss.ss_size = current->sas_ss_size;
2452 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
2454 if (uss) {
2455 void __user *ss_sp;
2456 size_t ss_size;
2457 int ss_flags;
2458
2459 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002460 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2461 goto out;
2462 error = __get_user(ss_sp, &uss->ss_sp) |
2463 __get_user(ss_flags, &uss->ss_flags) |
2464 __get_user(ss_size, &uss->ss_size);
2465 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 goto out;
2467
2468 error = -EPERM;
2469 if (on_sig_stack(sp))
2470 goto out;
2471
2472 error = -EINVAL;
2473 /*
2474 *
2475 * Note - this code used to test ss_flags incorrectly
2476 * old code may have been written using ss_flags==0
2477 * to mean ss_flags==SS_ONSTACK (as this was the only
2478 * way that worked) - this fix preserves that older
2479 * mechanism
2480 */
2481 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2482 goto out;
2483
2484 if (ss_flags == SS_DISABLE) {
2485 ss_size = 0;
2486 ss_sp = NULL;
2487 } else {
2488 error = -ENOMEM;
2489 if (ss_size < MINSIGSTKSZ)
2490 goto out;
2491 }
2492
2493 current->sas_ss_sp = (unsigned long) ss_sp;
2494 current->sas_ss_size = ss_size;
2495 }
2496
Linus Torvalds0083fc22009-08-01 10:34:56 -07002497 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (uoss) {
2499 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002500 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002502 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2503 __put_user(oss.ss_size, &uoss->ss_size) |
2504 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507out:
2508 return error;
2509}
2510
2511#ifdef __ARCH_WANT_SYS_SIGPENDING
2512
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002513SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
2515 return do_sigpending(set, sizeof(*set));
2516}
2517
2518#endif
2519
2520#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2521/* Some platforms have their own version with special arguments others
2522 support only sys_rt_sigprocmask. */
2523
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002524SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2525 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526{
2527 int error;
2528 old_sigset_t old_set, new_set;
2529
2530 if (set) {
2531 error = -EFAULT;
2532 if (copy_from_user(&new_set, set, sizeof(*set)))
2533 goto out;
2534 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2535
2536 spin_lock_irq(&current->sighand->siglock);
2537 old_set = current->blocked.sig[0];
2538
2539 error = 0;
2540 switch (how) {
2541 default:
2542 error = -EINVAL;
2543 break;
2544 case SIG_BLOCK:
2545 sigaddsetmask(&current->blocked, new_set);
2546 break;
2547 case SIG_UNBLOCK:
2548 sigdelsetmask(&current->blocked, new_set);
2549 break;
2550 case SIG_SETMASK:
2551 current->blocked.sig[0] = new_set;
2552 break;
2553 }
2554
2555 recalc_sigpending();
2556 spin_unlock_irq(&current->sighand->siglock);
2557 if (error)
2558 goto out;
2559 if (oset)
2560 goto set_old;
2561 } else if (oset) {
2562 old_set = current->blocked.sig[0];
2563 set_old:
2564 error = -EFAULT;
2565 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2566 goto out;
2567 }
2568 error = 0;
2569out:
2570 return error;
2571}
2572#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2573
2574#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002575SYSCALL_DEFINE4(rt_sigaction, int, sig,
2576 const struct sigaction __user *, act,
2577 struct sigaction __user *, oact,
2578 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 struct k_sigaction new_sa, old_sa;
2581 int ret = -EINVAL;
2582
2583 /* XXX: Don't preclude handling different sized sigset_t's. */
2584 if (sigsetsize != sizeof(sigset_t))
2585 goto out;
2586
2587 if (act) {
2588 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2589 return -EFAULT;
2590 }
2591
2592 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2593
2594 if (!ret && oact) {
2595 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2596 return -EFAULT;
2597 }
2598out:
2599 return ret;
2600}
2601#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2602
2603#ifdef __ARCH_WANT_SYS_SGETMASK
2604
2605/*
2606 * For backwards compatibility. Functionality superseded by sigprocmask.
2607 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002608SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 /* SMP safe */
2611 return current->blocked.sig[0];
2612}
2613
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002614SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
2616 int old;
2617
2618 spin_lock_irq(&current->sighand->siglock);
2619 old = current->blocked.sig[0];
2620
2621 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2622 sigmask(SIGSTOP)));
2623 recalc_sigpending();
2624 spin_unlock_irq(&current->sighand->siglock);
2625
2626 return old;
2627}
2628#endif /* __ARCH_WANT_SGETMASK */
2629
2630#ifdef __ARCH_WANT_SYS_SIGNAL
2631/*
2632 * For backwards compatibility. Functionality superseded by sigaction.
2633 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002634SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
2636 struct k_sigaction new_sa, old_sa;
2637 int ret;
2638
2639 new_sa.sa.sa_handler = handler;
2640 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03002641 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 ret = do_sigaction(sig, &new_sa, &old_sa);
2644
2645 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2646}
2647#endif /* __ARCH_WANT_SYS_SIGNAL */
2648
2649#ifdef __ARCH_WANT_SYS_PAUSE
2650
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002651SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 current->state = TASK_INTERRUPTIBLE;
2654 schedule();
2655 return -ERESTARTNOHAND;
2656}
2657
2658#endif
2659
David Woodhouse150256d2006-01-18 17:43:57 -08002660#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002661SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002662{
2663 sigset_t newset;
2664
2665 /* XXX: Don't preclude handling different sized sigset_t's. */
2666 if (sigsetsize != sizeof(sigset_t))
2667 return -EINVAL;
2668
2669 if (copy_from_user(&newset, unewset, sizeof(newset)))
2670 return -EFAULT;
2671 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2672
2673 spin_lock_irq(&current->sighand->siglock);
2674 current->saved_sigmask = current->blocked;
2675 current->blocked = newset;
2676 recalc_sigpending();
2677 spin_unlock_irq(&current->sighand->siglock);
2678
2679 current->state = TASK_INTERRUPTIBLE;
2680 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002681 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002682 return -ERESTARTNOHAND;
2683}
2684#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2685
David Howellsf269fdd2006-09-27 01:50:23 -07002686__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2687{
2688 return NULL;
2689}
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691void __init signals_init(void)
2692{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002693 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}