]>
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"
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"
23 typedef struct BaseFilesystem
{
24 const char *dir
; /* directory or symlink to create */
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 */
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 },
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" },
78 # error "Unknown LoongArch ABI"
80 #elif defined(__m68k__)
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
88 # error "Unknown MIPS ABI"
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 */
98 /* powerpc-linux-gnu */
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
108 # error "Unknown RISC-V ABI"
110 #elif defined(__s390__)
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__)
118 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
121 #ifndef KNOW_LIB64_DIRS
122 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
125 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
126 _cleanup_close_
int fd
= -1;
129 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
131 return log_error_errno(errno
, "Failed to open root file system: %m");
133 for (size_t i
= 0; i
< ELEMENTSOF(table
); i
++) {
134 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
137 if (table
[i
].target
) {
138 const char *target
= NULL
, *s
;
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)
145 /* check if a specific file exists at the target path */
146 if (table
[i
].exists
) {
147 _cleanup_free_
char *p
= NULL
;
149 p
= path_join(s
, table
[i
].exists
);
153 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
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
);
168 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
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
);
182 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
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
);
187 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
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
);