1ff636
From f8fd91c9f0f1f7feabf8567bdad61f57fe922011 Mon Sep 17 00:00:00 2001
1ff636
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
1ff636
Date: Sat, 14 Mar 2015 21:46:59 -0400
1ff636
Subject: [PATCH] sysv-generator: initialize LookupPaths just once
1ff636
1ff636
With debugging on, sysv-generator would print the full set of
1ff636
lookup paths for *every* sysv script.
1ff636
1ff636
While at it, pass LookupPaths as a pointer in sysv-generator,
1ff636
and constify it everywhere.
1ff636
1ff636
(cherry picked from commit a8ffe6fbcbfdba39aef8dce8b298b3e0cb377c0e)
1ff636
---
1ff636
 src/shared/install.c                | 55 ++++++++++++++++++++++---------------
1ff636
 src/shared/install.h                | 11 +++++++-
1ff636
 src/shared/path-lookup.c            |  1 +
1ff636
 src/shared/path-lookup.h            |  3 +-
1ff636
 src/sysv-generator/sysv-generator.c | 14 +++++-----
1ff636
 5 files changed, 53 insertions(+), 31 deletions(-)
1ff636
1ff636
diff --git a/src/shared/install.c b/src/shared/install.c
181b3f
index 65f1c245c..92b8d6e8e 100644
1ff636
--- a/src/shared/install.c
1ff636
+++ b/src/shared/install.c
1ff636
@@ -1084,7 +1084,7 @@ static int unit_file_load(
1ff636
 static int unit_file_search(
1ff636
                 InstallContext *c,
1ff636
                 InstallInfo *info,
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *root_dir,
1ff636
                 bool allow_symlink,
1ff636
                 bool load,
1ff636
@@ -1153,7 +1153,7 @@ static int unit_file_search(
1ff636
 }
1ff636
 
1ff636
 static int unit_file_can_install(
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *root_dir,
1ff636
                 const char *name,
1ff636
                 bool allow_symlink,
1ff636
@@ -1317,7 +1317,7 @@ static int install_info_symlink_wants(
1ff636
 
1ff636
 static int install_info_symlink_link(
1ff636
                 InstallInfo *i,
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *config_path,
1ff636
                 const char *root_dir,
1ff636
                 bool force,
1ff636
@@ -1345,7 +1345,7 @@ static int install_info_symlink_link(
1ff636
 
1ff636
 static int install_info_apply(
1ff636
                 InstallInfo *i,
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *config_path,
1ff636
                 const char *root_dir,
1ff636
                 bool force,
1ff636
@@ -1377,7 +1377,7 @@ static int install_info_apply(
1ff636
 
1ff636
 static int install_context_apply(
1ff636
                 InstallContext *c,
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *config_path,
1ff636
                 const char *root_dir,
1ff636
                 bool force,
1ff636
@@ -1424,7 +1424,7 @@ static int install_context_apply(
1ff636
 
1ff636
 static int install_context_mark_for_removal(
1ff636
                 InstallContext *c,
1ff636
-                LookupPaths *paths,
1ff636
+                const LookupPaths *paths,
1ff636
                 Set **remove_symlinks_to,
1ff636
                 const char *config_path,
1ff636
                 const char *root_dir) {
1ff636
@@ -1785,39 +1785,28 @@ int unit_file_get_default(
1ff636
         return -ENOENT;
1ff636
 }
1ff636
 
1ff636
-UnitFileState unit_file_get_state(
1ff636
+UnitFileState unit_file_lookup_state(
1ff636
                 UnitFileScope scope,
1ff636
                 const char *root_dir,
1ff636
+                const LookupPaths *paths,
1ff636
                 const char *name) {
1ff636
 
1ff636
-        _cleanup_lookup_paths_free_ LookupPaths paths = {};
1ff636
         UnitFileState state = _UNIT_FILE_STATE_INVALID;
1ff636
         char **i;
1ff636
         _cleanup_free_ char *path = NULL;
1ff636
         int r;
1ff636
 
1ff636
-        assert(scope >= 0);
1ff636
-        assert(scope < _UNIT_FILE_SCOPE_MAX);
1ff636
-        assert(name);
1ff636
-
1ff636
-        if (root_dir && scope != UNIT_FILE_SYSTEM)
1ff636
-                return -EINVAL;
1ff636
+        assert(paths);
1ff636
 
1ff636
         if (!unit_name_is_valid(name, TEMPLATE_VALID))
1ff636
                 return -EINVAL;
1ff636
 
1ff636
-        r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1ff636
-        if (r < 0)
1ff636
-                return r;
1ff636
-
1ff636
-        STRV_FOREACH(i, paths.unit_path) {
1ff636
+        STRV_FOREACH(i, paths->unit_path) {
1ff636
                 struct stat st;
1ff636
                 char *partial;
1ff636
                 bool also = false;
1ff636
 
1ff636
                 free(path);
1ff636
-                path = NULL;
1ff636
-
1ff636
                 path = path_join(root_dir, *i, name);
1ff636
                 if (!path)
1ff636
                         return -ENOMEM;
1ff636
@@ -1858,7 +1847,7 @@ UnitFileState unit_file_get_state(
1ff636
                 else if (r > 0)
1ff636
                         return state;
1ff636
 
1ff636
-                r = unit_file_can_install(&paths, root_dir, partial, true, &also);
1ff636
+                r = unit_file_can_install(paths, root_dir, partial, true, &also);
1ff636
                 if (r < 0 && errno != ENOENT)
1ff636
                         return r;
1ff636
                 else if (r > 0)
1ff636
@@ -1873,6 +1862,28 @@ UnitFileState unit_file_get_state(
1ff636
         return r < 0 ? r : state;
1ff636
 }
1ff636
 
1ff636
+UnitFileState unit_file_get_state(
1ff636
+                UnitFileScope scope,
1ff636
+                const char *root_dir,
1ff636
+                const char *name) {
1ff636
+
1ff636
+        _cleanup_lookup_paths_free_ LookupPaths paths = {};
1ff636
+        int r;
1ff636
+
1ff636
+        assert(scope >= 0);
1ff636
+        assert(scope < _UNIT_FILE_SCOPE_MAX);
1ff636
+        assert(name);
1ff636
+
1ff636
+        if (root_dir && scope != UNIT_FILE_SYSTEM)
1ff636
+                return -EINVAL;
1ff636
+
1ff636
+        r = lookup_paths_init_from_scope(&paths, scope, root_dir);
1ff636
+        if (r < 0)
1ff636
+                return r;
1ff636
+
1ff636
+        return unit_file_lookup_state(scope, root_dir, &paths, name);
1ff636
+}
1ff636
+
1ff636
 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
1ff636
         _cleanup_strv_free_ char **files = NULL;
1ff636
         char **p;
1ff636
diff --git a/src/shared/install.h b/src/shared/install.h
181b3f
index 357be0f92..3ca39397e 100644
1ff636
--- a/src/shared/install.h
1ff636
+++ b/src/shared/install.h
1ff636
@@ -23,6 +23,7 @@
1ff636
 
1ff636
 #include "hashmap.h"
1ff636
 #include "unit-name.h"
1ff636
+#include "path-lookup.h"
1ff636
 
1ff636
 typedef enum UnitFileScope {
1ff636
         UNIT_FILE_SYSTEM,
1ff636
@@ -98,7 +99,15 @@ int unit_file_set_default(UnitFileScope scope, const char *root_dir, const char
1ff636
 int unit_file_get_default(UnitFileScope scope, const char *root_dir, char **name);
1ff636
 int unit_file_add_dependency(UnitFileScope scope, bool runtime, const char *root_dir, char **files, char *target, UnitDependency dep, bool force, UnitFileChange **changes, unsigned *n_changes);
1ff636
 
1ff636
-UnitFileState unit_file_get_state(UnitFileScope scope, const char *root_dir, const char *filename);
1ff636
+UnitFileState unit_file_lookup_state(
1ff636
+                UnitFileScope scope,
1ff636
+                const char *root_dir,
1ff636
+                const LookupPaths *paths,
1ff636
+                const char *name);
1ff636
+UnitFileState unit_file_get_state(
1ff636
+                UnitFileScope scope,
1ff636
+                const char *root_dir,
1ff636
+                const char *filename);
1ff636
 
1ff636
 int unit_file_get_list(UnitFileScope scope, const char *root_dir, Hashmap *h);
1ff636
 
1ff636
diff --git a/src/shared/path-lookup.c b/src/shared/path-lookup.c
181b3f
index 291a2f405..812730be1 100644
1ff636
--- a/src/shared/path-lookup.c
1ff636
+++ b/src/shared/path-lookup.c
1ff636
@@ -31,6 +31,7 @@
1ff636
 #include "strv.h"
1ff636
 #include "path-util.h"
1ff636
 #include "path-lookup.h"
1ff636
+#include "install.h"
1ff636
 
1ff636
 int user_config_home(char **config_home) {
1ff636
         const char *e;
1ff636
diff --git a/src/shared/path-lookup.h b/src/shared/path-lookup.h
181b3f
index 2ec888da8..f1925eef6 100644
1ff636
--- a/src/shared/path-lookup.h
1ff636
+++ b/src/shared/path-lookup.h
1ff636
@@ -22,7 +22,8 @@
1ff636
 ***/
1ff636
 
1ff636
 #include "macro.h"
1ff636
-#include "install.h"
1ff636
+
1ff636
+typedef enum UnitFileScope UnitFileScope;
1ff636
 
1ff636
 typedef struct LookupPaths {
1ff636
         char **unit_path;
1ff636
diff --git a/src/sysv-generator/sysv-generator.c b/src/sysv-generator/sysv-generator.c
181b3f
index 6e39b449e..0125ca27d 100644
1ff636
--- a/src/sysv-generator/sysv-generator.c
1ff636
+++ b/src/sysv-generator/sysv-generator.c
1ff636
@@ -723,10 +723,10 @@ static int fix_order(SysvStub *s, Hashmap *all_services) {
1ff636
         return 0;
1ff636
 }
1ff636
 
1ff636
-static int enumerate_sysv(LookupPaths lp, Hashmap *all_services) {
1ff636
+static int enumerate_sysv(const LookupPaths *lp, Hashmap *all_services) {
1ff636
         char **path;
1ff636
 
1ff636
-        STRV_FOREACH(path, lp.sysvinit_path) {
1ff636
+        STRV_FOREACH(path, lp->sysvinit_path) {
1ff636
                 _cleanup_closedir_ DIR *d = NULL;
1ff636
                 struct dirent *de;
1ff636
 
1ff636
@@ -768,7 +768,7 @@ static int enumerate_sysv(LookupPaths lp, Hashmap *all_services) {
1ff636
                         if (!fpath)
1ff636
                                 return log_oom();
1ff636
 
1ff636
-                        if (unit_file_get_state(UNIT_FILE_SYSTEM, NULL, name) >= 0) {
1ff636
+                        if (unit_file_lookup_state(UNIT_FILE_SYSTEM, NULL, lp, name) >= 0) {
1ff636
                                 log_debug("Native unit for %s already exists, skipping", name);
1ff636
                                 continue;
1ff636
                         }
1ff636
@@ -793,7 +793,7 @@ static int enumerate_sysv(LookupPaths lp, Hashmap *all_services) {
1ff636
         return 0;
1ff636
 }
1ff636
 
1ff636
-static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) {
1ff636
+static int set_dependencies_from_rcnd(const LookupPaths *lp, Hashmap *all_services) {
1ff636
         char **p;
1ff636
         unsigned i;
1ff636
         _cleanup_closedir_ DIR *d = NULL;
1ff636
@@ -804,7 +804,7 @@ static int set_dependencies_from_rcnd(LookupPaths lp, Hashmap *all_services) {
1ff636
         _cleanup_set_free_ Set *shutdown_services = NULL;
1ff636
         int r = 0;
1ff636
 
1ff636
-        STRV_FOREACH(p, lp.sysvrcnd_path)
1ff636
+        STRV_FOREACH(p, lp->sysvrcnd_path)
1ff636
                 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
1ff636
                         struct dirent *de;
1ff636
 
1ff636
@@ -954,13 +954,13 @@ int main(int argc, char *argv[]) {
1ff636
                 return EXIT_FAILURE;
1ff636
         }
1ff636
 
1ff636
-        r = enumerate_sysv(lp, all_services);
1ff636
+        r = enumerate_sysv(&lp, all_services);
1ff636
         if (r < 0) {
1ff636
                 log_error("Failed to generate units for all init scripts.");
1ff636
                 return EXIT_FAILURE;
1ff636
         }
1ff636
 
1ff636
-        r = set_dependencies_from_rcnd(lp, all_services);
1ff636
+        r = set_dependencies_from_rcnd(&lp, all_services);
1ff636
         if (r < 0) {
1ff636
                 log_error("Failed to read runlevels from rcnd links.");
1ff636
                 return EXIT_FAILURE;