public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 1/4] passt-repair: Add die() macro
Date: Fri, 21 Feb 2025 17:50:07 +1100	[thread overview]
Message-ID: <20250221065010.3681262-2-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20250221065010.3681262-1-david@gibson.dropbear.id.au>

passt-repair has a frequently repeated idiom of printing an error message
then exiting with non-zero code.  Add our own version of a die() macro to
simplify this.  Probably because of confusion with passt's die() macro we
forgot to explicitly add a newline in some of those error messages.  Make
die() add this as well to be consistent.

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

diff --git a/passt-repair.c b/passt-repair.c
index e0c366e5..d785cd16 100644
--- a/passt-repair.c
+++ b/passt-repair.c
@@ -40,6 +40,13 @@
 
 #define SCM_MAX_FD 253 /* From Linux kernel (include/net/scm.h), not in UAPI */
 
+#define die(status, ...)			\
+	do {					\
+		fprintf(stderr, __VA_ARGS__);	\
+		fprintf(stderr, "\n");		\
+		_exit(status);			\
+	} while (0)
+
 /**
  * main() - Entry point and whole program with loop
  * @argc:	Argument count, must be 2
@@ -72,10 +79,8 @@ int main(int argc, char **argv)
 				   sizeof(filter_repair[0]);
 	prog.filter = filter_repair;
 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
-	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
-		fprintf(stderr, "Failed to apply seccomp filter");
-		_exit(1);
-	}
+	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
+		die(1, "Failed to apply seccomp filter");
 
 	iov = (struct iovec){ &cmd, sizeof(cmd) };
 	msg = (struct msghdr){ .msg_name = NULL, .msg_namelen = 0,
@@ -85,37 +90,28 @@ int main(int argc, char **argv)
 			       .msg_flags = 0 };
 	cmsg = CMSG_FIRSTHDR(&msg);
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s PATH\n", argv[0]);
-		_exit(2);
-	}
+	if (argc != 2)
+		die(2, "Usage: %s PATH", argv[0]);
 
 	ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", argv[1]);
-	if (ret <= 0 || ret >= (int)sizeof(a.sun_path)) {
-		fprintf(stderr, "Invalid socket path: %s\n", argv[1]);
-		_exit(2);
-	}
+	if (ret <= 0 || ret >= (int)sizeof(a.sun_path))
+		die(2, "Invalid socket path: %s", argv[1]);
 
-	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-		fprintf(stderr, "Failed to create AF_UNIX socket: %i\n", errno);
-		_exit(1);
-	}
+	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+		die(1, "Failed to create AF_UNIX socket: %i", errno);
 
 	if (connect(s, (struct sockaddr *)&a, sizeof(a))) {
-		fprintf(stderr, "Failed to connect to %s: %s\n", argv[1],
-			strerror(errno));
-		_exit(1);
+		die(1, "Failed to connect to %s: %s", argv[1],
+		    strerror(errno));
 	}
 
 loop:
 	ret = recvmsg(s, &msg, 0);
 	if (ret < 0) {
-		if (errno == ECONNRESET) {
+		if (errno == ECONNRESET)
 			ret = 0;
-		} else {
-			fprintf(stderr, "Failed to read message: %i\n", errno);
-			_exit(1);
-		}
+		else
+			die(1, "Failed to read message: %i", errno);
 	}
 
 	if (!ret)	/* Done */
@@ -124,10 +120,8 @@ loop:
 	if (!cmsg ||
 	    cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
 	    cmsg->cmsg_len > CMSG_LEN(sizeof(int) * SCM_MAX_FD) ||
-	    cmsg->cmsg_type != SCM_RIGHTS) {
-		fprintf(stderr, "No/bad ancillary data from peer\n");
-		_exit(1);
-	}
+	    cmsg->cmsg_type != SCM_RIGHTS)
+		die(1, "No/bad ancillary data from peer");
 
 	/* No inverse formula for CMSG_LEN(x), and building one with CMSG_LEN(0)
 	 * works but there's no guarantee it does. Search the whole domain.
@@ -140,27 +134,21 @@ loop:
 	}
 	if (!n) {
 		cmsg_len = cmsg->cmsg_len; /* socklen_t is 'unsigned' on musl */
-		fprintf(stderr, "Invalid ancillary data length %zu from peer\n",
-			cmsg_len);
-		_exit(1);
+		die(1, "Invalid ancillary data length %zu from peer", cmsg_len);
 	}
 
 	memcpy(fds, CMSG_DATA(cmsg), sizeof(int) * n);
 
 	if (cmd != TCP_REPAIR_ON && cmd != TCP_REPAIR_OFF &&
-	    cmd != TCP_REPAIR_OFF_NO_WP) {
-		fprintf(stderr, "Unsupported command 0x%04x\n", cmd);
-		_exit(1);
-	}
+	    cmd != TCP_REPAIR_OFF_NO_WP)
+		die(1, "Unsupported command 0x%04x", cmd);
 
 	op = cmd;
 
 	for (i = 0; i < n; i++) {
 		if (setsockopt(fds[i], SOL_TCP, TCP_REPAIR, &op, sizeof(op))) {
-			fprintf(stderr,
-				"Setting TCP_REPAIR to %i on socket %i: %s", op,
-				fds[i], strerror(errno));
-			_exit(1);
+			die(1, "Setting TCP_REPAIR to %i on socket %i: %s",
+			    op, fds[i], strerror(errno));
 		}
 
 		/* Close _our_ copy */
@@ -168,10 +156,8 @@ loop:
 	}
 
 	/* Confirm setting by echoing the command back */
-	if (send(s, &cmd, sizeof(cmd), 0) < 0) {
-		fprintf(stderr, "Reply to %i: %s\n", op, strerror(errno));
-		_exit(1);
-	}
+	if (send(s, &cmd, sizeof(cmd), 0) < 0)
+		die(1, "Reply to %i: %s", op, strerror(errno));
 
 	goto loop;
 
-- 
@@ -40,6 +40,13 @@
 
 #define SCM_MAX_FD 253 /* From Linux kernel (include/net/scm.h), not in UAPI */
 
+#define die(status, ...)			\
+	do {					\
+		fprintf(stderr, __VA_ARGS__);	\
+		fprintf(stderr, "\n");		\
+		_exit(status);			\
+	} while (0)
+
 /**
  * main() - Entry point and whole program with loop
  * @argc:	Argument count, must be 2
@@ -72,10 +79,8 @@ int main(int argc, char **argv)
 				   sizeof(filter_repair[0]);
 	prog.filter = filter_repair;
 	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) ||
-	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
-		fprintf(stderr, "Failed to apply seccomp filter");
-		_exit(1);
-	}
+	    prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
+		die(1, "Failed to apply seccomp filter");
 
 	iov = (struct iovec){ &cmd, sizeof(cmd) };
 	msg = (struct msghdr){ .msg_name = NULL, .msg_namelen = 0,
@@ -85,37 +90,28 @@ int main(int argc, char **argv)
 			       .msg_flags = 0 };
 	cmsg = CMSG_FIRSTHDR(&msg);
 
-	if (argc != 2) {
-		fprintf(stderr, "Usage: %s PATH\n", argv[0]);
-		_exit(2);
-	}
+	if (argc != 2)
+		die(2, "Usage: %s PATH", argv[0]);
 
 	ret = snprintf(a.sun_path, sizeof(a.sun_path), "%s", argv[1]);
-	if (ret <= 0 || ret >= (int)sizeof(a.sun_path)) {
-		fprintf(stderr, "Invalid socket path: %s\n", argv[1]);
-		_exit(2);
-	}
+	if (ret <= 0 || ret >= (int)sizeof(a.sun_path))
+		die(2, "Invalid socket path: %s", argv[1]);
 
-	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
-		fprintf(stderr, "Failed to create AF_UNIX socket: %i\n", errno);
-		_exit(1);
-	}
+	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+		die(1, "Failed to create AF_UNIX socket: %i", errno);
 
 	if (connect(s, (struct sockaddr *)&a, sizeof(a))) {
-		fprintf(stderr, "Failed to connect to %s: %s\n", argv[1],
-			strerror(errno));
-		_exit(1);
+		die(1, "Failed to connect to %s: %s", argv[1],
+		    strerror(errno));
 	}
 
 loop:
 	ret = recvmsg(s, &msg, 0);
 	if (ret < 0) {
-		if (errno == ECONNRESET) {
+		if (errno == ECONNRESET)
 			ret = 0;
-		} else {
-			fprintf(stderr, "Failed to read message: %i\n", errno);
-			_exit(1);
-		}
+		else
+			die(1, "Failed to read message: %i", errno);
 	}
 
 	if (!ret)	/* Done */
@@ -124,10 +120,8 @@ loop:
 	if (!cmsg ||
 	    cmsg->cmsg_len < CMSG_LEN(sizeof(int)) ||
 	    cmsg->cmsg_len > CMSG_LEN(sizeof(int) * SCM_MAX_FD) ||
-	    cmsg->cmsg_type != SCM_RIGHTS) {
-		fprintf(stderr, "No/bad ancillary data from peer\n");
-		_exit(1);
-	}
+	    cmsg->cmsg_type != SCM_RIGHTS)
+		die(1, "No/bad ancillary data from peer");
 
 	/* No inverse formula for CMSG_LEN(x), and building one with CMSG_LEN(0)
 	 * works but there's no guarantee it does. Search the whole domain.
@@ -140,27 +134,21 @@ loop:
 	}
 	if (!n) {
 		cmsg_len = cmsg->cmsg_len; /* socklen_t is 'unsigned' on musl */
-		fprintf(stderr, "Invalid ancillary data length %zu from peer\n",
-			cmsg_len);
-		_exit(1);
+		die(1, "Invalid ancillary data length %zu from peer", cmsg_len);
 	}
 
 	memcpy(fds, CMSG_DATA(cmsg), sizeof(int) * n);
 
 	if (cmd != TCP_REPAIR_ON && cmd != TCP_REPAIR_OFF &&
-	    cmd != TCP_REPAIR_OFF_NO_WP) {
-		fprintf(stderr, "Unsupported command 0x%04x\n", cmd);
-		_exit(1);
-	}
+	    cmd != TCP_REPAIR_OFF_NO_WP)
+		die(1, "Unsupported command 0x%04x", cmd);
 
 	op = cmd;
 
 	for (i = 0; i < n; i++) {
 		if (setsockopt(fds[i], SOL_TCP, TCP_REPAIR, &op, sizeof(op))) {
-			fprintf(stderr,
-				"Setting TCP_REPAIR to %i on socket %i: %s", op,
-				fds[i], strerror(errno));
-			_exit(1);
+			die(1, "Setting TCP_REPAIR to %i on socket %i: %s",
+			    op, fds[i], strerror(errno));
 		}
 
 		/* Close _our_ copy */
@@ -168,10 +156,8 @@ loop:
 	}
 
 	/* Confirm setting by echoing the command back */
-	if (send(s, &cmd, sizeof(cmd), 0) < 0) {
-		fprintf(stderr, "Reply to %i: %s\n", op, strerror(errno));
-		_exit(1);
-	}
+	if (send(s, &cmd, sizeof(cmd), 0) < 0)
+		die(1, "Reply to %i: %s", op, strerror(errno));
 
 	goto loop;
 
-- 
2.48.1


  reply	other threads:[~2025-02-21  8:19 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-21  6:50 [PATCH 0/4] passt-repair improvements David Gibson
2025-02-21  6:50 ` David Gibson [this message]
2025-02-21  6:50 ` [PATCH 2/4] passt-repair: Consistently avoid strerror() David Gibson
2025-02-21  6:50 ` [PATCH 3/4] passt-repair: Improve validation of anciliary data length David Gibson
2025-02-21  6:50 ` [PATCH 4/4] passt-repair: Allow passt-repair to report partial failures 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=20250221065010.3681262-2-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).