From: Oto Šťáva Date: Tue, 18 Jun 2024 15:18:37 +0000 (+0200) Subject: daemon: use __attribute__((constructor)) for protolayer_globals X-Git-Tag: v6.0.8~9^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ead0fed0bce014b5268ce9c67942618c9cc9de6f;p=thirdparty%2Fknot-resolver.git daemon: use __attribute__((constructor)) for protolayer_globals The `protolayer_globals` array can basically be treated as a constant by most of the program and its initialization only uses compile-time-known values. We basically only initialize parts of the array in different files throughout the codebase to maintain separation of concerns, so that each piece of Knot Resolver initializes the part that pertains to it. Therefore, I believe that it is more ergonomic to just use `__attribute__((constructor))` for these functions, so as not to pollute `daemon/main.c` with these calls. --- diff --git a/daemon/http.c b/daemon/http.c index 61ebcf9e7..89b5e4c42 100644 --- a/daemon/http.c +++ b/daemon/http.c @@ -13,8 +13,6 @@ #include "daemon/session2.h" #include "daemon/worker.h" -#include "daemon/http.h" - /** Makes a `nghttp2_nv`. `K` is the key, `KS` is the key length, * `V` is the value, `VS` is the value length. */ #define MAKE_NV(K, KS, V, VS) \ @@ -1030,7 +1028,8 @@ static void pl_http_request_init(struct session2 *session, } } -void http_protolayers_init(void) +__attribute__((constructor)) +static void http_protolayers_init(void) { protolayer_globals[PROTOLAYER_TYPE_HTTP] = (struct protolayer_globals) { .sess_size = sizeof(struct pl_http_sess_data), diff --git a/daemon/http.h b/daemon/http.h deleted file mode 100644 index cf5eaaca9..000000000 --- a/daemon/http.h +++ /dev/null @@ -1,6 +0,0 @@ -/* Copyright (C) CZ.NIC, z.s.p.o. - * SPDX-License-Identifier: GPL-3.0-or-later - */ - -/** Initializes the protocol layers managed by http. */ -void http_protolayers_init(void); diff --git a/daemon/io.c b/daemon/io.c index b6b289aea..90c55e8c9 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -19,7 +19,6 @@ #include "daemon/network.h" #include "daemon/worker.h" #include "daemon/tls.h" -#include "daemon/http.h" #include "daemon/session2.h" #include "contrib/cleanup.h" #include "lib/utils.h" @@ -177,7 +176,8 @@ static enum protolayer_event_cb_result pl_tcp_event_wrap( return PROTOLAYER_EVENT_PROPAGATE; } -void io_protolayers_init(void) +__attribute__((constructor)) +static void io_protolayers_init(void) { protolayer_globals[PROTOLAYER_TYPE_UDP] = (struct protolayer_globals){ .event_wrap = pl_udp_event_wrap, diff --git a/daemon/io.h b/daemon/io.h index b03c6aae5..268a8c9df 100644 --- a/daemon/io.h +++ b/daemon/io.h @@ -17,9 +17,6 @@ struct tls_ctx; struct tls_client_ctx; struct io_stream_data; -/** Initializes the protocol layers managed by io. */ -void io_protolayers_init(void); - /** Bind address into a file-descriptor (only, no libuv). type is e.g. SOCK_DGRAM */ int io_bind(const struct sockaddr *addr, int type, const endpoint_flags_t *flags); /** Initialize a UDP handle and start listening. */ diff --git a/daemon/main.c b/daemon/main.c index 63cdd7f24..44b8ae4c1 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -12,10 +12,6 @@ #include "daemon/udp_queue.h" #include "daemon/worker.h" -#ifdef ENABLE_DOH2 -#include "daemon/http.h" -#endif - #include "lib/defines.h" #include "lib/dnssec.h" #include "lib/log.h" @@ -584,13 +580,6 @@ int main(int argc, char **argv) uv_strerror(ret)); } - io_protolayers_init(); - tls_protolayers_init(); - proxy_protolayers_init(); -#ifdef ENABLE_DOH2 - http_protolayers_init(); -#endif - /* Start listening, in the sense of network_listen_fd(). */ if (start_listening(&the_args->fds) != 0) { ret = EXIT_FAILURE; diff --git a/daemon/proxyv2.c b/daemon/proxyv2.c index d080d0c65..110d34157 100644 --- a/daemon/proxyv2.c +++ b/daemon/proxyv2.c @@ -450,8 +450,8 @@ static enum protolayer_iter_cb_result pl_proxyv2_stream_unwrap( return protolayer_continue(ctx); } - -void proxy_protolayers_init(void) +__attribute__((constructor)) +static void proxy_protolayers_init(void) { protolayer_globals[PROTOLAYER_TYPE_PROXYV2_DGRAM] = (struct protolayer_globals){ .iter_size = sizeof(struct pl_proxyv2_dgram_iter_data), diff --git a/daemon/proxyv2.h b/daemon/proxyv2.h index 6a6bc1794..d514cd627 100644 --- a/daemon/proxyv2.h +++ b/daemon/proxyv2.h @@ -30,6 +30,3 @@ struct proxy_result { * the proxy is unencrypted. */ bool has_tls : 1; }; - -/** Initializes the protocol layers managed by the PROXYv2 "module". */ -void proxy_protolayers_init(void); diff --git a/daemon/tls.c b/daemon/tls.c index a5169ae36..daf6a7bed 100644 --- a/daemon/tls.c +++ b/daemon/tls.c @@ -1332,7 +1332,8 @@ static void pl_tls_request_init(struct session2 *session, req->qsource.comm_flags.tls = true; } -void tls_protolayers_init(void) +__attribute__((constructor)) +static void tls_protolayers_init(void) { protolayer_globals[PROTOLAYER_TYPE_TLS] = (struct protolayer_globals){ .sess_size = sizeof(struct pl_tls_sess_data), diff --git a/daemon/tls.h b/daemon/tls.h index 9fd45fb6f..ff1bbea2c 100644 --- a/daemon/tls.h +++ b/daemon/tls.h @@ -103,9 +103,6 @@ int tls_credentials_release(struct tls_credentials *tls_credentials); /*! Generate new ephemeral TLS credentials. */ struct tls_credentials * tls_get_ephemeral_credentials(void); -/*! Initializes the protocol layers managed by tls. */ -void tls_protolayers_init(void); - /* Session tickets, server side. Implementation in ./tls_session_ticket-srv.c */ /*! Opaque struct used by tls_session_ticket_* functions. */ diff --git a/daemon/worker.c b/daemon/worker.c index 562ebf079..2abaee074 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -2269,13 +2269,9 @@ static void pl_dns_stream_request_init(struct session2 *session, req->qsource.comm_flags.tcp = true; } -int worker_init(void) +__attribute__((constructor)) +static void worker_protolayers_init(void) { - if (kr_fails_assert(the_worker == NULL)) - return kr_error(EINVAL); - kr_bindings_register(the_engine->L); // TODO move - - /* DNS protocol layers */ protolayer_globals[PROTOLAYER_TYPE_DNS_DGRAM] = (struct protolayer_globals){ .wire_buf_overhead_cb = pl_dns_dgram_wire_buf_overhead, .wire_buf_max_overhead = KNOT_WIRE_MAX_PKTSIZE, @@ -2303,6 +2299,13 @@ int worker_init(void) protolayer_globals[PROTOLAYER_TYPE_DNS_MULTI_STREAM].sess_init = pl_dns_stream_sess_init; protolayer_globals[PROTOLAYER_TYPE_DNS_SINGLE_STREAM] = stream_common; protolayer_globals[PROTOLAYER_TYPE_DNS_SINGLE_STREAM].sess_init = pl_dns_single_stream_sess_init; +} + +int worker_init(void) +{ + if (kr_fails_assert(the_worker == NULL)) + return kr_error(EINVAL); + kr_bindings_register(the_engine->L); // TODO move /* Create main worker. */ the_worker = &the_worker_value;