(root)/
strace-6.5/
tests/
nlattr_unix_diag_msg.c
       1  /*
       2   * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
       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  
      11  #include <stdio.h>
      12  #include <string.h>
      13  #include <stdint.h>
      14  #include <sys/sysmacros.h>
      15  #include <netinet/tcp.h>
      16  #include "test_nlattr.h"
      17  #include <linux/sock_diag.h>
      18  #include <linux/unix_diag.h>
      19  
      20  static void
      21  init_unix_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
      22  {
      23  	SET_STRUCT(struct nlmsghdr, nlh,
      24  		.nlmsg_len = msg_len,
      25  		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
      26  		.nlmsg_flags = NLM_F_DUMP
      27  	);
      28  
      29  	struct unix_diag_msg *const msg = NLMSG_DATA(nlh);
      30  	SET_STRUCT(struct unix_diag_msg, msg,
      31  		.udiag_family = AF_UNIX,
      32  		.udiag_type = SOCK_STREAM,
      33  		.udiag_state = TCP_ESTABLISHED
      34  	);
      35  }
      36  
      37  static void
      38  print_unix_diag_msg(const unsigned int msg_len)
      39  {
      40  	printf("{nlmsg_len=%u, nlmsg_type=SOCK_DIAG_BY_FAMILY"
      41  	       ", nlmsg_flags=NLM_F_DUMP, nlmsg_seq=0, nlmsg_pid=0}"
      42  	       ", {udiag_family=AF_UNIX, udiag_type=SOCK_STREAM"
      43  	       ", udiag_state=TCP_ESTABLISHED"
      44  	       ", udiag_ino=0, udiag_cookie=[0, 0]}",
      45  	       msg_len);
      46  }
      47  
      48  static void
      49  print_uint(const unsigned int *p, size_t i)
      50  {
      51  	printf("%u", *p);
      52  }
      53  
      54  int
      55  main(void)
      56  {
      57  	skip_if_unavailable("/proc/self/fd/");
      58  
      59  	static const struct unix_diag_vfs uv = {
      60  		.udiag_vfs_dev = 0xabcddafa,
      61  		.udiag_vfs_ino = 0xbafabcda
      62  	};
      63  	static const struct unix_diag_rqlen rql = {
      64  		.udiag_rqueue = 0xfabdcdad,
      65  		.udiag_wqueue = 0xbacdadcf
      66  	};
      67  	static const uint32_t inode[] = { 0xadbcadbc, 0xfabdcdac };
      68  
      69  	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
      70  	const unsigned int hdrlen = sizeof(struct unix_diag_msg);
      71  	void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
      72  					 NLA_HDRLEN + sizeof(inode));
      73  
      74  	static char pattern[4096];
      75  	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
      76  
      77  	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
      78  			   init_unix_diag_msg, print_unix_diag_msg,
      79  			   UNIX_DIAG_VFS, pattern, uv,
      80  			   printf("{udiag_vfs_dev=makedev(%#x, %#x)",
      81  				  major(uv.udiag_vfs_dev),
      82  				  minor(uv.udiag_vfs_dev));
      83  			   printf(", ");
      84  			   PRINT_FIELD_U(uv, udiag_vfs_ino);
      85  			   printf("}"));
      86  
      87  	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
      88  			   init_unix_diag_msg, print_unix_diag_msg,
      89  			   UNIX_DIAG_RQLEN, pattern, rql,
      90  			   printf("{");
      91  			   PRINT_FIELD_U(rql, udiag_rqueue);
      92  			   printf(", ");
      93  			   PRINT_FIELD_U(rql, udiag_wqueue);
      94  			   printf("}"));
      95  
      96  	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
      97  			  init_unix_diag_msg, print_unix_diag_msg,
      98  			  UNIX_DIAG_ICONS, pattern, inode, print_uint);
      99  
     100  	puts("+++ exited with 0 +++");
     101  	return 0;
     102  }