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