Debian Codegraph @

src/shared/install-file.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/ioctl.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6
7 #include "btrfs-util.h"
8 #include "chattr-util.h"
9 #include "env-util.h"
10 #include "errno-util.h"
11 #include "fd-util.h"
12 #include "fs-util.h"
13 #include "install-file.h"
14 #include "log.h"
15 #include "rm-rf.h"
16 #include "sync-util.h"
17 #include "time-util.h"
18
19 static int fs_make_very_read_only(int fd) {
20 struct stat st;
21 int r;
22
23 assert(fd >= 0);
24
25 /* Tries to make the specified fd "comprehensively" read-only. Primary use case for this is OS images,
26 * i.e. either loopback files or larger directory hierarchies. Depending on the inode type and
27 * backing file system this means something different:
28 *
29 * 1. If the fd refers to a btrfs subvolume we'll mark it read-only as a whole
30 * 2. If the fd refers to any other directory we'll set the FS_IMMUTABLE_FL flag on it
31 * 3. If the fd refers to a regular file we'll drop the w bits.
32 * 4. If the fd refers to a block device, use BLKROSET to set read-only state
33 *
34 * You might wonder why not drop the x bits for directories. That's because we want to guarantee that
35 * everything "inside" the image remains largely the way it is, in case you mount it. And since the
36 * mode of the root dir of the image is pretty visible we don't want to modify it. btrfs subvol flags
37 * and the FS_IMMUTABLE_FL otoh are much less visible. Changing the mode of regular files should be
38 * OK though, since after all this is supposed to be used for disk images, i.e. the fs in the disk
39 * image doesn't make the mode of the loopback file it is stored in visible. */
40
41 if (fstat(fd, &st) < 0)
42 return -errno;
43
44 switch (st.st_mode & S_IFMT) {
45
46 case S_IFDIR:
47 if (btrfs_might_be_subvol(&st)) {
48 r = btrfs_subvol_set_read_only_fd(fd, true);
49 if (r >= 0)
50 return 0;
51 if (!ERRNO_IS_NEG_IOCTL_NOT_SUPPORTED(r))
52 return r;
53 }
54
55 r = chattr_fd(fd, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL);
56 if (r < 0)
57 return r;
58
59 break;
60
61 case S_IFREG:
62 if ((st.st_mode & 0222) != 0)
63 if (fchmod(fd, st.st_mode & 07555) < 0)
64 return -errno;
65
66 break;
67
68 case S_IFBLK: {
69 int ro = 1;
70
71 if (ioctl(fd, BLKROSET, &ro) < 0)
72 return -errno;
73
74 break;
75 }
76
77 default:
78 return -EBADFD;
79 }
80
81 return 0;
82 }
83
84 static int unlinkat_maybe_dir(int dirfd, const char *pathname) {
85
86 /* Invokes unlinkat() for regular files first, and if this fails with EISDIR tries again with
87 * AT_REMOVEDIR */
88
89 if (unlinkat(dirfd, pathname, 0) < 0) {
90 if (errno != EISDIR)
91 return -errno;
92
93 if (unlinkat(dirfd, pathname, AT_REMOVEDIR) < 0)
94 return -errno;
95 }
96
97 return 0;
98 }
99
100 static bool need_opath(InstallFileFlags flags) {
101 /* Returns true when we need to pin the source file via an O_PATH fd */
102 return (flags & (INSTALL_FSYNC|INSTALL_FSYNC_FULL|INSTALL_SYNCFS|INSTALL_READ_ONLY)) != 0;
103 }
104
105 int install_file(int source_atfd, const char *source_name,
106 int target_atfd, const char *target_name,
107 InstallFileFlags flags) {
108
109 _cleanup_close_ int rofd = -EBADF;
110 int r;
111
112 /* Moves a file or directory tree into place, with some bells and whistles:
113 *
114 * 1. Optionally syncs before/after to ensure file installation can be used as barrier
115 * 2. Optionally marks the file/directory read-only using fs_make_very_read_only()
116 * 3. Optionally operates in replacing or in non-replacing mode.
117 * 4. If it replaces will remove the old tree if needed.
118 */
119
120 assert(source_atfd >= 0 || source_atfd == AT_FDCWD);
121 assert(source_name);
122 assert(target_atfd >= 0 || target_atfd == AT_FDCWD);
123
124 /* If target_name is specified as NULL no renaming takes place. Instead it is assumed the file is
125 * already in place, and only the syncing/read-only marking shall be applied. Note that with
126 * target_name=NULL and flags=0 this call is a NOP */
127
128 if (need_opath(flags)) {
129 _cleanup_close_ int pfd = -EBADF;
130 struct stat st;
131
132 /* Open an O_PATH fd for the source if we need to sync things or mark things read only. */
133
134 pfd = openat(source_atfd, source_name, O_PATH|O_CLOEXEC|O_NOFOLLOW);
135 if (pfd < 0)
136 return -errno;
137
138 if (fstat(pfd, &st) < 0)
139 return -errno;
140
141 switch (st.st_mode & S_IFMT) {
142
143 case S_IFREG: {
144 _cleanup_close_ int regfd = -EBADF;
145
146 regfd = fd_reopen(pfd, O_RDONLY|O_CLOEXEC);
147 if (regfd < 0) {
148 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
149 return log_debug_errno(regfd, "Failed to open referenced inode: %m");
150
151 log_debug_errno(regfd, "Failed to open referenced inode, ignoring: %m");
152 } else {
153 if ((flags & (INSTALL_FSYNC_FULL|INSTALL_SYNCFS)) != 0)
154 /* If this is just a regular file (as opposed to a fully populated
155 * directory) let's downgrade INSTALL_SYNCFS to INSTALL_FSYNC_FULL,
156 * after all this is going to be a single inode we install */
157 r = fsync_full(regfd);
158 else if (flags & INSTALL_FSYNC)
159 r = RET_NERRNO(fsync(regfd));
160 else
161 r = 0;
162 if (r < 0) {
163 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
164 return log_debug_errno(r, "Failed to sync source inode: %m");
165
166 log_debug_errno(r, "Failed to sync source inode, ignoring: %m");
167 }
168
169 if (flags & INSTALL_READ_ONLY)
170 rofd = TAKE_FD(regfd);
171 }
172
173 break;
174 }
175
176 case S_IFDIR: {
177 _cleanup_close_ int dfd = -EBADF;
178
179 dfd = fd_reopen(pfd, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
180 if (dfd < 0) {
181 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
182 return log_debug_errno(dfd, "Failed to open referenced inode: %m");
183
184 log_debug_errno(dfd, "Failed to open referenced inode, ignoring: %m");
185 } else {
186 if (flags & INSTALL_SYNCFS)
187 r = RET_NERRNO(syncfs(dfd));
188 else if (flags & INSTALL_FSYNC_FULL)
189 r = fsync_full(dfd);
190 else if (flags & INSTALL_FSYNC)
191 r = RET_NERRNO(fsync(dfd));
192 else
193 r = 0;
194 if (r < 0) {
195 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
196 return log_debug_errno(r, "Failed to sync source inode: %m");
197
198 log_debug_errno(r, "Failed to sync source inode, ignoring: %m");
199 }
200
201 if (flags & INSTALL_READ_ONLY)
202 rofd = TAKE_FD(dfd);
203 }
204
205 break;
206 }
207
208 default:
209 /* Other inodes: char/block device inodes, fifos, symlinks, sockets don't need
210 * syncing themselves, as they only exist in the directory, and have no contents on
211 * disk */
212
213 if (target_name && (flags & (INSTALL_FSYNC_FULL|INSTALL_SYNCFS)) != 0) {
214 r = fsync_directory_of_file(pfd);
215 if (r < 0) {
216 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
217 return log_debug_errno(r, "Failed to sync source inode: %m");
218
219 log_debug_errno(r, "Failed to sync source inode, ignoring: %m");
220 }
221 }
222
223 break;
224 }
225 }
226
227 if (target_name) {
228 /* Rename the file */
229
230 if (flags & INSTALL_REPLACE) {
231 /* First, try a simple renamat(), maybe that's enough */
232 if (renameat(source_atfd, source_name, target_atfd, target_name) < 0) {
233 _cleanup_close_ int dfd = -EBADF;
234
235 if (!IN_SET(errno, EEXIST, ENOTDIR, ENOTEMPTY, EISDIR, EBUSY))
236 return -errno;
237
238 /* Hmm, the target apparently existed already. Let's try to use
239 * RENAME_EXCHANGE. But let's first open the inode if it's a directory, so
240 * that we can later remove its contents if it's a directory. Why do this
241 * before the rename()? Mostly because if we have trouble opening the thing
242 * we want to know before we start actually modifying the file system. */
243
244 dfd = openat(target_atfd, target_name, O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
245 if (dfd < 0 && errno != ENOTDIR)
246 return -errno;
247
248 if (renameat2(source_atfd, source_name, target_atfd, target_name, RENAME_EXCHANGE) < 0) {
249
250 if (!ERRNO_IS_NOT_SUPPORTED(errno) && errno != EINVAL)
251 return -errno;
252
253 /* The exchange didn't work, let's remove the target first, and try again */
254
255 if (dfd >= 0)
256 (void) rm_rf_children(TAKE_FD(dfd), REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_CHMOD, NULL);
257
258 r = unlinkat_maybe_dir(target_atfd, target_name);
259 if (r < 0)
260 return log_debug_errno(r, "Failed to remove target directory: %m");
261
262 if (renameat(source_atfd, source_name, target_atfd, target_name) < 0)
263 return -errno;
264 } else {
265 /* The exchange worked, hence let's remove the source (i.e. the old target) */
266 if (dfd >= 0)
267 (void) rm_rf_children(TAKE_FD(dfd), REMOVE_PHYSICAL|REMOVE_SUBVOLUME|REMOVE_CHMOD, NULL);
268
269 r = unlinkat_maybe_dir(source_atfd, source_name);
270 if (r < 0)
271 return log_debug_errno(r, "Failed to remove replaced target directory: %m");
272 }
273 }
274 } else {
275 r = rename_noreplace(source_atfd, source_name, target_atfd, target_name);
276 if (r < 0)
277 return r;
278 }
279 }
280
281 if (rofd >= 0) {
282 r = fs_make_very_read_only(rofd);
283 if (r < 0) {
284 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
285 return log_debug_errno(r, "Failed to make inode read-only: %m");
286
287 log_debug_errno(r, "Failed to make inode read-only, ignoring: %m");
288 }
289 }
290
291 if ((flags & (INSTALL_FSYNC_FULL|INSTALL_SYNCFS)) != 0) {
292 if (target_name)
293 r = fsync_parent_at(target_atfd, target_name);
294 else
295 r = fsync_parent_at(source_atfd, source_name);
296 if (r < 0) {
297 if (!FLAGS_SET(flags, INSTALL_GRACEFUL))
298 return log_debug_errno(r, "Failed to sync inode: %m");
299
300 log_debug_errno(r, "Failed to sync inode, ignoring: %m");
301 }
302 }
303
304 return 0;
305 }
306
307 usec_t parse_source_date_epoch(void) {
308 static usec_t cache;
309 static bool cached = false;
310 int r;
311
312 if (cached)
313 return cache;
314
315 uint64_t t;
316 r = secure_getenv_uint64("SOURCE_DATE_EPOCH", &t);
317 if (r >= 0) {
318 if (MUL_SAFE(&cache, t, USEC_PER_SEC)) {
319 cached = true;
320 return cache;
321 }
322
323 r = -ERANGE;
324 }
325 if (r != -ENXIO)
326 log_debug_errno(r, "Failed to parse $SOURCE_DATE_EPOCH, ignoring: %m");
327
328 cached = true;
329 return (cache = USEC_INFINITY);
330 }
331
332 usec_t source_date_epoch_or_now(void) {
333 usec_t epoch;
334
335 epoch = parse_source_date_epoch();
336 if (epoch != USEC_INFINITY)
337 return epoch;
338
339 return now(CLOCK_REALTIME);
340 }