chiark / gitweb /
path-util: unify code for detecting OS trees
authorLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:47:04 +0000 (05:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 16 Apr 2013 03:47:04 +0000 (05:47 +0200)
This also makes sure we always detect an OS tree the same way, by
checking for /etc/os-release.

src/core/dbus-manager.c
src/nspawn/nspawn.c
src/shared/path-util.c
src/shared/path-util.h
src/shared/util.h

index c23709c2b618f7f32ba0d5ead8fe8cbd7ccff91a..d767dd5236ba70247399f7fb89797a641259ec61 100644 (file)
@@ -1481,7 +1481,7 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SwitchRoot")) {
                 const char *switch_root, *switch_root_init;
                 char *u, *v;
         } else if (dbus_message_is_method_call(message, "org.freedesktop.systemd1.Manager", "SwitchRoot")) {
                 const char *switch_root, *switch_root_init;
                 char *u, *v;
-                int k;
+                bool good;
 
                 SELINUX_ACCESS_CHECK(connection, message, "reboot");
 
 
                 SELINUX_ACCESS_CHECK(connection, message, "reboot");
 
@@ -1506,19 +1506,18 @@ static DBusHandlerResult bus_manager_message_handler(DBusConnection *connection,
 
                 /* Safety check */
                 if (isempty(switch_root_init))
 
                 /* Safety check */
                 if (isempty(switch_root_init))
-                        k = access(switch_root, F_OK);
+                        good = path_is_os_tree(switch_root);
                 else {
                 else {
-                        char *p;
+                        _cleanup_free_ char *p = NULL;
 
                         p = strjoin(switch_root, "/", switch_root_init, NULL);
                         if (!p)
                                 goto oom;
 
 
                         p = strjoin(switch_root, "/", switch_root_init, NULL);
                         if (!p)
                                 goto oom;
 
-                        k = access(p, X_OK);
-                        free(p);
+                        good = access(p, X_OK) >= 0;
                 }
                 }
-                if (k < 0)
-                        return bus_send_error_reply(connection, message, NULL, -errno);
+                if (!good)
+                        return bus_send_error_reply(connection, message, NULL, -EINVAL);
 
                 u = strdup(switch_root);
                 if (!u)
 
                 u = strdup(switch_root);
                 if (!u)
index c25768245179cad1e2a1be819395d8ab4b65d7b5..416d4a69b494cc126a7f03908ace34d1b0bd2a78 100644 (file)
@@ -918,20 +918,6 @@ static int drop_capabilities(void) {
         return capability_bounding_set_drop(~arg_retain, false);
 }
 
         return capability_bounding_set_drop(~arg_retain, false);
 }
 
-static int is_os_tree(const char *path) {
-        int r;
-        char *p;
-        /* We use /bin/sh as flag file if something is an OS */
-
-        if (asprintf(&p, "%s/bin/sh", path) < 0)
-                return -ENOMEM;
-
-        r = access(p, F_OK);
-        free(p);
-
-        return r < 0 ? 0 : 1;
-}
-
 static int process_pty(int master, pid_t pid, sigset_t *mask) {
 
         char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
 static int process_pty(int master, pid_t pid, sigset_t *mask) {
 
         char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
@@ -1240,7 +1226,7 @@ int main(int argc, char *argv[]) {
                 goto finish;
         }
 
                 goto finish;
         }
 
-        if (is_os_tree(arg_directory) <= 0) {
+        if (path_is_os_tree(arg_directory) <= 0) {
                 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
                 goto finish;
         }
                 log_error("Directory %s doesn't look like an OS root directory. Refusing.", arg_directory);
                 goto finish;
         }
index 0b50ea646aa984a3a1a64b15946f438ac17527e1..b623fc3dd5adeb9cad7034ee05425b73dd29ab7b 100644 (file)
@@ -413,3 +413,15 @@ int path_is_read_only_fs(const char *path) {
 
         return !!(st.f_flag & ST_RDONLY);
 }
 
         return !!(st.f_flag & ST_RDONLY);
 }
+
+int path_is_os_tree(const char *path) {
+        char *p;
+        int r;
+
+        /* We use /etc/os-release as flag file if something is an OS */
+
+        p = strappenda(path, "/etc/os-release");
+        r = access(p, F_OK);
+
+        return r < 0 ? 0 : 1;
+}
index 9347bc3a9b28dfb9f387fc710439140fc706641a..ea0f1730826dbc6082ef522b87d695cc53d76dcb 100644 (file)
@@ -40,3 +40,4 @@ char **path_strv_canonicalize_uniq(char **l);
 
 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);
+int path_is_os_tree(const char *path);
index 3aac165e67d0792f11857bb1f830783bf1ab2f36..cfb54939cd5d21730b4adcc3cf96a915a9dd58d8 100644 (file)
@@ -684,3 +684,15 @@ int unlink_noerrno(const char *path);
                 _new_ = alloca(_len_);                  \
                 (void *) memset(_new_, 0, _len_);       \
         })
                 _new_ = alloca(_len_);                  \
                 (void *) memset(_new_, 0, _len_);       \
         })
+
+#define strappenda(a, b)                                \
+        ({                                              \
+                const char *_a_ = (a), *_b_ = (b);      \
+                char *_c_;                              \
+                size_t _x_, _y_;                        \
+                _x_ = strlen(_a_);                      \
+                _y_ = strlen(_b_);                      \
+                _c_ = alloca(_x_ + _y_ + 1);            \
+                strcpy(stpcpy(_c_, _a_), _b_);          \
+                _c_;                                    \
+        })