[PATCH] cpuset: better bitmap remap defaults
[linux-2.6/btrfs-unstable.git] / kernel / signal.c
blob9b6fda5e87f1c8104a30d7ac14685d4106bda53b
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/syscalls.h>
24 #include <linux/ptrace.h>
25 #include <linux/posix-timers.h>
26 #include <linux/signal.h>
27 #include <linux/audit.h>
28 #include <asm/param.h>
29 #include <asm/uaccess.h>
30 #include <asm/unistd.h>
31 #include <asm/siginfo.h>
34 * SLAB caches for signal bits.
37 static kmem_cache_t *sigqueue_cachep;
40 * In POSIX a signal is sent either to a specific thread (Linux task)
41 * or to the process as a whole (Linux thread group). How the signal
42 * is sent determines whether it's to one thread or the whole group,
43 * which determines which signal mask(s) are involved in blocking it
44 * from being delivered until later. When the signal is delivered,
45 * either it's caught or ignored by a user handler or it has a default
46 * effect that applies to the whole thread group (POSIX process).
48 * The possible effects an unblocked signal set to SIG_DFL can have are:
49 * ignore - Nothing Happens
50 * terminate - kill the process, i.e. all threads in the group,
51 * similar to exit_group. The group leader (only) reports
52 * WIFSIGNALED status to its parent.
53 * coredump - write a core dump file describing all threads using
54 * the same mm and then kill all those threads
55 * stop - stop all the threads in the group, i.e. TASK_STOPPED state
57 * SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
58 * Other signals when not blocked and set to SIG_DFL behaves as follows.
59 * The job control signals also have other special effects.
61 * +--------------------+------------------+
62 * | POSIX signal | default action |
63 * +--------------------+------------------+
64 * | SIGHUP | terminate |
65 * | SIGINT | terminate |
66 * | SIGQUIT | coredump |
67 * | SIGILL | coredump |
68 * | SIGTRAP | coredump |
69 * | SIGABRT/SIGIOT | coredump |
70 * | SIGBUS | coredump |
71 * | SIGFPE | coredump |
72 * | SIGKILL | terminate(+) |
73 * | SIGUSR1 | terminate |
74 * | SIGSEGV | coredump |
75 * | SIGUSR2 | terminate |
76 * | SIGPIPE | terminate |
77 * | SIGALRM | terminate |
78 * | SIGTERM | terminate |
79 * | SIGCHLD | ignore |
80 * | SIGCONT | ignore(*) |
81 * | SIGSTOP | stop(*)(+) |
82 * | SIGTSTP | stop(*) |
83 * | SIGTTIN | stop(*) |
84 * | SIGTTOU | stop(*) |
85 * | SIGURG | ignore |
86 * | SIGXCPU | coredump |
87 * | SIGXFSZ | coredump |
88 * | SIGVTALRM | terminate |
89 * | SIGPROF | terminate |
90 * | SIGPOLL/SIGIO | terminate |
91 * | SIGSYS/SIGUNUSED | coredump |
92 * | SIGSTKFLT | terminate |
93 * | SIGWINCH | ignore |
94 * | SIGPWR | terminate |
95 * | SIGRTMIN-SIGRTMAX | terminate |
96 * +--------------------+------------------+
97 * | non-POSIX signal | default action |
98 * +--------------------+------------------+
99 * | SIGEMT | coredump |
100 * +--------------------+------------------+
102 * (+) For SIGKILL and SIGSTOP the action is "always", not just "default".
103 * (*) Special job control effects:
104 * When SIGCONT is sent, it resumes the process (all threads in the group)
105 * from TASK_STOPPED state and also clears any pending/queued stop signals
106 * (any of those marked with "stop(*)"). This happens regardless of blocking,
107 * catching, or ignoring SIGCONT. When any stop signal is sent, it clears
108 * any pending/queued SIGCONT signals; this happens regardless of blocking,
109 * catching, or ignored the stop signal, though (except for SIGSTOP) the
110 * default action of stopping the process may happen later or never.
113 #ifdef SIGEMT
114 #define M_SIGEMT M(SIGEMT)
115 #else
116 #define M_SIGEMT 0
117 #endif
119 #if SIGRTMIN > BITS_PER_LONG
120 #define M(sig) (1ULL << ((sig)-1))
121 #else
122 #define M(sig) (1UL << ((sig)-1))
123 #endif
124 #define T(sig, mask) (M(sig) & (mask))
126 #define SIG_KERNEL_ONLY_MASK (\
127 M(SIGKILL) | M(SIGSTOP) )
129 #define SIG_KERNEL_STOP_MASK (\
130 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
132 #define SIG_KERNEL_COREDUMP_MASK (\
133 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
134 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
135 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
137 #define SIG_KERNEL_IGNORE_MASK (\
138 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
140 #define sig_kernel_only(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
142 #define sig_kernel_coredump(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
144 #define sig_kernel_ignore(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
146 #define sig_kernel_stop(sig) \
147 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
149 #define sig_user_defined(t, signr) \
150 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
151 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
153 #define sig_fatal(t, signr) \
154 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
155 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
157 static int sig_ignored(struct task_struct *t, int sig)
159 void __user * handler;
162 * Tracers always want to know about signals..
164 if (t->ptrace & PT_PTRACED)
165 return 0;
168 * Blocked signals are never ignored, since the
169 * signal handler may change by the time it is
170 * unblocked.
172 if (sigismember(&t->blocked, sig))
173 return 0;
175 /* Is it explicitly or implicitly ignored? */
176 handler = t->sighand->action[sig-1].sa.sa_handler;
177 return handler == SIG_IGN ||
178 (handler == SIG_DFL && sig_kernel_ignore(sig));
182 * Re-calculate pending state from the set of locally pending
183 * signals, globally pending signals, and blocked signals.
185 static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
187 unsigned long ready;
188 long i;
190 switch (_NSIG_WORDS) {
191 default:
192 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
193 ready |= signal->sig[i] &~ blocked->sig[i];
194 break;
196 case 4: ready = signal->sig[3] &~ blocked->sig[3];
197 ready |= signal->sig[2] &~ blocked->sig[2];
198 ready |= signal->sig[1] &~ blocked->sig[1];
199 ready |= signal->sig[0] &~ blocked->sig[0];
200 break;
202 case 2: ready = signal->sig[1] &~ blocked->sig[1];
203 ready |= signal->sig[0] &~ blocked->sig[0];
204 break;
206 case 1: ready = signal->sig[0] &~ blocked->sig[0];
208 return ready != 0;
211 #define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
213 fastcall void recalc_sigpending_tsk(struct task_struct *t)
215 if (t->signal->group_stop_count > 0 ||
216 (freezing(t)) ||
217 PENDING(&t->pending, &t->blocked) ||
218 PENDING(&t->signal->shared_pending, &t->blocked))
219 set_tsk_thread_flag(t, TIF_SIGPENDING);
220 else
221 clear_tsk_thread_flag(t, TIF_SIGPENDING);
224 void recalc_sigpending(void)
226 recalc_sigpending_tsk(current);
229 /* Given the mask, find the first available signal that should be serviced. */
231 static int
232 next_signal(struct sigpending *pending, sigset_t *mask)
234 unsigned long i, *s, *m, x;
235 int sig = 0;
237 s = pending->signal.sig;
238 m = mask->sig;
239 switch (_NSIG_WORDS) {
240 default:
241 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
242 if ((x = *s &~ *m) != 0) {
243 sig = ffz(~x) + i*_NSIG_BPW + 1;
244 break;
246 break;
248 case 2: if ((x = s[0] &~ m[0]) != 0)
249 sig = 1;
250 else if ((x = s[1] &~ m[1]) != 0)
251 sig = _NSIG_BPW + 1;
252 else
253 break;
254 sig += ffz(~x);
255 break;
257 case 1: if ((x = *s &~ *m) != 0)
258 sig = ffz(~x) + 1;
259 break;
262 return sig;
265 static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
266 int override_rlimit)
268 struct sigqueue *q = NULL;
270 atomic_inc(&t->user->sigpending);
271 if (override_rlimit ||
272 atomic_read(&t->user->sigpending) <=
273 t->signal->rlim[RLIMIT_SIGPENDING].rlim_cur)
274 q = kmem_cache_alloc(sigqueue_cachep, flags);
275 if (unlikely(q == NULL)) {
276 atomic_dec(&t->user->sigpending);
277 } else {
278 INIT_LIST_HEAD(&q->list);
279 q->flags = 0;
280 q->user = get_uid(t->user);
282 return(q);
285 static inline void __sigqueue_free(struct sigqueue *q)
287 if (q->flags & SIGQUEUE_PREALLOC)
288 return;
289 atomic_dec(&q->user->sigpending);
290 free_uid(q->user);
291 kmem_cache_free(sigqueue_cachep, q);
294 static void flush_sigqueue(struct sigpending *queue)
296 struct sigqueue *q;
298 sigemptyset(&queue->signal);
299 while (!list_empty(&queue->list)) {
300 q = list_entry(queue->list.next, struct sigqueue , list);
301 list_del_init(&q->list);
302 __sigqueue_free(q);
307 * Flush all pending signals for a task.
310 void
311 flush_signals(struct task_struct *t)
313 unsigned long flags;
315 spin_lock_irqsave(&t->sighand->siglock, flags);
316 clear_tsk_thread_flag(t,TIF_SIGPENDING);
317 flush_sigqueue(&t->pending);
318 flush_sigqueue(&t->signal->shared_pending);
319 spin_unlock_irqrestore(&t->sighand->siglock, flags);
323 * This function expects the tasklist_lock write-locked.
325 void __exit_sighand(struct task_struct *tsk)
327 struct sighand_struct * sighand = tsk->sighand;
329 /* Ok, we're done with the signal handlers */
330 tsk->sighand = NULL;
331 if (atomic_dec_and_test(&sighand->count))
332 sighand_free(sighand);
335 void exit_sighand(struct task_struct *tsk)
337 write_lock_irq(&tasklist_lock);
338 rcu_read_lock();
339 if (tsk->sighand != NULL) {
340 struct sighand_struct *sighand = rcu_dereference(tsk->sighand);
341 spin_lock(&sighand->siglock);
342 __exit_sighand(tsk);
343 spin_unlock(&sighand->siglock);
345 rcu_read_unlock();
346 write_unlock_irq(&tasklist_lock);
350 * This function expects the tasklist_lock write-locked.
352 void __exit_signal(struct task_struct *tsk)
354 struct signal_struct * sig = tsk->signal;
355 struct sighand_struct * sighand;
357 if (!sig)
358 BUG();
359 if (!atomic_read(&sig->count))
360 BUG();
361 rcu_read_lock();
362 sighand = rcu_dereference(tsk->sighand);
363 spin_lock(&sighand->siglock);
364 posix_cpu_timers_exit(tsk);
365 if (atomic_dec_and_test(&sig->count)) {
366 posix_cpu_timers_exit_group(tsk);
367 if (tsk == sig->curr_target)
368 sig->curr_target = next_thread(tsk);
369 tsk->signal = NULL;
370 __exit_sighand(tsk);
371 spin_unlock(&sighand->siglock);
372 flush_sigqueue(&sig->shared_pending);
373 } else {
375 * If there is any task waiting for the group exit
376 * then notify it:
378 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
379 wake_up_process(sig->group_exit_task);
380 sig->group_exit_task = NULL;
382 if (tsk == sig->curr_target)
383 sig->curr_target = next_thread(tsk);
384 tsk->signal = NULL;
386 * Accumulate here the counters for all threads but the
387 * group leader as they die, so they can be added into
388 * the process-wide totals when those are taken.
389 * The group leader stays around as a zombie as long
390 * as there are other threads. When it gets reaped,
391 * the exit.c code will add its counts into these totals.
392 * We won't ever get here for the group leader, since it
393 * will have been the last reference on the signal_struct.
395 sig->utime = cputime_add(sig->utime, tsk->utime);
396 sig->stime = cputime_add(sig->stime, tsk->stime);
397 sig->min_flt += tsk->min_flt;
398 sig->maj_flt += tsk->maj_flt;
399 sig->nvcsw += tsk->nvcsw;
400 sig->nivcsw += tsk->nivcsw;
401 sig->sched_time += tsk->sched_time;
402 __exit_sighand(tsk);
403 spin_unlock(&sighand->siglock);
404 sig = NULL; /* Marker for below. */
406 rcu_read_unlock();
407 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
408 flush_sigqueue(&tsk->pending);
409 if (sig) {
411 * We are cleaning up the signal_struct here.
413 exit_thread_group_keys(sig);
414 kmem_cache_free(signal_cachep, sig);
418 void exit_signal(struct task_struct *tsk)
420 atomic_dec(&tsk->signal->live);
422 write_lock_irq(&tasklist_lock);
423 __exit_signal(tsk);
424 write_unlock_irq(&tasklist_lock);
428 * Flush all handlers for a task.
431 void
432 flush_signal_handlers(struct task_struct *t, int force_default)
434 int i;
435 struct k_sigaction *ka = &t->sighand->action[0];
436 for (i = _NSIG ; i != 0 ; i--) {
437 if (force_default || ka->sa.sa_handler != SIG_IGN)
438 ka->sa.sa_handler = SIG_DFL;
439 ka->sa.sa_flags = 0;
440 sigemptyset(&ka->sa.sa_mask);
441 ka++;
446 /* Notify the system that a driver wants to block all signals for this
447 * process, and wants to be notified if any signals at all were to be
448 * sent/acted upon. If the notifier routine returns non-zero, then the
449 * signal will be acted upon after all. If the notifier routine returns 0,
450 * then then signal will be blocked. Only one block per process is
451 * allowed. priv is a pointer to private data that the notifier routine
452 * can use to determine if the signal should be blocked or not. */
454 void
455 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
457 unsigned long flags;
459 spin_lock_irqsave(&current->sighand->siglock, flags);
460 current->notifier_mask = mask;
461 current->notifier_data = priv;
462 current->notifier = notifier;
463 spin_unlock_irqrestore(&current->sighand->siglock, flags);
466 /* Notify the system that blocking has ended. */
468 void
469 unblock_all_signals(void)
471 unsigned long flags;
473 spin_lock_irqsave(&current->sighand->siglock, flags);
474 current->notifier = NULL;
475 current->notifier_data = NULL;
476 recalc_sigpending();
477 spin_unlock_irqrestore(&current->sighand->siglock, flags);
480 static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
482 struct sigqueue *q, *first = NULL;
483 int still_pending = 0;
485 if (unlikely(!sigismember(&list->signal, sig)))
486 return 0;
489 * Collect the siginfo appropriate to this signal. Check if
490 * there is another siginfo for the same signal.
492 list_for_each_entry(q, &list->list, list) {
493 if (q->info.si_signo == sig) {
494 if (first) {
495 still_pending = 1;
496 break;
498 first = q;
501 if (first) {
502 list_del_init(&first->list);
503 copy_siginfo(info, &first->info);
504 __sigqueue_free(first);
505 if (!still_pending)
506 sigdelset(&list->signal, sig);
507 } else {
509 /* Ok, it wasn't in the queue. This must be
510 a fast-pathed signal or we must have been
511 out of queue space. So zero out the info.
513 sigdelset(&list->signal, sig);
514 info->si_signo = sig;
515 info->si_errno = 0;
516 info->si_code = 0;
517 info->si_pid = 0;
518 info->si_uid = 0;
520 return 1;
523 static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
524 siginfo_t *info)
526 int sig = 0;
528 sig = next_signal(pending, mask);
529 if (sig) {
530 if (current->notifier) {
531 if (sigismember(current->notifier_mask, sig)) {
532 if (!(current->notifier)(current->notifier_data)) {
533 clear_thread_flag(TIF_SIGPENDING);
534 return 0;
539 if (!collect_signal(sig, pending, info))
540 sig = 0;
543 recalc_sigpending();
545 return sig;
549 * Dequeue a signal and return the element to the caller, which is
550 * expected to free it.
552 * All callers have to hold the siglock.
554 int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
556 int signr = __dequeue_signal(&tsk->pending, mask, info);
557 if (!signr)
558 signr = __dequeue_signal(&tsk->signal->shared_pending,
559 mask, info);
560 if (signr && unlikely(sig_kernel_stop(signr))) {
562 * Set a marker that we have dequeued a stop signal. Our
563 * caller might release the siglock and then the pending
564 * stop signal it is about to process is no longer in the
565 * pending bitmasks, but must still be cleared by a SIGCONT
566 * (and overruled by a SIGKILL). So those cases clear this
567 * shared flag after we've set it. Note that this flag may
568 * remain set after the signal we return is ignored or
569 * handled. That doesn't matter because its only purpose
570 * is to alert stop-signal processing code when another
571 * processor has come along and cleared the flag.
573 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
574 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
576 if ( signr &&
577 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
578 info->si_sys_private){
580 * Release the siglock to ensure proper locking order
581 * of timer locks outside of siglocks. Note, we leave
582 * irqs disabled here, since the posix-timers code is
583 * about to disable them again anyway.
585 spin_unlock(&tsk->sighand->siglock);
586 do_schedule_next_timer(info);
587 spin_lock(&tsk->sighand->siglock);
589 return signr;
593 * Tell a process that it has a new active signal..
595 * NOTE! we rely on the previous spin_lock to
596 * lock interrupts for us! We can only be called with
597 * "siglock" held, and the local interrupt must
598 * have been disabled when that got acquired!
600 * No need to set need_resched since signal event passing
601 * goes through ->blocked
603 void signal_wake_up(struct task_struct *t, int resume)
605 unsigned int mask;
607 set_tsk_thread_flag(t, TIF_SIGPENDING);
610 * For SIGKILL, we want to wake it up in the stopped/traced case.
611 * We don't check t->state here because there is a race with it
612 * executing another processor and just now entering stopped state.
613 * By using wake_up_state, we ensure the process will wake up and
614 * handle its death signal.
616 mask = TASK_INTERRUPTIBLE;
617 if (resume)
618 mask |= TASK_STOPPED | TASK_TRACED;
619 if (!wake_up_state(t, mask))
620 kick_process(t);
624 * Remove signals in mask from the pending set and queue.
625 * Returns 1 if any signals were found.
627 * All callers must be holding the siglock.
629 static int rm_from_queue(unsigned long mask, struct sigpending *s)
631 struct sigqueue *q, *n;
633 if (!sigtestsetmask(&s->signal, mask))
634 return 0;
636 sigdelsetmask(&s->signal, mask);
637 list_for_each_entry_safe(q, n, &s->list, list) {
638 if (q->info.si_signo < SIGRTMIN &&
639 (mask & sigmask(q->info.si_signo))) {
640 list_del_init(&q->list);
641 __sigqueue_free(q);
644 return 1;
648 * Bad permissions for sending the signal
650 static int check_kill_permission(int sig, struct siginfo *info,
651 struct task_struct *t)
653 int error = -EINVAL;
654 if (!valid_signal(sig))
655 return error;
656 error = -EPERM;
657 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
658 && ((sig != SIGCONT) ||
659 (current->signal->session != t->signal->session))
660 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
661 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
662 && !capable(CAP_KILL))
663 return error;
665 error = security_task_kill(t, info, sig);
666 if (!error)
667 audit_signal_info(sig, t); /* Let audit system see the signal */
668 return error;
671 /* forward decl */
672 static void do_notify_parent_cldstop(struct task_struct *tsk,
673 int to_self,
674 int why);
677 * Handle magic process-wide effects of stop/continue signals.
678 * Unlike the signal actions, these happen immediately at signal-generation
679 * time regardless of blocking, ignoring, or handling. This does the
680 * actual continuing for SIGCONT, but not the actual stopping for stop
681 * signals. The process stop is done as a signal action for SIG_DFL.
683 static void handle_stop_signal(int sig, struct task_struct *p)
685 struct task_struct *t;
687 if (p->signal->flags & SIGNAL_GROUP_EXIT)
689 * The process is in the middle of dying already.
691 return;
693 if (sig_kernel_stop(sig)) {
695 * This is a stop signal. Remove SIGCONT from all queues.
697 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
698 t = p;
699 do {
700 rm_from_queue(sigmask(SIGCONT), &t->pending);
701 t = next_thread(t);
702 } while (t != p);
703 } else if (sig == SIGCONT) {
705 * Remove all stop signals from all queues,
706 * and wake all threads.
708 if (unlikely(p->signal->group_stop_count > 0)) {
710 * There was a group stop in progress. We'll
711 * pretend it finished before we got here. We are
712 * obliged to report it to the parent: if the
713 * SIGSTOP happened "after" this SIGCONT, then it
714 * would have cleared this pending SIGCONT. If it
715 * happened "before" this SIGCONT, then the parent
716 * got the SIGCHLD about the stop finishing before
717 * the continue happened. We do the notification
718 * now, and it's as if the stop had finished and
719 * the SIGCHLD was pending on entry to this kill.
721 p->signal->group_stop_count = 0;
722 p->signal->flags = SIGNAL_STOP_CONTINUED;
723 spin_unlock(&p->sighand->siglock);
724 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_STOPPED);
725 spin_lock(&p->sighand->siglock);
727 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
728 t = p;
729 do {
730 unsigned int state;
731 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
734 * If there is a handler for SIGCONT, we must make
735 * sure that no thread returns to user mode before
736 * we post the signal, in case it was the only
737 * thread eligible to run the signal handler--then
738 * it must not do anything between resuming and
739 * running the handler. With the TIF_SIGPENDING
740 * flag set, the thread will pause and acquire the
741 * siglock that we hold now and until we've queued
742 * the pending signal.
744 * Wake up the stopped thread _after_ setting
745 * TIF_SIGPENDING
747 state = TASK_STOPPED;
748 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
749 set_tsk_thread_flag(t, TIF_SIGPENDING);
750 state |= TASK_INTERRUPTIBLE;
752 wake_up_state(t, state);
754 t = next_thread(t);
755 } while (t != p);
757 if (p->signal->flags & SIGNAL_STOP_STOPPED) {
759 * We were in fact stopped, and are now continued.
760 * Notify the parent with CLD_CONTINUED.
762 p->signal->flags = SIGNAL_STOP_CONTINUED;
763 p->signal->group_exit_code = 0;
764 spin_unlock(&p->sighand->siglock);
765 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_CONTINUED);
766 spin_lock(&p->sighand->siglock);
767 } else {
769 * We are not stopped, but there could be a stop
770 * signal in the middle of being processed after
771 * being removed from the queue. Clear that too.
773 p->signal->flags = 0;
775 } else if (sig == SIGKILL) {
777 * Make sure that any pending stop signal already dequeued
778 * is undone by the wakeup for SIGKILL.
780 p->signal->flags = 0;
784 static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
785 struct sigpending *signals)
787 struct sigqueue * q = NULL;
788 int ret = 0;
791 * fast-pathed signals for kernel-internal things like SIGSTOP
792 * or SIGKILL.
794 if (info == SEND_SIG_FORCED)
795 goto out_set;
797 /* Real-time signals must be queued if sent by sigqueue, or
798 some other real-time mechanism. It is implementation
799 defined whether kill() does so. We attempt to do so, on
800 the principle of least surprise, but since kill is not
801 allowed to fail with EAGAIN when low on memory we just
802 make sure at least one signal gets delivered and don't
803 pass on the info struct. */
805 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
806 (is_si_special(info) ||
807 info->si_code >= 0)));
808 if (q) {
809 list_add_tail(&q->list, &signals->list);
810 switch ((unsigned long) info) {
811 case (unsigned long) SEND_SIG_NOINFO:
812 q->info.si_signo = sig;
813 q->info.si_errno = 0;
814 q->info.si_code = SI_USER;
815 q->info.si_pid = current->pid;
816 q->info.si_uid = current->uid;
817 break;
818 case (unsigned long) SEND_SIG_PRIV:
819 q->info.si_signo = sig;
820 q->info.si_errno = 0;
821 q->info.si_code = SI_KERNEL;
822 q->info.si_pid = 0;
823 q->info.si_uid = 0;
824 break;
825 default:
826 copy_siginfo(&q->info, info);
827 break;
829 } else if (!is_si_special(info)) {
830 if (sig >= SIGRTMIN && info->si_code != SI_USER)
832 * Queue overflow, abort. We may abort if the signal was rt
833 * and sent by user using something other than kill().
835 return -EAGAIN;
838 out_set:
839 sigaddset(&signals->signal, sig);
840 return ret;
843 #define LEGACY_QUEUE(sigptr, sig) \
844 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
847 static int
848 specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
850 int ret = 0;
852 if (!irqs_disabled())
853 BUG();
854 assert_spin_locked(&t->sighand->siglock);
856 /* Short-circuit ignored signals. */
857 if (sig_ignored(t, sig))
858 goto out;
860 /* Support queueing exactly one non-rt signal, so that we
861 can get more detailed information about the cause of
862 the signal. */
863 if (LEGACY_QUEUE(&t->pending, sig))
864 goto out;
866 ret = send_signal(sig, info, t, &t->pending);
867 if (!ret && !sigismember(&t->blocked, sig))
868 signal_wake_up(t, sig == SIGKILL);
869 out:
870 return ret;
874 * Force a signal that the process can't ignore: if necessary
875 * we unblock the signal and change any SIG_IGN to SIG_DFL.
879 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
881 unsigned long int flags;
882 int ret;
884 spin_lock_irqsave(&t->sighand->siglock, flags);
885 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
886 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
888 if (sigismember(&t->blocked, sig)) {
889 sigdelset(&t->blocked, sig);
891 recalc_sigpending_tsk(t);
892 ret = specific_send_sig_info(sig, info, t);
893 spin_unlock_irqrestore(&t->sighand->siglock, flags);
895 return ret;
898 void
899 force_sig_specific(int sig, struct task_struct *t)
901 force_sig_info(sig, SEND_SIG_FORCED, t);
905 * Test if P wants to take SIG. After we've checked all threads with this,
906 * it's equivalent to finding no threads not blocking SIG. Any threads not
907 * blocking SIG were ruled out because they are not running and already
908 * have pending signals. Such threads will dequeue from the shared queue
909 * as soon as they're available, so putting the signal on the shared queue
910 * will be equivalent to sending it to one such thread.
912 static inline int wants_signal(int sig, struct task_struct *p)
914 if (sigismember(&p->blocked, sig))
915 return 0;
916 if (p->flags & PF_EXITING)
917 return 0;
918 if (sig == SIGKILL)
919 return 1;
920 if (p->state & (TASK_STOPPED | TASK_TRACED))
921 return 0;
922 return task_curr(p) || !signal_pending(p);
925 static void
926 __group_complete_signal(int sig, struct task_struct *p)
928 struct task_struct *t;
931 * Now find a thread we can wake up to take the signal off the queue.
933 * If the main thread wants the signal, it gets first crack.
934 * Probably the least surprising to the average bear.
936 if (wants_signal(sig, p))
937 t = p;
938 else if (thread_group_empty(p))
940 * There is just one thread and it does not need to be woken.
941 * It will dequeue unblocked signals before it runs again.
943 return;
944 else {
946 * Otherwise try to find a suitable thread.
948 t = p->signal->curr_target;
949 if (t == NULL)
950 /* restart balancing at this thread */
951 t = p->signal->curr_target = p;
952 BUG_ON(t->tgid != p->tgid);
954 while (!wants_signal(sig, t)) {
955 t = next_thread(t);
956 if (t == p->signal->curr_target)
958 * No thread needs to be woken.
959 * Any eligible threads will see
960 * the signal in the queue soon.
962 return;
964 p->signal->curr_target = t;
968 * Found a killable thread. If the signal will be fatal,
969 * then start taking the whole group down immediately.
971 if (sig_fatal(p, sig) && !(p->signal->flags & SIGNAL_GROUP_EXIT) &&
972 !sigismember(&t->real_blocked, sig) &&
973 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
975 * This signal will be fatal to the whole group.
977 if (!sig_kernel_coredump(sig)) {
979 * Start a group exit and wake everybody up.
980 * This way we don't have other threads
981 * running and doing things after a slower
982 * thread has the fatal signal pending.
984 p->signal->flags = SIGNAL_GROUP_EXIT;
985 p->signal->group_exit_code = sig;
986 p->signal->group_stop_count = 0;
987 t = p;
988 do {
989 sigaddset(&t->pending.signal, SIGKILL);
990 signal_wake_up(t, 1);
991 t = next_thread(t);
992 } while (t != p);
993 return;
997 * There will be a core dump. We make all threads other
998 * than the chosen one go into a group stop so that nothing
999 * happens until it gets scheduled, takes the signal off
1000 * the shared queue, and does the core dump. This is a
1001 * little more complicated than strictly necessary, but it
1002 * keeps the signal state that winds up in the core dump
1003 * unchanged from the death state, e.g. which thread had
1004 * the core-dump signal unblocked.
1006 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1007 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
1008 p->signal->group_stop_count = 0;
1009 p->signal->group_exit_task = t;
1010 t = p;
1011 do {
1012 p->signal->group_stop_count++;
1013 signal_wake_up(t, 0);
1014 t = next_thread(t);
1015 } while (t != p);
1016 wake_up_process(p->signal->group_exit_task);
1017 return;
1021 * The signal is already in the shared-pending queue.
1022 * Tell the chosen thread to wake up and dequeue it.
1024 signal_wake_up(t, sig == SIGKILL);
1025 return;
1029 __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1031 int ret = 0;
1033 assert_spin_locked(&p->sighand->siglock);
1034 handle_stop_signal(sig, p);
1036 /* Short-circuit ignored signals. */
1037 if (sig_ignored(p, sig))
1038 return ret;
1040 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
1041 /* This is a non-RT signal and we already have one queued. */
1042 return ret;
1045 * Put this signal on the shared-pending queue, or fail with EAGAIN.
1046 * We always use the shared queue for process-wide signals,
1047 * to avoid several races.
1049 ret = send_signal(sig, info, p, &p->signal->shared_pending);
1050 if (unlikely(ret))
1051 return ret;
1053 __group_complete_signal(sig, p);
1054 return 0;
1058 * Nuke all other threads in the group.
1060 void zap_other_threads(struct task_struct *p)
1062 struct task_struct *t;
1064 p->signal->flags = SIGNAL_GROUP_EXIT;
1065 p->signal->group_stop_count = 0;
1067 if (thread_group_empty(p))
1068 return;
1070 for (t = next_thread(p); t != p; t = next_thread(t)) {
1072 * Don't bother with already dead threads
1074 if (t->exit_state)
1075 continue;
1078 * We don't want to notify the parent, since we are
1079 * killed as part of a thread group due to another
1080 * thread doing an execve() or similar. So set the
1081 * exit signal to -1 to allow immediate reaping of
1082 * the process. But don't detach the thread group
1083 * leader.
1085 if (t != p->group_leader)
1086 t->exit_signal = -1;
1088 /* SIGKILL will be handled before any pending SIGSTOP */
1089 sigaddset(&t->pending.signal, SIGKILL);
1090 signal_wake_up(t, 1);
1095 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
1097 int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1099 unsigned long flags;
1100 struct sighand_struct *sp;
1101 int ret;
1103 retry:
1104 ret = check_kill_permission(sig, info, p);
1105 if (!ret && sig && (sp = rcu_dereference(p->sighand))) {
1106 spin_lock_irqsave(&sp->siglock, flags);
1107 if (p->sighand != sp) {
1108 spin_unlock_irqrestore(&sp->siglock, flags);
1109 goto retry;
1111 if ((atomic_read(&sp->count) == 0) ||
1112 (atomic_read(&p->usage) == 0)) {
1113 spin_unlock_irqrestore(&sp->siglock, flags);
1114 return -ESRCH;
1116 ret = __group_send_sig_info(sig, info, p);
1117 spin_unlock_irqrestore(&sp->siglock, flags);
1120 return ret;
1124 * kill_pg_info() sends a signal to a process group: this is what the tty
1125 * control characters do (^C, ^Z etc)
1128 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1130 struct task_struct *p = NULL;
1131 int retval, success;
1133 if (pgrp <= 0)
1134 return -EINVAL;
1136 success = 0;
1137 retval = -ESRCH;
1138 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1139 int err = group_send_sig_info(sig, info, p);
1140 success |= !err;
1141 retval = err;
1142 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1143 return success ? 0 : retval;
1147 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1149 int retval;
1151 read_lock(&tasklist_lock);
1152 retval = __kill_pg_info(sig, info, pgrp);
1153 read_unlock(&tasklist_lock);
1155 return retval;
1159 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1161 int error;
1162 int acquired_tasklist_lock = 0;
1163 struct task_struct *p;
1165 rcu_read_lock();
1166 if (unlikely(sig_kernel_stop(sig) || sig == SIGCONT)) {
1167 read_lock(&tasklist_lock);
1168 acquired_tasklist_lock = 1;
1170 p = find_task_by_pid(pid);
1171 error = -ESRCH;
1172 if (p)
1173 error = group_send_sig_info(sig, info, p);
1174 if (unlikely(acquired_tasklist_lock))
1175 read_unlock(&tasklist_lock);
1176 rcu_read_unlock();
1177 return error;
1180 /* like kill_proc_info(), but doesn't use uid/euid of "current" */
1181 int kill_proc_info_as_uid(int sig, struct siginfo *info, pid_t pid,
1182 uid_t uid, uid_t euid)
1184 int ret = -EINVAL;
1185 struct task_struct *p;
1187 if (!valid_signal(sig))
1188 return ret;
1190 read_lock(&tasklist_lock);
1191 p = find_task_by_pid(pid);
1192 if (!p) {
1193 ret = -ESRCH;
1194 goto out_unlock;
1196 if ((!info || ((unsigned long)info != 1 &&
1197 (unsigned long)info != 2 && SI_FROMUSER(info)))
1198 && (euid != p->suid) && (euid != p->uid)
1199 && (uid != p->suid) && (uid != p->uid)) {
1200 ret = -EPERM;
1201 goto out_unlock;
1203 if (sig && p->sighand) {
1204 unsigned long flags;
1205 spin_lock_irqsave(&p->sighand->siglock, flags);
1206 ret = __group_send_sig_info(sig, info, p);
1207 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1209 out_unlock:
1210 read_unlock(&tasklist_lock);
1211 return ret;
1213 EXPORT_SYMBOL_GPL(kill_proc_info_as_uid);
1216 * kill_something_info() interprets pid in interesting ways just like kill(2).
1218 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1219 * is probably wrong. Should make it like BSD or SYSV.
1222 static int kill_something_info(int sig, struct siginfo *info, int pid)
1224 if (!pid) {
1225 return kill_pg_info(sig, info, process_group(current));
1226 } else if (pid == -1) {
1227 int retval = 0, count = 0;
1228 struct task_struct * p;
1230 read_lock(&tasklist_lock);
1231 for_each_process(p) {
1232 if (p->pid > 1 && p->tgid != current->tgid) {
1233 int err = group_send_sig_info(sig, info, p);
1234 ++count;
1235 if (err != -EPERM)
1236 retval = err;
1239 read_unlock(&tasklist_lock);
1240 return count ? retval : -ESRCH;
1241 } else if (pid < 0) {
1242 return kill_pg_info(sig, info, -pid);
1243 } else {
1244 return kill_proc_info(sig, info, pid);
1249 * These are for backward compatibility with the rest of the kernel source.
1253 * These two are the most common entry points. They send a signal
1254 * just to the specific thread.
1257 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1259 int ret;
1260 unsigned long flags;
1263 * Make sure legacy kernel users don't send in bad values
1264 * (normal paths check this in check_kill_permission).
1266 if (!valid_signal(sig))
1267 return -EINVAL;
1270 * We need the tasklist lock even for the specific
1271 * thread case (when we don't need to follow the group
1272 * lists) in order to avoid races with "p->sighand"
1273 * going away or changing from under us.
1275 read_lock(&tasklist_lock);
1276 spin_lock_irqsave(&p->sighand->siglock, flags);
1277 ret = specific_send_sig_info(sig, info, p);
1278 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1279 read_unlock(&tasklist_lock);
1280 return ret;
1283 #define __si_special(priv) \
1284 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1287 send_sig(int sig, struct task_struct *p, int priv)
1289 return send_sig_info(sig, __si_special(priv), p);
1293 * This is the entry point for "process-wide" signals.
1294 * They will go to an appropriate thread in the thread group.
1297 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1299 int ret;
1300 read_lock(&tasklist_lock);
1301 ret = group_send_sig_info(sig, info, p);
1302 read_unlock(&tasklist_lock);
1303 return ret;
1306 void
1307 force_sig(int sig, struct task_struct *p)
1309 force_sig_info(sig, SEND_SIG_PRIV, p);
1313 * When things go south during signal handling, we
1314 * will force a SIGSEGV. And if the signal that caused
1315 * the problem was already a SIGSEGV, we'll want to
1316 * make sure we don't even try to deliver the signal..
1319 force_sigsegv(int sig, struct task_struct *p)
1321 if (sig == SIGSEGV) {
1322 unsigned long flags;
1323 spin_lock_irqsave(&p->sighand->siglock, flags);
1324 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1325 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1327 force_sig(SIGSEGV, p);
1328 return 0;
1332 kill_pg(pid_t pgrp, int sig, int priv)
1334 return kill_pg_info(sig, __si_special(priv), pgrp);
1338 kill_proc(pid_t pid, int sig, int priv)
1340 return kill_proc_info(sig, __si_special(priv), pid);
1344 * These functions support sending signals using preallocated sigqueue
1345 * structures. This is needed "because realtime applications cannot
1346 * afford to lose notifications of asynchronous events, like timer
1347 * expirations or I/O completions". In the case of Posix Timers
1348 * we allocate the sigqueue structure from the timer_create. If this
1349 * allocation fails we are able to report the failure to the application
1350 * with an EAGAIN error.
1353 struct sigqueue *sigqueue_alloc(void)
1355 struct sigqueue *q;
1357 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1358 q->flags |= SIGQUEUE_PREALLOC;
1359 return(q);
1362 void sigqueue_free(struct sigqueue *q)
1364 unsigned long flags;
1365 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1367 * If the signal is still pending remove it from the
1368 * pending queue.
1370 if (unlikely(!list_empty(&q->list))) {
1371 spinlock_t *lock = &current->sighand->siglock;
1372 read_lock(&tasklist_lock);
1373 spin_lock_irqsave(lock, flags);
1374 if (!list_empty(&q->list))
1375 list_del_init(&q->list);
1376 spin_unlock_irqrestore(lock, flags);
1377 read_unlock(&tasklist_lock);
1379 q->flags &= ~SIGQUEUE_PREALLOC;
1380 __sigqueue_free(q);
1384 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1386 unsigned long flags;
1387 int ret = 0;
1388 struct sighand_struct *sh;
1390 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1393 * The rcu based delayed sighand destroy makes it possible to
1394 * run this without tasklist lock held. The task struct itself
1395 * cannot go away as create_timer did get_task_struct().
1397 * We return -1, when the task is marked exiting, so
1398 * posix_timer_event can redirect it to the group leader
1400 rcu_read_lock();
1402 if (unlikely(p->flags & PF_EXITING)) {
1403 ret = -1;
1404 goto out_err;
1407 retry:
1408 sh = rcu_dereference(p->sighand);
1410 spin_lock_irqsave(&sh->siglock, flags);
1411 if (p->sighand != sh) {
1412 /* We raced with exec() in a multithreaded process... */
1413 spin_unlock_irqrestore(&sh->siglock, flags);
1414 goto retry;
1418 * We do the check here again to handle the following scenario:
1420 * CPU 0 CPU 1
1421 * send_sigqueue
1422 * check PF_EXITING
1423 * interrupt exit code running
1424 * __exit_signal
1425 * lock sighand->siglock
1426 * unlock sighand->siglock
1427 * lock sh->siglock
1428 * add(tsk->pending) flush_sigqueue(tsk->pending)
1432 if (unlikely(p->flags & PF_EXITING)) {
1433 ret = -1;
1434 goto out;
1437 if (unlikely(!list_empty(&q->list))) {
1439 * If an SI_TIMER entry is already queue just increment
1440 * the overrun count.
1442 if (q->info.si_code != SI_TIMER)
1443 BUG();
1444 q->info.si_overrun++;
1445 goto out;
1447 /* Short-circuit ignored signals. */
1448 if (sig_ignored(p, sig)) {
1449 ret = 1;
1450 goto out;
1453 list_add_tail(&q->list, &p->pending.list);
1454 sigaddset(&p->pending.signal, sig);
1455 if (!sigismember(&p->blocked, sig))
1456 signal_wake_up(p, sig == SIGKILL);
1458 out:
1459 spin_unlock_irqrestore(&sh->siglock, flags);
1460 out_err:
1461 rcu_read_unlock();
1463 return ret;
1467 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1469 unsigned long flags;
1470 int ret = 0;
1472 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1474 read_lock(&tasklist_lock);
1475 /* Since it_lock is held, p->sighand cannot be NULL. */
1476 spin_lock_irqsave(&p->sighand->siglock, flags);
1477 handle_stop_signal(sig, p);
1479 /* Short-circuit ignored signals. */
1480 if (sig_ignored(p, sig)) {
1481 ret = 1;
1482 goto out;
1485 if (unlikely(!list_empty(&q->list))) {
1487 * If an SI_TIMER entry is already queue just increment
1488 * the overrun count. Other uses should not try to
1489 * send the signal multiple times.
1491 if (q->info.si_code != SI_TIMER)
1492 BUG();
1493 q->info.si_overrun++;
1494 goto out;
1498 * Put this signal on the shared-pending queue.
1499 * We always use the shared queue for process-wide signals,
1500 * to avoid several races.
1502 list_add_tail(&q->list, &p->signal->shared_pending.list);
1503 sigaddset(&p->signal->shared_pending.signal, sig);
1505 __group_complete_signal(sig, p);
1506 out:
1507 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1508 read_unlock(&tasklist_lock);
1509 return ret;
1513 * Wake up any threads in the parent blocked in wait* syscalls.
1515 static inline void __wake_up_parent(struct task_struct *p,
1516 struct task_struct *parent)
1518 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1522 * Let a parent know about the death of a child.
1523 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1526 void do_notify_parent(struct task_struct *tsk, int sig)
1528 struct siginfo info;
1529 unsigned long flags;
1530 struct sighand_struct *psig;
1532 BUG_ON(sig == -1);
1534 /* do_notify_parent_cldstop should have been called instead. */
1535 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1537 BUG_ON(!tsk->ptrace &&
1538 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1540 info.si_signo = sig;
1541 info.si_errno = 0;
1542 info.si_pid = tsk->pid;
1543 info.si_uid = tsk->uid;
1545 /* FIXME: find out whether or not this is supposed to be c*time. */
1546 info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
1547 tsk->signal->utime));
1548 info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
1549 tsk->signal->stime));
1551 info.si_status = tsk->exit_code & 0x7f;
1552 if (tsk->exit_code & 0x80)
1553 info.si_code = CLD_DUMPED;
1554 else if (tsk->exit_code & 0x7f)
1555 info.si_code = CLD_KILLED;
1556 else {
1557 info.si_code = CLD_EXITED;
1558 info.si_status = tsk->exit_code >> 8;
1561 psig = tsk->parent->sighand;
1562 spin_lock_irqsave(&psig->siglock, flags);
1563 if (!tsk->ptrace && sig == SIGCHLD &&
1564 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1565 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1567 * We are exiting and our parent doesn't care. POSIX.1
1568 * defines special semantics for setting SIGCHLD to SIG_IGN
1569 * or setting the SA_NOCLDWAIT flag: we should be reaped
1570 * automatically and not left for our parent's wait4 call.
1571 * Rather than having the parent do it as a magic kind of
1572 * signal handler, we just set this to tell do_exit that we
1573 * can be cleaned up without becoming a zombie. Note that
1574 * we still call __wake_up_parent in this case, because a
1575 * blocked sys_wait4 might now return -ECHILD.
1577 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1578 * is implementation-defined: we do (if you don't want
1579 * it, just use SIG_IGN instead).
1581 tsk->exit_signal = -1;
1582 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1583 sig = 0;
1585 if (valid_signal(sig) && sig > 0)
1586 __group_send_sig_info(sig, &info, tsk->parent);
1587 __wake_up_parent(tsk, tsk->parent);
1588 spin_unlock_irqrestore(&psig->siglock, flags);
1591 static void do_notify_parent_cldstop(struct task_struct *tsk, int to_self, int why)
1593 struct siginfo info;
1594 unsigned long flags;
1595 struct task_struct *parent;
1596 struct sighand_struct *sighand;
1598 if (to_self)
1599 parent = tsk->parent;
1600 else {
1601 tsk = tsk->group_leader;
1602 parent = tsk->real_parent;
1605 info.si_signo = SIGCHLD;
1606 info.si_errno = 0;
1607 info.si_pid = tsk->pid;
1608 info.si_uid = tsk->uid;
1610 /* FIXME: find out whether or not this is supposed to be c*time. */
1611 info.si_utime = cputime_to_jiffies(tsk->utime);
1612 info.si_stime = cputime_to_jiffies(tsk->stime);
1614 info.si_code = why;
1615 switch (why) {
1616 case CLD_CONTINUED:
1617 info.si_status = SIGCONT;
1618 break;
1619 case CLD_STOPPED:
1620 info.si_status = tsk->signal->group_exit_code & 0x7f;
1621 break;
1622 case CLD_TRAPPED:
1623 info.si_status = tsk->exit_code & 0x7f;
1624 break;
1625 default:
1626 BUG();
1629 sighand = parent->sighand;
1630 spin_lock_irqsave(&sighand->siglock, flags);
1631 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1632 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1633 __group_send_sig_info(SIGCHLD, &info, parent);
1635 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1637 __wake_up_parent(tsk, parent);
1638 spin_unlock_irqrestore(&sighand->siglock, flags);
1642 * This must be called with current->sighand->siglock held.
1644 * This should be the path for all ptrace stops.
1645 * We always set current->last_siginfo while stopped here.
1646 * That makes it a way to test a stopped process for
1647 * being ptrace-stopped vs being job-control-stopped.
1649 * If we actually decide not to stop at all because the tracer is gone,
1650 * we leave nostop_code in current->exit_code.
1652 static void ptrace_stop(int exit_code, int nostop_code, siginfo_t *info)
1655 * If there is a group stop in progress,
1656 * we must participate in the bookkeeping.
1658 if (current->signal->group_stop_count > 0)
1659 --current->signal->group_stop_count;
1661 current->last_siginfo = info;
1662 current->exit_code = exit_code;
1664 /* Let the debugger run. */
1665 set_current_state(TASK_TRACED);
1666 spin_unlock_irq(&current->sighand->siglock);
1667 read_lock(&tasklist_lock);
1668 if (likely(current->ptrace & PT_PTRACED) &&
1669 likely(current->parent != current->real_parent ||
1670 !(current->ptrace & PT_ATTACHED)) &&
1671 (likely(current->parent->signal != current->signal) ||
1672 !unlikely(current->signal->flags & SIGNAL_GROUP_EXIT))) {
1673 do_notify_parent_cldstop(current, 1, CLD_TRAPPED);
1674 read_unlock(&tasklist_lock);
1675 schedule();
1676 } else {
1678 * By the time we got the lock, our tracer went away.
1679 * Don't stop here.
1681 read_unlock(&tasklist_lock);
1682 set_current_state(TASK_RUNNING);
1683 current->exit_code = nostop_code;
1687 * We are back. Now reacquire the siglock before touching
1688 * last_siginfo, so that we are sure to have synchronized with
1689 * any signal-sending on another CPU that wants to examine it.
1691 spin_lock_irq(&current->sighand->siglock);
1692 current->last_siginfo = NULL;
1695 * Queued signals ignored us while we were stopped for tracing.
1696 * So check for any that we should take before resuming user mode.
1698 recalc_sigpending();
1701 void ptrace_notify(int exit_code)
1703 siginfo_t info;
1705 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1707 memset(&info, 0, sizeof info);
1708 info.si_signo = SIGTRAP;
1709 info.si_code = exit_code;
1710 info.si_pid = current->pid;
1711 info.si_uid = current->uid;
1713 /* Let the debugger run. */
1714 spin_lock_irq(&current->sighand->siglock);
1715 ptrace_stop(exit_code, 0, &info);
1716 spin_unlock_irq(&current->sighand->siglock);
1719 static void
1720 finish_stop(int stop_count)
1722 int to_self;
1725 * If there are no other threads in the group, or if there is
1726 * a group stop in progress and we are the last to stop,
1727 * report to the parent. When ptraced, every thread reports itself.
1729 if (stop_count < 0 || (current->ptrace & PT_PTRACED))
1730 to_self = 1;
1731 else if (stop_count == 0)
1732 to_self = 0;
1733 else
1734 goto out;
1736 read_lock(&tasklist_lock);
1737 do_notify_parent_cldstop(current, to_self, CLD_STOPPED);
1738 read_unlock(&tasklist_lock);
1740 out:
1741 schedule();
1743 * Now we don't run again until continued.
1745 current->exit_code = 0;
1749 * This performs the stopping for SIGSTOP and other stop signals.
1750 * We have to stop all threads in the thread group.
1751 * Returns nonzero if we've actually stopped and released the siglock.
1752 * Returns zero if we didn't stop and still hold the siglock.
1754 static int
1755 do_signal_stop(int signr)
1757 struct signal_struct *sig = current->signal;
1758 struct sighand_struct *sighand = current->sighand;
1759 int stop_count = -1;
1761 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED))
1762 return 0;
1764 if (sig->group_stop_count > 0) {
1766 * There is a group stop in progress. We don't need to
1767 * start another one.
1769 signr = sig->group_exit_code;
1770 stop_count = --sig->group_stop_count;
1771 current->exit_code = signr;
1772 set_current_state(TASK_STOPPED);
1773 if (stop_count == 0)
1774 sig->flags = SIGNAL_STOP_STOPPED;
1775 spin_unlock_irq(&sighand->siglock);
1777 else if (thread_group_empty(current)) {
1779 * Lock must be held through transition to stopped state.
1781 current->exit_code = current->signal->group_exit_code = signr;
1782 set_current_state(TASK_STOPPED);
1783 sig->flags = SIGNAL_STOP_STOPPED;
1784 spin_unlock_irq(&sighand->siglock);
1786 else {
1788 * There is no group stop already in progress.
1789 * We must initiate one now, but that requires
1790 * dropping siglock to get both the tasklist lock
1791 * and siglock again in the proper order. Note that
1792 * this allows an intervening SIGCONT to be posted.
1793 * We need to check for that and bail out if necessary.
1795 struct task_struct *t;
1797 spin_unlock_irq(&sighand->siglock);
1799 /* signals can be posted during this window */
1801 read_lock(&tasklist_lock);
1802 spin_lock_irq(&sighand->siglock);
1804 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED)) {
1806 * Another stop or continue happened while we
1807 * didn't have the lock. We can just swallow this
1808 * signal now. If we raced with a SIGCONT, that
1809 * should have just cleared it now. If we raced
1810 * with another processor delivering a stop signal,
1811 * then the SIGCONT that wakes us up should clear it.
1813 read_unlock(&tasklist_lock);
1814 return 0;
1817 if (sig->group_stop_count == 0) {
1818 sig->group_exit_code = signr;
1819 stop_count = 0;
1820 for (t = next_thread(current); t != current;
1821 t = next_thread(t))
1823 * Setting state to TASK_STOPPED for a group
1824 * stop is always done with the siglock held,
1825 * so this check has no races.
1827 if (!t->exit_state &&
1828 !(t->state & (TASK_STOPPED|TASK_TRACED))) {
1829 stop_count++;
1830 signal_wake_up(t, 0);
1832 sig->group_stop_count = stop_count;
1834 else {
1835 /* A race with another thread while unlocked. */
1836 signr = sig->group_exit_code;
1837 stop_count = --sig->group_stop_count;
1840 current->exit_code = signr;
1841 set_current_state(TASK_STOPPED);
1842 if (stop_count == 0)
1843 sig->flags = SIGNAL_STOP_STOPPED;
1845 spin_unlock_irq(&sighand->siglock);
1846 read_unlock(&tasklist_lock);
1849 finish_stop(stop_count);
1850 return 1;
1854 * Do appropriate magic when group_stop_count > 0.
1855 * We return nonzero if we stopped, after releasing the siglock.
1856 * We return zero if we still hold the siglock and should look
1857 * for another signal without checking group_stop_count again.
1859 static inline int handle_group_stop(void)
1861 int stop_count;
1863 if (current->signal->group_exit_task == current) {
1865 * Group stop is so we can do a core dump,
1866 * We are the initiating thread, so get on with it.
1868 current->signal->group_exit_task = NULL;
1869 return 0;
1872 if (current->signal->flags & SIGNAL_GROUP_EXIT)
1874 * Group stop is so another thread can do a core dump,
1875 * or else we are racing against a death signal.
1876 * Just punt the stop so we can get the next signal.
1878 return 0;
1881 * There is a group stop in progress. We stop
1882 * without any associated signal being in our queue.
1884 stop_count = --current->signal->group_stop_count;
1885 if (stop_count == 0)
1886 current->signal->flags = SIGNAL_STOP_STOPPED;
1887 current->exit_code = current->signal->group_exit_code;
1888 set_current_state(TASK_STOPPED);
1889 spin_unlock_irq(&current->sighand->siglock);
1890 finish_stop(stop_count);
1891 return 1;
1894 int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1895 struct pt_regs *regs, void *cookie)
1897 sigset_t *mask = &current->blocked;
1898 int signr = 0;
1900 relock:
1901 spin_lock_irq(&current->sighand->siglock);
1902 for (;;) {
1903 struct k_sigaction *ka;
1905 if (unlikely(current->signal->group_stop_count > 0) &&
1906 handle_group_stop())
1907 goto relock;
1909 signr = dequeue_signal(current, mask, info);
1911 if (!signr)
1912 break; /* will return 0 */
1914 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1915 ptrace_signal_deliver(regs, cookie);
1917 /* Let the debugger run. */
1918 ptrace_stop(signr, signr, info);
1920 /* We're back. Did the debugger cancel the sig or group_exit? */
1921 signr = current->exit_code;
1922 if (signr == 0 || current->signal->flags & SIGNAL_GROUP_EXIT)
1923 continue;
1925 current->exit_code = 0;
1927 /* Update the siginfo structure if the signal has
1928 changed. If the debugger wanted something
1929 specific in the siginfo structure then it should
1930 have updated *info via PTRACE_SETSIGINFO. */
1931 if (signr != info->si_signo) {
1932 info->si_signo = signr;
1933 info->si_errno = 0;
1934 info->si_code = SI_USER;
1935 info->si_pid = current->parent->pid;
1936 info->si_uid = current->parent->uid;
1939 /* If the (new) signal is now blocked, requeue it. */
1940 if (sigismember(&current->blocked, signr)) {
1941 specific_send_sig_info(signr, info, current);
1942 continue;
1946 ka = &current->sighand->action[signr-1];
1947 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1948 continue;
1949 if (ka->sa.sa_handler != SIG_DFL) {
1950 /* Run the handler. */
1951 *return_ka = *ka;
1953 if (ka->sa.sa_flags & SA_ONESHOT)
1954 ka->sa.sa_handler = SIG_DFL;
1956 break; /* will return non-zero "signr" value */
1960 * Now we are doing the default action for this signal.
1962 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1963 continue;
1965 /* Init gets no signals it doesn't want. */
1966 if (current->pid == 1)
1967 continue;
1969 if (sig_kernel_stop(signr)) {
1971 * The default action is to stop all threads in
1972 * the thread group. The job control signals
1973 * do nothing in an orphaned pgrp, but SIGSTOP
1974 * always works. Note that siglock needs to be
1975 * dropped during the call to is_orphaned_pgrp()
1976 * because of lock ordering with tasklist_lock.
1977 * This allows an intervening SIGCONT to be posted.
1978 * We need to check for that and bail out if necessary.
1980 if (signr != SIGSTOP) {
1981 spin_unlock_irq(&current->sighand->siglock);
1983 /* signals can be posted during this window */
1985 if (is_orphaned_pgrp(process_group(current)))
1986 goto relock;
1988 spin_lock_irq(&current->sighand->siglock);
1991 if (likely(do_signal_stop(signr))) {
1992 /* It released the siglock. */
1993 goto relock;
1997 * We didn't actually stop, due to a race
1998 * with SIGCONT or something like that.
2000 continue;
2003 spin_unlock_irq(&current->sighand->siglock);
2006 * Anything else is fatal, maybe with a core dump.
2008 current->flags |= PF_SIGNALED;
2009 if (sig_kernel_coredump(signr)) {
2011 * If it was able to dump core, this kills all
2012 * other threads in the group and synchronizes with
2013 * their demise. If we lost the race with another
2014 * thread getting here, it set group_exit_code
2015 * first and our do_group_exit call below will use
2016 * that value and ignore the one we pass it.
2018 do_coredump((long)signr, signr, regs);
2022 * Death signals, no core dump.
2024 do_group_exit(signr);
2025 /* NOTREACHED */
2027 spin_unlock_irq(&current->sighand->siglock);
2028 return signr;
2031 EXPORT_SYMBOL(recalc_sigpending);
2032 EXPORT_SYMBOL_GPL(dequeue_signal);
2033 EXPORT_SYMBOL(flush_signals);
2034 EXPORT_SYMBOL(force_sig);
2035 EXPORT_SYMBOL(kill_pg);
2036 EXPORT_SYMBOL(kill_proc);
2037 EXPORT_SYMBOL(ptrace_notify);
2038 EXPORT_SYMBOL(send_sig);
2039 EXPORT_SYMBOL(send_sig_info);
2040 EXPORT_SYMBOL(sigprocmask);
2041 EXPORT_SYMBOL(block_all_signals);
2042 EXPORT_SYMBOL(unblock_all_signals);
2046 * System call entry points.
2049 asmlinkage long sys_restart_syscall(void)
2051 struct restart_block *restart = &current_thread_info()->restart_block;
2052 return restart->fn(restart);
2055 long do_no_restart_syscall(struct restart_block *param)
2057 return -EINTR;
2061 * We don't need to get the kernel lock - this is all local to this
2062 * particular thread.. (and that's good, because this is _heavily_
2063 * used by various programs)
2067 * This is also useful for kernel threads that want to temporarily
2068 * (or permanently) block certain signals.
2070 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2071 * interface happily blocks "unblockable" signals like SIGKILL
2072 * and friends.
2074 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2076 int error;
2077 sigset_t old_block;
2079 spin_lock_irq(&current->sighand->siglock);
2080 old_block = current->blocked;
2081 error = 0;
2082 switch (how) {
2083 case SIG_BLOCK:
2084 sigorsets(&current->blocked, &current->blocked, set);
2085 break;
2086 case SIG_UNBLOCK:
2087 signandsets(&current->blocked, &current->blocked, set);
2088 break;
2089 case SIG_SETMASK:
2090 current->blocked = *set;
2091 break;
2092 default:
2093 error = -EINVAL;
2095 recalc_sigpending();
2096 spin_unlock_irq(&current->sighand->siglock);
2097 if (oldset)
2098 *oldset = old_block;
2099 return error;
2102 asmlinkage long
2103 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2105 int error = -EINVAL;
2106 sigset_t old_set, new_set;
2108 /* XXX: Don't preclude handling different sized sigset_t's. */
2109 if (sigsetsize != sizeof(sigset_t))
2110 goto out;
2112 if (set) {
2113 error = -EFAULT;
2114 if (copy_from_user(&new_set, set, sizeof(*set)))
2115 goto out;
2116 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2118 error = sigprocmask(how, &new_set, &old_set);
2119 if (error)
2120 goto out;
2121 if (oset)
2122 goto set_old;
2123 } else if (oset) {
2124 spin_lock_irq(&current->sighand->siglock);
2125 old_set = current->blocked;
2126 spin_unlock_irq(&current->sighand->siglock);
2128 set_old:
2129 error = -EFAULT;
2130 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2131 goto out;
2133 error = 0;
2134 out:
2135 return error;
2138 long do_sigpending(void __user *set, unsigned long sigsetsize)
2140 long error = -EINVAL;
2141 sigset_t pending;
2143 if (sigsetsize > sizeof(sigset_t))
2144 goto out;
2146 spin_lock_irq(&current->sighand->siglock);
2147 sigorsets(&pending, &current->pending.signal,
2148 &current->signal->shared_pending.signal);
2149 spin_unlock_irq(&current->sighand->siglock);
2151 /* Outside the lock because only this thread touches it. */
2152 sigandsets(&pending, &current->blocked, &pending);
2154 error = -EFAULT;
2155 if (!copy_to_user(set, &pending, sigsetsize))
2156 error = 0;
2158 out:
2159 return error;
2162 asmlinkage long
2163 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2165 return do_sigpending(set, sigsetsize);
2168 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2170 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2172 int err;
2174 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2175 return -EFAULT;
2176 if (from->si_code < 0)
2177 return __copy_to_user(to, from, sizeof(siginfo_t))
2178 ? -EFAULT : 0;
2180 * If you change siginfo_t structure, please be sure
2181 * this code is fixed accordingly.
2182 * It should never copy any pad contained in the structure
2183 * to avoid security leaks, but must copy the generic
2184 * 3 ints plus the relevant union member.
2186 err = __put_user(from->si_signo, &to->si_signo);
2187 err |= __put_user(from->si_errno, &to->si_errno);
2188 err |= __put_user((short)from->si_code, &to->si_code);
2189 switch (from->si_code & __SI_MASK) {
2190 case __SI_KILL:
2191 err |= __put_user(from->si_pid, &to->si_pid);
2192 err |= __put_user(from->si_uid, &to->si_uid);
2193 break;
2194 case __SI_TIMER:
2195 err |= __put_user(from->si_tid, &to->si_tid);
2196 err |= __put_user(from->si_overrun, &to->si_overrun);
2197 err |= __put_user(from->si_ptr, &to->si_ptr);
2198 break;
2199 case __SI_POLL:
2200 err |= __put_user(from->si_band, &to->si_band);
2201 err |= __put_user(from->si_fd, &to->si_fd);
2202 break;
2203 case __SI_FAULT:
2204 err |= __put_user(from->si_addr, &to->si_addr);
2205 #ifdef __ARCH_SI_TRAPNO
2206 err |= __put_user(from->si_trapno, &to->si_trapno);
2207 #endif
2208 break;
2209 case __SI_CHLD:
2210 err |= __put_user(from->si_pid, &to->si_pid);
2211 err |= __put_user(from->si_uid, &to->si_uid);
2212 err |= __put_user(from->si_status, &to->si_status);
2213 err |= __put_user(from->si_utime, &to->si_utime);
2214 err |= __put_user(from->si_stime, &to->si_stime);
2215 break;
2216 case __SI_RT: /* This is not generated by the kernel as of now. */
2217 case __SI_MESGQ: /* But this is */
2218 err |= __put_user(from->si_pid, &to->si_pid);
2219 err |= __put_user(from->si_uid, &to->si_uid);
2220 err |= __put_user(from->si_ptr, &to->si_ptr);
2221 break;
2222 default: /* this is just in case for now ... */
2223 err |= __put_user(from->si_pid, &to->si_pid);
2224 err |= __put_user(from->si_uid, &to->si_uid);
2225 break;
2227 return err;
2230 #endif
2232 asmlinkage long
2233 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2234 siginfo_t __user *uinfo,
2235 const struct timespec __user *uts,
2236 size_t sigsetsize)
2238 int ret, sig;
2239 sigset_t these;
2240 struct timespec ts;
2241 siginfo_t info;
2242 long timeout = 0;
2244 /* XXX: Don't preclude handling different sized sigset_t's. */
2245 if (sigsetsize != sizeof(sigset_t))
2246 return -EINVAL;
2248 if (copy_from_user(&these, uthese, sizeof(these)))
2249 return -EFAULT;
2252 * Invert the set of allowed signals to get those we
2253 * want to block.
2255 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2256 signotset(&these);
2258 if (uts) {
2259 if (copy_from_user(&ts, uts, sizeof(ts)))
2260 return -EFAULT;
2261 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2262 || ts.tv_sec < 0)
2263 return -EINVAL;
2266 spin_lock_irq(&current->sighand->siglock);
2267 sig = dequeue_signal(current, &these, &info);
2268 if (!sig) {
2269 timeout = MAX_SCHEDULE_TIMEOUT;
2270 if (uts)
2271 timeout = (timespec_to_jiffies(&ts)
2272 + (ts.tv_sec || ts.tv_nsec));
2274 if (timeout) {
2275 /* None ready -- temporarily unblock those we're
2276 * interested while we are sleeping in so that we'll
2277 * be awakened when they arrive. */
2278 current->real_blocked = current->blocked;
2279 sigandsets(&current->blocked, &current->blocked, &these);
2280 recalc_sigpending();
2281 spin_unlock_irq(&current->sighand->siglock);
2283 timeout = schedule_timeout_interruptible(timeout);
2285 try_to_freeze();
2286 spin_lock_irq(&current->sighand->siglock);
2287 sig = dequeue_signal(current, &these, &info);
2288 current->blocked = current->real_blocked;
2289 siginitset(&current->real_blocked, 0);
2290 recalc_sigpending();
2293 spin_unlock_irq(&current->sighand->siglock);
2295 if (sig) {
2296 ret = sig;
2297 if (uinfo) {
2298 if (copy_siginfo_to_user(uinfo, &info))
2299 ret = -EFAULT;
2301 } else {
2302 ret = -EAGAIN;
2303 if (timeout)
2304 ret = -EINTR;
2307 return ret;
2310 asmlinkage long
2311 sys_kill(int pid, int sig)
2313 struct siginfo info;
2315 info.si_signo = sig;
2316 info.si_errno = 0;
2317 info.si_code = SI_USER;
2318 info.si_pid = current->tgid;
2319 info.si_uid = current->uid;
2321 return kill_something_info(sig, &info, pid);
2324 static int do_tkill(int tgid, int pid, int sig)
2326 int error;
2327 struct siginfo info;
2328 struct task_struct *p;
2330 error = -ESRCH;
2331 info.si_signo = sig;
2332 info.si_errno = 0;
2333 info.si_code = SI_TKILL;
2334 info.si_pid = current->tgid;
2335 info.si_uid = current->uid;
2337 read_lock(&tasklist_lock);
2338 p = find_task_by_pid(pid);
2339 if (p && (tgid <= 0 || p->tgid == tgid)) {
2340 error = check_kill_permission(sig, &info, p);
2342 * The null signal is a permissions and process existence
2343 * probe. No signal is actually delivered.
2345 if (!error && sig && p->sighand) {
2346 spin_lock_irq(&p->sighand->siglock);
2347 handle_stop_signal(sig, p);
2348 error = specific_send_sig_info(sig, &info, p);
2349 spin_unlock_irq(&p->sighand->siglock);
2352 read_unlock(&tasklist_lock);
2354 return error;
2358 * sys_tgkill - send signal to one specific thread
2359 * @tgid: the thread group ID of the thread
2360 * @pid: the PID of the thread
2361 * @sig: signal to be sent
2363 * This syscall also checks the tgid and returns -ESRCH even if the PID
2364 * exists but it's not belonging to the target process anymore. This
2365 * method solves the problem of threads exiting and PIDs getting reused.
2367 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2369 /* This is only valid for single tasks */
2370 if (pid <= 0 || tgid <= 0)
2371 return -EINVAL;
2373 return do_tkill(tgid, pid, sig);
2377 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2379 asmlinkage long
2380 sys_tkill(int pid, int sig)
2382 /* This is only valid for single tasks */
2383 if (pid <= 0)
2384 return -EINVAL;
2386 return do_tkill(0, pid, sig);
2389 asmlinkage long
2390 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2392 siginfo_t info;
2394 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2395 return -EFAULT;
2397 /* Not even root can pretend to send signals from the kernel.
2398 Nor can they impersonate a kill(), which adds source info. */
2399 if (info.si_code >= 0)
2400 return -EPERM;
2401 info.si_signo = sig;
2403 /* POSIX.1b doesn't mention process groups. */
2404 return kill_proc_info(sig, &info, pid);
2408 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2410 struct k_sigaction *k;
2412 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
2413 return -EINVAL;
2415 k = &current->sighand->action[sig-1];
2417 spin_lock_irq(&current->sighand->siglock);
2418 if (signal_pending(current)) {
2420 * If there might be a fatal signal pending on multiple
2421 * threads, make sure we take it before changing the action.
2423 spin_unlock_irq(&current->sighand->siglock);
2424 return -ERESTARTNOINTR;
2427 if (oact)
2428 *oact = *k;
2430 if (act) {
2432 * POSIX 3.3.1.3:
2433 * "Setting a signal action to SIG_IGN for a signal that is
2434 * pending shall cause the pending signal to be discarded,
2435 * whether or not it is blocked."
2437 * "Setting a signal action to SIG_DFL for a signal that is
2438 * pending and whose default action is to ignore the signal
2439 * (for example, SIGCHLD), shall cause the pending signal to
2440 * be discarded, whether or not it is blocked"
2442 if (act->sa.sa_handler == SIG_IGN ||
2443 (act->sa.sa_handler == SIG_DFL &&
2444 sig_kernel_ignore(sig))) {
2446 * This is a fairly rare case, so we only take the
2447 * tasklist_lock once we're sure we'll need it.
2448 * Now we must do this little unlock and relock
2449 * dance to maintain the lock hierarchy.
2451 struct task_struct *t = current;
2452 spin_unlock_irq(&t->sighand->siglock);
2453 read_lock(&tasklist_lock);
2454 spin_lock_irq(&t->sighand->siglock);
2455 *k = *act;
2456 sigdelsetmask(&k->sa.sa_mask,
2457 sigmask(SIGKILL) | sigmask(SIGSTOP));
2458 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2459 do {
2460 rm_from_queue(sigmask(sig), &t->pending);
2461 recalc_sigpending_tsk(t);
2462 t = next_thread(t);
2463 } while (t != current);
2464 spin_unlock_irq(&current->sighand->siglock);
2465 read_unlock(&tasklist_lock);
2466 return 0;
2469 *k = *act;
2470 sigdelsetmask(&k->sa.sa_mask,
2471 sigmask(SIGKILL) | sigmask(SIGSTOP));
2474 spin_unlock_irq(&current->sighand->siglock);
2475 return 0;
2478 int
2479 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2481 stack_t oss;
2482 int error;
2484 if (uoss) {
2485 oss.ss_sp = (void __user *) current->sas_ss_sp;
2486 oss.ss_size = current->sas_ss_size;
2487 oss.ss_flags = sas_ss_flags(sp);
2490 if (uss) {
2491 void __user *ss_sp;
2492 size_t ss_size;
2493 int ss_flags;
2495 error = -EFAULT;
2496 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2497 || __get_user(ss_sp, &uss->ss_sp)
2498 || __get_user(ss_flags, &uss->ss_flags)
2499 || __get_user(ss_size, &uss->ss_size))
2500 goto out;
2502 error = -EPERM;
2503 if (on_sig_stack(sp))
2504 goto out;
2506 error = -EINVAL;
2509 * Note - this code used to test ss_flags incorrectly
2510 * old code may have been written using ss_flags==0
2511 * to mean ss_flags==SS_ONSTACK (as this was the only
2512 * way that worked) - this fix preserves that older
2513 * mechanism
2515 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2516 goto out;
2518 if (ss_flags == SS_DISABLE) {
2519 ss_size = 0;
2520 ss_sp = NULL;
2521 } else {
2522 error = -ENOMEM;
2523 if (ss_size < MINSIGSTKSZ)
2524 goto out;
2527 current->sas_ss_sp = (unsigned long) ss_sp;
2528 current->sas_ss_size = ss_size;
2531 if (uoss) {
2532 error = -EFAULT;
2533 if (copy_to_user(uoss, &oss, sizeof(oss)))
2534 goto out;
2537 error = 0;
2538 out:
2539 return error;
2542 #ifdef __ARCH_WANT_SYS_SIGPENDING
2544 asmlinkage long
2545 sys_sigpending(old_sigset_t __user *set)
2547 return do_sigpending(set, sizeof(*set));
2550 #endif
2552 #ifdef __ARCH_WANT_SYS_SIGPROCMASK
2553 /* Some platforms have their own version with special arguments others
2554 support only sys_rt_sigprocmask. */
2556 asmlinkage long
2557 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2559 int error;
2560 old_sigset_t old_set, new_set;
2562 if (set) {
2563 error = -EFAULT;
2564 if (copy_from_user(&new_set, set, sizeof(*set)))
2565 goto out;
2566 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2568 spin_lock_irq(&current->sighand->siglock);
2569 old_set = current->blocked.sig[0];
2571 error = 0;
2572 switch (how) {
2573 default:
2574 error = -EINVAL;
2575 break;
2576 case SIG_BLOCK:
2577 sigaddsetmask(&current->blocked, new_set);
2578 break;
2579 case SIG_UNBLOCK:
2580 sigdelsetmask(&current->blocked, new_set);
2581 break;
2582 case SIG_SETMASK:
2583 current->blocked.sig[0] = new_set;
2584 break;
2587 recalc_sigpending();
2588 spin_unlock_irq(&current->sighand->siglock);
2589 if (error)
2590 goto out;
2591 if (oset)
2592 goto set_old;
2593 } else if (oset) {
2594 old_set = current->blocked.sig[0];
2595 set_old:
2596 error = -EFAULT;
2597 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2598 goto out;
2600 error = 0;
2601 out:
2602 return error;
2604 #endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2606 #ifdef __ARCH_WANT_SYS_RT_SIGACTION
2607 asmlinkage long
2608 sys_rt_sigaction(int sig,
2609 const struct sigaction __user *act,
2610 struct sigaction __user *oact,
2611 size_t sigsetsize)
2613 struct k_sigaction new_sa, old_sa;
2614 int ret = -EINVAL;
2616 /* XXX: Don't preclude handling different sized sigset_t's. */
2617 if (sigsetsize != sizeof(sigset_t))
2618 goto out;
2620 if (act) {
2621 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2622 return -EFAULT;
2625 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2627 if (!ret && oact) {
2628 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2629 return -EFAULT;
2631 out:
2632 return ret;
2634 #endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2636 #ifdef __ARCH_WANT_SYS_SGETMASK
2639 * For backwards compatibility. Functionality superseded by sigprocmask.
2641 asmlinkage long
2642 sys_sgetmask(void)
2644 /* SMP safe */
2645 return current->blocked.sig[0];
2648 asmlinkage long
2649 sys_ssetmask(int newmask)
2651 int old;
2653 spin_lock_irq(&current->sighand->siglock);
2654 old = current->blocked.sig[0];
2656 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2657 sigmask(SIGSTOP)));
2658 recalc_sigpending();
2659 spin_unlock_irq(&current->sighand->siglock);
2661 return old;
2663 #endif /* __ARCH_WANT_SGETMASK */
2665 #ifdef __ARCH_WANT_SYS_SIGNAL
2667 * For backwards compatibility. Functionality superseded by sigaction.
2669 asmlinkage unsigned long
2670 sys_signal(int sig, __sighandler_t handler)
2672 struct k_sigaction new_sa, old_sa;
2673 int ret;
2675 new_sa.sa.sa_handler = handler;
2676 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2678 ret = do_sigaction(sig, &new_sa, &old_sa);
2680 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2682 #endif /* __ARCH_WANT_SYS_SIGNAL */
2684 #ifdef __ARCH_WANT_SYS_PAUSE
2686 asmlinkage long
2687 sys_pause(void)
2689 current->state = TASK_INTERRUPTIBLE;
2690 schedule();
2691 return -ERESTARTNOHAND;
2694 #endif
2696 void __init signals_init(void)
2698 sigqueue_cachep =
2699 kmem_cache_create("sigqueue",
2700 sizeof(struct sigqueue),
2701 __alignof__(struct sigqueue),
2702 SLAB_PANIC, NULL, NULL);