socket-util: add trivial socket_get_cookie() helper
This commit is contained in:
@@ -1959,3 +1959,18 @@ finalize:
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int socket_get_cookie(int fd, uint64_t *ret) {
|
||||
assert(fd >= 0);
|
||||
|
||||
uint64_t cookie = 0;
|
||||
socklen_t cookie_len = sizeof(cookie);
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0)
|
||||
return -errno;
|
||||
|
||||
assert(cookie_len == sizeof(cookie));
|
||||
if (ret)
|
||||
*ret = cookie;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -354,3 +354,5 @@ int socket_address_equal_unix(const char *a, const char *b);
|
||||
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);
|
||||
|
||||
@@ -717,9 +717,7 @@ TEST(sock_diag_unix) {
|
||||
ASSERT_OK_ERRNO(fstat(unix_fd, &st));
|
||||
|
||||
uint64_t cookie;
|
||||
socklen_t cookie_len = sizeof(cookie);
|
||||
ASSERT_OK_ERRNO(getsockopt(unix_fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len));
|
||||
ASSERT_EQ(cookie_len, sizeof(cookie));
|
||||
ASSERT_OK(socket_get_cookie(unix_fd, &cookie));
|
||||
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
|
||||
ASSERT_OK(sd_sock_diag_message_new_unix(nl, &message, st.st_ino, cookie, UDIAG_SHOW_RQLEN));
|
||||
|
||||
@@ -504,12 +504,10 @@ int af_unix_get_qlen(int fd, uint32_t *ret) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
uint64_t cookie = 0;
|
||||
socklen_t cookie_len = sizeof(cookie);
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_COOKIE, &cookie, &cookie_len) < 0)
|
||||
return -errno;
|
||||
|
||||
assert(cookie_len == sizeof(cookie));
|
||||
uint64_t cookie;
|
||||
r = socket_get_cookie(fd, &cookie);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
|
||||
r = sd_sock_diag_message_new_unix(nl, &message, st.st_ino, cookie, UDIAG_SHOW_RQLEN);
|
||||
|
||||
Reference in New Issue
Block a user