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