From 2d686078fee562c9ae47595c9ebbb53fd4884a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 28 Nov 2014 11:28:10 -0500 Subject: [PATCH] systemctl: append default suffix only if none present Simplify unit_name_mangle() and unit_name_mangle_with_suffix() to always behave the same, and only append a suffix if there is no type suffix. If a user says 'isolate blah.device' it is better to return an error that the type cannot be isolated, than to try to isolate blah.device.target. (cherry picked from commit 5e03c6e3b517286bbd65b48d88f60e5b83721894) --- src/shared/unit-name.c | 44 +++++++++++---------------------------- src/shared/unit-name.h | 4 +++- src/systemctl/systemctl.c | 5 +---- 3 files changed, 16 insertions(+), 37 deletions(-) diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c index 6c6d7f4f8..21b66913c 100644 --- a/src/shared/unit-name.c +++ b/src/shared/unit-name.c @@ -502,37 +502,11 @@ int unit_name_from_dbus_path(const char *path, char **name) { } /** - * Try to turn a string that might not be a unit name into a - * sensible unit name. - */ -char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { - char *r, *t; - - assert(name); - - if (is_device_path(name)) - return unit_name_from_path(name, ".device"); - - if (path_is_absolute(name)) - return unit_name_from_path(name, ".mount"); - - r = new(char, strlen(name) * 4 + strlen(".service") + 1); - if (!r) - return NULL; - - t = do_escape_mangle(name, allow_globs, r); - - if (unit_name_to_type(name) < 0) - strcpy(t, ".service"); - else - *t = 0; - - return r; -} - -/** - * Similar to unit_name_mangle(), but is called when we know - * that this is about a specific unit type. + * Convert a string to a unit name. /dev/blah is converted to dev-blah.device, + * /blah/blah is converted to blah-blah.mount, anything else is left alone, + * except that @suffix is appended if a valid unit suffix is not present. + * + * If @allow_globs, globs characters are preserved. Otherwise they are escaped. */ char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix) { char *r, *t; @@ -541,13 +515,19 @@ char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow assert(suffix); assert(suffix[0] == '.'); + if (is_device_path(name)) + return unit_name_from_path(name, ".device"); + + if (path_is_absolute(name)) + return unit_name_from_path(name, ".mount"); + r = new(char, strlen(name) * 4 + strlen(suffix) + 1); if (!r) return NULL; t = do_escape_mangle(name, allow_globs, r); - if (!endswith(name, suffix)) + if (unit_name_to_type(name) < 0) strcpy(t, suffix); else *t = 0; diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h index daeb56a86..6f139cc4c 100644 --- a/src/shared/unit-name.h +++ b/src/shared/unit-name.h @@ -156,8 +156,10 @@ enum unit_name_mangle { MANGLE_GLOB, }; -char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs); char *unit_name_mangle_with_suffix(const char *name, enum unit_name_mangle allow_globs, const char *suffix); +static inline char *unit_name_mangle(const char *name, enum unit_name_mangle allow_globs) { + return unit_name_mangle_with_suffix(name, allow_globs, ".service"); +} int build_subslice(const char *slice, const char*name, char **subslice); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 409693e8f..70b92db01 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -2700,10 +2700,7 @@ static int expand_names(sd_bus *bus, char **names, const char* suffix, char ***r STRV_FOREACH(name, names) { char *t; - if (suffix) - t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix); - else - t = unit_name_mangle(*name, MANGLE_GLOB); + t = unit_name_mangle_with_suffix(*name, MANGLE_GLOB, suffix); if (!t) return log_oom();