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,113 @@
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
+ #include "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <unistd.h>
26
+ #include <assert.h>
27
+ #include <errno.h>
28
+
29
+
30
+ static void uv__poll_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
31
+ uv_poll_t* handle;
32
+ int pevents;
33
+
34
+ handle = container_of(w, uv_poll_t, io_watcher);
35
+
36
+ if (events & UV__POLLERR) {
37
+ uv__io_stop(loop, w, UV__POLLIN | UV__POLLOUT);
38
+ uv__handle_stop(handle);
39
+ handle->poll_cb(handle, -EBADF, 0);
40
+ return;
41
+ }
42
+
43
+ pevents = 0;
44
+ if (events & UV__POLLIN)
45
+ pevents |= UV_READABLE;
46
+ if (events & UV__POLLOUT)
47
+ pevents |= UV_WRITABLE;
48
+
49
+ handle->poll_cb(handle, 0, pevents);
50
+ }
51
+
52
+
53
+ int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
54
+ int err;
55
+
56
+ err = uv__nonblock(fd, 1);
57
+ if (err)
58
+ return err;
59
+
60
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
61
+ uv__io_init(&handle->io_watcher, uv__poll_io, fd);
62
+ handle->poll_cb = NULL;
63
+ return 0;
64
+ }
65
+
66
+
67
+ int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
68
+ uv_os_sock_t socket) {
69
+ return uv_poll_init(loop, handle, socket);
70
+ }
71
+
72
+
73
+ static void uv__poll_stop(uv_poll_t* handle) {
74
+ uv__io_stop(handle->loop, &handle->io_watcher, UV__POLLIN | UV__POLLOUT);
75
+ uv__handle_stop(handle);
76
+ }
77
+
78
+
79
+ int uv_poll_stop(uv_poll_t* handle) {
80
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
81
+ uv__poll_stop(handle);
82
+ return 0;
83
+ }
84
+
85
+
86
+ int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
87
+ int events;
88
+
89
+ assert((pevents & ~(UV_READABLE | UV_WRITABLE)) == 0);
90
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
91
+
92
+ uv__poll_stop(handle);
93
+
94
+ if (pevents == 0)
95
+ return 0;
96
+
97
+ events = 0;
98
+ if (pevents & UV_READABLE)
99
+ events |= UV__POLLIN;
100
+ if (pevents & UV_WRITABLE)
101
+ events |= UV__POLLOUT;
102
+
103
+ uv__io_start(handle->loop, &handle->io_watcher, events);
104
+ uv__handle_start(handle);
105
+ handle->poll_cb = poll_cb;
106
+
107
+ return 0;
108
+ }
109
+
110
+
111
+ void uv__poll_close(uv_poll_t* handle) {
112
+ uv__poll_stop(handle);
113
+ }
@@ -0,0 +1,551 @@
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
+ #include "uv.h"
23
+ #include "internal.h"
24
+
25
+ #include <stdio.h>
26
+ #include <stdlib.h>
27
+ #include <assert.h>
28
+ #include <errno.h>
29
+
30
+ #include <sys/types.h>
31
+ #include <sys/wait.h>
32
+ #include <unistd.h>
33
+ #include <fcntl.h>
34
+ #include <poll.h>
35
+
36
+ #if defined(__APPLE__) && !TARGET_OS_IPHONE
37
+ # include <crt_externs.h>
38
+ # define environ (*_NSGetEnviron())
39
+ #else
40
+ extern char **environ;
41
+ #endif
42
+
43
+ #ifdef __linux__
44
+ # include <grp.h>
45
+ #endif
46
+
47
+
48
+ static void uv__chld(uv_signal_t* handle, int signum) {
49
+ uv_process_t* process;
50
+ uv_loop_t* loop;
51
+ int exit_status;
52
+ int term_signal;
53
+ int status;
54
+ pid_t pid;
55
+ QUEUE pending;
56
+ QUEUE* q;
57
+ QUEUE* h;
58
+
59
+ assert(signum == SIGCHLD);
60
+
61
+ QUEUE_INIT(&pending);
62
+ loop = handle->loop;
63
+
64
+ h = &loop->process_handles;
65
+ q = QUEUE_HEAD(h);
66
+ while (q != h) {
67
+ process = QUEUE_DATA(q, uv_process_t, queue);
68
+ q = QUEUE_NEXT(q);
69
+
70
+ do
71
+ pid = waitpid(process->pid, &status, WNOHANG);
72
+ while (pid == -1 && errno == EINTR);
73
+
74
+ if (pid == 0)
75
+ continue;
76
+
77
+ if (pid == -1) {
78
+ if (errno != ECHILD)
79
+ abort();
80
+ continue;
81
+ }
82
+
83
+ process->status = status;
84
+ QUEUE_REMOVE(&process->queue);
85
+ QUEUE_INSERT_TAIL(&pending, &process->queue);
86
+ }
87
+
88
+ h = &pending;
89
+ q = QUEUE_HEAD(h);
90
+ while (q != h) {
91
+ process = QUEUE_DATA(q, uv_process_t, queue);
92
+ q = QUEUE_NEXT(q);
93
+
94
+ QUEUE_REMOVE(&process->queue);
95
+ QUEUE_INIT(&process->queue);
96
+ uv__handle_stop(process);
97
+
98
+ if (process->exit_cb == NULL)
99
+ continue;
100
+
101
+ exit_status = 0;
102
+ if (WIFEXITED(process->status))
103
+ exit_status = WEXITSTATUS(process->status);
104
+
105
+ term_signal = 0;
106
+ if (WIFSIGNALED(process->status))
107
+ term_signal = WTERMSIG(process->status);
108
+
109
+ process->exit_cb(process, exit_status, term_signal);
110
+ }
111
+ assert(QUEUE_EMPTY(&pending));
112
+ }
113
+
114
+
115
+ int uv__make_socketpair(int fds[2], int flags) {
116
+ #if defined(__linux__)
117
+ static int no_cloexec;
118
+
119
+ if (no_cloexec)
120
+ goto skip;
121
+
122
+ if (socketpair(AF_UNIX, SOCK_STREAM | UV__SOCK_CLOEXEC | flags, 0, fds) == 0)
123
+ return 0;
124
+
125
+ /* Retry on EINVAL, it means SOCK_CLOEXEC is not supported.
126
+ * Anything else is a genuine error.
127
+ */
128
+ if (errno != EINVAL)
129
+ return -errno;
130
+
131
+ no_cloexec = 1;
132
+
133
+ skip:
134
+ #endif
135
+
136
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
137
+ return -errno;
138
+
139
+ uv__cloexec(fds[0], 1);
140
+ uv__cloexec(fds[1], 1);
141
+
142
+ if (flags & UV__F_NONBLOCK) {
143
+ uv__nonblock(fds[0], 1);
144
+ uv__nonblock(fds[1], 1);
145
+ }
146
+
147
+ return 0;
148
+ }
149
+
150
+
151
+ int uv__make_pipe(int fds[2], int flags) {
152
+ #if defined(__linux__)
153
+ static int no_pipe2;
154
+
155
+ if (no_pipe2)
156
+ goto skip;
157
+
158
+ if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0)
159
+ return 0;
160
+
161
+ if (errno != ENOSYS)
162
+ return -errno;
163
+
164
+ no_pipe2 = 1;
165
+
166
+ skip:
167
+ #endif
168
+
169
+ if (pipe(fds))
170
+ return -errno;
171
+
172
+ uv__cloexec(fds[0], 1);
173
+ uv__cloexec(fds[1], 1);
174
+
175
+ if (flags & UV__F_NONBLOCK) {
176
+ uv__nonblock(fds[0], 1);
177
+ uv__nonblock(fds[1], 1);
178
+ }
179
+
180
+ return 0;
181
+ }
182
+
183
+
184
+ /*
185
+ * Used for initializing stdio streams like options.stdin_stream. Returns
186
+ * zero on success. See also the cleanup section in uv_spawn().
187
+ */
188
+ static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) {
189
+ int mask;
190
+ int fd;
191
+
192
+ mask = UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD | UV_INHERIT_STREAM;
193
+
194
+ switch (container->flags & mask) {
195
+ case UV_IGNORE:
196
+ return 0;
197
+
198
+ case UV_CREATE_PIPE:
199
+ assert(container->data.stream != NULL);
200
+ if (container->data.stream->type != UV_NAMED_PIPE)
201
+ return -EINVAL;
202
+ else
203
+ return uv__make_socketpair(fds, 0);
204
+
205
+ case UV_INHERIT_FD:
206
+ case UV_INHERIT_STREAM:
207
+ if (container->flags & UV_INHERIT_FD)
208
+ fd = container->data.fd;
209
+ else
210
+ fd = uv__stream_fd(container->data.stream);
211
+
212
+ if (fd == -1)
213
+ return -EINVAL;
214
+
215
+ fds[1] = fd;
216
+ return 0;
217
+
218
+ default:
219
+ assert(0 && "Unexpected flags");
220
+ return -EINVAL;
221
+ }
222
+ }
223
+
224
+
225
+ static int uv__process_open_stream(uv_stdio_container_t* container,
226
+ int pipefds[2],
227
+ int writable) {
228
+ int flags;
229
+
230
+ if (!(container->flags & UV_CREATE_PIPE) || pipefds[0] < 0)
231
+ return 0;
232
+
233
+ if (uv__close(pipefds[1]))
234
+ if (errno != EINTR && errno != EINPROGRESS)
235
+ abort();
236
+
237
+ pipefds[1] = -1;
238
+ uv__nonblock(pipefds[0], 1);
239
+
240
+ if (container->data.stream->type == UV_NAMED_PIPE &&
241
+ ((uv_pipe_t*)container->data.stream)->ipc)
242
+ flags = UV_STREAM_READABLE | UV_STREAM_WRITABLE;
243
+ else if (writable)
244
+ flags = UV_STREAM_WRITABLE;
245
+ else
246
+ flags = UV_STREAM_READABLE;
247
+
248
+ return uv__stream_open(container->data.stream, pipefds[0], flags);
249
+ }
250
+
251
+
252
+ static void uv__process_close_stream(uv_stdio_container_t* container) {
253
+ if (!(container->flags & UV_CREATE_PIPE)) return;
254
+ uv__stream_close((uv_stream_t*)container->data.stream);
255
+ }
256
+
257
+
258
+ static void uv__write_int(int fd, int val) {
259
+ ssize_t n;
260
+
261
+ do
262
+ n = write(fd, &val, sizeof(val));
263
+ while (n == -1 && errno == EINTR);
264
+
265
+ if (n == -1 && errno == EPIPE)
266
+ return; /* parent process has quit */
267
+
268
+ assert(n == sizeof(val));
269
+ }
270
+
271
+
272
+ static void uv__process_child_init(const uv_process_options_t* options,
273
+ int stdio_count,
274
+ int (*pipes)[2],
275
+ int error_fd) {
276
+ int close_fd;
277
+ int use_fd;
278
+ int fd;
279
+
280
+ if (options->flags & UV_PROCESS_DETACHED)
281
+ setsid();
282
+
283
+ /* First duplicate low numbered fds, since it's not safe to duplicate them,
284
+ * they could get replaced. Example: swapping stdout and stderr; without
285
+ * this fd 2 (stderr) would be duplicated into fd 1, thus making both
286
+ * stdout and stderr go to the same fd, which was not the intention. */
287
+ for (fd = 0; fd < stdio_count; fd++) {
288
+ use_fd = pipes[fd][1];
289
+ if (use_fd < 0 || use_fd >= fd)
290
+ continue;
291
+ pipes[fd][1] = fcntl(use_fd, F_DUPFD, stdio_count);
292
+ if (pipes[fd][1] == -1) {
293
+ uv__write_int(error_fd, -errno);
294
+ _exit(127);
295
+ }
296
+ }
297
+
298
+ for (fd = 0; fd < stdio_count; fd++) {
299
+ close_fd = pipes[fd][0];
300
+ use_fd = pipes[fd][1];
301
+
302
+ if (use_fd < 0) {
303
+ if (fd >= 3)
304
+ continue;
305
+ else {
306
+ /* redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
307
+ * set
308
+ */
309
+ use_fd = open("/dev/null", fd == 0 ? O_RDONLY : O_RDWR);
310
+ close_fd = use_fd;
311
+
312
+ if (use_fd == -1) {
313
+ uv__write_int(error_fd, -errno);
314
+ _exit(127);
315
+ }
316
+ }
317
+ }
318
+
319
+ if (fd == use_fd)
320
+ uv__cloexec(use_fd, 0);
321
+ else
322
+ fd = dup2(use_fd, fd);
323
+
324
+ if (fd == -1) {
325
+ uv__write_int(error_fd, -errno);
326
+ _exit(127);
327
+ }
328
+
329
+ if (fd <= 2)
330
+ uv__nonblock(fd, 0);
331
+
332
+ if (close_fd >= stdio_count)
333
+ uv__close(close_fd);
334
+ }
335
+
336
+ for (fd = 0; fd < stdio_count; fd++) {
337
+ use_fd = pipes[fd][1];
338
+
339
+ if (use_fd >= stdio_count)
340
+ uv__close(use_fd);
341
+ }
342
+
343
+ if (options->cwd != NULL && chdir(options->cwd)) {
344
+ uv__write_int(error_fd, -errno);
345
+ _exit(127);
346
+ }
347
+
348
+ if (options->flags & (UV_PROCESS_SETUID | UV_PROCESS_SETGID)) {
349
+ /* When dropping privileges from root, the `setgroups` call will
350
+ * remove any extraneous groups. If we don't call this, then
351
+ * even though our uid has dropped, we may still have groups
352
+ * that enable us to do super-user things. This will fail if we
353
+ * aren't root, so don't bother checking the return value, this
354
+ * is just done as an optimistic privilege dropping function.
355
+ */
356
+ SAVE_ERRNO(setgroups(0, NULL));
357
+ }
358
+
359
+ if ((options->flags & UV_PROCESS_SETGID) && setgid(options->gid)) {
360
+ uv__write_int(error_fd, -errno);
361
+ _exit(127);
362
+ }
363
+
364
+ if ((options->flags & UV_PROCESS_SETUID) && setuid(options->uid)) {
365
+ uv__write_int(error_fd, -errno);
366
+ _exit(127);
367
+ }
368
+
369
+ if (options->env != NULL) {
370
+ environ = options->env;
371
+ }
372
+
373
+ execvp(options->file, options->args);
374
+ uv__write_int(error_fd, -errno);
375
+ _exit(127);
376
+ }
377
+
378
+
379
+ int uv_spawn(uv_loop_t* loop,
380
+ uv_process_t* process,
381
+ const uv_process_options_t* options) {
382
+ int signal_pipe[2] = { -1, -1 };
383
+ int (*pipes)[2];
384
+ int stdio_count;
385
+ ssize_t r;
386
+ pid_t pid;
387
+ int err;
388
+ int exec_errorno;
389
+ int i;
390
+ int status;
391
+
392
+ assert(options->file != NULL);
393
+ assert(!(options->flags & ~(UV_PROCESS_DETACHED |
394
+ UV_PROCESS_SETGID |
395
+ UV_PROCESS_SETUID |
396
+ UV_PROCESS_WINDOWS_HIDE |
397
+ UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS)));
398
+
399
+ uv__handle_init(loop, (uv_handle_t*)process, UV_PROCESS);
400
+ QUEUE_INIT(&process->queue);
401
+
402
+ stdio_count = options->stdio_count;
403
+ if (stdio_count < 3)
404
+ stdio_count = 3;
405
+
406
+ err = -ENOMEM;
407
+ pipes = malloc(stdio_count * sizeof(*pipes));
408
+ if (pipes == NULL)
409
+ goto error;
410
+
411
+ for (i = 0; i < stdio_count; i++) {
412
+ pipes[i][0] = -1;
413
+ pipes[i][1] = -1;
414
+ }
415
+
416
+ for (i = 0; i < options->stdio_count; i++) {
417
+ err = uv__process_init_stdio(options->stdio + i, pipes[i]);
418
+ if (err)
419
+ goto error;
420
+ }
421
+
422
+ /* This pipe is used by the parent to wait until
423
+ * the child has called `execve()`. We need this
424
+ * to avoid the following race condition:
425
+ *
426
+ * if ((pid = fork()) > 0) {
427
+ * kill(pid, SIGTERM);
428
+ * }
429
+ * else if (pid == 0) {
430
+ * execve("/bin/cat", argp, envp);
431
+ * }
432
+ *
433
+ * The parent sends a signal immediately after forking.
434
+ * Since the child may not have called `execve()` yet,
435
+ * there is no telling what process receives the signal,
436
+ * our fork or /bin/cat.
437
+ *
438
+ * To avoid ambiguity, we create a pipe with both ends
439
+ * marked close-on-exec. Then, after the call to `fork()`,
440
+ * the parent polls the read end until it EOFs or errors with EPIPE.
441
+ */
442
+ err = uv__make_pipe(signal_pipe, 0);
443
+ if (err)
444
+ goto error;
445
+
446
+ uv_signal_start(&loop->child_watcher, uv__chld, SIGCHLD);
447
+
448
+ /* Acquire write lock to prevent opening new fds in worker threads */
449
+ uv_rwlock_wrlock(&loop->cloexec_lock);
450
+ pid = fork();
451
+
452
+ if (pid == -1) {
453
+ err = -errno;
454
+ uv_rwlock_wrunlock(&loop->cloexec_lock);
455
+ uv__close(signal_pipe[0]);
456
+ uv__close(signal_pipe[1]);
457
+ goto error;
458
+ }
459
+
460
+ if (pid == 0) {
461
+ uv__process_child_init(options, stdio_count, pipes, signal_pipe[1]);
462
+ abort();
463
+ }
464
+
465
+ /* Release lock in parent process */
466
+ uv_rwlock_wrunlock(&loop->cloexec_lock);
467
+ uv__close(signal_pipe[1]);
468
+
469
+ process->status = 0;
470
+ exec_errorno = 0;
471
+ do
472
+ r = read(signal_pipe[0], &exec_errorno, sizeof(exec_errorno));
473
+ while (r == -1 && errno == EINTR);
474
+
475
+ if (r == 0)
476
+ ; /* okay, EOF */
477
+ else if (r == sizeof(exec_errorno)) {
478
+ do
479
+ err = waitpid(pid, &status, 0); /* okay, read errorno */
480
+ while (err == -1 && errno == EINTR);
481
+ assert(err == pid);
482
+ } else if (r == -1 && errno == EPIPE) {
483
+ do
484
+ err = waitpid(pid, &status, 0); /* okay, got EPIPE */
485
+ while (err == -1 && errno == EINTR);
486
+ assert(err == pid);
487
+ } else
488
+ abort();
489
+
490
+ uv__close(signal_pipe[0]);
491
+
492
+ for (i = 0; i < options->stdio_count; i++) {
493
+ err = uv__process_open_stream(options->stdio + i, pipes[i], i == 0);
494
+ if (err == 0)
495
+ continue;
496
+
497
+ while (i--)
498
+ uv__process_close_stream(options->stdio + i);
499
+
500
+ goto error;
501
+ }
502
+
503
+ /* Only activate this handle if exec() happened successfully */
504
+ if (exec_errorno == 0) {
505
+ QUEUE_INSERT_TAIL(&loop->process_handles, &process->queue);
506
+ uv__handle_start(process);
507
+ }
508
+
509
+ process->pid = pid;
510
+ process->exit_cb = options->exit_cb;
511
+
512
+ free(pipes);
513
+ return exec_errorno;
514
+
515
+ error:
516
+ if (pipes != NULL) {
517
+ for (i = 0; i < stdio_count; i++) {
518
+ if (i < options->stdio_count)
519
+ if (options->stdio[i].flags & (UV_INHERIT_FD | UV_INHERIT_STREAM))
520
+ continue;
521
+ if (pipes[i][0] != -1)
522
+ close(pipes[i][0]);
523
+ if (pipes[i][1] != -1)
524
+ close(pipes[i][1]);
525
+ }
526
+ free(pipes);
527
+ }
528
+
529
+ return err;
530
+ }
531
+
532
+
533
+ int uv_process_kill(uv_process_t* process, int signum) {
534
+ return uv_kill(process->pid, signum);
535
+ }
536
+
537
+
538
+ int uv_kill(int pid, int signum) {
539
+ if (kill(pid, signum))
540
+ return -errno;
541
+ else
542
+ return 0;
543
+ }
544
+
545
+
546
+ void uv__process_close(uv_process_t* handle) {
547
+ QUEUE_REMOVE(&handle->queue);
548
+ uv__handle_stop(handle);
549
+ if (QUEUE_EMPTY(&handle->loop->process_handles))
550
+ uv_signal_stop(&handle->loop->child_watcher);
551
+ }