Back to index

tetex-bin  3.0
lib_tstp.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2001,2002 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  *     and: Thomas Dickey 1995-2001                                         *
00033  ****************************************************************************/
00034 
00035 /*
00036 **     lib_tstp.c
00037 **
00038 **     The routine _nc_signal_handler().
00039 **
00040 */
00041 #include <curses.priv.h>
00042 
00043 #include <SigAction.h>
00044 
00045 #if SVR4_ACTION && !defined(_POSIX_SOURCE)
00046 #define _POSIX_SOURCE
00047 #endif
00048 
00049 MODULE_ID("$Id: lib_tstp.c,v 1.30 2002/05/18 19:55:38 tom Exp $")
00050 
00051 #if defined(SIGTSTP) && (HAVE_SIGACTION || HAVE_SIGVEC)
00052 #define USE_SIGTSTP 1
00053 #else
00054 #define USE_SIGTSTP 0
00055 #endif
00056 
00057 #ifdef TRACE
00058 static const char *
00059 signal_name(int sig)
00060 {
00061     switch (sig) {
00062     case SIGALRM:
00063        return "SIGALRM";
00064 #ifdef SIGCONT
00065     case SIGCONT:
00066        return "SIGCONT";
00067 #endif
00068     case SIGINT:
00069        return "SIGINT";
00070     case SIGQUIT:
00071        return "SIGQUIT";
00072     case SIGTERM:
00073        return "SIGTERM";
00074 #ifdef SIGTSTP
00075     case SIGTSTP:
00076        return "SIGTSTP";
00077 #endif
00078 #ifdef SIGTTOU
00079     case SIGTTOU:
00080        return "SIGTTOU";
00081 #endif
00082 #ifdef SIGWINCH
00083     case SIGWINCH:
00084        return "SIGWINCH";
00085 #endif
00086     default:
00087        return "unknown signal";
00088     }
00089 }
00090 #endif
00091 
00092 /*
00093  * Note: This code is fragile!  Its problem is that different OSs
00094  * handle restart of system calls interrupted by signals differently.
00095  * The ncurses code needs signal-call restart to happen -- otherwise,
00096  * interrupted wgetch() calls will return FAIL, probably making the
00097  * application think the input stream has ended and it should
00098  * terminate.  In particular, you know you have this problem if, when
00099  * you suspend an ncurses-using lynx with ^Z and resume, it dies
00100  * immediately.
00101  *
00102  * Default behavior of POSIX sigaction(2) is not to restart
00103  * interrupted system calls, but Linux's sigaction does it anyway (at
00104  * least, on and after the 1.1.47 I (esr) use).  Thus this code works
00105  * OK under Linux.  The 4.4BSD sigaction(2) supports a (non-portable)
00106  * SA_RESTART flag that forces the right behavior.  Thus, this code
00107  * should work OK under BSD/OS, NetBSD, and FreeBSD (let us know if it
00108  * does not).
00109  *
00110  * Stock System Vs (and anything else using a strict-POSIX
00111  * sigaction(2) without SA_RESTART) may have a problem.  Possible
00112  * solutions:
00113  *
00114  *    sigvec      restarts by default (SV_INTERRUPT flag to not restart)
00115  *    signal      restarts by default in SVr4 (assuming you link with -lucb)
00116  *                and BSD, but not SVr3.
00117  *    sigset      restarts, but is only available under SVr4/Solaris.
00118  *
00119  * The signal(3) call is mandated by the ANSI standard, and its
00120  * interaction with sigaction(2) is described in the POSIX standard
00121  * (3.3.4.2, page 72,line 934).  According to section 8.1, page 191,
00122  * however, signal(3) itself is not required by POSIX.1.  And POSIX is
00123  * silent on whether it is required to restart signals.
00124  *
00125  * So.  The present situation is, we use sigaction(2) with no
00126  * guarantee of restart anywhere but on Linux and BSD.  We could
00127  * switch to signal(3) and collar Linux, BSD, and SVr4.  Any way
00128  * we slice it, System V UNIXes older than SVr4 will probably lose
00129  * (this may include XENIX).
00130  *
00131  * This implementation will probably be changed to use signal(3) in
00132  * the future.  If nothing else, it's simpler...
00133  */
00134 
00135 #if USE_SIGTSTP
00136 static void
00137 tstp(int dummy GCC_UNUSED)
00138 {
00139     sigset_t mask, omask;
00140     sigaction_t act, oact;
00141 
00142 #ifdef SIGTTOU
00143     int sigttou_blocked;
00144 #endif
00145 
00146     T(("tstp() called"));
00147 
00148     /*
00149      * The user may have changed the prog_mode tty bits, so save them.
00150      *
00151      * But first try to detect whether we still are in the foreground
00152      * process group - if not, an interactive shell may already have
00153      * taken ownership of the tty and modified the settings when our
00154      * parent was stopped before us, and we would likely pick up the
00155      * settings already modified by the shell.
00156      */
00157     if (SP != 0 && !SP->_endwin)   /* don't do this if we're not in curses */
00158 #if HAVE_TCGETPGRP
00159        if (tcgetpgrp(STDIN_FILENO) == getpgrp())
00160 #endif
00161            def_prog_mode();
00162 
00163     /*
00164      * Block window change and timer signals.  The latter
00165      * is because applications use timers to decide when
00166      * to repaint the screen.
00167      */
00168     (void) sigemptyset(&mask);
00169     (void) sigaddset(&mask, SIGALRM);
00170 #if USE_SIGWINCH
00171     (void) sigaddset(&mask, SIGWINCH);
00172 #endif
00173     (void) sigprocmask(SIG_BLOCK, &mask, &omask);
00174 
00175 #ifdef SIGTTOU
00176     sigttou_blocked = sigismember(&omask, SIGTTOU);
00177     if (!sigttou_blocked) {
00178        (void) sigemptyset(&mask);
00179        (void) sigaddset(&mask, SIGTTOU);
00180        (void) sigprocmask(SIG_BLOCK, &mask, NULL);
00181     }
00182 #endif
00183 
00184     /*
00185      * End window mode, which also resets the terminal state to the
00186      * original (pre-curses) modes.
00187      */
00188     endwin();
00189 
00190     /* Unblock SIGTSTP. */
00191     (void) sigemptyset(&mask);
00192     (void) sigaddset(&mask, SIGTSTP);
00193 #ifdef SIGTTOU
00194     if (!sigttou_blocked) {
00195        /* Unblock this too if it wasn't blocked on entry */
00196        (void) sigaddset(&mask, SIGTTOU);
00197     }
00198 #endif
00199     (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
00200 
00201     /* Now we want to resend SIGSTP to this process and suspend it */
00202     act.sa_handler = SIG_DFL;
00203     sigemptyset(&act.sa_mask);
00204     act.sa_flags = 0;
00205 #ifdef SA_RESTART
00206     act.sa_flags |= SA_RESTART;
00207 #endif /* SA_RESTART */
00208     sigaction(SIGTSTP, &act, &oact);
00209     kill(getpid(), SIGTSTP);
00210 
00211     /* Process gets suspended...time passes...process resumes */
00212 
00213     T(("SIGCONT received"));
00214     sigaction(SIGTSTP, &oact, NULL);
00215     flushinp();
00216 
00217     /*
00218      * If the user modified the tty state while suspended, he wants
00219      * those changes to stick.  So save the new "default" terminal state.
00220      */
00221     def_shell_mode();
00222 
00223     /*
00224      * This relies on the fact that doupdate() will restore the
00225      * program-mode tty state, and issue enter_ca_mode if need be.
00226      */
00227     doupdate();
00228 
00229     /* Reset the signals. */
00230     (void) sigprocmask(SIG_SETMASK, &omask, NULL);
00231 }
00232 #endif /* USE_SIGTSTP */
00233 
00234 static void
00235 cleanup(int sig)
00236 {
00237     static int nested;
00238 
00239     /*
00240      * Actually, doing any sort of I/O from within an signal handler is
00241      * "unsafe".  But we'll _try_ to clean up the screen and terminal
00242      * settings on the way out.
00243      */
00244     if (!nested++
00245        && (sig == SIGINT
00246            || sig == SIGQUIT)) {
00247 #if HAVE_SIGACTION || HAVE_SIGVEC
00248        sigaction_t act;
00249        sigemptyset(&act.sa_mask);
00250        act.sa_flags = 0;
00251        act.sa_handler = SIG_IGN;
00252        if (sigaction(sig, &act, NULL) == 0)
00253 #else
00254        if (signal(sig, SIG_IGN) != SIG_ERR)
00255 #endif
00256        {
00257            SCREEN *scan = _nc_screen_chain;
00258            while (scan) {
00259               if (SP != 0
00260                   && SP->_ofp != 0
00261                   && isatty(fileno(SP->_ofp))) {
00262                   SP->_cleanup = TRUE;
00263                   SP->_outch = _nc_outch;
00264               }
00265               set_term(scan);
00266               endwin();
00267               if (SP)
00268                   SP->_endwin = FALSE;    /* in case we have an atexit! */
00269               scan = scan->_next_screen;
00270            }
00271        }
00272     }
00273     exit(EXIT_FAILURE);
00274 }
00275 
00276 #if USE_SIGWINCH
00277 static void
00278 sigwinch(int sig GCC_UNUSED)
00279 {
00280     SCREEN *scan = _nc_screen_chain;
00281     while (scan) {
00282        scan->_sig_winch = TRUE;
00283        scan = scan->_next_screen;
00284     }
00285 }
00286 #endif /* USE_SIGWINCH */
00287 
00288 /*
00289  * If the given signal is still in its default state, set it to the given
00290  * handler.
00291  */
00292 static int
00293 CatchIfDefault(int sig, RETSIGTYPE (*handler) (int))
00294 {
00295     int result;
00296 #if HAVE_SIGACTION || HAVE_SIGVEC
00297     sigaction_t old_act;
00298     sigaction_t new_act;
00299 
00300     memset(&new_act, 0, sizeof(new_act));
00301     sigemptyset(&new_act.sa_mask);
00302 #ifdef SA_RESTART
00303 #ifdef SIGWINCH
00304     if (sig != SIGWINCH)
00305 #endif
00306        new_act.sa_flags |= SA_RESTART;
00307 #endif /* SA_RESTART */
00308     new_act.sa_handler = handler;
00309 
00310     if (sigaction(sig, NULL, &old_act) == 0
00311        && (old_act.sa_handler == SIG_DFL
00312            || old_act.sa_handler == handler
00313 #if USE_SIGWINCH
00314            || (sig == SIGWINCH && old_act.sa_handler == SIG_IGN)
00315 #endif
00316        )) {
00317        (void) sigaction(sig, &new_act, NULL);
00318        result = TRUE;
00319     } else {
00320        result = FALSE;
00321     }
00322 #else /* !HAVE_SIGACTION */
00323     RETSIGTYPE (*ohandler) (int);
00324 
00325     ohandler = signal(sig, SIG_IGN);
00326     if (ohandler == SIG_DFL
00327        || ohandler == handler
00328 #if USE_SIGWINCH
00329        || (sig == SIGWINCH && ohandler == SIG_IGN)
00330 #endif
00331        ) {
00332        signal(sig, handler);
00333        result = TRUE;
00334     } else {
00335        signal(sig, ohandler);
00336        result = FALSE;
00337     }
00338 #endif
00339     T(("CatchIfDefault - will %scatch %s",
00340        result ? "" : "not ", signal_name(sig)));
00341     return result;
00342 }
00343 
00344 /*
00345  * This is invoked once at the beginning (e.g., from 'initscr()'), to
00346  * initialize the signal catchers, and thereafter when spawning a shell (and
00347  * returning) to disable/enable the SIGTSTP (i.e., ^Z) catcher.
00348  *
00349  * If the application has already set one of the signals, we'll not modify it
00350  * (during initialization).
00351  *
00352  * The XSI document implies that we shouldn't keep the SIGTSTP handler if
00353  * the caller later changes its mind, but that doesn't seem correct.
00354  */
00355 NCURSES_EXPORT(void)
00356 _nc_signal_handler(bool enable)
00357 {
00358     static bool initialized = FALSE;
00359 
00360     T((T_CALLED("_nc_signal_handler(%d)"), enable));
00361 #if USE_SIGTSTP                    /* Xenix 2.x doesn't have SIGTSTP, for example */
00362     {
00363        static bool ignore_tstp = FALSE;
00364 
00365        if (!ignore_tstp) {
00366            static sigaction_t act, oact;
00367 
00368            if (!enable) {
00369               act.sa_handler = SIG_IGN;
00370               sigaction(SIGTSTP, &act, &oact);
00371            } else if (act.sa_handler != SIG_DFL) {
00372               sigaction(SIGTSTP, &oact, NULL);
00373            } else if (sigaction(SIGTSTP, NULL, &oact) == 0
00374                      && (oact.sa_handler == SIG_DFL)) {
00375               sigemptyset(&act.sa_mask);
00376 #ifdef SA_RESTART
00377               act.sa_flags |= SA_RESTART;
00378 #endif /* SA_RESTART */
00379               act.sa_handler = tstp;
00380               (void) sigaction(SIGTSTP, &act, NULL);
00381            } else {
00382               ignore_tstp = TRUE;
00383            }
00384        }
00385     }
00386 #endif /* !USE_SIGTSTP */
00387 
00388     if (!initialized) {
00389        if (enable) {
00390            CatchIfDefault(SIGINT, cleanup);
00391            CatchIfDefault(SIGTERM, cleanup);
00392 #if USE_SIGWINCH
00393            CatchIfDefault(SIGWINCH, sigwinch);
00394 #endif
00395            initialized = TRUE;
00396        }
00397     }
00398     returnVoid;
00399 }