2 * This file is part of net-yy-unix strace test.
4 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Copyright (c) 2014-2018 The strace developers.
8 * SPDX-License-Identifier: GPL-2.0-or-later
18 #include <sys/socket.h>
21 #include <linux/sock_diag.h>
22 #include <linux/unix_diag.h>
25 send_query(const int fd)
27 struct sockaddr_nl nladdr = {
28 .nl_family = AF_NETLINK
32 struct unix_diag_req udr;
35 .nlmsg_len = sizeof(req),
36 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
37 .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
40 .sdiag_family = AF_UNIX,
42 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
47 .iov_len = sizeof(req)
50 .msg_name = (void *) &nladdr,
51 .msg_namelen = sizeof(nladdr),
56 if (sendmsg(fd, &msg, 0) <= 0)
57 perror_msg_and_skip("sendmsg");
61 check_responses(const int fd)
65 long buf[8192 / sizeof(long)];
68 struct sockaddr_nl nladdr = {
69 .nl_family = AF_NETLINK
72 .iov_base = hdr_buf.buf,
73 .iov_len = sizeof(hdr_buf.buf)
76 .msg_name = (void *) &nladdr,
77 .msg_namelen = sizeof(nladdr),
82 ssize_t ret = recvmsg(fd, &msg, 0);
84 perror_msg_and_skip("recvmsg");
86 struct nlmsghdr *h = &hdr_buf.hdr;
87 if (!is_nlmsg_ok(h, ret))
88 error_msg_and_skip("!is_nlmsg_ok");
89 if (h->nlmsg_type == NLMSG_ERROR) {
90 const struct nlmsgerr *err = NLMSG_DATA(h);
91 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
92 error_msg_and_skip("NLMSG_ERROR");
94 perror_msg_and_skip("NLMSG_ERROR");
96 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
97 error_msg_and_skip("unexpected nlmsg_type %u",
98 (unsigned) h->nlmsg_type);
100 const struct unix_diag_msg *diag = NLMSG_DATA(h);
101 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
102 error_msg_and_skip("short response");
105 #define SUN_PATH "netlink_unix_diag_socket"
108 struct sockaddr_un addr = {
109 .sun_family = AF_UNIX,
112 socklen_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(SUN_PATH);
117 (void) unlink(SUN_PATH);
118 if (socket(AF_UNIX, SOCK_STREAM, 0))
119 perror_msg_and_skip("socket AF_UNIX");
120 if (bind(0, (struct sockaddr *) &addr, len))
121 perror_msg_and_skip("bind");
123 perror_msg_and_skip("listen");
125 assert(unlink(SUN_PATH) == 0);
127 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
128 perror_msg_and_skip("socket AF_NETLINK");