test-pipe-sendmsg.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "uv.h"
  22. #include "task.h"
  23. #ifndef _WIN32
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <sys/socket.h>
  30. #include <unistd.h>
  31. /* NOTE: size should be divisible by 2 */
  32. static uv_pipe_t incoming[4];
  33. static unsigned int incoming_count;
  34. static unsigned int close_called;
  35. static void set_nonblocking(uv_os_sock_t sock) {
  36. int r;
  37. #ifdef _WIN32
  38. unsigned long on = 1;
  39. r = ioctlsocket(sock, FIONBIO, &on);
  40. ASSERT(r == 0);
  41. #else
  42. int flags = fcntl(sock, F_GETFL, 0);
  43. ASSERT(flags >= 0);
  44. r = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
  45. ASSERT(r >= 0);
  46. #endif
  47. }
  48. static void close_cb(uv_handle_t* handle) {
  49. close_called++;
  50. }
  51. static void alloc_cb(uv_handle_t* handle, size_t size, uv_buf_t* buf) {
  52. static char base[1];
  53. buf->base = base;
  54. buf->len = sizeof(base);
  55. }
  56. static void read_cb(uv_stream_t* handle,
  57. ssize_t nread,
  58. const uv_buf_t* buf) {
  59. uv_pipe_t* p;
  60. uv_pipe_t* inc;
  61. uv_handle_type pending;
  62. unsigned int i;
  63. p = (uv_pipe_t*) handle;
  64. ASSERT(nread >= 0);
  65. while (uv_pipe_pending_count(p) != 0) {
  66. pending = uv_pipe_pending_type(p);
  67. ASSERT(pending == UV_NAMED_PIPE);
  68. ASSERT(incoming_count < ARRAY_SIZE(incoming));
  69. inc = &incoming[incoming_count++];
  70. ASSERT(0 == uv_pipe_init(p->loop, inc, 0));
  71. ASSERT(0 == uv_accept(handle, (uv_stream_t*) inc));
  72. }
  73. if (incoming_count != ARRAY_SIZE(incoming))
  74. return;
  75. ASSERT(0 == uv_read_stop((uv_stream_t*) p));
  76. uv_close((uv_handle_t*) p, close_cb);
  77. for (i = 0; i < ARRAY_SIZE(incoming); i++)
  78. uv_close((uv_handle_t*) &incoming[i], close_cb);
  79. }
  80. TEST_IMPL(pipe_sendmsg) {
  81. #if defined(NO_SEND_HANDLE_ON_PIPE)
  82. RETURN_SKIP(NO_SEND_HANDLE_ON_PIPE);
  83. #endif
  84. uv_pipe_t p;
  85. int r;
  86. int fds[2];
  87. int send_fds[ARRAY_SIZE(incoming)];
  88. struct msghdr msg;
  89. char scratch[64];
  90. struct cmsghdr *cmsg;
  91. unsigned int i;
  92. uv_buf_t buf;
  93. ASSERT(0 == socketpair(AF_UNIX, SOCK_STREAM, 0, fds));
  94. for (i = 0; i < ARRAY_SIZE(send_fds); i += 2)
  95. ASSERT(0 == socketpair(AF_UNIX, SOCK_STREAM, 0, send_fds + i));
  96. ASSERT(i == ARRAY_SIZE(send_fds));
  97. ASSERT(0 == uv_pipe_init(uv_default_loop(), &p, 1));
  98. ASSERT(0 == uv_pipe_open(&p, fds[1]));
  99. buf = uv_buf_init("X", 1);
  100. memset(&msg, 0, sizeof(msg));
  101. msg.msg_iov = (struct iovec*) &buf;
  102. msg.msg_iovlen = 1;
  103. msg.msg_flags = 0;
  104. msg.msg_control = (void*) scratch;
  105. msg.msg_controllen = CMSG_LEN(sizeof(send_fds));
  106. ASSERT(sizeof(scratch) >= msg.msg_controllen);
  107. cmsg = CMSG_FIRSTHDR(&msg);
  108. cmsg->cmsg_level = SOL_SOCKET;
  109. cmsg->cmsg_type = SCM_RIGHTS;
  110. cmsg->cmsg_len = msg.msg_controllen;
  111. /* silence aliasing warning */
  112. {
  113. void* pv = CMSG_DATA(cmsg);
  114. int* pi = pv;
  115. for (i = 0; i < ARRAY_SIZE(send_fds); i++)
  116. pi[i] = send_fds[i];
  117. }
  118. set_nonblocking(fds[1]);
  119. ASSERT(0 == uv_read_start((uv_stream_t*) &p, alloc_cb, read_cb));
  120. do
  121. r = sendmsg(fds[0], &msg, 0);
  122. while (r == -1 && errno == EINTR);
  123. ASSERT(r == 1);
  124. uv_run(uv_default_loop(), UV_RUN_DEFAULT);
  125. ASSERT(ARRAY_SIZE(incoming) == incoming_count);
  126. ASSERT(ARRAY_SIZE(incoming) + 1 == close_called);
  127. close(fds[0]);
  128. MAKE_VALGRIND_HAPPY();
  129. return 0;
  130. }
  131. #else /* !_WIN32 */
  132. TEST_IMPL(pipe_sendmsg) {
  133. MAKE_VALGRIND_HAPPY();
  134. return 0;
  135. }
  136. #endif /* _WIN32 */