]>
Commit | Line | Data |
---|---|---|
d58ad743 LP |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
3 | #include "alloc-util.h" | |
686d13b9 | 4 | #include "env-file.h" |
d58ad743 LP |
5 | #include "fd-util.h" |
6 | #include "fs-util.h" | |
7 | #include "macro.h" | |
8 | #include "os-util.h" | |
d58ad743 | 9 | #include "string-util.h" |
686d13b9 | 10 | #include "strv.h" |
d58ad743 LP |
11 | |
12 | int path_is_os_tree(const char *path) { | |
13 | int r; | |
14 | ||
15 | assert(path); | |
16 | ||
17 | /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir | |
18 | * always results in -ENOENT, and we can properly distuingish the case where the whole root doesn't exist from | |
19 | * the case where just the os-release file is missing. */ | |
20 | if (laccess(path, F_OK) < 0) | |
21 | return -errno; | |
22 | ||
23 | /* We use {/etc|/usr/lib}/os-release as flag file if something is an OS */ | |
24 | r = open_os_release(path, NULL, NULL); | |
25 | if (r == -ENOENT) /* We got nothing */ | |
26 | return 0; | |
27 | if (r < 0) | |
28 | return r; | |
29 | ||
30 | return 1; | |
31 | } | |
32 | ||
33 | int open_os_release(const char *root, char **ret_path, int *ret_fd) { | |
34 | _cleanup_free_ char *q = NULL; | |
35 | const char *p; | |
36 | int k; | |
37 | ||
38 | FOREACH_STRING(p, "/etc/os-release", "/usr/lib/os-release") { | |
39 | k = chase_symlinks(p, root, CHASE_PREFIX_ROOT|(ret_fd ? CHASE_OPEN : 0), (ret_path ? &q : NULL)); | |
40 | if (k != -ENOENT) | |
41 | break; | |
42 | } | |
43 | if (k < 0) | |
44 | return k; | |
45 | ||
46 | if (ret_fd) { | |
47 | int real_fd; | |
48 | ||
49 | /* Convert the O_PATH fd into a proper, readable one */ | |
50 | real_fd = fd_reopen(k, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
51 | safe_close(k); | |
52 | if (real_fd < 0) | |
53 | return real_fd; | |
54 | ||
55 | *ret_fd = real_fd; | |
56 | } | |
57 | ||
58 | if (ret_path) | |
59 | *ret_path = TAKE_PTR(q); | |
60 | ||
61 | return 0; | |
62 | } | |
63 | ||
64 | int fopen_os_release(const char *root, char **ret_path, FILE **ret_file) { | |
65 | _cleanup_free_ char *p = NULL; | |
66 | _cleanup_close_ int fd = -1; | |
67 | FILE *f; | |
68 | int r; | |
69 | ||
70 | if (!ret_file) | |
71 | return open_os_release(root, ret_path, NULL); | |
72 | ||
73 | r = open_os_release(root, ret_path ? &p : NULL, &fd); | |
74 | if (r < 0) | |
75 | return r; | |
76 | ||
e92aaed3 | 77 | f = fdopen(fd, "r"); |
d58ad743 LP |
78 | if (!f) |
79 | return -errno; | |
80 | fd = -1; | |
81 | ||
82 | *ret_file = f; | |
83 | ||
84 | if (ret_path) | |
85 | *ret_path = TAKE_PTR(p); | |
86 | ||
87 | return 0; | |
88 | } | |
89 | ||
90 | int parse_os_release(const char *root, ...) { | |
91 | _cleanup_fclose_ FILE *f = NULL; | |
92 | _cleanup_free_ char *p = NULL; | |
93 | va_list ap; | |
94 | int r; | |
95 | ||
96 | r = fopen_os_release(root, &p, &f); | |
97 | if (r < 0) | |
98 | return r; | |
99 | ||
100 | va_start(ap, root); | |
aa8fbc74 | 101 | r = parse_env_filev(f, p, ap); |
d58ad743 LP |
102 | va_end(ap); |
103 | ||
104 | return r; | |
105 | } | |
106 | ||
107 | int load_os_release_pairs(const char *root, char ***ret) { | |
108 | _cleanup_fclose_ FILE *f = NULL; | |
109 | _cleanup_free_ char *p = NULL; | |
110 | int r; | |
111 | ||
112 | r = fopen_os_release(root, &p, &f); | |
113 | if (r < 0) | |
114 | return r; | |
115 | ||
aa8fbc74 | 116 | return load_env_file_pairs(f, p, ret); |
d58ad743 | 117 | } |