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
9 #include <linux/config.h>
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/unistd.h>
13 #include <linux/smp_lock.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16
17 #include <asm/uaccess.h>
18
19 /*
20 * SLAB caches for signal bits.
21 */
22
23 #define DEBUG_SIG 0
24
25 #if DEBUG_SIG
26 #define SIG_SLAB_DEBUG (SLAB_DEBUG_FREE | SLAB_RED_ZONE /* | SLAB_POISON */)
27 #else
28 #define SIG_SLAB_DEBUG 0
29 #endif
30
31 static kmem_cache_t *sigqueue_cachep;
32
33 atomic_t nr_queued_signals;
34 int max_queued_signals = 1024;
35
36 void __init signals_init(void)
37 {
38 sigqueue_cachep =
39 kmem_cache_create("sigqueue",
40 sizeof(struct sigqueue),
41 __alignof__(struct sigqueue),
42 SIG_SLAB_DEBUG, NULL, NULL);
43 if (!sigqueue_cachep)
44 panic("signals_init(): cannot create sigqueue SLAB cache");
45 }
46
47
48 /* Given the mask, find the first available signal that should be serviced. */
49
50 static int
51 next_signal(struct task_struct *tsk, sigset_t *mask)
52 {
53 unsigned long i, *s, *m, x;
54 int sig = 0;
55
56 s = tsk->pending.signal.sig;
57 m = mask->sig;
58 switch (_NSIG_WORDS) {
59 default:
60 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
61 if ((x = *s &~ *m) != 0) {
62 sig = ffz(~x) + i*_NSIG_BPW + 1;
63 break;
64 }
65 break;
66
67 case 2: if ((x = s[0] &~ m[0]) != 0)
68 sig = 1;
69 else if ((x = s[1] &~ m[1]) != 0)
70 sig = _NSIG_BPW + 1;
71 else
72 break;
73 sig += ffz(~x);
74 break;
75
76 case 1: if ((x = *s &~ *m) != 0)
77 sig = ffz(~x) + 1;
78 break;
79 }
80
81 return sig;
82 }
83
84 static void flush_sigqueue(struct sigpending *queue)
85 {
86 struct sigqueue *q, *n;
87
88 sigemptyset(&queue->signal);
89 q = queue->head;
90 queue->head = NULL;
91 queue->tail = &queue->head;
92
93 while (q) {
94 n = q->next;
95 kmem_cache_free(sigqueue_cachep, q);
96 atomic_dec(&nr_queued_signals);
97 q = n;
98 }
99 }
100
101 /*
102 * Flush all pending signals for a task.
103 */
104
105 void
106 flush_signals(struct task_struct *t)
107 {
108 t->sigpending = 0;
109 flush_sigqueue(&t->pending);
110 }
111
112 void exit_sighand(struct task_struct *tsk)
113 {
114 struct signal_struct * sig = tsk->sig;
115
116 spin_lock_irq(&tsk->sigmask_lock);
117 if (sig) {
118 tsk->sig = NULL;
119 if (atomic_dec_and_test(&sig->count))
120 kmem_cache_free(sigact_cachep, sig);
121 }
122 tsk->sigpending = 0;
123 flush_sigqueue(&tsk->pending);
124 spin_unlock_irq(&tsk->sigmask_lock);
125 }
126
127 /*
128 * Flush all handlers for a task.
129 */
130
131 void
132 flush_signal_handlers(struct task_struct *t)
133 {
134 int i;
135 struct k_sigaction *ka = &t->sig->action[0];
136 for (i = _NSIG ; i != 0 ; i--) {
137 if (ka->sa.sa_handler != SIG_IGN)
138 ka->sa.sa_handler = SIG_DFL;
139 ka->sa.sa_flags = 0;
140 sigemptyset(&ka->sa.sa_mask);
141 ka++;
142 }
143 }
144
145 /* Notify the system that a driver wants to block all signals for this
146 * process, and wants to be notified if any signals at all were to be
147 * sent/acted upon. If the notifier routine returns non-zero, then the
148 * signal will be acted upon after all. If the notifier routine returns 0,
149 * then then signal will be blocked. Only one block per process is
150 * allowed. priv is a pointer to private data that the notifier routine
151 * can use to determine if the signal should be blocked or not. */
152
153 void
154 block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
155 {
156 unsigned long flags;
157
158 spin_lock_irqsave(¤t->sigmask_lock, flags);
159 current->notifier_mask = mask;
160 current->notifier_data = priv;
161 current->notifier = notifier;
162 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
163 }
164
165 /* Notify the system that blocking has ended. */
166
167 void
168 unblock_all_signals(void)
169 {
170 unsigned long flags;
171
172 spin_lock_irqsave(¤t->sigmask_lock, flags);
173 current->notifier = NULL;
174 current->notifier_data = NULL;
175 recalc_sigpending(current);
176 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
177 }
178
179 static int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
180 {
181 if (sigismember(&list->signal, sig)) {
182 /* Collect the siginfo appropriate to this signal. */
183 struct sigqueue *q, **pp;
184 pp = &list->head;
185 while ((q = *pp) != NULL) {
186 if (q->info.si_signo == sig)
187 goto found_it;
188 pp = &q->next;
189 }
190
191 /* Ok, it wasn't in the queue. We must have
192 been out of queue space. So zero out the
193 info. */
194 sigdelset(&list->signal, sig);
195 info->si_signo = sig;
196 info->si_errno = 0;
197 info->si_code = 0;
198 info->si_pid = 0;
199 info->si_uid = 0;
200 return 1;
201
202 found_it:
203 if ((*pp = q->next) == NULL)
204 list->tail = pp;
205
206 /* Copy the sigqueue information and free the queue entry */
207 copy_siginfo(info, &q->info);
208 kmem_cache_free(sigqueue_cachep,q);
209 atomic_dec(&nr_queued_signals);
210
211 /* Non-RT signals can exist multiple times.. */
212 if (sig >= SIGRTMIN) {
213 while ((q = *pp) != NULL) {
214 if (q->info.si_signo == sig)
215 goto found_another;
216 pp = &q->next;
217 }
218 }
219
220 sigdelset(&list->signal, sig);
221 found_another:
222 return 1;
223 }
224 return 0;
225 }
226
227 /*
228 * Dequeue a signal and return the element to the caller, which is
229 * expected to free it.
230 *
231 * All callers must be holding current->sigmask_lock.
232 */
233
234 int
235 dequeue_signal(sigset_t *mask, siginfo_t *info)
236 {
237 int sig = 0;
238
239 #if DEBUG_SIG
240 printk("SIG dequeue (%s:%d): %d ", current->comm, current->pid,
241 signal_pending(current));
242 #endif
243
244 sig = next_signal(current, mask);
245 if (current->notifier) {
246 if (sigismember(current->notifier_mask, sig)) {
247 if (!(current->notifier)(current->notifier_data)) {
248 current->sigpending = 0;
249 return 0;
250 }
251 }
252 }
253
254 if (sig) {
255 if (!collect_signal(sig, ¤t->pending, info))
256 sig = 0;
257
258 /* XXX: Once POSIX.1b timers are in, if si_code == SI_TIMER,
259 we need to xchg out the timer overrun values. */
260 }
261 recalc_sigpending(current);
262
263 #if DEBUG_SIG
264 printk(" %d -> %d\n", signal_pending(current), sig);
265 #endif
266
267 return sig;
268 }
269
270 static int rm_from_queue(int sig, struct sigpending *s)
271 {
272 struct sigqueue *q, **pp;
273
274 if (!sigismember(&s->signal, sig))
275 return 0;
276
277 sigdelset(&s->signal, sig);
278
279 pp = &s->head;
280
281 while ((q = *pp) != NULL) {
282 if (q->info.si_signo == sig) {
283 if ((*pp = q->next) == NULL)
284 s->tail = pp;
285 kmem_cache_free(sigqueue_cachep,q);
286 atomic_dec(&nr_queued_signals);
287 continue;
288 }
289 pp = &q->next;
290 }
291 return 1;
292 }
293
294 /*
295 * Remove signal sig from t->pending.
296 * Returns 1 if sig was found.
297 *
298 * All callers must be holding t->sigmask_lock.
299 */
300 static int rm_sig_from_queue(int sig, struct task_struct *t)
301 {
302 return rm_from_queue(sig, &t->pending);
303 }
304
305 /*
306 * Bad permissions for sending the signal
307 */
308 int bad_signal(int sig, struct siginfo *info, struct task_struct *t)
309 {
310 return (!info || ((unsigned long)info != 1 && SI_FROMUSER(info)))
311 && ((sig != SIGCONT) || (current->session != t->session))
312 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
313 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
314 && !capable(CAP_KILL);
315 }
316
317 /*
318 * Signal type:
319 * < 0 : global action (kill - spread to all non-blocked threads)
320 * = 0 : ignored
321 * > 0 : wake up.
322 */
323 static int signal_type(int sig, struct signal_struct *signals)
324 {
325 unsigned long handler;
326
327 if (!signals)
328 return 0;
329
330 handler = (unsigned long) signals->action[sig-1].sa.sa_handler;
331 if (handler > 1)
332 return 1;
333
334 /* "Ignore" handler.. Illogical, but that has an implicit handler for SIGCHLD */
335 if (handler == 1)
336 return sig == SIGCHLD;
337
338 /* Default handler. Normally lethal, but.. */
339 switch (sig) {
340
341 /* Ignored */
342 case SIGCONT: case SIGWINCH:
343 case SIGCHLD: case SIGURG:
344 return 0;
345
346 /* Implicit behaviour */
347 case SIGTSTP: case SIGTTIN: case SIGTTOU:
348 return 1;
349
350 /* Implicit actions (kill or do special stuff) */
351 default:
352 return -1;
353 }
354 }
355
356
357 /*
358 * Determine whether a signal should be posted or not.
359 *
360 * Signals with SIG_IGN can be ignored, except for the
361 * special case of a SIGCHLD.
362 *
363 * Some signals with SIG_DFL default to a non-action.
364 */
365 static int ignored_signal(int sig, struct task_struct *t)
366 {
367 /* Don't ignore traced or blocked signals */
368 if ((t->ptrace & PT_PTRACED) || sigismember(&t->blocked, sig))
369 return 0;
370
371 return signal_type(sig, t->sig) == 0;
372 }
373
374 /*
375 * Handle TASK_STOPPED cases etc implicit behaviour
376 * of certain magical signals.
377 *
378 * SIGKILL gets spread out to every thread.
379 */
380 static void handle_stop_signal(int sig, struct task_struct *t)
381 {
382 switch (sig) {
383 case SIGKILL: case SIGCONT:
384 /* Wake up the process if stopped. */
385 if (t->state == TASK_STOPPED)
386 wake_up_process(t);
387 t->exit_code = 0;
388 rm_sig_from_queue(SIGSTOP, t);
389 rm_sig_from_queue(SIGTSTP, t);
390 rm_sig_from_queue(SIGTTOU, t);
391 rm_sig_from_queue(SIGTTIN, t);
392 break;
393
394 case SIGSTOP: case SIGTSTP:
395 case SIGTTIN: case SIGTTOU:
396 /* If we're stopping again, cancel SIGCONT */
397 rm_sig_from_queue(SIGCONT, t);
398 break;
399 }
400 }
401
402 static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
403 {
404 struct sigqueue * q = NULL;
405
406 /* Real-time signals must be queued if sent by sigqueue, or
407 some other real-time mechanism. It is implementation
408 defined whether kill() does so. We attempt to do so, on
409 the principle of least surprise, but since kill is not
410 allowed to fail with EAGAIN when low on memory we just
411 make sure at least one signal gets delivered and don't
412 pass on the info struct. */
413
414 if (atomic_read(&nr_queued_signals) < max_queued_signals) {
415 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
416 }
417
418 if (q) {
419 atomic_inc(&nr_queued_signals);
420 q->next = NULL;
421 *signals->tail = q;
422 signals->tail = &q->next;
423 switch ((unsigned long) info) {
424 case 0:
425 q->info.si_signo = sig;
426 q->info.si_errno = 0;
427 q->info.si_code = SI_USER;
428 q->info.si_pid = current->pid;
429 q->info.si_uid = current->uid;
430 break;
431 case 1:
432 q->info.si_signo = sig;
433 q->info.si_errno = 0;
434 q->info.si_code = SI_KERNEL;
435 q->info.si_pid = 0;
436 q->info.si_uid = 0;
437 break;
438 default:
439 copy_siginfo(&q->info, info);
440 break;
441 }
442 } else if (sig >= SIGRTMIN && info && (unsigned long)info != 1
443 && info->si_code != SI_USER) {
444 /*
445 * Queue overflow, abort. We may abort if the signal was rt
446 * and sent by user using something other than kill().
447 */
448 return -EAGAIN;
449 }
450
451 sigaddset(&signals->signal, sig);
452 return 0;
453 }
454
455 /*
456 * Tell a process that it has a new active signal..
457 *
458 * NOTE! we rely on the previous spin_lock to
459 * lock interrupts for us! We can only be called with
460 * "sigmask_lock" held, and the local interrupt must
461 * have been disabled when that got acquired!
462 *
463 * No need to set need_resched since signal event passing
464 * goes through ->blocked
465 */
466 static inline void signal_wake_up(struct task_struct *t)
467 {
468 t->sigpending = 1;
469
470 if (t->state & TASK_INTERRUPTIBLE) {
471 wake_up_process(t);
472 return;
473 }
474
475 #ifdef CONFIG_SMP
476 /*
477 * If the task is running on a different CPU
478 * force a reschedule on the other CPU to make
479 * it notice the new signal quickly.
480 *
481 * The code below is a tad loose and might occasionally
482 * kick the wrong CPU if we catch the process in the
483 * process of changing - but no harm is done by that
484 * other than doing an extra (lightweight) IPI interrupt.
485 */
486 spin_lock(&runqueue_lock);
487 if (t->has_cpu && t->processor != smp_processor_id())
488 smp_send_reschedule(t->processor);
489 spin_unlock(&runqueue_lock);
490 #endif /* CONFIG_SMP */
491 }
492
493 static int deliver_signal(int sig, struct siginfo *info, struct task_struct *t)
494 {
495 int retval = send_signal(sig, info, &t->pending);
496
497 if (!retval && !sigismember(&t->blocked, sig))
498 signal_wake_up(t);
499
500 return retval;
501 }
502
503 int
504 send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
505 {
506 unsigned long flags;
507 int ret;
508
509
510 #if DEBUG_SIG
511 printk("SIG queue (%s:%d): %d ", t->comm, t->pid, sig);
512 #endif
513
514 ret = -EINVAL;
515 if (sig < 0 || sig > _NSIG)
516 goto out_nolock;
517 /* The somewhat baroque permissions check... */
518 ret = -EPERM;
519 if (bad_signal(sig, info, t))
520 goto out_nolock;
521
522 /* The null signal is a permissions and process existance probe.
523 No signal is actually delivered. Same goes for zombies. */
524 ret = 0;
525 if (!sig || !t->sig)
526 goto out_nolock;
527
528 spin_lock_irqsave(&t->sigmask_lock, flags);
529 handle_stop_signal(sig, t);
530
531 /* Optimize away the signal, if it's a signal that can be
532 handled immediately (ie non-blocked and untraced) and
533 that is ignored (either explicitly or by default). */
534
535 if (ignored_signal(sig, t))
536 goto out;
537
538 /* Support queueing exactly one non-rt signal, so that we
539 can get more detailed information about the cause of
540 the signal. */
541 if (sig < SIGRTMIN && sigismember(&t->pending.signal, sig))
542 goto out;
543
544 ret = deliver_signal(sig, info, t);
545 out:
546 spin_unlock_irqrestore(&t->sigmask_lock, flags);
547 if ((t->state & TASK_INTERRUPTIBLE) && signal_pending(t))
548 wake_up_process(t);
549 out_nolock:
550 #if DEBUG_SIG
551 printk(" %d -> %d\n", signal_pending(t), ret);
552 #endif
553
554 return ret;
555 }
556
557 /*
558 * Force a signal that the process can't ignore: if necessary
559 * we unblock the signal and change any SIG_IGN to SIG_DFL.
560 */
561
562 int
563 force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
564 {
565 unsigned long int flags;
566
567 spin_lock_irqsave(&t->sigmask_lock, flags);
568 if (t->sig == NULL) {
569 spin_unlock_irqrestore(&t->sigmask_lock, flags);
570 return -ESRCH;
571 }
572
573 if (t->sig->action[sig-1].sa.sa_handler == SIG_IGN)
574 t->sig->action[sig-1].sa.sa_handler = SIG_DFL;
575 sigdelset(&t->blocked, sig);
576 recalc_sigpending(t);
577 spin_unlock_irqrestore(&t->sigmask_lock, flags);
578
579 return send_sig_info(sig, info, t);
580 }
581
582 /*
583 * kill_pg_info() sends a signal to a process group: this is what the tty
584 * control characters do (^C, ^Z etc)
585 */
586
587 int
588 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
589 {
590 int retval = -EINVAL;
591 if (pgrp > 0) {
592 struct task_struct *p;
593
594 retval = -ESRCH;
595 read_lock(&tasklist_lock);
596 for_each_task(p) {
597 if (p->pgrp == pgrp) {
598 int err = send_sig_info(sig, info, p);
599 if (retval)
600 retval = err;
601 }
602 }
603 read_unlock(&tasklist_lock);
604 }
605 return retval;
606 }
607
608 /*
609 * kill_sl_info() sends a signal to the session leader: this is used
610 * to send SIGHUP to the controlling process of a terminal when
611 * the connection is lost.
612 */
613
614 int
615 kill_sl_info(int sig, struct siginfo *info, pid_t sess)
616 {
617 int retval = -EINVAL;
618 if (sess > 0) {
619 struct task_struct *p;
620
621 retval = -ESRCH;
622 read_lock(&tasklist_lock);
623 for_each_task(p) {
624 if (p->leader && p->session == sess) {
625 int err = send_sig_info(sig, info, p);
626 if (retval)
627 retval = err;
628 }
629 }
630 read_unlock(&tasklist_lock);
631 }
632 return retval;
633 }
634
635 inline int
636 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
637 {
638 int error;
639 struct task_struct *p;
640
641 read_lock(&tasklist_lock);
642 p = find_task_by_pid(pid);
643 error = -ESRCH;
644 if (p)
645 error = send_sig_info(sig, info, p);
646 read_unlock(&tasklist_lock);
647 return error;
648 }
649
650
651 /*
652 * kill_something_info() interprets pid in interesting ways just like kill(2).
653 *
654 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
655 * is probably wrong. Should make it like BSD or SYSV.
656 */
657
658 static int kill_something_info(int sig, struct siginfo *info, int pid)
659 {
660 if (!pid) {
661 return kill_pg_info(sig, info, current->pgrp);
662 } else if (pid == -1) {
663 int retval = 0, count = 0;
664 struct task_struct * p;
665
666 read_lock(&tasklist_lock);
667 for_each_task(p) {
668 if (p->pid > 1 && p != current) {
669 int err = send_sig_info(sig, info, p);
670 ++count;
671 if (err != -EPERM)
672 retval = err;
673 }
674 }
675 read_unlock(&tasklist_lock);
676 return count ? retval : -ESRCH;
677 } else if (pid < 0) {
678 return kill_pg_info(sig, info, -pid);
679 } else {
680 return kill_proc_info(sig, info, pid);
681 }
682 }
683
684 /*
685 * These are for backward compatibility with the rest of the kernel source.
686 */
687
688 int
689 send_sig(int sig, struct task_struct *p, int priv)
690 {
691 return send_sig_info(sig, (void*)(long)(priv != 0), p);
692 }
693
694 void
695 force_sig(int sig, struct task_struct *p)
696 {
697 force_sig_info(sig, (void*)1L, p);
698 }
699
700 int
701 kill_pg(pid_t pgrp, int sig, int priv)
702 {
703 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
704 }
705
706 int
707 kill_sl(pid_t sess, int sig, int priv)
708 {
709 return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
710 }
711
712 int
713 kill_proc(pid_t pid, int sig, int priv)
714 {
715 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
716 }
717
718 /*
719 * Joy. Or not. Pthread wants us to wake up every thread
720 * in our parent group.
721 */
722 static void wake_up_parent(struct task_struct *parent)
723 {
724 struct task_struct *tsk = parent;
725
726 do {
727 wake_up_interruptible(&tsk->wait_chldexit);
728 tsk = next_thread(tsk);
729 } while (tsk != parent);
730 }
731
732 /*
733 * Let a parent know about a status change of a child.
734 */
735
736 void do_notify_parent(struct task_struct *tsk, int sig)
737 {
738 struct siginfo info;
739 int why, status;
740
741 info.si_signo = sig;
742 info.si_errno = 0;
743 info.si_pid = tsk->pid;
744 info.si_uid = tsk->uid;
745
746 /* FIXME: find out whether or not this is supposed to be c*time. */
747 info.si_utime = tsk->times.tms_utime;
748 info.si_stime = tsk->times.tms_stime;
749
750 status = tsk->exit_code & 0x7f;
751 why = SI_KERNEL; /* shouldn't happen */
752 switch (tsk->state) {
753 case TASK_STOPPED:
754 /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
755 if (tsk->ptrace & PT_PTRACED)
756 why = CLD_TRAPPED;
757 else
758 why = CLD_STOPPED;
759 break;
760
761 default:
762 if (tsk->exit_code & 0x80)
763 why = CLD_DUMPED;
764 else if (tsk->exit_code & 0x7f)
765 why = CLD_KILLED;
766 else {
767 why = CLD_EXITED;
768 status = tsk->exit_code >> 8;
769 }
770 break;
771 }
772 info.si_code = why;
773 info.si_status = status;
774
775 send_sig_info(sig, &info, tsk->p_pptr);
776 wake_up_parent(tsk->p_pptr);
777 }
778
779
780 /*
781 * We need the tasklist lock because it's the only
782 * thing that protects out "parent" pointer.
783 *
784 * exit.c calls "do_notify_parent()" directly, because
785 * it already has the tasklist lock.
786 */
787 void
788 notify_parent(struct task_struct *tsk, int sig)
789 {
790 read_lock(&tasklist_lock);
791 do_notify_parent(tsk, sig);
792 read_unlock(&tasklist_lock);
793 }
794
795 EXPORT_SYMBOL(dequeue_signal);
796 EXPORT_SYMBOL(flush_signals);
797 EXPORT_SYMBOL(force_sig);
798 EXPORT_SYMBOL(force_sig_info);
799 EXPORT_SYMBOL(kill_pg);
800 EXPORT_SYMBOL(kill_pg_info);
801 EXPORT_SYMBOL(kill_proc);
802 EXPORT_SYMBOL(kill_proc_info);
803 EXPORT_SYMBOL(kill_sl);
804 EXPORT_SYMBOL(kill_sl_info);
805 EXPORT_SYMBOL(notify_parent);
806 EXPORT_SYMBOL(recalc_sigpending);
807 EXPORT_SYMBOL(send_sig);
808 EXPORT_SYMBOL(send_sig_info);
809 EXPORT_SYMBOL(block_all_signals);
810 EXPORT_SYMBOL(unblock_all_signals);
811
812
813 /*
814 * System call entry points.
815 */
816
817 /*
818 * We don't need to get the kernel lock - this is all local to this
819 * particular thread.. (and that's good, because this is _heavily_
820 * used by various programs)
821 */
822
823 asmlinkage long
824 sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset, size_t sigsetsize)
825 {
826 int error = -EINVAL;
827 sigset_t old_set, new_set;
828
829 /* XXX: Don't preclude handling different sized sigset_t's. */
830 if (sigsetsize != sizeof(sigset_t))
831 goto out;
832
833 if (set) {
834 error = -EFAULT;
835 if (copy_from_user(&new_set, set, sizeof(*set)))
836 goto out;
837 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
838
839 spin_lock_irq(¤t->sigmask_lock);
840 old_set = current->blocked;
841
842 error = 0;
843 switch (how) {
844 default:
845 error = -EINVAL;
846 break;
847 case SIG_BLOCK:
848 sigorsets(&new_set, &old_set, &new_set);
849 break;
850 case SIG_UNBLOCK:
851 signandsets(&new_set, &old_set, &new_set);
852 break;
853 case SIG_SETMASK:
854 break;
855 }
856
857 current->blocked = new_set;
858 recalc_sigpending(current);
859 spin_unlock_irq(¤t->sigmask_lock);
860 if (error)
861 goto out;
862 if (oset)
863 goto set_old;
864 } else if (oset) {
865 spin_lock_irq(¤t->sigmask_lock);
866 old_set = current->blocked;
867 spin_unlock_irq(¤t->sigmask_lock);
868
869 set_old:
870 error = -EFAULT;
871 if (copy_to_user(oset, &old_set, sizeof(*oset)))
872 goto out;
873 }
874 error = 0;
875 out:
876 return error;
877 }
878
879 long do_sigpending(void *set, unsigned long sigsetsize)
880 {
881 long error = -EINVAL;
882 sigset_t pending;
883
884 if (sigsetsize > sizeof(sigset_t))
885 goto out;
886
887 spin_lock_irq(¤t->sigmask_lock);
888 sigandsets(&pending, ¤t->blocked, ¤t->pending.signal);
889 spin_unlock_irq(¤t->sigmask_lock);
890
891 error = -EFAULT;
892 if (!copy_to_user(set, &pending, sigsetsize))
893 error = 0;
894 out:
895 return error;
896 }
897
898 asmlinkage long
899 sys_rt_sigpending(sigset_t *set, size_t sigsetsize)
900 {
901 return do_sigpending(set, sigsetsize);
902 }
903
904 asmlinkage long
905 sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
906 const struct timespec *uts, size_t sigsetsize)
907 {
908 int ret, sig;
909 sigset_t these;
910 struct timespec ts;
911 siginfo_t info;
912 long timeout = 0;
913
914 /* XXX: Don't preclude handling different sized sigset_t's. */
915 if (sigsetsize != sizeof(sigset_t))
916 return -EINVAL;
917
918 if (copy_from_user(&these, uthese, sizeof(these)))
919 return -EFAULT;
920
921 /*
922 * Invert the set of allowed signals to get those we
923 * want to block.
924 */
925 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
926 signotset(&these);
927
928 if (uts) {
929 if (copy_from_user(&ts, uts, sizeof(ts)))
930 return -EFAULT;
931 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
932 || ts.tv_sec < 0)
933 return -EINVAL;
934 }
935
936 spin_lock_irq(¤t->sigmask_lock);
937 sig = dequeue_signal(&these, &info);
938 if (!sig) {
939 timeout = MAX_SCHEDULE_TIMEOUT;
940 if (uts)
941 timeout = (timespec_to_jiffies(&ts)
942 + (ts.tv_sec || ts.tv_nsec));
943
944 if (timeout) {
945 /* None ready -- temporarily unblock those we're
946 * interested while we are sleeping in so that we'll
947 * be awakened when they arrive. */
948 sigset_t oldblocked = current->blocked;
949 sigandsets(¤t->blocked, ¤t->blocked, &these);
950 recalc_sigpending(current);
951 spin_unlock_irq(¤t->sigmask_lock);
952
953 current->state = TASK_INTERRUPTIBLE;
954 timeout = schedule_timeout(timeout);
955
956 spin_lock_irq(¤t->sigmask_lock);
957 sig = dequeue_signal(&these, &info);
958 current->blocked = oldblocked;
959 recalc_sigpending(current);
960 }
961 }
962 spin_unlock_irq(¤t->sigmask_lock);
963
964 if (sig) {
965 ret = sig;
966 if (uinfo) {
967 if (copy_siginfo_to_user(uinfo, &info))
968 ret = -EFAULT;
969 }
970 } else {
971 ret = -EAGAIN;
972 if (timeout)
973 ret = -EINTR;
974 }
975
976 return ret;
977 }
978
979 asmlinkage long
980 sys_kill(int pid, int sig)
981 {
982 struct siginfo info;
983
984 info.si_signo = sig;
985 info.si_errno = 0;
986 info.si_code = SI_USER;
987 info.si_pid = current->pid;
988 info.si_uid = current->uid;
989
990 return kill_something_info(sig, &info, pid);
991 }
992
993 asmlinkage long
994 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo)
995 {
996 siginfo_t info;
997
998 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
999 return -EFAULT;
1000
1001 /* Not even root can pretend to send signals from the kernel.
1002 Nor can they impersonate a kill(), which adds source info. */
1003 if (info.si_code >= 0)
1004 return -EPERM;
1005 info.si_signo = sig;
1006
1007 /* POSIX.1b doesn't mention process groups. */
1008 return kill_proc_info(sig, &info, pid);
1009 }
1010
1011 int
1012 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
1013 {
1014 struct k_sigaction *k;
1015
1016 if (sig < 1 || sig > _NSIG ||
1017 (act && (sig == SIGKILL || sig == SIGSTOP)))
1018 return -EINVAL;
1019
1020 k = ¤t->sig->action[sig-1];
1021
1022 spin_lock(¤t->sig->siglock);
1023
1024 if (oact)
1025 *oact = *k;
1026
1027 if (act) {
1028 *k = *act;
1029 sigdelsetmask(&k->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1030
1031 /*
1032 * POSIX 3.3.1.3:
1033 * "Setting a signal action to SIG_IGN for a signal that is
1034 * pending shall cause the pending signal to be discarded,
1035 * whether or not it is blocked."
1036 *
1037 * "Setting a signal action to SIG_DFL for a signal that is
1038 * pending and whose default action is to ignore the signal
1039 * (for example, SIGCHLD), shall cause the pending signal to
1040 * be discarded, whether or not it is blocked"
1041 *
1042 * Note the silly behaviour of SIGCHLD: SIG_IGN means that the
1043 * signal isn't actually ignored, but does automatic child
1044 * reaping, while SIG_DFL is explicitly said by POSIX to force
1045 * the signal to be ignored.
1046 */
1047
1048 if (k->sa.sa_handler == SIG_IGN
1049 || (k->sa.sa_handler == SIG_DFL
1050 && (sig == SIGCONT ||
1051 sig == SIGCHLD ||
1052 sig == SIGWINCH))) {
1053 spin_lock_irq(¤t->sigmask_lock);
1054 if (rm_sig_from_queue(sig, current))
1055 recalc_sigpending(current);
1056 spin_unlock_irq(¤t->sigmask_lock);
1057 }
1058 }
1059
1060 spin_unlock(¤t->sig->siglock);
1061 return 0;
1062 }
1063
1064 int
1065 do_sigaltstack (const stack_t *uss, stack_t *uoss, unsigned long sp)
1066 {
1067 stack_t oss;
1068 int error;
1069
1070 if (uoss) {
1071 oss.ss_sp = (void *) current->sas_ss_sp;
1072 oss.ss_size = current->sas_ss_size;
1073 oss.ss_flags = sas_ss_flags(sp);
1074 }
1075
1076 if (uss) {
1077 void *ss_sp;
1078 size_t ss_size;
1079 int ss_flags;
1080
1081 error = -EFAULT;
1082 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
1083 || __get_user(ss_sp, &uss->ss_sp)
1084 || __get_user(ss_flags, &uss->ss_flags)
1085 || __get_user(ss_size, &uss->ss_size))
1086 goto out;
1087
1088 error = -EPERM;
1089 if (on_sig_stack (sp))
1090 goto out;
1091
1092 error = -EINVAL;
1093 /*
1094 *
1095 * Note - this code used to test ss_flags incorrectly
1096 * old code may have been written using ss_flags==0
1097 * to mean ss_flags==SS_ONSTACK (as this was the only
1098 * way that worked) - this fix preserves that older
1099 * mechanism
1100 */
1101 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
1102 goto out;
1103
1104 if (ss_flags == SS_DISABLE) {
1105 ss_size = 0;
1106 ss_sp = NULL;
1107 } else {
1108 error = -ENOMEM;
1109 if (ss_size < MINSIGSTKSZ)
1110 goto out;
1111 }
1112
1113 current->sas_ss_sp = (unsigned long) ss_sp;
1114 current->sas_ss_size = ss_size;
1115 }
1116
1117 if (uoss) {
1118 error = -EFAULT;
1119 if (copy_to_user(uoss, &oss, sizeof(oss)))
1120 goto out;
1121 }
1122
1123 error = 0;
1124 out:
1125 return error;
1126 }
1127
1128 asmlinkage long
1129 sys_sigpending(old_sigset_t *set)
1130 {
1131 return do_sigpending(set, sizeof(*set));
1132 }
1133
1134 #if !defined(__alpha__)
1135 /* Alpha has its own versions with special arguments. */
1136
1137 asmlinkage long
1138 sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset)
1139 {
1140 int error;
1141 old_sigset_t old_set, new_set;
1142
1143 if (set) {
1144 error = -EFAULT;
1145 if (copy_from_user(&new_set, set, sizeof(*set)))
1146 goto out;
1147 new_set &= ~(sigmask(SIGKILL)|sigmask(SIGSTOP));
1148
1149 spin_lock_irq(¤t->sigmask_lock);
1150 old_set = current->blocked.sig[0];
1151
1152 error = 0;
1153 switch (how) {
1154 default:
1155 error = -EINVAL;
1156 break;
1157 case SIG_BLOCK:
1158 sigaddsetmask(¤t->blocked, new_set);
1159 break;
1160 case SIG_UNBLOCK:
1161 sigdelsetmask(¤t->blocked, new_set);
1162 break;
1163 case SIG_SETMASK:
1164 current->blocked.sig[0] = new_set;
1165 break;
1166 }
1167
1168 recalc_sigpending(current);
1169 spin_unlock_irq(¤t->sigmask_lock);
1170 if (error)
1171 goto out;
1172 if (oset)
1173 goto set_old;
1174 } else if (oset) {
1175 old_set = current->blocked.sig[0];
1176 set_old:
1177 error = -EFAULT;
1178 if (copy_to_user(oset, &old_set, sizeof(*oset)))
1179 goto out;
1180 }
1181 error = 0;
1182 out:
1183 return error;
1184 }
1185
1186 #ifndef __sparc__
1187 asmlinkage long
1188 sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
1189 size_t sigsetsize)
1190 {
1191 struct k_sigaction new_sa, old_sa;
1192 int ret = -EINVAL;
1193
1194 /* XXX: Don't preclude handling different sized sigset_t's. */
1195 if (sigsetsize != sizeof(sigset_t))
1196 goto out;
1197
1198 if (act) {
1199 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
1200 return -EFAULT;
1201 }
1202
1203 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
1204
1205 if (!ret && oact) {
1206 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
1207 return -EFAULT;
1208 }
1209 out:
1210 return ret;
1211 }
1212 #endif /* __sparc__ */
1213 #endif
1214
1215 #if !defined(__alpha__) && !defined(__ia64__)
1216 /*
1217 * For backwards compatibility. Functionality superseded by sigprocmask.
1218 */
1219 asmlinkage long
1220 sys_sgetmask(void)
1221 {
1222 /* SMP safe */
1223 return current->blocked.sig[0];
1224 }
1225
1226 asmlinkage long
1227 sys_ssetmask(int newmask)
1228 {
1229 int old;
1230
1231 spin_lock_irq(¤t->sigmask_lock);
1232 old = current->blocked.sig[0];
1233
1234 siginitset(¤t->blocked, newmask & ~(sigmask(SIGKILL)|
1235 sigmask(SIGSTOP)));
1236 recalc_sigpending(current);
1237 spin_unlock_irq(¤t->sigmask_lock);
1238
1239 return old;
1240 }
1241 #endif /* !defined(__alpha__) */
1242
1243 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__)
1244 /*
1245 * For backwards compatibility. Functionality superseded by sigaction.
1246 */
1247 asmlinkage unsigned long
1248 sys_signal(int sig, __sighandler_t handler)
1249 {
1250 struct k_sigaction new_sa, old_sa;
1251 int ret;
1252
1253 new_sa.sa.sa_handler = handler;
1254 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
1255
1256 ret = do_sigaction(sig, &new_sa, &old_sa);
1257
1258 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
1259 }
1260 #endif /* !alpha && !__ia64__ && !defined(__mips__) */
1261
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.