]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
network: DHCP version logging typos
[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 "errno-util.h"
15 #include "fd-util.h"
16 #include "log.h"
17 #include "macro.h"
18 #include "nulstr-util.h"
19 #include "path-util.h"
20 #include "string-util.h"
21 #include "umask-util.h"
22 #include "user-util.h"
23
24 typedef struct BaseFilesystem {
25 const char *dir; /* directory or symlink to create */
26 mode_t mode;
27 const char *target; /* if non-NULL create as symlink to this target */
28 const char *exists; /* conditionalize this entry on existence of this file */
29 bool ignore_failure;
30 } BaseFilesystem;
31
32 static const BaseFilesystem table[] = {
33 { "bin", 0, "usr/bin\0", NULL },
34 { "lib", 0, "usr/lib\0", NULL },
35 { "root", 0750, NULL, NULL, true },
36 { "sbin", 0, "usr/sbin\0", NULL },
37 { "usr", 0755, NULL, NULL },
38 { "var", 0755, NULL, NULL },
39 { "etc", 0755, NULL, NULL },
40 { "proc", 0555, NULL, NULL, true },
41 { "sys", 0555, NULL, NULL, true },
42 { "dev", 0555, NULL, NULL, true },
43 { "run", 0555, NULL, NULL, true },
44 /* We don't add /tmp/ here for now (even though it's necessary for regular operation), because we
45 * want to support both cases where /tmp/ is a mount of its own (in which case we probably should set
46 * the mode to 1555, to indicate that no one should write to it, not even root) and when it's part of
47 * the rootfs (in which case we should set mode 1777), and we simply don't know what's right. */
48
49 /* Various architecture ABIs define the path to the dynamic loader via the /lib64/ subdirectory of
50 * the root directory. When booting from an otherwise empty root file system (where only /usr/ has
51 * been mounted into) it is thus necessary to create a symlink pointing to the right subdirectory of
52 * /usr/ first — otherwise we couldn't invoke any dynamic binary. Let's detect this case here, and
53 * create the symlink as needed should it be missing. We prefer doing this consistently with Debian's
54 * multiarch logic, but support Fedora-style and Arch-style multilib too. */
55 #if defined(__aarch64__)
56 /* aarch64 ELF ABI actually says dynamic loader is in /lib/, but Fedora puts it in /lib64/ anyway and
57 * just symlinks /lib/ld-linux-aarch64.so.1 to ../lib64/ld-linux-aarch64.so.1. For this to work
58 * correctly, /lib64/ must be symlinked to /usr/lib64/. */
59 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
60 "usr/lib64\0"
61 "usr/lib\0", "ld-linux-aarch64.so.1" },
62 # define KNOW_LIB64_DIRS 1
63 #elif defined(__alpha__)
64 #elif defined(__arc__) || defined(__tilegx__)
65 #elif defined(__arm__)
66 /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */
67 # define KNOW_LIB64_DIRS 1
68 #elif defined(__i386__) || defined(__x86_64__)
69 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
70 "usr/lib64\0"
71 "usr/lib\0", "ld-linux-x86-64.so.2" },
72 # define KNOW_LIB64_DIRS 1
73 #elif defined(__ia64__)
74 #elif defined(__loongarch_lp64)
75 # define KNOW_LIB64_DIRS 1
76 # if defined(__loongarch_double_float)
77 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
78 "usr/lib64\0"
79 "usr/lib\0", "ld-linux-loongarch-lp64d.so.1" },
80 # elif defined(__loongarch_single_float)
81 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
82 "usr/lib64\0"
83 "usr/lib\0", "ld-linux-loongarch-lp64f.so.1" },
84 # elif defined(__loongarch_soft_float)
85 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
86 "usr/lib64\0"
87 "usr/lib\0", "ld-linux-loongarch-lp64s.so.1" },
88 # else
89 # error "Unknown LoongArch ABI"
90 # endif
91 #elif defined(__m68k__)
92 /* No link needed. */
93 # define KNOW_LIB64_DIRS 1
94 #elif defined(_MIPS_SIM)
95 # if _MIPS_SIM == _MIPS_SIM_ABI32
96 # elif _MIPS_SIM == _MIPS_SIM_NABI32
97 # elif _MIPS_SIM == _MIPS_SIM_ABI64
98 # else
99 # error "Unknown MIPS ABI"
100 # endif
101 #elif defined(__powerpc__)
102 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
103 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
104 "usr/lib64\0"
105 "usr/lib\0", "ld64.so.2" },
106 # define KNOW_LIB64_DIRS 1
107 # elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
108 /* powerpc64-linux-gnu */
109 # else
110 /* powerpc-linux-gnu */
111 # endif
112 #elif defined(__riscv)
113 # if __riscv_xlen == 32
114 # elif __riscv_xlen == 64
115 /* Same situation as for aarch64 */
116 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
117 "usr/lib64\0"
118 "usr/lib\0", "ld-linux-riscv64-lp64d.so.1" },
119 # define KNOW_LIB64_DIRS 1
120 # else
121 # error "Unknown RISC-V ABI"
122 # endif
123 #elif defined(__s390x__)
124 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
125 "usr/lib64\0"
126 "usr/lib\0", "ld-lsb-s390x.so.3" },
127 # define KNOW_LIB64_DIRS 1
128 #elif defined(__s390__)
129 /* s390-linux-gnu */
130 #elif defined(__sparc__)
131 #endif
132 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
133 };
134
135 #ifndef KNOW_LIB64_DIRS
136 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
137 #endif
138
139 int base_filesystem_create_fd(int fd, const char *root, uid_t uid, gid_t gid) {
140 int r;
141
142 assert(fd >= 0);
143 assert(root);
144
145 /* The "root" parameter is decoration only – it's only used as part of log messages */
146
147 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
148 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
149 continue;
150
151 if (table[i].target) { /* Create as symlink? */
152 const char *target = NULL;
153
154 /* check if one of the targets exists */
155 NULSTR_FOREACH(s, table[i].target) {
156 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
157 continue;
158
159 /* check if a specific file exists at the target path */
160 if (table[i].exists) {
161 _cleanup_free_ char *p = NULL;
162
163 p = path_join(s, table[i].exists);
164 if (!p)
165 return log_oom();
166
167 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
168 continue;
169 }
170
171 target = s;
172 break;
173 }
174
175 if (!target)
176 continue;
177
178 r = RET_NERRNO(symlinkat(target, fd, table[i].dir));
179 } else {
180 /* Create as directory. */
181 WITH_UMASK(0000)
182 r = RET_NERRNO(mkdirat(fd, table[i].dir, table[i].mode));
183 }
184 if (r < 0) {
185 bool ignore = IN_SET(r, -EEXIST, -EROFS) || table[i].ignore_failure;
186 log_full_errno(ignore ? LOG_DEBUG : LOG_ERR, r,
187 "Failed to create %s/%s: %m", root, table[i].dir);
188 if (ignore)
189 continue;
190
191 return r;
192 }
193
194 if (uid_is_valid(uid) || gid_is_valid(gid))
195 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
196 return log_error_errno(errno, "Failed to chown %s/%s: %m", root, table[i].dir);
197 }
198
199 return 0;
200 }
201
202 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
203 _cleanup_close_ int fd = -EBADF;
204
205 fd = open(ASSERT_PTR(root), O_DIRECTORY|O_CLOEXEC);
206 if (fd < 0)
207 return log_error_errno(errno, "Failed to open root file system: %m");
208
209 return base_filesystem_create_fd(fd, root, uid, gid);
210 }