]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/install.c
util-lib: introduce dirent-util.[ch] for directory entry calls
[thirdparty/systemd.git] / src / shared / install.c
index 9648c641d70e824952ad9354a1e9952cdfd945b6..bfafb59008e30a45a050a3e700fbd14e4539eeac 100644 (file)
 
 #include <errno.h>
 #include <fcntl.h>
-#include <unistd.h>
-#include <string.h>
 #include <fnmatch.h>
+#include <string.h>
+#include <unistd.h>
 
-#include "util.h"
-#include "mkdir.h"
+#include "conf-files.h"
+#include "conf-parser.h"
+#include "dirent-util.h"
+#include "fd-util.h"
 #include "hashmap.h"
-#include "set.h"
-#include "path-util.h"
+#include "install-printf.h"
+#include "install.h"
+#include "mkdir.h"
 #include "path-lookup.h"
+#include "path-util.h"
+#include "set.h"
+#include "special.h"
+#include "string-util.h"
 #include "strv.h"
 #include "unit-name.h"
-#include "install.h"
-#include "conf-parser.h"
-#include "conf-files.h"
-#include "install-printf.h"
-#include "special.h"
+#include "util.h"
 
 typedef struct {
         OrderedHashmap *will_install;
@@ -46,13 +49,12 @@ typedef struct {
 
 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;
+        parent = dirname_malloc(path);
+        if (!parent)
+                return -ENOMEM;
 
         return strv_contains(search, parent);
 }
@@ -112,51 +114,6 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
         return 0;
 }
 
-static int add_file_change(
-                UnitFileChange **changes,
-                unsigned *n_changes,
-                UnitFileChangeType type,
-                const char *path,
-                const char *source) {
-
-        UnitFileChange *c;
-        unsigned i;
-
-        assert(path);
-        assert(!changes == !n_changes);
-
-        if (!changes)
-                return 0;
-
-        c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
-        if (!c)
-                return -ENOMEM;
-
-        *changes = c;
-        i = *n_changes;
-
-        c[i].type = type;
-        c[i].path = strdup(path);
-        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;
-
-        *n_changes = i+1;
-        return 0;
-}
-
 static int mark_symlink_for_removal(
                 Set **remove_symlinks_to,
                 const char *p) {
@@ -265,7 +222,7 @@ static int remove_marked_symlinks_fd(
                             instance_whitelist &&
                             !strv_contains(instance_whitelist, de->d_name)) {
 
-                                _cleanup_free_ char *w;
+                                _cleanup_free_ char *w = NULL;
 
                                 /* OK, the file is not listed directly
                                  * in the whitelist, so let's check if
@@ -309,7 +266,7 @@ static int remove_marked_symlinks_fd(
 
                         path_kill_slashes(p);
                         rmdir_parents(p, config_path);
-                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
 
                         if (!set_get(remove_symlinks_to, p)) {
 
@@ -596,7 +553,7 @@ int unit_file_mask(
                 }
 
                 if (symlink("/dev/null", path) >= 0) {
-                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
                         continue;
                 }
 
@@ -607,8 +564,8 @@ int unit_file_mask(
 
                         if (force) {
                                 if (symlink_atomic("/dev/null", path) >= 0) {
-                                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
-                                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
                                         continue;
                                 }
                         }
@@ -664,7 +621,7 @@ int unit_file_unmask(
                                 q = -errno;
                         else {
                                 q = mark_symlink_for_removal(&remove_symlinks_to, path);
-                                add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
+                                unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
                         }
                 }
 
@@ -746,7 +703,7 @@ int unit_file_link(
                         return -ENOMEM;
 
                 if (symlink(*i, path) >= 0) {
-                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
+                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
                         continue;
                 }
 
@@ -765,8 +722,8 @@ int unit_file_link(
 
                         if (force) {
                                 if (symlink_atomic(*i, path) >= 0) {
-                                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
-                                        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, path, NULL);
+                                        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, path, *i);
                                         continue;
                                 }
                         }
@@ -793,6 +750,51 @@ void unit_file_list_free(Hashmap *h) {
         hashmap_free(h);
 }
 
+int unit_file_changes_add(
+                UnitFileChange **changes,
+                unsigned *n_changes,
+                UnitFileChangeType type,
+                const char *path,
+                const char *source) {
+
+        UnitFileChange *c;
+        unsigned i;
+
+        assert(path);
+        assert(!changes == !n_changes);
+
+        if (!changes)
+                return 0;
+
+        c = realloc(*changes, (*n_changes + 1) * sizeof(UnitFileChange));
+        if (!c)
+                return -ENOMEM;
+
+        *changes = c;
+        i = *n_changes;
+
+        c[i].type = type;
+        c[i].path = strdup(path);
+        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;
+
+        *n_changes = i+1;
+        return 0;
+}
+
 void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
         unsigned i;
 
@@ -809,7 +811,7 @@ void unit_file_changes_free(UnitFileChange *changes, unsigned n_changes) {
         free(changes);
 }
 
-static void install_info_free(InstallInfo *i) {
+static void install_info_free(UnitFileInstallInfo *i) {
         assert(i);
 
         free(i->name);
@@ -823,7 +825,7 @@ static void install_info_free(InstallInfo *i) {
 }
 
 static void install_info_hashmap_free(OrderedHashmap *m) {
-        InstallInfo *i;
+        UnitFileInstallInfo *i;
 
         if (!m)
                 return;
@@ -847,7 +849,7 @@ static int install_info_add(
                 InstallContext *c,
                 const char *name,
                 const char *path) {
-        InstallInfo *i = NULL;
+        UnitFileInstallInfo *i = NULL;
         int r;
 
         assert(c);
@@ -867,7 +869,7 @@ static int install_info_add(
         if (r < 0)
                 return r;
 
-        i = new0(InstallInfo, 1);
+        i = new0(UnitFileInstallInfo, 1);
         if (!i)
                 return -ENOMEM;
 
@@ -926,7 +928,7 @@ static int config_parse_also(
         size_t l;
         const char *word, *state;
         InstallContext *c = data;
-        InstallInfo *i = userdata;
+        UnitFileInstallInfo *i = userdata;
 
         assert(filename);
         assert(lvalue);
@@ -949,8 +951,7 @@ static int config_parse_also(
                         return r;
         }
         if (!isempty(state))
-                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
-                           "Trailing garbage, ignoring.");
+                log_syntax(unit, LOG_ERR, filename, line, 0, "Trailing garbage, ignoring.");
 
         return 0;
 }
@@ -967,7 +968,7 @@ static int config_parse_user(
                 void *data,
                 void *userdata) {
 
-        InstallInfo *i = data;
+        UnitFileInstallInfo *i = data;
         char *printed;
         int r;
 
@@ -997,7 +998,7 @@ static int config_parse_default_instance(
                 void *data,
                 void *userdata) {
 
-        InstallInfo *i = data;
+        UnitFileInstallInfo *i = data;
         char *printed;
         int r;
 
@@ -1022,7 +1023,7 @@ static int config_parse_default_instance(
 
 static int unit_file_load(
                 InstallContext *c,
-                InstallInfo *info,
+                UnitFileInstallInfo *info,
                 const char *path,
                 const char *root_dir,
                 bool allow_symlink,
@@ -1082,7 +1083,7 @@ static int unit_file_load(
 
 static int unit_file_search(
                 InstallContext *c,
-                InstallInfo *info,
+                UnitFileInstallInfo *info,
                 const LookupPaths *paths,
                 const char *root_dir,
                 bool allow_symlink,
@@ -1159,7 +1160,7 @@ static int unit_file_can_install(
                 bool *also) {
 
         _cleanup_(install_context_done) InstallContext c = {};
-        InstallInfo *i;
+        UnitFileInstallInfo *i;
         int r;
 
         assert(paths);
@@ -1198,7 +1199,7 @@ static int create_symlink(
         mkdir_parents_label(new_path, 0755);
 
         if (symlink(old_path, new_path) >= 0) {
-                add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
+                unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
                 return 0;
         }
 
@@ -1219,14 +1220,14 @@ static int create_symlink(
         if (r < 0)
                 return r;
 
-        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
-        add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
+        unit_file_changes_add(changes, n_changes, UNIT_FILE_UNLINK, new_path, NULL);
+        unit_file_changes_add(changes, n_changes, UNIT_FILE_SYMLINK, new_path, old_path);
 
         return 0;
 }
 
 static int install_info_symlink_alias(
-                InstallInfo *i,
+                UnitFileInstallInfo *i,
                 const char *config_path,
                 bool force,
                 UnitFileChange **changes,
@@ -1258,7 +1259,7 @@ static int install_info_symlink_alias(
 }
 
 static int install_info_symlink_wants(
-                InstallInfo *i,
+                UnitFileInstallInfo *i,
                 const char *config_path,
                 char **list,
                 const char *suffix,
@@ -1315,7 +1316,7 @@ static int install_info_symlink_wants(
 }
 
 static int install_info_symlink_link(
-                InstallInfo *i,
+                UnitFileInstallInfo *i,
                 const LookupPaths *paths,
                 const char *config_path,
                 const char *root_dir,
@@ -1343,7 +1344,7 @@ static int install_info_symlink_link(
 }
 
 static int install_info_apply(
-                InstallInfo *i,
+                UnitFileInstallInfo *i,
                 const LookupPaths *paths,
                 const char *config_path,
                 const char *root_dir,
@@ -1383,7 +1384,7 @@ static int install_context_apply(
                 UnitFileChange **changes,
                 unsigned *n_changes) {
 
-        InstallInfo *i;
+        UnitFileInstallInfo *i;
         int r, q;
 
         assert(c);
@@ -1428,7 +1429,7 @@ static int install_context_mark_for_removal(
                 const char *config_path,
                 const char *root_dir) {
 
-        InstallInfo *i;
+        UnitFileInstallInfo *i;
         int r, q;
 
         assert(c);
@@ -1513,7 +1514,7 @@ int unit_file_add_dependency(
         _cleanup_free_ char *config_path = NULL;
         char **i;
         int r;
-        InstallInfo *info;
+        UnitFileInstallInfo *info;
 
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
@@ -1702,7 +1703,7 @@ int unit_file_set_default(
         _cleanup_free_ char *config_path = NULL;
         char *path;
         int r;
-        InstallInfo *i = NULL;
+        UnitFileInstallInfo *i = NULL;
 
         assert(scope >= 0);
         assert(scope < _UNIT_FILE_SCOPE_MAX);
@@ -2190,6 +2191,7 @@ int unit_file_get_list(
                         _cleanup_(unit_file_list_free_onep) UnitFileList *f = NULL;
                         struct dirent *de;
                         _cleanup_free_ char *path = NULL;
+                        bool also = false;
 
                         errno = 0;
                         de = readdir(d);
@@ -2243,7 +2245,7 @@ int unit_file_get_list(
                         if (!path)
                                 return -ENOMEM;
 
-                        r = unit_file_can_install(&paths, root_dir, path, true, NULL);
+                        r = unit_file_can_install(&paths, root_dir, path, true, &also);
                         if (r == -EINVAL ||  /* Invalid setting? */
                             r == -EBADMSG || /* Invalid format? */
                             r == -ENOENT     /* Included file not found? */)
@@ -2253,7 +2255,7 @@ int unit_file_get_list(
                         else if (r > 0)
                                 f->state = UNIT_FILE_DISABLED;
                         else
-                                f->state = UNIT_FILE_STATIC;
+                                f->state = also ? UNIT_FILE_INDIRECT : UNIT_FILE_STATIC;
 
                 found:
                         r = hashmap_put(h, basename(f->path), f);
@@ -2263,7 +2265,7 @@ int unit_file_get_list(
                 }
         }
 
-        return r;
+        return 0;
 }
 
 static const char* const unit_file_state_table[_UNIT_FILE_STATE_MAX] = {