2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2018 The strace developers.
6 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include <sys/socket.h>
13 #include <arpa/inet.h>
15 #include "netlink_kobject_uevent.h"
17 static const char *errstr;
20 sys_send(const int fd, const void *const buf, const size_t len)
22 const ssize_t rc = sendto(fd, buf, len, MSG_DONTWAIT, NULL, 0);
23 errstr = sprintrc(rc);
28 test_nlmsg_type_udev(const int fd)
30 static const char extra[] = "12345678";
31 struct udev_monitor_netlink_header uh = {
33 .magic = htonl(0xfeedcafe),
34 .header_size = sizeof(uh),
36 .properties_len = 299,
37 .filter_subsystem_hash = htonl(0xc370b302),
38 .filter_devtype_hash = htonl(0x10800000),
39 .filter_tag_bloom_hi = htonl(0x2000400),
40 .filter_tag_bloom_lo = htonl(0x10800000),
42 const unsigned int extra_len = LENGTH_OF(extra);
43 const unsigned int uh_len = sizeof(uh);
45 char *const buf = tail_alloc(uh_len + extra_len);
46 memcpy(buf + extra_len, &uh, uh_len);
48 sys_send(fd, buf + extra_len, uh_len);
49 printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
50 ", header_size=%u, properties_off=%u, properties_len=%u"
51 ", filter_subsystem_hash=htonl(%#x)"
52 ", filter_devtype_hash=htonl(%#x)"
53 ", filter_tag_bloom_hi=htonl(%#x)"
54 ", filter_tag_bloom_lo=htonl(%#x)}}, %u, MSG_DONTWAIT, NULL, "
57 ntohl(uh.magic), uh.header_size, uh.properties_off,
58 uh.properties_len, ntohl(uh.filter_subsystem_hash),
59 ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
60 ntohl(uh.filter_tag_bloom_lo), uh_len, errstr);
62 memcpy(buf, &uh, uh_len);
63 memcpy(buf + uh_len, extra, extra_len);
64 sys_send(fd, buf, uh_len + extra_len);
65 printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
66 ", header_size=%u, properties_off=%u, properties_len=%u"
67 ", filter_subsystem_hash=htonl(%#x)"
68 ", filter_devtype_hash=htonl(%#x)"
69 ", filter_tag_bloom_hi=htonl(%#x)"
70 ", filter_tag_bloom_lo=htonl(%#x)}, "
72 ntohl(uh.magic), uh.header_size, uh.properties_off,
73 uh.properties_len, ntohl(uh.filter_subsystem_hash),
74 ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
75 ntohl(uh.filter_tag_bloom_lo));
76 print_quoted_memory(buf + uh_len, extra_len);
77 printf("}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
78 uh_len + extra_len, errstr);
80 memcpy(buf + extra_len + 1, &uh, uh_len - 1);
81 sys_send(fd, buf + extra_len + 1, uh_len);
82 printf("sendto(%d, ", fd);
83 print_quoted_memory(&uh, MIN(uh_len - 1, DEFAULT_STRLEN));
84 printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
85 (uh_len - 1 > DEFAULT_STRLEN ? "..." : ""),
90 test_nlmsg_type_kernel(const int fd)
92 struct udev_monitor_netlink_header uh = {
94 .magic = htonl(0xfeedcafe),
95 .header_size = sizeof(uh),
97 .properties_len = 299,
98 .filter_subsystem_hash = htonl(0xfffffff),
99 .filter_devtype_hash = htonl(0x10000000),
100 .filter_tag_bloom_hi = htonl(0x2000400),
102 const unsigned int uh_len = sizeof(uh);
104 TAIL_ALLOC_OBJECT_CONST_PTR(struct udev_monitor_netlink_header, p);
105 memcpy(p, &uh, uh_len);
107 sys_send(fd, p, uh_len);
108 printf("sendto(%d, ", fd);
109 print_quoted_memory(&uh, MIN(uh_len, DEFAULT_STRLEN));
110 printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
111 (uh_len > DEFAULT_STRLEN ? "..." : ""),
118 skip_if_unavailable("/proc/self/fd/");
120 int fd = create_nl_socket(NETLINK_KOBJECT_UEVENT);
122 test_nlmsg_type_udev(fd);
123 test_nlmsg_type_kernel(fd);
124 /* test using data that looks like a zero-length C string */
125 char *const buf = tail_alloc(DEFAULT_STRLEN + 1);
127 fill_memory_ex(buf + 1, DEFAULT_STRLEN, 0, DEFAULT_STRLEN);
129 sys_send(fd, buf + 1, DEFAULT_STRLEN);
130 printf("sendto(%d, ", fd);
131 print_quoted_memory(buf + 1, DEFAULT_STRLEN);
132 printf(", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
133 DEFAULT_STRLEN, errstr);
135 sys_send(fd, buf, DEFAULT_STRLEN + 1);
136 printf("sendto(%d, ", fd);
137 print_quoted_memory(buf, DEFAULT_STRLEN);
138 printf("..., %u, MSG_DONTWAIT, NULL, 0) = %s\n",
139 DEFAULT_STRLEN + 1, errstr);
141 puts("+++ exited with 0 +++");