1*cf84ac9aSAndroid Build Coastguard Worker /*
2*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2017 JingPiao Chen <[email protected]>
3*cf84ac9aSAndroid Build Coastguard Worker * Copyright (c) 2017-2018 The strace developers.
4*cf84ac9aSAndroid Build Coastguard Worker * All rights reserved.
5*cf84ac9aSAndroid Build Coastguard Worker *
6*cf84ac9aSAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
7*cf84ac9aSAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions
8*cf84ac9aSAndroid Build Coastguard Worker * are met:
9*cf84ac9aSAndroid Build Coastguard Worker * 1. Redistributions of source code must retain the above copyright
10*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
11*cf84ac9aSAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
12*cf84ac9aSAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
13*cf84ac9aSAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
14*cf84ac9aSAndroid Build Coastguard Worker * 3. The name of the author may not be used to endorse or promote products
15*cf84ac9aSAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
16*cf84ac9aSAndroid Build Coastguard Worker *
17*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18*cf84ac9aSAndroid Build Coastguard Worker * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19*cf84ac9aSAndroid Build Coastguard Worker * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*cf84ac9aSAndroid Build Coastguard Worker * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21*cf84ac9aSAndroid Build Coastguard Worker * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22*cf84ac9aSAndroid Build Coastguard Worker * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23*cf84ac9aSAndroid Build Coastguard Worker * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24*cf84ac9aSAndroid Build Coastguard Worker * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25*cf84ac9aSAndroid Build Coastguard Worker * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26*cf84ac9aSAndroid Build Coastguard Worker * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*cf84ac9aSAndroid Build Coastguard Worker */
28*cf84ac9aSAndroid Build Coastguard Worker
29*cf84ac9aSAndroid Build Coastguard Worker #include "tests.h"
30*cf84ac9aSAndroid Build Coastguard Worker #include <string.h>
31*cf84ac9aSAndroid Build Coastguard Worker #include <stdio.h>
32*cf84ac9aSAndroid Build Coastguard Worker #include <sys/socket.h>
33*cf84ac9aSAndroid Build Coastguard Worker #include <arpa/inet.h>
34*cf84ac9aSAndroid Build Coastguard Worker #include "netlink.h"
35*cf84ac9aSAndroid Build Coastguard Worker #include "netlink_kobject_uevent.h"
36*cf84ac9aSAndroid Build Coastguard Worker
37*cf84ac9aSAndroid Build Coastguard Worker static const char *errstr;
38*cf84ac9aSAndroid Build Coastguard Worker
39*cf84ac9aSAndroid Build Coastguard Worker static ssize_t
sys_send(const int fd,const void * const buf,const size_t len)40*cf84ac9aSAndroid Build Coastguard Worker sys_send(const int fd, const void *const buf, const size_t len)
41*cf84ac9aSAndroid Build Coastguard Worker {
42*cf84ac9aSAndroid Build Coastguard Worker const ssize_t rc = sendto(fd, buf, len, MSG_DONTWAIT, NULL, 0);
43*cf84ac9aSAndroid Build Coastguard Worker errstr = sprintrc(rc);
44*cf84ac9aSAndroid Build Coastguard Worker return rc;
45*cf84ac9aSAndroid Build Coastguard Worker }
46*cf84ac9aSAndroid Build Coastguard Worker
47*cf84ac9aSAndroid Build Coastguard Worker static void
test_nlmsg_type_udev(const int fd)48*cf84ac9aSAndroid Build Coastguard Worker test_nlmsg_type_udev(const int fd)
49*cf84ac9aSAndroid Build Coastguard Worker {
50*cf84ac9aSAndroid Build Coastguard Worker static const char extra[] = "12345678";
51*cf84ac9aSAndroid Build Coastguard Worker struct udev_monitor_netlink_header uh = {
52*cf84ac9aSAndroid Build Coastguard Worker .prefix = "libudev",
53*cf84ac9aSAndroid Build Coastguard Worker .magic = htonl(0xfeedcafe),
54*cf84ac9aSAndroid Build Coastguard Worker .header_size = sizeof(uh),
55*cf84ac9aSAndroid Build Coastguard Worker .properties_off = 40,
56*cf84ac9aSAndroid Build Coastguard Worker .properties_len = 299,
57*cf84ac9aSAndroid Build Coastguard Worker .filter_subsystem_hash = htonl(0xc370b302),
58*cf84ac9aSAndroid Build Coastguard Worker .filter_devtype_hash = htonl(0x10800000),
59*cf84ac9aSAndroid Build Coastguard Worker .filter_tag_bloom_hi = htonl(0x2000400),
60*cf84ac9aSAndroid Build Coastguard Worker .filter_tag_bloom_lo = htonl(0x10800000),
61*cf84ac9aSAndroid Build Coastguard Worker };
62*cf84ac9aSAndroid Build Coastguard Worker const unsigned int extra_len = LENGTH_OF(extra);
63*cf84ac9aSAndroid Build Coastguard Worker const unsigned int uh_len = sizeof(uh);
64*cf84ac9aSAndroid Build Coastguard Worker
65*cf84ac9aSAndroid Build Coastguard Worker char *const buf = tail_alloc(uh_len + extra_len);
66*cf84ac9aSAndroid Build Coastguard Worker memcpy(buf + extra_len, &uh, uh_len);
67*cf84ac9aSAndroid Build Coastguard Worker
68*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, buf + extra_len, uh_len);
69*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
70*cf84ac9aSAndroid Build Coastguard Worker ", header_size=%u, properties_off=%u, properties_len=%u"
71*cf84ac9aSAndroid Build Coastguard Worker ", filter_subsystem_hash=htonl(%#x)"
72*cf84ac9aSAndroid Build Coastguard Worker ", filter_devtype_hash=htonl(%#x)"
73*cf84ac9aSAndroid Build Coastguard Worker ", filter_tag_bloom_hi=htonl(%#x)"
74*cf84ac9aSAndroid Build Coastguard Worker ", filter_tag_bloom_lo=htonl(%#x)}}, %u, MSG_DONTWAIT, NULL, "
75*cf84ac9aSAndroid Build Coastguard Worker "0) = %s\n"
76*cf84ac9aSAndroid Build Coastguard Worker , fd, uh.prefix,
77*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.magic), uh.header_size, uh.properties_off,
78*cf84ac9aSAndroid Build Coastguard Worker uh.properties_len, ntohl(uh.filter_subsystem_hash),
79*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
80*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.filter_tag_bloom_lo), uh_len, errstr);
81*cf84ac9aSAndroid Build Coastguard Worker
82*cf84ac9aSAndroid Build Coastguard Worker memcpy(buf, &uh, uh_len);
83*cf84ac9aSAndroid Build Coastguard Worker memcpy(buf + uh_len, extra, extra_len);
84*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, buf, uh_len + extra_len);
85*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, {{prefix=\"%s\", magic=htonl(%#x)"
86*cf84ac9aSAndroid Build Coastguard Worker ", header_size=%u, properties_off=%u, properties_len=%u"
87*cf84ac9aSAndroid Build Coastguard Worker ", filter_subsystem_hash=htonl(%#x)"
88*cf84ac9aSAndroid Build Coastguard Worker ", filter_devtype_hash=htonl(%#x)"
89*cf84ac9aSAndroid Build Coastguard Worker ", filter_tag_bloom_hi=htonl(%#x)"
90*cf84ac9aSAndroid Build Coastguard Worker ", filter_tag_bloom_lo=htonl(%#x)}, "
91*cf84ac9aSAndroid Build Coastguard Worker , fd, uh.prefix,
92*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.magic), uh.header_size, uh.properties_off,
93*cf84ac9aSAndroid Build Coastguard Worker uh.properties_len, ntohl(uh.filter_subsystem_hash),
94*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.filter_devtype_hash), ntohl(uh.filter_tag_bloom_hi),
95*cf84ac9aSAndroid Build Coastguard Worker ntohl(uh.filter_tag_bloom_lo));
96*cf84ac9aSAndroid Build Coastguard Worker print_quoted_memory(buf + uh_len, extra_len);
97*cf84ac9aSAndroid Build Coastguard Worker printf("}, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
98*cf84ac9aSAndroid Build Coastguard Worker uh_len + extra_len, errstr);
99*cf84ac9aSAndroid Build Coastguard Worker
100*cf84ac9aSAndroid Build Coastguard Worker memcpy(buf + extra_len + 1, &uh, uh_len - 1);
101*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, buf + extra_len + 1, uh_len);
102*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, ", fd);
103*cf84ac9aSAndroid Build Coastguard Worker print_quoted_memory(&uh, MIN(uh_len - 1, DEFAULT_STRLEN));
104*cf84ac9aSAndroid Build Coastguard Worker printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
105*cf84ac9aSAndroid Build Coastguard Worker (uh_len - 1 > DEFAULT_STRLEN ? "..." : ""),
106*cf84ac9aSAndroid Build Coastguard Worker uh_len, errstr);
107*cf84ac9aSAndroid Build Coastguard Worker }
108*cf84ac9aSAndroid Build Coastguard Worker
109*cf84ac9aSAndroid Build Coastguard Worker static void
test_nlmsg_type_kernel(const int fd)110*cf84ac9aSAndroid Build Coastguard Worker test_nlmsg_type_kernel(const int fd)
111*cf84ac9aSAndroid Build Coastguard Worker {
112*cf84ac9aSAndroid Build Coastguard Worker struct udev_monitor_netlink_header uh = {
113*cf84ac9aSAndroid Build Coastguard Worker .prefix = "change@",
114*cf84ac9aSAndroid Build Coastguard Worker .magic = htonl(0xfeedcafe),
115*cf84ac9aSAndroid Build Coastguard Worker .header_size = sizeof(uh),
116*cf84ac9aSAndroid Build Coastguard Worker .properties_off = 10,
117*cf84ac9aSAndroid Build Coastguard Worker .properties_len = 299,
118*cf84ac9aSAndroid Build Coastguard Worker .filter_subsystem_hash = htonl(0xfffffff),
119*cf84ac9aSAndroid Build Coastguard Worker .filter_devtype_hash = htonl(0x10000000),
120*cf84ac9aSAndroid Build Coastguard Worker .filter_tag_bloom_hi = htonl(0x2000400),
121*cf84ac9aSAndroid Build Coastguard Worker };
122*cf84ac9aSAndroid Build Coastguard Worker const unsigned int uh_len = sizeof(uh);
123*cf84ac9aSAndroid Build Coastguard Worker
124*cf84ac9aSAndroid Build Coastguard Worker TAIL_ALLOC_OBJECT_CONST_PTR(struct udev_monitor_netlink_header, p);
125*cf84ac9aSAndroid Build Coastguard Worker memcpy(p, &uh, uh_len);
126*cf84ac9aSAndroid Build Coastguard Worker
127*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, p, uh_len);
128*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, ", fd);
129*cf84ac9aSAndroid Build Coastguard Worker print_quoted_memory(&uh, MIN(uh_len, DEFAULT_STRLEN));
130*cf84ac9aSAndroid Build Coastguard Worker printf("%s, %u, MSG_DONTWAIT, NULL, 0) = %s\n",
131*cf84ac9aSAndroid Build Coastguard Worker (uh_len > DEFAULT_STRLEN ? "..." : ""),
132*cf84ac9aSAndroid Build Coastguard Worker uh_len, errstr);
133*cf84ac9aSAndroid Build Coastguard Worker }
134*cf84ac9aSAndroid Build Coastguard Worker
135*cf84ac9aSAndroid Build Coastguard Worker int
main(void)136*cf84ac9aSAndroid Build Coastguard Worker main(void)
137*cf84ac9aSAndroid Build Coastguard Worker {
138*cf84ac9aSAndroid Build Coastguard Worker skip_if_unavailable("/proc/self/fd/");
139*cf84ac9aSAndroid Build Coastguard Worker
140*cf84ac9aSAndroid Build Coastguard Worker int fd = create_nl_socket(NETLINK_KOBJECT_UEVENT);
141*cf84ac9aSAndroid Build Coastguard Worker
142*cf84ac9aSAndroid Build Coastguard Worker test_nlmsg_type_udev(fd);
143*cf84ac9aSAndroid Build Coastguard Worker test_nlmsg_type_kernel(fd);
144*cf84ac9aSAndroid Build Coastguard Worker /* test using data that looks like a zero-length C string */
145*cf84ac9aSAndroid Build Coastguard Worker char *const buf = tail_alloc(DEFAULT_STRLEN + 1);
146*cf84ac9aSAndroid Build Coastguard Worker buf[0] = '=';
147*cf84ac9aSAndroid Build Coastguard Worker fill_memory_ex(buf + 1, DEFAULT_STRLEN, 0, DEFAULT_STRLEN);
148*cf84ac9aSAndroid Build Coastguard Worker
149*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, buf + 1, DEFAULT_STRLEN);
150*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, ", fd);
151*cf84ac9aSAndroid Build Coastguard Worker print_quoted_memory(buf + 1, DEFAULT_STRLEN);
152*cf84ac9aSAndroid Build Coastguard Worker printf(", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
153*cf84ac9aSAndroid Build Coastguard Worker DEFAULT_STRLEN, errstr);
154*cf84ac9aSAndroid Build Coastguard Worker
155*cf84ac9aSAndroid Build Coastguard Worker sys_send(fd, buf, DEFAULT_STRLEN + 1);
156*cf84ac9aSAndroid Build Coastguard Worker printf("sendto(%d, ", fd);
157*cf84ac9aSAndroid Build Coastguard Worker print_quoted_memory(buf, DEFAULT_STRLEN);
158*cf84ac9aSAndroid Build Coastguard Worker printf("..., %u, MSG_DONTWAIT, NULL, 0) = %s\n",
159*cf84ac9aSAndroid Build Coastguard Worker DEFAULT_STRLEN + 1, errstr);
160*cf84ac9aSAndroid Build Coastguard Worker
161*cf84ac9aSAndroid Build Coastguard Worker puts("+++ exited with 0 +++");
162*cf84ac9aSAndroid Build Coastguard Worker return 0;
163*cf84ac9aSAndroid Build Coastguard Worker }
164