]>
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", 0755, 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(__m68k__)
68 # define KNOW_LIB64_DIRS 1
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
74 # error "Unknown MIPS ABI"
76 #elif defined(__powerpc__)
77 # if defined(__PPC64__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
78 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE
"\0"
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 */
84 /* powerpc-linux-gnu */
86 #elif defined(__riscv)
87 # if __riscv_xlen == 32
88 # elif __riscv_xlen == 64
89 /* Same situation as for aarch64 */
90 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE
"\0"
91 "usr/lib64\0", "ld-linux-riscv64-lp64d.so.1" },
92 # define KNOW_LIB64_DIRS 1
94 # error "Unknown RISC-V ABI"
96 #elif defined(__s390__)
98 #elif defined(__s390x__)
99 { "lib64", 0, "usr/lib/"LIB_ARCH_TUPLE
"\0"
100 "usr/lib64", "ld-lsb-s390x.so.3" },
101 # define KNOW_LIB64_DIRS 1
102 #elif defined(__sparc__)
104 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
107 #ifndef KNOW_LIB64_DIRS
108 # pragma message "Please add an entry above specifying whether your architecture uses /lib64/, /lib32/, or no such links."
111 int base_filesystem_create(const char *root
, uid_t uid
, gid_t gid
) {
112 _cleanup_close_
int fd
= -1;
115 fd
= open(root
, O_RDONLY
|O_NONBLOCK
|O_DIRECTORY
|O_CLOEXEC
|O_NOFOLLOW
);
117 return log_error_errno(errno
, "Failed to open root file system: %m");
119 for (size_t i
= 0; i
< ELEMENTSOF(table
); i
++) {
120 if (faccessat(fd
, table
[i
].dir
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
123 if (table
[i
].target
) {
124 const char *target
= NULL
, *s
;
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)
131 /* check if a specific file exists at the target path */
132 if (table
[i
].exists
) {
133 _cleanup_free_
char *p
= NULL
;
135 p
= path_join(s
, table
[i
].exists
);
139 if (faccessat(fd
, p
, F_OK
, AT_SYMLINK_NOFOLLOW
) < 0)
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
);
154 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
160 if (uid_is_valid(uid
) || gid_is_valid(gid
))
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
);
168 r
= mkdirat(fd
, table
[i
].dir
, table
[i
].mode
);
170 log_full_errno(IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
? LOG_DEBUG
: LOG_ERR
, errno
,
171 "Failed to create directory at %s/%s: %m", root
, table
[i
].dir
);
173 if (IN_SET(errno
, EEXIST
, EROFS
) || table
[i
].ignore_failure
)
179 if (uid_is_valid(uid
) || gid_is_valid(gid
))
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
);