From b5d925ac026a9d06169691af2d381ba294ab4c7a Mon Sep 17 00:00:00 2001 From: Simon McVittie <smcv@collabora.com> Date: Mon, 3 Aug 2020 19:02:42 +0100 Subject: [PATCH] utils: Add pv_async_signal_safe_error() This gives us a way to report failure in a signal handler, or between fork() and exec(), both of which are contexts where only a restricted set of async-signal-safe functions are allowed. See signal-safety(7), signal(7) and fork(2) for details. Signed-off-by: Simon McVittie <smcv@collabora.com> --- src/utils.c | 21 +++++++++++++++++++++ src/utils.h | 3 +++ 2 files changed, 24 insertions(+) diff --git a/src/utils.c b/src/utils.c index 025fe1e42..55450d3c4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -523,3 +523,24 @@ pv_divert_stdout_to_stderr (GError **error) return g_steal_pointer (&original_stdout); } + +/** + * pv_async_signal_safe_error: + * @message: A human-readable message + * @exit_status: Call `_exit` with this status + * + * Exit with a fatal error, like g_error(), but async-signal-safe + * (see signal-safety(7)). + */ +void +pv_async_signal_safe_error (const char *message, + int exit_status) +{ + if (write (2, message, strlen (message)) < 0) + { + /* Ignore - there's nothing we can do about it anyway - but + * suppress -Wunused-result. */ + } + + _exit (exit_status); +} diff --git a/src/utils.h b/src/utils.h index 021a60dc5..49c065ef3 100644 --- a/src/utils.h +++ b/src/utils.h @@ -55,3 +55,6 @@ gboolean pv_boolean_environment (const gchar *name, gboolean def); FILE *pv_divert_stdout_to_stderr (GError **error); + +void pv_async_signal_safe_error (const char *message, + int exit_status) G_GNUC_NORETURN; -- GitLab