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