1 /*
2 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@strace.io>
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 <sys/socket.h>
11 #include "netlink.h"
12
13 int
14 create_nl_socket_ext(const int proto, const char *const name)
15 {
16 const int fd = socket(AF_NETLINK, SOCK_RAW, proto);
17 if (fd < 0)
18 perror_msg_and_skip("socket AF_NETLINK %s", name);
19
20 const struct sockaddr_nl addr = { .nl_family = AF_NETLINK };
21 socklen_t len = sizeof(addr);
22
23 if (bind(fd, (const struct sockaddr *) &addr, len))
24 perror_msg_and_skip("bind AF_NETLINK %s", name);
25
26 /* one more operation on this socket to win the race */
27 int listening;
28 len = sizeof(listening);
29 if (getsockopt(fd, SOL_SOCKET, SO_ACCEPTCONN, &listening, &len))
30 perror_msg_and_fail("getsockopt");
31
32 return fd;
33 }