Skip to content

Commit

Permalink
Merge pull request #110 from nirs/pidfile-cleanup
Browse files Browse the repository at this point in the history
Fix pidfile cleanup when receiving signal
  • Loading branch information
jandubois authored Dec 13, 2024
2 parents abdbc35 + d653ec4 commit 0f61b07
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ static void remove_pidfile(const char *pidfile)
{
if (unlink(pidfile) != 0) {
ERRORF("Failed to remove pidfile: \"%s\": %s", pidfile, strerror(errno));
return;
}
INFOF("Removed pidfile \"%s\" for process %d", pidfile, getpid());
}

static int create_pidfile(const char *pidfile)
Expand All @@ -385,6 +387,7 @@ static int create_pidfile(const char *pidfile)
return -1;
}

INFOF("Created pidfile \"%s\" for process %d", pidfile, getpid());
return fd;
}

Expand All @@ -406,16 +409,6 @@ int main(int argc, char *argv[]) {
WARN("Seems running with SETUID. This is insecure and highly discouraged: See README.md");
}

if (sigsetjmp(jmpbuf, 1) != 0) {
goto done;
}
signal(SIGHUP, signalhandler);
signal(SIGINT, signalhandler);
signal(SIGTERM, signalhandler);

// We will receive EPIPE on the socket.
signal(SIGPIPE, SIG_IGN);

int pidfile_fd = -1;
if (cliopt->pidfile != NULL) {
pidfile_fd = create_pidfile(cliopt->pidfile);
Expand All @@ -432,6 +425,16 @@ int main(int argc, char *argv[]) {
goto done;
}

if (sigsetjmp(jmpbuf, 1) != 0) {
goto done;
}
signal(SIGHUP, signalhandler);
signal(SIGINT, signalhandler);
signal(SIGTERM, signalhandler);

// We will receive EPIPE on the socket.
signal(SIGPIPE, SIG_IGN);

state.sem = dispatch_semaphore_create(1);

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

0 comments on commit 0f61b07

Please sign in to comment.