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