linux-bk/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
   9#define __KERNEL_SYSCALLS__
  10
  11#include <linux/config.h>
  12#include <linux/slab.h>
  13#include <linux/module.h>
  14#include <linux/unistd.h>
  15#include <linux/smp_lock.h>
  16#include <linux/init.h>
  17#include <linux/sched.h>
  18#include <linux/fs.h>
  19#include <linux/tty.h>
  20#include <linux/binfmts.h>
  21#include <asm/param.h>
  22#include <asm/uaccess.h>
  23#include <asm/siginfo.h>
  24
  25/*
  26 * SLAB caches for signal bits.
  27 */
  28
  29static kmem_cache_t *sigqueue_cachep;
  30
  31atomic_t nr_queued_signals;
  32int max_queued_signals = 1024;
  33
  34/*********************************************************
  35
  36    POSIX thread group signal behavior:
  37
  38----------------------------------------------------------
  39|                    |  userspace       |  kernel        |
  40----------------------------------------------------------
  41|  SIGHUP            |  load-balance    |  kill-all      |
  42|  SIGINT            |  load-balance    |  kill-all      |
  43|  SIGQUIT           |  load-balance    |  kill-all+core |
  44|  SIGILL            |  specific        |  kill-all+core |
  45|  SIGTRAP           |  specific        |  kill-all+core |
  46|  SIGABRT/SIGIOT    |  specific        |  kill-all+core |
  47|  SIGBUS            |  specific        |  kill-all+core |
  48|  SIGFPE            |  specific        |  kill-all+core |
  49|  SIGKILL           |  n/a             |  kill-all      |
  50|  SIGUSR1           |  load-balance    |  kill-all      |
  51|  SIGSEGV           |  specific        |  kill-all+core |
  52|  SIGUSR2           |  load-balance    |  kill-all      |
  53|  SIGPIPE           |  specific        |  kill-all      |
  54|  SIGALRM           |  load-balance    |  kill-all      |
  55|  SIGTERM           |  load-balance    |  kill-all      |
  56|  SIGCHLD           |  load-balance    |  ignore        |
  57|  SIGCONT           |  specific        |  continue-all  |
  58|  SIGSTOP           |  n/a             |  stop-all      |
  59|  SIGTSTP           |  load-balance    |  stop-all      |
  60|  SIGTTIN           |  load-balance    |  stop-all      |
  61|  SIGTTOU           |  load-balance    |  stop-all      |
  62|  SIGURG            |  load-balance    |  ignore        |
  63|  SIGXCPU           |  specific        |  kill-all+core |
  64|  SIGXFSZ           |  specific        |  kill-all+core |
  65|  SIGVTALRM         |  load-balance    |  kill-all      |
  66|  SIGPROF           |  specific        |  kill-all      |
  67|  SIGPOLL/SIGIO     |  load-balance    |  kill-all      |
  68|  SIGSYS/SIGUNUSED  |  specific        |  kill-all+core |
  69|  SIGSTKFLT         |  specific        |  kill-all      |
  70|  SIGWINCH          |  load-balance    |  ignore        |
  71|  SIGPWR            |  load-balance    |  kill-all      |
  72|  SIGRTMIN-SIGRTMAX |  load-balance    |  kill-all      |
  73----------------------------------------------------------
  74
  75    non-POSIX signal thread group behavior:
  76
  77----------------------------------------------------------
  78|                    |  userspace       |  kernel        |
  79----------------------------------------------------------
  80|  SIGEMT            |  specific        |  kill-all+core |
  81----------------------------------------------------------
  82*/
  83
  84/* Some systems do not have a SIGSTKFLT and the kernel never
  85 * generates such signals anyways.
  86 */
  87#ifdef SIGSTKFLT
  88#define M_SIGSTKFLT     M(SIGSTKFLT)
  89#else
  90#define M_SIGSTKFLT     0
  91#endif
  92
  93#ifdef SIGEMT
  94#define M_SIGEMT        M(SIGEMT)
  95#else
  96#define M_SIGEMT        0
  97#endif
  98
  99#define M(sig) (1UL << (sig))
 100
 101#define SIG_USER_SPECIFIC_MASK (\
 102        M(SIGILL)    |  M(SIGTRAP)   |  M(SIGABRT)   |  M(SIGBUS)    | \
 103        M(SIGFPE)    |  M(SIGSEGV)   |  M(SIGPIPE)   |  M(SIGXFSZ)   | \
 104        M(SIGPROF)   |  M(SIGSYS)    |  M_SIGSTKFLT  |  M(SIGCONT)   | \
 105        M_SIGEMT )
 106
 107#define SIG_USER_LOAD_BALANCE_MASK (\
 108        M(SIGHUP)    |  M(SIGINT)    |  M(SIGQUIT)   |  M(SIGUSR1)   | \
 109        M(SIGUSR2)   |  M(SIGALRM)   |  M(SIGTERM)   |  M(SIGCHLD)   | \
 110        M(SIGURG)    |  M(SIGVTALRM) |  M(SIGPOLL)   |  M(SIGWINCH)  | \
 111        M(SIGPWR)    |  M(SIGTSTP)   |  M(SIGTTIN)   |  M(SIGTTOU)   )
 112
 113#define SIG_KERNEL_SPECIFIC_MASK (\
 114        M(SIGCHLD)   |   M(SIGURG)   |  M(SIGWINCH)                  )
 115
 116#define SIG_KERNEL_BROADCAST_MASK (\
 117        M(SIGHUP)    |  M(SIGINT)    |  M(SIGQUIT)   |  M(SIGILL)    | \
 118        M(SIGTRAP)   |  M(SIGABRT)   |  M(SIGBUS)    |  M(SIGFPE)    | \
 119        M(SIGKILL)   |  M(SIGUSR1)   |  M(SIGSEGV)   |  M(SIGUSR2)   | \
 120        M(SIGPIPE)   |  M(SIGALRM)   |  M(SIGTERM)   |  M(SIGXCPU)   | \
 121        M(SIGXFSZ)   |  M(SIGVTALRM) |  M(SIGPROF)   |  M(SIGPOLL)   | \
 122        M(SIGSYS)    |  M_SIGSTKFLT  |  M(SIGPWR)    |  M(SIGCONT)   | \
 123        M(SIGSTOP)   |  M(SIGTSTP)   |  M(SIGTTIN)   |  M(SIGTTOU)   | \
 124        M_SIGEMT )
 125
 126#define SIG_KERNEL_ONLY_MASK (\
 127        M(SIGKILL)   |  M(SIGSTOP)                                   )
 128
 129#define SIG_KERNEL_COREDUMP_MASK (\
 130        M(SIGQUIT)   |  M(SIGILL)    |  M(SIGTRAP)   |  M(SIGABRT)   | \
 131        M(SIGFPE)    |  M(SIGSEGV)   |  M(SIGBUS)    |  M(SIGSYS)    | \
 132        M(SIGXCPU)   |  M(SIGXFSZ)   |  M_SIGEMT                     )
 133
 134#define T(sig, mask) \
 135        ((1UL << (sig)) & mask)
 136
 137#define sig_user_specific(sig) \
 138                (((sig) < SIGRTMIN)  && T(sig, SIG_USER_SPECIFIC_MASK))
 139#define sig_user_load_balance(sig) \
 140                (((sig) >= SIGRTMIN) || T(sig, SIG_USER_LOAD_BALANCE_MASK))
 141#define sig_kernel_specific(sig) \
 142                (((sig) < SIGRTMIN)  && T(sig, SIG_KERNEL_SPECIFIC_MASK))
 143#define sig_kernel_broadcast(sig) \
 144                (((sig) >= SIGRTMIN) || T(sig, SIG_KERNEL_BROADCAST_MASK))
 145#define sig_kernel_only(sig) \
 146                (((sig) < SIGRTMIN)  && T(sig, SIG_KERNEL_ONLY_MASK))
 147#define sig_kernel_coredump(sig) \
 148                (((sig) < SIGRTMIN)  && T(sig, SIG_KERNEL_COREDUMP_MASK))
 149
 150#define sig_user_defined(t, sig) \
 151        (((t)->sig->action[(sig)-1].sa.sa_handler != SIG_DFL) &&        \
 152         ((t)->sig->action[(sig)-1].sa.sa_handler != SIG_IGN))
 153
 154#define sig_ignored(t, sig) \
 155        (((sig) != SIGCHLD) && \
 156                ((t)->sig->action[(sig)-1].sa.sa_handler == SIG_IGN))
 157
 158void __init signals_init(void)
 159{
 160        sigqueue_cachep =
 161                kmem_cache_create("sigqueue",
 162                                  sizeof(struct sigqueue),
 163                                  __alignof__(struct sigqueue),
 164                                  0, NULL, NULL);
 165        if (!sigqueue_cachep)
 166                panic("signals_init(): cannot create sigqueue SLAB cache");
 167}
 168
 169#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
 170
 171void recalc_sigpending_tsk(struct task_struct *t)
 172{
 173        if (PENDING(&t->pending, &t->blocked) ||
 174                        PENDING(&t->sig->shared_pending, &t->blocked))
 175                set_tsk_thread_flag(t, TIF_SIGPENDING);
 176        else
 177                clear_tsk_thread_flag(t, TIF_SIGPENDING);
 178}
 179
 180void recalc_sigpending(void)
 181{
 182        if (PENDING(&current->pending, &current->blocked) ||
 183                    PENDING(&current->sig->shared_pending, &current->blocked))
 184                set_thread_flag(TIF_SIGPENDING);
 185        else
 186                clear_thread_flag(TIF_SIGPENDING);
 187}
 188
 189/* Given the mask, find the first available signal that should be serviced. */
 190
 191static int
 192next_signal(struct sigpending *pending, sigset_t *mask)
 193{
 194        unsigned long i, *s, *m, x;
 195        int sig = 0;
 196        
 197        s = pending->signal.sig;
 198        m = mask->sig;
 199        switch (_NSIG_WORDS) {
 200        default:
 201                for (i = 0; i < _NSIG_WORDS; ++i, ++s, ++m)
 202                        if ((x = *s &~ *m) != 0) {
 203                                sig = ffz(~x) + i*_NSIG_BPW + 1;
 204                                break;
 205                        }
 206                break;
 207
 208        case 2: if ((x = s[0] &~ m[0]) != 0)
 209                        sig = 1;
 210                else if ((x = s[1] &~ m[1]) != 0)
 211                        sig = _NSIG_BPW + 1;
 212                else
 213                        break;
 214                sig += ffz(~x);
 215                break;
 216
 217        case 1: if ((x = *s &~ *m) != 0)
 218                        sig = ffz(~x) + 1;
 219                break;
 220        }
 221        
 222        return sig;
 223}
 224
 225static void flush_sigqueue(struct sigpending *queue)
 226{
 227        struct sigqueue *q, *n;
 228
 229        sigemptyset(&queue->signal);
 230        q = queue->head;
 231        queue->head = NULL;
 232        queue->tail = &queue->head;
 233
 234        while (q) {
 235                n = q->next;
 236                kmem_cache_free(sigqueue_cachep, q);
 237                atomic_dec(&nr_queued_signals);
 238                q = n;
 239        }
 240}
 241
 242/*
 243 * Flush all pending signals for a task.
 244 */
 245
 246void
 247flush_signals(struct task_struct *t)
 248{
 249        clear_tsk_thread_flag(t,TIF_SIGPENDING);
 250        flush_sigqueue(&t->pending);
 251}
 252
 253static inline void __remove_thread_group(struct task_struct *tsk, struct signal_struct *sig)
 254{
 255        if (tsk == sig->curr_target)
 256                sig->curr_target = next_thread(tsk);
 257        list_del_init(&tsk->thread_group);
 258}
 259
 260void remove_thread_group(struct task_struct *tsk, struct signal_struct *sig)
 261{
 262        write_lock_irq(&tasklist_lock);
 263        spin_lock(&tsk->sig->siglock);
 264
 265        __remove_thread_group(tsk, sig);
 266
 267        spin_unlock(&tsk->sig->siglock);
 268        write_unlock_irq(&tasklist_lock);
 269}
 270
 271/*
 272 * This function expects the tasklist_lock write-locked.
 273 */
 274void __exit_sighand(struct task_struct *tsk)
 275{
 276        struct signal_struct * sig = tsk->sig;
 277
 278        if (!sig)
 279                BUG();
 280        if (!atomic_read(&sig->count))
 281                BUG();
 282        spin_lock(&sig->siglock);
 283        spin_lock(&tsk->sigmask_lock);
 284        tsk->sig = NULL;
 285        if (atomic_dec_and_test(&sig->count)) {
 286                __remove_thread_group(tsk, sig);
 287                spin_unlock(&sig->siglock);
 288                flush_sigqueue(&sig->shared_pending);
 289                kmem_cache_free(sigact_cachep, sig);
 290        } else {
 291                struct task_struct *leader = tsk->group_leader;
 292
 293                /*
 294                 * If there is any task waiting for the group exit
 295                 * then notify it:
 296                 */
 297                if (sig->group_exit_task && atomic_read(&sig->count) <= 2) {
 298                        wake_up_process(sig->group_exit_task);
 299                        sig->group_exit_task = NULL;
 300                }
 301                /*
 302                 * If we are the last non-leader member of the thread
 303                 * group, and the leader is zombie, then notify the
 304                 * group leader's parent process.
 305                 *
 306                 * (subtle: here we also rely on the fact that if we are the
 307                 *  thread group leader then we are not zombied yet.)
 308                 */
 309                if (atomic_read(&sig->count) == 1 &&
 310                                        leader->state == TASK_ZOMBIE) {
 311
 312                        __remove_thread_group(tsk, sig);
 313                        spin_unlock(&sig->siglock);
 314                        do_notify_parent(leader, leader->exit_signal);
 315                } else {
 316                        __remove_thread_group(tsk, sig);
 317                        spin_unlock(&sig->siglock);
 318                }
 319        }
 320        clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
 321        flush_sigqueue(&tsk->pending);
 322
 323        spin_unlock(&tsk->sigmask_lock);
 324}
 325
 326void exit_sighand(struct task_struct *tsk)
 327{
 328        write_lock_irq(&tasklist_lock);
 329        __exit_sighand(tsk);
 330        write_unlock_irq(&tasklist_lock);
 331}
 332
 333/*
 334 * Flush all handlers for a task.
 335 */
 336
 337void
 338flush_signal_handlers(struct task_struct *t)
 339{
 340        int i;
 341        struct k_sigaction *ka = &t->sig->action[0];
 342        for (i = _NSIG ; i != 0 ; i--) {
 343                if (ka->sa.sa_handler != SIG_IGN)
 344                        ka->sa.sa_handler = SIG_DFL;
 345                ka->sa.sa_flags = 0;
 346                sigemptyset(&ka->sa.sa_mask);
 347                ka++;
 348        }
 349}
 350
 351/*
 352 * sig_exit - cause the current task to exit due to a signal.
 353 */
 354
 355void
 356sig_exit(int sig, int exit_code, struct siginfo *info)
 357{
 358        sigaddset(&current->pending.signal, sig);
 359        recalc_sigpending();
 360        current->flags |= PF_SIGNALED;
 361
 362        if (current->sig->group_exit)
 363                exit_code = current->sig->group_exit_code;
 364
 365        do_exit(exit_code);
 366        /* NOTREACHED */
 367}
 368
 369/* Notify the system that a driver wants to block all signals for this
 370 * process, and wants to be notified if any signals at all were to be
 371 * sent/acted upon.  If the notifier routine returns non-zero, then the
 372 * signal will be acted upon after all.  If the notifier routine returns 0,
 373 * then then signal will be blocked.  Only one block per process is
 374 * allowed.  priv is a pointer to private data that the notifier routine
 375 * can use to determine if the signal should be blocked or not.  */
 376
 377void
 378block_all_signals(int (*notifier)(void *priv), void *priv, sigset_t *mask)
 379{
 380        unsigned long flags;
 381
 382        spin_lock_irqsave(&current->sigmask_lock, flags);
 383        current->notifier_mask = mask;
 384        current->notifier_data = priv;
 385        current->notifier = notifier;
 386        spin_unlock_irqrestore(&current->sigmask_lock, flags);
 387}
 388
 389/* Notify the system that blocking has ended. */
 390
 391void
 392unblock_all_signals(void)
 393{
 394        unsigned long flags;
 395
 396        spin_lock_irqsave(&current->sigmask_lock, flags);
 397        current->notifier = NULL;
 398        current->notifier_data = NULL;
 399        recalc_sigpending();
 400        spin_unlock_irqrestore(&current->sigmask_lock, flags);
 401}
 402
 403static inline int collect_signal(int sig, struct sigpending *list, siginfo_t *info)
 404{
 405        if (sigismember(&list->signal, sig)) {
 406                /* Collect the siginfo appropriate to this signal.  */
 407                struct sigqueue *q, **pp;
 408                pp = &list->head;
 409                while ((q = *pp) != NULL) {
 410                        if (q->info.si_signo == sig)
 411                                goto found_it;
 412                        pp = &q->next;
 413                }
 414
 415                /* Ok, it wasn't in the queue.  This must be
 416                   a fast-pathed signal or we must have been
 417                   out of queue space.  So zero out the info.
 418                 */
 419                sigdelset(&list->signal, sig);
 420                info->si_signo = sig;
 421                info->si_errno = 0;
 422                info->si_code = 0;
 423                info->si_pid = 0;
 424                info->si_uid = 0;
 425                return 1;
 426
 427found_it:
 428                if ((*pp = q->next) == NULL)
 429                        list->tail = pp;
 430
 431                /* Copy the sigqueue information and free the queue entry */
 432                copy_siginfo(info, &q->info);
 433                kmem_cache_free(sigqueue_cachep,q);
 434                atomic_dec(&nr_queued_signals);
 435
 436                /* Non-RT signals can exist multiple times.. */
 437                if (sig >= SIGRTMIN) {
 438                        while ((q = *pp) != NULL) {
 439                                if (q->info.si_signo == sig)
 440                                        goto found_another;
 441                                pp = &q->next;
 442                        }
 443                }
 444
 445                sigdelset(&list->signal, sig);
 446found_another:
 447                return 1;
 448        }
 449        return 0;
 450}
 451
 452/*
 453 * Dequeue a signal and return the element to the caller, which is 
 454 * expected to free it.
 455 *
 456 * All callers have to hold the siglock and the sigmask_lock.
 457 */
 458
 459int dequeue_signal(struct sigpending *pending, sigset_t *mask, siginfo_t *info)
 460{
 461        int sig = 0;
 462
 463        sig = next_signal(pending, mask);
 464        if (sig) {
 465                if (current->notifier) {
 466                        if (sigismember(current->notifier_mask, sig)) {
 467                                if (!(current->notifier)(current->notifier_data)) {
 468                                        clear_thread_flag(TIF_SIGPENDING);
 469                                        return 0;
 470                                }
 471                        }
 472                }
 473
 474                if (!collect_signal(sig, pending, info))
 475                        sig = 0;
 476                                
 477                /* XXX: Once POSIX.1b timers are in, if si_code == SI_TIMER,
 478                   we need to xchg out the timer overrun values.  */
 479        }
 480        recalc_sigpending();
 481
 482        return sig;
 483}
 484
 485static int rm_from_queue(int sig, struct sigpending *s)
 486{
 487        struct sigqueue *q, **pp;
 488
 489        if (!sigismember(&s->signal, sig))
 490                return 0;
 491
 492        sigdelset(&s->signal, sig);
 493
 494        pp = &s->head;
 495
 496        while ((q = *pp) != NULL) {
 497                if (q->info.si_signo == sig) {
 498                        if ((*pp = q->next) == NULL)
 499                                s->tail = pp;
 500                        kmem_cache_free(sigqueue_cachep,q);
 501                        atomic_dec(&nr_queued_signals);
 502                        continue;
 503                }
 504                pp = &q->next;
 505        }
 506        return 1;
 507}
 508
 509/*
 510 * Remove signal sig from t->pending.
 511 * Returns 1 if sig was found.
 512 *
 513 * All callers must be holding t->sigmask_lock.
 514 */
 515static int rm_sig_from_queue(int sig, struct task_struct *t)
 516{
 517        return rm_from_queue(sig, &t->pending);
 518}
 519
 520/*
 521 * Bad permissions for sending the signal
 522 */
 523static inline int bad_signal(int sig, struct siginfo *info, struct task_struct *t)
 524{
 525        return (!info || ((unsigned long)info != 1 &&
 526                        (unsigned long)info != 2 && SI_FROMUSER(info)))
 527            && ((sig != SIGCONT) || (current->session != t->session))
 528            && (current->euid ^ t->suid) && (current->euid ^ t->uid)
 529            && (current->uid ^ t->suid) && (current->uid ^ t->uid)
 530            && !capable(CAP_KILL);
 531}
 532
 533/*
 534 * Signal type:
 535 *    < 0 : global action (kill - spread to all non-blocked threads)
 536 *    = 0 : ignored
 537 *    > 0 : wake up.
 538 */
 539static int signal_type(int sig, struct signal_struct *signals)
 540{
 541        unsigned long handler;
 542
 543        if (!signals)
 544                return 0;
 545        
 546        handler = (unsigned long) signals->action[sig-1].sa.sa_handler;
 547        if (handler > 1)
 548                return 1;
 549
 550        /* "Ignore" handler.. Illogical, but that has an implicit handler for SIGCHLD */
 551        if (handler == 1)
 552                return sig == SIGCHLD;
 553
 554        /* Default handler. Normally lethal, but.. */
 555        switch (sig) {
 556
 557        /* Ignored */
 558        case SIGCONT: case SIGWINCH:
 559        case SIGCHLD: case SIGURG:
 560                return 0;
 561
 562        /* Implicit behaviour */
 563        case SIGTSTP: case SIGTTIN: case SIGTTOU:
 564                return 1;
 565
 566        /* Implicit actions (kill or do special stuff) */
 567        default:
 568                return -1;
 569        }
 570}
 571                
 572
 573/*
 574 * Determine whether a signal should be posted or not.
 575 *
 576 * Signals with SIG_IGN can be ignored, except for the
 577 * special case of a SIGCHLD. 
 578 *
 579 * Some signals with SIG_DFL default to a non-action.
 580 */
 581static int ignored_signal(int sig, struct task_struct *t)
 582{
 583        /* Don't ignore traced or blocked signals */
 584        if ((t->ptrace & PT_PTRACED) || sigismember(&t->blocked, sig))
 585                return 0;
 586
 587        return signal_type(sig, t->sig) == 0;
 588}
 589
 590/*
 591 * Handle TASK_STOPPED cases etc implicit behaviour
 592 * of certain magical signals.
 593 *
 594 * SIGKILL gets spread out to every thread. 
 595 */
 596static void handle_stop_signal(int sig, struct task_struct *t)
 597{
 598        switch (sig) {
 599        case SIGKILL: case SIGCONT:
 600                /* Wake up the process if stopped.  */
 601                if (t->state == TASK_STOPPED)
 602                        wake_up_process(t);
 603                t->exit_code = 0;
 604                rm_sig_from_queue(SIGSTOP, t);
 605                rm_sig_from_queue(SIGTSTP, t);
 606                rm_sig_from_queue(SIGTTOU, t);
 607                rm_sig_from_queue(SIGTTIN, t);
 608                break;
 609
 610        case SIGSTOP: case SIGTSTP:
 611        case SIGTTIN: case SIGTTOU:
 612                /* If we're stopping again, cancel SIGCONT */
 613                rm_sig_from_queue(SIGCONT, t);
 614                break;
 615        }
 616}
 617
 618static int send_signal(int sig, struct siginfo *info, struct sigpending *signals)
 619{
 620        struct sigqueue * q = NULL;
 621
 622        /*
 623         * fast-pathed signals for kernel-internal things like SIGSTOP
 624         * or SIGKILL.
 625         */
 626        if ((unsigned long)info == 2)
 627                goto out_set;
 628
 629        /* Real-time signals must be queued if sent by sigqueue, or
 630           some other real-time mechanism.  It is implementation
 631           defined whether kill() does so.  We attempt to do so, on
 632           the principle of least surprise, but since kill is not
 633           allowed to fail with EAGAIN when low on memory we just
 634           make sure at least one signal gets delivered and don't
 635           pass on the info struct.  */
 636
 637        if (atomic_read(&nr_queued_signals) < max_queued_signals)
 638                q = kmem_cache_alloc(sigqueue_cachep, GFP_ATOMIC);
 639
 640        if (q) {
 641                atomic_inc(&nr_queued_signals);
 642                q->next = NULL;
 643                *signals->tail = q;
 644                signals->tail = &q->next;
 645                switch ((unsigned long) info) {
 646                        case 0:
 647                                q->info.si_signo = sig;
 648                                q->info.si_errno = 0;
 649                                q->info.si_code = SI_USER;
 650                                q->info.si_pid = current->pid;
 651                                q->info.si_uid = current->uid;
 652                                break;
 653                        case 1:
 654                                q->info.si_signo = sig;
 655                                q->info.si_errno = 0;
 656                                q->info.si_code = SI_KERNEL;
 657                                q->info.si_pid = 0;
 658                                q->info.si_uid = 0;
 659                                break;
 660                        default:
 661                                copy_siginfo(&q->info, info);
 662                                break;
 663                }
 664        } else if (sig >= SIGRTMIN && info && (unsigned long)info != 1
 665                   && info->si_code != SI_USER)
 666                /*
 667                 * Queue overflow, abort.  We may abort if the signal was rt
 668                 * and sent by user using something other than kill().
 669                 */
 670                return -EAGAIN;
 671
 672out_set:
 673        sigaddset(&signals->signal, sig);
 674        return 0;
 675}
 676
 677/*
 678 * Tell a process that it has a new active signal..
 679 *
 680 * NOTE! we rely on the previous spin_lock to
 681 * lock interrupts for us! We can only be called with
 682 * "sigmask_lock" held, and the local interrupt must
 683 * have been disabled when that got acquired!
 684 *
 685 * No need to set need_resched since signal event passing
 686 * goes through ->blocked
 687 */
 688inline void signal_wake_up(struct task_struct *t)
 689{
 690        set_tsk_thread_flag(t,TIF_SIGPENDING);
 691
 692        /*
 693         * If the task is running on a different CPU 
 694         * force a reschedule on the other CPU to make
 695         * it notice the new signal quickly.
 696         *
 697         * The code below is a tad loose and might occasionally
 698         * kick the wrong CPU if we catch the process in the
 699         * process of changing - but no harm is done by that
 700         * other than doing an extra (lightweight) IPI interrupt.
 701         */
 702        if (t->state == TASK_RUNNING)
 703                kick_if_running(t);
 704        if (t->state & TASK_INTERRUPTIBLE) {
 705                wake_up_process(t);
 706                return;
 707        }
 708}
 709
 710static int deliver_signal(int sig, struct siginfo *info, struct task_struct *t)
 711{
 712        int retval = send_signal(sig, info, &t->pending);
 713
 714        if (!retval && !sigismember(&t->blocked, sig))
 715                signal_wake_up(t);
 716
 717        return retval;
 718}
 719
 720static int
 721__send_sig_info(int sig, struct siginfo *info, struct task_struct *t, int shared)
 722{
 723        int ret;
 724
 725        if (!irqs_disabled())
 726                BUG();
 727#if CONFIG_SMP
 728        if (!spin_is_locked(&t->sig->siglock))
 729                BUG();
 730#endif
 731        ret = -EINVAL;
 732        if (sig < 0 || sig > _NSIG)
 733                goto out_nolock;
 734        /* The somewhat baroque permissions check... */
 735        ret = -EPERM;
 736        if (bad_signal(sig, info, t))
 737                goto out_nolock;
 738        ret = security_ops->task_kill(t, info, sig);
 739        if (ret)
 740                goto out_nolock;
 741
 742        /* The null signal is a permissions and process existence probe.
 743           No signal is actually delivered.  Same goes for zombies. */
 744        ret = 0;
 745        if (!sig || !t->sig)
 746                goto out_nolock;
 747
 748        spin_lock(&t->sigmask_lock);
 749        handle_stop_signal(sig, t);
 750
 751        /* Optimize away the signal, if it's a signal that can be
 752           handled immediately (ie non-blocked and untraced) and
 753           that is ignored (either explicitly or by default).  */
 754
 755        if (ignored_signal(sig, t))
 756                goto out;
 757
 758#define LEGACY_QUEUE(sigptr, sig) \
 759        (((sig) < SIGRTMIN) && sigismember(&(sigptr)->signal, (sig)))
 760
 761        if (!shared) {
 762                /* Support queueing exactly one non-rt signal, so that we
 763                   can get more detailed information about the cause of
 764                   the signal. */
 765                if (LEGACY_QUEUE(&t->pending, sig))
 766                        goto out;
 767
 768                ret = deliver_signal(sig, info, t);
 769        } else {
 770                if (LEGACY_QUEUE(&t->sig->shared_pending, sig))
 771                        goto out;
 772                ret = send_signal(sig, info, &t->sig->shared_pending);
 773        }
 774out:
 775        spin_unlock(&t->sigmask_lock);
 776out_nolock:
 777        return ret;
 778}
 779
 780/*
 781 * Force a signal that the process can't ignore: if necessary
 782 * we unblock the signal and change any SIG_IGN to SIG_DFL.
 783 */
 784
 785int
 786force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
 787{
 788        unsigned long int flags;
 789
 790        spin_lock_irqsave(&t->sigmask_lock, flags);
 791        if (t->sig == NULL) {
 792                spin_unlock_irqrestore(&t->sigmask_lock, flags);
 793                return -ESRCH;
 794        }
 795
 796        if (t->sig->action[sig-1].sa.sa_handler == SIG_IGN)
 797                t->sig->action[sig-1].sa.sa_handler = SIG_DFL;
 798        sigdelset(&t->blocked, sig);
 799        recalc_sigpending_tsk(t);
 800        spin_unlock_irqrestore(&t->sigmask_lock, flags);
 801
 802        return send_sig_info(sig, (void *)1, t);
 803}
 804
 805static int
 806__force_sig_info(int sig, struct task_struct *t)
 807{
 808        unsigned long int flags;
 809
 810        spin_lock_irqsave(&t->sigmask_lock, flags);
 811        if (t->sig == NULL) {
 812                spin_unlock_irqrestore(&t->sigmask_lock, flags);
 813                return -ESRCH;
 814        }
 815
 816        if (t->sig->action[sig-1].sa.sa_handler == SIG_IGN)
 817                t->sig->action[sig-1].sa.sa_handler = SIG_DFL;
 818        sigdelset(&t->blocked, sig);
 819        recalc_sigpending_tsk(t);
 820        spin_unlock_irqrestore(&t->sigmask_lock, flags);
 821
 822        return __send_sig_info(sig, (void *)2, t, 0);
 823}
 824
 825#define can_take_signal(p, sig) \
 826        (((unsigned long) p->sig->action[sig-1].sa.sa_handler > 1) && \
 827        !sigismember(&p->blocked, sig) && (task_curr(p) || !signal_pending(p)))
 828
 829static inline
 830int load_balance_thread_group(struct task_struct *p, int sig,
 831                                struct siginfo *info)
 832{
 833        struct task_struct *tmp;
 834        int ret;
 835
 836        /*
 837         * if the specified thread is not blocking this signal
 838         * then deliver it.
 839         */
 840        if (can_take_signal(p, sig))
 841                return __send_sig_info(sig, info, p, 0);
 842
 843        /*
 844         * Otherwise try to find a suitable thread.
 845         * If no such thread is found then deliver to
 846         * the original thread.
 847         */
 848
 849        tmp = p->sig->curr_target;
 850
 851        if (!tmp || tmp->tgid != p->tgid)
 852                /* restart balancing at this thread */
 853                p->sig->curr_target = p;
 854
 855        else for (;;) {
 856                if (list_empty(&p->thread_group))
 857                        BUG();
 858                if (!tmp || tmp->tgid != p->tgid)
 859                        BUG();
 860
 861                /*
 862                 * Do not send signals that are ignored or blocked,
 863                 * or to not-running threads that are overworked:
 864                 */
 865                if (!can_take_signal(tmp, sig)) {
 866                        tmp = next_thread(tmp);
 867                        p->sig->curr_target = tmp;
 868                        if (tmp == p)
 869                                break;
 870                        continue;
 871                }
 872                ret = __send_sig_info(sig, info, tmp, 0);
 873                return ret;
 874        }
 875        /*
 876         * No suitable thread was found - put the signal
 877         * into the shared-pending queue.
 878         */
 879        return __send_sig_info(sig, info, p, 1);
 880}
 881
 882int __broadcast_thread_group(struct task_struct *p, int sig)
 883{
 884        struct task_struct *tmp;
 885        struct list_head *entry;
 886        int err = 0;
 887
 888        /* send a signal to the head of the list */
 889        err = __force_sig_info(sig, p);
 890
 891        /* send a signal to all members of the list */
 892        list_for_each(entry, &p->thread_group) {
 893                tmp = list_entry(entry, task_t, thread_group);
 894                err = __force_sig_info(sig, tmp);
 895        }
 896        return err;
 897}
 898
 899int
 900send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
 901{
 902        unsigned long flags;
 903        int ret = 0;
 904
 905        if (!p)
 906                BUG();
 907        if (!p->sig)
 908                BUG();
 909        spin_lock_irqsave(&p->sig->siglock, flags);
 910
 911        /* not a thread group - normal signal behavior */
 912        if (list_empty(&p->thread_group) || !sig)
 913                goto out_send;
 914
 915        if (sig_user_defined(p, sig)) {
 916                if (sig_user_specific(sig))
 917                        goto out_send;
 918                if (sig_user_load_balance(sig)) {
 919                        ret = load_balance_thread_group(p, sig, info);
 920                        goto out_unlock;
 921                }
 922
 923                /* must not happen */
 924                BUG();
 925        }
 926        /* optimize away ignored signals: */
 927        if (sig_ignored(p, sig))
 928                goto out_unlock;
 929
 930        /* blocked (or ptraced) signals get posted */
 931        spin_lock(&p->sigmask_lock);
 932        if ((p->ptrace & PT_PTRACED) || sigismember(&p->blocked, sig) ||
 933                                        sigismember(&p->real_blocked, sig)) {
 934                spin_unlock(&p->sigmask_lock);
 935                goto out_send;
 936        }
 937        spin_unlock(&p->sigmask_lock);
 938
 939        if (sig_kernel_broadcast(sig) || sig_kernel_coredump(sig)) {
 940                ret = __broadcast_thread_group(p, sig);
 941                goto out_unlock;
 942        }
 943        if (sig_kernel_specific(sig))
 944                goto out_send;
 945
 946        /* must not happen */
 947        BUG();
 948out_send:
 949        ret = __send_sig_info(sig, info, p, 0);
 950out_unlock:
 951        spin_unlock_irqrestore(&p->sig->siglock, flags);
 952        return ret;
 953}
 954
 955/*
 956 * kill_pg_info() sends a signal to a process group: this is what the tty
 957 * control characters do (^C, ^Z etc)
 958 */
 959
 960int __kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
 961{
 962        struct task_struct *p;
 963        struct list_head *l;
 964        struct pid *pid;
 965        int err, retval = -ESRCH;
 966
 967        if (pgrp <= 0)
 968                return -EINVAL;
 969
 970        for_each_task_pid(pgrp, PIDTYPE_PGID, p, l, pid) {
 971                err = send_sig_info(sig, info, p);
 972                if (retval)
 973                        retval = err;
 974        }
 975        return retval;
 976}
 977
 978int
 979kill_pg_info(int sig, struct siginfo *info, pid_t pgrp)
 980{
 981        int retval;
 982
 983        read_lock(&tasklist_lock);
 984        retval = __kill_pg_info(sig, info, pgrp);
 985        read_unlock(&tasklist_lock);
 986
 987        return retval;
 988}
 989
 990/*
 991 * kill_sl_info() sends a signal to the session leader: this is used
 992 * to send SIGHUP to the controlling process of a terminal when
 993 * the connection is lost.
 994 */
 995
 996
 997int
 998kill_sl_info(int sig, struct siginfo *info, pid_t sid)
 999{
1000        int err, retval = -EINVAL;
1001        struct pid *pid;
1002        struct list_head *l;
1003        struct task_struct *p;
1004
1005        if (sid <= 0)
1006                goto out;
1007
1008        retval = -ESRCH;
1009        read_lock(&tasklist_lock);
1010        for_each_task_pid(sid, PIDTYPE_SID, p, l, pid) {
1011                if (!p->leader)
1012                        continue;
1013                err = send_sig_info(sig, info, p);
1014                if (retval)
1015                        retval = err;
1016        }
1017        read_unlock(&tasklist_lock);
1018out:
1019        return retval;
1020}
1021
1022int
1023kill_proc_info(int sig, struct siginfo *info, pid_t pid)
1024{
1025        int error;
1026        struct task_struct *p;
1027
1028        read_lock(&tasklist_lock);
1029        p = find_task_by_pid(pid);
1030        error = -ESRCH;
1031        if (p)
1032                error = send_sig_info(sig, info, p);
1033        read_unlock(&tasklist_lock);
1034        return error;
1035}
1036
1037
1038/*
1039 * kill_something_info() interprets pid in interesting ways just like kill(2).
1040 *
1041 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1042 * is probably wrong.  Should make it like BSD or SYSV.
1043 */
1044
1045static int kill_something_info(int sig, struct siginfo *info, int pid)
1046{
1047        if (!pid) {
1048                return kill_pg_info(sig, info, current->pgrp);
1049        } else if (pid == -1) {
1050                int retval = 0, count = 0;
1051                struct task_struct * p;
1052
1053                read_lock(&tasklist_lock);
1054                for_each_process(p) {
1055                        if (p->pid > 1 && p != current) {
1056                                int err = send_sig_info(sig, info, p);
1057                                ++count;
1058                                if (err != -EPERM)
1059                                        retval = err;
1060                        }
1061                }
1062                read_unlock(&tasklist_lock);
1063                return count ? retval : -ESRCH;
1064        } else if (pid < 0) {
1065                return kill_pg_info(sig, info, -pid);
1066        } else {
1067                return kill_proc_info(sig, info, pid);
1068        }
1069}
1070
1071/*
1072 * These are for backward compatibility with the rest of the kernel source.
1073 */
1074
1075int
1076send_sig(int sig, struct task_struct *p, int priv)
1077{
1078        return send_sig_info(sig, (void*)(long)(priv != 0), p);
1079}
1080
1081void
1082force_sig(int sig, struct task_struct *p)
1083{
1084        force_sig_info(sig, (void*)1L, p);
1085}
1086
1087int
1088kill_pg(pid_t pgrp, int sig, int priv)
1089{
1090        return kill_pg_info(sig, (void *)(long)(priv != 0), pgrp);
1091}
1092
1093int
1094kill_sl(pid_t sess, int sig, int priv)
1095{
1096        return kill_sl_info(sig, (void *)(long)(priv != 0), sess);
1097}
1098
1099int
1100kill_proc(pid_t pid, int sig, int priv)
1101{
1102        return kill_proc_info(sig, (void *)(long)(priv != 0), pid);
1103}
1104
1105/*
1106 * Joy. Or not. Pthread wants us to wake up every thread
1107 * in our parent group.
1108 */
1109static inline void wake_up_parent(struct task_struct *p)
1110{
1111        struct task_struct *parent = p->parent, *tsk = parent;
1112
1113        /*
1114         * Fortunately this is not necessary for thread groups:
1115         */
1116        if (p->tgid == tsk->tgid) {
1117                wake_up_interruptible(&tsk->wait_chldexit);
1118                return;
1119        }
1120        spin_lock_irq(&parent->sig->siglock);
1121        do {
1122                wake_up_interruptible(&tsk->wait_chldexit);
1123                tsk = next_thread(tsk);
1124                if (tsk->sig != parent->sig)
1125                        BUG();
1126        } while (tsk != parent);
1127        spin_unlock_irq(&parent->sig->siglock);
1128}
1129
1130/*
1131 * Let a parent know about a status change of a child.
1132 */
1133
1134void do_notify_parent(struct task_struct *tsk, int sig)
1135{
1136        struct siginfo info;
1137        int why, status;
1138
1139        if (!tsk->ptrace && delay_group_leader(tsk))
1140                return;
1141        if (sig == -1)
1142                BUG();
1143
1144        info.si_signo = sig;
1145        info.si_errno = 0;
1146        info.si_pid = tsk->pid;
1147        info.si_uid = tsk->uid;
1148
1149        /* FIXME: find out whether or not this is supposed to be c*time. */
1150        info.si_utime = tsk->utime;
1151        info.si_stime = tsk->stime;
1152
1153        status = tsk->exit_code & 0x7f;
1154        why = SI_KERNEL;        /* shouldn't happen */
1155        switch (tsk->state) {
1156        case TASK_STOPPED:
1157                /* FIXME -- can we deduce CLD_TRAPPED or CLD_CONTINUED? */
1158                if (tsk->ptrace & PT_PTRACED)
1159                        why = CLD_TRAPPED;
1160                else
1161                        why = CLD_STOPPED;
1162                break;
1163
1164        default:
1165                if (tsk->exit_code & 0x80)
1166                        why = CLD_DUMPED;
1167                else if (tsk->exit_code & 0x7f)
1168                        why = CLD_KILLED;
1169                else {
1170                        why = CLD_EXITED;
1171                        status = tsk->exit_code >> 8;
1172                }
1173                break;
1174        }
1175        info.si_code = why;
1176        info.si_status = status;
1177
1178        send_sig_info(sig, &info, tsk->parent);
1179        wake_up_parent(tsk);
1180}
1181
1182
1183/*
1184 * We need the tasklist lock because it's the only
1185 * thing that protects out "parent" pointer.
1186 *
1187 * exit.c calls "do_notify_parent()" directly, because
1188 * it already has the tasklist lock.
1189 */
1190void
1191notify_parent(struct task_struct *tsk, int sig)
1192{
1193        if (sig != -1) {
1194                read_lock(&tasklist_lock);
1195                do_notify_parent(tsk, sig);
1196                read_unlock(&tasklist_lock);
1197        }
1198}
1199
1200#ifndef HAVE_ARCH_GET_SIGNAL_TO_DELIVER
1201
1202int get_signal_to_deliver(siginfo_t *info, struct pt_regs *regs)
1203{
1204        sigset_t *mask = &current->blocked;
1205
1206        for (;;) {
1207                unsigned long signr = 0;
1208                struct k_sigaction *ka;
1209
1210                local_irq_disable();
1211                if (current->sig->shared_pending.head) {
1212                        spin_lock(&current->sig->siglock);
1213                        signr = dequeue_signal(&current->sig->shared_pending, mask, info);
1214                        spin_unlock(&current->sig->siglock);
1215                }
1216                if (!signr) {
1217                        spin_lock(&current->sigmask_lock);
1218                        signr = dequeue_signal(&current->pending, mask, info);
1219                        spin_unlock(&current->sigmask_lock);
1220                }
1221                local_irq_enable();
1222
1223                if (!signr)
1224                        break;
1225
1226                if ((current->ptrace & PT_PTRACED) && signr != SIGKILL) {
1227                        /* Let the debugger run.  */
1228                        current->exit_code = signr;
1229                        set_current_state(TASK_STOPPED);
1230                        notify_parent(current, SIGCHLD);
1231                        schedule();
1232
1233                        /* We're back.  Did the debugger cancel the sig?  */
1234                        signr = current->exit_code;
1235                        if (signr == 0)
1236                                continue;
1237                        current->exit_code = 0;
1238
1239                        /* The debugger continued.  Ignore SIGSTOP.  */
1240                        if (signr == SIGSTOP)
1241                                continue;
1242
1243                        /* Update the siginfo structure.  Is this good?  */
1244                        if (signr != info->si_signo) {
1245                                info->si_signo = signr;
1246                                info->si_errno = 0;
1247                                info->si_code = SI_USER;
1248                                info->si_pid = current->parent->pid;
1249                                info->si_uid = current->parent->uid;
1250                        }
1251
1252                        /* If the (new) signal is now blocked, requeue it.  */
1253                        if (sigismember(&current->blocked, signr)) {
1254                                send_sig_info(signr, info, current);
1255                                continue;
1256                        }
1257                }
1258
1259                ka = &current->sig->action[signr-1];
1260                if (ka->sa.sa_handler == SIG_IGN) {
1261                        if (signr != SIGCHLD)
1262                                continue;
1263                        /* Check for SIGCHLD: it's special.  */
1264                        while (sys_wait4(-1, NULL, WNOHANG, NULL) > 0)
1265                                /* nothing */;
1266                        continue;
1267                }
1268
1269                if (ka->sa.sa_handler == SIG_DFL) {
1270                        int exit_code = signr;
1271
1272                        /* Init gets no signals it doesn't want.  */
1273                        if (current->pid == 1)
1274                                continue;
1275
1276                        switch (signr) {
1277                        case SIGCONT: case SIGCHLD: case SIGWINCH: case SIGURG:
1278                                continue;
1279
1280                        case SIGTSTP: case SIGTTIN: case SIGTTOU:
1281                                if (is_orphaned_pgrp(current->pgrp))
1282                                        continue;
1283                                /* FALLTHRU */
1284
1285                        case SIGSTOP: {
1286                                struct signal_struct *sig;
1287                                set_current_state(TASK_STOPPED);
1288                                current->exit_code = signr;
1289                                sig = current->parent->sig;
1290                                if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1291                                        notify_parent(current, SIGCHLD);
1292                                schedule();
1293                                continue;
1294                        }
1295
1296                        case SIGQUIT: case SIGILL: case SIGTRAP:
1297                        case SIGABRT: case SIGFPE: case SIGSEGV:
1298                        case SIGBUS: case SIGSYS: case SIGXCPU: case SIGXFSZ:
1299                                if (do_coredump(signr, regs))
1300                                        exit_code |= 0x80;
1301                                /* FALLTHRU */
1302
1303                        default:
1304                                sig_exit(signr, exit_code, info);
1305                                /* NOTREACHED */
1306                        }
1307                }
1308                return signr;
1309        }
1310        return 0;
1311}
1312
1313#endif
1314
1315EXPORT_SYMBOL(recalc_sigpending);
1316EXPORT_SYMBOL(dequeue_signal);
1317EXPORT_SYMBOL(flush_signals);
1318EXPORT_SYMBOL(force_sig);
1319EXPORT_SYMBOL(force_sig_info);
1320EXPORT_SYMBOL(kill_pg);
1321EXPORT_SYMBOL(kill_pg_info);
1322EXPORT_SYMBOL(kill_proc);
1323EXPORT_SYMBOL(kill_proc_info);
1324EXPORT_SYMBOL(kill_sl);
1325EXPORT_SYMBOL(kill_sl_info);
1326EXPORT_SYMBOL(notify_parent);
1327EXPORT_SYMBOL(send_sig);
1328EXPORT_SYMBOL(send_sig_info);
1329EXPORT_SYMBOL(block_all_signals);
1330EXPORT_SYMBOL(unblock_all_signals);
1331
1332
1333/*
1334 * System call entry points.
1335 */
1336
1337/*
1338 * We don't need to get the kernel lock - this is all local to this
1339 * particular thread.. (and that's good, because this is _heavily_
1340 * used by various programs)
1341 */
1342
1343asmlinkage long
1344sys_rt_sigprocmask(int how, sigset_t *set, sigset_t *oset, size_t sigsetsize)
1345{
1346        int error = -EINVAL;
1347        sigset_t old_set, new_set;
1348
1349        /* XXX: Don't preclude handling different sized sigset_t's.  */
1350        if (sigsetsize != sizeof(sigset_t))
1351                goto out;
1352
1353        if (set) {
1354                error = -EFAULT;
1355                if (copy_from_user(&new_set, set, sizeof(*set)))
1356                        goto out;
1357                sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
1358
1359                spin_lock_irq(&current->sigmask_lock);
1360                old_set = current->blocked;
1361
1362                error = 0;
1363                switch (how) {
1364                default:
1365                        error = -EINVAL;
1366                        break;
1367                case SIG_BLOCK:
1368                        sigorsets(&new_set, &old_set, &new_set);
1369                        break;
1370                case SIG_UNBLOCK:
1371                        signandsets(&new_set, &old_set, &new_set);
1372                        break;
1373                case SIG_SETMASK:
1374                        break;
1375                }
1376
1377                current->blocked = new_set;
1378                recalc_sigpending();
1379                spin_unlock_irq(&current->sigmask_lock);
1380                if (error)
1381                        goto out;
1382                if (oset)
1383                        goto set_old;
1384        } else if (oset) {
1385                spin_lock_irq(&current->sigmask_lock);
1386                old_set = current->blocked;
1387                spin_unlock_irq(&current->sigmask_lock);
1388
1389        set_old:
1390                error = -EFAULT;
1391                if (copy_to_user(oset, &old_set, sizeof(*oset)))
1392                        goto out;
1393        }
1394        error = 0;
1395out:
1396        return error;
1397}
1398
1399long do_sigpending(void *set, unsigned long sigsetsize)
1400{
1401        long error = -EINVAL;
1402        sigset_t pending;
1403
1404        if (sigsetsize > sizeof(sigset_t))
1405                goto out;
1406
1407        spin_lock_irq(&current->sigmask_lock);
1408        sigandsets(&pending, &current->blocked, &current->pending.signal);
1409        spin_unlock_irq(&current->sigmask_lock);
1410
1411        error = -EFAULT;
1412        if (!copy_to_user(set, &pending, sigsetsize))
1413                error = 0;
1414out:
1415        return error;
1416}       
1417
1418asmlinkage long
1419sys_rt_sigpending(sigset_t *set, size_t sigsetsize)
1420{
1421        return do_sigpending(set, sigsetsize);
1422}
1423
1424#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
1425
1426int copy_siginfo_to_user(siginfo_t *to, siginfo_t *from)
1427{
1428        int err;
1429
1430        if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
1431                return -EFAULT;
1432        if (from->si_code < 0)
1433                return __copy_to_user(to, from, sizeof(siginfo_t))
1434                        ? -EFAULT : 0;
1435        /*
1436         * If you change siginfo_t structure, please be sure
1437         * this code is fixed accordingly.
1438         * It should never copy any pad contained in the structure
1439         * to avoid security leaks, but must copy the generic
1440         * 3 ints plus the relevant union member.
1441         */
1442        err = __put_user(from->si_signo, &to->si_signo);
1443        err |= __put_user(from->si_errno, &to->si_errno);
1444        err |= __put_user((short)from->si_code, &to->si_code);
1445        switch (from->si_code & __SI_MASK) {
1446        case __SI_KILL:
1447                err |= __put_user(from->si_pid, &to->si_pid);
1448                err |= __put_user(from->si_uid, &to->si_uid);
1449                break;
1450        case __SI_TIMER:
1451                err |= __put_user(from->si_timer1, &to->si_timer1);
1452                err |= __put_user(from->si_timer2, &to->si_timer2);
1453                break;
1454        case __SI_POLL:
1455                err |= __put_user(from->si_band, &to->si_band);
1456                err |= __put_user(from->si_fd, &to->si_fd);
1457                break;
1458        case __SI_FAULT:
1459                err |= __put_user(from->si_addr, &to->si_addr);
1460                break;
1461        case __SI_CHLD:
1462                err |= __put_user(from->si_pid, &to->si_pid);
1463                err |= __put_user(from->si_uid, &to->si_uid);
1464                err |= __put_user(from->si_status, &to->si_status);
1465                err |= __put_user(from->si_utime, &to->si_utime);
1466                err |= __put_user(from->si_stime, &to->si_stime);
1467                break;
1468        case __SI_RT: /* This is not generated by the kernel as of now. */
1469                err |= __put_user(from->si_pid, &to->si_pid);
1470                err |= __put_user(from->si_uid, &to->si_uid);
1471                err |= __put_user(from->si_int, &to->si_int);
1472                err |= __put_user(from->si_ptr, &to->si_ptr);
1473                break;
1474        default: /* this is just in case for now ... */
1475                err |= __put_user(from->si_pid, &to->si_pid);
1476                err |= __put_user(from->si_uid, &to->si_uid);
1477                break;
1478        }
1479        return err;
1480}
1481
1482#endif
1483
1484asmlinkage long
1485sys_rt_sigtimedwait(const sigset_t *uthese, siginfo_t *uinfo,
1486                    const struct timespec *uts, size_t sigsetsize)
1487{
1488        int ret, sig;
1489        sigset_t these;
1490        struct timespec ts;
1491        siginfo_t info;
1492        long timeout = 0;
1493
1494        /* XXX: Don't preclude handling different sized sigset_t's.  */
1495        if (sigsetsize != sizeof(sigset_t))
1496                return -EINVAL;
1497
1498        if (copy_from_user(&these, uthese, sizeof(these)))
1499                return -EFAULT;
1500                
1501        /*
1502         * Invert the set of allowed signals to get those we
1503         * want to block.
1504         */
1505        sigdelsetmask(&these, sigmask(SIGKILL)|sigmask(SIGSTOP));
1506        signotset(&these);
1507
1508        if (uts) {
1509                if (copy_from_user(&ts, uts, sizeof(ts)))
1510                        return -EFAULT;
1511                if (ts.tv_nsec >= 1000000000L || ts.tv_nsec < 0
1512                    || ts.tv_sec < 0)
1513                        return -EINVAL;
1514        }
1515
1516        spin_lock_irq(&current->sig->siglock);
1517        spin_lock(&current->sigmask_lock);
1518        sig = dequeue_signal(&current->sig->shared_pending, &these, &info);
1519        if (!sig)
1520                sig = dequeue_signal(&current->pending, &these, &info);
1521        if (!sig) {
1522                timeout = MAX_SCHEDULE_TIMEOUT;
1523                if (uts)
1524                        timeout = (timespec_to_jiffies(&ts)
1525                                   + (ts.tv_sec || ts.tv_nsec));
1526
1527                if (timeout) {
1528                        /* None ready -- temporarily unblock those we're
1529                         * interested while we are sleeping in so that we'll
1530                         * be awakened when they arrive.  */
1531                        current->real_blocked = current->blocked;
1532                        sigandsets(&current->blocked, &current->blocked, &these);
1533                        recalc_sigpending();
1534                        spin_unlock(&current->sigmask_lock);
1535                        spin_unlock_irq(&current->sig->siglock);
1536
1537                        current->state = TASK_INTERRUPTIBLE;
1538                        timeout = schedule_timeout(timeout);
1539
1540                        spin_lock_irq(&current->sig->siglock);
1541                        spin_lock(&current->sigmask_lock);
1542                        sig = dequeue_signal(&current->sig->shared_pending, &these, &info);
1543                        if (!sig)
1544                                sig = dequeue_signal(&current->pending, &these, &info);
1545                        current->blocked = current->real_blocked;
1546                        siginitset(&current->real_blocked, 0);
1547                        recalc_sigpending();
1548                }
1549        }
1550        spin_unlock(&current->sigmask_lock);
1551        spin_unlock_irq(&current->sig->siglock);
1552
1553        if (sig) {
1554                ret = sig;
1555                if (uinfo) {
1556                        if (copy_siginfo_to_user(uinfo, &info))
1557                                ret = -EFAULT;
1558                }
1559        } else {
1560                ret = -EAGAIN;
1561                if (timeout)
1562                        ret = -EINTR;
1563        }
1564
1565        return ret;
1566}
1567
1568asmlinkage long
1569sys_kill(int pid, int sig)
1570{
1571        struct siginfo info;
1572
1573        info.si_signo = sig;
1574        info.si_errno = 0;
1575        info.si_code = SI_USER;
1576        info.si_pid = current->pid;
1577        info.si_uid = current->uid;
1578
1579        return kill_something_info(sig, &info, pid);
1580}
1581
1582/*
1583 *  Send a signal to only one task, even if it's a CLONE_THREAD task.
1584 */
1585asmlinkage long
1586sys_tkill(int pid, int sig)
1587{
1588        struct siginfo info;
1589        int error;
1590        struct task_struct *p;
1591
1592        /* This is only valid for single tasks */
1593        if (pid <= 0)
1594                return -EINVAL;
1595
1596        info.si_signo = sig;
1597        info.si_errno = 0;
1598        info.si_code = SI_TKILL;
1599        info.si_pid = current->pid;
1600        info.si_uid = current->uid;
1601
1602        read_lock(&tasklist_lock);
1603        p = find_task_by_pid(pid);
1604        error = -ESRCH;
1605        if (p) {
1606                spin_lock_irq(&p->sig->siglock);
1607                error = __send_sig_info(sig, &info, p, 0);
1608                spin_unlock_irq(&p->sig->siglock);
1609        }
1610        read_unlock(&tasklist_lock);
1611        return error;
1612}
1613
1614asmlinkage long
1615sys_rt_sigqueueinfo(int pid, int sig, siginfo_t *uinfo)
1616{
1617        siginfo_t info;
1618
1619        if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
1620                return -EFAULT;
1621
1622        /* Not even root can pretend to send signals from the kernel.
1623           Nor can they impersonate a kill(), which adds source info.  */
1624        if (info.si_code >= 0)
1625                return -EPERM;
1626        info.si_signo = sig;
1627
1628        /* POSIX.1b doesn't mention process groups.  */
1629        return kill_proc_info(sig, &info, pid);
1630}
1631
1632int
1633do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact)
1634{
1635        struct k_sigaction *k;
1636
1637        if (sig < 1 || sig > _NSIG || (act && sig_kernel_only(sig)))
1638                return -EINVAL;
1639
1640        k = &current->sig->action[sig-1];
1641
1642        spin_lock_irq(&current->sig->siglock);
1643
1644        if (oact)
1645                *oact = *k;
1646
1647        if (act) {
1648                *k = *act;
1649                sigdelsetmask(&k->sa.sa_mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
1650
1651                /*
1652                 * POSIX 3.3.1.3:
1653                 *  "Setting a signal action to SIG_IGN for a signal that is
1654                 *   pending shall cause the pending signal to be discarded,
1655                 *   whether or not it is blocked."
1656                 *
1657                 *  "Setting a signal action to SIG_DFL for a signal that is
1658                 *   pending and whose default action is to ignore the signal
1659                 *   (for example, SIGCHLD), shall cause the pending signal to
1660                 *   be discarded, whether or not it is blocked"
1661                 *
1662                 * Note the silly behaviour of SIGCHLD: SIG_IGN means that the
1663                 * signal isn't actually ignored, but does automatic child
1664                 * reaping, while SIG_DFL is explicitly said by POSIX to force
1665                 * the signal to be ignored.
1666                 */
1667
1668                if (k->sa.sa_handler == SIG_IGN
1669                    || (k->sa.sa_handler == SIG_DFL
1670                        && (sig == SIGCONT ||
1671                            sig == SIGCHLD ||
1672                            sig == SIGWINCH ||
1673                            sig == SIGURG))) {
1674                        spin_lock_irq(&current->sigmask_lock);
1675                        if (rm_sig_from_queue(sig, current))
1676                                recalc_sigpending();
1677                        spin_unlock_irq(&current->sigmask_lock);
1678                }
1679        }
1680
1681        spin_unlock_irq(&current->sig->siglock);
1682        return 0;
1683}
1684
1685int 
1686do_sigaltstack (const stack_t *uss, stack_t *uoss, unsigned long sp)
1687{
1688        stack_t oss;
1689        int error;
1690
1691        if (uoss) {
1692                oss.ss_sp = (void *) current->sas_ss_sp;
1693                oss.ss_size = current->sas_ss_size;
1694                oss.ss_flags = sas_ss_flags(sp);
1695        }
1696
1697        if (uss) {
1698                void *ss_sp;
1699                size_t ss_size;
1700                int ss_flags;
1701
1702                error = -EFAULT;
1703                if (verify_area(VERIFY_READ, uss, sizeof(*uss))
1704                    || __get_user(ss_sp, &uss->ss_sp)
1705                    || __get_user(ss_flags, &uss->ss_flags)
1706                    || __get_user(ss_size, &uss->ss_size))
1707                        goto out;
1708
1709                error = -EPERM;
1710                if (on_sig_stack (sp))
1711                        goto out;
1712
1713                error = -EINVAL;
1714                /*
1715                 *
1716                 * Note - this code used to test ss_flags incorrectly
1717                 *        old code may have been written using ss_flags==0
1718                 *        to mean ss_flags==SS_ONSTACK (as this was the only
1719                 *        way that worked) - this fix preserves that older
1720                 *        mechanism
1721                 */
1722                if (ss_flags != SS_DISABLE && ss_flags != SS_ONSTACK && ss_flags != 0)
1723                        goto out;
1724
1725                if (ss_flags == SS_DISABLE) {
1726                        ss_size = 0;
1727                        ss_sp = NULL;
1728                } else {
1729                        error = -ENOMEM;
1730                        if (ss_size < MINSIGSTKSZ)
1731                                goto out;
1732                }
1733
1734                current->sas_ss_sp = (unsigned long) ss_sp;
1735                current->sas_ss_size = ss_size;
1736        }
1737
1738        if (uoss) {
1739                error = -EFAULT;
1740                if (copy_to_user(uoss, &oss, sizeof(oss)))
1741                        goto out;
1742        }
1743
1744        error = 0;
1745out:
1746        return error;
1747}
1748
1749asmlinkage long
1750sys_sigpending(old_sigset_t *set)
1751{
1752        return do_sigpending(set, sizeof(*set));
1753}
1754
1755#if !defined(__alpha__)
1756/* Alpha has its own versions with special arguments.  */
1757
1758asmlinkage long
1759sys_sigprocmask(int how, old_sigset_t *set, old_sigset_t *oset)
1760{
1761        int error;
1762        old_sigset_t old_set, new_set;
1763
1764        if (set) {
1765                error = -EFAULT;
1766                if (copy_from_user(&new_set, set, sizeof(*set)))
1767                        goto out;
1768                new_set &= ~(sigmask(SIGKILL)|sigmask(SIGSTOP));
1769
1770                spin_lock_irq(&current->sigmask_lock);
1771                old_set = current->blocked.sig[0];
1772
1773                error = 0;
1774                switch (how) {
1775                default:
1776                        error = -EINVAL;
1777                        break;
1778                case SIG_BLOCK:
1779                        sigaddsetmask(&current->blocked, new_set);
1780                        break;
1781                case SIG_UNBLOCK:
1782                        sigdelsetmask(&current->blocked, new_set);
1783                        break;
1784                case SIG_SETMASK:
1785                        current->blocked.sig[0] = new_set;
1786                        break;
1787                }
1788
1789                recalc_sigpending();
1790                spin_unlock_irq(&current->sigmask_lock);
1791                if (error)
1792                        goto out;
1793                if (oset)
1794                        goto set_old;
1795        } else if (oset) {
1796                old_set = current->blocked.sig[0];
1797        set_old:
1798                error = -EFAULT;
1799                if (copy_to_user(oset, &old_set, sizeof(*oset)))
1800                        goto out;
1801        }
1802        error = 0;
1803out:
1804        return error;
1805}
1806
1807#ifndef __sparc__
1808asmlinkage long
1809sys_rt_sigaction(int sig, const struct sigaction *act, struct sigaction *oact,
1810                 size_t sigsetsize)
1811{
1812        struct k_sigaction new_sa, old_sa;
1813        int ret = -EINVAL;
1814
1815        /* XXX: Don't preclude handling different sized sigset_t's.  */
1816        if (sigsetsize != sizeof(sigset_t))
1817                goto out;
1818
1819        if (act) {
1820                if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
1821                        return -EFAULT;
1822        }
1823
1824        ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
1825
1826        if (!ret && oact) {
1827                if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
1828                        return -EFAULT;
1829        }
1830out:
1831        return ret;
1832}
1833#endif /* __sparc__ */
1834#endif
1835
1836#if !defined(__alpha__) && !defined(__ia64__) && !defined(__arm__)
1837/*
1838 * For backwards compatibility.  Functionality superseded by sigprocmask.
1839 */
1840asmlinkage long
1841sys_sgetmask(void)
1842{
1843        /* SMP safe */
1844        return current->blocked.sig[0];
1845}
1846
1847asmlinkage long
1848sys_ssetmask(int newmask)
1849{
1850        int old;
1851
1852        spin_lock_irq(&current->sigmask_lock);
1853        old = current->blocked.sig[0];
1854
1855        siginitset(&current->blocked, newmask & ~(sigmask(SIGKILL)|
1856                                                  sigmask(SIGSTOP)));
1857        recalc_sigpending();
1858        spin_unlock_irq(&current->sigmask_lock);
1859
1860        return old;
1861}
1862#endif /* !defined(__alpha__) */
1863
1864#if !defined(__alpha__) && !defined(__ia64__) && !defined(__mips__) && \
1865    !defined(__arm__)
1866/*
1867 * For backwards compatibility.  Functionality superseded by sigaction.
1868 */
1869asmlinkage unsigned long
1870sys_signal(int sig, __sighandler_t handler)
1871{
1872        struct k_sigaction new_sa, old_sa;
1873        int ret;
1874
1875        new_sa.sa.sa_handler = handler;
1876        new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
1877
1878        ret = do_sigaction(sig, &new_sa, &old_sa);
1879
1880        return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
1881}
1882#endif /* !alpha && !__ia64__ && !defined(__mips__) && !defined(__arm__) */
1883
1884#ifndef HAVE_ARCH_SYS_PAUSE
1885
1886asmlinkage int
1887sys_pause(void)
1888{
1889        current->state = TASK_INTERRUPTIBLE;
1890        schedule();
1891        return -ERESTARTNOHAND;
1892}
1893
1894#endif /* HAVE_ARCH_SYS_PAUSE */
1895
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.