test-eintr-handling.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Copyright libuv project 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. #ifdef _WIN32
  24. TEST_IMPL(eintr_handling) {
  25. RETURN_SKIP("Test not implemented on Windows.");
  26. }
  27. #else /* !_WIN32 */
  28. #include <string.h>
  29. #include <unistd.h>
  30. static uv_loop_t* loop;
  31. static uv_fs_t read_req;
  32. static uv_buf_t iov;
  33. static char buf[32];
  34. static char test_buf[] = "test-buffer\n";
  35. int pipe_fds[2];
  36. struct thread_ctx {
  37. uv_barrier_t barrier;
  38. int fd;
  39. };
  40. static void thread_main(void* arg) {
  41. int nwritten;
  42. ASSERT(0 == kill(getpid(), SIGUSR1));
  43. do
  44. nwritten = write(pipe_fds[1], test_buf, sizeof(test_buf));
  45. while (nwritten == -1 && errno == EINTR);
  46. ASSERT(nwritten == sizeof(test_buf));
  47. }
  48. static void sig_func(uv_signal_t* handle, int signum) {
  49. uv_signal_stop(handle);
  50. }
  51. TEST_IMPL(eintr_handling) {
  52. struct thread_ctx ctx;
  53. uv_thread_t thread;
  54. uv_signal_t signal;
  55. int nread;
  56. iov = uv_buf_init(buf, sizeof(buf));
  57. loop = uv_default_loop();
  58. ASSERT(0 == uv_signal_init(loop, &signal));
  59. ASSERT(0 == uv_signal_start(&signal, sig_func, SIGUSR1));
  60. ASSERT(0 == pipe(pipe_fds));
  61. ASSERT(0 == uv_thread_create(&thread, thread_main, &ctx));
  62. nread = uv_fs_read(loop, &read_req, pipe_fds[0], &iov, 1, -1, NULL);
  63. ASSERT(nread == sizeof(test_buf));
  64. ASSERT(0 == strcmp(buf, test_buf));
  65. ASSERT(0 == uv_run(loop, UV_RUN_DEFAULT));
  66. ASSERT(0 == close(pipe_fds[0]));
  67. ASSERT(0 == close(pipe_fds[1]));
  68. uv_close((uv_handle_t*) &signal, NULL);
  69. MAKE_VALGRIND_HAPPY();
  70. return 0;
  71. }
  72. #endif /* !_WIN32 */