Add an assert() variant that we may call between fork() and exec*().
Signed-off-by: Laszlo Ersek <lersek(a)redhat.com>
---
Notes:
context:-U13
lib/internal.h | 13 ++++++++++++
lib/utils.c | 22 ++++++++++++++++++++
2 files changed, 35 insertions(+)
diff --git a/lib/internal.h b/lib/internal.h
index 0b5f793011b8..9c9021ed366e 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -517,14 +517,27 @@ extern char *nbd_internal_printable_string (const char *str)
extern char *nbd_internal_printable_string_list (char **list)
LIBNBD_ATTRIBUTE_ALLOC_DEALLOC (free);
/* These are wrappers around socket(2) and socketpair(2). They
* always set SOCK_CLOEXEC. nbd_internal_socket can set SOCK_NONBLOCK
* according to the nonblock parameter.
*/
extern int nbd_internal_socket (int domain, int type, int protocol,
bool nonblock);
extern int nbd_internal_socketpair (int domain, int type, int protocol,
int *fds)
LIBNBD_ATTRIBUTE_NONNULL (4);
+extern void nbd_internal_fork_safe_assert (int result, const char *file,
+ long line, const char *func,
+ const char *assertion)
+ LIBNBD_ATTRIBUTE_NONNULL (2, 4, 5);
+
+#ifdef NDEBUG
+#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) ((void)0)
+#else
+#define NBD_INTERNAL_FORK_SAFE_ASSERT(expression) \
+ (nbd_internal_fork_safe_assert ((expression) != 0, __FILE__, __LINE__, \
+ __func__, #expression))
+#endif
+
#endif /* LIBNBD_INTERNAL_H */
diff --git a/lib/utils.c b/lib/utils.c
index 7fb16f6402d1..5dd00f82e073 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -431,13 +431,35 @@ nbd_internal_socketpair (int domain, int type, int protocol, int
*fds)
if (ret == 0) {
for (i = 0; i < 2; i++) {
if (fcntl (fds[i], F_SETFD, FD_CLOEXEC) == -1) {
close (fds[0]);
close (fds[1]);
return -1;
}
}
}
#endif
return ret;
}
+
+void
+nbd_internal_fork_safe_assert (int result, const char *file, long line,
+ const char *func, const char *assertion)
+{
+ const char *line_out;
+ char line_buf[32];
+
+ if (result)
+ return;
+
+ xwrite (STDERR_FILENO, file, strlen (file));
+ xwrite (STDERR_FILENO, ":", 1);
+ line_out = nbd_internal_fork_safe_itoa (line, line_buf, sizeof line_buf);
+ xwrite (STDERR_FILENO, line_out, strlen (line_out));
+ xwrite (STDERR_FILENO, ": ", 2);
+ xwrite (STDERR_FILENO, func, strlen (func));
+ xwrite (STDERR_FILENO, ": Assertion `", 13);
+ xwrite (STDERR_FILENO, assertion, strlen (assertion));
+ xwrite (STDERR_FILENO, "' failed.\n", 10);
+ abort ();
+}