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