]> git.ipfire.org Git - zone-sync.git/commitdiff
main: Make the context static
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 11:58:55 +0000 (11:58 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 11 May 2026 11:58:55 +0000 (11:58 +0000)
The BIND API does not allow us to carry around a custom pointer which
prevents us from passing the context dynamically.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
main.c

diff --git a/main.c b/main.c
index f210601372b6bc7d7da58a1da4c8bceb8f93e467..e9eafbfb6bcf9a91b6c173f66755b62a22935c04 100644 (file)
--- a/main.c
+++ b/main.c
@@ -50,14 +50,19 @@ typedef struct ctx {
        isc_nm_t* netmgr;
 } ctx_t;
 
-static void logger(ctx_t* ctx, int priority, const char* format, ...) {
+// Create the context
+static ctx_t ctx = {
+       .path = DEFAULT_PATH,
+};
+
+static void logger(int priority, const char* format, ...) {
        char buffer[4096];
        FILE* f = NULL;
        ssize_t length;
        va_list args;
 
        // Don't log if below log level
-       if (priority > ctx->log_level)
+       if (priority > ctx.log_level)
                return;
 
        // Format the log message
@@ -96,13 +101,11 @@ static void do_zone(ctx_t* ctx, const char* name) {
 }
 
 static void run_loop(void* data) {
-       ctx_t* ctx = data;
-
-       DEBUG(ctx, "Event loop started\n");
+       DEBUG("Event loop started\n");
 
        // Process all zones
-       for (unsigned int i = 0; i < ctx->num_zones; i++) {
-               do_zone(ctx, ctx->zones[i]);
+       for (unsigned int i = 0; i < ctx.num_zones; i++) {
+               do_zone(ctx.zones[i]);
        }
 }
 
@@ -122,34 +125,33 @@ static struct argp_option options[] = {
 };
 
 static error_t parse(int key, char* arg, struct argp_state* state) {
-       ctx_t* ctx = state->input;
        const char** zones = NULL;
 
        switch (key) {
                case OPT_DEBUG:
-                       ctx->log_level = LOG_DEBUG;
+                       ctx.log_level = LOG_DEBUG;
                        break;
 
                case OPT_PATH:
-                       ctx->path = arg;
+                       ctx.path = arg;
                        break;
 
                case ARGP_KEY_ARG:
-                       zones = reallocarray(ctx->zones, ctx->num_zones + 1, sizeof(*ctx->zones));
+                       zones = reallocarray(ctx.zones, ctx.num_zones + 1, sizeof(*ctx.zones));
                        if (!zones) {
                                argp_failure(state, EXIT_FAILURE, 0, "%m");
                                return ARGP_ERR_UNKNOWN;
                        }
 
                        // Store the pointer to the argument
-                       zones[ctx->num_zones++] = arg;
+                       zones[ctx.num_zones++] = arg;
 
-                       ctx->zones = zones;
+                       ctx.zones = zones;
                        break;
 
                case ARGP_KEY_SUCCESS:
                        // Fail if we don't have any zones
-                       if (!ctx->zones) {
+                       if (!ctx.zones) {
                                argp_failure(state, EXIT_FAILURE, 0, "You must pass a zone");
                        }
                        break;
@@ -171,11 +173,6 @@ static error_t parse(int key, char* arg, struct argp_state* state) {
 int main(int argc, char* argv[]) {
        int r;
 
-       // Create the context
-       ctx_t ctx = {
-               .path = DEFAULT_PATH,
-       };
-
        // Setup the command line parser
        struct argp parser = {
                .options   = options,
@@ -185,7 +182,7 @@ int main(int argc, char* argv[]) {
        int arg_index = 0;
 
        // Parse the command line
-       r = argp_parse(&parser, argc, argv, ARGP_IN_ORDER, &arg_index, &ctx);
+       r = argp_parse(&parser, argc, argv, ARGP_IN_ORDER, &arg_index, NULL);
        if (r)
                return r;