diff --git a/include/proto/mworker.h b/include/proto/mworker.h index 2386a445a..041878266 100644 --- a/include/proto/mworker.h +++ b/include/proto/mworker.h @@ -36,4 +36,6 @@ int mworker_ext_launch_all(); void mworker_kill_max_reloads(int sig); +void mworker_free_child(struct mworker_proc *); + #endif /* PROTO_MWORKER_H_ */ diff --git a/src/haproxy.c b/src/haproxy.c index f5d72e654..923b92327 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -3035,7 +3035,8 @@ int main(int argc, char **argv) continue; } LIST_DEL(&child->list); - free(child); + mworker_free_child(child); + child = NULL; } } diff --git a/src/mworker-prog.c b/src/mworker-prog.c index fd8e66384..467ce9b24 100644 --- a/src/mworker-prog.c +++ b/src/mworker-prog.c @@ -69,24 +69,7 @@ int mworker_ext_launch_all() LIST_DEL(&child->list); - if (child->command) { - int i; - - for (i = 0; child->command[i]; i++) { - if (child->command[i]) { - free(child->command[i]); - child->command[i] = NULL; - } - } - free(child->command); - child->command = NULL; - } - if (child->id) { - free(child->id); - child->id = NULL; - } - - free(child); + mworker_free_child(child); child = NULL; continue; diff --git a/src/mworker.c b/src/mworker.c index d5040b759..728a3a149 100644 --- a/src/mworker.c +++ b/src/mworker.c @@ -185,9 +185,7 @@ void mworker_env_to_proc_list() LIST_ADDQ(&proc_list, &child->list); } else { - free(child->id); - free(child); - + mworker_free_child(child); } } @@ -245,7 +243,6 @@ void mworker_catch_sigchld(struct sig_handler *sh) { int exitpid = -1; int status = 0; - struct mworker_proc *child, *it; int childfound; restart_wait: @@ -254,6 +251,8 @@ void mworker_catch_sigchld(struct sig_handler *sh) exitpid = waitpid(-1, &status, WNOHANG); if (exitpid > 0) { + struct mworker_proc *child, *it; + if (WIFEXITED(status)) status = WEXITSTATUS(status); else if (WIFSIGNALED(status)) @@ -301,7 +300,8 @@ void mworker_catch_sigchld(struct sig_handler *sh) ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit"); } } - free(child); + mworker_free_child(child); + child = NULL; } /* do it again to check if it was the last worker */ @@ -561,6 +561,29 @@ static int mworker_parse_global_max_reloads(char **args, int section_type, struc return err_code; } +void mworker_free_child(struct mworker_proc *child) +{ + if (child == NULL) + return; + + if (child->command) { + int i; + + for (i = 0; child->command[i]; i++) { + if (child->command[i]) { + free(child->command[i]); + child->command[i] = NULL; + } + } + free(child->command); + child->command = NULL; + } + if (child->id) { + free(child->id); + child->id = NULL; + } + free(child); +} static struct cfg_kw_list mworker_kws = {{ }, { { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },