1/* Copyright Joyent, Inc. and other Node 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#ifndef TASK_H_
23#define TASK_H_
24
25#include "uv.h"
26
27#include <stdio.h>
28#include <stddef.h>
29#include <stdlib.h>
30
31#if defined(_MSC_VER) && _MSC_VER < 1600
32# include "uv/stdint-msvc2008.h"
33#else
34# include <stdint.h>
35#endif
36
37#if !defined(_WIN32)
38# include <sys/time.h>
39# include <sys/resource.h> /* setrlimit() */
40#endif
41
42#ifdef __clang__
43# pragma clang diagnostic ignored "-Wvariadic-macros"
44# pragma clang diagnostic ignored "-Wc99-extensions"
45#endif
46
47#ifdef __GNUC__
48# pragma GCC diagnostic ignored "-Wvariadic-macros"
49#endif
50
51#define TEST_PORT 9123
52#define TEST_PORT_2 9124
53
54#ifdef _WIN32
55# define TEST_PIPENAME "\\\\?\\pipe\\uv-test"
56# define TEST_PIPENAME_2 "\\\\?\\pipe\\uv-test2"
57# define TEST_PIPENAME_3 "\\\\?\\pipe\\uv-test3"
58#else
59# define TEST_PIPENAME "/tmp/uv-test-sock"
60# define TEST_PIPENAME_2 "/tmp/uv-test-sock2"
61# define TEST_PIPENAME_3 "/tmp/uv-test-sock3"
62#endif
63
64#ifdef _WIN32
65# include <io.h>
66# ifndef S_IRUSR
67# define S_IRUSR _S_IREAD
68# endif
69# ifndef S_IWUSR
70# define S_IWUSR _S_IWRITE
71# endif
72#endif
73
74#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
75
76#define container_of(ptr, type, member) \
77 ((type *) ((char *) (ptr) - offsetof(type, member)))
78
79typedef enum {
80 TCP = 0,
81 UDP,
82 PIPE
83} stream_type;
84
85/* Die with fatal error. */
86#define FATAL(msg) \
87 do { \
88 fprintf(stderr, \
89 "Fatal error in %s on line %d: %s\n", \
90 __FILE__, \
91 __LINE__, \
92 msg); \
93 fflush(stderr); \
94 abort(); \
95 } while (0)
96
97/* Have our own assert, so we are sure it does not get optimized away in
98 * a release build.
99 */
100#define ASSERT(expr) \
101 do { \
102 if (!(expr)) { \
103 fprintf(stderr, \
104 "Assertion failed in %s on line %d: %s\n", \
105 __FILE__, \
106 __LINE__, \
107 #expr); \
108 abort(); \
109 } \
110 } while (0)
111
112/* This macro cleans up the main loop. This is used to avoid valgrind
113 * warnings about memory being "leaked" by the main event loop.
114 */
115#define MAKE_VALGRIND_HAPPY() \
116 do { \
117 close_loop(uv_default_loop()); \
118 ASSERT(0 == uv_loop_close(uv_default_loop())); \
119 } while (0)
120
121/* Just sugar for wrapping the main() for a task or helper. */
122#define TEST_IMPL(name) \
123 int run_test_##name(void); \
124 int run_test_##name(void)
125
126#define BENCHMARK_IMPL(name) \
127 int run_benchmark_##name(void); \
128 int run_benchmark_##name(void)
129
130#define HELPER_IMPL(name) \
131 int run_helper_##name(void); \
132 int run_helper_##name(void)
133
134/* Pause the calling thread for a number of milliseconds. */
135void uv_sleep(int msec);
136
137/* Format big numbers nicely. WARNING: leaks memory. */
138const char* fmt(double d);
139
140/* Reserved test exit codes. */
141enum test_status {
142 TEST_OK = 0,
143 TEST_SKIP
144};
145
146#define RETURN_OK() \
147 do { \
148 return TEST_OK; \
149 } while (0)
150
151#define RETURN_SKIP(explanation) \
152 do { \
153 fprintf(stderr, "%s\n", explanation); \
154 fflush(stderr); \
155 return TEST_SKIP; \
156 } while (0)
157
158#if !defined(_WIN32)
159
160# define TEST_FILE_LIMIT(num) \
161 do { \
162 struct rlimit lim; \
163 lim.rlim_cur = (num); \
164 lim.rlim_max = lim.rlim_cur; \
165 if (setrlimit(RLIMIT_NOFILE, &lim)) \
166 RETURN_SKIP("File descriptor limit too low."); \
167 } while (0)
168
169#else /* defined(_WIN32) */
170
171# define TEST_FILE_LIMIT(num) do {} while (0)
172
173#endif
174
175#if !defined(snprintf) && defined(_MSC_VER) && _MSC_VER < 1900
176extern int snprintf(char*, size_t, const char*, ...);
177#endif
178
179#if defined(__clang__) || \
180 defined(__GNUC__) || \
181 defined(__INTEL_COMPILER)
182# define UNUSED __attribute__((unused))
183#else
184# define UNUSED
185#endif
186
187#if defined(_WIN32)
188#define notify_parent_process() ((void) 0)
189#else
190extern void notify_parent_process(void);
191#endif
192
193/* Fully close a loop */
194static void close_walk_cb(uv_handle_t* handle, void* arg) {
195 if (!uv_is_closing(handle))
196 uv_close(handle, NULL);
197}
198
199UNUSED static void close_loop(uv_loop_t* loop) {
200 uv_walk(loop, close_walk_cb, NULL);
201 uv_run(loop, UV_RUN_DEFAULT);
202}
203
204UNUSED static int can_ipv6(void) {
205 uv_interface_address_t* addr;
206 int supported;
207 int count;
208 int i;
209
210 if (uv_interface_addresses(&addr, &count))
211 return 0; /* Assume no IPv6 support on failure. */
212
213 supported = 0;
214 for (i = 0; supported == 0 && i < count; i += 1)
215 supported = (AF_INET6 == addr[i].address.address6.sin6_family);
216
217 uv_free_interface_addresses(addr, count);
218 return supported;
219}
220
221#if defined(__CYGWIN__) || defined(__MSYS__)
222# define NO_FS_EVENTS "Filesystem watching not supported on this platform."
223#endif
224
225#if defined(__MSYS__)
226# define NO_SEND_HANDLE_ON_PIPE \
227 "MSYS2 runtime does not support sending handles on pipes."
228#elif defined(__CYGWIN__)
229# define NO_SEND_HANDLE_ON_PIPE \
230 "Cygwin runtime does not support sending handles on pipes."
231#endif
232
233#if defined(__MSYS__)
234# define NO_SELF_CONNECT \
235 "MSYS2 runtime hangs on listen+connect in same process."
236#elif defined(__CYGWIN__)
237# define NO_SELF_CONNECT \
238 "Cygwin runtime hangs on listen+connect in same process."
239#endif
240
241#endif /* TASK_H_ */
242