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