From a16685c656a90195867c90ef6f426de353401af8 Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Wed, 23 Jul 2014 13:33:15 +0800 Subject: [PATCH 1/1] utils: Add pb-config utility A simple tool to query platform configuration. Signed-off-by: Jeremy Kerr --- utils/Makefile.am | 5 +- utils/pb-config.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 utils/pb-config.c diff --git a/utils/Makefile.am b/utils/Makefile.am index 7c92f20..fde6e55 100644 --- a/utils/Makefile.am +++ b/utils/Makefile.am @@ -14,9 +14,10 @@ dist_sbin_SCRIPTS += utils/pb-udhcpc dist_pkglibexec_SCRIPTS = utils/pb-console utils/pb-sysinfo -sbin_PROGRAMS += utils/pb-event +sbin_PROGRAMS += utils/pb-event utils/pb-config -utils_pb_event_SOURCES = utils/pb-event.c +utils_pb_config_LDADD = $(top_builddir)/lib/libpbcore.la \ + $(top_builddir)/discover/platform.ro dist_pkgdata_DATA = \ utils/kboot.conf.sample \ diff --git a/utils/pb-config.c b/utils/pb-config.c new file mode 100644 index 0000000..d05538b --- /dev/null +++ b/utils/pb-config.c @@ -0,0 +1,121 @@ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +extern struct config *config_get(void); +extern int platform_init(void); + +static const struct option options[] = { + { + .name = "list", + .val = 'l', + }, + { 0 }, +}; + +static void usage(const char *progname) +{ + fprintf(stderr, "Usage:\t%1$s \n" + "\t%1$s --list\n", progname); +} + +static void print_one_config(void *ctx, const char *req, const char *name, + const char *fmt, ...) +{ + bool use_prefix = !req; + char *val, *sep; + va_list ap; + + if (req && strcmp(req, name)) + return; + + va_start(ap, fmt); + val = talloc_vasprintf(ctx, fmt, ap); + va_end(ap); + + if (!strcmp(val, "(null)")) { + talloc_free(val); + if (!use_prefix) + return; + val = talloc_strdup(ctx, ""); + } + + sep = use_prefix ? ": " : ""; + + printf("%s%s%s\n", use_prefix ? name : "", sep, val); + + talloc_free(val); +} + +static void print_config(void *ctx, struct config *config, const char *var) +{ + print_one_config(ctx, var, "bootdev", "%s", config->boot_device); + print_one_config(ctx, var, "autoboot", "%s", + config->autoboot_enabled ? "enabled" : "disabled"); + print_one_config(ctx, var, "timeout", "%d", + config->autoboot_timeout_sec); + print_one_config(ctx, var, "safe-mode", "%s", + config->safe_mode ? "enabled" : "disabled"); +} + +int main(int argc, char **argv) +{ + struct waitset *waitset; + struct config *config; + void *ctx; + bool list; + + list = false; + + for (;;) { + int opt = getopt_long(argc, argv, "l", options, NULL); + if (opt == -1) + break; + + switch (opt) { + case 'l': + list = true; + break; + default: + usage(argv[0]); + return EXIT_FAILURE; + } + } + + ctx = talloc_new(NULL); + + waitset = waitset_create(ctx); + + process_init(ctx, waitset, false); + + platform_init(); + + pb_log_init(stderr); + + config = config_get(); + + if (list) { + print_config(ctx, config, NULL); + } else { + int i; + + for (i = optind; i < argc; i++) + print_config(ctx, config, argv[i]); + } + + talloc_free(ctx); + + return EXIT_SUCCESS; +} -- 2.39.2