[systemd-devel] [PATCH] unit: make unit can start without instance

WaLyong Cho walyong.cho at samsung.com
Tue May 27 08:39:18 PDT 2014


---
 src/core/manager.c     | 10 ++++++----
 src/shared/unit-name.c | 12 ++++++++++++
 src/shared/unit-name.h |  1 +
 3 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/core/manager.c b/src/core/manager.c
index 0cb2044..089df43 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -1163,6 +1163,7 @@ int manager_load_unit_prepare(
         Unit *ret;
         UnitType t;
         int r;
+        _cleanup_free_ char *n = NULL;
 
         assert(m);
         assert(name || path);
@@ -1177,11 +1178,12 @@ int manager_load_unit_prepare(
                 name = basename(path);
 
         t = unit_name_to_type(name);
+        n = unit_name_from_instanceless(name);
 
-        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(name, TEMPLATE_INVALID))
-                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", name);
+        if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid(n, TEMPLATE_INVALID))
+                return sd_bus_error_setf(e, SD_BUS_ERROR_INVALID_ARGS, "Unit name %s is not valid.", n);
 
-        ret = manager_get_unit(m, name);
+        ret = manager_get_unit(m, n);
         if (ret) {
                 *_ret = ret;
                 return 1;
@@ -1199,7 +1201,7 @@ int manager_load_unit_prepare(
                 }
         }
 
-        r = unit_add_name(ret, name);
+        r = unit_add_name(ret, n);
         if (r < 0) {
                 unit_free(ret);
                 return r;
diff --git a/src/shared/unit-name.c b/src/shared/unit-name.c
index 6c167b4..c5584b5 100644
--- a/src/shared/unit-name.c
+++ b/src/shared/unit-name.c
@@ -431,6 +431,18 @@ char *unit_name_from_path_instance(const char *prefix, const char *path, const c
         return strjoin(prefix, "@", p, suffix, NULL);
 }
 
+char *unit_name_from_instanceless(const char *name) {
+        _cleanup_free_ char *i = NULL;
+
+        assert(name);
+
+        if (unit_name_is_template(name)) {
+                i = unit_name_to_prefix(name);
+                return unit_name_replace_instance(name, i);
+        } else
+                return strdup(name);
+}
+
 char *unit_name_to_path(const char *name) {
         _cleanup_free_ char *w = NULL;
 
diff --git a/src/shared/unit-name.h b/src/shared/unit-name.h
index d06d2b2..1251b18 100644
--- a/src/shared/unit-name.h
+++ b/src/shared/unit-name.h
@@ -98,6 +98,7 @@ char *unit_name_template(const char *f);
 
 char *unit_name_from_path(const char *path, const char *suffix);
 char *unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix);
+char *unit_name_from_instanceless(const char *name);
 char *unit_name_to_path(const char *name);
 
 char *unit_dbus_path_from_name(const char *name);
-- 
1.9.3



More information about the systemd-devel mailing list