|
Packit Service |
f991dd |
/* SPDX-License-Identifier: LGPL-2.1-only */
|
|
Packit Service |
f991dd |
/*
|
|
Packit Service |
f991dd |
* src/nl-monitor.c Monitor events
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
f991dd |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
f991dd |
* License as published by the Free Software Foundation version 2.1
|
|
Packit Service |
f991dd |
* of the License.
|
|
Packit Service |
f991dd |
*
|
|
Packit Service |
f991dd |
* Copyright (c) 2003-2009 Thomas Graf <tgraf@suug.ch>
|
|
Packit Service |
f991dd |
*/
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <netlink/cli/utils.h>
|
|
Packit Service |
f991dd |
#include <netlink/cli/link.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
#include <linux/rtnetlink.h>
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static const struct {
|
|
Packit Service |
f991dd |
enum rtnetlink_groups gr_id;
|
|
Packit Service |
f991dd |
const char* gr_name;
|
|
Packit Service |
f991dd |
} known_groups[] = {
|
|
Packit Service |
f991dd |
{ RTNLGRP_LINK, "link" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_NOTIFY, "notify" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_NEIGH, "neigh" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_TC, "tc" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV4_IFADDR, "ipv4-ifaddr" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV4_MROUTE, "ipv4-mroute" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV4_ROUTE, "ipv4-route" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_IFADDR, "ipv6-ifaddr" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_MROUTE, "ipv6-mroute" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_ROUTE, "ipv6-route" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_IFINFO, "ipv6-ifinfo" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_DECnet_IFADDR, "decnet-ifaddr" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_DECnet_ROUTE, "decnet-route" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_PREFIX, "ipv6-prefix" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV4_NETCONF, "ipv4-netconf" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_IPV6_NETCONF, "ipv6-netconf" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_MPLS_NETCONF, "mpls-netconf" },
|
|
Packit Service |
f991dd |
{ RTNLGRP_NONE, NULL }
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void obj_input(struct nl_object *obj, void *arg)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
nl_object_dump(obj, arg);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static int event_input(struct nl_msg *msg, void *arg)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
if (nl_msg_parse(msg, &obj_input, arg) < 0)
|
|
Packit Service |
f991dd |
fprintf(stderr, "<<EVENT>> Unknown message type\n");
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
/* Exit nl_recvmsgs_def() and return to the main select() */
|
|
Packit Service |
f991dd |
return NL_STOP;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
static void print_usage(void)
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
int i;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
printf(
|
|
Packit Service |
f991dd |
"Usage: nl-monitor [OPTION] [<groups>]\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
"Options\n"
|
|
Packit Service |
f991dd |
" -f, --format=TYPE Output format { brief | details | stats }\n"
|
|
Packit Service |
f991dd |
" -h, --help Show this help.\n"
|
|
Packit Service |
f991dd |
"\n"
|
|
Packit Service |
f991dd |
);
|
|
Packit Service |
f991dd |
printf("Known groups:");
|
|
Packit Service |
f991dd |
for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++)
|
|
Packit Service |
f991dd |
printf(" %s", known_groups[i].gr_name);
|
|
Packit Service |
f991dd |
printf("\n");
|
|
Packit Service |
f991dd |
exit(0);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
int main(int argc, char *argv[])
|
|
Packit Service |
f991dd |
{
|
|
Packit Service |
f991dd |
struct nl_dump_params dp = {
|
|
Packit Service |
f991dd |
.dp_type = NL_DUMP_STATS,
|
|
Packit Service |
f991dd |
.dp_fd = stdout,
|
|
Packit Service |
f991dd |
.dp_dump_msgtype = 1,
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
struct nl_sock *sock;
|
|
Packit Service |
f991dd |
int err = 1;
|
|
Packit Service |
f991dd |
int i, idx;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
sock = nl_cli_alloc_socket();
|
|
Packit Service |
f991dd |
nl_socket_disable_seq_check(sock);
|
|
Packit Service |
f991dd |
nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, &dp;;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
for (;;) {
|
|
Packit Service |
f991dd |
int c, optidx = 0;
|
|
Packit Service |
f991dd |
static struct option long_opts[] = {
|
|
Packit Service |
f991dd |
{ "format", 1, 0, 'f' },
|
|
Packit Service |
f991dd |
{ 0, 0, 0, 0 }
|
|
Packit Service |
f991dd |
};
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
c = getopt_long(argc, argv, "f:h", long_opts, &optidx);
|
|
Packit Service |
f991dd |
if (c == -1)
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
switch (c) {
|
|
Packit Service |
f991dd |
case 'f':
|
|
Packit Service |
f991dd |
dp.dp_type = nl_cli_parse_dumptype(optarg);
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
default:
|
|
Packit Service |
f991dd |
print_usage();
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_cli_connect(sock, NETLINK_ROUTE);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
for (idx = optind; argc > idx; idx++) {
|
|
Packit Service |
f991dd |
for (i = 0; known_groups[i].gr_id != RTNLGRP_NONE; i++) {
|
|
Packit Service |
f991dd |
if (!strcmp(argv[idx], known_groups[i].gr_name)) {
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if ((err = nl_socket_add_membership(sock, known_groups[i].gr_id)) < 0) {
|
|
Packit Service |
f991dd |
nl_cli_fatal(err, "%s: %s\n", argv[idx],
|
|
Packit Service |
f991dd |
nl_geterror(err));
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
break;
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
if (known_groups[i].gr_id == RTNLGRP_NONE)
|
|
Packit Service |
f991dd |
fprintf(stderr, "Warning: Unknown group: %s\n", argv[idx]);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
nl_cli_link_alloc_cache(sock);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
while (1) {
|
|
Packit Service |
f991dd |
fd_set rfds;
|
|
Packit Service |
f991dd |
int fd, retval;
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
fd = nl_socket_get_fd(sock);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
FD_ZERO(&rfds);
|
|
Packit Service |
f991dd |
FD_SET(fd, &rfds);
|
|
Packit Service |
f991dd |
/* wait for an incoming message on the netlink socket */
|
|
Packit Service |
f991dd |
retval = select(fd+1, &rfds, NULL, NULL, NULL);
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
if (retval) {
|
|
Packit Service |
f991dd |
/* FD_ISSET(fd, &rfds) will be true */
|
|
Packit Service |
f991dd |
nl_recvmsgs_default(sock);
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
}
|
|
Packit Service |
f991dd |
|
|
Packit Service |
f991dd |
return 0;
|
|
Packit Service |
f991dd |
}
|