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