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, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 10/13] passt-repair: Split out inotify handling to its own function
Date: Tue, 21 Apr 2026 12:43:41 +1000	[thread overview]
Message-ID: <20260421024344.1379633-11-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260421024344.1379633-1-david@gibson.dropbear.id.au>

passt-repair can operate two ways: either it can be given an explicit
socket path to connect to, or it can be given a directory.  In the second
mode, it will wait for a socket to appear in that directory before
connecting to it.

That waiting involves some inotify logic that is essentially unrelated to
the rest of the code.  However, it's currently inline in main() making that
very long.  Moreover, the block handling inotify shadows several variables
used in the rest of main() which will make static checkers complain once
we get them running on passt-repair.

Address this by moving the inotify handling into its own function.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 passt-repair.c | 137 +++++++++++++++++++++++++++----------------------
 1 file changed, 77 insertions(+), 60 deletions(-)

diff --git a/passt-repair.c b/passt-repair.c
index c3c140f5..980b0b09 100644
--- a/passt-repair.c
+++ b/passt-repair.c
@@ -46,6 +46,82 @@
 #define REPAIR_EXT		".repair"
 #define REPAIR_EXT_LEN		strlen(REPAIR_EXT)
 
+/**
+ * wait_for_socket() - Wait for a Unix socket to appear in a directory
+ * @a:		Unix domain address to update with socket's path
+ * @dir:	Path to directory to wait for socket in
+ * @sb:		Stat block, populated for the discovered socket on exit
+ *
+ * Return: Length of socket address
+ *
+ * #syscalls:repair close
+ * #syscalls:repair stat|statx stat64|statx statx
+ * #syscalls:repair inotify_init1 inotify_add_watch
+ */
+static int wait_for_socket(struct sockaddr_un *a, const char *dir,
+			   struct stat *sb)
+{
+	char buf[sizeof(struct inotify_event) + NAME_MAX + 1]
+		__attribute__ ((aligned(__alignof__(struct inotify_event))));
+	const struct inotify_event *ev = NULL;
+	char path[PATH_MAX + 1];
+	bool found = false;
+	ssize_t n;
+	int fd;
+
+	if ((fd = inotify_init1(IN_CLOEXEC)) < 0) {
+		fprintf(stderr, "inotify_init1: %i\n", errno);
+		_exit(1);
+	}
+
+	if (inotify_add_watch(fd, dir, IN_CREATE) < 0) {
+		fprintf(stderr, "inotify_add_watch: %i\n", errno);
+		_exit(1);
+	}
+
+	do {
+		char *p;
+
+		n = read(fd, buf, sizeof(buf));
+		if (n < 0) {
+			fprintf(stderr, "inotify read: %i\n", errno);
+			_exit(1);
+		}
+		buf[n - 1] = '\0';
+
+		if (n < (ssize_t)sizeof(*ev)) {
+			fprintf(stderr, "Short inotify read: %zi\n", n);
+			continue;
+		}
+
+		for (p = buf; p < buf + n; p += sizeof(*ev) + ev->len) {
+			ev = (const struct inotify_event *)p;
+
+			if (ev->len >= REPAIR_EXT_LEN &&
+			    !memcmp(ev->name +
+				    strnlen(ev->name, ev->len) -
+				    REPAIR_EXT_LEN,
+				    REPAIR_EXT, REPAIR_EXT_LEN)) {
+				found = true;
+				break;
+			}
+		}
+	} while (!found);
+
+	if (ev->len > NAME_MAX + 1 || ev->name[ev->len - 1] != '\0') {
+		fprintf(stderr, "Invalid filename from inotify\n");
+		_exit(1);
+	}
+
+	snprintf(path, sizeof(path), "%s/%s", dir, ev->name);
+	if ((stat(path, sb))) {
+		fprintf(stderr, "Can't stat() %s: %i\n", path, errno);
+		_exit(1);
+	}
+
+	return snprintf(a->sun_path, sizeof(a->sun_path), "%s", path);
+}
+
 /**
  * main() - Entry point and whole program with loop
  * @argc:	Argument count, must be 2
@@ -59,7 +135,6 @@
  * #syscalls:repair sendto sendmsg arm:send ppc64le:send
  * #syscalls:repair stat|statx stat64|statx statx
  * #syscalls:repair fstat|fstat64 newfstatat|fstatat64
- * #syscalls:repair inotify_init1 inotify_add_watch
  */
 int main(int argc, char **argv)
 {
@@ -112,65 +187,7 @@ int main(int argc, char **argv)
 	}
 
 	if ((sb.st_mode & S_IFMT) == S_IFDIR) {
-		char buf[sizeof(struct inotify_event) + NAME_MAX + 1]
-		   __attribute__ ((aligned(__alignof__(struct inotify_event))));
-		const struct inotify_event *ev = NULL;
-		char path[PATH_MAX + 1];
-		bool found = false;
-		ssize_t n;
-		int fd;
-
-		if ((fd = inotify_init1(IN_CLOEXEC)) < 0) {
-			fprintf(stderr, "inotify_init1: %i\n", errno);
-			_exit(1);
-		}
-
-		if (inotify_add_watch(fd, argv[1], IN_CREATE) < 0) {
-			fprintf(stderr, "inotify_add_watch: %i\n", errno);
-			_exit(1);
-		}
-
-		do {
-			char *p;
-
-			n = read(fd, buf, sizeof(buf));
-			if (n < 0) {
-				fprintf(stderr, "inotify read: %i\n", errno);
-				_exit(1);
-			}
-			buf[n - 1] = '\0';
-
-			if (n < (ssize_t)sizeof(*ev)) {
-				fprintf(stderr, "Short inotify read: %zi\n", n);
-				continue;
-			}
-
-			for (p = buf; p < buf + n; p += sizeof(*ev) + ev->len) {
-				ev = (const struct inotify_event *)p;
-
-				if (ev->len >= REPAIR_EXT_LEN &&
-				    !memcmp(ev->name +
-					    strnlen(ev->name, ev->len) -
-					    REPAIR_EXT_LEN,
-					    REPAIR_EXT, REPAIR_EXT_LEN)) {
-					found = true;
-					break;
-				}
-			}
-		} while (!found);
-
-		if (ev->len > NAME_MAX + 1 || ev->name[ev->len - 1] != '\0') {
-			fprintf(stderr, "Invalid filename from inotify\n");
-			_exit(1);
-		}
-
-		snprintf(path, sizeof(path), "%s/%s", argv[1], ev->name);
-		if ((stat(path, &sb))) {
-			fprintf(stderr, "Can't stat() %s: %i\n", path, errno);
-			_exit(1);
-		}
-
-		ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", path);
+		ret = wait_for_socket(&a, argv[1], &sb);
 		inotify_dir = true;
 	} else {
 		ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", argv[1]);
-- 
2.53.0


  parent reply	other threads:[~2026-04-21  2:43 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-21  2:43 [PATCH 00/13] Improvements to static checker invocation David Gibson
2026-04-21  2:43 ` [PATCH 01/13] Makefile: Use make variables for static checker configuration David Gibson
2026-04-21  2:43 ` [PATCH 02/13] cppcheck: Split out essential defines into a BASE_CPPFLAGS variable David Gibson
2026-04-21  2:43 ` [PATCH 03/13] Makefile: Remove preprocessor flags from $(FLAGS) David Gibson
2026-04-21  2:43 ` [PATCH 04/13] Makefile: Remove non-standard $(FLAGS) variable David Gibson
2026-04-21  2:43 ` [PATCH 05/13] Makefile: Make conditional definition of $(BIN) clearer David Gibson
2026-04-21  2:43 ` [PATCH 06/13] Makefile: Use common binary compilation rule David Gibson
2026-04-21  2:43 ` [PATCH 07/13] Makefile: Remove unhelpful $(HEADERS) variable David Gibson
2026-04-21  2:43 ` [PATCH 08/13] Makefile: Add header dependencies for secondary binaries David Gibson
2026-04-21  2:43 ` [PATCH 09/13] Makefile: Split static checker targets David Gibson
2026-04-21  2:43 ` David Gibson [this message]
2026-04-21  2:43 ` [PATCH 11/13] passt-repair: Simplify construction of Unix path from inotify David Gibson
2026-04-21  2:43 ` [PATCH 12/13] passt-repair: Run static checkers David Gibson
2026-04-21  2:43 ` [PATCH 13/13] qrap: " David Gibson
2026-04-21  3:03 ` [PATCH 00/13] Improvements to static checker invocation David Gibson

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=20260421024344.1379633-11-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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).