From: Alan T. DeKok Date: Thu, 2 Aug 2018 20:20:15 +0000 (-0400) Subject: fix up so that adding intermediate names work X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=167b5cf8dd33a4785805dc9f014cb8dbccf92fd8;p=thirdparty%2Ffreeradius-server.git fix up so that adding intermediate names work all fr_cmd_table_t now have to have `.name` where we want to add a qualifier, e.g. `network X`, the table has to have `.add_name = true,` added to it. and fix `,dup` in network.c && worker.c --- diff --git a/src/lib/io/network.c b/src/lib/io/network.c index e011ee4254b..c6abb441312 100644 --- a/src/lib/io/network.c +++ b/src/lib/io/network.c @@ -1496,13 +1496,13 @@ int fr_network_stats(fr_network_t const *nr, int num, uint64_t *stats) return 5; } -static int cmd_stats_network(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info) +static int cmd_stats_self(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_cmd_info_t const *info) { fr_network_t const *nr = ctx; fprintf(fp, "count.in\t%" PRIu64 "\n", nr->stats.in); fprintf(fp, "count.out\t%" PRIu64 "\n", nr->stats.out); - fprintf(fp, "stats,dup\t%" PRIu64 "\n", nr->stats.dup); + fprintf(fp, "count.dup\t%" PRIu64 "\n", nr->stats.dup); fprintf(fp, "count.dropped\t%" PRIu64 "\n", nr->stats.dropped); fprintf(fp, "count.sockets\t%d\n", rbtree_num_elements(nr->sockets)); @@ -1519,7 +1519,7 @@ static int socket_list(void *ctx, void *data) return 0; } - fprintf(fp, "%s\n", s->listen->app_io->get_name(s->listen->app_io_instance)); + fprintf(fp, "%d\t%s\n", s->number, s->listen->app_io->get_name(s->listen->app_io_instance)); return 0; } @@ -1531,30 +1531,67 @@ static int cmd_socket_list(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_c return 0; } +static int cmd_stats_socket(FILE *fp, FILE *fp_err, void *ctx, fr_cmd_info_t const *info) +{ + fr_network_t const *nr = ctx; + fr_network_socket_t *s, my_s; + + my_s.number = info->box[0]->vb_uint32; + + s = rbtree_finddata(nr->sockets_by_num, &my_s); + if (!s) { + fprintf(fp_err, "No such socket number '%s'.\n", info->argv[0]); + return -1; + } + + fprintf(fp, "count.in\t%" PRIu64 "\n", s->stats.in); + fprintf(fp, "count.out\t%" PRIu64 "\n", s->stats.out); + fprintf(fp, "count.dup\t%" PRIu64 "\n", s->stats.dup); + fprintf(fp, "count.dropped\t%" PRIu64 "\n", s->stats.dropped); + + return 0; +} + + fr_cmd_table_t cmd_network_table[] = { { - .parent = "stats network", + .parent = "stats", + .name = "network", .help = "Statistics for network threads.", .read_only = true }, { .parent = "stats network", - .syntax = "self", - .func = cmd_stats_network, + .add_name = true, + .name = "self", + .func = cmd_stats_self, .help = "Show statistics for a specific network thread.", .read_only = true }, { - .parent = "show network", + .parent = "stats network", + .add_name = true, + .name = "socket", + .syntax = "INTEGER", + .func = cmd_stats_socket, + .help = "Show statistics for a specific socket", + .read_only = true + }, + + { + .parent = "show", + .name = "network", .help = "Show information about network threads.", .read_only = true }, { .parent = "show network", - .syntax = "socket list", + .add_name = true, + .name = "socket", + .syntax = "list", .func = cmd_socket_list, .help = "List the sockets associated with this network thread.", .read_only = true diff --git a/src/lib/io/worker.c b/src/lib/io/worker.c index 0dc1891b121..46b3be0c23f 100644 --- a/src/lib/io/worker.c +++ b/src/lib/io/worker.c @@ -1631,7 +1631,7 @@ static int cmd_stats_worker(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_ fprintf(fp, "count.in\t%" PRIu64 "\n", worker->stats.in); fprintf(fp, "count.out\t%" PRIu64 "\n", worker->stats.out); - fprintf(fp, "stats,dup\t%" PRIu64 "\n", worker->stats.dup); + fprintf(fp, "count.dup\t%" PRIu64 "\n", worker->stats.dup); fprintf(fp, "count.dropped\t%" PRIu64 "\n", worker->stats.dropped); fprintf(fp, "count.decoded\t%" PRIu64 "\n", worker->num_decoded); fprintf(fp, "count.timeouts\t%" PRIu64 "\n", worker->num_timeouts); @@ -1652,13 +1652,16 @@ static int cmd_stats_worker(FILE *fp, UNUSED FILE *fp_err, void *ctx, UNUSED fr_ fr_cmd_table_t cmd_worker_table[] = { { - .parent = "stats worker", + .parent = "stats", + .name = "worker", .help = "Statistics for workers threads.", .read_only = true }, { .parent = "stats worker", + .add_name = true, + .name = "self", .func = cmd_stats_worker, .help = "Show statistics for a specific worker thread.", .read_only = true diff --git a/src/lib/server/command.c b/src/lib/server/command.c index 75fbdc9d5eb..f7a9e1dae83 100644 --- a/src/lib/server/command.c +++ b/src/lib/server/command.c @@ -702,12 +702,17 @@ int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, vo int argc = 0, depth = 0; fr_cmd_argv_t *syntax_argv; - if (name && !fr_command_valid_name(name)) { + if (!table->name) { + fr_strerror_printf("A name MUST be specified for table with parent %s syntax %s", table->parent, table->syntax); + return -1; + } + + if (!name && table->add_name) { + fr_strerror_printf("An additional name must be specified"); return -1; } - if (!name && !table->name) { - fr_strerror_printf("A name MUST be specified"); + if (name && !fr_command_valid_name(name)) { return -1; } @@ -763,6 +768,19 @@ int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, vo depth = i; } + /* + * Add an intermediate name, e.g. "network X" + */ + if (table->add_name) { + cmd = fr_command_find(start, name, &insert); + if (!cmd) { + cmd = fr_command_alloc(talloc_ctx, insert, name); + } + + start = &(cmd->child); + depth++; + } + /* * @todo - check syntax, too! * @@ -807,18 +825,12 @@ int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, vo } } - /* - * "name" is used only when the table doesn't specify a name. - */ - if (table->name) name = table->name; - /* * "head" is now pointing to the list where we insert * this new command. We now see if the "name" currently * exists. */ - - cmd = fr_command_find(start, name, &insert); + cmd = fr_command_find(start, table->name, &insert); /* * The command exists already. We can't have TWO @@ -838,8 +850,8 @@ int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, vo if (cmd->help == table->help) return 0; if (cmd->help != NULL) { - fr_strerror_printf("Cannot change help for command %s %s", - name , cmd->name); + fr_strerror_printf("Cannot change help for command %s", + cmd->name); return -1; } rad_assert(cmd->intermediate); @@ -862,7 +874,7 @@ int fr_command_add(TALLOC_CTX *talloc_ctx, fr_cmd_t **head, char const *name, vo * Allocate cmd and insert it into the current point. */ rad_assert(insert != NULL); - cmd = fr_command_alloc(talloc_ctx, insert, name); + cmd = fr_command_alloc(talloc_ctx, insert, table->name); } /* diff --git a/src/lib/server/command.h b/src/lib/server/command.h index 1a9fae41926..18b52eae3f1 100644 --- a/src/lib/server/command.h +++ b/src/lib/server/command.h @@ -56,6 +56,7 @@ typedef struct fr_cmd_table_t { fr_cmd_func_t func; //!< function to process this command fr_cmd_tab_t tab_expand; //!< tab expand things in the syntax string bool read_only; + bool add_name; //!< do we add a name here? } fr_cmd_table_t; #define CMD_TABLE_END { .help = NULL }