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