]> granicus.if.org Git - strace/blob - tests/nlattr_tcamsg.c
strace: terminate itself if interrupted by a signal
[strace] / tests / nlattr_tcamsg.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 #include <stdio.h>
12 #include "test_nlattr.h"
13 #include <linux/rtnetlink.h>
14
15 static void
16 init_tcamsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
17 {
18         SET_STRUCT(struct nlmsghdr, nlh,
19                 .nlmsg_len = msg_len,
20                 .nlmsg_type = RTM_GETACTION,
21                 .nlmsg_flags = NLM_F_DUMP
22         );
23
24         struct tcamsg *const msg = NLMSG_DATA(nlh);
25         SET_STRUCT(struct tcamsg, msg,
26                 .tca_family = AF_INET
27         );
28 }
29
30 static void
31 print_tcamsg(const unsigned int msg_len)
32 {
33         printf("{len=%u, type=RTM_GETACTION, flags=NLM_F_DUMP"
34                ", seq=0, pid=0}, {tca_family=AF_INET}",
35                msg_len);
36 }
37
38 int
39 main(void)
40 {
41         skip_if_unavailable("/proc/self/fd/");
42
43         const int fd = create_nl_socket(NETLINK_ROUTE);
44         const unsigned int hdrlen = sizeof(struct tcamsg);
45         void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen), NLA_HDRLEN + 4);
46
47         static char pattern[4096];
48         fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
49
50         const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
51         char nla_type_str[256];
52         sprintf(nla_type_str, "%#x /* TCA_ACT_??? */", nla_type);
53         TEST_NLATTR_(fd, nlh0, hdrlen,
54                      init_tcamsg, print_tcamsg,
55                      nla_type, nla_type_str,
56                      4, pattern, 4,
57                      print_quoted_hex(pattern, 4));
58
59         puts("+++ exited with 0 +++");
60         return 0;
61 }