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