1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include <linux/falloc.h>
8 #include "alloc-util.h"
10 #include "chattr-util.h"
11 #include "dirent-util.h"
12 #include "errno-util.h"
15 #include "hostname-util.h"
17 #include "lock-util.h"
19 #include "missing_fcntl.h"
20 #include "missing_syscall.h"
22 #include "path-util.h"
23 #include "process-util.h"
24 #include "random-util.h"
25 #include "ratelimit.h"
26 #include "stat-util.h"
27 #include "string-util.h"
29 #include "time-util.h"
30 #include "tmpfile-util.h"
31 #include "umask-util.h"
33 int rmdir_parents(const char *path
, const char *stop
) {
40 if (!path_is_safe(path
))
43 if (!path_is_safe(stop
))
46 p
= strdupa_safe(path
);
51 /* skip the last component. */
52 r
= path_find_last_component(p
, /* accept_dot_dot= */ false, (const char **) &slash
, NULL
);
58 assert(*slash
== '/');
61 if (path_startswith_full(stop
, p
, PATH_STARTSWITH_REFUSE_DOT_DOT
))
64 if (rmdir(p
) < 0 && errno
!= ENOENT
)
69 int rename_noreplace(int olddirfd
, const char *oldpath
, int newdirfd
, const char *newpath
) {
72 assert(olddirfd
>= 0 || olddirfd
== AT_FDCWD
);
74 assert(newdirfd
>= 0 || newdirfd
== AT_FDCWD
);
77 /* Try the ideal approach first */
78 if (renameat2(olddirfd
, oldpath
, newdirfd
, newpath
, RENAME_NOREPLACE
) >= 0)
81 /* renameat2() exists since Linux 3.15, btrfs and FAT added support for it later. If it is not implemented,
82 * fall back to a different method. */
83 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && errno
!= EINVAL
)
86 /* Let's try to use linkat()+unlinkat() as fallback. This doesn't work on directories and on some file systems
87 * that do not support hard links (such as FAT, most prominently), but for files it's pretty close to what we
88 * want — though not atomic (i.e. for a short period both the new and the old filename will exist). */
89 if (linkat(olddirfd
, oldpath
, newdirfd
, newpath
, 0) >= 0) {
91 r
= RET_NERRNO(unlinkat(olddirfd
, oldpath
, 0));
93 (void) unlinkat(newdirfd
, newpath
, 0);
100 if (!ERRNO_IS_NOT_SUPPORTED(errno
) && !IN_SET(errno
, EINVAL
, EPERM
)) /* FAT returns EPERM on link()… */
103 /* OK, neither RENAME_NOREPLACE nor linkat()+unlinkat() worked. Let's then fall back to the racy TOCTOU
104 * vulnerable accessat(F_OK) check followed by classic, replacing renameat(), we have nothing better. */
106 if (faccessat(newdirfd
, newpath
, F_OK
, AT_SYMLINK_NOFOLLOW
) >= 0)
111 return RET_NERRNO(renameat(olddirfd
, oldpath
, newdirfd
, newpath
));
114 int readlinkat_malloc(int fd
, const char *p
, char **ret
) {
117 assert(fd
>= 0 || fd
== AT_FDCWD
);
119 if (fd
< 0 && isempty(p
))
120 return -EISDIR
; /* In this case, the fd points to the current working directory, and is
121 * definitely not a symlink. Let's return earlier. */
124 _cleanup_free_
char *c
= NULL
;
131 n
= readlinkat(fd
, strempty(p
), c
, l
);
135 if ((size_t) n
< l
) {
144 if (l
> (SSIZE_MAX
-1)/2) /* readlinkat() returns an ssize_t, and we want an extra byte for a
145 * trailing NUL, hence do an overflow check relative to SSIZE_MAX-1
153 int readlink_value(const char *p
, char **ret
) {
154 _cleanup_free_
char *link
= NULL
, *name
= NULL
;
160 r
= readlink_malloc(p
, &link
);
164 r
= path_extract_filename(link
, &name
);
167 if (r
== O_DIRECTORY
)
170 *ret
= TAKE_PTR(name
);
174 int readlink_and_make_absolute(const char *p
, char **ret
) {
175 _cleanup_free_
char *target
= NULL
;
181 r
= readlink_malloc(p
, &target
);
185 return file_in_same_dir(p
, target
, ret
);
188 int chmod_and_chown_at(int dir_fd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
189 _cleanup_close_
int fd
= -EBADF
;
191 assert(dir_fd
>= 0 || dir_fd
== AT_FDCWD
);
194 /* Let's acquire an O_PATH fd, as precaution to change mode/owner on the same file */
195 fd
= openat(dir_fd
, path
, O_PATH
|O_CLOEXEC
|O_NOFOLLOW
);
200 } else if (dir_fd
== AT_FDCWD
) {
201 /* Let's acquire an O_PATH fd of the current directory */
202 fd
= openat(dir_fd
, ".", O_PATH
|O_CLOEXEC
|O_NOFOLLOW
|O_DIRECTORY
);
208 return fchmod_and_chown(dir_fd
, mode
, uid
, gid
);
211 int fchmod_and_chown_with_fallback(int fd
, const char *path
, mode_t mode
, uid_t uid
, gid_t gid
) {
212 bool do_chown
, do_chmod
;
216 /* Change ownership and access mode of the specified fd. Tries to do so safely, ensuring that at no
217 * point in time the access mode is above the old access mode under the old ownership or the new
218 * access mode under the new ownership. Note: this call tries hard to leave the access mode
219 * unaffected if the uid/gid is changed, i.e. it undoes implicit suid/sgid dropping the kernel does
222 * This call is happy with O_PATH fds.
224 * If path is given, allow a fallback path which does not use /proc/self/fd/. On any normal system
225 * /proc will be mounted, but in certain improperly assembled environments it might not be. This is
226 * less secure (potential TOCTOU), so should only be used after consideration. */
228 if (fstat(fd
, &st
) < 0)
232 (uid
!= UID_INVALID
&& st
.st_uid
!= uid
) ||
233 (gid
!= GID_INVALID
&& st
.st_gid
!= gid
);
236 !S_ISLNK(st
.st_mode
) && /* chmod is not defined on symlinks */
237 ((mode
!= MODE_INVALID
&& ((st
.st_mode
^ mode
) & 07777) != 0) ||
238 do_chown
); /* If we change ownership, make sure we reset the mode afterwards, since chown()
239 * modifies the access mode too */
241 if (mode
== MODE_INVALID
)
242 mode
= st
.st_mode
; /* If we only shall do a chown(), save original mode, since chown() might break it. */
243 else if ((mode
& S_IFMT
) != 0 && ((mode
^ st
.st_mode
) & S_IFMT
) != 0)
244 return -EINVAL
; /* insist on the right file type if it was specified */
246 if (do_chown
&& do_chmod
) {
247 mode_t minimal
= st
.st_mode
& mode
; /* the subset of the old and the new mask */
249 if (((minimal
^ st
.st_mode
) & 07777) != 0) {
250 r
= fchmod_opath(fd
, minimal
& 07777);
252 if (!path
|| r
!= -ENOSYS
)
255 /* Fallback path which doesn't use /proc/self/fd/. */
256 if (chmod(path
, minimal
& 07777) < 0)
263 if (fchownat(fd
, "", uid
, gid
, AT_EMPTY_PATH
) < 0)
267 r
= fchmod_opath(fd
, mode
& 07777);
269 if (!path
|| r
!= -ENOSYS
)
272 /* Fallback path which doesn't use /proc/self/fd/. */
273 if (chmod(path
, mode
& 07777) < 0)
278 return do_chown
|| do_chmod
;
281 int fchmod_umask(int fd
, mode_t m
) {
282 _cleanup_umask_ mode_t u
= umask(0777);
284 return RET_NERRNO(fchmod(fd
, m
& (~u
)));
287 int fchmod_opath(int fd
, mode_t m
) {
288 /* This function operates also on fd that might have been opened with
289 * O_PATH. The tool set we have is non-intuitive:
290 * - fchmod(2) only operates on open files (i. e., fds with an open file description);
291 * - fchmodat(2) does not have a flag arg like fchownat(2) does, so no way to pass AT_EMPTY_PATH;
292 * + it should not be confused with the libc fchmodat(3) interface, which adds 4th flag argument,
293 * but does not support AT_EMPTY_PATH (only supports AT_SYMLINK_NOFOLLOW);
294 * - fchmodat2(2) supports all the AT_* flags, but is still very recent.
296 * We try to use fchmodat2(), and, if it is not supported, resort
297 * to the /proc/self/fd dance. */
301 if (fchmodat2(fd
, "", m
, AT_EMPTY_PATH
) >= 0)
303 if (!IN_SET(errno
, ENOSYS
, EPERM
)) /* Some container managers block unknown syscalls with EPERM */
306 if (chmod(FORMAT_PROC_FD_PATH(fd
), m
) < 0) {
310 return proc_fd_enoent_errno();
316 int futimens_opath(int fd
, const struct timespec ts
[2]) {
317 /* Similar to fchmod_opath() but for futimens() */
321 if (utimensat(fd
, "", ts
, AT_EMPTY_PATH
) >= 0)
326 /* Support for AT_EMPTY_PATH is added rather late (kernel 5.8), so fall back to going through /proc/
329 if (utimensat(AT_FDCWD
, FORMAT_PROC_FD_PATH(fd
), ts
, /* flags = */ 0) < 0) {
333 return proc_fd_enoent_errno();
339 int stat_warn_permissions(const char *path
, const struct stat
*st
) {
343 /* Don't complain if we are reading something that is not a file, for example /dev/null */
344 if (!S_ISREG(st
->st_mode
))
347 if (st
->st_mode
& 0111)
348 log_warning("Configuration file %s is marked executable. Please remove executable permission bits. Proceeding anyway.", path
);
350 if (st
->st_mode
& 0002)
351 log_warning("Configuration file %s is marked world-writable. Please remove world writability permission bits. Proceeding anyway.", path
);
353 if (getpid_cached() == 1 && (st
->st_mode
& 0044) != 0044)
354 log_warning("Configuration file %s is marked world-inaccessible. This has no effect as configuration data is accessible via APIs without restrictions. Proceeding anyway.", path
);
359 int fd_warn_permissions(const char *path
, int fd
) {
365 if (fstat(fd
, &st
) < 0)
368 return stat_warn_permissions(path
, &st
);
371 int access_nofollow(const char *path
, int mode
) {
372 return RET_NERRNO(faccessat(AT_FDCWD
, path
, mode
, AT_SYMLINK_NOFOLLOW
));
375 int touch_fd(int fd
, usec_t stamp
) {
378 if (stamp
== USEC_INFINITY
)
379 return futimens_opath(fd
, /* ts= */ NULL
);
381 struct timespec ts
[2];
382 timespec_store(ts
+ 0, stamp
);
384 return futimens_opath(fd
, ts
);
387 int touch_file(const char *path
, bool parents
, usec_t stamp
, uid_t uid
, gid_t gid
, mode_t mode
) {
388 _cleanup_close_
int fd
= -EBADF
;
393 /* Note that touch_file() does not follow symlinks: if invoked on an existing symlink, then it is the symlink
394 * itself which is updated, not its target
396 * Returns the first error we encounter, but tries to apply as much as possible. */
399 (void) mkdir_parents(path
, 0755);
401 /* Initially, we try to open the node with O_PATH, so that we get a reference to the node. This is useful in
402 * case the path refers to an existing device or socket node, as we can open it successfully in all cases, and
403 * won't trigger any driver magic or so. */
404 fd
= open(path
, O_PATH
|O_CLOEXEC
|O_NOFOLLOW
);
409 /* if the node doesn't exist yet, we create it, but with O_EXCL, so that we only create a regular file
410 * here, and nothing else */
411 fd
= open(path
, O_WRONLY
|O_CREAT
|O_EXCL
|O_CLOEXEC
, IN_SET(mode
, 0, MODE_INVALID
) ? 0644 : mode
);
416 /* Let's make a path from the fd, and operate on that. With this logic, we can adjust the access mode,
417 * ownership and time of the file node in all cases, even if the fd refers to an O_PATH object — which is
418 * something fchown(), fchmod(), futimensat() don't allow. */
419 ret
= fchmod_and_chown(fd
, mode
, uid
, gid
);
421 return RET_GATHER(ret
, touch_fd(fd
, stamp
));
424 int touch(const char *path
) {
425 return touch_file(path
, false, USEC_INFINITY
, UID_INVALID
, GID_INVALID
, MODE_INVALID
);
428 int symlinkat_idempotent(const char *from
, int atfd
, const char *to
, bool make_relative
) {
429 _cleanup_free_
char *relpath
= NULL
;
436 r
= path_make_relative_parent(to
, from
, &relpath
);
443 if (symlinkat(from
, atfd
, to
) < 0) {
444 _cleanup_free_
char *p
= NULL
;
449 r
= readlinkat_malloc(atfd
, to
, &p
);
450 if (r
== -EINVAL
) /* Not a symlink? In that case return the original error we encountered: -EEXIST */
452 if (r
< 0) /* Any other error? In that case propagate it as is */
455 if (!streq(p
, from
)) /* Not the symlink we want it to be? In that case, propagate the original -EEXIST */
462 int symlinkat_atomic_full(const char *from
, int atfd
, const char *to
, SymlinkFlags flags
) {
468 _cleanup_free_
char *relpath
= NULL
;
469 if (FLAGS_SET(flags
, SYMLINK_MAKE_RELATIVE
)) {
470 r
= path_make_relative_parent(to
, from
, &relpath
);
477 _cleanup_free_
char *t
= NULL
;
478 r
= tempfn_random(to
, NULL
, &t
);
482 bool call_label_ops_post
= false;
483 if (FLAGS_SET(flags
, SYMLINK_LABEL
)) {
484 r
= label_ops_pre(atfd
, to
, S_IFLNK
);
488 call_label_ops_post
= true;
491 r
= RET_NERRNO(symlinkat(from
, atfd
, t
));
492 if (call_label_ops_post
)
493 RET_GATHER(r
, label_ops_post(atfd
, t
, /* created= */ r
>= 0));
497 r
= RET_NERRNO(renameat(atfd
, t
, atfd
, to
));
499 (void) unlinkat(atfd
, t
, 0);
506 int mknodat_atomic(int atfd
, const char *path
, mode_t mode
, dev_t dev
) {
507 _cleanup_free_
char *t
= NULL
;
512 r
= tempfn_random(path
, NULL
, &t
);
516 if (mknodat(atfd
, t
, mode
, dev
) < 0)
519 r
= RET_NERRNO(renameat(atfd
, t
, atfd
, path
));
521 (void) unlinkat(atfd
, t
, 0);
528 int mkfifoat_atomic(int atfd
, const char *path
, mode_t mode
) {
529 _cleanup_free_
char *t
= NULL
;
534 /* We're only interested in the (random) filename. */
535 r
= tempfn_random(path
, NULL
, &t
);
539 if (mkfifoat(atfd
, t
, mode
) < 0)
542 r
= RET_NERRNO(renameat(atfd
, t
, atfd
, path
));
544 (void) unlinkat(atfd
, t
, 0);
551 int get_files_in_directory(const char *path
, char ***ret_list
) {
552 _cleanup_strv_free_
char **l
= NULL
;
553 _cleanup_closedir_
DIR *d
= NULL
;
558 /* Returns all files in a directory in *list, and the number
559 * of files as return value. If list is NULL returns only the
566 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
567 if (!dirent_is_file(de
))
571 /* one extra slot is needed for the terminating NULL */
572 if (!GREEDY_REALLOC(l
, n
+ 2))
575 l
[n
] = strdup(de
->d_name
);
585 *ret_list
= TAKE_PTR(l
);
590 static int getenv_tmp_dir(const char **ret_path
) {
595 /* We use the same order of environment variables python uses in tempfile.gettempdir():
596 * https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir */
597 FOREACH_STRING(n
, "TMPDIR", "TEMP", "TMP") {
600 e
= secure_getenv(n
);
603 if (!path_is_absolute(e
)) {
607 if (!path_is_normalized(e
)) {
624 /* Remember first error, to make this more debuggable */
636 static int tmp_dir_internal(const char *def
, const char **ret
) {
643 r
= getenv_tmp_dir(&e
);
649 k
= is_dir(def
, /* follow = */ true);
653 return RET_GATHER(r
, k
);
659 int var_tmp_dir(const char **ret
) {
662 /* Returns the location for "larger" temporary files, that is backed by physical storage if available, and thus
663 * even might survive a boot: /var/tmp. If $TMPDIR (or related environment variables) are set, its value is
664 * returned preferably however. Note that both this function and tmp_dir() below are affected by $TMPDIR,
665 * making it a variable that overrides all temporary file storage locations. */
667 return tmp_dir_internal("/var/tmp", ret
);
670 int tmp_dir(const char **ret
) {
673 /* Similar to var_tmp_dir() above, but returns the location for "smaller" temporary files, which is usually
674 * backed by an in-memory file system: /tmp. */
676 return tmp_dir_internal("/tmp", ret
);
679 int unlink_or_warn(const char *filename
) {
682 if (unlink(filename
) < 0 && errno
!= ENOENT
)
683 /* If the file doesn't exist and the fs simply was read-only (in which
684 * case unlink() returns EROFS even if the file doesn't exist), don't
686 if (errno
!= EROFS
|| access(filename
, F_OK
) >= 0)
687 return log_error_errno(errno
, "Failed to remove \"%s\": %m", filename
);
692 char *rmdir_and_free(char *p
) {
702 char* unlink_and_free(char *p
) {
712 int access_fd(int fd
, int mode
) {
715 /* Like access() but operates on an already open fd */
717 if (faccessat(fd
, "", mode
, AT_EMPTY_PATH
) >= 0)
722 /* Support for AT_EMPTY_PATH is added rather late (kernel 5.8), so fall back to going through /proc/
725 if (access(FORMAT_PROC_FD_PATH(fd
), mode
) < 0) {
729 return proc_fd_enoent_errno();
735 int unlinkat_deallocate(int fd
, const char *name
, UnlinkDeallocateFlags flags
) {
736 _cleanup_close_
int truncate_fd
= -EBADF
;
740 assert(fd
>= 0 || fd
== AT_FDCWD
);
742 assert((flags
& ~(UNLINK_REMOVEDIR
|UNLINK_ERASE
)) == 0);
744 /* Operates like unlinkat() but also deallocates the file contents if it is a regular file and there's no other
745 * link to it. This is useful to ensure that other processes that might have the file open for reading won't be
746 * able to keep the data pinned on disk forever. This call is particular useful whenever we execute clean-up
747 * jobs ("vacuuming"), where we want to make sure the data is really gone and the disk space released and
748 * returned to the free pool.
750 * Deallocation is preferably done by FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE (👊) if supported, which means
751 * the file won't change size. That's a good thing since we shouldn't needlessly trigger SIGBUS in other
752 * programs that have mmap()ed the file. (The assumption here is that changing file contents to all zeroes
753 * underneath those programs is the better choice than simply triggering SIGBUS in them which truncation does.)
754 * However if hole punching is not implemented in the kernel or file system we'll fall back to normal file
755 * truncation (🔪), as our goal of deallocating the data space trumps our goal of being nice to readers (💐).
757 * Note that we attempt deallocation, but failure to succeed with that is not considered fatal, as long as the
758 * primary job – to delete the file – is accomplished. */
760 if (!FLAGS_SET(flags
, UNLINK_REMOVEDIR
)) {
761 truncate_fd
= openat(fd
, name
, O_WRONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
|O_NONBLOCK
);
762 if (truncate_fd
< 0) {
764 /* If this failed because the file doesn't exist propagate the error right-away. Also,
765 * AT_REMOVEDIR wasn't set, and we tried to open the file for writing, which means EISDIR is
766 * returned when this is a directory but we are not supposed to delete those, hence propagate
767 * the error right-away too. */
768 if (IN_SET(errno
, ENOENT
, EISDIR
))
771 if (errno
!= ELOOP
) /* don't complain if this is a symlink */
772 log_debug_errno(errno
, "Failed to open file '%s' for deallocation, ignoring: %m", name
);
776 if (unlinkat(fd
, name
, FLAGS_SET(flags
, UNLINK_REMOVEDIR
) ? AT_REMOVEDIR
: 0) < 0)
779 if (truncate_fd
< 0) /* Don't have a file handle, can't do more ☹️ */
782 if (fstat(truncate_fd
, &st
) < 0) {
783 log_debug_errno(errno
, "Failed to stat file '%s' for deallocation, ignoring: %m", name
);
787 if (!S_ISREG(st
.st_mode
))
790 if (FLAGS_SET(flags
, UNLINK_ERASE
) && st
.st_size
> 0 && st
.st_nlink
== 0) {
791 uint64_t left
= st
.st_size
;
792 char buffer
[64 * 1024];
794 /* If erasing is requested, let's overwrite the file with random data once before deleting
795 * it. This isn't going to give you shred(1) semantics, but hopefully should be good enough
796 * for stuff backed by tmpfs at least.
798 * Note that we only erase like this if the link count of the file is zero. If it is higher it
799 * is still linked by someone else and we'll leave it to them to remove it securely
802 random_bytes(buffer
, sizeof(buffer
));
807 n
= write(truncate_fd
, buffer
, MIN(sizeof(buffer
), left
));
809 log_debug_errno(errno
, "Failed to erase data in file '%s', ignoring.", name
);
813 assert(left
>= (size_t) n
);
817 /* Let's refresh metadata */
818 if (fstat(truncate_fd
, &st
) < 0) {
819 log_debug_errno(errno
, "Failed to stat file '%s' for deallocation, ignoring: %m", name
);
824 /* Don't deallocate if there's nothing to deallocate or if the file is linked elsewhere */
825 if (st
.st_blocks
== 0 || st
.st_nlink
> 0)
828 /* If this is a regular file, it actually took up space on disk and there are no other links it's time to
829 * punch-hole/truncate this to release the disk space. */
831 bs
= MAX(st
.st_blksize
, 512);
832 l
= ROUND_UP(st
.st_size
, bs
); /* Round up to next block size */
834 if (fallocate(truncate_fd
, FALLOC_FL_PUNCH_HOLE
|FALLOC_FL_KEEP_SIZE
, 0, l
) >= 0)
835 return 0; /* Successfully punched a hole! 😊 */
837 /* Fall back to truncation */
838 if (ftruncate(truncate_fd
, 0) < 0) {
839 log_debug_errno(errno
, "Failed to truncate file to 0, ignoring: %m");
846 int open_parent_at(int dir_fd
, const char *path
, int flags
, mode_t mode
) {
847 _cleanup_free_
char *parent
= NULL
;
850 assert(dir_fd
>= 0 || dir_fd
== AT_FDCWD
);
853 r
= path_extract_directory(path
, &parent
);
854 if (r
== -EDESTADDRREQ
) {
855 parent
= strdup(".");
858 } else if (r
== -EADDRNOTAVAIL
) {
859 parent
= strdup(path
);
865 /* Let's insist on O_DIRECTORY since the parent of a file or directory is a directory. Except if we open an
866 * O_TMPFILE file, because in that case we are actually create a regular file below the parent directory. */
868 if (FLAGS_SET(flags
, O_PATH
))
869 flags
|= O_DIRECTORY
;
870 else if (!FLAGS_SET(flags
, O_TMPFILE
))
871 flags
|= O_DIRECTORY
|O_RDONLY
;
873 return RET_NERRNO(openat(dir_fd
, parent
, flags
, mode
));
876 int conservative_renameat(
877 int olddirfd
, const char *oldpath
,
878 int newdirfd
, const char *newpath
) {
880 _cleanup_close_
int old_fd
= -EBADF
, new_fd
= -EBADF
;
881 struct stat old_stat
, new_stat
;
883 /* Renames the old path to the new path, much like renameat() — except if both are regular files and
884 * have the exact same contents and basic file attributes already. In that case remove the new file
885 * instead. This call is useful for reducing inotify wakeups on files that are updated but don't
886 * actually change. This function is written in a style that we rather rename too often than suppress
887 * too much. I.e. whenever we are in doubt, we rather rename than fail. After all reducing inotify
888 * events is an optimization only, not more. */
890 old_fd
= openat(olddirfd
, oldpath
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_NOFOLLOW
);
894 new_fd
= openat(newdirfd
, newpath
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_NOFOLLOW
);
898 if (fstat(old_fd
, &old_stat
) < 0)
901 if (!S_ISREG(old_stat
.st_mode
))
904 if (fstat(new_fd
, &new_stat
) < 0)
907 if (stat_inode_same(&new_stat
, &old_stat
))
910 if (old_stat
.st_mode
!= new_stat
.st_mode
||
911 old_stat
.st_size
!= new_stat
.st_size
||
912 old_stat
.st_uid
!= new_stat
.st_uid
||
913 old_stat
.st_gid
!= new_stat
.st_gid
)
917 uint8_t buf1
[16*1024];
918 uint8_t buf2
[sizeof(buf1
)];
921 l1
= read(old_fd
, buf1
, sizeof(buf1
));
925 if (l1
== sizeof(buf1
))
926 /* Read the full block, hence read a full block in the other file too */
928 l2
= read(new_fd
, buf2
, l1
);
930 assert((size_t) l1
< sizeof(buf1
));
932 /* Short read. This hence was the last block in the first file, and then came
933 * EOF. Read one byte more in the second file, so that we can verify we hit EOF there
936 assert((size_t) (l1
+ 1) <= sizeof(buf2
));
937 l2
= read(new_fd
, buf2
, l1
+ 1);
942 if (memcmp(buf1
, buf2
, l1
) != 0)
945 if ((size_t) l1
< sizeof(buf1
)) /* We hit EOF on the first file, and the second file too, hence exit
951 /* Everything matches? Then don't rename, instead remove the source file, and leave the existing
952 * destination in place */
954 if (unlinkat(olddirfd
, oldpath
, 0) < 0)
960 if (renameat(olddirfd
, oldpath
, newdirfd
, newpath
) < 0)
966 int posix_fallocate_loop(int fd
, uint64_t offset
, uint64_t size
) {
970 r
= posix_fallocate(fd
, offset
, size
); /* returns positive errnos on error */
972 return -r
; /* Let's return negative errnos, like common in our codebase */
974 /* On EINTR try a couple of times more, but protect against busy looping
975 * (not more than 16 times per 10s) */
976 rl
= (const RateLimit
) { 10 * USEC_PER_SEC
, 16 };
977 while (ratelimit_below(&rl
)) {
978 r
= posix_fallocate(fd
, offset
, size
);
986 int parse_cifs_service(
992 _cleanup_free_
char *h
= NULL
, *ss
= NULL
, *x
= NULL
;
993 const char *p
, *e
, *d
;
996 /* Parses a CIFS service in form of //host/service/path… and splitting it in three parts. The last
997 * part is optional, in which case NULL is returned there. To maximize compatibility syntax with
998 * backslashes instead of slashes is accepted too. */
1003 p
= startswith(s
, "//");
1005 p
= startswith(s
, "\\\\");
1011 e
= strchr(p
, delimiter
);
1015 h
= strndup(p
, e
- p
);
1019 if (!hostname_is_valid(h
, 0))
1024 d
= strchrnul(e
, delimiter
);
1026 ss
= strndup(e
, d
- e
);
1030 if (!filename_is_valid(ss
))
1034 x
= strdup(skip_leading_chars(d
, CHAR_TO_STR(delimiter
)));
1038 /* Make sure to convert Windows-style "\" → Unix-style / */
1039 for (char *i
= x
; *i
; i
++)
1040 if (*i
== delimiter
)
1043 if (!path_is_valid(x
))
1047 if (!path_is_normalized(x
))
1052 *ret_host
= TAKE_PTR(h
);
1054 *ret_service
= TAKE_PTR(ss
);
1056 *ret_path
= TAKE_PTR(x
);
1061 int open_mkdir_at_full(int dirfd
, const char *path
, int flags
, XOpenFlags xopen_flags
, mode_t mode
) {
1062 _cleanup_close_
int fd
= -EBADF
, parent_fd
= -EBADF
;
1063 _cleanup_free_
char *fname
= NULL
, *parent
= NULL
;
1066 /* Creates a directory with mkdirat() and then opens it, in the "most atomic" fashion we can
1067 * do. Guarantees that the returned fd refers to a directory. If O_EXCL is specified will fail if the
1068 * dir already exists. Otherwise will open an existing dir, but only if it is one. */
1070 if (flags
& ~(O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_EXCL
|O_NOATIME
|O_NOFOLLOW
|O_PATH
))
1072 if ((flags
& O_ACCMODE_STRICT
) != O_RDONLY
)
1075 /* Note that O_DIRECTORY|O_NOFOLLOW is implied, but we allow specifying it anyway. The following
1076 * flags actually make sense to specify: O_CLOEXEC, O_EXCL, O_NOATIME, O_PATH */
1078 /* If this is not a valid filename, it's a path. Let's open the parent directory then, so
1079 * that we can pin it, and operate below it. */
1080 r
= path_extract_directory(path
, &parent
);
1082 if (!IN_SET(r
, -EDESTADDRREQ
, -EADDRNOTAVAIL
))
1085 r
= path_extract_filename(path
, &fname
);
1089 parent_fd
= openat(dirfd
, parent
, O_PATH
|O_DIRECTORY
|O_CLOEXEC
);
1097 fd
= xopenat_full(dirfd
, path
, flags
|O_CREAT
|O_DIRECTORY
|O_NOFOLLOW
, xopen_flags
, mode
);
1098 if (IN_SET(fd
, -ELOOP
, -ENOTDIR
))
1106 int openat_report_new(int dirfd
, const char *pathname
, int flags
, mode_t mode
, bool *ret_newly_created
) {
1109 /* Just like openat(), but adds one thing: optionally returns whether we created the file anew or if
1110 * it already existed before. This is only relevant if O_CREAT is set without O_EXCL, and thus will
1111 * shortcut to openat() otherwise.
1113 * Note that this routine is a bit more strict with symlinks than regular openat() is. If O_NOFOLLOW
1114 * is not specified, then we'll follow the symlink when opening an existing file but we will *not*
1115 * follow it when creating a new one (because that's a terrible UNIX misfeature and generally a
1116 * security hole). */
1118 if (!FLAGS_SET(flags
, O_CREAT
) || FLAGS_SET(flags
, O_EXCL
)) {
1119 fd
= openat(dirfd
, pathname
, flags
, mode
);
1123 if (ret_newly_created
)
1124 *ret_newly_created
= FLAGS_SET(flags
, O_CREAT
);
1128 for (unsigned attempts
= 7;;) {
1129 /* First, attempt to open without O_CREAT/O_EXCL, i.e. open existing file */
1130 fd
= openat(dirfd
, pathname
, flags
& ~(O_CREAT
| O_EXCL
), mode
);
1132 if (ret_newly_created
)
1133 *ret_newly_created
= false;
1136 if (errno
!= ENOENT
)
1139 /* So the file didn't exist yet, hence create it with O_CREAT/O_EXCL/O_NOFOLLOW. */
1140 fd
= openat(dirfd
, pathname
, flags
| O_CREAT
| O_EXCL
| O_NOFOLLOW
, mode
);
1142 if (ret_newly_created
)
1143 *ret_newly_created
= true;
1146 if (errno
!= EEXIST
)
1149 /* Hmm, so now we got EEXIST? Then someone might have created the file between the first and
1150 * second call to openat(). Let's try again but with a limit so we don't spin forever. */
1152 if (--attempts
== 0) /* Give up eventually, somebody is playing with us */
1157 int xopenat_full(int dir_fd
, const char *path
, int open_flags
, XOpenFlags xopen_flags
, mode_t mode
) {
1158 _cleanup_close_
int fd
= -EBADF
;
1159 bool made_dir
= false, made_file
= false;
1162 assert(dir_fd
>= 0 || dir_fd
== AT_FDCWD
);
1164 /* An inode cannot be both a directory and a regular file at the same time. */
1165 assert(!(FLAGS_SET(open_flags
, O_DIRECTORY
) && FLAGS_SET(xopen_flags
, XO_REGULAR
)));
1167 /* This is like openat(), but has a few tricks up its sleeves, extending behaviour:
1169 * • O_DIRECTORY|O_CREAT is supported, which causes a directory to be created, and immediately
1170 * opened. When used with the XO_SUBVOLUME flag this will even create a btrfs subvolume.
1172 * • If O_CREAT is used with XO_LABEL, any created file will be immediately relabelled.
1174 * • If the path is specified NULL or empty, behaves like fd_reopen().
1176 * • If XO_NOCOW is specified will turn on the NOCOW btrfs flag on the file, if available.
1178 * • if XO_REGULAR is specified will return an error if inode is not a regular file.
1180 * • If mode is specified as MODE_INVALID, we'll use 0755 for dirs, and 0644 for regular files.
1183 if (mode
== MODE_INVALID
)
1184 mode
= (open_flags
& O_DIRECTORY
) ? 0755 : 0644;
1186 if (isempty(path
)) {
1187 assert(!FLAGS_SET(open_flags
, O_CREAT
|O_EXCL
));
1189 if (FLAGS_SET(xopen_flags
, XO_REGULAR
)) {
1190 r
= fd_verify_regular(dir_fd
);
1195 return fd_reopen(dir_fd
, open_flags
& ~O_NOFOLLOW
);
1198 bool call_label_ops_post
= false;
1200 if (FLAGS_SET(open_flags
, O_CREAT
) && FLAGS_SET(xopen_flags
, XO_LABEL
)) {
1201 r
= label_ops_pre(dir_fd
, path
, FLAGS_SET(open_flags
, O_DIRECTORY
) ? S_IFDIR
: S_IFREG
);
1205 call_label_ops_post
= true;
1208 if (FLAGS_SET(open_flags
, O_DIRECTORY
|O_CREAT
)) {
1209 if (FLAGS_SET(xopen_flags
, XO_SUBVOLUME
))
1210 r
= btrfs_subvol_make_fallback(dir_fd
, path
, mode
);
1212 r
= RET_NERRNO(mkdirat(dir_fd
, path
, mode
));
1214 if (FLAGS_SET(open_flags
, O_EXCL
))
1221 open_flags
&= ~(O_EXCL
|O_CREAT
);
1224 if (FLAGS_SET(xopen_flags
, XO_REGULAR
)) {
1225 /* Guarantee we return a regular fd only, and don't open the file unless we verified it
1228 if (FLAGS_SET(open_flags
, O_PATH
)) {
1229 fd
= openat(dir_fd
, path
, open_flags
, mode
);
1235 r
= fd_verify_regular(fd
);
1239 } else if (FLAGS_SET(open_flags
, O_CREAT
|O_EXCL
)) {
1240 /* In O_EXCL mode we can just create the thing, everything is dealt with for us */
1241 fd
= openat(dir_fd
, path
, open_flags
, mode
);
1249 /* Otherwise pin the inode first via O_PATH */
1250 _cleanup_close_
int inode_fd
= openat(dir_fd
, path
, O_PATH
|O_CLOEXEC
|(open_flags
& O_NOFOLLOW
));
1252 if (errno
!= ENOENT
|| !FLAGS_SET(open_flags
, O_CREAT
)) {
1257 /* Doesn't exist yet, then try to create it */
1258 fd
= openat(dir_fd
, path
, open_flags
|O_CREAT
|O_EXCL
, mode
);
1266 /* OK, we pinned it. Now verify it's actually a regular file, and then reopen it */
1267 r
= fd_verify_regular(inode_fd
);
1271 fd
= fd_reopen(inode_fd
, open_flags
& ~(O_NOFOLLOW
|O_CREAT
));
1279 fd
= openat_report_new(dir_fd
, path
, open_flags
, mode
, &made_file
);
1286 if (call_label_ops_post
) {
1287 call_label_ops_post
= false;
1289 r
= label_ops_post(fd
, /* path= */ NULL
, made_file
|| made_dir
);
1294 if (FLAGS_SET(xopen_flags
, XO_NOCOW
)) {
1295 r
= chattr_fd(fd
, FS_NOCOW_FL
, FS_NOCOW_FL
);
1296 if (r
< 0 && !ERRNO_IS_IOCTL_NOT_SUPPORTED(r
))
1303 if (call_label_ops_post
)
1304 (void) label_ops_post(fd
>= 0 ? fd
: dir_fd
, fd
>= 0 ? NULL
: path
, made_dir
|| made_file
);
1306 if (made_dir
|| made_file
)
1307 (void) unlinkat(dir_fd
, path
, made_dir
? AT_REMOVEDIR
: 0);
1312 int xopenat_lock_full(
1316 XOpenFlags xopen_flags
,
1321 _cleanup_close_
int fd
= -EBADF
;
1324 assert(dir_fd
>= 0 || dir_fd
== AT_FDCWD
);
1325 assert(IN_SET(operation
& ~LOCK_NB
, LOCK_EX
, LOCK_SH
));
1327 /* POSIX/UNPOSIX locks don't work on directories (errno is set to -EBADF so let's return early with
1328 * the same error here). */
1329 if (FLAGS_SET(open_flags
, O_DIRECTORY
) && !IN_SET(locktype
, LOCK_BSD
, LOCK_NONE
))
1335 fd
= xopenat_full(dir_fd
, path
, open_flags
, xopen_flags
, mode
);
1339 r
= lock_generic(fd
, locktype
, operation
);
1343 /* If we acquired the lock, let's check if the file/directory still exists in the file
1344 * system. If not, then the previous exclusive owner removed it and then closed it. In such a
1345 * case our acquired lock is worthless, hence try again. */
1347 if (fstat(fd
, &st
) < 0)
1349 if (st
.st_nlink
> 0)
1352 fd
= safe_close(fd
);
1358 int link_fd(int fd
, int newdirfd
, const char *newpath
) {
1362 assert(newdirfd
>= 0 || newdirfd
== AT_FDCWD
);
1365 /* Try to link via AT_EMPTY_PATH first. This fails with ENOENT if we don't have CAP_DAC_READ_SEARCH
1366 * on kernels < 6.10, in which case we'd then resort to /proc/self/fd/ dance.
1368 * See also: https://github.com/torvalds/linux/commit/42bd2af5950456d46fdaa91c3a8fb02e680f19f5 */
1369 r
= RET_NERRNO(linkat(fd
, "", newdirfd
, newpath
, AT_EMPTY_PATH
));
1371 r
= RET_NERRNO(linkat(AT_FDCWD
, FORMAT_PROC_FD_PATH(fd
), newdirfd
, newpath
, AT_SYMLINK_FOLLOW
));
1372 if (r
== -ENOENT
&& proc_mounted() == 0) /* No proc_fd_enoent_errno() here because we don't
1373 know if it's the target path that's missing. */
1380 int linkat_replace(int olddirfd
, const char *oldpath
, int newdirfd
, const char *newpath
) {
1381 _cleanup_close_
int old_fd
= -EBADF
;
1384 assert(olddirfd
>= 0 || olddirfd
== AT_FDCWD
);
1385 assert(newdirfd
>= 0 || newdirfd
== AT_FDCWD
);
1386 assert(!isempty(newpath
)); /* source path is optional, but the target path is not */
1388 /* Like linkat() but replaces the target if needed. Is a NOP if source and target already share the
1391 if (olddirfd
== AT_FDCWD
&& isempty(oldpath
)) /* Refuse operating on the cwd (which is a dir, and dirs can't be hardlinked) */
1394 if (path_implies_directory(oldpath
)) /* Refuse these definite directories early */
1397 if (path_implies_directory(newpath
))
1400 /* First, try to link this directly */
1402 r
= RET_NERRNO(linkat(olddirfd
, oldpath
, newdirfd
, newpath
, 0));
1404 r
= link_fd(olddirfd
, newdirfd
, newpath
);
1410 old_fd
= xopenat(olddirfd
, oldpath
, O_PATH
|O_CLOEXEC
);
1415 if (fstat(old_fd
, &old_st
) < 0)
1418 if (S_ISDIR(old_st
.st_mode
)) /* Don't bother if we are operating on a directory */
1422 if (fstatat(newdirfd
, newpath
, &new_st
, AT_SYMLINK_NOFOLLOW
) < 0)
1425 if (S_ISDIR(new_st
.st_mode
)) /* Refuse replacing directories */
1428 if (stat_inode_same(&old_st
, &new_st
)) /* Already the same inode? Then shortcut this */
1431 _cleanup_free_
char *tmp_path
= NULL
;
1432 r
= tempfn_random(newpath
, /* extra= */ NULL
, &tmp_path
);
1436 r
= link_fd(old_fd
, newdirfd
, tmp_path
);
1438 if (!ERRNO_IS_PRIVILEGE(r
))
1441 /* If that didn't work due to permissions then go via the path of the dentry */
1442 r
= RET_NERRNO(linkat(olddirfd
, oldpath
, newdirfd
, tmp_path
, 0));
1447 r
= RET_NERRNO(renameat(newdirfd
, tmp_path
, newdirfd
, newpath
));
1449 (void) unlinkat(newdirfd
, tmp_path
, /* flags= */ 0);