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.
This commit is contained in:
Lennart Poettering
2025-06-18 19:06:11 +09:00
committed by Yu Watanabe
parent 030b227cec
commit ea71d34738
4 changed files with 20 additions and 20 deletions
-18
View File
@@ -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;
-2
View File
@@ -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);
+18
View File
@@ -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));
}
}
}
+2
View File
@@ -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);