1 /*
2  * Copyright (c) 2017 The strace developers.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  *    derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 /* This test case is based on netlink_selinux.c */
28 
29 #include "tests.h"
30 
31 #ifdef HAVE_LINUX_GENETLINK_H
32 
33 # include <stdio.h>
34 # include <string.h>
35 # include <unistd.h>
36 # include <sys/socket.h>
37 # include "netlink.h"
38 # include <linux/genetlink.h>
39 
40 static void
41 test_nlmsg_type(const int fd)
42 {
43 	/*
44 	 * Though GENL_ID_CTRL number is statically fixed in this test case,
45 	 * strace does not have a builtin knowledge that the corresponding
46 	 * string is "nlctrl".
47 	 */
48 	long rc;
49 	struct {
50 		const struct nlmsghdr nlh;
51 		struct genlmsghdr gnlh;
52 	} req = {
53 		.nlh = {
54 			.nlmsg_len = sizeof(req),
55 			.nlmsg_type = GENL_ID_CTRL,
56 			.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
57 		},
58 		.gnlh = {
59 			.cmd = CTRL_CMD_GETFAMILY
60 		}
61 	};
62 
63 	rc = sendto(fd, &req, sizeof(req), MSG_DONTWAIT, NULL, 0);
64 	printf("sendto(%d, {{len=%u, type=nlctrl"
65 	       ", flags=NLM_F_REQUEST|0x300, seq=0, pid=0}"
66 	       ", \"\\x03\\x00\\x00\\x00\"}, %u"
67 	       ", MSG_DONTWAIT, NULL, 0) = %s\n",
68 	       fd, req.nlh.nlmsg_len,
69 	       (unsigned int) sizeof(req), sprintrc(rc));
70 }
71 
72 int main(void)
73 {
74 	skip_if_unavailable("/proc/self/fd/");
75 
76 	int fd = create_nl_socket(NETLINK_GENERIC);
77 
78 	test_nlmsg_type(fd);
79 
80 	printf("+++ exited with 0 +++\n");
81 
82 	return 0;
83 }
84 
85 #else
86 
87 SKIP_MAIN_UNDEFINED("HAVE_LINUX_GENETLINK_H")
88 
89 #endif
90