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