blob: fe08008133dad06e0acd6efdc98db25c64367565 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
14#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/sched.h>
17#include <linux/fs.h>
18#include <linux/tty.h>
19#include <linux/binfmts.h>
20#include <linux/security.h>
21#include <linux/syscalls.h>
22#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070023#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070024#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090025#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070026#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080027#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080028#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080029#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
Steven Rostedtad8d75f2009-04-14 19:39:12 -040031#include <trace/events/sched.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
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090045int print_fatal_signals __read_mostly;
46
Roland McGrath35de2542008-07-25 19:45:51 -070047static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070048{
Roland McGrath35de2542008-07-25 19:45:51 -070049 return t->sighand->action[sig - 1].sa.sa_handler;
50}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070051
Roland McGrath35de2542008-07-25 19:45:51 -070052static int sig_handler_ignored(void __user *handler, int sig)
53{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070054 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070055 return handler == SIG_IGN ||
56 (handler == SIG_DFL && sig_kernel_ignore(sig));
57}
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070059static int sig_task_ignored(struct task_struct *t, int sig,
60 int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Roland McGrath35de2542008-07-25 19:45:51 -070062 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Oleg Nesterovf008faf2009-04-02 16:58:02 -070064 handler = sig_handler(t, sig);
65
66 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070067 handler == SIG_DFL && !from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070068 return 1;
69
70 return sig_handler_ignored(handler, sig);
71}
72
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070073static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /*
76 * Blocked signals are never ignored, since the
77 * signal handler may change by the time it is
78 * unblocked.
79 */
Roland McGrath325d22d2007-11-12 15:41:55 -080080 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return 0;
82
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -070083 if (!sig_task_ignored(t, sig, from_ancestor_ns))
Roland McGrath35de2542008-07-25 19:45:51 -070084 return 0;
85
86 /*
87 * Tracers may want to know about even ignored signals.
88 */
Oleg Nesterov43918f22009-04-02 16:58:00 -070089 return !tracehook_consider_ignored_signal(t, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/*
93 * Re-calculate pending state from the set of locally pending
94 * signals, globally pending signals, and blocked signals.
95 */
96static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
97{
98 unsigned long ready;
99 long i;
100
101 switch (_NSIG_WORDS) {
102 default:
103 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
104 ready |= signal->sig[i] &~ blocked->sig[i];
105 break;
106
107 case 4: ready = signal->sig[3] &~ blocked->sig[3];
108 ready |= signal->sig[2] &~ blocked->sig[2];
109 ready |= signal->sig[1] &~ blocked->sig[1];
110 ready |= signal->sig[0] &~ blocked->sig[0];
111 break;
112
113 case 2: ready = signal->sig[1] &~ blocked->sig[1];
114 ready |= signal->sig[0] &~ blocked->sig[0];
115 break;
116
117 case 1: ready = signal->sig[0] &~ blocked->sig[0];
118 }
119 return ready != 0;
120}
121
122#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
123
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700124static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 if (t->signal->group_stop_count > 0 ||
127 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700128 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700130 return 1;
131 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700132 /*
133 * We must never clear the flag in another thread, or in current
134 * when it's possible the current syscall is returning -ERESTART*.
135 * So we don't clear it here, and only callers who know they should do.
136 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700137 return 0;
138}
139
140/*
141 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
142 * This is superfluous when called on current, the wakeup is a harmless no-op.
143 */
144void recalc_sigpending_and_wake(struct task_struct *t)
145{
146 if (recalc_sigpending_tsk(t))
147 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150void recalc_sigpending(void)
151{
Roland McGrathb787f7b2008-07-25 19:45:55 -0700152 if (unlikely(tracehook_force_sigpending()))
153 set_thread_flag(TIF_SIGPENDING);
154 else if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700155 clear_thread_flag(TIF_SIGPENDING);
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/* Given the mask, find the first available signal that should be serviced. */
160
Davide Libenzifba2afa2007-05-10 22:23:13 -0700161int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 unsigned long i, *s, *m, x;
164 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 s = pending->signal.sig;
167 m = mask->sig;
168 switch (_NSIG_WORDS) {
169 default:
170 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
171 if ((x = *s &~ *m) != 0) {
172 sig = ffz(~x) + i*_NSIG_BPW + 1;
173 break;
174 }
175 break;
176
177 case 2: if ((x = s[0] &~ m[0]) != 0)
178 sig = 1;
179 else if ((x = s[1] &~ m[1]) != 0)
180 sig = _NSIG_BPW + 1;
181 else
182 break;
183 sig += ffz(~x);
184 break;
185
186 case 1: if ((x = *s &~ *m) != 0)
187 sig = ffz(~x) + 1;
188 break;
189 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return sig;
192}
193
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900194static inline void print_dropped_signal(int sig)
195{
196 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
197
198 if (!print_fatal_signals)
199 return;
200
201 if (!__ratelimit(&ratelimit_state))
202 return;
203
204 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
205 current->comm, current->pid, sig);
206}
207
David Howellsc69e8d92008-11-14 10:39:19 +1100208/*
209 * allocate a new signal queue record
210 * - this may be called without locks if and only if t == current, otherwise an
David Howellsd84f4f92008-11-14 10:39:23 +1100211 * appopriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100212 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900213static struct sigqueue *
214__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800217 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800219 /*
David Howellsc69e8d92008-11-14 10:39:19 +1100220 * We won't get problems with the target's UID changing under us
221 * because changing it requires RCU be used, and if t != current, the
222 * caller must be holding the RCU readlock (by way of a spinlock) and
223 * we use RCU protection here
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800224 */
David Howellsd84f4f92008-11-14 10:39:23 +1100225 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800226 atomic_inc(&user->sigpending);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800229 atomic_read(&user->sigpending) <=
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900230 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900232 } else {
233 print_dropped_signal(sig);
234 }
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800237 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100238 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 } else {
240 INIT_LIST_HEAD(&q->list);
241 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100242 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
David Howellsd84f4f92008-11-14 10:39:23 +1100244
245 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Andrew Morton514a01b2006-02-03 03:04:41 -0800248static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 if (q->flags & SIGQUEUE_PREALLOC)
251 return;
252 atomic_dec(&q->user->sigpending);
253 free_uid(q->user);
254 kmem_cache_free(sigqueue_cachep, q);
255}
256
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800257void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct sigqueue *q;
260
261 sigemptyset(&queue->signal);
262 while (!list_empty(&queue->list)) {
263 q = list_entry(queue->list.next, struct sigqueue , list);
264 list_del_init(&q->list);
265 __sigqueue_free(q);
266 }
267}
268
269/*
270 * Flush all pending signals for a task.
271 */
David Howells3bcac022009-04-29 13:45:05 +0100272void __flush_signals(struct task_struct *t)
273{
274 clear_tsk_thread_flag(t, TIF_SIGPENDING);
275 flush_sigqueue(&t->pending);
276 flush_sigqueue(&t->signal->shared_pending);
277}
278
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800279void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 unsigned long flags;
282
283 spin_lock_irqsave(&t->sighand->siglock, flags);
David Howells3bcac022009-04-29 13:45:05 +0100284 __flush_signals(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 spin_unlock_irqrestore(&t->sighand->siglock, flags);
286}
287
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400288static void __flush_itimer_signals(struct sigpending *pending)
289{
290 sigset_t signal, retain;
291 struct sigqueue *q, *n;
292
293 signal = pending->signal;
294 sigemptyset(&retain);
295
296 list_for_each_entry_safe(q, n, &pending->list, list) {
297 int sig = q->info.si_signo;
298
299 if (likely(q->info.si_code != SI_TIMER)) {
300 sigaddset(&retain, sig);
301 } else {
302 sigdelset(&signal, sig);
303 list_del_init(&q->list);
304 __sigqueue_free(q);
305 }
306 }
307
308 sigorsets(&pending->signal, &signal, &retain);
309}
310
311void flush_itimer_signals(void)
312{
313 struct task_struct *tsk = current;
314 unsigned long flags;
315
316 spin_lock_irqsave(&tsk->sighand->siglock, flags);
317 __flush_itimer_signals(&tsk->pending);
318 __flush_itimer_signals(&tsk->signal->shared_pending);
319 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
320}
321
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700322void ignore_signals(struct task_struct *t)
323{
324 int i;
325
326 for (i = 0; i < _NSIG; ++i)
327 t->sighand->action[i].sa.sa_handler = SIG_IGN;
328
329 flush_signals(t);
330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * Flush all handlers for a task.
334 */
335
336void
337flush_signal_handlers(struct task_struct *t, int force_default)
338{
339 int i;
340 struct k_sigaction *ka = &t->sighand->action[0];
341 for (i = _NSIG ; i != 0 ; i--) {
342 if (force_default || ka->sa.sa_handler != SIG_IGN)
343 ka->sa.sa_handler = SIG_DFL;
344 ka->sa.sa_flags = 0;
345 sigemptyset(&ka->sa.sa_mask);
346 ka++;
347 }
348}
349
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200350int unhandled_signal(struct task_struct *tsk, int sig)
351{
Roland McGrath445a91d2008-07-25 19:45:52 -0700352 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700353 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200354 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700355 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200356 return 0;
Oleg Nesterov43918f22009-04-02 16:58:00 -0700357 return !tracehook_consider_fatal_signal(tsk, sig);
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/* Notify the system that a driver wants to block all signals for this
362 * process, and wants to be notified if any signals at all were to be
363 * sent/acted upon. If the notifier routine returns non-zero, then the
364 * signal will be acted upon after all. If the notifier routine returns 0,
365 * then then signal will be blocked. Only one block per process is
366 * allowed. priv is a pointer to private data that the notifier routine
367 * can use to determine if the signal should be blocked or not. */
368
369void
370block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
371{
372 unsigned long flags;
373
374 spin_lock_irqsave(&current->sighand->siglock, flags);
375 current->notifier_mask = mask;
376 current->notifier_data = priv;
377 current->notifier = notifier;
378 spin_unlock_irqrestore(&current->sighand->siglock, flags);
379}
380
381/* Notify the system that blocking has ended. */
382
383void
384unblock_all_signals(void)
385{
386 unsigned long flags;
387
388 spin_lock_irqsave(&current->sighand->siglock, flags);
389 current->notifier = NULL;
390 current->notifier_data = NULL;
391 recalc_sigpending();
392 spin_unlock_irqrestore(&current->sighand->siglock, flags);
393}
394
Oleg Nesterov100360f2008-07-25 01:47:29 -0700395static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /*
400 * Collect the siginfo appropriate to this signal. Check if
401 * there is another siginfo for the same signal.
402 */
403 list_for_each_entry(q, &list->list, list) {
404 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700405 if (first)
406 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 first = q;
408 }
409 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700410
411 sigdelset(&list->signal, sig);
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700414still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 list_del_init(&first->list);
416 copy_siginfo(info, &first->info);
417 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* Ok, it wasn't in the queue. This must be
420 a fast-pathed signal or we must have been
421 out of queue space. So zero out the info.
422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 info->si_signo = sig;
424 info->si_errno = 0;
425 info->si_code = 0;
426 info->si_pid = 0;
427 info->si_uid = 0;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
432 siginfo_t *info)
433{
Roland McGrath27d91e02006-09-29 02:00:31 -0700434 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (sig) {
437 if (current->notifier) {
438 if (sigismember(current->notifier_mask, sig)) {
439 if (!(current->notifier)(current->notifier_data)) {
440 clear_thread_flag(TIF_SIGPENDING);
441 return 0;
442 }
443 }
444 }
445
Oleg Nesterov100360f2008-07-25 01:47:29 -0700446 collect_signal(sig, pending, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 return sig;
450}
451
452/*
453 * Dequeue a signal and return the element to the caller, which is
454 * expected to free it.
455 *
456 * All callers have to hold the siglock.
457 */
458int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
459{
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700460 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000461
462 /* We only dequeue private signals from ourselves, we don't let
463 * signalfd steal them
464 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700465 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800466 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 signr = __dequeue_signal(&tsk->signal->shared_pending,
468 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800469 /*
470 * itimer signal ?
471 *
472 * itimers are process shared and we restart periodic
473 * itimers in the signal delivery path to prevent DoS
474 * attacks in the high resolution timer case. This is
475 * compliant with the old way of self restarting
476 * itimers, as the SIGALRM is a legacy signal and only
477 * queued once. Changing the restart behaviour to
478 * restart the timer in the signal dequeue path is
479 * reducing the timer noise on heavy loaded !highres
480 * systems too.
481 */
482 if (unlikely(signr == SIGALRM)) {
483 struct hrtimer *tmr = &tsk->signal->real_timer;
484
485 if (!hrtimer_is_queued(tmr) &&
486 tsk->signal->it_real_incr.tv64 != 0) {
487 hrtimer_forward(tmr, tmr->base->get_time(),
488 tsk->signal->it_real_incr);
489 hrtimer_restart(tmr);
490 }
491 }
492 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700493
Davide Libenzib8fceee2007-09-20 12:40:16 -0700494 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700495 if (!signr)
496 return 0;
497
498 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800499 /*
500 * Set a marker that we have dequeued a stop signal. Our
501 * caller might release the siglock and then the pending
502 * stop signal it is about to process is no longer in the
503 * pending bitmasks, but must still be cleared by a SIGCONT
504 * (and overruled by a SIGKILL). So those cases clear this
505 * shared flag after we've set it. Note that this flag may
506 * remain set after the signal we return is ignored or
507 * handled. That doesn't matter because its only purpose
508 * is to alert stop-signal processing code when another
509 * processor has come along and cleared the flag.
510 */
Oleg Nesterov92413d72008-07-25 01:47:30 -0700511 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800512 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700513 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /*
515 * Release the siglock to ensure proper locking order
516 * of timer locks outside of siglocks. Note, we leave
517 * irqs disabled here, since the posix-timers code is
518 * about to disable them again anyway.
519 */
520 spin_unlock(&tsk->sighand->siglock);
521 do_schedule_next_timer(info);
522 spin_lock(&tsk->sighand->siglock);
523 }
524 return signr;
525}
526
527/*
528 * Tell a process that it has a new active signal..
529 *
530 * NOTE! we rely on the previous spin_lock to
531 * lock interrupts for us! We can only be called with
532 * "siglock" held, and the local interrupt must
533 * have been disabled when that got acquired!
534 *
535 * No need to set need_resched since signal event passing
536 * goes through ->blocked
537 */
538void signal_wake_up(struct task_struct *t, int resume)
539{
540 unsigned int mask;
541
542 set_tsk_thread_flag(t, TIF_SIGPENDING);
543
544 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500545 * For SIGKILL, we want to wake it up in the stopped/traced/killable
546 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * executing another processor and just now entering stopped state.
548 * By using wake_up_state, we ensure the process will wake up and
549 * handle its death signal.
550 */
551 mask = TASK_INTERRUPTIBLE;
552 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500553 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (!wake_up_state(t, mask))
555 kick_process(t);
556}
557
558/*
559 * Remove signals in mask from the pending set and queue.
560 * Returns 1 if any signals were found.
561 *
562 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800563 *
564 * This version takes a sigset mask and looks at all signals,
565 * not just those in the first mask word.
566 */
567static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
568{
569 struct sigqueue *q, *n;
570 sigset_t m;
571
572 sigandsets(&m, mask, &s->signal);
573 if (sigisemptyset(&m))
574 return 0;
575
576 signandsets(&s->signal, &s->signal, mask);
577 list_for_each_entry_safe(q, n, &s->list, list) {
578 if (sigismember(mask, q->info.si_signo)) {
579 list_del_init(&q->list);
580 __sigqueue_free(q);
581 }
582 }
583 return 1;
584}
585/*
586 * Remove signals in mask from the pending set and queue.
587 * Returns 1 if any signals were found.
588 *
589 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591static int rm_from_queue(unsigned long mask, struct sigpending *s)
592{
593 struct sigqueue *q, *n;
594
595 if (!sigtestsetmask(&s->signal, mask))
596 return 0;
597
598 sigdelsetmask(&s->signal, mask);
599 list_for_each_entry_safe(q, n, &s->list, list) {
600 if (q->info.si_signo < SIGRTMIN &&
601 (mask & sigmask(q->info.si_signo))) {
602 list_del_init(&q->list);
603 __sigqueue_free(q);
604 }
605 }
606 return 1;
607}
608
609/*
610 * Bad permissions for sending the signal
David Howellsc69e8d92008-11-14 10:39:19 +1100611 * - the caller must hold at least the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
613static int check_kill_permission(int sig, struct siginfo *info,
614 struct task_struct *t)
615{
David Howellsc69e8d92008-11-14 10:39:19 +1100616 const struct cred *cred = current_cred(), *tcred;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700617 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700618 int error;
619
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700620 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700621 return -EINVAL;
622
623 if (info != SEND_SIG_NOINFO && (is_si_special(info) || SI_FROMKERNEL(info)))
624 return 0;
625
626 error = audit_signal_info(sig, t); /* Let audit system see the signal */
627 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400629
David Howellsc69e8d92008-11-14 10:39:19 +1100630 tcred = __task_cred(t);
631 if ((cred->euid ^ tcred->suid) &&
632 (cred->euid ^ tcred->uid) &&
633 (cred->uid ^ tcred->suid) &&
634 (cred->uid ^ tcred->uid) &&
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700635 !capable(CAP_KILL)) {
636 switch (sig) {
637 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700638 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700639 /*
640 * We don't return the error if sid == NULL. The
641 * task was unhashed, the caller must notice this.
642 */
643 if (!sid || sid == task_session(current))
644 break;
645 default:
646 return -EPERM;
647 }
648 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100649
Amy Griffise54dc242007-03-29 18:01:04 -0400650 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700654 * Handle magic process-wide effects of stop/continue signals. Unlike
655 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * time regardless of blocking, ignoring, or handling. This does the
657 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700658 * signals. The process stop is done as a signal action for SIG_DFL.
659 *
660 * Returns true if the signal should be actually delivered, otherwise
661 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700663static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Oleg Nesterovad16a462008-04-30 00:52:46 -0700665 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 struct task_struct *t;
667
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700668 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700670 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700672 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
674 * This is a stop signal. Remove SIGCONT from all queues.
675 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700676 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 t = p;
678 do {
679 rm_from_queue(sigmask(SIGCONT), &t->pending);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700680 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700682 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
684 * Remove all stop signals from all queues,
685 * and wake all threads.
686 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700687 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 t = p;
689 do {
690 unsigned int state;
691 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /*
693 * If there is a handler for SIGCONT, we must make
694 * sure that no thread returns to user mode before
695 * we post the signal, in case it was the only
696 * thread eligible to run the signal handler--then
697 * it must not do anything between resuming and
698 * running the handler. With the TIF_SIGPENDING
699 * flag set, the thread will pause and acquire the
700 * siglock that we hold now and until we've queued
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700701 * the pending signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 *
703 * Wake up the stopped thread _after_ setting
704 * TIF_SIGPENDING
705 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500706 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
708 set_tsk_thread_flag(t, TIF_SIGPENDING);
709 state |= TASK_INTERRUPTIBLE;
710 }
711 wake_up_state(t, state);
Oleg Nesterovad16a462008-04-30 00:52:46 -0700712 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700714 /*
715 * Notify the parent with CLD_CONTINUED if we were stopped.
716 *
717 * If we were in the middle of a group stop, we pretend it
718 * was already finished, and then continued. Since SIGCHLD
719 * doesn't queue we report only CLD_STOPPED, as if the next
720 * CLD_CONTINUED was dropped.
721 */
722 why = 0;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700723 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700724 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a462008-04-30 00:52:46 -0700725 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700726 why |= SIGNAL_CLD_STOPPED;
727
728 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700729 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700730 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700731 * will take ->siglock, notice SIGNAL_CLD_MASK, and
732 * notify its parent. See get_signal_to_deliver().
733 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700734 signal->flags = why | SIGNAL_STOP_CONTINUED;
735 signal->group_stop_count = 0;
736 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 } else {
738 /*
739 * We are not stopped, but there could be a stop
740 * signal in the middle of being processed after
741 * being removed from the queue. Clear that too.
742 */
Oleg Nesterovad16a462008-04-30 00:52:46 -0700743 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700746
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700747 return !sig_ignored(p, sig, from_ancestor_ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700750/*
751 * Test if P wants to take SIG. After we've checked all threads with this,
752 * it's equivalent to finding no threads not blocking SIG. Any threads not
753 * blocking SIG were ruled out because they are not running and already
754 * have pending signals. Such threads will dequeue from the shared queue
755 * as soon as they're available, so putting the signal on the shared queue
756 * will be equivalent to sending it to one such thread.
757 */
758static inline int wants_signal(int sig, struct task_struct *p)
759{
760 if (sigismember(&p->blocked, sig))
761 return 0;
762 if (p->flags & PF_EXITING)
763 return 0;
764 if (sig == SIGKILL)
765 return 1;
766 if (task_is_stopped_or_traced(p))
767 return 0;
768 return task_curr(p) || !signal_pending(p);
769}
770
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700771static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700772{
773 struct signal_struct *signal = p->signal;
774 struct task_struct *t;
775
776 /*
777 * Now find a thread we can wake up to take the signal off the queue.
778 *
779 * If the main thread wants the signal, it gets first crack.
780 * Probably the least surprising to the average bear.
781 */
782 if (wants_signal(sig, p))
783 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700784 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700785 /*
786 * There is just one thread and it does not need to be woken.
787 * It will dequeue unblocked signals before it runs again.
788 */
789 return;
790 else {
791 /*
792 * Otherwise try to find a suitable thread.
793 */
794 t = signal->curr_target;
795 while (!wants_signal(sig, t)) {
796 t = next_thread(t);
797 if (t == signal->curr_target)
798 /*
799 * No thread needs to be woken.
800 * Any eligible threads will see
801 * the signal in the queue soon.
802 */
803 return;
804 }
805 signal->curr_target = t;
806 }
807
808 /*
809 * Found a killable thread. If the signal will be fatal,
810 * then start taking the whole group down immediately.
811 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700812 if (sig_fatal(p, sig) &&
813 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700814 !sigismember(&t->real_blocked, sig) &&
Roland McGrath445a91d2008-07-25 19:45:52 -0700815 (sig == SIGKILL ||
Oleg Nesterov43918f22009-04-02 16:58:00 -0700816 !tracehook_consider_fatal_signal(t, sig))) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700817 /*
818 * This signal will be fatal to the whole group.
819 */
820 if (!sig_kernel_coredump(sig)) {
821 /*
822 * Start a group exit and wake everybody up.
823 * This way we don't have other threads
824 * running and doing things after a slower
825 * thread has the fatal signal pending.
826 */
827 signal->flags = SIGNAL_GROUP_EXIT;
828 signal->group_exit_code = sig;
829 signal->group_stop_count = 0;
830 t = p;
831 do {
832 sigaddset(&t->pending.signal, SIGKILL);
833 signal_wake_up(t, 1);
834 } while_each_thread(p, t);
835 return;
836 }
837 }
838
839 /*
840 * The signal is already in the shared-pending queue.
841 * Tell the chosen thread to wake up and dequeue it.
842 */
843 signal_wake_up(t, sig == SIGKILL);
844 return;
845}
846
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700847static inline int legacy_queue(struct sigpending *signals, int sig)
848{
849 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
850}
851
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700852static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
853 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700855 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700856 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200857 int override_rlimit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -0400859 trace_sched_signal_send(sig, t);
860
Oleg Nesterov6e65acb2008-04-30 00:52:50 -0700861 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700862
863 if (!prepare_signal(sig, t, from_ancestor_ns))
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700864 return 0;
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700865
866 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700868 * Short-circuit ignored signals and support queuing
869 * exactly one non-rt signal, so that we can get more
870 * detailed information about the cause of the signal.
871 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700872 if (legacy_queue(pending, sig))
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700873 return 0;
Davide Libenzifba2afa2007-05-10 22:23:13 -0700874 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * fast-pathed signals for kernel-internal things like SIGSTOP
876 * or SIGKILL.
877 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800878 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 goto out_set;
880
881 /* Real-time signals must be queued if sent by sigqueue, or
882 some other real-time mechanism. It is implementation
883 defined whether kill() does so. We attempt to do so, on
884 the principle of least surprise, but since kill is not
885 allowed to fail with EAGAIN when low on memory we just
886 make sure at least one signal gets delivered and don't
887 pass on the info struct. */
888
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200889 if (sig < SIGRTMIN)
890 override_rlimit = (is_si_special(info) || info->si_code >= 0);
891 else
892 override_rlimit = 0;
893
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900894 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +0200895 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700897 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800899 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 q->info.si_signo = sig;
901 q->info.si_errno = 0;
902 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -0800903 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -0800904 task_active_pid_ns(t));
David Howells76aac0e2008-11-14 10:39:12 +1100905 q->info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800907 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 q->info.si_signo = sig;
909 q->info.si_errno = 0;
910 q->info.si_code = SI_KERNEL;
911 q->info.si_pid = 0;
912 q->info.si_uid = 0;
913 break;
914 default:
915 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -0700916 if (from_ancestor_ns)
917 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 break;
919 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800920 } else if (!is_si_special(info)) {
921 if (sig >= SIGRTMIN && info->si_code != SI_USER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /*
923 * Queue overflow, abort. We may abort if the signal was rt
924 * and sent by user using something other than kill().
925 */
926 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928
929out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -0700930 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -0700931 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700932 complete_signal(sig, t, group);
933 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700936static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
937 int group)
938{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -0700939 int from_ancestor_ns = 0;
940
941#ifdef CONFIG_PID_NS
942 if (!is_si_special(info) && SI_FROMUSER(info) &&
943 task_pid_nr_ns(current, task_active_pid_ns(t)) <= 0)
944 from_ancestor_ns = 1;
945#endif
946
947 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -0700948}
949
Ingo Molnar45807a12007-07-15 23:40:10 -0700950static void print_fatal_signal(struct pt_regs *regs, int signr)
951{
952 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700953 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700954
Al Viroca5cd872007-10-29 04:31:16 +0000955#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100956 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700957 {
958 int i;
959 for (i = 0; i < 16; i++) {
960 unsigned char insn;
961
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100962 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700963 printk("%02x ", insn);
964 }
965 }
966#endif
967 printk("\n");
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800968 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700969 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -0800970 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -0700971}
972
973static int __init setup_print_fatal_signals(char *str)
974{
975 get_option (&str, &print_fatal_signals);
976
977 return 1;
978}
979
980__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700982int
983__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
984{
985 return send_signal(sig, info, p, 1);
986}
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static int
989specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
990{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -0700991 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Oleg Nesterov4a30deb2009-09-23 15:57:00 -0700994int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
995 bool group)
996{
997 unsigned long flags;
998 int ret = -ESRCH;
999
1000 if (lock_task_sighand(p, &flags)) {
1001 ret = send_signal(sig, info, p, group);
1002 unlock_task_sighand(p, &flags);
1003 }
1004
1005 return ret;
1006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008/*
1009 * Force a signal that the process can't ignore: if necessary
1010 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001011 *
1012 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1013 * since we do not want to have a signal handler that was blocked
1014 * be invoked when user space had explicitly blocked it.
1015 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001016 * We don't want to have recursive SIGSEGV's etc, for example,
1017 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019int
1020force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1021{
1022 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001023 int ret, blocked, ignored;
1024 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001027 action = &t->sighand->action[sig-1];
1028 ignored = action->sa.sa_handler == SIG_IGN;
1029 blocked = sigismember(&t->blocked, sig);
1030 if (blocked || ignored) {
1031 action->sa.sa_handler = SIG_DFL;
1032 if (blocked) {
1033 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001034 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001037 if (action->sa.sa_handler == SIG_DFL)
1038 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ret = specific_send_sig_info(sig, info, t);
1040 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1041
1042 return ret;
1043}
1044
1045void
1046force_sig_specific(int sig, struct task_struct *t)
1047{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -08001048 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051/*
1052 * Nuke all other threads in the group.
1053 */
1054void zap_other_threads(struct task_struct *p)
1055{
1056 struct task_struct *t;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 p->signal->group_stop_count = 0;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 for (t = next_thread(p); t != p; t = next_thread(t)) {
1061 /*
1062 * Don't bother with already dead threads
1063 */
1064 if (t->exit_state)
1065 continue;
1066
Andrea Arcangeli30e0fca2005-10-30 15:02:38 -08001067 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 signal_wake_up(t, 1);
1070 }
1071}
1072
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001073struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1074{
1075 struct sighand_struct *sighand;
1076
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001077 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001078 for (;;) {
1079 sighand = rcu_dereference(tsk->sighand);
1080 if (unlikely(sighand == NULL))
1081 break;
1082
1083 spin_lock_irqsave(&sighand->siglock, *flags);
1084 if (likely(sighand == tsk->sighand))
1085 break;
1086 spin_unlock_irqrestore(&sighand->siglock, *flags);
1087 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -07001088 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001089
1090 return sighand;
1091}
1092
David Howellsc69e8d92008-11-14 10:39:19 +11001093/*
1094 * send signal info to all the members of a group
1095 * - the caller must hold the RCU read lock at least
1096 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1098{
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001099 int ret = check_kill_permission(sig, info, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001101 if (!ret && sig)
1102 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 return ret;
1105}
1106
1107/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001108 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001110 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001112int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 struct task_struct *p = NULL;
1115 int retval, success;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 success = 0;
1118 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001119 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int err = group_send_sig_info(sig, info, p);
1121 success |= !err;
1122 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001123 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return success ? 0 : retval;
1125}
1126
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001127int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001129 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 struct task_struct *p;
1131
Ingo Molnare56d0902006-01-08 01:01:37 -08001132 rcu_read_lock();
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001133retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001134 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001135 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001137 if (unlikely(error == -ESRCH))
1138 /*
1139 * The task was unhashed in between, try again.
1140 * If it is dead, pid_task() will return NULL,
1141 * if we race with de_thread() it will find the
1142 * new leader.
1143 */
1144 goto retry;
1145 }
Ingo Molnare56d0902006-01-08 01:01:37 -08001146 rcu_read_unlock();
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return error;
1149}
1150
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001151int
1152kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001153{
1154 int error;
1155 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001156 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001157 rcu_read_unlock();
1158 return error;
1159}
1160
Eric W. Biederman2425c082006-10-02 02:17:28 -07001161/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1162int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001163 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001164{
1165 int ret = -EINVAL;
1166 struct task_struct *p;
David Howellsc69e8d92008-11-14 10:39:19 +11001167 const struct cred *pcred;
Harald Welte46113832005-10-10 19:44:29 +02001168
1169 if (!valid_signal(sig))
1170 return ret;
1171
1172 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001173 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001174 if (!p) {
1175 ret = -ESRCH;
1176 goto out_unlock;
1177 }
David Howellsc69e8d92008-11-14 10:39:19 +11001178 pcred = __task_cred(p);
1179 if ((info == SEND_SIG_NOINFO ||
1180 (!is_si_special(info) && SI_FROMUSER(info))) &&
1181 euid != pcred->suid && euid != pcred->uid &&
1182 uid != pcred->suid && uid != pcred->uid) {
Harald Welte46113832005-10-10 19:44:29 +02001183 ret = -EPERM;
1184 goto out_unlock;
1185 }
David Quigley8f95dc52006-06-30 01:55:47 -07001186 ret = security_task_kill(p, info, sig, secid);
1187 if (ret)
1188 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001189 if (sig && p->sighand) {
1190 unsigned long flags;
1191 spin_lock_irqsave(&p->sighand->siglock, flags);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001192 ret = __send_signal(sig, info, p, 1, 0);
Harald Welte46113832005-10-10 19:44:29 +02001193 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1194 }
1195out_unlock:
1196 read_unlock(&tasklist_lock);
1197 return ret;
1198}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001199EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201/*
1202 * kill_something_info() interprets pid in interesting ways just like kill(2).
1203 *
1204 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1205 * is probably wrong. Should make it like BSD or SYSV.
1206 */
1207
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001208static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001210 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001211
1212 if (pid > 0) {
1213 rcu_read_lock();
1214 ret = kill_pid_info(sig, info, find_vpid(pid));
1215 rcu_read_unlock();
1216 return ret;
1217 }
1218
1219 read_lock(&tasklist_lock);
1220 if (pid != -1) {
1221 ret = __kill_pgrp_info(sig, info,
1222 pid ? find_vpid(-pid) : task_pgrp(current));
1223 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int retval = 0, count = 0;
1225 struct task_struct * p;
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001228 if (task_pid_vnr(p) > 1 &&
1229 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int err = group_send_sig_info(sig, info, p);
1231 ++count;
1232 if (err != -EPERM)
1233 retval = err;
1234 }
1235 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001236 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001238 read_unlock(&tasklist_lock);
1239
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243/*
1244 * These are for backward compatibility with the rest of the kernel source.
1245 */
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247int
1248send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Make sure legacy kernel users don't send in bad values
1252 * (normal paths check this in check_kill_permission).
1253 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001254 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -EINVAL;
1256
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001257 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001260#define __si_special(priv) \
1261 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263int
1264send_sig(int sig, struct task_struct *p, int priv)
1265{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001266 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269void
1270force_sig(int sig, struct task_struct *p)
1271{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001272 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
1276 * When things go south during signal handling, we
1277 * will force a SIGSEGV. And if the signal that caused
1278 * the problem was already a SIGSEGV, we'll want to
1279 * make sure we don't even try to deliver the signal..
1280 */
1281int
1282force_sigsegv(int sig, struct task_struct *p)
1283{
1284 if (sig == SIGSEGV) {
1285 unsigned long flags;
1286 spin_lock_irqsave(&p->sighand->siglock, flags);
1287 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1288 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1289 }
1290 force_sig(SIGSEGV, p);
1291 return 0;
1292}
1293
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001294int kill_pgrp(struct pid *pid, int sig, int priv)
1295{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001296 int ret;
1297
1298 read_lock(&tasklist_lock);
1299 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1300 read_unlock(&tasklist_lock);
1301
1302 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001303}
1304EXPORT_SYMBOL(kill_pgrp);
1305
1306int kill_pid(struct pid *pid, int sig, int priv)
1307{
1308 return kill_pid_info(sig, __si_special(priv), pid);
1309}
1310EXPORT_SYMBOL(kill_pid);
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312/*
1313 * These functions support sending signals using preallocated sigqueue
1314 * structures. This is needed "because realtime applications cannot
1315 * afford to lose notifications of asynchronous events, like timer
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001316 * expirations or I/O completions". In the case of Posix Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 * we allocate the sigqueue structure from the timer_create. If this
1318 * allocation fails we are able to report the failure to the application
1319 * with an EAGAIN error.
1320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321struct sigqueue *sigqueue_alloc(void)
1322{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001323 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001325 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001327
1328 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
1331void sigqueue_free(struct sigqueue *q)
1332{
1333 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001334 spinlock_t *lock = &current->sighand->siglock;
1335
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1337 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001338 * We must hold ->siglock while testing q->list
1339 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001340 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001342 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001343 q->flags &= ~SIGQUEUE_PREALLOC;
1344 /*
1345 * If it is queued it will be freed when dequeued,
1346 * like the "regular" sigqueue.
1347 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001348 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001349 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001350 spin_unlock_irqrestore(lock, flags);
1351
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001352 if (q)
1353 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354}
1355
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001356int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001357{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001358 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001359 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001360 unsigned long flags;
1361 int ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001362
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001363 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001364
1365 ret = -1;
1366 if (!likely(lock_task_sighand(t, &flags)))
1367 goto ret;
1368
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001369 ret = 1; /* the signal is ignored */
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001370 if (!prepare_signal(sig, t, 0))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001371 goto out;
1372
1373 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001374 if (unlikely(!list_empty(&q->list))) {
1375 /*
1376 * If an SI_TIMER entry is already queue just increment
1377 * the overrun count.
1378 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001379 BUG_ON(q->info.si_code != SI_TIMER);
1380 q->info.si_overrun++;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001381 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001382 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001383 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001384
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001385 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001386 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001387 list_add_tail(&q->list, &pending->list);
1388 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001389 complete_signal(sig, t, group);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001390out:
1391 unlock_task_sighand(t, &flags);
1392ret:
1393 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 * Let a parent know about the death of a child.
1398 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001399 *
1400 * Returns -1 if our parent ignored us and so we've switched to
1401 * self-reaping, or else @sig.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 */
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001403int do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct siginfo info;
1406 unsigned long flags;
1407 struct sighand_struct *psig;
Roland McGrath1b046242008-08-19 20:37:07 -07001408 int ret = sig;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 BUG_ON(sig == -1);
1411
1412 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001413 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001415 BUG_ON(!task_ptrace(tsk) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1417
1418 info.si_signo = sig;
1419 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001420 /*
1421 * we are under tasklist_lock here so our parent is tied to
1422 * us and cannot exit and release its namespace.
1423 *
1424 * the only it can is to switch its nsproxy with sys_unshare,
1425 * bu uncharing pid namespaces is not allowed, so we'll always
1426 * see relevant namespace
1427 *
1428 * write_lock() currently calls preempt_disable() which is the
1429 * same as rcu_read_lock(), but according to Oleg, this is not
1430 * correct to rely on this
1431 */
1432 rcu_read_lock();
1433 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001434 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001435 rcu_read_unlock();
1436
Peter Zijlstra32bd6712009-02-05 12:24:15 +01001437 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1438 tsk->signal->utime));
1439 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1440 tsk->signal->stime));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 info.si_status = tsk->exit_code & 0x7f;
1443 if (tsk->exit_code & 0x80)
1444 info.si_code = CLD_DUMPED;
1445 else if (tsk->exit_code & 0x7f)
1446 info.si_code = CLD_KILLED;
1447 else {
1448 info.si_code = CLD_EXITED;
1449 info.si_status = tsk->exit_code >> 8;
1450 }
1451
1452 psig = tsk->parent->sighand;
1453 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001454 if (!task_ptrace(tsk) && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1456 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1457 /*
1458 * We are exiting and our parent doesn't care. POSIX.1
1459 * defines special semantics for setting SIGCHLD to SIG_IGN
1460 * or setting the SA_NOCLDWAIT flag: we should be reaped
1461 * automatically and not left for our parent's wait4 call.
1462 * Rather than having the parent do it as a magic kind of
1463 * signal handler, we just set this to tell do_exit that we
1464 * can be cleaned up without becoming a zombie. Note that
1465 * we still call __wake_up_parent in this case, because a
1466 * blocked sys_wait4 might now return -ECHILD.
1467 *
1468 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1469 * is implementation-defined: we do (if you don't want
1470 * it, just use SIG_IGN instead).
1471 */
Roland McGrath1b046242008-08-19 20:37:07 -07001472 ret = tsk->exit_signal = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001474 sig = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001476 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 __group_send_sig_info(sig, &info, tsk->parent);
1478 __wake_up_parent(tsk, tsk->parent);
1479 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001480
Roland McGrath1b046242008-08-19 20:37:07 -07001481 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001484static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 struct siginfo info;
1487 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001488 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 struct sighand_struct *sighand;
1490
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001491 if (task_ptrace(tsk))
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001492 parent = tsk->parent;
1493 else {
1494 tsk = tsk->group_leader;
1495 parent = tsk->real_parent;
1496 }
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 info.si_signo = SIGCHLD;
1499 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001500 /*
1501 * see comment in do_notify_parent() abot the following 3 lines
1502 */
1503 rcu_read_lock();
Oleg Nesterovd9265662009-06-17 16:27:35 -07001504 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
David Howellsc69e8d92008-11-14 10:39:19 +11001505 info.si_uid = __task_cred(tsk)->uid;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001506 rcu_read_unlock();
1507
Michael Kerriskd8878ba2008-07-25 01:47:32 -07001508 info.si_utime = cputime_to_clock_t(tsk->utime);
1509 info.si_stime = cputime_to_clock_t(tsk->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 info.si_code = why;
1512 switch (why) {
1513 case CLD_CONTINUED:
1514 info.si_status = SIGCONT;
1515 break;
1516 case CLD_STOPPED:
1517 info.si_status = tsk->signal->group_exit_code & 0x7f;
1518 break;
1519 case CLD_TRAPPED:
1520 info.si_status = tsk->exit_code & 0x7f;
1521 break;
1522 default:
1523 BUG();
1524 }
1525
1526 sighand = parent->sighand;
1527 spin_lock_irqsave(&sighand->siglock, flags);
1528 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1529 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1530 __group_send_sig_info(SIGCHLD, &info, parent);
1531 /*
1532 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1533 */
1534 __wake_up_parent(tsk, parent);
1535 spin_unlock_irqrestore(&sighand->siglock, flags);
1536}
1537
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001538static inline int may_ptrace_stop(void)
1539{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001540 if (!likely(task_ptrace(current)))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001541 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001542 /*
1543 * Are we in the middle of do_coredump?
1544 * If so and our tracer is also part of the coredump stopping
1545 * is a deadlock situation, and pointless because our tracer
1546 * is dead so don't allow us to stop.
1547 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001548 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001549 * is safe to enter schedule().
1550 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001551 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001552 unlikely(current->mm == current->parent->mm))
1553 return 0;
1554
1555 return 1;
1556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001559 * Return nonzero if there is a SIGKILL that should be waking us up.
1560 * Called with the siglock held.
1561 */
1562static int sigkill_pending(struct task_struct *tsk)
1563{
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001564 return sigismember(&tsk->pending.signal, SIGKILL) ||
1565 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08001566}
1567
1568/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * This must be called with current->sighand->siglock held.
1570 *
1571 * This should be the path for all ptrace stops.
1572 * We always set current->last_siginfo while stopped here.
1573 * That makes it a way to test a stopped process for
1574 * being ptrace-stopped vs being job-control-stopped.
1575 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001576 * If we actually decide not to stop at all because the tracer
1577 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001579static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Roland McGrath1a669c22008-02-06 01:37:37 -08001581 if (arch_ptrace_stop_needed(exit_code, info)) {
1582 /*
1583 * The arch code has something special to do before a
1584 * ptrace stop. This is allowed to block, e.g. for faults
1585 * on user stack pages. We can't keep the siglock while
1586 * calling arch_ptrace_stop, so we must release it now.
1587 * To preserve proper semantics, we must do this before
1588 * any signal bookkeeping like checking group_stop_count.
1589 * Meanwhile, a SIGKILL could come in before we retake the
1590 * siglock. That must prevent us from sleeping in TASK_TRACED.
1591 * So after regaining the lock, we must check for SIGKILL.
1592 */
1593 spin_unlock_irq(&current->sighand->siglock);
1594 arch_ptrace_stop(exit_code, info);
1595 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001596 if (sigkill_pending(current))
1597 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08001598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /*
1601 * If there is a group stop in progress,
1602 * we must participate in the bookkeeping.
1603 */
1604 if (current->signal->group_stop_count > 0)
1605 --current->signal->group_stop_count;
1606
1607 current->last_siginfo = info;
1608 current->exit_code = exit_code;
1609
1610 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001611 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 spin_unlock_irq(&current->sighand->siglock);
1613 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001614 if (may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001615 do_notify_parent_cldstop(current, CLD_TRAPPED);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001616 /*
1617 * Don't want to allow preemption here, because
1618 * sys_ptrace() needs this task to be inactive.
1619 *
1620 * XXX: implement read_unlock_no_resched().
1621 */
1622 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001624 preempt_enable_no_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 schedule();
1626 } else {
1627 /*
1628 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001629 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001631 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001632 if (clear_code)
1633 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001634 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001638 * While in TASK_TRACED, we were considered "frozen enough".
1639 * Now that we woke up, it's crucial if we're supposed to be
1640 * frozen that we freeze now before running anything substantial.
1641 */
1642 try_to_freeze();
1643
1644 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 * We are back. Now reacquire the siglock before touching
1646 * last_siginfo, so that we are sure to have synchronized with
1647 * any signal-sending on another CPU that wants to examine it.
1648 */
1649 spin_lock_irq(&current->sighand->siglock);
1650 current->last_siginfo = NULL;
1651
1652 /*
1653 * Queued signals ignored us while we were stopped for tracing.
1654 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001655 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001657 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
1660void ptrace_notify(int exit_code)
1661{
1662 siginfo_t info;
1663
1664 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1665
1666 memset(&info, 0, sizeof info);
1667 info.si_signo = SIGTRAP;
1668 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001669 info.si_pid = task_pid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11001670 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 /* Let the debugger run. */
1673 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001674 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 spin_unlock_irq(&current->sighand->siglock);
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * This performs the stopping for SIGSTOP and other stop signals.
1680 * We have to stop all threads in the thread group.
1681 * Returns nonzero if we've actually stopped and released the siglock.
1682 * Returns zero if we didn't stop and still hold the siglock.
1683 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001684static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct signal_struct *sig = current->signal;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001687 int notify;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001689 if (!sig->group_stop_count) {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001690 struct task_struct *t;
1691
Oleg Nesterov2b201a92008-07-25 01:47:31 -07001692 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001693 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001697 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001699 sig->group_exit_code = signr;
1700
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001701 sig->group_stop_count = 1;
Oleg Nesterova122b342006-03-28 16:11:22 -08001702 for (t = next_thread(current); t != current; t = next_thread(t))
1703 /*
1704 * Setting state to TASK_STOPPED for a group
1705 * stop is always done with the siglock held,
1706 * so this check has no races.
1707 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001708 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001709 !task_is_stopped_or_traced(t)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001710 sig->group_stop_count++;
Oleg Nesterova122b342006-03-28 16:11:22 -08001711 signal_wake_up(t, 0);
1712 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001713 }
1714 /*
1715 * If there are no other threads in the group, or if there is
1716 * a group stop in progress and we are the last to stop, report
1717 * to the parent. When ptraced, every thread reports itself.
1718 */
1719 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1720 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1721 /*
1722 * tracehook_notify_jctl() can drop and reacquire siglock, so
1723 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1724 * or SIGKILL comes in between ->group_stop_count == 0.
1725 */
1726 if (sig->group_stop_count) {
1727 if (!--sig->group_stop_count)
1728 sig->flags = SIGNAL_STOP_STOPPED;
1729 current->exit_code = sig->group_exit_code;
1730 __set_current_state(TASK_STOPPED);
1731 }
1732 spin_unlock_irq(&current->sighand->siglock);
1733
1734 if (notify) {
1735 read_lock(&tasklist_lock);
1736 do_notify_parent_cldstop(current, notify);
1737 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 }
1739
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001740 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1741 do {
1742 schedule();
1743 } while (try_to_freeze());
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001744
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001745 tracehook_finish_jctl();
1746 current->exit_code = 0;
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return 1;
1749}
1750
Roland McGrath18c98b62008-04-17 18:44:38 -07001751static int ptrace_signal(int signr, siginfo_t *info,
1752 struct pt_regs *regs, void *cookie)
1753{
Oleg Nesterov5cb11442009-06-17 16:27:30 -07001754 if (!task_ptrace(current))
Roland McGrath18c98b62008-04-17 18:44:38 -07001755 return signr;
1756
1757 ptrace_signal_deliver(regs, cookie);
1758
1759 /* Let the debugger run. */
1760 ptrace_stop(signr, 0, info);
1761
1762 /* We're back. Did the debugger cancel the sig? */
1763 signr = current->exit_code;
1764 if (signr == 0)
1765 return signr;
1766
1767 current->exit_code = 0;
1768
1769 /* Update the siginfo structure if the signal has
1770 changed. If the debugger wanted something
1771 specific in the siginfo structure then it should
1772 have updated *info via PTRACE_SETSIGINFO. */
1773 if (signr != info->si_signo) {
1774 info->si_signo = signr;
1775 info->si_errno = 0;
1776 info->si_code = SI_USER;
1777 info->si_pid = task_pid_vnr(current->parent);
David Howellsc69e8d92008-11-14 10:39:19 +11001778 info->si_uid = task_uid(current->parent);
Roland McGrath18c98b62008-04-17 18:44:38 -07001779 }
1780
1781 /* If the (new) signal is now blocked, requeue it. */
1782 if (sigismember(&current->blocked, signr)) {
1783 specific_send_sig_info(signr, info, current);
1784 signr = 0;
1785 }
1786
1787 return signr;
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1791 struct pt_regs *regs, void *cookie)
1792{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001793 struct sighand_struct *sighand = current->sighand;
1794 struct signal_struct *signal = current->signal;
1795 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001797relock:
1798 /*
1799 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1800 * While in TASK_STOPPED, we were considered "frozen enough".
1801 * Now that we woke up, it's crucial if we're supposed to be
1802 * frozen that we freeze now before running anything substantial.
1803 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08001804 try_to_freeze();
1805
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001806 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07001807 /*
1808 * Every stopped thread goes here after wakeup. Check to see if
1809 * we should notify the parent, prepare_signal(SIGCONT) encodes
1810 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1811 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001812 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1813 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
Oleg Nesterove4420552008-04-30 00:52:44 -07001814 ? CLD_CONTINUED : CLD_STOPPED;
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001815 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001816
1817 why = tracehook_notify_jctl(why, CLD_CONTINUED);
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001818 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07001819
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001820 if (why) {
1821 read_lock(&tasklist_lock);
1822 do_notify_parent_cldstop(current->group_leader, why);
1823 read_unlock(&tasklist_lock);
1824 }
Oleg Nesterove4420552008-04-30 00:52:44 -07001825 goto relock;
1826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 for (;;) {
1829 struct k_sigaction *ka;
1830
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001831 if (unlikely(signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001832 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto relock;
1834
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001835 /*
1836 * Tracing can induce an artifical signal and choose sigaction.
1837 * The return value in @signr determines the default action,
1838 * but @info->si_signo is the signal number we will report.
1839 */
1840 signr = tracehook_get_signal(current, regs, info, return_ka);
1841 if (unlikely(signr < 0))
1842 goto relock;
1843 if (unlikely(signr != 0))
1844 ka = return_ka;
1845 else {
1846 signr = dequeue_signal(current, &current->blocked,
1847 info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Roland McGrath18c98b62008-04-17 18:44:38 -07001849 if (!signr)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001850 break; /* will return 0 */
1851
1852 if (signr != SIGKILL) {
1853 signr = ptrace_signal(signr, info,
1854 regs, cookie);
1855 if (!signr)
1856 continue;
1857 }
1858
1859 ka = &sighand->action[signr-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1863 continue;
1864 if (ka->sa.sa_handler != SIG_DFL) {
1865 /* Run the handler. */
1866 *return_ka = *ka;
1867
1868 if (ka->sa.sa_flags & SA_ONESHOT)
1869 ka->sa.sa_handler = SIG_DFL;
1870
1871 break; /* will return non-zero "signr" value */
1872 }
1873
1874 /*
1875 * Now we are doing the default action for this signal.
1876 */
1877 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1878 continue;
1879
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001880 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001881 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001882 * Container-init gets no signals it doesn't want from same
1883 * container.
1884 *
1885 * Note that if global/container-init sees a sig_kernel_only()
1886 * signal here, the signal must have been generated internally
1887 * or must have come from an ancestor namespace. In either
1888 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001889 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001890 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07001891 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 continue;
1893
1894 if (sig_kernel_stop(signr)) {
1895 /*
1896 * The default action is to stop all threads in
1897 * the thread group. The job control signals
1898 * do nothing in an orphaned pgrp, but SIGSTOP
1899 * always works. Note that siglock needs to be
1900 * dropped during the call to is_orphaned_pgrp()
1901 * because of lock ordering with tasklist_lock.
1902 * This allows an intervening SIGCONT to be posted.
1903 * We need to check for that and bail out if necessary.
1904 */
1905 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001906 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* signals can be posted during this window */
1909
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001910 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto relock;
1912
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001913 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001916 if (likely(do_signal_stop(info->si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* It released the siglock. */
1918 goto relock;
1919 }
1920
1921 /*
1922 * We didn't actually stop, due to a race
1923 * with SIGCONT or something like that.
1924 */
1925 continue;
1926 }
1927
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001928 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /*
1931 * Anything else is fatal, maybe with a core dump.
1932 */
1933 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07001936 if (print_fatal_signals)
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001937 print_fatal_signal(regs, info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * If it was able to dump core, this kills all
1940 * other threads in the group and synchronizes with
1941 * their demise. If we lost the race with another
1942 * thread getting here, it set group_exit_code
1943 * first and our do_group_exit call below will use
1944 * that value and ignore the one we pass it.
1945 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001946 do_coredump(info->si_signo, info->si_signo, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 }
1948
1949 /*
1950 * Death signals, no core dump.
1951 */
Roland McGrath7bcf6a22008-07-25 19:45:53 -07001952 do_group_exit(info->si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /* NOTREACHED */
1954 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07001955 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return signr;
1957}
1958
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001959void exit_signals(struct task_struct *tsk)
1960{
1961 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001962 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001963
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001964 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1965 tsk->flags |= PF_EXITING;
1966 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001967 }
1968
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001969 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001970 /*
1971 * From now this task is not visible for group-wide signals,
1972 * see wants_signal(), do_signal_stop().
1973 */
1974 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001975 if (!signal_pending(tsk))
1976 goto out;
1977
1978 /* It could be that __group_complete_signal() choose us to
1979 * notify about group-wide signal. Another thread should be
1980 * woken now to take the signal since we will not.
1981 */
1982 for (t = tsk; (t = next_thread(t)) != tsk; )
1983 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1984 recalc_sigpending_and_wake(t);
1985
1986 if (unlikely(tsk->signal->group_stop_count) &&
1987 !--tsk->signal->group_stop_count) {
1988 tsk->signal->flags = SIGNAL_STOP_STOPPED;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001989 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001990 }
1991out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001992 spin_unlock_irq(&tsk->sighand->siglock);
1993
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001994 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001995 read_lock(&tasklist_lock);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07001996 do_notify_parent_cldstop(tsk, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001997 read_unlock(&tasklist_lock);
1998 }
1999}
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001EXPORT_SYMBOL(recalc_sigpending);
2002EXPORT_SYMBOL_GPL(dequeue_signal);
2003EXPORT_SYMBOL(flush_signals);
2004EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005EXPORT_SYMBOL(send_sig);
2006EXPORT_SYMBOL(send_sig_info);
2007EXPORT_SYMBOL(sigprocmask);
2008EXPORT_SYMBOL(block_all_signals);
2009EXPORT_SYMBOL(unblock_all_signals);
2010
2011
2012/*
2013 * System call entry points.
2014 */
2015
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002016SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 struct restart_block *restart = &current_thread_info()->restart_block;
2019 return restart->fn(restart);
2020}
2021
2022long do_no_restart_syscall(struct restart_block *param)
2023{
2024 return -EINTR;
2025}
2026
2027/*
2028 * We don't need to get the kernel lock - this is all local to this
2029 * particular thread.. (and that's good, because this is _heavily_
2030 * used by various programs)
2031 */
2032
2033/*
2034 * This is also useful for kernel threads that want to temporarily
2035 * (or permanently) block certain signals.
2036 *
2037 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2038 * interface happily blocks "unblockable" signals like SIGKILL
2039 * and friends.
2040 */
2041int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2042{
2043 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002046 if (oldset)
2047 *oldset = current->blocked;
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 error = 0;
2050 switch (how) {
2051 case SIG_BLOCK:
2052 sigorsets(&current->blocked, &current->blocked, set);
2053 break;
2054 case SIG_UNBLOCK:
2055 signandsets(&current->blocked, &current->blocked, set);
2056 break;
2057 case SIG_SETMASK:
2058 current->blocked = *set;
2059 break;
2060 default:
2061 error = -EINVAL;
2062 }
2063 recalc_sigpending();
2064 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return error;
2067}
2068
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002069SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2070 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071{
2072 int error = -EINVAL;
2073 sigset_t old_set, new_set;
2074
2075 /* XXX: Don't preclude handling different sized sigset_t's. */
2076 if (sigsetsize != sizeof(sigset_t))
2077 goto out;
2078
2079 if (set) {
2080 error = -EFAULT;
2081 if (copy_from_user(&new_set, set, sizeof(*set)))
2082 goto out;
2083 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2084
2085 error = sigprocmask(how, &new_set, &old_set);
2086 if (error)
2087 goto out;
2088 if (oset)
2089 goto set_old;
2090 } else if (oset) {
2091 spin_lock_irq(&current->sighand->siglock);
2092 old_set = current->blocked;
2093 spin_unlock_irq(&current->sighand->siglock);
2094
2095 set_old:
2096 error = -EFAULT;
2097 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2098 goto out;
2099 }
2100 error = 0;
2101out:
2102 return error;
2103}
2104
2105long do_sigpending(void __user *set, unsigned long sigsetsize)
2106{
2107 long error = -EINVAL;
2108 sigset_t pending;
2109
2110 if (sigsetsize > sizeof(sigset_t))
2111 goto out;
2112
2113 spin_lock_irq(&current->sighand->siglock);
2114 sigorsets(&pending, &current->pending.signal,
2115 &current->signal->shared_pending.signal);
2116 spin_unlock_irq(&current->sighand->siglock);
2117
2118 /* Outside the lock because only this thread touches it. */
2119 sigandsets(&pending, &current->blocked, &pending);
2120
2121 error = -EFAULT;
2122 if (!copy_to_user(set, &pending, sigsetsize))
2123 error = 0;
2124
2125out:
2126 return error;
2127}
2128
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002129SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
2131 return do_sigpending(set, sigsetsize);
2132}
2133
2134#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2135
2136int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2137{
2138 int err;
2139
2140 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2141 return -EFAULT;
2142 if (from->si_code < 0)
2143 return __copy_to_user(to, from, sizeof(siginfo_t))
2144 ? -EFAULT : 0;
2145 /*
2146 * If you change siginfo_t structure, please be sure
2147 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002148 * Please remember to update the signalfd_copyinfo() function
2149 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * It should never copy any pad contained in the structure
2151 * to avoid security leaks, but must copy the generic
2152 * 3 ints plus the relevant union member.
2153 */
2154 err = __put_user(from->si_signo, &to->si_signo);
2155 err |= __put_user(from->si_errno, &to->si_errno);
2156 err |= __put_user((short)from->si_code, &to->si_code);
2157 switch (from->si_code & __SI_MASK) {
2158 case __SI_KILL:
2159 err |= __put_user(from->si_pid, &to->si_pid);
2160 err |= __put_user(from->si_uid, &to->si_uid);
2161 break;
2162 case __SI_TIMER:
2163 err |= __put_user(from->si_tid, &to->si_tid);
2164 err |= __put_user(from->si_overrun, &to->si_overrun);
2165 err |= __put_user(from->si_ptr, &to->si_ptr);
2166 break;
2167 case __SI_POLL:
2168 err |= __put_user(from->si_band, &to->si_band);
2169 err |= __put_user(from->si_fd, &to->si_fd);
2170 break;
2171 case __SI_FAULT:
2172 err |= __put_user(from->si_addr, &to->si_addr);
2173#ifdef __ARCH_SI_TRAPNO
2174 err |= __put_user(from->si_trapno, &to->si_trapno);
2175#endif
2176 break;
2177 case __SI_CHLD:
2178 err |= __put_user(from->si_pid, &to->si_pid);
2179 err |= __put_user(from->si_uid, &to->si_uid);
2180 err |= __put_user(from->si_status, &to->si_status);
2181 err |= __put_user(from->si_utime, &to->si_utime);
2182 err |= __put_user(from->si_stime, &to->si_stime);
2183 break;
2184 case __SI_RT: /* This is not generated by the kernel as of now. */
2185 case __SI_MESGQ: /* But this is */
2186 err |= __put_user(from->si_pid, &to->si_pid);
2187 err |= __put_user(from->si_uid, &to->si_uid);
2188 err |= __put_user(from->si_ptr, &to->si_ptr);
2189 break;
2190 default: /* this is just in case for now ... */
2191 err |= __put_user(from->si_pid, &to->si_pid);
2192 err |= __put_user(from->si_uid, &to->si_uid);
2193 break;
2194 }
2195 return err;
2196}
2197
2198#endif
2199
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002200SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2201 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2202 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 int ret, sig;
2205 sigset_t these;
2206 struct timespec ts;
2207 siginfo_t info;
2208 long timeout = 0;
2209
2210 /* XXX: Don't preclude handling different sized sigset_t's. */
2211 if (sigsetsize != sizeof(sigset_t))
2212 return -EINVAL;
2213
2214 if (copy_from_user(&these, uthese, sizeof(these)))
2215 return -EFAULT;
2216
2217 /*
2218 * Invert the set of allowed signals to get those we
2219 * want to block.
2220 */
2221 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2222 signotset(&these);
2223
2224 if (uts) {
2225 if (copy_from_user(&ts, uts, sizeof(ts)))
2226 return -EFAULT;
2227 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2228 || ts.tv_sec < 0)
2229 return -EINVAL;
2230 }
2231
2232 spin_lock_irq(&current->sighand->siglock);
2233 sig = dequeue_signal(current, &these, &info);
2234 if (!sig) {
2235 timeout = MAX_SCHEDULE_TIMEOUT;
2236 if (uts)
2237 timeout = (timespec_to_jiffies(&ts)
2238 + (ts.tv_sec || ts.tv_nsec));
2239
2240 if (timeout) {
2241 /* None ready -- temporarily unblock those we're
2242 * interested while we are sleeping in so that we'll
2243 * be awakened when they arrive. */
2244 current->real_blocked = current->blocked;
2245 sigandsets(&current->blocked, &current->blocked, &these);
2246 recalc_sigpending();
2247 spin_unlock_irq(&current->sighand->siglock);
2248
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002249 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 spin_lock_irq(&current->sighand->siglock);
2252 sig = dequeue_signal(current, &these, &info);
2253 current->blocked = current->real_blocked;
2254 siginitset(&current->real_blocked, 0);
2255 recalc_sigpending();
2256 }
2257 }
2258 spin_unlock_irq(&current->sighand->siglock);
2259
2260 if (sig) {
2261 ret = sig;
2262 if (uinfo) {
2263 if (copy_siginfo_to_user(uinfo, &info))
2264 ret = -EFAULT;
2265 }
2266 } else {
2267 ret = -EAGAIN;
2268 if (timeout)
2269 ret = -EINTR;
2270 }
2271
2272 return ret;
2273}
2274
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002275SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct siginfo info;
2278
2279 info.si_signo = sig;
2280 info.si_errno = 0;
2281 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002282 info.si_pid = task_tgid_vnr(current);
David Howells76aac0e2008-11-14 10:39:12 +11002283 info.si_uid = current_uid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 return kill_something_info(sig, &info, pid);
2286}
2287
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002288static int
2289do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002290{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002291 struct task_struct *p;
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002292 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002293
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002294 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002295 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002296 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002297 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002298 /*
2299 * The null signal is a permissions and process existence
2300 * probe. No signal is actually delivered.
2301 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07002302 if (!error && sig) {
2303 error = do_send_sig_info(sig, info, p, false);
2304 /*
2305 * If lock_task_sighand() failed we pretend the task
2306 * dies after receiving the signal. The window is tiny,
2307 * and the signal is private anyway.
2308 */
2309 if (unlikely(error == -ESRCH))
2310 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002311 }
2312 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07002313 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002314
2315 return error;
2316}
2317
Thomas Gleixner30b4ae8a2009-04-04 21:01:01 +00002318static int do_tkill(pid_t tgid, pid_t pid, int sig)
2319{
2320 struct siginfo info;
2321
2322 info.si_signo = sig;
2323 info.si_errno = 0;
2324 info.si_code = SI_TKILL;
2325 info.si_pid = task_tgid_vnr(current);
2326 info.si_uid = current_uid();
2327
2328 return do_send_specific(tgid, pid, sig, &info);
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331/**
2332 * sys_tgkill - send signal to one specific thread
2333 * @tgid: the thread group ID of the thread
2334 * @pid: the PID of the thread
2335 * @sig: signal to be sent
2336 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002337 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 * exists but it's not belonging to the target process anymore. This
2339 * method solves the problem of threads exiting and PIDs getting reused.
2340 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002341SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /* This is only valid for single tasks */
2344 if (pid <= 0 || tgid <= 0)
2345 return -EINVAL;
2346
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002347 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350/*
2351 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2352 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002353SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /* This is only valid for single tasks */
2356 if (pid <= 0)
2357 return -EINVAL;
2358
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002359 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002362SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2363 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364{
2365 siginfo_t info;
2366
2367 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2368 return -EFAULT;
2369
2370 /* Not even root can pretend to send signals from the kernel.
2371 Nor can they impersonate a kill(), which adds source info. */
2372 if (info.si_code >= 0)
2373 return -EPERM;
2374 info.si_signo = sig;
2375
2376 /* POSIX.1b doesn't mention process groups. */
2377 return kill_proc_info(sig, &info, pid);
2378}
2379
Thomas Gleixner62ab4502009-04-04 21:01:06 +00002380long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2381{
2382 /* This is only valid for single tasks */
2383 if (pid <= 0 || tgid <= 0)
2384 return -EINVAL;
2385
2386 /* Not even root can pretend to send signals from the kernel.
2387 Nor can they impersonate a kill(), which adds source info. */
2388 if (info->si_code >= 0)
2389 return -EPERM;
2390 info->si_signo = sig;
2391
2392 return do_send_specific(tgid, pid, sig, info);
2393}
2394
2395SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2396 siginfo_t __user *, uinfo)
2397{
2398 siginfo_t info;
2399
2400 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2401 return -EFAULT;
2402
2403 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2404}
2405
Oleg Nesterov88531f72006-03-28 16:11:24 -08002406int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002408 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002410 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002412 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return -EINVAL;
2414
Pavel Emelyanov93585ee2008-04-30 00:52:39 -07002415 k = &t->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (oact)
2419 *oact = *k;
2420
2421 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002422 sigdelsetmask(&act->sa.sa_mask,
2423 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002424 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /*
2426 * POSIX 3.3.1.3:
2427 * "Setting a signal action to SIG_IGN for a signal that is
2428 * pending shall cause the pending signal to be discarded,
2429 * whether or not it is blocked."
2430 *
2431 * "Setting a signal action to SIG_DFL for a signal that is
2432 * pending and whose default action is to ignore the signal
2433 * (for example, SIGCHLD), shall cause the pending signal to
2434 * be discarded, whether or not it is blocked"
2435 */
Roland McGrath35de2542008-07-25 19:45:51 -07002436 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08002437 sigemptyset(&mask);
2438 sigaddset(&mask, sig);
2439 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002441 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 t = next_thread(t);
2443 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
2447 spin_unlock_irq(&current->sighand->siglock);
2448 return 0;
2449}
2450
2451int
2452do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2453{
2454 stack_t oss;
2455 int error;
2456
Linus Torvalds0083fc22009-08-01 10:34:56 -07002457 oss.ss_sp = (void __user *) current->sas_ss_sp;
2458 oss.ss_size = current->sas_ss_size;
2459 oss.ss_flags = sas_ss_flags(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 if (uss) {
2462 void __user *ss_sp;
2463 size_t ss_size;
2464 int ss_flags;
2465
2466 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07002467 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2468 goto out;
2469 error = __get_user(ss_sp, &uss->ss_sp) |
2470 __get_user(ss_flags, &uss->ss_flags) |
2471 __get_user(ss_size, &uss->ss_size);
2472 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 goto out;
2474
2475 error = -EPERM;
2476 if (on_sig_stack(sp))
2477 goto out;
2478
2479 error = -EINVAL;
2480 /*
2481 *
2482 * Note - this code used to test ss_flags incorrectly
2483 * old code may have been written using ss_flags==0
2484 * to mean ss_flags==SS_ONSTACK (as this was the only
2485 * way that worked) - this fix preserves that older
2486 * mechanism
2487 */
2488 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2489 goto out;
2490
2491 if (ss_flags == SS_DISABLE) {
2492 ss_size = 0;
2493 ss_sp = NULL;
2494 } else {
2495 error = -ENOMEM;
2496 if (ss_size < MINSIGSTKSZ)
2497 goto out;
2498 }
2499
2500 current->sas_ss_sp = (unsigned long) ss_sp;
2501 current->sas_ss_size = ss_size;
2502 }
2503
Linus Torvalds0083fc22009-08-01 10:34:56 -07002504 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (uoss) {
2506 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002507 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07002509 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2510 __put_user(oss.ss_size, &uoss->ss_size) |
2511 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514out:
2515 return error;
2516}
2517
2518#ifdef __ARCH_WANT_SYS_SIGPENDING
2519
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002520SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 return do_sigpending(set, sizeof(*set));
2523}
2524
2525#endif
2526
2527#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2528/* Some platforms have their own version with special arguments others
2529 support only sys_rt_sigprocmask. */
2530
Heiko Carstensb290ebe2009-01-14 14:14:06 +01002531SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2532 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
2534 int error;
2535 old_sigset_t old_set, new_set;
2536
2537 if (set) {
2538 error = -EFAULT;
2539 if (copy_from_user(&new_set, set, sizeof(*set)))
2540 goto out;
2541 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2542
2543 spin_lock_irq(&current->sighand->siglock);
2544 old_set = current->blocked.sig[0];
2545
2546 error = 0;
2547 switch (how) {
2548 default:
2549 error = -EINVAL;
2550 break;
2551 case SIG_BLOCK:
2552 sigaddsetmask(&current->blocked, new_set);
2553 break;
2554 case SIG_UNBLOCK:
2555 sigdelsetmask(&current->blocked, new_set);
2556 break;
2557 case SIG_SETMASK:
2558 current->blocked.sig[0] = new_set;
2559 break;
2560 }
2561
2562 recalc_sigpending();
2563 spin_unlock_irq(&current->sighand->siglock);
2564 if (error)
2565 goto out;
2566 if (oset)
2567 goto set_old;
2568 } else if (oset) {
2569 old_set = current->blocked.sig[0];
2570 set_old:
2571 error = -EFAULT;
2572 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2573 goto out;
2574 }
2575 error = 0;
2576out:
2577 return error;
2578}
2579#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2580
2581#ifdef __ARCH_WANT_SYS_RT_SIGACTION
Heiko Carstensd4e82042009-01-14 14:14:34 +01002582SYSCALL_DEFINE4(rt_sigaction, int, sig,
2583 const struct sigaction __user *, act,
2584 struct sigaction __user *, oact,
2585 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 struct k_sigaction new_sa, old_sa;
2588 int ret = -EINVAL;
2589
2590 /* XXX: Don't preclude handling different sized sigset_t's. */
2591 if (sigsetsize != sizeof(sigset_t))
2592 goto out;
2593
2594 if (act) {
2595 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2596 return -EFAULT;
2597 }
2598
2599 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2600
2601 if (!ret && oact) {
2602 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2603 return -EFAULT;
2604 }
2605out:
2606 return ret;
2607}
2608#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2609
2610#ifdef __ARCH_WANT_SYS_SGETMASK
2611
2612/*
2613 * For backwards compatibility. Functionality superseded by sigprocmask.
2614 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002615SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 /* SMP safe */
2618 return current->blocked.sig[0];
2619}
2620
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002621SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
2623 int old;
2624
2625 spin_lock_irq(&current->sighand->siglock);
2626 old = current->blocked.sig[0];
2627
2628 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2629 sigmask(SIGSTOP)));
2630 recalc_sigpending();
2631 spin_unlock_irq(&current->sighand->siglock);
2632
2633 return old;
2634}
2635#endif /* __ARCH_WANT_SGETMASK */
2636
2637#ifdef __ARCH_WANT_SYS_SIGNAL
2638/*
2639 * For backwards compatibility. Functionality superseded by sigaction.
2640 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002641SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 struct k_sigaction new_sa, old_sa;
2644 int ret;
2645
2646 new_sa.sa.sa_handler = handler;
2647 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03002648 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 ret = do_sigaction(sig, &new_sa, &old_sa);
2651
2652 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2653}
2654#endif /* __ARCH_WANT_SYS_SIGNAL */
2655
2656#ifdef __ARCH_WANT_SYS_PAUSE
2657
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01002658SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659{
2660 current->state = TASK_INTERRUPTIBLE;
2661 schedule();
2662 return -ERESTARTNOHAND;
2663}
2664
2665#endif
2666
David Woodhouse150256d2006-01-18 17:43:57 -08002667#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
Heiko Carstensd4e82042009-01-14 14:14:34 +01002668SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08002669{
2670 sigset_t newset;
2671
2672 /* XXX: Don't preclude handling different sized sigset_t's. */
2673 if (sigsetsize != sizeof(sigset_t))
2674 return -EINVAL;
2675
2676 if (copy_from_user(&newset, unewset, sizeof(newset)))
2677 return -EFAULT;
2678 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2679
2680 spin_lock_irq(&current->sighand->siglock);
2681 current->saved_sigmask = current->blocked;
2682 current->blocked = newset;
2683 recalc_sigpending();
2684 spin_unlock_irq(&current->sighand->siglock);
2685
2686 current->state = TASK_INTERRUPTIBLE;
2687 schedule();
Roland McGrath4e4c22c2008-04-30 00:53:06 -07002688 set_restore_sigmask();
David Woodhouse150256d2006-01-18 17:43:57 -08002689 return -ERESTARTNOHAND;
2690}
2691#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2692
David Howellsf269fdd2006-09-27 01:50:23 -07002693__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2694{
2695 return NULL;
2696}
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698void __init signals_init(void)
2699{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002700 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}