]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
daemon: use __attribute__((constructor)) for protolayer_globals docs-develop-prot-614eod/deployments/4380
authorOto Šťáva <oto.stava@nic.cz>
Tue, 18 Jun 2024 15:18:37 +0000 (17:18 +0200)
committerOto Šťáva <oto.stava@nic.cz>
Tue, 18 Jun 2024 15:18:37 +0000 (17:18 +0200)
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.

daemon/http.c
daemon/http.h [deleted file]
daemon/io.c
daemon/io.h
daemon/main.c
daemon/proxyv2.c
daemon/proxyv2.h
daemon/tls.c
daemon/tls.h
daemon/worker.c

index 61ebcf9e737349e3f894933d09f8b9ce8efb7102..89b5e4c42f8b4f1b34eacfa74fa37bb44f209321 100644 (file)
@@ -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 (file)
index cf5eaac..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-/*  Copyright (C) CZ.NIC, z.s.p.o. <knot-resolver@labs.nic.cz>
- *  SPDX-License-Identifier: GPL-3.0-or-later
- */
-
-/** Initializes the protocol layers managed by http. */
-void http_protolayers_init(void);
index b6b289aea8cc3f1c72f1fc70f90a5886e66b36a1..90c55e8c9785787211c36d1cfcf5252350037bad 100644 (file)
@@ -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,
index b03c6aae50b36d89af8ed7abb0cb08c7dcbb4f0a..268a8c9df480e20981aea2a267991b3b00412c27 100644 (file)
@@ -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. */
index 63cdd7f2493b13de8572f34c95faf8414a334fbe..44b8ae4c1b745c19ae91f6c9119b895d7738947f 100644 (file)
 #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;
index d080d0c6595c08a400524c369fa691af95b9b07d..110d341578f58827739ccc1dfa7803e322be8938 100644 (file)
@@ -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),
index 6a6bc1794905bdbf0617980442848acc652f5514..d514cd6272ec78c543e6f6f0f8f0e76fce762321 100644 (file)
@@ -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);
index a5169ae36c1179e2ee683a03fc133273dc26c0c5..daf6a7bed231c1278e601b1263b410104218462a 100644 (file)
@@ -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),
index 9fd45fb6f75d2b5badce75de93aaffd9ecdf5b99..ff1bbea2cfc02e8cf99d4cabbb9b117eb59719cb 100644 (file)
@@ -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. */
index 562ebf07912a30fd9db7b5224a88a675af1fbf36..2abaee074ff8bbd09b4bf0e9eb629e3ad227c0ef 100644 (file)
@@ -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;