Move string dope from strings.c to the new memory.c..
[s-mailx.git] / signal.c
blobb36ab712f091017b2c8c228de43fb5db4caecabd
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Signal related stuff as well as NotYetDead functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE signal
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
43 * TODO At the beginning of November 2015 -- for v14.9 -- i've tried for one
44 * TODO and a half week to convert this codebase to SysV style signal handling,
45 * TODO meaning no SA_RESTART and EINTR in a lot of places and error reporting
46 * TODO up the chain. I failed miserably, not only because S/MIME / SSL but
47 * TODO also because of general frustration. Directly after v14.9 i will strip
48 * TODO ANYTHING off the codebase (socket stuff etc.) and keep only the very
49 * TODO core, doing namespace and type cleanup and convert this core to a clean
50 * TODO approach, from which i plan to start this thing anew.
51 * TODO For now i introduced the n_sigman, yet another hack, to be used in a
52 * TODO few places.
53 * TODO The real solution:
54 * TODO - No SA_RESTART. Just like for my C++ library: encapsulate EINTR in
55 * TODO userspace at the systemcall (I/O rewrite: drop stdio), normal
56 * TODO interface auto-restarts, special _intr() series return EINTR.
57 * TODO Do n_sigman_poll()/peek()/whatever whenever desired for the former,
58 * TODO report errors up the call chain, have places where operations can be
59 * TODO "properly" aborted.
60 * TODO - We save the initial signal settings upon program startup.
61 * TODO - We register our sigman handlers once, at program startup.
62 * TODO Maximally, and most likely only due to lack of atomic CAS, ignore
63 * TODO or block some signals temporarily. Best if not.
64 * TODO The signal handlers only set a flag. Block all signals for handler
65 * TODO execution, like this we are safe to "set the first signal was x".
66 * TODO - In interactive context, ignore SIGTERM.
67 * TODO I.e., see the POSIX standard for what a shell does.
68 * TODO - In non-interactive context, don't know anything about job control!?!
69 * TODO - Place child processes in their own process group. Restore the signal
70 * TODO mask back to the saved original one for them, before exec.
71 * TODO - Except for job control related (<-> interactive) ignore any signals
72 * TODO while we are "behind" a child that occupies the terminal. For those,
73 * TODO perform proper terminal attribute handling. For childs that don't
74 * TODO occupy the terminal we "are the shell" and should therefore manage
75 * TODO them accordingly, including termination request as necessary.
76 * TODO And if we have a worker in a pipeline, we need to manage it and deal
77 * TODO with it properly, WITHOUT temporary signal overwrites.
78 * TODO - No more jumps.
79 * TODO - (When sockets will be reintroduced, non-blocking.)
80 * TODO - (When SSL is reintroduced, memory BIO. It MAY be necessary to
81 * TODO temporarily block signals during a few SSL functions? Read SSL docu!
82 * TODO But i prefer blocking since it's a single syscall, not temporary
83 * TODO SA_RESTART setting, since that has to be done for every signal.)
86 #ifdef HAVE_NYD
87 struct nyd_info {
88 char const *ni_file;
89 char const *ni_fun;
90 ui32_t ni_chirp_line;
91 ui32_t ni_level;
93 #endif
95 /* {hold,rele}_all_sigs() */
96 static size_t _alls_depth;
97 static sigset_t _alls_nset, _alls_oset;
99 /* {hold,rele}_sigs() */
100 static size_t _hold_sigdepth;
101 static sigset_t _hold_nset, _hold_oset;
103 /* NYD, memory pool debug */
104 #ifdef HAVE_NYD
105 static ui32_t _nyd_curr, _nyd_level;
106 static struct nyd_info _nyd_infos[NYD_CALLS_MAX];
107 #endif
109 /* */
110 #ifdef HAVE_NYD
111 static void _nyd_print(int fd, struct nyd_info *nip);
112 #endif
114 #ifdef HAVE_NYD
115 static void
116 _nyd_print(int fd, struct nyd_info *nip)
118 char buf[80];
119 union {int i; size_t z;} u;
121 u.i = snprintf(buf, sizeof buf,
122 "%c [%2" PRIu32 "] %.25s (%.16s:%" PRIu32 ")\n",
123 "=><"[(nip->ni_chirp_line >> 29) & 0x3], nip->ni_level, nip->ni_fun,
124 nip->ni_file, (nip->ni_chirp_line & 0x1FFFFFFFu));
125 if (u.i > 0) {
126 u.z = u.i;
127 if (u.z > sizeof buf)
128 u.z = sizeof buf - 1; /* (Skip \0) */
129 write(fd, buf, u.z);
132 #endif
134 FL void
135 n_raise(int signo)
137 NYD2_ENTER;
138 kill(getpid(), signo);
139 NYD2_LEAVE;
142 FL sighandler_type
143 safe_signal(int signum, sighandler_type handler)
145 struct sigaction nact, oact;
146 sighandler_type rv;
147 NYD2_ENTER;
149 nact.sa_handler = handler;
150 sigemptyset(&nact.sa_mask);
151 nact.sa_flags = SA_RESTART;
152 rv = (sigaction(signum, &nact, &oact) != 0) ? SIG_ERR : oact.sa_handler;
153 NYD2_LEAVE;
154 return rv;
157 FL void
158 hold_all_sigs(void)
160 NYD2_ENTER;
161 if (_alls_depth++ == 0) {
162 sigfillset(&_alls_nset);
163 sigdelset(&_alls_nset, SIGABRT);
164 #ifdef SIGBUS
165 sigdelset(&_alls_nset, SIGBUS);
166 #endif
167 sigdelset(&_alls_nset, SIGCHLD);
168 sigdelset(&_alls_nset, SIGFPE);
169 sigdelset(&_alls_nset, SIGILL);
170 sigdelset(&_alls_nset, SIGKILL);
171 sigdelset(&_alls_nset, SIGSEGV);
172 sigdelset(&_alls_nset, SIGSTOP);
173 sigprocmask(SIG_BLOCK, &_alls_nset, &_alls_oset);
175 NYD2_LEAVE;
178 FL void
179 rele_all_sigs(void)
181 NYD2_ENTER;
182 if (--_alls_depth == 0)
183 sigprocmask(SIG_SETMASK, &_alls_oset, (sigset_t*)NULL);
184 NYD2_LEAVE;
187 FL void
188 hold_sigs(void)
190 NYD2_ENTER;
191 if (_hold_sigdepth++ == 0) {
192 sigemptyset(&_hold_nset);
193 sigaddset(&_hold_nset, SIGHUP);
194 sigaddset(&_hold_nset, SIGINT);
195 sigaddset(&_hold_nset, SIGQUIT);
196 sigprocmask(SIG_BLOCK, &_hold_nset, &_hold_oset);
198 NYD2_LEAVE;
201 FL void
202 rele_sigs(void)
204 NYD2_ENTER;
205 if (--_hold_sigdepth == 0)
206 sigprocmask(SIG_SETMASK, &_hold_oset, NULL);
207 NYD2_LEAVE;
210 /* TODO This is temporary gracyness */
211 static struct n_sigman *n__sigman;
212 static void n__sigman_hdl(int signo);
213 static void
214 n__sigman_hdl(int signo){
215 NYD_X; /* Signal handler */
216 n__sigman->sm_signo = signo;
217 siglongjmp(n__sigman->sm_jump, 1);
220 FL int
221 n__sigman_enter(struct n_sigman *self, int flags){
222 /* TODO no error checking when installing sighdls */
223 int rv;
224 NYD2_ENTER;
226 if((int)flags >= 0){
227 self->sm_flags = (enum n_sigman_flags)flags;
228 self->sm_signo = 0;
229 self->sm_outer = n__sigman;
230 if(flags & n_SIGMAN_HUP)
231 self->sm_ohup = safe_signal(SIGHUP, &n__sigman_hdl);
232 if(flags & n_SIGMAN_INT)
233 self->sm_oint = safe_signal(SIGINT, &n__sigman_hdl);
234 if(flags & n_SIGMAN_QUIT)
235 self->sm_oquit = safe_signal(SIGQUIT, &n__sigman_hdl);
236 if(flags & n_SIGMAN_PIPE)
237 self->sm_opipe = safe_signal(SIGPIPE, &n__sigman_hdl);
238 n__sigman = self;
239 rv = 0;
240 }else{
241 flags = self->sm_flags;
243 /* Just in case of a race (signal while holding and ignoring? really?) */
244 if(!(flags & n__SIGMAN_PING)){
245 if(flags & n_SIGMAN_HUP)
246 safe_signal(SIGHUP, SIG_IGN);
247 if(flags & n_SIGMAN_INT)
248 safe_signal(SIGINT, SIG_IGN);
249 if(flags & n_SIGMAN_QUIT)
250 safe_signal(SIGQUIT, SIG_IGN);
251 if(flags & n_SIGMAN_PIPE)
252 safe_signal(SIGPIPE, SIG_IGN);
254 rv = self->sm_signo;
256 rele_sigs();
257 NYD2_LEAVE;
258 return rv;
261 FL void
262 n_sigman_cleanup_ping(struct n_sigman *self){
263 ui32_t f;
264 NYD2_ENTER;
266 hold_sigs();
268 f = self->sm_flags;
269 f |= n__SIGMAN_PING;
270 self->sm_flags = f;
272 if(f & n_SIGMAN_HUP)
273 safe_signal(SIGHUP, SIG_IGN);
274 if(f & n_SIGMAN_INT)
275 safe_signal(SIGINT, SIG_IGN);
276 if(f & n_SIGMAN_QUIT)
277 safe_signal(SIGQUIT, SIG_IGN);
278 if(f & n_SIGMAN_PIPE)
279 safe_signal(SIGPIPE, SIG_IGN);
281 rele_sigs();
282 NYD2_LEAVE;
285 FL void
286 n_sigman_leave(struct n_sigman *self,
287 enum n_sigman_flags reraise_flags){
288 ui32_t f;
289 int sig;
290 NYD2_ENTER;
292 hold_sigs();
293 n__sigman = self->sm_outer;
295 f = self->sm_flags;
296 if(f & n_SIGMAN_HUP)
297 safe_signal(SIGHUP, self->sm_ohup);
298 if(f & n_SIGMAN_INT)
299 safe_signal(SIGINT, self->sm_oint);
300 if(f & n_SIGMAN_QUIT)
301 safe_signal(SIGQUIT, self->sm_oquit);
302 if(f & n_SIGMAN_PIPE)
303 safe_signal(SIGPIPE, self->sm_opipe);
305 rele_sigs();
307 sig = 0;
308 switch(self->sm_signo){
309 case SIGPIPE:
310 if((reraise_flags & n_SIGMAN_PIPE) ||
311 ((reraise_flags & n_SIGMAN_NTTYOUT_PIPE) &&
312 !(options & OPT_TTYOUT)))
313 sig = SIGPIPE;
314 break;
315 case SIGHUP:
316 if(reraise_flags & n_SIGMAN_HUP)
317 sig = SIGHUP;
318 break;
319 case SIGINT:
320 if(reraise_flags & n_SIGMAN_INT)
321 sig = SIGINT;
322 break;
323 case SIGQUIT:
324 if(reraise_flags & n_SIGMAN_QUIT)
325 sig = SIGQUIT;
326 break;
327 default:
328 break;
331 NYD2_LEAVE;
332 if(sig != 0){
333 sigset_t cset;
335 sigemptyset(&cset);
336 sigaddset(&cset, sig);
337 sigprocmask(SIG_UNBLOCK, &cset, NULL);
338 n_raise(sig);
342 FL int
343 n_sigman_peek(void){
344 int rv;
345 NYD2_ENTER;
346 rv = 0;
347 NYD2_LEAVE;
348 return rv;
351 FL void
352 n_sigman_consume(void){
353 NYD2_ENTER;
354 NYD2_LEAVE;
357 #ifdef HAVE_NYD
358 FL void
359 _nyd_chirp(ui8_t act, char const *file, ui32_t line, char const *fun)
361 struct nyd_info *nip = _nyd_infos;
363 if (_nyd_curr != NELEM(_nyd_infos))
364 nip += _nyd_curr++;
365 else
366 _nyd_curr = 1;
367 nip->ni_file = file;
368 nip->ni_fun = fun;
369 nip->ni_chirp_line = ((ui32_t)(act & 0x3) << 29) | (line & 0x1FFFFFFFu);
370 nip->ni_level = ((act == 0) ? _nyd_level
371 : (act == 1) ? ++_nyd_level : _nyd_level--);
374 FL void
375 _nyd_oncrash(int signo)
377 char pathbuf[PATH_MAX], s2ibuf[32], *cp;
378 struct sigaction xact;
379 sigset_t xset;
380 size_t i, fnl;
381 int fd;
382 struct nyd_info *nip;
384 LCTA(sizeof("./") -1 + sizeof(UAGENT) -1 + sizeof(".dat") < PATH_MAX);
386 xact.sa_handler = SIG_DFL;
387 sigemptyset(&xact.sa_mask);
388 xact.sa_flags = 0;
389 sigaction(signo, &xact, NULL);
391 i = strlen(tempdir);
392 fnl = sizeof(UAGENT) -1;
394 if (i + 1 + fnl + 1 + sizeof(".dat") > sizeof(pathbuf)) {
395 (cp = pathbuf)[0] = '.';
396 i = 1;
397 } else
398 memcpy(cp = pathbuf, tempdir, i);
399 cp[i++] = '/'; /* xxx pathsep */
400 memcpy(cp += i, UAGENT, fnl);
401 i += fnl;
402 memcpy(cp += fnl, ".dat", sizeof(".dat"));
403 fnl = i + sizeof(".dat") -1;
405 if ((fd = open(pathbuf, O_WRONLY | O_CREAT | O_EXCL, 0666)) == -1)
406 fd = STDERR_FILENO;
408 # undef _X
409 # define _X(X) (X), sizeof(X) -1
410 write(fd, _X("\n\nNYD: program dying due to signal "));
412 cp = s2ibuf + sizeof(s2ibuf) -1;
413 *cp = '\0';
414 i = signo;
415 do {
416 *--cp = "0123456789"[i % 10];
417 i /= 10;
418 } while (i != 0);
419 write(fd, cp, PTR2SIZE((s2ibuf + sizeof(s2ibuf) -1) - cp));
421 write(fd, _X(":\n"));
423 if (_nyd_infos[NELEM(_nyd_infos) - 1].ni_file != NULL)
424 for (i = _nyd_curr, nip = _nyd_infos + i; i < NELEM(_nyd_infos); ++i)
425 _nyd_print(fd, nip++);
426 for (i = 0, nip = _nyd_infos; i < _nyd_curr; ++i)
427 _nyd_print(fd, nip++);
429 write(fd, _X("----------\nCome up to the lab and see what's on the slab\n"));
431 if (fd != STDERR_FILENO) {
432 write(STDERR_FILENO, _X("Crash NYD listing written to "));
433 write(STDERR_FILENO, pathbuf, fnl);
434 write(STDERR_FILENO, _X("\n"));
435 # undef _X
437 close(fd);
440 sigemptyset(&xset);
441 sigaddset(&xset, signo);
442 sigprocmask(SIG_UNBLOCK, &xset, NULL);
443 n_raise(signo);
444 for (;;)
445 _exit(EXIT_ERR);
447 #endif /* HAVE_NYD */
449 /* s-it-mode */