[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <af27a01e8e204838a1199632109bc9c0e159f586.1604869679.git.me@pmachata.org>
Date: Sun, 8 Nov 2020 22:14:06 +0100
From: Petr Machata <me@...chata.org>
To: netdev@...r.kernel.org, dsahern@...il.com,
stephen@...workplumber.org
Cc: john.fastabend@...il.com, jiri@...dia.com, idosch@...dia.com,
Jakub Kicinski <kuba@...nel.org>,
Roman Mashak <mrv@...atatu.com>,
Leon Romanovsky <leon@...nel.org>,
Petr Machata <me@...chata.org>
Subject: [PATCH iproute2-next v4 01/11] Unify batch processing across tools
The code for handling batches is largely the same across iproute2 tools.
Extract a helper to handle the batch, and adjust the tools to dispatch to
this helper. Sandwitch the invocation between prologue / epilogue code
specific for each tool.
Signed-off-by: Petr Machata <me@...chata.org>
---
bridge/bridge.c | 38 +++++++-------------------------------
devlink/devlink.c | 41 +++++++----------------------------------
include/utils.h | 3 +++
ip/ip.c | 46 ++++++++++------------------------------------
lib/utils.c | 40 ++++++++++++++++++++++++++++++++++++++++
rdma/rdma.c | 38 +++++++-------------------------------
tc/tc.c | 38 +++++++-------------------------------
7 files changed, 81 insertions(+), 163 deletions(-)
diff --git a/bridge/bridge.c b/bridge/bridge.c
index 453d689732bd..8f691cfdd466 100644
--- a/bridge/bridge.c
+++ b/bridge/bridge.c
@@ -77,20 +77,14 @@ static int do_cmd(const char *argv0, int argc, char **argv)
return -1;
}
-static int batch(const char *name)
+static int br_batch_cmd(int argc, char *argv[], void *data)
{
- char *line = NULL;
- size_t len = 0;
- int ret = EXIT_SUCCESS;
+ return do_cmd(argv[0], argc, argv);
+}
- if (name && strcmp(name, "-") != 0) {
- if (freopen(name, "r", stdin) == NULL) {
- fprintf(stderr,
- "Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return EXIT_FAILURE;
- }
- }
+static int batch(const char *name)
+{
+ int ret;
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
@@ -99,25 +93,7 @@ static int batch(const char *name)
rtnl_set_strict_dump(&rth);
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[100];
- int largc;
-
- largc = makeargs(line, largv, 100);
- if (largc == 0)
- continue; /* blank line */
-
- if (do_cmd(largv[0], largc, largv)) {
- fprintf(stderr, "Command failed %s:%d\n",
- name, cmdlineno);
- ret = EXIT_FAILURE;
- if (!force)
- break;
- }
- }
- if (line)
- free(line);
+ ret = do_batch(name, force, br_batch_cmd, NULL);
rtnl_close(&rth);
return ret;
diff --git a/devlink/devlink.c b/devlink/devlink.c
index 1ff865bc5c22..5bb00b3a5b8c 100644
--- a/devlink/devlink.c
+++ b/devlink/devlink.c
@@ -7912,43 +7912,16 @@ static void dl_free(struct dl *dl)
free(dl);
}
-static int dl_batch(struct dl *dl, const char *name, bool force)
+static int dl_batch_cmd(int argc, char *argv[], void *data)
{
- char *line = NULL;
- size_t len = 0;
- int ret = EXIT_SUCCESS;
-
- if (name && strcmp(name, "-") != 0) {
- if (freopen(name, "r", stdin) == NULL) {
- fprintf(stderr,
- "Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return EXIT_FAILURE;
- }
- }
-
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[100];
- int largc;
-
- largc = makeargs(line, largv, 100);
- if (!largc)
- continue; /* blank line */
-
- if (dl_cmd(dl, largc, largv)) {
- fprintf(stderr, "Command failed %s:%d\n",
- name, cmdlineno);
- ret = EXIT_FAILURE;
- if (!force)
- break;
- }
- }
+ struct dl *dl = data;
- if (line)
- free(line);
+ return dl_cmd(dl, argc, argv);
+}
- return ret;
+static int dl_batch(struct dl *dl, const char *name, bool force)
+{
+ return do_batch(name, force, dl_batch_cmd, dl);
}
int main(int argc, char **argv)
diff --git a/include/utils.h b/include/utils.h
index 7041c4612e46..085b17b1f6e3 100644
--- a/include/utils.h
+++ b/include/utils.h
@@ -322,4 +322,7 @@ int get_time64(__s64 *time, const char *str);
char *sprint_time(__u32 time, char *buf);
char *sprint_time64(__s64 time, char *buf);
+int do_batch(const char *name, bool force,
+ int (*cmd)(int argc, char *argv[], void *user), void *user);
+
#endif /* __UTILS_H__ */
diff --git a/ip/ip.c b/ip/ip.c
index ac4450235370..5e31957f2420 100644
--- a/ip/ip.c
+++ b/ip/ip.c
@@ -121,56 +121,30 @@ static int do_cmd(const char *argv0, int argc, char **argv)
return EXIT_FAILURE;
}
-static int batch(const char *name)
+static int ip_batch_cmd(int argc, char *argv[], void *data)
{
- char *line = NULL;
- size_t len = 0;
- int ret = EXIT_SUCCESS;
- int orig_family = preferred_family;
+ const int *orig_family = data;
- batch_mode = 1;
+ preferred_family = *orig_family;
+ return do_cmd(argv[0], argc, argv);
+}
- if (name && strcmp(name, "-") != 0) {
- if (freopen(name, "r", stdin) == NULL) {
- fprintf(stderr,
- "Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return EXIT_FAILURE;
- }
- }
+static int batch(const char *name)
+{
+ int orig_family = preferred_family;
+ int ret;
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return EXIT_FAILURE;
}
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[100];
- int largc;
-
- preferred_family = orig_family;
-
- largc = makeargs(line, largv, 100);
- if (largc == 0)
- continue; /* blank line */
-
- if (do_cmd(largv[0], largc, largv)) {
- fprintf(stderr, "Command failed %s:%d\n",
- name, cmdlineno);
- ret = EXIT_FAILURE;
- if (!force)
- break;
- }
- }
- if (line)
- free(line);
+ ret = do_batch(name, force, ip_batch_cmd, &orig_family);
rtnl_close(&rth);
return ret;
}
-
int main(int argc, char **argv)
{
char *basename;
diff --git a/lib/utils.c b/lib/utils.c
index c98021d6ecad..9815e328c9e0 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1695,3 +1695,43 @@ char *sprint_time64(__s64 time, char *buf)
print_time64(buf, SPRINT_BSIZE-1, time);
return buf;
}
+
+int do_batch(const char *name, bool force,
+ int (*cmd)(int argc, char *argv[], void *data), void *data)
+{
+ char *line = NULL;
+ size_t len = 0;
+ int ret = EXIT_SUCCESS;
+
+ if (name && strcmp(name, "-") != 0) {
+ if (freopen(name, "r", stdin) == NULL) {
+ fprintf(stderr,
+ "Cannot open file \"%s\" for reading: %s\n",
+ name, strerror(errno));
+ return EXIT_FAILURE;
+ }
+ }
+
+ cmdlineno = 0;
+ while (getcmdline(&line, &len, stdin) != -1) {
+ char *largv[100];
+ int largc;
+
+ largc = makeargs(line, largv, 100);
+ if (!largc)
+ continue; /* blank line */
+
+ if (cmd(largc, largv, data)) {
+ fprintf(stderr, "Command failed %s:%d\n",
+ name, cmdlineno);
+ ret = EXIT_FAILURE;
+ if (!force)
+ break;
+ }
+ }
+
+ if (line)
+ free(line);
+
+ return ret;
+}
diff --git a/rdma/rdma.c b/rdma/rdma.c
index 9ea2d17ffe9e..8dc2d3e344be 100644
--- a/rdma/rdma.c
+++ b/rdma/rdma.c
@@ -41,40 +41,16 @@ static int rd_cmd(struct rd *rd, int argc, char **argv)
return rd_exec_cmd(rd, cmds, "object");
}
-static int rd_batch(struct rd *rd, const char *name, bool force)
+static int rd_batch_cmd(int argc, char *argv[], void *data)
{
- char *line = NULL;
- size_t len = 0;
- int ret = 0;
-
- if (name && strcmp(name, "-") != 0) {
- if (!freopen(name, "r", stdin)) {
- pr_err("Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return errno;
- }
- }
+ struct rd *rd = data;
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[512];
- int largc;
-
- largc = makeargs(line, largv, ARRAY_SIZE(largv));
- if (!largc)
- continue; /* blank line */
-
- ret = rd_cmd(rd, largc, largv);
- if (ret) {
- pr_err("Command failed %s:%d\n", name, cmdlineno);
- if (!force)
- break;
- }
- }
-
- free(line);
+ return rd_cmd(rd, argc, argv);
+}
- return ret;
+static int rd_batch(struct rd *rd, const char *name, bool force)
+{
+ return do_batch(name, force, rd_batch_cmd, rd);
}
static int rd_init(struct rd *rd, char *filename)
diff --git a/tc/tc.c b/tc/tc.c
index 5d57054b45fb..01fe58d06202 100644
--- a/tc/tc.c
+++ b/tc/tc.c
@@ -231,22 +231,16 @@ static int do_cmd(int argc, char **argv)
return -1;
}
+static int tc_batch_cmd(int argc, char *argv[], void *data)
+{
+ return do_cmd(argc, argv);
+}
+
static int batch(const char *name)
{
- char *line = NULL;
- size_t len = 0;
- int ret = 0;
+ int ret;
batch_mode = 1;
- if (name && strcmp(name, "-") != 0) {
- if (freopen(name, "r", stdin) == NULL) {
- fprintf(stderr,
- "Cannot open file \"%s\" for reading: %s\n",
- name, strerror(errno));
- return -1;
- }
- }
-
tc_core_init();
if (rtnl_open(&rth, 0) < 0) {
@@ -254,26 +248,8 @@ static int batch(const char *name)
return -1;
}
- cmdlineno = 0;
- while (getcmdline(&line, &len, stdin) != -1) {
- char *largv[100];
- int largc;
-
- largc = makeargs(line, largv, 100);
- if (largc == 0)
- continue; /* blank line */
-
- if (do_cmd(largc, largv)) {
- fprintf(stderr, "Command failed %s:%d\n",
- name, cmdlineno);
- ret = 1;
- if (!force)
- break;
- }
- fflush(stdout);
- }
+ ret = do_batch(name, force, tc_batch_cmd, NULL);
- free(line);
rtnl_close(&rth);
return ret;
}
--
2.25.1
Powered by blists - more mailing lists