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