MEDIUM: Refactor init_check and move to checks.c
Refactor init_check so that an error string is returned
rather than alerts being printed by it. Also
init_check to checks.c and provide a prototype to allow
it to be used from multiple C files.
Signed-off-by: Simon Horman <horms@verge.net.au>
diff --git a/include/proto/checks.h b/include/proto/checks.h
index f3d4fa6..1e65652 100644
--- a/include/proto/checks.h
+++ b/include/proto/checks.h
@@ -44,6 +44,8 @@
return __health_adjust(s, status);
}
+const char *init_check(struct check *check, int type);
+
#endif /* _PROTO_CHECKS_H */
/*
diff --git a/src/checks.c b/src/checks.c
index 321fe34..ae981f8 100644
--- a/src/checks.c
+++ b/src/checks.c
@@ -2781,6 +2781,32 @@
return;
}
+const char *init_check(struct check *check, int type)
+{
+ check->type = type;
+
+ /* Allocate buffer for requests... */
+ if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
+ return "out of memory while allocating check buffer";
+ }
+ check->bi->size = global.tune.chksize;
+
+ /* Allocate buffer for responses... */
+ if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
+ return "out of memory while allocating check buffer";
+ }
+ check->bo->size = global.tune.chksize;
+
+ /* Allocate buffer for partial results... */
+ if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
+ return "out of memory while allocating check connection";
+ }
+
+ check->conn->t.sock.fd = -1; /* no agent in progress yet */
+
+ return NULL;
+}
+
/*
* Local variables:
diff --git a/src/server.c b/src/server.c
index b19ebbe..8554e75 100644
--- a/src/server.c
+++ b/src/server.c
@@ -21,6 +21,7 @@
#include <types/global.h>
+#include <proto/checks.h>
#include <proto/port_range.h>
#include <proto/protocol.h>
#include <proto/queue.h>
@@ -796,35 +797,6 @@
return NULL;
}
-static int init_check(struct check *check, int type, const char * file, int linenum)
-{
- check->type = type;
-
- /* Allocate buffer for requests... */
- if ((check->bi = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
- Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
- return ERR_ALERT | ERR_ABORT;
- }
- check->bi->size = global.tune.chksize;
-
- /* Allocate buffer for responses... */
- if ((check->bo = calloc(sizeof(struct buffer) + global.tune.chksize, sizeof(char))) == NULL) {
- Alert("parsing [%s:%d] : out of memory while allocating check buffer.\n", file, linenum);
- return ERR_ALERT | ERR_ABORT;
- }
- check->bo->size = global.tune.chksize;
-
- /* Allocate buffer for partial results... */
- if ((check->conn = calloc(1, sizeof(struct connection))) == NULL) {
- Alert("parsing [%s:%d] : out of memory while allocating check connection.\n", file, linenum);
- return ERR_ALERT | ERR_ABORT;
- }
-
- check->conn->t.sock.fd = -1; /* no agent in progress yet */
-
- return 0;
-}
-
int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
{
struct server *newsrv = NULL;
@@ -1592,7 +1564,7 @@
}
if (do_check) {
- int ret;
+ const char *ret;
if (newsrv->trackit) {
Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
@@ -1671,9 +1643,10 @@
}
/* note: check type will be set during the config review phase */
- ret = init_check(&newsrv->check, 0, file, linenum);
+ ret = init_check(&newsrv->check, 0);
if (ret) {
- err_code |= ret;
+ Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
+ err_code |= ERR_ALERT | ERR_ABORT;
goto out;
}
@@ -1681,7 +1654,7 @@
}
if (do_agent) {
- int ret;
+ const char *ret;
if (!newsrv->agent.port) {
Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
@@ -1693,9 +1666,10 @@
if (!newsrv->agent.inter)
newsrv->agent.inter = newsrv->check.inter;
- ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK, file, linenum);
+ ret = init_check(&newsrv->agent, PR_O2_LB_AGENT_CHK);
if (ret) {
- err_code |= ret;
+ Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
+ err_code |= ERR_ALERT | ERR_ABORT;
goto out;
}