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