]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/signal.c
[PATCH] RCU signal handling
[net-next-2.6.git] / kernel / signal.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/signal.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 *
6 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
7 *
8 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
9 * Changes to use preallocated sigqueue structures
10 * to allow signals to be sent reliably.
11 */
12
13#include <linux/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>
7ed20e1a 26#include <linux/signal.h>
c2f0c7c3 27#include <linux/audit.h>
1da177e4
LT
28#include <asm/param.h>
29#include <asm/uaccess.h>
30#include <asm/unistd.h>
31#include <asm/siginfo.h>
32
33/*
34 * SLAB caches for signal bits.
35 */
36
37static kmem_cache_t *sigqueue_cachep;
38
39/*
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).
47 *
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
56 *
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.
60 *
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 * +--------------------+------------------+
101 *
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.
111 */
112
113#ifdef SIGEMT
114#define M_SIGEMT M(SIGEMT)
115#else
116#define M_SIGEMT 0
117#endif
118
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))
125
126#define SIG_KERNEL_ONLY_MASK (\
127 M(SIGKILL) | M(SIGSTOP) )
128
129#define SIG_KERNEL_STOP_MASK (\
130 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
131
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 )
136
137#define SIG_KERNEL_IGNORE_MASK (\
138 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
139
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))
148
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))
152
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)
156
157static int sig_ignored(struct task_struct *t, int sig)
158{
159 void __user * handler;
160
161 /*
162 * Tracers always want to know about signals..
163 */
164 if (t->ptrace & PT_PTRACED)
165 return 0;
166
167 /*
168 * Blocked signals are never ignored, since the
169 * signal handler may change by the time it is
170 * unblocked.
171 */
172 if (sigismember(&t->blocked, sig))
173 return 0;
174
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));
179}
180
181/*
182 * Re-calculate pending state from the set of locally pending
183 * signals, globally pending signals, and blocked signals.
184 */
185static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
186{
187 unsigned long ready;
188 long i;
189
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;
195
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;
201
202 case 2: ready = signal->sig[1] &~ blocked->sig[1];
203 ready |= signal->sig[0] &~ blocked->sig[0];
204 break;
205
206 case 1: ready = signal->sig[0] &~ blocked->sig[0];
207 }
208 return ready != 0;
209}
210
211#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
212
213fastcall void recalc_sigpending_tsk(struct task_struct *t)
214{
215 if (t->signal->group_stop_count > 0 ||
3e1d1d28 216 (freezing(t)) ||
1da177e4
LT
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);
222}
223
224void recalc_sigpending(void)
225{
226 recalc_sigpending_tsk(current);
227}
228
229/* Given the mask, find the first available signal that should be serviced. */
230
231static int
232next_signal(struct sigpending *pending, sigset_t *mask)
233{
234 unsigned long i, *s, *m, x;
235 int sig = 0;
236
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;
245 }
246 break;
247
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;
256
257 case 1: if ((x = *s &~ *m) != 0)
258 sig = ffz(~x) + 1;
259 break;
260 }
261
262 return sig;
263}
264
dd0fc66f 265static struct sigqueue *__sigqueue_alloc(struct task_struct *t, gfp_t flags,
1da177e4
LT
266 int override_rlimit)
267{
268 struct sigqueue *q = NULL;
269
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;
1da177e4
LT
280 q->user = get_uid(t->user);
281 }
282 return(q);
283}
284
285static inline void __sigqueue_free(struct sigqueue *q)
286{
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);
292}
293
294static void flush_sigqueue(struct sigpending *queue)
295{
296 struct sigqueue *q;
297
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);
303 }
304}
305
306/*
307 * Flush all pending signals for a task.
308 */
309
310void
311flush_signals(struct task_struct *t)
312{
313 unsigned long flags;
314
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);
320}
321
322/*
323 * This function expects the tasklist_lock write-locked.
324 */
325void __exit_sighand(struct task_struct *tsk)
326{
327 struct sighand_struct * sighand = tsk->sighand;
328
329 /* Ok, we're done with the signal handlers */
330 tsk->sighand = NULL;
331 if (atomic_dec_and_test(&sighand->count))
e56d0903 332 sighand_free(sighand);
1da177e4
LT
333}
334
335void exit_sighand(struct task_struct *tsk)
336{
337 write_lock_irq(&tasklist_lock);
e56d0903
IM
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);
344 }
345 rcu_read_unlock();
1da177e4
LT
346 write_unlock_irq(&tasklist_lock);
347}
348
349/*
350 * This function expects the tasklist_lock write-locked.
351 */
352void __exit_signal(struct task_struct *tsk)
353{
354 struct signal_struct * sig = tsk->signal;
e56d0903 355 struct sighand_struct * sighand;
1da177e4
LT
356
357 if (!sig)
358 BUG();
359 if (!atomic_read(&sig->count))
360 BUG();
e56d0903
IM
361 rcu_read_lock();
362 sighand = rcu_dereference(tsk->sighand);
1da177e4
LT
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;
e56d0903 370 __exit_sighand(tsk);
1da177e4
LT
371 spin_unlock(&sighand->siglock);
372 flush_sigqueue(&sig->shared_pending);
373 } else {
374 /*
375 * If there is any task waiting for the group exit
376 * then notify it:
377 */
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;
381 }
382 if (tsk == sig->curr_target)
383 sig->curr_target = next_thread(tsk);
384 tsk->signal = NULL;
385 /*
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.
394 */
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;
e56d0903 402 __exit_sighand(tsk);
1da177e4
LT
403 spin_unlock(&sighand->siglock);
404 sig = NULL; /* Marker for below. */
405 }
e56d0903 406 rcu_read_unlock();
1da177e4
LT
407 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
408 flush_sigqueue(&tsk->pending);
409 if (sig) {
410 /*
25f407f0 411 * We are cleaning up the signal_struct here.
1da177e4 412 */
1da177e4
LT
413 exit_thread_group_keys(sig);
414 kmem_cache_free(signal_cachep, sig);
415 }
416}
417
418void exit_signal(struct task_struct *tsk)
419{
8d027de5
ON
420 atomic_dec(&tsk->signal->live);
421
1da177e4
LT
422 write_lock_irq(&tasklist_lock);
423 __exit_signal(tsk);
424 write_unlock_irq(&tasklist_lock);
425}
426
427/*
428 * Flush all handlers for a task.
429 */
430
431void
432flush_signal_handlers(struct task_struct *t, int force_default)
433{
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++;
442 }
443}
444
445
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. */
453
454void
455block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
456{
457 unsigned long flags;
458
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);
464}
465
466/* Notify the system that blocking has ended. */
467
468void
469unblock_all_signals(void)
470{
471 unsigned long flags;
472
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);
478}
479
480static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
481{
482 struct sigqueue *q, *first = NULL;
483 int still_pending = 0;
484
485 if (unlikely(!sigismember(&list->signal, sig)))
486 return 0;
487
488 /*
489 * Collect the siginfo appropriate to this signal. Check if
490 * there is another siginfo for the same signal.
491 */
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;
497 }
498 first = q;
499 }
500 }
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 {
508
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.
512 */
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;
519 }
520 return 1;
521}
522
523static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
524 siginfo_t *info)
525{
526 int sig = 0;
527
b17b0421 528 sig = next_signal(pending, mask);
1da177e4
LT
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;
535 }
536 }
537 }
538
539 if (!collect_signal(sig, pending, info))
540 sig = 0;
541
542 }
543 recalc_sigpending();
544
545 return sig;
546}
547
548/*
549 * Dequeue a signal and return the element to the caller, which is
550 * expected to free it.
551 *
552 * All callers have to hold the siglock.
553 */
554int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
555{
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))) {
561 /*
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.
572 */
788e05a6
ON
573 if (!(tsk->signal->flags & SIGNAL_GROUP_EXIT))
574 tsk->signal->flags |= SIGNAL_STOP_DEQUEUED;
1da177e4
LT
575 }
576 if ( signr &&
577 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
578 info->si_sys_private){
579 /*
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.
584 */
585 spin_unlock(&tsk->sighand->siglock);
586 do_schedule_next_timer(info);
587 spin_lock(&tsk->sighand->siglock);
588 }
589 return signr;
590}
591
592/*
593 * Tell a process that it has a new active signal..
594 *
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!
599 *
600 * No need to set need_resched since signal event passing
601 * goes through ->blocked
602 */
603void signal_wake_up(struct task_struct *t, int resume)
604{
605 unsigned int mask;
606
607 set_tsk_thread_flag(t, TIF_SIGPENDING);
608
609 /*
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.
615 */
616 mask = TASK_INTERRUPTIBLE;
617 if (resume)
618 mask |= TASK_STOPPED | TASK_TRACED;
619 if (!wake_up_state(t, mask))
620 kick_process(t);
621}
622
623/*
624 * Remove signals in mask from the pending set and queue.
625 * Returns 1 if any signals were found.
626 *
627 * All callers must be holding the siglock.
628 */
629static int rm_from_queue(unsigned long mask, struct sigpending *s)
630{
631 struct sigqueue *q, *n;
632
633 if (!sigtestsetmask(&s->signal, mask))
634 return 0;
635
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);
642 }
643 }
644 return 1;
645}
646
647/*
648 * Bad permissions for sending the signal
649 */
650static int check_kill_permission(int sig, struct siginfo *info,
651 struct task_struct *t)
652{
653 int error = -EINVAL;
7ed20e1a 654 if (!valid_signal(sig))
1da177e4
LT
655 return error;
656 error = -EPERM;
621d3121 657 if ((info == SEND_SIG_NOINFO || (!is_si_special(info) && SI_FROMUSER(info)))
1da177e4
LT
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;
c2f0c7c3
SG
664
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;
1da177e4
LT
669}
670
671/* forward decl */
672static void do_notify_parent_cldstop(struct task_struct *tsk,
bc505a47 673 int to_self,
1da177e4
LT
674 int why);
675
676/*
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.
682 */
683static void handle_stop_signal(int sig, struct task_struct *p)
684{
685 struct task_struct *t;
686
dd12f48d 687 if (p->signal->flags & SIGNAL_GROUP_EXIT)
1da177e4
LT
688 /*
689 * The process is in the middle of dying already.
690 */
691 return;
692
693 if (sig_kernel_stop(sig)) {
694 /*
695 * This is a stop signal. Remove SIGCONT from all queues.
696 */
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) {
704 /*
705 * Remove all stop signals from all queues,
706 * and wake all threads.
707 */
708 if (unlikely(p->signal->group_stop_count > 0)) {
709 /*
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.
720 */
721 p->signal->group_stop_count = 0;
722 p->signal->flags = SIGNAL_STOP_CONTINUED;
723 spin_unlock(&p->sighand->siglock);
bc505a47 724 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_STOPPED);
1da177e4
LT
725 spin_lock(&p->sighand->siglock);
726 }
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);
732
733 /*
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.
743 *
744 * Wake up the stopped thread _after_ setting
745 * TIF_SIGPENDING
746 */
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;
751 }
752 wake_up_state(t, state);
753
754 t = next_thread(t);
755 } while (t != p);
756
757 if (p->signal->flags & SIGNAL_STOP_STOPPED) {
758 /*
759 * We were in fact stopped, and are now continued.
760 * Notify the parent with CLD_CONTINUED.
761 */
762 p->signal->flags = SIGNAL_STOP_CONTINUED;
763 p->signal->group_exit_code = 0;
764 spin_unlock(&p->sighand->siglock);
bc505a47 765 do_notify_parent_cldstop(p, (p->ptrace & PT_PTRACED), CLD_CONTINUED);
1da177e4
LT
766 spin_lock(&p->sighand->siglock);
767 } else {
768 /*
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.
772 */
773 p->signal->flags = 0;
774 }
775 } else if (sig == SIGKILL) {
776 /*
777 * Make sure that any pending stop signal already dequeued
778 * is undone by the wakeup for SIGKILL.
779 */
780 p->signal->flags = 0;
781 }
782}
783
784static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
785 struct sigpending *signals)
786{
787 struct sigqueue * q = NULL;
788 int ret = 0;
789
790 /*
791 * fast-pathed signals for kernel-internal things like SIGSTOP
792 * or SIGKILL.
793 */
b67a1b9e 794 if (info == SEND_SIG_FORCED)
1da177e4
LT
795 goto out_set;
796
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. */
804
805 q = __sigqueue_alloc(t, GFP_ATOMIC, (sig < SIGRTMIN &&
621d3121 806 (is_si_special(info) ||
1da177e4
LT
807 info->si_code >= 0)));
808 if (q) {
809 list_add_tail(&q->list, &signals->list);
810 switch ((unsigned long) info) {
b67a1b9e 811 case (unsigned long) SEND_SIG_NOINFO:
1da177e4
LT
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;
b67a1b9e 818 case (unsigned long) SEND_SIG_PRIV:
1da177e4
LT
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;
828 }
621d3121
ON
829 } else if (!is_si_special(info)) {
830 if (sig >= SIGRTMIN && info->si_code != SI_USER)
1da177e4
LT
831 /*
832 * Queue overflow, abort. We may abort if the signal was rt
833 * and sent by user using something other than kill().
834 */
835 return -EAGAIN;
1da177e4
LT
836 }
837
838out_set:
839 sigaddset(&signals->signal, sig);
840 return ret;
841}
842
843#define LEGACY_QUEUE(sigptr, sig) \
844 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
845
846
847static int
848specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
849{
850 int ret = 0;
851
852 if (!irqs_disabled())
853 BUG();
854 assert_spin_locked(&t->sighand->siglock);
855
1da177e4
LT
856 /* Short-circuit ignored signals. */
857 if (sig_ignored(t, sig))
858 goto out;
859
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;
865
866 ret = send_signal(sig, info, t, &t->pending);
867 if (!ret && !sigismember(&t->blocked, sig))
868 signal_wake_up(t, sig == SIGKILL);
869out:
870 return ret;
871}
872
873/*
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.
876 */
877
878int
879force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
880{
881 unsigned long int flags;
882 int ret;
883
884 spin_lock_irqsave(&t->sighand->siglock, flags);
b0423a0d 885 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
1da177e4 886 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
b0423a0d
PM
887 }
888 if (sigismember(&t->blocked, sig)) {
1da177e4 889 sigdelset(&t->blocked, sig);
1da177e4 890 }
b0423a0d 891 recalc_sigpending_tsk(t);
1da177e4
LT
892 ret = specific_send_sig_info(sig, info, t);
893 spin_unlock_irqrestore(&t->sighand->siglock, flags);
894
895 return ret;
896}
897
898void
899force_sig_specific(int sig, struct task_struct *t)
900{
b0423a0d 901 force_sig_info(sig, SEND_SIG_FORCED, t);
1da177e4
LT
902}
903
904/*
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.
911 */
188a1eaf
LT
912static inline int wants_signal(int sig, struct task_struct *p)
913{
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);
923}
1da177e4
LT
924
925static void
926__group_complete_signal(int sig, struct task_struct *p)
927{
1da177e4
LT
928 struct task_struct *t;
929
1da177e4
LT
930 /*
931 * Now find a thread we can wake up to take the signal off the queue.
932 *
933 * If the main thread wants the signal, it gets first crack.
934 * Probably the least surprising to the average bear.
935 */
188a1eaf 936 if (wants_signal(sig, p))
1da177e4
LT
937 t = p;
938 else if (thread_group_empty(p))
939 /*
940 * There is just one thread and it does not need to be woken.
941 * It will dequeue unblocked signals before it runs again.
942 */
943 return;
944 else {
945 /*
946 * Otherwise try to find a suitable thread.
947 */
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);
953
188a1eaf 954 while (!wants_signal(sig, t)) {
1da177e4
LT
955 t = next_thread(t);
956 if (t == p->signal->curr_target)
957 /*
958 * No thread needs to be woken.
959 * Any eligible threads will see
960 * the signal in the queue soon.
961 */
962 return;
963 }
964 p->signal->curr_target = t;
965 }
966
967 /*
968 * Found a killable thread. If the signal will be fatal,
969 * then start taking the whole group down immediately.
970 */
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))) {
974 /*
975 * This signal will be fatal to the whole group.
976 */
977 if (!sig_kernel_coredump(sig)) {
978 /*
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.
983 */
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;
994 }
995
996 /*
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.
1005 */
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;
1018 }
1019
1020 /*
1021 * The signal is already in the shared-pending queue.
1022 * Tell the chosen thread to wake up and dequeue it.
1023 */
1024 signal_wake_up(t, sig == SIGKILL);
1025 return;
1026}
1027
1028int
1029__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1030{
1031 int ret = 0;
1032
1033 assert_spin_locked(&p->sighand->siglock);
1034 handle_stop_signal(sig, p);
1035
1da177e4
LT
1036 /* Short-circuit ignored signals. */
1037 if (sig_ignored(p, sig))
1038 return ret;
1039
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;
1043
1044 /*
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.
1048 */
1049 ret = send_signal(sig, info, p, &p->signal->shared_pending);
1050 if (unlikely(ret))
1051 return ret;
1052
1053 __group_complete_signal(sig, p);
1054 return 0;
1055}
1056
1057/*
1058 * Nuke all other threads in the group.
1059 */
1060void zap_other_threads(struct task_struct *p)
1061{
1062 struct task_struct *t;
1063
1064 p->signal->flags = SIGNAL_GROUP_EXIT;
1065 p->signal->group_stop_count = 0;
1066
1067 if (thread_group_empty(p))
1068 return;
1069
1070 for (t = next_thread(p); t != p; t = next_thread(t)) {
1071 /*
1072 * Don't bother with already dead threads
1073 */
1074 if (t->exit_state)
1075 continue;
1076
1077 /*
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.
1084 */
1085 if (t != p->group_leader)
1086 t->exit_signal = -1;
1087
30e0fca6 1088 /* SIGKILL will be handled before any pending SIGSTOP */
1da177e4 1089 sigaddset(&t->pending.signal, SIGKILL);
1da177e4
LT
1090 signal_wake_up(t, 1);
1091 }
1092}
1093
1094/*
e56d0903 1095 * Must be called under rcu_read_lock() or with tasklist_lock read-held.
1da177e4
LT
1096 */
1097int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1098{
1099 unsigned long flags;
e56d0903 1100 struct sighand_struct *sp;
1da177e4
LT
1101 int ret;
1102
e56d0903 1103retry:
1da177e4 1104 ret = check_kill_permission(sig, info, p);
e56d0903
IM
1105 if (!ret && sig && (sp = p->sighand)) {
1106 if (!get_task_struct_rcu(p))
1107 return -ESRCH;
1108 spin_lock_irqsave(&sp->siglock, flags);
1109 if (p->sighand != sp) {
1110 spin_unlock_irqrestore(&sp->siglock, flags);
1111 put_task_struct(p);
1112 goto retry;
1113 }
1da177e4 1114 ret = __group_send_sig_info(sig, info, p);
e56d0903
IM
1115 spin_unlock_irqrestore(&sp->siglock, flags);
1116 put_task_struct(p);
1da177e4
LT
1117 }
1118
1119 return ret;
1120}
1121
1122/*
1123 * kill_pg_info() sends a signal to a process group: this is what the tty
1124 * control characters do (^C, ^Z etc)
1125 */
1126
1127int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1128{
1129 struct task_struct *p = NULL;
1130 int retval, success;
1131
1132 if (pgrp <= 0)
1133 return -EINVAL;
1134
1135 success = 0;
1136 retval = -ESRCH;
1137 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1138 int err = group_send_sig_info(sig, info, p);
1139 success |= !err;
1140 retval = err;
1141 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1142 return success ? 0 : retval;
1143}
1144
1145int
1146kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1147{
1148 int retval;
1149
1150 read_lock(&tasklist_lock);
1151 retval = __kill_pg_info(sig, info, pgrp);
1152 read_unlock(&tasklist_lock);
1153
1154 return retval;
1155}
1156
1157int
1158kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1159{
1160 int error;
e56d0903 1161 int acquired_tasklist_lock = 0;
1da177e4
LT
1162 struct task_struct *p;
1163
e56d0903
IM
1164 rcu_read_lock();
1165 if (unlikely(sig_kernel_stop(sig) || sig == SIGCONT)) {
1166 read_lock(&tasklist_lock);
1167 acquired_tasklist_lock = 1;
1168 }
1da177e4
LT
1169 p = find_task_by_pid(pid);
1170 error = -ESRCH;
1171 if (p)
1172 error = group_send_sig_info(sig, info, p);
e56d0903
IM
1173 if (unlikely(acquired_tasklist_lock))
1174 read_unlock(&tasklist_lock);
1175 rcu_read_unlock();
1da177e4
LT
1176 return error;
1177}
1178
46113830
HW
1179/* like kill_proc_info(), but doesn't use uid/euid of "current" */
1180int kill_proc_info_as_uid(int sig, struct siginfo *info, pid_t pid,
1181 uid_t uid, uid_t euid)
1182{
1183 int ret = -EINVAL;
1184 struct task_struct *p;
1185
1186 if (!valid_signal(sig))
1187 return ret;
1188
1189 read_lock(&tasklist_lock);
1190 p = find_task_by_pid(pid);
1191 if (!p) {
1192 ret = -ESRCH;
1193 goto out_unlock;
1194 }
1195 if ((!info || ((unsigned long)info != 1 &&
1196 (unsigned long)info != 2 && SI_FROMUSER(info)))
1197 && (euid != p->suid) && (euid != p->uid)
1198 && (uid != p->suid) && (uid != p->uid)) {
1199 ret = -EPERM;
1200 goto out_unlock;
1201 }
1202 if (sig && p->sighand) {
1203 unsigned long flags;
1204 spin_lock_irqsave(&p->sighand->siglock, flags);
1205 ret = __group_send_sig_info(sig, info, p);
1206 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1207 }
1208out_unlock:
1209 read_unlock(&tasklist_lock);
1210 return ret;
1211}
1212EXPORT_SYMBOL_GPL(kill_proc_info_as_uid);
1da177e4
LT
1213
1214/*
1215 * kill_something_info() interprets pid in interesting ways just like kill(2).
1216 *
1217 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1218 * is probably wrong. Should make it like BSD or SYSV.
1219 */
1220
1221static int kill_something_info(int sig, struct siginfo *info, int pid)
1222{
1223 if (!pid) {
1224 return kill_pg_info(sig, info, process_group(current));
1225 } else if (pid == -1) {
1226 int retval = 0, count = 0;
1227 struct task_struct * p;
1228
1229 read_lock(&tasklist_lock);
1230 for_each_process(p) {
1231 if (p->pid > 1 && p->tgid != current->tgid) {
1232 int err = group_send_sig_info(sig, info, p);
1233 ++count;
1234 if (err != -EPERM)
1235 retval = err;
1236 }
1237 }
1238 read_unlock(&tasklist_lock);
1239 return count ? retval : -ESRCH;
1240 } else if (pid < 0) {
1241 return kill_pg_info(sig, info, -pid);
1242 } else {
1243 return kill_proc_info(sig, info, pid);
1244 }
1245}
1246
1247/*
1248 * These are for backward compatibility with the rest of the kernel source.
1249 */
1250
1251/*
1252 * These two are the most common entry points. They send a signal
1253 * just to the specific thread.
1254 */
1255int
1256send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1257{
1258 int ret;
1259 unsigned long flags;
1260
1261 /*
1262 * Make sure legacy kernel users don't send in bad values
1263 * (normal paths check this in check_kill_permission).
1264 */
7ed20e1a 1265 if (!valid_signal(sig))
1da177e4
LT
1266 return -EINVAL;
1267
1268 /*
1269 * We need the tasklist lock even for the specific
1270 * thread case (when we don't need to follow the group
1271 * lists) in order to avoid races with "p->sighand"
1272 * going away or changing from under us.
1273 */
1274 read_lock(&tasklist_lock);
1275 spin_lock_irqsave(&p->sighand->siglock, flags);
1276 ret = specific_send_sig_info(sig, info, p);
1277 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1278 read_unlock(&tasklist_lock);
1279 return ret;
1280}
1281
b67a1b9e
ON
1282#define __si_special(priv) \
1283 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1284
1da177e4
LT
1285int
1286send_sig(int sig, struct task_struct *p, int priv)
1287{
b67a1b9e 1288 return send_sig_info(sig, __si_special(priv), p);
1da177e4
LT
1289}
1290
1291/*
1292 * This is the entry point for "process-wide" signals.
1293 * They will go to an appropriate thread in the thread group.
1294 */
1295int
1296send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1297{
1298 int ret;
1299 read_lock(&tasklist_lock);
1300 ret = group_send_sig_info(sig, info, p);
1301 read_unlock(&tasklist_lock);
1302 return ret;
1303}
1304
1305void
1306force_sig(int sig, struct task_struct *p)
1307{
b67a1b9e 1308 force_sig_info(sig, SEND_SIG_PRIV, p);
1da177e4
LT
1309}
1310
1311/*
1312 * When things go south during signal handling, we
1313 * will force a SIGSEGV. And if the signal that caused
1314 * the problem was already a SIGSEGV, we'll want to
1315 * make sure we don't even try to deliver the signal..
1316 */
1317int
1318force_sigsegv(int sig, struct task_struct *p)
1319{
1320 if (sig == SIGSEGV) {
1321 unsigned long flags;
1322 spin_lock_irqsave(&p->sighand->siglock, flags);
1323 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1324 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1325 }
1326 force_sig(SIGSEGV, p);
1327 return 0;
1328}
1329
1330int
1331kill_pg(pid_t pgrp, int sig, int priv)
1332{
b67a1b9e 1333 return kill_pg_info(sig, __si_special(priv), pgrp);
1da177e4
LT
1334}
1335
1336int
1337kill_proc(pid_t pid, int sig, int priv)
1338{
b67a1b9e 1339 return kill_proc_info(sig, __si_special(priv), pid);
1da177e4
LT
1340}
1341
1342/*
1343 * These functions support sending signals using preallocated sigqueue
1344 * structures. This is needed "because realtime applications cannot
1345 * afford to lose notifications of asynchronous events, like timer
1346 * expirations or I/O completions". In the case of Posix Timers
1347 * we allocate the sigqueue structure from the timer_create. If this
1348 * allocation fails we are able to report the failure to the application
1349 * with an EAGAIN error.
1350 */
1351
1352struct sigqueue *sigqueue_alloc(void)
1353{
1354 struct sigqueue *q;
1355
1356 if ((q = __sigqueue_alloc(current, GFP_KERNEL, 0)))
1357 q->flags |= SIGQUEUE_PREALLOC;
1358 return(q);
1359}
1360
1361void sigqueue_free(struct sigqueue *q)
1362{
1363 unsigned long flags;
1364 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1365 /*
1366 * If the signal is still pending remove it from the
1367 * pending queue.
1368 */
1369 if (unlikely(!list_empty(&q->list))) {
19a4fcb5
ON
1370 spinlock_t *lock = &current->sighand->siglock;
1371 read_lock(&tasklist_lock);
1372 spin_lock_irqsave(lock, flags);
1da177e4
LT
1373 if (!list_empty(&q->list))
1374 list_del_init(&q->list);
19a4fcb5 1375 spin_unlock_irqrestore(lock, flags);
1da177e4
LT
1376 read_unlock(&tasklist_lock);
1377 }
1378 q->flags &= ~SIGQUEUE_PREALLOC;
1379 __sigqueue_free(q);
1380}
1381
1382int
1383send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1384{
1385 unsigned long flags;
1386 int ret = 0;
e56d0903 1387 struct sighand_struct *sh;
1da177e4 1388
1da177e4 1389 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
e56d0903
IM
1390
1391 /*
1392 * The rcu based delayed sighand destroy makes it possible to
1393 * run this without tasklist lock held. The task struct itself
1394 * cannot go away as create_timer did get_task_struct().
1395 *
1396 * We return -1, when the task is marked exiting, so
1397 * posix_timer_event can redirect it to the group leader
1398 */
1399 rcu_read_lock();
e752dd6c
ON
1400
1401 if (unlikely(p->flags & PF_EXITING)) {
1402 ret = -1;
1403 goto out_err;
1404 }
1405
e56d0903
IM
1406retry:
1407 sh = rcu_dereference(p->sighand);
1408
1409 spin_lock_irqsave(&sh->siglock, flags);
1410 if (p->sighand != sh) {
1411 /* We raced with exec() in a multithreaded process... */
1412 spin_unlock_irqrestore(&sh->siglock, flags);
1413 goto retry;
1414 }
1415
1416 /*
1417 * We do the check here again to handle the following scenario:
1418 *
1419 * CPU 0 CPU 1
1420 * send_sigqueue
1421 * check PF_EXITING
1422 * interrupt exit code running
1423 * __exit_signal
1424 * lock sighand->siglock
1425 * unlock sighand->siglock
1426 * lock sh->siglock
1427 * add(tsk->pending) flush_sigqueue(tsk->pending)
1428 *
1429 */
1430
1431 if (unlikely(p->flags & PF_EXITING)) {
1432 ret = -1;
1433 goto out;
1434 }
e752dd6c 1435
1da177e4
LT
1436 if (unlikely(!list_empty(&q->list))) {
1437 /*
1438 * If an SI_TIMER entry is already queue just increment
1439 * the overrun count.
1440 */
1441 if (q->info.si_code != SI_TIMER)
1442 BUG();
1443 q->info.si_overrun++;
1444 goto out;
e752dd6c 1445 }
1da177e4
LT
1446 /* Short-circuit ignored signals. */
1447 if (sig_ignored(p, sig)) {
1448 ret = 1;
1449 goto out;
1450 }
1451
1da177e4
LT
1452 list_add_tail(&q->list, &p->pending.list);
1453 sigaddset(&p->pending.signal, sig);
1454 if (!sigismember(&p->blocked, sig))
1455 signal_wake_up(p, sig == SIGKILL);
1456
1457out:
e56d0903 1458 spin_unlock_irqrestore(&sh->siglock, flags);
e752dd6c 1459out_err:
e56d0903 1460 rcu_read_unlock();
e752dd6c
ON
1461
1462 return ret;
1da177e4
LT
1463}
1464
1465int
1466send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1467{
1468 unsigned long flags;
1469 int ret = 0;
1470
1471 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
e56d0903 1472
1da177e4 1473 read_lock(&tasklist_lock);
e56d0903 1474 /* Since it_lock is held, p->sighand cannot be NULL. */
1da177e4
LT
1475 spin_lock_irqsave(&p->sighand->siglock, flags);
1476 handle_stop_signal(sig, p);
1477
1478 /* Short-circuit ignored signals. */
1479 if (sig_ignored(p, sig)) {
1480 ret = 1;
1481 goto out;
1482 }
1483
1484 if (unlikely(!list_empty(&q->list))) {
1485 /*
1486 * If an SI_TIMER entry is already queue just increment
1487 * the overrun count. Other uses should not try to
1488 * send the signal multiple times.
1489 */
1490 if (q->info.si_code != SI_TIMER)
1491 BUG();
1492 q->info.si_overrun++;
1493 goto out;
1494 }
1495
1496 /*
1497 * Put this signal on the shared-pending queue.
1498 * We always use the shared queue for process-wide signals,
1499 * to avoid several races.
1500 */
1da177e4
LT
1501 list_add_tail(&q->list, &p->signal->shared_pending.list);
1502 sigaddset(&p->signal->shared_pending.signal, sig);
1503
1504 __group_complete_signal(sig, p);
1505out:
1506 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1507 read_unlock(&tasklist_lock);
e56d0903 1508 return ret;
1da177e4
LT
1509}
1510
1511/*
1512 * Wake up any threads in the parent blocked in wait* syscalls.
1513 */
1514static inline void __wake_up_parent(struct task_struct *p,
1515 struct task_struct *parent)
1516{
1517 wake_up_interruptible_sync(&parent->signal->wait_chldexit);
1518}
1519
1520/*
1521 * Let a parent know about the death of a child.
1522 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
1523 */
1524
1525void do_notify_parent(struct task_struct *tsk, int sig)
1526{
1527 struct siginfo info;
1528 unsigned long flags;
1529 struct sighand_struct *psig;
1530
1531 BUG_ON(sig == -1);
1532
1533 /* do_notify_parent_cldstop should have been called instead. */
1534 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1535
1536 BUG_ON(!tsk->ptrace &&
1537 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1538
1539 info.si_signo = sig;
1540 info.si_errno = 0;
1541 info.si_pid = tsk->pid;
1542 info.si_uid = tsk->uid;
1543
1544 /* FIXME: find out whether or not this is supposed to be c*time. */
1545 info.si_utime = cputime_to_jiffies(cputime_add(tsk->utime,
1546 tsk->signal->utime));
1547 info.si_stime = cputime_to_jiffies(cputime_add(tsk->stime,
1548 tsk->signal->stime));
1549
1550 info.si_status = tsk->exit_code & 0x7f;
1551 if (tsk->exit_code & 0x80)
1552 info.si_code = CLD_DUMPED;
1553 else if (tsk->exit_code & 0x7f)
1554 info.si_code = CLD_KILLED;
1555 else {
1556 info.si_code = CLD_EXITED;
1557 info.si_status = tsk->exit_code >> 8;
1558 }
1559
1560 psig = tsk->parent->sighand;
1561 spin_lock_irqsave(&psig->siglock, flags);
7ed0175a 1562 if (!tsk->ptrace && sig == SIGCHLD &&
1da177e4
LT
1563 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1564 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1565 /*
1566 * We are exiting and our parent doesn't care. POSIX.1
1567 * defines special semantics for setting SIGCHLD to SIG_IGN
1568 * or setting the SA_NOCLDWAIT flag: we should be reaped
1569 * automatically and not left for our parent's wait4 call.
1570 * Rather than having the parent do it as a magic kind of
1571 * signal handler, we just set this to tell do_exit that we
1572 * can be cleaned up without becoming a zombie. Note that
1573 * we still call __wake_up_parent in this case, because a
1574 * blocked sys_wait4 might now return -ECHILD.
1575 *
1576 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1577 * is implementation-defined: we do (if you don't want
1578 * it, just use SIG_IGN instead).
1579 */
1580 tsk->exit_signal = -1;
1581 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1582 sig = 0;
1583 }
7ed20e1a 1584 if (valid_signal(sig) && sig > 0)
1da177e4
LT
1585 __group_send_sig_info(sig, &info, tsk->parent);
1586 __wake_up_parent(tsk, tsk->parent);
1587 spin_unlock_irqrestore(&psig->siglock, flags);
1588}
1589
bc505a47 1590static void do_notify_parent_cldstop(struct task_struct *tsk, int to_self, int why)
1da177e4
LT
1591{
1592 struct siginfo info;
1593 unsigned long flags;
bc505a47 1594 struct task_struct *parent;
1da177e4
LT
1595 struct sighand_struct *sighand;
1596
bc505a47
ON
1597 if (to_self)
1598 parent = tsk->parent;
1599 else {
1600 tsk = tsk->group_leader;
1601 parent = tsk->real_parent;
1602 }
1603
1da177e4
LT
1604 info.si_signo = SIGCHLD;
1605 info.si_errno = 0;
1606 info.si_pid = tsk->pid;
1607 info.si_uid = tsk->uid;
1608
1609 /* FIXME: find out whether or not this is supposed to be c*time. */
1610 info.si_utime = cputime_to_jiffies(tsk->utime);
1611 info.si_stime = cputime_to_jiffies(tsk->stime);
1612
1613 info.si_code = why;
1614 switch (why) {
1615 case CLD_CONTINUED:
1616 info.si_status = SIGCONT;
1617 break;
1618 case CLD_STOPPED:
1619 info.si_status = tsk->signal->group_exit_code & 0x7f;
1620 break;
1621 case CLD_TRAPPED:
1622 info.si_status = tsk->exit_code & 0x7f;
1623 break;
1624 default:
1625 BUG();
1626 }
1627
1628 sighand = parent->sighand;
1629 spin_lock_irqsave(&sighand->siglock, flags);
1630 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1631 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1632 __group_send_sig_info(SIGCHLD, &info, parent);
1633 /*
1634 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1635 */
1636 __wake_up_parent(tsk, parent);
1637 spin_unlock_irqrestore(&sighand->siglock, flags);
1638}
1639
1640/*
1641 * This must be called with current->sighand->siglock held.
1642 *
1643 * This should be the path for all ptrace stops.
1644 * We always set current->last_siginfo while stopped here.
1645 * That makes it a way to test a stopped process for
1646 * being ptrace-stopped vs being job-control-stopped.
1647 *
1648 * If we actually decide not to stop at all because the tracer is gone,
1649 * we leave nostop_code in current->exit_code.
1650 */
1651static void ptrace_stop(int exit_code, int nostop_code, siginfo_t *info)
1652{
1653 /*
1654 * If there is a group stop in progress,
1655 * we must participate in the bookkeeping.
1656 */
1657 if (current->signal->group_stop_count > 0)
1658 --current->signal->group_stop_count;
1659
1660 current->last_siginfo = info;
1661 current->exit_code = exit_code;
1662
1663 /* Let the debugger run. */
1664 set_current_state(TASK_TRACED);
1665 spin_unlock_irq(&current->sighand->siglock);
1666 read_lock(&tasklist_lock);
1667 if (likely(current->ptrace & PT_PTRACED) &&
1668 likely(current->parent != current->real_parent ||
1669 !(current->ptrace & PT_ATTACHED)) &&
1670 (likely(current->parent->signal != current->signal) ||
1671 !unlikely(current->signal->flags & SIGNAL_GROUP_EXIT))) {
bc505a47 1672 do_notify_parent_cldstop(current, 1, CLD_TRAPPED);
1da177e4
LT
1673 read_unlock(&tasklist_lock);
1674 schedule();
1675 } else {
1676 /*
1677 * By the time we got the lock, our tracer went away.
1678 * Don't stop here.
1679 */
1680 read_unlock(&tasklist_lock);
1681 set_current_state(TASK_RUNNING);
1682 current->exit_code = nostop_code;
1683 }
1684
1685 /*
1686 * We are back. Now reacquire the siglock before touching
1687 * last_siginfo, so that we are sure to have synchronized with
1688 * any signal-sending on another CPU that wants to examine it.
1689 */
1690 spin_lock_irq(&current->sighand->siglock);
1691 current->last_siginfo = NULL;
1692
1693 /*
1694 * Queued signals ignored us while we were stopped for tracing.
1695 * So check for any that we should take before resuming user mode.
1696 */
1697 recalc_sigpending();
1698}
1699
1700void ptrace_notify(int exit_code)
1701{
1702 siginfo_t info;
1703
1704 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1705
1706 memset(&info, 0, sizeof info);
1707 info.si_signo = SIGTRAP;
1708 info.si_code = exit_code;
1709 info.si_pid = current->pid;
1710 info.si_uid = current->uid;
1711
1712 /* Let the debugger run. */
1713 spin_lock_irq(&current->sighand->siglock);
1714 ptrace_stop(exit_code, 0, &info);
1715 spin_unlock_irq(&current->sighand->siglock);
1716}
1717
1da177e4
LT
1718static void
1719finish_stop(int stop_count)
1720{
bc505a47
ON
1721 int to_self;
1722
1da177e4
LT
1723 /*
1724 * If there are no other threads in the group, or if there is
1725 * a group stop in progress and we are the last to stop,
1726 * report to the parent. When ptraced, every thread reports itself.
1727 */
bc505a47
ON
1728 if (stop_count < 0 || (current->ptrace & PT_PTRACED))
1729 to_self = 1;
1730 else if (stop_count == 0)
1731 to_self = 0;
1732 else
1733 goto out;
1da177e4 1734
bc505a47
ON
1735 read_lock(&tasklist_lock);
1736 do_notify_parent_cldstop(current, to_self, CLD_STOPPED);
1737 read_unlock(&tasklist_lock);
1738
1739out:
1da177e4
LT
1740 schedule();
1741 /*
1742 * Now we don't run again until continued.
1743 */
1744 current->exit_code = 0;
1745}
1746
1747/*
1748 * This performs the stopping for SIGSTOP and other stop signals.
1749 * We have to stop all threads in the thread group.
1750 * Returns nonzero if we've actually stopped and released the siglock.
1751 * Returns zero if we didn't stop and still hold the siglock.
1752 */
1753static int
1754do_signal_stop(int signr)
1755{
1756 struct signal_struct *sig = current->signal;
1757 struct sighand_struct *sighand = current->sighand;
1758 int stop_count = -1;
1759
1760 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED))
1761 return 0;
1762
1763 if (sig->group_stop_count > 0) {
1764 /*
1765 * There is a group stop in progress. We don't need to
1766 * start another one.
1767 */
1768 signr = sig->group_exit_code;
1769 stop_count = --sig->group_stop_count;
1770 current->exit_code = signr;
1771 set_current_state(TASK_STOPPED);
1772 if (stop_count == 0)
1773 sig->flags = SIGNAL_STOP_STOPPED;
1774 spin_unlock_irq(&sighand->siglock);
1775 }
1776 else if (thread_group_empty(current)) {
1777 /*
1778 * Lock must be held through transition to stopped state.
1779 */
1780 current->exit_code = current->signal->group_exit_code = signr;
1781 set_current_state(TASK_STOPPED);
1782 sig->flags = SIGNAL_STOP_STOPPED;
1783 spin_unlock_irq(&sighand->siglock);
1784 }
1785 else {
1786 /*
1787 * There is no group stop already in progress.
1788 * We must initiate one now, but that requires
1789 * dropping siglock to get both the tasklist lock
1790 * and siglock again in the proper order. Note that
1791 * this allows an intervening SIGCONT to be posted.
1792 * We need to check for that and bail out if necessary.
1793 */
1794 struct task_struct *t;
1795
1796 spin_unlock_irq(&sighand->siglock);
1797
1798 /* signals can be posted during this window */
1799
1800 read_lock(&tasklist_lock);
1801 spin_lock_irq(&sighand->siglock);
1802
1803 if (!likely(sig->flags & SIGNAL_STOP_DEQUEUED)) {
1804 /*
1805 * Another stop or continue happened while we
1806 * didn't have the lock. We can just swallow this
1807 * signal now. If we raced with a SIGCONT, that
1808 * should have just cleared it now. If we raced
1809 * with another processor delivering a stop signal,
1810 * then the SIGCONT that wakes us up should clear it.
1811 */
1812 read_unlock(&tasklist_lock);
1813 return 0;
1814 }
1815
1816 if (sig->group_stop_count == 0) {
1817 sig->group_exit_code = signr;
1818 stop_count = 0;
1819 for (t = next_thread(current); t != current;
1820 t = next_thread(t))
1821 /*
1822 * Setting state to TASK_STOPPED for a group
1823 * stop is always done with the siglock held,
1824 * so this check has no races.
1825 */
5acbc5cb
RM
1826 if (!t->exit_state &&
1827 !(t->state & (TASK_STOPPED|TASK_TRACED))) {
1da177e4
LT
1828 stop_count++;
1829 signal_wake_up(t, 0);
1830 }
1831 sig->group_stop_count = stop_count;
1832 }
1833 else {
1834 /* A race with another thread while unlocked. */
1835 signr = sig->group_exit_code;
1836 stop_count = --sig->group_stop_count;
1837 }
1838
1839 current->exit_code = signr;
1840 set_current_state(TASK_STOPPED);
1841 if (stop_count == 0)
1842 sig->flags = SIGNAL_STOP_STOPPED;
1843
1844 spin_unlock_irq(&sighand->siglock);
1845 read_unlock(&tasklist_lock);
1846 }
1847
1848 finish_stop(stop_count);
1849 return 1;
1850}
1851
1852/*
1853 * Do appropriate magic when group_stop_count > 0.
1854 * We return nonzero if we stopped, after releasing the siglock.
1855 * We return zero if we still hold the siglock and should look
1856 * for another signal without checking group_stop_count again.
1857 */
1858static inline int handle_group_stop(void)
1859{
1860 int stop_count;
1861
1862 if (current->signal->group_exit_task == current) {
1863 /*
1864 * Group stop is so we can do a core dump,
1865 * We are the initiating thread, so get on with it.
1866 */
1867 current->signal->group_exit_task = NULL;
1868 return 0;
1869 }
1870
1871 if (current->signal->flags & SIGNAL_GROUP_EXIT)
1872 /*
1873 * Group stop is so another thread can do a core dump,
1874 * or else we are racing against a death signal.
1875 * Just punt the stop so we can get the next signal.
1876 */
1877 return 0;
1878
1879 /*
1880 * There is a group stop in progress. We stop
1881 * without any associated signal being in our queue.
1882 */
1883 stop_count = --current->signal->group_stop_count;
1884 if (stop_count == 0)
1885 current->signal->flags = SIGNAL_STOP_STOPPED;
1886 current->exit_code = current->signal->group_exit_code;
1887 set_current_state(TASK_STOPPED);
1888 spin_unlock_irq(&current->sighand->siglock);
1889 finish_stop(stop_count);
1890 return 1;
1891}
1892
1893int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1894 struct pt_regs *regs, void *cookie)
1895{
1896 sigset_t *mask = &current->blocked;
1897 int signr = 0;
1898
1899relock:
1900 spin_lock_irq(&current->sighand->siglock);
1901 for (;;) {
1902 struct k_sigaction *ka;
1903
1904 if (unlikely(current->signal->group_stop_count > 0) &&
1905 handle_group_stop())
1906 goto relock;
1907
1908 signr = dequeue_signal(current, mask, info);
1909
1910 if (!signr)
1911 break; /* will return 0 */
1912
1913 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1914 ptrace_signal_deliver(regs, cookie);
1915
1916 /* Let the debugger run. */
1917 ptrace_stop(signr, signr, info);
1918
30e0fca6 1919 /* We're back. Did the debugger cancel the sig or group_exit? */
1da177e4 1920 signr = current->exit_code;
30e0fca6 1921 if (signr == 0 || current->signal->flags & SIGNAL_GROUP_EXIT)
1da177e4
LT
1922 continue;
1923
1924 current->exit_code = 0;
1925
1926 /* Update the siginfo structure if the signal has
1927 changed. If the debugger wanted something
1928 specific in the siginfo structure then it should
1929 have updated *info via PTRACE_SETSIGINFO. */
1930 if (signr != info->si_signo) {
1931 info->si_signo = signr;
1932 info->si_errno = 0;
1933 info->si_code = SI_USER;
1934 info->si_pid = current->parent->pid;
1935 info->si_uid = current->parent->uid;
1936 }
1937
1938 /* If the (new) signal is now blocked, requeue it. */
1939 if (sigismember(&current->blocked, signr)) {
1940 specific_send_sig_info(signr, info, current);
1941 continue;
1942 }
1943 }
1944
1945 ka = &current->sighand->action[signr-1];
1946 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
1947 continue;
1948 if (ka->sa.sa_handler != SIG_DFL) {
1949 /* Run the handler. */
1950 *return_ka = *ka;
1951
1952 if (ka->sa.sa_flags & SA_ONESHOT)
1953 ka->sa.sa_handler = SIG_DFL;
1954
1955 break; /* will return non-zero "signr" value */
1956 }
1957
1958 /*
1959 * Now we are doing the default action for this signal.
1960 */
1961 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1962 continue;
1963
1964 /* Init gets no signals it doesn't want. */
1965 if (current->pid == 1)
1966 continue;
1967
1968 if (sig_kernel_stop(signr)) {
1969 /*
1970 * The default action is to stop all threads in
1971 * the thread group. The job control signals
1972 * do nothing in an orphaned pgrp, but SIGSTOP
1973 * always works. Note that siglock needs to be
1974 * dropped during the call to is_orphaned_pgrp()
1975 * because of lock ordering with tasklist_lock.
1976 * This allows an intervening SIGCONT to be posted.
1977 * We need to check for that and bail out if necessary.
1978 */
1979 if (signr != SIGSTOP) {
1980 spin_unlock_irq(&current->sighand->siglock);
1981
1982 /* signals can be posted during this window */
1983
1984 if (is_orphaned_pgrp(process_group(current)))
1985 goto relock;
1986
1987 spin_lock_irq(&current->sighand->siglock);
1988 }
1989
1990 if (likely(do_signal_stop(signr))) {
1991 /* It released the siglock. */
1992 goto relock;
1993 }
1994
1995 /*
1996 * We didn't actually stop, due to a race
1997 * with SIGCONT or something like that.
1998 */
1999 continue;
2000 }
2001
2002 spin_unlock_irq(&current->sighand->siglock);
2003
2004 /*
2005 * Anything else is fatal, maybe with a core dump.
2006 */
2007 current->flags |= PF_SIGNALED;
2008 if (sig_kernel_coredump(signr)) {
2009 /*
2010 * If it was able to dump core, this kills all
2011 * other threads in the group and synchronizes with
2012 * their demise. If we lost the race with another
2013 * thread getting here, it set group_exit_code
2014 * first and our do_group_exit call below will use
2015 * that value and ignore the one we pass it.
2016 */
2017 do_coredump((long)signr, signr, regs);
2018 }
2019
2020 /*
2021 * Death signals, no core dump.
2022 */
2023 do_group_exit(signr);
2024 /* NOTREACHED */
2025 }
2026 spin_unlock_irq(&current->sighand->siglock);
2027 return signr;
2028}
2029
1da177e4
LT
2030EXPORT_SYMBOL(recalc_sigpending);
2031EXPORT_SYMBOL_GPL(dequeue_signal);
2032EXPORT_SYMBOL(flush_signals);
2033EXPORT_SYMBOL(force_sig);
2034EXPORT_SYMBOL(kill_pg);
2035EXPORT_SYMBOL(kill_proc);
2036EXPORT_SYMBOL(ptrace_notify);
2037EXPORT_SYMBOL(send_sig);
2038EXPORT_SYMBOL(send_sig_info);
2039EXPORT_SYMBOL(sigprocmask);
2040EXPORT_SYMBOL(block_all_signals);
2041EXPORT_SYMBOL(unblock_all_signals);
2042
2043
2044/*
2045 * System call entry points.
2046 */
2047
2048asmlinkage long sys_restart_syscall(void)
2049{
2050 struct restart_block *restart = &current_thread_info()->restart_block;
2051 return restart->fn(restart);
2052}
2053
2054long do_no_restart_syscall(struct restart_block *param)
2055{
2056 return -EINTR;
2057}
2058
2059/*
2060 * We don't need to get the kernel lock - this is all local to this
2061 * particular thread.. (and that's good, because this is _heavily_
2062 * used by various programs)
2063 */
2064
2065/*
2066 * This is also useful for kernel threads that want to temporarily
2067 * (or permanently) block certain signals.
2068 *
2069 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2070 * interface happily blocks "unblockable" signals like SIGKILL
2071 * and friends.
2072 */
2073int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2074{
2075 int error;
2076 sigset_t old_block;
2077
2078 spin_lock_irq(&current->sighand->siglock);
2079 old_block = current->blocked;
2080 error = 0;
2081 switch (how) {
2082 case SIG_BLOCK:
2083 sigorsets(&current->blocked, &current->blocked, set);
2084 break;
2085 case SIG_UNBLOCK:
2086 signandsets(&current->blocked, &current->blocked, set);
2087 break;
2088 case SIG_SETMASK:
2089 current->blocked = *set;
2090 break;
2091 default:
2092 error = -EINVAL;
2093 }
2094 recalc_sigpending();
2095 spin_unlock_irq(&current->sighand->siglock);
2096 if (oldset)
2097 *oldset = old_block;
2098 return error;
2099}
2100
2101asmlinkage long
2102sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2103{
2104 int error = -EINVAL;
2105 sigset_t old_set, new_set;
2106
2107 /* XXX: Don't preclude handling different sized sigset_t's. */
2108 if (sigsetsize != sizeof(sigset_t))
2109 goto out;
2110
2111 if (set) {
2112 error = -EFAULT;
2113 if (copy_from_user(&new_set, set, sizeof(*set)))
2114 goto out;
2115 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2116
2117 error = sigprocmask(how, &new_set, &old_set);
2118 if (error)
2119 goto out;
2120 if (oset)
2121 goto set_old;
2122 } else if (oset) {
2123 spin_lock_irq(&current->sighand->siglock);
2124 old_set = current->blocked;
2125 spin_unlock_irq(&current->sighand->siglock);
2126
2127 set_old:
2128 error = -EFAULT;
2129 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2130 goto out;
2131 }
2132 error = 0;
2133out:
2134 return error;
2135}
2136
2137long do_sigpending(void __user *set, unsigned long sigsetsize)
2138{
2139 long error = -EINVAL;
2140 sigset_t pending;
2141
2142 if (sigsetsize > sizeof(sigset_t))
2143 goto out;
2144
2145 spin_lock_irq(&current->sighand->siglock);
2146 sigorsets(&pending, &current->pending.signal,
2147 &current->signal->shared_pending.signal);
2148 spin_unlock_irq(&current->sighand->siglock);
2149
2150 /* Outside the lock because only this thread touches it. */
2151 sigandsets(&pending, &current->blocked, &pending);
2152
2153 error = -EFAULT;
2154 if (!copy_to_user(set, &pending, sigsetsize))
2155 error = 0;
2156
2157out:
2158 return error;
2159}
2160
2161asmlinkage long
2162sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2163{
2164 return do_sigpending(set, sigsetsize);
2165}
2166
2167#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2168
2169int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2170{
2171 int err;
2172
2173 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2174 return -EFAULT;
2175 if (from->si_code < 0)
2176 return __copy_to_user(to, from, sizeof(siginfo_t))
2177 ? -EFAULT : 0;
2178 /*
2179 * If you change siginfo_t structure, please be sure
2180 * this code is fixed accordingly.
2181 * It should never copy any pad contained in the structure
2182 * to avoid security leaks, but must copy the generic
2183 * 3 ints plus the relevant union member.
2184 */
2185 err = __put_user(from->si_signo, &to->si_signo);
2186 err |= __put_user(from->si_errno, &to->si_errno);
2187 err |= __put_user((short)from->si_code, &to->si_code);
2188 switch (from->si_code & __SI_MASK) {
2189 case __SI_KILL:
2190 err |= __put_user(from->si_pid, &to->si_pid);
2191 err |= __put_user(from->si_uid, &to->si_uid);
2192 break;
2193 case __SI_TIMER:
2194 err |= __put_user(from->si_tid, &to->si_tid);
2195 err |= __put_user(from->si_overrun, &to->si_overrun);
2196 err |= __put_user(from->si_ptr, &to->si_ptr);
2197 break;
2198 case __SI_POLL:
2199 err |= __put_user(from->si_band, &to->si_band);
2200 err |= __put_user(from->si_fd, &to->si_fd);
2201 break;
2202 case __SI_FAULT:
2203 err |= __put_user(from->si_addr, &to->si_addr);
2204#ifdef __ARCH_SI_TRAPNO
2205 err |= __put_user(from->si_trapno, &to->si_trapno);
2206#endif
2207 break;
2208 case __SI_CHLD:
2209 err |= __put_user(from->si_pid, &to->si_pid);
2210 err |= __put_user(from->si_uid, &to->si_uid);
2211 err |= __put_user(from->si_status, &to->si_status);
2212 err |= __put_user(from->si_utime, &to->si_utime);
2213 err |= __put_user(from->si_stime, &to->si_stime);
2214 break;
2215 case __SI_RT: /* This is not generated by the kernel as of now. */
2216 case __SI_MESGQ: /* But this is */
2217 err |= __put_user(from->si_pid, &to->si_pid);
2218 err |= __put_user(from->si_uid, &to->si_uid);
2219 err |= __put_user(from->si_ptr, &to->si_ptr);
2220 break;
2221 default: /* this is just in case for now ... */
2222 err |= __put_user(from->si_pid, &to->si_pid);
2223 err |= __put_user(from->si_uid, &to->si_uid);
2224 break;
2225 }
2226 return err;
2227}
2228
2229#endif
2230
2231asmlinkage long
2232sys_rt_sigtimedwait(const sigset_t __user *uthese,
2233 siginfo_t __user *uinfo,
2234 const struct timespec __user *uts,
2235 size_t sigsetsize)
2236{
2237 int ret, sig;
2238 sigset_t these;
2239 struct timespec ts;
2240 siginfo_t info;
2241 long timeout = 0;
2242
2243 /* XXX: Don't preclude handling different sized sigset_t's. */
2244 if (sigsetsize != sizeof(sigset_t))
2245 return -EINVAL;
2246
2247 if (copy_from_user(&these, uthese, sizeof(these)))
2248 return -EFAULT;
2249
2250 /*
2251 * Invert the set of allowed signals to get those we
2252 * want to block.
2253 */
2254 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2255 signotset(&these);
2256
2257 if (uts) {
2258 if (copy_from_user(&ts, uts, sizeof(ts)))
2259 return -EFAULT;
2260 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2261 || ts.tv_sec < 0)
2262 return -EINVAL;
2263 }
2264
2265 spin_lock_irq(&current->sighand->siglock);
2266 sig = dequeue_signal(current, &these, &info);
2267 if (!sig) {
2268 timeout = MAX_SCHEDULE_TIMEOUT;
2269 if (uts)
2270 timeout = (timespec_to_jiffies(&ts)
2271 + (ts.tv_sec || ts.tv_nsec));
2272
2273 if (timeout) {
2274 /* None ready -- temporarily unblock those we're
2275 * interested while we are sleeping in so that we'll
2276 * be awakened when they arrive. */
2277 current->real_blocked = current->blocked;
2278 sigandsets(&current->blocked, &current->blocked, &these);
2279 recalc_sigpending();
2280 spin_unlock_irq(&current->sighand->siglock);
2281
75bcc8c5 2282 timeout = schedule_timeout_interruptible(timeout);
1da177e4 2283
3e1d1d28 2284 try_to_freeze();
1da177e4
LT
2285 spin_lock_irq(&current->sighand->siglock);
2286 sig = dequeue_signal(current, &these, &info);
2287 current->blocked = current->real_blocked;
2288 siginitset(&current->real_blocked, 0);
2289 recalc_sigpending();
2290 }
2291 }
2292 spin_unlock_irq(&current->sighand->siglock);
2293
2294 if (sig) {
2295 ret = sig;
2296 if (uinfo) {
2297 if (copy_siginfo_to_user(uinfo, &info))
2298 ret = -EFAULT;
2299 }
2300 } else {
2301 ret = -EAGAIN;
2302 if (timeout)
2303 ret = -EINTR;
2304 }
2305
2306 return ret;
2307}
2308
2309asmlinkage long
2310sys_kill(int pid, int sig)
2311{
2312 struct siginfo info;
2313
2314 info.si_signo = sig;
2315 info.si_errno = 0;
2316 info.si_code = SI_USER;
2317 info.si_pid = current->tgid;
2318 info.si_uid = current->uid;
2319
2320 return kill_something_info(sig, &info, pid);
2321}
2322
6dd69f10 2323static int do_tkill(int tgid, int pid, int sig)
1da177e4 2324{
1da177e4 2325 int error;
6dd69f10 2326 struct siginfo info;
1da177e4
LT
2327 struct task_struct *p;
2328
6dd69f10 2329 error = -ESRCH;
1da177e4
LT
2330 info.si_signo = sig;
2331 info.si_errno = 0;
2332 info.si_code = SI_TKILL;
2333 info.si_pid = current->tgid;
2334 info.si_uid = current->uid;
2335
2336 read_lock(&tasklist_lock);
2337 p = find_task_by_pid(pid);
6dd69f10 2338 if (p && (tgid <= 0 || p->tgid == tgid)) {
1da177e4
LT
2339 error = check_kill_permission(sig, &info, p);
2340 /*
2341 * The null signal is a permissions and process existence
2342 * probe. No signal is actually delivered.
2343 */
2344 if (!error && sig && p->sighand) {
2345 spin_lock_irq(&p->sighand->siglock);
2346 handle_stop_signal(sig, p);
2347 error = specific_send_sig_info(sig, &info, p);
2348 spin_unlock_irq(&p->sighand->siglock);
2349 }
2350 }
2351 read_unlock(&tasklist_lock);
6dd69f10 2352
1da177e4
LT
2353 return error;
2354}
2355
6dd69f10
VL
2356/**
2357 * sys_tgkill - send signal to one specific thread
2358 * @tgid: the thread group ID of the thread
2359 * @pid: the PID of the thread
2360 * @sig: signal to be sent
2361 *
2362 * This syscall also checks the tgid and returns -ESRCH even if the PID
2363 * exists but it's not belonging to the target process anymore. This
2364 * method solves the problem of threads exiting and PIDs getting reused.
2365 */
2366asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2367{
2368 /* This is only valid for single tasks */
2369 if (pid <= 0 || tgid <= 0)
2370 return -EINVAL;
2371
2372 return do_tkill(tgid, pid, sig);
2373}
2374
1da177e4
LT
2375/*
2376 * Send a signal to only one task, even if it's a CLONE_THREAD task.
2377 */
2378asmlinkage long
2379sys_tkill(int pid, int sig)
2380{
1da177e4
LT
2381 /* This is only valid for single tasks */
2382 if (pid <= 0)
2383 return -EINVAL;
2384
6dd69f10 2385 return do_tkill(0, pid, sig);
1da177e4
LT
2386}
2387
2388asmlinkage long
2389sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2390{
2391 siginfo_t info;
2392
2393 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2394 return -EFAULT;
2395
2396 /* Not even root can pretend to send signals from the kernel.
2397 Nor can they impersonate a kill(), which adds source info. */
2398 if (info.si_code >= 0)
2399 return -EPERM;
2400 info.si_signo = sig;
2401
2402 /* POSIX.1b doesn't mention process groups. */
2403 return kill_proc_info(sig, &info, pid);
2404}
2405
2406int
2407do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2408{
2409 struct k_sigaction *k;
2410
7ed20e1a 2411 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
1da177e4
LT
2412 return -EINVAL;
2413
2414 k = &current->sighand->action[sig-1];
2415
2416 spin_lock_irq(&current->sighand->siglock);
2417 if (signal_pending(current)) {
2418 /*
2419 * If there might be a fatal signal pending on multiple
2420 * threads, make sure we take it before changing the action.
2421 */
2422 spin_unlock_irq(&current->sighand->siglock);
2423 return -ERESTARTNOINTR;
2424 }
2425
2426 if (oact)
2427 *oact = *k;
2428
2429 if (act) {
2430 /*
2431 * POSIX 3.3.1.3:
2432 * "Setting a signal action to SIG_IGN for a signal that is
2433 * pending shall cause the pending signal to be discarded,
2434 * whether or not it is blocked."
2435 *
2436 * "Setting a signal action to SIG_DFL for a signal that is
2437 * pending and whose default action is to ignore the signal
2438 * (for example, SIGCHLD), shall cause the pending signal to
2439 * be discarded, whether or not it is blocked"
2440 */
2441 if (act->sa.sa_handler == SIG_IGN ||
2442 (act->sa.sa_handler == SIG_DFL &&
2443 sig_kernel_ignore(sig))) {
2444 /*
2445 * This is a fairly rare case, so we only take the
2446 * tasklist_lock once we're sure we'll need it.
2447 * Now we must do this little unlock and relock
2448 * dance to maintain the lock hierarchy.
2449 */
2450 struct task_struct *t = current;
2451 spin_unlock_irq(&t->sighand->siglock);
2452 read_lock(&tasklist_lock);
2453 spin_lock_irq(&t->sighand->siglock);
2454 *k = *act;
2455 sigdelsetmask(&k->sa.sa_mask,
2456 sigmask(SIGKILL) | sigmask(SIGSTOP));
2457 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2458 do {
2459 rm_from_queue(sigmask(sig), &t->pending);
2460 recalc_sigpending_tsk(t);
2461 t = next_thread(t);
2462 } while (t != current);
2463 spin_unlock_irq(&current->sighand->siglock);
2464 read_unlock(&tasklist_lock);
2465 return 0;
2466 }
2467
2468 *k = *act;
2469 sigdelsetmask(&k->sa.sa_mask,
2470 sigmask(SIGKILL) | sigmask(SIGSTOP));
2471 }
2472
2473 spin_unlock_irq(&current->sighand->siglock);
2474 return 0;
2475}
2476
2477int
2478do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2479{
2480 stack_t oss;
2481 int error;
2482
2483 if (uoss) {
2484 oss.ss_sp = (void __user *) current->sas_ss_sp;
2485 oss.ss_size = current->sas_ss_size;
2486 oss.ss_flags = sas_ss_flags(sp);
2487 }
2488
2489 if (uss) {
2490 void __user *ss_sp;
2491 size_t ss_size;
2492 int ss_flags;
2493
2494 error = -EFAULT;
2495 if (!access_ok(VERIFY_READ, uss, sizeof(*uss))
2496 || __get_user(ss_sp, &uss->ss_sp)
2497 || __get_user(ss_flags, &uss->ss_flags)
2498 || __get_user(ss_size, &uss->ss_size))
2499 goto out;
2500
2501 error = -EPERM;
2502 if (on_sig_stack(sp))
2503 goto out;
2504
2505 error = -EINVAL;
2506 /*
2507 *
2508 * Note - this code used to test ss_flags incorrectly
2509 * old code may have been written using ss_flags==0
2510 * to mean ss_flags==SS_ONSTACK (as this was the only
2511 * way that worked) - this fix preserves that older
2512 * mechanism
2513 */
2514 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2515 goto out;
2516
2517 if (ss_flags == SS_DISABLE) {
2518 ss_size = 0;
2519 ss_sp = NULL;
2520 } else {
2521 error = -ENOMEM;
2522 if (ss_size < MINSIGSTKSZ)
2523 goto out;
2524 }
2525
2526 current->sas_ss_sp = (unsigned long) ss_sp;
2527 current->sas_ss_size = ss_size;
2528 }
2529
2530 if (uoss) {
2531 error = -EFAULT;
2532 if (copy_to_user(uoss, &oss, sizeof(oss)))
2533 goto out;
2534 }
2535
2536 error = 0;
2537out:
2538 return error;
2539}
2540
2541#ifdef __ARCH_WANT_SYS_SIGPENDING
2542
2543asmlinkage long
2544sys_sigpending(old_sigset_t __user *set)
2545{
2546 return do_sigpending(set, sizeof(*set));
2547}
2548
2549#endif
2550
2551#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2552/* Some platforms have their own version with special arguments others
2553 support only sys_rt_sigprocmask. */
2554
2555asmlinkage long
2556sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2557{
2558 int error;
2559 old_sigset_t old_set, new_set;
2560
2561 if (set) {
2562 error = -EFAULT;
2563 if (copy_from_user(&new_set, set, sizeof(*set)))
2564 goto out;
2565 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2566
2567 spin_lock_irq(&current->sighand->siglock);
2568 old_set = current->blocked.sig[0];
2569
2570 error = 0;
2571 switch (how) {
2572 default:
2573 error = -EINVAL;
2574 break;
2575 case SIG_BLOCK:
2576 sigaddsetmask(&current->blocked, new_set);
2577 break;
2578 case SIG_UNBLOCK:
2579 sigdelsetmask(&current->blocked, new_set);
2580 break;
2581 case SIG_SETMASK:
2582 current->blocked.sig[0] = new_set;
2583 break;
2584 }
2585
2586 recalc_sigpending();
2587 spin_unlock_irq(&current->sighand->siglock);
2588 if (error)
2589 goto out;
2590 if (oset)
2591 goto set_old;
2592 } else if (oset) {
2593 old_set = current->blocked.sig[0];
2594 set_old:
2595 error = -EFAULT;
2596 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2597 goto out;
2598 }
2599 error = 0;
2600out:
2601 return error;
2602}
2603#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
2604
2605#ifdef __ARCH_WANT_SYS_RT_SIGACTION
2606asmlinkage long
2607sys_rt_sigaction(int sig,
2608 const struct sigaction __user *act,
2609 struct sigaction __user *oact,
2610 size_t sigsetsize)
2611{
2612 struct k_sigaction new_sa, old_sa;
2613 int ret = -EINVAL;
2614
2615 /* XXX: Don't preclude handling different sized sigset_t's. */
2616 if (sigsetsize != sizeof(sigset_t))
2617 goto out;
2618
2619 if (act) {
2620 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2621 return -EFAULT;
2622 }
2623
2624 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2625
2626 if (!ret && oact) {
2627 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2628 return -EFAULT;
2629 }
2630out:
2631 return ret;
2632}
2633#endif /* __ARCH_WANT_SYS_RT_SIGACTION */
2634
2635#ifdef __ARCH_WANT_SYS_SGETMASK
2636
2637/*
2638 * For backwards compatibility. Functionality superseded by sigprocmask.
2639 */
2640asmlinkage long
2641sys_sgetmask(void)
2642{
2643 /* SMP safe */
2644 return current->blocked.sig[0];
2645}
2646
2647asmlinkage long
2648sys_ssetmask(int newmask)
2649{
2650 int old;
2651
2652 spin_lock_irq(&current->sighand->siglock);
2653 old = current->blocked.sig[0];
2654
2655 siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2656 sigmask(SIGSTOP)));
2657 recalc_sigpending();
2658 spin_unlock_irq(&current->sighand->siglock);
2659
2660 return old;
2661}
2662#endif /* __ARCH_WANT_SGETMASK */
2663
2664#ifdef __ARCH_WANT_SYS_SIGNAL
2665/*
2666 * For backwards compatibility. Functionality superseded by sigaction.
2667 */
2668asmlinkage unsigned long
2669sys_signal(int sig, __sighandler_t handler)
2670{
2671 struct k_sigaction new_sa, old_sa;
2672 int ret;
2673
2674 new_sa.sa.sa_handler = handler;
2675 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2676
2677 ret = do_sigaction(sig, &new_sa, &old_sa);
2678
2679 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2680}
2681#endif /* __ARCH_WANT_SYS_SIGNAL */
2682
2683#ifdef __ARCH_WANT_SYS_PAUSE
2684
2685asmlinkage long
2686sys_pause(void)
2687{
2688 current->state = TASK_INTERRUPTIBLE;
2689 schedule();
2690 return -ERESTARTNOHAND;
2691}
2692
2693#endif
2694
2695void __init signals_init(void)
2696{
2697 sigqueue_cachep =
2698 kmem_cache_create("sigqueue",
2699 sizeof(struct sigqueue),
2700 __alignof__(struct sigqueue),
2701 SLAB_PANIC, NULL, NULL);
2702}