summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-01-19 19:57:56 +0100
committerLennart Poettering <lennart@poettering.net>2021-01-20 17:50:23 +0100
commit8662fcbcf1f31725998bd1ebe706ac813b053962 (patch)
treeef419ce44d80c09be06e799481ec19da0d30fa9b
parent9901835d80adc6358a7092d3cb3bbd7689d63caa (diff)
downloadsystemd-8662fcbcf1f31725998bd1ebe706ac813b053962.tar.gz
sysext: rework command line interface to be verb-based
As suggested by @yuwata: https://github.com/systemd/systemd/pull/18181#pullrequestreview-570826113
-rw-r--r--src/sysext/sysext.c325
-rw-r--r--units/systemd-sysext.service4
2 files changed, 163 insertions, 166 deletions
diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c
index e54f1845f8..98f0e53a60 100644
--- a/src/sysext/sysext.c
+++ b/src/sysext/sysext.c
@@ -29,14 +29,8 @@
#include "stat-util.h"
#include "terminal-util.h"
#include "user-util.h"
+#include "verbs.h"
-static enum {
- ACTION_STATUS,
- ACTION_MERGE,
- ACTION_UNMERGE,
- ACTION_REFRESH,
- ACTION_LIST,
-} arg_action = ACTION_STATUS;
static char **arg_hierarchies = NULL; /* "/usr" + "/opt" by default */
static char *arg_root = NULL;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
@@ -149,7 +143,15 @@ static int unmerge(void) {
return ret;
}
-static int status(void) {
+static int verb_unmerge(int argc, char **argv, void *userdata) {
+
+ if (!have_effective_cap(CAP_SYS_ADMIN))
+ return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be privileged.");
+
+ return unmerge();
+}
+
+static int verb_status(int argc, char **argv, void *userdata) {
_cleanup_(table_unrefp) Table *t = NULL;
int r, ret = 0;
char **p;
@@ -735,7 +737,134 @@ static int merge(Hashmap *images) {
return r != 123; /* exit code 123 means: didn't do anything */
}
-static int help(void) {
+static int verb_merge(int argc, char **argv, void *userdata) {
+ _cleanup_(hashmap_freep) Hashmap *images = NULL;
+ char **p;
+ int r;
+
+ if (!have_effective_cap(CAP_SYS_ADMIN))
+ return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be privileged.");
+
+ images = hashmap_new(&image_hash_ops);
+ if (!images)
+ return log_oom();
+
+ r = image_discover(IMAGE_EXTENSION, arg_root, images);
+ if (r < 0)
+ return log_error_errno(r, "Failed to discover extension images: %m");
+
+ /* In merge mode fail if things are already merged. (In --refresh mode below we'll unmerge if we find
+ * things are already merged...) */
+ STRV_FOREACH(p, arg_hierarchies) {
+ _cleanup_free_ char *resolved = NULL;
+
+ r = chase_symlinks(*p, arg_root, CHASE_PREFIX_ROOT, &resolved, NULL);
+ if (r == -ENOENT) {
+ log_debug_errno(r, "Hierarchy '%s%s' does not exist, ignoring.", strempty(arg_root), *p);
+ continue;
+ }
+ if (r < 0)
+ return log_error_errno(r, "Failed to resolve path to hierarchy '%s%s': %m", strempty(arg_root), *p);
+
+ r = is_our_mount_point(resolved);
+ if (r < 0)
+ return r;
+ if (r > 0)
+ return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
+ "Hierarchy '%s' is already merged.", *p);
+ }
+
+ return merge(images);
+}
+
+static int verb_refresh(int argc, char **argv, void *userdata) {
+ _cleanup_(hashmap_freep) Hashmap *images = NULL;
+ int r;
+
+ if (!have_effective_cap(CAP_SYS_ADMIN))
+ return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be privileged.");
+
+ images = hashmap_new(&image_hash_ops);
+ if (!images)
+ return log_oom();
+
+ r = image_discover(IMAGE_EXTENSION, arg_root, images);
+ if (r < 0)
+ return log_error_errno(r, "Failed to discover extension images: %m");
+
+ r = merge(images); /* Returns > 0 if it did something, i.e. a new overlayfs is mounted now. When it
+ * does so it implicitly unmounts any overlayfs placed there before. Returns == 0
+ * if it did nothing, i.e. no extension images found. In this case the old
+ * overlayfs remains in place if there was one. */
+ if (r < 0)
+ return r;
+ if (r == 0) /* No images found? Then unmerge. The goal of --refresh is after all that after having
+ * called there's a guarantee that the merge status matches the installed extensions. */
+ r = unmerge();
+
+ /* Net result here is that:
+ *
+ * 1. If an overlayfs was mounted before and no extensions exist anymore, we'll have unmerged things.
+ *
+ * 2. If an overlayfs was mounted before, and there are still extensions installed' we'll have
+ * unmerged and then merged things again.
+ *
+ * 3. If an overlayfs so far wasn't mounted, and there are extensions installed, we'll have it
+ * mounted now.
+ *
+ * 4. If there was no overlayfs mount so far, and no extensions installed, we implement a NOP.
+ */
+
+ return 0;
+}
+
+static int verb_list(int argc, char **argv, void *userdata) {
+ _cleanup_(hashmap_freep) Hashmap *images = NULL;
+ _cleanup_(table_unrefp) Table *t = NULL;
+ Image *img;
+ int r;
+
+ images = hashmap_new(&image_hash_ops);
+ if (!images)
+ return log_oom();
+
+ r = image_discover(IMAGE_EXTENSION, arg_root, images);
+ if (r < 0)
+ return log_error_errno(r, "Failed to discover extension images: %m");
+
+ if ((arg_json_format_flags & JSON_FORMAT_OFF) && hashmap_isempty(images)) {
+ log_info("No OS extensions found.");
+ return 0;
+ }
+
+ t = table_new("name", "type", "path", "time");
+ if (!t)
+ return log_oom();
+
+ HASHMAP_FOREACH(img, images) {
+ r = table_add_many(
+ t,
+ TABLE_STRING, img->name,
+ TABLE_STRING, image_type_to_string(img->type),
+ TABLE_PATH, img->path,
+ TABLE_TIMESTAMP, img->mtime != 0 ? img->mtime : img->crtime);
+ if (r < 0)
+ return table_log_add_error(r);
+ }
+
+ (void) table_set_sort(t, (size_t) 0, (size_t) -1);
+
+ if (arg_json_format_flags & (JSON_FORMAT_OFF|JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO))
+ (void) pager_open(arg_pager_flags);
+
+ r = table_print_json(t, stdout, arg_json_format_flags);
+ if (r < 0)
+ return table_log_print_error(r);
+
+ return 0;
+}
+
+static int verb_help(int argc, char **argv, void *userdata) {
_cleanup_free_ char *link = NULL;
int r;
@@ -746,12 +875,13 @@ static int help(void) {
printf("%1$s [OPTIONS...] [DEVICE]\n"
"\n%5$sMerge extension images into /usr/ and /opt/ hierarchies.%6$s\n"
"\n%3$sCommands:%4$s\n"
+ " status Show current merge status (default)\n"
+ " merge Merge extensions into /usr/ and /opt/\n"
+ " unmerge Unmerge extensions from /usr/ and /opt/\n"
+ " refresh Unmerge/merge extensions again\n"
+ " list List installed extensions\n"
" -h --help Show this help\n"
" --version Show package version\n"
- " -m --merge Merge extensions into /usr/ and /opt/\n"
- " -u --unmerge Unmerge extensions from /usr/ and /opt/\n"
- " -R --refresh Unmerge/merge extensions again\n"
- " -l --list List all OS images\n"
"\n%3$sOptions:%4$s\n"
" --no-pager Do not pipe output into a pager\n"
" --root=PATH Operate relative to root path\n"
@@ -772,10 +902,6 @@ static int parse_argv(int argc, char *argv[]) {
enum {
ARG_VERSION = 0x100,
ARG_NO_PAGER,
- ARG_MERGE,
- ARG_UNMERGE,
- ARG_REFRESH,
- ARG_LIST,
ARG_ROOT,
ARG_JSON,
};
@@ -785,10 +911,6 @@ static int parse_argv(int argc, char *argv[]) {
{ "version", no_argument, NULL, ARG_VERSION },
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
{ "root", required_argument, NULL, ARG_ROOT },
- { "merge", no_argument, NULL, 'm' },
- { "unmerge", no_argument, NULL, 'u' },
- { "refresh", no_argument, NULL, 'R' },
- { "list", no_argument, NULL, 'l' },
{ "json", required_argument, NULL, ARG_JSON },
{}
};
@@ -798,12 +920,12 @@ static int parse_argv(int argc, char *argv[]) {
assert(argc >= 0);
assert(argv);
- while ((c = getopt_long(argc, argv, "hmuRl", options, NULL)) >= 0)
+ while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
switch (c) {
case 'h':
- return help();
+ return verb_help(argc, argv, NULL);
case ARG_VERSION:
return version();
@@ -812,22 +934,6 @@ static int parse_argv(int argc, char *argv[]) {
arg_pager_flags |= PAGER_DISABLE;
break;
- case 'm':
- arg_action = ACTION_MERGE;
- break;
-
- case 'u':
- arg_action = ACTION_UNMERGE;
- break;
-
- case 'R':
- arg_action = ACTION_REFRESH;
- break;
-
- case 'l':
- arg_action = ACTION_LIST;
- break;
-
case ARG_ROOT:
r = parse_path_argument_and_warn(optarg, false, &arg_root);
if (r < 0)
@@ -848,10 +954,6 @@ static int parse_argv(int argc, char *argv[]) {
assert_not_reached("Unhandled option");
}
- if (argc - optind > 0)
- return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
- "Unexpected argument.");
-
return 1;
}
@@ -895,8 +997,22 @@ static int parse_env(void) {
return 0;
}
+static int sysext_main(int argc, char *argv[]) {
+
+ static const Verb verbs[] = {
+ { "status", VERB_ANY, 1, VERB_DEFAULT, verb_status },
+ { "merge", VERB_ANY, 1, 0, verb_merge },
+ { "unmerge", VERB_ANY, 1, 0, verb_unmerge },
+ { "refresh", VERB_ANY, 1, 0, verb_refresh },
+ { "list", VERB_ANY, 1, 0, verb_list },
+ { "help", VERB_ANY, 1, 0, verb_help },
+ {}
+ };
+
+ return dispatch_verb(argc, argv, verbs, NULL);
+}
+
static int run(int argc, char *argv[]) {
- _cleanup_(hashmap_freep) Hashmap *images = NULL;
int r;
log_setup_cli();
@@ -915,126 +1031,7 @@ static int run(int argc, char *argv[]) {
return log_oom();
}
- /* Given that things deep down in the child process will fail, let's catch the no-privilege issue
- * early on */
- if (!IN_SET(arg_action, ACTION_STATUS, ACTION_LIST) && !have_effective_cap(CAP_SYS_ADMIN))
- return log_error_errno(SYNTHETIC_ERRNO(EPERM), "Need to be privileged.");
-
- if (arg_action == ACTION_STATUS)
- return status();
-
- if (arg_action == ACTION_UNMERGE)
- return unmerge();
-
- images = hashmap_new(&image_hash_ops);
- if (!images)
- return log_oom();
-
- r = image_discover(IMAGE_EXTENSION, arg_root, images);
- if (r < 0)
- return log_error_errno(r, "Failed to discover extension images: %m");
-
- switch (arg_action) {
-
- case ACTION_LIST: {
- _cleanup_(table_unrefp) Table *t = NULL;
- Image *img;
-
- if ((arg_json_format_flags & JSON_FORMAT_OFF) && hashmap_isempty(images)) {
- log_info("No OS extensions found.");
- return 0;
- }
-
- t = table_new("name", "type", "path", "time");
- if (!t)
- return log_oom();
-
- HASHMAP_FOREACH(img, images) {
- r = table_add_many(
- t,
- TABLE_STRING, img->name,
- TABLE_STRING, image_type_to_string(img->type),
- TABLE_PATH, img->path,
- TABLE_TIMESTAMP, img->mtime != 0 ? img->mtime : img->crtime);
- if (r < 0)
- return table_log_add_error(r);
- }
-
- (void) table_set_sort(t, (size_t) 0, (size_t) -1);
-
- if (arg_json_format_flags & (JSON_FORMAT_OFF|JSON_FORMAT_PRETTY|JSON_FORMAT_PRETTY_AUTO))
- (void) pager_open(arg_pager_flags);
-
- r = table_print_json(t, stdout, arg_json_format_flags);
- if (r < 0)
- return table_log_print_error(r);
-
- r = 0;
- break;
- }
-
- case ACTION_MERGE: {
- char **p;
-
- /* In merge mode fail if things are already merged. (In --refresh mode below we'll unmerge if
- * we find things are already merged...) */
- STRV_FOREACH(p, arg_hierarchies) {
- _cleanup_free_ char *resolved = NULL;
-
- r = chase_symlinks(*p, arg_root, CHASE_PREFIX_ROOT, &resolved, NULL);
- if (r == -ENOENT) {
- log_debug_errno(r, "Hierarchy '%s%s' does not exist, ignoring.", strempty(arg_root), *p);
- continue;
- }
- if (r < 0)
- return log_error_errno(r, "Failed to resolve path to hierarchy '%s%s': %m", strempty(arg_root), *p);
-
- r = is_our_mount_point(resolved);
- if (r < 0)
- return r;
- if (r > 0)
- return log_error_errno(SYNTHETIC_ERRNO(EBUSY),
- "Hierarchy '%s' is already merged.", *p);
- }
-
- r = merge(images);
- break;
- }
-
- case ACTION_REFRESH:
- r = merge(images); /* Returns > 0 if it did something, i.e. a new overlayfs is mounted
- * now. When it does so it implicitly unmounts any overlayfs placed there
- * before. Returns == 0 if it did nothing, i.e. no extension images
- * found. In this case the old overlayfs remains in place if there was
- * one. */
- if (r < 0)
- return r;
- if (r == 0) /* No images found? Then unmerge. The goal of --refresh is after all that after
- * having called there's a guarantee that the merge status matches the installed
- * extensions. */
- r = unmerge();
-
- /* Net result here is that:
- *
- * 1. If an overlayfs was mounted before and no extensions exist anymore, we'll have unmerged
- * things.
- *
- * 2. If an overlayfs was mounted before, and there are still extensions installed' we'll
- * have unmerged and then merged things again.
- *
- * 3. If an overlayfs so far wasn't mounted, and there are extensions installed, we'll have
- * it mounted now.
- *
- * 4. If there was no overlayfs mount so far, and no extensions installed, we implement a
- * NOP.
- */
- break;
-
- default:
- assert_not_reached("Uneexpected action");
- }
-
- return r;
+ return sysext_main(argc, argv);
}
DEFINE_MAIN_FUNCTION(run);
diff --git a/units/systemd-sysext.service b/units/systemd-sysext.service
index cc31f9ca9b..35b5edca1d 100644
--- a/units/systemd-sysext.service
+++ b/units/systemd-sysext.service
@@ -24,8 +24,8 @@ ConditionDirectoryNotEmpty=|/usr/lib/extensions
[Service]
Type=oneshot
RemainAfterExit=yes
-ExecStart=systemd-sysext --merge
-ExecStop=systemd-sysext --unmerge
+ExecStart=systemd-sysext merge
+ExecStop=systemd-sysext unmerge
[Install]
WantedBy=sysinit.target