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