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