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