Skip to content

Commit ab78cb5

Browse files
authored
Merge pull request #103 from nirs/pidfile
Improve pidfile management
2 parents 1483f6c + 8c7691a commit ab78cb5

File tree

1 file changed

+42
-18
lines changed

1 file changed

+42
-18
lines changed

main.c

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,40 @@ static int socket_bindlisten(const char *socket_path,
364364
return -1;
365365
}
366366

367+
static void remove_pidfile(const char *pidfile)
368+
{
369+
if (unlink(pidfile) != 0) {
370+
ERRORF("Failed to remove pidfile: \"%s\": %s", pidfile, strerror(errno));
371+
}
372+
}
373+
374+
static int create_pidfile(const char *pidfile)
375+
{
376+
int flags = O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK;
377+
int fd = open(pidfile, flags, 0644);
378+
if (fd == -1) {
379+
ERRORF("Failed to open pidfile: \"%s\": %s", pidfile, strerror(errno));
380+
return -1;
381+
}
382+
383+
char pid[20];
384+
snprintf(pid, sizeof(pid), "%u", getpid());
385+
ssize_t n = write(fd, pid, strlen(pid));
386+
if (n != (ssize_t)strlen(pid)) {
387+
if (n < 0) {
388+
ERRORF("Failed to write pidfile: \"%s\": %s", pidfile, strerror(errno));
389+
} else {
390+
// Should never happen, but if it does errno is not set.
391+
ERRORF("Short write to pidfile: \"%s\"", pidfile);
392+
}
393+
remove_pidfile(pidfile);
394+
close(fd);
395+
return -1;
396+
}
397+
398+
return fd;
399+
}
400+
367401
static void on_accept(struct state *state, int accept_fd, interface_ref iface);
368402

369403
int main(int argc, char *argv[]) {
@@ -394,15 +428,14 @@ int main(int argc, char *argv[]) {
394428
// We will receive EPIPE on the socket.
395429
signal(SIGPIPE, SIG_IGN);
396430

397-
int pid_fd = -1;
431+
int pidfile_fd = -1;
398432
if (cliopt->pidfile != NULL) {
399-
pid_fd = open(cliopt->pidfile,
400-
O_WRONLY | O_CREAT | O_EXLOCK | O_TRUNC | O_NONBLOCK, 0644);
401-
if (pid_fd == -1) {
402-
ERRORN("pidfile_open");
403-
goto done;
433+
pidfile_fd = create_pidfile(cliopt->pidfile);
434+
if (pidfile_fd == -1) {
435+
goto done; // error already logged.
404436
}
405437
}
438+
406439
DEBUGF("Opening socket \"%s\" (for UNIX group \"%s\")", cliopt->socket_path,
407440
cliopt->socket_group);
408441
listen_fd = socket_bindlisten(cliopt->socket_path, cliopt->socket_group);
@@ -427,15 +460,6 @@ int main(int argc, char *argv[]) {
427460
goto done;
428461
}
429462

430-
if (pid_fd != -1) {
431-
char pid[20];
432-
snprintf(pid, sizeof(pid), "%u", getpid());
433-
if (write(pid_fd, pid, strlen(pid)) != (ssize_t)strlen(pid)) {
434-
ERRORN("write");
435-
goto done;
436-
}
437-
}
438-
439463
while (1) {
440464
int accept_fd = accept(listen_fd, NULL, NULL);
441465
if (accept_fd < 0) {
@@ -456,9 +480,9 @@ int main(int argc, char *argv[]) {
456480
if (listen_fd != -1) {
457481
close(listen_fd);
458482
}
459-
if (pid_fd != -1) {
460-
unlink(cliopt->pidfile);
461-
close(pid_fd);
483+
if (pidfile_fd != -1) {
484+
remove_pidfile(cliopt->pidfile);
485+
close(pidfile_fd);
462486
}
463487
if (state.vms_queue != NULL)
464488
dispatch_release(state.vms_queue);

0 commit comments

Comments
 (0)