• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * src/genl-ctrl-list.c	List Generic Netlink Families
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-2012 Thomas Graf <tgraf@suug.ch>
11  */
12 
13 #include <netlink/cli/utils.h>
14 
15 #include <linux/genetlink.h>
16 
alloc_genl_family_cache(struct nl_sock * sk)17 static struct nl_cache *alloc_genl_family_cache(struct nl_sock *sk)
18 {
19 	return nl_cli_alloc_cache(sk, "generic netlink family",
20 				  genl_ctrl_alloc_cache);
21 }
22 
print_usage(void)23 static void print_usage(void)
24 {
25 	printf(
26 	"Usage: genl-ctrl-list [--details]\n"
27 	"\n"
28 	"Options\n"
29 	" -d, --details         Include detailed information in the list\n"
30 	" -h, --help            Show this help\n"
31 	" -v, --version         Show versioning information\n"
32 	);
33 	exit(0);
34 }
35 
main(int argc,char * argv[])36 int main(int argc, char *argv[])
37 {
38 	struct nl_sock *sock;
39 	struct nl_cache *family_cache;
40 	struct nl_dump_params params = {
41 		.dp_type = NL_DUMP_LINE,
42 		.dp_fd = stdout,
43 	};
44 
45 	sock = nl_cli_alloc_socket();
46 	nl_cli_connect(sock, NETLINK_GENERIC);
47 	family_cache = alloc_genl_family_cache(sock);
48 
49 	for (;;) {
50 		int c, optidx = 0;
51 		static struct option long_opts[] = {
52 			{ "details", 0, 0, 'd' },
53 			{ "format", 1, 0, 'f' },
54 			{ "help", 0, 0, 'h' },
55 			{ "version", 0, 0, 'v' },
56 			{ 0, 0, 0, 0 }
57 		};
58 
59 		c = getopt_long(argc, argv, "df:hv", long_opts, &optidx);
60 		if (c == -1)
61 			break;
62 
63 		switch (c) {
64 		case 'f': params.dp_type = nl_cli_parse_dumptype(optarg); break;
65 		case 'd': params.dp_type = NL_DUMP_DETAILS; break;
66 		case 'h': print_usage(); break;
67 		case 'v': nl_cli_print_version(); break;
68 		}
69 	}
70 
71 	nl_cache_dump(family_cache, &params);
72 
73 	return 0;
74 }
75