]> git.proxmox.com Git - mirror_iproute2.git/blame - tc/tc_monitor.c
tc: Remove pointless assignments in batch()
[mirror_iproute2.git] / tc / tc_monitor.c
CommitLineData
5bec3484
JHS
1/*
2 * tc_monitor.c "tc monitor".
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Jamal Hadi Salim
10 *
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
5bec3484
JHS
16#include <fcntl.h>
17#include <sys/socket.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20#include <string.h>
21#include <time.h>
22#include "rt_names.h"
23#include "utils.h"
24#include "tc_util.h"
25#include "tc_common.h"
26
27
28static void usage(void) __attribute__((noreturn));
29
30static void usage(void)
31{
32a6fbe5 32 fprintf(stderr, "Usage: tc [-timestamp [-tshort] monitor\n");
5bec3484
JHS
33 exit(-1);
34}
35
36
d1f28cf1 37static int accept_tcmsg(const struct sockaddr_nl *who,
0628cddd 38 struct rtnl_ctrl_data *ctrl,
d1f28cf1 39 struct nlmsghdr *n, void *arg)
5bec3484 40{
32a121cb 41 FILE *fp = (FILE *)arg;
5bec3484 42
32a6fbe5
ED
43 if (timestamp)
44 print_timestamp(fp);
45
afcd0699
JP
46 if (n->nlmsg_type == RTM_NEWTFILTER ||
47 n->nlmsg_type == RTM_DELTFILTER ||
48 n->nlmsg_type == RTM_NEWCHAIN ||
49 n->nlmsg_type == RTM_DELCHAIN) {
5bec3484
JHS
50 print_filter(who, n, arg);
51 return 0;
52 }
53 if (n->nlmsg_type == RTM_NEWTCLASS || n->nlmsg_type == RTM_DELTCLASS) {
54 print_class(who, n, arg);
55 return 0;
56 }
57 if (n->nlmsg_type == RTM_NEWQDISC || n->nlmsg_type == RTM_DELQDISC) {
58 print_qdisc(who, n, arg);
59 return 0;
60 }
61 if (n->nlmsg_type == RTM_GETACTION || n->nlmsg_type == RTM_NEWACTION ||
62 n->nlmsg_type == RTM_DELACTION) {
63 print_action(who, n, arg);
64 return 0;
65 }
66 if (n->nlmsg_type != NLMSG_ERROR && n->nlmsg_type != NLMSG_NOOP &&
67 n->nlmsg_type != NLMSG_DONE) {
68 fprintf(fp, "Unknown message: length %08d type %08x flags %08x\n",
69 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
70 }
71 return 0;
72}
73
74int do_tcmonitor(int argc, char **argv)
75{
76 struct rtnl_handle rth;
77 char *file = NULL;
32a121cb 78 unsigned int groups = nl_mgrp(RTNLGRP_TC);
5bec3484
JHS
79
80 while (argc > 0) {
81 if (matches(*argv, "file") == 0) {
82 NEXT_ARG();
83 file = *argv;
84 } else {
85 if (matches(*argv, "help") == 0) {
86 usage();
87 } else {
88 fprintf(stderr, "Argument \"%s\" is unknown, try \"tc monitor help\".\n", *argv);
89 exit(-1);
90 }
91 }
92 argc--; argv++;
93 }
94
95 if (file) {
e49b51d6
SH
96 FILE *fp = fopen(file, "r");
97 int ret;
98
5bec3484
JHS
99 if (fp == NULL) {
100 perror("Cannot fopen");
101 exit(-1);
102 }
e49b51d6
SH
103
104 ret = rtnl_from_file(fp, accept_tcmsg, stdout);
105 fclose(fp);
106 return ret;
5bec3484
JHS
107 }
108
109 if (rtnl_open(&rth, groups) < 0)
110 exit(1);
111
112 ll_init_map(&rth);
113
32a121cb 114 if (rtnl_listen(&rth, accept_tcmsg, (void *)stdout) < 0) {
5bec3484
JHS
115 rtnl_close(&rth);
116 exit(2);
117 }
118
119 rtnl_close(&rth);
120 exit(0);
121}