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