From 45cb29a2ada4edfc2b547fe023d923ce0299a61d Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 8 Aug 2019 00:30:28 +0200 Subject: src: remove global symbol_table Store symbol tables in context object instead. Use the nft_ctx object to store the dynamic symbol table. Pass it on to the parse_ctx object so this can be accessed from the parse routines. This dynamic symbol table is also accesible from the output_ctx object for print routines. Signed-off-by: Pablo Neira Ayuso --- src/ct.c | 17 ++++++++--------- src/datatype.c | 16 +++++++--------- src/json.c | 6 +++--- src/libnftables.c | 29 ++++++++++++++--------------- src/meta.c | 27 +++++++-------------------- src/rt.c | 13 ++++++------- 6 files changed, 45 insertions(+), 63 deletions(-) (limited to 'src') diff --git a/src/ct.c b/src/ct.c index c66b327a..ed458e6b 100644 --- a/src/ct.c +++ b/src/ct.c @@ -141,11 +141,10 @@ static const struct datatype ct_event_type = { .sym_tbl = &ct_events_tbl, }; -static struct symbol_table *ct_label_tbl; - #define CT_LABEL_BIT_SIZE 128 -const char *ct_label2str(unsigned long value) +const char *ct_label2str(const struct symbol_table *ct_label_tbl, + unsigned long value) { const struct symbolic_constant *s; @@ -161,7 +160,7 @@ static void ct_label_type_print(const struct expr *expr, struct output_ctx *octx) { unsigned long bit = mpz_scan1(expr->value, 0); - const char *labelstr = ct_label2str(bit); + const char *labelstr = ct_label2str(octx->tbl.ct_label, bit); if (labelstr) { nft_print(octx, "\"%s\"", labelstr); @@ -181,7 +180,7 @@ static struct error_record *ct_label_type_parse(struct parse_ctx *ctx, uint64_t bit; mpz_t value; - for (s = ct_label_tbl->symbols; s->identifier != NULL; s++) { + for (s = ctx->tbl->ct_label->symbols; s->identifier != NULL; s++) { if (!strcmp(sym->identifier, s->identifier)) break; } @@ -230,14 +229,14 @@ static const struct datatype ct_label_type = { .parse = ct_label_type_parse, }; -void ct_label_table_init(void) +void ct_label_table_init(struct nft_ctx *ctx) { - ct_label_tbl = rt_symbol_table_init(CONNLABEL_CONF); + ctx->output.tbl.ct_label = rt_symbol_table_init(CONNLABEL_CONF); } -void ct_label_table_exit(void) +void ct_label_table_exit(struct nft_ctx *ctx) { - rt_symbol_table_free(ct_label_tbl); + rt_symbol_table_free(ctx->output.tbl.ct_label); } #ifndef NF_CT_HELPER_NAME_LEN diff --git a/src/datatype.c b/src/datatype.c index 039b4e52..396a300c 100644 --- a/src/datatype.c +++ b/src/datatype.c @@ -779,7 +779,7 @@ out: return tbl; } -void rt_symbol_table_free(struct symbol_table *tbl) +void rt_symbol_table_free(const struct symbol_table *tbl) { const struct symbolic_constant *s; @@ -788,28 +788,26 @@ void rt_symbol_table_free(struct symbol_table *tbl) xfree(tbl); } -struct symbol_table *mark_tbl = NULL; - -void mark_table_init(void) +void mark_table_init(struct nft_ctx *ctx) { - mark_tbl = rt_symbol_table_init("/etc/iproute2/rt_marks"); + ctx->output.tbl.mark = rt_symbol_table_init("/etc/iproute2/rt_marks"); } -void mark_table_exit(void) +void mark_table_exit(struct nft_ctx *ctx) { - rt_symbol_table_free(mark_tbl); + rt_symbol_table_free(ctx->output.tbl.mark); } static void mark_type_print(const struct expr *expr, struct output_ctx *octx) { - return symbolic_constant_print(mark_tbl, expr, true, octx); + return symbolic_constant_print(octx->tbl.mark, expr, true, octx); } static struct error_record *mark_type_parse(struct parse_ctx *ctx, const struct expr *sym, struct expr **res) { - return symbolic_constant_parse(ctx, sym, mark_tbl, res); + return symbolic_constant_parse(ctx, sym, ctx->tbl->mark, res); } const struct datatype mark_type = { diff --git a/src/json.c b/src/json.c index 33e0ec15..9dfa3076 100644 --- a/src/json.c +++ b/src/json.c @@ -1006,18 +1006,18 @@ json_t *inet_service_type_json(const struct expr *expr, struct output_ctx *octx) json_t *mark_type_json(const struct expr *expr, struct output_ctx *octx) { - return symbolic_constant_json(mark_tbl, expr, octx); + return symbolic_constant_json(octx->tbl.mark, expr, octx); } json_t *devgroup_type_json(const struct expr *expr, struct output_ctx *octx) { - return symbolic_constant_json(devgroup_tbl, expr, octx); + return symbolic_constant_json(octx->tbl.devgroup, expr, octx); } json_t *ct_label_type_json(const struct expr *expr, struct output_ctx *octx) { unsigned long bit = mpz_scan1(expr->value, 0); - const char *labelstr = ct_label2str(bit); + const char *labelstr = ct_label2str(octx->tbl.ct_label, bit); if (labelstr) return json_string(labelstr); diff --git a/src/libnftables.c b/src/libnftables.c index 4a139c58..a693c0c6 100644 --- a/src/libnftables.c +++ b/src/libnftables.c @@ -84,26 +84,25 @@ out: return ret; } -static void nft_init(void) +static void nft_init(struct nft_ctx *ctx) { - mark_table_init(); - realm_table_rt_init(); - devgroup_table_init(); - realm_table_meta_init(); - ct_label_table_init(); + mark_table_init(ctx); + realm_table_rt_init(ctx); + devgroup_table_init(ctx); + ct_label_table_init(ctx); + gmp_init(); #ifdef HAVE_LIBXTABLES xt_init(); #endif } -static void nft_exit(void) +static void nft_exit(struct nft_ctx *ctx) { - ct_label_table_exit(); - realm_table_rt_exit(); - devgroup_table_exit(); - realm_table_meta_exit(); - mark_table_exit(); + ct_label_table_exit(ctx); + realm_table_rt_exit(ctx); + devgroup_table_exit(ctx); + mark_table_exit(ctx); } EXPORT_SYMBOL(nft_ctx_add_include_path); @@ -145,10 +144,10 @@ struct nft_ctx *nft_ctx_new(uint32_t flags) { struct nft_ctx *ctx; - nft_init(); ctx = xzalloc(sizeof(struct nft_ctx)); - ctx->state = xzalloc(sizeof(struct parser_state)); + nft_init(ctx); + ctx->state = xzalloc(sizeof(struct parser_state)); nft_ctx_add_include_path(ctx, DEFAULT_INCLUDE_PATH); ctx->parser_max_errors = 10; init_list_head(&ctx->cache.list); @@ -291,7 +290,7 @@ void nft_ctx_free(struct nft_ctx *ctx) nft_ctx_clear_include_paths(ctx); xfree(ctx->state); xfree(ctx); - nft_exit(); + nft_exit(ctx); } EXPORT_SYMBOL(nft_ctx_set_output); diff --git a/src/meta.c b/src/meta.c index 5c0c4e29..5901c991 100644 --- a/src/meta.c +++ b/src/meta.c @@ -37,17 +37,6 @@ #include #include -static struct symbol_table *realm_tbl; -void realm_table_meta_init(void) -{ - realm_tbl = rt_symbol_table_init("/etc/iproute2/rt_realms"); -} - -void realm_table_meta_exit(void) -{ - rt_symbol_table_free(realm_tbl); -} - static void tchandle_type_print(const struct expr *expr, struct output_ctx *octx) { @@ -341,29 +330,27 @@ const struct datatype pkttype_type = { .sym_tbl = &pkttype_type_tbl, }; -struct symbol_table *devgroup_tbl = NULL; - -void devgroup_table_init(void) +void devgroup_table_init(struct nft_ctx *ctx) { - devgroup_tbl = rt_symbol_table_init("/etc/iproute2/group"); + ctx->output.tbl.devgroup = rt_symbol_table_init("/etc/iproute2/group"); } -void devgroup_table_exit(void) +void devgroup_table_exit(struct nft_ctx *ctx) { - rt_symbol_table_free(devgroup_tbl); + rt_symbol_table_free(ctx->output.tbl.devgroup); } static void devgroup_type_print(const struct expr *expr, - struct output_ctx *octx) + struct output_ctx *octx) { - return symbolic_constant_print(devgroup_tbl, expr, true, octx); + return symbolic_constant_print(octx->tbl.devgroup, expr, true, octx); } static struct error_record *devgroup_type_parse(struct parse_ctx *ctx, const struct expr *sym, struct expr **res) { - return symbolic_constant_parse(ctx, sym, devgroup_tbl, res); + return symbolic_constant_parse(ctx, sym, ctx->tbl->devgroup, res); } const struct datatype devgroup_type = { diff --git a/src/rt.c b/src/rt.c index cdd5e9d8..b19c44d6 100644 --- a/src/rt.c +++ b/src/rt.c @@ -24,27 +24,26 @@ #include #include -static struct symbol_table *realm_tbl; -void realm_table_rt_init(void) +void realm_table_rt_init(struct nft_ctx *ctx) { - realm_tbl = rt_symbol_table_init("/etc/iproute2/rt_realms"); + ctx->output.tbl.realm = rt_symbol_table_init("/etc/iproute2/rt_realms"); } -void realm_table_rt_exit(void) +void realm_table_rt_exit(struct nft_ctx *ctx) { - rt_symbol_table_free(realm_tbl); + rt_symbol_table_free(ctx->output.tbl.realm); } static void realm_type_print(const struct expr *expr, struct output_ctx *octx) { - return symbolic_constant_print(realm_tbl, expr, true, octx); + return symbolic_constant_print(octx->tbl.realm, expr, true, octx); } static struct error_record *realm_type_parse(struct parse_ctx *ctx, const struct expr *sym, struct expr **res) { - return symbolic_constant_parse(ctx, sym, realm_tbl, res); + return symbolic_constant_parse(ctx, sym, ctx->tbl->realm, res); } const struct datatype realm_type = { -- cgit v1.2.3