CLEANUP: mworker: remove the type field in mworker_proc

Since the introduction of the options field, we can use it to store the
type of process.

type = 'm' is replaced by PROC_O_TYPE_MASTER
type = 'w' is replaced by PROC_O_TYPE_WORKER
type = 'e' is replaced by PROC_O_TYPE_PROG

The old values are still used in the HAPROXY_PROCESSES environment
variable to pass the information during a reload.
This commit is contained in:
William Lallemand 2019-04-12 16:09:23 +02:00 committed by Willy Tarreau
parent bd3de3efb7
commit 8f7069a389
5 changed files with 43 additions and 27 deletions

View File

@ -195,8 +195,6 @@ struct global {
*/
struct mworker_proc {
int pid;
char type; /* m(aster), w(orker) */
/* 3 bytes hole here */
int options;
char *id;
char **command;

View File

@ -1789,7 +1789,7 @@ static int pcli_prefix_to_pid(const char *prefix)
if (*errtol != '\0')
return -1;
list_for_each_entry(child, &proc_list, list) {
if (child->type != 'w')
if (!(child->options & PROC_O_TYPE_WORKER))
continue;
if (child->pid == proc_pid){
return child->pid;
@ -1809,7 +1809,7 @@ static int pcli_prefix_to_pid(const char *prefix)
/* chose the right process, the current one is the one with the
least number of reloads */
list_for_each_entry(child, &proc_list, list) {
if (child->type != 'w')
if (!(child->options & PROC_O_TYPE_WORKER))
continue;
if (child->relative_pid == proc_pid){
if (child->reloads == 0)

View File

@ -608,7 +608,7 @@ void mworker_reload()
next_argv[next_argc++] = "-sf";
list_for_each_entry(child, &proc_list, list) {
if (child->type != 'w' && child->type != 'e')
if (!(child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)))
continue;
next_argv[next_argc] = memprintf(&msg, "%d", child->pid);
if (next_argv[next_argc] == NULL)
@ -1601,7 +1601,7 @@ static void init(int argc, char **argv)
ha_alert("Cannot allocate process structures.\n");
exit(EXIT_FAILURE);
}
tmproc->type = 'm'; /* master */
tmproc->options |= PROC_O_TYPE_MASTER; /* master */
tmproc->reloads = 0;
tmproc->relative_pid = 0;
tmproc->pid = pid;
@ -1622,7 +1622,7 @@ static void init(int argc, char **argv)
exit(EXIT_FAILURE);
}
tmproc->type = 'w'; /* worker */
tmproc->options |= PROC_O_TYPE_WORKER; /* worker */
tmproc->pid = -1;
tmproc->reloads = 0;
tmproc->timestamp = -1;
@ -2864,7 +2864,7 @@ int main(int argc, char **argv)
/* find the right mworker_proc */
list_for_each_entry(child, &proc_list, list) {
if (child->relative_pid == relative_pid &&
child->reloads == 0 && child->type == 'w') {
child->reloads == 0 && child->options & PROC_O_TYPE_WORKER) {
child->timestamp = now.tv_sec;
child->pid = ret;
break;

View File

@ -45,7 +45,7 @@ int mworker_ext_launch_all()
/* find the right mworker_proc */
list_for_each_entry_safe(child, tmp, &proc_list, list) {
if (child->reloads == 0 && child->type == 'e') {
if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
if (reexec && (!(child->options & PROC_O_START_RELOAD))) {
struct mworker_proc *old_child;
@ -60,7 +60,7 @@ int mworker_ext_launch_all()
*/
list_for_each_entry(old_child, &proc_list, list) {
if (old_child->type != 'e' || (!(old_child->options & PROC_O_LEAVING)))
if (!(old_child->options & PROC_O_TYPE_PROG) || (!(old_child->options & PROC_O_LEAVING)))
continue;
if (!strcmp(old_child->id, child->id))
@ -149,7 +149,7 @@ int cfg_parse_program(const char *file, int linenum, char **args, int kwm)
goto error;
}
ext_child->type = 'e'; /* external process */
ext_child->options |= PROC_O_TYPE_PROG; /* external process */
ext_child->command = NULL;
ext_child->path = NULL;
ext_child->id = NULL;
@ -163,7 +163,7 @@ int cfg_parse_program(const char *file, int linenum, char **args, int kwm)
LIST_INIT(&ext_child->list);
list_for_each_entry(child, &proc_list, list) {
if (child->reloads == 0 && child->type == 'e') {
if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
if (!strcmp(args[1], child->id)) {
ha_alert("parsing [%s:%d]: '%s' program section already exists in the configuration.\n", file, linenum, args[1]);
err_code |= ERR_ALERT | ERR_ABORT;
@ -279,7 +279,7 @@ int cfg_program_postparser()
struct mworker_proc *child;
list_for_each_entry(child, &proc_list, list) {
if (child->reloads == 0 && child->type == 'e') {
if (child->reloads == 0 && (child->options & PROC_O_TYPE_PROG)) {
if (child->command == NULL) {
ha_alert("The program section '%s' lacks a command to launch.\n", child->id);
err_code |= ERR_ALERT | ERR_FATAL;

View File

@ -54,7 +54,7 @@ static void mworker_kill(int sig)
list_for_each_entry(child, &proc_list, list) {
/* careful there, we must be sure that the pid > 0, we don't want to emit a kill -1 */
if ((child->type == 'w' || child->type == 'e') && (child->reloads == 0) && (child->pid > 0))
if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (child->reloads == 0) && (child->pid > 0))
kill(child->pid, sig);
}
}
@ -66,7 +66,7 @@ int mworker_current_child(int pid)
struct mworker_proc *child;
list_for_each_entry(child, &proc_list, list) {
if ((child->type == 'w' || child->type == 'e') && (!(child->options & PROC_O_LEAVING)) && (child->pid == pid))
if ((child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG)) && (!(child->options & PROC_O_LEAVING)) && (child->pid == pid))
return 1;
}
return 0;
@ -82,7 +82,7 @@ int mworker_child_nb()
int ret = 0;
list_for_each_entry(child, &proc_list, list) {
if ((child->type == 'w' || child->type == 'e'))
if (child->options & (PROC_O_TYPE_WORKER|PROC_O_TYPE_PROG))
ret++;
}
@ -99,8 +99,17 @@ void mworker_proc_list_to_env()
struct mworker_proc *child;
list_for_each_entry(child, &proc_list, list) {
char type = '?';
if (child->options & PROC_O_TYPE_MASTER)
type = 'm';
else if (child->options & PROC_O_TYPE_PROG)
type = 'e';
else if (child->options &= PROC_O_TYPE_WORKER)
type = 'w';
if (child->pid > -1)
memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s", msg ? msg : "", child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "");
memprintf(&msg, "%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d;id=%s", msg ? msg : "", type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, child->timestamp, child->id ? child->id : "");
}
if (msg)
setenv("HAPROXY_PROCESSES", msg, 1);
@ -131,9 +140,18 @@ void mworker_env_to_proc_list()
token = NULL;
if (strncmp(subtoken, "type=", 5) == 0) {
child->type = *(subtoken+5);
if (child->type == 'm') /* we are in the master, assign it */
char type;
type = *(subtoken+5);
if (type == 'm') { /* we are in the master, assign it */
proc_self = child;
child->options |= PROC_O_TYPE_MASTER;
} else if (type == 'e') {
child->options |= PROC_O_TYPE_PROG;
} else if (type == 'w') {
child->options |= PROC_O_TYPE_WORKER;
}
} else if (strncmp(subtoken, "fd=", 3) == 0) {
child->ipc_fd[0] = atoi(subtoken+3);
} else if (strncmp(subtoken, "pid=", 4) == 0) {
@ -249,9 +267,9 @@ void mworker_catch_sigchld(struct sig_handler *sh)
} else {
/* check if exited child is a current child */
if (!(child->options & PROC_O_LEAVING)) {
if (child->type == 'w')
if (child->options & PROC_O_TYPE_WORKER)
ha_alert("Current worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
else if (child->type == 'e')
else if (child->options & PROC_O_TYPE_PROG)
ha_alert("Current program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
if (status != 0 && status != 130 && status != 143
@ -262,10 +280,10 @@ void mworker_catch_sigchld(struct sig_handler *sh)
mworker_kill(SIGTERM);
}
} else {
if (child->type == 'w') {
if (child->options & PROC_O_TYPE_WORKER) {
ha_warning("Former worker #%d (%d) exited with code %d (%s)\n", child->relative_pid, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
delete_oldpid(exitpid);
} else if (child->type == 'e') {
} else if (child->options & PROC_O_TYPE_PROG) {
ha_warning("Former program '%s' (%d) exited with code %d (%s)\n", child->id, exitpid, status, (status >= 128) ? strsignal(status - 128) : "Exit");
}
}
@ -414,7 +432,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
list_for_each_entry(child, &proc_list, list) {
up = now.tv_sec - child->timestamp;
if (child->type != 'w')
if (!(child->options & PROC_O_TYPE_WORKER))
continue;
if (child->options & PROC_O_LEAVING) {
@ -433,7 +451,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
list_for_each_entry(child, &proc_list, list) {
up = now.tv_sec - child->timestamp;
if (child->type != 'w')
if (!(child->options & PROC_O_TYPE_WORKER))
continue;
if (child->options & PROC_O_LEAVING) {
@ -450,7 +468,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
list_for_each_entry(child, &proc_list, list) {
up = now.tv_sec - child->timestamp;
if (child->type != 'e')
if (!(child->options & PROC_O_TYPE_PROG))
continue;
if (child->options & PROC_O_LEAVING) {
@ -465,7 +483,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
list_for_each_entry(child, &proc_list, list) {
up = now.tv_sec - child->timestamp;
if (child->type != 'e')
if (!(child->options & PROC_O_TYPE_PROG))
continue;
if (child->options & PROC_O_LEAVING) {