[systemd-commits] 2 commits - src/systemctl src/test
Ronny Chevalier
rchevalier at kemper.freedesktop.org
Sat Dec 13 06:30:59 PST 2014
src/systemctl/systemctl.c | 173 +++++++++++++++++++++++-----------------------
src/test/test-unit-name.c | 35 +++++++++
2 files changed, 122 insertions(+), 86 deletions(-)
New commits:
commit e9e310f8e99c63c764f71ed0c224ccd3cceb90c7
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Dec 13 15:14:48 2014 +0100
systemctl: handle correctly template units for edit verb
Previously, if we provided getty at .service to systemctl edit it would
have failed when using the bus because it is an invalid unit name.
But it would have succeeded when searching in the filesystem.
Now, we check if we have a template, if we do we search in the
filesystem, if we don't have a templae and we can use the bus, we do.
Furthermore, if we provided getty at tty1.service it would not have worked
when searching the filesystem, but it would have worked with the bus.
So now, when using the filesystem we use the template name and not the
unit name, and the same when logging errors.
(Also did a refactoring to avoid a long function)
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index a75e9de..8400bc8 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -6026,9 +6026,65 @@ static int run_editor(char **paths) {
return r;
}
+static int unit_find_path(sd_bus *bus, const char *unit_name, const char *template, bool avoid_bus_cache, LookupPaths *lp, char **path) {
+ int r;
+
+ assert(unit_name);
+ assert(path);
+ assert(lp);
+
+ if (!avoid_bus_cache && !unit_name_is_template(unit_name)) {
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_free_ char *unit = NULL;
+ _cleanup_free_ char *tmp_path = NULL;
+
+ unit = unit_dbus_path_from_name(unit_name);
+ if (!unit)
+ return log_oom();
+
+ if (need_daemon_reload(bus, unit_name) > 0) {
+ log_warning("%s ignored: unit file changed on disk. Run 'systemctl%s daemon-reload'.",
+ unit_name, arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
+ return 0;
+ }
+
+ r = sd_bus_get_property_string(
+ bus,
+ "org.freedesktop.systemd1",
+ unit,
+ "org.freedesktop.systemd1.Unit",
+ "FragmentPath",
+ &error,
+ &tmp_path);
+ if (r < 0) {
+ log_warning("Failed to get FragmentPath: %s", bus_error_message(&error, r));
+ return 0;
+ }
+
+ if (isempty(tmp_path)) {
+ log_warning("%s ignored: not found", template);
+ return 0;
+ }
+
+ *path = tmp_path;
+ tmp_path = NULL;
+
+ return 1;
+ } else {
+ r = unit_file_find_path(lp, template, path);
+ if (r == 0)
+ log_warning("%s ignored: not found", template);
+ return r;
+ }
+
+ return 0;
+}
+
static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
_cleanup_free_ char *user_home = NULL;
_cleanup_free_ char *user_runtime = NULL;
+ _cleanup_lookup_paths_free_ LookupPaths lp = {};
+ bool avoid_bus_cache;
char **name;
int r;
@@ -6053,100 +6109,45 @@ static int find_paths_to_edit(sd_bus *bus, char **names, char ***paths) {
}
}
- if (!bus || avoid_bus()) {
- _cleanup_lookup_paths_free_ LookupPaths lp = {};
-
- /* If there is no bus, we try to find the units by testing each available directory
- * according to the scope.
- */
- r = lookup_paths_init(&lp,
- arg_scope == UNIT_FILE_SYSTEM ? SYSTEMD_SYSTEM : SYSTEMD_USER,
- arg_scope == UNIT_FILE_USER,
- arg_root,
- NULL, NULL, NULL);
- if (r < 0) {
- log_error_errno(r, "Failed get lookup paths: %m");
- return r;
- }
-
- STRV_FOREACH(name, names) {
- _cleanup_free_ char *path = NULL;
- char *new_path, *tmp_path;
-
- r = unit_file_find_path(&lp, *name, &path);
- if (r < 0)
- return r;
- if (r == 0) {
- log_warning("%s ignored: not found", *name);
- continue;
- }
+ r = lookup_paths_init(&lp,
+ arg_scope == UNIT_FILE_SYSTEM ? SYSTEMD_SYSTEM : SYSTEMD_USER,
+ arg_scope == UNIT_FILE_USER,
+ arg_root,
+ NULL, NULL, NULL);
+ if (r < 0) {
+ log_error_errno(r, "Failed get lookup paths: %m");
+ return r;
+ }
- if (arg_full)
- r = unit_file_create_copy(*name, path, user_home, user_runtime, &new_path, &tmp_path);
- else
- r = unit_file_create_drop_in(*name, user_home, user_runtime, &new_path, &tmp_path);
+ avoid_bus_cache = !bus || avoid_bus();
- if (r < 0)
- continue;
+ STRV_FOREACH(name, names) {
+ _cleanup_free_ char *path = NULL;
+ _cleanup_free_ char *template = NULL;
+ char *new_path, *tmp_path;
- r = strv_push(paths, new_path);
- if (r < 0)
- return log_oom();
+ template = unit_name_template(*name);
+ if (!template)
+ return log_oom();
- r = strv_push(paths, tmp_path);
- if (r < 0)
- return log_oom();
+ r = unit_find_path(bus, *name, template, avoid_bus_cache, &lp, &path);
+ if (r < 0)
+ return r;
+ else if (r == 0) {
+ continue;
}
- } else {
- STRV_FOREACH(name, names) {
- _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
- _cleanup_free_ char *fragment_path = NULL;
- _cleanup_free_ char *unit = NULL;
- char *new_path, *tmp_path;
-
- unit = unit_dbus_path_from_name(*name);
- if (!unit)
- return log_oom();
-
- if (need_daemon_reload(bus, *name) > 0) {
- log_warning("%s ignored: unit file changed on disk. Run 'systemctl%s daemon-reload'.",
- *name, arg_scope == UNIT_FILE_SYSTEM ? "" : " --user");
- continue;
- }
- r = sd_bus_get_property_string(
- bus,
- "org.freedesktop.systemd1",
- unit,
- "org.freedesktop.systemd1.Unit",
- "FragmentPath",
- &error,
- &fragment_path);
- if (r < 0) {
- log_warning("Failed to get FragmentPath: %s", bus_error_message(&error, r));
- continue;
- }
-
- if (isempty(fragment_path)) {
- log_warning("%s ignored: not found", *name);
- continue;
- }
-
- if (arg_full)
- r = unit_file_create_copy(*name, fragment_path, user_home, user_runtime, &new_path, &tmp_path);
- else
- r = unit_file_create_drop_in(*name, user_home, user_runtime, &new_path, &tmp_path);
- if (r < 0)
- continue;
+ if (arg_full)
+ r = unit_file_create_copy(template, path, user_home, user_runtime, &new_path, &tmp_path);
+ else
+ r = unit_file_create_drop_in(template, user_home, user_runtime, &new_path, &tmp_path);
- r = strv_push(paths, new_path);
- if (r < 0)
- return log_oom();
+ if (r < 0)
+ continue;
- r = strv_push(paths, tmp_path);
- if (r < 0)
- return log_oom();
- }
+ r = strv_push_pair(paths, new_path, tmp_path);
+ if (r < 0)
+ return log_oom();
}
return 0;
commit fee0a921830166abffe5a806a512da6ceb2fe2eb
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Dec 13 15:12:38 2014 +0100
test-unit-name: add more tests
Add more test cases for:
- unit_name_is_instance
- unit_name_to_instance
Add tests for:
- unit_name_template
- unit_name_is_template
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
index 65b6975..5c7f8b4 100644
--- a/src/test/test-unit-name.c
+++ b/src/test/test-unit-name.c
@@ -193,6 +193,7 @@ static int test_unit_printf(void) {
expect(u2, "%t", "/run/user/*");
manager_free(m);
+#undef expect
return 0;
}
@@ -262,6 +263,7 @@ static void test_unit_name_is_instance(void) {
assert_se(unit_name_is_instance("a-c_c01Aj at b05Dii_-oioi.service"));
assert_se(!unit_name_is_instance("a.service"));
+ assert_se(!unit_name_is_instance("a at .service"));
assert_se(!unit_name_is_instance("junk"));
assert_se(!unit_name_is_instance(""));
}
@@ -293,6 +295,11 @@ static void test_unit_name_to_instance(void) {
assert_se(streq(instance, "bar"));
free(instance);
+ r = unit_name_to_instance("foo at .service", &instance);
+ assert_se(r >= 0);
+ assert_se(streq(instance, ""));
+ free(instance);
+
r = unit_name_to_instance("fo0-stUff_b at b.e", &instance);
assert_se(r >= 0);
assert_se(streq(instance, "b"));
@@ -304,6 +311,9 @@ static void test_unit_name_to_instance(void) {
r = unit_name_to_instance("fooj at unk", &instance);
assert_se(r < 0);
+
+ r = unit_name_to_instance("foo@", &instance);
+ assert_se(r < 0);
}
static void test_unit_name_escape(void) {
@@ -314,6 +324,29 @@ static void test_unit_name_escape(void) {
assert_se(streq(r, "ab\\x2b\\x2dc.a-bc\\x40foo.service"));
}
+static void test_unit_name_template(void) {
+#define expect(name, expected) \
+ { \
+ _cleanup_free_ char *f = NULL; \
+ f = unit_name_template(name); \
+ assert_se(f); \
+ printf("got: %s, expected: %s\n", f, expected); \
+ assert_se(streq(f, expected)); \
+ }
+ expect("foo at bar.service", "foo at .service")
+ expect("foo.mount", "foo.mount")
+#undef expect
+}
+
+static void test_unit_name_is_template(void) {
+ assert_se(unit_name_is_template("foo at .service"));
+ assert_se(unit_name_is_template("bar at .path"));
+
+ assert_se(!unit_name_is_template("bar at i.mount"));
+ assert_se(!unit_name_is_template("bar at foobbbb.service"));
+ assert_se(!unit_name_is_template("barfoo.service"));
+}
+
int main(int argc, char* argv[]) {
int rc = 0;
test_replacements();
@@ -326,6 +359,8 @@ int main(int argc, char* argv[]) {
test_build_subslice();
test_unit_name_to_instance();
test_unit_name_escape();
+ test_unit_name_template();
+ test_unit_name_is_template();
return rc;
}
More information about the systemd-commits
mailing list