1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 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,size_t i)68 print_uint(const unsigned int *p, size_t i)
69 {
70 printf("%u", *p);
71 }
72
73 int
main(void)74 main(void)
75 {
76 skip_if_unavailable("/proc/self/fd/");
77
78 static const struct unix_diag_vfs uv = {
79 .udiag_vfs_dev = 0xabcddafa,
80 .udiag_vfs_ino = 0xbafabcda
81 };
82 static const struct unix_diag_rqlen rql = {
83 .udiag_rqueue = 0xfabdcdad,
84 .udiag_wqueue = 0xbacdadcf
85 };
86 static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac };
87
88 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
89 const unsigned int hdrlen = sizeof(struct unix_diag_msg);
90 void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
91 NLA_HDRLEN + sizeof(inode));
92
93 static char pattern[4096];
94 fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
95
96 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
97 init_unix_diag_msg, print_unix_diag_msg,
98 UNIX_DIAG_VFS, pattern, uv,
99 printf("{udiag_vfs_dev=makedev(%u, %u)",
100 major(uv.udiag_vfs_dev),
101 minor(uv.udiag_vfs_dev));
102 PRINT_FIELD_U(", ", uv, udiag_vfs_ino);
103 printf("}"));
104
105 TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
106 init_unix_diag_msg, print_unix_diag_msg,
107 UNIX_DIAG_RQLEN, pattern, rql,
108 PRINT_FIELD_U("{", rql, udiag_rqueue);
109 PRINT_FIELD_U(", ", rql, udiag_wqueue);
110 printf("}"));
111
112 TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
113 init_unix_diag_msg, print_unix_diag_msg,
114 UNIX_DIAG_ICONS, pattern, inode, print_uint);
115
116 puts("+++ exited with 0 +++");
117 return 0;
118 }
119