]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
11 #include "alloc-util.h"
12 #include "architecture.h"
13 #include "base-filesystem.h"
14 #include "errno-util.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"
24 typedef struct BaseFilesystem
{
25 const char *dir
; /* directory or symlink to create */
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 */
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. */
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"
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"
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"
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"
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"
87 "usr/lib\0", "ld-linux-loongarch-lp64s.so.1" },
89 # error "Unknown LoongArch ABI"
91 #elif defined(__m68k__)
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
99 # error "Unknown MIPS ABI"
101 #elif defined(__powerpc__)
102 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
103 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE
"\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 */
110 /* powerpc-linux-gnu */
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"
118 "usr/lib\0", "ld-linux-riscv64-lp64d.so.1" },
119 # define KNOW_LIB64_DIRS 1
121 # error "Unknown RISC-V ABI"
123 #elif defined(__s390x__)
124 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE
"\0"
126 "usr/lib\0", "ld-lsb-s390x.so.3" },
127 # define KNOW_LIB64_DIRS 1
128 #elif defined(__s390__)
130 #elif defined(__sparc__)
132 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
135 #ifndef KNOW_LIB64_DIRS
136 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
139 int base_filesystem_create_fd(int fd
, const char *root
, uid_t uid
, gid_t gid
) {
145 /* The "root" parameter is decoration only – it's only used as part of log messages */
147 for (size_t i
= 0; i
< ELEMENTSOF(table
); i
++) {
148 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
151 if (table
[i
].target
) { /* Create as symlink? */
152 const char *target
= NULL
;
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)
159 /* check if a specific file exists at the target path */
160 if (table
[i
].exists
) {
161 _cleanup_free_
char *p
= NULL
;
163 p
= path_join(s
, table
[i
].exists
);
167 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
178 r
= RET_NERRNO(symlinkat(target
, fd
, table
[i
].dir
));
180 /* Create as directory. */
182 r
= RET_NERRNO(mkdirat(fd
, table
[i
].dir
, table
[i
].mode
));
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
);
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
);
202 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
203 _cleanup_close_
int fd
= -EBADF
;
205 fd
= open(ASSERT_PTR(root
), O_DIRECTORY
|O_CLOEXEC
);
207 return log_error_errno(errno
, "Failed to open root file system: %m");
209 return base_filesystem_create_fd(fd
, root
, uid
, gid
);