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