1
2
3
4
5
6
7
8
9
10
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/ptrace.h>
24#include <linux/audit.h>
25#include <linux/task_io_accounting_ops.h>
26#include <asm/param.h>
27#include <asm/uaccess.h>
28#include <asm/unistd.h>
29#include <asm/siginfo.h>
30
31extern void k_getrusage(struct task_struct *, int, struct rusage *);
32
33
34
35
36
37static kmem_cache_t *sigqueue_cachep;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113#ifdef SIGEMT
114#define M_SIGEMT M(SIGEMT)
115#else
116#define M_SIGEMT 0
117#endif
118
119#if SIGRTMIN > BITS_PER_LONG
120#define M(sig) (1ULL << ((sig)-1))
121#else
122#define M(sig) (1UL << ((sig)-1))
123#endif
124#define T(sig, mask) (M(sig) & (mask))
125
126#define SIG_KERNEL_ONLY_MASK (\
127 M(SIGKILL) | M(SIGSTOP) )
128
129#define SIG_KERNEL_STOP_MASK (\
130 M(SIGSTOP) | M(SIGTSTP) | M(SIGTTIN) | M(SIGTTOU) )
131
132#define SIG_KERNEL_COREDUMP_MASK (\
133 M(SIGQUIT) | M(SIGILL) | M(SIGTRAP) | M(SIGABRT) | \
134 M(SIGFPE) | M(SIGSEGV) | M(SIGBUS) | M(SIGSYS) | \
135 M(SIGXCPU) | M(SIGXFSZ) | M_SIGEMT )
136
137#define SIG_KERNEL_IGNORE_MASK (\
138 M(SIGCONT) | M(SIGCHLD) | M(SIGWINCH) | M(SIGURG) )
139
140#define sig_kernel_only(sig) \
141 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_ONLY_MASK))
142#define sig_kernel_coredump(sig) \
143 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_COREDUMP_MASK))
144#define sig_kernel_ignore(sig) \
145 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_IGNORE_MASK))
146#define sig_kernel_stop(sig) \
147 (((sig) < SIGRTMIN) && T(sig, SIG_KERNEL_STOP_MASK))
148
149#define sig_user_defined(t, signr) \
150 (((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_DFL) && \
151 ((t)->sighand->action[(signr)-1].sa.sa_handler != SIG_IGN))
152
153#define sig_fatal(t, signr) \
154 (!T(signr, SIG_KERNEL_IGNORE_MASK|SIG_KERNEL_STOP_MASK) && \
155 (t)->sighand->action[(signr)-1].sa.sa_handler == SIG_DFL)
156
157#define sig_avoid_stop_race() \
158 (sigtestsetmask(¤t->pending.signal, M(SIGCONT) | M(SIGKILL)) || \
159 sigtestsetmask(¤t->signal->shared_pending.signal, \
160 M(SIGCONT) | M(SIGKILL)))
161
162static int sig_ignored(struct task_struct *t, int sig)
163{
164 void __user * handler;
165
166
167
168
169 if (t->ptrace & PT_PTRACED)
170 return 0;
171
172
173
174
175
176
177 if (sigismember(&t->blocked, sig))
178 return 0;
179
180
181 handler = t->sighand->action[sig-1].sa.sa_handler;
182 return handler == SIG_IGN ||
183 (handler == SIG_DFL && sig_kernel_ignore(sig));
184}
185
186
187
188
189
190static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
191{
192 unsigned long ready;
193 long i;
194
195 switch (_NSIG_WORDS) {
196 default:
197 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
198 ready |= signal->sig[i] &~ blocked->sig[i];
199 break;
200
201 case 4: ready = signal->sig[3] &~ blocked->sig[3];
202 ready |= signal->sig[2] &~ blocked->sig[2];
203 ready |= signal->sig[1] &~ blocked->sig[1];
204 ready |= signal->sig[0] &~ blocked->sig[0];
205 break;
206
207 case 2: ready = signal->sig[1] &~ blocked->sig[1];
208 ready |= signal->sig[0] &~ blocked->sig[0];
209 break;
210
211 case 1: ready = signal->sig[0] &~ blocked->sig[0];
212 }
213 return ready != 0;
214}
215
216#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
217
218static int recalc_sigpending_tsk(struct task_struct *t)
219{
220 if (t->signal->group_stop_count > 0 ||
221 PENDING(&t->pending, &t->blocked) ||
222 PENDING(&t->signal->shared_pending, &t->blocked)) {
223 set_tsk_thread_flag(t, TIF_SIGPENDING);
224 return 1;
225 }
226
227
228
229
230
231 return 0;
232}
233
234
235
236
237
238void recalc_sigpending_and_wake(struct task_struct *t)
239{
240 if (recalc_sigpending_tsk(t))
241 signal_wake_up(t, 0);
242}
243
244void recalc_sigpending(void)
245{
246 if (!recalc_sigpending_tsk(current))
247 clear_thread_flag(TIF_SIGPENDING);
248
249}
250
251
252
253static int
254next_signal(struct sigpending *pending, sigset_t *mask)
255{
256 unsigned long i, *s, *m, x;
257 int sig = 0;
258
259 s = pending->signal.sig;
260 m = mask->sig;
261 switch (_NSIG_WORDS) {
262 default:
263 for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
264 if ((x = *s &~ *m) != 0) {
265 sig = ffz(~x) + i*_NSIG_BPW + 1;
266 break;
267 }
268 break;
269
270 case 2: if ((x = s[0] &~ m[0]) != 0)
271 sig = 1;
272 else if ((x = s[1] &~ m[1]) != 0)
273 sig = _NSIG_BPW + 1;
274 else
275 break;
276 sig += ffz(~x);
277 break;
278
279 case 1: if ((x = *s &~ *m) != 0)
280 sig = ffz(~x) + 1;
281 break;
282 }
283
284 return sig;
285}
286
287static struct sigqueue *__sigqueue_alloc(void)
288{
289 struct sigqueue *q = NULL;
290 struct user_struct *user;
291
292
293
294
295
296 user = current->user;
297 barrier();
298 if (atomic_read(&user->sigpending) <
299 current->rlim[RLIMIT_SIGPENDING].rlim_cur)
300 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
301 if (q) {
302 INIT_LIST_HEAD(&q->list);
303 q->flags = 0;
304 q->lock = NULL;
305 q->user = get_uid(user);
306 atomic_inc(&user->sigpending);
307 }
308 return(q);
309}
310
311static inline void __sigqueue_free(struct sigqueue *q)
312{
313 if (q->flags & SIGQUEUE_PREALLOC)
314 return;
315 atomic_dec(&q->user->sigpending);
316 free_uid(q->user);
317 kmem_cache_free(sigqueue_cachep, q);
318}
319
320static void flush_sigqueue(struct sigpending *queue)
321{
322 struct sigqueue *q;
323
324 sigemptyset(&queue->signal);
325 while (!list_empty(&queue->list)) {
326 q = list_entry(queue->list.next, struct sigqueue , list);
327 list_del_init(&q->list);
328 __sigqueue_free(q);
329 }
330}
331
332
333
334
335
336void
337flush_signals(struct task_struct *t)
338{
339 unsigned long flags;
340
341 spin_lock_irqsave(&t->sighand->siglock, flags);
342 clear_tsk_thread_flag(t,TIF_SIGPENDING);
343 flush_sigqueue(&t->pending);
344 flush_sigqueue(&t->signal->shared_pending);
345 spin_unlock_irqrestore(&t->sighand->siglock, flags);
346}
347
348
349
350
351void __exit_sighand(struct task_struct *tsk)
352{
353 struct sighand_struct * sighand = tsk->sighand;
354
355
356 tsk->sighand = NULL;
357 if (atomic_dec_and_test(&sighand->count))
358 kmem_cache_free(sighand_cachep, sighand);
359}
360
361void exit_sighand(struct task_struct *tsk)
362{
363 write_lock_irq(&tasklist_lock);
364 __exit_sighand(tsk);
365 write_unlock_irq(&tasklist_lock);
366}
367
368
369
370
371void __exit_signal(struct task_struct *tsk)
372{
373 struct signal_struct * sig = tsk->signal;
374 struct sighand_struct * sighand = tsk->sighand;
375
376 if (!sig)
377 BUG();
378 if (!atomic_read(&sig->count))
379 BUG();
380 spin_lock(&sighand->siglock);
381 if (atomic_dec_and_test(&sig->count)) {
382 if (tsk == sig->curr_target)
383 sig->curr_target = next_thread(tsk);
384 tsk->signal = NULL;
385 spin_unlock(&sighand->siglock);
386 flush_sigqueue(&sig->shared_pending);
387 } else {
388
389
390
391
392 if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
393 wake_up_process(sig->group_exit_task);
394 sig->group_exit_task = NULL;
395 }
396 if (tsk == sig->curr_target)
397 sig->curr_target = next_thread(tsk);
398 tsk->signal = NULL;
399
400
401
402
403
404
405
406
407
408
409 sig->utime += tsk->utime;
410 sig->stime += tsk->stime;
411 sig->min_flt += tsk->min_flt;
412 sig->maj_flt += tsk->maj_flt;
413 sig->nvcsw += tsk->nvcsw;
414 sig->nivcsw += tsk->nivcsw;
415 sig->inblock += task_io_get_inblock(tsk);
416 sig->oublock += task_io_get_oublock(tsk);
417 spin_unlock(&sighand->siglock);
418 sig = NULL;
419 }
420 clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
421 flush_sigqueue(&tsk->pending);
422 if (sig) {
423
424
425
426 exit_thread_group_keys(sig);
427 kmem_cache_free(signal_cachep, sig);
428 }
429}
430
431void exit_signal(struct task_struct *tsk)
432{
433 atomic_dec(&tsk->signal->live);
434
435 write_lock_irq(&tasklist_lock);
436 __exit_signal(tsk);
437 write_unlock_irq(&tasklist_lock);
438}
439
440
441
442
443
444void
445flush_signal_handlers(struct task_struct *t, int force_default)
446{
447 int i;
448 struct k_sigaction *ka = &t->sighand->action[0];
449 for (i = _NSIG ; i != 0 ; i--) {
450 if (force_default || ka->sa.sa_handler != SIG_IGN)
451 ka->sa.sa_handler = SIG_DFL;
452 ka->sa.sa_flags = 0;
453 sigemptyset(&ka->sa.sa_mask);
454 ka++;
455 }
456}
457
458EXPORT_SYMBOL_GPL(flush_signal_handlers);
459
460
461
462
463
464
465
466
467
468void
469block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
470{
471 unsigned long flags;
472
473 spin_lock_irqsave(¤t->sighand->siglock, flags);
474 current->notifier_mask = mask;
475 current->notifier_data = priv;
476 current->notifier = notifier;
477 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
478}
479
480
481
482void
483unblock_all_signals(void)
484{
485 unsigned long flags;
486
487 spin_lock_irqsave(¤t->sighand->siglock, flags);
488 current->notifier = NULL;
489 current->notifier_data = NULL;
490 recalc_sigpending();
491 spin_unlock_irqrestore(¤t->sighand->siglock, flags);
492}
493
494static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
495{
496 struct sigqueue *q, *first = NULL;
497 int still_pending = 0;
498
499 if (unlikely(!sigismember(&list->signal, sig)))
500 return 0;
501
502
503
504
505
506 list_for_each_entry(q, &list->list, list) {
507 if (q->info.si_signo == sig) {
508 if (first) {
509 still_pending = 1;
510 break;
511 }
512 first = q;
513 }
514 }
515 if (first) {
516 list_del_init(&first->list);
517 copy_siginfo(info, &first->info);
518 __sigqueue_free(first);
519 if (!still_pending)
520 sigdelset(&list->signal, sig);
521 } else {
522
523
524
525
526
527 sigdelset(&list->signal, sig);
528 info->si_signo = sig;
529 info->si_errno = 0;
530 info->si_code = 0;
531 info->si_pid = 0;
532 info->si_uid = 0;
533 }
534 return 1;
535}
536
537static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
538 siginfo_t *info)
539{
540 int sig = 0;
541
542
543
544
545 if (unlikely(sigismember(&pending->signal, SIGKILL))) {
546 if (!sigismember(mask, SIGKILL))
547 sig = SIGKILL;
548 }
549
550 if (likely(!sig))
551 sig = next_signal(pending, mask);
552
553 if (sig) {
554 if (current->notifier) {
555 if (sigismember(current->notifier_mask, sig)) {
556 if (!(current->notifier)(current->notifier_data)) {
557 clear_thread_flag(TIF_SIGPENDING);
558 return 0;
559 }
560 }
561 }
562
563 if (!collect_signal(sig, pending, info))
564 sig = 0;
565
566 }
567 recalc_sigpending();
568
569 return sig;
570}
571
572
573
574
575
576
577
578int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
579{
580 int signr = __dequeue_signal(&tsk->pending, mask, info);
581 if (!signr)
582 signr = __dequeue_signal(&tsk->signal->shared_pending,
583 mask, info);
584 if ( signr &&
585 ((info->si_code & __SI_MASK) == __SI_TIMER) &&
586 info->si_sys_private){
587 do_schedule_next_timer(info);
588 }
589 return signr;
590}
591
592
593
594
595
596
597
598
599
600
601
602
603void signal_wake_up(struct task_struct *t, int resume)
604{
605 unsigned int mask;
606
607 set_tsk_thread_flag(t, TIF_SIGPENDING);
608
609
610
611
612
613
614
615
616 mask = TASK_INTERRUPTIBLE;
617 if (resume)
618 mask |= TASK_STOPPED | TASK_TRACED;
619 if (!wake_up_state(t, mask))
620 kick_process(t);
621}
622
623
624
625
626
627
628
629static int rm_from_queue(unsigned long mask, struct sigpending *s)
630{
631 struct sigqueue *q, *n;
632
633 if (!sigtestsetmask(&s->signal, mask))
634 return 0;
635
636 sigdelsetmask(&s->signal, mask);
637 list_for_each_entry_safe(q, n, &s->list, list) {
638 if (q->info.si_signo < SIGRTMIN &&
639 (mask & sigmask(q->info.si_signo))) {
640 list_del_init(&q->list);
641 __sigqueue_free(q);
642 }
643 }
644 return 1;
645}
646
647
648
649
650static int check_kill_permission(int sig, struct siginfo *info,
651 struct task_struct *t)
652{
653 int error = -EINVAL;
654 if (sig < 0 || sig > _NSIG)
655 return error;
656 error = -EPERM;
657 if ((!info || ((unsigned long)info != 1 &&
658 (unsigned long)info != 2 && SI_FROMUSER(info)))
659 && ((sig != SIGCONT) ||
660 (current->signal->session != t->signal->session))
661 && (current->euid ^ t->suid) && (current->euid ^ t->uid)
662 && (current->uid ^ t->suid) && (current->uid ^ t->uid)
663 && !capable(CAP_KILL))
664 return error;
665
666 error = security_task_kill(t, info, sig);
667 if (!error)
668 audit_signal_info(sig, t);
669 return error;
670}
671
672
673static void do_notify_parent_cldstop(struct task_struct *tsk,
674 struct task_struct *parent,
675 int why);
676
677
678
679
680
681
682
683
684static void handle_stop_signal(int sig, struct task_struct *p)
685{
686 struct task_struct *t;
687
688 if (sig_kernel_stop(sig)) {
689
690
691
692 rm_from_queue(sigmask(SIGCONT), &p->signal->shared_pending);
693 t = p;
694 do {
695 rm_from_queue(sigmask(SIGCONT), &t->pending);
696 t = next_thread(t);
697 } while (t != p);
698 } else if (sig == SIGCONT) {
699
700
701
702
703 if (unlikely(p->signal->group_stop_count > 0)) {
704
705
706
707
708
709
710
711
712
713
714
715
716 p->signal->group_stop_count = 0;
717 p->signal->stop_state = 1;
718 spin_unlock(&p->sighand->siglock);
719 if (p->ptrace & PT_PTRACED)
720 do_notify_parent_cldstop(p, p->parent,
721 CLD_STOPPED);
722 else
723 do_notify_parent_cldstop(
724 p->group_leader,
725 p->group_leader->real_parent,
726 CLD_STOPPED);
727 spin_lock(&p->sighand->siglock);
728 }
729 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
730 t = p;
731 do {
732 unsigned int state;
733 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749 state = TASK_STOPPED;
750 if (sig_user_defined(t, SIGCONT) && !sigismember(&t->blocked, SIGCONT)) {
751 set_tsk_thread_flag(t, TIF_SIGPENDING);
752 state |= TASK_INTERRUPTIBLE;
753 }
754 wake_up_state(t, state);
755
756 t = next_thread(t);
757 } while (t != p);
758
759 if (p->signal->stop_state > 0) {
760
761
762
763
764 p->signal->stop_state = -1;
765 p->signal->group_exit_code = 0;
766 spin_unlock(&p->sighand->siglock);
767 if (p->ptrace & PT_PTRACED)
768 do_notify_parent_cldstop(p, p->parent,
769 CLD_CONTINUED);
770 else
771 do_notify_parent_cldstop(
772 p->group_leader,
773 p->group_leader->real_parent,
774 CLD_CONTINUED);
775 spin_lock(&p->sighand->siglock);
776 }
777 }
778}
779
780static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
781 struct sigpending *signals)
782{
783 struct sigqueue * q = NULL;
784 int ret = 0;
785
786
787
788
789
790 if ((unsigned long)info == 2)
791 goto out_set;
792
793
794
795
796
797
798
799
800
801 if (atomic_read(&t->user->sigpending) <
802 t->rlim[RLIMIT_SIGPENDING].rlim_cur)
803 q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
804
805 if (q) {
806 q->flags = 0;
807 q->user = get_uid(t->user);
808 atomic_inc(&q->user->sigpending);
809 list_add_tail(&q->list, &signals->list);
810 switch ((unsigned long) info) {
811 case 0:
812 q->info.si_signo = sig;
813 q->info.si_errno = 0;
814 q->info.si_code = SI_USER;
815 q->info.si_pid = current->pid;
816 q->info.si_uid = current->uid;
817 break;
818 case 1:
819 q->info.si_signo = sig;
820 q->info.si_errno = 0;
821 q->info.si_code = SI_KERNEL;
822 q->info.si_pid = 0;
823 q->info.si_uid = 0;
824 break;
825 default:
826 copy_siginfo(&q->info, info);
827 break;
828 }
829 } else {
830 if (sig >= SIGRTMIN && info && (unsigned long)info != 1
831 && info->si_code != SI_USER)
832
833
834
835
836 return -EAGAIN;
837 if (((unsigned long)info > 1) && (info->si_code == SI_TIMER))
838
839
840
841
842 ret = info->si_sys_private;
843 }
844
845out_set:
846 sigaddset(&signals->signal, sig);
847 return ret;
848}
849
850#define LEGACY_QUEUE(sigptr, sig) \
851 (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
852
853
854static int
855specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
856{
857 int ret = 0;
858
859 if (!irqs_disabled())
860 BUG();
861#ifdef CONFIG_SMP
862 if (!spin_is_locked(&t->sighand->siglock))
863 BUG();
864#endif
865
866 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
867
868
869
870 ret = info->si_sys_private;
871
872
873 if (sig_ignored(t, sig))
874 goto out;
875
876
877
878
879 if (LEGACY_QUEUE(&t->pending, sig))
880 goto out;
881
882 ret = send_signal(sig, info, t, &t->pending);
883 if (!ret && !sigismember(&t->blocked, sig))
884 signal_wake_up(t, sig == SIGKILL);
885out:
886 return ret;
887}
888
889
890
891
892
893
894int
895force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
896{
897 unsigned long int flags;
898 int ret;
899
900 spin_lock_irqsave(&t->sighand->siglock, flags);
901 if (sigismember(&t->blocked, sig) || t->sighand->action[sig-1].sa.sa_handler == SIG_IGN) {
902 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
903 sigdelset(&t->blocked, sig);
904 recalc_sigpending_and_wake(t);
905 }
906 ret = specific_send_sig_info(sig, info, t);
907 spin_unlock_irqrestore(&t->sighand->siglock, flags);
908
909 return ret;
910}
911
912void
913force_sig_specific(int sig, struct task_struct *t)
914{
915 unsigned long int flags;
916
917 spin_lock_irqsave(&t->sighand->siglock, flags);
918 if (t->sighand->action[sig-1].sa.sa_handler == SIG_IGN)
919 t->sighand->action[sig-1].sa.sa_handler = SIG_DFL;
920 sigdelset(&t->blocked, sig);
921 recalc_sigpending_and_wake(t);
922 specific_send_sig_info(sig, (void *)2, t);
923 spin_unlock_irqrestore(&t->sighand->siglock, flags);
924}
925
926
927
928
929
930
931
932
933
934static inline int wants_signal(int sig, struct task_struct *p)
935{
936 if (sigismember(&p->blocked, sig))
937 return 0;
938 if (p->flags & PF_EXITING)
939 return 0;
940 if (sig == SIGKILL)
941 return 1;
942 if (p->state & (TASK_STOPPED | TASK_TRACED))
943 return 0;
944 return task_curr(p) || !signal_pending(p);
945}
946
947static void
948__group_complete_signal(int sig, struct task_struct *p)
949{
950 struct task_struct *t;
951
952
953
954
955
956
957
958 if (wants_signal(sig, p))
959 t = p;
960 else if (thread_group_empty(p))
961
962
963
964
965 return;
966 else {
967
968
969
970 t = p->signal->curr_target;
971 if (t == NULL)
972
973 t = p->signal->curr_target = p;
974 BUG_ON(t->tgid != p->tgid);
975
976 while (!wants_signal(sig, t)) {
977 t = next_thread(t);
978 if (t == p->signal->curr_target)
979
980
981
982
983
984 return;
985 }
986 p->signal->curr_target = t;
987 }
988
989
990
991
992
993 if (sig_fatal(p, sig) && !p->signal->group_exit &&
994 !sigismember(&t->real_blocked, sig) &&
995 (sig == SIGKILL || !(t->ptrace & PT_PTRACED))) {
996
997
998
999 if (!sig_kernel_coredump(sig)) {
1000
1001
1002
1003
1004
1005
1006 p->signal->group_exit = 1;
1007 p->signal->group_exit_code = sig;
1008 p->signal->group_stop_count = 0;
1009 t = p;
1010 do {
1011 sigaddset(&t->pending.signal, SIGKILL);
1012 signal_wake_up(t, 1);
1013 t = next_thread(t);
1014 } while (t != p);
1015 return;
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1029 rm_from_queue(SIG_KERNEL_STOP_MASK, &p->signal->shared_pending);
1030 p->signal->group_stop_count = 0;
1031 p->signal->group_exit_task = t;
1032 t = p;
1033 do {
1034 p->signal->group_stop_count++;
1035 signal_wake_up(t, 0);
1036 t = next_thread(t);
1037 } while (t != p);
1038 wake_up_process(p->signal->group_exit_task);
1039 return;
1040 }
1041
1042
1043
1044
1045
1046 signal_wake_up(t, sig == SIGKILL);
1047 return;
1048}
1049
1050static int
1051__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1052{
1053 int ret = 0;
1054
1055#ifdef CONFIG_SMP
1056 if (!spin_is_locked(&p->sighand->siglock))
1057 BUG();
1058#endif
1059 handle_stop_signal(sig, p);
1060
1061 if (((unsigned long)info > 2) && (info->si_code == SI_TIMER))
1062
1063
1064
1065 ret = info->si_sys_private;
1066
1067
1068 if (sig_ignored(p, sig))
1069 return ret;
1070
1071 if (LEGACY_QUEUE(&p->signal->shared_pending, sig))
1072
1073 return ret;
1074
1075
1076
1077
1078
1079
1080 ret = send_signal(sig, info, p, &p->signal->shared_pending);
1081 if (unlikely(ret))
1082 return ret;
1083
1084 __group_complete_signal(sig, p);
1085 return 0;
1086}
1087
1088
1089
1090
1091void zap_other_threads(struct task_struct *p)
1092{
1093 struct task_struct *t;
1094
1095 p->signal->group_stop_count = 0;
1096
1097 if (thread_group_empty(p))
1098 return;
1099
1100 for (t = next_thread(p); t != p; t = next_thread(t)) {
1101
1102
1103
1104 if (t->exit_state & (EXIT_ZOMBIE|EXIT_DEAD))
1105 continue;
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115 if (t != p->group_leader)
1116 t->exit_signal = -1;
1117
1118 sigaddset(&t->pending.signal, SIGKILL);
1119 rm_from_queue(SIG_KERNEL_STOP_MASK, &t->pending);
1120 signal_wake_up(t, 1);
1121 }
1122}
1123
1124
1125
1126
1127int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1128{
1129 unsigned long flags;
1130 int ret;
1131
1132 ret = check_kill_permission(sig, info, p);
1133 if (!ret && sig && p->sighand) {
1134 spin_lock_irqsave(&p->sighand->siglock, flags);
1135 ret = __group_send_sig_info(sig, info, p);
1136 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1137 }
1138
1139 return ret;
1140}
1141
1142
1143
1144
1145
1146
1147int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1148{
1149 struct task_struct *p;
1150 int retval, success;
1151
1152 if (pgrp <= 0)
1153 return -EINVAL;
1154
1155 success = 0;
1156 retval = -ESRCH;
1157 do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
1158 int err = group_send_sig_info(sig, info, p);
1159 success |= !err;
1160 retval = err;
1161 } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
1162 return success ? 0 : retval;
1163}
1164
1165int
1166kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1167{
1168 int retval;
1169
1170 read_lock(&tasklist_lock);
1171 retval = __kill_pg_info(sig, info, pgrp);
1172 read_unlock(&tasklist_lock);
1173
1174 return retval;
1175}
1176
1177int
1178kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1179{
1180 int error;
1181 struct task_struct *p;
1182
1183 read_lock(&tasklist_lock);
1184 p = find_task_by_pid(pid);
1185 error = -ESRCH;
1186 if (p)
1187 error = group_send_sig_info(sig, info, p);
1188 read_unlock(&tasklist_lock);
1189 return error;
1190}
1191
1192
1193int kill_proc_info_as_uid(int sig, struct siginfo *info, pid_t pid,
1194 uid_t uid, uid_t euid)
1195{
1196 int ret = -EINVAL;
1197 struct task_struct *p;
1198
1199 if (sig < 0 || sig > _NSIG)
1200 return ret;
1201
1202 read_lock(&tasklist_lock);
1203 p = find_task_by_pid(pid);
1204 if (!p) {
1205 ret = -ESRCH;
1206 goto out_unlock;
1207 }
1208 if ((!info || ((unsigned long)info != 1 &&
1209 (unsigned long)info != 2 && SI_FROMUSER(info)))
1210 && (euid != p->suid) && (euid != p->uid)
1211 && (uid != p->suid) && (uid != p->uid)) {
1212 ret = -EPERM;
1213 goto out_unlock;
1214 }
1215 if (sig && p->sighand) {
1216 unsigned long flags;
1217 spin_lock_irqsave(&p->sighand->siglock, flags);
1218 ret = __group_send_sig_info(sig, info, p);
1219 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1220 }
1221out_unlock:
1222 read_unlock(&tasklist_lock);
1223 return ret;
1224}
1225EXPORT_SYMBOL_GPL(kill_proc_info_as_uid);
1226
1227int print_fatal_signals = 0;
1228
1229static void print_fatal_signal(struct pt_regs *regs, int signr)
1230{
1231 int i;
1232 unsigned char insn;
1233 printk("%s/%d: potentially unexpected fatal signal %d.\n",
1234 current->comm, current->pid, signr);
1235
1236#ifdef __i386__
1237 printk("code at %08lx: ", regs->eip);
1238 for (i = 0; i < 16; i++) {
1239 __get_user(insn, (unsigned char *)(regs->eip + i));
1240 printk("%02x ", insn);
1241 }
1242#endif
1243 printk("\n");
1244 show_regs(regs);
1245}
1246
1247static int __init setup_print_fatal_signals(char *str)
1248{
1249 get_option (&str, &print_fatal_signals);
1250
1251 return 1;
1252}
1253
1254__setup("print-fatal-signals=", setup_print_fatal_signals);
1255
1256
1257
1258
1259
1260
1261
1262
1263static int kill_something_info(int sig, struct siginfo *info, int pid)
1264{
1265 if (!pid) {
1266 return kill_pg_info(sig, info, process_group(current));
1267 } else if (pid == -1) {
1268 int retval = 0, count = 0;
1269 struct task_struct * p;
1270
1271 read_lock(&tasklist_lock);
1272 for_each_process(p) {
1273 if (p->pid > 1 && p->tgid != current->tgid) {
1274 int err = group_send_sig_info(sig, info, p);
1275 ++count;
1276 if (err != -EPERM)
1277 retval = err;
1278 }
1279 }
1280 read_unlock(&tasklist_lock);
1281 return count ? retval : -ESRCH;
1282 } else if (pid < 0) {
1283 return kill_pg_info(sig, info, -pid);
1284 } else {
1285 return kill_proc_info(sig, info, pid);
1286 }
1287}
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297int
1298send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1299{
1300 int ret;
1301 unsigned long flags;
1302
1303
1304
1305
1306
1307 if (sig < 0 || sig > _NSIG)
1308 return -EINVAL;
1309
1310
1311
1312
1313
1314
1315
1316 read_lock(&tasklist_lock);
1317 spin_lock_irqsave(&p->sighand->siglock, flags);
1318 ret = specific_send_sig_info(sig, info, p);
1319 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1320 read_unlock(&tasklist_lock);
1321 return ret;
1322}
1323
1324int
1325send_sig(int sig, struct task_struct *p, int priv)
1326{
1327 return send_sig_info(sig, (void*)(long)(priv != 0), p);
1328}
1329
1330
1331
1332
1333
1334int
1335send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1336{
1337 int ret;
1338 read_lock(&tasklist_lock);
1339 ret = group_send_sig_info(sig, info, p);
1340 read_unlock(&tasklist_lock);
1341 return ret;
1342}
1343
1344void
1345force_sig(int sig, struct task_struct *p)
1346{
1347 force_sig_info(sig, (void*)1L, p);
1348}
1349
1350
1351
1352
1353
1354
1355
1356int
1357force_sigsegv(int sig, struct task_struct *p)
1358{
1359 if (sig == SIGSEGV) {
1360 unsigned long flags;
1361 spin_lock_irqsave(&p->sighand->siglock, flags);
1362 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1363 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1364 }
1365 force_sig(SIGSEGV, p);
1366 return 0;
1367}
1368
1369int
1370kill_pg(pid_t pgrp, int sig, int priv)
1371{
1372 return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1373}
1374
1375int
1376kill_proc(pid_t pid, int sig, int priv)
1377{
1378 return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1379}
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391struct sigqueue *sigqueue_alloc(void)
1392{
1393 struct sigqueue *q;
1394
1395 if ((q = __sigqueue_alloc()))
1396 q->flags |= SIGQUEUE_PREALLOC;
1397 return(q);
1398}
1399
1400void sigqueue_free(struct sigqueue *q)
1401{
1402 unsigned long flags;
1403 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1404
1405
1406
1407
1408 if (unlikely(!list_empty(&q->list))) {
1409 read_lock(&tasklist_lock);
1410 spin_lock_irqsave(q->lock, flags);
1411 if (!list_empty(&q->list))
1412 list_del_init(&q->list);
1413 spin_unlock_irqrestore(q->lock, flags);
1414 read_unlock(&tasklist_lock);
1415 }
1416 q->flags &= ~SIGQUEUE_PREALLOC;
1417 __sigqueue_free(q);
1418}
1419
1420int
1421send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1422{
1423 unsigned long flags;
1424 int ret = 0;
1425
1426
1427
1428
1429
1430
1431
1432 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1433 read_lock(&tasklist_lock);
1434 spin_lock_irqsave(&p->sighand->siglock, flags);
1435
1436 if (unlikely(!list_empty(&q->list))) {
1437
1438
1439
1440
1441 if (q->info.si_code != SI_TIMER)
1442 BUG();
1443 q->info.si_overrun++;
1444 goto out;
1445 }
1446
1447 if (sig_ignored(p, sig)) {
1448 ret = 1;
1449 goto out;
1450 }
1451
1452 q->lock = &p->sighand->siglock;
1453 list_add_tail(&q->list, &p->pending.list);
1454 sigaddset(&p->pending.signal, sig);
1455 if (!sigismember(&p->blocked, sig))
1456 signal_wake_up(p, sig == SIGKILL);
1457
1458out:
1459 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1460 read_unlock(&tasklist_lock);
1461 return(ret);
1462}
1463
1464int
1465send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1466{
1467 unsigned long flags;
1468 int ret = 0;
1469
1470 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1471 read_lock(&tasklist_lock);
1472 spin_lock_irqsave(&p->sighand->siglock, flags);
1473 handle_stop_signal(sig, p);
1474
1475
1476 if (sig_ignored(p, sig)) {
1477 ret = 1;
1478 goto out;
1479 }
1480
1481 if (unlikely(!list_empty(&q->list))) {
1482
1483
1484
1485
1486
1487 if (q->info.si_code != SI_TIMER)
1488 BUG();
1489 q->info.si_overrun++;
1490 goto out;
1491 }
1492
1493
1494
1495
1496
1497
1498 q->lock = &p->sighand->siglock;
1499 list_add_tail(&q->list, &p->signal->shared_pending.list);
1500 sigaddset(&p->signal->shared_pending.signal, sig);
1501
1502 __group_complete_signal(sig, p);
1503out:
1504 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1505 read_unlock(&tasklist_lock);
1506 return(ret);
1507}
1508
1509
1510
1511
1512
1513static void __wake_up_parent(struct task_struct *p,
1514 struct task_struct *parent)
1515{
1516 struct task_struct *tsk = parent;
1517
1518
1519
1520
1521 if (p->tgid == tsk->tgid) {
1522 wake_up_interruptible_sync(&tsk->wait_chldexit);
1523 return;
1524 }
1525
1526 do {
1527 wake_up_interruptible_sync(&tsk->wait_chldexit);
1528 tsk = next_thread(tsk);
1529 if (tsk->signal != parent->signal)
1530 BUG();
1531 } while (tsk != parent);
1532}
1533
1534
1535
1536
1537
1538
1539void do_notify_parent(struct task_struct *tsk, int sig)
1540{
1541 struct siginfo info;
1542 unsigned long flags;
1543 struct sighand_struct *psig;
1544
1545 if (sig == -1)
1546 BUG();
1547
1548
1549 BUG_ON(tsk->state & (TASK_STOPPED|TASK_TRACED));
1550
1551 BUG_ON(!tsk->ptrace &&
1552 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1553
1554 info.si_signo = sig;
1555 info.si_errno = 0;
1556 info.si_pid = tsk->pid;
1557 info.si_uid = tsk->uid;
1558
1559
1560 info.si_utime = tsk->utime + tsk->signal->utime;
1561 info.si_stime = tsk->stime + tsk->signal->stime;
1562
1563 info.si_status = tsk->exit_code & 0x7f;
1564 if (tsk->exit_code & 0x80)
1565 info.si_code = CLD_DUMPED;
1566 else if (tsk->exit_code & 0x7f)
1567 info.si_code = CLD_KILLED;
1568 else {
1569 info.si_code = CLD_EXITED;
1570 info.si_status = tsk->exit_code >> 8;
1571 }
1572
1573 psig = tsk->parent->sighand;
1574 spin_lock_irqsave(&psig->siglock, flags);
1575 if (!tsk->ptrace && sig == SIGCHLD &&
1576 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1577 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593 tsk->exit_signal = -1;
1594 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1595 sig = 0;
1596 }
1597 if (sig > 0 && sig <= _NSIG)
1598 __group_send_sig_info(sig, &info, tsk->parent);
1599 __wake_up_parent(tsk, tsk->parent);
1600 spin_unlock_irqrestore(&psig->siglock, flags);
1601}
1602
1603static void
1604do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent,
1605 int why)
1606{
1607 struct siginfo info;
1608 unsigned long flags;
1609 struct sighand_struct *sighand;
1610
1611 info.si_signo = SIGCHLD;
1612 info.si_errno = 0;
1613 info.si_pid = tsk->pid;
1614 info.si_uid = tsk->uid;
1615
1616
1617 info.si_utime = tsk->utime;
1618 info.si_stime = tsk->stime;
1619
1620 info.si_code = why;
1621 switch (why) {
1622 case CLD_CONTINUED:
1623 info.si_status = SIGCONT;
1624 break;
1625 case CLD_STOPPED:
1626 info.si_status = tsk->signal->group_exit_code & 0x7f;
1627 break;
1628 case CLD_TRAPPED:
1629 info.si_status = tsk->exit_code & 0x7f;
1630 break;
1631 default:
1632 BUG();
1633 }
1634
1635 sighand = parent->sighand;
1636 spin_lock_irqsave(&sighand->siglock, flags);
1637 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1638 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1639 __group_send_sig_info(SIGCHLD, &info, parent);
1640
1641
1642
1643 __wake_up_parent(tsk, parent);
1644 spin_unlock_irqrestore(&sighand->siglock, flags);
1645}
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658static void ptrace_stop(int exit_code, int nostop_code, siginfo_t *info)
1659{
1660
1661
1662
1663
1664 if (current->signal->group_stop_count > 0)
1665 --current->signal->group_stop_count;
1666
1667 current->last_siginfo = info;
1668 current->exit_code = exit_code;
1669
1670
1671 set_current_state(TASK_TRACED);
1672 spin_unlock_irq(¤t->sighand->siglock);
1673 read_lock(&tasklist_lock);
1674 if (likely(current->ptrace & PT_PTRACED) &&
1675 likely(current->parent != current->real_parent ||
1676 !(current->ptrace & PT_ATTACHED)) &&
1677 (likely(current->parent->signal != current->signal) ||
1678 !unlikely(current->signal->group_exit))) {
1679 do_notify_parent_cldstop(current, current->parent,
1680 CLD_TRAPPED);
1681 read_unlock(&tasklist_lock);
1682 schedule();
1683 } else {
1684
1685
1686
1687
1688 read_unlock(&tasklist_lock);
1689 set_current_state(TASK_RUNNING);
1690 current->exit_code = nostop_code;
1691 }
1692
1693
1694
1695
1696
1697
1698 spin_lock_irq(¤t->sighand->siglock);
1699 current->last_siginfo = NULL;
1700
1701
1702
1703
1704
1705
1706 recalc_sigpending_tsk(current);
1707}
1708
1709void ptrace_notify(int exit_code)
1710{
1711 siginfo_t info;
1712
1713 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
1714
1715 memset(&info, 0, sizeof info);
1716 info.si_signo = SIGTRAP;
1717 info.si_code = exit_code;
1718 info.si_pid = current->pid;
1719 info.si_uid = current->uid;
1720
1721
1722 spin_lock_irq(¤t->sighand->siglock);
1723 ptrace_stop(exit_code, 0, &info);
1724 spin_unlock_irq(¤t->sighand->siglock);
1725}
1726
1727#ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1728
1729static void
1730finish_stop(int stop_count)
1731{
1732
1733
1734
1735
1736
1737 if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1738 read_lock(&tasklist_lock);
1739 do_notify_parent_cldstop(current, current->parent,
1740 CLD_STOPPED);
1741 read_unlock(&tasklist_lock);
1742 }
1743 else if (stop_count == 0) {
1744 read_lock(&tasklist_lock);
1745 do_notify_parent_cldstop(current->group_leader,
1746 current->group_leader->real_parent,
1747 CLD_STOPPED);
1748 read_unlock(&tasklist_lock);
1749 }
1750
1751 schedule();
1752
1753
1754
1755 current->exit_code = 0;
1756}
1757
1758
1759
1760
1761
1762static void
1763do_signal_stop(int signr)
1764{
1765 struct signal_struct *sig = current->signal;
1766 struct sighand_struct *sighand = current->sighand;
1767 int stop_count = -1;
1768
1769
1770
1771 if (sig->group_stop_count > 0) {
1772
1773
1774
1775
1776 signr = sig->group_exit_code;
1777 stop_count = --sig->group_stop_count;
1778 current->exit_code = signr;
1779 set_current_state(TASK_STOPPED);
1780 if (stop_count == 0)
1781 sig->stop_state = 1;
1782 spin_unlock_irq(&sighand->siglock);
1783 }
1784 else if (thread_group_empty(current)) {
1785
1786
1787
1788 current->exit_code = current->signal->group_exit_code = signr;
1789 set_current_state(TASK_STOPPED);
1790 sig->stop_state = 1;
1791 spin_unlock_irq(&sighand->siglock);
1792 }
1793 else {
1794
1795
1796
1797
1798
1799
1800
1801
1802 struct task_struct *t;
1803
1804 spin_unlock_irq(&sighand->siglock);
1805
1806
1807
1808 read_lock(&tasklist_lock);
1809 spin_lock_irq(&sighand->siglock);
1810
1811 if (unlikely(sig->group_exit)) {
1812
1813
1814
1815
1816
1817 spin_unlock_irq(&sighand->siglock);
1818 read_unlock(&tasklist_lock);
1819 return;
1820 }
1821
1822 if (unlikely(sig_avoid_stop_race())) {
1823
1824
1825
1826
1827 spin_unlock_irq(&sighand->siglock);
1828 read_unlock(&tasklist_lock);
1829 return;
1830 }
1831
1832 if (sig->group_stop_count == 0) {
1833 sig->group_exit_code = signr;
1834 stop_count = 0;
1835 for (t = next_thread(current); t != current;
1836 t = next_thread(t))
1837
1838
1839
1840
1841
1842 if (t->state < TASK_STOPPED) {
1843 stop_count++;
1844 signal_wake_up(t, 0);
1845 }
1846 sig->group_stop_count = stop_count;
1847 }
1848 else {
1849
1850 signr = sig->group_exit_code;
1851 stop_count = --sig->group_stop_count;
1852 }
1853
1854 current->exit_code = signr;
1855 set_current_state(TASK_STOPPED);
1856 if (stop_count == 0)
1857 sig->stop_state = 1;
1858
1859 spin_unlock_irq(&sighand->siglock);
1860 read_unlock(&tasklist_lock);
1861 }
1862
1863 finish_stop(stop_count);
1864}
1865
1866
1867
1868
1869
1870
1871
1872static inline int handle_group_stop(void)
1873{
1874 int stop_count;
1875
1876 if (current->signal->group_exit_task == current) {
1877
1878
1879
1880
1881 current->signal->group_exit_task = NULL;
1882 return 0;
1883 }
1884
1885 if (current->signal->group_exit)
1886
1887
1888
1889
1890
1891 return 0;
1892
1893
1894
1895
1896
1897 stop_count = --current->signal->group_stop_count;
1898 if (stop_count == 0)
1899 current->signal->stop_state = 1;
1900 current->exit_code = current->signal->group_exit_code;
1901 set_current_state(TASK_STOPPED);
1902 spin_unlock_irq(¤t->sighand->siglock);
1903 finish_stop(stop_count);
1904 return 1;
1905}
1906
1907int get_signal_to_deliver(siginfo_t *info, struct k_sigaction *return_ka,
1908 struct pt_regs *regs, void *cookie)
1909{
1910 sigset_t *mask = ¤t->blocked;
1911 int signr = 0;
1912
1913relock:
1914 spin_lock_irq(¤t->sighand->siglock);
1915 for (;;) {
1916 struct k_sigaction *ka;
1917
1918 if (unlikely(current->signal->group_stop_count > 0) &&
1919 handle_group_stop())
1920 goto relock;
1921
1922 signr = dequeue_signal(current, mask, info);
1923
1924 if (!signr)
1925 break;
1926
1927 if ((signr == SIGSEGV) && print_fatal_signals) {
1928 spin_unlock_irq(¤t->sighand->siglock);
1929 print_fatal_signal(regs, signr);
1930 spin_lock_irq(¤t->sighand->siglock);
1931 }
1932 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1933 ptrace_signal_deliver(regs, cookie);
1934
1935
1936 ptrace_stop(signr, signr, info);
1937
1938
1939 signr = current->exit_code;
1940 if (signr == 0)
1941 continue;
1942
1943 current->exit_code = 0;
1944
1945
1946
1947
1948
1949 if (signr != info->si_signo) {
1950 info->si_signo = signr;
1951 info->si_errno = 0;
1952 info->si_code = SI_USER;
1953 info->si_pid = current->parent->pid;
1954 info->si_uid = current->parent->uid;
1955 }
1956
1957
1958 if (sigismember(¤t->blocked, signr)) {
1959 specific_send_sig_info(signr, info, current);
1960 continue;
1961 }
1962 }
1963
1964 ka = ¤t->sighand->action[signr-1];
1965 if (ka->sa.sa_handler == SIG_IGN)
1966 continue;
1967 if (ka->sa.sa_handler != SIG_DFL) {
1968
1969 *return_ka = *ka;
1970
1971 if (ka->sa.sa_flags & SA_ONESHOT)
1972 ka->sa.sa_handler = SIG_DFL;
1973
1974 break;
1975 }
1976
1977
1978
1979
1980 if (sig_kernel_ignore(signr))
1981 continue;
1982
1983
1984 if (current->pid == 1)
1985 continue;
1986
1987 if (sig_kernel_stop(signr)) {
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998 if (signr == SIGSTOP) {
1999 do_signal_stop(signr);
2000 goto relock;
2001 }
2002 spin_unlock_irq(¤t->sighand->siglock);
2003
2004
2005
2006 if (is_orphaned_pgrp(process_group(current)))
2007 goto relock;
2008
2009 spin_lock_irq(¤t->sighand->siglock);
2010 if (unlikely(sig_avoid_stop_race())) {
2011
2012
2013
2014
2015 continue;
2016 }
2017
2018 do_signal_stop(signr);
2019 goto relock;
2020 }
2021
2022 spin_unlock_irq(¤t->sighand->siglock);
2023
2024
2025
2026
2027 current->flags |= PF_SIGNALED;
2028 if (print_fatal_signals)
2029 print_fatal_signal(regs, signr);
2030 if (sig_kernel_coredump(signr)) {
2031
2032
2033
2034
2035
2036
2037
2038
2039 do_coredump((long)signr, signr, regs);
2040 }
2041
2042
2043
2044
2045 do_group_exit(signr);
2046
2047 }
2048 spin_unlock_irq(¤t->sighand->siglock);
2049 return signr;
2050}
2051
2052#endif
2053
2054EXPORT_SYMBOL(recalc_sigpending);
2055EXPORT_SYMBOL_GPL(dequeue_signal);
2056EXPORT_SYMBOL(flush_signals);
2057EXPORT_SYMBOL(force_sig);
2058EXPORT_SYMBOL(kill_pg);
2059EXPORT_SYMBOL(kill_proc);
2060EXPORT_SYMBOL(ptrace_notify);
2061EXPORT_SYMBOL(send_sig);
2062EXPORT_SYMBOL(send_sig_info);
2063EXPORT_SYMBOL(sigprocmask);
2064EXPORT_SYMBOL(block_all_signals);
2065EXPORT_SYMBOL(unblock_all_signals);
2066
2067
2068
2069
2070
2071
2072asmlinkage long sys_restart_syscall(void)
2073{
2074 struct restart_block *restart = ¤t_thread_info()->restart_block;
2075 return restart->fn(restart);
2076}
2077
2078long do_no_restart_syscall(struct restart_block *param)
2079{
2080 return -EINTR;
2081}
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2098{
2099 int error;
2100 sigset_t old_block;
2101
2102 spin_lock_irq(¤t->sighand->siglock);
2103 old_block = current->blocked;
2104 error = 0;
2105 switch (how) {
2106 case SIG_BLOCK:
2107 sigorsets(¤t->blocked, ¤t->blocked, set);
2108 break;
2109 case SIG_UNBLOCK:
2110 signandsets(¤t->blocked, ¤t->blocked, set);
2111 break;
2112 case SIG_SETMASK:
2113 current->blocked = *set;
2114 break;
2115 default:
2116 error = -EINVAL;
2117 }
2118 recalc_sigpending();
2119 spin_unlock_irq(¤t->sighand->siglock);
2120 if (oldset)
2121 *oldset = old_block;
2122 return error;
2123}
2124
2125asmlinkage long
2126sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
2127{
2128 int error = -EINVAL;
2129 sigset_t old_set, new_set;
2130
2131
2132 if (sigsetsize != sizeof(sigset_t))
2133 goto out;
2134
2135 if (set) {
2136 error = -EFAULT;
2137 if (copy_from_user(&new_set, set, sizeof(*set)))
2138 goto out;
2139 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2140
2141 error = sigprocmask(how, &new_set, &old_set);
2142 if (error)
2143 goto out;
2144 if (oset)
2145 goto set_old;
2146 } else if (oset) {
2147 spin_lock_irq(¤t->sighand->siglock);
2148 old_set = current->blocked;
2149 spin_unlock_irq(¤t->sighand->siglock);
2150
2151 set_old:
2152 error = -EFAULT;
2153 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2154 goto out;
2155 }
2156 error = 0;
2157out:
2158 return error;
2159}
2160
2161long do_sigpending(void __user *set, unsigned long sigsetsize)
2162{
2163 long error = -EINVAL;
2164 sigset_t pending;
2165
2166 if (sigsetsize > sizeof(sigset_t))
2167 goto out;
2168
2169 spin_lock_irq(¤t->sighand->siglock);
2170 sigorsets(&pending, ¤t->pending.signal,
2171 ¤t->signal->shared_pending.signal);
2172 spin_unlock_irq(¤t->sighand->siglock);
2173
2174
2175 sigandsets(&pending, ¤t->blocked, &pending);
2176
2177 error = -EFAULT;
2178 if (!copy_to_user(set, &pending, sigsetsize))
2179 error = 0;
2180
2181out:
2182 return error;
2183}
2184
2185asmlinkage long
2186sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2187{
2188 return do_sigpending(set, sigsetsize);
2189}
2190
2191#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2192
2193int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2194{
2195 int err;
2196
2197 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2198 return -EFAULT;
2199 if (from->si_code < 0)
2200 return __copy_to_user(to, from, sizeof(siginfo_t))
2201 ? -EFAULT : 0;
2202
2203
2204
2205
2206
2207
2208
2209 err = __put_user(from->si_signo, &to->si_signo);
2210 err |= __put_user(from->si_errno, &to->si_errno);
2211 err |= __put_user((short)from->si_code, &to->si_code);
2212 switch (from->si_code & __SI_MASK) {
2213 case __SI_KILL:
2214 err |= __put_user(from->si_pid, &to->si_pid);
2215 err |= __put_user(from->si_uid, &to->si_uid);
2216 break;
2217 case __SI_TIMER:
2218 err |= __put_user(from->si_tid, &to->si_tid);
2219 err |= __put_user(from->si_overrun, &to->si_overrun);
2220 err |= __put_user(from->si_ptr, &to->si_ptr);
2221 break;
2222 case __SI_POLL:
2223 err |= __put_user(from->si_band, &to->si_band);
2224 err |= __put_user(from->si_fd, &to->si_fd);
2225 break;
2226 case __SI_FAULT:
2227 err |= __put_user(from->si_addr, &to->si_addr);
2228#ifdef __ARCH_SI_TRAPNO
2229 err |= __put_user(from->si_trapno, &to->si_trapno);
2230#endif
2231 break;
2232 case __SI_CHLD:
2233 err |= __put_user(from->si_pid, &to->si_pid);
2234 err |= __put_user(from->si_uid, &to->si_uid);
2235 err |= __put_user(from->si_status, &to->si_status);
2236 err |= __put_user(from->si_utime, &to->si_utime);
2237 err |= __put_user(from->si_stime, &to->si_stime);
2238 break;
2239 case __SI_RT:
2240 case __SI_MESGQ:
2241 err |= __put_user(from->si_pid, &to->si_pid);
2242 err |= __put_user(from->si_uid, &to->si_uid);
2243 err |= __put_user(from->si_ptr, &to->si_ptr);
2244 break;
2245 default:
2246 err |= __put_user(from->si_pid, &to->si_pid);
2247 err |= __put_user(from->si_uid, &to->si_uid);
2248 break;
2249 }
2250 return err;
2251}
2252
2253#endif
2254
2255asmlinkage long
2256sys_rt_sigtimedwait(const sigset_t __user *uthese,
2257 siginfo_t __user *uinfo,
2258 const struct timespec __user *uts,
2259 size_t sigsetsize)
2260{
2261 int ret, sig;
2262 sigset_t these;
2263 struct timespec ts;
2264 siginfo_t info;
2265 long timeout = 0;
2266
2267
2268 if (sigsetsize != sizeof(sigset_t))
2269 return -EINVAL;
2270
2271 if (copy_from_user(&these, uthese, sizeof(these)))
2272 return -EFAULT;
2273
2274
2275
2276
2277
2278 sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2279 signotset(&these);
2280
2281 if (uts) {
2282 if (copy_from_user(&ts, uts, sizeof(ts)))
2283 return -EFAULT;
2284 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2285 || ts.tv_sec < 0)
2286 return -EINVAL;
2287 }
2288
2289 spin_lock_irq(¤t->sighand->siglock);
2290 sig = dequeue_signal(current, &these, &info);
2291 if (!sig) {
2292 timeout = MAX_SCHEDULE_TIMEOUT;
2293 if (uts)
2294 timeout = (timespec_to_jiffies(&ts)
2295 + (ts.tv_sec || ts.tv_nsec));
2296
2297 if (timeout) {
2298
2299
2300
2301 current->real_blocked = current->blocked;
2302 sigandsets(¤t->blocked, ¤t->blocked, &these);
2303 recalc_sigpending();
2304 spin_unlock_irq(¤t->sighand->siglock);
2305
2306 current->state = TASK_INTERRUPTIBLE;
2307 timeout = schedule_timeout(timeout);
2308
2309 spin_lock_irq(¤t->sighand->siglock);
2310 sig = dequeue_signal(current, &these, &info);
2311 current->blocked = current->real_blocked;
2312 siginitset(¤t->real_blocked, 0);
2313 recalc_sigpending();
2314 }
2315 }
2316 spin_unlock_irq(¤t->sighand->siglock);
2317
2318 if (sig) {
2319 ret = sig;
2320 if (uinfo) {
2321 if (copy_siginfo_to_user(uinfo, &info))
2322 ret = -EFAULT;
2323 }
2324 } else {
2325 ret = -EAGAIN;
2326 if (timeout)
2327 ret = -EINTR;
2328 }
2329
2330 return ret;
2331}
2332
2333asmlinkage long
2334sys_kill(int pid, int sig)
2335{
2336 struct siginfo info;
2337
2338 info.si_signo = sig;
2339 info.si_errno = 0;
2340 info.si_code = SI_USER;
2341 info.si_pid = current->tgid;
2342 info.si_uid = current->uid;
2343
2344 return kill_something_info(sig, &info, pid);
2345}
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2358{
2359 struct siginfo info;
2360 int error;
2361 struct task_struct *p;
2362
2363
2364 if (pid <= 0 || tgid <= 0)
2365 return -EINVAL;
2366
2367 info.si_signo = sig;
2368 info.si_errno = 0;
2369 info.si_code = SI_TKILL;
2370 info.si_pid = current->tgid;
2371 info.si_uid = current->uid;
2372
2373 read_lock(&tasklist_lock);
2374 p = find_task_by_pid(pid);
2375 error = -ESRCH;
2376 if (p && (p->tgid == tgid)) {
2377 error = check_kill_permission(sig, &info, p);
2378
2379
2380
2381
2382 if (!error && sig && p->sighand) {
2383 spin_lock_irq(&p->sighand->siglock);
2384 handle_stop_signal(sig, p);
2385 error = specific_send_sig_info(sig, &info, p);
2386 spin_unlock_irq(&p->sighand->siglock);
2387 }
2388 }
2389 read_unlock(&tasklist_lock);
2390 return error;
2391}
2392
2393
2394
2395
2396asmlinkage long
2397sys_tkill(int pid, int sig)
2398{
2399 struct siginfo info;
2400 int error;
2401 struct task_struct *p;
2402
2403
2404 if (pid <= 0)
2405 return -EINVAL;
2406
2407 info.si_signo = sig;
2408 info.si_errno = 0;
2409 info.si_code = SI_TKILL;
2410 info.si_pid = current->tgid;
2411 info.si_uid = current->uid;
2412
2413 read_lock(&tasklist_lock);
2414 p = find_task_by_pid(pid);
2415 error = -ESRCH;
2416 if (p) {
2417 error = check_kill_permission(sig, &info, p);
2418
2419
2420
2421
2422 if (!error && sig && p->sighand) {
2423 spin_lock_irq(&p->sighand->siglock);
2424 handle_stop_signal(sig, p);
2425 error = specific_send_sig_info(sig, &info, p);
2426 spin_unlock_irq(&p->sighand->siglock);
2427 }
2428 }
2429 read_unlock(&tasklist_lock);
2430 return error;
2431}
2432
2433asmlinkage long
2434sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2435{
2436 siginfo_t info;
2437
2438 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2439 return -EFAULT;
2440
2441
2442
2443 if (info.si_code >= 0)
2444 return -EPERM;
2445 info.si_signo = sig;
2446
2447
2448 return kill_proc_info(sig, &info, pid);
2449}
2450
2451int
2452do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
2453{
2454 struct k_sigaction *k;
2455
2456 if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2457 return -EINVAL;
2458
2459 k = ¤t->sighand->action[sig-1];
2460
2461 spin_lock_irq(¤t->sighand->siglock);
2462 if (signal_pending(current)) {
2463
2464
2465
2466
2467 spin_unlock_irq(¤t->sighand->siglock);
2468 return -ERESTARTNOINTR;
2469 }
2470
2471 if (oact)
2472 *oact = *k;
2473
2474 if (act) {
2475 sigdelsetmask(&act->sa.sa_mask,
2476 sigmask(SIGKILL) | sigmask(SIGSTOP));
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488 if (act->sa.sa_handler == SIG_IGN ||
2489 (act->sa.sa_handler == SIG_DFL &&
2490 sig_kernel_ignore(sig))) {
2491
2492
2493
2494
2495
2496
2497 struct task_struct *t = current;
2498 spin_unlock_irq(&t->sighand->siglock);
2499 read_lock(&tasklist_lock);
2500 spin_lock_irq(&t->sighand->siglock);
2501 *k = *act;
2502 rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2503 do {
2504 rm_from_queue(sigmask(sig), &t->pending);
2505 recalc_sigpending_and_wake(t);
2506 t = next_thread(t);
2507 } while (t != current);
2508 spin_unlock_irq(¤t->sighand->siglock);
2509 read_unlock(&tasklist_lock);
2510 return 0;
2511 }
2512
2513 *k = *act;
2514 }
2515
2516 spin_unlock_irq(¤t->sighand->siglock);
2517 return 0;
2518}
2519
2520int
2521do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2522{
2523 stack_t oss;
2524 int error;
2525
2526 if (uoss) {
2527 oss.ss_sp = (void __user *) current->sas_ss_sp;
2528 oss.ss_size = current->sas_ss_size;
2529 oss.ss_flags = sas_ss_flags(sp);
2530 }
2531
2532 if (uss) {
2533 void __user *ss_sp;
2534 size_t ss_size;
2535 int ss_flags;
2536
2537 error = -EFAULT;
2538 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2539 || __get_user(ss_sp, &uss->ss_sp)
2540 || __get_user(ss_flags, &uss->ss_flags)
2541 || __get_user(ss_size, &uss->ss_size))
2542 goto out;
2543
2544 error = -EPERM;
2545 if (on_sig_stack(sp))
2546 goto out;
2547
2548 error = -EINVAL;
2549
2550
2551
2552
2553
2554
2555
2556
2557 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2558 goto out;
2559
2560 if (ss_flags == SS_DISABLE) {
2561 ss_size = 0;
2562 ss_sp = NULL;
2563 } else {
2564 error = -ENOMEM;
2565 if (ss_size < MINSIGSTKSZ)
2566 goto out;
2567 }
2568
2569 current->sas_ss_sp = (unsigned long) ss_sp;
2570 current->sas_ss_size = ss_size;
2571 }
2572
2573 if (uoss) {
2574 error = -EFAULT;
2575 if (copy_to_user(uoss, &oss, sizeof(oss)))
2576 goto out;
2577 }
2578
2579 error = 0;
2580out:
2581 return error;
2582}
2583
2584#ifdef __ARCH_WANT_SYS_SIGPENDING
2585
2586asmlinkage long
2587sys_sigpending(old_sigset_t __user *set)
2588{
2589 return do_sigpending(set, sizeof(*set));
2590}
2591
2592#endif
2593
2594#ifdef __ARCH_WANT_SYS_SIGPROCMASK
2595
2596
2597
2598asmlinkage long
2599sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2600{
2601 int error;
2602 old_sigset_t old_set, new_set;
2603
2604 if (set) {
2605 error = -EFAULT;
2606 if (copy_from_user(&new_set, set, sizeof(*set)))
2607 goto out;
2608 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2609
2610 spin_lock_irq(¤t->sighand->siglock);
2611 old_set = current->blocked.sig[0];
2612
2613 error = 0;
2614 switch (how) {
2615 default:
2616 error = -EINVAL;
2617 break;
2618 case SIG_BLOCK:
2619 sigaddsetmask(¤t->blocked, new_set);
2620 break;
2621 case SIG_UNBLOCK:
2622 sigdelsetmask(¤t->blocked, new_set);
2623 break;
2624 case SIG_SETMASK:
2625 current->blocked.sig[0] = new_set;
2626 break;
2627 }
2628
2629 recalc_sigpending();
2630 spin_unlock_irq(¤t->sighand->siglock);
2631 if (error)
2632 goto out;
2633 if (oset)
2634 goto set_old;
2635 } else if (oset) {
2636 old_set = current->blocked.sig[0];
2637 set_old:
2638 error = -EFAULT;
2639 if (copy_to_user(oset, &a