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