"Fossies" - the Fresh Open Source Software Archive

Member "tnftp-20200705/libedit/sig.c" (4 Jul 2020, 5367 Bytes) of package /linux/privat/tnftp-20200705.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sig.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 20151004_vs_20200705.

    1 /*  $NetBSD: sig.c,v 1.9 2020/07/04 13:43:21 lukem Exp $    */
    2 /*  from    NetBSD: sig.c,v 1.26 2016/05/09 21:46:56 christos Exp   */
    3 
    4 /*-
    5  * Copyright (c) 1992, 1993
    6  *  The Regents of the University of California.  All rights reserved.
    7  *
    8  * This code is derived from software contributed to Berkeley by
    9  * Christos Zoulas of Cornell University.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  */
   35 
   36 #include "config.h"
   37 
   38 #if 0 /* tnftp */
   39 #if !defined(lint) && !defined(SCCSID)
   40 #if 0
   41 static char sccsid[] = "@(#)sig.c   8.1 (Berkeley) 6/4/93";
   42 #else
   43 __RCSID(" NetBSD: sig.c,v 1.26 2016/05/09 21:46:56 christos Exp  ");
   44 #endif
   45 #endif /* not lint && not SCCSID */
   46 #endif /* tnftp */
   47 
   48 /*
   49  * sig.c: Signal handling stuff.
   50  *    our policy is to trap all signals, set a good state
   51  *    and pass the ball to our caller.
   52  */
   53 #if 0 /* tnftp */
   54 #include <errno.h>
   55 #include <stdlib.h>
   56 #endif /* tnftp */
   57 
   58 #include "el.h"
   59 #include "common.h"
   60 
   61 static EditLine *sel = NULL;
   62 
   63 static const int sighdl[] = {
   64 #define _DO(a)  (a),
   65     ALLSIGS
   66 #undef  _DO
   67     - 1
   68 };
   69 
   70 static void sig_handler(int);
   71 
   72 /* sig_handler():
   73  *  This is the handler called for all signals
   74  *  XXX: we cannot pass any data so we just store the old editline
   75  *  state in a private variable
   76  */
   77 static void
   78 sig_handler(int signo)
   79 {
   80     int i, save_errno;
   81     sigset_t nset, oset;
   82 
   83     save_errno = errno;
   84     (void) sigemptyset(&nset);
   85     (void) sigaddset(&nset, signo);
   86     (void) sigprocmask(SIG_BLOCK, &nset, &oset);
   87 
   88     sel->el_signal->sig_no = signo;
   89 
   90     switch (signo) {
   91     case SIGCONT:
   92         tty_rawmode(sel);
   93         if (ed_redisplay(sel, 0) == CC_REFRESH)
   94             re_refresh(sel);
   95         terminal__flush(sel);
   96         break;
   97 
   98     case SIGWINCH:
   99         el_resize(sel);
  100         break;
  101 
  102     default:
  103         tty_cookedmode(sel);
  104         break;
  105     }
  106 
  107     for (i = 0; sighdl[i] != -1; i++)
  108         if (signo == sighdl[i])
  109             break;
  110 
  111     (void) sigaction(signo, &sel->el_signal->sig_action[i], NULL);
  112     sel->el_signal->sig_action[i].sa_handler = SIG_ERR;
  113     sel->el_signal->sig_action[i].sa_flags = 0;
  114     sigemptyset(&sel->el_signal->sig_action[i].sa_mask);
  115     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  116     (void) kill(0, signo);
  117     errno = save_errno;
  118 }
  119 
  120 
  121 /* sig_init():
  122  *  Initialize all signal stuff
  123  */
  124 libedit_private int
  125 sig_init(EditLine *el)
  126 {
  127     size_t i;
  128     sigset_t *nset, oset;
  129 
  130     el->el_signal = el_malloc(sizeof(*el->el_signal));
  131     if (el->el_signal == NULL)
  132         return -1;
  133 
  134     nset = &el->el_signal->sig_set;
  135     (void) sigemptyset(nset);
  136 #define _DO(a) (void) sigaddset(nset, a);
  137     ALLSIGS
  138 #undef  _DO
  139     (void) sigprocmask(SIG_BLOCK, nset, &oset);
  140 
  141     for (i = 0; sighdl[i] != -1; i++) {
  142         el->el_signal->sig_action[i].sa_handler = SIG_ERR;
  143         el->el_signal->sig_action[i].sa_flags = 0;
  144         sigemptyset(&el->el_signal->sig_action[i].sa_mask);
  145     }
  146 
  147     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  148 
  149     return 0;
  150 }
  151 
  152 
  153 /* sig_end():
  154  *  Clear all signal stuff
  155  */
  156 libedit_private void
  157 sig_end(EditLine *el)
  158 {
  159 
  160     el_free(el->el_signal);
  161     el->el_signal = NULL;
  162 }
  163 
  164 
  165 /* sig_set():
  166  *  set all the signal handlers
  167  */
  168 libedit_private void
  169 sig_set(EditLine *el)
  170 {
  171     size_t i;
  172     sigset_t oset;
  173     struct sigaction osa, nsa;
  174 
  175     nsa.sa_handler = sig_handler;
  176     nsa.sa_flags = 0;
  177     sigemptyset(&nsa.sa_mask);
  178 
  179     (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
  180 
  181     for (i = 0; sighdl[i] != -1; i++) {
  182         /* This could happen if we get interrupted */
  183         if (sigaction(sighdl[i], &nsa, &osa) != -1 &&
  184             osa.sa_handler != sig_handler)
  185             el->el_signal->sig_action[i] = osa;
  186     }
  187     sel = el;
  188     (void) sigprocmask(SIG_SETMASK, &oset, NULL);
  189 }
  190 
  191 
  192 /* sig_clr():
  193  *  clear all the signal handlers
  194  */
  195 libedit_private void
  196 sig_clr(EditLine *el)
  197 {
  198     size_t i;
  199     sigset_t oset;
  200 
  201     (void) sigprocmask(SIG_BLOCK, &el->el_signal->sig_set, &oset);
  202 
  203     for (i = 0; sighdl[i] != -1; i++)
  204         if (el->el_signal->sig_action[i].sa_handler != SIG_ERR)
  205             (void)sigaction(sighdl[i],
  206                 &el->el_signal->sig_action[i], NULL);
  207 
  208     sel = NULL;     /* we are going to die if the handler is
  209                  * called */
  210     (void)sigprocmask(SIG_SETMASK, &oset, NULL);
  211 }