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,102 @@
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 <stdlib.h>
25
+ #include <string.h>
26
+
27
+ extern void uv__set_process_title(const char* title);
28
+
29
+ static void* args_mem;
30
+
31
+ static struct {
32
+ char* str;
33
+ size_t len;
34
+ } process_title;
35
+
36
+
37
+ char** uv_setup_args(int argc, char** argv) {
38
+ char** new_argv;
39
+ size_t size;
40
+ char* s;
41
+ int i;
42
+
43
+ if (argc <= 0)
44
+ return argv;
45
+
46
+ /* Calculate how much memory we need for the argv strings. */
47
+ size = 0;
48
+ for (i = 0; i < argc; i++)
49
+ size += strlen(argv[i]) + 1;
50
+
51
+ process_title.str = argv[0];
52
+ process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0];
53
+ assert(process_title.len + 1 == size); /* argv memory should be adjacent. */
54
+
55
+ /* Add space for the argv pointers. */
56
+ size += (argc + 1) * sizeof(char*);
57
+
58
+ new_argv = malloc(size);
59
+ if (new_argv == NULL)
60
+ return argv;
61
+ args_mem = new_argv;
62
+
63
+ /* Copy over the strings and set up the pointer table. */
64
+ s = (char*) &new_argv[argc + 1];
65
+ for (i = 0; i < argc; i++) {
66
+ size = strlen(argv[i]) + 1;
67
+ memcpy(s, argv[i], size);
68
+ new_argv[i] = s;
69
+ s += size;
70
+ }
71
+ new_argv[i] = NULL;
72
+
73
+ return new_argv;
74
+ }
75
+
76
+
77
+ int uv_set_process_title(const char* title) {
78
+ if (process_title.len == 0)
79
+ return 0;
80
+
81
+ /* No need to terminate, byte after is always '\0'. */
82
+ strncpy(process_title.str, title, process_title.len);
83
+ uv__set_process_title(title);
84
+
85
+ return 0;
86
+ }
87
+
88
+
89
+ int uv_get_process_title(char* buffer, size_t size) {
90
+ if (process_title.len > 0)
91
+ strncpy(buffer, process_title.str, size);
92
+ else if (size > 0)
93
+ buffer[0] = '\0';
94
+
95
+ return 0;
96
+ }
97
+
98
+
99
+ UV_DESTRUCTOR(static void free_args_mem(void)) {
100
+ free(args_mem); /* Keep valgrind happy. */
101
+ args_mem = NULL;
102
+ }
@@ -0,0 +1,103 @@
1
+ /* Copyright (c) 2013, Sony Mobile Communications AB
2
+ * Copyright (c) 2012, Google Inc.
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are
7
+ met:
8
+
9
+ * Redistributions of source code must retain the above copyright
10
+ notice, this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above
12
+ copyright notice, this list of conditions and the following disclaimer
13
+ in the documentation and/or other materials provided with the
14
+ distribution.
15
+ * Neither the name of Google Inc. nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ /* Android versions < 4.1 have a broken pthread_sigmask.
33
+ * Note that this block of code must come before any inclusion of
34
+ * pthread-fixes.h so that the real pthread_sigmask can be referenced.
35
+ * */
36
+ #include <errno.h>
37
+ #include <pthread.h>
38
+
39
+ int uv__pthread_sigmask(int how, const sigset_t* set, sigset_t* oset) {
40
+ static int workaround;
41
+
42
+ if (workaround) {
43
+ return sigprocmask(how, set, oset);
44
+ } else if (pthread_sigmask(how, set, oset)) {
45
+ if (errno == EINVAL && sigprocmask(how, set, oset) == 0) {
46
+ workaround = 1;
47
+ return 0;
48
+ } else {
49
+ return -1;
50
+ }
51
+ } else {
52
+ return 0;
53
+ }
54
+ }
55
+
56
+ /*Android doesn't provide pthread_barrier_t for now.*/
57
+ #ifndef PTHREAD_BARRIER_SERIAL_THREAD
58
+
59
+ #include "pthread-fixes.h"
60
+
61
+ int pthread_barrier_init(pthread_barrier_t* barrier,
62
+ const void* barrier_attr,
63
+ unsigned count) {
64
+ barrier->count = count;
65
+ pthread_mutex_init(&barrier->mutex, NULL);
66
+ pthread_cond_init(&barrier->cond, NULL);
67
+ return 0;
68
+ }
69
+
70
+ int pthread_barrier_wait(pthread_barrier_t* barrier) {
71
+ /* Lock the mutex*/
72
+ pthread_mutex_lock(&barrier->mutex);
73
+ /* Decrement the count. If this is the first thread to reach 0, wake up
74
+ waiters, unlock the mutex, then return PTHREAD_BARRIER_SERIAL_THREAD.*/
75
+ if (--barrier->count == 0) {
76
+ /* First thread to reach the barrier */
77
+ pthread_cond_broadcast(&barrier->cond);
78
+ pthread_mutex_unlock(&barrier->mutex);
79
+ return PTHREAD_BARRIER_SERIAL_THREAD;
80
+ }
81
+ /* Otherwise, wait for other threads until the count reaches 0, then
82
+ return 0 to indicate this is not the first thread.*/
83
+ do {
84
+ pthread_cond_wait(&barrier->cond, &barrier->mutex);
85
+ } while (barrier->count > 0);
86
+
87
+ pthread_mutex_unlock(&barrier->mutex);
88
+ return 0;
89
+ }
90
+
91
+ int pthread_barrier_destroy(pthread_barrier_t *barrier) {
92
+ barrier->count = 0;
93
+ pthread_cond_destroy(&barrier->cond);
94
+ pthread_mutex_destroy(&barrier->mutex);
95
+ return 0;
96
+ }
97
+
98
+ #endif /* defined(PTHREAD_BARRIER_SERIAL_THREAD) */
99
+
100
+ int pthread_yield(void) {
101
+ sched_yield();
102
+ return 0;
103
+ }
@@ -0,0 +1,465 @@
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(void);
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
+ 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
+ if (uv__signal_lock()) {
145
+ errno = saved_errno;
146
+ return;
147
+ }
148
+
149
+ for (handle = uv__signal_first_handle(signum);
150
+ handle != NULL && handle->signum == signum;
151
+ handle = RB_NEXT(uv__signal_tree_s, &uv__signal_tree, handle)) {
152
+ int r;
153
+
154
+ msg.signum = signum;
155
+ msg.handle = handle;
156
+
157
+ /* write() should be atomic for small data chunks, so the entire message
158
+ * should be written at once. In theory the pipe could become full, in
159
+ * which case the user is out of luck.
160
+ */
161
+ do {
162
+ r = write(handle->loop->signal_pipefd[1], &msg, sizeof msg);
163
+ } while (r == -1 && errno == EINTR);
164
+
165
+ assert(r == sizeof msg ||
166
+ (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)));
167
+
168
+ if (r != -1)
169
+ handle->caught_signals++;
170
+ }
171
+
172
+ uv__signal_unlock();
173
+ errno = saved_errno;
174
+ }
175
+
176
+
177
+ static int uv__signal_register_handler(int signum) {
178
+ /* When this function is called, the signal lock must be held. */
179
+ struct sigaction sa;
180
+
181
+ /* XXX use a separate signal stack? */
182
+ memset(&sa, 0, sizeof(sa));
183
+ if (sigfillset(&sa.sa_mask))
184
+ abort();
185
+ sa.sa_handler = uv__signal_handler;
186
+
187
+ /* XXX save old action so we can restore it later on? */
188
+ if (sigaction(signum, &sa, NULL))
189
+ return -errno;
190
+
191
+ return 0;
192
+ }
193
+
194
+
195
+ static void uv__signal_unregister_handler(int signum) {
196
+ /* When this function is called, the signal lock must be held. */
197
+ struct sigaction sa;
198
+
199
+ memset(&sa, 0, sizeof(sa));
200
+ sa.sa_handler = SIG_DFL;
201
+
202
+ /* sigaction can only fail with EINVAL or EFAULT; an attempt to deregister a
203
+ * signal implies that it was successfully registered earlier, so EINVAL
204
+ * should never happen.
205
+ */
206
+ if (sigaction(signum, &sa, NULL))
207
+ abort();
208
+ }
209
+
210
+
211
+ static int uv__signal_loop_once_init(uv_loop_t* loop) {
212
+ int err;
213
+
214
+ /* Return if already initialized. */
215
+ if (loop->signal_pipefd[0] != -1)
216
+ return 0;
217
+
218
+ err = uv__make_pipe(loop->signal_pipefd, UV__F_NONBLOCK);
219
+ if (err)
220
+ return err;
221
+
222
+ uv__io_init(&loop->signal_io_watcher,
223
+ uv__signal_event,
224
+ loop->signal_pipefd[0]);
225
+ uv__io_start(loop, &loop->signal_io_watcher, UV__POLLIN);
226
+
227
+ return 0;
228
+ }
229
+
230
+
231
+ void uv__signal_loop_cleanup(uv_loop_t* loop) {
232
+ QUEUE* q;
233
+
234
+ /* Stop all the signal watchers that are still attached to this loop. This
235
+ * ensures that the (shared) signal tree doesn't contain any invalid entries
236
+ * entries, and that signal handlers are removed when appropriate.
237
+ */
238
+ QUEUE_FOREACH(q, &loop->handle_queue) {
239
+ uv_handle_t* handle = QUEUE_DATA(q, uv_handle_t, handle_queue);
240
+
241
+ if (handle->type == UV_SIGNAL)
242
+ uv__signal_stop((uv_signal_t*) handle);
243
+ }
244
+
245
+ if (loop->signal_pipefd[0] != -1) {
246
+ uv__close(loop->signal_pipefd[0]);
247
+ loop->signal_pipefd[0] = -1;
248
+ }
249
+
250
+ if (loop->signal_pipefd[1] != -1) {
251
+ uv__close(loop->signal_pipefd[1]);
252
+ loop->signal_pipefd[1] = -1;
253
+ }
254
+ }
255
+
256
+
257
+ int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
258
+ int err;
259
+
260
+ err = uv__signal_loop_once_init(loop);
261
+ if (err)
262
+ return err;
263
+
264
+ uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
265
+ handle->signum = 0;
266
+ handle->caught_signals = 0;
267
+ handle->dispatched_signals = 0;
268
+
269
+ return 0;
270
+ }
271
+
272
+
273
+ void uv__signal_close(uv_signal_t* handle) {
274
+
275
+ uv__signal_stop(handle);
276
+
277
+ /* If there are any caught signals "trapped" in the signal pipe, we can't
278
+ * call the close callback yet. Otherwise, add the handle to the finish_close
279
+ * queue.
280
+ */
281
+ if (handle->caught_signals == handle->dispatched_signals) {
282
+ uv__make_close_pending((uv_handle_t*) handle);
283
+ }
284
+ }
285
+
286
+
287
+ int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
288
+ sigset_t saved_sigmask;
289
+ int err;
290
+
291
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
292
+
293
+ /* If the user supplies signum == 0, then return an error already. If the
294
+ * signum is otherwise invalid then uv__signal_register will find out
295
+ * eventually.
296
+ */
297
+ if (signum == 0)
298
+ return -EINVAL;
299
+
300
+ /* Short circuit: if the signal watcher is already watching {signum} don't
301
+ * go through the process of deregistering and registering the handler.
302
+ * Additionally, this avoids pending signals getting lost in the small time
303
+ * time frame that handle->signum == 0.
304
+ */
305
+ if (signum == handle->signum) {
306
+ handle->signal_cb = signal_cb;
307
+ return 0;
308
+ }
309
+
310
+ /* If the signal handler was already active, stop it first. */
311
+ if (handle->signum != 0) {
312
+ uv__signal_stop(handle);
313
+ }
314
+
315
+ uv__signal_block_and_lock(&saved_sigmask);
316
+
317
+ /* If at this point there are no active signal watchers for this signum (in
318
+ * any of the loops), it's time to try and register a handler for it here.
319
+ */
320
+ if (uv__signal_first_handle(signum) == NULL) {
321
+ err = uv__signal_register_handler(signum);
322
+ if (err) {
323
+ /* Registering the signal handler failed. Must be an invalid signal. */
324
+ uv__signal_unlock_and_unblock(&saved_sigmask);
325
+ return err;
326
+ }
327
+ }
328
+
329
+ handle->signum = signum;
330
+ RB_INSERT(uv__signal_tree_s, &uv__signal_tree, handle);
331
+
332
+ uv__signal_unlock_and_unblock(&saved_sigmask);
333
+
334
+ handle->signal_cb = signal_cb;
335
+ uv__handle_start(handle);
336
+
337
+ return 0;
338
+ }
339
+
340
+
341
+ static void uv__signal_event(uv_loop_t* loop,
342
+ uv__io_t* w,
343
+ unsigned int events) {
344
+ uv__signal_msg_t* msg;
345
+ uv_signal_t* handle;
346
+ char buf[sizeof(uv__signal_msg_t) * 32];
347
+ size_t bytes, end, i;
348
+ int r;
349
+
350
+ bytes = 0;
351
+ end = 0;
352
+
353
+ do {
354
+ r = read(loop->signal_pipefd[0], buf + bytes, sizeof(buf) - bytes);
355
+
356
+ if (r == -1 && errno == EINTR)
357
+ continue;
358
+
359
+ if (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
360
+ /* If there are bytes in the buffer already (which really is extremely
361
+ * unlikely if possible at all) we can't exit the function here. We'll
362
+ * spin until more bytes are read instead.
363
+ */
364
+ if (bytes > 0)
365
+ continue;
366
+
367
+ /* Otherwise, there was nothing there. */
368
+ return;
369
+ }
370
+
371
+ /* Other errors really should never happen. */
372
+ if (r == -1)
373
+ abort();
374
+
375
+ bytes += r;
376
+
377
+ /* `end` is rounded down to a multiple of sizeof(uv__signal_msg_t). */
378
+ end = (bytes / sizeof(uv__signal_msg_t)) * sizeof(uv__signal_msg_t);
379
+
380
+ for (i = 0; i < end; i += sizeof(uv__signal_msg_t)) {
381
+ msg = (uv__signal_msg_t*) (buf + i);
382
+ handle = msg->handle;
383
+
384
+ if (msg->signum == handle->signum) {
385
+ assert(!(handle->flags & UV_CLOSING));
386
+ handle->signal_cb(handle, handle->signum);
387
+ }
388
+
389
+ handle->dispatched_signals++;
390
+
391
+ /* If uv_close was called while there were caught signals that were not
392
+ * yet dispatched, the uv__finish_close was deferred. Make close pending
393
+ * now if this has happened.
394
+ */
395
+ if ((handle->flags & UV_CLOSING) &&
396
+ (handle->caught_signals == handle->dispatched_signals)) {
397
+ uv__make_close_pending((uv_handle_t*) handle);
398
+ }
399
+ }
400
+
401
+ bytes -= end;
402
+
403
+ /* If there are any "partial" messages left, move them to the start of the
404
+ * the buffer, and spin. This should not happen.
405
+ */
406
+ if (bytes) {
407
+ memmove(buf, buf + end, bytes);
408
+ continue;
409
+ }
410
+ } while (end == sizeof buf);
411
+ }
412
+
413
+
414
+ static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
415
+ /* Compare signums first so all watchers with the same signnum end up
416
+ * adjacent.
417
+ */
418
+ if (w1->signum < w2->signum) return -1;
419
+ if (w1->signum > w2->signum) return 1;
420
+
421
+ /* Sort by loop pointer, so we can easily look up the first item after
422
+ * { .signum = x, .loop = NULL }.
423
+ */
424
+ if (w1->loop < w2->loop) return -1;
425
+ if (w1->loop > w2->loop) return 1;
426
+
427
+ if (w1 < w2) return -1;
428
+ if (w1 > w2) return 1;
429
+
430
+ return 0;
431
+ }
432
+
433
+
434
+ int uv_signal_stop(uv_signal_t* handle) {
435
+ assert(!(handle->flags & (UV_CLOSING | UV_CLOSED)));
436
+ uv__signal_stop(handle);
437
+ return 0;
438
+ }
439
+
440
+
441
+ static void uv__signal_stop(uv_signal_t* handle) {
442
+ uv_signal_t* removed_handle;
443
+ sigset_t saved_sigmask;
444
+
445
+ /* If the watcher wasn't started, this is a no-op. */
446
+ if (handle->signum == 0)
447
+ return;
448
+
449
+ uv__signal_block_and_lock(&saved_sigmask);
450
+
451
+ removed_handle = RB_REMOVE(uv__signal_tree_s, &uv__signal_tree, handle);
452
+ assert(removed_handle == handle);
453
+ (void) removed_handle;
454
+
455
+ /* Check if there are other active signal watchers observing this signal. If
456
+ * not, unregister the signal handler.
457
+ */
458
+ if (uv__signal_first_handle(handle->signum) == NULL)
459
+ uv__signal_unregister_handler(handle->signum);
460
+
461
+ uv__signal_unlock_and_unblock(&saved_sigmask);
462
+
463
+ handle->signum = 0;
464
+ uv__handle_stop(handle);
465
+ }