chiark / gitweb /
shared: include root when canonicalizing conf paths
authorMichael Marineau <michael.marineau@coreos.com>
Fri, 31 Jan 2014 23:35:04 +0000 (15:35 -0800)
committerLennart Poettering <lennart@poettering.net>
Thu, 13 Feb 2014 23:58:00 +0000 (00:58 +0100)
The conf_files_list family accepts an alternate root path to prefix all
directories in the list but path_strv_canonicalize_uniq doesn't use it.
This results in the suspicious behavior of resolving directory symlinks
based on the contents of / instead of the alternate root.

This adds a prefix argument to path_strv_canonicalize which will now
prepend the prefix, if given, to every path in the list. To avoid
answering what a relative path means when called with a root prefix
path_strv_canonicalize is now path_strv_canonicalize_absolute and only
considers absolute paths. Fortunately all users of already call
path_strv_canonicalize with a list of absolute paths.

src/shared/conf-files.c
src/shared/path-lookup.c
src/shared/path-util.c
src/shared/path-util.h
src/shared/util.c

index c86bb03074474006247516019cbd41bb2175d457..52017821834f4663b540371e34561056e51af81d 100644 (file)
 #include "hashmap.h"
 #include "conf-files.h"
 
 #include "hashmap.h"
 #include "conf-files.h"
 
-static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
+static int files_add(Hashmap *h, const char *dirpath, const char *suffix) {
         _cleanup_closedir_ DIR *dir = NULL;
         _cleanup_closedir_ DIR *dir = NULL;
-        _cleanup_free_ char *dirpath = NULL;
-
-        if (asprintf(&dirpath, "%s%s", root ? root : "", path) < 0)
-                return -ENOMEM;
 
         dir = opendir(dirpath);
         if (!dir) {
 
         dir = opendir(dirpath);
         if (!dir) {
@@ -104,7 +100,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
         assert(suffix);
 
         /* This alters the dirs string array */
         assert(suffix);
 
         /* This alters the dirs string array */
-        if (!path_strv_canonicalize_uniq(dirs))
+        if (!path_strv_canonicalize_absolute_uniq(dirs, root))
                 return -ENOMEM;
 
         fh = hashmap_new(string_hash_func, string_compare_func);
                 return -ENOMEM;
 
         fh = hashmap_new(string_hash_func, string_compare_func);
@@ -112,7 +108,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
                 return -ENOMEM;
 
         STRV_FOREACH(p, dirs) {
                 return -ENOMEM;
 
         STRV_FOREACH(p, dirs) {
-                r = files_add(fh, root, *p, suffix);
+                r = files_add(fh, *p, suffix);
                 if (r == -ENOMEM) {
                         hashmap_free_free(fh);
                         return r;
                 if (r == -ENOMEM) {
                         hashmap_free_free(fh);
                         return r;
index e2ca9420d97c5335a321e47ccd79a136d6f6a2e9..63af43cdbb0de55542e54e54b88f0321a5d8bb70 100644 (file)
@@ -275,7 +275,7 @@ int lookup_paths_init(
                 }
         }
 
                 }
         }
 
-        if (!path_strv_canonicalize(p->unit_path))
+        if (!path_strv_canonicalize_absolute(p->unit_path, NULL))
                 return -ENOMEM;
 
         strv_uniq(p->unit_path);
                 return -ENOMEM;
 
         strv_uniq(p->unit_path);
@@ -331,10 +331,10 @@ int lookup_paths_init(
                                 return -ENOMEM;
                 }
 
                                 return -ENOMEM;
                 }
 
-                if (!path_strv_canonicalize(p->sysvinit_path))
+                if (!path_strv_canonicalize_absolute(p->sysvinit_path, NULL))
                         return -ENOMEM;
 
                         return -ENOMEM;
 
-                if (!path_strv_canonicalize(p->sysvrcnd_path))
+                if (!path_strv_canonicalize_absolute(p->sysvrcnd_path, NULL))
                         return -ENOMEM;
 
                 strv_uniq(p->sysvinit_path);
                         return -ENOMEM;
 
                 strv_uniq(p->sysvinit_path);
index fc42a704b7fd77742da60639333e848452e90d6b..bdc54a9aa5f9a23d0dbfd89bcc801eee14564df3 100644 (file)
@@ -153,7 +153,7 @@ char **path_strv_make_absolute_cwd(char **l) {
         return l;
 }
 
         return l;
 }
 
-char **path_strv_canonicalize(char **l) {
+char **path_strv_canonicalize_absolute(char **l, const char *prefix) {
         char **s;
         unsigned k = 0;
         bool enomem = false;
         char **s;
         unsigned k = 0;
         bool enomem = false;
@@ -168,13 +168,21 @@ char **path_strv_canonicalize(char **l) {
         STRV_FOREACH(s, l) {
                 char *t, *u;
 
         STRV_FOREACH(s, l) {
                 char *t, *u;
 
-                t = path_make_absolute_cwd(*s);
-                free(*s);
-                *s = NULL;
-
-                if (!t) {
-                        enomem = true;
+                if (!path_is_absolute(*s))
                         continue;
                         continue;
+
+                if (prefix) {
+                        t = strappend(prefix, *s);
+                        free(*s);
+                        *s = NULL;
+
+                        if (!t) {
+                                enomem = true;
+                                continue;
+                        }
+                } else {
+                        t = *s;
+                        *s = NULL;
                 }
 
                 errno = 0;
                 }
 
                 errno = 0;
@@ -184,7 +192,7 @@ char **path_strv_canonicalize(char **l) {
                                 u = t;
                         else {
                                 free(t);
                                 u = t;
                         else {
                                 free(t);
-                                if (errno == ENOMEM || !errno)
+                                if (errno == ENOMEM || errno == 0)
                                         enomem = true;
 
                                 continue;
                                         enomem = true;
 
                                 continue;
@@ -203,11 +211,12 @@ char **path_strv_canonicalize(char **l) {
         return l;
 }
 
         return l;
 }
 
-char **path_strv_canonicalize_uniq(char **l) {
+char **path_strv_canonicalize_absolute_uniq(char **l, const char *prefix) {
+
         if (strv_isempty(l))
                 return l;
 
         if (strv_isempty(l))
                 return l;
 
-        if (!path_strv_canonicalize(l))
+        if (!path_strv_canonicalize_absolute(l, prefix))
                 return NULL;
 
         return strv_uniq(l);
                 return NULL;
 
         return strv_uniq(l);
index 178bed5f175384e595b1cd711c718666fd282310..2b8ea0260e0c4a3296e3ace88dba2eb29bc54446 100644 (file)
@@ -46,8 +46,8 @@ char* path_startswith(const char *path, const char *prefix) _pure_;
 bool path_equal(const char *a, const char *b) _pure_;
 
 char** path_strv_make_absolute_cwd(char **l);
 bool path_equal(const char *a, const char *b) _pure_;
 
 char** path_strv_make_absolute_cwd(char **l);
-char** path_strv_canonicalize(char **l);
-char** path_strv_canonicalize_uniq(char **l);
+char** path_strv_canonicalize_absolute(char **l, const char *prefix);
+char** path_strv_canonicalize_absolute_uniq(char **l, const char *prefix);
 
 int path_is_mount_point(const char *path, bool allow_symlink);
 int path_is_read_only_fs(const char *path);
 
 int path_is_mount_point(const char *path, bool allow_symlink);
 int path_is_read_only_fs(const char *path);
index 9be11f5605c829fa7016bffd07fe36e88ac1e6dc..3482b9b743b8e1cc7861dcf8c746807cc7dcd6eb 100644 (file)
@@ -5613,7 +5613,7 @@ static int search_and_fopen_internal(const char *path, const char *mode, char **
         assert(mode);
         assert(_f);
 
         assert(mode);
         assert(_f);
 
-        if (!path_strv_canonicalize_uniq(search))
+        if (!path_strv_canonicalize_absolute_uniq(search, NULL))
                 return -ENOMEM;
 
         STRV_FOREACH(i, search) {
                 return -ENOMEM;
 
         STRV_FOREACH(i, search) {