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