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