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