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