]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: make smp_resolve_args() return an allocate error message
authorWilly Tarreau <w@1wt.eu>
Fri, 26 Mar 2021 15:11:55 +0000 (16:11 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 26 Mar 2021 15:23:45 +0000 (16:23 +0100)
For now smp_resolve_args() complains on stderr via ha_alert(), but if we
want to make it a bit more dynamic, we need it to return errors in an
allocated message. Let's pass it an error pointer and have it fill it.
On return we indent the output if it contains more than one line.

include/haproxy/sample.h
src/cfgparse.c
src/sample.c

index e5378b03cd689c87fb2ab7f79c80c80d8e090745..aac6efa603beb13fe1a127c9c573d9cb81da9819 100644 (file)
@@ -48,7 +48,7 @@ const char *sample_ckp_names(unsigned int use);
 struct sample_fetch *find_sample_fetch(const char *kw, int len);
 struct sample_fetch *sample_fetch_getnext(struct sample_fetch *current, int *idx);
 struct sample_conv *sample_conv_getnext(struct sample_conv *current, int *idx);
-int smp_resolve_args(struct proxy *p);
+int smp_resolve_args(struct proxy *p, char **err);
 int smp_check_date_unit(struct arg *args, char **err);
 int smp_expr_output_type(struct sample_expr *expr);
 int c_none(struct sample *smp);
index a718cd6149fb9aef8b5605e0f737d78d9bc2906f..2a0e98bcf3ed6ba169c2d1ce68f069584ef1960c 100644 (file)
@@ -2742,8 +2742,14 @@ out_uri_auth_compat:
                /* only now we can check if some args remain unresolved.
                 * This must be done after the users and groups resolution.
                 */
-               cfgerr += smp_resolve_args(curproxy);
-               if (!cfgerr)
+               err = NULL;
+               i = smp_resolve_args(curproxy, &err);
+               cfgerr += i;
+               if (i) {
+                       indent_msg(&err, 8);
+                       ha_alert("%s%s\n", i > 1 ? "multiple argument resolution errors:" : "", err);
+                       ha_free(&err);
+               } else
                        cfgerr += acl_find_targets(curproxy);
 
                if ((curproxy->mode == PR_MODE_TCP || curproxy->mode == PR_MODE_HTTP) &&
index 81c96bb7661922ee43eebe5bcc4595557328b9a6..b05259bbb73fe2902ea45fc3af7747b49c9108a0 100644 (file)
@@ -1090,9 +1090,10 @@ struct sample *sample_process(struct proxy *px, struct session *sess,
 
 /*
  * Resolve all remaining arguments in proxy <p>. Returns the number of
- * errors or 0 if everything is fine.
+ * errors or 0 if everything is fine. If at least one error is met, it will
+ * be appended to *err. If *err==NULL it will be allocated first.
  */
-int smp_resolve_args(struct proxy *p)
+int smp_resolve_args(struct proxy *p, char **err)
 {
        struct arg_list *cur, *bak;
        const char *ctx, *where;
@@ -1108,7 +1109,7 @@ int smp_resolve_args(struct proxy *p)
                struct server *srv;
                struct stktable *t;
                char *pname, *sname, *stktname;
-               char *err;
+               char *err2;
 
                arg = cur->arg;
 
@@ -1146,8 +1147,8 @@ int smp_resolve_args(struct proxy *p)
                switch (arg->type) {
                case ARGT_SRV:
                        if (!arg->data.str.data) {
-                               ha_alert("parsing [%s:%d] : missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line,
+                               memprintf(err, "%sparsing [%s:%d]: missing server name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                continue;
@@ -1162,8 +1163,8 @@ int smp_resolve_args(struct proxy *p)
 
                                px = proxy_be_by_name(pname);
                                if (!px) {
-                                       ha_alert("parsing [%s:%d] : unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                                cur->file, cur->line, pname,
+                                       memprintf(err, "%sparsing [%s:%d]: unable to find proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                                *err ? *err : "", cur->file, cur->line, pname,
                                                 cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                        cfgerr++;
                                        break;
@@ -1174,8 +1175,8 @@ int smp_resolve_args(struct proxy *p)
 
                        srv = findserver(px, sname);
                        if (!srv) {
-                               ha_alert("parsing [%s:%d] : unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line, sname, pname,
+                               memprintf(err, "%sparsing [%s:%d]: unable to find server '%s' in proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, sname, pname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
@@ -1193,16 +1194,16 @@ int smp_resolve_args(struct proxy *p)
                        }
 
                        if (!px) {
-                               ha_alert("parsing [%s:%d] : unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line, pname,
+                               memprintf(err, "%sparsing [%s:%d]: unable to find frontend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, pname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
                        }
 
                        if (!(px->cap & PR_CAP_FE)) {
-                               ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
-                                        cur->file, cur->line, pname,
+                               memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not frontend capability.\n",
+                                        *err ? *err : "", cur->file, cur->line, pname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
@@ -1220,16 +1221,16 @@ int smp_resolve_args(struct proxy *p)
                        }
 
                        if (!px) {
-                               ha_alert("parsing [%s:%d] : unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line, pname,
+                               memprintf(err, "%sparsing [%s:%d]: unable to find backend '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, pname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
                        }
 
                        if (!(px->cap & PR_CAP_BE)) {
-                               ha_alert("parsing [%s:%d] : proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
-                                        cur->file, cur->line, pname,
+                               memprintf(err, "%sparsing [%s:%d]: proxy '%s', referenced in arg %d of %s%s%s%s '%s' %s proxy '%s', has not backend capability.\n",
+                                        *err ? *err : "", cur->file, cur->line, pname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
@@ -1248,24 +1249,24 @@ int smp_resolve_args(struct proxy *p)
 
                        t = stktable_find_by_name(stktname);
                        if (!t) {
-                               ha_alert("parsing [%s:%d] : unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line, stktname,
+                               memprintf(err, "%sparsing [%s:%d]: unable to find table '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, stktname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
                        }
 
                        if (!t->size) {
-                               ha_alert("parsing [%s:%d] : no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line, stktname,
+                               memprintf(err, "%sparsing [%s:%d]: no table in proxy '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, stktname,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
                        }
 
                        if (t->proxy && (p->bind_proc & ~t->proxy->bind_proc)) {
-                               ha_alert("parsing [%s:%d] : stick-table '%s' not present on all processes covered by proxy '%s'.\n",
-                                        cur->file, cur->line, t->proxy->id, p->id);
+                               memprintf(err, "%sparsing [%s:%d]: stick-table '%s' not present on all processes covered by proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line, t->proxy->id, p->id);
                                cfgerr++;
                                break;
                        }
@@ -1282,8 +1283,8 @@ int smp_resolve_args(struct proxy *p)
 
                case ARGT_USR:
                        if (!arg->data.str.data) {
-                               ha_alert("parsing [%s:%d] : missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line,
+                               memprintf(err, "%sparsing [%s:%d]: missing userlist name in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                break;
@@ -1296,8 +1297,8 @@ int smp_resolve_args(struct proxy *p)
                                ul = auth_find_userlist(arg->data.str.area);
 
                        if (!ul) {
-                               ha_alert("parsing [%s:%d] : unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line,
+                               memprintf(err, "%sparsing [%s:%d]: unable to find userlist '%s' referenced in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line,
                                         arg->data.str.area,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
@@ -1311,8 +1312,8 @@ int smp_resolve_args(struct proxy *p)
 
                case ARGT_REG:
                        if (!arg->data.str.data) {
-                               ha_alert("parsing [%s:%d] : missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
-                                        cur->file, cur->line,
+                               memprintf(err, "%sparsing [%s:%d]: missing regex in arg %d of %s%s%s%s '%s' %s proxy '%s'.\n",
+                                        *err ? *err : "", cur->file, cur->line,
                                         cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id);
                                cfgerr++;
                                continue;
@@ -1320,13 +1321,13 @@ int smp_resolve_args(struct proxy *p)
 
                        rflags = 0;
                        rflags |= (arg->type_flags & ARGF_REG_ICASE) ? REG_ICASE : 0;
-                       err = NULL;
+                       err2 = NULL;
 
-                       if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err))) {
-                               ha_alert("parsing [%s:%d] : error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
-                                        cur->file, cur->line,
+                       if (!(reg = regex_comp(arg->data.str.area, !(rflags & REG_ICASE), 1 /* capture substr */, &err2))) {
+                               memprintf(err, "%sparsing [%s:%d]: error in regex '%s' in arg %d of %s%s%s%s '%s' %s proxy '%s' : %s.\n",
+                                         *err ? *err : "", cur->file, cur->line,
                                         arg->data.str.area,
-                                        cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err);
+                                        cur->arg_pos + 1, conv_pre, conv_ctx, conv_pos, ctx, cur->kw, where, p->id, err2);
                                cfgerr++;
                                continue;
                        }