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 29static uv_loop_t loop; 30static uv_signal_t signal_hdl; 31static uv_pipe_t pipe_hdl; 32static uv_write_t write_req; 33static char* buf; 34static int close_cb_called; 35 36 37static void stop_loop_cb(uv_signal_t* signal, int signum) { 38 ASSERT_EQ(signum, SIGPIPE); 39 uv_stop(signal->loop); 40} 41 42static void signal_cb(uv_signal_t* signal, int signum) { 43 ASSERT(0); 44} 45 46static void close_cb(uv_handle_t *handle) { 47 close_cb_called++; 48} 49 50 51static void write_cb(uv_write_t* req, int status) { 52 ASSERT_NOT_NULL(req); 53 ASSERT_EQ(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 60TEST_IMPL(signal_pending_on_close) { 61 int pipefds[2]; 62 uv_buf_t buffer; 63 int r; 64 65 ASSERT_OK(uv_loop_init(&loop)); 66 67 ASSERT_OK(uv_signal_init(&loop, &signal_hdl)); 68 69 ASSERT_OK(uv_signal_start(&signal_hdl, signal_cb, SIGPIPE)); 70 71 ASSERT_OK(pipe(pipefds)); 72 73 ASSERT_OK(uv_pipe_init(&loop, &pipe_hdl, 0)); 74 75 ASSERT_OK(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_OK(r); 85 86 /* cause a SIGPIPE on write in next iteration */ 87 close(pipefds[0]); 88 89 ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT)); 90 91 ASSERT_EQ(2, close_cb_called); 92 93 MAKE_VALGRIND_HAPPY(&loop); 94 return 0; 95} 96 97 98TEST_IMPL(signal_close_loop_alive) { 99 ASSERT_OK(uv_loop_init(&loop)); 100 ASSERT_OK(uv_signal_init(&loop, &signal_hdl)); 101 ASSERT_OK(uv_signal_start(&signal_hdl, stop_loop_cb, SIGPIPE)); 102 uv_unref((uv_handle_t*) &signal_hdl); 103 104 ASSERT_OK(uv_kill(uv_os_getpid(), SIGPIPE)); 105 ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT)); 106 uv_close((uv_handle_t*) &signal_hdl, close_cb); 107 ASSERT_EQ(1, uv_loop_alive(&loop)); 108 109 ASSERT_OK(uv_run(&loop, UV_RUN_DEFAULT)); 110 ASSERT_EQ(1, close_cb_called); 111 112 MAKE_VALGRIND_HAPPY(&loop); 113 return 0; 114} 115 116#endif 117