]> granicus.if.org Git - strace/blob - tests/netlink_xfrm.c
strace: terminate itself if interrupted by a signal
[strace] / tests / netlink_xfrm.c
1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  */
7
8 #include "tests.h"
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdint.h>
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include "netlink.h"
15 #include <linux/xfrm.h>
16
17 static void
18 test_nlmsg_type(const int fd)
19 {
20         long rc;
21         struct nlmsghdr nlh = {
22                 .nlmsg_len = sizeof(nlh),
23                 .nlmsg_type = XFRM_MSG_NEWSA,
24                 .nlmsg_flags = NLM_F_REQUEST,
25         };
26
27         rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
28         printf("sendto(%d, {len=%u, type=XFRM_MSG_NEWSA"
29                ", flags=NLM_F_REQUEST, seq=0, pid=0}"
30                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
31                fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
32 }
33
34 static void
35 test_nlmsg_flags(const int fd)
36 {
37         long rc;
38         struct nlmsghdr nlh = {
39                 .nlmsg_len = sizeof(nlh),
40         };
41
42         nlh.nlmsg_type = XFRM_MSG_GETSA;
43         nlh.nlmsg_flags = NLM_F_DUMP;
44         rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
45         printf("sendto(%d, {len=%u, type=XFRM_MSG_GETSA"
46                ", flags=NLM_F_DUMP, seq=0, pid=0}"
47                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
48                fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
49
50         nlh.nlmsg_type = XFRM_MSG_NEWSA;
51         nlh.nlmsg_flags = NLM_F_REPLACE;
52         rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
53         printf("sendto(%d, {len=%u, type=XFRM_MSG_NEWSA"
54                ", flags=NLM_F_REPLACE, seq=0, pid=0}"
55                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
56                fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
57
58         nlh.nlmsg_type = XFRM_MSG_DELSA;
59         nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_NONREC;
60         rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
61         printf("sendto(%d, {len=%u, type=XFRM_MSG_DELSA"
62                ", flags=NLM_F_ECHO|NLM_F_NONREC, seq=0, pid=0}"
63                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
64                fd, nlh.nlmsg_len, (unsigned) sizeof(nlh), sprintrc(rc));
65
66         nlh.nlmsg_type = XFRM_MSG_ALLOCSPI;
67         nlh.nlmsg_flags = NLM_F_ECHO | NLM_F_REPLACE;
68         rc = sendto(fd, &nlh, sizeof(nlh), MSG_DONTWAIT, NULL, 0);
69         printf("sendto(%d, {len=%u, type=XFRM_MSG_ALLOCSPI"
70                ", flags=NLM_F_ECHO|%#x, seq=0, pid=0}"
71                ", %u, MSG_DONTWAIT, NULL, 0) = %s\n",
72                fd, nlh.nlmsg_len, NLM_F_REPLACE,
73                (unsigned) sizeof(nlh), sprintrc(rc));
74 }
75
76 int main(void)
77 {
78         skip_if_unavailable("/proc/self/fd/");
79
80         int fd = create_nl_socket(NETLINK_XFRM);
81
82         test_nlmsg_type(fd);
83         test_nlmsg_flags(fd);
84
85         printf("+++ exited with 0 +++\n");
86
87         return 0;
88 }