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 <errno.h>
23 
24 #ifndef _WIN32
25 # include <fcntl.h>
26 # include <sys/socket.h>
27 # include <unistd.h>
28 #endif
29 
30 #include "uv.h"
31 #include "task.h"
32 
33 
34 static int close_cb_called = 0;
35 
36 
close_cb(uv_handle_t * handle)37 static void close_cb(uv_handle_t* handle) {
38   close_cb_called++;
39 }
40 
poll_cb(uv_poll_t * handle,int status,int events)41 static void poll_cb(uv_poll_t* handle, int status, int events) {
42   /* Not a bound socket, linux immediately reports UV_READABLE, other OS do not */
43   ASSERT_EQ(events, UV_READABLE);
44 }
45 
TEST_IMPL(poll_multiple_handles)46 TEST_IMPL(poll_multiple_handles) {
47   uv_os_sock_t sock;
48   uv_poll_t first_poll_handle, second_poll_handle;
49 
50 #ifdef _WIN32
51   {
52     struct WSAData wsa_data;
53     int r = WSAStartup(MAKEWORD(2, 2), &wsa_data);
54     ASSERT_OK(r);
55   }
56 #endif
57 
58   sock = socket(AF_INET, SOCK_STREAM, 0);
59 #ifdef _WIN32
60   ASSERT_NE(sock, INVALID_SOCKET);
61 #else
62   ASSERT_NE(sock, -1);
63 #endif
64   ASSERT_OK(uv_poll_init_socket(uv_default_loop(),
65                                 &first_poll_handle,
66                                 sock));
67   ASSERT_OK(uv_poll_init_socket(uv_default_loop(),
68                                 &second_poll_handle,
69                                 sock));
70 
71   ASSERT_OK(uv_poll_start(&first_poll_handle, UV_READABLE, poll_cb));
72 
73   /* We may not start polling while another polling handle is active
74    * on that fd.
75    */
76 #ifndef _WIN32
77   /* We do not track handles in an O(1) lookupable way on Windows,
78    * so not checking that here.
79    */
80   ASSERT_EQ(uv_poll_start(&second_poll_handle, UV_READABLE, poll_cb),
81             UV_EEXIST);
82 #endif
83 
84   /* After stopping the other polling handle, we now should be able to poll */
85   ASSERT_OK(uv_poll_stop(&first_poll_handle));
86   ASSERT_OK(uv_poll_start(&second_poll_handle, UV_READABLE, poll_cb));
87 
88   /* Closing an already stopped polling handle is safe in any case */
89   uv_close((uv_handle_t*) &first_poll_handle, close_cb);
90 
91   uv_unref((uv_handle_t*) &second_poll_handle);
92   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
93   ASSERT_EQ(1, close_cb_called);
94   uv_ref((uv_handle_t*) &second_poll_handle);
95 
96   ASSERT(uv_is_active((uv_handle_t*) &second_poll_handle));
97   uv_close((uv_handle_t*) &second_poll_handle, close_cb);
98 
99   ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
100   ASSERT_EQ(2, close_cb_called);
101 
102   MAKE_VALGRIND_HAPPY(uv_default_loop());
103   return 0;
104 }
105