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