1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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 * and: Thomas E. Dickey 1995-on *
33 ****************************************************************************/
38 ** The routine _nc_signal_handler().
41 #include <curses.priv.h>
43 #include <SigAction.h>
45 MODULE_ID("$Id: lib_tstp.c,v 1.42 2011/04/16 16:42:10 tom Exp $")
47 #if defined(SIGTSTP) && (HAVE_SIGACTION || HAVE_SIGVEC)
87 return "unknown signal";
93 * Note: This code is fragile! Its problem is that different OSs
94 * handle restart of system calls interrupted by signals differently.
95 * The ncurses code needs signal-call restart to happen -- otherwise,
96 * interrupted wgetch() calls will return FAIL, probably making the
97 * application think the input stream has ended and it should
98 * terminate. In particular, you know you have this problem if, when
99 * you suspend an ncurses-using lynx with ^Z and resume, it dies
102 * Default behavior of POSIX sigaction(2) is not to restart
103 * interrupted system calls, but Linux's sigaction does it anyway (at
104 * least, on and after the 1.1.47 I (esr) use). Thus this code works
105 * OK under Linux. The 4.4BSD sigaction(2) supports a (non-portable)
106 * SA_RESTART flag that forces the right behavior. Thus, this code
107 * should work OK under BSD/OS, NetBSD, and FreeBSD (let us know if it
110 * Stock System Vs (and anything else using a strict-POSIX
111 * sigaction(2) without SA_RESTART) may have a problem. Possible
114 * sigvec restarts by default (SV_INTERRUPT flag to not restart)
115 * signal restarts by default in SVr4 (assuming you link with -lucb)
116 * and BSD, but not SVr3.
117 * sigset restarts, but is only available under SVr4/Solaris.
119 * The signal(3) call is mandated by the ANSI standard, and its
120 * interaction with sigaction(2) is described in the POSIX standard
121 * (3.3.4.2, page 72,line 934). According to section 8.1, page 191,
122 * however, signal(3) itself is not required by POSIX.1. And POSIX is
123 * silent on whether it is required to restart signals.
125 * So. The present situation is, we use sigaction(2) with no
126 * guarantee of restart anywhere but on Linux and BSD. We could
127 * switch to signal(3) and collar Linux, BSD, and SVr4. Any way
128 * we slice it, System V UNIXes older than SVr4 will probably lose
129 * (this may include XENIX).
131 * This implementation will probably be changed to use signal(3) in
132 * the future. If nothing else, it's simpler...
137 tstp(int dummy GCC_UNUSED)
139 SCREEN *sp = CURRENT_SCREEN;
140 sigset_t mask, omask;
141 sigaction_t act, oact;
147 T(("tstp() called"));
150 * The user may have changed the prog_mode tty bits, so save them.
152 * But first try to detect whether we still are in the foreground
153 * process group - if not, an interactive shell may already have
154 * taken ownership of the tty and modified the settings when our
155 * parent was stopped before us, and we would likely pick up the
156 * settings already modified by the shell.
158 if (sp != 0 && !sp->_endwin) /* don't do this if we're not in curses */
160 if (tcgetpgrp(STDIN_FILENO) == getpgrp())
162 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
165 * Block window change and timer signals. The latter
166 * is because applications use timers to decide when
167 * to repaint the screen.
169 (void) sigemptyset(&mask);
171 (void) sigaddset(&mask, SIGALRM);
174 (void) sigaddset(&mask, SIGWINCH);
176 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
179 sigttou_blocked = sigismember(&omask, SIGTTOU);
180 if (!sigttou_blocked) {
181 (void) sigemptyset(&mask);
182 (void) sigaddset(&mask, SIGTTOU);
183 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
188 * End window mode, which also resets the terminal state to the
189 * original (pre-curses) modes.
191 NCURSES_SP_NAME(endwin) (NCURSES_SP_ARG);
193 /* Unblock SIGTSTP. */
194 (void) sigemptyset(&mask);
195 (void) sigaddset(&mask, SIGTSTP);
197 if (!sigttou_blocked) {
198 /* Unblock this too if it wasn't blocked on entry */
199 (void) sigaddset(&mask, SIGTTOU);
202 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
204 /* Now we want to resend SIGSTP to this process and suspend it */
205 act.sa_handler = SIG_DFL;
206 sigemptyset(&act.sa_mask);
209 act.sa_flags |= SA_RESTART;
210 #endif /* SA_RESTART */
211 sigaction(SIGTSTP, &act, &oact);
212 kill(getpid(), SIGTSTP);
214 /* Process gets suspended...time passes...process resumes */
216 T(("SIGCONT received"));
217 sigaction(SIGTSTP, &oact, NULL);
218 NCURSES_SP_NAME(flushinp) (NCURSES_SP_ARG);
221 * If the user modified the tty state while suspended, he wants
222 * those changes to stick. So save the new "default" terminal state.
224 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
227 * This relies on the fact that doupdate() will restore the
228 * program-mode tty state, and issue enter_ca_mode if need be.
230 NCURSES_SP_NAME(doupdate) (NCURSES_SP_ARG);
232 /* Reset the signals. */
233 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
235 #endif /* USE_SIGTSTP */
240 SCREEN *sp = CURRENT_SCREEN;
243 * Actually, doing any sort of I/O from within an signal handler is
244 * "unsafe". But we'll _try_ to clean up the screen and terminal
245 * settings on the way out.
247 if (!_nc_globals.cleanup_nested++
253 #if HAVE_SIGACTION || HAVE_SIGVEC
255 sigemptyset(&act.sa_mask);
257 act.sa_handler = SIG_IGN;
258 if (sigaction(sig, &act, NULL) == 0)
260 if (signal(sig, SIG_IGN) != SIG_ERR)
264 for (each_screen(scan)) {
266 && isatty(fileno(scan->_ofp))) {
267 scan->_cleanup = TRUE;
268 scan->_outch = NCURSES_SP_NAME(_nc_outch);
271 NCURSES_SP_NAME(endwin) (NCURSES_SP_ARG);
273 sp->_endwin = FALSE; /* in case we have an atexit! */
282 sigwinch(int sig GCC_UNUSED)
284 _nc_globals.have_sigwinch = 1;
285 # if USE_PTHREADS_EINTR
286 if (_nc_globals.read_thread) {
287 if (!pthread_equal(pthread_self(), _nc_globals.read_thread))
288 pthread_kill(_nc_globals.read_thread, SIGWINCH);
289 _nc_globals.read_thread = 0;
293 #endif /* USE_SIGWINCH */
296 * If the given signal is still in its default state, set it to the given
300 CatchIfDefault(int sig, RETSIGTYPE (*handler) (int))
303 #if HAVE_SIGACTION || HAVE_SIGVEC
307 memset(&new_act, 0, sizeof(new_act));
308 sigemptyset(&new_act.sa_mask);
313 new_act.sa_flags |= SA_RESTART;
314 #endif /* SA_RESTART */
315 new_act.sa_handler = handler;
317 if (sigaction(sig, NULL, &old_act) == 0
318 && (old_act.sa_handler == SIG_DFL
319 || old_act.sa_handler == handler
321 || (sig == SIGWINCH && old_act.sa_handler == SIG_IGN)
324 (void) sigaction(sig, &new_act, NULL);
329 #else /* !HAVE_SIGACTION */
330 RETSIGTYPE (*ohandler) (int);
332 ohandler = signal(sig, SIG_IGN);
333 if (ohandler == SIG_DFL
334 || ohandler == handler
336 || (sig == SIGWINCH && ohandler == SIG_IGN)
339 signal(sig, handler);
342 signal(sig, ohandler);
346 T(("CatchIfDefault - will %scatch %s",
347 result ? "" : "not ", signal_name(sig)));
352 * This is invoked once at the beginning (e.g., from 'initscr()'), to
353 * initialize the signal catchers, and thereafter when spawning a shell (and
354 * returning) to disable/enable the SIGTSTP (i.e., ^Z) catcher.
356 * If the application has already set one of the signals, we'll not modify it
357 * (during initialization).
359 * The XSI document implies that we shouldn't keep the SIGTSTP handler if
360 * the caller later changes its mind, but that doesn't seem correct.
363 _nc_signal_handler(bool enable)
365 T((T_CALLED("_nc_signal_handler(%d)"), enable));
366 #if USE_SIGTSTP /* Xenix 2.x doesn't have SIGTSTP, for example */
368 static bool ignore_tstp = FALSE;
371 static sigaction_t new_sigaction, old_sigaction;
374 new_sigaction.sa_handler = SIG_IGN;
375 sigaction(SIGTSTP, &new_sigaction, &old_sigaction);
376 } else if (new_sigaction.sa_handler != SIG_DFL) {
377 sigaction(SIGTSTP, &old_sigaction, NULL);
378 } else if (sigaction(SIGTSTP, NULL, &old_sigaction) == 0
379 && (old_sigaction.sa_handler == SIG_DFL)) {
380 sigemptyset(&new_sigaction.sa_mask);
382 new_sigaction.sa_flags |= SA_RESTART;
383 #endif /* SA_RESTART */
384 new_sigaction.sa_handler = tstp;
385 (void) sigaction(SIGTSTP, &new_sigaction, NULL);
391 #endif /* !USE_SIGTSTP */
393 if (!_nc_globals.init_signals) {
395 CatchIfDefault(SIGINT, cleanup);
396 CatchIfDefault(SIGTERM, cleanup);
398 CatchIfDefault(SIGWINCH, sigwinch);
400 _nc_globals.init_signals = TRUE;