blob: 8999e587473fda52b02cc8499489cd67c9210ae6 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#include <netlink/netlink.h>
2#include <netlink/cache.h>
3#include <netlink/cli/utils.h>
4#include <signal.h>
5
6#include <netlink-private/cache-api.h>
7
8static int quit = 0;
9
10static struct nl_dump_params dp = {
11 .dp_type = NL_DUMP_LINE,
12};
13
14
15static void change_cb(struct nl_cache *cache, struct nl_object *obj,
16 int action, void *data)
17{
18 if (action == NL_ACT_NEW)
19 printf("NEW ");
20 else if (action == NL_ACT_DEL)
21 printf("DEL ");
22 else if (action == NL_ACT_CHANGE)
23 printf("CHANGE ");
24
25 nl_object_dump(obj, &dp);
26}
27
28static void sigint(int arg)
29{
30 quit = 1;
31}
32
33int main(int argc, char *argv[])
34{
35 struct nl_cache_mngr *mngr;
36 struct nl_cache *cache;
37 int err, i;
38
39 dp.dp_fd = stdout;
40
41 signal(SIGINT, sigint);
42
43 err = nl_cache_mngr_alloc(NULL, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);
44 if (err < 0)
45 nl_cli_fatal(err, "Unable to allocate cache manager: %s",
46 nl_geterror(err));
47
48 for (i = 1; i < argc; i++) {
49 err = nl_cache_mngr_add(mngr, argv[i], &change_cb, NULL, &cache);
50 if (err < 0)
51 nl_cli_fatal(err, "Unable to add cache %s: %s",
52 argv[i], nl_geterror(err));
53 }
54
55 while (!quit) {
56 int err = nl_cache_mngr_poll(mngr, 1000);
57 if (err < 0 && err != -NLE_INTR)
58 nl_cli_fatal(err, "Polling failed: %s", nl_geterror(err));
59
60 nl_cache_mngr_info(mngr, &dp);
61 }
62
63 nl_cache_mngr_free(mngr);
64
65 return 0;
66}