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