From 7901660a0de70528c50e812f76df9ba224680258 Mon Sep 17 00:00:00 2001 From: "osdl.net!shemminger" Date: Mon, 14 Mar 2005 19:34:12 +0000 Subject: [PATCH] change how netlink socket is handled for batch mode. (Logical change 1.166) --- tc/tc.c | 32 ++++++++++++++------------------ tc/tc_class.c | 27 +++++---------------------- tc/tc_common.h | 1 + tc/tc_filter.c | 30 +++++------------------------- tc/tc_qdisc.c | 35 +++++------------------------------ 5 files changed, 30 insertions(+), 95 deletions(-) diff --git a/tc/tc.c b/tc/tc.c index 60987e3b..5a98d4ff 100644 --- a/tc/tc.c +++ b/tc/tc.c @@ -35,8 +35,7 @@ int show_details = 0; int show_raw = 0; int resolve_hosts = 0; int use_iec = 0; -struct rtnl_handle g_rth; -int is_batch_mode = 0; +struct rtnl_handle rth; static void *BODY; /* cached handle dlopen(NULL) */ static struct qdisc_util * qdisc_list; @@ -189,15 +188,6 @@ static int usage(void) int main(int argc, char **argv) { - char *basename; - - basename = strrchr(argv[0], '/'); - if (basename == NULL) - basename = argv[0]; - else - basename++; - - /* batch mode */ if (argc > 1 && matches(argv[1], "-batch") == 0) { FILE *batch; @@ -223,12 +213,10 @@ int main(int argc, char **argv) } tc_core_init(); - is_batch_mode=1; - - if (rtnl_open(&g_rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } + if (rtnl_open(&rth, 0) < 0) { + fprintf(stderr, "Cannot open rtnetlink\n"); + exit(1); + } while (fgets(line, sizeof(line)-1, batch)) { if (line[strlen(line)-1]=='\n') { @@ -252,6 +240,7 @@ int main(int argc, char **argv) if (largv[0][0]=='#') continue; + if (matches(largv[0], "qdisc") == 0) { ret += do_qdisc(largc-1, largv+1); } else if (matches(largv[0], "class") == 0) { @@ -267,7 +256,9 @@ int main(int argc, char **argv) } } fclose(batch); - rtnl_close(&g_rth); + + rtnl_close(&rth); + return 0; /* end of batch, that's all */ } @@ -296,6 +287,10 @@ int main(int argc, char **argv) } tc_core_init(); + if (rtnl_open(&rth, 0) < 0) { + fprintf(stderr, "Cannot open rtnetlink\n"); + exit(1); + } if (argc > 1) { if (matches(argv[1], "qdisc") == 0) @@ -312,6 +307,7 @@ int main(int argc, char **argv) return -1; } + rtnl_close(&rth); usage(); return 0; } diff --git a/tc/tc_class.c b/tc/tc_class.c index aba06930..e84112d0 100644 --- a/tc/tc_class.c +++ b/tc/tc_class.c @@ -42,7 +42,6 @@ static void usage(void) int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) { - struct rtnl_handle rth; struct { struct nlmsghdr n; struct tcmsg t; @@ -127,14 +126,8 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) } } - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } - if (d[0]) { - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", d); @@ -142,11 +135,9 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) } } - if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0) + if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) return 2; - if (!is_batch_mode) - rtnl_close(&rth); return 0; } @@ -280,13 +271,7 @@ int tc_class_list(int argc, char **argv) argc--; argv++; } - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } - - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if (d[0]) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { @@ -296,20 +281,18 @@ int tc_class_list(int argc, char **argv) filter_ifindex = t.tcm_ifindex; } - if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETTCLASS, &t, sizeof(t)) < 0) { + if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) { perror("Cannot send dump request"); rtnl_close(&rth); return 1; } - if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_class, stdout, NULL, NULL) < 0) { + if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); rtnl_close(&rth); return 1; } - if (!is_batch_mode) - rtnl_close(&rth); return 0; } diff --git a/tc/tc_common.h b/tc/tc_common.h index 82f59e86..62eca47e 100644 --- a/tc/tc_common.h +++ b/tc/tc_common.h @@ -1,6 +1,7 @@ #define TCA_BUF_MAX (64*1024) +extern struct rtnl_handle rth; extern int do_qdisc(int argc, char **argv); extern int do_class(int argc, char **argv); extern int do_filter(int argc, char **argv); diff --git a/tc/tc_filter.c b/tc/tc_filter.c index 4d3d3765..b68fa63e 100644 --- a/tc/tc_filter.c +++ b/tc/tc_filter.c @@ -49,7 +49,6 @@ static void usage(void) int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) { - struct rtnl_handle rth; struct { struct nlmsghdr n; struct tcmsg t; @@ -154,14 +153,9 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) if (est.ewma_log) addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } if (d[0]) { - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", d); @@ -170,15 +164,11 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) } } - if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0) { + if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) { fprintf(stderr, "We have an error talking to the kernel\n"); - if (!is_batch_mode) - rtnl_close(&rth); return 2; } - if (!is_batch_mode) - rtnl_close(&rth); return 0; } @@ -333,13 +323,7 @@ int tc_filter_list(int argc, char **argv) t.tcm_info = TC_H_MAKE(prio<<16, protocol); - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } - - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if (d[0]) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { @@ -350,20 +334,16 @@ int tc_filter_list(int argc, char **argv) filter_ifindex = t.tcm_ifindex; } - if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETTFILTER, &t, sizeof(t)) < 0) { + if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) { perror("Cannot send dump request"); - if (!is_batch_mode) - rtnl_close(&rth); return 1; } - if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_filter, stdout, NULL, NULL) < 0) { + if (rtnl_dump_filter(&rth, print_filter, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); return 1; } - if (!is_batch_mode) - rtnl_close(&rth); return 0; } diff --git a/tc/tc_qdisc.c b/tc/tc_qdisc.c index 2eacdcd7..2a71208f 100644 --- a/tc/tc_qdisc.c +++ b/tc/tc_qdisc.c @@ -43,7 +43,6 @@ static int usage(void) int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) { - struct rtnl_handle rth; struct qdisc_util *q = NULL; struct tc_estimator est; char d[16]; @@ -139,17 +138,10 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) } } - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - rtnl_close(&rth); - return 1; - } - if (d[0]) { int idx; - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if ((idx = ll_name_to_index(d)) == 0) { fprintf(stderr, "Cannot find device \"%s\"\n", d); @@ -159,14 +151,9 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) req.t.tcm_ifindex = idx; } - if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0) { - if (!is_batch_mode) - rtnl_close(&rth); + if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) return 2; - } - if (!is_batch_mode) - rtnl_close(&rth); return 0; } @@ -283,13 +270,7 @@ int tc_qdisc_list(int argc, char **argv) argc--; argv++; } - if (!is_batch_mode) - if (rtnl_open(&rth, 0) < 0) { - fprintf(stderr, "Cannot open rtnetlink\n"); - return 1; - } - - ll_init_map(&(is_batch_mode?g_rth:rth)); + ll_init_map(&rth); if (d[0]) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { @@ -300,22 +281,16 @@ int tc_qdisc_list(int argc, char **argv) filter_ifindex = t.tcm_ifindex; } - if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETQDISC, &t, sizeof(t)) < 0) { + if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) { perror("Cannot send dump request"); - if (!is_batch_mode) - rtnl_close(&rth); return 1; } - if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_qdisc, stdout, NULL, NULL) < 0) { + if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) { fprintf(stderr, "Dump terminated\n"); - if (!is_batch_mode) - rtnl_close(&rth); return 1; } - if (!is_batch_mode) - rtnl_close(&rth); return 0; }