diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/fd-util.c | 15 | ||||
-rw-r--r-- | src/basic/fs-util.c | 13 |
2 files changed, 23 insertions, 5 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 678ab12bb8..6d7875361c 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -361,14 +361,21 @@ bool fdname_is_valid(const char *s) { } int fd_get_path(int fd, char **ret) { - char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; + _cleanup_close_ int dir = -1; + char fdname[DECIMAL_STR_MAX(int)]; int r; - xsprintf(procfs_path, "/proc/self/fd/%i", fd); + dir = open("/proc/self/fd/", O_CLOEXEC | O_DIRECTORY | O_PATH); + if (dir < 0) + /* /proc is not available or not set up properly, we're most likely + * in some chroot environment. */ + return errno == ENOENT ? -EOPNOTSUPP : -errno; - r = readlink_malloc(procfs_path, ret); + xsprintf(fdname, "%i", fd); - if (r == -ENOENT) /* If the file doesn't exist the fd is invalid */ + r = readlinkat_malloc(dir, fdname, ret); + if (r == -ENOENT) + /* If the file doesn't exist the fd is invalid */ return -EBADF; return r; diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 85c8070a1b..c65ba4bfe5 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -978,8 +978,19 @@ int fsync_directory_of_file(int fd) { return r; r = fd_get_path(fd, &path); - if (r < 0) + if (r < 0) { + log_debug("Failed to query /proc/self/fd/%d%s: %m", + fd, + r == -EOPNOTSUPP ? ", ignoring" : ""); + + if (r == -EOPNOTSUPP) + /* If /proc is not available, we're most likely running in some + * chroot environment, and syncing the directory is not very + * important in that case. Let's just silently do nothing. */ + return 0; + return r; + } if (!path_is_absolute(path)) return -EINVAL; |