From: Marcos de Oliveira Date: Thu, 20 Jul 2023 20:21:10 +0000 (-0300) Subject: BUG/MINOR: server-state: Ignore empty files X-Git-Tag: v2.9-dev2~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=122a903b94d5db52923ebd2f49a960defddfd40a;p=thirdparty%2Fhaproxy.git BUG/MINOR: server-state: Ignore empty files Users might want to pre-create an empty file for later dumping server-states. This commit allows for that by emiting a notice in case file is empty and a warning if file is not empty, but version is unknown Fix partially: #2190 CF: Technically speaking, this patch can be backported to all stable versions. But it is better to do so to 2.8 only for now. --- diff --git a/src/server_state.c b/src/server_state.c index d2cc4d8205..9636b9f6d3 100644 --- a/src/server_state.c +++ b/src/server_state.c @@ -522,6 +522,7 @@ static void srv_state_px_update(const struct proxy *px, int vsn, struct eb_root /* * read next line from file and return the server state version if one found. + * If file is empty, then -1 is returned * If no version is found, then 0 is returned * Note that this should be the first read on */ @@ -532,7 +533,7 @@ static int srv_state_get_version(FILE *f) { /* first character of first line of the file must contain the version of the export */ if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) - return 0; + return -1; vsn = strtol(mybuf, &endptr, 10); if (endptr == mybuf || *endptr != '\n') { @@ -803,9 +804,13 @@ void apply_server_state(void) } global_vsn = srv_state_get_version(f); - if (global_vsn == 0) { - ha_warning("config: Can't get version of the global server state file '%s'.\n", - file); + if (global_vsn < 1) { + if (global_vsn == -1) + ha_notice("config: Empty global server state file '%s'.\n", + file); + if (global_vsn == 0) + ha_warning("config: Can't get version of the global server state file '%s'.\n", + file); goto close_globalfile; } @@ -873,9 +878,13 @@ void apply_server_state(void) /* first character of first line of the file must contain the version of the export */ local_vsn = srv_state_get_version(f); - if (local_vsn == 0) { - ha_warning("Proxy '%s': Can't get version of the server state file '%s'.\n", - curproxy->id, file); + if (local_vsn < 1) { + if (local_vsn == -1) + ha_notice("Proxy '%s': Empty server state file '%s'.\n", + curproxy->id, file); + if (local_vsn == 0) + ha_warning("Proxy '%s': Can't get version of the server state file '%s'.\n", + curproxy->id, file); goto close_localfile; }