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