1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 //#include "alloc-util.h"
10 //#include "string-util.h"
12 int path_is_os_tree(const char *path) {
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)
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 */
33 int open_os_release(const char *root, char **ret_path, int *ret_fd) {
34 _cleanup_free_ char *q = NULL;
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));
49 /* Convert the O_PATH fd into a proper, readable one */
50 real_fd = fd_reopen(k, O_RDONLY|O_CLOEXEC|O_NOCTTY);
59 *ret_path = TAKE_PTR(q);
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;
71 return open_os_release(root, ret_path, NULL);
73 r = open_os_release(root, ret_path ? &p : NULL, &fd);
85 *ret_path = TAKE_PTR(p);
90 int parse_os_release(const char *root, ...) {
91 _cleanup_fclose_ FILE *f = NULL;
92 _cleanup_free_ char *p = NULL;
96 r = fopen_os_release(root, &p, &f);
101 r = parse_env_filev(f, p, NEWLINE, ap);
107 int load_os_release_pairs(const char *root, char ***ret) {
108 _cleanup_fclose_ FILE *f = NULL;
109 _cleanup_free_ char *p = NULL;
112 r = fopen_os_release(root, &p, &f);
116 return load_env_file_pairs(f, p, NEWLINE, ret);