Skip to content

Commit

Permalink
core: fix config-check log file truncate on /dev/null etc
Browse files Browse the repository at this point in the history
Prior to a config-check, the log file is truncated (to 0 size). If
/dev/null (or any other non-regular) file was specified, truncate()
gives error EINVAL, which was not being handled in the code.

This commit stops keepalived logging an error if the output file
specified (or the default /dev/null) is a character device.

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
  • Loading branch information
pqarmitage committed Oct 1, 2023
1 parent bd7cc2b commit 37d449a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions keepalived/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ start_validate_reload_conf_child(void)
int fd;
int len;
char exe_buf[128];
struct stat sb;

exe_buf[sizeof(exe_buf) - 1] = '\0';
ret = readlink("/proc/self/exe", exe_buf, sizeof(exe_buf));
Expand Down Expand Up @@ -1055,8 +1056,13 @@ start_validate_reload_conf_child(void)
script.uid = 0;
script.gid = 0;

if (truncate(global_data->reload_check_config, 0) && errno != ENOENT)
log_message(LOG_INFO, "truncate of config check log %s failed (%d) - %m", global_data->reload_check_config, errno);
if (truncate(global_data->reload_check_config, 0) && errno != ENOENT) {
/* The file exists, but truncate failed. It might be a character
* device like /dev/null. truncate() returns EINVAL in this case. */
if (stat(global_data->reload_check_config, &sb) ||
!S_ISCHR(sb.st_mode))
log_message(LOG_INFO, "truncate of config check log %s failed (%d) - %m", global_data->reload_check_config, errno);
}

create_reload_file();

Expand Down

0 comments on commit 37d449a

Please sign in to comment.