From b1900d55dfe0211e9ef5c43de23fa63ab32c5674 Mon Sep 17 00:00:00 2001 From: Simon Horman Date: Fri, 30 Jan 2015 11:22:54 +0900 Subject: [PATCH] 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 --- include/proto/checks.h | 2 ++ src/checks.c | 26 +++++++++++++++++++++++++ src/server.c | 44 +++++++++--------------------------------- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/include/proto/checks.h b/include/proto/checks.h index f3d4fa6ed..1e65652eb 100644 --- a/include/proto/checks.h +++ b/include/proto/checks.h @@ -44,6 +44,8 @@ static inline void health_adjust(struct server *s, short status) 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 321fe3415..ae981f8fb 100644 --- a/src/checks.c +++ b/src/checks.c @@ -2781,6 +2781,32 @@ static void tcpcheck_main(struct connection *conn) 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 b19ebbeca..8554e759d 100644 --- a/src/server.c +++ b/src/server.c @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -796,35 +797,6 @@ const char *server_parse_weight_change_request(struct server *sv, 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 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr } 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 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr } /* 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 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr } 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 @@ int parse_server(const char *file, int linenum, char **args, struct proxy *curpr 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; }