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-2017 The strace developers.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/socket.h>
41 #include <linux/sock_diag.h>
42 #include <linux/unix_diag.h>
45 send_query(const int fd)
47 struct sockaddr_nl nladdr = {
48 .nl_family = AF_NETLINK
52 struct unix_diag_req udr;
55 .nlmsg_len = sizeof(req),
56 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
57 .nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP
60 .sdiag_family = AF_UNIX,
62 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
67 .iov_len = sizeof(req)
70 .msg_name = (void *) &nladdr,
71 .msg_namelen = sizeof(nladdr),
76 if (sendmsg(fd, &msg, 0) <= 0)
77 perror_msg_and_skip("sendmsg");
81 check_responses(const int fd)
85 long buf[8192 / sizeof(long)];
88 struct sockaddr_nl nladdr = {
89 .nl_family = AF_NETLINK
92 .iov_base = hdr_buf.buf,
93 .iov_len = sizeof(hdr_buf.buf)
96 .msg_name = (void *) &nladdr,
97 .msg_namelen = sizeof(nladdr),
102 ssize_t ret = recvmsg(fd, &msg, 0);
104 perror_msg_and_skip("recvmsg");
106 struct nlmsghdr *h = &hdr_buf.hdr;
107 if (!is_nlmsg_ok(h, ret))
108 error_msg_and_skip("!is_nlmsg_ok");
109 if (h->nlmsg_type == NLMSG_ERROR) {
110 const struct nlmsgerr *err = NLMSG_DATA(h);
111 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
112 error_msg_and_skip("NLMSG_ERROR");
114 perror_msg_and_skip("NLMSG_ERROR");
116 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
117 error_msg_and_skip("unexpected nlmsg_type %u",
118 (unsigned) h->nlmsg_type);
120 const struct unix_diag_msg *diag = NLMSG_DATA(h);
121 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
122 error_msg_and_skip("short response");
125 #define SUN_PATH "netlink_unix_diag_socket"
128 struct sockaddr_un addr = {
129 .sun_family = AF_UNIX,
132 socklen_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(SUN_PATH);
137 (void) unlink(SUN_PATH);
138 if (socket(AF_UNIX, SOCK_STREAM, 0))
139 perror_msg_and_skip("socket AF_UNIX");
140 if (bind(0, (struct sockaddr *) &addr, len))
141 perror_msg_and_skip("bind");
143 perror_msg_and_skip("listen");
145 assert(unlink(SUN_PATH) == 0);
147 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
148 perror_msg_and_skip("socket AF_NETLINK");