From 88dc7c5de9b9a05220fd737d76f1fbe75b67405e Mon Sep 17 00:00:00 2001 From: William Lallemand Date: Mon, 1 Apr 2019 11:30:01 +0200 Subject: [PATCH] REORG: mworker/cli: move CLI functions to mworker.c Move the CLI functions of the master worker to mworker.c --- src/cli.c | 75 ------------------------------------- src/mworker.c | 101 +++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 96 insertions(+), 80 deletions(-) diff --git a/src/cli.c b/src/cli.c index def5ebacc..38c178dc2 100644 --- a/src/cli.c +++ b/src/cli.c @@ -1324,16 +1324,6 @@ static int cli_parse_set_lvl(char **args, char *payload, struct appctx *appctx, return 1; } -/* reload the master process */ -static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private) -{ - if (!cli_has_level(appctx, ACCESS_LVL_OPER)) - return 1; - - mworker_reload(); - - return 1; -} int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private) { @@ -1461,66 +1451,6 @@ static int bind_parse_severity_output(char **args, int cur_arg, struct proxy *px } - /* Displays workers and processes */ -static int cli_io_handler_show_proc(struct appctx *appctx) -{ - struct stream_interface *si = appctx->owner; - struct mworker_proc *child; - int old = 0; - int up = now.tv_sec - proc_self->timestamp; - - if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) - return 1; - - chunk_reset(&trash); - - chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "", "", "", "", ""); - chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); - - /* displays current processes */ - - chunk_appendf(&trash, "# workers\n"); - list_for_each_entry(child, &proc_list, list) { - up = now.tv_sec - child->timestamp; - - if (child->type != 'w') - continue; - - if (child->reloads > 0) { - old++; - continue; - } - chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); - } - - /* displays old processes */ - - if (old) { - char *msg = NULL; - - chunk_appendf(&trash, "# old workers\n"); - list_for_each_entry(child, &proc_list, list) { - up = now.tv_sec - child->timestamp; - - if (child->type != 'w') - continue; - - if (child->reloads > 0) { - memprintf(&msg, "[was: %u]", child->relative_pid); - chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", msg, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); - } - } - free(msg); - } - - if (ci_putchk(si_ic(si), &trash) == -1) { - si_rx_room_blk(si); - return 0; - } - - /* dump complete */ - return 1; -} /* Send all the bound sockets, always returns 1 */ static int _getsocks(char **args, char *payload, struct appctx *appctx, void *private) @@ -2615,9 +2545,6 @@ static struct applet cli_applet = { /* register cli keywords */ static struct cli_kw_list cli_kws = {{ },{ - { { "@", NULL }, "@ : send a command to the process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, - { { "@!", NULL }, "@! : send a command to the process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, - { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, { { "help", NULL }, NULL, cli_parse_simple, NULL }, { { "prompt", NULL }, NULL, cli_parse_simple, NULL }, { { "quit", NULL }, NULL, cli_parse_simple, NULL }, @@ -2630,10 +2557,8 @@ static struct cli_kw_list cli_kws = {{ },{ { { "show", "cli", "level", NULL }, "show cli level : display the level of the current CLI session", cli_parse_show_lvl, NULL, NULL, NULL, ACCESS_MASTER}, { { "show", "fd", NULL }, "show fd [num] : dump list of file descriptors in use", cli_parse_show_fd, cli_io_handler_show_fd, NULL }, { { "show", "activity", NULL }, "show activity : show per-thread activity stats (for support/developers)", cli_parse_default, cli_io_handler_show_activity, NULL }, - { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, { { "operator", NULL }, "operator : lower the level of the current CLI session to operator", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER}, { { "user", NULL }, "user : lower the level of the current CLI session to user", cli_parse_set_lvl, NULL, NULL, NULL, ACCESS_MASTER}, - { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, { { "_getsocks", NULL }, NULL, _getsocks, NULL }, {{},} }}; diff --git a/src/mworker.c b/src/mworker.c index 0c4ae21b2..d92358d33 100644 --- a/src/mworker.c +++ b/src/mworker.c @@ -17,18 +17,24 @@ #include #include +#include #include +#include +#include +#include +#include + +#include #include #include #include #include -#include - -#include -#include #include -#include +#include +#include +#include + #if defined(USE_SYSTEMD) #include @@ -374,3 +380,88 @@ void mworker_cleanlisteners() curproxy->state = PR_STSTOPPED; } } + +/* Displays workers and processes */ +static int cli_io_handler_show_proc(struct appctx *appctx) +{ + struct stream_interface *si = appctx->owner; + struct mworker_proc *child; + int old = 0; + int up = now.tv_sec - proc_self->timestamp; + + if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) + return 1; + + chunk_reset(&trash); + + chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "", "", "", "", ""); + chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); + + /* displays current processes */ + + chunk_appendf(&trash, "# workers\n"); + list_for_each_entry(child, &proc_list, list) { + up = now.tv_sec - child->timestamp; + + if (child->type != 'w') + continue; + + if (child->reloads > 0) { + old++; + continue; + } + chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); + } + + /* displays old processes */ + + if (old) { + char *msg = NULL; + + chunk_appendf(&trash, "# old workers\n"); + list_for_each_entry(child, &proc_list, list) { + up = now.tv_sec - child->timestamp; + + if (child->type != 'w') + continue; + + if (child->reloads > 0) { + memprintf(&msg, "[was: %u]", child->relative_pid); + chunk_appendf(&trash, "%-15u %-15s %-15s %-15d %dd %02dh%02dm%02ds\n", child->pid, "worker", msg, child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); + } + } + free(msg); + } + + if (ci_putchk(si_ic(si), &trash) == -1) { + si_rx_room_blk(si); + return 0; + } + + /* dump complete */ + return 1; +} + +/* reload the master process */ +static int cli_parse_reload(char **args, char *payload, struct appctx *appctx, void *private) +{ + if (!cli_has_level(appctx, ACCESS_LVL_OPER)) + return 1; + + mworker_reload(); + + return 1; +} + + +/* register cli keywords */ +static struct cli_kw_list cli_kws = {{ },{ + { { "@", NULL }, "@ : send a command to the process", NULL, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, + { { "@!", NULL }, "@! : send a command to the process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, + { { "@master", NULL }, "@master : send a command to the master process", cli_parse_default, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, + { { "show", "proc", NULL }, "show proc : show processes status", cli_parse_default, cli_io_handler_show_proc, NULL, NULL, ACCESS_MASTER_ONLY}, + { { "reload", NULL }, "reload : reload haproxy", cli_parse_reload, NULL, NULL, NULL, ACCESS_MASTER_ONLY}, + {{},} +}}; + +INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);