MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / kernel / signal.c
blobdf2cd4216838cce95f10b8f6cf4b2156008cfa10
1 /*
2 * linux/kernel/signal.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
13 #include <linux/config.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/smp_lock.h>
17 #include <linux/init.h>
18 #include <linux/sched.h>
19 #include <linux/fs.h>
20 #include <linux/tty.h>
21 #include <linux/binfmts.h>
22 #include <linux/security.h>
23 #include <linux/ptrace.h>
24 #include <asm/param.h>
25 #include <asm/uaccess.h>
26 #include <asm/unistd.h>
27 #include <asm/siginfo.h>
29 extern void k_getrusage(struct task_struct *, int, struct rusage *);
32 * SLAB caches for signal bits.
35 static kmem_cache_t *sigqueue_cachep;
38 * In POSIX a signal is sent either to a specific thread (Linux task)
39 * or to the process as a whole (Linux thread group). How the signal
40 * is sent determines whether it's to one thread or the whole group,
41 * which determines which signal mask(s) are involved in blocking it
42 * from being delivered until later. When the signal is delivered,
43 * either it's caught or ignored by a user handler or it has a default
44 * effect that applies to the whole thread group (POSIX process).
46 * The possible effects an unblocked signal set to SIG_DFL can have are:
47 * ignore - Nothing Happens
48 * terminate - kill the process, i.e. all threads in the group,
49 * similar to exit_group. The group leader (only) reports
50 * WIFSIGNALED status to its parent.
51 * coredump - write a core dump file describing all threads using
52 * the same mm and then kill all those threads
53 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
55 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
56 * Other signals when not blocked and set to SIG_DFL behaves as follows.
57 * The job control signals also have other special effects.
59 * +--------------------+------------------+
60 * | POSIX signal | default action |
61 * +--------------------+------------------+
62 * | SIGHUP | terminate |
63 * | SIGINT | terminate |
64 * | SIGQUIT | coredump |
65 * | SIGILL | coredump |
66 * | SIGTRAP | coredump |
67 * | SIGABRT/SIGIOT | coredump |
68 * | SIGBUS | coredump |
69 * | SIGFPE | coredump |
70 * | SIGKILL | terminate(+) |
71 * | SIGUSR1 | terminate |
72 * | SIGSEGV | coredump |
73 * | SIGUSR2 | terminate |
74 * | SIGPIPE | terminate |
75 * | SIGALRM | terminate |
76 * | SIGTERM | terminate |
77 * | SIGCHLD | ignore |
78 * | SIGCONT | ignore(*) |
79 * | SIGSTOP | stop(*)(+) |
80 * | SIGTSTP | stop(*) |
81 * | SIGTTIN | stop(*) |
82 * | SIGTTOU | stop(*) |
83 * | SIGURG | ignore |
84 * | SIGXCPU | coredump |
85 * | SIGXFSZ | coredump |
86 * | SIGVTALRM | terminate |
87 * | SIGPROF | terminate |
88 * | SIGPOLL/SIGIO | terminate |
89 * | SIGSYS/SIGUNUSED | coredump |
90 * | SIGSTKFLT | terminate |
91 * | SIGWINCH | ignore |
92 * | SIGPWR | terminate |
93 * | SIGRTMIN-SIGRTMAX | terminate |
94 * +--------------------+------------------+
95 * | non-POSIX signal | default action |
96 * +--------------------+------------------+
97 * | SIGEMT | coredump |
98 * +--------------------+------------------+
100 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
101 * (*) Special job control effects:
102 * When SIGCONT is sent, it resumes the process (all threads in the group)
103 * from TASK_STOPPED state and also clears any pending/queued stop signals
104 * (any of those marked with "stop(*)"). This happens regardless of blocking,
105 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
106 * any pending/queued SIGCONT signals; this happens regardless of blocking,
107 * catching, or ignored the stop signal, though (except for SIGSTOP) the
108 * default action of stopping the process may happen later or never.
111 #ifdef SIGEMT
112 #define M_SIGEMT M(SIGEMT)
113 #else
114 #define M_SIGEMT 0
115 #endif
117 #if SIGRTMIN > BITS_PER_LONG
118 #define M(sig) (1ULL << ((sig)-1))
119 #else
120 #define M(sig) (1UL << ((sig)-1))
121 #endif
122 #define T(sig, mask) (M(sig) & (mask))
124 #define SIG_KERNEL_ONLY_MASK (\
125 M(SIGKILL) | M(SIGSTOP) )
127 #define SIG_KERNEL_STOP_MASK (\
128 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
130 #define SIG_KERNEL_COREDUMP_MASK (\
131 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
132 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
133 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
135 #define SIG_KERNEL_IGNORE_MASK (\
136 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
138 #define sig_kernel_only(sig) \
139 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
140 #define sig_kernel_coredump(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
142 #define sig_kernel_ignore(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
144 #define sig_kernel_stop(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
147 #define sig_user_defined(t, signr) \
148 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
149 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
151 #define sig_fatal(t, signr) \
152 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
153 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
155 #define sig_avoid_stop_race() \
156 (sigtestsetmask(&current->pending.signal, M(SIGCONT) | M(SIGKILL)) || \
157 sigtestsetmask(&current->signal->shared_pending.signal, \
158 M(SIGCONT) | M(SIGKILL)))
160 static int sig_ignored(struct task_struct *t, int sig)
162 void __user * handler;
165 * Tracers always want to know about signals..
167 if (t->ptrace & PT_PTRACED)
168 return 0;
171 * Blocked signals are never ignored, since the
172 * signal handler may change by the time it is
173 * unblocked.
175 if (sigismember(&t->blocked, sig))
176 return 0;
178 /* Is it explicitly or implicitly ignored? */
179 handler = t->sighand->action[sig-1].sa.sa_handler;
180 return handler == SIG_IGN ||
181 (handler == SIG_DFL && sig_kernel_ignore(sig));
185 * Re-calculate pending state from the set of locally pending
186 * signals, globally pending signals, and blocked signals.
188 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
190 unsigned long ready;
191 long i;
193 switch (_NSIG_WORDS) {
194 default:
195 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
196 ready |= signal->sig[i] &~ blocked->sig[i];
197 break;
199 case 4: ready = signal->sig[3] &~ blocked->sig[3];
200 ready |= signal->sig[2] &~ blocked->sig[2];
201 ready |= signal->sig[1] &~ blocked->sig[1];
202 ready |= signal->sig[0] &~ blocked->sig[0];
203 break;
205 case 2: ready = signal->sig[1] &~ blocked->sig[1];
206 ready |= signal->sig[0] &~ blocked->sig[0];
207 break;
209 case 1: ready = signal->sig[0] &~ blocked->sig[0];
211 return ready != 0;
214 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
216 fastcall void recalc_sigpending_tsk(struct task_struct *t)
218 if (t->signal->group_stop_count > 0 ||
219 PENDING(&t->pending, &t->blocked) ||
220 PENDING(&t->signal->shared_pending, &t->blocked))
221 set_tsk_thread_flag(t, TIF_SIGPENDING);
222 else
223 clear_tsk_thread_flag(t, TIF_SIGPENDING);
226 void recalc_sigpending(void)
228 recalc_sigpending_tsk(current);
231 /* Given the mask, find the first available signal that should be serviced. */
233 static int
234 next_signal(struct sigpending *pending, sigset_t *mask)
236 unsigned long i, *s, *m, x;
237 int sig = 0;
239 s = pending->signal.sig;
240 m = mask->sig;
241 switch (_NSIG_WORDS) {
242 default:
243 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
244 if ((x = *s &~ *m) != 0) {
245 sig = ffz(~x) + i*_NSIG_BPW + 1;
246 break;
248 break;
250 case 2: if ((x = s[0] &~ m[0]) != 0)
251 sig = 1;
252 else if ((x = s[1] &~ m[1]) != 0)
253 sig = _NSIG_BPW + 1;
254 else
255 break;
256 sig += ffz(~x);
257 break;
259 case 1: if ((x = *s &~ *m) != 0)
260 sig = ffz(~x) + 1;
261 break;
264 return sig;
267 static struct sigqueue *__sigqueue_alloc(void)
269 struct sigqueue *q = NULL;
271 if (atomic_read(&current->user->sigpending) <
272 current->rlim[RLIMIT_SIGPENDING].rlim_cur)
273 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
274 if (q) {
275 INIT_LIST_HEAD(&q->list);
276 q->flags = 0;
277 q->lock = NULL;
278 q->user = get_uid(current->user);
279 atomic_inc(&q->user->sigpending);
281 return(q);
284 static inline void __sigqueue_free(struct sigqueue *q)
286 if (q->flags & SIGQUEUE_PREALLOC)
287 return;
288 atomic_dec(&q->user->sigpending);
289 free_uid(q->user);
290 kmem_cache_free(sigqueue_cachep, q);
293 static void flush_sigqueue(struct sigpending *queue)
295 struct sigqueue *q;
297 sigemptyset(&queue->signal);
298 while (!list_empty(&queue->list)) {
299 q = list_entry(queue->list.next, struct sigqueue , list);
300 list_del_init(&q->list);
301 __sigqueue_free(q);
306 * Flush all pending signals for a task.
309 void
310 flush_signals(struct task_struct *t)
312 unsigned long flags;
314 spin_lock_irqsave(&t->sighand->siglock, flags);
315 clear_tsk_thread_flag(t,TIF_SIGPENDING);
316 flush_sigqueue(&t->pending);
317 flush_sigqueue(&t->signal->shared_pending);
318 spin_unlock_irqrestore(&t->sighand->siglock, flags);
322 * This function expects the tasklist_lock write-locked.
324 void __exit_sighand(struct task_struct *tsk)
326 struct sighand_struct * sighand = tsk->sighand;
328 /* Ok, we're done with the signal handlers */
329 tsk->sighand = NULL;
330 if (atomic_dec_and_test(&sighand->count))
331 kmem_cache_free(sighand_cachep, sighand);
334 void exit_sighand(struct task_struct *tsk)
336 write_lock_irq(&tasklist_lock);
337 __exit_sighand(tsk);
338 write_unlock_irq(&tasklist_lock);
342 * This function expects the tasklist_lock write-locked.
344 void __exit_signal(struct task_struct *tsk)
346 struct signal_struct * sig = tsk->signal;
347 struct sighand_struct * sighand = tsk->sighand;
349 if (!sig)
350 BUG();
351 if (!atomic_read(&sig->count))
352 BUG();
353 spin_lock(&sighand->siglock);
354 if (atomic_dec_and_test(&sig->count)) {
355 if (tsk == sig->curr_target)
356 sig->curr_target = next_thread(tsk);
357 tsk->signal = NULL;
358 spin_unlock(&sighand->siglock);
359 flush_sigqueue(&sig->shared_pending);
360 } else {
362 * If there is any task waiting for the group exit
363 * then notify it:
365 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
366 wake_up_process(sig->group_exit_task);
367 sig->group_exit_task = NULL;
369 if (tsk == sig->curr_target)
370 sig->curr_target = next_thread(tsk);
371 tsk->signal = NULL;
373 * Accumulate here the counters for all threads but the
374 * group leader as they die, so they can be added into
375 * the process-wide totals when those are taken.
376 * The group leader stays around as a zombie as long
377 * as there are other threads. When it gets reaped,
378 * the exit.c code will add its counts into these totals.
379 * We won't ever get here for the group leader, since it
380 * will have been the last reference on the signal_struct.
382 sig->utime += tsk->utime;
383 sig->stime += tsk->stime;
384 sig->min_flt += tsk->min_flt;
385 sig->maj_flt += tsk->maj_flt;
386 sig->nvcsw += tsk->nvcsw;
387 sig->nivcsw += tsk->nivcsw;
388 spin_unlock(&sighand->siglock);
389 sig = NULL; /* Marker for below. */
391 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
392 flush_sigqueue(&tsk->pending);
393 if (sig) {
395 * We are cleaning up the signal_struct here. We delayed
396 * calling exit_itimers until after flush_sigqueue, just in
397 * case our thread-local pending queue contained a queued
398 * timer signal that would have been cleared in
399 * exit_itimers. When that called sigqueue_free, it would
400 * attempt to re-take the tasklist_lock and deadlock. This
401 * can never happen if we ensure that all queues the
402 * timer's signal might be queued on have been flushed
403 * first. The shared_pending queue, and our own pending
404 * queue are the only queues the timer could be on, since
405 * there are no other threads left in the group and timer
406 * signals are constrained to threads inside the group.
408 exit_itimers(sig);
409 kmem_cache_free(signal_cachep, sig);
413 void exit_signal(struct task_struct *tsk)
415 write_lock_irq(&tasklist_lock);
416 __exit_signal(tsk);
417 write_unlock_irq(&tasklist_lock);
421 * Flush all handlers for a task.
424 void
425 flush_signal_handlers(struct task_struct *t, int force_default)
427 int i;
428 struct k_sigaction *ka = &t->sighand->action[0];
429 for (i = _NSIG ; i != 0 ; i--) {
430 if (force_default || ka->sa.sa_handler != SIG_IGN)
431 ka->sa.sa_handler = SIG_DFL;
432 ka->sa.sa_flags = 0;
433 sigemptyset(&ka->sa.sa_mask);
434 ka++;
439 /* Notify the system that a driver wants to block all signals for this
440 * process, and wants to be notified if any signals at all were to be
441 * sent/acted upon. If the notifier routine returns non-zero, then the
442 * signal will be acted upon after all. If the notifier routine returns 0,
443 * then then signal will be blocked. Only one block per process is
444 * allowed. priv is a pointer to private data that the notifier routine
445 * can use to determine if the signal should be blocked or not. */
447 void
448 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
450 unsigned long flags;
452 spin_lock_irqsave(&current->sighand->siglock, flags);
453 current->notifier_mask = mask;
454 current->notifier_data = priv;
455 current->notifier = notifier;
456 spin_unlock_irqrestore(&current->sighand->siglock, flags);
459 /* Notify the system that blocking has ended. */
461 void
462 unblock_all_signals(void)
464 unsigned long flags;
466 spin_lock_irqsave(&current->sighand->siglock, flags);
467 current->notifier = NULL;
468 current->notifier_data = NULL;
469 recalc_sigpending();
470 spin_unlock_irqrestore(&current->sighand->siglock, flags);
473 static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
475 struct sigqueue *q, *first = NULL;
476 int still_pending = 0;
478 if (unlikely(!sigismember(&list->signal, sig)))
479 return 0;
482 * Collect the siginfo appropriate to this signal. Check if
483 * there is another siginfo for the same signal.
485 list_for_each_entry(q, &list->list, list) {
486 if (q->info.si_signo == sig) {
487 if (first) {
488 still_pending = 1;
489 break;
491 first = q;
494 if (first) {
495 list_del_init(&first->list);
496 copy_siginfo(info, &first->info);
497 __sigqueue_free(first);
498 if (!still_pending)
499 sigdelset(&list->signal, sig);
500 } else {
502 /* Ok, it wasn't in the queue. This must be
503 a fast-pathed signal or we must have been
504 out of queue space. So zero out the info.
506 sigdelset(&list->signal, sig);
507 info->si_signo = sig;
508 info->si_errno = 0;
509 info->si_code = 0;
510 info->si_pid = 0;
511 info->si_uid = 0;
513 return 1;
516 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
517 siginfo_t *info)
519 int sig = 0;
521 sig = next_signal(pending, mask);
522 if (sig) {
523 if (current->notifier) {
524 if (sigismember(current->notifier_mask, sig)) {
525 if (!(current->notifier)(current->notifier_data)) {
526 clear_thread_flag(TIF_SIGPENDING);
527 return 0;
532 if (!collect_signal(sig, pending, info))
533 sig = 0;
536 recalc_sigpending();
538 return sig;
542 * Dequeue a signal and return the element to the caller, which is
543 * expected to free it.
545 * All callers have to hold the siglock.
547 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
549 int signr = __dequeue_signal(&tsk->pending, mask, info);
550 if (!signr)
551 signr = __dequeue_signal(&tsk->signal->shared_pending,
552 mask, info);
553 if ( signr &&
554 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
555 info->si_sys_private){
556 do_schedule_next_timer(info);
558 return signr;
562 * Tell a process that it has a new active signal..
564 * NOTE! we rely on the previous spin_lock to
565 * lock interrupts for us! We can only be called with
566 * "siglock" held, and the local interrupt must
567 * have been disabled when that got acquired!
569 * No need to set need_resched since signal event passing
570 * goes through ->blocked
572 void signal_wake_up(struct task_struct *t, int resume)
574 unsigned int mask;
576 set_tsk_thread_flag(t, TIF_SIGPENDING);
579 * If resume is set, we want to wake it up in the TASK_STOPPED case.
580 * We don't check for TASK_STOPPED because there is a race with it
581 * executing another processor and just now entering stopped state.
582 * By calling wake_up_process any time resume is set, we ensure
583 * the process will wake up and handle its stop or death signal.
585 mask = TASK_INTERRUPTIBLE;
586 if (resume)
587 mask |= TASK_STOPPED;
588 if (!wake_up_state(t, mask))
589 kick_process(t);
593 * Remove signals in mask from the pending set and queue.
594 * Returns 1 if any signals were found.
596 * All callers must be holding the siglock.
598 static int rm_from_queue(unsigned long mask, struct sigpending *s)
600 struct sigqueue *q, *n;
602 if (!sigtestsetmask(&s->signal, mask))
603 return 0;
605 sigdelsetmask(&s->signal, mask);
606 list_for_each_entry_safe(q, n, &s->list, list) {
607 if (q->info.si_signo < SIGRTMIN &&
608 (mask & sigmask(q->info.si_signo))) {
609 list_del_init(&q->list);
610 __sigqueue_free(q);
613 return 1;
617 * Bad permissions for sending the signal
619 static int check_kill_permission(int sig, struct siginfo *info,
620 struct task_struct *t)
622 int error = -EINVAL;
623 if (sig < 0 || sig > _NSIG)
624 return error;
625 error = -EPERM;
626 if ((!info || ((unsigned long)info != 1 &&
627 (unsigned long)info != 2 && SI_FROMUSER(info)))
628 && ((sig != SIGCONT) ||
629 (current->signal->session != t->signal->session))
630 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
631 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
632 && !capable(CAP_KILL))
633 return error;
634 return security_task_kill(t, info, sig);
637 /* forward decl */
638 static void do_notify_parent_cldstop(struct task_struct *tsk,
639 struct task_struct *parent,
640 int why);
643 * Handle magic process-wide effects of stop/continue signals.
644 * Unlike the signal actions, these happen immediately at signal-generation
645 * time regardless of blocking, ignoring, or handling. This does the
646 * actual continuing for SIGCONT, but not the actual stopping for stop
647 * signals. The process stop is done as a signal action for SIG_DFL.
649 static void handle_stop_signal(int sig, struct task_struct *p)
651 struct task_struct *t;
653 if (sig_kernel_stop(sig)) {
655 * This is a stop signal. Remove SIGCONT from all queues.
657 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
658 t = p;
659 do {
660 rm_from_queue(sigmask(SIGCONT), &t->pending);
661 t = next_thread(t);
662 } while (t != p);
663 } else if (sig == SIGCONT) {
665 * Remove all stop signals from all queues,
666 * and wake all threads.
668 if (unlikely(p->signal->group_stop_count > 0)) {
670 * There was a group stop in progress. We'll
671 * pretend it finished before we got here. We are
672 * obliged to report it to the parent: if the
673 * SIGSTOP happened "after" this SIGCONT, then it
674 * would have cleared this pending SIGCONT. If it
675 * happened "before" this SIGCONT, then the parent
676 * got the SIGCHLD about the stop finishing before
677 * the continue happened. We do the notification
678 * now, and it's as if the stop had finished and
679 * the SIGCHLD was pending on entry to this kill.
681 p->signal->group_stop_count = 0;
682 p->signal->stop_state = 1;
683 spin_unlock(&p->sighand->siglock);
684 if (p->ptrace & PT_PTRACED)
685 do_notify_parent_cldstop(p, p->parent,
686 CLD_STOPPED);
687 else
688 do_notify_parent_cldstop(
689 p->group_leader,
690 p->group_leader->real_parent,
691 CLD_STOPPED);
692 spin_lock(&p->sighand->siglock);
694 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
695 t = p;
696 do {
697 unsigned int state;
698 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
701 * If there is a handler for SIGCONT, we must make
702 * sure that no thread returns to user mode before
703 * we post the signal, in case it was the only
704 * thread eligible to run the signal handler--then
705 * it must not do anything between resuming and
706 * running the handler. With the TIF_SIGPENDING
707 * flag set, the thread will pause and acquire the
708 * siglock that we hold now and until we've queued
709 * the pending signal.
711 * Wake up the stopped thread _after_ setting
712 * TIF_SIGPENDING
714 state = TASK_STOPPED;
715 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
716 set_tsk_thread_flag(t, TIF_SIGPENDING);
717 state |= TASK_INTERRUPTIBLE;
719 wake_up_state(t, state);
721 t = next_thread(t);
722 } while (t != p);
724 if (p->signal->stop_state > 0) {
726 * We were in fact stopped, and are now continued.
727 * Notify the parent with CLD_CONTINUED.
729 p->signal->stop_state = -1;
730 p->signal->group_exit_code = 0;
731 spin_unlock(&p->sighand->siglock);
732 if (p->ptrace & PT_PTRACED)
733 do_notify_parent_cldstop(p, p->parent,
734 CLD_CONTINUED);
735 else
736 do_notify_parent_cldstop(
737 p->group_leader,
738 p->group_leader->real_parent,
739 CLD_CONTINUED);
740 spin_lock(&p->sighand->siglock);
745 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
746 struct sigpending *signals)
748 struct sigqueue * q = NULL;
749 int ret = 0;
752 * fast-pathed signals for kernel-internal things like SIGSTOP
753 * or SIGKILL.
755 if ((unsigned long)info == 2)
756 goto out_set;
758 /* Real-time signals must be queued if sent by sigqueue, or
759 some other real-time mechanism. It is implementation
760 defined whether kill() does so. We attempt to do so, on
761 the principle of least surprise, but since kill is not
762 allowed to fail with EAGAIN when low on memory we just
763 make sure at least one signal gets delivered and don't
764 pass on the info struct. */
766 if (atomic_read(&t->user->sigpending) <
767 t->rlim[RLIMIT_SIGPENDING].rlim_cur)
768 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
770 if (q) {
771 q->flags = 0;
772 q->user = get_uid(t->user);
773 atomic_inc(&q->user->sigpending);
774 list_add_tail(&q->list, &signals->list);
775 switch ((unsigned long) info) {
776 case 0:
777 q->info.si_signo = sig;
778 q->info.si_errno = 0;
779 q->info.si_code = SI_USER;
780 q->info.si_pid = current->pid;
781 q->info.si_uid = current->uid;
782 break;
783 case 1:
784 q->info.si_signo = sig;
785 q->info.si_errno = 0;
786 q->info.si_code = SI_KERNEL;
787 q->info.si_pid = 0;
788 q->info.si_uid = 0;
789 break;
790 default:
791 copy_siginfo(&q->info, info);
792 break;
794 } else {
795 if (sig >= SIGRTMIN && info && (unsigned long)info != 1
796 && info->si_code != SI_USER)
798 * Queue overflow, abort. We may abort if the signal was rt
799 * and sent by user using something other than kill().
801 return -EAGAIN;
802 if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
804 * Set up a return to indicate that we dropped
805 * the signal.
807 ret = info->si_sys_private;
810 out_set:
811 sigaddset(&signals->signal, sig);
812 return ret;
815 #define LEGACY_QUEUE(sigptr, sig) \
816 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
819 static int
820 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
822 int ret = 0;
824 if (!irqs_disabled())
825 BUG();
826 #ifdef CONFIG_SMP
827 if (!spin_is_locked(&t->sighand->siglock))
828 BUG();
829 #endif
831 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
833 * Set up a return to indicate that we dropped the signal.
835 ret = info->si_sys_private;
837 /* Short-circuit ignored signals. */
838 if (sig_ignored(t, sig))
839 goto out;
841 /* Support queueing exactly one non-rt signal, so that we
842 can get more detailed information about the cause of
843 the signal. */
844 if (LEGACY_QUEUE(&t->pending, sig))
845 goto out;
847 ret = send_signal(sig, info, t, &t->pending);
848 if (!ret && !sigismember(&t->blocked, sig))
849 signal_wake_up(t, sig == SIGKILL);
850 out:
851 return ret;
855 * Force a signal that the process can't ignore: if necessary
856 * we unblock the signal and change any SIG_IGN to SIG_DFL.
860 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
862 unsigned long int flags;
863 int ret;
865 spin_lock_irqsave(&t->sighand->siglock, flags);
866 if (sigismember(&t->blocked, sig) || t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
867 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
868 sigdelset(&t->blocked, sig);
869 recalc_sigpending_tsk(t);
871 ret = specific_send_sig_info(sig, info, t);
872 spin_unlock_irqrestore(&t->sighand->siglock, flags);
874 return ret;
877 void
878 force_sig_specific(int sig, struct task_struct *t)
880 unsigned long int flags;
882 spin_lock_irqsave(&t->sighand->siglock, flags);
883 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN)
884 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
885 sigdelset(&t->blocked, sig);
886 recalc_sigpending_tsk(t);
887 specific_send_sig_info(sig, (void *)2, t);
888 spin_unlock_irqrestore(&t->sighand->siglock, flags);
892 * Test if P wants to take SIG. After we've checked all threads with this,
893 * it's equivalent to finding no threads not blocking SIG. Any threads not
894 * blocking SIG were ruled out because they are not running and already
895 * have pending signals. Such threads will dequeue from the shared queue
896 * as soon as they're available, so putting the signal on the shared queue
897 * will be equivalent to sending it to one such thread.
899 #define wants_signal(sig, p, mask) \
900 (!sigismember(&(p)->blocked, sig) \
901 && !((p)->state & mask) \
902 && !((p)->flags & PF_EXITING) \
903 && (task_curr(p) || !signal_pending(p)))
906 static void
907 __group_complete_signal(int sig, struct task_struct *p)
909 unsigned int mask;
910 struct task_struct *t;
913 * Don't bother zombies and stopped tasks (but
914 * SIGKILL will punch through stopped state)
916 mask = TASK_DEAD | TASK_ZOMBIE | TASK_TRACED;
917 if (sig != SIGKILL)
918 mask |= TASK_STOPPED;
921 * Now find a thread we can wake up to take the signal off the queue.
923 * If the main thread wants the signal, it gets first crack.
924 * Probably the least surprising to the average bear.
926 if (wants_signal(sig, p, mask))
927 t = p;
928 else if (thread_group_empty(p))
930 * There is just one thread and it does not need to be woken.
931 * It will dequeue unblocked signals before it runs again.
933 return;
934 else {
936 * Otherwise try to find a suitable thread.
938 t = p->signal->curr_target;
939 if (t == NULL)
940 /* restart balancing at this thread */
941 t = p->signal->curr_target = p;
942 BUG_ON(t->tgid != p->tgid);
944 while (!wants_signal(sig, t, mask)) {
945 t = next_thread(t);
946 if (t == p->signal->curr_target)
948 * No thread needs to be woken.
949 * Any eligible threads will see
950 * the signal in the queue soon.
952 return;
954 p->signal->curr_target = t;
958 * Found a killable thread. If the signal will be fatal,
959 * then start taking the whole group down immediately.
961 if (sig_fatal(p, sig) && !p->signal->group_exit &&
962 !sigismember(&t->real_blocked, sig) &&
963 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
965 * This signal will be fatal to the whole group.
967 if (!sig_kernel_coredump(sig)) {
969 * Start a group exit and wake everybody up.
970 * This way we don't have other threads
971 * running and doing things after a slower
972 * thread has the fatal signal pending.
974 p->signal->group_exit = 1;
975 p->signal->group_exit_code = sig;
976 p->signal->group_stop_count = 0;
977 t = p;
978 do {
979 sigaddset(&t->pending.signal, SIGKILL);
980 signal_wake_up(t, 1);
981 t = next_thread(t);
982 } while (t != p);
983 return;
987 * There will be a core dump. We make all threads other
988 * than the chosen one go into a group stop so that nothing
989 * happens until it gets scheduled, takes the signal off
990 * the shared queue, and does the core dump. This is a
991 * little more complicated than strictly necessary, but it
992 * keeps the signal state that winds up in the core dump
993 * unchanged from the death state, e.g. which thread had
994 * the core-dump signal unblocked.
996 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
997 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
998 p->signal->group_stop_count = 0;
999 p->signal->group_exit_task = t;
1000 t = p;
1001 do {
1002 p->signal->group_stop_count++;
1003 signal_wake_up(t, 0);
1004 t = next_thread(t);
1005 } while (t != p);
1006 wake_up_process(p->signal->group_exit_task);
1007 return;
1011 * The signal is already in the shared-pending queue.
1012 * Tell the chosen thread to wake up and dequeue it.
1014 signal_wake_up(t, sig == SIGKILL);
1015 return;
1018 static int
1019 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1021 int ret = 0;
1023 #ifdef CONFIG_SMP
1024 if (!spin_is_locked(&p->sighand->siglock))
1025 BUG();
1026 #endif
1027 handle_stop_signal(sig, p);
1029 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
1031 * Set up a return to indicate that we dropped the signal.
1033 ret = info->si_sys_private;
1035 /* Short-circuit ignored signals. */
1036 if (sig_ignored(p, sig))
1037 return ret;
1039 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
1040 /* This is a non-RT signal and we already have one queued. */
1041 return ret;
1044 * Put this signal on the shared-pending queue, or fail with EAGAIN.
1045 * We always use the shared queue for process-wide signals,
1046 * to avoid several races.
1048 ret = send_signal(sig, info, p, &p->signal->shared_pending);
1049 if (unlikely(ret))
1050 return ret;
1052 __group_complete_signal(sig, p);
1053 return 0;
1057 * Nuke all other threads in the group.
1059 void zap_other_threads(struct task_struct *p)
1061 struct task_struct *t;
1063 p->signal->group_stop_count = 0;
1065 if (thread_group_empty(p))
1066 return;
1068 for (t = next_thread(p); t != p; t = next_thread(t)) {
1070 * Don't bother with already dead threads
1072 if (t->state & (TASK_ZOMBIE|TASK_DEAD))
1073 continue;
1076 * We don't want to notify the parent, since we are
1077 * killed as part of a thread group due to another
1078 * thread doing an execve() or similar. So set the
1079 * exit signal to -1 to allow immediate reaping of
1080 * the process. But don't detach the thread group
1081 * leader.
1083 if (t != p->group_leader)
1084 t->exit_signal = -1;
1086 sigaddset(&t->pending.signal, SIGKILL);
1087 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1088 signal_wake_up(t, 1);
1093 * Must be called with the tasklist_lock held for reading!
1095 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1097 unsigned long flags;
1098 int ret;
1100 ret = check_kill_permission(sig, info, p);
1101 if (!ret && sig && p->sighand) {
1102 spin_lock_irqsave(&p->sighand->siglock, flags);
1103 ret = __group_send_sig_info(sig, info, p);
1104 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1107 return ret;
1111 * kill_pg_info() sends a signal to a process group: this is what the tty
1112 * control characters do (^C, ^Z etc)
1115 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1117 struct task_struct *p;
1118 int retval, success;
1120 if (pgrp <= 0)
1121 return -EINVAL;
1123 success = 0;
1124 retval = -ESRCH;
1125 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1126 int err = group_send_sig_info(sig, info, p);
1127 success |= !err;
1128 retval = err;
1129 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1130 return success ? 0 : retval;
1134 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1136 int retval;
1138 read_lock(&tasklist_lock);
1139 retval = __kill_pg_info(sig, info, pgrp);
1140 read_unlock(&tasklist_lock);
1142 return retval;
1146 * kill_sl_info() sends a signal to the session leader: this is used
1147 * to send SIGHUP to the controlling process of a terminal when
1148 * the connection is lost.
1153 kill_sl_info(int sig, struct siginfo *info, pid_t sid)
1155 int err, retval = -EINVAL;
1156 struct task_struct *p;
1158 if (sid <= 0)
1159 goto out;
1161 retval = -ESRCH;
1162 read_lock(&tasklist_lock);
1163 do_each_task_pid(sid, PIDTYPE_SID, p) {
1164 if (!p->signal->leader)
1165 continue;
1166 err = group_send_sig_info(sig, info, p);
1167 if (retval)
1168 retval = err;
1169 } while_each_task_pid(sid, PIDTYPE_SID, p);
1170 read_unlock(&tasklist_lock);
1171 out:
1172 return retval;
1176 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1178 int error;
1179 struct task_struct *p;
1181 read_lock(&tasklist_lock);
1182 p = find_task_by_pid(pid);
1183 error = -ESRCH;
1184 if (p)
1185 error = group_send_sig_info(sig, info, p);
1186 read_unlock(&tasklist_lock);
1187 return error;
1192 * kill_something_info() interprets pid in interesting ways just like kill(2).
1194 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1195 * is probably wrong. Should make it like BSD or SYSV.
1198 static int kill_something_info(int sig, struct siginfo *info, int pid)
1200 if (!pid) {
1201 return kill_pg_info(sig, info, process_group(current));
1202 } else if (pid == -1) {
1203 int retval = 0, count = 0;
1204 struct task_struct * p;
1206 read_lock(&tasklist_lock);
1207 for_each_process(p) {
1208 if (p->pid > 1 && p->tgid != current->tgid) {
1209 int err = group_send_sig_info(sig, info, p);
1210 ++count;
1211 if (err != -EPERM)
1212 retval = err;
1215 read_unlock(&tasklist_lock);
1216 return count ? retval : -ESRCH;
1217 } else if (pid < 0) {
1218 return kill_pg_info(sig, info, -pid);
1219 } else {
1220 return kill_proc_info(sig, info, pid);
1225 * These are for backward compatibility with the rest of the kernel source.
1229 * These two are the most common entry points. They send a signal
1230 * just to the specific thread.
1233 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1235 int ret;
1236 unsigned long flags;
1239 * Make sure legacy kernel users don't send in bad values
1240 * (normal paths check this in check_kill_permission).
1242 if (sig < 0 || sig > _NSIG)
1243 return -EINVAL;
1246 * We need the tasklist lock even for the specific
1247 * thread case (when we don't need to follow the group
1248 * lists) in order to avoid races with "p->sighand"
1249 * going away or changing from under us.
1251 read_lock(&tasklist_lock);
1252 spin_lock_irqsave(&p->sighand->siglock, flags);
1253 ret = specific_send_sig_info(sig, info, p);
1254 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1255 read_unlock(&tasklist_lock);
1256 return ret;
1260 send_sig(int sig, struct task_struct *p, int priv)
1262 return send_sig_info(sig, (void*)(long)(priv != 0), p);
1266 * This is the entry point for "process-wide" signals.
1267 * They will go to an appropriate thread in the thread group.
1270 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1272 int ret;
1273 read_lock(&tasklist_lock);
1274 ret = group_send_sig_info(sig, info, p);
1275 read_unlock(&tasklist_lock);
1276 return ret;
1279 void
1280 force_sig(int sig, struct task_struct *p)
1282 force_sig_info(sig, (void*)1L, p);
1286 * When things go south during signal handling, we
1287 * will force a SIGSEGV. And if the signal that caused
1288 * the problem was already a SIGSEGV, we'll want to
1289 * make sure we don't even try to deliver the signal..
1292 force_sigsegv(int sig, struct task_struct *p)
1294 if (sig == SIGSEGV) {
1295 unsigned long flags;
1296 spin_lock_irqsave(&p->sighand->siglock, flags);
1297 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1298 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1300 force_sig(SIGSEGV, p);
1301 return 0;
1305 kill_pg(pid_t pgrp, int sig, int priv)
1307 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1311 kill_sl(pid_t sess, int sig, int priv)
1313 return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
1317 kill_proc(pid_t pid, int sig, int priv)
1319 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1323 * These functions support sending signals using preallocated sigqueue
1324 * structures. This is needed "because realtime applications cannot
1325 * afford to lose notifications of asynchronous events, like timer
1326 * expirations or I/O completions". In the case of Posix Timers
1327 * we allocate the sigqueue structure from the timer_create. If this
1328 * allocation fails we are able to report the failure to the application
1329 * with an EAGAIN error.
1332 struct sigqueue *sigqueue_alloc(void)
1334 struct sigqueue *q;
1336 if ((q = __sigqueue_alloc()))
1337 q->flags |= SIGQUEUE_PREALLOC;
1338 return(q);
1341 void sigqueue_free(struct sigqueue *q)
1343 unsigned long flags;
1344 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1346 * If the signal is still pending remove it from the
1347 * pending queue.
1349 if (unlikely(!list_empty(&q->list))) {
1350 read_lock(&tasklist_lock);
1351 spin_lock_irqsave(q->lock, flags);
1352 if (!list_empty(&q->list))
1353 list_del_init(&q->list);
1354 spin_unlock_irqrestore(q->lock, flags);
1355 read_unlock(&tasklist_lock);
1357 q->flags &= ~SIGQUEUE_PREALLOC;
1358 __sigqueue_free(q);
1362 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1364 unsigned long flags;
1365 int ret = 0;
1368 * We need the tasklist lock even for the specific
1369 * thread case (when we don't need to follow the group
1370 * lists) in order to avoid races with "p->sighand"
1371 * going away or changing from under us.
1373 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1374 read_lock(&tasklist_lock);
1375 spin_lock_irqsave(&p->sighand->siglock, flags);
1377 if (unlikely(!list_empty(&q->list))) {
1379 * If an SI_TIMER entry is already queue just increment
1380 * the overrun count.
1382 if (q->info.si_code != SI_TIMER)
1383 BUG();
1384 q->info.si_overrun++;
1385 goto out;
1387 /* Short-circuit ignored signals. */
1388 if (sig_ignored(p, sig)) {
1389 ret = 1;
1390 goto out;
1393 q->lock = &p->sighand->siglock;
1394 list_add_tail(&q->list, &p->pending.list);
1395 sigaddset(&p->pending.signal, sig);
1396 if (!sigismember(&p->blocked, sig))
1397 signal_wake_up(p, sig == SIGKILL);
1399 out:
1400 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1401 read_unlock(&tasklist_lock);
1402 return(ret);
1406 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1408 unsigned long flags;
1409 int ret = 0;
1411 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1412 read_lock(&tasklist_lock);
1413 spin_lock_irqsave(&p->sighand->siglock, flags);
1414 handle_stop_signal(sig, p);
1416 /* Short-circuit ignored signals. */
1417 if (sig_ignored(p, sig)) {
1418 ret = 1;
1419 goto out;
1422 if (unlikely(!list_empty(&q->list))) {
1424 * If an SI_TIMER entry is already queue just increment
1425 * the overrun count. Other uses should not try to
1426 * send the signal multiple times.
1428 if (q->info.si_code != SI_TIMER)
1429 BUG();
1430 q->info.si_overrun++;
1431 goto out;
1435 * Put this signal on the shared-pending queue.
1436 * We always use the shared queue for process-wide signals,
1437 * to avoid several races.
1439 q->lock = &p->sighand->siglock;
1440 list_add_tail(&q->list, &p->signal->shared_pending.list);
1441 sigaddset(&p->signal->shared_pending.signal, sig);
1443 __group_complete_signal(sig, p);
1444 out:
1445 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1446 read_unlock(&tasklist_lock);
1447 return(ret);
1451 * Joy. Or not. Pthread wants us to wake up every thread
1452 * in our parent group.
1454 static void __wake_up_parent(struct task_struct *p,
1455 struct task_struct *parent)
1457 struct task_struct *tsk = parent;
1460 * Fortunately this is not necessary for thread groups:
1462 if (p->tgid == tsk->tgid) {
1463 wake_up_interruptible_sync(&tsk->wait_chldexit);
1464 return;
1467 do {
1468 wake_up_interruptible_sync(&tsk->wait_chldexit);
1469 tsk = next_thread(tsk);
1470 if (tsk->signal != parent->signal)
1471 BUG();
1472 } while (tsk != parent);
1476 * Let a parent know about the death of a child.
1477 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1480 void do_notify_parent(struct task_struct *tsk, int sig)
1482 struct siginfo info;
1483 unsigned long flags;
1484 struct sighand_struct *psig;
1486 if (sig == -1)
1487 BUG();
1489 /* do_notify_parent_cldstop should have been called instead. */
1490 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1492 BUG_ON(!tsk->ptrace &&
1493 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1495 info.si_signo = sig;
1496 info.si_errno = 0;
1497 info.si_pid = tsk->pid;
1498 info.si_uid = tsk->uid;
1500 /* FIXME: find out whether or not this is supposed to be c*time. */
1501 info.si_utime = tsk->utime + tsk->signal->utime;
1502 info.si_stime = tsk->stime + tsk->signal->stime;
1504 info.si_status = tsk->exit_code & 0x7f;
1505 if (tsk->exit_code & 0x80)
1506 info.si_code = CLD_DUMPED;
1507 else if (tsk->exit_code & 0x7f)
1508 info.si_code = CLD_KILLED;
1509 else {
1510 info.si_code = CLD_EXITED;
1511 info.si_status = tsk->exit_code >> 8;
1514 psig = tsk->parent->sighand;
1515 spin_lock_irqsave(&psig->siglock, flags);
1516 if (sig == SIGCHLD &&
1517 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1518 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1520 * We are exiting and our parent doesn't care. POSIX.1
1521 * defines special semantics for setting SIGCHLD to SIG_IGN
1522 * or setting the SA_NOCLDWAIT flag: we should be reaped
1523 * automatically and not left for our parent's wait4 call.
1524 * Rather than having the parent do it as a magic kind of
1525 * signal handler, we just set this to tell do_exit that we
1526 * can be cleaned up without becoming a zombie. Note that
1527 * we still call __wake_up_parent in this case, because a
1528 * blocked sys_wait4 might now return -ECHILD.
1530 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1531 * is implementation-defined: we do (if you don't want
1532 * it, just use SIG_IGN instead).
1534 tsk->exit_signal = -1;
1535 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1536 sig = 0;
1538 if (sig > 0 && sig <= _NSIG)
1539 __group_send_sig_info(sig, &info, tsk->parent);
1540 __wake_up_parent(tsk, tsk->parent);
1541 spin_unlock_irqrestore(&psig->siglock, flags);
1544 static void
1545 do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent,
1546 int why)
1548 struct siginfo info;
1549 unsigned long flags;
1550 struct sighand_struct *sighand;
1552 info.si_signo = SIGCHLD;
1553 info.si_errno = 0;
1554 info.si_pid = tsk->pid;
1555 info.si_uid = tsk->uid;
1557 /* FIXME: find out whether or not this is supposed to be c*time. */
1558 info.si_utime = tsk->utime;
1559 info.si_stime = tsk->stime;
1561 info.si_code = why;
1562 switch (why) {
1563 case CLD_CONTINUED:
1564 info.si_status = SIGCONT;
1565 break;
1566 case CLD_STOPPED:
1567 info.si_status = tsk->signal->group_exit_code & 0x7f;
1568 break;
1569 case CLD_TRAPPED:
1570 info.si_status = tsk->exit_code & 0x7f;
1571 break;
1572 default:
1573 BUG();
1576 sighand = parent->sighand;
1577 spin_lock_irqsave(&sighand->siglock, flags);
1578 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1579 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1580 __group_send_sig_info(SIGCHLD, &info, parent);
1582 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1584 __wake_up_parent(tsk, parent);
1585 spin_unlock_irqrestore(&sighand->siglock, flags);
1589 * This must be called with current->sighand->siglock held.
1591 * This should be the path for all ptrace stops.
1592 * We always set current->last_siginfo while stopped here.
1593 * That makes it a way to test a stopped process for
1594 * being ptrace-stopped vs being job-control-stopped.
1596 static void ptrace_stop(int exit_code, siginfo_t *info)
1598 BUG_ON(!(current->ptrace & PT_PTRACED));
1601 * If there is a group stop in progress,
1602 * we must participate in the bookkeeping.
1604 if (current->signal->group_stop_count > 0)
1605 --current->signal->group_stop_count;
1607 current->last_siginfo = info;
1608 current->exit_code = exit_code;
1610 /* Let the debugger run. */
1611 set_current_state(TASK_TRACED);
1612 spin_unlock_irq(&current->sighand->siglock);
1613 read_lock(&tasklist_lock);
1614 do_notify_parent_cldstop(current, current->parent, CLD_TRAPPED);
1615 read_unlock(&tasklist_lock);
1616 schedule();
1619 * We are back. Now reacquire the siglock before touching
1620 * last_siginfo, so that we are sure to have synchronized with
1621 * any signal-sending on another CPU that wants to examine it.
1623 spin_lock_irq(&current->sighand->siglock);
1624 current->last_siginfo = NULL;
1627 * Queued signals ignored us while we were stopped for tracing.
1628 * So check for any that we should take before resuming user mode.
1630 recalc_sigpending();
1633 void ptrace_notify(int exit_code)
1635 siginfo_t info;
1637 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1639 memset(&info, 0, sizeof info);
1640 info.si_signo = SIGTRAP;
1641 info.si_code = exit_code;
1642 info.si_pid = current->pid;
1643 info.si_uid = current->uid;
1645 /* Let the debugger run. */
1646 spin_lock_irq(&current->sighand->siglock);
1647 ptrace_stop(exit_code, &info);
1648 spin_unlock_irq(&current->sighand->siglock);
1651 #ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1653 static void
1654 finish_stop(int stop_count)
1657 * If there are no other threads in the group, or if there is
1658 * a group stop in progress and we are the last to stop,
1659 * report to the parent. When ptraced, every thread reports itself.
1661 if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1662 read_lock(&tasklist_lock);
1663 do_notify_parent_cldstop(current, current->parent,
1664 CLD_STOPPED);
1665 read_unlock(&tasklist_lock);
1667 else if (stop_count == 0) {
1668 read_lock(&tasklist_lock);
1669 do_notify_parent_cldstop(current->group_leader,
1670 current->group_leader->real_parent,
1671 CLD_STOPPED);
1672 read_unlock(&tasklist_lock);
1675 schedule();
1677 * Now we don't run again until continued.
1679 current->exit_code = 0;
1683 * This performs the stopping for SIGSTOP and other stop signals.
1684 * We have to stop all threads in the thread group.
1686 static void
1687 do_signal_stop(int signr)
1689 struct signal_struct *sig = current->signal;
1690 struct sighand_struct *sighand = current->sighand;
1691 int stop_count = -1;
1693 /* spin_lock_irq(&sighand->siglock) is now done in caller */
1695 if (sig->group_stop_count > 0) {
1697 * There is a group stop in progress. We don't need to
1698 * start another one.
1700 signr = sig->group_exit_code;
1701 stop_count = --sig->group_stop_count;
1702 current->exit_code = signr;
1703 set_current_state(TASK_STOPPED);
1704 if (stop_count == 0)
1705 sig->stop_state = 1;
1706 spin_unlock_irq(&sighand->siglock);
1708 else if (thread_group_empty(current)) {
1710 * Lock must be held through transition to stopped state.
1712 current->exit_code = current->signal->group_exit_code = signr;
1713 set_current_state(TASK_STOPPED);
1714 sig->stop_state = 1;
1715 spin_unlock_irq(&sighand->siglock);
1717 else {
1719 * There is no group stop already in progress.
1720 * We must initiate one now, but that requires
1721 * dropping siglock to get both the tasklist lock
1722 * and siglock again in the proper order. Note that
1723 * this allows an intervening SIGCONT to be posted.
1724 * We need to check for that and bail out if necessary.
1726 struct task_struct *t;
1728 spin_unlock_irq(&sighand->siglock);
1730 /* signals can be posted during this window */
1732 read_lock(&tasklist_lock);
1733 spin_lock_irq(&sighand->siglock);
1735 if (unlikely(sig->group_exit)) {
1737 * There is a group exit in progress now.
1738 * We'll just ignore the stop and process the
1739 * associated fatal signal.
1741 spin_unlock_irq(&sighand->siglock);
1742 read_unlock(&tasklist_lock);
1743 return;
1746 if (unlikely(sig_avoid_stop_race())) {
1748 * Either a SIGCONT or a SIGKILL signal was
1749 * posted in the siglock-not-held window.
1751 spin_unlock_irq(&sighand->siglock);
1752 read_unlock(&tasklist_lock);
1753 return;
1756 if (sig->group_stop_count == 0) {
1757 sig->group_exit_code = signr;
1758 stop_count = 0;
1759 for (t = next_thread(current); t != current;
1760 t = next_thread(t))
1762 * Setting state to TASK_STOPPED for a group
1763 * stop is always done with the siglock held,
1764 * so this check has no races.
1766 if (t->state < TASK_STOPPED) {
1767 stop_count++;
1768 signal_wake_up(t, 0);
1770 sig->group_stop_count = stop_count;
1772 else {
1773 /* A race with another thread while unlocked. */
1774 signr = sig->group_exit_code;
1775 stop_count = --sig->group_stop_count;
1778 current->exit_code = signr;
1779 set_current_state(TASK_STOPPED);
1780 if (stop_count == 0)
1781 sig->stop_state = 1;
1783 spin_unlock_irq(&sighand->siglock);
1784 read_unlock(&tasklist_lock);
1787 finish_stop(stop_count);
1791 * Do appropriate magic when group_stop_count > 0.
1792 * We return nonzero if we stopped, after releasing the siglock.
1793 * We return zero if we still hold the siglock and should look
1794 * for another signal without checking group_stop_count again.
1796 static inline int handle_group_stop(void)
1798 int stop_count;
1800 if (current->signal->group_exit_task == current) {
1802 * Group stop is so we can do a core dump,
1803 * We are the initiating thread, so get on with it.
1805 current->signal->group_exit_task = NULL;
1806 return 0;
1809 if (current->signal->group_exit)
1811 * Group stop is so another thread can do a core dump,
1812 * or else we are racing against a death signal.
1813 * Just punt the stop so we can get the next signal.
1815 return 0;
1818 * There is a group stop in progress. We stop
1819 * without any associated signal being in our queue.
1821 stop_count = --current->signal->group_stop_count;
1822 if (stop_count == 0)
1823 current->signal->stop_state = 1;
1824 current->exit_code = current->signal->group_exit_code;
1825 set_current_state(TASK_STOPPED);
1826 spin_unlock_irq(&current->sighand->siglock);
1827 finish_stop(stop_count);
1828 return 1;
1831 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1832 struct pt_regs *regs, void *cookie)
1834 sigset_t *mask = &current->blocked;
1835 int signr = 0;
1837 relock:
1838 spin_lock_irq(&current->sighand->siglock);
1839 for (;;) {
1840 struct k_sigaction *ka;
1842 if (unlikely(current->signal->group_stop_count > 0) &&
1843 handle_group_stop())
1844 goto relock;
1846 signr = dequeue_signal(current, mask, info);
1848 if (!signr)
1849 break; /* will return 0 */
1851 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1852 ptrace_signal_deliver(regs, cookie);
1854 /* Let the debugger run. */
1855 ptrace_stop(signr, info);
1857 /* We're back. Did the debugger cancel the sig? */
1858 signr = current->exit_code;
1859 if (signr == 0)
1860 continue;
1862 current->exit_code = 0;
1864 /* Update the siginfo structure if the signal has
1865 changed. If the debugger wanted something
1866 specific in the siginfo structure then it should
1867 have updated *info via PTRACE_SETSIGINFO. */
1868 if (signr != info->si_signo) {
1869 info->si_signo = signr;
1870 info->si_errno = 0;
1871 info->si_code = SI_USER;
1872 info->si_pid = current->parent->pid;
1873 info->si_uid = current->parent->uid;
1876 /* If the (new) signal is now blocked, requeue it. */
1877 if (sigismember(&current->blocked, signr)) {
1878 specific_send_sig_info(signr, info, current);
1879 continue;
1883 ka = &current->sighand->action[signr-1];
1884 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1885 continue;
1886 if (ka->sa.sa_handler != SIG_DFL) {
1887 /* Run the handler. */
1888 *return_ka = *ka;
1890 if (ka->sa.sa_flags & SA_ONESHOT)
1891 ka->sa.sa_handler = SIG_DFL;
1893 break; /* will return non-zero "signr" value */
1897 * Now we are doing the default action for this signal.
1899 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1900 continue;
1902 /* Init gets no signals it doesn't want. */
1903 if (current->pid == 1)
1904 continue;
1906 if (sig_kernel_stop(signr)) {
1908 * The default action is to stop all threads in
1909 * the thread group. The job control signals
1910 * do nothing in an orphaned pgrp, but SIGSTOP
1911 * always works. Note that siglock needs to be
1912 * dropped during the call to is_orphaned_pgrp()
1913 * because of lock ordering with tasklist_lock.
1914 * This allows an intervening SIGCONT to be posted.
1915 * We need to check for that and bail out if necessary.
1917 if (signr == SIGSTOP) {
1918 do_signal_stop(signr); /* releases siglock */
1919 goto relock;
1921 spin_unlock_irq(&current->sighand->siglock);
1923 /* signals can be posted during this window */
1925 if (is_orphaned_pgrp(process_group(current)))
1926 goto relock;
1928 spin_lock_irq(&current->sighand->siglock);
1929 if (unlikely(sig_avoid_stop_race())) {
1931 * Either a SIGCONT or a SIGKILL signal was
1932 * posted in the siglock-not-held window.
1934 continue;
1937 do_signal_stop(signr); /* releases siglock */
1938 goto relock;
1941 spin_unlock_irq(&current->sighand->siglock);
1944 * Anything else is fatal, maybe with a core dump.
1946 current->flags |= PF_SIGNALED;
1947 if (sig_kernel_coredump(signr) &&
1948 do_coredump((long)signr, signr, regs)) {
1950 * That killed all other threads in the group and
1951 * synchronized with their demise, so there can't
1952 * be any more left to kill now. The group_exit
1953 * flags are set by do_coredump. Note that
1954 * thread_group_empty won't always be true yet,
1955 * because those threads were blocked in __exit_mm
1956 * and we just let them go to finish dying.
1958 const int code = signr | 0x80;
1959 BUG_ON(!current->signal->group_exit);
1960 BUG_ON(current->signal->group_exit_code != code);
1961 do_exit(code);
1962 /* NOTREACHED */
1966 * Death signals, no core dump.
1968 do_group_exit(signr);
1969 /* NOTREACHED */
1971 spin_unlock_irq(&current->sighand->siglock);
1972 return signr;
1975 #endif
1977 EXPORT_SYMBOL(recalc_sigpending);
1978 EXPORT_SYMBOL_GPL(dequeue_signal);
1979 EXPORT_SYMBOL(flush_signals);
1980 EXPORT_SYMBOL(force_sig);
1981 EXPORT_SYMBOL(force_sig_info);
1982 EXPORT_SYMBOL(kill_pg);
1983 EXPORT_SYMBOL(kill_pg_info);
1984 EXPORT_SYMBOL(kill_proc);
1985 EXPORT_SYMBOL(kill_proc_info);
1986 EXPORT_SYMBOL(kill_sl);
1987 EXPORT_SYMBOL(kill_sl_info);
1988 EXPORT_SYMBOL(ptrace_notify);
1989 EXPORT_SYMBOL(send_sig);
1990 EXPORT_SYMBOL(send_sig_info);
1991 EXPORT_SYMBOL(send_group_sig_info);
1992 EXPORT_SYMBOL(sigqueue_alloc);
1993 EXPORT_SYMBOL(sigqueue_free);
1994 EXPORT_SYMBOL(send_sigqueue);
1995 EXPORT_SYMBOL(send_group_sigqueue);
1996 EXPORT_SYMBOL(sigprocmask);
1997 EXPORT_SYMBOL(block_all_signals);
1998 EXPORT_SYMBOL(unblock_all_signals);
2002 * System call entry points.
2005 asmlinkage long sys_restart_syscall(void)
2007 struct restart_block *restart = &current_thread_info()->restart_block;
2008 return restart->fn(restart);
2011 long do_no_restart_syscall(struct restart_block *param)
2013 return -EINTR;
2017 * We don't need to get the kernel lock - this is all local to this
2018 * particular thread.. (and that's good, because this is _heavily_
2019 * used by various programs)
2023 * This is also useful for kernel threads that want to temporarily
2024 * (or permanently) block certain signals.
2026 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2027 * interface happily blocks "unblockable" signals like SIGKILL
2028 * and friends.
2030 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2032 int error;
2033 sigset_t old_block;
2035 spin_lock_irq(&current->sighand->siglock);
2036 old_block = current->blocked;
2037 error = 0;
2038 switch (how) {
2039 case SIG_BLOCK:
2040 sigorsets(&current->blocked, &current->blocked, set);
2041 break;
2042 case SIG_UNBLOCK:
2043 signandsets(&current->blocked, &current->blocked, set);
2044 break;
2045 case SIG_SETMASK:
2046 current->blocked = *set;
2047 break;
2048 default:
2049 error = -EINVAL;
2051 recalc_sigpending();
2052 spin_unlock_irq(&current->sighand->siglock);
2053 if (oldset)
2054 *oldset = old_block;
2055 return error;
2058 asmlinkage long
2059 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2061 int error = -EINVAL;
2062 sigset_t old_set, new_set;
2064 /* XXX: Don't preclude handling different sized sigset_t's. */
2065 if (sigsetsize != sizeof(sigset_t))
2066 goto out;
2068 if (set) {
2069 error = -EFAULT;
2070 if (copy_from_user(&new_set, set, sizeof(*set)))
2071 goto out;
2072 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2074 error = sigprocmask(how, &new_set, &old_set);
2075 if (error)
2076 goto out;
2077 if (oset)
2078 goto set_old;
2079 } else if (oset) {
2080 spin_lock_irq(&current->sighand->siglock);
2081 old_set = current->blocked;
2082 spin_unlock_irq(&current->sighand->siglock);
2084 set_old:
2085 error = -EFAULT;
2086 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2087 goto out;
2089 error = 0;
2090 out:
2091 return error;
2094 long do_sigpending(void __user *set, unsigned long sigsetsize)
2096 long error = -EINVAL;
2097 sigset_t pending;
2099 if (sigsetsize > sizeof(sigset_t))
2100 goto out;
2102 spin_lock_irq(&current->sighand->siglock);
2103 sigorsets(&pending, &current->pending.signal,
2104 &current->signal->shared_pending.signal);
2105 spin_unlock_irq(&current->sighand->siglock);
2107 /* Outside the lock because only this thread touches it. */
2108 sigandsets(&pending, &current->blocked, &pending);
2110 error = -EFAULT;
2111 if (!copy_to_user(set, &pending, sigsetsize))
2112 error = 0;
2114 out:
2115 return error;
2118 asmlinkage long
2119 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2121 return do_sigpending(set, sigsetsize);
2124 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2126 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2128 int err;
2130 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2131 return -EFAULT;
2132 if (from->si_code < 0)
2133 return __copy_to_user(to, from, sizeof(siginfo_t))
2134 ? -EFAULT : 0;
2136 * If you change siginfo_t structure, please be sure
2137 * this code is fixed accordingly.
2138 * It should never copy any pad contained in the structure
2139 * to avoid security leaks, but must copy the generic
2140 * 3 ints plus the relevant union member.
2142 err = __put_user(from->si_signo, &to->si_signo);
2143 err |= __put_user(from->si_errno, &to->si_errno);
2144 err |= __put_user((short)from->si_code, &to->si_code);
2145 switch (from->si_code & __SI_MASK) {
2146 case __SI_KILL:
2147 err |= __put_user(from->si_pid, &to->si_pid);
2148 err |= __put_user(from->si_uid, &to->si_uid);
2149 break;
2150 case __SI_TIMER:
2151 err |= __put_user(from->si_tid, &to->si_tid);
2152 err |= __put_user(from->si_overrun, &to->si_overrun);
2153 err |= __put_user(from->si_ptr, &to->si_ptr);
2154 break;
2155 case __SI_POLL:
2156 err |= __put_user(from->si_band, &to->si_band);
2157 err |= __put_user(from->si_fd, &to->si_fd);
2158 break;
2159 case __SI_FAULT:
2160 err |= __put_user(from->si_addr, &to->si_addr);
2161 #ifdef __ARCH_SI_TRAPNO
2162 err |= __put_user(from->si_trapno, &to->si_trapno);
2163 #endif
2164 break;
2165 case __SI_CHLD:
2166 err |= __put_user(from->si_pid, &to->si_pid);
2167 err |= __put_user(from->si_uid, &to->si_uid);
2168 err |= __put_user(from->si_status, &to->si_status);
2169 err |= __put_user(from->si_utime, &to->si_utime);
2170 err |= __put_user(from->si_stime, &to->si_stime);
2171 break;
2172 case __SI_RT: /* This is not generated by the kernel as of now. */
2173 case __SI_MESGQ: /* But this is */
2174 err |= __put_user(from->si_pid, &to->si_pid);
2175 err |= __put_user(from->si_uid, &to->si_uid);
2176 err |= __put_user(from->si_ptr, &to->si_ptr);
2177 break;
2178 default: /* this is just in case for now ... */
2179 err |= __put_user(from->si_pid, &to->si_pid);
2180 err |= __put_user(from->si_uid, &to->si_uid);
2181 break;
2183 return err;
2186 #endif
2188 asmlinkage long
2189 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2190 siginfo_t __user *uinfo,
2191 const struct timespec __user *uts,
2192 size_t sigsetsize)
2194 int ret, sig;
2195 sigset_t these;
2196 struct timespec ts;
2197 siginfo_t info;
2198 long timeout = 0;
2200 /* XXX: Don't preclude handling different sized sigset_t's. */
2201 if (sigsetsize != sizeof(sigset_t))
2202 return -EINVAL;
2204 if (copy_from_user(&these, uthese, sizeof(these)))
2205 return -EFAULT;
2208 * Invert the set of allowed signals to get those we
2209 * want to block.
2211 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2212 signotset(&these);
2214 if (uts) {
2215 if (copy_from_user(&ts, uts, sizeof(ts)))
2216 return -EFAULT;
2217 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2218 || ts.tv_sec < 0)
2219 return -EINVAL;
2222 spin_lock_irq(&current->sighand->siglock);
2223 sig = dequeue_signal(current, &these, &info);
2224 if (!sig) {
2225 timeout = MAX_SCHEDULE_TIMEOUT;
2226 if (uts)
2227 timeout = (timespec_to_jiffies(&ts)
2228 + (ts.tv_sec || ts.tv_nsec));
2230 if (timeout) {
2231 /* None ready -- temporarily unblock those we're
2232 * interested while we are sleeping in so that we'll
2233 * be awakened when they arrive. */
2234 current->real_blocked = current->blocked;
2235 sigandsets(&current->blocked, &current->blocked, &these);
2236 recalc_sigpending();
2237 spin_unlock_irq(&current->sighand->siglock);
2239 current->state = TASK_INTERRUPTIBLE;
2240 timeout = schedule_timeout(timeout);
2242 spin_lock_irq(&current->sighand->siglock);
2243 sig = dequeue_signal(current, &these, &info);
2244 current->blocked = current->real_blocked;
2245 siginitset(&current->real_blocked, 0);
2246 recalc_sigpending();
2249 spin_unlock_irq(&current->sighand->siglock);
2251 if (sig) {
2252 ret = sig;
2253 if (uinfo) {
2254 if (copy_siginfo_to_user(uinfo, &info))
2255 ret = -EFAULT;
2257 } else {
2258 ret = -EAGAIN;
2259 if (timeout)
2260 ret = -EINTR;
2263 return ret;
2266 asmlinkage long
2267 sys_kill(int pid, int sig)
2269 struct siginfo info;
2271 info.si_signo = sig;
2272 info.si_errno = 0;
2273 info.si_code = SI_USER;
2274 info.si_pid = current->tgid;
2275 info.si_uid = current->uid;
2277 return kill_something_info(sig, &info, pid);
2281 * sys_tgkill - send signal to one specific thread
2282 * @tgid: the thread group ID of the thread
2283 * @pid: the PID of the thread
2284 * @sig: signal to be sent
2286 * This syscall also checks the tgid and returns -ESRCH even if the PID
2287 * exists but it's not belonging to the target process anymore. This
2288 * method solves the problem of threads exiting and PIDs getting reused.
2290 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2292 struct siginfo info;
2293 int error;
2294 struct task_struct *p;
2296 /* This is only valid for single tasks */
2297 if (pid <= 0 || tgid <= 0)
2298 return -EINVAL;
2300 info.si_signo = sig;
2301 info.si_errno = 0;
2302 info.si_code = SI_TKILL;
2303 info.si_pid = current->tgid;
2304 info.si_uid = current->uid;
2306 read_lock(&tasklist_lock);
2307 p = find_task_by_pid(pid);
2308 error = -ESRCH;
2309 if (p && (p->tgid == tgid)) {
2310 error = check_kill_permission(sig, &info, p);
2312 * The null signal is a permissions and process existence
2313 * probe. No signal is actually delivered.
2315 if (!error && sig && p->sighand) {
2316 spin_lock_irq(&p->sighand->siglock);
2317 handle_stop_signal(sig, p);
2318 error = specific_send_sig_info(sig, &info, p);
2319 spin_unlock_irq(&p->sighand->siglock);
2322 read_unlock(&tasklist_lock);
2323 return error;
2327 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2329 asmlinkage long
2330 sys_tkill(int pid, int sig)
2332 struct siginfo info;
2333 int error;
2334 struct task_struct *p;
2336 /* This is only valid for single tasks */
2337 if (pid <= 0)
2338 return -EINVAL;
2340 info.si_signo = sig;
2341 info.si_errno = 0;
2342 info.si_code = SI_TKILL;
2343 info.si_pid = current->tgid;
2344 info.si_uid = current->uid;
2346 read_lock(&tasklist_lock);
2347 p = find_task_by_pid(pid);
2348 error = -ESRCH;
2349 if (p) {
2350 error = check_kill_permission(sig, &info, p);
2352 * The null signal is a permissions and process existence
2353 * probe. No signal is actually delivered.
2355 if (!error && sig && p->sighand) {
2356 spin_lock_irq(&p->sighand->siglock);
2357 handle_stop_signal(sig, p);
2358 error = specific_send_sig_info(sig, &info, p);
2359 spin_unlock_irq(&p->sighand->siglock);
2362 read_unlock(&tasklist_lock);
2363 return error;
2366 asmlinkage long
2367 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2369 siginfo_t info;
2371 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2372 return -EFAULT;
2374 /* Not even root can pretend to send signals from the kernel.
2375 Nor can they impersonate a kill(), which adds source info. */
2376 if (info.si_code >= 0)
2377 return -EPERM;
2378 info.si_signo = sig;
2380 /* POSIX.1b doesn't mention process groups. */
2381 return kill_proc_info(sig, &info, pid);
2385 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2387 struct k_sigaction *k;
2389 if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2390 return -EINVAL;
2392 k = &current->sighand->action[sig-1];
2394 spin_lock_irq(&current->sighand->siglock);
2395 if (signal_pending(current)) {
2397 * If there might be a fatal signal pending on multiple
2398 * threads, make sure we take it before changing the action.
2400 spin_unlock_irq(&current->sighand->siglock);
2401 return -ERESTARTNOINTR;
2404 if (oact)
2405 *oact = *k;
2407 if (act) {
2409 * POSIX 3.3.1.3:
2410 * "Setting a signal action to SIG_IGN for a signal that is
2411 * pending shall cause the pending signal to be discarded,
2412 * whether or not it is blocked."
2414 * "Setting a signal action to SIG_DFL for a signal that is
2415 * pending and whose default action is to ignore the signal
2416 * (for example, SIGCHLD), shall cause the pending signal to
2417 * be discarded, whether or not it is blocked"
2419 if (act->sa.sa_handler == SIG_IGN ||
2420 (act->sa.sa_handler == SIG_DFL &&
2421 sig_kernel_ignore(sig))) {
2423 * This is a fairly rare case, so we only take the
2424 * tasklist_lock once we're sure we'll need it.
2425 * Now we must do this little unlock and relock
2426 * dance to maintain the lock hierarchy.
2428 struct task_struct *t = current;
2429 spin_unlock_irq(&t->sighand->siglock);
2430 read_lock(&tasklist_lock);
2431 spin_lock_irq(&t->sighand->siglock);
2432 *k = *act;
2433 sigdelsetmask(&k->sa.sa_mask,
2434 sigmask(SIGKILL) | sigmask(SIGSTOP));
2435 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2436 do {
2437 rm_from_queue(sigmask(sig), &t->pending);
2438 recalc_sigpending_tsk(t);
2439 t = next_thread(t);
2440 } while (t != current);
2441 spin_unlock_irq(&current->sighand->siglock);
2442 read_unlock(&tasklist_lock);
2443 return 0;
2446 *k = *act;
2447 sigdelsetmask(&k->sa.sa_mask,
2448 sigmask(SIGKILL) | sigmask(SIGSTOP));
2451 spin_unlock_irq(&current->sighand->siglock);
2452 return 0;
2455 int
2456 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2458 stack_t oss;
2459 int error;
2461 if (uoss) {
2462 oss.ss_sp = (void __user *) current->sas_ss_sp;
2463 oss.ss_size = current->sas_ss_size;
2464 oss.ss_flags = sas_ss_flags(sp);
2467 if (uss) {
2468 void __user *ss_sp;
2469 size_t ss_size;
2470 int ss_flags;
2472 error = -EFAULT;
2473 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2474 || __get_user(ss_sp, &uss->ss_sp)
2475 || __get_user(ss_flags, &uss->ss_flags)
2476 || __get_user(ss_size, &uss->ss_size))
2477 goto out;
2479 error = -EPERM;
2480 if (on_sig_stack(sp))
2481 goto out;
2483 error = -EINVAL;
2486 * Note - this code used to test ss_flags incorrectly
2487 * old code may have been written using ss_flags==0
2488 * to mean ss_flags==SS_ONSTACK (as this was the only
2489 * way that worked) - this fix preserves that older
2490 * mechanism
2492 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2493 goto out;
2495 if (ss_flags == SS_DISABLE) {
2496 ss_size = 0;
2497 ss_sp = NULL;
2498 } else {
2499 error = -ENOMEM;
2500 if (ss_size < MINSIGSTKSZ)
2501 goto out;
2504 current->sas_ss_sp = (unsigned long) ss_sp;
2505 current->sas_ss_size = ss_size;
2508 if (uoss) {
2509 error = -EFAULT;
2510 if (copy_to_user(uoss, &oss, sizeof(oss)))
2511 goto out;
2514 error = 0;
2515 out:
2516 return error;
2519 #ifdef __ARCH_WANT_SYS_SIGPENDING
2521 asmlinkage long
2522 sys_sigpending(old_sigset_t __user *set)
2524 return do_sigpending(set, sizeof(*set));
2527 #endif
2529 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2530 /* Some platforms have their own version with special arguments others
2531 support only sys_rt_sigprocmask. */
2533 asmlinkage long
2534 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2536 int error;
2537 old_sigset_t old_set, new_set;
2539 if (set) {
2540 error = -EFAULT;
2541 if (copy_from_user(&new_set, set, sizeof(*set)))
2542 goto out;
2543 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2545 spin_lock_irq(&current->sighand->siglock);
2546 old_set = current->blocked.sig[0];
2548 error = 0;
2549 switch (how) {
2550 default:
2551 error = -EINVAL;
2552 break;
2553 case SIG_BLOCK:
2554 sigaddsetmask(&current->blocked, new_set);
2555 break;
2556 case SIG_UNBLOCK:
2557 sigdelsetmask(&current->blocked, new_set);
2558 break;
2559 case SIG_SETMASK:
2560 current->blocked.sig[0] = new_set;
2561 break;
2564 recalc_sigpending();
2565 spin_unlock_irq(&current->sighand->siglock);
2566 if (error)
2567 goto out;
2568 if (oset)
2569 goto set_old;
2570 } else if (oset) {
2571 old_set = current->blocked.sig[0];
2572 set_old:
2573 error = -EFAULT;
2574 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2575 goto out;
2577 error = 0;
2578 out:
2579 return error;
2581 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2583 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2584 asmlinkage long
2585 sys_rt_sigaction(int sig,
2586 const struct sigaction __user *act,
2587 struct sigaction __user *oact,
2588 size_t sigsetsize)
2590 struct k_sigaction new_sa, old_sa;
2591 int ret = -EINVAL;
2593 /* XXX: Don't preclude handling different sized sigset_t's. */
2594 if (sigsetsize != sizeof(sigset_t))
2595 goto out;
2597 if (act) {
2598 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2599 return -EFAULT;
2602 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2604 if (!ret && oact) {
2605 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2606 return -EFAULT;
2608 out:
2609 return ret;
2611 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2613 #ifdef __ARCH_WANT_SYS_SGETMASK
2616 * For backwards compatibility. Functionality superseded by sigprocmask.
2618 asmlinkage long
2619 sys_sgetmask(void)
2621 /* SMP safe */
2622 return current->blocked.sig[0];
2625 asmlinkage long
2626 sys_ssetmask(int newmask)
2628 int old;
2630 spin_lock_irq(&current->sighand->siglock);
2631 old = current->blocked.sig[0];
2633 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2634 sigmask(SIGSTOP)));
2635 recalc_sigpending();
2636 spin_unlock_irq(&current->sighand->siglock);
2638 return old;
2640 #endif /* __ARCH_WANT_SGETMASK */
2642 #ifdef __ARCH_WANT_SYS_SIGNAL
2644 * For backwards compatibility. Functionality superseded by sigaction.
2646 asmlinkage unsigned long
2647 sys_signal(int sig, __sighandler_t handler)
2649 struct k_sigaction new_sa, old_sa;
2650 int ret;
2652 new_sa.sa.sa_handler = handler;
2653 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2655 ret = do_sigaction(sig, &new_sa, &old_sa);
2657 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2659 #endif /* __ARCH_WANT_SYS_SIGNAL */
2661 #ifdef __ARCH_WANT_SYS_PAUSE
2663 asmlinkage long
2664 sys_pause(void)
2666 current->state = TASK_INTERRUPTIBLE;
2667 schedule();
2668 return -ERESTARTNOHAND;
2671 #endif
2673 void __init signals_init(void)
2675 sigqueue_cachep =
2676 kmem_cache_create("sigqueue",
2677 sizeof(struct sigqueue),
2678 __alignof__(struct sigqueue),
2679 SLAB_PANIC, NULL, NULL);