]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: server-state: Ignore empty files
authorMarcos de Oliveira <markinholiveira@gmail.com>
Thu, 20 Jul 2023 20:21:10 +0000 (17:21 -0300)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 21 Jul 2023 13:08:27 +0000 (15:08 +0200)
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.

src/server_state.c

index d2cc4d8205ab815df11e30d02b35f38087082c93..9636b9f6d3d0fee4c0c1c37dc3689d25d5ee4a11 100644 (file)
@@ -522,6 +522,7 @@ static void srv_state_px_update(const struct proxy *px, int vsn, struct eb_root
 
 /*
  * read next line from file <f> 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 <f>
  */
@@ -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;
                }