@@ -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;
@@ -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)
@@ -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__ */
@@ -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;
@@ -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;
+}
@@ -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)
@@ -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;
}
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@pmachata.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(-)