Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

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