signal.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. #include "uv.h"
  21. #include "internal.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <signal.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #ifndef SA_RESTART
  29. # define SA_RESTART 0
  30. #endif
  31. typedef struct {
  32. uv_signal_t* handle;
  33. int signum;
  34. } uv__signal_msg_t;
  35. RB_HEAD(uv__signal_tree_s, uv_signal_s);
  36. static int uv__signal_unlock(void);
  37. static int uv__signal_start(uv_signal_t* handle,
  38. uv_signal_cb signal_cb,
  39. int signum,
  40. int oneshot);
  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. static void uv__signal_unregister_handler(int signum);
  45. static uv_once_t uv__signal_global_init_guard = UV_ONCE_INIT;
  46. static struct uv__signal_tree_s uv__signal_tree =
  47. RB_INITIALIZER(uv__signal_tree);
  48. static int uv__signal_lock_pipefd[2] = { -1, -1 };
  49. RB_GENERATE_STATIC(uv__signal_tree_s,
  50. uv_signal_s, tree_entry,
  51. uv__signal_compare)
  52. static void uv__signal_global_reinit(void);
  53. static void uv__signal_global_init(void) {
  54. if (uv__signal_lock_pipefd[0] == -1)
  55. /* pthread_atfork can register before and after handlers, one
  56. * for each child. This only registers one for the child. That
  57. * state is both persistent and cumulative, so if we keep doing
  58. * it the handler functions will be called multiple times. Thus
  59. * we only want to do it once.
  60. */
  61. if (pthread_atfork(NULL, NULL, &uv__signal_global_reinit))
  62. abort();
  63. uv__signal_global_reinit();
  64. }
  65. void uv__signal_cleanup(void) {
  66. /* We can only use signal-safe functions here.
  67. * That includes read/write and close, fortunately.
  68. * We do all of this directly here instead of resetting
  69. * uv__signal_global_init_guard because
  70. * uv__signal_global_once_init is only called from uv_loop_init
  71. * and this needs to function in existing loops.
  72. */
  73. if (uv__signal_lock_pipefd[0] != -1) {
  74. uv__close(uv__signal_lock_pipefd[0]);
  75. uv__signal_lock_pipefd[0] = -1;
  76. }
  77. if (uv__signal_lock_pipefd[1] != -1) {
  78. uv__close(uv__signal_lock_pipefd[1]);
  79. uv__signal_lock_pipefd[1] = -1;
  80. }
  81. }
  82. static void uv__signal_global_reinit(void) {
  83. uv__signal_cleanup();
  84. if (uv__make_pipe(uv__signal_lock_pipefd, 0))
  85. abort();
  86. if (uv__signal_unlock())
  87. abort();
  88. }
  89. void uv__signal_global_once_init(void) {
  90. uv_once(&uv__signal_global_init_guard, uv__signal_global_init);
  91. }
  92. static int uv__signal_lock(void) {
  93. int r;
  94. char data;
  95. do {
  96. r = read(uv__signal_lock_pipefd[0], &data, sizeof data);
  97. } while (r < 0 && errno == EINTR);
  98. return (r < 0) ? -1 : 0;
  99. }
  100. static int uv__signal_unlock(void) {
  101. int r;
  102. char data = 42;
  103. do {
  104. r = write(uv__signal_lock_pipefd[1], &data, sizeof data);
  105. } while (r < 0 && errno == EINTR);
  106. return (r < 0) ? -1 : 0;
  107. }
  108. static void uv__signal_block_and_lock(sigset_t* saved_sigmask) {
  109. sigset_t new_mask;
  110. if (sigfillset(&new_mask))
  111. abort();
  112. /* to shut up valgrind */
  113. sigemptyset(saved_sigmask);
  114. if (pthread_sigmask(SIG_SETMASK, &new_mask, saved_sigmask))
  115. abort();
  116. if (uv__signal_lock())
  117. abort();
  118. }
  119. static void uv__signal_unlock_and_unblock(sigset_t* saved_sigmask) {
  120. if (uv__signal_unlock())
  121. abort();
  122. if (pthread_sigmask(SIG_SETMASK, saved_sigmask, NULL))
  123. abort();
  124. }
  125. static uv_signal_t* uv__signal_first_handle(int signum) {
  126. /* This function must be called with the signal lock held. */
  127. uv_signal_t lookup;
  128. uv_signal_t* handle;
  129. lookup.signum = signum;
  130. lookup.flags = 0;
  131. lookup.loop = NULL;
  132. handle = RB_NFIND(uv__signal_tree_s, &uv__signal_tree, &lookup);
  133. if (handle != NULL && handle->signum == signum)
  134. return handle;
  135. return NULL;
  136. }
  137. static void uv__signal_handler(int signum) {
  138. uv__signal_msg_t msg;
  139. uv_signal_t* handle;
  140. int saved_errno;
  141. saved_errno = errno;
  142. memset(&msg, 0, sizeof msg);
  143. if (uv__signal_lock()) {
  144. errno = saved_errno;
  145. return;
  146. }
  147. for (handle = uv__signal_first_handle(signum);
  148. handle != NULL && handle->signum == signum;
  149. handle = RB_NEXT(uv__signal_tree_s, &uv__signal_tree, handle)) {
  150. int r;
  151. msg.signum = signum;
  152. msg.handle = handle;
  153. /* write() should be atomic for small data chunks, so the entire message
  154. * should be written at once. In theory the pipe could become full, in
  155. * which case the user is out of luck.
  156. */
  157. do {
  158. r = write(handle->loop->signal_pipefd[1], &msg, sizeof msg);
  159. } while (r == -1 && errno == EINTR);
  160. assert(r == sizeof msg ||
  161. (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)));
  162. if (r != -1)
  163. handle->caught_signals++;
  164. }
  165. uv__signal_unlock();
  166. errno = saved_errno;
  167. }
  168. static int uv__signal_register_handler(int signum, int oneshot) {
  169. /* When this function is called, the signal lock must be held. */
  170. struct sigaction sa;
  171. /* XXX use a separate signal stack? */
  172. memset(&sa, 0, sizeof(sa));
  173. if (sigfillset(&sa.sa_mask))
  174. abort();
  175. sa.sa_handler = uv__signal_handler;
  176. sa.sa_flags = SA_RESTART;
  177. if (oneshot)
  178. sa.sa_flags |= SA_RESETHAND;
  179. /* XXX save old action so we can restore it later on? */
  180. if (sigaction(signum, &sa, NULL))
  181. return UV__ERR(errno);
  182. return 0;
  183. }
  184. static void uv__signal_unregister_handler(int signum) {
  185. /* When this function is called, the signal lock must be held. */
  186. struct sigaction sa;
  187. memset(&sa, 0, sizeof(sa));
  188. sa.sa_handler = SIG_DFL;
  189. /* sigaction can only fail with EINVAL or EFAULT; an attempt to deregister a
  190. * signal implies that it was successfully registered earlier, so EINVAL
  191. * should never happen.
  192. */
  193. if (sigaction(signum, &sa, NULL))
  194. abort();
  195. }
  196. static int uv__signal_loop_once_init(uv_loop_t* loop) {
  197. int err;
  198. /* Return if already initialized. */
  199. if (loop->signal_pipefd[0] != -1)
  200. return 0;
  201. err = uv__make_pipe(loop->signal_pipefd, UV__F_NONBLOCK);
  202. if (err)
  203. return err;
  204. uv__io_init(&loop->signal_io_watcher,
  205. uv__signal_event,
  206. loop->signal_pipefd[0]);
  207. uv__io_start(loop, &loop->signal_io_watcher, POLLIN);
  208. return 0;
  209. }
  210. int uv__signal_loop_fork(uv_loop_t* loop) {
  211. uv__io_stop(loop, &loop->signal_io_watcher, POLLIN);
  212. uv__close(loop->signal_pipefd[0]);
  213. uv__close(loop->signal_pipefd[1]);
  214. loop->signal_pipefd[0] = -1;
  215. loop->signal_pipefd[1] = -1;
  216. return uv__signal_loop_once_init(loop);
  217. }
  218. void uv__signal_loop_cleanup(uv_loop_t* loop) {
  219. QUEUE* q;
  220. /* Stop all the signal watchers that are still attached to this loop. This
  221. * ensures that the (shared) signal tree doesn't contain any invalid entries
  222. * entries, and that signal handlers are removed when appropriate.
  223. * It's safe to use QUEUE_FOREACH here because the handles and the handle
  224. * queue are not modified by uv__signal_stop().
  225. */
  226. QUEUE_FOREACH(q, &loop->handle_queue) {
  227. uv_handle_t* handle = QUEUE_DATA(q, uv_handle_t, handle_queue);
  228. if (handle->type == UV_SIGNAL)
  229. uv__signal_stop((uv_signal_t*) handle);
  230. }
  231. if (loop->signal_pipefd[0] != -1) {
  232. uv__close(loop->signal_pipefd[0]);
  233. loop->signal_pipefd[0] = -1;
  234. }
  235. if (loop->signal_pipefd[1] != -1) {
  236. uv__close(loop->signal_pipefd[1]);
  237. loop->signal_pipefd[1] = -1;
  238. }
  239. }
  240. int uv_signal_init(uv_loop_t* loop, uv_signal_t* handle) {
  241. int err;
  242. err = uv__signal_loop_once_init(loop);
  243. if (err)
  244. return err;
  245. uv__handle_init(loop, (uv_handle_t*) handle, UV_SIGNAL);
  246. handle->signum = 0;
  247. handle->caught_signals = 0;
  248. handle->dispatched_signals = 0;
  249. return 0;
  250. }
  251. void uv__signal_close(uv_signal_t* handle) {
  252. uv__signal_stop(handle);
  253. }
  254. int uv_signal_start(uv_signal_t* handle, uv_signal_cb signal_cb, int signum) {
  255. return uv__signal_start(handle, signal_cb, signum, 0);
  256. }
  257. int uv_signal_start_oneshot(uv_signal_t* handle,
  258. uv_signal_cb signal_cb,
  259. int signum) {
  260. return uv__signal_start(handle, signal_cb, signum, 1);
  261. }
  262. static int uv__signal_start(uv_signal_t* handle,
  263. uv_signal_cb signal_cb,
  264. int signum,
  265. int oneshot) {
  266. sigset_t saved_sigmask;
  267. int err;
  268. uv_signal_t* first_handle;
  269. assert(!uv__is_closing(handle));
  270. /* If the user supplies signum == 0, then return an error already. If the
  271. * signum is otherwise invalid then uv__signal_register will find out
  272. * eventually.
  273. */
  274. if (signum == 0)
  275. return UV_EINVAL;
  276. /* Short circuit: if the signal watcher is already watching {signum} don't
  277. * go through the process of deregistering and registering the handler.
  278. * Additionally, this avoids pending signals getting lost in the small
  279. * time frame that handle->signum == 0.
  280. */
  281. if (signum == handle->signum) {
  282. handle->signal_cb = signal_cb;
  283. return 0;
  284. }
  285. /* If the signal handler was already active, stop it first. */
  286. if (handle->signum != 0) {
  287. uv__signal_stop(handle);
  288. }
  289. uv__signal_block_and_lock(&saved_sigmask);
  290. /* If at this point there are no active signal watchers for this signum (in
  291. * any of the loops), it's time to try and register a handler for it here.
  292. * Also in case there's only one-shot handlers and a regular handler comes in.
  293. */
  294. first_handle = uv__signal_first_handle(signum);
  295. if (first_handle == NULL ||
  296. (!oneshot && (first_handle->flags & UV_SIGNAL_ONE_SHOT))) {
  297. err = uv__signal_register_handler(signum, oneshot);
  298. if (err) {
  299. /* Registering the signal handler failed. Must be an invalid signal. */
  300. uv__signal_unlock_and_unblock(&saved_sigmask);
  301. return err;
  302. }
  303. }
  304. handle->signum = signum;
  305. if (oneshot)
  306. handle->flags |= UV_SIGNAL_ONE_SHOT;
  307. RB_INSERT(uv__signal_tree_s, &uv__signal_tree, handle);
  308. uv__signal_unlock_and_unblock(&saved_sigmask);
  309. handle->signal_cb = signal_cb;
  310. uv__handle_start(handle);
  311. return 0;
  312. }
  313. static void uv__signal_event(uv_loop_t* loop,
  314. uv__io_t* w,
  315. unsigned int events) {
  316. uv__signal_msg_t* msg;
  317. uv_signal_t* handle;
  318. char buf[sizeof(uv__signal_msg_t) * 32];
  319. size_t bytes, end, i;
  320. int r;
  321. bytes = 0;
  322. end = 0;
  323. do {
  324. r = read(loop->signal_pipefd[0], buf + bytes, sizeof(buf) - bytes);
  325. if (r == -1 && errno == EINTR)
  326. continue;
  327. if (r == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  328. /* If there are bytes in the buffer already (which really is extremely
  329. * unlikely if possible at all) we can't exit the function here. We'll
  330. * spin until more bytes are read instead.
  331. */
  332. if (bytes > 0)
  333. continue;
  334. /* Otherwise, there was nothing there. */
  335. return;
  336. }
  337. /* Other errors really should never happen. */
  338. if (r == -1)
  339. abort();
  340. bytes += r;
  341. /* `end` is rounded down to a multiple of sizeof(uv__signal_msg_t). */
  342. end = (bytes / sizeof(uv__signal_msg_t)) * sizeof(uv__signal_msg_t);
  343. for (i = 0; i < end; i += sizeof(uv__signal_msg_t)) {
  344. msg = (uv__signal_msg_t*) (buf + i);
  345. handle = msg->handle;
  346. if (msg->signum == handle->signum) {
  347. assert(!(handle->flags & UV_HANDLE_CLOSING));
  348. handle->signal_cb(handle, handle->signum);
  349. }
  350. handle->dispatched_signals++;
  351. if (handle->flags & UV_SIGNAL_ONE_SHOT)
  352. uv__signal_stop(handle);
  353. }
  354. bytes -= end;
  355. /* If there are any "partial" messages left, move them to the start of the
  356. * the buffer, and spin. This should not happen.
  357. */
  358. if (bytes) {
  359. memmove(buf, buf + end, bytes);
  360. continue;
  361. }
  362. } while (end == sizeof buf);
  363. }
  364. static int uv__signal_compare(uv_signal_t* w1, uv_signal_t* w2) {
  365. int f1;
  366. int f2;
  367. /* Compare signums first so all watchers with the same signnum end up
  368. * adjacent.
  369. */
  370. if (w1->signum < w2->signum) return -1;
  371. if (w1->signum > w2->signum) return 1;
  372. /* Handlers without UV_SIGNAL_ONE_SHOT set will come first, so if the first
  373. * handler returned is a one-shot handler, the rest will be too.
  374. */
  375. f1 = w1->flags & UV_SIGNAL_ONE_SHOT;
  376. f2 = w2->flags & UV_SIGNAL_ONE_SHOT;
  377. if (f1 < f2) return -1;
  378. if (f1 > f2) return 1;
  379. /* Sort by loop pointer, so we can easily look up the first item after
  380. * { .signum = x, .loop = NULL }.
  381. */
  382. if (w1->loop < w2->loop) return -1;
  383. if (w1->loop > w2->loop) return 1;
  384. if (w1 < w2) return -1;
  385. if (w1 > w2) return 1;
  386. return 0;
  387. }
  388. int uv_signal_stop(uv_signal_t* handle) {
  389. assert(!uv__is_closing(handle));
  390. uv__signal_stop(handle);
  391. return 0;
  392. }
  393. static void uv__signal_stop(uv_signal_t* handle) {
  394. uv_signal_t* removed_handle;
  395. sigset_t saved_sigmask;
  396. uv_signal_t* first_handle;
  397. int rem_oneshot;
  398. int first_oneshot;
  399. int ret;
  400. /* If the watcher wasn't started, this is a no-op. */
  401. if (handle->signum == 0)
  402. return;
  403. uv__signal_block_and_lock(&saved_sigmask);
  404. removed_handle = RB_REMOVE(uv__signal_tree_s, &uv__signal_tree, handle);
  405. assert(removed_handle == handle);
  406. (void) removed_handle;
  407. /* Check if there are other active signal watchers observing this signal. If
  408. * not, unregister the signal handler.
  409. */
  410. first_handle = uv__signal_first_handle(handle->signum);
  411. if (first_handle == NULL) {
  412. uv__signal_unregister_handler(handle->signum);
  413. } else {
  414. rem_oneshot = handle->flags & UV_SIGNAL_ONE_SHOT;
  415. first_oneshot = first_handle->flags & UV_SIGNAL_ONE_SHOT;
  416. if (first_oneshot && !rem_oneshot) {
  417. ret = uv__signal_register_handler(handle->signum, 1);
  418. assert(ret == 0);
  419. (void)ret;
  420. }
  421. }
  422. uv__signal_unlock_and_unblock(&saved_sigmask);
  423. handle->signum = 0;
  424. uv__handle_stop(handle);
  425. }