• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/nl-monitor.c     Monitor events
4  *
5  *	This library is free software; you can redistribute it and/or
6  *	modify it under the terms of the GNU Lesser General Public
7  *	License as published by the Free Software Foundation version 2.1
8  *	of the License.
9  *
10  * Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 #include <netlink/cli/link.h>
15 
16 #include <linux/rtnetlink.h>
17 
18 static const struct {
19 	enum rtnetlink_groups gr_id;
20 	const char* gr_name;
21 } known_groups[] = {
22 	{ RTNLGRP_LINK, "link" },
23 	{ RTNLGRP_NOTIFY, "notify" },
24 	{ RTNLGRP_NEIGH, "neigh" },
25 	{ RTNLGRP_TC, "tc" },
26 	{ RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
27 	{ RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
28 	{ RTNLGRP_IPV4_ROUTE, "ipv4-route" },
29 	{ RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
30 	{ RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
31 	{ RTNLGRP_IPV6_ROUTE, "ipv6-route" },
32 	{ RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
33 	{ RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
34 	{ RTNLGRP_DECnet_ROUTE, "decnet-route" },
35 	{ RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
36 	{ RTNLGRP_IPV4_NETCONF, "ipv4-netconf" },
37 	{ RTNLGRP_IPV6_NETCONF, "ipv6-netconf" },
38 	{ RTNLGRP_MPLS_NETCONF, "mpls-netconf" },
39 	{ RTNLGRP_NONE, NULL }
40 };
41 
obj_input(struct nl_object * obj,void * arg)42 static void obj_input(struct nl_object *obj, void *arg)
43 {
44 	nl_object_dump(obj, arg);
45 }
46 
event_input(struct nl_msg * msg,void * arg)47 static int event_input(struct nl_msg *msg, void *arg)
48 {
49 	if (nl_msg_parse(msg, &obj_input, arg) < 0)
50 		fprintf(stderr, "<<EVENT>> Unknown message type\n");
51 
52 	/* Exit nl_recvmsgs_def() and return to the main select() */
53 	return NL_STOP;
54 }
55 
print_usage(void)56 static void print_usage(void)
57 {
58 	int i;
59 
60         printf(
61 	"Usage: nl-monitor [OPTION] [<groups>]\n"
62 	"\n"
63 	"Options\n"
64 	" -f, --format=TYPE     Output format { brief | details | stats }\n"
65 	" -h, --help            Show this help.\n"
66 	"\n"
67         );
68 	printf("Known groups:");
69 	for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
70 		printf(" %s", known_groups[i].gr_name);
71 	printf("\n");
72         exit(0);
73 }
74 
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 	struct nl_dump_params dp = {
78 		.dp_type = NL_DUMP_STATS,
79 		.dp_fd = stdout,
80 		.dp_dump_msgtype = 1,
81 	};
82 
83 	struct nl_sock *sock;
84 	int err = 1;
85 	int i, idx;
86 
87 	sock = nl_cli_alloc_socket();
88 	nl_socket_disable_seq_check(sock);
89 	nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, &dp);
90 
91 	for (;;) {
92 		int c, optidx = 0;
93 		static struct option long_opts[] = {
94 			{ "format", 1, 0, 'f' },
95 			{ 0, 0, 0, 0 }
96 		};
97 
98 		c = getopt_long(argc, argv, "f:h", long_opts, &optidx);
99 		if (c == -1)
100                         break;
101 
102                 switch (c) {
103                 case 'f':
104 			dp.dp_type = nl_cli_parse_dumptype(optarg);
105 			break;
106 		default:
107 			print_usage();
108 			break;
109 		}
110 	}
111 
112 	nl_cli_connect(sock, NETLINK_ROUTE);
113 
114 	for (idx = optind; argc > idx; idx++) {
115 		for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
116 			if (!strcmp(argv[idx], known_groups[i].gr_name)) {
117 
118 				if ((err = nl_socket_add_membership(sock, known_groups[i].gr_id)) < 0) {
119 					nl_cli_fatal(err, "%s: %s\n", argv[idx],
120 						     nl_geterror(err));
121 				}
122 
123 				break;
124 			}
125 		}
126 		if (known_groups[i].gr_id == RTNLGRP_NONE)
127 			fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
128 	}
129 
130 	nl_cli_link_alloc_cache(sock);
131 
132 	while (1) {
133 		fd_set rfds;
134 		int fd, retval;
135 
136 		fd = nl_socket_get_fd(sock);
137 
138 		FD_ZERO(&rfds);
139 		FD_SET(fd, &rfds);
140 		/* wait for an incoming message on the netlink socket */
141 		retval = select(fd+1, &rfds, NULL, NULL, NULL);
142 
143 		if (retval) {
144 			/* FD_ISSET(fd, &rfds) will be true */
145 			nl_recvmsgs_default(sock);
146 		}
147 	}
148 
149 	return 0;
150 }
151