/src/bind9/lib/isc/signal.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | #include <stdlib.h> |
15 | | |
16 | | #include <isc/loop.h> |
17 | | #include <isc/signal.h> |
18 | | #include <isc/uv.h> |
19 | | |
20 | | #include "loop_p.h" |
21 | | |
22 | | isc_signal_t * |
23 | 4 | isc_signal_new(isc_signal_cb cb, void *cbarg, int signum) { |
24 | 4 | int r; |
25 | 4 | isc_loop_t *loop = isc_loop_main(); |
26 | 4 | isc_signal_t *signal = isc_mem_get(isc_loop_getmctx(loop), |
27 | 4 | sizeof(*signal)); |
28 | | |
29 | 4 | *signal = (isc_signal_t){ |
30 | 4 | .magic = SIGNAL_MAGIC, |
31 | 4 | .cb = cb, |
32 | 4 | .cbarg = cbarg, |
33 | 4 | .signum = signum, |
34 | 4 | }; |
35 | | |
36 | 4 | isc_loop_attach(loop, &signal->loop); |
37 | | |
38 | 4 | r = uv_signal_init(&loop->loop, &signal->signal); |
39 | 4 | UV_RUNTIME_CHECK(uv_signal_init, r); |
40 | | |
41 | 4 | uv_handle_set_data((uv_handle_t *)&signal->signal, signal); |
42 | | |
43 | 4 | return signal; |
44 | 4 | } |
45 | | |
46 | | static void |
47 | 0 | isc__signal_destroy_cb(uv_handle_t *handle) { |
48 | 0 | isc_signal_t *signal = uv_handle_get_data(handle); |
49 | 0 | isc_loop_t *loop = NULL; |
50 | |
|
51 | 0 | REQUIRE(VALID_SIGNAL(signal)); |
52 | |
|
53 | 0 | loop = signal->loop; |
54 | 0 | isc_mem_put(loop->mctx, signal, sizeof(*signal)); |
55 | 0 | isc_loop_detach(&loop); |
56 | 0 | } |
57 | | |
58 | | void |
59 | 0 | isc_signal_destroy(isc_signal_t **signalp) { |
60 | 0 | isc_signal_t *signal = NULL; |
61 | |
|
62 | 0 | REQUIRE(signalp != NULL); |
63 | 0 | REQUIRE(VALID_SIGNAL(*signalp)); |
64 | |
|
65 | 0 | signal = *signalp; |
66 | 0 | *signalp = NULL; |
67 | |
|
68 | 0 | uv_close((uv_handle_t *)&signal->signal, isc__signal_destroy_cb); |
69 | 0 | } |
70 | | |
71 | | void |
72 | 0 | isc_signal_stop(isc_signal_t *signal) { |
73 | 0 | int r; |
74 | |
|
75 | 0 | REQUIRE(VALID_SIGNAL(signal)); |
76 | 0 | r = uv_signal_stop(&signal->signal); |
77 | 0 | UV_RUNTIME_CHECK(uv_signal_stop, r); |
78 | 0 | } |
79 | | |
80 | | static void |
81 | 0 | isc__signal_cb(uv_signal_t *handle, int signum) { |
82 | 0 | isc_signal_t *signal = uv_handle_get_data((uv_handle_t *)handle); |
83 | |
|
84 | 0 | REQUIRE(VALID_SIGNAL(signal)); |
85 | 0 | REQUIRE(signum == signal->signum); |
86 | |
|
87 | 0 | signal->cb(signal->cbarg, signum); |
88 | 0 | } |
89 | | |
90 | | void |
91 | 4 | isc_signal_start(isc_signal_t *signal) { |
92 | 4 | int r; |
93 | | |
94 | 4 | REQUIRE(VALID_SIGNAL(signal)); |
95 | 4 | r = uv_signal_start(&signal->signal, isc__signal_cb, signal->signum); |
96 | 4 | UV_RUNTIME_CHECK(uv_signal_start, r); |
97 | 4 | } |