summaryrefslogtreecommitdiff
path: root/src/basic/fs-util.c
Commit message (Collapse)AuthorAge
* fs-util: don't alter errno in unlink_tempfilep()Lennart Poettering2018-08-24
| | | | | | | | | | | Functions whose only purpose is to be used with _cleanup_() should not touch errno, so that failing removals do not alter errno at unexpected places. This is already done in unlink_and_freep(), rmdir_and_freep(), rm_rf_physical_and_freep(), hence do so for unlink_tempfilep(), too. Follow-up for #9013
* Use STRLEN in two placesZbigniew Jędrzejewski-Szmek2018-08-24
|
* fs-util,test: add helper to remove tempfilesZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | This simplifies the use of tempfiles in tests and fixes "leaked" temporary files in test-fileio, test-catalog, test-conf-parser. Not the whole tree is converted.
* basic/fs-util: remove logically dead codeZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | We can jump to chase_one from two places. In the first 'todo' is set to 'buffer', which comes from path_make_absolute_cwd() and is nonnull In the second 'todo' is set to 'joined' which is checked to be nonull a few lines above the jump. So let's kill the code that deals with null todo there. CID #1390941.
* fs-util: introduce fchmod_opath()Franck Bui2018-08-24
| | | | fchmod(2) still doesn't take file descriptors opened with O_PATH.
* fs-util: export how many iterations chase_symlinks() executes at maxLennart Poettering2018-08-24
|
* fs-util: add new CHASE_STEP flag to chase_symlinks()Lennart Poettering2018-08-24
| | | | | | | | If the flag is set only a single step of the normalization is executed, and the resulting path is returned. This allows callers to normalize piecemeal, taking into account every single intermediary path of the normalization.
* path-lookup: properly chase paths when reducing with root dir (#8750)Lennart Poettering2018-08-24
| | | | Let's make this correct.
* util-lib: introduce new empty_or_root() helper (#8746)Lennart Poettering2018-08-24
| | | | | | | | We check the same condition at various places. Let's add a trivial, common helper for this, and use it everywhere. It's not going to make things much faster or much shorter, but I think a lot more readable
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-08-24
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* fs-util: add shortcut for chase_symlinks() when it is called like open(O_PATH)Lennart Poettering2018-08-24
| | | | | Let's optimize things, and let the kernel chase the paths if none of the features chase_symlinks() offers are actually used.
* fs-util: add calls that combine chase_symlinks() and open()/opendir() in oneLennart Poettering2018-08-24
| | | | | This is useful when opening files within disk images, as we'll then take the relative root directory properly into account.
* fs-util: add shortcut for chase_symlinks() when it is called like open(O_PATH)Lennart Poettering2018-08-24
| | | | | Let's optimize things, and let the kernel chase the paths if none of the features chase_symlinks() offers are actually used.
* fs-util: add calls that combine chase_symlinks() and open()/opendir() in oneLennart Poettering2018-08-24
| | | | | This is useful when opening files within disk images, as we'll then take the relative root directory properly into account.
* fs-util: add new CHASE_TRAIL_SLASH flag for chase_symlinks()Lennart Poettering2018-08-24
| | | | | | | | | | | | | | | | | | | | | | | | This rearranges chase_symlinks() a bit: if no special flags are specified it will now revert to behaviour before b12d25a8d631af00b200e7aa9dbba6ba4a4a59ff. However, if the new CHASE_TRAIL_SLASH flag is specified it will follow the behaviour introduced by that commit. I wasn't sure which one to make the beaviour that requires specification of a flag to enable. I opted to make the "append trailing slash" behaviour the one to enable by a flag, following the thinking that the function should primarily be used to generate a normalized path, and I am pretty sure a path without trailing slash is the more "normalized" one, as the trailing slash is not really a part of it, but merely a "decorator" that tells various system calls to generate ENOTDIR if the path doesn't refer to a path. Or to say this differently: if the slash was part of normalization then we really should add it in all cases when the final path is a directory, not just when the user originally specified it. Fixes: #8544 Replaces: #8545
* macro: introduce new TAKE_FD() macroLennart Poettering2018-08-24
| | | | | | | This is similar to TAKE_PTR() but operates on file descriptors, and thus assigns -1 to the fd parameter after returning it. Removes 60 lines from our codebase. Pretty good too I think.
* macro: introduce TAKE_PTR() macroLennart Poettering2018-08-24
| | | | | | | | | | | | | | | | This macro will read a pointer of any type, return it, and set the pointer to NULL. This is useful as an explicit concept of passing ownership of a memory area between pointers. This takes inspiration from Rust: https://doc.rust-lang.org/std/option/enum.Option.html#method.take and was suggested by Alan Jenkins (@sourcejedi). It drops ~160 lines of code from our codebase, which makes me like it. Also, I think it clarifies passing of ownership, and thus helps readability a bit (at least for the initiated who know the new macro)
* basic/fs-util: skip fsync_directory_of_file() if /proc/self/fd/ is not ↵Zbigniew Jędrzejewski-Szmek2018-06-28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | available (#8386) When systemd is running under lorax (in Fedora compose process), it'd think that it failed to write /etc/machine-id, even though the write succeeded, because fsync_directory_of_file() would fail, because /proc/self/fd/ is not available. fsync_directory_of_file() is mostly an additional safety net, so I think it's best to just silently ignore the error. Strace of pid1: 35791 stat("/etc", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0 35791 openat(AT_FDCWD, "/etc/machine-id", O_RDWR|O_CREAT|O_NOCTTY|O_CLOEXEC, 0444) = 3 35791 umask(022) = 000 35791 read(3, "", 38) = 0 35791 openat(AT_FDCWD, "/var/lib/dbus/machine-id", O_RDONLY|O_NOCTTY|O_NOFOLLOW|O_CLOEXEC) = -1 ENOENT (No such file o r directory) 35791 openat(AT_FDCWD, "/sys/class/dmi/id/product_name", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 openat(AT_FDCWD, "/sys/class/dmi/id/sys_vendor", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 openat(AT_FDCWD, "/sys/class/dmi/id/board_vendor", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 openat(AT_FDCWD, "/sys/class/dmi/id/bios_vendor", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 access("/proc/xen", F_OK) = -1 ENOENT (No such file or directory) 35791 openat(AT_FDCWD, "/sys/hypervisor/type", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 openat(AT_FDCWD, "/proc/cpuinfo", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) 35791 getrandom("\xb8\x82\xed\xd4\x35\x11\xd0\xeb\xa6\x79\xd7\x31\x6e\x7b\x99\xce", 16, GRND_NONBLOCK) = 16 35791 writev(2, [{iov_base="Initializing machine ID from random generator.", iov_len=46}, {iov_base="\n", iov_len=1}], 2) = 47 35791 lseek(3, 0, SEEK_SET) = 0 35791 ftruncate(3, 0) = 0 35791 write(3, "b882edd4351140eba679d7316e7b99ce\n", 33) = 33 35791 fsync(3) = 0 35791 fstat(3, {st_mode=S_IFREG|0444, st_size=33, ...}) = 0 35791 readlinkat(AT_FDCWD, "/proc/self/fd/3", 0x564df8c694c0, 99) = -1 ENOENT (No such file or directory) 35791 close(3) = 0 35791 umask(022) = 022 35791 openat(AT_FDCWD, "/run/machine-id", O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC|O_CLOEXEC, 0444) = 3 35791 write(3, "b882edd4351140eba679d7316e7b99ce\n", 33) = 33 35791 close(3) = 0 35791 umask(022) = 022 35791 mount("/run/machine-id", "/etc/machine-id", NULL, MS_BIND, NULL) = 0 35791 writev(2, [{iov_base="Installed transient /etc/machine-id file.", iov_len=41}, {iov_base="\n", iov_len=1}], 2) = 42 35791 mount(NULL, "/etc/machine-id", NULL, MS_RDONLY|MS_REMOUNT|MS_BIND, NULL) = 0 https://bugzilla.redhat.com/show_bug.cgi?id=1552843 (cherry picked from commit 3ceae1bc14d2da3fc1fe4753d6657759012256dc)
* fs-util: move fsync_directory_of_file() into generic codeLennart Poettering2018-05-30
| | | | | This function used by the journal code is pretty useful generically, let's move it to fs-util.c to make it useful for other code too.
* login,user-sessions: always warn when we fail to remove nologin fileZbigniew Jędrzejewski-Szmek2018-05-30
| | | | | | | This usually is very annoying to users who then cannot log in, so make sure we always warn if that happens (selinux, or whatever other reason). This reverts a790812cb349c5cef95d1b4a20fc80ca08d3a145.
* fs-util: add new unlinkat_deallocate() helperLennart Poettering2018-05-30
| | | | | | | | | This new helper not only removes a file from a directory but also ensures its space on disk is deallocated, by either punching a hole over the full file or truncating the file afterwards if the file's link counter is 0. This is useful in "vacuuming" algorithms to ensure that client's can't keep the disk space the vacuuming is supposed to recover pinned simply by keeping an fd open to it.
* fs-util: drop readlink_and_make_absolute_root()Yu Watanabe2018-05-30
|
* fs-util: drop readlink_and_canonicalize()Yu Watanabe2018-05-30
|
* fs-util: add new unlinkat_deallocate() helperLennart Poettering2018-05-30
| | | | | | | | | | | This new helper not only removes a file from a directory but also ensures its space on disk is deallocated, by either punching a hole over the full file or truncating the file afterwards if the file's link counter is 0. This is useful in "vacuuming" algorithms to ensure that client's can't keep the disk space the vacuuming is supposed to recover pinned simply by keeping an fd open to it. (cherry picked from commit 43767d9d5e0ce8923828aebf9154da7af83916f7)
* fs-util: chase_symlinks(): prevent double freeYu Watanabe2018-05-30
| | | | Fixes CID #1385316.
* fs-util: use _cleanup_close_ attributeYu Watanabe2018-05-30
| | | | | | | The commit f14f1806e329fe92d01f15c22a384702f0cb4ae0 introduced CHASE_SAFE flag. When the flag is set, then `fd_parent` may not be properly closed. This sets `_cleanup_close_` attribute to `fd_parent`. Thus, now `fd_parent` is always closed properly.
* fs-util: chase_symlinks(): support empty rootYu Watanabe2018-05-30
| | | | | | | | | The commit b1bfb848046e457f3cd623286b8cc1a5e5440023 makes chase_symlinks() recognize empty string for root as an invalid parameter. However, empty root is often used e.g. systemd-nspawn. This makes chase_symlinks() support empty string safely. Fixes #7927.
* fs-util: refuse taking a relative path to chase if "root" is specified and ↵Lennart Poettering2018-05-30
| | | | | | | | | | | CHASE_PREFIX_ROOT is set If we take a relative path we first make it absolute, based on the current working directory. But if CHASE_PREFIX_ROOT is passe we are supposed to make the path absolute taking the specified root path into account, but that makes no sense if we talk about the current working directory as that is relative to the host's root in any case. Hence, let's refuse this politely.
* fs-util: extra chase_symlink() safety check on "path" parameterLennart Poettering2018-05-30
| | | | | It's not clear what an empty "path" is even supposed to mean, hence refuse.
* fs-util: extra safety checks on chase_symlinks() root parameterLennart Poettering2018-05-30
| | | | Let's handle root="" and root="/" safely.
* fs-util: add new chase_symlinks() flag CHASE_OPENLennart Poettering2018-05-30
| | | | | | | | | | | The new flag returns the O_PATH fd of the final component, which may be converted into a proper fd by open()ing it again through the /proc/self/fd/xyz path. Together with O_SAFE this provides us with a somewhat safe way to open() files in directories potentially owned by unprivileged code, where we want to refuse operation if any symlink tricks are played pointing to privileged files.
* fs-util: add new CHASE_SAFE flag to chase_symlinks()Lennart Poettering2018-05-30
| | | | | | | | | When the flag is specified we won't transition to a privilege-owned file or directory from an unprivileged-owned one. This is useful when privileged code wants to load data from a file unprivileged users have write access to, and validates the ownership, but want's to make sure that no symlink games are played to read a root-owned system file belonging to a different context.
* log: minimize includes in log.hLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | | | | | | | | | log.h really should only include the bare minimum of other headers, as it is really pulled into pretty much everything else and already in itself one of the most basic pieces of code we have. Let's hence drop inclusion of: 1. sd-id128.h because it's entirely unneeded in current log.h 2. errno.h, dito. 3. sys/signalfd.h which we can replace by a simple struct forward declaration 4. process-util.h which was needed for getpid_cached() which we now hide in a funciton log_emergency_level() instead, which nicely abstracts the details away. 5. sys/socket.h which was needed for struct iovec, but a simple struct forward declaration suffices for that too. Ultimately this actually makes our source tree larger (since users of the functionality above must now include it themselves, log.h won't do that for them), but I think it helps to untangle our web of includes a tiny bit. (Background: I'd like to isolate the generic bits of src/basic/ enough so that we can do a git submodule import into casync for it)
* fs-util: rework touch_file() so that it can touch socket file nodesLennart Poettering2018-05-30
| | | | | | | | | | | | | | | | | | Let's rework touch_file() so that it works correctly on sockets, fifos, and device nodes: let's open an O_PATH file descriptor first and operate based on that, if we can. This is usually the better option as it this means we can open AF_UNIX nodes in the file system, and update their timestamps and ownership correctly. It also means we can correctly touch symlinks and block/character devices without triggering their drivers. Moreover, by operating on an O_PATH fd we can make sure that we operate on the same inode the whole time, and it can't be swapped out in the middle. While we are at it, rework the call so that we try to adjust as much as we can before returning on error. This is a good idea as we call the function quite often without checking its result, and hence it's best to leave the files around in the most "correct" fashion possible.
* Prep v236 : Add missing SPDX-License-Identifier (2/9) src/basicSven Eden2018-03-26
|
* fs-util: remove comment about non-existing functionYu Watanabe2017-12-01
|
* fs-util: chase_symlinks(): remove unnecessary slash at the headYu Watanabe2017-12-01
| | | | | Before this, chase_symlinks("/../../foo/bar",...) returns //foo/bar. This removes the unnecessary slash at the head.
* fs-util: small tweak in chase_symlinks()Lennart Poettering2017-10-04
| | | | | | | | If we follow an absolute symlink there's no need to prefix the path with a "/", since by definition it already has one. This helps suppressing double "/" in resolved paths containing absolute symlinks.
* tree-wide: use IN_SET macro (#6977)Yu Watanabe2017-12-08
|
* build-sys: use #if Y instead of #ifdef Y everywhereZbigniew Jędrzejewski-Szmek2017-11-23
| | | | | | | | | | | | | | | The advantage is that is the name is mispellt, cpp will warn us. $ git grep -Ee "conf.set\('(HAVE|ENABLE)_" -l|xargs sed -r -i "s/conf.set\('(HAVE|ENABLE)_/conf.set10('\1_/" $ git grep -Ee '#ifn?def (HAVE|ENABLE)' -l|xargs sed -r -i 's/#ifdef (HAVE|ENABLE)/#if \1/; s/#ifndef (HAVE|ENABLE)/#if ! \1/;' $ git grep -Ee 'if.*defined\(HAVE' -l|xargs sed -i -r 's/defined\((HAVE_[A-Z0-9_]*)\)/\1/g' $ git grep -Ee 'if.*defined\(ENABLE' -l|xargs sed -i -r 's/defined\((ENABLE_[A-Z0-9_]*)\)/\1/g' + manual changes to meson.build squash! build-sys: use #if Y instead of #ifdef Y everywhere v2: - fix incorrect setting of HAVE_LIBIDN2
* fs-util: propagate EEXIST error in symlink_idempotent() as EEXISTLennart Poettering2017-11-22
| | | | | We really shouldn't silently translate the error code here for no reason.
* cgroup, unit, fragment parser: make use of new firewall functionsDaniel Mack2017-11-21
|
* Add abstraction model for BPF programsDaniel Mack2017-11-20
| | | | | This object takes a number of bpf_insn members and wraps them together with the in-kernel reference id. Will be needed by the firewall code.
* tmpfiles: silently ignore any path that passes through autofs (#6506)NeilBrown2017-09-04
| | | | | | | | | | | | | | | | | If a path passes though an autofs filesystem, then accessing the path might trigger and automount. As elogind-tmpfiles is run before the network is up, and as automounts are often used for networked filesystems, this can cause a deadlock. So chase_symlinks is enhance to accept a new flag which tells it to check for autofs, and return -EREMOTE if autofs is found. tmpfiles is changed to check just before acting on a path so that it can avoid autofs even if a symlink was created earlier by tmpfiles that would send this path through an autofs. This fixes a deadlock that happens when /home is listed in /etc/fstab as x-elogind.automount for an NFS directory.
* Prep v235: Add missing includes and dependencies.Sven Eden2017-08-14
|
* Prep v235: Apply upstream fixes (3/10) [src/basic]Sven Eden2017-08-14
|
* Prep v233.3: Unmask various functions for future coverage tests.Sven Eden2017-07-19
| | | | | These functions, although not used by elogind itself, are mostly tiny and crucial for important tests to work.
* Prep v233: Add missing updates from upstream in src/basicSven Eden2017-07-17
|
* Prep v233: Unmask now needed functions in src/basicSven Eden2017-07-17
|
* util-lib: Fix chase_symlinks() with absolute symlinks (#5185)3chas32017-07-17
| | | | | | | | If chase_symlinks() encouters an absolute symlink, it resets the todo buffer to just the newly discovered symlink and discards any of the remaining previous symlink path. Regardless of whether or not the symlink is absolute or relative, we need to preserve the remainder of the path that has not yet been resolved.