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