]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
Merge pull request #21578 from bluca/json_spec
[thirdparty/systemd.git] / src / shared / base-filesystem.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7 #include <sys/stat.h>
8 #include <syslog.h>
9 #include <unistd.h>
10
11 #include "alloc-util.h"
12 #include "architecture.h"
13 #include "base-filesystem.h"
14 #include "fd-util.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "nulstr-util.h"
18 #include "path-util.h"
19 #include "string-util.h"
20 #include "umask-util.h"
21 #include "user-util.h"
22
23 typedef struct BaseFilesystem {
24 const char *dir; /* directory or symlink to create */
25 mode_t mode;
26 const char *target; /* if non-NULL create as symlink to this target */
27 const char *exists; /* conditionalize this entry on existence of this file */
28 bool ignore_failure;
29 } BaseFilesystem;
30
31 static const BaseFilesystem table[] = {
32 { "bin", 0, "usr/bin\0", NULL },
33 { "lib", 0, "usr/lib\0", NULL },
34 { "root", 0755, NULL, NULL, true },
35 { "sbin", 0, "usr/sbin\0", NULL },
36 { "usr", 0755, NULL, NULL },
37 { "var", 0755, NULL, NULL },
38 { "etc", 0755, NULL, NULL },
39 { "proc", 0755, NULL, NULL, true },
40 { "sys", 0755, NULL, NULL, true },
41 { "dev", 0755, NULL, NULL, true },
42
43 /* Various architecture ABIs define the path to the dynamic loader via the /lib64/ subdirectory of
44 * the root directory. When booting from an otherwise empty root file system (where only /usr/ has
45 * been mounted into) it is thus necessary to create a symlink pointing to the right subdirectory of
46 * /usr/ first — otherwise we couldn't invoke any dynamic binary. Let's detect this case here, and
47 * create the symlink as needed should it be missing. We prefer doing this consistently with Debian's
48 * multiarch logic, but support Fedora-style multilib too.*/
49 #if defined(__aarch64__)
50 /* aarch64 ELF ABI actually says dynamic loader is in /lib/, but Fedora puts it in /lib64/ anyway and
51 * just symlinks /lib/ld-linux-aarch64.so.1 to ../lib64/ld-linux-aarch64.so.1. For this to work
52 * correctly, /lib64/ must be symlinked to /usr/lib64/. */
53 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
54 "usr/lib64\0", "ld-linux-aarch64.so.1" },
55 # define KNOW_LIB64_DIRS 1
56 #elif defined(__alpha__)
57 #elif defined(__arc__) || defined(__tilegx__)
58 #elif defined(__arm__)
59 /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */
60 # define KNOW_LIB64_DIRS 1
61 #elif defined(__i386__) || defined(__x86_64__)
62 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
63 "usr/lib64\0", "ld-linux-x86-64.so.2" },
64 # define KNOW_LIB64_DIRS 1
65 #elif defined(__ia64__)
66 #elif defined(__m68k__)
67 /* No link needed. */
68 # define KNOW_LIB64_DIRS 1
69 #elif defined(_MIPS_SIM)
70 # if _MIPS_SIM == _MIPS_SIM_ABI32
71 # elif _MIPS_SIM == _MIPS_SIM_NABI32
72 # elif _MIPS_SIM == _MIPS_SIM_ABI64
73 # else
74 # error "Unknown MIPS ABI"
75 # endif
76 #elif defined(__powerpc__)
77 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
78 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
79 "usr/lib64\0", "ld64.so.2" },
80 # define KNOW_LIB64_DIRS 1
81 # elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
82 /* powerpc64-linux-gnu */
83 # else
84 /* powerpc-linux-gnu */
85 # endif
86 #elif defined(__riscv)
87 # if __riscv_xlen == 32
88 # elif __riscv_xlen == 64
89 /* Same situation as for aarch64 */
90 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
91 "usr/lib64\0", "ld-linux-riscv64-lp64d.so.1" },
92 # define KNOW_LIB64_DIRS 1
93 # else
94 # error "Unknown RISC-V ABI"
95 # endif
96 #elif defined(__s390__)
97 /* s390-linux-gnu */
98 #elif defined(__s390x__)
99 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
100 "usr/lib64", "ld-lsb-s390x.so.3" },
101 # define KNOW_LIB64_DIRS 1
102 #elif defined(__sparc__)
103 #endif
104 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
105 };
106
107 #ifndef KNOW_LIB64_DIRS
108 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
109 #endif
110
111 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
112 _cleanup_close_ int fd = -1;
113 int r;
114
115 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
116 if (fd < 0)
117 return log_error_errno(errno, "Failed to open root file system: %m");
118
119 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
120 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
121 continue;
122
123 if (table[i].target) {
124 const char *target = NULL, *s;
125
126 /* check if one of the targets exists */
127 NULSTR_FOREACH(s, table[i].target) {
128 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
129 continue;
130
131 /* check if a specific file exists at the target path */
132 if (table[i].exists) {
133 _cleanup_free_ char *p = NULL;
134
135 p = path_join(s, table[i].exists);
136 if (!p)
137 return log_oom();
138
139 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
140 continue;
141 }
142
143 target = s;
144 break;
145 }
146
147 if (!target)
148 continue;
149
150 if (symlinkat(target, fd, table[i].dir) < 0) {
151 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
152 "Failed to create symlink at %s/%s: %m", root, table[i].dir);
153
154 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
155 continue;
156
157 return -errno;
158 }
159
160 if (uid_is_valid(uid) || gid_is_valid(gid))
161 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
162 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
163
164 continue;
165 }
166
167 RUN_WITH_UMASK(0000)
168 r = mkdirat(fd, table[i].dir, table[i].mode);
169 if (r < 0) {
170 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
171 "Failed to create directory at %s/%s: %m", root, table[i].dir);
172
173 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
174 continue;
175
176 return -errno;
177 }
178
179 if (uid != UID_INVALID || gid != UID_INVALID)
180 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
181 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
182 }
183
184 return 0;
185 }