blob: 932a9e184e0dd533d08bc929a42812b5d59dcae7 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +00002 * This file is part of net-yy-unix strace test.
3 *
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +00004 * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00005 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +000030#include "tests.h"
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000031#include <assert.h>
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000032#include <errno.h>
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000033#include <stddef.h>
Dmitry V. Levinb6535092015-01-08 22:29:12 +000034#include <stdint.h>
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000035#include <string.h>
36#include <unistd.h>
37#include <sys/socket.h>
38#include <sys/un.h>
39#include <linux/netlink.h>
40#include <linux/sock_diag.h>
41#include <linux/unix_diag.h>
42
Dmitry V. Levind9f7e7a2015-01-09 03:03:39 +000043#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
44# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
45#endif
46
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +000047static void
Dmitry V. Levin69bfc892016-01-31 00:35:29 +000048send_query(const int fd, const unsigned int inode)
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000049{
50 struct sockaddr_nl nladdr = {
51 .nl_family = AF_NETLINK
52 };
53 struct {
54 struct nlmsghdr nlh;
55 struct unix_diag_req udr;
56 } req = {
57 .nlh = {
58 .nlmsg_len = sizeof(req),
59 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
Dmitry V. Levin69bfc892016-01-31 00:35:29 +000060 .nlmsg_flags = NLM_F_REQUEST
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000061 },
62 .udr = {
Dmitry V. Levin69bfc892016-01-31 00:35:29 +000063 .sdiag_family = AF_UNIX,
64 .udiag_ino = inode,
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000065 .udiag_states = -1,
Dmitry V. Levin69bfc892016-01-31 00:35:29 +000066 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER,
67 .udiag_cookie = { ~0U, ~0U }
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000068 }
69 };
70 struct iovec iov = {
71 .iov_base = &req,
72 .iov_len = sizeof(req)
73 };
74 struct msghdr msg = {
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000075 .msg_name = (void *) &nladdr,
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000076 .msg_namelen = sizeof(nladdr),
77 .msg_iov = &iov,
78 .msg_iovlen = 1
79 };
80
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +000081 if (sendmsg(fd, &msg, 0) <= 0)
82 perror_msg_and_skip("sendmsg");
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000083}
84
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +000085static void
Dmitry V. Levin69bfc892016-01-31 00:35:29 +000086check_responses(const int fd, const unsigned int inode)
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000087{
88 static char buf[8192];
89 struct sockaddr_nl nladdr = {
90 .nl_family = AF_NETLINK
91 };
92 struct iovec iov = {
93 .iov_base = buf,
94 .iov_len = sizeof(buf)
95 };
96 struct msghdr msg = {
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +000097 .msg_name = (void *) &nladdr,
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000098 .msg_namelen = sizeof(nladdr),
99 .msg_iov = &iov,
100 .msg_iovlen = 1
101 };
102
103 ssize_t ret = recvmsg(fd, &msg, 0);
104 if (ret <= 0)
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +0000105 perror_msg_and_skip("recvmsg");
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000106
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000107 struct nlmsghdr *h = (struct nlmsghdr *) buf;
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +0000108 if (!NLMSG_OK(h, ret))
109 error_msg_and_skip("!NLMSG_OK");
Dmitry V. Levin3c86e0e2016-01-30 23:50:54 +0000110 if (h->nlmsg_type == NLMSG_ERROR) {
111 const struct nlmsgerr *err = NLMSG_DATA(h);
112 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
113 error_msg_and_skip("NLMSG_ERROR");
114 errno = -err->error;
115 perror_msg_and_skip("NLMSG_ERROR");
116 }
117 if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
118 error_msg_and_skip("unexpected nlmsg_type %u",
119 (unsigned) h->nlmsg_type);
120
121 const struct unix_diag_msg *diag = NLMSG_DATA(h);
122 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
123 error_msg_and_skip("short response");
Dmitry V. Levin69bfc892016-01-31 00:35:29 +0000124 if (diag->udiag_ino != inode)
125 error_msg_and_skip("inode mismatch");
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000126}
127
128#define SUN_PATH "netlink_unix_diag_socket"
129int main(void)
130{
131 struct sockaddr_un addr = {
132 .sun_family = AF_UNIX,
133 .sun_path = SUN_PATH
134 };
135 socklen_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(SUN_PATH);
136
137 close(0);
138 close(1);
139
140 (void) unlink(SUN_PATH);
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +0000141 if (socket(PF_LOCAL, SOCK_STREAM, 0))
142 perror_msg_and_skip("socket PF_LOCAL");
143 if (bind(0, (struct sockaddr *) &addr, len))
144 perror_msg_and_skip("bind");
145 if (listen(0, 5))
146 perror_msg_and_skip("listen");
Dmitry V. Levin6add1b02015-03-18 20:18:27 +0000147
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000148 assert(unlink(SUN_PATH) == 0);
149
150 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +0000151 perror_msg_and_skip("socket AF_NETLINK");
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000152
Dmitry V. Levin69bfc892016-01-31 00:35:29 +0000153 unsigned int inode = inode_of_sockfd(0);
154 send_query(1, inode);
155 check_responses(1, inode);
Dmitry V. Levinea56b5b2016-01-06 15:55:12 +0000156 return 0;
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000157}