diff --git a/keepalived/core/pidfile.c b/keepalived/core/pidfile.c index 8e2214d86d..7696e8912a 100644 --- a/keepalived/core/pidfile.c +++ b/keepalived/core/pidfile.c @@ -253,7 +253,11 @@ pidfile_write(pidfile_t *pidf) { int ret; - if (pidf->fd == -1) + /* If keepalived originally started with no configuration for this process, + * the process won't have originally been started, and the parent process + * will not have created and opened a pid file. This means that pidf->fd + * could be -1 after a reload. */ + if (!children_started && pidf->fd == -1) return false; if (children_started) { @@ -263,28 +267,30 @@ pidfile_write(pidfile_t *pidf) * complicate the code for minimal benefit. */ if (stat(pidf->path, &statb)) { /* pidfile no longer exists */ - close(pidf->fd); + if (pidf->fd != -1) + close(pidf->fd); create_pidfile(pidf); } else { - if (fstat(pidf->fd, &fstatb) || + if (pidf->fd == -1 || + fstat(pidf->fd, &fstatb) || statb.st_dev != fstatb.st_dev || statb.st_ino != fstatb.st_ino) { - /* The pidfile has been deleted and recreated. Open the new one. */ - close(pidf->fd); + if (pidf->fd != -1) { + /* The pidfile has been deleted and recreated. Open the new one. */ + close(pidf->fd); + } + while ((pidf->fd = open(pidf->path, O_NOFOLLOW | O_WRONLY | O_NONBLOCK)) == -1 && errno == EINTR); if (pidf->fd == -1) return false; } - /* Since we have already written to the pid file, + /* Since we may have already written to the pid file, * we need to reset the file offset and truncate the file. */ - off_t offs = lseek(pidf->fd, 0, SEEK_CUR); - if (offs) { - lseek(pidf->fd, 0, SEEK_SET); - if (ftruncate(pidf->fd, 0)) - log_message(LOG_INFO, "ftruncate error %d - %m", errno); - } + lseek(pidf->fd, 0, SEEK_SET); + if (ftruncate(pidf->fd, 0)) + log_message(LOG_INFO, "ftruncate error %d - %m", errno); } }