test-pipe-close-stdout-read-stdin.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef _WIN32
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #include <sys/wait.h>
  25. #include <sys/types.h>
  26. #include "uv.h"
  27. #include "task.h"
  28. void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t* buf)
  29. {
  30. static char buffer[1024];
  31. buf->base = buffer;
  32. buf->len = sizeof(buffer);
  33. }
  34. void read_stdin(uv_stream_t *stream, ssize_t nread, const uv_buf_t* buf)
  35. {
  36. if (nread < 0) {
  37. uv_close((uv_handle_t*)stream, NULL);
  38. return;
  39. }
  40. }
  41. /*
  42. * This test is a reproduction of joyent/libuv#1419 .
  43. */
  44. TEST_IMPL(pipe_close_stdout_read_stdin) {
  45. int r = -1;
  46. int pid;
  47. int fd[2];
  48. int status;
  49. char buf;
  50. uv_pipe_t stdin_pipe;
  51. r = pipe(fd);
  52. ASSERT(r == 0);
  53. if ((pid = fork()) == 0) {
  54. /*
  55. * Make the read side of the pipe our stdin.
  56. * The write side will be closed by the parent process.
  57. */
  58. close(fd[1]);
  59. /* block until write end of pipe is closed */
  60. r = read(fd[0], &buf, 1);
  61. ASSERT(-1 <= r && r <= 1);
  62. close(0);
  63. r = dup(fd[0]);
  64. ASSERT(r != -1);
  65. /* Create a stream that reads from the pipe. */
  66. r = uv_pipe_init(uv_default_loop(), (uv_pipe_t *)&stdin_pipe, 0);
  67. ASSERT(r == 0);
  68. r = uv_pipe_open((uv_pipe_t *)&stdin_pipe, 0);
  69. ASSERT(r == 0);
  70. r = uv_read_start((uv_stream_t *)&stdin_pipe, alloc_buffer, read_stdin);
  71. ASSERT(r == 0);
  72. /*
  73. * Because the other end of the pipe was closed, there should
  74. * be no event left to process after one run of the event loop.
  75. * Otherwise, it means that events were not processed correctly.
  76. */
  77. ASSERT(uv_run(uv_default_loop(), UV_RUN_NOWAIT) == 0);
  78. } else {
  79. /*
  80. * Close both ends of the pipe so that the child
  81. * get a POLLHUP event when it tries to read from
  82. * the other end.
  83. */
  84. close(fd[1]);
  85. close(fd[0]);
  86. waitpid(pid, &status, 0);
  87. ASSERT(WIFEXITED(status) && WEXITSTATUS(status) == 0);
  88. }
  89. MAKE_VALGRIND_HAPPY();
  90. return 0;
  91. }
  92. #else
  93. typedef int file_has_no_tests; /* ISO C forbids an empty translation unit. */
  94. #endif /* ifndef _WIN32 */