diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-03-26 13:25:51 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-03-29 15:33:12 +0200 |
commit | f2324783ceee84fbd24fd66f34d379fa1e6dc887 (patch) | |
tree | 8a11285a10b7aa91d3cebb407c2dc66c3be60401 /src/basic/fd-util.c | |
parent | tree-wide: remove unused variables (#8612) (diff) | |
download | systemd-f2324783ceee84fbd24fd66f34d379fa1e6dc887.tar.xz systemd-f2324783ceee84fbd24fd66f34d379fa1e6dc887.zip |
fd-util: introduce fd_reopen() helper for reopening an fd
We have the same code for this in place at various locations, let's
unify that. Also, let's repurpose test-fs-util.c as a test for this new
helper cal..
Diffstat (limited to 'src/basic/fd-util.c')
-rw-r--r-- | src/basic/fd-util.c | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index 0726aac38c..8bb8c9a629 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -427,7 +427,6 @@ int move_fd(int from, int to, int cloexec) { int acquire_data_fd(const void *data, size_t size, unsigned flags) { - char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; _cleanup_close_pair_ int pipefds[2] = { -1, -1 }; char pattern[] = "/dev/shm/data-fd-XXXXXX"; _cleanup_close_ int fd = -1; @@ -537,12 +536,7 @@ try_dev_shm: return -EIO; /* Let's reopen the thing, in order to get an O_RDONLY fd for the original O_RDWR one */ - xsprintf(procfs_path, "/proc/self/fd/%i", fd); - r = open(procfs_path, O_RDONLY|O_CLOEXEC); - if (r < 0) - return -errno; - - return r; + return fd_reopen(fd, O_RDONLY|O_CLOEXEC); } try_dev_shm_without_o_tmpfile: @@ -725,3 +719,22 @@ finish: return r; } + +int fd_reopen(int fd, int flags) { + char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; + int new_fd; + + /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to + * turn O_RDWR fds into O_RDONLY fds. + * + * This doesn't work on sockets (since they cannot be open()ed, ever). + * + * This implicitly resets the file read index to 0. */ + + xsprintf(procfs_path, "/proc/self/fd/%i", fd); + new_fd = open(procfs_path, flags); + if (new_fd < 0) + return -errno; + + return new_fd; +} |