Make-initrd development discussion
 help / color / mirror / Atom feed
From: Alexey Gladkov <gladkov.alexey@gmail.com>
To: make-initrd@lists.altlinux.org
Subject: [make-initrd] [PATCH v1 05/11] feature/procacct: Use msgtemplate instead of custom struct
Date: Thu, 15 Jun 2023 19:59:14 +0200
Message-ID: <3ba27a32bfb13c42047525048da76b26f87df669.1686851829.git.gladkov.alexey@gmail.com> (raw)
In-Reply-To: <cover.1686851829.git.gladkov.alexey@gmail.com>

Signed-off-by: Alexey Gladkov <gladkov.alexey@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



  parent reply	other threads:[~2023-06-15 17:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-15 17:59 [make-initrd] [PATCH v1 00/11] Add accounting feature Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 01/11] feature/procacct: New feature to debug initramfs Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 02/11] feature/procacct: Use epoll Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 03/11] feature/procacct: Use default rcvbufsz Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 04/11] feature/procacct: Track more values Alexey Gladkov
2023-06-15 17:59 ` Alexey Gladkov [this message]
2023-06-15 17:59 ` [make-initrd] [PATCH v1 06/11] feature/procacct: Use nonblocking per-call Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 07/11] feature/procacct: Add bpf helper Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 08/11] feature/procacct: Add accounting report Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 09/11] feature/procacct: Wait until procacct is initialized Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 10/11] feature/procacct: Make procacct optional Alexey Gladkov
2023-06-15 17:59 ` [make-initrd] [PATCH v1 11/11] feature/procacct: Add to testing Alexey Gladkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ba27a32bfb13c42047525048da76b26f87df669.1686851829.git.gladkov.alexey@gmail.com \
    --to=gladkov.alexey@gmail.com \
    --cc=make-initrd@lists.altlinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Make-initrd development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/make-initrd/0 make-initrd/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 make-initrd make-initrd/ http://lore.altlinux.org/make-initrd \
		make-initrd@lists.altlinux.org make-initrd@lists.altlinux.ru make-initrd@lists.altlinux.com
	public-inbox-index make-initrd

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.make-initrd


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git