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