From: Phil Sutter <phil@nwl.cc>
To: Pablo Neira Ayuso <pablo@netfilter.org>
Cc: netfilter-devel@vger.kernel.org
Subject: [nft PATCH] libnftables: Unexport enum nftables_exit_codes
Date: Thu, 9 Nov 2017 14:25:26 +0100 [thread overview]
Message-ID: <20171109132526.1493-1-phil@nwl.cc> (raw)
In-Reply-To: <20171024154800.GA11705@salvia>
Apart from SUCCESS/FAILURE, these codes were not used by library
functions simply because NOMEM and NONL conditions lead to calling
exit() instead of propagating the error condition back up the call
stack.
Instead, make nft_run_cmd_from_*() return either 0 or -1 on error.
Usually errno will then contain more details about what happened and/or
there are messages in erec.
Calls to exit() in main() are adjusted to stay compatible.
Signed-off-by: Phil Sutter <phil@nwl.cc>
---
include/nftables.h | 7 +++++++
include/nftables/nftables.h | 10 ----------
src/libnftables.c | 10 +++++-----
src/main.c | 16 ++++++++--------
4 files changed, 20 insertions(+), 23 deletions(-)
diff --git a/include/nftables.h b/include/nftables.h
index d69079fe03e3a..3bfa33e5cb336 100644
--- a/include/nftables.h
+++ b/include/nftables.h
@@ -35,6 +35,13 @@ struct nft_ctx {
uint32_t flags;
};
+enum nftables_exit_codes {
+ NFT_EXIT_SUCCESS = 0,
+ NFT_EXIT_FAILURE = 1,
+ NFT_EXIT_NOMEM = 2,
+ NFT_EXIT_NONL = 3,
+};
+
struct input_descriptor;
struct location {
const struct input_descriptor *indesc;
diff --git a/include/nftables/nftables.h b/include/nftables/nftables.h
index 4211be76e6e40..8e59f2b2a59ab 100644
--- a/include/nftables/nftables.h
+++ b/include/nftables/nftables.h
@@ -38,16 +38,6 @@ enum nft_numeric_level {
*/
#define NFT_CTX_DEFAULT 0
-/**
- * Exit codes returned by nft_run_cmd_from_*()
- */
-enum nftables_exit_codes {
- NFT_EXIT_SUCCESS = 0,
- NFT_EXIT_FAILURE = 1,
- NFT_EXIT_NOMEM = 2,
- NFT_EXIT_NONL = 3,
-};
-
struct nft_ctx *nft_ctx_new(uint32_t flags);
void nft_ctx_free(struct nft_ctx *ctx);
diff --git a/src/libnftables.c b/src/libnftables.c
index dc6a5fdf32640..e8fa6742f7d17 100644
--- a/src/libnftables.c
+++ b/src/libnftables.c
@@ -272,7 +272,7 @@ static const struct input_descriptor indesc_cmdline = {
int nft_run_cmd_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen)
{
- int rc = NFT_EXIT_SUCCESS;
+ int rc = 0;
struct parser_state state;
LIST_HEAD(msgs);
void *scanner;
@@ -284,7 +284,7 @@ int nft_run_cmd_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen)
scanner_push_buffer(scanner, &indesc_cmdline, buf);
if (nft_run(nft, nft->nf_sock, scanner, &state, &msgs) != 0)
- rc = NFT_EXIT_FAILURE;
+ rc = -1;
fp = nft_ctx_set_output(nft, stderr);
erec_print_list(&nft->output, &msgs, nft->debug_mask);
@@ -306,18 +306,18 @@ int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename)
rc = cache_update(nft->nf_sock, &nft->cache, CMD_INVALID, &msgs,
nft->debug_mask, &nft->output);
if (rc < 0)
- return NFT_EXIT_FAILURE;
+ return -1;
parser_init(nft->nf_sock, &nft->cache, &state,
&msgs, nft->debug_mask, &nft->output);
scanner = scanner_init(&state);
if (scanner_read_file(scanner, filename, &internal_location) < 0) {
- rc = NFT_EXIT_FAILURE;
+ rc = -1;
goto err;
}
if (nft_run(nft, nft->nf_sock, scanner, &state, &msgs) != 0)
- rc = NFT_EXIT_FAILURE;
+ rc = -1;
err:
fp = nft_ctx_set_output(nft, stderr);
erec_print_list(&nft->output, &msgs, nft->debug_mask);
diff --git a/src/main.c b/src/main.c
index 529bedffc2e3b..b4648d7008d04 100644
--- a/src/main.c
+++ b/src/main.c
@@ -183,11 +183,11 @@ int main(int argc, char * const *argv)
switch (val) {
case OPT_HELP:
show_help(argv[0]);
- exit(NFT_EXIT_SUCCESS);
+ exit(0);
case OPT_VERSION:
printf("%s v%s (%s)\n",
PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);
- exit(NFT_EXIT_SUCCESS);
+ exit(0);
case OPT_CHECK:
nft_ctx_set_dry_run(nft, true);
break;
@@ -202,7 +202,7 @@ int main(int argc, char * const *argv)
fprintf(stderr,
"Failed to add include path '%s'\n",
optarg);
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
break;
case OPT_NUMERIC:
@@ -211,7 +211,7 @@ int main(int argc, char * const *argv)
fprintf(stderr, "Too many numeric options "
"used, max. %u\n",
NFT_NUMERIC_ALL);
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
nft_ctx_output_set_numeric(nft, numeric + 1);
break;
@@ -241,7 +241,7 @@ int main(int argc, char * const *argv)
if (i == array_size(debug_param)) {
fprintf(stderr, "invalid debug parameter `%s'\n",
optarg);
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
if (end == NULL)
@@ -257,7 +257,7 @@ int main(int argc, char * const *argv)
nft_ctx_output_set_echo(nft, true);
break;
case OPT_INVALID:
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
}
@@ -279,12 +279,12 @@ int main(int argc, char * const *argv)
if (cli_init(nft) < 0) {
fprintf(stderr, "%s: interactive CLI not supported in this build\n",
argv[0]);
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
return 0;
} else {
fprintf(stderr, "%s: no command specified\n", argv[0]);
- exit(NFT_EXIT_FAILURE);
+ exit(1);
}
xfree(buf);
--
2.13.1
next prev parent reply other threads:[~2017-11-09 13:25 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-23 15:33 [nft PATCH v2 0/4] libnftables preparations Phil Sutter
2017-10-23 15:33 ` [nft PATCH v2 1/4] libnftables: Move library stuff out of main.c Phil Sutter
2017-10-24 15:48 ` Pablo Neira Ayuso
2017-10-24 16:42 ` Phil Sutter
2017-11-09 13:25 ` Phil Sutter [this message]
2017-11-09 19:27 ` [nft PATCH] libnftables: Unexport enum nftables_exit_codes Phil Sutter
2017-11-10 11:27 ` [nft PATCH v2] " Phil Sutter
2017-11-13 12:31 ` Pablo Neira Ayuso
2017-11-13 12:38 ` Phil Sutter
2017-11-13 14:08 ` [nft PATCH v3] " Phil Sutter
2017-11-16 13:33 ` Pablo Neira Ayuso
2017-11-16 13:48 ` Phil Sutter
2017-11-13 13:49 ` [nft PATCH v2] " Phil Sutter
2017-11-13 13:53 ` Pablo Neira Ayuso
2017-11-13 14:04 ` Phil Sutter
2017-10-23 15:33 ` [nft PATCH v2 2/4] libnftables: Introduce nft_ctx_flush_cache() Phil Sutter
2017-10-24 15:52 ` Pablo Neira Ayuso
2017-10-24 17:40 ` Phil Sutter
2017-10-25 9:25 ` Pablo Neira Ayuso
2017-10-25 11:40 ` [nft PATCH] libnftables: Get rid of explicit cache flushes Phil Sutter
2017-10-26 18:15 ` Pablo Neira Ayuso
2017-10-23 15:33 ` [nft PATCH v2 3/4] cli: Use nft_run_cmd_from_buffer() Phil Sutter
2017-10-23 15:33 ` [nft PATCH v2 4/4] libnftables: Introduce getters and setters for everything Phil Sutter
2017-10-24 15:20 ` [nft PATCH v2 0/4] libnftables preparations Pablo Neira Ayuso
2017-10-24 16:29 ` Phil Sutter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171109132526.1493-1-phil@nwl.cc \
--to=phil@nwl.cc \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).