From ea71d3473890f8d243ad2744c45b53cfcd60b146 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 18 Jun 2025 09:29:48 +0200 Subject: [PATCH] fd-util: move cmsg_close_all() to socket-util.[ch] This call only makes sense on sockets, it's not a generic fd concept, hence let's move this over. --- src/basic/fd-util.c | 18 ------------------ src/basic/fd-util.h | 2 -- src/basic/socket-util.c | 18 ++++++++++++++++++ src/basic/socket-util.h | 2 ++ 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c index d58f5a987a6..2056e029ccc 100644 --- a/src/basic/fd-util.c +++ b/src/basic/fd-util.c @@ -606,24 +606,6 @@ int same_fd(int a, int b) { return fa == fb; } -void cmsg_close_all(struct msghdr *mh) { - assert(mh); - - struct cmsghdr *cmsg; - CMSG_FOREACH(cmsg, mh) { - if (cmsg->cmsg_level != SOL_SOCKET) - continue; - - if (cmsg->cmsg_type == SCM_RIGHTS) - close_many(CMSG_TYPED_DATA(cmsg, int), - (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int)); - else if (cmsg->cmsg_type == SCM_PIDFD) { - assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int))); - safe_close(*CMSG_TYPED_DATA(cmsg, int)); - } - } -} - bool fdname_is_valid(const char *s) { const char *p; diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h index 3bb8152a7cf..c0b9c1b51ca 100644 --- a/src/basic/fd-util.h +++ b/src/basic/fd-util.h @@ -79,8 +79,6 @@ int pack_fds(int fds[], size_t n); int fd_validate(int fd); int same_fd(int a, int b); -void cmsg_close_all(struct msghdr *mh); - bool fdname_is_valid(const char *s); int fd_get_path(int fd, char **ret); diff --git a/src/basic/socket-util.c b/src/basic/socket-util.c index 387e4118464..2e76b3f6e10 100644 --- a/src/basic/socket-util.c +++ b/src/basic/socket-util.c @@ -1981,3 +1981,21 @@ int socket_get_cookie(int fd, uint64_t *ret) { return 0; } + +void cmsg_close_all(struct msghdr *mh) { + assert(mh); + + struct cmsghdr *cmsg; + CMSG_FOREACH(cmsg, mh) { + if (cmsg->cmsg_level != SOL_SOCKET) + continue; + + if (cmsg->cmsg_type == SCM_RIGHTS) + close_many(CMSG_TYPED_DATA(cmsg, int), + (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int)); + else if (cmsg->cmsg_type == SCM_PIDFD) { + assert(cmsg->cmsg_len == CMSG_LEN(sizeof(int))); + safe_close(*CMSG_TYPED_DATA(cmsg, int)); + } + } +} diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h index 962e9dd5a27..6493ccb4687 100644 --- a/src/basic/socket-util.h +++ b/src/basic/socket-util.h @@ -337,3 +337,5 @@ int vsock_get_local_cid(unsigned *ret); int netlink_socket_get_multicast_groups(int fd, size_t *ret_len, uint32_t **ret_groups); int socket_get_cookie(int fd, uint64_t *ret); + +void cmsg_close_all(struct msghdr *mh); -- 2.47.3