Skip to content

Commit

Permalink
all: update how pidfile handled after reload with new configuration
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
pqarmitage committed Aug 22, 2024
1 parent cbf0a7d commit 2103ba2
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions keepalived/core/pidfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 2103ba2

Please sign in to comment.