]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/base-filesystem.c
tree-wide: fix typo
[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
LP
13#include "base-filesystem.h"
14#include "fd-util.h"
3577de7a
KS
15#include "log.h"
16#include "macro.h"
d8b4d14d 17#include "nulstr-util.h"
65290fbf 18#include "path-util.h"
07630cea 19#include "string-util.h"
affb60b1 20#include "umask-util.h"
ee104e11 21#include "user-util.h"
3577de7a
KS
22
23typedef struct BaseFilesystem {
04ba1bb0 24 const char *dir; /* directory or symlink to create */
3577de7a 25 mode_t mode;
04ba1bb0 26 const char *target; /* if non-NULL create as symlink to this target */
a6f44d61 27 const char *exists; /* conditionalize this entry on existence of this file */
6404ecc8 28 bool ignore_failure;
3577de7a
KS
29} BaseFilesystem;
30
31static const BaseFilesystem table[] = {
30d7c9c4
HH
32 { "bin", 0, "usr/bin\0", NULL },
33 { "lib", 0, "usr/lib\0", NULL },
6404ecc8 34 { "root", 0755, NULL, NULL, true },
30d7c9c4 35 { "sbin", 0, "usr/sbin\0", NULL },
03cfe0d5
LP
36 { "usr", 0755, NULL, NULL },
37 { "var", 0755, NULL, NULL },
38 { "etc", 0755, NULL, NULL },
10404d52
DH
39 { "proc", 0755, NULL, NULL, true },
40 { "sys", 0755, NULL, NULL, true },
41 { "dev", 0755, NULL, NULL, true },
6f32005f 42
04ba1bb0
LP
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.*/
6f32005f 49#if defined(__aarch64__)
dcc87c68
ZJS
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/. */
a965a319 53 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
dcc87c68
ZJS
54 "usr/lib64\0", "ld-linux-aarch64.so.1" },
55# define KNOW_LIB64_DIRS 1
6f32005f
ZJS
56#elif defined(__alpha__)
57#elif defined(__arc__) || defined(__tilegx__)
58#elif defined(__arm__)
2db409ce
ZJS
59 /* No /lib64 on arm. The linker is /lib/ld-linux-armhf.so.3. */
60# define KNOW_LIB64_DIRS 1
6f32005f 61#elif defined(__i386__) || defined(__x86_64__)
a965a319 62 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
30d7c9c4 63 "usr/lib64\0", "ld-linux-x86-64.so.2" },
6f32005f
ZJS
64# define KNOW_LIB64_DIRS 1
65#elif defined(__ia64__)
66#elif defined(__m68k__)
996eaea5
ZJS
67 /* No link needed. */
68# define KNOW_LIB64_DIRS 1
6f32005f
ZJS
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__)
e98157b9 77# if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
a965a319 78 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
e98157b9
ZJS
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
6f32005f
ZJS
86#elif defined(__riscv)
87# if __riscv_xlen == 32
88# elif __riscv_xlen == 64
761e9382 89 /* Same situation as for aarch64 */
a965a319 90 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
761e9382
ZJS
91 "usr/lib64\0", "ld-linux-riscv64-lp64d.so.1" },
92# define KNOW_LIB64_DIRS 1
6f32005f
ZJS
93# else
94# error "Unknown RISC-V ABI"
95# endif
96#elif defined(__s390__)
fe037986 97 /* s390-linux-gnu */
6f32005f 98#elif defined(__s390x__)
a965a319 99 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE"\0"
fe037986
ZJS
100 "usr/lib64", "ld-lsb-s390x.so.3" },
101# define KNOW_LIB64_DIRS 1
6f32005f 102#elif defined(__sparc__)
e1ae9755 103#endif
6f32005f 104 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
3577de7a
KS
105};
106
6f32005f
ZJS
107#ifndef KNOW_LIB64_DIRS
108# pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
04ba1bb0
LP
109#endif
110
03cfe0d5 111int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
3577de7a 112 _cleanup_close_ int fd = -1;
8a383bf2 113 int r;
3577de7a
KS
114
115 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
4a62c710
MS
116 if (fd < 0)
117 return log_error_errno(errno, "Failed to open root file system: %m");
3577de7a 118
9b4aba10 119 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
6f4f8056
HH
120 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
121 continue;
122
3577de7a 123 if (table[i].target) {
6dc2852c 124 const char *target = NULL, *s;
e1ae9755
KS
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
3fd165e5
KS
131 /* check if a specific file exists at the target path */
132 if (table[i].exists) {
133 _cleanup_free_ char *p = NULL;
134
65290fbf 135 p = path_join(s, table[i].exists);
3fd165e5
KS
136 if (!p)
137 return log_oom();
138
139 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
140 continue;
141 }
142
e1ae9755
KS
143 target = s;
144 break;
145 }
146
147 if (!target)
3577de7a
KS
148 continue;
149
8258578f
LP
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 }
03cfe0d5 159
9b4aba10 160 if (uid_is_valid(uid) || gid_is_valid(gid))
03cfe0d5
LP
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);
03cfe0d5 163
3577de7a
KS
164 continue;
165 }
166
167 RUN_WITH_UMASK(0000)
168 r = mkdirat(fd, table[i].dir, table[i].mode);
8258578f
LP
169 if (r < 0) {
170 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
6404ecc8
LP
171 "Failed to create directory at %s/%s: %m", root, table[i].dir);
172
8258578f
LP
173 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
174 continue;
d1d59eeb 175
8258578f 176 return -errno;
6404ecc8 177 }
03cfe0d5 178
9b4aba10 179 if (uid != UID_INVALID || gid != UID_INVALID)
03cfe0d5
LP
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);
3577de7a
KS
182 }
183
184 return 0;
185}