blob: 817bf01ee0de57f3c6a755e00e17af9d97a5450b [file] [log] [blame]
Dmitry V. Levin6b5df322014-12-25 00:11:40 +00001#include <assert.h>
2#include <stddef.h>
Dmitry V. Levinb6535092015-01-08 22:29:12 +00003#include <stdint.h>
Dmitry V. Levin6b5df322014-12-25 00:11:40 +00004#include <string.h>
5#include <unistd.h>
6#include <sys/socket.h>
7#include <sys/un.h>
8#include <linux/netlink.h>
9#include <linux/sock_diag.h>
10#include <linux/unix_diag.h>
11
Dmitry V. Levind9f7e7a2015-01-09 03:03:39 +000012#if !defined NETLINK_SOCK_DIAG && defined NETLINK_INET_DIAG
13# define NETLINK_SOCK_DIAG NETLINK_INET_DIAG
14#endif
15
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000016static int
17send_query(const int fd, const int family, const int proto)
18{
19 struct sockaddr_nl nladdr = {
20 .nl_family = AF_NETLINK
21 };
22 struct {
23 struct nlmsghdr nlh;
24 struct unix_diag_req udr;
25 } req = {
26 .nlh = {
27 .nlmsg_len = sizeof(req),
28 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
29 .nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
30 },
31 .udr = {
32 .sdiag_family = family,
33 .sdiag_protocol = proto,
34 .udiag_states = -1,
35 .udiag_show = UDIAG_SHOW_NAME | UDIAG_SHOW_PEER
36 }
37 };
38 struct iovec iov = {
39 .iov_base = &req,
40 .iov_len = sizeof(req)
41 };
42 struct msghdr msg = {
43 .msg_name = (void*)&nladdr,
44 .msg_namelen = sizeof(nladdr),
45 .msg_iov = &iov,
46 .msg_iovlen = 1
47 };
48
49 return sendmsg(fd, &msg, 0) > 0;
50}
51
52static int
53check_responses(const int fd)
54{
55 static char buf[8192];
56 struct sockaddr_nl nladdr = {
57 .nl_family = AF_NETLINK
58 };
59 struct iovec iov = {
60 .iov_base = buf,
61 .iov_len = sizeof(buf)
62 };
63 struct msghdr msg = {
64 .msg_name = (void*)&nladdr,
65 .msg_namelen = sizeof(nladdr),
66 .msg_iov = &iov,
67 .msg_iovlen = 1
68 };
69
70 ssize_t ret = recvmsg(fd, &msg, 0);
71 if (ret <= 0)
72 return 0;
73
74 struct nlmsghdr *h = (struct nlmsghdr*)buf;
75 return (NLMSG_OK(h, ret) &&
76 h->nlmsg_type != NLMSG_ERROR &&
77 h->nlmsg_type != NLMSG_DONE) ? 1 : 0;
78}
79
80#define SUN_PATH "netlink_unix_diag_socket"
81int main(void)
82{
83 struct sockaddr_un addr = {
84 .sun_family = AF_UNIX,
85 .sun_path = SUN_PATH
86 };
87 socklen_t len = offsetof(struct sockaddr_un, sun_path) + sizeof(SUN_PATH);
88
89 close(0);
90 close(1);
91
92 (void) unlink(SUN_PATH);
Dmitry V. Levin6add1b02015-03-18 20:18:27 +000093 if (socket(PF_LOCAL, SOCK_STREAM, 0) ||
94 bind(0, (struct sockaddr *) &addr, len) ||
95 listen(0, 5))
96 return 77;
97
Dmitry V. Levin6b5df322014-12-25 00:11:40 +000098 assert(unlink(SUN_PATH) == 0);
99
100 if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
101 return 77;
102
103 return (send_query(1, AF_UNIX, 0) &&
104 check_responses(1)) ? 0 : 77;
105}