patch-2.6.6-vs1.9.0
[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         if (!vx_check(vx_task_xid(p), VX_ADMIN|VX_WATCH|VX_IDENT))
1054                 return -ESRCH;
1055
1056         ret = check_kill_permission(sig, info, p);
1057         if (!ret && sig && p->sighand) {
1058                 spin_lock_irqsave(&p->sighand->siglock, flags);
1059                 ret = __group_send_sig_info(sig, info, p);
1060                 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1061         }
1062
1063         return ret;
1064 }
1065
1066 /*
1067  * kill_pg_info() sends a signal to a process group: this is what the tty
1068  * control characters do (^C, ^Z etc)
1069  */
1070
1071 int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1072 {
1073         struct task_struct *p;
1074         struct list_head *l;
1075         struct pid *pid;
1076         int retval;
1077         int found;
1078
1079         if (pgrp <= 0)
1080                 return -EINVAL;
1081
1082         found = 0;
1083         retval = 0;
1084         for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
1085                 int err;
1086
1087                 found = 1;
1088                 err = group_send_sig_info(sig, info, p);
1089                 if (!retval)
1090                         retval = err;
1091         }
1092         return found ? retval : -ESRCH;
1093 }
1094
1095 int
1096 kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
1097 {
1098         int retval;
1099
1100         read_lock(&tasklist_lock);
1101         retval = __kill_pg_info(sig, info, pgrp);
1102         read_unlock(&tasklist_lock);
1103
1104         return retval;
1105 }
1106
1107 /*
1108  * kill_sl_info() sends a signal to the session leader: this is used
1109  * to send SIGHUP to the controlling process of a terminal when
1110  * the connection is lost.
1111  */
1112
1113
1114 int
1115 kill_sl_info(int sig, struct siginfo *info, pid_t sid)
1116 {
1117         int err, retval = -EINVAL;
1118         struct pid *pid;
1119         struct list_head *l;
1120         struct task_struct *p;
1121
1122         if (sid <= 0)
1123                 goto out;
1124
1125         retval = -ESRCH;
1126         read_lock(&tasklist_lock);
1127         for_each_task_pid(sid, PIDTYPE_SID, p, l, pid) {
1128                 if (!p->signal->leader)
1129                         continue;
1130                 err = group_send_sig_info(sig, info, p);
1131                 if (retval)
1132                         retval = err;
1133         }
1134         read_unlock(&tasklist_lock);
1135 out:
1136         return retval;
1137 }
1138
1139 int
1140 kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1141 {
1142         int error;
1143         struct task_struct *p;
1144
1145         read_lock(&tasklist_lock);
1146         p = find_task_by_pid(pid);
1147         error = -ESRCH;
1148         if (p)
1149                 error = group_send_sig_info(sig, info, p);
1150         read_unlock(&tasklist_lock);
1151         return error;
1152 }
1153
1154
1155 /*
1156  * kill_something_info() interprets pid in interesting ways just like kill(2).
1157  *
1158  * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1159  * is probably wrong.  Should make it like BSD or SYSV.
1160  */
1161
1162 static int kill_something_info(int sig, struct siginfo *info, int pid)
1163 {
1164         if (!pid) {
1165                 return kill_pg_info(sig, info, process_group(current));
1166         } else if (pid == -1) {
1167                 int retval = 0, count = 0;
1168                 struct task_struct * p;
1169
1170                 read_lock(&tasklist_lock);
1171                 for_each_process(p) {
1172                         if (p->pid > 1 && p->tgid != current->tgid) {
1173                                 int err = group_send_sig_info(sig, info, p);
1174                                 ++count;
1175                                 if (err != -EPERM)
1176                                         retval = err;
1177                         }
1178                 }
1179                 read_unlock(&tasklist_lock);
1180                 return count ? retval : -ESRCH;
1181         } else if (pid < 0) {
1182                 return kill_pg_info(sig, info, -pid);
1183         } else {
1184                 return kill_proc_info(sig, info, pid);
1185         }
1186 }
1187
1188 /*
1189  * These are for backward compatibility with the rest of the kernel source.
1190  */
1191
1192 /*
1193  * These two are the most common entry points.  They send a signal
1194  * just to the specific thread.
1195  */
1196 int
1197 send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1198 {
1199         int ret;
1200         unsigned long flags;
1201
1202         /*
1203          * We need the tasklist lock even for the specific
1204          * thread case (when we don't need to follow the group
1205          * lists) in order to avoid races with "p->sighand"
1206          * going away or changing from under us.
1207          */
1208         read_lock(&tasklist_lock);  
1209         spin_lock_irqsave(&p->sighand->siglock, flags);
1210         ret = specific_send_sig_info(sig, info, p);
1211         spin_unlock_irqrestore(&p->sighand->siglock, flags);
1212         read_unlock(&tasklist_lock);
1213         return ret;
1214 }
1215
1216 int
1217 send_sig(int sig, struct task_struct *p, int priv)
1218 {
1219         return send_sig_info(sig, (void*)(long)(priv != 0), p);
1220 }
1221
1222 /*
1223  * This is the entry point for "process-wide" signals.
1224  * They will go to an appropriate thread in the thread group.
1225  */
1226 int
1227 send_group_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1228 {
1229         int ret;
1230         read_lock(&tasklist_lock);
1231         ret = group_send_sig_info(sig, info, p);
1232         read_unlock(&tasklist_lock);
1233         return ret;
1234 }
1235
1236 void
1237 force_sig(int sig, struct task_struct *p)
1238 {
1239         force_sig_info(sig, (void*)1L, p);
1240 }
1241
1242 int
1243 kill_pg(pid_t pgrp, int sig, int priv)
1244 {
1245         return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1246 }
1247
1248 int
1249 kill_sl(pid_t sess, int sig, int priv)
1250 {
1251         return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
1252 }
1253
1254 int
1255 kill_proc(pid_t pid, int sig, int priv)
1256 {
1257         return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1258 }
1259
1260 /*
1261  * These functions support sending signals using preallocated sigqueue
1262  * structures.  This is needed "because realtime applications cannot
1263  * afford to lose notifications of asynchronous events, like timer
1264  * expirations or I/O completions".  In the case of Posix Timers 
1265  * we allocate the sigqueue structure from the timer_create.  If this
1266  * allocation fails we are able to report the failure to the application
1267  * with an EAGAIN error.
1268  */
1269  
1270 struct sigqueue *sigqueue_alloc(void)
1271 {
1272         struct sigqueue *q;
1273
1274         if ((q = __sigqueue_alloc()))
1275                 q->flags |= SIGQUEUE_PREALLOC;
1276         return(q);
1277 }
1278
1279 void sigqueue_free(struct sigqueue *q)
1280 {
1281         unsigned long flags;
1282         BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1283         /*
1284          * If the signal is still pending remove it from the
1285          * pending queue.
1286          */
1287         if (unlikely(!list_empty(&q->list))) {
1288                 read_lock(&tasklist_lock);  
1289                 spin_lock_irqsave(q->lock, flags);
1290                 if (!list_empty(&q->list))
1291                         list_del_init(&q->list);
1292                 spin_unlock_irqrestore(q->lock, flags);
1293                 read_unlock(&tasklist_lock);
1294         }
1295         q->flags &= ~SIGQUEUE_PREALLOC;
1296         __sigqueue_free(q);
1297 }
1298
1299 int
1300 send_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1301 {
1302         unsigned long flags;
1303         int ret = 0;
1304
1305         /*
1306          * We need the tasklist lock even for the specific
1307          * thread case (when we don't need to follow the group
1308          * lists) in order to avoid races with "p->sighand"
1309          * going away or changing from under us.
1310          */
1311         BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1312         read_lock(&tasklist_lock);  
1313         spin_lock_irqsave(&p->sighand->siglock, flags);
1314         
1315         if (unlikely(!list_empty(&q->list))) {
1316                 /*
1317                  * If an SI_TIMER entry is already queue just increment
1318                  * the overrun count.
1319                  */
1320                 if (q->info.si_code != SI_TIMER)
1321                         BUG();
1322                 q->info.si_overrun++;
1323                 goto out;
1324         } 
1325         /* Short-circuit ignored signals.  */
1326         if (sig_ignored(p, sig)) {
1327                 ret = 1;
1328                 goto out;
1329         }
1330
1331         q->lock = &p->sighand->siglock;
1332         list_add_tail(&q->list, &p->pending.list);
1333         sigaddset(&p->pending.signal, sig);
1334         if (!sigismember(&p->blocked, sig))
1335                 signal_wake_up(p, sig == SIGKILL);
1336
1337 out:
1338         spin_unlock_irqrestore(&p->sighand->siglock, flags);
1339         read_unlock(&tasklist_lock);
1340         return(ret);
1341 }
1342
1343 int
1344 send_group_sigqueue(int sig, struct sigqueue *q, struct task_struct *p)
1345 {
1346         unsigned long flags;
1347         unsigned int mask;
1348         int ret = 0;
1349
1350         BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1351         read_lock(&tasklist_lock);
1352         spin_lock_irqsave(&p->sighand->siglock, flags);
1353         handle_stop_signal(sig, p);
1354
1355         /* Short-circuit ignored signals.  */
1356         if (sig_ignored(p, sig)) {
1357                 ret = 1;
1358                 goto out;
1359         }
1360
1361         if (unlikely(!list_empty(&q->list))) {
1362                 /*
1363                  * If an SI_TIMER entry is already queue just increment
1364                  * the overrun count.  Other uses should not try to
1365                  * send the signal multiple times.
1366                  */
1367                 if (q->info.si_code != SI_TIMER)
1368                         BUG();
1369                 q->info.si_overrun++;
1370                 goto out;
1371         } 
1372         /*
1373          * Don't bother zombies and stopped tasks (but
1374          * SIGKILL will punch through stopped state)
1375          */
1376         mask = TASK_DEAD | TASK_ZOMBIE;
1377         if (sig != SIGKILL)
1378                 mask |= TASK_STOPPED;
1379
1380         /*
1381          * Put this signal on the shared-pending queue.
1382          * We always use the shared queue for process-wide signals,
1383          * to avoid several races.
1384          */
1385         q->lock = &p->sighand->siglock;
1386         list_add_tail(&q->list, &p->signal->shared_pending.list);
1387         sigaddset(&p->signal->shared_pending.signal, sig);
1388
1389         __group_complete_signal(sig, p, mask);
1390 out:
1391         spin_unlock_irqrestore(&p->sighand->siglock, flags);
1392         read_unlock(&tasklist_lock);
1393         return(ret);
1394 }
1395
1396 /*
1397  * Joy. Or not. Pthread wants us to wake up every thread
1398  * in our parent group.
1399  */
1400 static void __wake_up_parent(struct task_struct *p,
1401                                     struct task_struct *parent)
1402 {
1403         struct task_struct *tsk = parent;
1404
1405         /*
1406          * Fortunately this is not necessary for thread groups:
1407          */
1408         if (p->tgid == tsk->tgid) {
1409                 wake_up_interruptible(&tsk->wait_chldexit);
1410                 return;
1411         }
1412
1413         do {
1414                 wake_up_interruptible(&tsk->wait_chldexit);
1415                 tsk = next_thread(tsk);
1416                 if (tsk->signal != parent->signal)
1417                         BUG();
1418         } while (tsk != parent);
1419 }
1420
1421 /*
1422  * Let a parent know about a status change of a child.
1423  */
1424
1425 void do_notify_parent(struct task_struct *tsk, int sig)
1426 {
1427         struct siginfo info;
1428         unsigned long flags;
1429         int why, status;
1430         struct sighand_struct *psig;
1431
1432         if (sig == -1)
1433                 BUG();
1434
1435         BUG_ON(tsk->group_leader != tsk && tsk->group_leader->state != TASK_ZOMBIE && !tsk->ptrace);
1436         BUG_ON(tsk->group_leader == tsk && !thread_group_empty(tsk) && !tsk->ptrace);
1437
1438         info.si_signo = sig;
1439         info.si_errno = 0;
1440         info.si_pid = tsk->pid;
1441         info.si_uid = tsk->uid;
1442
1443         /* FIXME: find out whether or not this is supposed to be c*time. */
1444         info.si_utime = tsk->utime;
1445         info.si_stime = tsk->stime;
1446
1447         status = tsk->exit_code & 0x7f;
1448         why = SI_KERNEL;        /* shouldn't happen */
1449         switch (tsk->state) {
1450         case TASK_STOPPED:
1451                 /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
1452                 if (tsk->ptrace & PT_PTRACED)
1453                         why = CLD_TRAPPED;
1454                 else
1455                         why = CLD_STOPPED;
1456                 break;
1457
1458         default:
1459                 if (tsk->exit_code & 0x80)
1460                         why = CLD_DUMPED;
1461                 else if (tsk->exit_code & 0x7f)
1462                         why = CLD_KILLED;
1463                 else {
1464                         why = CLD_EXITED;
1465                         status = tsk->exit_code >> 8;
1466                 }
1467                 break;
1468         }
1469         info.si_code = why;
1470         info.si_status = status;
1471
1472         psig = tsk->parent->sighand;
1473         spin_lock_irqsave(&psig->siglock, flags);
1474         if (sig == SIGCHLD && tsk->state != TASK_STOPPED &&
1475             (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1476              (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1477                 /*
1478                  * We are exiting and our parent doesn't care.  POSIX.1
1479                  * defines special semantics for setting SIGCHLD to SIG_IGN
1480                  * or setting the SA_NOCLDWAIT flag: we should be reaped
1481                  * automatically and not left for our parent's wait4 call.
1482                  * Rather than having the parent do it as a magic kind of
1483                  * signal handler, we just set this to tell do_exit that we
1484                  * can be cleaned up without becoming a zombie.  Note that
1485                  * we still call __wake_up_parent in this case, because a
1486                  * blocked sys_wait4 might now return -ECHILD.
1487                  *
1488                  * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1489                  * is implementation-defined: we do (if you don't want
1490                  * it, just use SIG_IGN instead).
1491                  */
1492                 tsk->exit_signal = -1;
1493                 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
1494                         sig = 0;
1495         }
1496         if (sig > 0 && sig <= _NSIG)
1497                 __group_send_sig_info(sig, &info, tsk->parent);
1498         __wake_up_parent(tsk, tsk->parent);
1499         spin_unlock_irqrestore(&psig->siglock, flags);
1500 }
1501
1502
1503 /*
1504  * We need the tasklist lock because it's the only
1505  * thing that protects out "parent" pointer.
1506  *
1507  * exit.c calls "do_notify_parent()" directly, because
1508  * it already has the tasklist lock.
1509  */
1510 void
1511 notify_parent(struct task_struct *tsk, int sig)
1512 {
1513         if (sig != -1) {
1514                 read_lock(&tasklist_lock);
1515                 do_notify_parent(tsk, sig);
1516                 read_unlock(&tasklist_lock);
1517         }
1518 }
1519
1520 static void
1521 do_notify_parent_cldstop(struct task_struct *tsk, struct task_struct *parent)
1522 {
1523         struct siginfo info;
1524         unsigned long flags;
1525         struct sighand_struct *sighand;
1526
1527         info.si_signo = SIGCHLD;
1528         info.si_errno = 0;
1529         info.si_pid = tsk->pid;
1530         info.si_uid = tsk->uid;
1531
1532         /* FIXME: find out whether or not this is supposed to be c*time. */
1533         info.si_utime = tsk->utime;
1534         info.si_stime = tsk->stime;
1535
1536         info.si_status = tsk->exit_code & 0x7f;
1537         info.si_code = CLD_STOPPED;
1538
1539         sighand = parent->sighand;
1540         spin_lock_irqsave(&sighand->siglock, flags);
1541         if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1542             !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1543                 __group_send_sig_info(SIGCHLD, &info, parent);
1544         /*
1545          * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1546          */
1547         __wake_up_parent(tsk, parent);
1548         spin_unlock_irqrestore(&sighand->siglock, flags);
1549 }
1550
1551
1552 #ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1553
1554 static void
1555 finish_stop(int stop_count)
1556 {
1557         /*
1558          * If there are no other threads in the group, or if there is
1559          * a group stop in progress and we are the last to stop,
1560          * report to the parent.  When ptraced, every thread reports itself.
1561          */
1562         if (stop_count < 0 || (current->ptrace & PT_PTRACED)) {
1563                 read_lock(&tasklist_lock);
1564                 do_notify_parent_cldstop(current, current->parent);
1565                 read_unlock(&tasklist_lock);
1566         }
1567         else if (stop_count == 0) {
1568                 read_lock(&tasklist_lock);
1569                 do_notify_parent_cldstop(current->group_leader,
1570                                          current->group_leader->real_parent);
1571                 read_unlock(&tasklist_lock);
1572         }
1573
1574         schedule();
1575         /*
1576          * Now we don't run again until continued.
1577          */
1578         current->exit_code = 0;
1579 }
1580
1581 /*
1582  * This performs the stopping for SIGSTOP and other stop signals.
1583  * We have to stop all threads in the thread group.
1584  */
1585 static void
1586 do_signal_stop(int signr)
1587 {
1588         struct signal_struct *sig = current->signal;
1589         struct sighand_struct *sighand = current->sighand;
1590         int stop_count = -1;
1591
1592         /* spin_lock_irq(&sighand->siglock) is now done in caller */
1593
1594         if (sig->group_stop_count > 0) {
1595                 /*
1596                  * There is a group stop in progress.  We don't need to
1597                  * start another one.
1598                  */
1599                 signr = sig->group_exit_code;
1600                 stop_count = --sig->group_stop_count;
1601                 current->exit_code = signr;
1602                 set_current_state(TASK_STOPPED);
1603                 spin_unlock_irq(&sighand->siglock);
1604         }
1605         else if (thread_group_empty(current)) {
1606                 /*
1607                  * Lock must be held through transition to stopped state.
1608                  */
1609                 current->exit_code = signr;
1610                 set_current_state(TASK_STOPPED);
1611                 spin_unlock_irq(&sighand->siglock);
1612         }
1613         else {
1614                 /*
1615                  * There is no group stop already in progress.
1616                  * We must initiate one now, but that requires
1617                  * dropping siglock to get both the tasklist lock
1618                  * and siglock again in the proper order.  Note that
1619                  * this allows an intervening SIGCONT to be posted.
1620                  * We need to check for that and bail out if necessary.
1621                  */
1622                 struct task_struct *t;
1623
1624                 spin_unlock_irq(&sighand->siglock);
1625
1626                 /* signals can be posted during this window */
1627
1628                 read_lock(&tasklist_lock);
1629                 spin_lock_irq(&sighand->siglock);
1630
1631                 if (unlikely(sig->group_exit)) {
1632                         /*
1633                          * There is a group exit in progress now.
1634                          * We'll just ignore the stop and process the
1635                          * associated fatal signal.
1636                          */
1637                         spin_unlock_irq(&sighand->siglock);
1638                         read_unlock(&tasklist_lock);
1639                         return;
1640                 }
1641
1642                 if (unlikely(sig_avoid_stop_race())) {
1643                         /*
1644                          * Either a SIGCONT or a SIGKILL signal was
1645                          * posted in the siglock-not-held window.
1646                          */
1647                         spin_unlock_irq(&sighand->siglock);
1648                         read_unlock(&tasklist_lock);
1649                         return;
1650                 }
1651
1652                 if (sig->group_stop_count == 0) {
1653                         sig->group_exit_code = signr;
1654                         stop_count = 0;
1655                         for (t = next_thread(current); t != current;
1656                              t = next_thread(t))
1657                                 /*
1658                                  * Setting state to TASK_STOPPED for a group
1659                                  * stop is always done with the siglock held,
1660                                  * so this check has no races.
1661                                  */
1662                                 if (t->state < TASK_STOPPED) {
1663                                         stop_count++;
1664                                         signal_wake_up(t, 0);
1665                                 }
1666                         sig->group_stop_count = stop_count;
1667                 }
1668                 else {
1669                         /* A race with another thread while unlocked.  */
1670                         signr = sig->group_exit_code;
1671                         stop_count = --sig->group_stop_count;
1672                 }
1673
1674                 current->exit_code = signr;
1675                 set_current_state(TASK_STOPPED);
1676
1677                 spin_unlock_irq(&sighand->siglock);
1678                 read_unlock(&tasklist_lock);
1679         }
1680
1681         finish_stop(stop_count);
1682 }
1683
1684 /*
1685  * Do appropriate magic when group_stop_count > 0.
1686  * We return nonzero if we stopped, after releasing the siglock.
1687  * We return zero if we still hold the siglock and should look
1688  * for another signal without checking group_stop_count again.
1689  */
1690 static inline int handle_group_stop(void)
1691 {
1692         int stop_count;
1693
1694         if (current->signal->group_exit_task == current) {
1695                 /*
1696                  * Group stop is so we can do a core dump,
1697                  * We are the initiating thread, so get on with it.
1698                  */
1699                 current->signal->group_exit_task = NULL;
1700                 return 0;
1701         }
1702
1703         if (current->signal->group_exit)
1704                 /*
1705                  * Group stop is so another thread can do a core dump,
1706                  * or else we are racing against a death signal.
1707                  * Just punt the stop so we can get the next signal.
1708                  */
1709                 return 0;
1710
1711         /*
1712          * There is a group stop in progress.  We stop
1713          * without any associated signal being in our queue.
1714          */
1715         stop_count = --current->signal->group_stop_count;
1716         current->exit_code = current->signal->group_exit_code;
1717         set_current_state(TASK_STOPPED);
1718         spin_unlock_irq(&current->sighand->siglock);
1719         finish_stop(stop_count);
1720         return 1;
1721 }
1722
1723 int get_signal_to_deliver(siginfo_t *info, struct pt_regs *regs, void *cookie)
1724 {
1725         sigset_t *mask = &current->blocked;
1726         int signr = 0;
1727
1728 relock:
1729         spin_lock_irq(&current->sighand->siglock);
1730         for (;;) {
1731                 struct k_sigaction *ka;
1732
1733                 if (unlikely(current->signal->group_stop_count > 0) &&
1734                     handle_group_stop())
1735                         goto relock;
1736
1737                 signr = dequeue_signal(current, mask, info);
1738
1739                 if (!signr)
1740                         break; /* will return 0 */
1741
1742                 if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1743                         ptrace_signal_deliver(regs, cookie);
1744
1745                         /*
1746                          * If there is a group stop in progress,
1747                          * we must participate in the bookkeeping.
1748                          */
1749                         if (current->signal->group_stop_count > 0)
1750                                 --current->signal->group_stop_count;
1751
1752                         /* Let the debugger run.  */
1753                         current->exit_code = signr;
1754                         current->last_siginfo = info;
1755                         set_current_state(TASK_STOPPED);
1756                         spin_unlock_irq(&current->sighand->siglock);
1757                         notify_parent(current, SIGCHLD);
1758                         schedule();
1759
1760                         current->last_siginfo = NULL;
1761
1762                         /* We're back.  Did the debugger cancel the sig?  */
1763                         spin_lock_irq(&current->sighand->siglock);
1764                         signr = current->exit_code;
1765                         if (signr == 0)
1766                                 continue;
1767
1768                         current->exit_code = 0;
1769
1770                         /* Update the siginfo structure if the signal has
1771                            changed.  If the debugger wanted something
1772                            specific in the siginfo structure then it should
1773                            have updated *info via PTRACE_SETSIGINFO.  */
1774                         if (signr != info->si_signo) {
1775                                 info->si_signo = signr;
1776                                 info->si_errno = 0;
1777                                 info->si_code = SI_USER;
1778                                 info->si_pid = current->parent->pid;
1779                                 info->si_uid = current->parent->uid;
1780                         }
1781
1782                         /* If the (new) signal is now blocked, requeue it.  */
1783                         if (sigismember(&current->blocked, signr)) {
1784                                 specific_send_sig_info(signr, info, current);
1785                                 continue;
1786                         }
1787                 }
1788
1789                 ka = &current->sighand->action[signr-1];
1790                 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing.  */
1791                         continue;
1792                 if (ka->sa.sa_handler != SIG_DFL) /* Run the handler.  */
1793                         break; /* will return non-zero "signr" value */
1794
1795                 /*
1796                  * Now we are doing the default action for this signal.
1797                  */
1798                 if (sig_kernel_ignore(signr)) /* Default is nothing. */
1799                         continue;
1800
1801                 /* Init gets no signals it doesn't want.  */
1802                 if (current->pid == 1)
1803                         continue;
1804
1805                 if (sig_kernel_stop(signr)) {
1806                         /*
1807                          * The default action is to stop all threads in
1808                          * the thread group.  The job control signals
1809                          * do nothing in an orphaned pgrp, but SIGSTOP
1810                          * always works.  Note that siglock needs to be
1811                          * dropped during the call to is_orphaned_pgrp()
1812                          * because of lock ordering with tasklist_lock.
1813                          * This allows an intervening SIGCONT to be posted.
1814                          * We need to check for that and bail out if necessary.
1815                          */
1816                         if (signr == SIGSTOP) {
1817                                 do_signal_stop(signr); /* releases siglock */
1818                                 goto relock;
1819                         }
1820                         spin_unlock_irq(&current->sighand->siglock);
1821
1822                         /* signals can be posted during this window */
1823
1824                         if (is_orphaned_pgrp(process_group(current)))
1825                                 goto relock;
1826
1827                         spin_lock_irq(&current->sighand->siglock);
1828                         if (unlikely(sig_avoid_stop_race())) {
1829                                 /*
1830                                  * Either a SIGCONT or a SIGKILL signal was
1831                                  * posted in the siglock-not-held window.
1832                                  */
1833                                 continue;
1834                         }
1835
1836                         do_signal_stop(signr); /* releases siglock */
1837                         goto relock;
1838                 }
1839
1840                 spin_unlock_irq(&current->sighand->siglock);
1841
1842                 /*
1843                  * Anything else is fatal, maybe with a core dump.
1844                  */
1845                 current->flags |= PF_SIGNALED;
1846                 if (sig_kernel_coredump(signr) &&
1847                     do_coredump((long)signr, signr, regs)) {
1848                         /*
1849                          * That killed all other threads in the group and
1850                          * synchronized with their demise, so there can't
1851                          * be any more left to kill now.  The group_exit
1852                          * flags are set by do_coredump.  Note that
1853                          * thread_group_empty won't always be true yet,
1854                          * because those threads were blocked in __exit_mm
1855                          * and we just let them go to finish dying.
1856                          */
1857                         const int code = signr | 0x80;
1858                         BUG_ON(!current->signal->group_exit);
1859                         BUG_ON(current->signal->group_exit_code != code);
1860                         do_exit(code);
1861                         /* NOTREACHED */
1862                 }
1863
1864                 /*
1865                  * Death signals, no core dump.
1866                  */
1867                 do_group_exit(signr);
1868                 /* NOTREACHED */
1869         }
1870         spin_unlock_irq(&current->sighand->siglock);
1871         return signr;
1872 }
1873
1874 #endif
1875
1876 EXPORT_SYMBOL(recalc_sigpending);
1877 EXPORT_SYMBOL_GPL(dequeue_signal);
1878 EXPORT_SYMBOL(flush_signals);
1879 EXPORT_SYMBOL(force_sig);
1880 EXPORT_SYMBOL(force_sig_info);
1881 EXPORT_SYMBOL(kill_pg);
1882 EXPORT_SYMBOL(kill_pg_info);
1883 EXPORT_SYMBOL(kill_proc);
1884 EXPORT_SYMBOL(kill_proc_info);
1885 EXPORT_SYMBOL(kill_sl);
1886 EXPORT_SYMBOL(kill_sl_info);
1887 EXPORT_SYMBOL(notify_parent);
1888 EXPORT_SYMBOL(send_sig);
1889 EXPORT_SYMBOL(send_sig_info);
1890 EXPORT_SYMBOL(send_group_sig_info);
1891 EXPORT_SYMBOL(sigqueue_alloc);
1892 EXPORT_SYMBOL(sigqueue_free);
1893 EXPORT_SYMBOL(send_sigqueue);
1894 EXPORT_SYMBOL(send_group_sigqueue);
1895 EXPORT_SYMBOL(sigprocmask);
1896 EXPORT_SYMBOL(block_all_signals);
1897 EXPORT_SYMBOL(unblock_all_signals);
1898
1899
1900 /*
1901  * System call entry points.
1902  */
1903
1904 asmlinkage long sys_restart_syscall(void)
1905 {
1906         struct restart_block *restart = &current_thread_info()->restart_block;
1907         return restart->fn(restart);
1908 }
1909
1910 long do_no_restart_syscall(struct restart_block *param)
1911 {
1912         return -EINTR;
1913 }
1914
1915 /*
1916  * We don't need to get the kernel lock - this is all local to this
1917  * particular thread.. (and that's good, because this is _heavily_
1918  * used by various programs)
1919  */
1920
1921 /*
1922  * This is also useful for kernel threads that want to temporarily
1923  * (or permanently) block certain signals.
1924  *
1925  * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
1926  * interface happily blocks "unblockable" signals like SIGKILL
1927  * and friends.
1928  */
1929 int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
1930 {
1931         int error;
1932         sigset_t old_block;
1933
1934         spin_lock_irq(&current->sighand->siglock);
1935         old_block = current->blocked;
1936         error = 0;
1937         switch (how) {
1938         case SIG_BLOCK:
1939                 sigorsets(&current->blocked, &current->blocked, set);
1940                 break;
1941         case SIG_UNBLOCK:
1942                 signandsets(&current->blocked, &current->blocked, set);
1943                 break;
1944         case SIG_SETMASK:
1945                 current->blocked = *set;
1946                 break;
1947         default:
1948                 error = -EINVAL;
1949         }
1950         recalc_sigpending();
1951         spin_unlock_irq(&current->sighand->siglock);
1952         if (oldset)
1953                 *oldset = old_block;
1954         return error;
1955 }
1956
1957 asmlinkage long
1958 sys_rt_sigprocmask(int how, sigset_t __user *set, sigset_t __user *oset, size_t sigsetsize)
1959 {
1960         int error = -EINVAL;
1961         sigset_t old_set, new_set;
1962
1963         /* XXX: Don't preclude handling different sized sigset_t's.  */
1964         if (sigsetsize != sizeof(sigset_t))
1965                 goto out;
1966
1967         if (set) {
1968                 error = -EFAULT;
1969                 if (copy_from_user(&new_set, set, sizeof(*set)))
1970                         goto out;
1971                 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
1972
1973                 error = sigprocmask(how, &new_set, &old_set);
1974                 if (error)
1975                         goto out;
1976                 if (oset)
1977                         goto set_old;
1978         } else if (oset) {
1979                 spin_lock_irq(&current->sighand->siglock);
1980                 old_set = current->blocked;
1981                 spin_unlock_irq(&current->sighand->siglock);
1982
1983         set_old:
1984                 error = -EFAULT;
1985                 if (copy_to_user(oset, &old_set, sizeof(*oset)))
1986                         goto out;
1987         }
1988         error = 0;
1989 out:
1990         return error;
1991 }
1992
1993 long do_sigpending(void __user *set, unsigned long sigsetsize)
1994 {
1995         long error = -EINVAL;
1996         sigset_t pending;
1997
1998         if (sigsetsize > sizeof(sigset_t))
1999                 goto out;
2000
2001         spin_lock_irq(&current->sighand->siglock);
2002         sigorsets(&pending, &current->pending.signal,
2003                   &current->signal->shared_pending.signal);
2004         spin_unlock_irq(&current->sighand->siglock);
2005
2006         /* Outside the lock because only this thread touches it.  */
2007         sigandsets(&pending, &current->blocked, &pending);
2008
2009         error = -EFAULT;
2010         if (!copy_to_user(set, &pending, sigsetsize))
2011                 error = 0;
2012
2013 out:
2014         return error;
2015 }       
2016
2017 asmlinkage long
2018 sys_rt_sigpending(sigset_t __user *set, size_t sigsetsize)
2019 {
2020         return do_sigpending(set, sigsetsize);
2021 }
2022
2023 #ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2024
2025 int copy_siginfo_to_user(siginfo_t __user *to, siginfo_t *from)
2026 {
2027         int err;
2028
2029         if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2030                 return -EFAULT;
2031         if (from->si_code < 0)
2032                 return __copy_to_user(to, from, sizeof(siginfo_t))
2033                         ? -EFAULT : 0;
2034         /*
2035          * If you change siginfo_t structure, please be sure
2036          * this code is fixed accordingly.
2037          * It should never copy any pad contained in the structure
2038          * to avoid security leaks, but must copy the generic
2039          * 3 ints plus the relevant union member.
2040          */
2041         err = __put_user(from->si_signo, &to->si_signo);
2042         err |= __put_user(from->si_errno, &to->si_errno);
2043         err |= __put_user((short)from->si_code, &to->si_code);
2044         switch (from->si_code & __SI_MASK) {
2045         case __SI_KILL:
2046                 err |= __put_user(from->si_pid, &to->si_pid);
2047                 err |= __put_user(from->si_uid, &to->si_uid);
2048                 break;
2049         case __SI_TIMER:
2050                  err |= __put_user(from->si_tid, &to->si_tid);
2051                  err |= __put_user(from->si_overrun, &to->si_overrun);
2052                  err |= __put_user(from->si_ptr, &to->si_ptr);
2053                 break;
2054         case __SI_POLL:
2055                 err |= __put_user(from->si_band, &to->si_band);
2056                 err |= __put_user(from->si_fd, &to->si_fd);
2057                 break;
2058         case __SI_FAULT:
2059                 err |= __put_user(from->si_addr, &to->si_addr);
2060 #ifdef __ARCH_SI_TRAPNO
2061                 err |= __put_user(from->si_trapno, &to->si_trapno);
2062 #endif
2063                 break;
2064         case __SI_CHLD:
2065                 err |= __put_user(from->si_pid, &to->si_pid);
2066                 err |= __put_user(from->si_uid, &to->si_uid);
2067                 err |= __put_user(from->si_status, &to->si_status);
2068                 err |= __put_user(from->si_utime, &to->si_utime);
2069                 err |= __put_user(from->si_stime, &to->si_stime);
2070                 break;
2071         case __SI_RT: /* This is not generated by the kernel as of now. */
2072         case __SI_MESGQ: /* But this is */
2073                 err |= __put_user(from->si_pid, &to->si_pid);
2074                 err |= __put_user(from->si_uid, &to->si_uid);
2075                 err |= __put_user(from->si_ptr, &to->si_ptr);
2076                 break;
2077         default: /* this is just in case for now ... */
2078                 err |= __put_user(from->si_pid, &to->si_pid);
2079                 err |= __put_user(from->si_uid, &to->si_uid);
2080                 break;
2081         }
2082         return err;
2083 }
2084
2085 #endif
2086
2087 asmlinkage long
2088 sys_rt_sigtimedwait(const sigset_t __user *uthese,
2089                     siginfo_t __user *uinfo,
2090                     const struct timespec __user *uts,
2091                     size_t sigsetsize)
2092 {
2093         int ret, sig;
2094         sigset_t these;
2095         struct timespec ts;
2096         siginfo_t info;
2097         long timeout = 0;
2098
2099         /* XXX: Don't preclude handling different sized sigset_t's.  */
2100         if (sigsetsize != sizeof(sigset_t))
2101                 return -EINVAL;
2102
2103         if (copy_from_user(&these, uthese, sizeof(these)))
2104                 return -EFAULT;
2105                 
2106         /*
2107          * Invert the set of allowed signals to get those we
2108          * want to block.
2109          */
2110         sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
2111         signotset(&these);
2112
2113         if (uts) {
2114                 if (copy_from_user(&ts, uts, sizeof(ts)))
2115                         return -EFAULT;
2116                 if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
2117                     || ts.tv_sec < 0)
2118                         return -EINVAL;
2119         }
2120
2121         spin_lock_irq(&current->sighand->siglock);
2122         sig = dequeue_signal(current, &these, &info);
2123         if (!sig) {
2124                 timeout = MAX_SCHEDULE_TIMEOUT;
2125                 if (uts)
2126                         timeout = (timespec_to_jiffies(&ts)
2127                                    + (ts.tv_sec || ts.tv_nsec));
2128
2129                 if (timeout) {
2130                         /* None ready -- temporarily unblock those we're
2131                          * interested while we are sleeping in so that we'll
2132                          * be awakened when they arrive.  */
2133                         current->real_blocked = current->blocked;
2134                         sigandsets(&current->blocked, &current->blocked, &these);
2135                         recalc_sigpending();
2136                         spin_unlock_irq(&current->sighand->siglock);
2137
2138                         current->state = TASK_INTERRUPTIBLE;
2139                         timeout = schedule_timeout(timeout);
2140
2141                         spin_lock_irq(&current->sighand->siglock);
2142                         sig = dequeue_signal(current, &these, &info);
2143                         current->blocked = current->real_blocked;
2144                         siginitset(&current->real_blocked, 0);
2145                         recalc_sigpending();
2146                 }
2147         }
2148         spin_unlock_irq(&current->sighand->siglock);
2149
2150         if (sig) {
2151                 ret = sig;
2152                 if (uinfo) {
2153                         if (copy_siginfo_to_user(uinfo, &info))
2154                                 ret = -EFAULT;
2155                 }
2156         } else {
2157                 ret = -EAGAIN;
2158                 if (timeout)
2159                         ret = -EINTR;
2160         }
2161
2162         return ret;
2163 }
2164
2165 asmlinkage long
2166 sys_kill(int pid, int sig)
2167 {
2168         struct siginfo info;
2169
2170         info.si_signo = sig;
2171         info.si_errno = 0;
2172         info.si_code = SI_USER;
2173         info.si_pid = current->tgid;
2174         info.si_uid = current->uid;
2175
2176         return kill_something_info(sig, &info, pid);
2177 }
2178
2179 /**
2180  *  sys_tkill - send signal to one specific thread
2181  *  @tgid: the thread group ID of the thread
2182  *  @pid: the PID of the thread
2183  *  @sig: signal to be sent
2184  *
2185  *  This syscall also checks the tgid and returns -ESRCH even if the PID
2186  *  exists but it's not belonging to the target process anymore. This
2187  *  method solves the problem of threads exiting and PIDs getting reused.
2188  */
2189 asmlinkage long sys_tgkill(int tgid, int pid, int sig)
2190 {
2191         struct siginfo info;
2192         int error;
2193         struct task_struct *p;
2194
2195         /* This is only valid for single tasks */
2196         if (pid <= 0 || tgid <= 0)
2197                 return -EINVAL;
2198
2199         info.si_signo = sig;
2200         info.si_errno = 0;
2201         info.si_code = SI_TKILL;
2202         info.si_pid = current->tgid;
2203         info.si_uid = current->uid;
2204
2205         read_lock(&tasklist_lock);
2206         p = find_task_by_pid(pid);
2207         error = -ESRCH;
2208         if (p && (p->tgid == tgid)) {
2209                 error = check_kill_permission(sig, &info, p);
2210                 /*
2211                  * The null signal is a permissions and process existence
2212                  * probe.  No signal is actually delivered.
2213                  */
2214                 if (!error && sig && p->sighand) {
2215                         spin_lock_irq(&p->sighand->siglock);
2216                         handle_stop_signal(sig, p);
2217                         error = specific_send_sig_info(sig, &info, p);
2218                         spin_unlock_irq(&p->sighand->siglock);
2219                 }
2220         }
2221         read_unlock(&tasklist_lock);
2222         return error;
2223 }
2224
2225 /*
2226  *  Send a signal to only one task, even if it's a CLONE_THREAD task.
2227  */
2228 asmlinkage long
2229 sys_tkill(int pid, int sig)
2230 {
2231         struct siginfo info;
2232         int error;
2233         struct task_struct *p;
2234
2235         /* This is only valid for single tasks */
2236         if (pid <= 0)
2237                 return -EINVAL;
2238
2239         info.si_signo = sig;
2240         info.si_errno = 0;
2241         info.si_code = SI_TKILL;
2242         info.si_pid = current->tgid;
2243         info.si_uid = current->uid;
2244
2245         read_lock(&tasklist_lock);
2246         p = find_task_by_pid(pid);
2247         error = -ESRCH;
2248         if (p) {
2249                 error = check_kill_permission(sig, &info, p);
2250                 /*
2251                  * The null signal is a permissions and process existence
2252                  * probe.  No signal is actually delivered.
2253                  */
2254                 if (!error && sig && p->sighand) {
2255                         spin_lock_irq(&p->sighand->siglock);
2256                         handle_stop_signal(sig, p);
2257                         error = specific_send_sig_info(sig, &info, p);
2258                         spin_unlock_irq(&p->sighand->siglock);
2259                 }
2260         }
2261         read_unlock(&tasklist_lock);
2262         return error;
2263 }
2264
2265 asmlinkage long
2266 sys_rt_sigqueueinfo(int pid, int sig, siginfo_t __user *uinfo)
2267 {
2268         siginfo_t info;
2269
2270         if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
2271                 return -EFAULT;
2272
2273         /* Not even root can pretend to send signals from the kernel.
2274            Nor can they impersonate a kill(), which adds source info.  */
2275         if (info.si_code >= 0)
2276                 return -EPERM;
2277         info.si_signo = sig;
2278
2279         /* POSIX.1b doesn't mention process groups.  */
2280         return kill_proc_info(sig, &info, pid);
2281 }
2282
2283 int
2284 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
2285 {
2286         struct k_sigaction *k;
2287
2288         if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
2289                 return -EINVAL;
2290
2291         k = &current->sighand->action[sig-1];
2292
2293         spin_lock_irq(&current->sighand->siglock);
2294         if (signal_pending(current)) {
2295                 /*
2296                  * If there might be a fatal signal pending on multiple
2297                  * threads, make sure we take it before changing the action.
2298                  */
2299                 spin_unlock_irq(&current->sighand->siglock);
2300                 return -ERESTARTNOINTR;
2301         }
2302
2303         if (oact)
2304                 *oact = *k;
2305
2306         if (act) {
2307                 /*
2308                  * POSIX 3.3.1.3:
2309                  *  "Setting a signal action to SIG_IGN for a signal that is
2310                  *   pending shall cause the pending signal to be discarded,
2311                  *   whether or not it is blocked."
2312                  *
2313                  *  "Setting a signal action to SIG_DFL for a signal that is
2314                  *   pending and whose default action is to ignore the signal
2315                  *   (for example, SIGCHLD), shall cause the pending signal to
2316                  *   be discarded, whether or not it is blocked"
2317                  */
2318                 if (act->sa.sa_handler == SIG_IGN ||
2319                     (act->sa.sa_handler == SIG_DFL &&
2320                      sig_kernel_ignore(sig))) {
2321                         /*
2322                          * This is a fairly rare case, so we only take the
2323                          * tasklist_lock once we're sure we'll need it.
2324                          * Now we must do this little unlock and relock
2325                          * dance to maintain the lock hierarchy.
2326                          */
2327                         struct task_struct *t = current;
2328                         spin_unlock_irq(&t->sighand->siglock);
2329                         read_lock(&tasklist_lock);
2330                         spin_lock_irq(&t->sighand->siglock);
2331                         *k = *act;
2332                         sigdelsetmask(&k->sa.sa_mask,
2333                                       sigmask(SIGKILL) | sigmask(SIGSTOP));
2334                         rm_from_queue(sigmask(sig), &t->signal->shared_pending);
2335                         do {
2336                                 rm_from_queue(sigmask(sig), &t->pending);
2337                                 recalc_sigpending_tsk(t);
2338                                 t = next_thread(t);
2339                         } while (t != current);
2340                         spin_unlock_irq(&current->sighand->siglock);
2341                         read_unlock(&tasklist_lock);
2342                         return 0;
2343                 }
2344
2345                 *k = *act;
2346                 sigdelsetmask(&k->sa.sa_mask,
2347                               sigmask(SIGKILL) | sigmask(SIGSTOP));
2348         }
2349
2350         spin_unlock_irq(&current->sighand->siglock);
2351         return 0;
2352 }
2353
2354 int 
2355 do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp)
2356 {
2357         stack_t oss;
2358         int error;
2359
2360         if (uoss) {
2361                 oss.ss_sp = (void *) current->sas_ss_sp;
2362                 oss.ss_size = current->sas_ss_size;
2363                 oss.ss_flags = sas_ss_flags(sp);
2364         }
2365
2366         if (uss) {
2367                 void *ss_sp;
2368                 size_t ss_size;
2369                 int ss_flags;
2370
2371                 error = -EFAULT;
2372                 if (verify_area(VERIFY_READ, uss, sizeof(*uss))
2373                     || __get_user(ss_sp, &uss->ss_sp)
2374                     || __get_user(ss_flags, &uss->ss_flags)
2375                     || __get_user(ss_size, &uss->ss_size))
2376                         goto out;
2377
2378                 error = -EPERM;
2379                 if (on_sig_stack(sp))
2380                         goto out;
2381
2382                 error = -EINVAL;
2383                 /*
2384                  *
2385                  * Note - this code used to test ss_flags incorrectly
2386                  *        old code may have been written using ss_flags==0
2387                  *        to mean ss_flags==SS_ONSTACK (as this was the only
2388                  *        way that worked) - this fix preserves that older
2389                  *        mechanism
2390                  */
2391                 if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
2392                         goto out;
2393
2394                 if (ss_flags == SS_DISABLE) {
2395                         ss_size = 0;
2396                         ss_sp = NULL;
2397                 } else {
2398                         error = -ENOMEM;
2399                         if (ss_size < MINSIGSTKSZ)
2400                                 goto out;
2401                 }
2402
2403                 current->sas_ss_sp = (unsigned long) ss_sp;
2404                 current->sas_ss_size = ss_size;
2405         }
2406
2407         if (uoss) {
2408                 error = -EFAULT;
2409                 if (copy_to_user(uoss, &oss, sizeof(oss)))
2410                         goto out;
2411         }
2412
2413         error = 0;
2414 out:
2415         return error;
2416 }
2417
2418 asmlinkage long
2419 sys_sigpending(old_sigset_t __user *set)
2420 {
2421         return do_sigpending(set, sizeof(*set));
2422 }
2423
2424 #if !defined(__alpha__)
2425 /* Alpha has its own versions with special arguments.  */
2426
2427 asmlinkage long
2428 sys_sigprocmask(int how, old_sigset_t __user *set, old_sigset_t __user *oset)
2429 {
2430         int error;
2431         old_sigset_t old_set, new_set;
2432
2433         if (set) {
2434                 error = -EFAULT;
2435                 if (copy_from_user(&new_set, set, sizeof(*set)))
2436                         goto out;
2437                 new_set &= ~(sigmask(SIGKILL) | sigmask(SIGSTOP));
2438
2439                 spin_lock_irq(&current->sighand->siglock);
2440                 old_set = current->blocked.sig[0];
2441
2442                 error = 0;
2443                 switch (how) {
2444                 default:
2445                         error = -EINVAL;
2446                         break;
2447                 case SIG_BLOCK:
2448                         sigaddsetmask(&current->blocked, new_set);
2449                         break;
2450                 case SIG_UNBLOCK:
2451                         sigdelsetmask(&current->blocked, new_set);
2452                         break;
2453                 case SIG_SETMASK:
2454                         current->blocked.sig[0] = new_set;
2455                         break;
2456                 }
2457
2458                 recalc_sigpending();
2459                 spin_unlock_irq(&current->sighand->siglock);
2460                 if (error)
2461                         goto out;
2462                 if (oset)
2463                         goto set_old;
2464         } else if (oset) {
2465                 old_set = current->blocked.sig[0];
2466         set_old:
2467                 error = -EFAULT;
2468                 if (copy_to_user(oset, &old_set, sizeof(*oset)))
2469                         goto out;
2470         }
2471         error = 0;
2472 out:
2473         return error;
2474 }
2475
2476 #ifndef __sparc__
2477 asmlinkage long
2478 sys_rt_sigaction(int sig,
2479                  const struct sigaction __user *act,
2480                  struct sigaction __user *oact,
2481                  size_t sigsetsize)
2482 {
2483         struct k_sigaction new_sa, old_sa;
2484         int ret = -EINVAL;
2485
2486         /* XXX: Don't preclude handling different sized sigset_t's.  */
2487         if (sigsetsize != sizeof(sigset_t))
2488                 goto out;
2489
2490         if (act) {
2491                 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
2492                         return -EFAULT;
2493         }
2494
2495         ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
2496
2497         if (!ret && oact) {
2498                 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
2499                         return -EFAULT;
2500         }
2501 out:
2502         return ret;
2503 }
2504 #endif /* __sparc__ */
2505 #endif
2506
2507 #if !defined(__alpha__) && !defined(__ia64__) && \
2508     !defined(__arm__) && !defined(__s390__)
2509 /*
2510  * For backwards compatibility.  Functionality superseded by sigprocmask.
2511  */
2512 asmlinkage long
2513 sys_sgetmask(void)
2514 {
2515         /* SMP safe */
2516         return current->blocked.sig[0];
2517 }
2518
2519 asmlinkage long
2520 sys_ssetmask(int newmask)
2521 {
2522         int old;
2523
2524         spin_lock_irq(&current->sighand->siglock);
2525         old = current->blocked.sig[0];
2526
2527         siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
2528                                                   sigmask(SIGSTOP)));
2529         recalc_sigpending();
2530         spin_unlock_irq(&current->sighand->siglock);
2531
2532         return old;
2533 }
2534 #endif /* !defined(__alpha__) */
2535
2536 #if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__) && \
2537     !defined(__arm__)
2538 /*
2539  * For backwards compatibility.  Functionality superseded by sigaction.
2540  */
2541 asmlinkage unsigned long
2542 sys_signal(int sig, __sighandler_t handler)
2543 {
2544         struct k_sigaction new_sa, old_sa;
2545         int ret;
2546
2547         new_sa.sa.sa_handler = handler;
2548         new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
2549
2550         ret = do_sigaction(sig, &new_sa, &old_sa);
2551
2552         return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
2553 }
2554 #endif /* !alpha && !__ia64__ && !defined(__mips__) && !defined(__arm__) */
2555
2556 #ifndef HAVE_ARCH_SYS_PAUSE
2557
2558 asmlinkage long
2559 sys_pause(void)
2560 {
2561         current->state = TASK_INTERRUPTIBLE;
2562         schedule();
2563         return -ERESTARTNOHAND;
2564 }
2565
2566 #endif /* HAVE_ARCH_SYS_PAUSE */
2567
2568 void __init signals_init(void)
2569 {
2570         sigqueue_cachep =
2571                 kmem_cache_create("sigqueue",
2572                                   sizeof(struct sigqueue),
2573                                   __alignof__(struct sigqueue),
2574                                   0, NULL, NULL);
2575         if (!sigqueue_cachep)
2576                 panic("signals_init(): cannot create sigqueue SLAB cache");
2577 }