git: 883722891aa9 - main - genl: add parser for nlsysevent

From: Baptiste Daroussin <bapt_at_FreeBSD.org>
Date: Mon, 07 Oct 2024 10:14:58 UTC
The branch main has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=883722891aa92fc6033020c67ec7f2f0d9fedf95

commit 883722891aa92fc6033020c67ec7f2f0d9fedf95
Author:     Baptiste Daroussin <bapt@FreeBSD.org>
AuthorDate: 2024-10-07 09:43:50 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2024-10-07 10:11:20 +0000

    genl: add parser for nlsysevent
    
    genl monitor nlsysevent is now able to print the messages received
---
 usr.bin/genl/genl.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)

diff --git a/usr.bin/genl/genl.c b/usr.bin/genl/genl.c
index 88880c4bacbd..48c74fa20c81 100644
--- a/usr.bin/genl/genl.c
+++ b/usr.bin/genl/genl.c
@@ -40,10 +40,12 @@
 #include <netlink/netlink_generic.h>
 #include <netlink/netlink_snl.h>
 #include <netlink/netlink_snl_generic.h>
+#include <netlink/netlink_sysevent.h>
 
 static int monitor_mcast(int argc, char **argv);
 static int list_families(int argc, char **argv);
 static void parser_nlctrl_notify(struct snl_state *ss, struct nlmsghdr *hdr);
+static void parser_nlsysevent(struct snl_state *ss, struct nlmsghdr *hdr);
 static void parser_fallback(struct snl_state *ss, struct nlmsghdr *hdr);
 
 static struct commands {
@@ -60,8 +62,25 @@ static struct mcast_parsers {
 	void (*parser)(struct snl_state *ss, struct nlmsghdr *hdr);
 } mcast_parsers [] = {
 	{ "nlctrl", parser_nlctrl_notify },
+	{ "nlsysevent", parser_nlsysevent },
 };
 
+struct nlevent {
+	const char *name;
+	const char *subsystem;
+	const char *type;
+	const char *data;
+};
+#define _OUT(_field) offsetof(struct nlevent, _field)
+static struct snl_attr_parser ap_nlevent_get[] = {
+	{ .type = NLSE_ATTR_SYSTEM, .off = _OUT(name), .cb = snl_attr_get_string },
+	{ .type = NLSE_ATTR_SUBSYSTEM, .off = _OUT(subsystem), .cb = snl_attr_get_string },
+	{ .type = NLSE_ATTR_TYPE, .off = _OUT(type), .cb = snl_attr_get_string },
+	{ .type = NLSE_ATTR_DATA, .off = _OUT(data), .cb = snl_attr_get_string },
+};
+#undef _OUT
+SNL_DECLARE_GENL_PARSER(nlevent_get_parser, ap_nlevent_get);
+
 struct genl_ctrl_op {
 	uint32_t id;
 	uint32_t flags;
@@ -183,6 +202,20 @@ parser_nlctrl_notify(struct snl_state *ss, struct nlmsghdr *hdr)
 		dump_family(&family);
 }
 
+void
+parser_nlsysevent(struct snl_state *ss, struct nlmsghdr *hdr)
+{
+	struct nlevent ne = {};
+	if (snl_parse_nlmsg(ss, hdr, &nlevent_get_parser, &ne)) {
+		printf("system=%s subsystem=%s type=%s", ne.name, ne.subsystem, ne.type);
+		if (ne.data) {
+			printf(" %s", ne.data);
+			if (ne.data[strlen(ne.data) -1] != '\n')
+				printf("\n");
+		}
+	}
+}
+
 void
 parser_fallback(struct snl_state *ss __unused, struct nlmsghdr *hdr __unused)
 {
@@ -209,6 +242,7 @@ monitor_mcast(int argc __unused, char **argv)
 		usage();
 		return (EXIT_FAILURE);
 	}
+
 	if (!snl_get_genl_family_info(&ss, argv[0], &attrs))
 		errx(EXIT_FAILURE, "Unknown family '%s'", argv[0]);
 	if (argc == 1)