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