1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017 The strace developers.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "tests.h"
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include <sys/sysmacros.h>
35 #include <netinet/tcp.h>
36 #include "test_nlattr.h"
37 #include <linux/sock_diag.h>
38 #include <linux/unix_diag.h>
39
40 static void
init_unix_diag_msg(struct nlmsghdr * const nlh,const unsigned int msg_len)41 init_unix_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
42 {
43 SET_STRUCT(struct nlmsghdr, nlh,
44 .nlmsg_len = msg_len,
45 .nlmsg_type = SOCK_DIAG_BY_FAMILY,
46 .nlmsg_flags = NLM_F_DUMP
47 );
48
49 struct unix_diag_msg *const msg = NLMSG_DATA(nlh);
50 SET_STRUCT(struct unix_diag_msg, msg,
51 .udiag_family = AF_UNIX,
52 .udiag_type = SOCK_STREAM,
53 .udiag_state = TCP_ESTABLISHED
54 );
55 }
56
57 static void
print_unix_diag_msg(const unsigned int msg_len)58 print_unix_diag_msg(const unsigned int msg_len)
59 {
60 printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
61 ", flags=NLM_F_DUMP, seq=0, pid=0}, {udiag_family=AF_UNIX"
62 ", udiag_type=SOCK_STREAM, udiag_state=TCP_ESTABLISHED"
63 ", udiag_ino=0, udiag_cookie=[0, 0]}",
64 msg_len);
65 }
66
67 static void
print_uint(const unsigned int * p)68 print_uint(const unsigned int *p)
69 {
70 printf("%u", *p);
71 }
72
73 int
main(void)74 main(void)
75 {
76 skip_if_unavailable("/proc/self/fd/");
77
78 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
79 const unsigned int hdrlen = sizeof(struct unix_diag_msg);
80 void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
81
82 static char pattern[4096];
83 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
84
85 static const struct unix_diag_vfs uv = {
86 .udiag_vfs_dev = 0xabcddafa,
87 .udiag_vfs_ino = 0xbafabcda
88 };
89 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
90 init_unix_diag_msg, print_unix_diag_msg,
91 UNIX_DIAG_VFS, pattern, uv,
92 printf("{udiag_vfs_dev=makedev(%u, %u)",
93 major(uv.udiag_vfs_dev),
94 minor(uv.udiag_vfs_dev));
95 PRINT_FIELD_U(", ", uv, udiag_vfs_ino);
96 printf("}"));
97
98 static const struct unix_diag_rqlen rql = {
99 .udiag_rqueue = 0xfabdcdad,
100 .udiag_wqueue = 0xbacdadcf
101 };
102 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
103 init_unix_diag_msg, print_unix_diag_msg,
104 UNIX_DIAG_RQLEN, pattern, rql,
105 PRINT_FIELD_U("{", rql, udiag_rqueue);
106 PRINT_FIELD_U(", ", rql, udiag_wqueue);
107 printf("}"));
108
109 static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac };
110 TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
111 init_unix_diag_msg, print_unix_diag_msg,
112 UNIX_DIAG_ICONS, pattern, inode, print_uint);
113
114 puts("+++ exited with 0 +++");
115 return 0;
116 }
117