libnl  3.6.0
genl-ctrl-list.c
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2012 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 #include <netlink/cli/utils.h>
7 
8 #include <linux/genetlink.h>
9 
10 static struct nl_cache *alloc_genl_family_cache(struct nl_sock *sk)
11 {
12  return nl_cli_alloc_cache(sk, "generic netlink family",
14 }
15 
16 static void print_usage(void)
17 {
18  printf(
19  "Usage: genl-ctrl-list [--details]\n"
20  "\n"
21  "Options\n"
22  " -d, --details Include detailed information in the list\n"
23  " -h, --help Show this help\n"
24  " -v, --version Show versioning information\n"
25  );
26  exit(0);
27 }
28 
29 int main(int argc, char *argv[])
30 {
31  struct nl_sock *sock;
32  struct nl_cache *family_cache;
33  struct nl_dump_params params = {
35  .dp_fd = stdout,
36  };
37 
38  sock = nl_cli_alloc_socket();
39  nl_cli_connect(sock, NETLINK_GENERIC);
40  family_cache = alloc_genl_family_cache(sock);
41 
42  for (;;) {
43  int c, optidx = 0;
44  static struct option long_opts[] = {
45  { "details", 0, 0, 'd' },
46  { "format", 1, 0, 'f' },
47  { "help", 0, 0, 'h' },
48  { "version", 0, 0, 'v' },
49  { 0, 0, 0, 0 }
50  };
51 
52  c = getopt_long(argc, argv, "df:hv", long_opts, &optidx);
53  if (c == -1)
54  break;
55 
56  switch (c) {
57  case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
58  case 'd': params.dp_type = NL_DUMP_DETAILS; break;
59  case 'h': print_usage(); break;
60  case 'v': nl_cli_print_version(); break;
61  }
62  }
63 
64  nl_cache_dump(family_cache, &params);
65 
66  return 0;
67 }
void nl_cache_dump(struct nl_cache *cache, struct nl_dump_params *params)
Dump all elements of a cache.
Definition: cache.c:1197
int genl_ctrl_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Allocate a new controller cache.
Definition: ctrl.c:327
@ NL_DUMP_LINE
Dump object briefly on one line.
Definition: types.h:16
@ NL_DUMP_DETAILS
Dump all attributes but no statistics.
Definition: types.h:17
Dumping parameters.
Definition: types.h:28
enum nl_dump_type dp_type
Specifies the type of dump that is requested.
Definition: types.h:32