From 30a34657b84e8a1acc91fb0695ac97c90303fe9c Mon Sep 17 00:00:00 2001 From: Ivan Kruglov Date: Thu, 31 Oct 2024 10:58:21 +0100 Subject: [PATCH] machine: introduce report_errno_and_exit() --- src/machine/operation.c | 15 +++++++++++++++ src/machine/operation.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/src/machine/operation.c b/src/machine/operation.c index 7d7bfa50627..0b2139acf03 100644 --- a/src/machine/operation.c +++ b/src/machine/operation.c @@ -154,3 +154,18 @@ Operation *operation_free(Operation *o) { return mfree(o); } + +_noreturn_ void report_errno_and_exit(int errno_fd, int r) { + if (r >= 0) + _exit(EXIT_SUCCESS); + + assert(errno_fd >= 0); + + ssize_t n = write(errno_fd, &r, sizeof(r)); + if (n < 0) + log_debug_errno(errno, "Failed to write operation's errno: %m"); + if (n != sizeof(r)) + log_debug_errno(SYNTHETIC_ERRNO(EIO), "Sent unexpectedly short message"); + + _exit(EXIT_FAILURE); +} diff --git a/src/machine/operation.h b/src/machine/operation.h index 75bf918c2b1..ee9149854ac 100644 --- a/src/machine/operation.h +++ b/src/machine/operation.h @@ -39,3 +39,5 @@ static inline int operation_new_with_bus_reply(Manager *manager, Machine *machin static inline int operation_new_with_varlink_reply(Manager *manager, Machine *machine, pid_t child, sd_varlink *link, int errno_fd, Operation **ret) { return operation_new(manager, machine, child, /* message = */ NULL, link, errno_fd, ret); } + +void report_errno_and_exit(int errno_fd, int r); -- 2.47.3