The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/sys_sig.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: sys_sig.c,v 1.17.4.4 2011/02/16 21:22:45 bouyer Exp $  */
    2 
    3 /*-
    4  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Andrew Doran.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*
   33  * Copyright (c) 1982, 1986, 1989, 1991, 1993
   34  *      The Regents of the University of California.  All rights reserved.
   35  * (c) UNIX System Laboratories, Inc.
   36  * All or some portions of this file are derived from material licensed
   37  * to the University of California by American Telephone and Telegraph
   38  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   39  * the permission of UNIX System Laboratories, Inc.
   40  *
   41  * Redistribution and use in source and binary forms, with or without
   42  * modification, are permitted provided that the following conditions
   43  * are met:
   44  * 1. Redistributions of source code must retain the above copyright
   45  *    notice, this list of conditions and the following disclaimer.
   46  * 2. Redistributions in binary form must reproduce the above copyright
   47  *    notice, this list of conditions and the following disclaimer in the
   48  *    documentation and/or other materials provided with the distribution.
   49  * 3. Neither the name of the University nor the names of its contributors
   50  *    may be used to endorse or promote products derived from this software
   51  *    without specific prior written permission.
   52  *
   53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   63  * SUCH DAMAGE.
   64  *
   65  *      @(#)kern_sig.c  8.14 (Berkeley) 5/14/95
   66  */
   67 
   68 #include <sys/cdefs.h>
   69 __KERNEL_RCSID(0, "$NetBSD: sys_sig.c,v 1.17.4.4 2011/02/16 21:22:45 bouyer Exp $");
   70 
   71 #include "opt_ptrace.h"
   72 #include "opt_compat_netbsd.h"
   73 #include "opt_compat_netbsd32.h"
   74 
   75 #include <sys/param.h>
   76 #include <sys/kernel.h>
   77 #include <sys/signalvar.h>
   78 #include <sys/proc.h>
   79 #include <sys/pool.h>
   80 #include <sys/sa.h>
   81 #include <sys/savar.h>
   82 #include <sys/syscallargs.h>
   83 #include <sys/kauth.h>
   84 #include <sys/wait.h>
   85 #include <sys/kmem.h>
   86 
   87 #ifdef COMPAT_16
   88 /* ARGSUSED */
   89 int
   90 compat_16_sys___sigaction14(struct lwp *l, const struct compat_16_sys___sigaction14_args *uap, register_t *retval)
   91 {
   92         /* {
   93                 syscallarg(int)                         signum;
   94                 syscallarg(const struct sigaction *)    nsa;
   95                 syscallarg(struct sigaction *)          osa;
   96         } */
   97         struct sigaction        nsa, osa;
   98         int                     error;
   99 
  100         if (SCARG(uap, nsa)) {
  101                 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
  102                 if (error)
  103                         return (error);
  104         }
  105         error = sigaction1(l, SCARG(uap, signum),
  106             SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
  107             NULL, 0);
  108         if (error)
  109                 return (error);
  110         if (SCARG(uap, osa)) {
  111                 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
  112                 if (error)
  113                         return (error);
  114         }
  115         return (0);
  116 }
  117 #endif
  118 
  119 /* ARGSUSED */
  120 int
  121 sys___sigaction_sigtramp(struct lwp *l, const struct sys___sigaction_sigtramp_args *uap, register_t *retval)
  122 {
  123         /* {
  124                 syscallarg(int)                         signum;
  125                 syscallarg(const struct sigaction *)    nsa;
  126                 syscallarg(struct sigaction *)          osa;
  127                 syscallarg(void *)                      tramp;
  128                 syscallarg(int)                         vers;
  129         } */
  130         struct sigaction nsa, osa;
  131         int error;
  132 
  133         if (SCARG(uap, nsa)) {
  134                 error = copyin(SCARG(uap, nsa), &nsa, sizeof(nsa));
  135                 if (error)
  136                         return (error);
  137         }
  138         error = sigaction1(l, SCARG(uap, signum),
  139             SCARG(uap, nsa) ? &nsa : 0, SCARG(uap, osa) ? &osa : 0,
  140             SCARG(uap, tramp), SCARG(uap, vers));
  141         if (error)
  142                 return (error);
  143         if (SCARG(uap, osa)) {
  144                 error = copyout(&osa, SCARG(uap, osa), sizeof(osa));
  145                 if (error)
  146                         return (error);
  147         }
  148         return (0);
  149 }
  150 
  151 /*
  152  * Manipulate signal mask.  Note that we receive new mask, not pointer, and
  153  * return old mask as return value; the library stub does the rest.
  154  */
  155 int
  156 sys___sigprocmask14(struct lwp *l, const struct sys___sigprocmask14_args *uap, register_t *retval)
  157 {
  158         /* {
  159                 syscallarg(int)                 how;
  160                 syscallarg(const sigset_t *)    set;
  161                 syscallarg(sigset_t *)          oset;
  162         } */
  163         struct proc     *p = l->l_proc;
  164         sigset_t        nss, oss;
  165         int             error;
  166 
  167         if (SCARG(uap, set)) {
  168                 error = copyin(SCARG(uap, set), &nss, sizeof(nss));
  169                 if (error)
  170                         return (error);
  171         }
  172         mutex_enter(p->p_lock);
  173         error = sigprocmask1(l, SCARG(uap, how),
  174             SCARG(uap, set) ? &nss : 0, SCARG(uap, oset) ? &oss : 0);
  175         mutex_exit(p->p_lock);
  176         if (error)
  177                 return (error);
  178         if (SCARG(uap, oset)) {
  179                 error = copyout(&oss, SCARG(uap, oset), sizeof(oss));
  180                 if (error)
  181                         return (error);
  182         }
  183         return (0);
  184 }
  185 
  186 /* ARGSUSED */
  187 int
  188 sys___sigpending14(struct lwp *l, const struct sys___sigpending14_args *uap, register_t *retval)
  189 {
  190         /* {
  191                 syscallarg(sigset_t *)  set;
  192         } */
  193         sigset_t ss;
  194 
  195         sigpending1(l, &ss);
  196         return (copyout(&ss, SCARG(uap, set), sizeof(ss)));
  197 }
  198 
  199 /*
  200  * Suspend process until signal, providing mask to be set in the meantime. 
  201  * Note nonstandard calling convention: libc stub passes mask, not pointer,
  202  * to save a copyin.
  203  */
  204 /* ARGSUSED */
  205 int
  206 sys___sigsuspend14(struct lwp *l, const struct sys___sigsuspend14_args *uap, register_t *retval)
  207 {
  208         /* {
  209                 syscallarg(const sigset_t *)    set;
  210         } */
  211         sigset_t        ss;
  212         int             error;
  213 
  214         if (SCARG(uap, set)) {
  215                 error = copyin(SCARG(uap, set), &ss, sizeof(ss));
  216                 if (error)
  217                         return (error);
  218         }
  219 
  220         return (sigsuspend1(l, SCARG(uap, set) ? &ss : 0));
  221 }
  222 
  223 /* ARGSUSED */
  224 int
  225 sys___sigaltstack14(struct lwp *l, const struct sys___sigaltstack14_args *uap, register_t *retval)
  226 {
  227         /* {
  228                 syscallarg(const struct sigaltstack *)  nss;
  229                 syscallarg(struct sigaltstack *)        oss;
  230         } */
  231         struct sigaltstack      nss, oss;
  232         int                     error;
  233 
  234         if (SCARG(uap, nss)) {
  235                 error = copyin(SCARG(uap, nss), &nss, sizeof(nss));
  236                 if (error)
  237                         return (error);
  238         }
  239         error = sigaltstack1(l,
  240             SCARG(uap, nss) ? &nss : 0, SCARG(uap, oss) ? &oss : 0);
  241         if (error)
  242                 return (error);
  243         if (SCARG(uap, oss)) {
  244                 error = copyout(&oss, SCARG(uap, oss), sizeof(oss));
  245                 if (error)
  246                         return (error);
  247         }
  248         return (0);
  249 }
  250 
  251 /* ARGSUSED */
  252 int
  253 sys_kill(struct lwp *l, const struct sys_kill_args *uap, register_t *retval)
  254 {
  255         /* {
  256                 syscallarg(int) pid;
  257                 syscallarg(int) signum;
  258         } */
  259         struct proc     *p;
  260         ksiginfo_t      ksi;
  261         int signum = SCARG(uap, signum);
  262         int error;
  263 
  264         if ((u_int)signum >= NSIG)
  265                 return (EINVAL);
  266         KSI_INIT(&ksi);
  267         ksi.ksi_signo = signum;
  268         ksi.ksi_code = SI_USER;
  269         ksi.ksi_pid = l->l_proc->p_pid;
  270         ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
  271         if (SCARG(uap, pid) > 0) {
  272                 /* kill single process */
  273                 mutex_enter(proc_lock);
  274                 if ((p = p_find(SCARG(uap, pid), PFIND_LOCKED)) == NULL) {
  275                         mutex_exit(proc_lock);
  276                         return (ESRCH);
  277                 }
  278                 mutex_enter(p->p_lock);
  279                 error = kauth_authorize_process(l->l_cred,
  280                     KAUTH_PROCESS_SIGNAL, p, KAUTH_ARG(signum),
  281                     NULL, NULL);
  282                 if (!error && signum) {
  283                         kpsignal2(p, &ksi);
  284                 }
  285                 mutex_exit(p->p_lock);
  286                 mutex_exit(proc_lock);
  287                 return (error);
  288         }
  289         switch (SCARG(uap, pid)) {
  290         case -1:                /* broadcast signal */
  291                 return (killpg1(l, &ksi, 0, 1));
  292         case 0:                 /* signal own process group */
  293                 return (killpg1(l, &ksi, 0, 0));
  294         default:                /* negative explicit process group */
  295                 return (killpg1(l, &ksi, -SCARG(uap, pid), 0));
  296         }
  297         /* NOTREACHED */
  298 }
  299 
  300 /* ARGSUSED */
  301 int
  302 sys_getcontext(struct lwp *l, const struct sys_getcontext_args *uap, register_t *retval)
  303 {
  304         /* {
  305                 syscallarg(struct __ucontext *) ucp;
  306         } */
  307         struct proc *p = l->l_proc;
  308         ucontext_t uc;
  309 
  310         memset(&uc, 0, sizeof(uc));
  311 
  312         mutex_enter(p->p_lock);
  313         getucontext(l, &uc);
  314         mutex_exit(p->p_lock);
  315 
  316         return (copyout(&uc, SCARG(uap, ucp), sizeof (*SCARG(uap, ucp))));
  317 }
  318 
  319 /* ARGSUSED */
  320 int
  321 sys_setcontext(struct lwp *l, const struct sys_setcontext_args *uap, register_t *retval)
  322 {
  323         /* {
  324                 syscallarg(const ucontext_t *) ucp;
  325         } */
  326         struct proc *p = l->l_proc;
  327         ucontext_t uc;
  328         int error;
  329 
  330         error = copyin(SCARG(uap, ucp), &uc, sizeof (uc));
  331         if (error)
  332                 return (error);
  333         if (!(uc.uc_flags & _UC_CPU))
  334                 return (EINVAL);
  335         mutex_enter(p->p_lock);
  336         error = setucontext(l, &uc);
  337         mutex_exit(p->p_lock);
  338         if (error)
  339                 return (error);
  340 
  341         return (EJUSTRETURN);
  342 }
  343 
  344 /*
  345  * sigtimedwait(2) system call, used also for implementation
  346  * of sigwaitinfo() and sigwait().
  347  *
  348  * This only handles single LWP in signal wait. libpthread provides
  349  * it's own sigtimedwait() wrapper to DTRT WRT individual threads.
  350  */
  351 int
  352 sys___sigtimedwait(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval)
  353 {
  354 
  355         return __sigtimedwait1(l, uap, retval, copyout, copyin, copyout);
  356 }
  357 
  358 int
  359 sigaction1(struct lwp *l, int signum, const struct sigaction *nsa,
  360         struct sigaction *osa, const void *tramp, int vers)
  361 {
  362         struct proc *p;
  363         struct sigacts *ps;
  364         sigset_t tset;
  365         int prop, error;
  366         ksiginfoq_t kq;
  367 
  368         if (signum <= 0 || signum >= NSIG)
  369                 return (EINVAL);
  370 
  371         p = l->l_proc;
  372         error = 0;
  373         ksiginfo_queue_init(&kq);
  374 
  375         /*
  376          * Trampoline ABI version 0 is reserved for the legacy kernel
  377          * provided on-stack trampoline.  Conversely, if we are using a
  378          * non-0 ABI version, we must have a trampoline.  Only validate the
  379          * vers if a new sigaction was supplied. Emulations use legacy
  380          * kernel trampolines with version 0, alternatively check for that
  381          * too.
  382          */
  383         if ((vers != 0 && tramp == NULL) ||
  384 #ifdef SIGTRAMP_VALID
  385             (nsa != NULL &&
  386             ((vers == 0) ?
  387                 (p->p_emul->e_sigcode == NULL) :
  388                 !SIGTRAMP_VALID(vers))) ||
  389 #endif
  390             (vers == 0 && tramp != NULL)) {
  391                 return (EINVAL);
  392         }
  393 
  394         mutex_enter(p->p_lock);
  395 
  396         ps = p->p_sigacts;
  397         if (osa)
  398                 *osa = SIGACTION_PS(ps, signum);
  399         if (!nsa)
  400                 goto out;
  401 
  402         prop = sigprop[signum];
  403         if ((nsa->sa_flags & ~SA_ALLBITS) || (prop & SA_CANTMASK)) {
  404                 error = EINVAL;
  405                 goto out;
  406         }
  407 
  408         SIGACTION_PS(ps, signum) = *nsa;
  409         ps->sa_sigdesc[signum].sd_tramp = tramp;
  410         ps->sa_sigdesc[signum].sd_vers = vers;
  411         sigminusset(&sigcantmask, &SIGACTION_PS(ps, signum).sa_mask);
  412 
  413         if ((prop & SA_NORESET) != 0)
  414                 SIGACTION_PS(ps, signum).sa_flags &= ~SA_RESETHAND;
  415 
  416         if (signum == SIGCHLD) {
  417                 if (nsa->sa_flags & SA_NOCLDSTOP)
  418                         p->p_sflag |= PS_NOCLDSTOP;
  419                 else
  420                         p->p_sflag &= ~PS_NOCLDSTOP;
  421                 if (nsa->sa_flags & SA_NOCLDWAIT) {
  422                         /*
  423                          * Paranoia: since SA_NOCLDWAIT is implemented by
  424                          * reparenting the dying child to PID 1 (and trust
  425                          * it to reap the zombie), PID 1 itself is forbidden
  426                          * to set SA_NOCLDWAIT.
  427                          */
  428                         if (p->p_pid == 1)
  429                                 p->p_flag &= ~PK_NOCLDWAIT;
  430                         else
  431                                 p->p_flag |= PK_NOCLDWAIT;
  432                 } else
  433                         p->p_flag &= ~PK_NOCLDWAIT;
  434 
  435                 if (nsa->sa_handler == SIG_IGN) {
  436                         /*
  437                          * Paranoia: same as above.
  438                          */
  439                         if (p->p_pid == 1)
  440                                 p->p_flag &= ~PK_CLDSIGIGN;
  441                         else
  442                                 p->p_flag |= PK_CLDSIGIGN;
  443                 } else
  444                         p->p_flag &= ~PK_CLDSIGIGN;
  445         }
  446 
  447         if ((nsa->sa_flags & SA_NODEFER) == 0)
  448                 sigaddset(&SIGACTION_PS(ps, signum).sa_mask, signum);
  449         else
  450                 sigdelset(&SIGACTION_PS(ps, signum).sa_mask, signum);
  451 
  452         /*
  453          * Set bit in p_sigctx.ps_sigignore for signals that are set to
  454          * SIG_IGN, and for signals set to SIG_DFL where the default is to
  455          * ignore. However, don't put SIGCONT in p_sigctx.ps_sigignore, as
  456          * we have to restart the process.
  457          */
  458         if (nsa->sa_handler == SIG_IGN ||
  459             (nsa->sa_handler == SIG_DFL && (prop & SA_IGNORE) != 0)) {
  460                 /* Never to be seen again. */
  461                 sigemptyset(&tset);
  462                 sigaddset(&tset, signum);
  463                 sigclearall(p, &tset, &kq);
  464                 if (signum != SIGCONT) {
  465                         /* Easier in psignal */
  466                         sigaddset(&p->p_sigctx.ps_sigignore, signum);
  467                 }
  468                 sigdelset(&p->p_sigctx.ps_sigcatch, signum);
  469         } else {
  470                 sigdelset(&p->p_sigctx.ps_sigignore, signum);
  471                 if (nsa->sa_handler == SIG_DFL)
  472                         sigdelset(&p->p_sigctx.ps_sigcatch, signum);
  473                 else
  474                         sigaddset(&p->p_sigctx.ps_sigcatch, signum);
  475         }
  476 
  477         /*
  478          * Previously held signals may now have become visible.  Ensure that
  479          * we check for them before returning to userspace.
  480          */
  481         if (sigispending(l, 0)) {
  482                 lwp_lock(l);
  483                 l->l_flag |= LW_PENDSIG;
  484                 lwp_unlock(l);
  485         }
  486  out:
  487         mutex_exit(p->p_lock);
  488         ksiginfo_queue_drain(&kq);
  489 
  490         return (error);
  491 }
  492 
  493 int
  494 sigprocmask1(struct lwp *l, int how, const sigset_t *nss, sigset_t *oss)
  495 {
  496         int more;
  497         struct proc *p = l->l_proc;
  498         sigset_t *mask;
  499         mask = (p->p_sa != NULL) ? &p->p_sa->sa_sigmask : &l->l_sigmask;
  500 
  501         KASSERT(mutex_owned(p->p_lock));
  502 
  503         if (oss)
  504                 *oss = *mask;
  505         if (nss) {
  506                 switch (how) {
  507                 case SIG_BLOCK:
  508                         sigplusset(nss, mask);
  509                         more = 0;
  510                         break;
  511                 case SIG_UNBLOCK:
  512                         sigminusset(nss, mask);
  513                         more = 1;
  514                         break;
  515                 case SIG_SETMASK:
  516                         *mask = *nss;
  517                         more = 1;
  518                         break;
  519                 default:
  520                         return (EINVAL);
  521                 }
  522                 sigminusset(&sigcantmask, mask);
  523                 if (more && sigispending(l, 0)) {
  524                         /*
  525                          * Check for pending signals on return to user.
  526                          */
  527                         lwp_lock(l);
  528                         l->l_flag |= LW_PENDSIG;
  529                         lwp_unlock(l);
  530                 }
  531         }
  532 
  533         return (0);
  534 }
  535 
  536 void
  537 sigpending1(struct lwp *l, sigset_t *ss)
  538 {
  539         struct proc *p = l->l_proc;
  540 
  541         mutex_enter(p->p_lock);
  542         *ss = l->l_sigpend.sp_set;
  543         sigplusset(&p->p_sigpend.sp_set, ss);
  544         mutex_exit(p->p_lock);
  545 }
  546 
  547 int
  548 sigsuspend1(struct lwp *l, const sigset_t *ss)
  549 {
  550         struct proc *p;
  551 
  552         p = l->l_proc;
  553 
  554         if (ss) {
  555                 /*
  556                  * When returning from sigsuspend, we want
  557                  * the old mask to be restored after the
  558                  * signal handler has finished.  Thus, we
  559                  * save it here and mark the sigctx structure
  560                  * to indicate this.
  561                  */
  562                 mutex_enter(p->p_lock);
  563                 l->l_sigrestore = 1;
  564                 l->l_sigoldmask = l->l_sigmask;
  565                 l->l_sigmask = *ss;
  566                 sigminusset(&sigcantmask, &l->l_sigmask);
  567 
  568                 /* Check for pending signals when sleeping. */
  569                 if (sigispending(l, 0)) {
  570                         lwp_lock(l);
  571                         l->l_flag |= LW_PENDSIG;
  572                         lwp_unlock(l);
  573                 }
  574                 mutex_exit(p->p_lock);
  575         }
  576 
  577         while (kpause("pause", true, 0, NULL) == 0)
  578                 ;
  579 
  580         /* always return EINTR rather than ERESTART... */
  581         return (EINTR);
  582 }
  583 
  584 int
  585 sigaltstack1(struct lwp *l, const struct sigaltstack *nss,
  586              struct sigaltstack *oss)
  587 {
  588         struct proc *p = l->l_proc;
  589         int error = 0;
  590 
  591         mutex_enter(p->p_lock);
  592 
  593         if (oss)
  594                 *oss = l->l_sigstk;
  595 
  596         if (nss) {
  597                 if (nss->ss_flags & ~SS_ALLBITS)
  598                         error = EINVAL;
  599                 else if (nss->ss_flags & SS_DISABLE) {
  600                         if (l->l_sigstk.ss_flags & SS_ONSTACK)
  601                                 error = EINVAL;
  602                 } else if (nss->ss_size < MINSIGSTKSZ)
  603                         error = ENOMEM;
  604 
  605                 if (!error)
  606                         l->l_sigstk = *nss;
  607         }
  608 
  609         mutex_exit(p->p_lock);
  610 
  611         return (error);
  612 }
  613 
  614 int
  615 __sigtimedwait1(struct lwp *l, const struct sys___sigtimedwait_args *uap, register_t *retval,
  616     copyout_t put_info, copyin_t fetch_timeout, copyout_t put_timeout)
  617 {
  618         /* {
  619                 syscallarg(const sigset_t *) set;
  620                 syscallarg(siginfo_t *) info;
  621                 syscallarg(struct timespec *) timeout;
  622         } */
  623         struct proc *p = l->l_proc;
  624         int error, signum;
  625         int timo = 0;
  626         struct timespec ts, tsstart, tsnow;
  627         ksiginfo_t ksi;
  628 
  629         memset(&tsstart, 0, sizeof tsstart);     /* XXX gcc */
  630 
  631         /*
  632          * Calculate timeout, if it was specified.
  633          */
  634         if (SCARG(uap, timeout)) {
  635                 error = (*fetch_timeout)(SCARG(uap, timeout), &ts, sizeof(ts));
  636                 if (error)
  637                         return error;
  638 
  639                 if ((error = itimespecfix(&ts)) != 0)
  640                         return error;
  641 
  642                 timo = tstohz(&ts);
  643                 if (timo == 0 && ts.tv_sec == 0 && ts.tv_nsec != 0)
  644                         timo++;
  645 
  646                 /*
  647                  * Remember current uptime, it would be used in
  648                  * ECANCELED/ERESTART case.
  649                  */
  650                 getnanouptime(&tsstart);
  651         }
  652 
  653         error = copyin(SCARG(uap, set), &l->l_sigwaitset,
  654             sizeof(l->l_sigwaitset));
  655         if (error != 0)
  656                 return (error);
  657 
  658         /*
  659          * Silently ignore SA_CANTMASK signals. psignal1() would ignore
  660          * SA_CANTMASK signals in waitset, we do this only for the below
  661          * siglist check.
  662          */
  663         sigminusset(&sigcantmask, &l->l_sigwaitset);
  664 
  665         mutex_enter(p->p_lock);
  666 
  667         /*
  668          * SA processes can have no more than 1 sigwaiter.
  669          */
  670         if ((p->p_sflag & PS_SA) != 0 && !LIST_EMPTY(&p->p_sigwaiters)) {
  671                 mutex_exit(p->p_lock);
  672                 error = EINVAL;
  673                 goto out;
  674         }
  675 
  676         if ((signum = sigget(&p->p_sigpend, &ksi, 0, &l->l_sigwaitset)) == 0)
  677                 signum = sigget(&l->l_sigpend, &ksi, 0, &l->l_sigwaitset);
  678 
  679         if (signum != 0) {
  680                 /*
  681                  * We found a pending signal - copy it out to the user.
  682                  */
  683                 mutex_exit(p->p_lock);
  684                 goto out;
  685         }
  686 
  687         /*
  688          * Set up the sigwait list.
  689          */
  690         l->l_sigwaited = &ksi;
  691         LIST_INSERT_HEAD(&p->p_sigwaiters, l, l_sigwaiter);
  692 
  693         /*
  694          * Wait for signal to arrive. We can either be woken up or time out.
  695          */
  696         error = cv_timedwait_sig(&l->l_sigcv, p->p_lock, timo);
  697 
  698         /*
  699          * Need to find out if we woke as a result of lwp_wakeup() or a
  700          * signal outside our wait set.
  701          */
  702         if (l->l_sigwaited != NULL) {
  703                 if (error == EINTR) {
  704                         /* wakeup via _lwp_wakeup() */
  705                         error = ECANCELED;
  706                 } else if (!error) {
  707                         /* spurious wakeup - arrange for syscall restart */
  708                         error = ERESTART;
  709                 }
  710                 l->l_sigwaited = NULL;
  711                 LIST_REMOVE(l, l_sigwaiter);
  712         }
  713 
  714         mutex_exit(p->p_lock);
  715 
  716         /*
  717          * If the sleep was interrupted (either by signal or wakeup), update
  718          * the timeout and copyout new value back.  It would be used when
  719          * the syscall would be restarted or called again.
  720          */
  721         if (timo && (error == ERESTART || error == ECANCELED)) {
  722                 getnanouptime(&tsnow);
  723 
  724                 /* compute how much time has passed since start */
  725                 timespecsub(&tsnow, &tsstart, &tsnow);
  726                 /* substract passed time from timeout */
  727                 timespecsub(&ts, &tsnow, &ts);
  728 
  729                 if (ts.tv_sec < 0)
  730                         error = EAGAIN;
  731                 else {
  732                         /* copy updated timeout to userland */
  733                         error = (*put_timeout)(&ts, SCARG(uap, timeout),
  734                             sizeof(ts));
  735                 }
  736         }
  737 
  738         /*
  739          * If a signal from the wait set arrived, copy it to userland.
  740          * Copy only the used part of siginfo, the padding part is
  741          * left unchanged (userland is not supposed to touch it anyway).
  742          */
  743  out:
  744         if (error == 0)
  745                 error = (*put_info)(&ksi.ksi_info, SCARG(uap, info),
  746                     sizeof(ksi.ksi_info));
  747 
  748         return error;
  749 }

Cache object: 86bbe1094a064f14d23eb7487e5512fc


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.