(root)/
strace-6.5/
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@strace.io>
       5   * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
       6   * Copyright (c) 2016-2020 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  #ifndef PRINT_SOCK
      25  # define PRINT_SOCK 2
      26  #endif
      27  
      28  #if PRINT_SOCK == 2
      29  # define FMT_UNBOUND "<NETLINK:[%lu]>"
      30  # define FMT_BOUND   "<NETLINK:[SOCK_DIAG:%u]>"
      31  # define ARG_UNBOUND inode
      32  # define ARG_BOUND   addr.nl_pid
      33  #elif PRINT_SOCK == 1
      34  # define FMT_UNBOUND "<socket:[%lu]>"
      35  # define FMT_BOUND   "<socket:[%lu]>"
      36  # define ARG_UNBOUND inode
      37  # define ARG_BOUND   inode
      38  #else
      39  # define FMT_UNBOUND "%s"
      40  # define FMT_BOUND   "%s"
      41  # define ARG_UNBOUND ""
      42  # define ARG_BOUND   ""
      43  #endif
      44  
      45  int
      46  main(void)
      47  {
      48  	skip_if_unavailable("/proc/self/fd/");
      49  
      50  	struct sockaddr_nl addr = {
      51  		.nl_family = AF_NETLINK,
      52  		.nl_pid = getpid()
      53  	};
      54  	struct sockaddr *const sa = tail_memdup(&addr, sizeof(addr));
      55  	TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
      56  	*len = sizeof(addr);
      57  
      58  	const int fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG);
      59  	if (fd < 0)
      60  		perror_msg_and_skip("socket");
      61  #if PRINT_SOCK
      62  	const unsigned long inode = inode_of_sockfd(fd);
      63  #endif
      64  	printf("socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) = "
      65  	       "%d" FMT_UNBOUND "\n", fd, ARG_UNBOUND);
      66  
      67  	if (bind(fd, sa, *len))
      68  		perror_msg_and_skip("bind");
      69  	printf("bind(%d" FMT_UNBOUND ", {sa_family=AF_NETLINK"
      70  	       ", nl_pid=%d, nl_groups=00000000}, %u) = 0\n",
      71  	       fd, ARG_UNBOUND, addr.nl_pid, (unsigned) *len);
      72  
      73  	if (getsockname(fd, sa, len))
      74  		perror_msg_and_fail("getsockname");
      75  	printf("getsockname(%d" FMT_BOUND ", {sa_family=AF_NETLINK"
      76  	       ", nl_pid=%d, nl_groups=00000000}, [%u]) = 0\n",
      77  	       fd, ARG_BOUND, addr.nl_pid, (unsigned) *len);
      78  
      79  	if (close(fd))
      80  		perror_msg_and_fail("close");
      81  	printf("close(%d" FMT_BOUND ") = 0\n", fd, ARG_BOUND);
      82  
      83  	puts("+++ exited with 0 +++");
      84  	return 0;
      85  }