]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: init: encapsulate CHECK_CONDITION logic in a func
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Wed, 26 Jun 2024 16:39:45 +0000 (18:39 +0200)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 27 Jun 2024 14:01:01 +0000 (16:01 +0200)
As MODE_CHECK_CONDITION logic terminates the process anyway, no matter if
the test for the provided condition was successfull or not, let's
encapsulate it in a separate function. This makes the code of init() more
readable.

src/haproxy.c

index 155779e3ec7ec62c242289da30dd204e3b72bb78..e6712576bc30e48d985294d8e6eb8f5b91a68d61 100644 (file)
@@ -1539,6 +1539,71 @@ static int check_if_maxsock_permitted(int maxsock)
        return ret == 0;
 }
 
+/* Evaluates a condition provided within a conditional block of the
+ * configuration. Makes process to exit with 0, if the condition is true, with
+ * 1, if the condition is false or with 2, if parse_line encounters an error.
+ */
+static void do_check_condition(char *progname)
+{
+       int result;
+       uint32_t err;
+       const char *errptr;
+       char *errmsg = NULL;
+
+       char *args[MAX_LINE_ARGS+1];
+       int arg = sizeof(args) / sizeof(*args);
+       size_t outlen;
+       char *w;
+
+       if (!check_condition)
+               usage(progname);
+
+       outlen = strlen(check_condition) + 1;
+       err = parse_line(check_condition, check_condition, &outlen, args, &arg,
+                         PARSE_OPT_ENV | PARSE_OPT_WORD_EXPAND | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE | PARSE_OPT_BKSLASH,
+                         &errptr);
+
+       if (err & PARSE_ERR_QUOTE) {
+               ha_alert("Syntax Error in condition: Unmatched quote.\n");
+               exit(2);
+       }
+
+       if (err & PARSE_ERR_HEX) {
+               ha_alert("Syntax Error in condition: Truncated or invalid hexadecimal sequence.\n");
+               exit(2);
+       }
+
+       if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
+               ha_alert("Error in condition: Line too long.\n");
+               exit(2);
+       }
+
+       if (err & PARSE_ERR_TOOMANY) {
+               ha_alert("Error in condition: Too many words.\n");
+               exit(2);
+       }
+
+       if (err) {
+               ha_alert("Unhandled error in condition, please report this to the developers.\n");
+               exit(2);
+       }
+
+       /* remerge all words into a single expression */
+       for (w = *args; (w += strlen(w)) < check_condition + outlen - 1; *w = ' ')
+               ;
+
+       result = cfg_eval_condition(args, &errmsg, &errptr);
+
+       if (result < 0) {
+               if (errmsg)
+                       ha_alert("Failed to evaluate condition: %s\n", errmsg);
+
+               exit(2);
+       }
+
+       exit(result ? 0 : 1);
+}
+
 /* This performs th every basic early initialization at the end of the PREPARE
  * init stage. It may only assume that list heads are initialized, but not that
  * anything else is correct. It will initialize a number of variables that
@@ -2063,66 +2128,9 @@ static void init(int argc, char **argv)
                global.mode &= ~MODE_MWORKER;
        }
 
-       if (global.mode & MODE_CHECK_CONDITION) {
-               int result;
-
-               uint32_t err;
-               const char *errptr;
-               char *errmsg = NULL;
-
-               char *args[MAX_LINE_ARGS+1];
-               int arg = sizeof(args) / sizeof(*args);
-               size_t outlen;
-               char *w;
-
-               if (!check_condition)
-                       usage(progname);
-
-               outlen = strlen(check_condition) + 1;
-               err = parse_line(check_condition, check_condition, &outlen, args, &arg,
-                                PARSE_OPT_ENV | PARSE_OPT_WORD_EXPAND | PARSE_OPT_DQUOTE | PARSE_OPT_SQUOTE | PARSE_OPT_BKSLASH,
-                                &errptr);
-
-               if (err & PARSE_ERR_QUOTE) {
-                       ha_alert("Syntax Error in condition: Unmatched quote.\n");
-                       exit(2);
-               }
-
-               if (err & PARSE_ERR_HEX) {
-                       ha_alert("Syntax Error in condition: Truncated or invalid hexadecimal sequence.\n");
-                       exit(2);
-               }
-
-               if (err & (PARSE_ERR_TOOLARGE|PARSE_ERR_OVERLAP)) {
-                       ha_alert("Error in condition: Line too long.\n");
-                       exit(2);
-               }
-
-               if (err & PARSE_ERR_TOOMANY) {
-                       ha_alert("Error in condition: Too many words.\n");
-                       exit(2);
-               }
-
-               if (err) {
-                       ha_alert("Unhandled error in condition, please report this to the developers.\n");
-                       exit(2);
-               }
-
-               /* remerge all words into a single expression */
-               for (w = *args; (w += strlen(w)) < check_condition + outlen - 1; *w = ' ')
-                       ;
-
-               result = cfg_eval_condition(args, &errmsg, &errptr);
-
-               if (result < 0) {
-                       if (errmsg)
-                               ha_alert("Failed to evaluate condition: %s\n", errmsg);
-
-                       exit(2);
-               }
-
-               exit(result ? 0 : 1);
-       }
+       /* Do check_condition, if we started with -cc, and exit. */
+       if (global.mode & MODE_CHECK_CONDITION)
+               do_check_condition(progname);
 
        /* set the atexit functions when not doing configuration check */
        if (!(global.mode & MODE_CHECK) && (getenv("HAPROXY_MWORKER_REEXEC") != NULL)) {