summaryrefslogtreecommitdiffstats
path: root/src/basic/socket-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2020-04-17 22:26:14 +0200
committerLennart Poettering <lennart@poettering.net>2020-04-23 09:40:56 +0200
commit47eae6ce0c28b1984f8f5ec4c2f7bc428cf3b6ad (patch)
tree305b8e28d27e8055ab21fc4944adecf189eaf817 /src/basic/socket-util.c
parentMerge pull request #15530 from ssahani/lpr-dhcpv4-option-9 (diff)
downloadsystemd-47eae6ce0c28b1984f8f5ec4c2f7bc428cf3b6ad.tar.xz
systemd-47eae6ce0c28b1984f8f5ec4c2f7bc428cf3b6ad.zip
socket-util: add recvmsg_safe() wrapper that handles MSG_CTRUNC
Diffstat (limited to 'src/basic/socket-util.c')
-rw-r--r--src/basic/socket-util.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c
index ad467ab851..2d0564e66f 100644
--- a/src/basic/socket-util.c
+++ b/src/basic/socket-util.c
@@ -1171,3 +1171,24 @@ int socket_bind_to_ifindex(int fd, int ifindex) {
return socket_bind_to_ifname(fd, ifname);
}
+
+ssize_t recvmsg_safe(int sockfd, struct msghdr *msg, int flags) {
+ ssize_t n;
+
+ /* A wrapper around recvmsg() that checks for MSG_CTRUNC, and turns it into an error, in a reasonably
+ * safe way, closing any SCM_RIGHTS fds in the error path.
+ *
+ * Note that unlike our usual coding style this might modify *msg on failure. */
+
+ n = recvmsg(sockfd, msg, flags);
+ if (n < 0)
+ return -errno;
+
+ if (FLAGS_SET(msg->msg_flags, MSG_CTRUNC)) {
+ cmsg_close_all(msg);
+ return -EXFULL; /* a recognizable error code */
+ }
+
+ return n;
+
+}