blob: b6e026e335041215ae664f2036b4e14ccee45dc0 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2014-2015 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000028#include <assert.h>
29#include <stddef.h>
Dmitry V. Levinb6535092015-01-08 22:29:12 +000030#include <stdint.h>
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000031#include <string.h>
32#include <unistd.h>
33#include <sys/socket.h>
34#include <sys/un.h>
35#include <linux/netlink.h>
36#include <linux/sock_diag.h>
37#include <linux/unix_diag.h>
38
Dmitry V. Levind9f7e7a2015-01-09 03:03:39 +000039#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
40# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
41#endif
42
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000043static int
44send_query(const int fd, const int family, const int proto)
45{
46 struct sockaddr_nl nladdr = {
47 .nl_family = AF_NETLINK
48 };
49 struct {
50 struct nlmsghdr nlh;
51 struct unix_diag_req udr;
52 } req = {
53 .nlh = {
54 .nlmsg_len = sizeof(req),
55 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
56 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
57 },
58 .udr = {
59 .sdiag_family = family,
60 .sdiag_protocol = proto,
61 .udiag_states = -1,
62 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
63 }
64 };
65 struct iovec iov = {
66 .iov_base = &req,
67 .iov_len = sizeof(req)
68 };
69 struct msghdr msg = {
70 .msg_name = (void*)&nladdr,
71 .msg_namelen = sizeof(nladdr),
72 .msg_iov = &iov,
73 .msg_iovlen = 1
74 };
75
76 return sendmsg(fd, &msg, 0) > 0;
77}
78
79static int
80check_responses(const int fd)
81{
82 static char buf[8192];
83 struct sockaddr_nl nladdr = {
84 .nl_family = AF_NETLINK
85 };
86 struct iovec iov = {
87 .iov_base = buf,
88 .iov_len = sizeof(buf)
89 };
90 struct msghdr msg = {
91 .msg_name = (void*)&nladdr,
92 .msg_namelen = sizeof(nladdr),
93 .msg_iov = &iov,
94 .msg_iovlen = 1
95 };
96
97 ssize_t ret = recvmsg(fd, &msg, 0);
98 if (ret <= 0)
99 return 0;
100
101 struct nlmsghdr *h = (struct nlmsghdr*)buf;
102 return (NLMSG_OK(h, ret) &&
103 h->nlmsg_type != NLMSG_ERROR &&
104 h->nlmsg_type != NLMSG_DONE) ? 1 : 0;
105}
106
107#define SUN_PATH "netlink_unix_diag_socket"
108int main(void)
109{
110 struct sockaddr_un addr = {
111 .sun_family = AF_UNIX,
112 .sun_path = SUN_PATH
113 };
114 socklen_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(SUN_PATH);
115
116 close(0);
117 close(1);
118
119 (void) unlink(SUN_PATH);
Dmitry V. Levin6add1b02015-03-18 20:18:27 +0000120 if (socket(PF_LOCAL, SOCK_STREAM, 0) ||
121 bind(0, (struct sockaddr *) &addr, len) ||
122 listen(0, 5))
123 return 77;
124
Dmitry V. Levin6b5df322014-12-25 00:11:40 +0000125 assert(unlink(SUN_PATH) == 0);
126
127 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
128 return 77;
129
130 return (send_query(1, AF_UNIX, 0) &&
131 check_responses(1)) ? 0 : 77;
132}