From 2103ba27cea348b70f0a3ba7f22629144eaf7097 Mon Sep 17 00:00:00 2001 From: Quentin Armitage Date: Thu, 22 Aug 2024 12:29:15 +0100 Subject: [PATCH] all: update how pidfile handled after reload with new configuration This commit comprises only keepalived/core/pidfile.c and is the changes in commits: 3ea412d: all: ensure pidfile is created if a reload causes child to start b93596f: all: update how pidfile handled after reload with new configuration Signed-off-by: Quentin Armitage --- keepalived/core/pidfile.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) 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); } }