diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-05-10 16:18:35 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-05-14 05:01:38 +0900 |
commit | 28ae8da97240c0048f03ed516d64fe064a7fc161 (patch) | |
tree | cff26bfa8ca06c1179d01a2cfdfbe27a17c9fe0e /src/basic/fileio.c | |
parent | 2679aee440409b89b8922f268c3e27f4327f8093 (diff) | |
download | systemd-28ae8da97240c0048f03ed516d64fe064a7fc161.tar.gz |
fileio: port read_file_full() to use connect_unix_path()
This way we can connect correctly to any AF_UNIX socket in the file
system, and even save some code. Yay!
This also adds some test code for this, that ensures read_file_full()
works correctly for AF_UNIX sockets that violate the 108 char limit.
Supporting sockets like this kinda matters I think, for the simple
reason that apps want to build socket paths via XDG_RUNTIME_DIR and
suchlike, and we should be able to connect to them, even via
non-normalized paths.
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r-- | src/basic/fileio.c | 27 |
1 files changed, 6 insertions, 21 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c index c2497ff856..2c4ba89a15 100644 --- a/src/basic/fileio.c +++ b/src/basic/fileio.c @@ -763,8 +763,7 @@ int read_full_file_full( r = xfopenat(dir_fd, filename, "re", 0, &f); if (r < 0) { - _cleanup_close_ int dfd = -1, sk = -1; - union sockaddr_union sa; + _cleanup_close_ int sk = -1; /* ENXIO is what Linux returns if we open a node that is an AF_UNIX socket */ if (r != -ENXIO) @@ -778,22 +777,6 @@ int read_full_file_full( if (offset != UINT64_MAX) return -ENXIO; - if (dir_fd == AT_FDCWD) - r = sockaddr_un_set_path(&sa.un, filename); - else { - /* If we shall operate relative to some directory, then let's use O_PATH first to - * open the socket inode, and then connect to it via /proc/self/fd/. We have to do - * this since there's not connectat() that takes a directory fd as first arg. */ - - dfd = openat(dir_fd, filename, O_PATH|O_CLOEXEC); - if (dfd < 0) - return -errno; - - r = sockaddr_un_set_path(&sa.un, FORMAT_PROC_FD_PATH(dfd)); - } - if (r < 0) - return r; - sk = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0); if (sk < 0) return -errno; @@ -812,9 +795,11 @@ int read_full_file_full( return -errno; } - if (connect(sk, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) - return errno == ENOTSOCK ? -ENXIO : -errno; /* propagate original error if this is - * not a socket after all */ + r = connect_unix_path(sk, dir_fd, filename); + if (IN_SET(r, -ENOTSOCK, -EINVAL)) /* propagate original error if this is not a socket after all */ + return -ENXIO; + if (r < 0) + return r; if (shutdown(sk, SHUT_WR) < 0) return -errno; |