summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-03-26 16:32:40 +0200
committerSven Eden <yamakuzure@gmx.net>2018-08-24 16:47:08 +0200
commit2cefb09ff69c3595e98c55f0da1bd137c612276c (patch)
tree413850b2eb4d2cd031c2d31ef66b657baf5d66d4 /src
parent1049fbda947f856942a693f011fe013a2ee798f0 (diff)
os-util: add helpers for finding /etc/os-release
Place this new helpers in a new source file os-util.[ch], and move the existing and related call path_is_os_tree() to it as well.
Diffstat (limited to 'src')
-rw-r--r--src/basic/meson.build3
-rw-r--r--src/basic/os-util.c117
-rw-r--r--src/basic/os-util.h12
-rw-r--r--src/basic/stat-util.c26
-rw-r--r--src/basic/stat-util.h1
-rw-r--r--src/test/meson.build11
-rw-r--r--src/test/test-os-util.c22
-rw-r--r--src/test/test-stat-util.c7
8 files changed, 164 insertions, 35 deletions
diff --git a/src/basic/meson.build b/src/basic/meson.build
index 5eef906d4..cd823c0f9 100644
--- a/src/basic/meson.build
+++ b/src/basic/meson.build
@@ -132,6 +132,8 @@
# ordered-set.h
# pager.c
# pager.h
+# os-util.c
+# os-util.h
# parse-util.c
# parse-util.h
# path-util.c
@@ -164,7 +166,6 @@
# securebits.h
# selinux-util.c
# selinux-util.h
-# set.c
# set.h
# sigbus.c
# sigbus.h
diff --git a/src/basic/os-util.c b/src/basic/os-util.c
new file mode 100644
index 000000000..a215b631d
--- /dev/null
+++ b/src/basic/os-util.c
@@ -0,0 +1,117 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+//#include "alloc-util.h"
+//#include "fd-util.h"
+//#include "fs-util.h"
+//#include "macro.h"
+//#include "os-util.h"
+//#include "strv.h"
+//#include "fileio.h"
+//#include "string-util.h"
+
+int path_is_os_tree(const char *path) {
+ int r;
+
+ assert(path);
+
+ /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir
+ * always results in -ENOENT, and we can properly distuingish the case where the whole root doesn't exist from
+ * the case where just the os-release file is missing. */
+ if (laccess(path, F_OK) < 0)
+ return -errno;
+
+ /* We use {/etc|/usr/lib}/os-release as flag file if something is an OS */
+ r = open_os_release(path, NULL, NULL);
+ if (r == -ENOENT) /* We got nothing */
+ return 0;
+ if (r < 0)
+ return r;
+
+ return 1;
+}
+
+int open_os_release(const char *root, char **ret_path, int *ret_fd) {
+ _cleanup_free_ char *q = NULL;
+ const char *p;
+ int k;
+
+ FOREACH_STRING(p, "/etc/os-release", "/usr/lib/os-release") {
+ k = chase_symlinks(p, root, CHASE_PREFIX_ROOT|(ret_fd ? CHASE_OPEN : 0), (ret_path ? &q : NULL));
+ if (k != -ENOENT)
+ break;
+ }
+ if (k < 0)
+ return k;
+
+ if (ret_fd) {
+ int real_fd;
+
+ /* Convert the O_PATH fd into a proper, readable one */
+ real_fd = fd_reopen(k, O_RDONLY|O_CLOEXEC|O_NOCTTY);
+ safe_close(k);
+ if (real_fd < 0)
+ return real_fd;
+
+ *ret_fd = real_fd;
+ }
+
+ if (ret_path)
+ *ret_path = TAKE_PTR(q);
+
+ return 0;
+}
+
+int fopen_os_release(const char *root, char **ret_path, FILE **ret_file) {
+ _cleanup_free_ char *p = NULL;
+ _cleanup_close_ int fd = -1;
+ FILE *f;
+ int r;
+
+ if (!ret_file)
+ return open_os_release(root, ret_path, NULL);
+
+ r = open_os_release(root, ret_path ? &p : NULL, &fd);
+ if (r < 0)
+ return r;
+
+ f = fdopen(fd, "re");
+ if (!f)
+ return -errno;
+ fd = -1;
+
+ *ret_file = f;
+
+ if (ret_path)
+ *ret_path = TAKE_PTR(p);
+
+ return 0;
+}
+
+int parse_os_release(const char *root, ...) {
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *p = NULL;
+ va_list ap;
+ int r;
+
+ r = fopen_os_release(root, &p, &f);
+ if (r < 0)
+ return r;
+
+ va_start(ap, root);
+ r = parse_env_filev(f, p, NEWLINE, ap);
+ va_end(ap);
+
+ return r;
+}
+
+int load_os_release_pairs(const char *root, char ***ret) {
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *p = NULL;
+ int r;
+
+ r = fopen_os_release(root, &p, &f);
+ if (r < 0)
+ return r;
+
+ return load_env_file_pairs(f, p, NEWLINE, ret);
+}
diff --git a/src/basic/os-util.h b/src/basic/os-util.h
new file mode 100644
index 000000000..93eea24eb
--- /dev/null
+++ b/src/basic/os-util.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+#pragma once
+
+//#include <stdio.h>
+
+int path_is_os_tree(const char *path);
+
+int open_os_release(const char *root, char **ret_path, int *ret_fd);
+int fopen_os_release(const char *root, char **ret_path, FILE **ret_file);
+
+int parse_os_release(const char *root, ...);
+int load_os_release_pairs(const char *root, char ***ret);
diff --git a/src/basic/stat-util.c b/src/basic/stat-util.c
index a1628efd9..d88f97fdf 100644
--- a/src/basic/stat-util.c
+++ b/src/basic/stat-util.c
@@ -139,33 +139,7 @@ int path_is_read_only_fs(const char *path) {
}
#if 0 /// UNNEEDED by elogind
-int path_is_os_tree(const char *path) {
- int r;
-
- assert(path);
-
- /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir
- * always results in -ENOENT, and we can properly distuingish the case where the whole root doesn't exist from
- * the case where just the os-release file is missing. */
- if (laccess(path, F_OK) < 0)
- return -errno;
-
- /* We use /usr/lib/os-release as flag file if something is an OS */
- r = chase_symlinks("/usr/lib/os-release", path, CHASE_PREFIX_ROOT, NULL);
- if (r == -ENOENT) {
-
- /* Also check for the old location in /etc, just in case. */
- r = chase_symlinks("/etc/os-release", path, CHASE_PREFIX_ROOT, NULL);
- if (r == -ENOENT)
- return 0; /* We got nothing */
- }
- if (r < 0)
- return r;
-
- return 1;
-}
#endif // 0
-
int files_same(const char *filea, const char *fileb, int flags) {
struct stat a, b;
diff --git a/src/basic/stat-util.h b/src/basic/stat-util.h
index 0a30c510a..325caf445 100644
--- a/src/basic/stat-util.h
+++ b/src/basic/stat-util.h
@@ -42,7 +42,6 @@ int null_or_empty_fd(int fd);
int path_is_read_only_fs(const char *path);
#if 0 /// UNNEEDED by elogind
-int path_is_os_tree(const char *path);
#endif // 0
int files_same(const char *filea, const char *fileb, int flags);
diff --git a/src/test/meson.build b/src/test/meson.build
index 833634ffa..8d694b379 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -264,6 +264,10 @@ tests += [
[],
[]],
+ [['src/test/test-os-util.c'],
+ [],
+ []],
+
[['src/test/test-escape.c'],
[],
[]],
@@ -956,6 +960,7 @@ tests += [
# 'src/libsystemd-network/dhcp-internal.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libsystemd-network/test-sd-dhcp-lease.c',
@@ -970,11 +975,13 @@ tests += [
# 'src/systemd/sd-dhcp-client.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libelogind-network/test-dhcp-server.c'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libsystemd-network/test-ipv4ll.c',
@@ -982,12 +989,14 @@ tests += [
# 'src/systemd/sd-ipv4ll.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libelogind-network/test-ipv4ll-manual.c',
# 'src/systemd/sd-ipv4ll.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# [],
# '', 'manual'],
#
@@ -1006,6 +1015,7 @@ tests += [
# 'src/systemd/sd-ndisc.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libsystemd-network/test-ndisc-ra.c',
@@ -1022,6 +1032,7 @@ tests += [
# 'src/systemd/sd-dhcp6-client.h'],
# [libshared,
# libelogind_network],
+# libsystemd_network],
# []],
#
# [['src/libelogind-network/test-lldp.c'],
diff --git a/src/test/test-os-util.c b/src/test/test-os-util.c
new file mode 100644
index 000000000..1e78f352a
--- /dev/null
+++ b/src/test/test-os-util.c
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+//#include <errno.h>
+
+//#include "log.h"
+//#include "os-util.h"
+
+static void test_path_is_os_tree(void) {
+ assert_se(path_is_os_tree("/") > 0);
+ assert_se(path_is_os_tree("/etc") == 0);
+ assert_se(path_is_os_tree("/idontexist") == -ENOENT);
+}
+
+int main(int argc, char *argv[]) {
+ log_set_max_level(LOG_DEBUG);
+ log_parse_environment();
+ log_open();
+
+ test_path_is_os_tree();
+
+ return 0;
+}
diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c
index 3d8d911ac..52142a53f 100644
--- a/src/test/test-stat-util.c
+++ b/src/test/test-stat-util.c
@@ -53,12 +53,6 @@ static void test_is_symlink(void) {
unlink(name_link);
}
-static void test_path_is_os_tree(void) {
- assert_se(path_is_os_tree("/") > 0);
- assert_se(path_is_os_tree("/etc") == 0);
- assert_se(path_is_os_tree("/idontexist") == -ENOENT);
-}
-
static void test_path_is_fs_type(void) {
/* run might not be a mount point in build chroots */
if (path_is_mount_point("/run", NULL, AT_SYMLINK_FOLLOW) > 0) {
@@ -84,7 +78,6 @@ int main(int argc, char *argv[]) {
test_files_same();
#if 0 /// UNNEEDED by elogind
test_is_symlink();
- test_path_is_os_tree();
test_path_is_fs_type();
test_path_is_temporary_fs();
#endif // 0