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