summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2014-07-23 13:33:15 +0800
committerJeremy Kerr <jk@ozlabs.org>2014-08-05 10:47:04 +0800
commita16685c656a90195867c90ef6f426de353401af8 (patch)
tree371d6ced7ca0e1910e38f316e70254a4afc52ae0
parentddb1a5dddc20c208c1db311ba17e77de109b99cd (diff)
downloadtalos-petitboot-a16685c656a90195867c90ef6f426de353401af8.tar.gz
talos-petitboot-a16685c656a90195867c90ef6f426de353401af8.zip
utils: Add pb-config utility
A simple tool to query platform configuration. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r--utils/Makefile.am5
-rw-r--r--utils/pb-config.c121
2 files changed, 124 insertions, 2 deletions
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 <stdlib.h>
+#include <stdio.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <string.h>
+
+#include <log/log.h>
+#include <pb-config/pb-config.h>
+#include <types/types.h>
+#include <waiter/waiter.h>
+#include <process/process.h>
+#include <talloc/talloc.h>
+
+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 <var>\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;
+}
OpenPOWER on IntegriCloud