tst-cmsg_cloexec.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Smoke test for MSG_CMSG_CLOEXEC.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <support/xunistd.h>
  16. #include <support/check.h>
  17. #include <sys/socket.h>
  18. #include <sys/un.h>
  19. #include <string.h>
  20. #include <fcntl.h>
  21. static void
  22. send_fd (int sockfd, int fd)
  23. {
  24. char data[] = "hello";
  25. struct iovec iov = { .iov_base = data, .iov_len = sizeof (data) };
  26. union
  27. {
  28. struct cmsghdr header;
  29. char bytes[CMSG_SPACE (sizeof (fd))];
  30. } cmsg_storage;
  31. struct msghdr msg =
  32. {
  33. .msg_iov = &iov,
  34. .msg_iovlen = 1,
  35. .msg_control = cmsg_storage.bytes,
  36. .msg_controllen = sizeof (cmsg_storage)
  37. };
  38. memset (&cmsg_storage, 0, sizeof (cmsg_storage));
  39. struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
  40. cmsg->cmsg_level = SOL_SOCKET;
  41. cmsg->cmsg_type = SCM_RIGHTS;
  42. cmsg->cmsg_len = CMSG_LEN (sizeof (fd));
  43. memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd));
  44. ssize_t nsent = sendmsg (sockfd, &msg, 0);
  45. if (nsent < 0)
  46. FAIL_EXIT1 ("sendmsg (%d): %m", sockfd);
  47. TEST_COMPARE (nsent, sizeof (data));
  48. }
  49. static int
  50. recv_fd (int sockfd, int flags)
  51. {
  52. char buffer[100];
  53. struct iovec iov = { .iov_base = buffer, .iov_len = sizeof (buffer) };
  54. union
  55. {
  56. struct cmsghdr header;
  57. char bytes[100];
  58. } cmsg_storage;
  59. struct msghdr msg =
  60. {
  61. .msg_iov = &iov,
  62. .msg_iovlen = 1,
  63. .msg_control = cmsg_storage.bytes,
  64. .msg_controllen = sizeof (cmsg_storage)
  65. };
  66. ssize_t nrecv = recvmsg (sockfd, &msg, flags);
  67. if (nrecv < 0)
  68. FAIL_EXIT1 ("recvmsg (%d): %m", sockfd);
  69. TEST_COMPARE (msg.msg_controllen, CMSG_SPACE (sizeof (int)));
  70. struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg);
  71. TEST_COMPARE (cmsg->cmsg_level, SOL_SOCKET);
  72. TEST_COMPARE (cmsg->cmsg_type, SCM_RIGHTS);
  73. TEST_COMPARE (cmsg->cmsg_len, CMSG_LEN (sizeof (int)));
  74. int fd;
  75. memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
  76. return fd;
  77. }
  78. static int
  79. do_test (void)
  80. {
  81. int sockfd[2];
  82. int newfd;
  83. int flags;
  84. int rc = socketpair (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockfd);
  85. if (rc < 0)
  86. FAIL_EXIT1 ("socketpair: %m");
  87. send_fd (sockfd[1], STDIN_FILENO);
  88. newfd = recv_fd (sockfd[0], 0);
  89. TEST_VERIFY_EXIT (newfd > 0);
  90. flags = fcntl (newfd, F_GETFD, 0);
  91. TEST_VERIFY_EXIT (flags != -1);
  92. TEST_VERIFY (!(flags & FD_CLOEXEC));
  93. xclose (newfd);
  94. send_fd (sockfd[1], STDIN_FILENO);
  95. newfd = recv_fd (sockfd[0], MSG_CMSG_CLOEXEC);
  96. TEST_VERIFY_EXIT (newfd > 0);
  97. flags = fcntl (newfd, F_GETFD, 0);
  98. TEST_VERIFY_EXIT (flags != -1);
  99. TEST_VERIFY (flags & FD_CLOEXEC);
  100. xclose (newfd);
  101. xclose (sockfd[0]);
  102. xclose (sockfd[1]);
  103. return 0;
  104. }
  105. #include <support/test-driver.c>