]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tty/lib_tstp.c
ncurses 5.0
[ncurses.git] / ncurses / tty / lib_tstp.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999 Free Software Foundation, Inc.                   *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34
35 /*
36 **      lib_tstp.c
37 **
38 **      The routine _nc_signal_handler().
39 **
40 */
41
42 #include <curses.priv.h>
43
44 #include <signal.h>
45 #include <SigAction.h>
46
47 #if defined(SVR4_ACTION) && !defined(_POSIX_SOURCE)
48 #define _POSIX_SOURCE
49 #endif
50
51 MODULE_ID("$Id: lib_tstp.c,v 1.20 1999/10/22 23:11:09 tom Exp $")
52
53 #if defined(SIGTSTP) && (HAVE_SIGACTION || HAVE_SIGVEC)
54 #define USE_SIGTSTP 1
55 #else
56 #define USE_SIGTSTP 0
57 #endif
58
59 /*
60  * Note: This code is fragile!  Its problem is that different OSs
61  * handle restart of system calls interrupted by signals differently.
62  * The ncurses code needs signal-call restart to happen -- otherwise,
63  * interrupted wgetch() calls will return FAIL, probably making the
64  * application think the input stream has ended and it should
65  * terminate.  In particular, you know you have this problem if, when
66  * you suspend an ncurses-using lynx with ^Z and resume, it dies
67  * immediately.
68  *
69  * Default behavior of POSIX sigaction(2) is not to restart
70  * interrupted system calls, but Linux's sigaction does it anyway (at
71  * least, on and after the 1.1.47 I (esr) use).  Thus this code works
72  * OK under Linux.  The 4.4BSD sigaction(2) supports a (non-portable)
73  * SA_RESTART flag that forces the right behavior.  Thus, this code
74  * should work OK under BSD/OS, NetBSD, and FreeBSD (let us know if it
75  * does not).
76  *
77  * Stock System Vs (and anything else using a strict-POSIX
78  * sigaction(2) without SA_RESTART) may have a problem.  Possible
79  * solutions:
80  *
81  *    sigvec      restarts by default (SV_INTERRUPT flag to not restart)
82  *    signal      restarts by default in SVr4 (assuming you link with -lucb)
83  *                and BSD, but not SVr3.
84  *    sigset      restarts, but is only available under SVr4/Solaris.
85  *
86  * The signal(3) call is mandated by the ANSI standard, and its
87  * interaction with sigaction(2) is described in the POSIX standard
88  * (3.3.4.2, page 72,line 934).  According to section 8.1, page 191,
89  * however, signal(3) itself is not required by POSIX.1.  And POSIX is
90  * silent on whether it is required to restart signals.
91  *
92  * So.  The present situation is, we use sigaction(2) with no
93  * guarantee of restart anywhere but on Linux and BSD.  We could
94  * switch to signal(3) and collar Linux, BSD, and SVr4.  Any way
95  * we slice it, System V UNIXes older than SVr4 will probably lose
96  * (this may include XENIX).
97  *
98  * This implementation will probably be changed to use signal(3) in
99  * the future.  If nothing else, it's simpler...
100  */
101
102 #if USE_SIGTSTP
103 static void tstp(int dummy GCC_UNUSED)
104 {
105         sigset_t mask, omask;
106         sigaction_t act, oact;
107
108 #ifdef SIGTTOU
109         int sigttou_blocked;
110 #endif
111
112         T(("tstp() called"));
113
114         /*
115          * The user may have changed the prog_mode tty bits, so save them.
116          *
117          * But first try to detect whether we still are in the foreground
118          * process group - if not, an interactive shell may already have
119          * taken ownership of the tty and modified the settings when our
120          * parent was stopped before us, and we would likely pick up the
121          * settings already modified by the shell.
122          */
123         if (SP != 0 && !SP->_endwin) /* don't do this if we're not in curses */
124 #if HAVE_TCGETPGRP
125         if (tcgetpgrp(STDIN_FILENO) == getpgrp())
126 #endif
127             def_prog_mode();
128
129         /*
130          * Block window change and timer signals.  The latter
131          * is because applications use timers to decide when
132          * to repaint the screen.
133          */
134         (void)sigemptyset(&mask);
135         (void)sigaddset(&mask, SIGALRM);
136 #if USE_SIGWINCH
137         (void)sigaddset(&mask, SIGWINCH);
138 #endif
139         (void)sigprocmask(SIG_BLOCK, &mask, &omask);
140
141 #ifdef SIGTTOU
142         sigttou_blocked = sigismember(&omask, SIGTTOU);
143         if (!sigttou_blocked) {
144             (void)sigemptyset(&mask);
145             (void)sigaddset(&mask, SIGTTOU);
146             (void)sigprocmask(SIG_BLOCK, &mask, NULL);
147         }
148 #endif
149
150         /*
151          * End window mode, which also resets the terminal state to the
152          * original (pre-curses) modes.
153          */
154         endwin();
155
156         /* Unblock SIGTSTP. */
157         (void)sigemptyset(&mask);
158         (void)sigaddset(&mask, SIGTSTP);
159 #ifdef SIGTTOU
160         if (!sigttou_blocked) {
161             /* Unblock this too if it wasn't blocked on entry */
162             (void)sigaddset(&mask, SIGTTOU);
163         }
164 #endif
165         (void)sigprocmask(SIG_UNBLOCK, &mask, NULL);
166
167         /* Now we want to resend SIGSTP to this process and suspend it */
168         act.sa_handler = SIG_DFL;
169         sigemptyset(&act.sa_mask);
170         act.sa_flags = 0;
171 #ifdef SA_RESTART
172         act.sa_flags |= SA_RESTART;
173 #endif /* SA_RESTART */
174         sigaction(SIGTSTP, &act, &oact);
175         kill(getpid(), SIGTSTP);
176
177         /* Process gets suspended...time passes...process resumes */
178
179         T(("SIGCONT received"));
180         sigaction(SIGTSTP, &oact, NULL);
181         flushinp();
182
183         /*
184          * If the user modified the tty state while suspended, he wants
185          * those changes to stick.  So save the new "default" terminal state.
186          */
187         def_shell_mode();
188
189         /*
190          * This relies on the fact that doupdate() will restore the
191          * program-mode tty state, and issue enter_ca_mode if need be.
192          */
193         doupdate();
194
195         /* Reset the signals. */
196         (void)sigprocmask(SIG_SETMASK, &omask, NULL);
197 }
198 #endif  /* USE_SIGTSTP */
199
200 static void cleanup(int sig)
201 {
202         static int nested;
203
204         /*
205          * Actually, doing any sort of I/O from within an signal handler is
206          * "unsafe".  But we'll _try_ to clean up the screen and terminal
207          * settings on the way out.
208          */
209         if (!nested++
210          && (sig == SIGINT
211           || sig == SIGQUIT)) {
212 #if HAVE_SIGACTION || HAVE_SIGVEC
213                 sigaction_t act;
214                 sigemptyset(&act.sa_mask);
215                 act.sa_flags = 0;
216                 act.sa_handler = SIG_IGN;
217                 if (sigaction(sig, &act, (sigaction_t *)0) == 0)
218 #else
219                 if (signal(sig, SIG_IGN) != SIG_ERR)
220 #endif
221                 {
222                     SCREEN *scan = _nc_screen_chain;
223                     while(scan)
224                     {
225                         if (SP != 0
226                         && SP->_ofp != 0
227                         && isatty(fileno(SP->_ofp))) {
228                             SP->_cleanup = TRUE;
229                         }
230                         set_term(scan);
231                         endwin();
232                         if (SP)
233                             SP->_endwin = FALSE; /* in case we have an atexit! */
234                         scan = scan->_next_screen;
235                     }
236                 }
237         }
238         exit(EXIT_FAILURE);
239 }
240
241 #if USE_SIGWINCH
242 static void sigwinch(int sig GCC_UNUSED)
243 {
244     SCREEN *scan = _nc_screen_chain;
245     while(scan)
246     {
247         scan->_sig_winch = TRUE;
248         scan = scan->_next_screen;
249     }
250 }
251 #endif /* USE_SIGWINCH */
252
253 /*
254  * If the given signal is still in its default state, set it to the given
255  * handler.
256  */
257 #if HAVE_SIGACTION || HAVE_SIGVEC
258 static int CatchIfDefault(int sig, sigaction_t *act)
259 {
260         sigaction_t old_act;
261
262         if (sigaction(sig, (sigaction_t *)0, &old_act) == 0
263          && (old_act.sa_handler == SIG_DFL
264 #if USE_SIGWINCH
265             || (sig == SIGWINCH && old_act.sa_handler == SIG_IGN)
266 #endif
267             )) {
268                 (void)sigaction(sig, act, (sigaction_t *)0);
269                 return TRUE;
270         }
271         return FALSE;
272 }
273 #else
274 static int CatchIfDefault(int sig, RETSIGTYPE (*handler)(int))
275 {
276         void    (*ohandler)(int);
277
278         ohandler = signal(sig, SIG_IGN);
279         if (ohandler == SIG_DFL
280 #if USE_SIGWINCH
281             || (sig == SIGWINCH && ohandler == SIG_IGN)
282 #endif
283         ) {
284                 signal(sig, handler);
285                 return TRUE;
286         } else {
287                 signal(sig, ohandler);
288                 return FALSE;
289         }
290 }
291 #endif
292
293 /*
294  * This is invoked once at the beginning (e.g., from 'initscr()'), to
295  * initialize the signal catchers, and thereafter when spawning a shell (and
296  * returning) to disable/enable the SIGTSTP (i.e., ^Z) catcher.
297  *
298  * If the application has already set one of the signals, we'll not modify it
299  * (during initialization).
300  *
301  * The XSI document implies that we shouldn't keep the SIGTSTP handler if
302  * the caller later changes its mind, but that doesn't seem correct.
303  */
304 void _nc_signal_handler(bool enable)
305 {
306 #if USE_SIGTSTP         /* Xenix 2.x doesn't have SIGTSTP, for example */
307 static sigaction_t act, oact;
308 static int ignore;
309
310         if (!ignore)
311         {
312                 if (!enable)
313                 {
314                         act.sa_handler = SIG_IGN;
315                         sigaction(SIGTSTP, &act, &oact);
316                 }
317                 else if (act.sa_handler)
318                 {
319                         sigaction(SIGTSTP, &oact, NULL);
320                 }
321                 else    /*initialize */
322                 {
323                         sigemptyset(&act.sa_mask);
324                         act.sa_flags = 0;
325 #if USE_SIGWINCH
326                         act.sa_handler = sigwinch;
327                         CatchIfDefault(SIGWINCH, &act);
328 #endif
329
330 #ifdef SA_RESTART
331                         act.sa_flags |= SA_RESTART;
332 #endif /* SA_RESTART */
333                         act.sa_handler = cleanup;
334                         CatchIfDefault(SIGINT,  &act);
335                         CatchIfDefault(SIGTERM, &act);
336
337                         act.sa_handler = tstp;
338                         if (!CatchIfDefault(SIGTSTP, &act))
339                                 ignore = TRUE;
340                 }
341         }
342 #else /* !USE_SIGTSTP */
343         if (enable)
344         {
345 #if HAVE_SIGACTION || HAVE_SIGVEC
346                 static sigaction_t act;
347                 sigemptyset(&act.sa_mask);
348 #if USE_SIGWINCH
349                 act.sa_handler = sigwinch;
350                 CatchIfDefault(SIGWINCH, &act);
351 #endif
352 #ifdef SA_RESTART
353                 act.sa_flags |= SA_RESTART;
354 #endif /* SA_RESTART */
355                 act.sa_handler = cleanup;
356                 CatchIfDefault(SIGINT,  &act);
357                 CatchIfDefault(SIGTERM, &act);
358
359 #else /* !(HAVE_SIGACTION || HAVE_SIGVEC) */
360
361                 CatchIfDefault(SIGINT,  cleanup);
362                 CatchIfDefault(SIGTERM, cleanup);
363 #if USE_SIGWINCH
364                 CatchIfDefault(SIGWINCH, sigwinch);
365 #endif
366 #endif /* !(HAVE_SIGACTION || HAVE_SIGVEC) */
367         }
368 #endif /* !USE_SIGTSTP */
369 }