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