xref: /libuv/test/test-eintr-handling.c (revision 011a1ac1)
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 
TEST_IMPL(eintr_handling)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 
thread_main(void * arg)49 static void thread_main(void* arg) {
50   int nwritten;
51   ASSERT_OK(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_EQ(nwritten, sizeof(test_buf));
58 }
59 
sig_func(uv_signal_t * handle,int signum)60 static void sig_func(uv_signal_t* handle, int signum) {
61   uv_signal_stop(handle);
62 }
63 
TEST_IMPL(eintr_handling)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_OK(uv_signal_init(loop, &signal));
74   ASSERT_OK(uv_signal_start(&signal, sig_func, SIGUSR1));
75 
76   ASSERT_OK(pipe(pipe_fds));
77   ASSERT_OK(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_EQ(nread, sizeof(test_buf));
82   ASSERT_OK(strcmp(buf, test_buf));
83 
84   ASSERT_OK(uv_run(loop, UV_RUN_DEFAULT));
85 
86   ASSERT_OK(close(pipe_fds[0]));
87   ASSERT_OK(close(pipe_fds[1]));
88   uv_close((uv_handle_t*) &signal, NULL);
89 
90   ASSERT_OK(uv_thread_join(&thread));
91 
92   MAKE_VALGRIND_HAPPY(loop);
93   return 0;
94 }
95 
96 #endif  /* !_WIN32 */
97