chiark / gitweb /
Add utility function to append root to path
[elogind.git] / src / shared / install.c
index 1e7863acbf45efad707fe80b8abfd05524ef9007..cc61c01e20d05ad2587606a3fa725f6e45de4037 100644 (file)
@@ -47,6 +47,19 @@ typedef struct {
 
 #define _cleanup_install_context_done_ _cleanup_(install_context_done)
 
+static int in_search_path(const char *path, char **search) {
+        _cleanup_free_ char *parent = NULL;
+        int r;
+
+        assert(path);
+
+        r = path_get_parent(path, &parent);
+        if (r < 0)
+                return r;
+
+        return strv_contains(search, parent);
+}
+
 static int lookup_paths_init_from_scope(LookupPaths *paths,
                                         UnitFileScope scope,
                                         const char *root_dir) {
@@ -75,15 +88,10 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
 
         case UNIT_FILE_SYSTEM:
 
-                if (root_dir && runtime)
-                        asprintf(&p, "%s/run/systemd/system", root_dir);
-                else if (runtime)
-                        p = strdup("/run/systemd/system");
-                else if (root_dir)
-                        asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
+                if (runtime)
+                        p = path_join(root_dir, "/run/systemd/system", NULL);
                 else
-                        p = strdup(SYSTEM_CONFIG_UNIT_PATH);
-
+                        p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
                 break;
 
         case UNIT_FILE_GLOBAL:
@@ -147,12 +155,16 @@ static int add_file_change(
         if (!c[i].path)
                 return -ENOMEM;
 
+        path_kill_slashes(c[i].path);
+
         if (source) {
                 c[i].source = strdup(source);
                 if (!c[i].source) {
                         free(c[i].path);
                         return -ENOMEM;
                 }
+
+                path_kill_slashes(c[i].path);
         } else
                 c[i].source = NULL;
 
@@ -266,8 +278,22 @@ static int remove_marked_symlinks_fd(
 
                         if (unit_name_is_instance(de->d_name) &&
                             instance_whitelist &&
-                            !strv_contains(instance_whitelist, de->d_name))
-                                continue;
+                            !strv_contains(instance_whitelist, de->d_name)) {
+
+                                _cleanup_free_ char *w;
+
+                                /* OK, the file is not listed directly
+                                 * in the whitelist, so let's check if
+                                 * the template of it might be
+                                 * listed. */
+
+                                w = unit_name_template(de->d_name);
+                                if (!w)
+                                        return -ENOMEM;
+
+                                if (!strv_contains(instance_whitelist, w))
+                                        continue;
+                        }
 
                         p = path_make_absolute(de->d_name, path);
                         if (!p)
@@ -290,18 +316,14 @@ static int remove_marked_symlinks_fd(
                         if (!found)
                                 continue;
 
-
                         if (unlink(p) < 0 && errno != ENOENT) {
-
                                 if (r == 0)
                                         r = -errno;
                                 continue;
                         }
 
-                        rmdir_parents(p, config_path);
-
                         path_kill_slashes(p);
-
+                        rmdir_parents(p, config_path);
                         add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
 
                         if (!set_get(remove_symlinks_to, p)) {
@@ -1016,6 +1038,7 @@ static int unit_file_load(
                 InstallContext *c,
                 InstallInfo *info,
                 const char *path,
+                const char *root_dir,
                 bool allow_symlink) {
 
         const ConfigTableItem items[] = {
@@ -1028,14 +1051,16 @@ static int unit_file_load(
                 {}
         };
 
-        int fd;
         _cleanup_fclose_ FILE *f = NULL;
-        int r;
+        int fd, r;
 
         assert(c);
         assert(info);
         assert(path);
 
+        if (!isempty(root_dir))
+                path = strappenda3(root_dir, "/", path);
+
         fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
         if (fd < 0)
                 return -errno;
@@ -1046,7 +1071,10 @@ static int unit_file_load(
                 return -ENOMEM;
         }
 
-        r = config_parse(NULL, path, f, NULL, config_item_table_lookup, (void*) items, true, true, info);
+        r = config_parse(NULL, path, f,
+                         NULL,
+                         config_item_table_lookup, items,
+                         true, true, false, info);
         if (r < 0)
                 return r;
 
@@ -1070,30 +1098,19 @@ static int unit_file_search(
         assert(info);
         assert(paths);
 
-        if (info->path) {
-                const char *path;
-
-                if (isempty(root_dir))
-                        path = info->path;
-                else
-                        path = strappenda(root_dir, info->path);
-
-                return unit_file_load(c, info, path, allow_symlink);
-        }
+        if (info->path)
+                return unit_file_load(c, info, info->path, root_dir, allow_symlink);
 
         assert(info->name);
 
         STRV_FOREACH(p, paths->unit_path) {
                 _cleanup_free_ char *path = NULL;
 
-                if (isempty(root_dir))
-                        path = strjoin(*p, "/", info->name, NULL);
-                else
-                        path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
+                path = strjoin(*p, "/", info->name, NULL);
                 if (!path)
                         return -ENOMEM;
 
-                r = unit_file_load(c, info, path, allow_symlink);
+                r = unit_file_load(c, info, path, root_dir, allow_symlink);
                 if (r >= 0) {
                         info->path = path;
                         path = NULL;
@@ -1109,7 +1126,7 @@ static int unit_file_search(
                  * enablement was requested.  We will check if it is
                  * possible to load template unit file. */
 
-                _cleanup_free_ char *template = NULL, *template_dir = NULL;
+                _cleanup_free_ char *template = NULL;
 
                 template = unit_name_template(info->name);
                 if (!template)
@@ -1118,14 +1135,11 @@ static int unit_file_search(
                 STRV_FOREACH(p, paths->unit_path) {
                         _cleanup_free_ char *path = NULL;
 
-                        if (isempty(root_dir))
-                                path = strjoin(*p, "/", template, NULL);
-                        else
-                                path = strjoin(root_dir, "/", *p, "/", template, NULL);
+                        path = strjoin(*p, "/", template, NULL);
                         if (!path)
                                 return -ENOMEM;
 
-                        r = unit_file_load(c, info, path, allow_symlink);
+                        r = unit_file_load(c, info, path, root_dir, allow_symlink);
                         if (r >= 0) {
                                 info->path = path;
                                 path = NULL;
@@ -1261,7 +1275,14 @@ static int install_info_symlink_wants(
         assert(i);
         assert(config_path);
 
-        if (unit_name_is_template(i->name) && i->default_instance) {
+        if (unit_name_is_template(i->name)) {
+
+                /* Don't install any symlink if there's no default
+                 * instance configured */
+
+                if (!i->default_instance)
+                        return 0;
+
                 buf = unit_name_replace_instance(i->name, i->default_instance);
                 if (!buf)
                         return -ENOMEM;
@@ -1298,6 +1319,7 @@ static int install_info_symlink_link(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1325,6 +1347,7 @@ static int install_info_apply(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1345,7 +1368,7 @@ static int install_info_apply(
         if (r == 0)
                 r = q;
 
-        q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes);
+        q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
         if (r == 0)
                 r = q;
 
@@ -1385,7 +1408,7 @@ static int install_context_apply(
                 } else if (r >= 0)
                         r += q;
 
-                q = install_info_apply(i, paths, config_path, force, changes, n_changes);
+                q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
                 if (r >= 0 && q < 0)
                         r = q;
         }
@@ -1633,11 +1656,7 @@ int unit_file_get_default(
                 _cleanup_free_ char *path = NULL, *tmp = NULL;
                 char *n;
 
-                if (isempty(root_dir))
-                        path = strappend(*p, "/" SPECIAL_DEFAULT_TARGET);
-                else
-                        path = strjoin(root_dir, "/", *p, "/" SPECIAL_DEFAULT_TARGET, NULL);
-
+                path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
                 if (!path)
                         return -ENOMEM;
 
@@ -1694,15 +1713,12 @@ UnitFileState unit_file_get_state(
                 free(path);
                 path = NULL;
 
-                if (root_dir)
-                        asprintf(&path, "%s/%s/%s", root_dir, *i, name);
-                else
-                        asprintf(&path, "%s/%s", *i, name);
+                path = path_join(root_dir, *i, name);
                 if (!path)
                         return -ENOMEM;
 
                 if (root_dir)
-                        partial = path + strlen(root_dir) + 1;
+                        partial = path + strlen(root_dir);
                 else
                         partial = path;
 
@@ -1751,7 +1767,7 @@ UnitFileState unit_file_get_state(
 
 int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
         _cleanup_strv_free_ char **files = NULL;
-        char **i;
+        char **p;
         int r;
 
         assert(scope >= 0);
@@ -1779,17 +1795,10 @@ int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char
         if (r < 0)
                 return r;
 
-        STRV_FOREACH(i, files) {
-                _cleanup_free_ char *buf = NULL;
+        STRV_FOREACH(p, files) {
                 _cleanup_fclose_ FILE *f;
-                const char *p;
 
-                if (root_dir)
-                        p = buf = strjoin(root_dir, "/", *i, NULL);
-                else
-                        p = *i;
-
-                f = fopen(p, "re");
+                f = fopen(*p, "re");
                 if (!f) {
                         if (errno == ENOENT)
                                 continue;
@@ -1856,7 +1865,7 @@ int unit_file_preset(
 
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
-        assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
+        assert(mode < _UNIT_FILE_PRESET_MAX);
 
         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
         if (r < 0)
@@ -1924,7 +1933,7 @@ int unit_file_preset_all(
 
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
-        assert(mode < _UNIT_FILE_PRESET_MODE_MAX);
+        assert(mode < _UNIT_FILE_PRESET_MAX);
 
         r = lookup_paths_init_from_scope(&paths, scope, root_dir);
         if (r < 0)
@@ -1936,17 +1945,11 @@ int unit_file_preset_all(
 
         STRV_FOREACH(i, paths.unit_path) {
                 _cleanup_closedir_ DIR *d = NULL;
-                _cleanup_free_ char *buf = NULL;
-                const char *units_dir;
+                _cleanup_free_ char *units_dir;
 
-                if (!isempty(root_dir)) {
-                        buf = strjoin(root_dir, "/", *i, NULL);
-                        if (!buf)
-                                return -ENOMEM;
-
-                        units_dir = buf;
-                } else
-                        units_dir = *i;
+                units_dir = path_join(root_dir, *i, NULL);
+                if (!units_dir)
+                        return -ENOMEM;
 
                 d = opendir(units_dir);
                 if (!d) {
@@ -2045,17 +2048,11 @@ int unit_file_get_list(
 
         STRV_FOREACH(i, paths.unit_path) {
                 _cleanup_closedir_ DIR *d = NULL;
-                _cleanup_free_ char *buf = NULL;
-                const char *units_dir;
+                _cleanup_free_ char *units_dir;
 
-                if (!isempty(root_dir)) {
-                        buf = strjoin(root_dir, "/", *i, NULL);
-                        if (!buf)
-                                return -ENOMEM;
-
-                        units_dir = buf;
-                } else
-                        units_dir = *i;
+                units_dir = path_join(root_dir, *i, NULL);
+                if (!units_dir)
+                        return -ENOMEM;
 
                 d = opendir(units_dir);
                 if (!d) {
@@ -2161,7 +2158,7 @@ static const char* const unit_file_change_type_table[_UNIT_FILE_CHANGE_TYPE_MAX]
 
 DEFINE_STRING_TABLE_LOOKUP(unit_file_change_type, UnitFileChangeType);
 
-static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MODE_MAX] = {
+static const char* const unit_file_preset_mode_table[_UNIT_FILE_PRESET_MAX] = {
         [UNIT_FILE_PRESET_FULL] = "full",
         [UNIT_FILE_PRESET_ENABLE_ONLY] = "enable-only",
         [UNIT_FILE_PRESET_DISABLE_ONLY] = "disable-only",