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