Skip to content

Commit daadaf3

Browse files
committed
Fix pidfile cleanup when receiving signal
When not using --pidfile option, pidfile_fd is initialized to -1. However if we are interrupted by signal, when we check the value during cleanup, the value is 0(!) and we try to delete a null pidfile and log this error: ^CINFO | Received signal: Interrupt: 2 ERROR| Failed to remove pidfile: "(null)": Bad address This does not make sense, but we use sigsetjmp(3) for handling signals and its manual mentions: The sigsetjmp()/siglongjmp() function pairs save and restore the signal mask if the argument savemask is non-zero; otherwise, only the register set and the stack are saved. It seems that pidfile_fd is restored to the value at the time sigsetjmp() was called, which may be 0 (or any other value on the stack). I think this is an old bug, exposed by logging an error when unlink() fails. Fix by creating the pidfile and binding listen_fd before calling sigsetjmp(). When sigsetjmp() restores the stack the value of pidfile_fd and listen_fd are not affected. Signed-off-by: Nir Soffer <nirsof@gmail.com>
1 parent abdbc35 commit daadaf3

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -406,16 +406,6 @@ int main(int argc, char *argv[]) {
406406
WARN("Seems running with SETUID. This is insecure and highly discouraged: See README.md");
407407
}
408408

409-
if (sigsetjmp(jmpbuf, 1) != 0) {
410-
goto done;
411-
}
412-
signal(SIGHUP, signalhandler);
413-
signal(SIGINT, signalhandler);
414-
signal(SIGTERM, signalhandler);
415-
416-
// We will receive EPIPE on the socket.
417-
signal(SIGPIPE, SIG_IGN);
418-
419409
int pidfile_fd = -1;
420410
if (cliopt->pidfile != NULL) {
421411
pidfile_fd = create_pidfile(cliopt->pidfile);
@@ -432,6 +422,16 @@ int main(int argc, char *argv[]) {
432422
goto done;
433423
}
434424

425+
if (sigsetjmp(jmpbuf, 1) != 0) {
426+
goto done;
427+
}
428+
signal(SIGHUP, signalhandler);
429+
signal(SIGINT, signalhandler);
430+
signal(SIGTERM, signalhandler);
431+
432+
// We will receive EPIPE on the socket.
433+
signal(SIGPIPE, SIG_IGN);
434+
435435
state.sem = dispatch_semaphore_create(1);
436436

437437
// Queue for vm connections, allowing processing vms requests in parallel.

0 commit comments

Comments
 (0)