~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/signal.c

Version: ~ [ linux-5.2-rc1 ] ~ [ linux-5.1.2 ] ~ [ linux-5.0.16 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.43 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.119 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.176 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.179 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.139 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.67 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp