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,763 @@
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 <stdio.h>
25
+ #include <stdint.h>
26
+ #include <stdlib.h>
27
+ #include <string.h>
28
+ #include <assert.h>
29
+ #include <errno.h>
30
+
31
+ #ifndef SUNOS_NO_IFADDRS
32
+ # include <ifaddrs.h>
33
+ #endif
34
+ #include <net/if.h>
35
+ #include <net/if_dl.h>
36
+
37
+ #include <sys/loadavg.h>
38
+ #include <sys/time.h>
39
+ #include <unistd.h>
40
+ #include <kstat.h>
41
+ #include <fcntl.h>
42
+
43
+ #include <sys/port.h>
44
+ #include <port.h>
45
+
46
+ #define PORT_FIRED 0x69
47
+ #define PORT_UNUSED 0x0
48
+ #define PORT_LOADED 0x99
49
+ #define PORT_DELETED -1
50
+
51
+ #if (!defined(_LP64)) && (_FILE_OFFSET_BITS - 0 == 64)
52
+ #define PROCFS_FILE_OFFSET_BITS_HACK 1
53
+ #undef _FILE_OFFSET_BITS
54
+ #else
55
+ #define PROCFS_FILE_OFFSET_BITS_HACK 0
56
+ #endif
57
+
58
+ #include <procfs.h>
59
+
60
+ #if (PROCFS_FILE_OFFSET_BITS_HACK - 0 == 1)
61
+ #define _FILE_OFFSET_BITS 64
62
+ #endif
63
+
64
+
65
+ int uv__platform_loop_init(uv_loop_t* loop) {
66
+ int err;
67
+ int fd;
68
+
69
+ loop->fs_fd = -1;
70
+ loop->backend_fd = -1;
71
+
72
+ fd = port_create();
73
+ if (fd == -1)
74
+ return -errno;
75
+
76
+ err = uv__cloexec(fd, 1);
77
+ if (err) {
78
+ uv__close(fd);
79
+ return err;
80
+ }
81
+ loop->backend_fd = fd;
82
+
83
+ return 0;
84
+ }
85
+
86
+
87
+ void uv__platform_loop_delete(uv_loop_t* loop) {
88
+ if (loop->fs_fd != -1) {
89
+ uv__close(loop->fs_fd);
90
+ loop->fs_fd = -1;
91
+ }
92
+
93
+ if (loop->backend_fd != -1) {
94
+ uv__close(loop->backend_fd);
95
+ loop->backend_fd = -1;
96
+ }
97
+ }
98
+
99
+
100
+ void uv__platform_invalidate_fd(uv_loop_t* loop, int fd) {
101
+ struct port_event* events;
102
+ uintptr_t i;
103
+ uintptr_t nfds;
104
+
105
+ assert(loop->watchers != NULL);
106
+
107
+ events = (struct port_event*) loop->watchers[loop->nwatchers];
108
+ nfds = (uintptr_t) loop->watchers[loop->nwatchers + 1];
109
+ if (events == NULL)
110
+ return;
111
+
112
+ /* Invalidate events with same file descriptor */
113
+ for (i = 0; i < nfds; i++)
114
+ if ((int) events[i].portev_object == fd)
115
+ events[i].portev_object = -1;
116
+ }
117
+
118
+
119
+ void uv__io_poll(uv_loop_t* loop, int timeout) {
120
+ struct port_event events[1024];
121
+ struct port_event* pe;
122
+ struct timespec spec;
123
+ QUEUE* q;
124
+ uv__io_t* w;
125
+ sigset_t* pset;
126
+ sigset_t set;
127
+ uint64_t base;
128
+ uint64_t diff;
129
+ unsigned int nfds;
130
+ unsigned int i;
131
+ int saved_errno;
132
+ int nevents;
133
+ int count;
134
+ int err;
135
+ int fd;
136
+
137
+ if (loop->nfds == 0) {
138
+ assert(QUEUE_EMPTY(&loop->watcher_queue));
139
+ return;
140
+ }
141
+
142
+ while (!QUEUE_EMPTY(&loop->watcher_queue)) {
143
+ q = QUEUE_HEAD(&loop->watcher_queue);
144
+ QUEUE_REMOVE(q);
145
+ QUEUE_INIT(q);
146
+
147
+ w = QUEUE_DATA(q, uv__io_t, watcher_queue);
148
+ assert(w->pevents != 0);
149
+
150
+ if (port_associate(loop->backend_fd, PORT_SOURCE_FD, w->fd, w->pevents, 0))
151
+ abort();
152
+
153
+ w->events = w->pevents;
154
+ }
155
+
156
+ pset = NULL;
157
+ if (loop->flags & UV_LOOP_BLOCK_SIGPROF) {
158
+ pset = &set;
159
+ sigemptyset(pset);
160
+ sigaddset(pset, SIGPROF);
161
+ }
162
+
163
+ assert(timeout >= -1);
164
+ base = loop->time;
165
+ count = 48; /* Benchmarks suggest this gives the best throughput. */
166
+
167
+ for (;;) {
168
+ if (timeout != -1) {
169
+ spec.tv_sec = timeout / 1000;
170
+ spec.tv_nsec = (timeout % 1000) * 1000000;
171
+ }
172
+
173
+ /* Work around a kernel bug where nfds is not updated. */
174
+ events[0].portev_source = 0;
175
+
176
+ nfds = 1;
177
+ saved_errno = 0;
178
+
179
+ if (pset != NULL)
180
+ pthread_sigmask(SIG_BLOCK, pset, NULL);
181
+
182
+ err = port_getn(loop->backend_fd,
183
+ events,
184
+ ARRAY_SIZE(events),
185
+ &nfds,
186
+ timeout == -1 ? NULL : &spec);
187
+
188
+ if (pset != NULL)
189
+ pthread_sigmask(SIG_UNBLOCK, pset, NULL);
190
+
191
+ if (err) {
192
+ /* Work around another kernel bug: port_getn() may return events even
193
+ * on error.
194
+ */
195
+ if (errno == EINTR || errno == ETIME)
196
+ saved_errno = errno;
197
+ else
198
+ abort();
199
+ }
200
+
201
+ /* Update loop->time unconditionally. It's tempting to skip the update when
202
+ * timeout == 0 (i.e. non-blocking poll) but there is no guarantee that the
203
+ * operating system didn't reschedule our process while in the syscall.
204
+ */
205
+ SAVE_ERRNO(uv__update_time(loop));
206
+
207
+ if (events[0].portev_source == 0) {
208
+ if (timeout == 0)
209
+ return;
210
+
211
+ if (timeout == -1)
212
+ continue;
213
+
214
+ goto update_timeout;
215
+ }
216
+
217
+ if (nfds == 0) {
218
+ assert(timeout != -1);
219
+ return;
220
+ }
221
+
222
+ nevents = 0;
223
+
224
+ assert(loop->watchers != NULL);
225
+ loop->watchers[loop->nwatchers] = (void*) events;
226
+ loop->watchers[loop->nwatchers + 1] = (void*) (uintptr_t) nfds;
227
+ for (i = 0; i < nfds; i++) {
228
+ pe = events + i;
229
+ fd = pe->portev_object;
230
+
231
+ /* Skip invalidated events, see uv__platform_invalidate_fd */
232
+ if (fd == -1)
233
+ continue;
234
+
235
+ assert(fd >= 0);
236
+ assert((unsigned) fd < loop->nwatchers);
237
+
238
+ w = loop->watchers[fd];
239
+
240
+ /* File descriptor that we've stopped watching, ignore. */
241
+ if (w == NULL)
242
+ continue;
243
+
244
+ w->cb(loop, w, pe->portev_events);
245
+ nevents++;
246
+
247
+ if (w != loop->watchers[fd])
248
+ continue; /* Disabled by callback. */
249
+
250
+ /* Events Ports operates in oneshot mode, rearm timer on next run. */
251
+ if (w->pevents != 0 && QUEUE_EMPTY(&w->watcher_queue))
252
+ QUEUE_INSERT_TAIL(&loop->watcher_queue, &w->watcher_queue);
253
+ }
254
+ loop->watchers[loop->nwatchers] = NULL;
255
+ loop->watchers[loop->nwatchers + 1] = NULL;
256
+
257
+ if (nevents != 0) {
258
+ if (nfds == ARRAY_SIZE(events) && --count != 0) {
259
+ /* Poll for more events but don't block this time. */
260
+ timeout = 0;
261
+ continue;
262
+ }
263
+ return;
264
+ }
265
+
266
+ if (saved_errno == ETIME) {
267
+ assert(timeout != -1);
268
+ return;
269
+ }
270
+
271
+ if (timeout == 0)
272
+ return;
273
+
274
+ if (timeout == -1)
275
+ continue;
276
+
277
+ update_timeout:
278
+ assert(timeout > 0);
279
+
280
+ diff = loop->time - base;
281
+ if (diff >= (uint64_t) timeout)
282
+ return;
283
+
284
+ timeout -= diff;
285
+ }
286
+ }
287
+
288
+
289
+ uint64_t uv__hrtime(uv_clocktype_t type) {
290
+ return gethrtime();
291
+ }
292
+
293
+
294
+ /*
295
+ * We could use a static buffer for the path manipulations that we need outside
296
+ * of the function, but this function could be called by multiple consumers and
297
+ * we don't want to potentially create a race condition in the use of snprintf.
298
+ */
299
+ int uv_exepath(char* buffer, size_t* size) {
300
+ ssize_t res;
301
+ char buf[128];
302
+
303
+ if (buffer == NULL || size == NULL || *size == 0)
304
+ return -EINVAL;
305
+
306
+ snprintf(buf, sizeof(buf), "/proc/%lu/path/a.out", (unsigned long) getpid());
307
+
308
+ res = *size - 1;
309
+ if (res > 0)
310
+ res = readlink(buf, buffer, res);
311
+
312
+ if (res == -1)
313
+ return -errno;
314
+
315
+ buffer[res] = '\0';
316
+ *size = res;
317
+ return 0;
318
+ }
319
+
320
+
321
+ uint64_t uv_get_free_memory(void) {
322
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_AVPHYS_PAGES);
323
+ }
324
+
325
+
326
+ uint64_t uv_get_total_memory(void) {
327
+ return (uint64_t) sysconf(_SC_PAGESIZE) * sysconf(_SC_PHYS_PAGES);
328
+ }
329
+
330
+
331
+ void uv_loadavg(double avg[3]) {
332
+ (void) getloadavg(avg, 3);
333
+ }
334
+
335
+
336
+ #if defined(PORT_SOURCE_FILE)
337
+
338
+ static int uv__fs_event_rearm(uv_fs_event_t *handle) {
339
+ if (handle->fd == -1)
340
+ return -EBADF;
341
+
342
+ if (port_associate(handle->loop->fs_fd,
343
+ PORT_SOURCE_FILE,
344
+ (uintptr_t) &handle->fo,
345
+ FILE_ATTRIB | FILE_MODIFIED,
346
+ handle) == -1) {
347
+ return -errno;
348
+ }
349
+ handle->fd = PORT_LOADED;
350
+
351
+ return 0;
352
+ }
353
+
354
+
355
+ static void uv__fs_event_read(uv_loop_t* loop,
356
+ uv__io_t* w,
357
+ unsigned int revents) {
358
+ uv_fs_event_t *handle = NULL;
359
+ timespec_t timeout;
360
+ port_event_t pe;
361
+ int events;
362
+ int r;
363
+
364
+ (void) w;
365
+ (void) revents;
366
+
367
+ do {
368
+ uint_t n = 1;
369
+
370
+ /*
371
+ * Note that our use of port_getn() here (and not port_get()) is deliberate:
372
+ * there is a bug in event ports (Sun bug 6456558) whereby a zeroed timeout
373
+ * causes port_get() to return success instead of ETIME when there aren't
374
+ * actually any events (!); by using port_getn() in lieu of port_get(),
375
+ * we can at least workaround the bug by checking for zero returned events
376
+ * and treating it as we would ETIME.
377
+ */
378
+ do {
379
+ memset(&timeout, 0, sizeof timeout);
380
+ r = port_getn(loop->fs_fd, &pe, 1, &n, &timeout);
381
+ }
382
+ while (r == -1 && errno == EINTR);
383
+
384
+ if ((r == -1 && errno == ETIME) || n == 0)
385
+ break;
386
+
387
+ handle = (uv_fs_event_t*) pe.portev_user;
388
+ assert((r == 0) && "unexpected port_get() error");
389
+
390
+ events = 0;
391
+ if (pe.portev_events & (FILE_ATTRIB | FILE_MODIFIED))
392
+ events |= UV_CHANGE;
393
+ if (pe.portev_events & ~(FILE_ATTRIB | FILE_MODIFIED))
394
+ events |= UV_RENAME;
395
+ assert(events != 0);
396
+ handle->fd = PORT_FIRED;
397
+ handle->cb(handle, NULL, events, 0);
398
+
399
+ if (handle->fd != PORT_DELETED) {
400
+ r = uv__fs_event_rearm(handle);
401
+ if (r != 0)
402
+ handle->cb(handle, NULL, 0, r);
403
+ }
404
+ }
405
+ while (handle->fd != PORT_DELETED);
406
+ }
407
+
408
+
409
+ int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle) {
410
+ uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
411
+ return 0;
412
+ }
413
+
414
+
415
+ int uv_fs_event_start(uv_fs_event_t* handle,
416
+ uv_fs_event_cb cb,
417
+ const char* path,
418
+ unsigned int flags) {
419
+ int portfd;
420
+ int first_run;
421
+ int err;
422
+
423
+ if (uv__is_active(handle))
424
+ return -EINVAL;
425
+
426
+ first_run = 0;
427
+ if (handle->loop->fs_fd == -1) {
428
+ portfd = port_create();
429
+ if (portfd == -1)
430
+ return -errno;
431
+ handle->loop->fs_fd = portfd;
432
+ first_run = 1;
433
+ }
434
+
435
+ uv__handle_start(handle);
436
+ handle->path = strdup(path);
437
+ handle->fd = PORT_UNUSED;
438
+ handle->cb = cb;
439
+
440
+ memset(&handle->fo, 0, sizeof handle->fo);
441
+ handle->fo.fo_name = handle->path;
442
+ err = uv__fs_event_rearm(handle);
443
+ if (err != 0)
444
+ return err;
445
+
446
+ if (first_run) {
447
+ uv__io_init(&handle->loop->fs_event_watcher, uv__fs_event_read, portfd);
448
+ uv__io_start(handle->loop, &handle->loop->fs_event_watcher, UV__POLLIN);
449
+ }
450
+
451
+ return 0;
452
+ }
453
+
454
+
455
+ int uv_fs_event_stop(uv_fs_event_t* handle) {
456
+ if (!uv__is_active(handle))
457
+ return 0;
458
+
459
+ if (handle->fd == PORT_FIRED || handle->fd == PORT_LOADED) {
460
+ port_dissociate(handle->loop->fs_fd,
461
+ PORT_SOURCE_FILE,
462
+ (uintptr_t) &handle->fo);
463
+ }
464
+
465
+ handle->fd = PORT_DELETED;
466
+ free(handle->path);
467
+ handle->path = NULL;
468
+ handle->fo.fo_name = NULL;
469
+ uv__handle_stop(handle);
470
+
471
+ return 0;
472
+ }
473
+
474
+ void uv__fs_event_close(uv_fs_event_t* handle) {
475
+ uv_fs_event_stop(handle);
476
+ }
477
+
478
+ #else /* !defined(PORT_SOURCE_FILE) */
479
+
480
+ int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle) {
481
+ return -ENOSYS;
482
+ }
483
+
484
+
485
+ int uv_fs_event_start(uv_fs_event_t* handle,
486
+ uv_fs_event_cb cb,
487
+ const char* filename,
488
+ unsigned int flags) {
489
+ return -ENOSYS;
490
+ }
491
+
492
+
493
+ int uv_fs_event_stop(uv_fs_event_t* handle) {
494
+ return -ENOSYS;
495
+ }
496
+
497
+
498
+ void uv__fs_event_close(uv_fs_event_t* handle) {
499
+ UNREACHABLE();
500
+ }
501
+
502
+ #endif /* defined(PORT_SOURCE_FILE) */
503
+
504
+
505
+ char** uv_setup_args(int argc, char** argv) {
506
+ return argv;
507
+ }
508
+
509
+
510
+ int uv_set_process_title(const char* title) {
511
+ return 0;
512
+ }
513
+
514
+
515
+ int uv_get_process_title(char* buffer, size_t size) {
516
+ if (size > 0) {
517
+ buffer[0] = '\0';
518
+ }
519
+ return 0;
520
+ }
521
+
522
+
523
+ int uv_resident_set_memory(size_t* rss) {
524
+ psinfo_t psinfo;
525
+ int err;
526
+ int fd;
527
+
528
+ fd = open("/proc/self/psinfo", O_RDONLY);
529
+ if (fd == -1)
530
+ return -errno;
531
+
532
+ /* FIXME(bnoordhuis) Handle EINTR. */
533
+ err = -EINVAL;
534
+ if (read(fd, &psinfo, sizeof(psinfo)) == sizeof(psinfo)) {
535
+ *rss = (size_t)psinfo.pr_rssize * 1024;
536
+ err = 0;
537
+ }
538
+ uv__close(fd);
539
+
540
+ return err;
541
+ }
542
+
543
+
544
+ int uv_uptime(double* uptime) {
545
+ kstat_ctl_t *kc;
546
+ kstat_t *ksp;
547
+ kstat_named_t *knp;
548
+
549
+ long hz = sysconf(_SC_CLK_TCK);
550
+
551
+ kc = kstat_open();
552
+ if (kc == NULL)
553
+ return -EPERM;
554
+
555
+ ksp = kstat_lookup(kc, (char*) "unix", 0, (char*) "system_misc");
556
+ if (kstat_read(kc, ksp, NULL) == -1) {
557
+ *uptime = -1;
558
+ } else {
559
+ knp = (kstat_named_t*) kstat_data_lookup(ksp, (char*) "clk_intr");
560
+ *uptime = knp->value.ul / hz;
561
+ }
562
+ kstat_close(kc);
563
+
564
+ return 0;
565
+ }
566
+
567
+
568
+ int uv_cpu_info(uv_cpu_info_t** cpu_infos, int* count) {
569
+ int lookup_instance;
570
+ kstat_ctl_t *kc;
571
+ kstat_t *ksp;
572
+ kstat_named_t *knp;
573
+ uv_cpu_info_t* cpu_info;
574
+
575
+ kc = kstat_open();
576
+ if (kc == NULL)
577
+ return -EPERM;
578
+
579
+ /* Get count of cpus */
580
+ lookup_instance = 0;
581
+ while ((ksp = kstat_lookup(kc, (char*) "cpu_info", lookup_instance, NULL))) {
582
+ lookup_instance++;
583
+ }
584
+
585
+ *cpu_infos = malloc(lookup_instance * sizeof(**cpu_infos));
586
+ if (!(*cpu_infos)) {
587
+ kstat_close(kc);
588
+ return -ENOMEM;
589
+ }
590
+
591
+ *count = lookup_instance;
592
+
593
+ cpu_info = *cpu_infos;
594
+ lookup_instance = 0;
595
+ while ((ksp = kstat_lookup(kc, (char*) "cpu_info", lookup_instance, NULL))) {
596
+ if (kstat_read(kc, ksp, NULL) == -1) {
597
+ cpu_info->speed = 0;
598
+ cpu_info->model = NULL;
599
+ } else {
600
+ knp = kstat_data_lookup(ksp, (char*) "clock_MHz");
601
+ assert(knp->data_type == KSTAT_DATA_INT32 ||
602
+ knp->data_type == KSTAT_DATA_INT64);
603
+ cpu_info->speed = (knp->data_type == KSTAT_DATA_INT32) ? knp->value.i32
604
+ : knp->value.i64;
605
+
606
+ knp = kstat_data_lookup(ksp, (char*) "brand");
607
+ assert(knp->data_type == KSTAT_DATA_STRING);
608
+ cpu_info->model = strdup(KSTAT_NAMED_STR_PTR(knp));
609
+ }
610
+
611
+ lookup_instance++;
612
+ cpu_info++;
613
+ }
614
+
615
+ cpu_info = *cpu_infos;
616
+ lookup_instance = 0;
617
+ for (;;) {
618
+ ksp = kstat_lookup(kc, (char*) "cpu", lookup_instance, (char*) "sys");
619
+
620
+ if (ksp == NULL)
621
+ break;
622
+
623
+ if (kstat_read(kc, ksp, NULL) == -1) {
624
+ cpu_info->cpu_times.user = 0;
625
+ cpu_info->cpu_times.nice = 0;
626
+ cpu_info->cpu_times.sys = 0;
627
+ cpu_info->cpu_times.idle = 0;
628
+ cpu_info->cpu_times.irq = 0;
629
+ } else {
630
+ knp = kstat_data_lookup(ksp, (char*) "cpu_ticks_user");
631
+ assert(knp->data_type == KSTAT_DATA_UINT64);
632
+ cpu_info->cpu_times.user = knp->value.ui64;
633
+
634
+ knp = kstat_data_lookup(ksp, (char*) "cpu_ticks_kernel");
635
+ assert(knp->data_type == KSTAT_DATA_UINT64);
636
+ cpu_info->cpu_times.sys = knp->value.ui64;
637
+
638
+ knp = kstat_data_lookup(ksp, (char*) "cpu_ticks_idle");
639
+ assert(knp->data_type == KSTAT_DATA_UINT64);
640
+ cpu_info->cpu_times.idle = knp->value.ui64;
641
+
642
+ knp = kstat_data_lookup(ksp, (char*) "intr");
643
+ assert(knp->data_type == KSTAT_DATA_UINT64);
644
+ cpu_info->cpu_times.irq = knp->value.ui64;
645
+ cpu_info->cpu_times.nice = 0;
646
+ }
647
+
648
+ lookup_instance++;
649
+ cpu_info++;
650
+ }
651
+
652
+ kstat_close(kc);
653
+
654
+ return 0;
655
+ }
656
+
657
+
658
+ void uv_free_cpu_info(uv_cpu_info_t* cpu_infos, int count) {
659
+ int i;
660
+
661
+ for (i = 0; i < count; i++) {
662
+ free(cpu_infos[i].model);
663
+ }
664
+
665
+ free(cpu_infos);
666
+ }
667
+
668
+
669
+ int uv_interface_addresses(uv_interface_address_t** addresses, int* count) {
670
+ #ifdef SUNOS_NO_IFADDRS
671
+ return -ENOSYS;
672
+ #else
673
+ uv_interface_address_t* address;
674
+ struct sockaddr_dl* sa_addr;
675
+ struct ifaddrs* addrs;
676
+ struct ifaddrs* ent;
677
+ int i;
678
+
679
+ if (getifaddrs(&addrs))
680
+ return -errno;
681
+
682
+ *count = 0;
683
+
684
+ /* Count the number of interfaces */
685
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
686
+ if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)) ||
687
+ (ent->ifa_addr == NULL) ||
688
+ (ent->ifa_addr->sa_family == PF_PACKET)) {
689
+ continue;
690
+ }
691
+
692
+ (*count)++;
693
+ }
694
+
695
+ *addresses = malloc(*count * sizeof(**addresses));
696
+ if (!(*addresses))
697
+ return -ENOMEM;
698
+
699
+ address = *addresses;
700
+
701
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
702
+ if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)))
703
+ continue;
704
+
705
+ if (ent->ifa_addr == NULL)
706
+ continue;
707
+
708
+ address->name = strdup(ent->ifa_name);
709
+
710
+ if (ent->ifa_addr->sa_family == AF_INET6) {
711
+ address->address.address6 = *((struct sockaddr_in6*) ent->ifa_addr);
712
+ } else {
713
+ address->address.address4 = *((struct sockaddr_in*) ent->ifa_addr);
714
+ }
715
+
716
+ if (ent->ifa_netmask->sa_family == AF_INET6) {
717
+ address->netmask.netmask6 = *((struct sockaddr_in6*) ent->ifa_netmask);
718
+ } else {
719
+ address->netmask.netmask4 = *((struct sockaddr_in*) ent->ifa_netmask);
720
+ }
721
+
722
+ address->is_internal = !!((ent->ifa_flags & IFF_PRIVATE) ||
723
+ (ent->ifa_flags & IFF_LOOPBACK));
724
+
725
+ address++;
726
+ }
727
+
728
+ /* Fill in physical addresses for each interface */
729
+ for (ent = addrs; ent != NULL; ent = ent->ifa_next) {
730
+ if (!((ent->ifa_flags & IFF_UP) && (ent->ifa_flags & IFF_RUNNING)) ||
731
+ (ent->ifa_addr == NULL) ||
732
+ (ent->ifa_addr->sa_family != AF_LINK)) {
733
+ continue;
734
+ }
735
+
736
+ address = *addresses;
737
+
738
+ for (i = 0; i < (*count); i++) {
739
+ if (strcmp(address->name, ent->ifa_name) == 0) {
740
+ sa_addr = (struct sockaddr_dl*)(ent->ifa_addr);
741
+ memcpy(address->phys_addr, LLADDR(sa_addr), sizeof(address->phys_addr));
742
+ }
743
+ address++;
744
+ }
745
+ }
746
+
747
+ freeifaddrs(addrs);
748
+
749
+ return 0;
750
+ #endif /* SUNOS_NO_IFADDRS */
751
+ }
752
+
753
+
754
+ void uv_free_interface_addresses(uv_interface_address_t* addresses,
755
+ int count) {
756
+ int i;
757
+
758
+ for (i = 0; i < count; i++) {
759
+ free(addresses[i].name);
760
+ }
761
+
762
+ free(addresses);
763
+ }