[make-initrd] [PATCH v1 05/11] feature/procacct: Use msgtemplate instead of custom struct

Alexey Gladkov gladkov.alexey at gmail.com
Thu Jun 15 20:59:14 MSK 2023


Signed-off-by: Alexey Gladkov <gladkov.alexey at gmail.com>
---
 features/debug-procacct/src/procacct.c | 35 ++++++++++++--------------
 1 file changed, 16 insertions(+), 19 deletions(-)

diff --git a/features/debug-procacct/src/procacct.c b/features/debug-procacct/src/procacct.c
index 522a82f1..0ff9030f 100644
--- a/features/debug-procacct/src/procacct.c
+++ b/features/debug-procacct/src/procacct.c
@@ -47,7 +47,7 @@
 #include "rd/memory.h"
 
 /* Maximum size of response requested or message sent */
-#define MAX_MSG_SIZE 1024
+#define MAX_MSG_SIZE 2048
 
 /* Maximum number of cpus expected to be specified in a cpumask */
 #define MAX_CPUS 64
@@ -213,35 +213,32 @@ ssize_t send_cmd(int fd, uint16_t nlmsg_type, uint8_t genl_cmd, uint16_t nla_typ
  */
 uint16_t get_family_id(int fd)
 {
-	struct {
-		struct nlmsghdr n;
-		struct genlmsghdr g;
-		char buf[256];
-	} ans;
-
-	strcpy(name, TASKSTATS_GENL_NAME);
+	struct msgtemplate msg;
 
 	ssize_t ret = send_cmd(fd, GENL_ID_CTRL, CTRL_CMD_GETFAMILY,
-	                       CTRL_ATTR_FAMILY_NAME, name, strlen(TASKSTATS_GENL_NAME) + 1);
+	                       CTRL_ATTR_FAMILY_NAME,
+	                       (char *) TASKSTATS_GENL_NAME, sizeof(TASKSTATS_GENL_NAME));
 	if (ret < 0)
 		return 0;
 
-	ret = recv(fd, &ans, sizeof(ans), 0);
+	ret = recv(fd, &msg, sizeof(msg), 0);
+	if (ret < 0)
+		rd_fatal("receive NETLINK family: %m");
 
-	if (ans.n.nlmsg_type == NLMSG_ERROR || (ret < 0) || !NLMSG_OK((&ans.n), ret))
-		return 0;
+	if (msg.n.nlmsg_type == NLMSG_ERROR || !NLMSG_OK((&msg.n), ret)) {
+		struct nlmsgerr *err = NLMSG_DATA(&msg);
+		rd_fatal("receive NETLINK family: %s (errno=%d)", strerror(-err->error),  err->error);
+	}
 
 	struct nlattr *na;
 
-	na = (struct nlattr *) GENLMSG_DATA(&ans);
+	na = (struct nlattr *) GENLMSG_DATA(&msg);
 	na = (struct nlattr *) ((char *) na + NLA_ALIGN(na->nla_len));
 
-	uint16_t id = 0;
-
-	if (na->nla_type == CTRL_ATTR_FAMILY_ID)
-		id = *(uint16_t *) NLA_DATA(na);
+	if (na->nla_type != CTRL_ATTR_FAMILY_ID)
+		rd_fatal("unexpected family id");
 
-	return id;
+	return *(uint16_t *) NLA_DATA(na);
 }
 
 void print_procacct(int fd, struct taskstats *t)
@@ -375,7 +372,7 @@ int process_netlink_events(struct fd_handler *el)
 
 		if (msg.n.nlmsg_type == NLMSG_ERROR || !NLMSG_OK((&msg.n), ret)) {
 			struct nlmsgerr *err = NLMSG_DATA(&msg);
-			rd_fatal("fatal reply error, errno=%d", err->error);
+			rd_fatal("reply error: %s (errno=%d)", strerror(-err->error), err->error);
 		}
 
 		ret = GENLMSG_PAYLOAD(&msg.n);
-- 
2.33.8



More information about the Make-initrd mailing list