blob: 8624b7604e78c8f0c3e0699be0d2b80fb4324110 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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 * and: Thomas E. Dickey 1995-on *
33 ****************************************************************************/
34
35/*
36** lib_tstp.c
37**
38** The routine _nc_signal_handler().
39**
40*/
41#include <curses.priv.h>
42
43#include <SigAction.h>
44
Steve Kondikae271bc2015-11-15 02:50:53 +010045MODULE_ID("$Id: lib_tstp.c,v 1.48 2014/04/26 18:47:35 juergen Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
47#if defined(SIGTSTP) && (HAVE_SIGACTION || HAVE_SIGVEC)
48#define USE_SIGTSTP 1
49#else
50#define USE_SIGTSTP 0
51#endif
52
53#ifdef TRACE
54static const char *
55signal_name(int sig)
56{
57 switch (sig) {
Steve Kondikae271bc2015-11-15 02:50:53 +010058#ifdef SIGALRM
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053059 case SIGALRM:
60 return "SIGALRM";
Steve Kondikae271bc2015-11-15 02:50:53 +010061#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053062#ifdef SIGCONT
63 case SIGCONT:
64 return "SIGCONT";
65#endif
66 case SIGINT:
67 return "SIGINT";
Steve Kondikae271bc2015-11-15 02:50:53 +010068#ifdef SIGQUIT
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053069 case SIGQUIT:
70 return "SIGQUIT";
Steve Kondikae271bc2015-11-15 02:50:53 +010071#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053072 case SIGTERM:
73 return "SIGTERM";
74#ifdef SIGTSTP
75 case SIGTSTP:
76 return "SIGTSTP";
77#endif
78#ifdef SIGTTOU
79 case SIGTTOU:
80 return "SIGTTOU";
81#endif
82#ifdef SIGWINCH
83 case SIGWINCH:
84 return "SIGWINCH";
85#endif
86 default:
87 return "unknown signal";
88 }
89}
90#endif
91
92/*
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
100 * immediately.
101 *
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
108 * does not).
109 *
110 * Stock System Vs (and anything else using a strict-POSIX
111 * sigaction(2) without SA_RESTART) may have a problem. Possible
112 * solutions:
113 *
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.
118 *
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.
124 *
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).
130 *
131 * This implementation will probably be changed to use signal(3) in
132 * the future. If nothing else, it's simpler...
133 */
134
135#if USE_SIGTSTP
136static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100137handle_SIGTSTP(int dummy GCC_UNUSED)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530138{
Steve Kondikae271bc2015-11-15 02:50:53 +0100139 SCREEN *sp = CURRENT_SCREEN;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530140 sigset_t mask, omask;
141 sigaction_t act, oact;
142
143#ifdef SIGTTOU
144 int sigttou_blocked;
145#endif
146
Steve Kondikae271bc2015-11-15 02:50:53 +0100147 _nc_globals.have_sigtstp = 1;
148 T(("handle_SIGTSTP() called"));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530149
150 /*
151 * The user may have changed the prog_mode tty bits, so save them.
152 *
153 * But first try to detect whether we still are in the foreground
154 * process group - if not, an interactive shell may already have
155 * taken ownership of the tty and modified the settings when our
156 * parent was stopped before us, and we would likely pick up the
157 * settings already modified by the shell.
158 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100159 if (sp != 0 && !sp->_endwin) /* don't do this if we're not in curses */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530160#if HAVE_TCGETPGRP
161 if (tcgetpgrp(STDIN_FILENO) == getpgrp())
162#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100163 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530164
165 /*
166 * Block window change and timer signals. The latter
167 * is because applications use timers to decide when
168 * to repaint the screen.
169 */
170 (void) sigemptyset(&mask);
Steve Kondikae271bc2015-11-15 02:50:53 +0100171#ifdef SIGALRM
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530172 (void) sigaddset(&mask, SIGALRM);
Steve Kondikae271bc2015-11-15 02:50:53 +0100173#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530174#if USE_SIGWINCH
175 (void) sigaddset(&mask, SIGWINCH);
176#endif
177 (void) sigprocmask(SIG_BLOCK, &mask, &omask);
178
179#ifdef SIGTTOU
180 sigttou_blocked = sigismember(&omask, SIGTTOU);
181 if (!sigttou_blocked) {
182 (void) sigemptyset(&mask);
183 (void) sigaddset(&mask, SIGTTOU);
184 (void) sigprocmask(SIG_BLOCK, &mask, NULL);
185 }
186#endif
187
188 /*
189 * End window mode, which also resets the terminal state to the
190 * original (pre-curses) modes.
191 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 NCURSES_SP_NAME(endwin) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530193
194 /* Unblock SIGTSTP. */
195 (void) sigemptyset(&mask);
196 (void) sigaddset(&mask, SIGTSTP);
197#ifdef SIGTTOU
198 if (!sigttou_blocked) {
199 /* Unblock this too if it wasn't blocked on entry */
200 (void) sigaddset(&mask, SIGTTOU);
201 }
202#endif
203 (void) sigprocmask(SIG_UNBLOCK, &mask, NULL);
204
205 /* Now we want to resend SIGSTP to this process and suspend it */
206 act.sa_handler = SIG_DFL;
207 sigemptyset(&act.sa_mask);
208 act.sa_flags = 0;
209#ifdef SA_RESTART
210 act.sa_flags |= SA_RESTART;
211#endif /* SA_RESTART */
212 sigaction(SIGTSTP, &act, &oact);
213 kill(getpid(), SIGTSTP);
214
215 /* Process gets suspended...time passes...process resumes */
216
217 T(("SIGCONT received"));
218 sigaction(SIGTSTP, &oact, NULL);
Steve Kondikae271bc2015-11-15 02:50:53 +0100219 NCURSES_SP_NAME(flushinp) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530220
221 /*
222 * If the user modified the tty state while suspended, he wants
223 * those changes to stick. So save the new "default" terminal state.
224 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100225 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226
227 /*
228 * This relies on the fact that doupdate() will restore the
229 * program-mode tty state, and issue enter_ca_mode if need be.
230 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100231 NCURSES_SP_NAME(doupdate) (NCURSES_SP_ARG);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530232
233 /* Reset the signals. */
234 (void) sigprocmask(SIG_SETMASK, &omask, NULL);
235}
236#endif /* USE_SIGTSTP */
237
238static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100239handle_SIGINT(int sig)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530240{
Steve Kondikae271bc2015-11-15 02:50:53 +0100241 SCREEN *sp = CURRENT_SCREEN;
242
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530243 /*
Steve Kondikae271bc2015-11-15 02:50:53 +0100244 * Much of this is unsafe from a signal handler. But we'll _try_ to clean
245 * up the screen and terminal settings on the way out.
246 *
247 * There are at least the following problems:
248 * 1) Walking the SCREEN list is unsafe, since all list management
249 * is done without any signal blocking.
250 * 2) On systems which have REENTRANT turned on, set_term() uses
251 * _nc_lock_global() which could deadlock or misbehave in other ways.
252 * 3) endwin() calls all sorts of stuff, many of which use stdio or
253 * other library functions which are clearly unsafe.
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530254 */
255 if (!_nc_globals.cleanup_nested++
Steve Kondikae271bc2015-11-15 02:50:53 +0100256 && (sig == SIGINT || sig == SIGTERM)) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530257#if HAVE_SIGACTION || HAVE_SIGVEC
258 sigaction_t act;
259 sigemptyset(&act.sa_mask);
260 act.sa_flags = 0;
261 act.sa_handler = SIG_IGN;
262 if (sigaction(sig, &act, NULL) == 0)
263#else
264 if (signal(sig, SIG_IGN) != SIG_ERR)
265#endif
266 {
267 SCREEN *scan;
268 for (each_screen(scan)) {
269 if (scan->_ofp != 0
Steve Kondikae271bc2015-11-15 02:50:53 +0100270 && NC_ISATTY(fileno(scan->_ofp))) {
271 scan->_outch = NCURSES_SP_NAME(_nc_outch);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530272 }
273 set_term(scan);
Steve Kondikae271bc2015-11-15 02:50:53 +0100274 NCURSES_SP_NAME(endwin) (NCURSES_SP_ARG);
275 if (sp)
276 sp->_endwin = FALSE; /* in case of reuse */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530277 }
278 }
279 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100280 _exit(EXIT_FAILURE);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530281}
282
283#if USE_SIGWINCH
284static void
Steve Kondikae271bc2015-11-15 02:50:53 +0100285handle_SIGWINCH(int sig GCC_UNUSED)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530286{
287 _nc_globals.have_sigwinch = 1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100288# if USE_PTHREADS_EINTR
289 if (_nc_globals.read_thread) {
290 if (!pthread_equal(pthread_self(), _nc_globals.read_thread))
291 pthread_kill(_nc_globals.read_thread, SIGWINCH);
292 _nc_globals.read_thread = 0;
293 }
294# endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530295}
296#endif /* USE_SIGWINCH */
297
298/*
299 * If the given signal is still in its default state, set it to the given
300 * handler.
301 */
302static int
Steve Kondikae271bc2015-11-15 02:50:53 +0100303CatchIfDefault(int sig, void (*handler) (int))
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530304{
305 int result;
306#if HAVE_SIGACTION || HAVE_SIGVEC
307 sigaction_t old_act;
308 sigaction_t new_act;
309
310 memset(&new_act, 0, sizeof(new_act));
311 sigemptyset(&new_act.sa_mask);
312#ifdef SA_RESTART
313#ifdef SIGWINCH
314 if (sig != SIGWINCH)
315#endif
316 new_act.sa_flags |= SA_RESTART;
317#endif /* SA_RESTART */
318 new_act.sa_handler = handler;
319
320 if (sigaction(sig, NULL, &old_act) == 0
321 && (old_act.sa_handler == SIG_DFL
322 || old_act.sa_handler == handler
323#if USE_SIGWINCH
324 || (sig == SIGWINCH && old_act.sa_handler == SIG_IGN)
325#endif
326 )) {
327 (void) sigaction(sig, &new_act, NULL);
328 result = TRUE;
329 } else {
330 result = FALSE;
331 }
332#else /* !HAVE_SIGACTION */
Steve Kondikae271bc2015-11-15 02:50:53 +0100333 void (*ohandler) (int);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530334
335 ohandler = signal(sig, SIG_IGN);
336 if (ohandler == SIG_DFL
337 || ohandler == handler
338#if USE_SIGWINCH
339 || (sig == SIGWINCH && ohandler == SIG_IGN)
340#endif
341 ) {
342 signal(sig, handler);
343 result = TRUE;
344 } else {
345 signal(sig, ohandler);
346 result = FALSE;
347 }
348#endif
349 T(("CatchIfDefault - will %scatch %s",
350 result ? "" : "not ", signal_name(sig)));
351 return result;
352}
353
354/*
355 * This is invoked once at the beginning (e.g., from 'initscr()'), to
356 * initialize the signal catchers, and thereafter when spawning a shell (and
357 * returning) to disable/enable the SIGTSTP (i.e., ^Z) catcher.
358 *
359 * If the application has already set one of the signals, we'll not modify it
360 * (during initialization).
361 *
362 * The XSI document implies that we shouldn't keep the SIGTSTP handler if
363 * the caller later changes its mind, but that doesn't seem correct.
364 */
365NCURSES_EXPORT(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100366_nc_signal_handler(int enable)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530367{
368 T((T_CALLED("_nc_signal_handler(%d)"), enable));
369#if USE_SIGTSTP /* Xenix 2.x doesn't have SIGTSTP, for example */
370 {
371 static bool ignore_tstp = FALSE;
372
373 if (!ignore_tstp) {
374 static sigaction_t new_sigaction, old_sigaction;
375
376 if (!enable) {
377 new_sigaction.sa_handler = SIG_IGN;
378 sigaction(SIGTSTP, &new_sigaction, &old_sigaction);
379 } else if (new_sigaction.sa_handler != SIG_DFL) {
380 sigaction(SIGTSTP, &old_sigaction, NULL);
381 } else if (sigaction(SIGTSTP, NULL, &old_sigaction) == 0
382 && (old_sigaction.sa_handler == SIG_DFL)) {
383 sigemptyset(&new_sigaction.sa_mask);
384#ifdef SA_RESTART
385 new_sigaction.sa_flags |= SA_RESTART;
386#endif /* SA_RESTART */
Steve Kondikae271bc2015-11-15 02:50:53 +0100387 new_sigaction.sa_handler = handle_SIGTSTP;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530388 (void) sigaction(SIGTSTP, &new_sigaction, NULL);
389 } else {
390 ignore_tstp = TRUE;
391 }
392 }
393 }
394#endif /* !USE_SIGTSTP */
395
396 if (!_nc_globals.init_signals) {
397 if (enable) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100398 CatchIfDefault(SIGINT, handle_SIGINT);
399 CatchIfDefault(SIGTERM, handle_SIGINT);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530400#if USE_SIGWINCH
Steve Kondikae271bc2015-11-15 02:50:53 +0100401 CatchIfDefault(SIGWINCH, handle_SIGWINCH);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530402#endif
403 _nc_globals.init_signals = TRUE;
404 }
405 }
406 returnVoid;
407}