]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/base-filesystem.c
shared: nudge people into sending us patches to make /lib64/ symlink generation work...
[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 #if defined(__i386__) || defined(__x86_64__)
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 { "lib64", 0, "usr/lib/x86_64-linux-gnu\0"
49 "usr/lib64\0", "ld-linux-x86-64.so.2" },
50 #else
51 /* gcc doesn't allow pragma to be used within constructs, hence log about this separately below */
52 # define WARN_LIB64 1
53 #endif
54 };
55
56 #ifdef WARN_LIB64
57 #pragma message "If your architecture knows a /lib64/ or /lib32/ directory, please add an entry creating it here."
58 /* And if your architecture doesn't know these directories, make sure to add ifdeffery here to
59 * suppress this pragma message. */
60 #endif
61
62 int base_filesystem_create(const char *root, uid_t uid, gid_t gid) {
63 _cleanup_close_ int fd = -1;
64 int r;
65
66 fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW);
67 if (fd < 0)
68 return log_error_errno(errno, "Failed to open root file system: %m");
69
70 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
71 if (faccessat(fd, table[i].dir, F_OK, AT_SYMLINK_NOFOLLOW) >= 0)
72 continue;
73
74 if (table[i].target) {
75 const char *target = NULL, *s;
76
77 /* check if one of the targets exists */
78 NULSTR_FOREACH(s, table[i].target) {
79 if (faccessat(fd, s, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
80 continue;
81
82 /* check if a specific file exists at the target path */
83 if (table[i].exists) {
84 _cleanup_free_ char *p = NULL;
85
86 p = path_join(s, table[i].exists);
87 if (!p)
88 return log_oom();
89
90 if (faccessat(fd, p, F_OK, AT_SYMLINK_NOFOLLOW) < 0)
91 continue;
92 }
93
94 target = s;
95 break;
96 }
97
98 if (!target)
99 continue;
100
101 if (symlinkat(target, fd, table[i].dir) < 0) {
102 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
103 "Failed to create symlink at %s/%s: %m", root, table[i].dir);
104
105 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
106 continue;
107
108 return -errno;
109 }
110
111 if (uid_is_valid(uid) || gid_is_valid(gid))
112 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
113 return log_error_errno(errno, "Failed to chown symlink at %s/%s: %m", root, table[i].dir);
114
115 continue;
116 }
117
118 RUN_WITH_UMASK(0000)
119 r = mkdirat(fd, table[i].dir, table[i].mode);
120 if (r < 0) {
121 log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno,
122 "Failed to create directory at %s/%s: %m", root, table[i].dir);
123
124 if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure)
125 continue;
126
127 return -errno;
128 }
129
130 if (uid != UID_INVALID || gid != UID_INVALID)
131 if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0)
132 return log_error_errno(errno, "Failed to chown directory at %s/%s: %m", root, table[i].dir);
133 }
134
135 return 0;
136 }