]> granicus.if.org Git - strace/blob - tests/nlattr_mdba_router_port.c
strace: terminate itself if interrupted by a signal
[strace] / tests / nlattr_mdba_router_port.c
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 #include "tests.h"
10
11 #ifdef HAVE_STRUCT_BR_PORT_MSG
12
13 # include <stdio.h>
14 # include "test_nlattr.h"
15 # include <linux/if_bridge.h>
16 # include <linux/rtnetlink.h>
17
18 # ifndef MDBA_ROUTER
19 #  define MDBA_ROUTER 2
20 # endif
21 # ifndef MDBA_ROUTER_PORT
22 #  define MDBA_ROUTER_PORT 1
23 # endif
24 # ifndef MDBA_ROUTER_PATTR_TYPE
25 #  define MDBA_ROUTER_PATTR_TYPE 2
26 # endif
27 # ifndef MDB_RTR_TYPE_DISABLED
28 #  define MDB_RTR_TYPE_DISABLED 0
29 # endif
30
31 const unsigned int hdrlen = sizeof(struct br_port_msg);
32
33 static void
34 init_br_port_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
35 {
36         SET_STRUCT(struct nlmsghdr, nlh,
37                 .nlmsg_len = msg_len,
38                 .nlmsg_type = RTM_GETMDB,
39                 .nlmsg_flags = NLM_F_DUMP
40         );
41
42         struct br_port_msg *const msg = NLMSG_DATA(nlh);
43         SET_STRUCT(struct br_port_msg, msg,
44                 .family = AF_UNIX,
45                 .ifindex = ifindex_lo()
46         );
47
48         struct nlattr *nla = NLMSG_ATTR(nlh, sizeof(*msg));
49         SET_STRUCT(struct nlattr, nla,
50                 .nla_len = msg_len - NLMSG_SPACE(hdrlen),
51                 .nla_type = MDBA_ROUTER
52         );
53 }
54
55 static void
56 print_br_port_msg(const unsigned int msg_len)
57 {
58         printf("{len=%u, type=RTM_GETMDB, flags=NLM_F_DUMP"
59                ", seq=0, pid=0}, {family=AF_UNIX"
60                ", ifindex=" IFINDEX_LO_STR "}"
61                ", {{nla_len=%u, nla_type=MDBA_ROUTER}",
62                msg_len, msg_len - NLMSG_SPACE(hdrlen));
63 }
64
65 int
66 main(void)
67 {
68         skip_if_unavailable("/proc/self/fd/");
69
70         const uint32_t ifindex = ifindex_lo();
71         const uint8_t type = MDB_RTR_TYPE_DISABLED;
72         static const struct nlattr nla = {
73                 .nla_len = NLA_HDRLEN + sizeof(type),
74                 .nla_type = MDBA_ROUTER_PATTR_TYPE
75         };
76         char buf[NLMSG_ALIGN(ifindex) + NLA_HDRLEN + sizeof(type)];
77
78         const int fd = create_nl_socket(NETLINK_ROUTE);
79
80         void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
81                                    NLA_HDRLEN + sizeof(buf));
82
83         static char pattern[4096];
84         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
85
86         TEST_NESTED_NLATTR_OBJECT(fd, nlh0, hdrlen,
87                                   init_br_port_msg, print_br_port_msg,
88                                   MDBA_ROUTER_PORT, pattern, ifindex,
89                                   printf(IFINDEX_LO_STR));
90
91         memcpy(buf, &ifindex, sizeof(ifindex));
92         memcpy(buf + NLMSG_ALIGN(ifindex), &nla, sizeof(nla));
93         memcpy(buf + NLMSG_ALIGN(ifindex) + NLA_HDRLEN, &type, sizeof(type));
94         TEST_NLATTR(fd, nlh0 - NLA_HDRLEN, hdrlen + NLA_HDRLEN,
95                     init_br_port_msg, print_br_port_msg,
96                     MDBA_ROUTER_PORT, sizeof(buf), buf, sizeof(buf),
97                     printf(IFINDEX_LO_STR
98                            ", {{nla_len=%u, nla_type=MDBA_ROUTER_PATTR_TYPE}"
99                            ", MDB_RTR_TYPE_DISABLED}}",
100                            nla.nla_len));
101
102         puts("+++ exited with 0 +++");
103         return 0;
104 }
105
106 #else
107
108 SKIP_MAIN_UNDEFINED("HAVE_STRUCT_BR_PORT_MSG")
109
110 #endif