privsep.c: and just verify the box is also in CWD (wapiflapi)
[s-mailx.git] / signal.c
blobfd37f4bd186cf0aa90058c1bafeca11a0bc6a626
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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 #ifdef HAVE_DEVEL
135 FL int
136 c_sigstate(void *vp){ /* TODO remove again */
137 struct{
138 int val;
139 char const name[12];
140 } const *hdlp, hdla[] = {
141 {SIGINT, "SIGINT"}, {SIGHUP, "SIGHUP"}, {SIGQUIT, "SIGQUIT"},
142 {SIGTSTP, "SIGTSTP"}, {SIGTTIN, "SIGTTIN"}, {SIGTTOU, "SIGTTOU"},
143 {SIGCHLD, "SIGCHLD"}, {SIGPIPE, "SIGPIPE"}
145 char const *cp;
146 NYD2_ENTER;
148 if((cp = vp) != NULL && cp[0] != '\0'){
149 if(!asccasecmp(&cp[1], "all")){
150 if(cp[0] == '+')
151 hold_all_sigs();
152 else
153 rele_all_sigs();
154 }else if(!asccasecmp(&cp[1], "hold")){
155 if(cp[0] == '+')
156 hold_sigs();
157 else
158 rele_sigs();
162 printf("alls_depth %zu, hold_sigdepth %zu\nHandlers:\n",
163 _alls_depth, _hold_sigdepth);
164 for(hdlp = hdla; hdlp < &hdla[n_NELEM(hdla)]; ++hdlp){
165 sighandler_type shp;
167 shp = safe_signal(hdlp->val, SIG_IGN);
168 safe_signal(hdlp->val, shp);
169 printf(" %s: %p (%s)\n", hdlp->name, shp,
170 (shp == SIG_ERR ? "ERR" : (shp == SIG_DFL ? "DFL"
171 : (shp == SIG_IGN ? "IGN" : "ptf?"))));
173 NYD2_LEAVE;
174 return OKAY;
176 #endif /* HAVE_DEVEL */
178 FL void
179 n_raise(int signo)
181 NYD2_ENTER;
182 kill(getpid(), signo);
183 NYD2_LEAVE;
186 FL sighandler_type
187 safe_signal(int signum, sighandler_type handler)
189 struct sigaction nact, oact;
190 sighandler_type rv;
191 NYD2_ENTER;
193 nact.sa_handler = handler;
194 sigemptyset(&nact.sa_mask);
195 nact.sa_flags = SA_RESTART;
196 rv = (sigaction(signum, &nact, &oact) != 0) ? SIG_ERR : oact.sa_handler;
197 NYD2_LEAVE;
198 return rv;
201 FL n_sighdl_t
202 n_signal(int signo, n_sighdl_t hdl){
203 struct sigaction nact, oact;
204 NYD2_ENTER;
206 nact.sa_handler = hdl;
207 sigemptyset(&nact.sa_mask);
208 nact.sa_flags = 0;
209 hdl = (sigaction(signo, &nact, &oact) != 0) ? SIG_ERR : oact.sa_handler;
210 NYD2_LEAVE;
211 return hdl;
214 FL void
215 hold_all_sigs(void)
217 NYD2_ENTER;
218 if (_alls_depth++ == 0) {
219 sigfillset(&_alls_nset);
220 sigdelset(&_alls_nset, SIGABRT);
221 #ifdef SIGBUS
222 sigdelset(&_alls_nset, SIGBUS);
223 #endif
224 sigdelset(&_alls_nset, SIGCHLD);
225 sigdelset(&_alls_nset, SIGFPE);
226 sigdelset(&_alls_nset, SIGILL);
227 sigdelset(&_alls_nset, SIGKILL);
228 sigdelset(&_alls_nset, SIGSEGV);
229 sigdelset(&_alls_nset, SIGSTOP);
230 sigprocmask(SIG_BLOCK, &_alls_nset, &_alls_oset);
232 NYD2_LEAVE;
235 FL void
236 rele_all_sigs(void)
238 NYD2_ENTER;
239 if (--_alls_depth == 0)
240 sigprocmask(SIG_SETMASK, &_alls_oset, (sigset_t*)NULL);
241 NYD2_LEAVE;
244 FL void
245 hold_sigs(void)
247 NYD2_ENTER;
248 if (_hold_sigdepth++ == 0) {
249 sigemptyset(&_hold_nset);
250 sigaddset(&_hold_nset, SIGHUP);
251 sigaddset(&_hold_nset, SIGINT);
252 sigaddset(&_hold_nset, SIGQUIT);
253 sigprocmask(SIG_BLOCK, &_hold_nset, &_hold_oset);
255 NYD2_LEAVE;
258 FL void
259 rele_sigs(void)
261 NYD2_ENTER;
262 if (--_hold_sigdepth == 0)
263 sigprocmask(SIG_SETMASK, &_hold_oset, NULL);
264 NYD2_LEAVE;
267 /* TODO This is temporary gracyness */
268 static struct n_sigman *n__sigman;
269 static void n__sigman_hdl(int signo);
270 static void
271 n__sigman_hdl(int signo){
272 NYD_X; /* Signal handler */
273 n__sigman->sm_signo = signo;
274 siglongjmp(n__sigman->sm_jump, 1);
277 FL int
278 n__sigman_enter(struct n_sigman *self, int flags){
279 /* TODO no error checking when installing sighdls */
280 int rv;
281 NYD2_ENTER;
283 if((int)flags >= 0){
284 self->sm_flags = (enum n_sigman_flags)flags;
285 self->sm_signo = 0;
286 self->sm_outer = n__sigman;
287 if(flags & n_SIGMAN_HUP)
288 self->sm_ohup = safe_signal(SIGHUP, &n__sigman_hdl);
289 if(flags & n_SIGMAN_INT)
290 self->sm_oint = safe_signal(SIGINT, &n__sigman_hdl);
291 if(flags & n_SIGMAN_QUIT)
292 self->sm_oquit = safe_signal(SIGQUIT, &n__sigman_hdl);
293 if(flags & n_SIGMAN_PIPE)
294 self->sm_opipe = safe_signal(SIGPIPE, &n__sigman_hdl);
295 n__sigman = self;
296 rv = 0;
297 }else{
298 flags = self->sm_flags;
300 /* Just in case of a race (signal while holding and ignoring? really?) */
301 if(!(flags & n__SIGMAN_PING)){
302 if(flags & n_SIGMAN_HUP)
303 safe_signal(SIGHUP, SIG_IGN);
304 if(flags & n_SIGMAN_INT)
305 safe_signal(SIGINT, SIG_IGN);
306 if(flags & n_SIGMAN_QUIT)
307 safe_signal(SIGQUIT, SIG_IGN);
308 if(flags & n_SIGMAN_PIPE)
309 safe_signal(SIGPIPE, SIG_IGN);
311 rv = self->sm_signo;
312 /* The signal mask has been restored, but of course rele_sigs() has
313 * already been called: account for restoration due to jump */
314 ++_hold_sigdepth;
316 rele_sigs();
317 NYD2_LEAVE;
318 return rv;
321 FL void
322 n_sigman_cleanup_ping(struct n_sigman *self){
323 ui32_t f;
324 NYD2_ENTER;
326 hold_sigs();
328 f = self->sm_flags;
329 f |= n__SIGMAN_PING;
330 self->sm_flags = f;
332 if(f & n_SIGMAN_HUP)
333 safe_signal(SIGHUP, SIG_IGN);
334 if(f & n_SIGMAN_INT)
335 safe_signal(SIGINT, SIG_IGN);
336 if(f & n_SIGMAN_QUIT)
337 safe_signal(SIGQUIT, SIG_IGN);
338 if(f & n_SIGMAN_PIPE)
339 safe_signal(SIGPIPE, SIG_IGN);
341 rele_sigs();
342 NYD2_LEAVE;
345 FL void
346 n_sigman_leave(struct n_sigman *self,
347 enum n_sigman_flags reraise_flags){
348 ui32_t f;
349 int sig;
350 NYD2_ENTER;
352 hold_sigs();
353 n__sigman = self->sm_outer;
355 f = self->sm_flags;
356 if(f & n_SIGMAN_HUP)
357 safe_signal(SIGHUP, self->sm_ohup);
358 if(f & n_SIGMAN_INT)
359 safe_signal(SIGINT, self->sm_oint);
360 if(f & n_SIGMAN_QUIT)
361 safe_signal(SIGQUIT, self->sm_oquit);
362 if(f & n_SIGMAN_PIPE)
363 safe_signal(SIGPIPE, self->sm_opipe);
365 rele_sigs();
367 sig = 0;
368 switch(self->sm_signo){
369 case SIGPIPE:
370 if((reraise_flags & n_SIGMAN_PIPE) ||
371 ((reraise_flags & n_SIGMAN_NTTYOUT_PIPE) &&
372 !(options & OPT_TTYOUT)))
373 sig = SIGPIPE;
374 break;
375 case SIGHUP:
376 if(reraise_flags & n_SIGMAN_HUP)
377 sig = SIGHUP;
378 break;
379 case SIGINT:
380 if(reraise_flags & n_SIGMAN_INT)
381 sig = SIGINT;
382 break;
383 case SIGQUIT:
384 if(reraise_flags & n_SIGMAN_QUIT)
385 sig = SIGQUIT;
386 break;
387 default:
388 break;
391 NYD2_LEAVE;
392 if(sig != 0){
393 sigset_t cset;
395 sigemptyset(&cset);
396 sigaddset(&cset, sig);
397 sigprocmask(SIG_UNBLOCK, &cset, NULL);
398 n_raise(sig);
402 FL int
403 n_sigman_peek(void){
404 int rv;
405 NYD2_ENTER;
406 rv = 0;
407 NYD2_LEAVE;
408 return rv;
411 FL void
412 n_sigman_consume(void){
413 NYD2_ENTER;
414 NYD2_LEAVE;
417 #ifdef HAVE_NYD
418 FL void
419 _nyd_chirp(ui8_t act, char const *file, ui32_t line, char const *fun)
421 struct nyd_info *nip = _nyd_infos;
423 if (_nyd_curr != n_NELEM(_nyd_infos))
424 nip += _nyd_curr++;
425 else
426 _nyd_curr = 1;
427 nip->ni_file = file;
428 nip->ni_fun = fun;
429 nip->ni_chirp_line = ((ui32_t)(act & 0x3) << 29) | (line & 0x1FFFFFFFu);
430 nip->ni_level = ((act == 0) ? _nyd_level
431 : (act == 1) ? ++_nyd_level : _nyd_level--);
434 FL void
435 _nyd_oncrash(int signo)
437 char pathbuf[PATH_MAX], s2ibuf[32], *cp;
438 struct sigaction xact;
439 sigset_t xset;
440 struct nyd_info *nip;
441 int fd;
442 size_t i, fnl;
443 char const *tmpdir;
445 n_LCTA(sizeof("./") -1 + sizeof(VAL_UAGENT) -1 + sizeof(".dat") < PATH_MAX,
446 "System limits too low for fixed-size buffer operation");
448 xact.sa_handler = SIG_DFL;
449 sigemptyset(&xact.sa_mask);
450 xact.sa_flags = 0;
451 sigaction(signo, &xact, NULL);
453 i = strlen(tmpdir = ok_vlook(TMPDIR));
454 fnl = sizeof(VAL_UAGENT) -1;
456 if (i + 1 + fnl + 1 + sizeof(".dat") > sizeof(pathbuf)) {
457 (cp = pathbuf)[0] = '.';
458 i = 1;
459 } else
460 memcpy(cp = pathbuf, tmpdir, i);
461 cp[i++] = '/'; /* xxx pathsep */
462 memcpy(cp += i, VAL_UAGENT, fnl);
463 i += fnl;
464 memcpy(cp += fnl, ".dat", sizeof(".dat"));
465 fnl = i + sizeof(".dat") -1;
467 if ((fd = open(pathbuf, O_WRONLY | O_CREAT | O_EXCL, 0666)) == -1)
468 fd = STDERR_FILENO;
470 # undef _X
471 # define _X(X) (X), sizeof(X) -1
472 write(fd, _X("\n\nNYD: program dying due to signal "));
474 cp = s2ibuf + sizeof(s2ibuf) -1;
475 *cp = '\0';
476 i = signo;
477 do {
478 *--cp = "0123456789"[i % 10];
479 i /= 10;
480 } while (i != 0);
481 write(fd, cp, PTR2SIZE((s2ibuf + sizeof(s2ibuf) -1) - cp));
483 write(fd, _X(":\n"));
485 if (_nyd_infos[n_NELEM(_nyd_infos) - 1].ni_file != NULL)
486 for (i = _nyd_curr, nip = _nyd_infos + i; i < n_NELEM(_nyd_infos); ++i)
487 _nyd_print(fd, nip++);
488 for (i = 0, nip = _nyd_infos; i < _nyd_curr; ++i)
489 _nyd_print(fd, nip++);
491 write(fd, _X("----------\nCome up to the lab and see what's on the slab\n"));
493 if (fd != STDERR_FILENO) {
494 write(STDERR_FILENO, _X("Crash NYD listing written to "));
495 write(STDERR_FILENO, pathbuf, fnl);
496 write(STDERR_FILENO, _X("\n"));
497 # undef _X
499 close(fd);
502 sigemptyset(&xset);
503 sigaddset(&xset, signo);
504 sigprocmask(SIG_UNBLOCK, &xset, NULL);
505 n_raise(signo);
506 for (;;)
507 _exit(EXIT_ERR);
509 #endif /* HAVE_NYD */
511 /* s-it-mode */