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