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