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