blob: 4a45bac2c63266f21fc3517cda9c1f3ef5a68c1e [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>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080025#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080026#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080027#include <linux/pid_namespace.h>
28#include <linux/nsproxy.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/param.h>
31#include <asm/uaccess.h>
32#include <asm/unistd.h>
33#include <asm/siginfo.h>
Al Viroe1396062006-05-25 10:19:47 -040034#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36/*
37 * SLAB caches for signal bits.
38 */
39
Christoph Lametere18b8902006-12-06 20:33:20 -080040static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static int sig_ignored(struct task_struct *t, int sig)
44{
45 void __user * handler;
46
47 /*
48 * Tracers always want to know about signals..
49 */
50 if (t->ptrace & PT_PTRACED)
51 return 0;
52
53 /*
54 * Blocked signals are never ignored, since the
55 * signal handler may change by the time it is
56 * unblocked.
57 */
Roland McGrath325d22d2007-11-12 15:41:55 -080058 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return 0;
60
61 /* Is it explicitly or implicitly ignored? */
62 handler = t->sighand->action[sig-1].sa.sa_handler;
63 return handler == SIG_IGN ||
64 (handler == SIG_DFL && sig_kernel_ignore(sig));
65}
66
67/*
68 * Re-calculate pending state from the set of locally pending
69 * signals, globally pending signals, and blocked signals.
70 */
71static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
72{
73 unsigned long ready;
74 long i;
75
76 switch (_NSIG_WORDS) {
77 default:
78 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
79 ready |= signal->sig[i] &~ blocked->sig[i];
80 break;
81
82 case 4: ready = signal->sig[3] &~ blocked->sig[3];
83 ready |= signal->sig[2] &~ blocked->sig[2];
84 ready |= signal->sig[1] &~ blocked->sig[1];
85 ready |= signal->sig[0] &~ blocked->sig[0];
86 break;
87
88 case 2: ready = signal->sig[1] &~ blocked->sig[1];
89 ready |= signal->sig[0] &~ blocked->sig[0];
90 break;
91
92 case 1: ready = signal->sig[0] &~ blocked->sig[0];
93 }
94 return ready != 0;
95}
96
97#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
98
Roland McGrath7bb44ad2007-05-23 13:57:44 -070099static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 if (t->signal->group_stop_count > 0 ||
102 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700103 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700105 return 1;
106 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700107 /*
108 * We must never clear the flag in another thread, or in current
109 * when it's possible the current syscall is returning -ERESTART*.
110 * So we don't clear it here, and only callers who know they should do.
111 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700112 return 0;
113}
114
115/*
116 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
117 * This is superfluous when called on current, the wakeup is a harmless no-op.
118 */
119void recalc_sigpending_and_wake(struct task_struct *t)
120{
121 if (recalc_sigpending_tsk(t))
122 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125void recalc_sigpending(void)
126{
Rafael J. Wysockicc5f9162007-10-29 14:37:12 -0700127 if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700128 clear_thread_flag(TIF_SIGPENDING);
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132/* Given the mask, find the first available signal that should be serviced. */
133
Davide Libenzifba2afa2007-05-10 22:23:13 -0700134int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 unsigned long i, *s, *m, x;
137 int sig = 0;
138
139 s = pending->signal.sig;
140 m = mask->sig;
141 switch (_NSIG_WORDS) {
142 default:
143 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
144 if ((x = *s &~ *m) != 0) {
145 sig = ffz(~x) + i*_NSIG_BPW + 1;
146 break;
147 }
148 break;
149
150 case 2: if ((x = s[0] &~ m[0]) != 0)
151 sig = 1;
152 else if ((x = s[1] &~ m[1]) != 0)
153 sig = _NSIG_BPW + 1;
154 else
155 break;
156 sig += ffz(~x);
157 break;
158
159 case 1: if ((x = *s &~ *m) != 0)
160 sig = ffz(~x) + 1;
161 break;
162 }
163
164 return sig;
165}
166
Al Virodd0fc662005-10-07 07:46:04 +0100167static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 int override_rlimit)
169{
170 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800171 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800173 /*
174 * In order to avoid problems with "switch_user()", we want to make
175 * sure that the compiler doesn't re-load "t->user"
176 */
177 user = t->user;
178 barrier();
179 atomic_inc(&user->sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800181 atomic_read(&user->sigpending) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
183 q = kmem_cache_alloc(sigqueue_cachep, flags);
184 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800185 atomic_dec(&user->sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 } else {
187 INIT_LIST_HEAD(&q->list);
188 q->flags = 0;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800189 q->user = get_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 return(q);
192}
193
Andrew Morton514a01b2006-02-03 03:04:41 -0800194static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 if (q->flags & SIGQUEUE_PREALLOC)
197 return;
198 atomic_dec(&q->user->sigpending);
199 free_uid(q->user);
200 kmem_cache_free(sigqueue_cachep, q);
201}
202
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800203void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct sigqueue *q;
206
207 sigemptyset(&queue->signal);
208 while (!list_empty(&queue->list)) {
209 q = list_entry(queue->list.next, struct sigqueue , list);
210 list_del_init(&q->list);
211 __sigqueue_free(q);
212 }
213}
214
215/*
216 * Flush all pending signals for a task.
217 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800218void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 unsigned long flags;
221
222 spin_lock_irqsave(&t->sighand->siglock, flags);
Pavel Machekf5264482008-04-21 22:15:06 +0000223 clear_tsk_thread_flag(t, TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 flush_sigqueue(&t->pending);
225 flush_sigqueue(&t->signal->shared_pending);
226 spin_unlock_irqrestore(&t->sighand->siglock, flags);
227}
228
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700229void ignore_signals(struct task_struct *t)
230{
231 int i;
232
233 for (i = 0; i < _NSIG; ++i)
234 t->sighand->action[i].sa.sa_handler = SIG_IGN;
235
236 flush_signals(t);
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * Flush all handlers for a task.
241 */
242
243void
244flush_signal_handlers(struct task_struct *t, int force_default)
245{
246 int i;
247 struct k_sigaction *ka = &t->sighand->action[0];
248 for (i = _NSIG ; i != 0 ; i--) {
249 if (force_default || ka->sa.sa_handler != SIG_IGN)
250 ka->sa.sa_handler = SIG_DFL;
251 ka->sa.sa_flags = 0;
252 sigemptyset(&ka->sa.sa_mask);
253 ka++;
254 }
255}
256
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200257int unhandled_signal(struct task_struct *tsk, int sig)
258{
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700259 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200260 return 1;
261 if (tsk->ptrace & PT_PTRACED)
262 return 0;
263 return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) ||
264 (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL);
265}
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268/* Notify the system that a driver wants to block all signals for this
269 * process, and wants to be notified if any signals at all were to be
270 * sent/acted upon. If the notifier routine returns non-zero, then the
271 * signal will be acted upon after all. If the notifier routine returns 0,
272 * then then signal will be blocked. Only one block per process is
273 * allowed. priv is a pointer to private data that the notifier routine
274 * can use to determine if the signal should be blocked or not. */
275
276void
277block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
278{
279 unsigned long flags;
280
281 spin_lock_irqsave(&current->sighand->siglock, flags);
282 current->notifier_mask = mask;
283 current->notifier_data = priv;
284 current->notifier = notifier;
285 spin_unlock_irqrestore(&current->sighand->siglock, flags);
286}
287
288/* Notify the system that blocking has ended. */
289
290void
291unblock_all_signals(void)
292{
293 unsigned long flags;
294
295 spin_lock_irqsave(&current->sighand->siglock, flags);
296 current->notifier = NULL;
297 current->notifier_data = NULL;
298 recalc_sigpending();
299 spin_unlock_irqrestore(&current->sighand->siglock, flags);
300}
301
Arjan van de Ven858119e2006-01-14 13:20:43 -0800302static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct sigqueue *q, *first = NULL;
305 int still_pending = 0;
306
307 if (unlikely(!sigismember(&list->signal, sig)))
308 return 0;
309
310 /*
311 * Collect the siginfo appropriate to this signal. Check if
312 * there is another siginfo for the same signal.
313 */
314 list_for_each_entry(q, &list->list, list) {
315 if (q->info.si_signo == sig) {
316 if (first) {
317 still_pending = 1;
318 break;
319 }
320 first = q;
321 }
322 }
323 if (first) {
324 list_del_init(&first->list);
325 copy_siginfo(info, &first->info);
326 __sigqueue_free(first);
327 if (!still_pending)
328 sigdelset(&list->signal, sig);
329 } else {
330
331 /* Ok, it wasn't in the queue. This must be
332 a fast-pathed signal or we must have been
333 out of queue space. So zero out the info.
334 */
335 sigdelset(&list->signal, sig);
336 info->si_signo = sig;
337 info->si_errno = 0;
338 info->si_code = 0;
339 info->si_pid = 0;
340 info->si_uid = 0;
341 }
342 return 1;
343}
344
345static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
346 siginfo_t *info)
347{
Roland McGrath27d91e02006-09-29 02:00:31 -0700348 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (sig) {
351 if (current->notifier) {
352 if (sigismember(current->notifier_mask, sig)) {
353 if (!(current->notifier)(current->notifier_data)) {
354 clear_thread_flag(TIF_SIGPENDING);
355 return 0;
356 }
357 }
358 }
359
360 if (!collect_signal(sig, pending, info))
361 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 return sig;
365}
366
367/*
368 * Dequeue a signal and return the element to the caller, which is
369 * expected to free it.
370 *
371 * All callers have to hold the siglock.
372 */
373int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
374{
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000375 int signr = 0;
376
377 /* We only dequeue private signals from ourselves, we don't let
378 * signalfd steal them
379 */
Davide Libenzib8fceee2007-09-20 12:40:16 -0700380 signr = __dequeue_signal(&tsk->pending, mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800381 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 signr = __dequeue_signal(&tsk->signal->shared_pending,
383 mask, info);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800384 /*
385 * itimer signal ?
386 *
387 * itimers are process shared and we restart periodic
388 * itimers in the signal delivery path to prevent DoS
389 * attacks in the high resolution timer case. This is
390 * compliant with the old way of self restarting
391 * itimers, as the SIGALRM is a legacy signal and only
392 * queued once. Changing the restart behaviour to
393 * restart the timer in the signal dequeue path is
394 * reducing the timer noise on heavy loaded !highres
395 * systems too.
396 */
397 if (unlikely(signr == SIGALRM)) {
398 struct hrtimer *tmr = &tsk->signal->real_timer;
399
400 if (!hrtimer_is_queued(tmr) &&
401 tsk->signal->it_real_incr.tv64 != 0) {
402 hrtimer_forward(tmr, tmr->base->get_time(),
403 tsk->signal->it_real_incr);
404 hrtimer_restart(tmr);
405 }
406 }
407 }
Davide Libenzib8fceee2007-09-20 12:40:16 -0700408 recalc_sigpending();
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800409 if (signr && unlikely(sig_kernel_stop(signr))) {
410 /*
411 * Set a marker that we have dequeued a stop signal. Our
412 * caller might release the siglock and then the pending
413 * stop signal it is about to process is no longer in the
414 * pending bitmasks, but must still be cleared by a SIGCONT
415 * (and overruled by a SIGKILL). So those cases clear this
416 * shared flag after we've set it. Note that this flag may
417 * remain set after the signal we return is ignored or
418 * handled. That doesn't matter because its only purpose
419 * is to alert stop-signal processing code when another
420 * processor has come along and cleared the flag.
421 */
422 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
423 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
424 }
Davide Libenzib8fceee2007-09-20 12:40:16 -0700425 if (signr &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
Pavel Machekf5264482008-04-21 22:15:06 +0000427 info->si_sys_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /*
429 * Release the siglock to ensure proper locking order
430 * of timer locks outside of siglocks. Note, we leave
431 * irqs disabled here, since the posix-timers code is
432 * about to disable them again anyway.
433 */
434 spin_unlock(&tsk->sighand->siglock);
435 do_schedule_next_timer(info);
436 spin_lock(&tsk->sighand->siglock);
437 }
438 return signr;
439}
440
441/*
442 * Tell a process that it has a new active signal..
443 *
444 * NOTE! we rely on the previous spin_lock to
445 * lock interrupts for us! We can only be called with
446 * "siglock" held, and the local interrupt must
447 * have been disabled when that got acquired!
448 *
449 * No need to set need_resched since signal event passing
450 * goes through ->blocked
451 */
452void signal_wake_up(struct task_struct *t, int resume)
453{
454 unsigned int mask;
455
456 set_tsk_thread_flag(t, TIF_SIGPENDING);
457
458 /*
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500459 * For SIGKILL, we want to wake it up in the stopped/traced/killable
460 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 * executing another processor and just now entering stopped state.
462 * By using wake_up_state, we ensure the process will wake up and
463 * handle its death signal.
464 */
465 mask = TASK_INTERRUPTIBLE;
466 if (resume)
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500467 mask |= TASK_WAKEKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!wake_up_state(t, mask))
469 kick_process(t);
470}
471
472/*
473 * Remove signals in mask from the pending set and queue.
474 * Returns 1 if any signals were found.
475 *
476 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800477 *
478 * This version takes a sigset mask and looks at all signals,
479 * not just those in the first mask word.
480 */
481static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
482{
483 struct sigqueue *q, *n;
484 sigset_t m;
485
486 sigandsets(&m, mask, &s->signal);
487 if (sigisemptyset(&m))
488 return 0;
489
490 signandsets(&s->signal, &s->signal, mask);
491 list_for_each_entry_safe(q, n, &s->list, list) {
492 if (sigismember(mask, q->info.si_signo)) {
493 list_del_init(&q->list);
494 __sigqueue_free(q);
495 }
496 }
497 return 1;
498}
499/*
500 * Remove signals in mask from the pending set and queue.
501 * Returns 1 if any signals were found.
502 *
503 * All callers must be holding the siglock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505static int rm_from_queue(unsigned long mask, struct sigpending *s)
506{
507 struct sigqueue *q, *n;
508
509 if (!sigtestsetmask(&s->signal, mask))
510 return 0;
511
512 sigdelsetmask(&s->signal, mask);
513 list_for_each_entry_safe(q, n, &s->list, list) {
514 if (q->info.si_signo < SIGRTMIN &&
515 (mask & sigmask(q->info.si_signo))) {
516 list_del_init(&q->list);
517 __sigqueue_free(q);
518 }
519 }
520 return 1;
521}
522
523/*
524 * Bad permissions for sending the signal
525 */
526static int check_kill_permission(int sig, struct siginfo *info,
527 struct task_struct *t)
528{
529 int error = -EINVAL;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700530 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400532
Al Viro291041e2007-10-07 00:24:36 -0700533 if (info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info))) {
534 error = audit_signal_info(sig, t); /* Let audit system see the signal */
535 if (error)
536 return error;
537 error = -EPERM;
538 if (((sig != SIGCONT) ||
Pavel Emelianova47afb02007-10-18 23:39:46 -0700539 (task_session_nr(current) != task_session_nr(t)))
Al Viro291041e2007-10-07 00:24:36 -0700540 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
541 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
542 && !capable(CAP_KILL))
Amy Griffise54dc242007-03-29 18:01:04 -0400543 return error;
Al Viro291041e2007-10-07 00:24:36 -0700544 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100545
Amy Griffise54dc242007-03-29 18:01:04 -0400546 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/* forward decl */
Oleg Nesterova1d5e212006-03-28 16:11:29 -0800550static void do_notify_parent_cldstop(struct task_struct *tsk, int why);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552/*
553 * Handle magic process-wide effects of stop/continue signals.
554 * Unlike the signal actions, these happen immediately at signal-generation
555 * time regardless of blocking, ignoring, or handling. This does the
556 * actual continuing for SIGCONT, but not the actual stopping for stop
557 * signals. The process stop is done as a signal action for SIG_DFL.
558 */
559static void handle_stop_signal(int sig, struct task_struct *p)
560{
561 struct task_struct *t;
562
Bhavesh P. Davdadd12f482005-08-17 12:26:33 -0600563 if (p->signal->flags & SIGNAL_GROUP_EXIT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * The process is in the middle of dying already.
566 */
567 return;
568
569 if (sig_kernel_stop(sig)) {
570 /*
571 * This is a stop signal. Remove SIGCONT from all queues.
572 */
573 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
574 t = p;
575 do {
576 rm_from_queue(sigmask(SIGCONT), &t->pending);
577 t = next_thread(t);
578 } while (t != p);
579 } else if (sig == SIGCONT) {
580 /*
581 * Remove all stop signals from all queues,
582 * and wake all threads.
583 */
584 if (unlikely(p->signal->group_stop_count > 0)) {
585 /*
586 * There was a group stop in progress. We'll
587 * pretend it finished before we got here. We are
588 * obliged to report it to the parent: if the
589 * SIGSTOP happened "after" this SIGCONT, then it
590 * would have cleared this pending SIGCONT. If it
591 * happened "before" this SIGCONT, then the parent
592 * got the SIGCHLD about the stop finishing before
593 * the continue happened. We do the notification
594 * now, and it's as if the stop had finished and
595 * the SIGCHLD was pending on entry to this kill.
596 */
597 p->signal->group_stop_count = 0;
598 p->signal->flags = SIGNAL_STOP_CONTINUED;
599 spin_unlock(&p->sighand->siglock);
Oleg Nesterova1d5e212006-03-28 16:11:29 -0800600 do_notify_parent_cldstop(p, CLD_STOPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 spin_lock(&p->sighand->siglock);
602 }
603 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
604 t = p;
605 do {
606 unsigned int state;
607 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
608
609 /*
610 * If there is a handler for SIGCONT, we must make
611 * sure that no thread returns to user mode before
612 * we post the signal, in case it was the only
613 * thread eligible to run the signal handler--then
614 * it must not do anything between resuming and
615 * running the handler. With the TIF_SIGPENDING
616 * flag set, the thread will pause and acquire the
617 * siglock that we hold now and until we've queued
618 * the pending signal.
619 *
620 * Wake up the stopped thread _after_ setting
621 * TIF_SIGPENDING
622 */
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500623 state = __TASK_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
625 set_tsk_thread_flag(t, TIF_SIGPENDING);
626 state |= TASK_INTERRUPTIBLE;
627 }
628 wake_up_state(t, state);
629
630 t = next_thread(t);
631 } while (t != p);
632
633 if (p->signal->flags & SIGNAL_STOP_STOPPED) {
634 /*
635 * We were in fact stopped, and are now continued.
636 * Notify the parent with CLD_CONTINUED.
637 */
638 p->signal->flags = SIGNAL_STOP_CONTINUED;
639 p->signal->group_exit_code = 0;
640 spin_unlock(&p->sighand->siglock);
Oleg Nesterova1d5e212006-03-28 16:11:29 -0800641 do_notify_parent_cldstop(p, CLD_CONTINUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 spin_lock(&p->sighand->siglock);
643 } else {
644 /*
645 * We are not stopped, but there could be a stop
646 * signal in the middle of being processed after
647 * being removed from the queue. Clear that too.
648 */
649 p->signal->flags = 0;
650 }
651 } else if (sig == SIGKILL) {
652 /*
653 * Make sure that any pending stop signal already dequeued
654 * is undone by the wakeup for SIGKILL.
655 */
656 p->signal->flags = 0;
657 }
658}
659
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -0700660static inline int legacy_queue(struct sigpending *signals, int sig)
661{
662 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
663}
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
666 struct sigpending *signals)
667{
668 struct sigqueue * q = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700671 * Short-circuit ignored signals and support queuing
672 * exactly one non-rt signal, so that we can get more
673 * detailed information about the cause of the signal.
674 */
675 if (sig_ignored(t, sig) || legacy_queue(signals, sig))
676 return 0;
677
678 /*
Davide Libenzifba2afa2007-05-10 22:23:13 -0700679 * Deliver the signal to listening signalfds. This must be called
680 * with the sighand lock held.
681 */
682 signalfd_notify(t, sig);
683
684 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * fast-pathed signals for kernel-internal things like SIGSTOP
686 * or SIGKILL.
687 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800688 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto out_set;
690
691 /* Real-time signals must be queued if sent by sigqueue, or
692 some other real-time mechanism. It is implementation
693 defined whether kill() does so. We attempt to do so, on
694 the principle of least surprise, but since kill is not
695 allowed to fail with EAGAIN when low on memory we just
696 make sure at least one signal gets delivered and don't
697 pass on the info struct. */
698
699 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
Oleg Nesterov621d3122005-10-30 15:03:45 -0800700 (is_si_special(info) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 info->si_code >= 0)));
702 if (q) {
703 list_add_tail(&q->list, &signals->list);
704 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800705 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 q->info.si_signo = sig;
707 q->info.si_errno = 0;
708 q->info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -0700709 q->info.si_pid = task_pid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 q->info.si_uid = current->uid;
711 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -0800712 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 q->info.si_signo = sig;
714 q->info.si_errno = 0;
715 q->info.si_code = SI_KERNEL;
716 q->info.si_pid = 0;
717 q->info.si_uid = 0;
718 break;
719 default:
720 copy_siginfo(&q->info, info);
721 break;
722 }
Oleg Nesterov621d3122005-10-30 15:03:45 -0800723 } else if (!is_si_special(info)) {
724 if (sig >= SIGRTMIN && info->si_code != SI_USER)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /*
726 * Queue overflow, abort. We may abort if the signal was rt
727 * and sent by user using something other than kill().
728 */
729 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732out_set:
733 sigaddset(&signals->signal, sig);
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700734 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Ingo Molnar45807a12007-07-15 23:40:10 -0700737int print_fatal_signals;
738
739static void print_fatal_signal(struct pt_regs *regs, int signr)
740{
741 printk("%s/%d: potentially unexpected fatal signal %d.\n",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -0700742 current->comm, task_pid_nr(current), signr);
Ingo Molnar45807a12007-07-15 23:40:10 -0700743
Al Viroca5cd872007-10-29 04:31:16 +0000744#if defined(__i386__) && !defined(__arch_um__)
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100745 printk("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -0700746 {
747 int i;
748 for (i = 0; i < 16; i++) {
749 unsigned char insn;
750
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100751 __get_user(insn, (unsigned char *)(regs->ip + i));
Ingo Molnar45807a12007-07-15 23:40:10 -0700752 printk("%02x ", insn);
753 }
754 }
755#endif
756 printk("\n");
757 show_regs(regs);
758}
759
760static int __init setup_print_fatal_signals(char *str)
761{
762 get_option (&str, &print_fatal_signals);
763
764 return 1;
765}
766
767__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769static int
770specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
771{
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700772 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Eric Sesterhennfda8bd72006-04-02 13:44:47 +0200774 BUG_ON(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 assert_spin_locked(&t->sighand->siglock);
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 ret = send_signal(sig, info, t, &t->pending);
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700778 if (ret <= 0)
779 return ret;
780
781 if (!sigismember(&t->blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 signal_wake_up(t, sig == SIGKILL);
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700783 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786/*
787 * Force a signal that the process can't ignore: if necessary
788 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700789 *
790 * Note: If we unblock the signal, we always reset it to SIG_DFL,
791 * since we do not want to have a signal handler that was blocked
792 * be invoked when user space had explicitly blocked it.
793 *
794 * We don't want to have recursive SIGSEGV's etc, for example.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796int
797force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
798{
799 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700800 int ret, blocked, ignored;
801 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700804 action = &t->sighand->action[sig-1];
805 ignored = action->sa.sa_handler == SIG_IGN;
806 blocked = sigismember(&t->blocked, sig);
807 if (blocked || ignored) {
808 action->sa.sa_handler = SIG_DFL;
809 if (blocked) {
810 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700811 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
814 ret = specific_send_sig_info(sig, info, t);
815 spin_unlock_irqrestore(&t->sighand->siglock, flags);
816
817 return ret;
818}
819
820void
821force_sig_specific(int sig, struct task_struct *t)
822{
Paul E. McKenneyb0423a02005-10-30 15:03:46 -0800823 force_sig_info(sig, SEND_SIG_FORCED, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * Test if P wants to take SIG. After we've checked all threads with this,
828 * it's equivalent to finding no threads not blocking SIG. Any threads not
829 * blocking SIG were ruled out because they are not running and already
830 * have pending signals. Such threads will dequeue from the shared queue
831 * as soon as they're available, so putting the signal on the shared queue
832 * will be equivalent to sending it to one such thread.
833 */
Linus Torvalds188a1ea2005-09-23 13:22:21 -0700834static inline int wants_signal(int sig, struct task_struct *p)
835{
836 if (sigismember(&p->blocked, sig))
837 return 0;
838 if (p->flags & PF_EXITING)
839 return 0;
840 if (sig == SIGKILL)
841 return 1;
Matthew Wilcoxe1abb392007-12-06 11:07:35 -0500842 if (task_is_stopped_or_traced(p))
Linus Torvalds188a1ea2005-09-23 13:22:21 -0700843 return 0;
844 return task_curr(p) || !signal_pending(p);
845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847static void
848__group_complete_signal(int sig, struct task_struct *p)
849{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct task_struct *t;
851
852 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 * Now find a thread we can wake up to take the signal off the queue.
854 *
855 * If the main thread wants the signal, it gets first crack.
856 * Probably the least surprising to the average bear.
857 */
Linus Torvalds188a1ea2005-09-23 13:22:21 -0700858 if (wants_signal(sig, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 t = p;
860 else if (thread_group_empty(p))
861 /*
862 * There is just one thread and it does not need to be woken.
863 * It will dequeue unblocked signals before it runs again.
864 */
865 return;
866 else {
867 /*
868 * Otherwise try to find a suitable thread.
869 */
870 t = p->signal->curr_target;
871 if (t == NULL)
872 /* restart balancing at this thread */
873 t = p->signal->curr_target = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds188a1ea2005-09-23 13:22:21 -0700875 while (!wants_signal(sig, t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 t = next_thread(t);
877 if (t == p->signal->curr_target)
878 /*
879 * No thread needs to be woken.
880 * Any eligible threads will see
881 * the signal in the queue soon.
882 */
883 return;
884 }
885 p->signal->curr_target = t;
886 }
887
888 /*
889 * Found a killable thread. If the signal will be fatal,
890 * then start taking the whole group down immediately.
891 */
892 if (sig_fatal(p, sig) && !(p->signal->flags & SIGNAL_GROUP_EXIT) &&
893 !sigismember(&t->real_blocked, sig) &&
894 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
895 /*
896 * This signal will be fatal to the whole group.
897 */
898 if (!sig_kernel_coredump(sig)) {
899 /*
900 * Start a group exit and wake everybody up.
901 * This way we don't have other threads
902 * running and doing things after a slower
903 * thread has the fatal signal pending.
904 */
905 p->signal->flags = SIGNAL_GROUP_EXIT;
906 p->signal->group_exit_code = sig;
907 p->signal->group_stop_count = 0;
908 t = p;
909 do {
910 sigaddset(&t->pending.signal, SIGKILL);
911 signal_wake_up(t, 1);
Oleg Nesterov18442cf2007-10-16 23:27:00 -0700912 } while_each_thread(p, t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 return;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
917 /*
918 * The signal is already in the shared-pending queue.
919 * Tell the chosen thread to wake up and dequeue it.
920 */
921 signal_wake_up(t, sig == SIGKILL);
922 return;
923}
924
925int
926__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
927{
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700928 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 assert_spin_locked(&p->sighand->siglock);
931 handle_stop_signal(sig, p);
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /*
934 * Put this signal on the shared-pending queue, or fail with EAGAIN.
935 * We always use the shared queue for process-wide signals,
936 * to avoid several races.
937 */
938 ret = send_signal(sig, info, p, &p->signal->shared_pending);
Pavel Emelyanov2acb0242008-04-30 00:52:35 -0700939 if (ret <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return ret;
941
942 __group_complete_signal(sig, p);
943 return 0;
944}
945
946/*
947 * Nuke all other threads in the group.
948 */
949void zap_other_threads(struct task_struct *p)
950{
951 struct task_struct *t;
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 p->signal->group_stop_count = 0;
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 for (t = next_thread(p); t != p; t = next_thread(t)) {
956 /*
957 * Don't bother with already dead threads
958 */
959 if (t->exit_state)
960 continue;
961
Andrea Arcangeli30e0fca2005-10-30 15:02:38 -0800962 /* SIGKILL will be handled before any pending SIGSTOP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 signal_wake_up(t, 1);
965 }
966}
967
Harvey Harrisonb5606c22008-02-13 15:03:16 -0800968int __fatal_signal_pending(struct task_struct *tsk)
Matthew Wilcoxf776d122007-12-06 11:15:50 -0500969{
970 return sigismember(&tsk->pending.signal, SIGKILL);
971}
Trond Myklebust13f09b92008-01-31 20:40:29 -0500972EXPORT_SYMBOL(__fatal_signal_pending);
Matthew Wilcoxf776d122007-12-06 11:15:50 -0500973
Oleg Nesterovf63ee722006-03-28 16:11:13 -0800974struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
975{
976 struct sighand_struct *sighand;
977
Oleg Nesterov1406f2d2008-04-30 00:52:37 -0700978 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -0800979 for (;;) {
980 sighand = rcu_dereference(tsk->sighand);
981 if (unlikely(sighand == NULL))
982 break;
983
984 spin_lock_irqsave(&sighand->siglock, *flags);
985 if (likely(sighand == tsk->sighand))
986 break;
987 spin_unlock_irqrestore(&sighand->siglock, *flags);
988 }
Oleg Nesterov1406f2d2008-04-30 00:52:37 -0700989 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -0800990
991 return sighand;
992}
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
995{
996 unsigned long flags;
997 int ret;
998
999 ret = check_kill_permission(sig, info, p);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001000
1001 if (!ret && sig) {
1002 ret = -ESRCH;
1003 if (lock_task_sighand(p, &flags)) {
1004 ret = __group_send_sig_info(sig, info, p);
1005 unlock_task_sighand(p, &flags);
Ingo Molnare56d0902006-01-08 01:01:37 -08001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
1009 return ret;
1010}
1011
1012/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001013 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 * control characters do (^C, ^Z etc)
1015 */
1016
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001017int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct task_struct *p = NULL;
1020 int retval, success;
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 success = 0;
1023 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001024 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int err = group_send_sig_info(sig, info, p);
1026 success |= !err;
1027 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001028 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return success ? 0 : retval;
1030}
1031
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001032int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001034 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct task_struct *p;
1036
Ingo Molnare56d0902006-01-08 01:01:37 -08001037 rcu_read_lock();
Oleg Nesterov0c12b512007-02-10 01:44:56 -08001038 if (unlikely(sig_needs_tasklist(sig)))
Ingo Molnare56d0902006-01-08 01:01:37 -08001039 read_lock(&tasklist_lock);
Oleg Nesterov0c12b512007-02-10 01:44:56 -08001040
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001041retry:
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001042 p = pid_task(pid, PIDTYPE_PID);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001043 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 error = group_send_sig_info(sig, info, p);
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001045 if (unlikely(error == -ESRCH))
1046 /*
1047 * The task was unhashed in between, try again.
1048 * If it is dead, pid_task() will return NULL,
1049 * if we race with de_thread() it will find the
1050 * new leader.
1051 */
1052 goto retry;
1053 }
Oleg Nesterov0c12b512007-02-10 01:44:56 -08001054
1055 if (unlikely(sig_needs_tasklist(sig)))
Ingo Molnare56d0902006-01-08 01:01:37 -08001056 read_unlock(&tasklist_lock);
1057 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return error;
1059}
1060
Matthew Wilcoxc3de4b32007-02-09 08:11:47 -07001061int
1062kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001063{
1064 int error;
1065 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001066 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001067 rcu_read_unlock();
1068 return error;
1069}
1070
Eric W. Biederman2425c082006-10-02 02:17:28 -07001071/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1072int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
David Quigley8f95dc52006-06-30 01:55:47 -07001073 uid_t uid, uid_t euid, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001074{
1075 int ret = -EINVAL;
1076 struct task_struct *p;
1077
1078 if (!valid_signal(sig))
1079 return ret;
1080
1081 read_lock(&tasklist_lock);
Eric W. Biederman2425c082006-10-02 02:17:28 -07001082 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001083 if (!p) {
1084 ret = -ESRCH;
1085 goto out_unlock;
1086 }
Oleg Nesterov0811af22006-01-08 01:03:09 -08001087 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
Harald Welte46113832005-10-10 19:44:29 +02001088 && (euid != p->suid) && (euid != p->uid)
1089 && (uid != p->suid) && (uid != p->uid)) {
1090 ret = -EPERM;
1091 goto out_unlock;
1092 }
David Quigley8f95dc52006-06-30 01:55:47 -07001093 ret = security_task_kill(p, info, sig, secid);
1094 if (ret)
1095 goto out_unlock;
Harald Welte46113832005-10-10 19:44:29 +02001096 if (sig && p->sighand) {
1097 unsigned long flags;
1098 spin_lock_irqsave(&p->sighand->siglock, flags);
1099 ret = __group_send_sig_info(sig, info, p);
1100 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1101 }
1102out_unlock:
1103 read_unlock(&tasklist_lock);
1104 return ret;
1105}
Eric W. Biederman2425c082006-10-02 02:17:28 -07001106EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108/*
1109 * kill_something_info() interprets pid in interesting ways just like kill(2).
1110 *
1111 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1112 * is probably wrong. Should make it like BSD or SYSV.
1113 */
1114
1115static int kill_something_info(int sig, struct siginfo *info, int pid)
1116{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001117 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001118
1119 if (pid > 0) {
1120 rcu_read_lock();
1121 ret = kill_pid_info(sig, info, find_vpid(pid));
1122 rcu_read_unlock();
1123 return ret;
1124 }
1125
1126 read_lock(&tasklist_lock);
1127 if (pid != -1) {
1128 ret = __kill_pgrp_info(sig, info,
1129 pid ? find_vpid(-pid) : task_pgrp(current));
1130 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int retval = 0, count = 0;
1132 struct task_struct * p;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 for_each_process(p) {
Pavel Emelyanovbac0abd2007-10-18 23:40:18 -07001135 if (p->pid > 1 && !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 int err = group_send_sig_info(sig, info, p);
1137 ++count;
1138 if (err != -EPERM)
1139 retval = err;
1140 }
1141 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001142 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001144 read_unlock(&tasklist_lock);
1145
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001146 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
1149/*
1150 * These are for backward compatibility with the rest of the kernel source.
1151 */
1152
1153/*
1154 * These two are the most common entry points. They send a signal
1155 * just to the specific thread.
1156 */
1157int
1158send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1159{
1160 int ret;
1161 unsigned long flags;
1162
1163 /*
1164 * Make sure legacy kernel users don't send in bad values
1165 * (normal paths check this in check_kill_permission).
1166 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001167 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return -EINVAL;
1169
1170 /*
1171 * We need the tasklist lock even for the specific
1172 * thread case (when we don't need to follow the group
1173 * lists) in order to avoid races with "p->sighand"
1174 * going away or changing from under us.
1175 */
1176 read_lock(&tasklist_lock);
1177 spin_lock_irqsave(&p->sighand->siglock, flags);
1178 ret = specific_send_sig_info(sig, info, p);
1179 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1180 read_unlock(&tasklist_lock);
1181 return ret;
1182}
1183
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001184#define __si_special(priv) \
1185 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187int
1188send_sig(int sig, struct task_struct *p, int priv)
1189{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001190 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193void
1194force_sig(int sig, struct task_struct *p)
1195{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001196 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
1199/*
1200 * When things go south during signal handling, we
1201 * will force a SIGSEGV. And if the signal that caused
1202 * the problem was already a SIGSEGV, we'll want to
1203 * make sure we don't even try to deliver the signal..
1204 */
1205int
1206force_sigsegv(int sig, struct task_struct *p)
1207{
1208 if (sig == SIGSEGV) {
1209 unsigned long flags;
1210 spin_lock_irqsave(&p->sighand->siglock, flags);
1211 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1212 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1213 }
1214 force_sig(SIGSEGV, p);
1215 return 0;
1216}
1217
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001218int kill_pgrp(struct pid *pid, int sig, int priv)
1219{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001220 int ret;
1221
1222 read_lock(&tasklist_lock);
1223 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1224 read_unlock(&tasklist_lock);
1225
1226 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001227}
1228EXPORT_SYMBOL(kill_pgrp);
1229
1230int kill_pid(struct pid *pid, int sig, int priv)
1231{
1232 return kill_pid_info(sig, __si_special(priv), pid);
1233}
1234EXPORT_SYMBOL(kill_pid);
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237kill_proc(pid_t pid, int sig, int priv)
1238{
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001239 int ret;
1240
1241 rcu_read_lock();
1242 ret = kill_pid_info(sig, __si_special(priv), find_pid(pid));
1243 rcu_read_unlock();
1244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247/*
1248 * These functions support sending signals using preallocated sigqueue
1249 * structures. This is needed "because realtime applications cannot
1250 * afford to lose notifications of asynchronous events, like timer
1251 * expirations or I/O completions". In the case of Posix Timers
1252 * we allocate the sigqueue structure from the timer_create. If this
1253 * allocation fails we are able to report the failure to the application
1254 * with an EAGAIN error.
1255 */
1256
1257struct sigqueue *sigqueue_alloc(void)
1258{
1259 struct sigqueue *q;
1260
1261 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1262 q->flags |= SIGQUEUE_PREALLOC;
1263 return(q);
1264}
1265
1266void sigqueue_free(struct sigqueue *q)
1267{
1268 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001269 spinlock_t *lock = &current->sighand->siglock;
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1272 /*
1273 * If the signal is still pending remove it from the
Oleg Nesterov60187d22007-08-30 23:56:35 -07001274 * pending queue. We must hold ->siglock while testing
1275 * q->list to serialize with collect_signal().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001277 spin_lock_irqsave(lock, flags);
1278 if (!list_empty(&q->list))
1279 list_del_init(&q->list);
1280 spin_unlock_irqrestore(lock, flags);
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 q->flags &= ~SIGQUEUE_PREALLOC;
1283 __sigqueue_free(q);
1284}
1285
Oleg Nesterov54767902006-03-28 16:11:30 -08001286int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 unsigned long flags;
1289 int ret = 0;
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Ingo Molnare56d0902006-01-08 01:01:37 -08001292
1293 /*
1294 * The rcu based delayed sighand destroy makes it possible to
1295 * run this without tasklist lock held. The task struct itself
1296 * cannot go away as create_timer did get_task_struct().
1297 *
1298 * We return -1, when the task is marked exiting, so
1299 * posix_timer_event can redirect it to the group leader
1300 */
1301 rcu_read_lock();
Oleg Nesterove752dd62005-09-06 15:17:42 -07001302
Oleg Nesterov54767902006-03-28 16:11:30 -08001303 if (!likely(lock_task_sighand(p, &flags))) {
Oleg Nesterove752dd62005-09-06 15:17:42 -07001304 ret = -1;
1305 goto out_err;
1306 }
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (unlikely(!list_empty(&q->list))) {
1309 /*
1310 * If an SI_TIMER entry is already queue just increment
1311 * the overrun count.
1312 */
Oleg Nesterov54767902006-03-28 16:11:30 -08001313 BUG_ON(q->info.si_code != SI_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 q->info.si_overrun++;
1315 goto out;
Oleg Nesterove752dd62005-09-06 15:17:42 -07001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 /* Short-circuit ignored signals. */
1318 if (sig_ignored(p, sig)) {
1319 ret = 1;
1320 goto out;
1321 }
Davide Libenzifba2afa2007-05-10 22:23:13 -07001322 /*
1323 * Deliver the signal to listening signalfds. This must be called
1324 * with the sighand lock held.
1325 */
1326 signalfd_notify(p, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 list_add_tail(&q->list, &p->pending.list);
1329 sigaddset(&p->pending.signal, sig);
1330 if (!sigismember(&p->blocked, sig))
1331 signal_wake_up(p, sig == SIGKILL);
1332
1333out:
Oleg Nesterov54767902006-03-28 16:11:30 -08001334 unlock_task_sighand(p, &flags);
Oleg Nesterove752dd62005-09-06 15:17:42 -07001335out_err:
Ingo Molnare56d0902006-01-08 01:01:37 -08001336 rcu_read_unlock();
Oleg Nesterove752dd62005-09-06 15:17:42 -07001337
1338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341int
1342send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1343{
1344 unsigned long flags;
1345 int ret = 0;
1346
1347 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Ingo Molnare56d0902006-01-08 01:01:37 -08001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 read_lock(&tasklist_lock);
Ingo Molnare56d0902006-01-08 01:01:37 -08001350 /* Since it_lock is held, p->sighand cannot be NULL. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 spin_lock_irqsave(&p->sighand->siglock, flags);
1352 handle_stop_signal(sig, p);
1353
1354 /* Short-circuit ignored signals. */
1355 if (sig_ignored(p, sig)) {
1356 ret = 1;
1357 goto out;
1358 }
1359
1360 if (unlikely(!list_empty(&q->list))) {
1361 /*
1362 * If an SI_TIMER entry is already queue just increment
1363 * the overrun count. Other uses should not try to
1364 * send the signal multiple times.
1365 */
Eric Sesterhennfda8bd72006-04-02 13:44:47 +02001366 BUG_ON(q->info.si_code != SI_TIMER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 q->info.si_overrun++;
1368 goto out;
1369 }
Davide Libenzifba2afa2007-05-10 22:23:13 -07001370 /*
1371 * Deliver the signal to listening signalfds. This must be called
1372 * with the sighand lock held.
1373 */
1374 signalfd_notify(p, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /*
1377 * Put this signal on the shared-pending queue.
1378 * We always use the shared queue for process-wide signals,
1379 * to avoid several races.
1380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 list_add_tail(&q->list, &p->signal->shared_pending.list);
1382 sigaddset(&p->signal->shared_pending.signal, sig);
1383
1384 __group_complete_signal(sig, p);
1385out:
1386 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1387 read_unlock(&tasklist_lock);
Ingo Molnare56d0902006-01-08 01:01:37 -08001388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
1392 * Wake up any threads in the parent blocked in wait* syscalls.
1393 */
1394static inline void __wake_up_parent(struct task_struct *p,
1395 struct task_struct *parent)
1396{
1397 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1398}
1399
1400/*
1401 * Let a parent know about the death of a child.
1402 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1403 */
1404
1405void do_notify_parent(struct task_struct *tsk, int sig)
1406{
1407 struct siginfo info;
1408 unsigned long flags;
1409 struct sighand_struct *psig;
1410
1411 BUG_ON(sig == -1);
1412
1413 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001414 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 BUG_ON(!tsk->ptrace &&
1417 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1418
1419 info.si_signo = sig;
1420 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001421 /*
1422 * we are under tasklist_lock here so our parent is tied to
1423 * us and cannot exit and release its namespace.
1424 *
1425 * the only it can is to switch its nsproxy with sys_unshare,
1426 * bu uncharing pid namespaces is not allowed, so we'll always
1427 * see relevant namespace
1428 *
1429 * write_lock() currently calls preempt_disable() which is the
1430 * same as rcu_read_lock(), but according to Oleg, this is not
1431 * correct to rely on this
1432 */
1433 rcu_read_lock();
1434 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1435 rcu_read_unlock();
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 info.si_uid = tsk->uid;
1438
1439 /* FIXME: find out whether or not this is supposed to be c*time. */
1440 info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
1441 tsk->signal->utime));
1442 info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
1443 tsk->signal->stime));
1444
1445 info.si_status = tsk->exit_code & 0x7f;
1446 if (tsk->exit_code & 0x80)
1447 info.si_code = CLD_DUMPED;
1448 else if (tsk->exit_code & 0x7f)
1449 info.si_code = CLD_KILLED;
1450 else {
1451 info.si_code = CLD_EXITED;
1452 info.si_status = tsk->exit_code >> 8;
1453 }
1454
1455 psig = tsk->parent->sighand;
1456 spin_lock_irqsave(&psig->siglock, flags);
Oleg Nesterov7ed01752005-11-10 17:22:18 +03001457 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1459 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1460 /*
1461 * We are exiting and our parent doesn't care. POSIX.1
1462 * defines special semantics for setting SIGCHLD to SIG_IGN
1463 * or setting the SA_NOCLDWAIT flag: we should be reaped
1464 * automatically and not left for our parent's wait4 call.
1465 * Rather than having the parent do it as a magic kind of
1466 * signal handler, we just set this to tell do_exit that we
1467 * can be cleaned up without becoming a zombie. Note that
1468 * we still call __wake_up_parent in this case, because a
1469 * blocked sys_wait4 might now return -ECHILD.
1470 *
1471 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1472 * is implementation-defined: we do (if you don't want
1473 * it, just use SIG_IGN instead).
1474 */
1475 tsk->exit_signal = -1;
1476 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1477 sig = 0;
1478 }
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001479 if (valid_signal(sig) && sig > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 __group_send_sig_info(sig, &info, tsk->parent);
1481 __wake_up_parent(tsk, tsk->parent);
1482 spin_unlock_irqrestore(&psig->siglock, flags);
1483}
1484
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001485static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
1487 struct siginfo info;
1488 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001489 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 struct sighand_struct *sighand;
1491
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001492 if (tsk->ptrace & PT_PTRACED)
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001493 parent = tsk->parent;
1494 else {
1495 tsk = tsk->group_leader;
1496 parent = tsk->real_parent;
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 info.si_signo = SIGCHLD;
1500 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001501 /*
1502 * see comment in do_notify_parent() abot the following 3 lines
1503 */
1504 rcu_read_lock();
1505 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
1506 rcu_read_unlock();
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 info.si_uid = tsk->uid;
1509
1510 /* FIXME: find out whether or not this is supposed to be c*time. */
1511 info.si_utime = cputime_to_jiffies(tsk->utime);
1512 info.si_stime = cputime_to_jiffies(tsk->stime);
1513
1514 info.si_code = why;
1515 switch (why) {
1516 case CLD_CONTINUED:
1517 info.si_status = SIGCONT;
1518 break;
1519 case CLD_STOPPED:
1520 info.si_status = tsk->signal->group_exit_code & 0x7f;
1521 break;
1522 case CLD_TRAPPED:
1523 info.si_status = tsk->exit_code & 0x7f;
1524 break;
1525 default:
1526 BUG();
1527 }
1528
1529 sighand = parent->sighand;
1530 spin_lock_irqsave(&sighand->siglock, flags);
1531 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1532 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1533 __group_send_sig_info(SIGCHLD, &info, parent);
1534 /*
1535 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1536 */
1537 __wake_up_parent(tsk, parent);
1538 spin_unlock_irqrestore(&sighand->siglock, flags);
1539}
1540
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001541static inline int may_ptrace_stop(void)
1542{
1543 if (!likely(current->ptrace & PT_PTRACED))
1544 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001545 /*
1546 * Are we in the middle of do_coredump?
1547 * If so and our tracer is also part of the coredump stopping
1548 * is a deadlock situation, and pointless because our tracer
1549 * is dead so don't allow us to stop.
1550 * If SIGKILL was already sent before the caller unlocked
1551 * ->siglock we must see ->core_waiters != 0. Otherwise it
1552 * is safe to enter schedule().
1553 */
1554 if (unlikely(current->mm->core_waiters) &&
1555 unlikely(current->mm == current->parent->mm))
1556 return 0;
1557
1558 return 1;
1559}
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561/*
Roland McGrath1a669c22008-02-06 01:37:37 -08001562 * Return nonzero if there is a SIGKILL that should be waking us up.
1563 * Called with the siglock held.
1564 */
1565static int sigkill_pending(struct task_struct *tsk)
1566{
1567 return ((sigismember(&tsk->pending.signal, SIGKILL) ||
1568 sigismember(&tsk->signal->shared_pending.signal, SIGKILL)) &&
1569 !unlikely(sigismember(&tsk->blocked, SIGKILL)));
1570}
1571
1572/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 * This must be called with current->sighand->siglock held.
1574 *
1575 * This should be the path for all ptrace stops.
1576 * We always set current->last_siginfo while stopped here.
1577 * That makes it a way to test a stopped process for
1578 * being ptrace-stopped vs being job-control-stopped.
1579 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001580 * If we actually decide not to stop at all because the tracer
1581 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
Oleg Nesterov20686a32008-02-08 04:19:03 -08001583static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Roland McGrath1a669c22008-02-06 01:37:37 -08001585 int killed = 0;
1586
1587 if (arch_ptrace_stop_needed(exit_code, info)) {
1588 /*
1589 * The arch code has something special to do before a
1590 * ptrace stop. This is allowed to block, e.g. for faults
1591 * on user stack pages. We can't keep the siglock while
1592 * calling arch_ptrace_stop, so we must release it now.
1593 * To preserve proper semantics, we must do this before
1594 * any signal bookkeeping like checking group_stop_count.
1595 * Meanwhile, a SIGKILL could come in before we retake the
1596 * siglock. That must prevent us from sleeping in TASK_TRACED.
1597 * So after regaining the lock, we must check for SIGKILL.
1598 */
1599 spin_unlock_irq(&current->sighand->siglock);
1600 arch_ptrace_stop(exit_code, info);
1601 spin_lock_irq(&current->sighand->siglock);
1602 killed = sigkill_pending(current);
1603 }
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 /*
1606 * If there is a group stop in progress,
1607 * we must participate in the bookkeeping.
1608 */
1609 if (current->signal->group_stop_count > 0)
1610 --current->signal->group_stop_count;
1611
1612 current->last_siginfo = info;
1613 current->exit_code = exit_code;
1614
1615 /* Let the debugger run. */
Oleg Nesterovd9ae90a2008-02-06 01:36:13 -08001616 __set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 spin_unlock_irq(&current->sighand->siglock);
1618 read_lock(&tasklist_lock);
Roland McGrath1a669c22008-02-06 01:37:37 -08001619 if (!unlikely(killed) && may_ptrace_stop()) {
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001620 do_notify_parent_cldstop(current, CLD_TRAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 read_unlock(&tasklist_lock);
1622 schedule();
1623 } else {
1624 /*
1625 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001626 * Don't drop the lock yet, another tracer may come.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001628 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001629 if (clear_code)
1630 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001631 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633
1634 /*
Roland McGrath13b1c3d2008-03-03 20:22:05 -08001635 * While in TASK_TRACED, we were considered "frozen enough".
1636 * Now that we woke up, it's crucial if we're supposed to be
1637 * frozen that we freeze now before running anything substantial.
1638 */
1639 try_to_freeze();
1640
1641 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * We are back. Now reacquire the siglock before touching
1643 * last_siginfo, so that we are sure to have synchronized with
1644 * any signal-sending on another CPU that wants to examine it.
1645 */
1646 spin_lock_irq(&current->sighand->siglock);
1647 current->last_siginfo = NULL;
1648
1649 /*
1650 * Queued signals ignored us while we were stopped for tracing.
1651 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001652 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001654 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657void ptrace_notify(int exit_code)
1658{
1659 siginfo_t info;
1660
1661 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1662
1663 memset(&info, 0, sizeof info);
1664 info.si_signo = SIGTRAP;
1665 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001666 info.si_pid = task_pid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 info.si_uid = current->uid;
1668
1669 /* Let the debugger run. */
1670 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001671 ptrace_stop(exit_code, 1, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 spin_unlock_irq(&current->sighand->siglock);
1673}
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675static void
1676finish_stop(int stop_count)
1677{
1678 /*
1679 * If there are no other threads in the group, or if there is
1680 * a group stop in progress and we are the last to stop,
1681 * report to the parent. When ptraced, every thread reports itself.
1682 */
Oleg Nesterova1d5e212006-03-28 16:11:29 -08001683 if (stop_count == 0 || (current->ptrace & PT_PTRACED)) {
1684 read_lock(&tasklist_lock);
1685 do_notify_parent_cldstop(current, CLD_STOPPED);
1686 read_unlock(&tasklist_lock);
1687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Rafael J. Wysocki3df494a2006-12-13 00:34:28 -08001689 do {
1690 schedule();
1691 } while (try_to_freeze());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Now we don't run again until continued.
1694 */
1695 current->exit_code = 0;
1696}
1697
1698/*
1699 * This performs the stopping for SIGSTOP and other stop signals.
1700 * We have to stop all threads in the thread group.
1701 * Returns nonzero if we've actually stopped and released the siglock.
1702 * Returns zero if we didn't stop and still hold the siglock.
1703 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001704static int do_signal_stop(int signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705{
1706 struct signal_struct *sig = current->signal;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001707 int stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 if (sig->group_stop_count > 0) {
1710 /*
1711 * There is a group stop in progress. We don't need to
1712 * start another one.
1713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 stop_count = --sig->group_stop_count;
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001715 } else {
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001716 struct task_struct *t;
1717
Oleg Nesteroved5d2ca2008-02-04 22:27:24 -08001718 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07001719 unlikely(signal_group_exit(sig)))
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 * There is no group stop already in progress.
Oleg Nesterova122b342006-03-28 16:11:22 -08001723 * We must initiate one now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 */
Oleg Nesterova122b342006-03-28 16:11:22 -08001725 sig->group_exit_code = signr;
1726
1727 stop_count = 0;
1728 for (t = next_thread(current); t != current; t = next_thread(t))
1729 /*
1730 * Setting state to TASK_STOPPED for a group
1731 * stop is always done with the siglock held,
1732 * so this check has no races.
1733 */
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001734 if (!(t->flags & PF_EXITING) &&
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001735 !task_is_stopped_or_traced(t)) {
Oleg Nesterova122b342006-03-28 16:11:22 -08001736 stop_count++;
1737 signal_wake_up(t, 0);
1738 }
1739 sig->group_stop_count = stop_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
Oleg Nesterovdac27f42006-03-28 16:11:28 -08001742 if (stop_count == 0)
1743 sig->flags = SIGNAL_STOP_STOPPED;
1744 current->exit_code = sig->group_exit_code;
1745 __set_current_state(TASK_STOPPED);
1746
1747 spin_unlock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 finish_stop(stop_count);
1749 return 1;
1750}
1751
Roland McGrath18c98b62008-04-17 18:44:38 -07001752static int ptrace_signal(int signr, siginfo_t *info,
1753 struct pt_regs *regs, void *cookie)
1754{
1755 if (!(current->ptrace & PT_PTRACED))
1756 return signr;
1757
1758 ptrace_signal_deliver(regs, cookie);
1759
1760 /* Let the debugger run. */
1761 ptrace_stop(signr, 0, info);
1762
1763 /* We're back. Did the debugger cancel the sig? */
1764 signr = current->exit_code;
1765 if (signr == 0)
1766 return signr;
1767
1768 current->exit_code = 0;
1769
1770 /* Update the siginfo structure if the signal has
1771 changed. If the debugger wanted something
1772 specific in the siginfo structure then it should
1773 have updated *info via PTRACE_SETSIGINFO. */
1774 if (signr != info->si_signo) {
1775 info->si_signo = signr;
1776 info->si_errno = 0;
1777 info->si_code = SI_USER;
1778 info->si_pid = task_pid_vnr(current->parent);
1779 info->si_uid = current->parent->uid;
1780 }
1781
1782 /* If the (new) signal is now blocked, requeue it. */
1783 if (sigismember(&current->blocked, signr)) {
1784 specific_send_sig_info(signr, info, current);
1785 signr = 0;
1786 }
1787
1788 return signr;
1789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1792 struct pt_regs *regs, void *cookie)
1793{
1794 sigset_t *mask = &current->blocked;
1795 int signr = 0;
1796
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 spin_lock_irq(&current->sighand->siglock);
1807 for (;;) {
1808 struct k_sigaction *ka;
1809
1810 if (unlikely(current->signal->group_stop_count > 0) &&
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08001811 do_signal_stop(0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto relock;
1813
1814 signr = dequeue_signal(current, mask, info);
1815
1816 if (!signr)
1817 break; /* will return 0 */
1818
Roland McGrath18c98b62008-04-17 18:44:38 -07001819 if (signr != SIGKILL) {
1820 signr = ptrace_signal(signr, info, regs, cookie);
1821 if (!signr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
1825 ka = &current->sighand->action[signr-1];
1826 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1827 continue;
1828 if (ka->sa.sa_handler != SIG_DFL) {
1829 /* Run the handler. */
1830 *return_ka = *ka;
1831
1832 if (ka->sa.sa_flags & SA_ONESHOT)
1833 ka->sa.sa_handler = SIG_DFL;
1834
1835 break; /* will return non-zero "signr" value */
1836 }
1837
1838 /*
1839 * Now we are doing the default action for this signal.
1840 */
1841 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1842 continue;
1843
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001844 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001845 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08001846 */
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07001847 if (is_global_init(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 continue;
1849
1850 if (sig_kernel_stop(signr)) {
1851 /*
1852 * The default action is to stop all threads in
1853 * the thread group. The job control signals
1854 * do nothing in an orphaned pgrp, but SIGSTOP
1855 * always works. Note that siglock needs to be
1856 * dropped during the call to is_orphaned_pgrp()
1857 * because of lock ordering with tasklist_lock.
1858 * This allows an intervening SIGCONT to be posted.
1859 * We need to check for that and bail out if necessary.
1860 */
1861 if (signr != SIGSTOP) {
1862 spin_unlock_irq(&current->sighand->siglock);
1863
1864 /* signals can be posted during this window */
1865
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08001866 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto relock;
1868
1869 spin_lock_irq(&current->sighand->siglock);
1870 }
1871
1872 if (likely(do_signal_stop(signr))) {
1873 /* It released the siglock. */
1874 goto relock;
1875 }
1876
1877 /*
1878 * We didn't actually stop, due to a race
1879 * with SIGCONT or something like that.
1880 */
1881 continue;
1882 }
1883
1884 spin_unlock_irq(&current->sighand->siglock);
1885
1886 /*
1887 * Anything else is fatal, maybe with a core dump.
1888 */
1889 current->flags |= PF_SIGNALED;
Ingo Molnar45807a12007-07-15 23:40:10 -07001890 if ((signr != SIGKILL) && print_fatal_signals)
1891 print_fatal_signal(regs, signr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (sig_kernel_coredump(signr)) {
1893 /*
1894 * If it was able to dump core, this kills all
1895 * other threads in the group and synchronizes with
1896 * their demise. If we lost the race with another
1897 * thread getting here, it set group_exit_code
1898 * first and our do_group_exit call below will use
1899 * that value and ignore the one we pass it.
1900 */
1901 do_coredump((long)signr, signr, regs);
1902 }
1903
1904 /*
1905 * Death signals, no core dump.
1906 */
1907 do_group_exit(signr);
1908 /* NOTREACHED */
1909 }
1910 spin_unlock_irq(&current->sighand->siglock);
1911 return signr;
1912}
1913
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001914void exit_signals(struct task_struct *tsk)
1915{
1916 int group_stop = 0;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001917 struct task_struct *t;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001918
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001919 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1920 tsk->flags |= PF_EXITING;
1921 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001922 }
1923
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001924 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001925 /*
1926 * From now this task is not visible for group-wide signals,
1927 * see wants_signal(), do_signal_stop().
1928 */
1929 tsk->flags |= PF_EXITING;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08001930 if (!signal_pending(tsk))
1931 goto out;
1932
1933 /* It could be that __group_complete_signal() choose us to
1934 * notify about group-wide signal. Another thread should be
1935 * woken now to take the signal since we will not.
1936 */
1937 for (t = tsk; (t = next_thread(t)) != tsk; )
1938 if (!signal_pending(t) && !(t->flags & PF_EXITING))
1939 recalc_sigpending_and_wake(t);
1940
1941 if (unlikely(tsk->signal->group_stop_count) &&
1942 !--tsk->signal->group_stop_count) {
1943 tsk->signal->flags = SIGNAL_STOP_STOPPED;
1944 group_stop = 1;
1945 }
1946out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08001947 spin_unlock_irq(&tsk->sighand->siglock);
1948
1949 if (unlikely(group_stop)) {
1950 read_lock(&tasklist_lock);
1951 do_notify_parent_cldstop(tsk, CLD_STOPPED);
1952 read_unlock(&tasklist_lock);
1953 }
1954}
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956EXPORT_SYMBOL(recalc_sigpending);
1957EXPORT_SYMBOL_GPL(dequeue_signal);
1958EXPORT_SYMBOL(flush_signals);
1959EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960EXPORT_SYMBOL(kill_proc);
1961EXPORT_SYMBOL(ptrace_notify);
1962EXPORT_SYMBOL(send_sig);
1963EXPORT_SYMBOL(send_sig_info);
1964EXPORT_SYMBOL(sigprocmask);
1965EXPORT_SYMBOL(block_all_signals);
1966EXPORT_SYMBOL(unblock_all_signals);
1967
1968
1969/*
1970 * System call entry points.
1971 */
1972
1973asmlinkage long sys_restart_syscall(void)
1974{
1975 struct restart_block *restart = &current_thread_info()->restart_block;
1976 return restart->fn(restart);
1977}
1978
1979long do_no_restart_syscall(struct restart_block *param)
1980{
1981 return -EINTR;
1982}
1983
1984/*
1985 * We don't need to get the kernel lock - this is all local to this
1986 * particular thread.. (and that's good, because this is _heavily_
1987 * used by various programs)
1988 */
1989
1990/*
1991 * This is also useful for kernel threads that want to temporarily
1992 * (or permanently) block certain signals.
1993 *
1994 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
1995 * interface happily blocks "unblockable" signals like SIGKILL
1996 * and friends.
1997 */
1998int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
1999{
2000 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002003 if (oldset)
2004 *oldset = current->blocked;
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 error = 0;
2007 switch (how) {
2008 case SIG_BLOCK:
2009 sigorsets(&current->blocked, &current->blocked, set);
2010 break;
2011 case SIG_UNBLOCK:
2012 signandsets(&current->blocked, &current->blocked, set);
2013 break;
2014 case SIG_SETMASK:
2015 current->blocked = *set;
2016 break;
2017 default:
2018 error = -EINVAL;
2019 }
2020 recalc_sigpending();
2021 spin_unlock_irq(&current->sighand->siglock);
Oleg Nesterova26fd332006-03-23 03:00:49 -08002022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 return error;
2024}
2025
2026asmlinkage long
2027sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2028{
2029 int error = -EINVAL;
2030 sigset_t old_set, new_set;
2031
2032 /* XXX: Don't preclude handling different sized sigset_t's. */
2033 if (sigsetsize != sizeof(sigset_t))
2034 goto out;
2035
2036 if (set) {
2037 error = -EFAULT;
2038 if (copy_from_user(&new_set, set, sizeof(*set)))
2039 goto out;
2040 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2041
2042 error = sigprocmask(how, &new_set, &old_set);
2043 if (error)
2044 goto out;
2045 if (oset)
2046 goto set_old;
2047 } else if (oset) {
2048 spin_lock_irq(&current->sighand->siglock);
2049 old_set = current->blocked;
2050 spin_unlock_irq(&current->sighand->siglock);
2051
2052 set_old:
2053 error = -EFAULT;
2054 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2055 goto out;
2056 }
2057 error = 0;
2058out:
2059 return error;
2060}
2061
2062long do_sigpending(void __user *set, unsigned long sigsetsize)
2063{
2064 long error = -EINVAL;
2065 sigset_t pending;
2066
2067 if (sigsetsize > sizeof(sigset_t))
2068 goto out;
2069
2070 spin_lock_irq(&current->sighand->siglock);
2071 sigorsets(&pending, &current->pending.signal,
2072 &current->signal->shared_pending.signal);
2073 spin_unlock_irq(&current->sighand->siglock);
2074
2075 /* Outside the lock because only this thread touches it. */
2076 sigandsets(&pending, &current->blocked, &pending);
2077
2078 error = -EFAULT;
2079 if (!copy_to_user(set, &pending, sigsetsize))
2080 error = 0;
2081
2082out:
2083 return error;
2084}
2085
2086asmlinkage long
2087sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2088{
2089 return do_sigpending(set, sigsetsize);
2090}
2091
2092#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2093
2094int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2095{
2096 int err;
2097
2098 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2099 return -EFAULT;
2100 if (from->si_code < 0)
2101 return __copy_to_user(to, from, sizeof(siginfo_t))
2102 ? -EFAULT : 0;
2103 /*
2104 * If you change siginfo_t structure, please be sure
2105 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002106 * Please remember to update the signalfd_copyinfo() function
2107 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * It should never copy any pad contained in the structure
2109 * to avoid security leaks, but must copy the generic
2110 * 3 ints plus the relevant union member.
2111 */
2112 err = __put_user(from->si_signo, &to->si_signo);
2113 err |= __put_user(from->si_errno, &to->si_errno);
2114 err |= __put_user((short)from->si_code, &to->si_code);
2115 switch (from->si_code & __SI_MASK) {
2116 case __SI_KILL:
2117 err |= __put_user(from->si_pid, &to->si_pid);
2118 err |= __put_user(from->si_uid, &to->si_uid);
2119 break;
2120 case __SI_TIMER:
2121 err |= __put_user(from->si_tid, &to->si_tid);
2122 err |= __put_user(from->si_overrun, &to->si_overrun);
2123 err |= __put_user(from->si_ptr, &to->si_ptr);
2124 break;
2125 case __SI_POLL:
2126 err |= __put_user(from->si_band, &to->si_band);
2127 err |= __put_user(from->si_fd, &to->si_fd);
2128 break;
2129 case __SI_FAULT:
2130 err |= __put_user(from->si_addr, &to->si_addr);
2131#ifdef __ARCH_SI_TRAPNO
2132 err |= __put_user(from->si_trapno, &to->si_trapno);
2133#endif
2134 break;
2135 case __SI_CHLD:
2136 err |= __put_user(from->si_pid, &to->si_pid);
2137 err |= __put_user(from->si_uid, &to->si_uid);
2138 err |= __put_user(from->si_status, &to->si_status);
2139 err |= __put_user(from->si_utime, &to->si_utime);
2140 err |= __put_user(from->si_stime, &to->si_stime);
2141 break;
2142 case __SI_RT: /* This is not generated by the kernel as of now. */
2143 case __SI_MESGQ: /* But this is */
2144 err |= __put_user(from->si_pid, &to->si_pid);
2145 err |= __put_user(from->si_uid, &to->si_uid);
2146 err |= __put_user(from->si_ptr, &to->si_ptr);
2147 break;
2148 default: /* this is just in case for now ... */
2149 err |= __put_user(from->si_pid, &to->si_pid);
2150 err |= __put_user(from->si_uid, &to->si_uid);
2151 break;
2152 }
2153 return err;
2154}
2155
2156#endif
2157
2158asmlinkage long
2159sys_rt_sigtimedwait(const sigset_t __user *uthese,
2160 siginfo_t __user *uinfo,
2161 const struct timespec __user *uts,
2162 size_t sigsetsize)
2163{
2164 int ret, sig;
2165 sigset_t these;
2166 struct timespec ts;
2167 siginfo_t info;
2168 long timeout = 0;
2169
2170 /* XXX: Don't preclude handling different sized sigset_t's. */
2171 if (sigsetsize != sizeof(sigset_t))
2172 return -EINVAL;
2173
2174 if (copy_from_user(&these, uthese, sizeof(these)))
2175 return -EFAULT;
2176
2177 /*
2178 * Invert the set of allowed signals to get those we
2179 * want to block.
2180 */
2181 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2182 signotset(&these);
2183
2184 if (uts) {
2185 if (copy_from_user(&ts, uts, sizeof(ts)))
2186 return -EFAULT;
2187 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2188 || ts.tv_sec < 0)
2189 return -EINVAL;
2190 }
2191
2192 spin_lock_irq(&current->sighand->siglock);
2193 sig = dequeue_signal(current, &these, &info);
2194 if (!sig) {
2195 timeout = MAX_SCHEDULE_TIMEOUT;
2196 if (uts)
2197 timeout = (timespec_to_jiffies(&ts)
2198 + (ts.tv_sec || ts.tv_nsec));
2199
2200 if (timeout) {
2201 /* None ready -- temporarily unblock those we're
2202 * interested while we are sleeping in so that we'll
2203 * be awakened when they arrive. */
2204 current->real_blocked = current->blocked;
2205 sigandsets(&current->blocked, &current->blocked, &these);
2206 recalc_sigpending();
2207 spin_unlock_irq(&current->sighand->siglock);
2208
Nishanth Aravamudan75bcc8c2005-09-10 00:27:24 -07002209 timeout = schedule_timeout_interruptible(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 spin_lock_irq(&current->sighand->siglock);
2212 sig = dequeue_signal(current, &these, &info);
2213 current->blocked = current->real_blocked;
2214 siginitset(&current->real_blocked, 0);
2215 recalc_sigpending();
2216 }
2217 }
2218 spin_unlock_irq(&current->sighand->siglock);
2219
2220 if (sig) {
2221 ret = sig;
2222 if (uinfo) {
2223 if (copy_siginfo_to_user(uinfo, &info))
2224 ret = -EFAULT;
2225 }
2226 } else {
2227 ret = -EAGAIN;
2228 if (timeout)
2229 ret = -EINTR;
2230 }
2231
2232 return ret;
2233}
2234
2235asmlinkage long
2236sys_kill(int pid, int sig)
2237{
2238 struct siginfo info;
2239
2240 info.si_signo = sig;
2241 info.si_errno = 0;
2242 info.si_code = SI_USER;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002243 info.si_pid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 info.si_uid = current->uid;
2245
2246 return kill_something_info(sig, &info, pid);
2247}
2248
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002249static int do_tkill(int tgid, int pid, int sig)
2250{
2251 int error;
2252 struct siginfo info;
2253 struct task_struct *p;
2254
2255 error = -ESRCH;
2256 info.si_signo = sig;
2257 info.si_errno = 0;
2258 info.si_code = SI_TKILL;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002259 info.si_pid = task_tgid_vnr(current);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002260 info.si_uid = current->uid;
2261
2262 read_lock(&tasklist_lock);
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07002263 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002264 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002265 error = check_kill_permission(sig, &info, p);
2266 /*
2267 * The null signal is a permissions and process existence
2268 * probe. No signal is actually delivered.
2269 */
2270 if (!error && sig && p->sighand) {
2271 spin_lock_irq(&p->sighand->siglock);
2272 handle_stop_signal(sig, p);
2273 error = specific_send_sig_info(sig, &info, p);
2274 spin_unlock_irq(&p->sighand->siglock);
2275 }
2276 }
2277 read_unlock(&tasklist_lock);
2278
2279 return error;
2280}
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282/**
2283 * sys_tgkill - send signal to one specific thread
2284 * @tgid: the thread group ID of the thread
2285 * @pid: the PID of the thread
2286 * @sig: signal to be sent
2287 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002288 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 * exists but it's not belonging to the target process anymore. This
2290 * method solves the problem of threads exiting and PIDs getting reused.
2291 */
2292asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2293{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* This is only valid for single tasks */
2295 if (pid <= 0 || tgid <= 0)
2296 return -EINVAL;
2297
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002298 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
2300
2301/*
2302 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2303 */
2304asmlinkage long
2305sys_tkill(int pid, int sig)
2306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 /* This is only valid for single tasks */
2308 if (pid <= 0)
2309 return -EINVAL;
2310
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08002311 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314asmlinkage long
2315sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2316{
2317 siginfo_t info;
2318
2319 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2320 return -EFAULT;
2321
2322 /* Not even root can pretend to send signals from the kernel.
2323 Nor can they impersonate a kill(), which adds source info. */
2324 if (info.si_code >= 0)
2325 return -EPERM;
2326 info.si_signo = sig;
2327
2328 /* POSIX.1b doesn't mention process groups. */
2329 return kill_proc_info(sig, &info, pid);
2330}
2331
Oleg Nesterov88531f72006-03-28 16:11:24 -08002332int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08002335 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Jesper Juhl7ed20e12005-05-01 08:59:14 -07002337 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return -EINVAL;
2339
2340 k = &current->sighand->action[sig-1];
2341
2342 spin_lock_irq(&current->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (oact)
2344 *oact = *k;
2345
2346 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03002347 sigdelsetmask(&act->sa.sa_mask,
2348 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08002349 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * POSIX 3.3.1.3:
2352 * "Setting a signal action to SIG_IGN for a signal that is
2353 * pending shall cause the pending signal to be discarded,
2354 * whether or not it is blocked."
2355 *
2356 * "Setting a signal action to SIG_DFL for a signal that is
2357 * pending and whose default action is to ignore the signal
2358 * (for example, SIGCHLD), shall cause the pending signal to
2359 * be discarded, whether or not it is blocked"
2360 */
2361 if (act->sa.sa_handler == SIG_IGN ||
Oleg Nesterov88531f72006-03-28 16:11:24 -08002362 (act->sa.sa_handler == SIG_DFL && sig_kernel_ignore(sig))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 struct task_struct *t = current;
George Anzinger71fabd52006-01-08 01:02:48 -08002364 sigemptyset(&mask);
2365 sigaddset(&mask, sig);
2366 rm_from_queue_full(&mask, &t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 do {
George Anzinger71fabd52006-01-08 01:02:48 -08002368 rm_from_queue_full(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 t = next_thread(t);
2370 } while (t != current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373
2374 spin_unlock_irq(&current->sighand->siglock);
2375 return 0;
2376}
2377
2378int
2379do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2380{
2381 stack_t oss;
2382 int error;
2383
2384 if (uoss) {
2385 oss.ss_sp = (void __user *) current->sas_ss_sp;
2386 oss.ss_size = current->sas_ss_size;
2387 oss.ss_flags = sas_ss_flags(sp);
2388 }
2389
2390 if (uss) {
2391 void __user *ss_sp;
2392 size_t ss_size;
2393 int ss_flags;
2394
2395 error = -EFAULT;
2396 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2397 || __get_user(ss_sp, &uss->ss_sp)
2398 || __get_user(ss_flags, &uss->ss_flags)
2399 || __get_user(ss_size, &uss->ss_size))
2400 goto out;
2401
2402 error = -EPERM;
2403 if (on_sig_stack(sp))
2404 goto out;
2405
2406 error = -EINVAL;
2407 /*
2408 *
2409 * Note - this code used to test ss_flags incorrectly
2410 * old code may have been written using ss_flags==0
2411 * to mean ss_flags==SS_ONSTACK (as this was the only
2412 * way that worked) - this fix preserves that older
2413 * mechanism
2414 */
2415 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2416 goto out;
2417
2418 if (ss_flags == SS_DISABLE) {
2419 ss_size = 0;
2420 ss_sp = NULL;
2421 } else {
2422 error = -ENOMEM;
2423 if (ss_size < MINSIGSTKSZ)
2424 goto out;
2425 }
2426
2427 current->sas_ss_sp = (unsigned long) ss_sp;
2428 current->sas_ss_size = ss_size;
2429 }
2430
2431 if (uoss) {
2432 error = -EFAULT;
2433 if (copy_to_user(uoss, &oss, sizeof(oss)))
2434 goto out;
2435 }
2436
2437 error = 0;
2438out:
2439 return error;
2440}
2441
2442#ifdef __ARCH_WANT_SYS_SIGPENDING
2443
2444asmlinkage long
2445sys_sigpending(old_sigset_t __user *set)
2446{
2447 return do_sigpending(set, sizeof(*set));
2448}
2449
2450#endif
2451
2452#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2453/* Some platforms have their own version with special arguments others
2454 support only sys_rt_sigprocmask. */
2455
2456asmlinkage long
2457sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2458{
2459 int error;
2460 old_sigset_t old_set, new_set;
2461
2462 if (set) {
2463 error = -EFAULT;
2464 if (copy_from_user(&new_set, set, sizeof(*set)))
2465 goto out;
2466 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2467
2468 spin_lock_irq(&current->sighand->siglock);
2469 old_set = current->blocked.sig[0];
2470
2471 error = 0;
2472 switch (how) {
2473 default:
2474 error = -EINVAL;
2475 break;
2476 case SIG_BLOCK:
2477 sigaddsetmask(&current->blocked, new_set);
2478 break;
2479 case SIG_UNBLOCK:
2480 sigdelsetmask(&current->blocked, new_set);
2481 break;
2482 case SIG_SETMASK:
2483 current->blocked.sig[0] = new_set;
2484 break;
2485 }
2486
2487 recalc_sigpending();
2488 spin_unlock_irq(&current->sighand->siglock);
2489 if (error)
2490 goto out;
2491 if (oset)
2492 goto set_old;
2493 } else if (oset) {
2494 old_set = current->blocked.sig[0];
2495 set_old:
2496 error = -EFAULT;
2497 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2498 goto out;
2499 }
2500 error = 0;
2501out:
2502 return error;
2503}
2504#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2505
2506#ifdef __ARCH_WANT_SYS_RT_SIGACTION
2507asmlinkage long
2508sys_rt_sigaction(int sig,
2509 const struct sigaction __user *act,
2510 struct sigaction __user *oact,
2511 size_t sigsetsize)
2512{
2513 struct k_sigaction new_sa, old_sa;
2514 int ret = -EINVAL;
2515
2516 /* XXX: Don't preclude handling different sized sigset_t's. */
2517 if (sigsetsize != sizeof(sigset_t))
2518 goto out;
2519
2520 if (act) {
2521 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2522 return -EFAULT;
2523 }
2524
2525 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2526
2527 if (!ret && oact) {
2528 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2529 return -EFAULT;
2530 }
2531out:
2532 return ret;
2533}
2534#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2535
2536#ifdef __ARCH_WANT_SYS_SGETMASK
2537
2538/*
2539 * For backwards compatibility. Functionality superseded by sigprocmask.
2540 */
2541asmlinkage long
2542sys_sgetmask(void)
2543{
2544 /* SMP safe */
2545 return current->blocked.sig[0];
2546}
2547
2548asmlinkage long
2549sys_ssetmask(int newmask)
2550{
2551 int old;
2552
2553 spin_lock_irq(&current->sighand->siglock);
2554 old = current->blocked.sig[0];
2555
2556 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2557 sigmask(SIGSTOP)));
2558 recalc_sigpending();
2559 spin_unlock_irq(&current->sighand->siglock);
2560
2561 return old;
2562}
2563#endif /* __ARCH_WANT_SGETMASK */
2564
2565#ifdef __ARCH_WANT_SYS_SIGNAL
2566/*
2567 * For backwards compatibility. Functionality superseded by sigaction.
2568 */
2569asmlinkage unsigned long
2570sys_signal(int sig, __sighandler_t handler)
2571{
2572 struct k_sigaction new_sa, old_sa;
2573 int ret;
2574
2575 new_sa.sa.sa_handler = handler;
2576 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d72006-02-09 22:41:41 +03002577 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 ret = do_sigaction(sig, &new_sa, &old_sa);
2580
2581 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2582}
2583#endif /* __ARCH_WANT_SYS_SIGNAL */
2584
2585#ifdef __ARCH_WANT_SYS_PAUSE
2586
2587asmlinkage long
2588sys_pause(void)
2589{
2590 current->state = TASK_INTERRUPTIBLE;
2591 schedule();
2592 return -ERESTARTNOHAND;
2593}
2594
2595#endif
2596
David Woodhouse150256d2006-01-18 17:43:57 -08002597#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
2598asmlinkage long sys_rt_sigsuspend(sigset_t __user *unewset, size_t sigsetsize)
2599{
2600 sigset_t newset;
2601
2602 /* XXX: Don't preclude handling different sized sigset_t's. */
2603 if (sigsetsize != sizeof(sigset_t))
2604 return -EINVAL;
2605
2606 if (copy_from_user(&newset, unewset, sizeof(newset)))
2607 return -EFAULT;
2608 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2609
2610 spin_lock_irq(&current->sighand->siglock);
2611 current->saved_sigmask = current->blocked;
2612 current->blocked = newset;
2613 recalc_sigpending();
2614 spin_unlock_irq(&current->sighand->siglock);
2615
2616 current->state = TASK_INTERRUPTIBLE;
2617 schedule();
2618 set_thread_flag(TIF_RESTORE_SIGMASK);
2619 return -ERESTARTNOHAND;
2620}
2621#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2622
David Howellsf269fdd2006-09-27 01:50:23 -07002623__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2624{
2625 return NULL;
2626}
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628void __init signals_init(void)
2629{
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002630 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}