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