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 #ifndef _WIN32
22
23 #include "uv.h"
24 #include "task.h"
25
26 #include <string.h>
27 #include <unistd.h>
28
29 static uv_loop_t loop;
30 static uv_signal_t signal_hdl;
31 static uv_pipe_t pipe_hdl;
32 static uv_write_t write_req;
33 static char* buf;
34 static int close_cb_called;
35
36
stop_loop_cb(uv_signal_t * signal,int signum)37 static void stop_loop_cb(uv_signal_t* signal, int signum) {
38 ASSERT(signum == SIGPIPE);
39 uv_stop(signal->loop);
40 }
41
signal_cb(uv_signal_t * signal,int signum)42 static void signal_cb(uv_signal_t* signal, int signum) {
43 ASSERT(0);
44 }
45
close_cb(uv_handle_t * handle)46 static void close_cb(uv_handle_t *handle) {
47 close_cb_called++;
48 }
49
50
write_cb(uv_write_t * req,int status)51 static void write_cb(uv_write_t* req, int status) {
52 ASSERT_NOT_NULL(req);
53 ASSERT(status == UV_EPIPE);
54 free(buf);
55 uv_close((uv_handle_t *) &pipe_hdl, close_cb);
56 uv_close((uv_handle_t *) &signal_hdl, close_cb);
57 }
58
59
TEST_IMPL(signal_pending_on_close)60 TEST_IMPL(signal_pending_on_close) {
61 int pipefds[2];
62 uv_buf_t buffer;
63 int r;
64
65 ASSERT(0 == uv_loop_init(&loop));
66
67 ASSERT(0 == uv_signal_init(&loop, &signal_hdl));
68
69 ASSERT(0 == uv_signal_start(&signal_hdl, signal_cb, SIGPIPE));
70
71 ASSERT(0 == pipe(pipefds));
72
73 ASSERT(0 == uv_pipe_init(&loop, &pipe_hdl, 0));
74
75 ASSERT(0 == uv_pipe_open(&pipe_hdl, pipefds[1]));
76
77 /* Write data large enough so it needs loop iteration */
78 buf = malloc(1<<24);
79 ASSERT_NOT_NULL(buf);
80 memset(buf, '.', 1<<24);
81 buffer = uv_buf_init(buf, 1<<24);
82
83 r = uv_write(&write_req, (uv_stream_t *) &pipe_hdl, &buffer, 1, write_cb);
84 ASSERT(0 == r);
85
86 /* cause a SIGPIPE on write in next iteration */
87 close(pipefds[0]);
88
89 ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
90
91 ASSERT(0 == uv_loop_close(&loop));
92
93 ASSERT(2 == close_cb_called);
94
95 MAKE_VALGRIND_HAPPY();
96 return 0;
97 }
98
99
TEST_IMPL(signal_close_loop_alive)100 TEST_IMPL(signal_close_loop_alive) {
101 ASSERT(0 == uv_loop_init(&loop));
102 ASSERT(0 == uv_signal_init(&loop, &signal_hdl));
103 ASSERT(0 == uv_signal_start(&signal_hdl, stop_loop_cb, SIGPIPE));
104 uv_unref((uv_handle_t*) &signal_hdl);
105
106 ASSERT(0 == uv_kill(uv_os_getpid(), SIGPIPE));
107 ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
108 uv_close((uv_handle_t*) &signal_hdl, close_cb);
109 ASSERT(1 == uv_loop_alive(&loop));
110
111 ASSERT(0 == uv_run(&loop, UV_RUN_DEFAULT));
112 ASSERT(0 == uv_loop_close(&loop));
113 ASSERT(1 == close_cb_called);
114
115 MAKE_VALGRIND_HAPPY();
116 return 0;
117 }
118
119 #endif
120