]> git.ipfire.org Git - people/ms/linux.git/blame - kernel/signal.c
do_task_stat: don't take rcu_read_lock()
[people/ms/linux.git] / kernel / signal.c
CommitLineData
1da177e4
LT
1/*
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
1da177e4
LT
13#include <linux/slab.h>
14#include <linux/module.h>
1da177e4
LT
15#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>
7ed20e1a 23#include <linux/signal.h>
fba2afaa 24#include <linux/signalfd.h>
c59ede7b 25#include <linux/capability.h>
7dfb7103 26#include <linux/freezer.h>
84d73786
SB
27#include <linux/pid_namespace.h>
28#include <linux/nsproxy.h>
29
1da177e4
LT
30#include <asm/param.h>
31#include <asm/uaccess.h>
32#include <asm/unistd.h>
33#include <asm/siginfo.h>
e1396065 34#include "audit.h" /* audit_signal_info() */
1da177e4
LT
35
36/*
37 * SLAB caches for signal bits.
38 */
39
e18b890b 40static struct kmem_cache *sigqueue_cachep;
1da177e4 41
1da177e4
LT
42
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 */
325d22df 58 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
1da177e4
LT
59 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
7bb44ade 99static int recalc_sigpending_tsk(struct task_struct *t)
1da177e4
LT
100{
101 if (t->signal->group_stop_count > 0 ||
102 PENDING(&t->pending, &t->blocked) ||
7bb44ade 103 PENDING(&t->signal->shared_pending, &t->blocked)) {
1da177e4 104 set_tsk_thread_flag(t, TIF_SIGPENDING);
7bb44ade
RM
105 return 1;
106 }
b74d0deb
RM
107 /*
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 */
7bb44ade
RM
112 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);
1da177e4
LT
123}
124
125void recalc_sigpending(void)
126{
cc5f916e 127 if (!recalc_sigpending_tsk(current) && !freezing(current))
b74d0deb
RM
128 clear_thread_flag(TIF_SIGPENDING);
129
1da177e4
LT
130}
131
132/* Given the mask, find the first available signal that should be serviced. */
133
fba2afaa 134int next_signal(struct sigpending *pending, sigset_t *mask)
1da177e4
LT
135{
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
dd0fc66f 167static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
1da177e4
LT
168 int override_rlimit)
169{
170 struct sigqueue *q = NULL;
10b1fbdb 171 struct user_struct *user;
1da177e4 172
10b1fbdb
LT
173 /*
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);
1da177e4 180 if (override_rlimit ||
10b1fbdb 181 atomic_read(&user->sigpending) <=
1da177e4
LT
182 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
183 q = kmem_cache_alloc(sigqueue_cachep, flags);
184 if (unlikely(q == NULL)) {
10b1fbdb 185 atomic_dec(&user->sigpending);
1da177e4
LT
186 } else {
187 INIT_LIST_HEAD(&q->list);
188 q->flags = 0;
10b1fbdb 189 q->user = get_uid(user);
1da177e4
LT
190 }
191 return(q);
192}
193
514a01b8 194static void __sigqueue_free(struct sigqueue *q)
1da177e4
LT
195{
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
6a14c5c9 203void flush_sigqueue(struct sigpending *queue)
1da177e4
LT
204{
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 */
c81addc9 218void flush_signals(struct task_struct *t)
1da177e4
LT
219{
220 unsigned long flags;
221
222 spin_lock_irqsave(&t->sighand->siglock, flags);
f5264481 223 clear_tsk_thread_flag(t, TIF_SIGPENDING);
1da177e4
LT
224 flush_sigqueue(&t->pending);
225 flush_sigqueue(&t->signal->shared_pending);
226 spin_unlock_irqrestore(&t->sighand->siglock, flags);
227}
228
10ab825b
ON
229void 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
1da177e4
LT
239/*
240 * 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
abd4f750
MAS
257int unhandled_signal(struct task_struct *tsk, int sig)
258{
b460cbc5 259 if (is_global_init(tsk))
abd4f750
MAS
260 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
1da177e4
LT
267
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
858119e1 302static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
1da177e4
LT
303{
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{
27d91e07 348 int sig = next_signal(pending, mask);
1da177e4 349
1da177e4
LT
350 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;
1da177e4 362 }
1da177e4
LT
363
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{
caec4e8d
BH
375 int signr = 0;
376
377 /* We only dequeue private signals from ourselves, we don't let
378 * signalfd steal them
379 */
b8fceee1 380 signr = __dequeue_signal(&tsk->pending, mask, info);
8bfd9a7a 381 if (!signr) {
1da177e4
LT
382 signr = __dequeue_signal(&tsk->signal->shared_pending,
383 mask, info);
8bfd9a7a
TG
384 /*
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 }
b8fceee1 408 recalc_sigpending();
8bfd9a7a
TG
409 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 }
b8fceee1 425 if (signr &&
1da177e4 426 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
f5264481 427 info->si_sys_private) {
1da177e4
LT
428 /*
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 /*
f021a3c2
MW
459 * 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
1da177e4
LT
461 * 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)
f021a3c2 467 mask |= TASK_WAKEKILL;
1da177e4
LT
468 if (!wake_up_state(t, mask))
469 kick_process(t);
470}
471
71fabd5e
GA
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.
477 *
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}
1da177e4
LT
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.
504 */
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;
7ed20e1a 530 if (!valid_signal(sig))
1da177e4 531 return error;
e54dc243 532
291041e9
AV
533 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) ||
a47afb0f 539 (task_session_nr(current) != task_session_nr(t)))
291041e9
AV
540 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
541 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
542 && !capable(CAP_KILL))
1da177e4 543 return error;
291041e9 544 }
c2f0c7c3 545
e54dc243 546 return security_task_kill(t, info, sig, 0);
1da177e4
LT
547}
548
549/* forward decl */
a1d5e21e 550static void do_notify_parent_cldstop(struct task_struct *tsk, int why);
1da177e4
LT
551
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
dd12f48d 563 if (p->signal->flags & SIGNAL_GROUP_EXIT)
1da177e4
LT
564 /*
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);
a1d5e21e 600 do_notify_parent_cldstop(p, CLD_STOPPED);
1da177e4
LT
601 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 */
f021a3c2 623 state = __TASK_STOPPED;
1da177e4
LT
624 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);
a1d5e21e 641 do_notify_parent_cldstop(p, CLD_CONTINUED);
1da177e4
LT
642 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
af7fff9c
PE
660static inline int legacy_queue(struct sigpending *signals, int sig)
661{
662 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
663}
664
1da177e4
LT
665static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
666 struct sigpending *signals)
667{
668 struct sigqueue * q = NULL;
1da177e4 669
2acb024d
PE
670 /*
671 * 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
fba2afaa
DL
678 /*
679 * Deliver the signal to listening signalfds. This must be called
680 * with the sighand lock held.
681 */
682 signalfd_notify(t, sig);
683
1da177e4
LT
684 /*
685 * fast-pathed signals for kernel-internal things like SIGSTOP
686 * or SIGKILL.
687 */
b67a1b9e 688 if (info == SEND_SIG_FORCED)
1da177e4
LT
689 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 &&
621d3121 700 (is_si_special(info) ||
1da177e4
LT
701 info->si_code >= 0)));
702 if (q) {
703 list_add_tail(&q->list, &signals->list);
704 switch ((unsigned long) info) {
b67a1b9e 705 case (unsigned long) SEND_SIG_NOINFO:
1da177e4
LT
706 q->info.si_signo = sig;
707 q->info.si_errno = 0;
708 q->info.si_code = SI_USER;
b488893a 709 q->info.si_pid = task_pid_vnr(current);
1da177e4
LT
710 q->info.si_uid = current->uid;
711 break;
b67a1b9e 712 case (unsigned long) SEND_SIG_PRIV:
1da177e4
LT
713 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 }
621d3121
ON
723 } else if (!is_si_special(info)) {
724 if (sig >= SIGRTMIN && info->si_code != SI_USER)
1da177e4
LT
725 /*
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;
1da177e4
LT
730 }
731
732out_set:
733 sigaddset(&signals->signal, sig);
2acb024d 734 return 1;
1da177e4
LT
735}
736
45807a1d
IM
737int 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",
ba25f9dc 742 current->comm, task_pid_nr(current), signr);
45807a1d 743
ca5cd877 744#if defined(__i386__) && !defined(__arch_um__)
65ea5b03 745 printk("code at %08lx: ", regs->ip);
45807a1d
IM
746 {
747 int i;
748 for (i = 0; i < 16; i++) {
749 unsigned char insn;
750
65ea5b03 751 __get_user(insn, (unsigned char *)(regs->ip + i));
45807a1d
IM
752 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);
1da177e4
LT
768
769static int
770specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
771{
2acb024d 772 int ret;
1da177e4 773
fda8bd78 774 BUG_ON(!irqs_disabled());
1da177e4
LT
775 assert_spin_locked(&t->sighand->siglock);
776
1da177e4 777 ret = send_signal(sig, info, t, &t->pending);
2acb024d
PE
778 if (ret <= 0)
779 return ret;
780
781 if (!sigismember(&t->blocked, sig))
1da177e4 782 signal_wake_up(t, sig == SIGKILL);
2acb024d 783 return 0;
1da177e4
LT
784}
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.
ae74c3b6
LT
789 *
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.
1da177e4 795 */
1da177e4
LT
796int
797force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
798{
799 unsigned long int flags;
ae74c3b6
LT
800 int ret, blocked, ignored;
801 struct k_sigaction *action;
1da177e4
LT
802
803 spin_lock_irqsave(&t->sighand->siglock, flags);
ae74c3b6
LT
804 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);
7bb44ade 811 recalc_sigpending_and_wake(t);
ae74c3b6 812 }
1da177e4
LT
813 }
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{
b0423a0d 823 force_sig_info(sig, SEND_SIG_FORCED, t);
1da177e4
LT
824}
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 */
188a1eaf
LT
834static 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;
e1abb39c 842 if (task_is_stopped_or_traced(p))
188a1eaf
LT
843 return 0;
844 return task_curr(p) || !signal_pending(p);
845}
1da177e4
LT
846
847static void
848__group_complete_signal(int sig, struct task_struct *p)
849{
1da177e4
LT
850 struct task_struct *t;
851
1da177e4
LT
852 /*
853 * 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 */
188a1eaf 858 if (wants_signal(sig, p))
1da177e4
LT
859 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;
1da177e4 874
188a1eaf 875 while (!wants_signal(sig, t)) {
1da177e4
LT
876 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);
18442cf2 912 } while_each_thread(p, t);
1da177e4
LT
913 return;
914 }
1da177e4
LT
915 }
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{
2acb024d 928 int ret;
1da177e4
LT
929
930 assert_spin_locked(&p->sighand->siglock);
931 handle_stop_signal(sig, p);
932
1da177e4
LT
933 /*
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);
2acb024d 939 if (ret <= 0)
1da177e4
LT
940 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
1da177e4
LT
953 p->signal->group_stop_count = 0;
954
1da177e4
LT
955 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
30e0fca6 962 /* SIGKILL will be handled before any pending SIGSTOP */
1da177e4 963 sigaddset(&t->pending.signal, SIGKILL);
1da177e4
LT
964 signal_wake_up(t, 1);
965 }
966}
967
b5606c2d 968int __fatal_signal_pending(struct task_struct *tsk)
f776d12d
MW
969{
970 return sigismember(&tsk->pending.signal, SIGKILL);
971}
13f09b95 972EXPORT_SYMBOL(__fatal_signal_pending);
f776d12d 973
f63ee72e
ON
974struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
975{
976 struct sighand_struct *sighand;
977
1406f2d3 978 rcu_read_lock();
f63ee72e
ON
979 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 }
1406f2d3 989 rcu_read_unlock();
f63ee72e
ON
990
991 return sighand;
992}
993
1da177e4
LT
994int 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);
f63ee72e
ON
1000
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);
2d89c929 1006 }
1da177e4
LT
1007 }
1008
1009 return ret;
1010}
1011
1012/*
146a505d 1013 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1da177e4
LT
1014 * control characters do (^C, ^Z etc)
1015 */
1016
c4b92fc1 1017int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1da177e4
LT
1018{
1019 struct task_struct *p = NULL;
1020 int retval, success;
1021
1da177e4
LT
1022 success = 0;
1023 retval = -ESRCH;
c4b92fc1 1024 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
1025 int err = group_send_sig_info(sig, info, p);
1026 success |= !err;
1027 retval = err;
c4b92fc1 1028 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
1029 return success ? 0 : retval;
1030}
1031
c4b92fc1 1032int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1da177e4 1033{
d36174bc 1034 int error = -ESRCH;
1da177e4
LT
1035 struct task_struct *p;
1036
e56d0903 1037 rcu_read_lock();
0c12b517 1038 if (unlikely(sig_needs_tasklist(sig)))
e56d0903 1039 read_lock(&tasklist_lock);
0c12b517 1040
d36174bc 1041retry:
c4b92fc1 1042 p = pid_task(pid, PIDTYPE_PID);
d36174bc 1043 if (p) {
1da177e4 1044 error = group_send_sig_info(sig, info, p);
d36174bc
ON
1045 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 }
0c12b517
ON
1054
1055 if (unlikely(sig_needs_tasklist(sig)))
e56d0903
IM
1056 read_unlock(&tasklist_lock);
1057 rcu_read_unlock();
1da177e4
LT
1058 return error;
1059}
1060
c3de4b38
MW
1061int
1062kill_proc_info(int sig, struct siginfo *info, pid_t pid)
c4b92fc1
EB
1063{
1064 int error;
1065 rcu_read_lock();
b488893a 1066 error = kill_pid_info(sig, info, find_vpid(pid));
c4b92fc1
EB
1067 rcu_read_unlock();
1068 return error;
1069}
1070
2425c08b
EB
1071/* 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,
8f95dc58 1073 uid_t uid, uid_t euid, u32 secid)
46113830
HW
1074{
1075 int ret = -EINVAL;
1076 struct task_struct *p;
1077
1078 if (!valid_signal(sig))
1079 return ret;
1080
1081 read_lock(&tasklist_lock);
2425c08b 1082 p = pid_task(pid, PIDTYPE_PID);
46113830
HW
1083 if (!p) {
1084 ret = -ESRCH;
1085 goto out_unlock;
1086 }
0811af28 1087 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
46113830
HW
1088 && (euid != p->suid) && (euid != p->uid)
1089 && (uid != p->suid) && (uid != p->uid)) {
1090 ret = -EPERM;
1091 goto out_unlock;
1092 }
8f95dc58
DQ
1093 ret = security_task_kill(p, info, sig, secid);
1094 if (ret)
1095 goto out_unlock;
46113830
HW
1096 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}
2425c08b 1106EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1da177e4
LT
1107
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{
8d42db18 1117 int ret;
d5df763b
PE
1118
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 {
1da177e4
LT
1131 int retval = 0, count = 0;
1132 struct task_struct * p;
1133
1da177e4 1134 for_each_process(p) {
bac0abd6 1135 if (p->pid > 1 && !same_thread_group(p, current)) {
1da177e4
LT
1136 int err = group_send_sig_info(sig, info, p);
1137 ++count;
1138 if (err != -EPERM)
1139 retval = err;
1140 }
1141 }
8d42db18 1142 ret = count ? retval : -ESRCH;
1da177e4 1143 }
d5df763b
PE
1144 read_unlock(&tasklist_lock);
1145
8d42db18 1146 return ret;
1da177e4
LT
1147}
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 */
7ed20e1a 1167 if (!valid_signal(sig))
1da177e4
LT
1168 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
b67a1b9e
ON
1184#define __si_special(priv) \
1185 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1186
1da177e4
LT
1187int
1188send_sig(int sig, struct task_struct *p, int priv)
1189{
b67a1b9e 1190 return send_sig_info(sig, __si_special(priv), p);
1da177e4
LT
1191}
1192
1da177e4
LT
1193void
1194force_sig(int sig, struct task_struct *p)
1195{
b67a1b9e 1196 force_sig_info(sig, SEND_SIG_PRIV, p);
1da177e4
LT
1197}
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
c4b92fc1
EB
1218int kill_pgrp(struct pid *pid, int sig, int priv)
1219{
146a505d
PE
1220 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;
c4b92fc1
EB
1227}
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
1da177e4
LT
1236int
1237kill_proc(pid_t pid, int sig, int priv)
1238{
b488893a
PE
1239 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;
1da177e4
LT
1245}
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;
60187d27
ON
1269 spinlock_t *lock = &current->sighand->siglock;
1270
1da177e4
LT
1271 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1272 /*
1273 * If the signal is still pending remove it from the
60187d27
ON
1274 * pending queue. We must hold ->siglock while testing
1275 * q->list to serialize with collect_signal().
1da177e4 1276 */
60187d27
ON
1277 spin_lock_irqsave(lock, flags);
1278 if (!list_empty(&q->list))
1279 list_del_init(&q->list);
1280 spin_unlock_irqrestore(lock, flags);
1281
1da177e4
LT
1282 q->flags &= ~SIGQUEUE_PREALLOC;
1283 __sigqueue_free(q);
1284}
1285
54767908 1286int send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1da177e4
LT
1287{
1288 unsigned long flags;
1289 int ret = 0;
1290
1da177e4 1291 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
e56d0903
IM
1292
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();
e752dd6c 1302
54767908 1303 if (!likely(lock_task_sighand(p, &flags))) {
e752dd6c
ON
1304 ret = -1;
1305 goto out_err;
1306 }
1307
1da177e4
LT
1308 if (unlikely(!list_empty(&q->list))) {
1309 /*
1310 * If an SI_TIMER entry is already queue just increment
1311 * the overrun count.
1312 */
54767908 1313 BUG_ON(q->info.si_code != SI_TIMER);
1da177e4
LT
1314 q->info.si_overrun++;
1315 goto out;
e752dd6c 1316 }
1da177e4
LT
1317 /* Short-circuit ignored signals. */
1318 if (sig_ignored(p, sig)) {
1319 ret = 1;
1320 goto out;
1321 }
fba2afaa
DL
1322 /*
1323 * Deliver the signal to listening signalfds. This must be called
1324 * with the sighand lock held.
1325 */
1326 signalfd_notify(p, sig);
1da177e4 1327
1da177e4
LT
1328 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:
54767908 1334 unlock_task_sighand(p, &flags);
e752dd6c 1335out_err:
e56d0903 1336 rcu_read_unlock();
e752dd6c
ON
1337
1338 return ret;
1da177e4
LT
1339}
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));
e56d0903 1348
1da177e4 1349 read_lock(&tasklist_lock);
e56d0903 1350 /* Since it_lock is held, p->sighand cannot be NULL. */
1da177e4
LT
1351 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 */
fda8bd78 1366 BUG_ON(q->info.si_code != SI_TIMER);
1da177e4
LT
1367 q->info.si_overrun++;
1368 goto out;
1369 }
fba2afaa
DL
1370 /*
1371 * Deliver the signal to listening signalfds. This must be called
1372 * with the sighand lock held.
1373 */
1374 signalfd_notify(p, sig);
1da177e4
LT
1375
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 */
1da177e4
LT
1381 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);
e56d0903 1388 return ret;
1da177e4
LT
1389}
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. */
e1abb39c 1414 BUG_ON(task_is_stopped_or_traced(tsk));
1da177e4
LT
1415
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;
b488893a
PE
1421 /*
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
1da177e4
LT
1437 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);
7ed0175a 1457 if (!tsk->ptrace && sig == SIGCHLD &&
1da177e4
LT
1458 (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 }
7ed20e1a 1479 if (valid_signal(sig) && sig > 0)
1da177e4
LT
1480 __group_send_sig_info(sig, &info, tsk->parent);
1481 __wake_up_parent(tsk, tsk->parent);
1482 spin_unlock_irqrestore(&psig->siglock, flags);
1483}
1484
a1d5e21e 1485static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1da177e4
LT
1486{
1487 struct siginfo info;
1488 unsigned long flags;
bc505a47 1489 struct task_struct *parent;
1da177e4
LT
1490 struct sighand_struct *sighand;
1491
a1d5e21e 1492 if (tsk->ptrace & PT_PTRACED)
bc505a47
ON
1493 parent = tsk->parent;
1494 else {
1495 tsk = tsk->group_leader;
1496 parent = tsk->real_parent;
1497 }
1498
1da177e4
LT
1499 info.si_signo = SIGCHLD;
1500 info.si_errno = 0;
b488893a
PE
1501 /*
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
1da177e4
LT
1508 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
d5f70c00
ON
1541static inline int may_ptrace_stop(void)
1542{
1543 if (!likely(current->ptrace & PT_PTRACED))
1544 return 0;
d5f70c00
ON
1545 /*
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
1a669c2f
RM
1561/*
1562 * 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
1da177e4
LT
1572/*
1573 * 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 *
20686a30
ON
1580 * If we actually decide not to stop at all because the tracer
1581 * is gone, we keep current->exit_code unless clear_code.
1da177e4 1582 */
20686a30 1583static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1da177e4 1584{
1a669c2f
RM
1585 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
1da177e4
LT
1605 /*
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. */
d9ae90ac 1616 __set_current_state(TASK_TRACED);
1da177e4
LT
1617 spin_unlock_irq(&current->sighand->siglock);
1618 read_lock(&tasklist_lock);
1a669c2f 1619 if (!unlikely(killed) && may_ptrace_stop()) {
a1d5e21e 1620 do_notify_parent_cldstop(current, CLD_TRAPPED);
1da177e4
LT
1621 read_unlock(&tasklist_lock);
1622 schedule();
1623 } else {
1624 /*
1625 * By the time we got the lock, our tracer went away.
6405f7f4 1626 * Don't drop the lock yet, another tracer may come.
1da177e4 1627 */
6405f7f4 1628 __set_current_state(TASK_RUNNING);
20686a30
ON
1629 if (clear_code)
1630 current->exit_code = 0;
6405f7f4 1631 read_unlock(&tasklist_lock);
1da177e4
LT
1632 }
1633
13b1c3d4
RM
1634 /*
1635 * 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
1da177e4
LT
1641 /*
1642 * 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.
b74d0deb 1652 * This sets TIF_SIGPENDING, but never clears it.
1da177e4 1653 */
b74d0deb 1654 recalc_sigpending_tsk(current);
1da177e4
LT
1655}
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;
b488893a 1666 info.si_pid = task_pid_vnr(current);
1da177e4
LT
1667 info.si_uid = current->uid;
1668
1669 /* Let the debugger run. */
1670 spin_lock_irq(&current->sighand->siglock);
20686a30 1671 ptrace_stop(exit_code, 1, &info);
1da177e4
LT
1672 spin_unlock_irq(&current->sighand->siglock);
1673}
1674
1da177e4
LT
1675static 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 */
a1d5e21e
ON
1683 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 }
bc505a47 1688
3df494a3
RW
1689 do {
1690 schedule();
1691 } while (try_to_freeze());
1da177e4
LT
1692 /*
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 */
a122b341 1704static int do_signal_stop(int signr)
1da177e4
LT
1705{
1706 struct signal_struct *sig = current->signal;
dac27f4a 1707 int stop_count;
1da177e4 1708
1da177e4
LT
1709 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 */
1da177e4 1714 stop_count = --sig->group_stop_count;
dac27f4a 1715 } else {
f558b7e4
ON
1716 struct task_struct *t;
1717
ed5d2cac 1718 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
573cf9ad 1719 unlikely(signal_group_exit(sig)))
f558b7e4 1720 return 0;
1da177e4
LT
1721 /*
1722 * There is no group stop already in progress.
a122b341 1723 * We must initiate one now.
1da177e4 1724 */
a122b341 1725 sig->group_exit_code = signr;
1da177e4 1726
a122b341
ON
1727 stop_count = 0;
1728 for (t = next_thread(current); t != current; t = next_thread(t))
1da177e4 1729 /*
a122b341
ON
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.
1da177e4 1733 */
d12619b5 1734 if (!(t->flags & PF_EXITING) &&
e1abb39c 1735 !task_is_stopped_or_traced(t)) {
a122b341
ON
1736 stop_count++;
1737 signal_wake_up(t, 0);
1738 }
1739 sig->group_stop_count = stop_count;
1da177e4
LT
1740 }
1741
dac27f4a
ON
1742 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);
1da177e4
LT
1748 finish_stop(stop_count);
1749 return 1;
1750}
1751
18c98b65
RM
1752static 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
1da177e4
LT
1791int 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
13b1c3d4
RM
1797relock:
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 */
fc558a74
RW
1804 try_to_freeze();
1805
1da177e4
LT
1806 spin_lock_irq(&current->sighand->siglock);
1807 for (;;) {
1808 struct k_sigaction *ka;
1809
1810 if (unlikely(current->signal->group_stop_count > 0) &&
f558b7e4 1811 do_signal_stop(0))
1da177e4
LT
1812 goto relock;
1813
1814 signr = dequeue_signal(current, mask, info);
1815
1816 if (!signr)
1817 break; /* will return 0 */
1818
18c98b65
RM
1819 if (signr != SIGKILL) {
1820 signr = ptrace_signal(signr, info, regs, cookie);
1821 if (!signr)
1da177e4 1822 continue;
1da177e4
LT
1823 }
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
84d73786 1844 /*
0fbc26a6 1845 * Global init gets no signals it doesn't want.
84d73786 1846 */
0fbc26a6 1847 if (is_global_init(current))
1da177e4
LT
1848 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
3e7cd6c4 1866 if (is_current_pgrp_orphaned())
1da177e4
LT
1867 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;
45807a1d
IM
1890 if ((signr != SIGKILL) && print_fatal_signals)
1891 print_fatal_signal(regs, signr);
1da177e4
LT
1892 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
d12619b5
ON
1914void exit_signals(struct task_struct *tsk)
1915{
1916 int group_stop = 0;
5dee1707 1917 struct task_struct *t;
d12619b5 1918
5dee1707
ON
1919 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
1920 tsk->flags |= PF_EXITING;
1921 return;
d12619b5
ON
1922 }
1923
5dee1707 1924 spin_lock_irq(&tsk->sighand->siglock);
d12619b5
ON
1925 /*
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;
5dee1707
ON
1930 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:
d12619b5
ON
1947 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
1da177e4
LT
1956EXPORT_SYMBOL(recalc_sigpending);
1957EXPORT_SYMBOL_GPL(dequeue_signal);
1958EXPORT_SYMBOL(flush_signals);
1959EXPORT_SYMBOL(force_sig);
1da177e4
LT
1960EXPORT_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;
1da177e4
LT
2001
2002 spin_lock_irq(&current->sighand->siglock);
a26fd335
ON
2003 if (oldset)
2004 *oldset = current->blocked;
2005
1da177e4
LT
2006 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);
a26fd335 2022
1da177e4
LT
2023 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.
fba2afaa
DL
2106 * Please remember to update the signalfd_copyinfo() function
2107 * inside fs/signalfd.c too, in case siginfo_t changes.
1da177e4
LT
2108 * 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
75bcc8c5 2209 timeout = schedule_timeout_interruptible(timeout);
1da177e4 2210
1da177e4
LT
2211 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;
b488893a 2243 info.si_pid = task_tgid_vnr(current);
1da177e4
LT
2244 info.si_uid = current->uid;
2245
2246 return kill_something_info(sig, &info, pid);
2247}
2248
6dd69f10 2249static int do_tkill(int tgid, int pid, int sig)
1da177e4 2250{
1da177e4 2251 int error;
6dd69f10 2252 struct siginfo info;
1da177e4
LT
2253 struct task_struct *p;
2254
6dd69f10 2255 error = -ESRCH;
1da177e4
LT
2256 info.si_signo = sig;
2257 info.si_errno = 0;
2258 info.si_code = SI_TKILL;
b488893a 2259 info.si_pid = task_tgid_vnr(current);
1da177e4
LT
2260 info.si_uid = current->uid;
2261
2262 read_lock(&tasklist_lock);
228ebcbe 2263 p = find_task_by_vpid(pid);
b488893a 2264 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
1da177e4
LT
2265 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);
6dd69f10 2278
1da177e4
LT
2279 return error;
2280}
2281
6dd69f10
VL
2282/**
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 *
72fd4a35 2288 * This syscall also checks the @tgid and returns -ESRCH even if the PID
6dd69f10
VL
2289 * 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{
2294 /* This is only valid for single tasks */
2295 if (pid <= 0 || tgid <= 0)
2296 return -EINVAL;
2297
2298 return do_tkill(tgid, pid, sig);
2299}
2300
1da177e4
LT
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{
1da177e4
LT
2307 /* This is only valid for single tasks */
2308 if (pid <= 0)
2309 return -EINVAL;
2310
6dd69f10 2311 return do_tkill(0, pid, sig);
1da177e4
LT
2312}
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
88531f72 2332int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
1da177e4
LT
2333{
2334 struct k_sigaction *k;
71fabd5e 2335 sigset_t mask;
1da177e4 2336
7ed20e1a 2337 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
1da177e4
LT
2338 return -EINVAL;
2339
2340 k = &current->sighand->action[sig-1];
2341
2342 spin_lock_irq(&current->sighand->siglock);
1da177e4
LT
2343 if (oact)
2344 *oact = *k;
2345
2346 if (act) {
9ac95f2f
ON
2347 sigdelsetmask(&act->sa.sa_mask,
2348 sigmask(SIGKILL) | sigmask(SIGSTOP));
88531f72 2349 *k = *act;
1da177e4
LT
2350 /*
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 ||
88531f72 2362 (act->sa.sa_handler == SIG_DFL && sig_kernel_ignore(sig))) {
1da177e4 2363 struct task_struct *t = current;
71fabd5e
GA
2364 sigemptyset(&mask);
2365 sigaddset(&mask, sig);
2366 rm_from_queue_full(&mask, &t->signal->shared_pending);
1da177e4 2367 do {
71fabd5e 2368 rm_from_queue_full(&mask, &t->pending);
1da177e4
LT
2369 t = next_thread(t);
2370 } while (t != current);
1da177e4 2371 }
1da177e4
LT
2372 }
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;
c70d3d70 2577 sigemptyset(&new_sa.sa.sa_mask);
1da177e4
LT
2578
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
150256d8
DW
2597#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
f269fdd1
DH
2623__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2624{
2625 return NULL;
2626}
2627
1da177e4
LT
2628void __init signals_init(void)
2629{
0a31bd5f 2630 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
1da177e4 2631}