]> git.ipfire.org Git - people/ms/linux.git/blame - kernel/signal.c
exit: exit_notify() can trust signal->notify_count < 0
[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>
f84d49b2 25#include <linux/ratelimit.h>
35de254d 26#include <linux/tracehook.h>
c59ede7b 27#include <linux/capability.h>
7dfb7103 28#include <linux/freezer.h>
84d73786
SB
29#include <linux/pid_namespace.h>
30#include <linux/nsproxy.h>
d1eb650f
MH
31#define CREATE_TRACE_POINTS
32#include <trace/events/signal.h>
84d73786 33
1da177e4
LT
34#include <asm/param.h>
35#include <asm/uaccess.h>
36#include <asm/unistd.h>
37#include <asm/siginfo.h>
e1396065 38#include "audit.h" /* audit_signal_info() */
1da177e4
LT
39
40/*
41 * SLAB caches for signal bits.
42 */
43
e18b890b 44static struct kmem_cache *sigqueue_cachep;
1da177e4 45
f84d49b2
NO
46int print_fatal_signals __read_mostly;
47
35de254d 48static void __user *sig_handler(struct task_struct *t, int sig)
93585eea 49{
35de254d
RM
50 return t->sighand->action[sig - 1].sa.sa_handler;
51}
93585eea 52
35de254d
RM
53static int sig_handler_ignored(void __user *handler, int sig)
54{
93585eea 55 /* Is it explicitly or implicitly ignored? */
93585eea
PE
56 return handler == SIG_IGN ||
57 (handler == SIG_DFL && sig_kernel_ignore(sig));
58}
1da177e4 59
921cf9f6
SB
60static int sig_task_ignored(struct task_struct *t, int sig,
61 int from_ancestor_ns)
1da177e4 62{
35de254d 63 void __user *handler;
1da177e4 64
f008faff
ON
65 handler = sig_handler(t, sig);
66
67 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
921cf9f6 68 handler == SIG_DFL && !from_ancestor_ns)
f008faff
ON
69 return 1;
70
71 return sig_handler_ignored(handler, sig);
72}
73
921cf9f6 74static int sig_ignored(struct task_struct *t, int sig, int from_ancestor_ns)
f008faff 75{
1da177e4
LT
76 /*
77 * Blocked signals are never ignored, since the
78 * signal handler may change by the time it is
79 * unblocked.
80 */
325d22df 81 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
1da177e4
LT
82 return 0;
83
921cf9f6 84 if (!sig_task_ignored(t, sig, from_ancestor_ns))
35de254d
RM
85 return 0;
86
87 /*
88 * Tracers may want to know about even ignored signals.
89 */
43918f2b 90 return !tracehook_consider_ignored_signal(t, sig);
1da177e4
LT
91}
92
93/*
94 * Re-calculate pending state from the set of locally pending
95 * signals, globally pending signals, and blocked signals.
96 */
97static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
98{
99 unsigned long ready;
100 long i;
101
102 switch (_NSIG_WORDS) {
103 default:
104 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
105 ready |= signal->sig[i] &~ blocked->sig[i];
106 break;
107
108 case 4: ready = signal->sig[3] &~ blocked->sig[3];
109 ready |= signal->sig[2] &~ blocked->sig[2];
110 ready |= signal->sig[1] &~ blocked->sig[1];
111 ready |= signal->sig[0] &~ blocked->sig[0];
112 break;
113
114 case 2: ready = signal->sig[1] &~ blocked->sig[1];
115 ready |= signal->sig[0] &~ blocked->sig[0];
116 break;
117
118 case 1: ready = signal->sig[0] &~ blocked->sig[0];
119 }
120 return ready != 0;
121}
122
123#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
124
7bb44ade 125static int recalc_sigpending_tsk(struct task_struct *t)
1da177e4
LT
126{
127 if (t->signal->group_stop_count > 0 ||
128 PENDING(&t->pending, &t->blocked) ||
7bb44ade 129 PENDING(&t->signal->shared_pending, &t->blocked)) {
1da177e4 130 set_tsk_thread_flag(t, TIF_SIGPENDING);
7bb44ade
RM
131 return 1;
132 }
b74d0deb
RM
133 /*
134 * We must never clear the flag in another thread, or in current
135 * when it's possible the current syscall is returning -ERESTART*.
136 * So we don't clear it here, and only callers who know they should do.
137 */
7bb44ade
RM
138 return 0;
139}
140
141/*
142 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
143 * This is superfluous when called on current, the wakeup is a harmless no-op.
144 */
145void recalc_sigpending_and_wake(struct task_struct *t)
146{
147 if (recalc_sigpending_tsk(t))
148 signal_wake_up(t, 0);
1da177e4
LT
149}
150
151void recalc_sigpending(void)
152{
b787f7ba
RM
153 if (unlikely(tracehook_force_sigpending()))
154 set_thread_flag(TIF_SIGPENDING);
155 else if (!recalc_sigpending_tsk(current) && !freezing(current))
b74d0deb
RM
156 clear_thread_flag(TIF_SIGPENDING);
157
1da177e4
LT
158}
159
160/* Given the mask, find the first available signal that should be serviced. */
161
a27341cd
LT
162#define SYNCHRONOUS_MASK \
163 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
164 sigmask(SIGTRAP) | sigmask(SIGFPE))
165
fba2afaa 166int next_signal(struct sigpending *pending, sigset_t *mask)
1da177e4
LT
167{
168 unsigned long i, *s, *m, x;
169 int sig = 0;
f84d49b2 170
1da177e4
LT
171 s = pending->signal.sig;
172 m = mask->sig;
a27341cd
LT
173
174 /*
175 * Handle the first word specially: it contains the
176 * synchronous signals that need to be dequeued first.
177 */
178 x = *s &~ *m;
179 if (x) {
180 if (x & SYNCHRONOUS_MASK)
181 x &= SYNCHRONOUS_MASK;
182 sig = ffz(~x) + 1;
183 return sig;
184 }
185
1da177e4
LT
186 switch (_NSIG_WORDS) {
187 default:
a27341cd
LT
188 for (i = 1; i < _NSIG_WORDS; ++i) {
189 x = *++s &~ *++m;
190 if (!x)
191 continue;
192 sig = ffz(~x) + i*_NSIG_BPW + 1;
193 break;
194 }
1da177e4
LT
195 break;
196
a27341cd
LT
197 case 2:
198 x = s[1] &~ m[1];
199 if (!x)
1da177e4 200 break;
a27341cd 201 sig = ffz(~x) + _NSIG_BPW + 1;
1da177e4
LT
202 break;
203
a27341cd
LT
204 case 1:
205 /* Nothing to do */
1da177e4
LT
206 break;
207 }
f84d49b2 208
1da177e4
LT
209 return sig;
210}
211
f84d49b2
NO
212static inline void print_dropped_signal(int sig)
213{
214 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
215
216 if (!print_fatal_signals)
217 return;
218
219 if (!__ratelimit(&ratelimit_state))
220 return;
221
222 printk(KERN_INFO "%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
223 current->comm, current->pid, sig);
224}
225
c69e8d9c
DH
226/*
227 * allocate a new signal queue record
228 * - this may be called without locks if and only if t == current, otherwise an
d84f4f99 229 * appopriate lock must be held to stop the target task from exiting
c69e8d9c 230 */
f84d49b2
NO
231static struct sigqueue *
232__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
1da177e4
LT
233{
234 struct sigqueue *q = NULL;
10b1fbdb 235 struct user_struct *user;
1da177e4 236
10b1fbdb 237 /*
7cf7db8d
TG
238 * Protect access to @t credentials. This can go away when all
239 * callers hold rcu read lock.
10b1fbdb 240 */
7cf7db8d 241 rcu_read_lock();
d84f4f99 242 user = get_uid(__task_cred(t)->user);
10b1fbdb 243 atomic_inc(&user->sigpending);
7cf7db8d 244 rcu_read_unlock();
f84d49b2 245
1da177e4 246 if (override_rlimit ||
10b1fbdb 247 atomic_read(&user->sigpending) <=
78d7d407 248 task_rlimit(t, RLIMIT_SIGPENDING)) {
1da177e4 249 q = kmem_cache_alloc(sigqueue_cachep, flags);
f84d49b2
NO
250 } else {
251 print_dropped_signal(sig);
252 }
253
1da177e4 254 if (unlikely(q == NULL)) {
10b1fbdb 255 atomic_dec(&user->sigpending);
d84f4f99 256 free_uid(user);
1da177e4
LT
257 } else {
258 INIT_LIST_HEAD(&q->list);
259 q->flags = 0;
d84f4f99 260 q->user = user;
1da177e4 261 }
d84f4f99
DH
262
263 return q;
1da177e4
LT
264}
265
514a01b8 266static void __sigqueue_free(struct sigqueue *q)
1da177e4
LT
267{
268 if (q->flags & SIGQUEUE_PREALLOC)
269 return;
270 atomic_dec(&q->user->sigpending);
271 free_uid(q->user);
272 kmem_cache_free(sigqueue_cachep, q);
273}
274
6a14c5c9 275void flush_sigqueue(struct sigpending *queue)
1da177e4
LT
276{
277 struct sigqueue *q;
278
279 sigemptyset(&queue->signal);
280 while (!list_empty(&queue->list)) {
281 q = list_entry(queue->list.next, struct sigqueue , list);
282 list_del_init(&q->list);
283 __sigqueue_free(q);
284 }
285}
286
287/*
288 * Flush all pending signals for a task.
289 */
3bcac026
DH
290void __flush_signals(struct task_struct *t)
291{
292 clear_tsk_thread_flag(t, TIF_SIGPENDING);
293 flush_sigqueue(&t->pending);
294 flush_sigqueue(&t->signal->shared_pending);
295}
296
c81addc9 297void flush_signals(struct task_struct *t)
1da177e4
LT
298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&t->sighand->siglock, flags);
3bcac026 302 __flush_signals(t);
1da177e4
LT
303 spin_unlock_irqrestore(&t->sighand->siglock, flags);
304}
305
cbaffba1
ON
306static void __flush_itimer_signals(struct sigpending *pending)
307{
308 sigset_t signal, retain;
309 struct sigqueue *q, *n;
310
311 signal = pending->signal;
312 sigemptyset(&retain);
313
314 list_for_each_entry_safe(q, n, &pending->list, list) {
315 int sig = q->info.si_signo;
316
317 if (likely(q->info.si_code != SI_TIMER)) {
318 sigaddset(&retain, sig);
319 } else {
320 sigdelset(&signal, sig);
321 list_del_init(&q->list);
322 __sigqueue_free(q);
323 }
324 }
325
326 sigorsets(&pending->signal, &signal, &retain);
327}
328
329void flush_itimer_signals(void)
330{
331 struct task_struct *tsk = current;
332 unsigned long flags;
333
334 spin_lock_irqsave(&tsk->sighand->siglock, flags);
335 __flush_itimer_signals(&tsk->pending);
336 __flush_itimer_signals(&tsk->signal->shared_pending);
337 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
338}
339
10ab825b
ON
340void ignore_signals(struct task_struct *t)
341{
342 int i;
343
344 for (i = 0; i < _NSIG; ++i)
345 t->sighand->action[i].sa.sa_handler = SIG_IGN;
346
347 flush_signals(t);
348}
349
1da177e4
LT
350/*
351 * Flush all handlers for a task.
352 */
353
354void
355flush_signal_handlers(struct task_struct *t, int force_default)
356{
357 int i;
358 struct k_sigaction *ka = &t->sighand->action[0];
359 for (i = _NSIG ; i != 0 ; i--) {
360 if (force_default || ka->sa.sa_handler != SIG_IGN)
361 ka->sa.sa_handler = SIG_DFL;
362 ka->sa.sa_flags = 0;
363 sigemptyset(&ka->sa.sa_mask);
364 ka++;
365 }
366}
367
abd4f750
MAS
368int unhandled_signal(struct task_struct *tsk, int sig)
369{
445a91d2 370 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
b460cbc5 371 if (is_global_init(tsk))
abd4f750 372 return 1;
445a91d2 373 if (handler != SIG_IGN && handler != SIG_DFL)
abd4f750 374 return 0;
43918f2b 375 return !tracehook_consider_fatal_signal(tsk, sig);
abd4f750
MAS
376}
377
1da177e4
LT
378
379/* Notify the system that a driver wants to block all signals for this
380 * process, and wants to be notified if any signals at all were to be
381 * sent/acted upon. If the notifier routine returns non-zero, then the
382 * signal will be acted upon after all. If the notifier routine returns 0,
383 * then then signal will be blocked. Only one block per process is
384 * allowed. priv is a pointer to private data that the notifier routine
385 * can use to determine if the signal should be blocked or not. */
386
387void
388block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
389{
390 unsigned long flags;
391
392 spin_lock_irqsave(&current->sighand->siglock, flags);
393 current->notifier_mask = mask;
394 current->notifier_data = priv;
395 current->notifier = notifier;
396 spin_unlock_irqrestore(&current->sighand->siglock, flags);
397}
398
399/* Notify the system that blocking has ended. */
400
401void
402unblock_all_signals(void)
403{
404 unsigned long flags;
405
406 spin_lock_irqsave(&current->sighand->siglock, flags);
407 current->notifier = NULL;
408 current->notifier_data = NULL;
409 recalc_sigpending();
410 spin_unlock_irqrestore(&current->sighand->siglock, flags);
411}
412
100360f0 413static void collect_signal(int sig, struct sigpending *list, siginfo_t *info)
1da177e4
LT
414{
415 struct sigqueue *q, *first = NULL;
1da177e4 416
1da177e4
LT
417 /*
418 * Collect the siginfo appropriate to this signal. Check if
419 * there is another siginfo for the same signal.
420 */
421 list_for_each_entry(q, &list->list, list) {
422 if (q->info.si_signo == sig) {
d4434207
ON
423 if (first)
424 goto still_pending;
1da177e4
LT
425 first = q;
426 }
427 }
d4434207
ON
428
429 sigdelset(&list->signal, sig);
430
1da177e4 431 if (first) {
d4434207 432still_pending:
1da177e4
LT
433 list_del_init(&first->list);
434 copy_siginfo(info, &first->info);
435 __sigqueue_free(first);
1da177e4 436 } else {
1da177e4
LT
437 /* Ok, it wasn't in the queue. This must be
438 a fast-pathed signal or we must have been
439 out of queue space. So zero out the info.
440 */
1da177e4
LT
441 info->si_signo = sig;
442 info->si_errno = 0;
7486e5d9 443 info->si_code = SI_USER;
1da177e4
LT
444 info->si_pid = 0;
445 info->si_uid = 0;
446 }
1da177e4
LT
447}
448
449static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
450 siginfo_t *info)
451{
27d91e07 452 int sig = next_signal(pending, mask);
1da177e4 453
1da177e4
LT
454 if (sig) {
455 if (current->notifier) {
456 if (sigismember(current->notifier_mask, sig)) {
457 if (!(current->notifier)(current->notifier_data)) {
458 clear_thread_flag(TIF_SIGPENDING);
459 return 0;
460 }
461 }
462 }
463
100360f0 464 collect_signal(sig, pending, info);
1da177e4 465 }
1da177e4
LT
466
467 return sig;
468}
469
470/*
471 * Dequeue a signal and return the element to the caller, which is
472 * expected to free it.
473 *
474 * All callers have to hold the siglock.
475 */
476int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
477{
c5363d03 478 int signr;
caec4e8d
BH
479
480 /* We only dequeue private signals from ourselves, we don't let
481 * signalfd steal them
482 */
b8fceee1 483 signr = __dequeue_signal(&tsk->pending, mask, info);
8bfd9a7a 484 if (!signr) {
1da177e4
LT
485 signr = __dequeue_signal(&tsk->signal->shared_pending,
486 mask, info);
8bfd9a7a
TG
487 /*
488 * itimer signal ?
489 *
490 * itimers are process shared and we restart periodic
491 * itimers in the signal delivery path to prevent DoS
492 * attacks in the high resolution timer case. This is
493 * compliant with the old way of self restarting
494 * itimers, as the SIGALRM is a legacy signal and only
495 * queued once. Changing the restart behaviour to
496 * restart the timer in the signal dequeue path is
497 * reducing the timer noise on heavy loaded !highres
498 * systems too.
499 */
500 if (unlikely(signr == SIGALRM)) {
501 struct hrtimer *tmr = &tsk->signal->real_timer;
502
503 if (!hrtimer_is_queued(tmr) &&
504 tsk->signal->it_real_incr.tv64 != 0) {
505 hrtimer_forward(tmr, tmr->base->get_time(),
506 tsk->signal->it_real_incr);
507 hrtimer_restart(tmr);
508 }
509 }
510 }
c5363d03 511
b8fceee1 512 recalc_sigpending();
c5363d03
PE
513 if (!signr)
514 return 0;
515
516 if (unlikely(sig_kernel_stop(signr))) {
8bfd9a7a
TG
517 /*
518 * Set a marker that we have dequeued a stop signal. Our
519 * caller might release the siglock and then the pending
520 * stop signal it is about to process is no longer in the
521 * pending bitmasks, but must still be cleared by a SIGCONT
522 * (and overruled by a SIGKILL). So those cases clear this
523 * shared flag after we've set it. Note that this flag may
524 * remain set after the signal we return is ignored or
525 * handled. That doesn't matter because its only purpose
526 * is to alert stop-signal processing code when another
527 * processor has come along and cleared the flag.
528 */
92413d77 529 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
8bfd9a7a 530 }
c5363d03 531 if ((info->si_code & __SI_MASK) == __SI_TIMER && info->si_sys_private) {
1da177e4
LT
532 /*
533 * Release the siglock to ensure proper locking order
534 * of timer locks outside of siglocks. Note, we leave
535 * irqs disabled here, since the posix-timers code is
536 * about to disable them again anyway.
537 */
538 spin_unlock(&tsk->sighand->siglock);
539 do_schedule_next_timer(info);
540 spin_lock(&tsk->sighand->siglock);
541 }
542 return signr;
543}
544
545/*
546 * Tell a process that it has a new active signal..
547 *
548 * NOTE! we rely on the previous spin_lock to
549 * lock interrupts for us! We can only be called with
550 * "siglock" held, and the local interrupt must
551 * have been disabled when that got acquired!
552 *
553 * No need to set need_resched since signal event passing
554 * goes through ->blocked
555 */
556void signal_wake_up(struct task_struct *t, int resume)
557{
558 unsigned int mask;
559
560 set_tsk_thread_flag(t, TIF_SIGPENDING);
561
562 /*
f021a3c2
MW
563 * For SIGKILL, we want to wake it up in the stopped/traced/killable
564 * case. We don't check t->state here because there is a race with it
1da177e4
LT
565 * executing another processor and just now entering stopped state.
566 * By using wake_up_state, we ensure the process will wake up and
567 * handle its death signal.
568 */
569 mask = TASK_INTERRUPTIBLE;
570 if (resume)
f021a3c2 571 mask |= TASK_WAKEKILL;
1da177e4
LT
572 if (!wake_up_state(t, mask))
573 kick_process(t);
574}
575
71fabd5e
GA
576/*
577 * Remove signals in mask from the pending set and queue.
578 * Returns 1 if any signals were found.
579 *
580 * All callers must be holding the siglock.
581 *
582 * This version takes a sigset mask and looks at all signals,
583 * not just those in the first mask word.
584 */
585static int rm_from_queue_full(sigset_t *mask, struct sigpending *s)
586{
587 struct sigqueue *q, *n;
588 sigset_t m;
589
590 sigandsets(&m, mask, &s->signal);
591 if (sigisemptyset(&m))
592 return 0;
593
594 signandsets(&s->signal, &s->signal, mask);
595 list_for_each_entry_safe(q, n, &s->list, list) {
596 if (sigismember(mask, q->info.si_signo)) {
597 list_del_init(&q->list);
598 __sigqueue_free(q);
599 }
600 }
601 return 1;
602}
1da177e4
LT
603/*
604 * Remove signals in mask from the pending set and queue.
605 * Returns 1 if any signals were found.
606 *
607 * All callers must be holding the siglock.
608 */
609static int rm_from_queue(unsigned long mask, struct sigpending *s)
610{
611 struct sigqueue *q, *n;
612
613 if (!sigtestsetmask(&s->signal, mask))
614 return 0;
615
616 sigdelsetmask(&s->signal, mask);
617 list_for_each_entry_safe(q, n, &s->list, list) {
618 if (q->info.si_signo < SIGRTMIN &&
619 (mask & sigmask(q->info.si_signo))) {
620 list_del_init(&q->list);
621 __sigqueue_free(q);
622 }
623 }
624 return 1;
625}
626
614c517d
ON
627static inline int is_si_special(const struct siginfo *info)
628{
629 return info <= SEND_SIG_FORCED;
630}
631
632static inline bool si_fromuser(const struct siginfo *info)
633{
634 return info == SEND_SIG_NOINFO ||
635 (!is_si_special(info) && SI_FROMUSER(info));
636}
637
1da177e4
LT
638/*
639 * Bad permissions for sending the signal
c69e8d9c 640 * - the caller must hold at least the RCU read lock
1da177e4
LT
641 */
642static int check_kill_permission(int sig, struct siginfo *info,
643 struct task_struct *t)
644{
065add39 645 const struct cred *cred, *tcred;
2e2ba22e 646 struct pid *sid;
3b5e9e53
ON
647 int error;
648
7ed20e1a 649 if (!valid_signal(sig))
3b5e9e53
ON
650 return -EINVAL;
651
614c517d 652 if (!si_fromuser(info))
3b5e9e53 653 return 0;
e54dc243 654
3b5e9e53
ON
655 error = audit_signal_info(sig, t); /* Let audit system see the signal */
656 if (error)
1da177e4 657 return error;
3b5e9e53 658
065add39 659 cred = current_cred();
c69e8d9c 660 tcred = __task_cred(t);
065add39
ON
661 if (!same_thread_group(current, t) &&
662 (cred->euid ^ tcred->suid) &&
c69e8d9c
DH
663 (cred->euid ^ tcred->uid) &&
664 (cred->uid ^ tcred->suid) &&
665 (cred->uid ^ tcred->uid) &&
2e2ba22e
ON
666 !capable(CAP_KILL)) {
667 switch (sig) {
668 case SIGCONT:
2e2ba22e 669 sid = task_session(t);
2e2ba22e
ON
670 /*
671 * We don't return the error if sid == NULL. The
672 * task was unhashed, the caller must notice this.
673 */
674 if (!sid || sid == task_session(current))
675 break;
676 default:
677 return -EPERM;
678 }
679 }
c2f0c7c3 680
e54dc243 681 return security_task_kill(t, info, sig, 0);
1da177e4
LT
682}
683
1da177e4 684/*
7e695a5e
ON
685 * Handle magic process-wide effects of stop/continue signals. Unlike
686 * the signal actions, these happen immediately at signal-generation
1da177e4
LT
687 * time regardless of blocking, ignoring, or handling. This does the
688 * actual continuing for SIGCONT, but not the actual stopping for stop
7e695a5e
ON
689 * signals. The process stop is done as a signal action for SIG_DFL.
690 *
691 * Returns true if the signal should be actually delivered, otherwise
692 * it should be dropped.
1da177e4 693 */
921cf9f6 694static int prepare_signal(int sig, struct task_struct *p, int from_ancestor_ns)
1da177e4 695{
ad16a460 696 struct signal_struct *signal = p->signal;
1da177e4
LT
697 struct task_struct *t;
698
7e695a5e 699 if (unlikely(signal->flags & SIGNAL_GROUP_EXIT)) {
1da177e4 700 /*
7e695a5e 701 * The process is in the middle of dying, nothing to do.
1da177e4 702 */
7e695a5e 703 } else if (sig_kernel_stop(sig)) {
1da177e4
LT
704 /*
705 * This is a stop signal. Remove SIGCONT from all queues.
706 */
ad16a460 707 rm_from_queue(sigmask(SIGCONT), &signal->shared_pending);
1da177e4
LT
708 t = p;
709 do {
710 rm_from_queue(sigmask(SIGCONT), &t->pending);
ad16a460 711 } while_each_thread(p, t);
1da177e4 712 } else if (sig == SIGCONT) {
fc321d2e 713 unsigned int why;
1da177e4
LT
714 /*
715 * Remove all stop signals from all queues,
716 * and wake all threads.
717 */
ad16a460 718 rm_from_queue(SIG_KERNEL_STOP_MASK, &signal->shared_pending);
1da177e4
LT
719 t = p;
720 do {
721 unsigned int state;
722 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1da177e4
LT
723 /*
724 * If there is a handler for SIGCONT, we must make
725 * sure that no thread returns to user mode before
726 * we post the signal, in case it was the only
727 * thread eligible to run the signal handler--then
728 * it must not do anything between resuming and
729 * running the handler. With the TIF_SIGPENDING
730 * flag set, the thread will pause and acquire the
731 * siglock that we hold now and until we've queued
fc321d2e 732 * the pending signal.
1da177e4
LT
733 *
734 * Wake up the stopped thread _after_ setting
735 * TIF_SIGPENDING
736 */
f021a3c2 737 state = __TASK_STOPPED;
1da177e4
LT
738 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
739 set_tsk_thread_flag(t, TIF_SIGPENDING);
740 state |= TASK_INTERRUPTIBLE;
741 }
742 wake_up_state(t, state);
ad16a460 743 } while_each_thread(p, t);
1da177e4 744
fc321d2e
ON
745 /*
746 * Notify the parent with CLD_CONTINUED if we were stopped.
747 *
748 * If we were in the middle of a group stop, we pretend it
749 * was already finished, and then continued. Since SIGCHLD
750 * doesn't queue we report only CLD_STOPPED, as if the next
751 * CLD_CONTINUED was dropped.
752 */
753 why = 0;
ad16a460 754 if (signal->flags & SIGNAL_STOP_STOPPED)
fc321d2e 755 why |= SIGNAL_CLD_CONTINUED;
ad16a460 756 else if (signal->group_stop_count)
fc321d2e
ON
757 why |= SIGNAL_CLD_STOPPED;
758
759 if (why) {
021e1ae3 760 /*
ae6d2ed7 761 * The first thread which returns from do_signal_stop()
021e1ae3
ON
762 * will take ->siglock, notice SIGNAL_CLD_MASK, and
763 * notify its parent. See get_signal_to_deliver().
764 */
ad16a460
ON
765 signal->flags = why | SIGNAL_STOP_CONTINUED;
766 signal->group_stop_count = 0;
767 signal->group_exit_code = 0;
1da177e4
LT
768 } else {
769 /*
770 * We are not stopped, but there could be a stop
771 * signal in the middle of being processed after
772 * being removed from the queue. Clear that too.
773 */
ad16a460 774 signal->flags &= ~SIGNAL_STOP_DEQUEUED;
1da177e4 775 }
1da177e4 776 }
7e695a5e 777
921cf9f6 778 return !sig_ignored(p, sig, from_ancestor_ns);
1da177e4
LT
779}
780
71f11dc0
ON
781/*
782 * Test if P wants to take SIG. After we've checked all threads with this,
783 * it's equivalent to finding no threads not blocking SIG. Any threads not
784 * blocking SIG were ruled out because they are not running and already
785 * have pending signals. Such threads will dequeue from the shared queue
786 * as soon as they're available, so putting the signal on the shared queue
787 * will be equivalent to sending it to one such thread.
788 */
789static inline int wants_signal(int sig, struct task_struct *p)
790{
791 if (sigismember(&p->blocked, sig))
792 return 0;
793 if (p->flags & PF_EXITING)
794 return 0;
795 if (sig == SIGKILL)
796 return 1;
797 if (task_is_stopped_or_traced(p))
798 return 0;
799 return task_curr(p) || !signal_pending(p);
800}
801
5fcd835b 802static void complete_signal(int sig, struct task_struct *p, int group)
71f11dc0
ON
803{
804 struct signal_struct *signal = p->signal;
805 struct task_struct *t;
806
807 /*
808 * Now find a thread we can wake up to take the signal off the queue.
809 *
810 * If the main thread wants the signal, it gets first crack.
811 * Probably the least surprising to the average bear.
812 */
813 if (wants_signal(sig, p))
814 t = p;
5fcd835b 815 else if (!group || thread_group_empty(p))
71f11dc0
ON
816 /*
817 * There is just one thread and it does not need to be woken.
818 * It will dequeue unblocked signals before it runs again.
819 */
820 return;
821 else {
822 /*
823 * Otherwise try to find a suitable thread.
824 */
825 t = signal->curr_target;
826 while (!wants_signal(sig, t)) {
827 t = next_thread(t);
828 if (t == signal->curr_target)
829 /*
830 * No thread needs to be woken.
831 * Any eligible threads will see
832 * the signal in the queue soon.
833 */
834 return;
835 }
836 signal->curr_target = t;
837 }
838
839 /*
840 * Found a killable thread. If the signal will be fatal,
841 * then start taking the whole group down immediately.
842 */
fae5fa44
ON
843 if (sig_fatal(p, sig) &&
844 !(signal->flags & (SIGNAL_UNKILLABLE | SIGNAL_GROUP_EXIT)) &&
71f11dc0 845 !sigismember(&t->real_blocked, sig) &&
445a91d2 846 (sig == SIGKILL ||
43918f2b 847 !tracehook_consider_fatal_signal(t, sig))) {
71f11dc0
ON
848 /*
849 * This signal will be fatal to the whole group.
850 */
851 if (!sig_kernel_coredump(sig)) {
852 /*
853 * Start a group exit and wake everybody up.
854 * This way we don't have other threads
855 * running and doing things after a slower
856 * thread has the fatal signal pending.
857 */
858 signal->flags = SIGNAL_GROUP_EXIT;
859 signal->group_exit_code = sig;
860 signal->group_stop_count = 0;
861 t = p;
862 do {
863 sigaddset(&t->pending.signal, SIGKILL);
864 signal_wake_up(t, 1);
865 } while_each_thread(p, t);
866 return;
867 }
868 }
869
870 /*
871 * The signal is already in the shared-pending queue.
872 * Tell the chosen thread to wake up and dequeue it.
873 */
874 signal_wake_up(t, sig == SIGKILL);
875 return;
876}
877
af7fff9c
PE
878static inline int legacy_queue(struct sigpending *signals, int sig)
879{
880 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
881}
882
7978b567
SB
883static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
884 int group, int from_ancestor_ns)
1da177e4 885{
2ca3515a 886 struct sigpending *pending;
6e65acba 887 struct sigqueue *q;
7a0aeb14 888 int override_rlimit;
1da177e4 889
d1eb650f 890 trace_signal_generate(sig, info, t);
0a16b607 891
6e65acba 892 assert_spin_locked(&t->sighand->siglock);
921cf9f6
SB
893
894 if (!prepare_signal(sig, t, from_ancestor_ns))
7e695a5e 895 return 0;
2ca3515a
ON
896
897 pending = group ? &t->signal->shared_pending : &t->pending;
2acb024d
PE
898 /*
899 * Short-circuit ignored signals and support queuing
900 * exactly one non-rt signal, so that we can get more
901 * detailed information about the cause of the signal.
902 */
7e695a5e 903 if (legacy_queue(pending, sig))
2acb024d 904 return 0;
1da177e4
LT
905 /*
906 * fast-pathed signals for kernel-internal things like SIGSTOP
907 * or SIGKILL.
908 */
b67a1b9e 909 if (info == SEND_SIG_FORCED)
1da177e4
LT
910 goto out_set;
911
912 /* Real-time signals must be queued if sent by sigqueue, or
913 some other real-time mechanism. It is implementation
914 defined whether kill() does so. We attempt to do so, on
915 the principle of least surprise, but since kill is not
916 allowed to fail with EAGAIN when low on memory we just
917 make sure at least one signal gets delivered and don't
918 pass on the info struct. */
919
7a0aeb14
VN
920 if (sig < SIGRTMIN)
921 override_rlimit = (is_si_special(info) || info->si_code >= 0);
922 else
923 override_rlimit = 0;
924
f84d49b2 925 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
7a0aeb14 926 override_rlimit);
1da177e4 927 if (q) {
2ca3515a 928 list_add_tail(&q->list, &pending->list);
1da177e4 929 switch ((unsigned long) info) {
b67a1b9e 930 case (unsigned long) SEND_SIG_NOINFO:
1da177e4
LT
931 q->info.si_signo = sig;
932 q->info.si_errno = 0;
933 q->info.si_code = SI_USER;
9cd4fd10 934 q->info.si_pid = task_tgid_nr_ns(current,
09bca05c 935 task_active_pid_ns(t));
76aac0e9 936 q->info.si_uid = current_uid();
1da177e4 937 break;
b67a1b9e 938 case (unsigned long) SEND_SIG_PRIV:
1da177e4
LT
939 q->info.si_signo = sig;
940 q->info.si_errno = 0;
941 q->info.si_code = SI_KERNEL;
942 q->info.si_pid = 0;
943 q->info.si_uid = 0;
944 break;
945 default:
946 copy_siginfo(&q->info, info);
6588c1e3
SB
947 if (from_ancestor_ns)
948 q->info.si_pid = 0;
1da177e4
LT
949 break;
950 }
621d3121 951 } else if (!is_si_special(info)) {
ba005e1f
MH
952 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
953 /*
954 * Queue overflow, abort. We may abort if the
955 * signal was rt and sent by user using something
956 * other than kill().
957 */
958 trace_signal_overflow_fail(sig, group, info);
1da177e4 959 return -EAGAIN;
ba005e1f
MH
960 } else {
961 /*
962 * This is a silent loss of information. We still
963 * send the signal, but the *info bits are lost.
964 */
965 trace_signal_lose_info(sig, group, info);
966 }
1da177e4
LT
967 }
968
969out_set:
53c30337 970 signalfd_notify(t, sig);
2ca3515a 971 sigaddset(&pending->signal, sig);
4cd4b6d4
PE
972 complete_signal(sig, t, group);
973 return 0;
1da177e4
LT
974}
975
7978b567
SB
976static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
977 int group)
978{
921cf9f6
SB
979 int from_ancestor_ns = 0;
980
981#ifdef CONFIG_PID_NS
dd34200a
ON
982 from_ancestor_ns = si_fromuser(info) &&
983 !task_pid_nr_ns(current, task_active_pid_ns(t));
921cf9f6
SB
984#endif
985
986 return __send_signal(sig, info, t, group, from_ancestor_ns);
7978b567
SB
987}
988
45807a1d
IM
989static void print_fatal_signal(struct pt_regs *regs, int signr)
990{
991 printk("%s/%d: potentially unexpected fatal signal %d.\n",
ba25f9dc 992 current->comm, task_pid_nr(current), signr);
45807a1d 993
ca5cd877 994#if defined(__i386__) && !defined(__arch_um__)
65ea5b03 995 printk("code at %08lx: ", regs->ip);
45807a1d
IM
996 {
997 int i;
998 for (i = 0; i < 16; i++) {
999 unsigned char insn;
1000
b45c6e76
AK
1001 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1002 break;
45807a1d
IM
1003 printk("%02x ", insn);
1004 }
1005 }
1006#endif
1007 printk("\n");
3a9f84d3 1008 preempt_disable();
45807a1d 1009 show_regs(regs);
3a9f84d3 1010 preempt_enable();
45807a1d
IM
1011}
1012
1013static int __init setup_print_fatal_signals(char *str)
1014{
1015 get_option (&str, &print_fatal_signals);
1016
1017 return 1;
1018}
1019
1020__setup("print-fatal-signals=", setup_print_fatal_signals);
1da177e4 1021
4cd4b6d4
PE
1022int
1023__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1024{
1025 return send_signal(sig, info, p, 1);
1026}
1027
1da177e4
LT
1028static int
1029specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1030{
4cd4b6d4 1031 return send_signal(sig, info, t, 0);
1da177e4
LT
1032}
1033
4a30debf
ON
1034int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1035 bool group)
1036{
1037 unsigned long flags;
1038 int ret = -ESRCH;
1039
1040 if (lock_task_sighand(p, &flags)) {
1041 ret = send_signal(sig, info, p, group);
1042 unlock_task_sighand(p, &flags);
1043 }
1044
1045 return ret;
1046}
1047
1da177e4
LT
1048/*
1049 * Force a signal that the process can't ignore: if necessary
1050 * we unblock the signal and change any SIG_IGN to SIG_DFL.
ae74c3b6
LT
1051 *
1052 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1053 * since we do not want to have a signal handler that was blocked
1054 * be invoked when user space had explicitly blocked it.
1055 *
80fe728d
ON
1056 * We don't want to have recursive SIGSEGV's etc, for example,
1057 * that is why we also clear SIGNAL_UNKILLABLE.
1da177e4 1058 */
1da177e4
LT
1059int
1060force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1061{
1062 unsigned long int flags;
ae74c3b6
LT
1063 int ret, blocked, ignored;
1064 struct k_sigaction *action;
1da177e4
LT
1065
1066 spin_lock_irqsave(&t->sighand->siglock, flags);
ae74c3b6
LT
1067 action = &t->sighand->action[sig-1];
1068 ignored = action->sa.sa_handler == SIG_IGN;
1069 blocked = sigismember(&t->blocked, sig);
1070 if (blocked || ignored) {
1071 action->sa.sa_handler = SIG_DFL;
1072 if (blocked) {
1073 sigdelset(&t->blocked, sig);
7bb44ade 1074 recalc_sigpending_and_wake(t);
ae74c3b6 1075 }
1da177e4 1076 }
80fe728d
ON
1077 if (action->sa.sa_handler == SIG_DFL)
1078 t->signal->flags &= ~SIGNAL_UNKILLABLE;
1da177e4
LT
1079 ret = specific_send_sig_info(sig, info, t);
1080 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1081
1082 return ret;
1083}
1084
1da177e4
LT
1085/*
1086 * Nuke all other threads in the group.
1087 */
1088void zap_other_threads(struct task_struct *p)
1089{
1090 struct task_struct *t;
1091
1da177e4
LT
1092 p->signal->group_stop_count = 0;
1093
1da177e4
LT
1094 for (t = next_thread(p); t != p; t = next_thread(t)) {
1095 /*
1096 * Don't bother with already dead threads
1097 */
1098 if (t->exit_state)
1099 continue;
1100
30e0fca6 1101 /* SIGKILL will be handled before any pending SIGSTOP */
1da177e4 1102 sigaddset(&t->pending.signal, SIGKILL);
1da177e4
LT
1103 signal_wake_up(t, 1);
1104 }
1105}
1106
f63ee72e
ON
1107struct sighand_struct *lock_task_sighand(struct task_struct *tsk, unsigned long *flags)
1108{
1109 struct sighand_struct *sighand;
1110
1406f2d3 1111 rcu_read_lock();
f63ee72e
ON
1112 for (;;) {
1113 sighand = rcu_dereference(tsk->sighand);
1114 if (unlikely(sighand == NULL))
1115 break;
1116
1117 spin_lock_irqsave(&sighand->siglock, *flags);
1118 if (likely(sighand == tsk->sighand))
1119 break;
1120 spin_unlock_irqrestore(&sighand->siglock, *flags);
1121 }
1406f2d3 1122 rcu_read_unlock();
f63ee72e
ON
1123
1124 return sighand;
1125}
1126
c69e8d9c
DH
1127/*
1128 * send signal info to all the members of a group
1129 * - the caller must hold the RCU read lock at least
1130 */
1da177e4
LT
1131int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1132{
4a30debf 1133 int ret = check_kill_permission(sig, info, p);
f63ee72e 1134
4a30debf
ON
1135 if (!ret && sig)
1136 ret = do_send_sig_info(sig, info, p, true);
1da177e4
LT
1137
1138 return ret;
1139}
1140
1141/*
146a505d 1142 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
1da177e4 1143 * control characters do (^C, ^Z etc)
c69e8d9c 1144 * - the caller must hold at least a readlock on tasklist_lock
1da177e4 1145 */
c4b92fc1 1146int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
1da177e4
LT
1147{
1148 struct task_struct *p = NULL;
1149 int retval, success;
1150
1da177e4
LT
1151 success = 0;
1152 retval = -ESRCH;
c4b92fc1 1153 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
1154 int err = group_send_sig_info(sig, info, p);
1155 success |= !err;
1156 retval = err;
c4b92fc1 1157 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
1158 return success ? 0 : retval;
1159}
1160
c4b92fc1 1161int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
1da177e4 1162{
d36174bc 1163 int error = -ESRCH;
1da177e4
LT
1164 struct task_struct *p;
1165
e56d0903 1166 rcu_read_lock();
d36174bc 1167retry:
c4b92fc1 1168 p = pid_task(pid, PIDTYPE_PID);
d36174bc 1169 if (p) {
1da177e4 1170 error = group_send_sig_info(sig, info, p);
d36174bc
ON
1171 if (unlikely(error == -ESRCH))
1172 /*
1173 * The task was unhashed in between, try again.
1174 * If it is dead, pid_task() will return NULL,
1175 * if we race with de_thread() it will find the
1176 * new leader.
1177 */
1178 goto retry;
1179 }
e56d0903 1180 rcu_read_unlock();
6ca25b55 1181
1da177e4
LT
1182 return error;
1183}
1184
c3de4b38
MW
1185int
1186kill_proc_info(int sig, struct siginfo *info, pid_t pid)
c4b92fc1
EB
1187{
1188 int error;
1189 rcu_read_lock();
b488893a 1190 error = kill_pid_info(sig, info, find_vpid(pid));
c4b92fc1
EB
1191 rcu_read_unlock();
1192 return error;
1193}
1194
2425c08b
EB
1195/* like kill_pid_info(), but doesn't use uid/euid of "current" */
1196int kill_pid_info_as_uid(int sig, struct siginfo *info, struct pid *pid,
8f95dc58 1197 uid_t uid, uid_t euid, u32 secid)
46113830
HW
1198{
1199 int ret = -EINVAL;
1200 struct task_struct *p;
c69e8d9c 1201 const struct cred *pcred;
14d8c9f3 1202 unsigned long flags;
46113830
HW
1203
1204 if (!valid_signal(sig))
1205 return ret;
1206
14d8c9f3 1207 rcu_read_lock();
2425c08b 1208 p = pid_task(pid, PIDTYPE_PID);
46113830
HW
1209 if (!p) {
1210 ret = -ESRCH;
1211 goto out_unlock;
1212 }
c69e8d9c 1213 pcred = __task_cred(p);
614c517d 1214 if (si_fromuser(info) &&
c69e8d9c
DH
1215 euid != pcred->suid && euid != pcred->uid &&
1216 uid != pcred->suid && uid != pcred->uid) {
46113830
HW
1217 ret = -EPERM;
1218 goto out_unlock;
1219 }
8f95dc58
DQ
1220 ret = security_task_kill(p, info, sig, secid);
1221 if (ret)
1222 goto out_unlock;
14d8c9f3
TG
1223
1224 if (sig) {
1225 if (lock_task_sighand(p, &flags)) {
1226 ret = __send_signal(sig, info, p, 1, 0);
1227 unlock_task_sighand(p, &flags);
1228 } else
1229 ret = -ESRCH;
46113830
HW
1230 }
1231out_unlock:
14d8c9f3 1232 rcu_read_unlock();
46113830
HW
1233 return ret;
1234}
2425c08b 1235EXPORT_SYMBOL_GPL(kill_pid_info_as_uid);
1da177e4
LT
1236
1237/*
1238 * kill_something_info() interprets pid in interesting ways just like kill(2).
1239 *
1240 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1241 * is probably wrong. Should make it like BSD or SYSV.
1242 */
1243
bc64efd2 1244static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
1da177e4 1245{
8d42db18 1246 int ret;
d5df763b
PE
1247
1248 if (pid > 0) {
1249 rcu_read_lock();
1250 ret = kill_pid_info(sig, info, find_vpid(pid));
1251 rcu_read_unlock();
1252 return ret;
1253 }
1254
1255 read_lock(&tasklist_lock);
1256 if (pid != -1) {
1257 ret = __kill_pgrp_info(sig, info,
1258 pid ? find_vpid(-pid) : task_pgrp(current));
1259 } else {
1da177e4
LT
1260 int retval = 0, count = 0;
1261 struct task_struct * p;
1262
1da177e4 1263 for_each_process(p) {
d25141a8
SB
1264 if (task_pid_vnr(p) > 1 &&
1265 !same_thread_group(p, current)) {
1da177e4
LT
1266 int err = group_send_sig_info(sig, info, p);
1267 ++count;
1268 if (err != -EPERM)
1269 retval = err;
1270 }
1271 }
8d42db18 1272 ret = count ? retval : -ESRCH;
1da177e4 1273 }
d5df763b
PE
1274 read_unlock(&tasklist_lock);
1275
8d42db18 1276 return ret;
1da177e4
LT
1277}
1278
1279/*
1280 * These are for backward compatibility with the rest of the kernel source.
1281 */
1282
1da177e4
LT
1283int
1284send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1285{
1da177e4
LT
1286 /*
1287 * Make sure legacy kernel users don't send in bad values
1288 * (normal paths check this in check_kill_permission).
1289 */
7ed20e1a 1290 if (!valid_signal(sig))
1da177e4
LT
1291 return -EINVAL;
1292
4a30debf 1293 return do_send_sig_info(sig, info, p, false);
1da177e4
LT
1294}
1295
b67a1b9e
ON
1296#define __si_special(priv) \
1297 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1298
1da177e4
LT
1299int
1300send_sig(int sig, struct task_struct *p, int priv)
1301{
b67a1b9e 1302 return send_sig_info(sig, __si_special(priv), p);
1da177e4
LT
1303}
1304
1da177e4
LT
1305void
1306force_sig(int sig, struct task_struct *p)
1307{
b67a1b9e 1308 force_sig_info(sig, SEND_SIG_PRIV, p);
1da177e4
LT
1309}
1310
1311/*
1312 * When things go south during signal handling, we
1313 * will force a SIGSEGV. And if the signal that caused
1314 * the problem was already a SIGSEGV, we'll want to
1315 * make sure we don't even try to deliver the signal..
1316 */
1317int
1318force_sigsegv(int sig, struct task_struct *p)
1319{
1320 if (sig == SIGSEGV) {
1321 unsigned long flags;
1322 spin_lock_irqsave(&p->sighand->siglock, flags);
1323 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1324 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1325 }
1326 force_sig(SIGSEGV, p);
1327 return 0;
1328}
1329
c4b92fc1
EB
1330int kill_pgrp(struct pid *pid, int sig, int priv)
1331{
146a505d
PE
1332 int ret;
1333
1334 read_lock(&tasklist_lock);
1335 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1336 read_unlock(&tasklist_lock);
1337
1338 return ret;
c4b92fc1
EB
1339}
1340EXPORT_SYMBOL(kill_pgrp);
1341
1342int kill_pid(struct pid *pid, int sig, int priv)
1343{
1344 return kill_pid_info(sig, __si_special(priv), pid);
1345}
1346EXPORT_SYMBOL(kill_pid);
1347
1da177e4
LT
1348/*
1349 * These functions support sending signals using preallocated sigqueue
1350 * structures. This is needed "because realtime applications cannot
1351 * afford to lose notifications of asynchronous events, like timer
f84d49b2 1352 * expirations or I/O completions". In the case of Posix Timers
1da177e4
LT
1353 * we allocate the sigqueue structure from the timer_create. If this
1354 * allocation fails we are able to report the failure to the application
1355 * with an EAGAIN error.
1356 */
1da177e4
LT
1357struct sigqueue *sigqueue_alloc(void)
1358{
f84d49b2 1359 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
1da177e4 1360
f84d49b2 1361 if (q)
1da177e4 1362 q->flags |= SIGQUEUE_PREALLOC;
f84d49b2
NO
1363
1364 return q;
1da177e4
LT
1365}
1366
1367void sigqueue_free(struct sigqueue *q)
1368{
1369 unsigned long flags;
60187d27
ON
1370 spinlock_t *lock = &current->sighand->siglock;
1371
1da177e4
LT
1372 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1373 /*
c8e85b4f
ON
1374 * We must hold ->siglock while testing q->list
1375 * to serialize with collect_signal() or with
da7978b0 1376 * __exit_signal()->flush_sigqueue().
1da177e4 1377 */
60187d27 1378 spin_lock_irqsave(lock, flags);
c8e85b4f
ON
1379 q->flags &= ~SIGQUEUE_PREALLOC;
1380 /*
1381 * If it is queued it will be freed when dequeued,
1382 * like the "regular" sigqueue.
1383 */
60187d27 1384 if (!list_empty(&q->list))
c8e85b4f 1385 q = NULL;
60187d27
ON
1386 spin_unlock_irqrestore(lock, flags);
1387
c8e85b4f
ON
1388 if (q)
1389 __sigqueue_free(q);
1da177e4
LT
1390}
1391
ac5c2153 1392int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
9e3bd6c3 1393{
e62e6650 1394 int sig = q->info.si_signo;
2ca3515a 1395 struct sigpending *pending;
e62e6650
ON
1396 unsigned long flags;
1397 int ret;
2ca3515a 1398
4cd4b6d4 1399 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
e62e6650
ON
1400
1401 ret = -1;
1402 if (!likely(lock_task_sighand(t, &flags)))
1403 goto ret;
1404
7e695a5e 1405 ret = 1; /* the signal is ignored */
921cf9f6 1406 if (!prepare_signal(sig, t, 0))
e62e6650
ON
1407 goto out;
1408
1409 ret = 0;
9e3bd6c3
PE
1410 if (unlikely(!list_empty(&q->list))) {
1411 /*
1412 * If an SI_TIMER entry is already queue just increment
1413 * the overrun count.
1414 */
9e3bd6c3
PE
1415 BUG_ON(q->info.si_code != SI_TIMER);
1416 q->info.si_overrun++;
e62e6650 1417 goto out;
9e3bd6c3 1418 }
ba661292 1419 q->info.si_overrun = 0;
9e3bd6c3 1420
9e3bd6c3 1421 signalfd_notify(t, sig);
2ca3515a 1422 pending = group ? &t->signal->shared_pending : &t->pending;
9e3bd6c3
PE
1423 list_add_tail(&q->list, &pending->list);
1424 sigaddset(&pending->signal, sig);
4cd4b6d4 1425 complete_signal(sig, t, group);
e62e6650
ON
1426out:
1427 unlock_task_sighand(t, &flags);
1428ret:
1429 return ret;
9e3bd6c3
PE
1430}
1431
1da177e4
LT
1432/*
1433 * Let a parent know about the death of a child.
1434 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
2b2a1ff6
RM
1435 *
1436 * Returns -1 if our parent ignored us and so we've switched to
1437 * self-reaping, or else @sig.
1da177e4 1438 */
2b2a1ff6 1439int do_notify_parent(struct task_struct *tsk, int sig)
1da177e4
LT
1440{
1441 struct siginfo info;
1442 unsigned long flags;
1443 struct sighand_struct *psig;
1b04624f 1444 int ret = sig;
1da177e4
LT
1445
1446 BUG_ON(sig == -1);
1447
1448 /* do_notify_parent_cldstop should have been called instead. */
e1abb39c 1449 BUG_ON(task_is_stopped_or_traced(tsk));
1da177e4 1450
5cb11446 1451 BUG_ON(!task_ptrace(tsk) &&
1da177e4
LT
1452 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1453
1454 info.si_signo = sig;
1455 info.si_errno = 0;
b488893a
PE
1456 /*
1457 * we are under tasklist_lock here so our parent is tied to
1458 * us and cannot exit and release its namespace.
1459 *
1460 * the only it can is to switch its nsproxy with sys_unshare,
1461 * bu uncharing pid namespaces is not allowed, so we'll always
1462 * see relevant namespace
1463 *
1464 * write_lock() currently calls preempt_disable() which is the
1465 * same as rcu_read_lock(), but according to Oleg, this is not
1466 * correct to rely on this
1467 */
1468 rcu_read_lock();
1469 info.si_pid = task_pid_nr_ns(tsk, tsk->parent->nsproxy->pid_ns);
c69e8d9c 1470 info.si_uid = __task_cred(tsk)->uid;
b488893a
PE
1471 rcu_read_unlock();
1472
32bd671d
PZ
1473 info.si_utime = cputime_to_clock_t(cputime_add(tsk->utime,
1474 tsk->signal->utime));
1475 info.si_stime = cputime_to_clock_t(cputime_add(tsk->stime,
1476 tsk->signal->stime));
1da177e4
LT
1477
1478 info.si_status = tsk->exit_code & 0x7f;
1479 if (tsk->exit_code & 0x80)
1480 info.si_code = CLD_DUMPED;
1481 else if (tsk->exit_code & 0x7f)
1482 info.si_code = CLD_KILLED;
1483 else {
1484 info.si_code = CLD_EXITED;
1485 info.si_status = tsk->exit_code >> 8;
1486 }
1487
1488 psig = tsk->parent->sighand;
1489 spin_lock_irqsave(&psig->siglock, flags);
5cb11446 1490 if (!task_ptrace(tsk) && sig == SIGCHLD &&
1da177e4
LT
1491 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1492 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1493 /*
1494 * We are exiting and our parent doesn't care. POSIX.1
1495 * defines special semantics for setting SIGCHLD to SIG_IGN
1496 * or setting the SA_NOCLDWAIT flag: we should be reaped
1497 * automatically and not left for our parent's wait4 call.
1498 * Rather than having the parent do it as a magic kind of
1499 * signal handler, we just set this to tell do_exit that we
1500 * can be cleaned up without becoming a zombie. Note that
1501 * we still call __wake_up_parent in this case, because a
1502 * blocked sys_wait4 might now return -ECHILD.
1503 *
1504 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1505 * is implementation-defined: we do (if you don't want
1506 * it, just use SIG_IGN instead).
1507 */
1b04624f 1508 ret = tsk->exit_signal = -1;
1da177e4 1509 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
2b2a1ff6 1510 sig = -1;
1da177e4 1511 }
7ed20e1a 1512 if (valid_signal(sig) && sig > 0)
1da177e4
LT
1513 __group_send_sig_info(sig, &info, tsk->parent);
1514 __wake_up_parent(tsk, tsk->parent);
1515 spin_unlock_irqrestore(&psig->siglock, flags);
2b2a1ff6 1516
1b04624f 1517 return ret;
1da177e4
LT
1518}
1519
a1d5e21e 1520static void do_notify_parent_cldstop(struct task_struct *tsk, int why)
1da177e4
LT
1521{
1522 struct siginfo info;
1523 unsigned long flags;
bc505a47 1524 struct task_struct *parent;
1da177e4
LT
1525 struct sighand_struct *sighand;
1526
5cb11446 1527 if (task_ptrace(tsk))
bc505a47
ON
1528 parent = tsk->parent;
1529 else {
1530 tsk = tsk->group_leader;
1531 parent = tsk->real_parent;
1532 }
1533
1da177e4
LT
1534 info.si_signo = SIGCHLD;
1535 info.si_errno = 0;
b488893a
PE
1536 /*
1537 * see comment in do_notify_parent() abot the following 3 lines
1538 */
1539 rcu_read_lock();
d9265663 1540 info.si_pid = task_pid_nr_ns(tsk, parent->nsproxy->pid_ns);
c69e8d9c 1541 info.si_uid = __task_cred(tsk)->uid;
b488893a
PE
1542 rcu_read_unlock();
1543
d8878ba3
MK
1544 info.si_utime = cputime_to_clock_t(tsk->utime);
1545 info.si_stime = cputime_to_clock_t(tsk->stime);
1da177e4
LT
1546
1547 info.si_code = why;
1548 switch (why) {
1549 case CLD_CONTINUED:
1550 info.si_status = SIGCONT;
1551 break;
1552 case CLD_STOPPED:
1553 info.si_status = tsk->signal->group_exit_code & 0x7f;
1554 break;
1555 case CLD_TRAPPED:
1556 info.si_status = tsk->exit_code & 0x7f;
1557 break;
1558 default:
1559 BUG();
1560 }
1561
1562 sighand = parent->sighand;
1563 spin_lock_irqsave(&sighand->siglock, flags);
1564 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1565 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1566 __group_send_sig_info(SIGCHLD, &info, parent);
1567 /*
1568 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1569 */
1570 __wake_up_parent(tsk, parent);
1571 spin_unlock_irqrestore(&sighand->siglock, flags);
1572}
1573
d5f70c00
ON
1574static inline int may_ptrace_stop(void)
1575{
5cb11446 1576 if (!likely(task_ptrace(current)))
d5f70c00 1577 return 0;
d5f70c00
ON
1578 /*
1579 * Are we in the middle of do_coredump?
1580 * If so and our tracer is also part of the coredump stopping
1581 * is a deadlock situation, and pointless because our tracer
1582 * is dead so don't allow us to stop.
1583 * If SIGKILL was already sent before the caller unlocked
999d9fc1 1584 * ->siglock we must see ->core_state != NULL. Otherwise it
d5f70c00
ON
1585 * is safe to enter schedule().
1586 */
999d9fc1 1587 if (unlikely(current->mm->core_state) &&
d5f70c00
ON
1588 unlikely(current->mm == current->parent->mm))
1589 return 0;
1590
1591 return 1;
1592}
1593
1a669c2f
RM
1594/*
1595 * Return nonzero if there is a SIGKILL that should be waking us up.
1596 * Called with the siglock held.
1597 */
1598static int sigkill_pending(struct task_struct *tsk)
1599{
3d749b9e
ON
1600 return sigismember(&tsk->pending.signal, SIGKILL) ||
1601 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
1a669c2f
RM
1602}
1603
1da177e4
LT
1604/*
1605 * This must be called with current->sighand->siglock held.
1606 *
1607 * This should be the path for all ptrace stops.
1608 * We always set current->last_siginfo while stopped here.
1609 * That makes it a way to test a stopped process for
1610 * being ptrace-stopped vs being job-control-stopped.
1611 *
20686a30
ON
1612 * If we actually decide not to stop at all because the tracer
1613 * is gone, we keep current->exit_code unless clear_code.
1da177e4 1614 */
20686a30 1615static void ptrace_stop(int exit_code, int clear_code, siginfo_t *info)
1da177e4 1616{
1a669c2f
RM
1617 if (arch_ptrace_stop_needed(exit_code, info)) {
1618 /*
1619 * The arch code has something special to do before a
1620 * ptrace stop. This is allowed to block, e.g. for faults
1621 * on user stack pages. We can't keep the siglock while
1622 * calling arch_ptrace_stop, so we must release it now.
1623 * To preserve proper semantics, we must do this before
1624 * any signal bookkeeping like checking group_stop_count.
1625 * Meanwhile, a SIGKILL could come in before we retake the
1626 * siglock. That must prevent us from sleeping in TASK_TRACED.
1627 * So after regaining the lock, we must check for SIGKILL.
1628 */
1629 spin_unlock_irq(&current->sighand->siglock);
1630 arch_ptrace_stop(exit_code, info);
1631 spin_lock_irq(&current->sighand->siglock);
3d749b9e
ON
1632 if (sigkill_pending(current))
1633 return;
1a669c2f
RM
1634 }
1635
1da177e4
LT
1636 /*
1637 * If there is a group stop in progress,
1638 * we must participate in the bookkeeping.
1639 */
1640 if (current->signal->group_stop_count > 0)
1641 --current->signal->group_stop_count;
1642
1643 current->last_siginfo = info;
1644 current->exit_code = exit_code;
1645
1646 /* Let the debugger run. */
d9ae90ac 1647 __set_current_state(TASK_TRACED);
1da177e4
LT
1648 spin_unlock_irq(&current->sighand->siglock);
1649 read_lock(&tasklist_lock);
3d749b9e 1650 if (may_ptrace_stop()) {
a1d5e21e 1651 do_notify_parent_cldstop(current, CLD_TRAPPED);
53da1d94
MS
1652 /*
1653 * Don't want to allow preemption here, because
1654 * sys_ptrace() needs this task to be inactive.
1655 *
1656 * XXX: implement read_unlock_no_resched().
1657 */
1658 preempt_disable();
1da177e4 1659 read_unlock(&tasklist_lock);
53da1d94 1660 preempt_enable_no_resched();
1da177e4
LT
1661 schedule();
1662 } else {
1663 /*
1664 * By the time we got the lock, our tracer went away.
6405f7f4 1665 * Don't drop the lock yet, another tracer may come.
1da177e4 1666 */
6405f7f4 1667 __set_current_state(TASK_RUNNING);
20686a30
ON
1668 if (clear_code)
1669 current->exit_code = 0;
6405f7f4 1670 read_unlock(&tasklist_lock);
1da177e4
LT
1671 }
1672
13b1c3d4
RM
1673 /*
1674 * While in TASK_TRACED, we were considered "frozen enough".
1675 * Now that we woke up, it's crucial if we're supposed to be
1676 * frozen that we freeze now before running anything substantial.
1677 */
1678 try_to_freeze();
1679
1da177e4
LT
1680 /*
1681 * We are back. Now reacquire the siglock before touching
1682 * last_siginfo, so that we are sure to have synchronized with
1683 * any signal-sending on another CPU that wants to examine it.
1684 */
1685 spin_lock_irq(&current->sighand->siglock);
1686 current->last_siginfo = NULL;
1687
1688 /*
1689 * Queued signals ignored us while we were stopped for tracing.
1690 * So check for any that we should take before resuming user mode.
b74d0deb 1691 * This sets TIF_SIGPENDING, but never clears it.
1da177e4 1692 */
b74d0deb 1693 recalc_sigpending_tsk(current);
1da177e4
LT
1694}
1695
1696void ptrace_notify(int exit_code)
1697{
1698 siginfo_t info;
1699
1700 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1701
1702 memset(&info, 0, sizeof info);
1703 info.si_signo = SIGTRAP;
1704 info.si_code = exit_code;
b488893a 1705 info.si_pid = task_pid_vnr(current);
76aac0e9 1706 info.si_uid = current_uid();
1da177e4
LT
1707
1708 /* Let the debugger run. */
1709 spin_lock_irq(&current->sighand->siglock);
20686a30 1710 ptrace_stop(exit_code, 1, &info);
1da177e4
LT
1711 spin_unlock_irq(&current->sighand->siglock);
1712}
1713
1da177e4
LT
1714/*
1715 * This performs the stopping for SIGSTOP and other stop signals.
1716 * We have to stop all threads in the thread group.
1717 * Returns nonzero if we've actually stopped and released the siglock.
1718 * Returns zero if we didn't stop and still hold the siglock.
1719 */
a122b341 1720static int do_signal_stop(int signr)
1da177e4
LT
1721{
1722 struct signal_struct *sig = current->signal;
ae6d2ed7 1723 int notify;
1da177e4 1724
ae6d2ed7 1725 if (!sig->group_stop_count) {
f558b7e4
ON
1726 struct task_struct *t;
1727
2b201a9e 1728 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED) ||
573cf9ad 1729 unlikely(signal_group_exit(sig)))
f558b7e4 1730 return 0;
1da177e4
LT
1731 /*
1732 * There is no group stop already in progress.
a122b341 1733 * We must initiate one now.
1da177e4 1734 */
a122b341 1735 sig->group_exit_code = signr;
1da177e4 1736
ae6d2ed7 1737 sig->group_stop_count = 1;
a122b341 1738 for (t = next_thread(current); t != current; t = next_thread(t))
1da177e4 1739 /*
a122b341
ON
1740 * Setting state to TASK_STOPPED for a group
1741 * stop is always done with the siglock held,
1742 * so this check has no races.
1da177e4 1743 */
d12619b5 1744 if (!(t->flags & PF_EXITING) &&
e1abb39c 1745 !task_is_stopped_or_traced(t)) {
ae6d2ed7 1746 sig->group_stop_count++;
a122b341
ON
1747 signal_wake_up(t, 0);
1748 }
1da177e4 1749 }
ae6d2ed7
RM
1750 /*
1751 * If there are no other threads in the group, or if there is
1752 * a group stop in progress and we are the last to stop, report
1753 * to the parent. When ptraced, every thread reports itself.
1754 */
1755 notify = sig->group_stop_count == 1 ? CLD_STOPPED : 0;
1756 notify = tracehook_notify_jctl(notify, CLD_STOPPED);
1757 /*
1758 * tracehook_notify_jctl() can drop and reacquire siglock, so
1759 * we keep ->group_stop_count != 0 before the call. If SIGCONT
1760 * or SIGKILL comes in between ->group_stop_count == 0.
1761 */
1762 if (sig->group_stop_count) {
1763 if (!--sig->group_stop_count)
1764 sig->flags = SIGNAL_STOP_STOPPED;
1765 current->exit_code = sig->group_exit_code;
1766 __set_current_state(TASK_STOPPED);
1767 }
1768 spin_unlock_irq(&current->sighand->siglock);
1da177e4 1769
ae6d2ed7
RM
1770 if (notify) {
1771 read_lock(&tasklist_lock);
1772 do_notify_parent_cldstop(current, notify);
1773 read_unlock(&tasklist_lock);
1774 }
1775
1776 /* Now we don't run again until woken by SIGCONT or SIGKILL */
1777 do {
1778 schedule();
1779 } while (try_to_freeze());
1780
1781 tracehook_finish_jctl();
1782 current->exit_code = 0;
dac27f4a 1783
1da177e4
LT
1784 return 1;
1785}
1786
18c98b65
RM
1787static int ptrace_signal(int signr, siginfo_t *info,
1788 struct pt_regs *regs, void *cookie)
1789{
5cb11446 1790 if (!task_ptrace(current))
18c98b65
RM
1791 return signr;
1792
1793 ptrace_signal_deliver(regs, cookie);
1794
1795 /* Let the debugger run. */
1796 ptrace_stop(signr, 0, info);
1797
1798 /* We're back. Did the debugger cancel the sig? */
1799 signr = current->exit_code;
1800 if (signr == 0)
1801 return signr;
1802
1803 current->exit_code = 0;
1804
1805 /* Update the siginfo structure if the signal has
1806 changed. If the debugger wanted something
1807 specific in the siginfo structure then it should
1808 have updated *info via PTRACE_SETSIGINFO. */
1809 if (signr != info->si_signo) {
1810 info->si_signo = signr;
1811 info->si_errno = 0;
1812 info->si_code = SI_USER;
1813 info->si_pid = task_pid_vnr(current->parent);
c69e8d9c 1814 info->si_uid = task_uid(current->parent);
18c98b65
RM
1815 }
1816
1817 /* If the (new) signal is now blocked, requeue it. */
1818 if (sigismember(&current->blocked, signr)) {
1819 specific_send_sig_info(signr, info, current);
1820 signr = 0;
1821 }
1822
1823 return signr;
1824}
1825
1da177e4
LT
1826int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1827 struct pt_regs *regs, void *cookie)
1828{
f6b76d4f
ON
1829 struct sighand_struct *sighand = current->sighand;
1830 struct signal_struct *signal = current->signal;
1831 int signr;
1da177e4 1832
13b1c3d4
RM
1833relock:
1834 /*
1835 * We'll jump back here after any time we were stopped in TASK_STOPPED.
1836 * While in TASK_STOPPED, we were considered "frozen enough".
1837 * Now that we woke up, it's crucial if we're supposed to be
1838 * frozen that we freeze now before running anything substantial.
1839 */
fc558a74
RW
1840 try_to_freeze();
1841
f6b76d4f 1842 spin_lock_irq(&sighand->siglock);
021e1ae3
ON
1843 /*
1844 * Every stopped thread goes here after wakeup. Check to see if
1845 * we should notify the parent, prepare_signal(SIGCONT) encodes
1846 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
1847 */
f6b76d4f
ON
1848 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
1849 int why = (signal->flags & SIGNAL_STOP_CONTINUED)
e4420551 1850 ? CLD_CONTINUED : CLD_STOPPED;
f6b76d4f 1851 signal->flags &= ~SIGNAL_CLD_MASK;
e4420551 1852
ae6d2ed7
RM
1853 why = tracehook_notify_jctl(why, CLD_CONTINUED);
1854 spin_unlock_irq(&sighand->siglock);
fa00b80b 1855
ae6d2ed7
RM
1856 if (why) {
1857 read_lock(&tasklist_lock);
1858 do_notify_parent_cldstop(current->group_leader, why);
1859 read_unlock(&tasklist_lock);
1860 }
e4420551
ON
1861 goto relock;
1862 }
1863
1da177e4
LT
1864 for (;;) {
1865 struct k_sigaction *ka;
7bcf6a2c
RM
1866 /*
1867 * Tracing can induce an artifical signal and choose sigaction.
1868 * The return value in @signr determines the default action,
1869 * but @info->si_signo is the signal number we will report.
1870 */
1871 signr = tracehook_get_signal(current, regs, info, return_ka);
1872 if (unlikely(signr < 0))
1873 goto relock;
1874 if (unlikely(signr != 0))
1875 ka = return_ka;
1876 else {
1be53963
ON
1877 if (unlikely(signal->group_stop_count > 0) &&
1878 do_signal_stop(0))
1879 goto relock;
1880
7bcf6a2c
RM
1881 signr = dequeue_signal(current, &current->blocked,
1882 info);
1da177e4 1883
18c98b65 1884 if (!signr)
7bcf6a2c
RM
1885 break; /* will return 0 */
1886
1887 if (signr != SIGKILL) {
1888 signr = ptrace_signal(signr, info,
1889 regs, cookie);
1890 if (!signr)
1891 continue;
1892 }
1893
1894 ka = &sighand->action[signr-1];
1da177e4
LT
1895 }
1896
f9d4257e
MH
1897 /* Trace actually delivered signals. */
1898 trace_signal_deliver(signr, info, ka);
1899
1da177e4
LT
1900 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1901 continue;
1902 if (ka->sa.sa_handler != SIG_DFL) {
1903 /* Run the handler. */
1904 *return_ka = *ka;
1905
1906 if (ka->sa.sa_flags & SA_ONESHOT)
1907 ka->sa.sa_handler = SIG_DFL;
1908
1909 break; /* will return non-zero "signr" value */
1910 }
1911
1912 /*
1913 * Now we are doing the default action for this signal.
1914 */
1915 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1916 continue;
1917
84d73786 1918 /*
0fbc26a6 1919 * Global init gets no signals it doesn't want.
b3bfa0cb
SB
1920 * Container-init gets no signals it doesn't want from same
1921 * container.
1922 *
1923 * Note that if global/container-init sees a sig_kernel_only()
1924 * signal here, the signal must have been generated internally
1925 * or must have come from an ancestor namespace. In either
1926 * case, the signal cannot be dropped.
84d73786 1927 */
fae5fa44 1928 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
b3bfa0cb 1929 !sig_kernel_only(signr))
1da177e4
LT
1930 continue;
1931
1932 if (sig_kernel_stop(signr)) {
1933 /*
1934 * The default action is to stop all threads in
1935 * the thread group. The job control signals
1936 * do nothing in an orphaned pgrp, but SIGSTOP
1937 * always works. Note that siglock needs to be
1938 * dropped during the call to is_orphaned_pgrp()
1939 * because of lock ordering with tasklist_lock.
1940 * This allows an intervening SIGCONT to be posted.
1941 * We need to check for that and bail out if necessary.
1942 */
1943 if (signr != SIGSTOP) {
f6b76d4f 1944 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
1945
1946 /* signals can be posted during this window */
1947
3e7cd6c4 1948 if (is_current_pgrp_orphaned())
1da177e4
LT
1949 goto relock;
1950
f6b76d4f 1951 spin_lock_irq(&sighand->siglock);
1da177e4
LT
1952 }
1953
7bcf6a2c 1954 if (likely(do_signal_stop(info->si_signo))) {
1da177e4
LT
1955 /* It released the siglock. */
1956 goto relock;
1957 }
1958
1959 /*
1960 * We didn't actually stop, due to a race
1961 * with SIGCONT or something like that.
1962 */
1963 continue;
1964 }
1965
f6b76d4f 1966 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
1967
1968 /*
1969 * Anything else is fatal, maybe with a core dump.
1970 */
1971 current->flags |= PF_SIGNALED;
2dce81bf 1972
1da177e4 1973 if (sig_kernel_coredump(signr)) {
2dce81bf 1974 if (print_fatal_signals)
7bcf6a2c 1975 print_fatal_signal(regs, info->si_signo);
1da177e4
LT
1976 /*
1977 * If it was able to dump core, this kills all
1978 * other threads in the group and synchronizes with
1979 * their demise. If we lost the race with another
1980 * thread getting here, it set group_exit_code
1981 * first and our do_group_exit call below will use
1982 * that value and ignore the one we pass it.
1983 */
7bcf6a2c 1984 do_coredump(info->si_signo, info->si_signo, regs);
1da177e4
LT
1985 }
1986
1987 /*
1988 * Death signals, no core dump.
1989 */
7bcf6a2c 1990 do_group_exit(info->si_signo);
1da177e4
LT
1991 /* NOTREACHED */
1992 }
f6b76d4f 1993 spin_unlock_irq(&sighand->siglock);
1da177e4
LT
1994 return signr;
1995}
1996
d12619b5
ON
1997void exit_signals(struct task_struct *tsk)
1998{
1999 int group_stop = 0;
5dee1707 2000 struct task_struct *t;
d12619b5 2001
5dee1707
ON
2002 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2003 tsk->flags |= PF_EXITING;
2004 return;
d12619b5
ON
2005 }
2006
5dee1707 2007 spin_lock_irq(&tsk->sighand->siglock);
d12619b5
ON
2008 /*
2009 * From now this task is not visible for group-wide signals,
2010 * see wants_signal(), do_signal_stop().
2011 */
2012 tsk->flags |= PF_EXITING;
5dee1707
ON
2013 if (!signal_pending(tsk))
2014 goto out;
2015
2016 /* It could be that __group_complete_signal() choose us to
2017 * notify about group-wide signal. Another thread should be
2018 * woken now to take the signal since we will not.
2019 */
2020 for (t = tsk; (t = next_thread(t)) != tsk; )
2021 if (!signal_pending(t) && !(t->flags & PF_EXITING))
2022 recalc_sigpending_and_wake(t);
2023
2024 if (unlikely(tsk->signal->group_stop_count) &&
2025 !--tsk->signal->group_stop_count) {
2026 tsk->signal->flags = SIGNAL_STOP_STOPPED;
ae6d2ed7 2027 group_stop = tracehook_notify_jctl(CLD_STOPPED, CLD_STOPPED);
5dee1707
ON
2028 }
2029out:
d12619b5
ON
2030 spin_unlock_irq(&tsk->sighand->siglock);
2031
ae6d2ed7 2032 if (unlikely(group_stop)) {
d12619b5 2033 read_lock(&tasklist_lock);
ae6d2ed7 2034 do_notify_parent_cldstop(tsk, group_stop);
d12619b5
ON
2035 read_unlock(&tasklist_lock);
2036 }
2037}
2038
1da177e4
LT
2039EXPORT_SYMBOL(recalc_sigpending);
2040EXPORT_SYMBOL_GPL(dequeue_signal);
2041EXPORT_SYMBOL(flush_signals);
2042EXPORT_SYMBOL(force_sig);
1da177e4
LT
2043EXPORT_SYMBOL(send_sig);
2044EXPORT_SYMBOL(send_sig_info);
2045EXPORT_SYMBOL(sigprocmask);
2046EXPORT_SYMBOL(block_all_signals);
2047EXPORT_SYMBOL(unblock_all_signals);
2048
2049
2050/*
2051 * System call entry points.
2052 */
2053
754fe8d2 2054SYSCALL_DEFINE0(restart_syscall)
1da177e4
LT
2055{
2056 struct restart_block *restart = &current_thread_info()->restart_block;
2057 return restart->fn(restart);
2058}
2059
2060long do_no_restart_syscall(struct restart_block *param)
2061{
2062 return -EINTR;
2063}
2064
2065/*
2066 * We don't need to get the kernel lock - this is all local to this
2067 * particular thread.. (and that's good, because this is _heavily_
2068 * used by various programs)
2069 */
2070
2071/*
2072 * This is also useful for kernel threads that want to temporarily
2073 * (or permanently) block certain signals.
2074 *
2075 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2076 * interface happily blocks "unblockable" signals like SIGKILL
2077 * and friends.
2078 */
2079int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2080{
2081 int error;
1da177e4
LT
2082
2083 spin_lock_irq(&current->sighand->siglock);
a26fd335
ON
2084 if (oldset)
2085 *oldset = current->blocked;
2086
1da177e4
LT
2087 error = 0;
2088 switch (how) {
2089 case SIG_BLOCK:
2090 sigorsets(&current->blocked, &current->blocked, set);
2091 break;
2092 case SIG_UNBLOCK:
2093 signandsets(&current->blocked, &current->blocked, set);
2094 break;
2095 case SIG_SETMASK:
2096 current->blocked = *set;
2097 break;
2098 default:
2099 error = -EINVAL;
2100 }
2101 recalc_sigpending();
2102 spin_unlock_irq(&current->sighand->siglock);
a26fd335 2103
1da177e4
LT
2104 return error;
2105}
2106
17da2bd9
HC
2107SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, set,
2108 sigset_t __user *, oset, size_t, sigsetsize)
1da177e4
LT
2109{
2110 int error = -EINVAL;
2111 sigset_t old_set, new_set;
2112
2113 /* XXX: Don't preclude handling different sized sigset_t's. */
2114 if (sigsetsize != sizeof(sigset_t))
2115 goto out;
2116
2117 if (set) {
2118 error = -EFAULT;
2119 if (copy_from_user(&new_set, set, sizeof(*set)))
2120 goto out;
2121 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2122
2123 error = sigprocmask(how, &new_set, &old_set);
2124 if (error)
2125 goto out;
2126 if (oset)
2127 goto set_old;
2128 } else if (oset) {
2129 spin_lock_irq(&current->sighand->siglock);
2130 old_set = current->blocked;
2131 spin_unlock_irq(&current->sighand->siglock);
2132
2133 set_old:
2134 error = -EFAULT;
2135 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2136 goto out;
2137 }
2138 error = 0;
2139out:
2140 return error;
2141}
2142
2143long do_sigpending(void __user *set, unsigned long sigsetsize)
2144{
2145 long error = -EINVAL;
2146 sigset_t pending;
2147
2148 if (sigsetsize > sizeof(sigset_t))
2149 goto out;
2150
2151 spin_lock_irq(&current->sighand->siglock);
2152 sigorsets(&pending, &current->pending.signal,
2153 &current->signal->shared_pending.signal);
2154 spin_unlock_irq(&current->sighand->siglock);
2155
2156 /* Outside the lock because only this thread touches it. */
2157 sigandsets(&pending, &current->blocked, &pending);
2158
2159 error = -EFAULT;
2160 if (!copy_to_user(set, &pending, sigsetsize))
2161 error = 0;
2162
2163out:
2164 return error;
2165}
2166
17da2bd9 2167SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, set, size_t, sigsetsize)
1da177e4
LT
2168{
2169 return do_sigpending(set, sigsetsize);
2170}
2171
2172#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2173
2174int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2175{
2176 int err;
2177
2178 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2179 return -EFAULT;
2180 if (from->si_code < 0)
2181 return __copy_to_user(to, from, sizeof(siginfo_t))
2182 ? -EFAULT : 0;
2183 /*
2184 * If you change siginfo_t structure, please be sure
2185 * this code is fixed accordingly.
fba2afaa
DL
2186 * Please remember to update the signalfd_copyinfo() function
2187 * inside fs/signalfd.c too, in case siginfo_t changes.
1da177e4
LT
2188 * It should never copy any pad contained in the structure
2189 * to avoid security leaks, but must copy the generic
2190 * 3 ints plus the relevant union member.
2191 */
2192 err = __put_user(from->si_signo, &to->si_signo);
2193 err |= __put_user(from->si_errno, &to->si_errno);
2194 err |= __put_user((short)from->si_code, &to->si_code);
2195 switch (from->si_code & __SI_MASK) {
2196 case __SI_KILL:
2197 err |= __put_user(from->si_pid, &to->si_pid);
2198 err |= __put_user(from->si_uid, &to->si_uid);
2199 break;
2200 case __SI_TIMER:
2201 err |= __put_user(from->si_tid, &to->si_tid);
2202 err |= __put_user(from->si_overrun, &to->si_overrun);
2203 err |= __put_user(from->si_ptr, &to->si_ptr);
2204 break;
2205 case __SI_POLL:
2206 err |= __put_user(from->si_band, &to->si_band);
2207 err |= __put_user(from->si_fd, &to->si_fd);
2208 break;
2209 case __SI_FAULT:
2210 err |= __put_user(from->si_addr, &to->si_addr);
2211#ifdef __ARCH_SI_TRAPNO
2212 err |= __put_user(from->si_trapno, &to->si_trapno);
2213#endif
2214 break;
2215 case __SI_CHLD:
2216 err |= __put_user(from->si_pid, &to->si_pid);
2217 err |= __put_user(from->si_uid, &to->si_uid);
2218 err |= __put_user(from->si_status, &to->si_status);
2219 err |= __put_user(from->si_utime, &to->si_utime);
2220 err |= __put_user(from->si_stime, &to->si_stime);
2221 break;
2222 case __SI_RT: /* This is not generated by the kernel as of now. */
2223 case __SI_MESGQ: /* But this is */
2224 err |= __put_user(from->si_pid, &to->si_pid);
2225 err |= __put_user(from->si_uid, &to->si_uid);
2226 err |= __put_user(from->si_ptr, &to->si_ptr);
2227 break;
2228 default: /* this is just in case for now ... */
2229 err |= __put_user(from->si_pid, &to->si_pid);
2230 err |= __put_user(from->si_uid, &to->si_uid);
2231 break;
2232 }
2233 return err;
2234}
2235
2236#endif
2237
17da2bd9
HC
2238SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2239 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2240 size_t, sigsetsize)
1da177e4
LT
2241{
2242 int ret, sig;
2243 sigset_t these;
2244 struct timespec ts;
2245 siginfo_t info;
2246 long timeout = 0;
2247
2248 /* XXX: Don't preclude handling different sized sigset_t's. */
2249 if (sigsetsize != sizeof(sigset_t))
2250 return -EINVAL;
2251
2252 if (copy_from_user(&these, uthese, sizeof(these)))
2253 return -EFAULT;
2254
2255 /*
2256 * Invert the set of allowed signals to get those we
2257 * want to block.
2258 */
2259 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2260 signotset(&these);
2261
2262 if (uts) {
2263 if (copy_from_user(&ts, uts, sizeof(ts)))
2264 return -EFAULT;
2265 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2266 || ts.tv_sec < 0)
2267 return -EINVAL;
2268 }
2269
2270 spin_lock_irq(&current->sighand->siglock);
2271 sig = dequeue_signal(current, &these, &info);
2272 if (!sig) {
2273 timeout = MAX_SCHEDULE_TIMEOUT;
2274 if (uts)
2275 timeout = (timespec_to_jiffies(&ts)
2276 + (ts.tv_sec || ts.tv_nsec));
2277
2278 if (timeout) {
2279 /* None ready -- temporarily unblock those we're
2280 * interested while we are sleeping in so that we'll
2281 * be awakened when they arrive. */
2282 current->real_blocked = current->blocked;
2283 sigandsets(&current->blocked, &current->blocked, &these);
2284 recalc_sigpending();
2285 spin_unlock_irq(&current->sighand->siglock);
2286
75bcc8c5 2287 timeout = schedule_timeout_interruptible(timeout);
1da177e4 2288
1da177e4
LT
2289 spin_lock_irq(&current->sighand->siglock);
2290 sig = dequeue_signal(current, &these, &info);
2291 current->blocked = current->real_blocked;
2292 siginitset(&current->real_blocked, 0);
2293 recalc_sigpending();
2294 }
2295 }
2296 spin_unlock_irq(&current->sighand->siglock);
2297
2298 if (sig) {
2299 ret = sig;
2300 if (uinfo) {
2301 if (copy_siginfo_to_user(uinfo, &info))
2302 ret = -EFAULT;
2303 }
2304 } else {
2305 ret = -EAGAIN;
2306 if (timeout)
2307 ret = -EINTR;
2308 }
2309
2310 return ret;
2311}
2312
17da2bd9 2313SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
1da177e4
LT
2314{
2315 struct siginfo info;
2316
2317 info.si_signo = sig;
2318 info.si_errno = 0;
2319 info.si_code = SI_USER;
b488893a 2320 info.si_pid = task_tgid_vnr(current);
76aac0e9 2321 info.si_uid = current_uid();
1da177e4
LT
2322
2323 return kill_something_info(sig, &info, pid);
2324}
2325
30b4ae8a
TG
2326static int
2327do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
1da177e4 2328{
1da177e4 2329 struct task_struct *p;
30b4ae8a 2330 int error = -ESRCH;
1da177e4 2331
3547ff3a 2332 rcu_read_lock();
228ebcbe 2333 p = find_task_by_vpid(pid);
b488893a 2334 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
30b4ae8a 2335 error = check_kill_permission(sig, info, p);
1da177e4
LT
2336 /*
2337 * The null signal is a permissions and process existence
2338 * probe. No signal is actually delivered.
2339 */
4a30debf
ON
2340 if (!error && sig) {
2341 error = do_send_sig_info(sig, info, p, false);
2342 /*
2343 * If lock_task_sighand() failed we pretend the task
2344 * dies after receiving the signal. The window is tiny,
2345 * and the signal is private anyway.
2346 */
2347 if (unlikely(error == -ESRCH))
2348 error = 0;
1da177e4
LT
2349 }
2350 }
3547ff3a 2351 rcu_read_unlock();
6dd69f10 2352
1da177e4
LT
2353 return error;
2354}
2355
30b4ae8a
TG
2356static int do_tkill(pid_t tgid, pid_t pid, int sig)
2357{
2358 struct siginfo info;
2359
2360 info.si_signo = sig;
2361 info.si_errno = 0;
2362 info.si_code = SI_TKILL;
2363 info.si_pid = task_tgid_vnr(current);
2364 info.si_uid = current_uid();
2365
2366 return do_send_specific(tgid, pid, sig, &info);
2367}
2368
6dd69f10
VL
2369/**
2370 * sys_tgkill - send signal to one specific thread
2371 * @tgid: the thread group ID of the thread
2372 * @pid: the PID of the thread
2373 * @sig: signal to be sent
2374 *
72fd4a35 2375 * This syscall also checks the @tgid and returns -ESRCH even if the PID
6dd69f10
VL
2376 * exists but it's not belonging to the target process anymore. This
2377 * method solves the problem of threads exiting and PIDs getting reused.
2378 */
a5f8fa9e 2379SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
6dd69f10
VL
2380{
2381 /* This is only valid for single tasks */
2382 if (pid <= 0 || tgid <= 0)
2383 return -EINVAL;
2384
2385 return do_tkill(tgid, pid, sig);
2386}
2387
1da177e4
LT
2388/*
2389 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2390 */
a5f8fa9e 2391SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
1da177e4 2392{
1da177e4
LT
2393 /* This is only valid for single tasks */
2394 if (pid <= 0)
2395 return -EINVAL;
2396
6dd69f10 2397 return do_tkill(0, pid, sig);
1da177e4
LT
2398}
2399
a5f8fa9e
HC
2400SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
2401 siginfo_t __user *, uinfo)
1da177e4
LT
2402{
2403 siginfo_t info;
2404
2405 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2406 return -EFAULT;
2407
2408 /* Not even root can pretend to send signals from the kernel.
2409 Nor can they impersonate a kill(), which adds source info. */
2410 if (info.si_code >= 0)
2411 return -EPERM;
2412 info.si_signo = sig;
2413
2414 /* POSIX.1b doesn't mention process groups. */
2415 return kill_proc_info(sig, &info, pid);
2416}
2417
62ab4505
TG
2418long do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
2419{
2420 /* This is only valid for single tasks */
2421 if (pid <= 0 || tgid <= 0)
2422 return -EINVAL;
2423
2424 /* Not even root can pretend to send signals from the kernel.
2425 Nor can they impersonate a kill(), which adds source info. */
2426 if (info->si_code >= 0)
2427 return -EPERM;
2428 info->si_signo = sig;
2429
2430 return do_send_specific(tgid, pid, sig, info);
2431}
2432
2433SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
2434 siginfo_t __user *, uinfo)
2435{
2436 siginfo_t info;
2437
2438 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2439 return -EFAULT;
2440
2441 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
2442}
2443
88531f72 2444int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
1da177e4 2445{
93585eea 2446 struct task_struct *t = current;
1da177e4 2447 struct k_sigaction *k;
71fabd5e 2448 sigset_t mask;
1da177e4 2449
7ed20e1a 2450 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
1da177e4
LT
2451 return -EINVAL;
2452
93585eea 2453 k = &t->sighand->action[sig-1];
1da177e4
LT
2454
2455 spin_lock_irq(&current->sighand->siglock);
1da177e4
LT
2456 if (oact)
2457 *oact = *k;
2458
2459 if (act) {
9ac95f2f
ON
2460 sigdelsetmask(&act->sa.sa_mask,
2461 sigmask(SIGKILL) | sigmask(SIGSTOP));
88531f72 2462 *k = *act;
1da177e4
LT
2463 /*
2464 * POSIX 3.3.1.3:
2465 * "Setting a signal action to SIG_IGN for a signal that is
2466 * pending shall cause the pending signal to be discarded,
2467 * whether or not it is blocked."
2468 *
2469 * "Setting a signal action to SIG_DFL for a signal that is
2470 * pending and whose default action is to ignore the signal
2471 * (for example, SIGCHLD), shall cause the pending signal to
2472 * be discarded, whether or not it is blocked"
2473 */
35de254d 2474 if (sig_handler_ignored(sig_handler(t, sig), sig)) {
71fabd5e
GA
2475 sigemptyset(&mask);
2476 sigaddset(&mask, sig);
2477 rm_from_queue_full(&mask, &t->signal->shared_pending);
1da177e4 2478 do {
71fabd5e 2479 rm_from_queue_full(&mask, &t->pending);
1da177e4
LT
2480 t = next_thread(t);
2481 } while (t != current);
1da177e4 2482 }
1da177e4
LT
2483 }
2484
2485 spin_unlock_irq(&current->sighand->siglock);
2486 return 0;
2487}
2488
2489int
2490do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2491{
2492 stack_t oss;
2493 int error;
2494
0083fc2c
LT
2495 oss.ss_sp = (void __user *) current->sas_ss_sp;
2496 oss.ss_size = current->sas_ss_size;
2497 oss.ss_flags = sas_ss_flags(sp);
1da177e4
LT
2498
2499 if (uss) {
2500 void __user *ss_sp;
2501 size_t ss_size;
2502 int ss_flags;
2503
2504 error = -EFAULT;
0dd8486b
LT
2505 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
2506 goto out;
2507 error = __get_user(ss_sp, &uss->ss_sp) |
2508 __get_user(ss_flags, &uss->ss_flags) |
2509 __get_user(ss_size, &uss->ss_size);
2510 if (error)
1da177e4
LT
2511 goto out;
2512
2513 error = -EPERM;
2514 if (on_sig_stack(sp))
2515 goto out;
2516
2517 error = -EINVAL;
2518 /*
2519 *
2520 * Note - this code used to test ss_flags incorrectly
2521 * old code may have been written using ss_flags==0
2522 * to mean ss_flags==SS_ONSTACK (as this was the only
2523 * way that worked) - this fix preserves that older
2524 * mechanism
2525 */
2526 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2527 goto out;
2528
2529 if (ss_flags == SS_DISABLE) {
2530 ss_size = 0;
2531 ss_sp = NULL;
2532 } else {
2533 error = -ENOMEM;
2534 if (ss_size < MINSIGSTKSZ)
2535 goto out;
2536 }
2537
2538 current->sas_ss_sp = (unsigned long) ss_sp;
2539 current->sas_ss_size = ss_size;
2540 }
2541
0083fc2c 2542 error = 0;
1da177e4
LT
2543 if (uoss) {
2544 error = -EFAULT;
0083fc2c 2545 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
1da177e4 2546 goto out;
0083fc2c
LT
2547 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
2548 __put_user(oss.ss_size, &uoss->ss_size) |
2549 __put_user(oss.ss_flags, &uoss->ss_flags);
1da177e4
LT
2550 }
2551
1da177e4
LT
2552out:
2553 return error;
2554}
2555
2556#ifdef __ARCH_WANT_SYS_SIGPENDING
2557
b290ebe2 2558SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
1da177e4
LT
2559{
2560 return do_sigpending(set, sizeof(*set));
2561}
2562
2563#endif
2564
2565#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2566/* Some platforms have their own version with special arguments others
2567 support only sys_rt_sigprocmask. */
2568
b290ebe2
HC
2569SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, set,
2570 old_sigset_t __user *, oset)
1da177e4
LT
2571{
2572 int error;
2573 old_sigset_t old_set, new_set;
2574
2575 if (set) {
2576 error = -EFAULT;
2577 if (copy_from_user(&new_set, set, sizeof(*set)))
2578 goto out;
2579 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2580
2581 spin_lock_irq(&current->sighand->siglock);
2582 old_set = current->blocked.sig[0];
2583
2584 error = 0;
2585 switch (how) {
2586 default:
2587 error = -EINVAL;
2588 break;
2589 case SIG_BLOCK:
2590 sigaddsetmask(&current->blocked, new_set);
2591 break;
2592 case SIG_UNBLOCK:
2593 sigdelsetmask(&current->blocked, new_set);
2594 break;
2595 case SIG_SETMASK:
2596 current->blocked.sig[0] = new_set;
2597 break;
2598 }
2599
2600 recalc_sigpending();
2601 spin_unlock_irq(&current->sighand->siglock);
2602 if (error)
2603 goto out;
2604 if (oset)
2605 goto set_old;
2606 } else if (oset) {
2607 old_set = current->blocked.sig[0];
2608 set_old:
2609 error = -EFAULT;
2610 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2611 goto out;
2612 }
2613 error = 0;
2614out:
2615 return error;
2616}
2617#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2618
2619#ifdef __ARCH_WANT_SYS_RT_SIGACTION
d4e82042
HC
2620SYSCALL_DEFINE4(rt_sigaction, int, sig,
2621 const struct sigaction __user *, act,
2622 struct sigaction __user *, oact,
2623 size_t, sigsetsize)
1da177e4
LT
2624{
2625 struct k_sigaction new_sa, old_sa;
2626 int ret = -EINVAL;
2627
2628 /* XXX: Don't preclude handling different sized sigset_t's. */
2629 if (sigsetsize != sizeof(sigset_t))
2630 goto out;
2631
2632 if (act) {
2633 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2634 return -EFAULT;
2635 }
2636
2637 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2638
2639 if (!ret && oact) {
2640 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2641 return -EFAULT;
2642 }
2643out:
2644 return ret;
2645}
2646#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2647
2648#ifdef __ARCH_WANT_SYS_SGETMASK
2649
2650/*
2651 * For backwards compatibility. Functionality superseded by sigprocmask.
2652 */
a5f8fa9e 2653SYSCALL_DEFINE0(sgetmask)
1da177e4
LT
2654{
2655 /* SMP safe */
2656 return current->blocked.sig[0];
2657}
2658
a5f8fa9e 2659SYSCALL_DEFINE1(ssetmask, int, newmask)
1da177e4
LT
2660{
2661 int old;
2662
2663 spin_lock_irq(&current->sighand->siglock);
2664 old = current->blocked.sig[0];
2665
2666 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2667 sigmask(SIGSTOP)));
2668 recalc_sigpending();
2669 spin_unlock_irq(&current->sighand->siglock);
2670
2671 return old;
2672}
2673#endif /* __ARCH_WANT_SGETMASK */
2674
2675#ifdef __ARCH_WANT_SYS_SIGNAL
2676/*
2677 * For backwards compatibility. Functionality superseded by sigaction.
2678 */
a5f8fa9e 2679SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
1da177e4
LT
2680{
2681 struct k_sigaction new_sa, old_sa;
2682 int ret;
2683
2684 new_sa.sa.sa_handler = handler;
2685 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
c70d3d70 2686 sigemptyset(&new_sa.sa.sa_mask);
1da177e4
LT
2687
2688 ret = do_sigaction(sig, &new_sa, &old_sa);
2689
2690 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2691}
2692#endif /* __ARCH_WANT_SYS_SIGNAL */
2693
2694#ifdef __ARCH_WANT_SYS_PAUSE
2695
a5f8fa9e 2696SYSCALL_DEFINE0(pause)
1da177e4
LT
2697{
2698 current->state = TASK_INTERRUPTIBLE;
2699 schedule();
2700 return -ERESTARTNOHAND;
2701}
2702
2703#endif
2704
150256d8 2705#ifdef __ARCH_WANT_SYS_RT_SIGSUSPEND
d4e82042 2706SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
150256d8
DW
2707{
2708 sigset_t newset;
2709
2710 /* XXX: Don't preclude handling different sized sigset_t's. */
2711 if (sigsetsize != sizeof(sigset_t))
2712 return -EINVAL;
2713
2714 if (copy_from_user(&newset, unewset, sizeof(newset)))
2715 return -EFAULT;
2716 sigdelsetmask(&newset, sigmask(SIGKILL)|sigmask(SIGSTOP));
2717
2718 spin_lock_irq(&current->sighand->siglock);
2719 current->saved_sigmask = current->blocked;
2720 current->blocked = newset;
2721 recalc_sigpending();
2722 spin_unlock_irq(&current->sighand->siglock);
2723
2724 current->state = TASK_INTERRUPTIBLE;
2725 schedule();
4e4c22c7 2726 set_restore_sigmask();
150256d8
DW
2727 return -ERESTARTNOHAND;
2728}
2729#endif /* __ARCH_WANT_SYS_RT_SIGSUSPEND */
2730
f269fdd1
DH
2731__attribute__((weak)) const char *arch_vma_name(struct vm_area_struct *vma)
2732{
2733 return NULL;
2734}
2735
1da177e4
LT
2736void __init signals_init(void)
2737{
0a31bd5f 2738 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
1da177e4 2739}
67fc4e0c
JW
2740
2741#ifdef CONFIG_KGDB_KDB
2742#include <linux/kdb.h>
2743/*
2744 * kdb_send_sig_info - Allows kdb to send signals without exposing
2745 * signal internals. This function checks if the required locks are
2746 * available before calling the main signal code, to avoid kdb
2747 * deadlocks.
2748 */
2749void
2750kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
2751{
2752 static struct task_struct *kdb_prev_t;
2753 int sig, new_t;
2754 if (!spin_trylock(&t->sighand->siglock)) {
2755 kdb_printf("Can't do kill command now.\n"
2756 "The sigmask lock is held somewhere else in "
2757 "kernel, try again later\n");
2758 return;
2759 }
2760 spin_unlock(&t->sighand->siglock);
2761 new_t = kdb_prev_t != t;
2762 kdb_prev_t = t;
2763 if (t->state != TASK_RUNNING && new_t) {
2764 kdb_printf("Process is not RUNNING, sending a signal from "
2765 "kdb risks deadlock\n"
2766 "on the run queue locks. "
2767 "The signal has _not_ been sent.\n"
2768 "Reissue the kill command if you want to risk "
2769 "the deadlock.\n");
2770 return;
2771 }
2772 sig = info->si_signo;
2773 if (send_sig_info(sig, info, t))
2774 kdb_printf("Fail to deliver Signal %d to process %d.\n",
2775 sig, t->pid);
2776 else
2777 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
2778}
2779#endif /* CONFIG_KGDB_KDB */