]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
systemctl: move check whether a service exists as native unit file to install.c
authorLennart Poettering <lennart@poettering.net>
Thu, 7 Apr 2016 15:40:11 +0000 (17:40 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 12 Apr 2016 11:43:32 +0000 (13:43 +0200)
Move the search path check from the SysV service compat support into install.c
so that we can reuse the usual algorithm instead of rolling a private loop for
this.

src/shared/install.c
src/shared/install.h
src/systemctl/systemctl.c

index f78cc814e5c7a9faf54c51d8f2cf42f5df0f6470..b5453adeee7ec17f76c628e08214e57e8fd5f740 100644 (file)
@@ -2052,6 +2052,25 @@ int unit_file_get_state(
         return unit_file_lookup_state(scope, &paths, name, ret);
 }
 
+int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *name) {
+        _cleanup_(install_context_done) InstallContext c = {};
+        int r;
+
+        assert(paths);
+        assert(name);
+
+        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
+                return -EINVAL;
+
+        r = install_info_discover(scope, &c, paths, name, 0, NULL);
+        if (r == -ENOENT)
+                return 0;
+        if (r < 0)
+                return r;
+
+        return 1;
+}
+
 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
         _cleanup_strv_free_ char **files = NULL;
         char **p;
index 578664dd48860865f1b60e58cef3378663f28c9e..c57c23934bad80afdbf84fe3858f2a49c8d51a03 100644 (file)
@@ -139,6 +139,7 @@ int unit_file_add_dependency(UnitFileScope scope, bool runtime, const char *root
 
 int unit_file_lookup_state(UnitFileScope scope, const LookupPaths *paths, const char *name, UnitFileState *ret);
 int unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename, UnitFileState *ret);
+int unit_file_exists(UnitFileScope scope, const LookupPaths *paths, const char *name);
 
 int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
 Hashmap* unit_file_list_free(Hashmap *h);
index b1c4a84effd504a9662993e39fe3ffbba250a432..da2715810e4f2c4722663f6601f6131b0d5e481d 100644 (file)
@@ -5223,8 +5223,10 @@ static int enable_sysv_units(const char *verb, char **args) {
         int r = 0;
 
 #if defined(HAVE_SYSV_COMPAT)
-        unsigned f = 0;
         _cleanup_lookup_paths_free_ LookupPaths paths = {};
+        unsigned f = 0;
+
+        /* Processes all SysV units, and reshuffles the array so that afterwards only the native units remain */
 
         if (arg_scope != UNIT_FILE_SYSTEM)
                 return 0;
@@ -5238,24 +5240,28 @@ static int enable_sysv_units(const char *verb, char **args) {
                         "is-enabled"))
                 return 0;
 
-        /* Processes all SysV units, and reshuffles the array so that
-         * afterwards only the native units remain */
-
         r = lookup_paths_init(&paths, arg_scope, arg_root);
         if (r < 0)
                 return r;
 
         r = 0;
         while (args[f]) {
-                const char *name;
+
+                const char *argv[] = {
+                        ROOTLIBEXECDIR "/systemd-sysv-install",
+                        NULL,
+                        NULL,
+                        NULL,
+                        NULL,
+                };
+
                 _cleanup_free_ char *p = NULL, *q = NULL, *l = NULL;
                 bool found_native = false, found_sysv;
+                siginfo_t status;
+                const char *name;
                 unsigned c = 1;
-                const char *argv[6] = { ROOTLIBEXECDIR "/systemd-sysv-install", NULL, NULL, NULL, NULL };
-                char **k;
-                int j;
                 pid_t pid;
-                siginfo_t status;
+                int j;
 
                 name = args[f++];
 
@@ -5265,21 +5271,13 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (path_is_absolute(name))
                         continue;
 
-                STRV_FOREACH(k, paths.search_path) {
-                        _cleanup_free_ char *path = NULL;
-
-                        path = path_join(arg_root, *k, name);
-                        if (!path)
-                                return log_oom();
-
-                        found_native = access(path, F_OK) >= 0;
-                        if (found_native)
-                                break;
-                }
+                j = unit_file_exists(arg_scope, &paths, name);
+                if (j < 0)
+                        return log_error_errno(j, "Failed to lookup unit file state: %m");
+                found_native = j > 0;
 
-                /* If we have both a native unit and a SysV script,
-                 * enable/disable them both (below); for is-enabled, prefer the
-                 * native unit */
+                /* If we have both a native unit and a SysV script, enable/disable them both (below); for is-enabled,
+                 * prefer the native unit */
                 if (found_native && streq(verb, "is-enabled"))
                         continue;
 
@@ -5293,9 +5291,9 @@ static int enable_sysv_units(const char *verb, char **args) {
                         continue;
 
                 if (found_native)
-                        log_info("Synchronizing state of %s with SysV init with %s...", name, argv[0]);
+                        log_info("Synchronizing state of %s with SysV service script with %s.", name, argv[0]);
                 else
-                        log_info("%s is not a native service, redirecting to systemd-sysv-install", name);
+                        log_info("%s is not a native service, redirecting to systemd-sysv-install.", name);
 
                 if (!isempty(arg_root))
                         argv[c++] = q = strappend("--root=", arg_root);
@@ -5308,7 +5306,7 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (!l)
                         return log_oom();
 
-                log_info("Executing %s", l);
+                log_info("Executing: %s", l);
 
                 pid = fork();
                 if (pid < 0)