]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: haproxy: add a registration for post-check functions
authorWilly Tarreau <w@1wt.eu>
Wed, 21 Dec 2016 18:57:00 +0000 (19:57 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 21 Dec 2016 20:30:54 +0000 (21:30 +0100)
There's a significant amount of late initialization calls which are
performed after the point where we exit in check mode. These calls
are used to allocate resource and perform certain slow operations.
Let's have a way to register some functions which need to be called
there instead of having this multitude of #ifdef in the init path.

include/types/global.h
src/haproxy.c

index 46d3951837bb935abb815377df356fa1f7c3f2e0..9bf77d332371b516d04bfd2770183ec50dccc62d 100644 (file)
@@ -265,6 +265,7 @@ static inline int already_warned(unsigned int warning)
 }
 
 void hap_register_build_opts(const char *str, int must_free);
+void hap_register_post_check(int (*fct)());
 
 #endif /* _TYPES_GLOBAL_H */
 
index cd537acf0317a3247b9c13a83bcc26290963ddda..780ca3c1a7e3fc126420ba96af670f75e11b8e7b 100644 (file)
@@ -273,6 +273,19 @@ struct build_opts_str {
        int must_free;
 };
 
+/* These functions are called just after the point where the program exits
+ * after a config validity check, so they are generally suited for resource
+ * allocation and slow initializations that should be skipped during basic
+ * config checks. The functions must return 0 on success, or a combination
+ * of ERR_* flags (ERR_WARN, ERR_ABORT, ERR_FATAL, ...). The 2 latter cause
+ * and immediate exit, so the function must have emitted any useful error.
+ */
+struct list post_check_list = LIST_HEAD_INIT(post_check_list);
+struct post_check_fct {
+       struct list list;
+       int (*fct)();
+};
+
 /*********************************************************************/
 /*  general purpose functions  ***************************************/
 /*********************************************************************/
@@ -294,6 +307,20 @@ void hap_register_build_opts(const char *str, int must_free)
        LIST_ADDQ(&build_opts_list, &b->list);
 }
 
+/* used to register some initialization functions to call after the checks. */
+void hap_register_post_check(int (*fct)())
+{
+       struct post_check_fct *b;
+
+       b = calloc(1, sizeof(*b));
+       if (!b) {
+               fprintf(stderr, "out of memory\n");
+               exit(1);
+       }
+       b->fct = fct;
+       LIST_ADDQ(&post_check_list, &b->list);
+}
+
 static void display_version()
 {
        printf("HA-Proxy version " HAPROXY_VERSION " " HAPROXY_DATE"\n");
@@ -584,6 +611,7 @@ static void init(int argc, char **argv)
        char *progname;
        char *change_dir = NULL;
        struct proxy *px;
+       struct post_check_fct *pcf;
 
        chunk_init(&trash, malloc(global.tune.bufsize), global.tune.bufsize);
        alloc_trash_buffers(global.tune.bufsize);
@@ -921,6 +949,12 @@ static void init(int argc, char **argv)
        if (start_checks() < 0)
                exit(1);
 
+       list_for_each_entry(pcf, &post_check_list, list) {
+               err_code |= pcf->fct();
+               if (err_code & (ERR_ABORT|ERR_FATAL))
+                       exit(1);
+       }
+
        if (cfg_maxconn > 0)
                global.maxconn = cfg_maxconn;