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