]> granicus.if.org Git - strace/blob - tests/net-yy-netlink.c
f1395bdd618b2f72b995247e23420550ffeaeacd
[strace] / tests / net-yy-netlink.c
1 /*
2  * This file is part of net-yy-netlink strace test.
3  *
4  * Copyright (c) 2013-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/socket.h>
19 #include "netlink.h"
20 #include <linux/sock_diag.h>
21 #include <linux/netlink_diag.h>
22
23 int
24 main(void)
25 {
26         skip_if_unavailable("/proc/self/fd/");
27
28         struct sockaddr_nl addr = {
29                 .nl_family = AF_NETLINK,
30                 .nl_pid = getpid()
31         };
32         struct sockaddr *const sa = tail_memdup(&addr, sizeof(addr));
33         TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
34         *len = sizeof(addr);
35
36         const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
37         if (fd < 0)
38                 perror_msg_and_skip("socket");
39         const unsigned long inode = inode_of_sockfd(fd);
40         printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = "
41                "%d<NETLINK:[%lu]>\n", fd, inode);
42
43         if (bind(fd, sa, *len))
44                 perror_msg_and_skip("bind");
45         printf("bind(%d<NETLINK:[%lu]>, {sa_family=AF_NETLINK"
46                ", nl_pid=%u, nl_groups=00000000}, %u) = 0\n",
47                fd, inode, addr.nl_pid, (unsigned) *len);
48
49         if (getsockname(fd, sa, len))
50                 perror_msg_and_fail("getsockname");
51         printf("getsockname(%d<NETLINK:[SOCK_DIAG:%u]>, {sa_family=AF_NETLINK"
52                ", nl_pid=%u, nl_groups=00000000}, [%u]) = 0\n",
53                fd, addr.nl_pid, addr.nl_pid, (unsigned) *len);
54
55         if (close(fd))
56                 perror_msg_and_fail("close");
57         printf("close(%d<NETLINK:[SOCK_DIAG:%u]>) = 0\n", fd, addr.nl_pid);
58
59         puts("+++ exited with 0 +++");
60         return 0;
61 }