1 /****************************************************************************
2 * Copyright (c) 1998,1999 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
38 ** The routine _nc_signal_handler().
42 #include <curses.priv.h>
45 #include <SigAction.h>
47 #if defined(SVR4_ACTION) && !defined(_POSIX_SOURCE)
51 MODULE_ID("$Id: lib_tstp.c,v 1.21 2000/05/20 23:28:56 tom Exp $")
53 #if defined(SIGTSTP) && (HAVE_SIGACTION || HAVE_SIGVEC)
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
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
77 * Stock System Vs (and anything else using a strict-POSIX
78 * sigaction(2) without SA_RESTART) may have a problem. Possible
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.
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.
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).
98 * This implementation will probably be changed to use signal(3) in
99 * the future. If nothing else, it's simpler...
103 static void tstp(int dummy GCC_UNUSED)
105 sigset_t mask, omask;
106 sigaction_t act, oact;
112 T(("tstp() called"));
115 * The user may have changed the prog_mode tty bits, so save them.
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.
123 if (SP != 0 && !SP->_endwin) /* don't do this if we're not in curses */
125 if (tcgetpgrp(STDIN_FILENO) == getpgrp())
130 * Block window change and timer signals. The latter
131 * is because applications use timers to decide when
132 * to repaint the screen.
134 (void)sigemptyset(&mask);
135 (void)sigaddset(&mask, SIGALRM);
137 (void)sigaddset(&mask, SIGWINCH);
139 (void)sigprocmask(SIG_BLOCK, &mask, &omask);
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);
151 * End window mode, which also resets the terminal state to the
152 * original (pre-curses) modes.
156 /* Unblock SIGTSTP. */
157 (void)sigemptyset(&mask);
158 (void)sigaddset(&mask, SIGTSTP);
160 if (!sigttou_blocked) {
161 /* Unblock this too if it wasn't blocked on entry */
162 (void)sigaddset(&mask, SIGTTOU);
165 (void)sigprocmask(SIG_UNBLOCK, &mask, NULL);
167 /* Now we want to resend SIGSTP to this process and suspend it */
168 act.sa_handler = SIG_DFL;
169 sigemptyset(&act.sa_mask);
172 act.sa_flags |= SA_RESTART;
173 #endif /* SA_RESTART */
174 sigaction(SIGTSTP, &act, &oact);
175 kill(getpid(), SIGTSTP);
177 /* Process gets suspended...time passes...process resumes */
179 T(("SIGCONT received"));
180 sigaction(SIGTSTP, &oact, NULL);
184 * If the user modified the tty state while suspended, he wants
185 * those changes to stick. So save the new "default" terminal state.
190 * This relies on the fact that doupdate() will restore the
191 * program-mode tty state, and issue enter_ca_mode if need be.
195 /* Reset the signals. */
196 (void)sigprocmask(SIG_SETMASK, &omask, NULL);
198 #endif /* USE_SIGTSTP */
200 static void cleanup(int sig)
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.
211 || sig == SIGQUIT)) {
212 #if HAVE_SIGACTION || HAVE_SIGVEC
214 sigemptyset(&act.sa_mask);
216 act.sa_handler = SIG_IGN;
217 if (sigaction(sig, &act, (sigaction_t *)0) == 0)
219 if (signal(sig, SIG_IGN) != SIG_ERR)
222 SCREEN *scan = _nc_screen_chain;
227 && isatty(fileno(SP->_ofp))) {
229 SP->_outch = _nc_outch;
234 SP->_endwin = FALSE; /* in case we have an atexit! */
235 scan = scan->_next_screen;
243 static void sigwinch(int sig GCC_UNUSED)
245 SCREEN *scan = _nc_screen_chain;
248 scan->_sig_winch = TRUE;
249 scan = scan->_next_screen;
252 #endif /* USE_SIGWINCH */
255 * If the given signal is still in its default state, set it to the given
258 #if HAVE_SIGACTION || HAVE_SIGVEC
259 static int CatchIfDefault(int sig, sigaction_t *act)
263 if (sigaction(sig, (sigaction_t *)0, &old_act) == 0
264 && (old_act.sa_handler == SIG_DFL
266 || (sig == SIGWINCH && old_act.sa_handler == SIG_IGN)
269 (void)sigaction(sig, act, (sigaction_t *)0);
275 static int CatchIfDefault(int sig, RETSIGTYPE (*handler)(int))
277 void (*ohandler)(int);
279 ohandler = signal(sig, SIG_IGN);
280 if (ohandler == SIG_DFL
282 || (sig == SIGWINCH && ohandler == SIG_IGN)
285 signal(sig, handler);
288 signal(sig, ohandler);
295 * This is invoked once at the beginning (e.g., from 'initscr()'), to
296 * initialize the signal catchers, and thereafter when spawning a shell (and
297 * returning) to disable/enable the SIGTSTP (i.e., ^Z) catcher.
299 * If the application has already set one of the signals, we'll not modify it
300 * (during initialization).
302 * The XSI document implies that we shouldn't keep the SIGTSTP handler if
303 * the caller later changes its mind, but that doesn't seem correct.
305 void _nc_signal_handler(bool enable)
307 #if USE_SIGTSTP /* Xenix 2.x doesn't have SIGTSTP, for example */
308 static sigaction_t act, oact;
315 act.sa_handler = SIG_IGN;
316 sigaction(SIGTSTP, &act, &oact);
318 else if (act.sa_handler)
320 sigaction(SIGTSTP, &oact, NULL);
324 sigemptyset(&act.sa_mask);
327 act.sa_handler = sigwinch;
328 CatchIfDefault(SIGWINCH, &act);
332 act.sa_flags |= SA_RESTART;
333 #endif /* SA_RESTART */
334 act.sa_handler = cleanup;
335 CatchIfDefault(SIGINT, &act);
336 CatchIfDefault(SIGTERM, &act);
338 act.sa_handler = tstp;
339 if (!CatchIfDefault(SIGTSTP, &act))
343 #else /* !USE_SIGTSTP */
346 #if HAVE_SIGACTION || HAVE_SIGVEC
347 static sigaction_t act;
348 sigemptyset(&act.sa_mask);
350 act.sa_handler = sigwinch;
351 CatchIfDefault(SIGWINCH, &act);
354 act.sa_flags |= SA_RESTART;
355 #endif /* SA_RESTART */
356 act.sa_handler = cleanup;
357 CatchIfDefault(SIGINT, &act);
358 CatchIfDefault(SIGTERM, &act);
360 #else /* !(HAVE_SIGACTION || HAVE_SIGVEC) */
362 CatchIfDefault(SIGINT, cleanup);
363 CatchIfDefault(SIGTERM, cleanup);
365 CatchIfDefault(SIGWINCH, sigwinch);
367 #endif /* !(HAVE_SIGACTION || HAVE_SIGVEC) */
369 #endif /* !USE_SIGTSTP */