public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top
Subject: [PATCH v2 3/4] Use new lineread implementation for procfs_scan_listen()
Date: Fri, 24 Jun 2022 12:17:31 +1000	[thread overview]
Message-ID: <20220624021732.4062212-4-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20220624021732.4062212-1-david@gibson.dropbear.id.au>

[-- Attachment #1: Type: text/plain, Size: 1410 bytes --]

Use the new more solid implementation of line by line reading for
procfs_scan_listen().

Signed-off-by: David Gibson <david(a)gibson.dropbear.id.au>
---
 util.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/util.c b/util.c
index 7ffd9d1..83729d2 100644
--- a/util.c
+++ b/util.c
@@ -41,6 +41,7 @@
 #include "util.h"
 #include "passt.h"
 #include "packet.h"
+#include "lineread.h"
 
 /* For __openlog() and __setlogmask() wrappers, and passt_vsyslog() */
 static int	log_mask;
@@ -476,7 +477,8 @@ char *line_read(char *buf, size_t len, int fd)
 void procfs_scan_listen(struct ctx *c, uint8_t proto, int ip_version, int ns,
 			uint8_t *map, uint8_t *exclude)
 {
-	char line[BUFSIZ], *path;
+	char *path, *line;
+	struct lineread lr;
 	unsigned long port;
 	unsigned int state;
 	int *fd;
@@ -500,9 +502,9 @@ void procfs_scan_listen(struct ctx *c, uint8_t proto, int ip_version, int ns,
 	else if ((*fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
 		return;
 
-	*line = 0;
-	line_read(line, sizeof(line), *fd);
-	while (line_read(line, sizeof(line), *fd)) {
+	lineread_init(&lr, *fd);
+	lineread_get(&lr, &line); /* throw away header */
+	while (lineread_get(&lr, &line) > 0) {
 		/* NOLINTNEXTLINE(cert-err34-c): != 2 if conversion fails */
 		if (sscanf(line, "%*u: %*x:%lx %*x:%*x %x", &port, &state) != 2)
 			continue;
-- 
@@ -41,6 +41,7 @@
 #include "util.h"
 #include "passt.h"
 #include "packet.h"
+#include "lineread.h"
 
 /* For __openlog() and __setlogmask() wrappers, and passt_vsyslog() */
 static int	log_mask;
@@ -476,7 +477,8 @@ char *line_read(char *buf, size_t len, int fd)
 void procfs_scan_listen(struct ctx *c, uint8_t proto, int ip_version, int ns,
 			uint8_t *map, uint8_t *exclude)
 {
-	char line[BUFSIZ], *path;
+	char *path, *line;
+	struct lineread lr;
 	unsigned long port;
 	unsigned int state;
 	int *fd;
@@ -500,9 +502,9 @@ void procfs_scan_listen(struct ctx *c, uint8_t proto, int ip_version, int ns,
 	else if ((*fd = open(path, O_RDONLY | O_CLOEXEC)) < 0)
 		return;
 
-	*line = 0;
-	line_read(line, sizeof(line), *fd);
-	while (line_read(line, sizeof(line), *fd)) {
+	lineread_init(&lr, *fd);
+	lineread_get(&lr, &line); /* throw away header */
+	while (lineread_get(&lr, &line) > 0) {
 		/* NOLINTNEXTLINE(cert-err34-c): != 2 if conversion fails */
 		if (sscanf(line, "%*u: %*x:%lx %*x:%*x %x", &port, &state) != 2)
 			continue;
-- 
2.36.1


  parent reply	other threads:[~2022-06-24  2:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-24  2:17 [PATCH v2 0/4] New line reading implementation David Gibson
2022-06-24  2:17 ` [PATCH v2 1/4] Add cleaner line-by-line reading primitives David Gibson
2022-06-27 10:36   ` Stefano Brivio
2022-06-28  0:47     ` David Gibson
2022-06-24  2:17 ` [PATCH v2 2/4] Parse resolv.conf with new lineread implementation David Gibson
2022-06-24  2:17 ` David Gibson [this message]
2022-06-24  2:17 ` [PATCH v2 4/4] Remove unused line_read() David Gibson
2022-07-06 17:46 ` [PATCH v2 0/4] New line reading implementation Stefano Brivio

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=20220624021732.4062212-4-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).