libnftables: minor cleanups initalizing nf_sock instance of nft_ctx
cover libnftables: minor cleanups initalizing nf_sock instance of nft_ctx
There is some unnecessary or redundant code in "src/libnftables.c". Clean up.
This was motivated by an attempt to add a new flag for nft_ctx_new(), beside NFT_CTX_DEFAULT. The current "if (flags == NFT_CTX_DEFAULT)" check is an odd usage for flags (because we would want that behavior for all flags).
Thomas Haller (4):
libnftables: always initialize netlink socket in nft_ctx_new()
libnftables: drop unused argument nf_sock from nft_netlink()
libnftables: inline creation of nf_sock in nft_ctx_new()
libnftables: drop check for nf_sock in nft_ctx_free()
src/libnftables.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/4 libnftables: always initialize netlink socket in nft_ctx_new()
nft_ctx_new() has a flags argument, but currently no flags are supported. The documentation suggests to pass 0 (NFT_CTX_DEFAULT).
Initializing the netlink socket happens by default already, we should do it for all flags. Also because nft_ctx_netlink_init() is not public API so it's not clear how the user gets a functioning context instance otherwise.
If we ever want to not initialize the netlink socket for a context instance, then there should be a dedicated flag for doing that (and additional API for making that mode of operation usable).
--- src/libnftables.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
src/libnftables.c +1 -2
@@ -219,8 +219,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags) ctx->output.error_fp = stderr;init_list_head (&ctx->vars_ctx.indesc_list); -if (flags ==NFT_CTX_DEFAULT ) -nft_ctx_netlink_init (ctx); +nft_ctx_netlink_init (ctx);return ctx; }
Signature · 1 line
2.41.0
PATCH 2/4 libnftables: drop unused argument nf_sock from nft_netlink()
In reply to Thomas Haller--- src/libnftables.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-)
src/libnftables.c +3 -4
@@ -17,8 +17,7 @@#include <string.h> static int nft_netlink (struct nft_ctx *nft, -struct list_head *cmds,struct list_head *msgs, -struct mnl_socket *nf_sock) +struct list_head *cmds,struct list_head *msgs) {uint32_t batch_seqnum, seqnum =0 , last_seqnum =UINT32_MAX , num_cmds =0 ;struct netlink_ctx ctx = {
@@ -595,7 +594,7 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, const char *buf) goto err; } -if (nft_netlink (nft, &cmds, &msgs, nft->nf_sock) !=0 ) +if (nft_netlink (nft, &cmds, &msgs) !=0 ) rc =-1 ; err:erec_print_list (&nft->output, &msgs, nft->debug_mask);
@@ -691,7 +690,7 @@ static int __nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename goto err; } -if (nft_netlink (nft, &cmds, &msgs, nft->nf_sock) !=0 ) +if (nft_netlink (nft, &cmds, &msgs) !=0 ) rc =-1 ; err:erec_print_list (&nft->output, &msgs, nft->debug_mask);
Signature · 1 line
2.41.0
PATCH 3/4 libnftables: inline creation of nf_sock in nft_ctx_new()
In reply to Thomas HallerThe function only has one caller. It's not clear how to extend this in a useful way, so that it makes sense to keep the initialization in a separate function.
Simplify the code, by inlining and dropping the static function nft_ctx_netlink_init(). There was only one caller.
--- src/libnftables.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
src/libnftables.c +1 -6
@@ -186,11 +186,6 @@ void nft_ctx_clear_include_paths(struct nft_ctx *ctx) ctx->include_paths =NULL ; } -static void nft_ctx_netlink_init (struct nft_ctx *ctx) -{ - ctx->nf_sock =nft_mnl_socket_open (); -} -EXPORT_SYMBOL (nft_ctx_new);struct nft_ctx *nft_ctx_new (uint32_t flags) {
@@ -218,7 +213,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags) ctx->output.error_fp = stderr;init_list_head (&ctx->vars_ctx.indesc_list); -nft_ctx_netlink_init (ctx); + ctx->nf_sock =nft_mnl_socket_open ();return ctx; }
Signature · 1 line
2.41.0
PATCH 4/4 libnftables: drop check for nf_sock in nft_ctx_free()
In reply to Thomas HallerThe "nft_ctx" API does not provide a way to change or reconnect the netlink socket. And none of the users would rely on that.
Also note that nft_ctx_new() initializes nf_sock via nft_mnl_socket_open(), which panics of the socket could not be initialized.
This means, the check is unnecessary and needlessly confusing. Drop it.
--- src/libnftables.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
src/libnftables.c +1 -2
@@ -337,8 +337,7 @@ const char *nft_ctx_get_error_buffer(struct nft_ctx *ctx)EXPORT_SYMBOL (nft_ctx_free);void nft_ctx_free (struct nft_ctx *ctx) { -if (ctx->nf_sock) -mnl_socket_close (ctx->nf_sock); +mnl_socket_close (ctx->nf_sock);exit_cookie (&ctx->output.output_cookie);exit_cookie (&ctx->output.error_cookie);
Signature · 1 line
2.41.0
Pablo Neira Ayuso
maintainerQuoted 7 lines · Thomas Haller
There is some unnecessary or redundant code in "
src/libnftables.c". Clean up.This was motivated by an attempt to add a new flag for
nft_ctx_new(), besideNFT_CTX_DEFAULT. The current "if (flags ==NFT_CTX_DEFAULT)" check is an odd usage for flags (because we would want that behavior for all flags).
Series applied, thanks