1 /*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017-2021 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include <stdio.h>
11 #include <stdint.h>
12 #include "test_nlattr.h"
13
14 #define NLMSGERR_ATTR_COOKIE 3
15
16 static void
17 init_nlmsgerr(struct nlmsghdr *const nlh, const unsigned int msg_len)
18 {
19 SET_STRUCT(struct nlmsghdr, nlh,
20 .nlmsg_len = msg_len,
21 .nlmsg_type = NLMSG_ERROR,
22 .nlmsg_flags = NLM_F_REQUEST | NLM_F_CAPPED
23 );
24
25 struct nlmsgerr *const err = NLMSG_DATA(nlh);
26 SET_STRUCT(struct nlmsgerr, err,
27 .error = -13,
28 .msg = {
29 .nlmsg_len = NLMSG_HDRLEN + 4,
30 .nlmsg_type = NLMSG_NOOP,
31 .nlmsg_flags = NLM_F_REQUEST,
32 }
33 );
34 }
35
36 static void
37 print_nlmsgerr(const unsigned int msg_len)
38 {
39 printf("{nlmsg_len=%u, nlmsg_type=NLMSG_ERROR"
40 ", nlmsg_flags=NLM_F_REQUEST|NLM_F_CAPPED"
41 ", nlmsg_seq=0, nlmsg_pid=0}, [{error=-EACCES"
42 ", msg={nlmsg_len=%u, nlmsg_type=NLMSG_NOOP"
43 ", nlmsg_flags=NLM_F_REQUEST, nlmsg_seq=0, nlmsg_pid=0}}",
44 msg_len, NLMSG_HDRLEN + 4);
45 }
46
47 int
48 main(void)
49 {
50 skip_if_unavailable("/proc/self/fd/");
51
52 static const uint8_t cookie[] = { 0xab, 0xfe };
53
54 const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
55 const unsigned int hdrlen = sizeof(struct nlmsgerr);
56 void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
57 NLA_HDRLEN + sizeof(cookie));
58
59 TEST_NLATTR(fd, nlh0, hdrlen,
60 init_nlmsgerr, print_nlmsgerr,
61 NLMSGERR_ATTR_COOKIE,
62 sizeof(cookie), cookie, sizeof(cookie),
63 printf("[%u, %u]", cookie[0], cookie[1]);
64 printf("]"));
65
66 printf("+++ exited with 0 +++\n");
67 return 0;
68 }