rbuv 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +0 -1
- data/README.md +6 -1
- data/Rakefile +42 -0
- data/deps/libuv/.gitignore +34 -0
- data/deps/libuv/.mailmap +16 -0
- data/deps/libuv/AUTHORS +81 -0
- data/deps/libuv/ChangeLog +45 -0
- data/deps/libuv/LICENSE +41 -0
- data/deps/libuv/Makefile +53 -0
- data/deps/libuv/README.md +118 -0
- data/deps/libuv/build.mk +164 -0
- data/deps/libuv/checksparse.sh +230 -0
- data/deps/libuv/common.gypi +197 -0
- data/deps/libuv/config-mingw.mk +48 -0
- data/deps/libuv/config-unix.mk +167 -0
- data/deps/libuv/gyp_uv +98 -0
- data/deps/libuv/include/uv-private/ngx-queue.h +129 -0
- data/deps/libuv/include/uv-private/stdint-msvc2008.h +247 -0
- data/deps/libuv/include/uv-private/tree.h +768 -0
- data/deps/libuv/include/uv-private/uv-bsd.h +34 -0
- data/deps/libuv/include/uv-private/uv-darwin.h +61 -0
- data/deps/libuv/include/uv-private/uv-linux.h +34 -0
- data/deps/libuv/include/uv-private/uv-sunos.h +44 -0
- data/deps/libuv/include/uv-private/uv-unix.h +332 -0
- data/deps/libuv/include/uv-private/uv-win.h +585 -0
- data/deps/libuv/include/uv.h +1987 -0
- data/deps/libuv/src/fs-poll.c +248 -0
- data/deps/libuv/src/inet.c +298 -0
- data/deps/libuv/src/unix/aix.c +393 -0
- data/deps/libuv/src/unix/async.c +281 -0
- data/deps/libuv/src/unix/core.c +714 -0
- data/deps/libuv/src/unix/cygwin.c +93 -0
- data/deps/libuv/src/unix/darwin-proctitle.m +78 -0
- data/deps/libuv/src/unix/darwin.c +431 -0
- data/deps/libuv/src/unix/dl.c +83 -0
- data/deps/libuv/src/unix/error.c +109 -0
- data/deps/libuv/src/unix/freebsd.c +343 -0
- data/deps/libuv/src/unix/fs.c +869 -0
- data/deps/libuv/src/unix/fsevents.c +299 -0
- data/deps/libuv/src/unix/getaddrinfo.c +159 -0
- data/deps/libuv/src/unix/internal.h +259 -0
- data/deps/libuv/src/unix/kqueue.c +347 -0
- data/deps/libuv/src/unix/linux-core.c +724 -0
- data/deps/libuv/src/unix/linux-inotify.c +236 -0
- data/deps/libuv/src/unix/linux-syscalls.c +388 -0
- data/deps/libuv/src/unix/linux-syscalls.h +150 -0
- data/deps/libuv/src/unix/loop-watcher.c +64 -0
- data/deps/libuv/src/unix/loop.c +114 -0
- data/deps/libuv/src/unix/netbsd.c +353 -0
- data/deps/libuv/src/unix/openbsd.c +304 -0
- data/deps/libuv/src/unix/pipe.c +261 -0
- data/deps/libuv/src/unix/poll.c +108 -0
- data/deps/libuv/src/unix/process.c +501 -0
- data/deps/libuv/src/unix/proctitle.c +103 -0
- data/deps/libuv/src/unix/signal.c +455 -0
- data/deps/libuv/src/unix/stream.c +1380 -0
- data/deps/libuv/src/unix/sunos.c +647 -0
- data/deps/libuv/src/unix/tcp.c +357 -0
- data/deps/libuv/src/unix/thread.c +431 -0
- data/deps/libuv/src/unix/threadpool.c +286 -0
- data/deps/libuv/src/unix/timer.c +153 -0
- data/deps/libuv/src/unix/tty.c +179 -0
- data/deps/libuv/src/unix/udp.c +715 -0
- data/deps/libuv/src/uv-common.c +431 -0
- data/deps/libuv/src/uv-common.h +204 -0
- data/deps/libuv/src/version.c +60 -0
- data/deps/libuv/src/win/async.c +99 -0
- data/deps/libuv/src/win/atomicops-inl.h +56 -0
- data/deps/libuv/src/win/core.c +310 -0
- data/deps/libuv/src/win/dl.c +86 -0
- data/deps/libuv/src/win/error.c +164 -0
- data/deps/libuv/src/win/fs-event.c +506 -0
- data/deps/libuv/src/win/fs.c +1951 -0
- data/deps/libuv/src/win/getaddrinfo.c +365 -0
- data/deps/libuv/src/win/handle-inl.h +164 -0
- data/deps/libuv/src/win/handle.c +153 -0
- data/deps/libuv/src/win/internal.h +346 -0
- data/deps/libuv/src/win/loop-watcher.c +124 -0
- data/deps/libuv/src/win/pipe.c +1656 -0
- data/deps/libuv/src/win/poll.c +615 -0
- data/deps/libuv/src/win/process-stdio.c +503 -0
- data/deps/libuv/src/win/process.c +1048 -0
- data/deps/libuv/src/win/req-inl.h +224 -0
- data/deps/libuv/src/win/req.c +25 -0
- data/deps/libuv/src/win/signal.c +354 -0
- data/deps/libuv/src/win/stream-inl.h +67 -0
- data/deps/libuv/src/win/stream.c +198 -0
- data/deps/libuv/src/win/tcp.c +1422 -0
- data/deps/libuv/src/win/thread.c +666 -0
- data/deps/libuv/src/win/threadpool.c +82 -0
- data/deps/libuv/src/win/timer.c +230 -0
- data/deps/libuv/src/win/tty.c +1857 -0
- data/deps/libuv/src/win/udp.c +744 -0
- data/deps/libuv/src/win/util.c +946 -0
- data/deps/libuv/src/win/winapi.c +152 -0
- data/deps/libuv/src/win/winapi.h +4476 -0
- data/deps/libuv/src/win/winsock.c +560 -0
- data/deps/libuv/src/win/winsock.h +171 -0
- data/deps/libuv/test/benchmark-async-pummel.c +119 -0
- data/deps/libuv/test/benchmark-async.c +139 -0
- data/deps/libuv/test/benchmark-fs-stat.c +136 -0
- data/deps/libuv/test/benchmark-getaddrinfo.c +91 -0
- data/deps/libuv/test/benchmark-list.h +163 -0
- data/deps/libuv/test/benchmark-loop-count.c +90 -0
- data/deps/libuv/test/benchmark-million-async.c +112 -0
- data/deps/libuv/test/benchmark-million-timers.c +77 -0
- data/deps/libuv/test/benchmark-multi-accept.c +432 -0
- data/deps/libuv/test/benchmark-ping-pongs.c +212 -0
- data/deps/libuv/test/benchmark-pound.c +325 -0
- data/deps/libuv/test/benchmark-pump.c +459 -0
- data/deps/libuv/test/benchmark-sizes.c +45 -0
- data/deps/libuv/test/benchmark-spawn.c +163 -0
- data/deps/libuv/test/benchmark-tcp-write-batch.c +141 -0
- data/deps/libuv/test/benchmark-thread.c +64 -0
- data/deps/libuv/test/benchmark-udp-pummel.c +238 -0
- data/deps/libuv/test/blackhole-server.c +118 -0
- data/deps/libuv/test/dns-server.c +329 -0
- data/deps/libuv/test/echo-server.c +384 -0
- data/deps/libuv/test/fixtures/empty_file +0 -0
- data/deps/libuv/test/fixtures/load_error.node +1 -0
- data/deps/libuv/test/run-benchmarks.c +64 -0
- data/deps/libuv/test/run-tests.c +159 -0
- data/deps/libuv/test/runner-unix.c +328 -0
- data/deps/libuv/test/runner-unix.h +36 -0
- data/deps/libuv/test/runner-win.c +318 -0
- data/deps/libuv/test/runner-win.h +43 -0
- data/deps/libuv/test/runner.c +394 -0
- data/deps/libuv/test/runner.h +165 -0
- data/deps/libuv/test/task.h +122 -0
- data/deps/libuv/test/test-active.c +83 -0
- data/deps/libuv/test/test-async.c +136 -0
- data/deps/libuv/test/test-barrier.c +98 -0
- data/deps/libuv/test/test-callback-order.c +77 -0
- data/deps/libuv/test/test-callback-stack.c +204 -0
- data/deps/libuv/test/test-condvar.c +173 -0
- data/deps/libuv/test/test-connection-fail.c +150 -0
- data/deps/libuv/test/test-cwd-and-chdir.c +64 -0
- data/deps/libuv/test/test-delayed-accept.c +189 -0
- data/deps/libuv/test/test-dlerror.c +58 -0
- data/deps/libuv/test/test-embed.c +136 -0
- data/deps/libuv/test/test-error.c +59 -0
- data/deps/libuv/test/test-fail-always.c +29 -0
- data/deps/libuv/test/test-fs-event.c +504 -0
- data/deps/libuv/test/test-fs-poll.c +148 -0
- data/deps/libuv/test/test-fs.c +1899 -0
- data/deps/libuv/test/test-get-currentexe.c +63 -0
- data/deps/libuv/test/test-get-loadavg.c +36 -0
- data/deps/libuv/test/test-get-memory.c +38 -0
- data/deps/libuv/test/test-getaddrinfo.c +120 -0
- data/deps/libuv/test/test-getsockname.c +344 -0
- data/deps/libuv/test/test-hrtime.c +54 -0
- data/deps/libuv/test/test-idle.c +82 -0
- data/deps/libuv/test/test-ipc-send-recv.c +218 -0
- data/deps/libuv/test/test-ipc.c +625 -0
- data/deps/libuv/test/test-list.h +492 -0
- data/deps/libuv/test/test-loop-handles.c +337 -0
- data/deps/libuv/test/test-loop-stop.c +73 -0
- data/deps/libuv/test/test-multiple-listen.c +103 -0
- data/deps/libuv/test/test-mutexes.c +63 -0
- data/deps/libuv/test/test-pass-always.c +28 -0
- data/deps/libuv/test/test-ping-pong.c +250 -0
- data/deps/libuv/test/test-pipe-bind-error.c +144 -0
- data/deps/libuv/test/test-pipe-connect-error.c +98 -0
- data/deps/libuv/test/test-platform-output.c +87 -0
- data/deps/libuv/test/test-poll-close.c +73 -0
- data/deps/libuv/test/test-poll.c +575 -0
- data/deps/libuv/test/test-process-title.c +49 -0
- data/deps/libuv/test/test-ref.c +415 -0
- data/deps/libuv/test/test-run-nowait.c +46 -0
- data/deps/libuv/test/test-run-once.c +49 -0
- data/deps/libuv/test/test-semaphore.c +111 -0
- data/deps/libuv/test/test-shutdown-close.c +105 -0
- data/deps/libuv/test/test-shutdown-eof.c +184 -0
- data/deps/libuv/test/test-signal-multiple-loops.c +270 -0
- data/deps/libuv/test/test-signal.c +152 -0
- data/deps/libuv/test/test-spawn.c +938 -0
- data/deps/libuv/test/test-stdio-over-pipes.c +250 -0
- data/deps/libuv/test/test-tcp-bind-error.c +198 -0
- data/deps/libuv/test/test-tcp-bind6-error.c +159 -0
- data/deps/libuv/test/test-tcp-close-while-connecting.c +81 -0
- data/deps/libuv/test/test-tcp-close.c +130 -0
- data/deps/libuv/test/test-tcp-connect-error-after-write.c +96 -0
- data/deps/libuv/test/test-tcp-connect-error.c +71 -0
- data/deps/libuv/test/test-tcp-connect-timeout.c +86 -0
- data/deps/libuv/test/test-tcp-connect6-error.c +69 -0
- data/deps/libuv/test/test-tcp-flags.c +52 -0
- data/deps/libuv/test/test-tcp-open.c +175 -0
- data/deps/libuv/test/test-tcp-read-stop.c +73 -0
- data/deps/libuv/test/test-tcp-shutdown-after-write.c +132 -0
- data/deps/libuv/test/test-tcp-unexpected-read.c +114 -0
- data/deps/libuv/test/test-tcp-write-to-half-open-connection.c +136 -0
- data/deps/libuv/test/test-tcp-writealot.c +171 -0
- data/deps/libuv/test/test-thread.c +183 -0
- data/deps/libuv/test/test-threadpool-cancel.c +311 -0
- data/deps/libuv/test/test-threadpool.c +77 -0
- data/deps/libuv/test/test-timer-again.c +142 -0
- data/deps/libuv/test/test-timer.c +266 -0
- data/deps/libuv/test/test-tty.c +111 -0
- data/deps/libuv/test/test-udp-dgram-too-big.c +87 -0
- data/deps/libuv/test/test-udp-ipv6.c +158 -0
- data/deps/libuv/test/test-udp-multicast-join.c +140 -0
- data/deps/libuv/test/test-udp-multicast-ttl.c +87 -0
- data/deps/libuv/test/test-udp-open.c +154 -0
- data/deps/libuv/test/test-udp-options.c +87 -0
- data/deps/libuv/test/test-udp-send-and-recv.c +210 -0
- data/deps/libuv/test/test-util.c +97 -0
- data/deps/libuv/test/test-walk-handles.c +78 -0
- data/deps/libuv/uv.gyp +431 -0
- data/deps/libuv/vcbuild.bat +128 -0
- data/ext/rbuv/debug.h +27 -0
- data/ext/rbuv/error.c +7 -0
- data/ext/rbuv/error.h +10 -0
- data/ext/rbuv/extconf.rb +35 -0
- data/ext/rbuv/handle.c +40 -0
- data/ext/rbuv/handle.h +14 -0
- data/ext/rbuv/libuv.mk +12 -0
- data/ext/rbuv/loop.c +50 -0
- data/ext/rbuv/loop.h +13 -0
- data/ext/rbuv/rbuv.c +15 -0
- data/ext/rbuv/rbuv.h +27 -0
- data/ext/rbuv/timer.c +133 -0
- data/ext/rbuv/timer.h +13 -0
- data/lib/rbuv/timer.rb +7 -0
- data/lib/rbuv/version.rb +1 -1
- data/lib/rbuv.rb +24 -2
- data/rbuv.gemspec +5 -1
- data/spec/spec_helper.rb +22 -0
- data/spec/timer_spec.rb +144 -0
- metadata +278 -9
@@ -0,0 +1,455 @@
|
|
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 <assert.h>
|
25
|
+
#include <errno.h>
|
26
|
+
#include <signal.h>
|
27
|
+
#include <stdlib.h>
|
28
|
+
#include <string.h>
|
29
|
+
#include <unistd.h>
|
30
|
+
|
31
|
+
|
32
|
+
typedef struct {
|
33
|
+
uv_signal_t* handle;
|
34
|
+
int signum;
|
35
|
+
} uv__signal_msg_t;
|
36
|
+
|
37
|
+
RB_HEAD(uv__signal_tree_s, uv_signal_s);
|
38
|
+
|
39
|
+
|
40
|
+
static int uv__signal_unlock();
|
41
|
+
static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, unsigned int events);
|
42
|
+
static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2);
|
43
|
+
static void uv__signal_stop(uv_signal_t* handle);
|
44
|
+
|
45
|
+
|
46
|
+
static pthread_once_t uv__signal_global_init_guard = PTHREAD_ONCE_INIT;
|
47
|
+
static struct uv__signal_tree_s uv__signal_tree =
|
48
|
+
RB_INITIALIZER(uv__signal_tree);
|
49
|
+
static int uv__signal_lock_pipefd[2];
|
50
|
+
|
51
|
+
|
52
|
+
RB_GENERATE_STATIC(uv__signal_tree_s,
|
53
|
+
uv_signal_s, tree_entry,
|
54
|
+
uv__signal_compare)
|
55
|
+
|
56
|
+
|
57
|
+
static void uv__signal_global_init(void) {
|
58
|
+
if (uv__make_pipe(uv__signal_lock_pipefd, 0))
|
59
|
+
abort();
|
60
|
+
|
61
|
+
if (uv__signal_unlock())
|
62
|
+
abort();
|
63
|
+
}
|
64
|
+
|
65
|
+
|
66
|
+
void uv__signal_global_once_init(void) {
|
67
|
+
pthread_once(&uv__signal_global_init_guard, uv__signal_global_init);
|
68
|
+
}
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
static int uv__signal_lock(void) {
|
73
|
+
int r;
|
74
|
+
char data;
|
75
|
+
|
76
|
+
do {
|
77
|
+
r = read(uv__signal_lock_pipefd[0], &data, sizeof data);
|
78
|
+
} while (r < 0 && errno == EINTR);
|
79
|
+
|
80
|
+
return (r < 0) ? -1 : 0;
|
81
|
+
}
|
82
|
+
|
83
|
+
|
84
|
+
static int uv__signal_unlock(void) {
|
85
|
+
int r;
|
86
|
+
char data = 42;
|
87
|
+
|
88
|
+
do {
|
89
|
+
r = write(uv__signal_lock_pipefd[1], &data, sizeof data);
|
90
|
+
} while (r < 0 && errno == EINTR);
|
91
|
+
|
92
|
+
return (r < 0) ? -1 : 0;
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
static void uv__signal_block_and_lock(sigset_t* saved_sigmask) {
|
97
|
+
sigset_t new_mask;
|
98
|
+
|
99
|
+
if (sigfillset(&new_mask))
|
100
|
+
abort();
|
101
|
+
|
102
|
+
if (pthread_sigmask(SIG_SETMASK, &new_mask, saved_sigmask))
|
103
|
+
abort();
|
104
|
+
|
105
|
+
if (uv__signal_lock())
|
106
|
+
abort();
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
static void uv__signal_unlock_and_unblock(sigset_t* saved_sigmask) {
|
111
|
+
if (uv__signal_unlock())
|
112
|
+
abort();
|
113
|
+
|
114
|
+
if (pthread_sigmask(SIG_SETMASK, saved_sigmask, NULL))
|
115
|
+
abort();
|
116
|
+
}
|
117
|
+
|
118
|
+
|
119
|
+
inline static uv_signal_t* uv__signal_first_handle(int signum) {
|
120
|
+
/* This function must be called with the signal lock held. */
|
121
|
+
uv_signal_t lookup;
|
122
|
+
uv_signal_t* handle;
|
123
|
+
|
124
|
+
lookup.signum = signum;
|
125
|
+
lookup.loop = NULL;
|
126
|
+
|
127
|
+
handle = RB_NFIND(uv__signal_tree_s, &uv__signal_tree, &lookup);
|
128
|
+
|
129
|
+
if (handle != NULL && handle->signum == signum)
|
130
|
+
return handle;
|
131
|
+
|
132
|
+
return NULL;
|
133
|
+
}
|
134
|
+
|
135
|
+
|
136
|
+
static void uv__signal_handler(int signum) {
|
137
|
+
uv__signal_msg_t msg;
|
138
|
+
uv_signal_t* handle;
|
139
|
+
int saved_errno;
|
140
|
+
|
141
|
+
saved_errno = errno;
|
142
|
+
memset(&msg, 0, sizeof msg);
|
143
|
+
|
144
|
+
uv__signal_lock();
|
145
|
+
|
146
|
+
for (handle = uv__signal_first_handle(signum);
|
147
|
+
handle != NULL && handle->signum == signum;
|
148
|
+
handle = RB_NEXT(uv__signal_tree_s, &uv__signal_tree, handle)) {
|
149
|
+
int r;
|
150
|
+
|
151
|
+
msg.signum = signum;
|
152
|
+
msg.handle = handle;
|
153
|
+
|
154
|
+
/* write() should be atomic for small data chunks, so the entire message
|
155
|
+
* should be written at once. In theory the pipe could become full, in
|
156
|
+
* which case the user is out of luck.
|
157
|
+
*/
|
158
|
+
do {
|
159
|
+
r = write(handle->loop->signal_pipefd[1], &msg, sizeof msg);
|
160
|
+
} while (r == -1 && errno == EINTR);
|
161
|
+
|
162
|
+
assert(r == sizeof msg ||
|
163
|
+
(r == -1 && errno != EAGAIN && errno != EWOULDBLOCK));
|
164
|
+
|
165
|
+
if (r != -1)
|
166
|
+
handle->caught_signals++;
|
167
|
+
}
|
168
|
+
|
169
|
+
uv__signal_unlock();
|
170
|
+
errno = saved_errno;
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
static uv_err_t uv__signal_register_handler(int signum) {
|
175
|
+
/* When this function is called, the signal lock must be held. */
|
176
|
+
struct sigaction sa;
|
177
|
+
|
178
|
+
/* XXX use a separate signal stack? */
|
179
|
+
memset(&sa, 0, sizeof(sa));
|
180
|
+
if (sigfillset(&sa.sa_mask))
|
181
|
+
abort();
|
182
|
+
sa.sa_handler = uv__signal_handler;
|
183
|
+
|
184
|
+
/* XXX save old action so we can restore it later on? */
|
185
|
+
if (sigaction(signum, &sa, NULL))
|
186
|
+
return uv__new_sys_error(errno);
|
187
|
+
|
188
|
+
return uv_ok_;
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
static void uv__signal_unregister_handler(int signum) {
|
193
|
+
/* When this function is called, the signal lock must be held. */
|
194
|
+
struct sigaction sa;
|
195
|
+
|
196
|
+
memset(&sa, 0, sizeof(sa));
|
197
|
+
sa.sa_handler = SIG_DFL;
|
198
|
+
|
199
|
+
/* sigaction can only fail with EINVAL or EFAULT; an attempt to deregister a
|
200
|
+
* signal implies that it was successfully registered earlier, so EINVAL
|
201
|
+
* should never happen.
|
202
|
+
*/
|
203
|
+
if (sigaction(signum, &sa, NULL))
|
204
|
+
abort();
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
static int uv__signal_loop_once_init(uv_loop_t* loop) {
|
209
|
+
/* Return if already initialized. */
|
210
|
+
if (loop->signal_pipefd[0] != -1)
|
211
|
+
return 0;
|
212
|
+
|
213
|
+
if (uv__make_pipe(loop->signal_pipefd, UV__F_NONBLOCK))
|
214
|
+
return -1;
|
215
|
+
|
216
|
+
uv__io_init(&loop->signal_io_watcher,
|
217
|
+
uv__signal_event,
|
218
|
+
loop->signal_pipefd[0]);
|
219
|
+
uv__io_start(loop, &loop->signal_io_watcher, UV__POLLIN);
|
220
|
+
|
221
|
+
return 0;
|
222
|
+
}
|
223
|
+
|
224
|
+
|
225
|
+
void uv__signal_loop_cleanup(uv_loop_t* loop) {
|
226
|
+
ngx_queue_t* q;
|
227
|
+
|
228
|
+
/* Stop all the signal watchers that are still attached to this loop. This
|
229
|
+
* ensures that the (shared) signal tree doesn't contain any invalid entries
|
230
|
+
* entries, and that signal handlers are removed when appropriate.
|
231
|
+
*/
|
232
|
+
ngx_queue_foreach(q, &loop->handle_queue) {
|
233
|
+
uv_handle_t* handle = ngx_queue_data(q, uv_handle_t, handle_queue);
|
234
|
+
|
235
|
+
if (handle->type == UV_SIGNAL)
|
236
|
+
uv__signal_stop((uv_signal_t*) handle);
|
237
|
+
}
|
238
|
+
|
239
|
+
if (loop->signal_pipefd[0] != -1) {
|
240
|
+
close(loop->signal_pipefd[0]);
|
241
|
+
loop->signal_pipefd[0] = -1;
|
242
|
+
}
|
243
|
+
|
244
|
+
if (loop->signal_pipefd[1] != -1) {
|
245
|
+
close(loop->signal_pipefd[1]);
|
246
|
+
loop->signal_pipefd[1] = -1;
|
247
|
+
}
|
248
|
+
}
|
249
|
+
|
250
|
+
|
251
|
+
int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
|
252
|
+
if (uv__signal_loop_once_init(loop))
|
253
|
+
return uv__set_sys_error(loop, errno);
|
254
|
+
|
255
|
+
uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
|
256
|
+
handle->signum = 0;
|
257
|
+
handle->caught_signals = 0;
|
258
|
+
handle->dispatched_signals = 0;
|
259
|
+
|
260
|
+
return 0;
|
261
|
+
}
|
262
|
+
|
263
|
+
|
264
|
+
void uv__signal_close(uv_signal_t* handle) {
|
265
|
+
|
266
|
+
uv__signal_stop(handle);
|
267
|
+
|
268
|
+
/* If there are any caught signals "trapped" in the signal pipe, we can't
|
269
|
+
* call the close callback yet. Otherwise, add the handle to the finish_close
|
270
|
+
* queue.
|
271
|
+
*/
|
272
|
+
if (handle->caught_signals == handle->dispatched_signals) {
|
273
|
+
uv__make_close_pending((uv_handle_t*) handle);
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
|
278
|
+
int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
|
279
|
+
sigset_t saved_sigmask;
|
280
|
+
|
281
|
+
assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
|
282
|
+
|
283
|
+
/* If the user supplies signum == 0, then return an error already. If the
|
284
|
+
* signum is otherwise invalid then uv__signal_register will find out
|
285
|
+
* eventually.
|
286
|
+
*/
|
287
|
+
if (signum == 0) {
|
288
|
+
uv__set_artificial_error(handle->loop, UV_EINVAL);
|
289
|
+
return -1;
|
290
|
+
}
|
291
|
+
|
292
|
+
/* Short circuit: if the signal watcher is already watching {signum} don't
|
293
|
+
* go through the process of deregistering and registering the handler.
|
294
|
+
* Additionally, this avoids pending signals getting lost in the small time
|
295
|
+
* time frame that handle->signum == 0.
|
296
|
+
*/
|
297
|
+
if (signum == handle->signum) {
|
298
|
+
handle->signal_cb = signal_cb;
|
299
|
+
return 0;
|
300
|
+
}
|
301
|
+
|
302
|
+
/* If the signal handler was already active, stop it first. */
|
303
|
+
if (handle->signum != 0) {
|
304
|
+
uv__signal_stop(handle);
|
305
|
+
}
|
306
|
+
|
307
|
+
uv__signal_block_and_lock(&saved_sigmask);
|
308
|
+
|
309
|
+
/* If at this point there are no active signal watchers for this signum (in
|
310
|
+
* any of the loops), it's time to try and register a handler for it here.
|
311
|
+
*/
|
312
|
+
if (uv__signal_first_handle(signum) == NULL) {
|
313
|
+
uv_err_t err = uv__signal_register_handler(signum);
|
314
|
+
if (err.code != UV_OK) {
|
315
|
+
/* Registering the signal handler failed. Must be an invalid signal. */
|
316
|
+
handle->loop->last_err = err;
|
317
|
+
uv__signal_unlock_and_unblock(&saved_sigmask);
|
318
|
+
return -1;
|
319
|
+
}
|
320
|
+
}
|
321
|
+
|
322
|
+
handle->signum = signum;
|
323
|
+
RB_INSERT(uv__signal_tree_s, &uv__signal_tree, handle);
|
324
|
+
|
325
|
+
uv__signal_unlock_and_unblock(&saved_sigmask);
|
326
|
+
|
327
|
+
handle->signal_cb = signal_cb;
|
328
|
+
uv__handle_start(handle);
|
329
|
+
|
330
|
+
return 0;
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
static void uv__signal_event(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
|
335
|
+
uv__signal_msg_t* msg;
|
336
|
+
uv_signal_t* handle;
|
337
|
+
char buf[sizeof(uv__signal_msg_t) * 32];
|
338
|
+
size_t bytes, end, i;
|
339
|
+
int r;
|
340
|
+
|
341
|
+
bytes = 0;
|
342
|
+
|
343
|
+
do {
|
344
|
+
r = read(loop->signal_pipefd[0], buf + bytes, sizeof(buf) - bytes);
|
345
|
+
|
346
|
+
if (r == -1 && errno == EINTR)
|
347
|
+
continue;
|
348
|
+
|
349
|
+
if (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
350
|
+
/* If there are bytes in the buffer already (which really is extremely
|
351
|
+
* unlikely if possible at all) we can't exit the function here. We'll
|
352
|
+
* spin until more bytes are read instead.
|
353
|
+
*/
|
354
|
+
if (bytes > 0)
|
355
|
+
continue;
|
356
|
+
|
357
|
+
/* Otherwise, there was nothing there. */
|
358
|
+
return;
|
359
|
+
}
|
360
|
+
|
361
|
+
/* Other errors really should never happen. */
|
362
|
+
if (r == -1)
|
363
|
+
abort();
|
364
|
+
|
365
|
+
bytes += r;
|
366
|
+
|
367
|
+
/* `end` is rounded down to a multiple of sizeof(uv__signal_msg_t). */
|
368
|
+
end = (bytes / sizeof(uv__signal_msg_t)) * sizeof(uv__signal_msg_t);
|
369
|
+
|
370
|
+
for (i = 0; i < end; i += sizeof(uv__signal_msg_t)) {
|
371
|
+
msg = (uv__signal_msg_t*) (buf + i);
|
372
|
+
handle = msg->handle;
|
373
|
+
|
374
|
+
if (msg->signum == handle->signum) {
|
375
|
+
assert(!(handle->flags & UV_CLOSING));
|
376
|
+
handle->signal_cb(handle, handle->signum);
|
377
|
+
}
|
378
|
+
|
379
|
+
handle->dispatched_signals++;
|
380
|
+
|
381
|
+
/* If uv_close was called while there were caught signals that were not
|
382
|
+
* yet dispatched, the uv__finish_close was deferred. Make close pending
|
383
|
+
* now if this has happened.
|
384
|
+
*/
|
385
|
+
if ((handle->flags & UV_CLOSING) &&
|
386
|
+
(handle->caught_signals == handle->dispatched_signals)) {
|
387
|
+
uv__make_close_pending((uv_handle_t*) handle);
|
388
|
+
}
|
389
|
+
}
|
390
|
+
|
391
|
+
bytes -= end;
|
392
|
+
|
393
|
+
/* If there are any "partial" messages left, move them to the start of the
|
394
|
+
* the buffer, and spin. This should not happen.
|
395
|
+
*/
|
396
|
+
if (bytes) {
|
397
|
+
memmove(buf, buf + end, bytes);
|
398
|
+
continue;
|
399
|
+
}
|
400
|
+
} while (end == sizeof buf);
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
|
405
|
+
/* Compare signums first so all watchers with the same signnum end up
|
406
|
+
* adjacent.
|
407
|
+
*/
|
408
|
+
if (w1->signum < w2->signum) return -1;
|
409
|
+
if (w1->signum > w2->signum) return 1;
|
410
|
+
|
411
|
+
/* Sort by loop pointer, so we can easily look up the first item after
|
412
|
+
* { .signum = x, .loop = NULL }.
|
413
|
+
*/
|
414
|
+
if (w1->loop < w2->loop) return -1;
|
415
|
+
if (w1->loop > w2->loop) return 1;
|
416
|
+
|
417
|
+
if (w1 < w2) return -1;
|
418
|
+
if (w1 > w2) return 1;
|
419
|
+
|
420
|
+
return 0;
|
421
|
+
}
|
422
|
+
|
423
|
+
|
424
|
+
int uv_signal_stop(uv_signal_t* handle) {
|
425
|
+
assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
|
426
|
+
uv__signal_stop(handle);
|
427
|
+
return 0;
|
428
|
+
}
|
429
|
+
|
430
|
+
|
431
|
+
static void uv__signal_stop(uv_signal_t* handle) {
|
432
|
+
uv_signal_t* removed_handle;
|
433
|
+
sigset_t saved_sigmask;
|
434
|
+
|
435
|
+
/* If the watcher wasn't started, this is a no-op. */
|
436
|
+
if (handle->signum == 0)
|
437
|
+
return;
|
438
|
+
|
439
|
+
uv__signal_block_and_lock(&saved_sigmask);
|
440
|
+
|
441
|
+
removed_handle = RB_REMOVE(uv__signal_tree_s, &uv__signal_tree, handle);
|
442
|
+
assert(removed_handle == handle);
|
443
|
+
(void) removed_handle;
|
444
|
+
|
445
|
+
/* Check if there are other active signal watchers observing this signal. If
|
446
|
+
* not, unregister the signal handler.
|
447
|
+
*/
|
448
|
+
if (uv__signal_first_handle(handle->signum) == NULL)
|
449
|
+
uv__signal_unregister_handler(handle->signum);
|
450
|
+
|
451
|
+
uv__signal_unlock_and_unblock(&saved_sigmask);
|
452
|
+
|
453
|
+
handle->signum = 0;
|
454
|
+
uv__handle_stop(handle);
|
455
|
+
}
|