netlink-dumps.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <sys/syscall.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include <linux/genetlink.h>
  12. #include <linux/netlink.h>
  13. #include <linux/mqueue.h>
  14. #include "../kselftest_harness.h"
  15. static const struct {
  16. struct nlmsghdr nlhdr;
  17. struct genlmsghdr genlhdr;
  18. struct nlattr ahdr;
  19. __u16 val;
  20. __u16 pad;
  21. } dump_policies = {
  22. .nlhdr = {
  23. .nlmsg_len = sizeof(dump_policies),
  24. .nlmsg_type = GENL_ID_CTRL,
  25. .nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_DUMP,
  26. .nlmsg_seq = 1,
  27. },
  28. .genlhdr = {
  29. .cmd = CTRL_CMD_GETPOLICY,
  30. .version = 2,
  31. },
  32. .ahdr = {
  33. .nla_len = 6,
  34. .nla_type = CTRL_ATTR_FAMILY_ID,
  35. },
  36. .val = GENL_ID_CTRL,
  37. .pad = 0,
  38. };
  39. // Sanity check for the test itself, make sure the dump doesn't fit in one msg
  40. TEST(test_sanity)
  41. {
  42. int netlink_sock;
  43. char buf[8192];
  44. ssize_t n;
  45. netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  46. ASSERT_GE(netlink_sock, 0);
  47. n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
  48. ASSERT_EQ(n, sizeof(dump_policies));
  49. n = recv(netlink_sock, buf, sizeof(buf), MSG_DONTWAIT);
  50. ASSERT_GE(n, sizeof(struct nlmsghdr));
  51. n = recv(netlink_sock, buf, sizeof(buf), MSG_DONTWAIT);
  52. ASSERT_GE(n, sizeof(struct nlmsghdr));
  53. close(netlink_sock);
  54. }
  55. TEST(close_in_progress)
  56. {
  57. int netlink_sock;
  58. ssize_t n;
  59. netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  60. ASSERT_GE(netlink_sock, 0);
  61. n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
  62. ASSERT_EQ(n, sizeof(dump_policies));
  63. close(netlink_sock);
  64. }
  65. TEST(close_with_ref)
  66. {
  67. char cookie[NOTIFY_COOKIE_LEN] = {};
  68. int netlink_sock, mq_fd;
  69. struct sigevent sigev;
  70. ssize_t n;
  71. netlink_sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  72. ASSERT_GE(netlink_sock, 0);
  73. n = send(netlink_sock, &dump_policies, sizeof(dump_policies), 0);
  74. ASSERT_EQ(n, sizeof(dump_policies));
  75. mq_fd = syscall(__NR_mq_open, "sed", O_CREAT | O_WRONLY, 0600, 0);
  76. ASSERT_GE(mq_fd, 0);
  77. memset(&sigev, 0, sizeof(sigev));
  78. sigev.sigev_notify = SIGEV_THREAD;
  79. sigev.sigev_value.sival_ptr = cookie;
  80. sigev.sigev_signo = netlink_sock;
  81. syscall(__NR_mq_notify, mq_fd, &sigev);
  82. close(netlink_sock);
  83. // give mqueue time to fire
  84. usleep(100 * 1000);
  85. }
  86. TEST_HARNESS_MAIN