passenger 5.0.8 → 5.0.9

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of passenger might be problematic. Click here for more details.

Files changed (168) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.asc +7 -7
  3. data.tar.gz.asc +7 -7
  4. data/.editorconfig +20 -0
  5. data/CHANGELOG +21 -0
  6. data/bin/passenger-install-apache2-module +3 -1
  7. data/build/agents.rb +7 -5
  8. data/build/basics.rb +3 -3
  9. data/build/common_library.rb +52 -30
  10. data/build/cxx_tests.rb +20 -13
  11. data/build/misc.rb +5 -5
  12. data/doc/Design and Architecture.html +1 -1
  13. data/doc/Design and Architecture.txt +1 -1
  14. data/doc/Packaging.html +4 -4
  15. data/doc/Packaging.txt.md +4 -4
  16. data/doc/Users guide Apache.html +22 -9
  17. data/doc/Users guide Apache.idmap.txt +4 -2
  18. data/doc/Users guide Apache.txt +2 -0
  19. data/doc/Users guide Nginx.html +22 -9
  20. data/doc/Users guide Nginx.idmap.txt +4 -2
  21. data/doc/Users guide Nginx.txt +2 -0
  22. data/doc/Users guide Standalone.html +14 -9
  23. data/doc/Users guide Standalone.idmap.txt +4 -2
  24. data/doc/users_guide_snippets/installation.txt +10 -6
  25. data/ext/apache2/Hooks.cpp +13 -2
  26. data/ext/common/ApplicationPool2/Pool/Inspection.h +8 -3
  27. data/ext/common/BackgroundEventLoop.cpp +249 -67
  28. data/ext/common/BackgroundEventLoop.h +5 -5
  29. data/ext/common/Constants.h +1 -1
  30. data/ext/common/InstanceDirectory.h +8 -6
  31. data/ext/common/ServerKit/Context.h +8 -2
  32. data/ext/common/ServerKit/FileBufferedChannel.h +262 -226
  33. data/ext/common/ServerKit/HeaderTable.h +28 -3
  34. data/ext/common/ServerKit/HttpHeaderParser.h +37 -13
  35. data/ext/common/ServerKit/HttpServer.h +17 -1
  36. data/ext/common/ServerKit/Implementation.cpp +2 -0
  37. data/ext/common/ServerKit/Server.h +25 -28
  38. data/ext/common/Utils/IOUtils.cpp +11 -0
  39. data/ext/common/Utils/ProcessMetricsCollector.h +4 -0
  40. data/ext/common/Utils/StrIntUtils.cpp +11 -7
  41. data/ext/common/Utils/StrIntUtils.h +1 -1
  42. data/ext/common/Utils/StrIntUtilsNoStrictAliasing.cpp +21 -16
  43. data/ext/common/agents/Base.cpp +6 -0
  44. data/ext/common/agents/Base.h +2 -0
  45. data/ext/common/agents/HelperAgent/AdminServer.h +25 -25
  46. data/ext/common/agents/HelperAgent/Main.cpp +37 -12
  47. data/ext/common/agents/HelperAgent/RequestHandler.h +18 -20
  48. data/ext/common/agents/HelperAgent/RequestHandler/AppResponse.h +4 -0
  49. data/ext/common/agents/HelperAgent/RequestHandler/ForwardResponse.cpp +10 -6
  50. data/ext/common/agents/HelperAgent/RequestHandler/Hooks.cpp +2 -0
  51. data/ext/common/agents/HelperAgent/RequestHandler/InitRequest.cpp +1 -1
  52. data/ext/common/agents/HelperAgent/RequestHandler/SendRequest.cpp +1 -1
  53. data/ext/common/agents/HelperAgent/RequestHandler/Utils.cpp +9 -2
  54. data/ext/common/agents/HelperAgent/ResponseCache.h +11 -11
  55. data/ext/common/agents/LoggingAgent/AdminServer.h +8 -8
  56. data/ext/common/agents/LoggingAgent/Main.cpp +6 -5
  57. data/ext/common/agents/Watchdog/AdminServer.h +13 -13
  58. data/ext/common/agents/Watchdog/Main.cpp +8 -3
  59. data/ext/libuv/.gitignore +72 -0
  60. data/ext/libuv/AUTHORS +199 -0
  61. data/ext/libuv/ChangeLog +2023 -0
  62. data/ext/libuv/LICENSE +46 -0
  63. data/ext/libuv/Makefile.am +336 -0
  64. data/ext/libuv/README.md +197 -0
  65. data/ext/libuv/checksparse.sh +233 -0
  66. data/ext/libuv/common.gypi +210 -0
  67. data/ext/libuv/configure.ac +67 -0
  68. data/ext/libuv/gyp_uv.py +96 -0
  69. data/ext/libuv/include/android-ifaddrs.h +54 -0
  70. data/ext/libuv/include/pthread-fixes.h +72 -0
  71. data/ext/libuv/include/tree.h +768 -0
  72. data/ext/libuv/include/uv-aix.h +32 -0
  73. data/ext/libuv/include/uv-bsd.h +34 -0
  74. data/ext/libuv/include/uv-darwin.h +61 -0
  75. data/ext/libuv/include/uv-errno.h +418 -0
  76. data/ext/libuv/include/uv-linux.h +34 -0
  77. data/ext/libuv/include/uv-sunos.h +44 -0
  78. data/ext/libuv/include/uv-threadpool.h +37 -0
  79. data/ext/libuv/include/uv-unix.h +383 -0
  80. data/ext/libuv/include/uv-version.h +39 -0
  81. data/ext/libuv/include/uv.h +1455 -0
  82. data/ext/libuv/libuv.pc.in +11 -0
  83. data/ext/libuv/m4/.gitignore +4 -0
  84. data/ext/libuv/m4/as_case.m4 +21 -0
  85. data/ext/libuv/m4/libuv-check-flags.m4 +319 -0
  86. data/ext/libuv/src/fs-poll.c +255 -0
  87. data/ext/libuv/src/heap-inl.h +245 -0
  88. data/ext/libuv/src/inet.c +313 -0
  89. data/ext/libuv/src/queue.h +92 -0
  90. data/ext/libuv/src/threadpool.c +303 -0
  91. data/ext/libuv/src/unix/aix.c +1240 -0
  92. data/ext/libuv/src/unix/android-ifaddrs.c +703 -0
  93. data/ext/libuv/src/unix/async.c +284 -0
  94. data/ext/libuv/src/unix/atomic-ops.h +60 -0
  95. data/ext/libuv/src/unix/core.c +985 -0
  96. data/ext/libuv/src/unix/darwin-proctitle.c +206 -0
  97. data/ext/libuv/src/unix/darwin.c +331 -0
  98. data/ext/libuv/src/unix/dl.c +83 -0
  99. data/ext/libuv/src/unix/freebsd.c +435 -0
  100. data/ext/libuv/src/unix/fs.c +1189 -0
  101. data/ext/libuv/src/unix/fsevents.c +899 -0
  102. data/ext/libuv/src/unix/getaddrinfo.c +202 -0
  103. data/ext/libuv/src/unix/getnameinfo.c +120 -0
  104. data/ext/libuv/src/unix/internal.h +314 -0
  105. data/ext/libuv/src/unix/kqueue.c +418 -0
  106. data/ext/libuv/src/unix/linux-core.c +876 -0
  107. data/ext/libuv/src/unix/linux-inotify.c +257 -0
  108. data/ext/libuv/src/unix/linux-syscalls.c +471 -0
  109. data/ext/libuv/src/unix/linux-syscalls.h +158 -0
  110. data/ext/libuv/src/unix/loop-watcher.c +63 -0
  111. data/ext/libuv/src/unix/loop.c +135 -0
  112. data/ext/libuv/src/unix/netbsd.c +368 -0
  113. data/ext/libuv/src/unix/openbsd.c +384 -0
  114. data/ext/libuv/src/unix/pipe.c +288 -0
  115. data/ext/libuv/src/unix/poll.c +113 -0
  116. data/ext/libuv/src/unix/process.c +551 -0
  117. data/ext/libuv/src/unix/proctitle.c +102 -0
  118. data/ext/libuv/src/unix/pthread-fixes.c +103 -0
  119. data/ext/libuv/src/unix/signal.c +465 -0
  120. data/ext/libuv/src/unix/spinlock.h +53 -0
  121. data/ext/libuv/src/unix/stream.c +1598 -0
  122. data/ext/libuv/src/unix/sunos.c +763 -0
  123. data/ext/libuv/src/unix/tcp.c +327 -0
  124. data/ext/libuv/src/unix/thread.c +519 -0
  125. data/ext/libuv/src/unix/timer.c +172 -0
  126. data/ext/libuv/src/unix/tty.c +265 -0
  127. data/ext/libuv/src/unix/udp.c +833 -0
  128. data/ext/libuv/src/uv-common.c +544 -0
  129. data/ext/libuv/src/uv-common.h +214 -0
  130. data/ext/libuv/src/version.c +49 -0
  131. data/ext/libuv/uv.gyp +487 -0
  132. data/ext/nginx/ContentHandler.c +21 -10
  133. data/ext/nginx/ngx_http_passenger_module.c +7 -0
  134. data/ext/oxt/implementation.cpp +9 -2
  135. data/ext/oxt/initialize.hpp +5 -1
  136. data/lib/phusion_passenger.rb +3 -3
  137. data/lib/phusion_passenger/admin_tools/instance.rb +10 -6
  138. data/lib/phusion_passenger/admin_tools/instance_registry.rb +6 -2
  139. data/lib/phusion_passenger/packaging.rb +3 -4
  140. data/lib/phusion_passenger/platform_info.rb +13 -1
  141. data/lib/phusion_passenger/platform_info/apache.rb +15 -4
  142. data/lib/phusion_passenger/platform_info/apache_detector.rb +5 -1
  143. data/lib/phusion_passenger/rack/thread_handler_extension.rb +184 -99
  144. data/lib/phusion_passenger/request_handler/thread_handler.rb +13 -6
  145. data/lib/phusion_passenger/standalone/start_command.rb +2 -2
  146. data/resources/templates/apache2/apache_install_broken.txt.erb +2 -1
  147. metadata +99 -22
  148. metadata.gz.asc +7 -7
  149. data/ext/libeio/Changes +0 -76
  150. data/ext/libeio/LICENSE +0 -36
  151. data/ext/libeio/Makefile.am +0 -15
  152. data/ext/libeio/Makefile.in +0 -694
  153. data/ext/libeio/aclocal.m4 +0 -9418
  154. data/ext/libeio/autogen.sh +0 -3
  155. data/ext/libeio/config.guess +0 -1540
  156. data/ext/libeio/config.h.in +0 -136
  157. data/ext/libeio/config.sub +0 -1779
  158. data/ext/libeio/configure +0 -14822
  159. data/ext/libeio/configure.ac +0 -22
  160. data/ext/libeio/demo.c +0 -194
  161. data/ext/libeio/ecb.h +0 -714
  162. data/ext/libeio/eio.c +0 -2818
  163. data/ext/libeio/eio.h +0 -414
  164. data/ext/libeio/install-sh +0 -520
  165. data/ext/libeio/libeio.m4 +0 -195
  166. data/ext/libeio/ltmain.sh +0 -9636
  167. data/ext/libeio/missing +0 -376
  168. data/ext/libeio/xthread.h +0 -166
@@ -0,0 +1,284 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
3
+ * of this software and associated documentation files (the "Software"), to
4
+ * deal in the Software without restriction, including without limitation the
5
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
+ * sell copies of the Software, and to permit persons to whom the Software is
7
+ * furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in
10
+ * all copies or substantial portions of the Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18
+ * IN THE SOFTWARE.
19
+ */
20
+
21
+ /* This file contains both the uv__async internal infrastructure and the
22
+ * user-facing uv_async_t functions.
23
+ */
24
+
25
+ #include "uv.h"
26
+ #include "internal.h"
27
+ #include "atomic-ops.h"
28
+
29
+ #include <errno.h>
30
+ #include <stdio.h> /* snprintf() */
31
+ #include <assert.h>
32
+ #include <stdlib.h>
33
+ #include <string.h>
34
+ #include <unistd.h>
35
+
36
+ static void uv__async_event(uv_loop_t* loop,
37
+ struct uv__async* w,
38
+ unsigned int nevents);
39
+ static int uv__async_eventfd(void);
40
+
41
+
42
+ int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
43
+ int err;
44
+
45
+ err = uv__async_start(loop, &loop->async_watcher, uv__async_event);
46
+ if (err)
47
+ return err;
48
+
49
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC);
50
+ handle->async_cb = async_cb;
51
+ handle->pending = 0;
52
+
53
+ QUEUE_INSERT_TAIL(&loop->async_handles, &handle->queue);
54
+ uv__handle_start(handle);
55
+
56
+ return 0;
57
+ }
58
+
59
+
60
+ int uv_async_send(uv_async_t* handle) {
61
+ /* Do a cheap read first. */
62
+ if (ACCESS_ONCE(int, handle->pending) != 0)
63
+ return 0;
64
+
65
+ if (cmpxchgi(&handle->pending, 0, 1) == 0)
66
+ uv__async_send(&handle->loop->async_watcher);
67
+
68
+ return 0;
69
+ }
70
+
71
+
72
+ void uv__async_close(uv_async_t* handle) {
73
+ QUEUE_REMOVE(&handle->queue);
74
+ uv__handle_stop(handle);
75
+ }
76
+
77
+
78
+ static void uv__async_event(uv_loop_t* loop,
79
+ struct uv__async* w,
80
+ unsigned int nevents) {
81
+ QUEUE* q;
82
+ uv_async_t* h;
83
+
84
+ QUEUE_FOREACH(q, &loop->async_handles) {
85
+ h = QUEUE_DATA(q, uv_async_t, queue);
86
+
87
+ if (cmpxchgi(&h->pending, 1, 0) == 0)
88
+ continue;
89
+
90
+ if (h->async_cb == NULL)
91
+ continue;
92
+ h->async_cb(h);
93
+ }
94
+ }
95
+
96
+
97
+ static void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
98
+ struct uv__async* wa;
99
+ char buf[1024];
100
+ unsigned n;
101
+ ssize_t r;
102
+
103
+ n = 0;
104
+ for (;;) {
105
+ r = read(w->fd, buf, sizeof(buf));
106
+
107
+ if (r > 0)
108
+ n += r;
109
+
110
+ if (r == sizeof(buf))
111
+ continue;
112
+
113
+ if (r != -1)
114
+ break;
115
+
116
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
117
+ break;
118
+
119
+ if (errno == EINTR)
120
+ continue;
121
+
122
+ abort();
123
+ }
124
+
125
+ wa = container_of(w, struct uv__async, io_watcher);
126
+
127
+ #if defined(__linux__)
128
+ if (wa->wfd == -1) {
129
+ uint64_t val;
130
+ assert(n == sizeof(val));
131
+ memcpy(&val, buf, sizeof(val)); /* Avoid alignment issues. */
132
+ wa->cb(loop, wa, val);
133
+ return;
134
+ }
135
+ #endif
136
+
137
+ wa->cb(loop, wa, n);
138
+ }
139
+
140
+
141
+ void uv__async_send(struct uv__async* wa) {
142
+ const void* buf;
143
+ ssize_t len;
144
+ int fd;
145
+ int r;
146
+
147
+ buf = "";
148
+ len = 1;
149
+ fd = wa->wfd;
150
+
151
+ #if defined(__linux__)
152
+ if (fd == -1) {
153
+ static const uint64_t val = 1;
154
+ buf = &val;
155
+ len = sizeof(val);
156
+ fd = wa->io_watcher.fd; /* eventfd */
157
+ }
158
+ #endif
159
+
160
+ do
161
+ r = write(fd, buf, len);
162
+ while (r == -1 && errno == EINTR);
163
+
164
+ if (r == len)
165
+ return;
166
+
167
+ if (r == -1)
168
+ if (errno == EAGAIN || errno == EWOULDBLOCK)
169
+ return;
170
+
171
+ abort();
172
+ }
173
+
174
+
175
+ void uv__async_init(struct uv__async* wa) {
176
+ wa->io_watcher.fd = -1;
177
+ wa->wfd = -1;
178
+ }
179
+
180
+
181
+ int uv__async_start(uv_loop_t* loop, struct uv__async* wa, uv__async_cb cb) {
182
+ int pipefd[2];
183
+ int err;
184
+
185
+ if (wa->io_watcher.fd != -1)
186
+ return 0;
187
+
188
+ err = uv__async_eventfd();
189
+ if (err >= 0) {
190
+ pipefd[0] = err;
191
+ pipefd[1] = -1;
192
+ }
193
+ else if (err == -ENOSYS) {
194
+ err = uv__make_pipe(pipefd, UV__F_NONBLOCK);
195
+ #if defined(__linux__)
196
+ /* Save a file descriptor by opening one of the pipe descriptors as
197
+ * read/write through the procfs. That file descriptor can then
198
+ * function as both ends of the pipe.
199
+ */
200
+ if (err == 0) {
201
+ char buf[32];
202
+ int fd;
203
+
204
+ snprintf(buf, sizeof(buf), "/proc/self/fd/%d", pipefd[0]);
205
+ fd = uv__open_cloexec(buf, O_RDWR);
206
+ if (fd >= 0) {
207
+ uv__close(pipefd[0]);
208
+ uv__close(pipefd[1]);
209
+ pipefd[0] = fd;
210
+ pipefd[1] = fd;
211
+ }
212
+ }
213
+ #endif
214
+ }
215
+
216
+ if (err < 0)
217
+ return err;
218
+
219
+ uv__io_init(&wa->io_watcher, uv__async_io, pipefd[0]);
220
+ uv__io_start(loop, &wa->io_watcher, UV__POLLIN);
221
+ wa->wfd = pipefd[1];
222
+ wa->cb = cb;
223
+
224
+ return 0;
225
+ }
226
+
227
+
228
+ void uv__async_stop(uv_loop_t* loop, struct uv__async* wa) {
229
+ if (wa->io_watcher.fd == -1)
230
+ return;
231
+
232
+ if (wa->wfd != -1) {
233
+ if (wa->wfd != wa->io_watcher.fd)
234
+ uv__close(wa->wfd);
235
+ wa->wfd = -1;
236
+ }
237
+
238
+ uv__io_stop(loop, &wa->io_watcher, UV__POLLIN);
239
+ uv__close(wa->io_watcher.fd);
240
+ wa->io_watcher.fd = -1;
241
+ }
242
+
243
+
244
+ static int uv__async_eventfd() {
245
+ #if defined(__linux__)
246
+ static int no_eventfd2;
247
+ static int no_eventfd;
248
+ int fd;
249
+
250
+ if (no_eventfd2)
251
+ goto skip_eventfd2;
252
+
253
+ fd = uv__eventfd2(0, UV__EFD_CLOEXEC | UV__EFD_NONBLOCK);
254
+ if (fd != -1)
255
+ return fd;
256
+
257
+ if (errno != ENOSYS)
258
+ return -errno;
259
+
260
+ no_eventfd2 = 1;
261
+
262
+ skip_eventfd2:
263
+
264
+ if (no_eventfd)
265
+ goto skip_eventfd;
266
+
267
+ fd = uv__eventfd(0);
268
+ if (fd != -1) {
269
+ uv__cloexec(fd, 1);
270
+ uv__nonblock(fd, 1);
271
+ return fd;
272
+ }
273
+
274
+ if (errno != ENOSYS)
275
+ return -errno;
276
+
277
+ no_eventfd = 1;
278
+
279
+ skip_eventfd:
280
+
281
+ #endif
282
+
283
+ return -ENOSYS;
284
+ }
@@ -0,0 +1,60 @@
1
+ /* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+ */
15
+
16
+ #ifndef UV_ATOMIC_OPS_H_
17
+ #define UV_ATOMIC_OPS_H_
18
+
19
+ #include "internal.h" /* UV_UNUSED */
20
+
21
+ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval));
22
+ UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval));
23
+ UV_UNUSED(static void cpu_relax(void));
24
+
25
+ /* Prefer hand-rolled assembly over the gcc builtins because the latter also
26
+ * issue full memory barriers.
27
+ */
28
+ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)) {
29
+ #if defined(__i386__) || defined(__x86_64__)
30
+ int out;
31
+ __asm__ __volatile__ ("lock; cmpxchg %2, %1;"
32
+ : "=a" (out), "+m" (*(volatile int*) ptr)
33
+ : "r" (newval), "0" (oldval)
34
+ : "memory");
35
+ return out;
36
+ #else
37
+ return __sync_val_compare_and_swap(ptr, oldval, newval);
38
+ #endif
39
+ }
40
+
41
+ UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval)) {
42
+ #if defined(__i386__) || defined(__x86_64__)
43
+ long out;
44
+ __asm__ __volatile__ ("lock; cmpxchg %2, %1;"
45
+ : "=a" (out), "+m" (*(volatile long*) ptr)
46
+ : "r" (newval), "0" (oldval)
47
+ : "memory");
48
+ return out;
49
+ #else
50
+ return __sync_val_compare_and_swap(ptr, oldval, newval);
51
+ #endif
52
+ }
53
+
54
+ UV_UNUSED(static void cpu_relax(void)) {
55
+ #if defined(__i386__) || defined(__x86_64__)
56
+ __asm__ __volatile__ ("rep; nop"); /* a.k.a. PAUSE */
57
+ #endif
58
+ }
59
+
60
+ #endif /* UV_ATOMIC_OPS_H_ */
@@ -0,0 +1,985 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
3
+ * of this software and associated documentation files (the "Software"), to
4
+ * deal in the Software without restriction, including without limitation the
5
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
+ * sell copies of the Software, and to permit persons to whom the Software is
7
+ * furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in
10
+ * all copies or substantial portions of the Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18
+ * IN THE SOFTWARE.
19
+ */
20
+
21
+ #include "uv.h"
22
+ #include "internal.h"
23
+
24
+ #include <stddef.h> /* NULL */
25
+ #include <stdio.h> /* printf */
26
+ #include <stdlib.h>
27
+ #include <string.h> /* strerror */
28
+ #include <errno.h>
29
+ #include <assert.h>
30
+ #include <unistd.h>
31
+ #include <sys/types.h>
32
+ #include <sys/stat.h>
33
+ #include <fcntl.h>
34
+ #include <sys/socket.h>
35
+ #include <sys/un.h>
36
+ #include <netinet/in.h>
37
+ #include <arpa/inet.h>
38
+ #include <limits.h> /* INT_MAX, PATH_MAX */
39
+ #include <sys/uio.h> /* writev */
40
+ #include <sys/resource.h> /* getrusage */
41
+
42
+ #ifdef __linux__
43
+ # include <sys/ioctl.h>
44
+ #endif
45
+
46
+ #ifdef __sun
47
+ # include <sys/types.h>
48
+ # include <sys/wait.h>
49
+ #endif
50
+
51
+ #ifdef __APPLE__
52
+ # include <mach-o/dyld.h> /* _NSGetExecutablePath */
53
+ # include <sys/filio.h>
54
+ # include <sys/ioctl.h>
55
+ #endif
56
+
57
+ #ifdef __FreeBSD__
58
+ # include <sys/sysctl.h>
59
+ # include <sys/filio.h>
60
+ # include <sys/ioctl.h>
61
+ # include <sys/wait.h>
62
+ # define UV__O_CLOEXEC O_CLOEXEC
63
+ # if __FreeBSD__ >= 10
64
+ # define uv__accept4 accept4
65
+ # define UV__SOCK_NONBLOCK SOCK_NONBLOCK
66
+ # define UV__SOCK_CLOEXEC SOCK_CLOEXEC
67
+ # endif
68
+ # if !defined(F_DUP2FD_CLOEXEC) && defined(_F_DUP2FD_CLOEXEC)
69
+ # define F_DUP2FD_CLOEXEC _F_DUP2FD_CLOEXEC
70
+ # endif
71
+ #endif
72
+
73
+ #ifdef _AIX
74
+ #include <sys/ioctl.h>
75
+ #endif
76
+
77
+ static int uv__run_pending(uv_loop_t* loop);
78
+
79
+ /* Verify that uv_buf_t is ABI-compatible with struct iovec. */
80
+ STATIC_ASSERT(sizeof(uv_buf_t) == sizeof(struct iovec));
81
+ STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->base) ==
82
+ sizeof(((struct iovec*) 0)->iov_base));
83
+ STATIC_ASSERT(sizeof(&((uv_buf_t*) 0)->len) ==
84
+ sizeof(((struct iovec*) 0)->iov_len));
85
+ STATIC_ASSERT(offsetof(uv_buf_t, base) == offsetof(struct iovec, iov_base));
86
+ STATIC_ASSERT(offsetof(uv_buf_t, len) == offsetof(struct iovec, iov_len));
87
+
88
+
89
+ uint64_t uv_hrtime(void) {
90
+ return uv__hrtime(UV_CLOCK_PRECISE);
91
+ }
92
+
93
+
94
+ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
95
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
96
+
97
+ handle->flags |= UV_CLOSING;
98
+ handle->close_cb = close_cb;
99
+
100
+ switch (handle->type) {
101
+ case UV_NAMED_PIPE:
102
+ uv__pipe_close((uv_pipe_t*)handle);
103
+ break;
104
+
105
+ case UV_TTY:
106
+ uv__stream_close((uv_stream_t*)handle);
107
+ break;
108
+
109
+ case UV_TCP:
110
+ uv__tcp_close((uv_tcp_t*)handle);
111
+ break;
112
+
113
+ case UV_UDP:
114
+ uv__udp_close((uv_udp_t*)handle);
115
+ break;
116
+
117
+ case UV_PREPARE:
118
+ uv__prepare_close((uv_prepare_t*)handle);
119
+ break;
120
+
121
+ case UV_CHECK:
122
+ uv__check_close((uv_check_t*)handle);
123
+ break;
124
+
125
+ case UV_IDLE:
126
+ uv__idle_close((uv_idle_t*)handle);
127
+ break;
128
+
129
+ case UV_ASYNC:
130
+ uv__async_close((uv_async_t*)handle);
131
+ break;
132
+
133
+ case UV_TIMER:
134
+ uv__timer_close((uv_timer_t*)handle);
135
+ break;
136
+
137
+ case UV_PROCESS:
138
+ uv__process_close((uv_process_t*)handle);
139
+ break;
140
+
141
+ case UV_FS_EVENT:
142
+ uv__fs_event_close((uv_fs_event_t*)handle);
143
+ break;
144
+
145
+ case UV_POLL:
146
+ uv__poll_close((uv_poll_t*)handle);
147
+ break;
148
+
149
+ case UV_FS_POLL:
150
+ uv__fs_poll_close((uv_fs_poll_t*)handle);
151
+ break;
152
+
153
+ case UV_SIGNAL:
154
+ uv__signal_close((uv_signal_t*) handle);
155
+ /* Signal handles may not be closed immediately. The signal code will */
156
+ /* itself close uv__make_close_pending whenever appropriate. */
157
+ return;
158
+
159
+ default:
160
+ assert(0);
161
+ }
162
+
163
+ uv__make_close_pending(handle);
164
+ }
165
+
166
+ int uv__socket_sockopt(uv_handle_t* handle, int optname, int* value) {
167
+ int r;
168
+ int fd;
169
+ socklen_t len;
170
+
171
+ if (handle == NULL || value == NULL)
172
+ return -EINVAL;
173
+
174
+ if (handle->type == UV_TCP || handle->type == UV_NAMED_PIPE)
175
+ fd = uv__stream_fd((uv_stream_t*) handle);
176
+ else if (handle->type == UV_UDP)
177
+ fd = ((uv_udp_t *) handle)->io_watcher.fd;
178
+ else
179
+ return -ENOTSUP;
180
+
181
+ len = sizeof(*value);
182
+
183
+ if (*value == 0)
184
+ r = getsockopt(fd, SOL_SOCKET, optname, value, &len);
185
+ else
186
+ r = setsockopt(fd, SOL_SOCKET, optname, (const void*) value, len);
187
+
188
+ if (r < 0)
189
+ return -errno;
190
+
191
+ return 0;
192
+ }
193
+
194
+ void uv__make_close_pending(uv_handle_t* handle) {
195
+ assert(handle->flags & UV_CLOSING);
196
+ assert(!(handle->flags & UV_CLOSED));
197
+ handle->next_closing = handle->loop->closing_handles;
198
+ handle->loop->closing_handles = handle;
199
+ }
200
+
201
+
202
+ static void uv__finish_close(uv_handle_t* handle) {
203
+ /* Note: while the handle is in the UV_CLOSING state now, it's still possible
204
+ * for it to be active in the sense that uv__is_active() returns true.
205
+ * A good example is when the user calls uv_shutdown(), immediately followed
206
+ * by uv_close(). The handle is considered active at this point because the
207
+ * completion of the shutdown req is still pending.
208
+ */
209
+ assert(handle->flags & UV_CLOSING);
210
+ assert(!(handle->flags & UV_CLOSED));
211
+ handle->flags |= UV_CLOSED;
212
+
213
+ switch (handle->type) {
214
+ case UV_PREPARE:
215
+ case UV_CHECK:
216
+ case UV_IDLE:
217
+ case UV_ASYNC:
218
+ case UV_TIMER:
219
+ case UV_PROCESS:
220
+ case UV_FS_EVENT:
221
+ case UV_FS_POLL:
222
+ case UV_POLL:
223
+ case UV_SIGNAL:
224
+ break;
225
+
226
+ case UV_NAMED_PIPE:
227
+ case UV_TCP:
228
+ case UV_TTY:
229
+ uv__stream_destroy((uv_stream_t*)handle);
230
+ break;
231
+
232
+ case UV_UDP:
233
+ uv__udp_finish_close((uv_udp_t*)handle);
234
+ break;
235
+
236
+ default:
237
+ assert(0);
238
+ break;
239
+ }
240
+
241
+ uv__handle_unref(handle);
242
+ QUEUE_REMOVE(&handle->handle_queue);
243
+
244
+ if (handle->close_cb) {
245
+ handle->close_cb(handle);
246
+ }
247
+ }
248
+
249
+
250
+ static void uv__run_closing_handles(uv_loop_t* loop) {
251
+ uv_handle_t* p;
252
+ uv_handle_t* q;
253
+
254
+ p = loop->closing_handles;
255
+ loop->closing_handles = NULL;
256
+
257
+ while (p) {
258
+ q = p->next_closing;
259
+ uv__finish_close(p);
260
+ p = q;
261
+ }
262
+ }
263
+
264
+
265
+ int uv_is_closing(const uv_handle_t* handle) {
266
+ return uv__is_closing(handle);
267
+ }
268
+
269
+
270
+ int uv_backend_fd(const uv_loop_t* loop) {
271
+ return loop->backend_fd;
272
+ }
273
+
274
+
275
+ int uv_backend_timeout(const uv_loop_t* loop) {
276
+ if (loop->stop_flag != 0)
277
+ return 0;
278
+
279
+ if (!uv__has_active_handles(loop) && !uv__has_active_reqs(loop))
280
+ return 0;
281
+
282
+ if (!QUEUE_EMPTY(&loop->idle_handles))
283
+ return 0;
284
+
285
+ if (loop->closing_handles)
286
+ return 0;
287
+
288
+ return uv__next_timeout(loop);
289
+ }
290
+
291
+
292
+ static int uv__loop_alive(const uv_loop_t* loop) {
293
+ return uv__has_active_handles(loop) ||
294
+ uv__has_active_reqs(loop) ||
295
+ loop->closing_handles != NULL;
296
+ }
297
+
298
+
299
+ int uv_loop_alive(const uv_loop_t* loop) {
300
+ return uv__loop_alive(loop);
301
+ }
302
+
303
+
304
+ int uv_run(uv_loop_t* loop, uv_run_mode mode) {
305
+ int timeout;
306
+ int r;
307
+ int ran_pending;
308
+
309
+ r = uv__loop_alive(loop);
310
+ if (!r)
311
+ uv__update_time(loop);
312
+
313
+ while (r != 0 && loop->stop_flag == 0) {
314
+ uv__update_time(loop);
315
+ uv__run_timers(loop);
316
+ ran_pending = uv__run_pending(loop);
317
+ uv__run_idle(loop);
318
+ uv__run_prepare(loop);
319
+
320
+ timeout = 0;
321
+ if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT)
322
+ timeout = uv_backend_timeout(loop);
323
+
324
+ uv__io_poll(loop, timeout);
325
+ uv__run_check(loop);
326
+ uv__run_closing_handles(loop);
327
+
328
+ if (mode == UV_RUN_ONCE) {
329
+ /* UV_RUN_ONCE implies forward progress: at least one callback must have
330
+ * been invoked when it returns. uv__io_poll() can return without doing
331
+ * I/O (meaning: no callbacks) when its timeout expires - which means we
332
+ * have pending timers that satisfy the forward progress constraint.
333
+ *
334
+ * UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from
335
+ * the check.
336
+ */
337
+ uv__update_time(loop);
338
+ uv__run_timers(loop);
339
+ }
340
+
341
+ r = uv__loop_alive(loop);
342
+ if (mode == UV_RUN_ONCE || mode == UV_RUN_NOWAIT)
343
+ break;
344
+ }
345
+
346
+ /* The if statement lets gcc compile it to a conditional store. Avoids
347
+ * dirtying a cache line.
348
+ */
349
+ if (loop->stop_flag != 0)
350
+ loop->stop_flag = 0;
351
+
352
+ return r;
353
+ }
354
+
355
+
356
+ void uv_update_time(uv_loop_t* loop) {
357
+ uv__update_time(loop);
358
+ }
359
+
360
+
361
+ int uv_is_active(const uv_handle_t* handle) {
362
+ return uv__is_active(handle);
363
+ }
364
+
365
+
366
+ /* Open a socket in non-blocking close-on-exec mode, atomically if possible. */
367
+ int uv__socket(int domain, int type, int protocol) {
368
+ int sockfd;
369
+ int err;
370
+
371
+ #if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
372
+ sockfd = socket(domain, type | SOCK_NONBLOCK | SOCK_CLOEXEC, protocol);
373
+ if (sockfd != -1)
374
+ return sockfd;
375
+
376
+ if (errno != EINVAL)
377
+ return -errno;
378
+ #endif
379
+
380
+ sockfd = socket(domain, type, protocol);
381
+ if (sockfd == -1)
382
+ return -errno;
383
+
384
+ err = uv__nonblock(sockfd, 1);
385
+ if (err == 0)
386
+ err = uv__cloexec(sockfd, 1);
387
+
388
+ if (err) {
389
+ uv__close(sockfd);
390
+ return err;
391
+ }
392
+
393
+ #if defined(SO_NOSIGPIPE)
394
+ {
395
+ int on = 1;
396
+ setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof(on));
397
+ }
398
+ #endif
399
+
400
+ return sockfd;
401
+ }
402
+
403
+
404
+ int uv__accept(int sockfd) {
405
+ int peerfd;
406
+ int err;
407
+
408
+ assert(sockfd >= 0);
409
+
410
+ while (1) {
411
+ #if defined(__linux__) || __FreeBSD__ >= 10
412
+ static int no_accept4;
413
+
414
+ if (no_accept4)
415
+ goto skip;
416
+
417
+ peerfd = uv__accept4(sockfd,
418
+ NULL,
419
+ NULL,
420
+ UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
421
+ if (peerfd != -1)
422
+ return peerfd;
423
+
424
+ if (errno == EINTR)
425
+ continue;
426
+
427
+ if (errno != ENOSYS)
428
+ return -errno;
429
+
430
+ no_accept4 = 1;
431
+ skip:
432
+ #endif
433
+
434
+ peerfd = accept(sockfd, NULL, NULL);
435
+ if (peerfd == -1) {
436
+ if (errno == EINTR)
437
+ continue;
438
+ return -errno;
439
+ }
440
+
441
+ err = uv__cloexec(peerfd, 1);
442
+ if (err == 0)
443
+ err = uv__nonblock(peerfd, 1);
444
+
445
+ if (err) {
446
+ uv__close(peerfd);
447
+ return err;
448
+ }
449
+
450
+ return peerfd;
451
+ }
452
+ }
453
+
454
+
455
+ int uv__close(int fd) {
456
+ int saved_errno;
457
+ int rc;
458
+
459
+ assert(fd > -1); /* Catch uninitialized io_watcher.fd bugs. */
460
+ assert(fd > STDERR_FILENO); /* Catch stdio close bugs. */
461
+
462
+ saved_errno = errno;
463
+ rc = close(fd);
464
+ if (rc == -1) {
465
+ rc = -errno;
466
+ if (rc == -EINTR)
467
+ rc = -EINPROGRESS; /* For platform/libc consistency. */
468
+ errno = saved_errno;
469
+ }
470
+
471
+ return rc;
472
+ }
473
+
474
+
475
+ #if defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) || \
476
+ defined(_AIX)
477
+
478
+ int uv__nonblock(int fd, int set) {
479
+ int r;
480
+
481
+ do
482
+ r = ioctl(fd, FIONBIO, &set);
483
+ while (r == -1 && errno == EINTR);
484
+
485
+ if (r)
486
+ return -errno;
487
+
488
+ return 0;
489
+ }
490
+
491
+
492
+ int uv__cloexec(int fd, int set) {
493
+ int r;
494
+
495
+ do
496
+ r = ioctl(fd, set ? FIOCLEX : FIONCLEX);
497
+ while (r == -1 && errno == EINTR);
498
+
499
+ if (r)
500
+ return -errno;
501
+
502
+ return 0;
503
+ }
504
+
505
+ #else /* !(defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__)) */
506
+
507
+ int uv__nonblock(int fd, int set) {
508
+ int flags;
509
+ int r;
510
+
511
+ do
512
+ r = fcntl(fd, F_GETFL);
513
+ while (r == -1 && errno == EINTR);
514
+
515
+ if (r == -1)
516
+ return -errno;
517
+
518
+ /* Bail out now if already set/clear. */
519
+ if (!!(r & O_NONBLOCK) == !!set)
520
+ return 0;
521
+
522
+ if (set)
523
+ flags = r | O_NONBLOCK;
524
+ else
525
+ flags = r & ~O_NONBLOCK;
526
+
527
+ do
528
+ r = fcntl(fd, F_SETFL, flags);
529
+ while (r == -1 && errno == EINTR);
530
+
531
+ if (r)
532
+ return -errno;
533
+
534
+ return 0;
535
+ }
536
+
537
+
538
+ int uv__cloexec(int fd, int set) {
539
+ int flags;
540
+ int r;
541
+
542
+ do
543
+ r = fcntl(fd, F_GETFD);
544
+ while (r == -1 && errno == EINTR);
545
+
546
+ if (r == -1)
547
+ return -errno;
548
+
549
+ /* Bail out now if already set/clear. */
550
+ if (!!(r & FD_CLOEXEC) == !!set)
551
+ return 0;
552
+
553
+ if (set)
554
+ flags = r | FD_CLOEXEC;
555
+ else
556
+ flags = r & ~FD_CLOEXEC;
557
+
558
+ do
559
+ r = fcntl(fd, F_SETFD, flags);
560
+ while (r == -1 && errno == EINTR);
561
+
562
+ if (r)
563
+ return -errno;
564
+
565
+ return 0;
566
+ }
567
+
568
+ #endif /* defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__) */
569
+
570
+
571
+ /* This function is not execve-safe, there is a race window
572
+ * between the call to dup() and fcntl(FD_CLOEXEC).
573
+ */
574
+ int uv__dup(int fd) {
575
+ int err;
576
+
577
+ fd = dup(fd);
578
+
579
+ if (fd == -1)
580
+ return -errno;
581
+
582
+ err = uv__cloexec(fd, 1);
583
+ if (err) {
584
+ uv__close(fd);
585
+ return err;
586
+ }
587
+
588
+ return fd;
589
+ }
590
+
591
+
592
+ ssize_t uv__recvmsg(int fd, struct msghdr* msg, int flags) {
593
+ struct cmsghdr* cmsg;
594
+ ssize_t rc;
595
+ int* pfd;
596
+ int* end;
597
+ #if defined(__linux__)
598
+ static int no_msg_cmsg_cloexec;
599
+ if (no_msg_cmsg_cloexec == 0) {
600
+ rc = recvmsg(fd, msg, flags | 0x40000000); /* MSG_CMSG_CLOEXEC */
601
+ if (rc != -1)
602
+ return rc;
603
+ if (errno != EINVAL)
604
+ return -errno;
605
+ rc = recvmsg(fd, msg, flags);
606
+ if (rc == -1)
607
+ return -errno;
608
+ no_msg_cmsg_cloexec = 1;
609
+ } else {
610
+ rc = recvmsg(fd, msg, flags);
611
+ }
612
+ #else
613
+ rc = recvmsg(fd, msg, flags);
614
+ #endif
615
+ if (rc == -1)
616
+ return -errno;
617
+ if (msg->msg_controllen == 0)
618
+ return rc;
619
+ for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL; cmsg = CMSG_NXTHDR(msg, cmsg))
620
+ if (cmsg->cmsg_type == SCM_RIGHTS)
621
+ for (pfd = (int*) CMSG_DATA(cmsg),
622
+ end = (int*) ((char*) cmsg + cmsg->cmsg_len);
623
+ pfd < end;
624
+ pfd += 1)
625
+ uv__cloexec(*pfd, 1);
626
+ return rc;
627
+ }
628
+
629
+
630
+ int uv_cwd(char* buffer, size_t* size) {
631
+ if (buffer == NULL || size == NULL)
632
+ return -EINVAL;
633
+
634
+ if (getcwd(buffer, *size) == NULL)
635
+ return -errno;
636
+
637
+ *size = strlen(buffer);
638
+ if (*size > 1 && buffer[*size - 1] == '/') {
639
+ buffer[*size-1] = '\0';
640
+ (*size)--;
641
+ }
642
+
643
+ return 0;
644
+ }
645
+
646
+
647
+ int uv_chdir(const char* dir) {
648
+ if (chdir(dir))
649
+ return -errno;
650
+
651
+ return 0;
652
+ }
653
+
654
+
655
+ void uv_disable_stdio_inheritance(void) {
656
+ int fd;
657
+
658
+ /* Set the CLOEXEC flag on all open descriptors. Unconditionally try the
659
+ * first 16 file descriptors. After that, bail out after the first error.
660
+ */
661
+ for (fd = 0; ; fd++)
662
+ if (uv__cloexec(fd, 1) && fd > 15)
663
+ break;
664
+ }
665
+
666
+
667
+ int uv_fileno(const uv_handle_t* handle, uv_os_fd_t* fd) {
668
+ int fd_out;
669
+
670
+ switch (handle->type) {
671
+ case UV_TCP:
672
+ case UV_NAMED_PIPE:
673
+ case UV_TTY:
674
+ fd_out = uv__stream_fd((uv_stream_t*) handle);
675
+ break;
676
+
677
+ case UV_UDP:
678
+ fd_out = ((uv_udp_t *) handle)->io_watcher.fd;
679
+ break;
680
+
681
+ case UV_POLL:
682
+ fd_out = ((uv_poll_t *) handle)->io_watcher.fd;
683
+ break;
684
+
685
+ default:
686
+ return -EINVAL;
687
+ }
688
+
689
+ if (uv__is_closing(handle) || fd_out == -1)
690
+ return -EBADF;
691
+
692
+ *fd = fd_out;
693
+ return 0;
694
+ }
695
+
696
+
697
+ static int uv__run_pending(uv_loop_t* loop) {
698
+ QUEUE* q;
699
+ uv__io_t* w;
700
+
701
+ if (QUEUE_EMPTY(&loop->pending_queue))
702
+ return 0;
703
+
704
+ while (!QUEUE_EMPTY(&loop->pending_queue)) {
705
+ q = QUEUE_HEAD(&loop->pending_queue);
706
+ QUEUE_REMOVE(q);
707
+ QUEUE_INIT(q);
708
+
709
+ w = QUEUE_DATA(q, uv__io_t, pending_queue);
710
+ w->cb(loop, w, UV__POLLOUT);
711
+ }
712
+
713
+ return 1;
714
+ }
715
+
716
+
717
+ static unsigned int next_power_of_two(unsigned int val) {
718
+ val -= 1;
719
+ val |= val >> 1;
720
+ val |= val >> 2;
721
+ val |= val >> 4;
722
+ val |= val >> 8;
723
+ val |= val >> 16;
724
+ val += 1;
725
+ return val;
726
+ }
727
+
728
+ static void maybe_resize(uv_loop_t* loop, unsigned int len) {
729
+ uv__io_t** watchers;
730
+ void* fake_watcher_list;
731
+ void* fake_watcher_count;
732
+ unsigned int nwatchers;
733
+ unsigned int i;
734
+
735
+ if (len <= loop->nwatchers)
736
+ return;
737
+
738
+ /* Preserve fake watcher list and count at the end of the watchers */
739
+ if (loop->watchers != NULL) {
740
+ fake_watcher_list = loop->watchers[loop->nwatchers];
741
+ fake_watcher_count = loop->watchers[loop->nwatchers + 1];
742
+ } else {
743
+ fake_watcher_list = NULL;
744
+ fake_watcher_count = NULL;
745
+ }
746
+
747
+ nwatchers = next_power_of_two(len + 2) - 2;
748
+ watchers = realloc(loop->watchers,
749
+ (nwatchers + 2) * sizeof(loop->watchers[0]));
750
+
751
+ if (watchers == NULL)
752
+ abort();
753
+ for (i = loop->nwatchers; i < nwatchers; i++)
754
+ watchers[i] = NULL;
755
+ watchers[nwatchers] = fake_watcher_list;
756
+ watchers[nwatchers + 1] = fake_watcher_count;
757
+
758
+ loop->watchers = watchers;
759
+ loop->nwatchers = nwatchers;
760
+ }
761
+
762
+
763
+ void uv__io_init(uv__io_t* w, uv__io_cb cb, int fd) {
764
+ assert(cb != NULL);
765
+ assert(fd >= -1);
766
+ QUEUE_INIT(&w->pending_queue);
767
+ QUEUE_INIT(&w->watcher_queue);
768
+ w->cb = cb;
769
+ w->fd = fd;
770
+ w->events = 0;
771
+ w->pevents = 0;
772
+
773
+ #if defined(UV_HAVE_KQUEUE)
774
+ w->rcount = 0;
775
+ w->wcount = 0;
776
+ #endif /* defined(UV_HAVE_KQUEUE) */
777
+ }
778
+
779
+
780
+ void uv__io_start(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
781
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
782
+ assert(0 != events);
783
+ assert(w->fd >= 0);
784
+ assert(w->fd < INT_MAX);
785
+
786
+ w->pevents |= events;
787
+ maybe_resize(loop, w->fd + 1);
788
+
789
+ #if !defined(__sun)
790
+ /* The event ports backend needs to rearm all file descriptors on each and
791
+ * every tick of the event loop but the other backends allow us to
792
+ * short-circuit here if the event mask is unchanged.
793
+ */
794
+ if (w->events == w->pevents) {
795
+ if (w->events == 0 && !QUEUE_EMPTY(&w->watcher_queue)) {
796
+ QUEUE_REMOVE(&w->watcher_queue);
797
+ QUEUE_INIT(&w->watcher_queue);
798
+ }
799
+ return;
800
+ }
801
+ #endif
802
+
803
+ if (QUEUE_EMPTY(&w->watcher_queue))
804
+ QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
805
+
806
+ if (loop->watchers[w->fd] == NULL) {
807
+ loop->watchers[w->fd] = w;
808
+ loop->nfds++;
809
+ }
810
+ }
811
+
812
+
813
+ void uv__io_stop(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
814
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
815
+ assert(0 != events);
816
+
817
+ if (w->fd == -1)
818
+ return;
819
+
820
+ assert(w->fd >= 0);
821
+
822
+ /* Happens when uv__io_stop() is called on a handle that was never started. */
823
+ if ((unsigned) w->fd >= loop->nwatchers)
824
+ return;
825
+
826
+ w->pevents &= ~events;
827
+
828
+ if (w->pevents == 0) {
829
+ QUEUE_REMOVE(&w->watcher_queue);
830
+ QUEUE_INIT(&w->watcher_queue);
831
+
832
+ if (loop->watchers[w->fd] != NULL) {
833
+ assert(loop->watchers[w->fd] == w);
834
+ assert(loop->nfds > 0);
835
+ loop->watchers[w->fd] = NULL;
836
+ loop->nfds--;
837
+ w->events = 0;
838
+ }
839
+ }
840
+ else if (QUEUE_EMPTY(&w->watcher_queue))
841
+ QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
842
+ }
843
+
844
+
845
+ void uv__io_close(uv_loop_t* loop, uv__io_t* w) {
846
+ uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT);
847
+ QUEUE_REMOVE(&w->pending_queue);
848
+
849
+ /* Remove stale events for this file descriptor */
850
+ uv__platform_invalidate_fd(loop, w->fd);
851
+ }
852
+
853
+
854
+ void uv__io_feed(uv_loop_t* loop, uv__io_t* w) {
855
+ if (QUEUE_EMPTY(&w->pending_queue))
856
+ QUEUE_INSERT_TAIL(&loop->pending_queue, &w->pending_queue);
857
+ }
858
+
859
+
860
+ int uv__io_active(const uv__io_t* w, unsigned int events) {
861
+ assert(0 == (events & ~(UV__POLLIN | UV__POLLOUT)));
862
+ assert(0 != events);
863
+ return 0 != (w->pevents & events);
864
+ }
865
+
866
+
867
+ int uv_getrusage(uv_rusage_t* rusage) {
868
+ struct rusage usage;
869
+
870
+ if (getrusage(RUSAGE_SELF, &usage))
871
+ return -errno;
872
+
873
+ rusage->ru_utime.tv_sec = usage.ru_utime.tv_sec;
874
+ rusage->ru_utime.tv_usec = usage.ru_utime.tv_usec;
875
+
876
+ rusage->ru_stime.tv_sec = usage.ru_stime.tv_sec;
877
+ rusage->ru_stime.tv_usec = usage.ru_stime.tv_usec;
878
+
879
+ rusage->ru_maxrss = usage.ru_maxrss;
880
+ rusage->ru_ixrss = usage.ru_ixrss;
881
+ rusage->ru_idrss = usage.ru_idrss;
882
+ rusage->ru_isrss = usage.ru_isrss;
883
+ rusage->ru_minflt = usage.ru_minflt;
884
+ rusage->ru_majflt = usage.ru_majflt;
885
+ rusage->ru_nswap = usage.ru_nswap;
886
+ rusage->ru_inblock = usage.ru_inblock;
887
+ rusage->ru_oublock = usage.ru_oublock;
888
+ rusage->ru_msgsnd = usage.ru_msgsnd;
889
+ rusage->ru_msgrcv = usage.ru_msgrcv;
890
+ rusage->ru_nsignals = usage.ru_nsignals;
891
+ rusage->ru_nvcsw = usage.ru_nvcsw;
892
+ rusage->ru_nivcsw = usage.ru_nivcsw;
893
+
894
+ return 0;
895
+ }
896
+
897
+
898
+ int uv__open_cloexec(const char* path, int flags) {
899
+ int err;
900
+ int fd;
901
+
902
+ #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD__ >= 9)
903
+ static int no_cloexec;
904
+
905
+ if (!no_cloexec) {
906
+ fd = open(path, flags | UV__O_CLOEXEC);
907
+ if (fd != -1)
908
+ return fd;
909
+
910
+ if (errno != EINVAL)
911
+ return -errno;
912
+
913
+ /* O_CLOEXEC not supported. */
914
+ no_cloexec = 1;
915
+ }
916
+ #endif
917
+
918
+ fd = open(path, flags);
919
+ if (fd == -1)
920
+ return -errno;
921
+
922
+ err = uv__cloexec(fd, 1);
923
+ if (err) {
924
+ uv__close(fd);
925
+ return err;
926
+ }
927
+
928
+ return fd;
929
+ }
930
+
931
+
932
+ int uv__dup2_cloexec(int oldfd, int newfd) {
933
+ int r;
934
+ #if defined(__FreeBSD__) && __FreeBSD__ >= 10
935
+ do
936
+ r = dup3(oldfd, newfd, O_CLOEXEC);
937
+ while (r == -1 && errno == EINTR);
938
+ if (r == -1)
939
+ return -errno;
940
+ return r;
941
+ #elif defined(__FreeBSD__) && defined(F_DUP2FD_CLOEXEC)
942
+ do
943
+ r = fcntl(oldfd, F_DUP2FD_CLOEXEC, newfd);
944
+ while (r == -1 && errno == EINTR);
945
+ if (r != -1)
946
+ return r;
947
+ if (errno != EINVAL)
948
+ return -errno;
949
+ /* Fall through. */
950
+ #elif defined(__linux__)
951
+ static int no_dup3;
952
+ if (!no_dup3) {
953
+ do
954
+ r = uv__dup3(oldfd, newfd, UV__O_CLOEXEC);
955
+ while (r == -1 && (errno == EINTR || errno == EBUSY));
956
+ if (r != -1)
957
+ return r;
958
+ if (errno != ENOSYS)
959
+ return -errno;
960
+ /* Fall through. */
961
+ no_dup3 = 1;
962
+ }
963
+ #endif
964
+ {
965
+ int err;
966
+ do
967
+ r = dup2(oldfd, newfd);
968
+ #if defined(__linux__)
969
+ while (r == -1 && (errno == EINTR || errno == EBUSY));
970
+ #else
971
+ while (r == -1 && errno == EINTR);
972
+ #endif
973
+
974
+ if (r == -1)
975
+ return -errno;
976
+
977
+ err = uv__cloexec(newfd, 1);
978
+ if (err) {
979
+ uv__close(newfd);
980
+ return err;
981
+ }
982
+
983
+ return r;
984
+ }
985
+ }