From: Michael Tremer Date: Mon, 11 May 2026 11:58:55 +0000 (+0000) Subject: main: Make the context static X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7d69d2e07d41b8648a13c3627a4ef5d4823acca2;p=zone-sync.git main: Make the context static 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 --- diff --git a/main.c b/main.c index f210601..e9eafbf 100644 --- 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;