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