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