foolio 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +21 -0
- data/examples/timer.rb +20 -0
- data/ext/foolio/extconf.rb +34 -0
- data/ext/foolio/foolio_ext.c +921 -0
- data/ext/foolio/gen.rb +50 -0
- data/ext/foolio/make_table.rb +12 -0
- data/ext/foolio/templ +243 -0
- data/ext/libuv/.gitignore +33 -0
- data/ext/libuv/.mailmap +13 -0
- data/ext/libuv/.travis.yml +9 -0
- data/ext/libuv/AUTHORS +61 -0
- data/ext/libuv/LICENSE +44 -0
- data/ext/libuv/Makefile +71 -0
- data/ext/libuv/README.md +90 -0
- data/ext/libuv/common.gypi +178 -0
- data/ext/libuv/gyp_uv +73 -0
- data/ext/libuv/include/uv-private/eio.h +403 -0
- data/ext/libuv/include/uv-private/ev.h +838 -0
- data/ext/libuv/include/uv-private/ngx-queue.h +108 -0
- data/ext/libuv/include/uv-private/tree.h +768 -0
- data/ext/libuv/include/uv-private/uv-unix.h +324 -0
- data/ext/libuv/include/uv-private/uv-win.h +517 -0
- data/ext/libuv/include/uv.h +1838 -0
- data/ext/libuv/src/fs-poll.c +235 -0
- data/ext/libuv/src/inet.c +293 -0
- data/ext/libuv/src/unix/async.c +148 -0
- data/ext/libuv/src/unix/core.c +696 -0
- data/ext/libuv/src/unix/cygwin.c +83 -0
- data/ext/libuv/src/unix/darwin.c +342 -0
- data/ext/libuv/src/unix/dl.c +83 -0
- data/ext/libuv/src/unix/eio/Changes +63 -0
- data/ext/libuv/src/unix/eio/LICENSE +36 -0
- data/ext/libuv/src/unix/eio/Makefile.am +15 -0
- data/ext/libuv/src/unix/eio/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/eio/autogen.sh +3 -0
- data/ext/libuv/src/unix/eio/config.h.in +86 -0
- data/ext/libuv/src/unix/eio/config_cygwin.h +80 -0
- data/ext/libuv/src/unix/eio/config_darwin.h +141 -0
- data/ext/libuv/src/unix/eio/config_freebsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_linux.h +94 -0
- data/ext/libuv/src/unix/eio/config_netbsd.h +81 -0
- data/ext/libuv/src/unix/eio/config_openbsd.h +137 -0
- data/ext/libuv/src/unix/eio/config_sunos.h +84 -0
- data/ext/libuv/src/unix/eio/configure.ac +22 -0
- data/ext/libuv/src/unix/eio/demo.c +194 -0
- data/ext/libuv/src/unix/eio/ecb.h +370 -0
- data/ext/libuv/src/unix/eio/eio.3 +3428 -0
- data/ext/libuv/src/unix/eio/eio.c +2593 -0
- data/ext/libuv/src/unix/eio/eio.pod +969 -0
- data/ext/libuv/src/unix/eio/libeio.m4 +195 -0
- data/ext/libuv/src/unix/eio/xthread.h +164 -0
- data/ext/libuv/src/unix/error.c +105 -0
- data/ext/libuv/src/unix/ev/Changes +388 -0
- data/ext/libuv/src/unix/ev/LICENSE +36 -0
- data/ext/libuv/src/unix/ev/Makefile.am +18 -0
- data/ext/libuv/src/unix/ev/Makefile.in +771 -0
- data/ext/libuv/src/unix/ev/README +58 -0
- data/ext/libuv/src/unix/ev/aclocal.m4 +8957 -0
- data/ext/libuv/src/unix/ev/autogen.sh +6 -0
- data/ext/libuv/src/unix/ev/config.guess +1526 -0
- data/ext/libuv/src/unix/ev/config.h.in +125 -0
- data/ext/libuv/src/unix/ev/config.sub +1658 -0
- data/ext/libuv/src/unix/ev/config_cygwin.h +123 -0
- data/ext/libuv/src/unix/ev/config_darwin.h +122 -0
- data/ext/libuv/src/unix/ev/config_freebsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_linux.h +141 -0
- data/ext/libuv/src/unix/ev/config_netbsd.h +120 -0
- data/ext/libuv/src/unix/ev/config_openbsd.h +126 -0
- data/ext/libuv/src/unix/ev/config_sunos.h +122 -0
- data/ext/libuv/src/unix/ev/configure +13037 -0
- data/ext/libuv/src/unix/ev/configure.ac +18 -0
- data/ext/libuv/src/unix/ev/depcomp +630 -0
- data/ext/libuv/src/unix/ev/ev++.h +816 -0
- data/ext/libuv/src/unix/ev/ev.3 +5311 -0
- data/ext/libuv/src/unix/ev/ev.c +3925 -0
- data/ext/libuv/src/unix/ev/ev.pod +5243 -0
- data/ext/libuv/src/unix/ev/ev_epoll.c +266 -0
- data/ext/libuv/src/unix/ev/ev_kqueue.c +235 -0
- data/ext/libuv/src/unix/ev/ev_poll.c +148 -0
- data/ext/libuv/src/unix/ev/ev_port.c +179 -0
- data/ext/libuv/src/unix/ev/ev_select.c +310 -0
- data/ext/libuv/src/unix/ev/ev_vars.h +203 -0
- data/ext/libuv/src/unix/ev/ev_win32.c +153 -0
- data/ext/libuv/src/unix/ev/ev_wrap.h +196 -0
- data/ext/libuv/src/unix/ev/event.c +402 -0
- data/ext/libuv/src/unix/ev/event.h +170 -0
- data/ext/libuv/src/unix/ev/install-sh +294 -0
- data/ext/libuv/src/unix/ev/libev.m4 +39 -0
- data/ext/libuv/src/unix/ev/ltmain.sh +8413 -0
- data/ext/libuv/src/unix/ev/missing +336 -0
- data/ext/libuv/src/unix/ev/mkinstalldirs +111 -0
- data/ext/libuv/src/unix/freebsd.c +326 -0
- data/ext/libuv/src/unix/fs.c +739 -0
- data/ext/libuv/src/unix/internal.h +188 -0
- data/ext/libuv/src/unix/kqueue.c +120 -0
- data/ext/libuv/src/unix/linux/inotify.c +239 -0
- data/ext/libuv/src/unix/linux/linux-core.c +557 -0
- data/ext/libuv/src/unix/linux/syscalls.c +388 -0
- data/ext/libuv/src/unix/linux/syscalls.h +124 -0
- data/ext/libuv/src/unix/loop-watcher.c +62 -0
- data/ext/libuv/src/unix/loop.c +94 -0
- data/ext/libuv/src/unix/netbsd.c +108 -0
- data/ext/libuv/src/unix/openbsd.c +295 -0
- data/ext/libuv/src/unix/pipe.c +259 -0
- data/ext/libuv/src/unix/poll.c +114 -0
- data/ext/libuv/src/unix/process.c +495 -0
- data/ext/libuv/src/unix/signal.c +269 -0
- data/ext/libuv/src/unix/stream.c +990 -0
- data/ext/libuv/src/unix/sunos.c +481 -0
- data/ext/libuv/src/unix/tcp.c +393 -0
- data/ext/libuv/src/unix/thread.c +251 -0
- data/ext/libuv/src/unix/timer.c +136 -0
- data/ext/libuv/src/unix/tty.c +145 -0
- data/ext/libuv/src/unix/udp.c +659 -0
- data/ext/libuv/src/unix/uv-eio.c +107 -0
- data/ext/libuv/src/unix/uv-eio.h +13 -0
- data/ext/libuv/src/uv-common.c +380 -0
- data/ext/libuv/src/uv-common.h +170 -0
- data/ext/libuv/src/win/async.c +100 -0
- data/ext/libuv/src/win/atomicops-inl.h +56 -0
- data/ext/libuv/src/win/core.c +278 -0
- data/ext/libuv/src/win/dl.c +86 -0
- data/ext/libuv/src/win/error.c +155 -0
- data/ext/libuv/src/win/fs-event.c +510 -0
- data/ext/libuv/src/win/fs.c +1948 -0
- data/ext/libuv/src/win/getaddrinfo.c +365 -0
- data/ext/libuv/src/win/handle-inl.h +149 -0
- data/ext/libuv/src/win/handle.c +154 -0
- data/ext/libuv/src/win/internal.h +343 -0
- data/ext/libuv/src/win/loop-watcher.c +122 -0
- data/ext/libuv/src/win/pipe.c +1672 -0
- data/ext/libuv/src/win/poll.c +616 -0
- data/ext/libuv/src/win/process-stdio.c +500 -0
- data/ext/libuv/src/win/process.c +1013 -0
- data/ext/libuv/src/win/req-inl.h +220 -0
- data/ext/libuv/src/win/req.c +25 -0
- data/ext/libuv/src/win/signal.c +57 -0
- data/ext/libuv/src/win/stream-inl.h +67 -0
- data/ext/libuv/src/win/stream.c +167 -0
- data/ext/libuv/src/win/tcp.c +1394 -0
- data/ext/libuv/src/win/thread.c +372 -0
- data/ext/libuv/src/win/threadpool.c +74 -0
- data/ext/libuv/src/win/timer.c +224 -0
- data/ext/libuv/src/win/tty.c +1799 -0
- data/ext/libuv/src/win/udp.c +716 -0
- data/ext/libuv/src/win/util.c +864 -0
- data/ext/libuv/src/win/winapi.c +132 -0
- data/ext/libuv/src/win/winapi.h +4452 -0
- data/ext/libuv/src/win/winsock.c +557 -0
- data/ext/libuv/src/win/winsock.h +171 -0
- data/ext/libuv/test/benchmark-async-pummel.c +97 -0
- data/ext/libuv/test/benchmark-async.c +137 -0
- data/ext/libuv/test/benchmark-fs-stat.c +135 -0
- data/ext/libuv/test/benchmark-getaddrinfo.c +94 -0
- data/ext/libuv/test/benchmark-list.h +127 -0
- data/ext/libuv/test/benchmark-loop-count.c +88 -0
- data/ext/libuv/test/benchmark-million-timers.c +65 -0
- data/ext/libuv/test/benchmark-ping-pongs.c +213 -0
- data/ext/libuv/test/benchmark-pound.c +324 -0
- data/ext/libuv/test/benchmark-pump.c +462 -0
- data/ext/libuv/test/benchmark-sizes.c +44 -0
- data/ext/libuv/test/benchmark-spawn.c +162 -0
- data/ext/libuv/test/benchmark-tcp-write-batch.c +140 -0
- data/ext/libuv/test/benchmark-thread.c +64 -0
- data/ext/libuv/test/benchmark-udp-packet-storm.c +247 -0
- data/ext/libuv/test/blackhole-server.c +118 -0
- data/ext/libuv/test/dns-server.c +321 -0
- data/ext/libuv/test/echo-server.c +378 -0
- data/ext/libuv/test/fixtures/empty_file +0 -0
- data/ext/libuv/test/fixtures/load_error.node +1 -0
- data/ext/libuv/test/run-benchmarks.c +64 -0
- data/ext/libuv/test/run-tests.c +138 -0
- data/ext/libuv/test/runner-unix.c +295 -0
- data/ext/libuv/test/runner-unix.h +36 -0
- data/ext/libuv/test/runner-win.c +285 -0
- data/ext/libuv/test/runner-win.h +42 -0
- data/ext/libuv/test/runner.c +355 -0
- data/ext/libuv/test/runner.h +159 -0
- data/ext/libuv/test/task.h +112 -0
- data/ext/libuv/test/test-async.c +118 -0
- data/ext/libuv/test/test-callback-order.c +76 -0
- data/ext/libuv/test/test-callback-stack.c +203 -0
- data/ext/libuv/test/test-connection-fail.c +148 -0
- data/ext/libuv/test/test-cwd-and-chdir.c +64 -0
- data/ext/libuv/test/test-delayed-accept.c +188 -0
- data/ext/libuv/test/test-dlerror.c +58 -0
- data/ext/libuv/test/test-error.c +59 -0
- data/ext/libuv/test/test-fail-always.c +29 -0
- data/ext/libuv/test/test-fs-event.c +474 -0
- data/ext/libuv/test/test-fs-poll.c +146 -0
- data/ext/libuv/test/test-fs.c +1843 -0
- data/ext/libuv/test/test-get-currentexe.c +63 -0
- data/ext/libuv/test/test-get-loadavg.c +36 -0
- data/ext/libuv/test/test-get-memory.c +38 -0
- data/ext/libuv/test/test-getaddrinfo.c +122 -0
- data/ext/libuv/test/test-getsockname.c +342 -0
- data/ext/libuv/test/test-hrtime.c +54 -0
- data/ext/libuv/test/test-idle.c +81 -0
- data/ext/libuv/test/test-ipc-send-recv.c +209 -0
- data/ext/libuv/test/test-ipc.c +620 -0
- data/ext/libuv/test/test-list.h +427 -0
- data/ext/libuv/test/test-loop-handles.c +336 -0
- data/ext/libuv/test/test-multiple-listen.c +102 -0
- data/ext/libuv/test/test-mutexes.c +63 -0
- data/ext/libuv/test/test-pass-always.c +28 -0
- data/ext/libuv/test/test-ping-pong.c +253 -0
- data/ext/libuv/test/test-pipe-bind-error.c +140 -0
- data/ext/libuv/test/test-pipe-connect-error.c +96 -0
- data/ext/libuv/test/test-platform-output.c +87 -0
- data/ext/libuv/test/test-poll-close.c +72 -0
- data/ext/libuv/test/test-poll.c +573 -0
- data/ext/libuv/test/test-process-title.c +49 -0
- data/ext/libuv/test/test-ref.c +338 -0
- data/ext/libuv/test/test-run-once.c +48 -0
- data/ext/libuv/test/test-semaphore.c +111 -0
- data/ext/libuv/test/test-shutdown-close.c +103 -0
- data/ext/libuv/test/test-shutdown-eof.c +183 -0
- data/ext/libuv/test/test-signal.c +162 -0
- data/ext/libuv/test/test-spawn.c +863 -0
- data/ext/libuv/test/test-stdio-over-pipes.c +246 -0
- data/ext/libuv/test/test-tcp-bind-error.c +191 -0
- data/ext/libuv/test/test-tcp-bind6-error.c +154 -0
- data/ext/libuv/test/test-tcp-close-while-connecting.c +80 -0
- data/ext/libuv/test/test-tcp-close.c +129 -0
- data/ext/libuv/test/test-tcp-connect-error-after-write.c +95 -0
- data/ext/libuv/test/test-tcp-connect-error.c +70 -0
- data/ext/libuv/test/test-tcp-connect-timeout.c +85 -0
- data/ext/libuv/test/test-tcp-connect6-error.c +68 -0
- data/ext/libuv/test/test-tcp-flags.c +51 -0
- data/ext/libuv/test/test-tcp-shutdown-after-write.c +131 -0
- data/ext/libuv/test/test-tcp-unexpected-read.c +113 -0
- data/ext/libuv/test/test-tcp-write-error.c +168 -0
- data/ext/libuv/test/test-tcp-write-to-half-open-connection.c +135 -0
- data/ext/libuv/test/test-tcp-writealot.c +170 -0
- data/ext/libuv/test/test-thread.c +183 -0
- data/ext/libuv/test/test-threadpool.c +57 -0
- data/ext/libuv/test/test-timer-again.c +141 -0
- data/ext/libuv/test/test-timer.c +152 -0
- data/ext/libuv/test/test-tty.c +110 -0
- data/ext/libuv/test/test-udp-dgram-too-big.c +86 -0
- data/ext/libuv/test/test-udp-ipv6.c +156 -0
- data/ext/libuv/test/test-udp-multicast-join.c +139 -0
- data/ext/libuv/test/test-udp-multicast-ttl.c +86 -0
- data/ext/libuv/test/test-udp-options.c +86 -0
- data/ext/libuv/test/test-udp-send-and-recv.c +208 -0
- data/ext/libuv/test/test-util.c +97 -0
- data/ext/libuv/test/test-walk-handles.c +77 -0
- data/ext/libuv/uv.gyp +375 -0
- data/ext/libuv/vcbuild.bat +105 -0
- data/foolio.gemspec +18 -0
- data/lib/foolio.rb +9 -0
- data/lib/foolio/handle.rb +27 -0
- data/lib/foolio/listener.rb +26 -0
- data/lib/foolio/loop.rb +79 -0
- data/lib/foolio/stream.rb +109 -0
- data/lib/foolio/version.rb +3 -0
- metadata +309 -0
@@ -0,0 +1,259 @@
|
|
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 <assert.h>
|
26
|
+
#include <errno.h>
|
27
|
+
#include <string.h>
|
28
|
+
#include <sys/un.h>
|
29
|
+
#include <unistd.h>
|
30
|
+
#include <stdlib.h>
|
31
|
+
|
32
|
+
static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events);
|
33
|
+
|
34
|
+
|
35
|
+
int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc) {
|
36
|
+
uv__stream_init(loop, (uv_stream_t*)handle, UV_NAMED_PIPE);
|
37
|
+
handle->shutdown_req = NULL;
|
38
|
+
handle->connect_req = NULL;
|
39
|
+
handle->pipe_fname = NULL;
|
40
|
+
handle->ipc = ipc;
|
41
|
+
return 0;
|
42
|
+
}
|
43
|
+
|
44
|
+
|
45
|
+
int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
|
46
|
+
struct sockaddr_un saddr;
|
47
|
+
const char* pipe_fname;
|
48
|
+
int saved_errno;
|
49
|
+
int sockfd;
|
50
|
+
int status;
|
51
|
+
int bound;
|
52
|
+
|
53
|
+
saved_errno = errno;
|
54
|
+
pipe_fname = NULL;
|
55
|
+
sockfd = -1;
|
56
|
+
status = -1;
|
57
|
+
bound = 0;
|
58
|
+
|
59
|
+
/* Already bound? */
|
60
|
+
if (handle->fd >= 0) {
|
61
|
+
uv__set_artificial_error(handle->loop, UV_EINVAL);
|
62
|
+
goto out;
|
63
|
+
}
|
64
|
+
|
65
|
+
/* Make a copy of the file name, it outlives this function's scope. */
|
66
|
+
if ((pipe_fname = strdup(name)) == NULL) {
|
67
|
+
uv__set_sys_error(handle->loop, ENOMEM);
|
68
|
+
goto out;
|
69
|
+
}
|
70
|
+
|
71
|
+
/* We've got a copy, don't touch the original any more. */
|
72
|
+
name = NULL;
|
73
|
+
|
74
|
+
if ((sockfd = uv__socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
|
75
|
+
uv__set_sys_error(handle->loop, errno);
|
76
|
+
goto out;
|
77
|
+
}
|
78
|
+
|
79
|
+
memset(&saddr, 0, sizeof saddr);
|
80
|
+
uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
|
81
|
+
saddr.sun_family = AF_UNIX;
|
82
|
+
|
83
|
+
if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
|
84
|
+
/* Convert ENOENT to EACCES for compatibility with Windows. */
|
85
|
+
uv__set_sys_error(handle->loop, (errno == ENOENT) ? EACCES : errno);
|
86
|
+
goto out;
|
87
|
+
}
|
88
|
+
bound = 1;
|
89
|
+
|
90
|
+
/* Success. */
|
91
|
+
handle->pipe_fname = pipe_fname; /* Is a strdup'ed copy. */
|
92
|
+
handle->fd = sockfd;
|
93
|
+
status = 0;
|
94
|
+
|
95
|
+
out:
|
96
|
+
/* Clean up on error. */
|
97
|
+
if (status) {
|
98
|
+
if (bound) {
|
99
|
+
/* unlink() before close() to avoid races. */
|
100
|
+
assert(pipe_fname != NULL);
|
101
|
+
unlink(pipe_fname);
|
102
|
+
}
|
103
|
+
close(sockfd);
|
104
|
+
|
105
|
+
free((void*)pipe_fname);
|
106
|
+
}
|
107
|
+
|
108
|
+
errno = saved_errno;
|
109
|
+
return status;
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb) {
|
114
|
+
int saved_errno;
|
115
|
+
int status;
|
116
|
+
|
117
|
+
saved_errno = errno;
|
118
|
+
status = -1;
|
119
|
+
|
120
|
+
if (handle->fd == -1) {
|
121
|
+
uv__set_artificial_error(handle->loop, UV_EINVAL);
|
122
|
+
goto out;
|
123
|
+
}
|
124
|
+
assert(handle->fd >= 0);
|
125
|
+
|
126
|
+
if ((status = listen(handle->fd, backlog)) == -1) {
|
127
|
+
uv__set_sys_error(handle->loop, errno);
|
128
|
+
} else {
|
129
|
+
handle->connection_cb = cb;
|
130
|
+
uv__io_init(&handle->read_watcher,
|
131
|
+
uv__pipe_accept,
|
132
|
+
handle->fd,
|
133
|
+
UV__IO_READ);
|
134
|
+
uv__io_start(handle->loop, &handle->read_watcher);
|
135
|
+
}
|
136
|
+
|
137
|
+
out:
|
138
|
+
errno = saved_errno;
|
139
|
+
return status;
|
140
|
+
}
|
141
|
+
|
142
|
+
|
143
|
+
void uv__pipe_close(uv_pipe_t* handle) {
|
144
|
+
if (handle->pipe_fname) {
|
145
|
+
/*
|
146
|
+
* Unlink the file system entity before closing the file descriptor.
|
147
|
+
* Doing it the other way around introduces a race where our process
|
148
|
+
* unlinks a socket with the same name that's just been created by
|
149
|
+
* another thread or process.
|
150
|
+
*/
|
151
|
+
unlink(handle->pipe_fname);
|
152
|
+
free((void*)handle->pipe_fname);
|
153
|
+
}
|
154
|
+
|
155
|
+
uv__stream_close((uv_stream_t*)handle);
|
156
|
+
}
|
157
|
+
|
158
|
+
|
159
|
+
void uv_pipe_open(uv_pipe_t* handle, uv_file fd) {
|
160
|
+
uv__stream_open((uv_stream_t*)handle,
|
161
|
+
fd,
|
162
|
+
UV_STREAM_READABLE | UV_STREAM_WRITABLE);
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
void uv_pipe_connect(uv_connect_t* req,
|
167
|
+
uv_pipe_t* handle,
|
168
|
+
const char* name,
|
169
|
+
uv_connect_cb cb) {
|
170
|
+
struct sockaddr_un saddr;
|
171
|
+
int saved_errno;
|
172
|
+
int new_sock;
|
173
|
+
int err;
|
174
|
+
int r;
|
175
|
+
|
176
|
+
saved_errno = errno;
|
177
|
+
new_sock = (handle->fd == -1);
|
178
|
+
err = -1;
|
179
|
+
|
180
|
+
if (new_sock)
|
181
|
+
if ((handle->fd = uv__socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
182
|
+
goto out;
|
183
|
+
|
184
|
+
memset(&saddr, 0, sizeof saddr);
|
185
|
+
uv_strlcpy(saddr.sun_path, name, sizeof(saddr.sun_path));
|
186
|
+
saddr.sun_family = AF_UNIX;
|
187
|
+
|
188
|
+
/* We don't check for EINPROGRESS. Think about it: the socket
|
189
|
+
* is either there or not.
|
190
|
+
*/
|
191
|
+
do {
|
192
|
+
r = connect(handle->fd, (struct sockaddr*)&saddr, sizeof saddr);
|
193
|
+
}
|
194
|
+
while (r == -1 && errno == EINTR);
|
195
|
+
|
196
|
+
if (r == -1)
|
197
|
+
goto out;
|
198
|
+
|
199
|
+
if (new_sock)
|
200
|
+
if (uv__stream_open((uv_stream_t*)handle,
|
201
|
+
handle->fd,
|
202
|
+
UV_STREAM_READABLE | UV_STREAM_WRITABLE))
|
203
|
+
goto out;
|
204
|
+
|
205
|
+
uv__io_start(handle->loop, &handle->read_watcher);
|
206
|
+
uv__io_start(handle->loop, &handle->write_watcher);
|
207
|
+
err = 0;
|
208
|
+
|
209
|
+
out:
|
210
|
+
handle->delayed_error = err ? errno : 0; /* Passed to callback. */
|
211
|
+
handle->connect_req = req;
|
212
|
+
|
213
|
+
uv__req_init(handle->loop, req, UV_CONNECT);
|
214
|
+
req->handle = (uv_stream_t*)handle;
|
215
|
+
req->cb = cb;
|
216
|
+
ngx_queue_init(&req->queue);
|
217
|
+
|
218
|
+
/* Run callback on next tick. */
|
219
|
+
uv__io_feed(handle->loop, &handle->write_watcher, UV__IO_WRITE);
|
220
|
+
|
221
|
+
/* Mimic the Windows pipe implementation, always
|
222
|
+
* return 0 and let the callback handle errors.
|
223
|
+
*/
|
224
|
+
errno = saved_errno;
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
/* TODO merge with uv__server_io()? */
|
229
|
+
static void uv__pipe_accept(uv_loop_t* loop, uv__io_t* w, int events) {
|
230
|
+
uv_pipe_t* pipe;
|
231
|
+
int saved_errno;
|
232
|
+
int sockfd;
|
233
|
+
|
234
|
+
saved_errno = errno;
|
235
|
+
pipe = container_of(w, uv_pipe_t, read_watcher);
|
236
|
+
|
237
|
+
assert(pipe->type == UV_NAMED_PIPE);
|
238
|
+
|
239
|
+
sockfd = uv__accept(pipe->fd);
|
240
|
+
if (sockfd == -1) {
|
241
|
+
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
242
|
+
uv__set_sys_error(pipe->loop, errno);
|
243
|
+
pipe->connection_cb((uv_stream_t*)pipe, -1);
|
244
|
+
}
|
245
|
+
} else {
|
246
|
+
pipe->accepted_fd = sockfd;
|
247
|
+
pipe->connection_cb((uv_stream_t*)pipe, 0);
|
248
|
+
if (pipe->accepted_fd == sockfd) {
|
249
|
+
/* The user hasn't called uv_accept() yet */
|
250
|
+
uv__io_stop(pipe->loop, &pipe->read_watcher);
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
errno = saved_errno;
|
255
|
+
}
|
256
|
+
|
257
|
+
|
258
|
+
void uv_pipe_pending_instances(uv_pipe_t* handle, int count) {
|
259
|
+
}
|
@@ -0,0 +1,114 @@
|
|
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, 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__IO_ERROR) {
|
37
|
+
/* An error happened. Libev has implicitly stopped the watcher, but we */
|
38
|
+
/* need to fix the refcount. */
|
39
|
+
uv__handle_stop(handle);
|
40
|
+
uv__set_sys_error(handle->loop, EBADF);
|
41
|
+
handle->poll_cb(handle, -1, 0);
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
|
45
|
+
pevents = 0;
|
46
|
+
if (events & UV__IO_READ)
|
47
|
+
pevents |= UV_READABLE;
|
48
|
+
if (events & UV__IO_WRITE)
|
49
|
+
pevents |= UV_WRITABLE;
|
50
|
+
|
51
|
+
handle->poll_cb(handle, 0, pevents);
|
52
|
+
}
|
53
|
+
|
54
|
+
|
55
|
+
int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
|
56
|
+
uv__handle_init(loop, (uv_handle_t*) handle, UV_POLL);
|
57
|
+
handle->fd = fd;
|
58
|
+
handle->poll_cb = NULL;
|
59
|
+
uv__io_init(&handle->io_watcher, uv__poll_io, fd, 0);
|
60
|
+
|
61
|
+
return 0;
|
62
|
+
}
|
63
|
+
|
64
|
+
|
65
|
+
int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
|
66
|
+
uv_os_sock_t socket) {
|
67
|
+
return uv_poll_init(loop, handle, socket);
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
static void uv__poll_stop(uv_poll_t* handle) {
|
72
|
+
uv__io_stop(handle->loop, &handle->io_watcher);
|
73
|
+
uv__handle_stop(handle);
|
74
|
+
}
|
75
|
+
|
76
|
+
|
77
|
+
int uv_poll_stop(uv_poll_t* handle) {
|
78
|
+
assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
|
79
|
+
uv__poll_stop(handle);
|
80
|
+
return 0;
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
int uv_poll_start(uv_poll_t* handle, int pevents, uv_poll_cb poll_cb) {
|
85
|
+
int events;
|
86
|
+
|
87
|
+
assert((pevents & ~(UV_READABLE | UV_WRITABLE)) == 0);
|
88
|
+
assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
|
89
|
+
|
90
|
+
if (pevents == 0) {
|
91
|
+
uv__poll_stop(handle);
|
92
|
+
return 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
events = 0;
|
96
|
+
if (pevents & UV_READABLE)
|
97
|
+
events |= UV__IO_READ;
|
98
|
+
if (pevents & UV_WRITABLE)
|
99
|
+
events |= UV__IO_WRITE;
|
100
|
+
|
101
|
+
uv__io_stop(handle->loop, &handle->io_watcher);
|
102
|
+
uv__io_set(&handle->io_watcher, uv__poll_io, handle->fd, events);
|
103
|
+
uv__io_start(handle->loop, &handle->io_watcher);
|
104
|
+
|
105
|
+
handle->poll_cb = poll_cb;
|
106
|
+
uv__handle_start(handle);
|
107
|
+
|
108
|
+
return 0;
|
109
|
+
}
|
110
|
+
|
111
|
+
|
112
|
+
void uv__poll_close(uv_poll_t* handle) {
|
113
|
+
uv__poll_stop(handle);
|
114
|
+
}
|
@@ -0,0 +1,495 @@
|
|
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
|
+
#ifdef __APPLE__
|
37
|
+
# include <TargetConditionals.h>
|
38
|
+
#endif
|
39
|
+
|
40
|
+
#if defined(__APPLE__) && !TARGET_OS_IPHONE
|
41
|
+
# include <crt_externs.h>
|
42
|
+
# define environ (*_NSGetEnviron())
|
43
|
+
#else
|
44
|
+
extern char **environ;
|
45
|
+
#endif
|
46
|
+
|
47
|
+
|
48
|
+
static ngx_queue_t* uv__process_queue(uv_loop_t* loop, int pid) {
|
49
|
+
assert(pid > 0);
|
50
|
+
return loop->process_handles + pid % ARRAY_SIZE(loop->process_handles);
|
51
|
+
}
|
52
|
+
|
53
|
+
|
54
|
+
static uv_process_t* uv__process_find(uv_loop_t* loop, int pid) {
|
55
|
+
uv_process_t* handle;
|
56
|
+
ngx_queue_t* h;
|
57
|
+
ngx_queue_t* q;
|
58
|
+
|
59
|
+
h = uv__process_queue(loop, pid);
|
60
|
+
|
61
|
+
ngx_queue_foreach(q, h) {
|
62
|
+
handle = ngx_queue_data(q, uv_process_t, queue);
|
63
|
+
if (handle->pid == pid) return handle;
|
64
|
+
}
|
65
|
+
|
66
|
+
return NULL;
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
static void uv__chld(uv_signal_t* handle, int signum) {
|
71
|
+
uv_process_t* process;
|
72
|
+
int exit_status;
|
73
|
+
int term_signal;
|
74
|
+
int status;
|
75
|
+
pid_t pid;
|
76
|
+
|
77
|
+
assert(signum == SIGCHLD);
|
78
|
+
|
79
|
+
for (;;) {
|
80
|
+
pid = waitpid(-1, &status, WNOHANG);
|
81
|
+
|
82
|
+
if (pid == 0)
|
83
|
+
return;
|
84
|
+
|
85
|
+
if (pid == -1) {
|
86
|
+
if (errno == ECHILD)
|
87
|
+
return; /* XXX stop signal watcher? */
|
88
|
+
else
|
89
|
+
abort();
|
90
|
+
}
|
91
|
+
|
92
|
+
process = uv__process_find(handle->loop, pid);
|
93
|
+
if (process == NULL)
|
94
|
+
continue; /* XXX bug? abort? */
|
95
|
+
|
96
|
+
if (process->exit_cb == NULL)
|
97
|
+
continue;
|
98
|
+
|
99
|
+
exit_status = 0;
|
100
|
+
term_signal = 0;
|
101
|
+
|
102
|
+
if (WIFEXITED(status))
|
103
|
+
exit_status = WEXITSTATUS(status);
|
104
|
+
|
105
|
+
if (WIFSIGNALED(status))
|
106
|
+
term_signal = WTERMSIG(status);
|
107
|
+
|
108
|
+
if (process->errorno) {
|
109
|
+
uv__set_sys_error(process->loop, process->errorno);
|
110
|
+
exit_status = -1; /* execve() failed */
|
111
|
+
}
|
112
|
+
|
113
|
+
process->exit_cb(process, exit_status, term_signal);
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
int uv__make_socketpair(int fds[2], int flags) {
|
119
|
+
#if __linux__
|
120
|
+
if (socketpair(AF_UNIX, SOCK_STREAM | UV__SOCK_CLOEXEC | flags, 0, fds) == 0)
|
121
|
+
return 0;
|
122
|
+
|
123
|
+
/* Retry on EINVAL, it means SOCK_CLOEXEC is not supported.
|
124
|
+
* Anything else is a genuine error.
|
125
|
+
*/
|
126
|
+
if (errno != EINVAL)
|
127
|
+
return -1;
|
128
|
+
#endif
|
129
|
+
|
130
|
+
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds))
|
131
|
+
return -1;
|
132
|
+
|
133
|
+
uv__cloexec(fds[0], 1);
|
134
|
+
uv__cloexec(fds[1], 1);
|
135
|
+
|
136
|
+
if (flags & UV__F_NONBLOCK) {
|
137
|
+
uv__nonblock(fds[0], 1);
|
138
|
+
uv__nonblock(fds[1], 1);
|
139
|
+
}
|
140
|
+
|
141
|
+
return 0;
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
int uv__make_pipe(int fds[2], int flags) {
|
146
|
+
#if __linux__
|
147
|
+
if (uv__pipe2(fds, flags | UV__O_CLOEXEC) == 0)
|
148
|
+
return 0;
|
149
|
+
|
150
|
+
if (errno != ENOSYS)
|
151
|
+
return -1;
|
152
|
+
#endif
|
153
|
+
|
154
|
+
if (pipe(fds))
|
155
|
+
return -1;
|
156
|
+
|
157
|
+
uv__cloexec(fds[0], 1);
|
158
|
+
uv__cloexec(fds[1], 1);
|
159
|
+
|
160
|
+
if (flags & UV__F_NONBLOCK) {
|
161
|
+
uv__nonblock(fds[0], 1);
|
162
|
+
uv__nonblock(fds[1], 1);
|
163
|
+
}
|
164
|
+
|
165
|
+
return 0;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
/*
|
170
|
+
* Used for initializing stdio streams like options.stdin_stream. Returns
|
171
|
+
* zero on success.
|
172
|
+
*/
|
173
|
+
static int uv__process_init_stdio(uv_stdio_container_t* container, int fds[2]) {
|
174
|
+
int fd = -1;
|
175
|
+
switch (container->flags & (UV_IGNORE | UV_CREATE_PIPE | UV_INHERIT_FD |
|
176
|
+
UV_INHERIT_STREAM)) {
|
177
|
+
case UV_IGNORE:
|
178
|
+
return 0;
|
179
|
+
case UV_CREATE_PIPE:
|
180
|
+
assert(container->data.stream != NULL);
|
181
|
+
|
182
|
+
if (container->data.stream->type != UV_NAMED_PIPE) {
|
183
|
+
errno = EINVAL;
|
184
|
+
return -1;
|
185
|
+
}
|
186
|
+
|
187
|
+
return uv__make_socketpair(fds, 0);
|
188
|
+
case UV_INHERIT_FD:
|
189
|
+
case UV_INHERIT_STREAM:
|
190
|
+
if (container->flags & UV_INHERIT_FD) {
|
191
|
+
fd = container->data.fd;
|
192
|
+
} else {
|
193
|
+
fd = container->data.stream->fd;
|
194
|
+
}
|
195
|
+
|
196
|
+
if (fd == -1) {
|
197
|
+
errno = EINVAL;
|
198
|
+
return -1;
|
199
|
+
}
|
200
|
+
|
201
|
+
fds[1] = fd;
|
202
|
+
|
203
|
+
return 0;
|
204
|
+
default:
|
205
|
+
assert(0 && "Unexpected flags");
|
206
|
+
return -1;
|
207
|
+
}
|
208
|
+
}
|
209
|
+
|
210
|
+
|
211
|
+
static int uv__process_stdio_flags(uv_stdio_container_t* container,
|
212
|
+
int writable) {
|
213
|
+
if (container->data.stream->type == UV_NAMED_PIPE &&
|
214
|
+
((uv_pipe_t*)container->data.stream)->ipc) {
|
215
|
+
return UV_STREAM_READABLE | UV_STREAM_WRITABLE;
|
216
|
+
} else if (writable) {
|
217
|
+
return UV_STREAM_WRITABLE;
|
218
|
+
} else {
|
219
|
+
return UV_STREAM_READABLE;
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
static int uv__process_open_stream(uv_stdio_container_t* container,
|
225
|
+
int fds[2],
|
226
|
+
int writable) {
|
227
|
+
int child_fd;
|
228
|
+
int flags;
|
229
|
+
int fd;
|
230
|
+
|
231
|
+
fd = fds[0];
|
232
|
+
child_fd = fds[1];
|
233
|
+
|
234
|
+
/* No need to create stream */
|
235
|
+
if (!(container->flags & UV_CREATE_PIPE) || fd < 0)
|
236
|
+
return 0;
|
237
|
+
|
238
|
+
assert(child_fd >= 0);
|
239
|
+
close(child_fd);
|
240
|
+
|
241
|
+
uv__nonblock(fd, 1);
|
242
|
+
flags = uv__process_stdio_flags(container, writable);
|
243
|
+
|
244
|
+
return uv__stream_open((uv_stream_t*)container->data.stream, fd, flags);
|
245
|
+
}
|
246
|
+
|
247
|
+
|
248
|
+
static void uv__process_close_stream(uv_stdio_container_t* container) {
|
249
|
+
if (!(container->flags & UV_CREATE_PIPE)) return;
|
250
|
+
uv__stream_close((uv_stream_t*)container->data.stream);
|
251
|
+
}
|
252
|
+
|
253
|
+
|
254
|
+
static void uv__write_int(int fd, int val) {
|
255
|
+
ssize_t n;
|
256
|
+
|
257
|
+
do
|
258
|
+
n = write(fd, &val, sizeof(val));
|
259
|
+
while (n == -1 && errno == EINTR);
|
260
|
+
|
261
|
+
if (n == -1 && errno == EPIPE)
|
262
|
+
return; /* parent process has quit */
|
263
|
+
|
264
|
+
assert(n == sizeof(val));
|
265
|
+
}
|
266
|
+
|
267
|
+
|
268
|
+
static void uv__process_child_init(uv_process_options_t options,
|
269
|
+
int stdio_count,
|
270
|
+
int (*pipes)[2],
|
271
|
+
int error_fd) {
|
272
|
+
int close_fd;
|
273
|
+
int use_fd;
|
274
|
+
int i;
|
275
|
+
|
276
|
+
if (options.flags & UV_PROCESS_DETACHED)
|
277
|
+
setsid();
|
278
|
+
|
279
|
+
for (i = 0; i < stdio_count; i++) {
|
280
|
+
close_fd = pipes[i][0];
|
281
|
+
use_fd = pipes[i][1];
|
282
|
+
|
283
|
+
if (use_fd >= 0)
|
284
|
+
close(close_fd);
|
285
|
+
else if (i >= 3)
|
286
|
+
continue;
|
287
|
+
else {
|
288
|
+
/* redirect stdin, stdout and stderr to /dev/null even if UV_IGNORE is
|
289
|
+
* set
|
290
|
+
*/
|
291
|
+
use_fd = open("/dev/null", i == 0 ? O_RDONLY : O_RDWR);
|
292
|
+
|
293
|
+
if (use_fd == -1) {
|
294
|
+
uv__write_int(error_fd, errno);
|
295
|
+
perror("failed to open stdio");
|
296
|
+
_exit(127);
|
297
|
+
}
|
298
|
+
}
|
299
|
+
|
300
|
+
if (i == use_fd)
|
301
|
+
uv__cloexec(use_fd, 0);
|
302
|
+
else {
|
303
|
+
dup2(use_fd, i);
|
304
|
+
close(use_fd);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
if (options.cwd && chdir(options.cwd)) {
|
309
|
+
uv__write_int(error_fd, errno);
|
310
|
+
perror("chdir()");
|
311
|
+
_exit(127);
|
312
|
+
}
|
313
|
+
|
314
|
+
if ((options.flags & UV_PROCESS_SETGID) && setgid(options.gid)) {
|
315
|
+
uv__write_int(error_fd, errno);
|
316
|
+
perror("setgid()");
|
317
|
+
_exit(127);
|
318
|
+
}
|
319
|
+
|
320
|
+
if ((options.flags & UV_PROCESS_SETUID) && setuid(options.uid)) {
|
321
|
+
uv__write_int(error_fd, errno);
|
322
|
+
perror("setuid()");
|
323
|
+
_exit(127);
|
324
|
+
}
|
325
|
+
|
326
|
+
environ = options.env;
|
327
|
+
|
328
|
+
execvp(options.file, options.args);
|
329
|
+
uv__write_int(error_fd, errno);
|
330
|
+
perror("execvp()");
|
331
|
+
_exit(127);
|
332
|
+
}
|
333
|
+
|
334
|
+
|
335
|
+
int uv_spawn(uv_loop_t* loop,
|
336
|
+
uv_process_t* process,
|
337
|
+
const uv_process_options_t options) {
|
338
|
+
int signal_pipe[2] = { -1, -1 };
|
339
|
+
int (*pipes)[2];
|
340
|
+
int stdio_count;
|
341
|
+
ngx_queue_t* q;
|
342
|
+
ssize_t r;
|
343
|
+
pid_t pid;
|
344
|
+
int i;
|
345
|
+
|
346
|
+
assert(options.file != NULL);
|
347
|
+
assert(!(options.flags & ~(UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS |
|
348
|
+
UV_PROCESS_DETACHED |
|
349
|
+
UV_PROCESS_SETGID |
|
350
|
+
UV_PROCESS_SETUID)));
|
351
|
+
|
352
|
+
uv__handle_init(loop, (uv_handle_t*)process, UV_PROCESS);
|
353
|
+
ngx_queue_init(&process->queue);
|
354
|
+
|
355
|
+
stdio_count = options.stdio_count;
|
356
|
+
if (stdio_count < 3)
|
357
|
+
stdio_count = 3;
|
358
|
+
|
359
|
+
pipes = malloc(stdio_count * sizeof(*pipes));
|
360
|
+
if (pipes == NULL) {
|
361
|
+
errno = ENOMEM;
|
362
|
+
goto error;
|
363
|
+
}
|
364
|
+
|
365
|
+
for (i = 0; i < stdio_count; i++) {
|
366
|
+
pipes[i][0] = -1;
|
367
|
+
pipes[i][1] = -1;
|
368
|
+
}
|
369
|
+
|
370
|
+
for (i = 0; i < options.stdio_count; i++)
|
371
|
+
if (uv__process_init_stdio(options.stdio + i, pipes[i]))
|
372
|
+
goto error;
|
373
|
+
|
374
|
+
/* swap stdin file descriptors, it's the only writable stream */
|
375
|
+
{
|
376
|
+
int* p = pipes[0];
|
377
|
+
int t = p[0];
|
378
|
+
p[0] = p[1];
|
379
|
+
p[1] = t;
|
380
|
+
}
|
381
|
+
|
382
|
+
/* This pipe is used by the parent to wait until
|
383
|
+
* the child has called `execve()`. We need this
|
384
|
+
* to avoid the following race condition:
|
385
|
+
*
|
386
|
+
* if ((pid = fork()) > 0) {
|
387
|
+
* kill(pid, SIGTERM);
|
388
|
+
* }
|
389
|
+
* else if (pid == 0) {
|
390
|
+
* execve("/bin/cat", argp, envp);
|
391
|
+
* }
|
392
|
+
*
|
393
|
+
* The parent sends a signal immediately after forking.
|
394
|
+
* Since the child may not have called `execve()` yet,
|
395
|
+
* there is no telling what process receives the signal,
|
396
|
+
* our fork or /bin/cat.
|
397
|
+
*
|
398
|
+
* To avoid ambiguity, we create a pipe with both ends
|
399
|
+
* marked close-on-exec. Then, after the call to `fork()`,
|
400
|
+
* the parent polls the read end until it EOFs or errors with EPIPE.
|
401
|
+
*/
|
402
|
+
if (uv__make_pipe(signal_pipe, 0))
|
403
|
+
goto error;
|
404
|
+
|
405
|
+
uv_signal_start(&loop->child_watcher, uv__chld, SIGCHLD);
|
406
|
+
|
407
|
+
pid = fork();
|
408
|
+
|
409
|
+
if (pid == -1) {
|
410
|
+
close(signal_pipe[0]);
|
411
|
+
close(signal_pipe[1]);
|
412
|
+
goto error;
|
413
|
+
}
|
414
|
+
|
415
|
+
if (pid == 0) {
|
416
|
+
uv__process_child_init(options, stdio_count, pipes, signal_pipe[1]);
|
417
|
+
abort();
|
418
|
+
}
|
419
|
+
|
420
|
+
close(signal_pipe[1]);
|
421
|
+
|
422
|
+
process->errorno = 0;
|
423
|
+
do
|
424
|
+
r = read(signal_pipe[0], &process->errorno, sizeof(process->errorno));
|
425
|
+
while (r == -1 && errno == EINTR);
|
426
|
+
|
427
|
+
if (r == 0)
|
428
|
+
; /* okay, EOF */
|
429
|
+
else if (r == sizeof(process->errorno))
|
430
|
+
; /* okay, read errorno */
|
431
|
+
else if (r == -1 && errno == EPIPE)
|
432
|
+
; /* okay, got EPIPE */
|
433
|
+
else
|
434
|
+
abort();
|
435
|
+
|
436
|
+
close(signal_pipe[0]);
|
437
|
+
|
438
|
+
for (i = 0; i < options.stdio_count; i++) {
|
439
|
+
if (uv__process_open_stream(options.stdio + i, pipes[i], i == 0)) {
|
440
|
+
while (i--) uv__process_close_stream(options.stdio + i);
|
441
|
+
goto error;
|
442
|
+
}
|
443
|
+
}
|
444
|
+
|
445
|
+
q = uv__process_queue(loop, pid);
|
446
|
+
ngx_queue_insert_tail(q, &process->queue);
|
447
|
+
|
448
|
+
process->pid = pid;
|
449
|
+
process->exit_cb = options.exit_cb;
|
450
|
+
uv__handle_start(process);
|
451
|
+
|
452
|
+
free(pipes);
|
453
|
+
return 0;
|
454
|
+
|
455
|
+
error:
|
456
|
+
uv__set_sys_error(process->loop, errno);
|
457
|
+
|
458
|
+
for (i = 0; i < stdio_count; i++) {
|
459
|
+
close(pipes[i][0]);
|
460
|
+
close(pipes[i][1]);
|
461
|
+
}
|
462
|
+
free(pipes);
|
463
|
+
|
464
|
+
return -1;
|
465
|
+
}
|
466
|
+
|
467
|
+
|
468
|
+
int uv_process_kill(uv_process_t* process, int signum) {
|
469
|
+
int r = kill(process->pid, signum);
|
470
|
+
|
471
|
+
if (r) {
|
472
|
+
uv__set_sys_error(process->loop, errno);
|
473
|
+
return -1;
|
474
|
+
} else {
|
475
|
+
return 0;
|
476
|
+
}
|
477
|
+
}
|
478
|
+
|
479
|
+
|
480
|
+
uv_err_t uv_kill(int pid, int signum) {
|
481
|
+
int r = kill(pid, signum);
|
482
|
+
|
483
|
+
if (r) {
|
484
|
+
return uv__new_sys_error(errno);
|
485
|
+
} else {
|
486
|
+
return uv_ok_;
|
487
|
+
}
|
488
|
+
}
|
489
|
+
|
490
|
+
|
491
|
+
void uv__process_close(uv_process_t* handle) {
|
492
|
+
/* TODO stop signal watcher when this is the last handle */
|
493
|
+
ngx_queue_remove(&handle->queue);
|
494
|
+
uv__handle_stop(handle);
|
495
|
+
}
|