From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202502 header.b=KJ8eW29Q; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 2D48A5A062A for ; Fri, 21 Feb 2025 09:19:51 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202502; t=1740125982; bh=1UqQN4f+MFdNEzdb1ZGvzsZDpKhK23guvCRPpfrNFxA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KJ8eW29QYgUJbAcHgD9FbC+pds5I2ADryL4y6UwaIwvsmDWfODSYG9LSWZVYs/Js1 7K58fdB+KYcQWeHDGrNWpWFFkdXRrGSl9QBzNvphV979l1WUL+jyQ9uGaM8SPRVdRI bxBdD0lbO1vZEAvMvC4fjqUgvDwafYmRKf/DPsHzWHC0ll7Jwdf6HwqWtKVleNMIhT n2ysydaIwvzv5oHoDyDjA0ZEBgbp0ZxGLVeO0ksvRy3R55MIqCLPapP9aytI05czpr zsjnhXaNAPHFgBmykt6tX23hmzLmVqq2FoY/6XmqVzATs5GoeL7sCmlUd8Lpuds2+q RzSZSEZJ6Hqhw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4YzjjB6WNgz4wy6; Fri, 21 Feb 2025 19:19:42 +1100 (AEDT) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 1/4] passt-repair: Add die() macro Date: Fri, 21 Feb 2025 17:50:07 +1100 Message-ID: <20250221065010.3681262-2-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250221065010.3681262-1-david@gibson.dropbear.id.au> References: <20250221065010.3681262-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 7HW5SL4E4GBV3EOFRPARUU5ALARMFPIU X-Message-ID-Hash: 7HW5SL4E4GBV3EOFRPARUU5ALARMFPIU X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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; -- 2.48.1