]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
repart: Remove outdated comment
[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", 0750, 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(__loongarch64)
67 # define KNOW_LIB64_DIRS 1
68 # if defined(__loongarch_double_float)
69 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
70 "usr/lib64\0", "ld-linux-loongarch-lp64d.so.1" },
71 # elif defined(__loongarch_single_float)
72 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
73 "usr/lib64\0", "ld-linux-loongarch-lp64f.so.1" },
74 # elif defined(__loongarch_soft_float)
75 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
76 "usr/lib64\0", "ld-linux-loongarch-lp64s.so.1" },
77 # else
78 # error "Unknown LoongArch ABI"
79 # endif
80 #elif defined(__m68k__)
81 /* No link needed. */
82 # define KNOW_LIB64_DIRS 1
83 #elif defined(_MIPS_SIM)
84 # if _MIPS_SIM == _MIPS_SIM_ABI32
85 # elif _MIPS_SIM == _MIPS_SIM_NABI32
86 # elif _MIPS_SIM == _MIPS_SIM_ABI64
87 # else
88 # error "Unknown MIPS ABI"
89 # endif
90 #elif defined(__powerpc__)
91 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
92 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
93 "usr/lib64\0", "ld64.so.2" },
94 # define KNOW_LIB64_DIRS 1
95 # elif defined(__powerpc64__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
96 /* powerpc64-linux-gnu */
97 # else
98 /* powerpc-linux-gnu */
99 # endif
100 #elif defined(__riscv)
101 # if __riscv_xlen == 32
102 # elif __riscv_xlen == 64
103 /* Same situation as for aarch64 */
104 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
105 "usr/lib64\0", "ld-linux-riscv64-lp64d.so.1" },
106 # define KNOW_LIB64_DIRS 1
107 # else
108 # error "Unknown RISC-V ABI"
109 # endif
110 #elif defined(__s390__)
111 /* s390-linux-gnu */
112 #elif defined(__s390x__)
113 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
114 "usr/lib64\0", "ld-lsb-s390x.so.3" },
115 # define KNOW_LIB64_DIRS 1
116 #elif defined(__sparc__)
117 #endif
118 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
119 };
120
121 #ifndef KNOW_LIB64_DIRS
122 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
123 #endif
124
125 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
126 _cleanup_close_ int fd = -EBADF;
127 int r;
128
129 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
130 if (fd < 0)
131 return log_error_errno(errno, "Failed to open root file system: %m");
132
133 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
134 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
135 continue;
136
137 if (table[i].target) {
138 const char *target = NULL;
139
140 /* check if one of the targets exists */
141 NULSTR_FOREACH(s, table[i].target) {
142 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
143 continue;
144
145 /* check if a specific file exists at the target path */
146 if (table[i].exists) {
147 _cleanup_free_ char *p = NULL;
148
149 p = path_join(s, table[i].exists);
150 if (!p)
151 return log_oom();
152
153 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
154 continue;
155 }
156
157 target = s;
158 break;
159 }
160
161 if (!target)
162 continue;
163
164 if (symlinkat(target, fd, table[i].dir) < 0) {
165 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
166 "Failed to create symlink at %s/%s: %m", root, table[i].dir);
167
168 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
169 continue;
170
171 return -errno;
172 }
173
174 if (uid_is_valid(uid) || gid_is_valid(gid))
175 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
176 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
177
178 continue;
179 }
180
181 WITH_UMASK(0000)
182 r = mkdirat(fd, table[i].dir, table[i].mode);
183 if (r < 0) {
184 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
185 "Failed to create directory at %s/%s: %m", root, table[i].dir);
186
187 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
188 continue;
189
190 return -errno;
191 }
192
193 if (uid_is_valid(uid) || gid_is_valid(gid))
194 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
195 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
196 }
197
198 return 0;
199 }