summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2019-03-19 23:34:47 +0100
committerBruno Haible <bruno@clisp.org>2019-03-19 23:34:47 +0100
commit4a7bec23342ecb43d6e367ed43d067e2a0507f6d (patch)
treeedb976c5082002583b5c6e77a8d6121d82ae60ec
parentcf8abf91bee57994da2b891077e6c7d4fb113445 (diff)
downloadgnulib-4a7bec23342ecb43d6e367ed43d067e2a0507f6d.tar.gz
c-stack: Make signal handlers more reliable.
* lib/c-stack.c (progname): New variable. (die): Use it. (c_stack_action): Initialize it. (segv_handler): Save and restore errno.
-rw-r--r--ChangeLog8
-rw-r--r--lib/c-stack.c8
2 files changed, 15 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 9313fdc488..8a4c5f3d16 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
2019-03-19 Bruno Haible <bruno@clisp.org>
+ c-stack: Make signal handlers more reliable.
+ * lib/c-stack.c (progname): New variable.
+ (die): Use it.
+ (c_stack_action): Initialize it.
+ (segv_handler): Save and restore errno.
+
+2019-03-19 Bruno Haible <bruno@clisp.org>
+
Help making signal handlers more reliable.
* m4/gnulib-common.m4 (gl_COMMON_BODY): Emit definition of
_GL_ASYNC_SAFE into config.h.
diff --git a/lib/c-stack.c b/lib/c-stack.c
index 929bb3a315..f50a4a5dc9 100644
--- a/lib/c-stack.c
+++ b/lib/c-stack.c
@@ -107,6 +107,8 @@ static char const * volatile stack_overflow_message;
appears to have been a stack overflow, or with a core dump
otherwise. This function is async-signal-safe. */
+static char const * volatile progname;
+
static _GL_ASYNC_SAFE _Noreturn void
die (int signo)
{
@@ -119,7 +121,7 @@ die (int signo)
#endif /* !SIGINFO_WORKS && !HAVE_LIBSIGSEGV */
segv_action (signo);
message = signo ? program_error_message : stack_overflow_message;
- ignore_value (write (STDERR_FILENO, getprogname (), strlen (getprogname ())));
+ ignore_value (write (STDERR_FILENO, progname, strlen (progname)));
ignore_value (write (STDERR_FILENO, ": ", 2));
ignore_value (write (STDERR_FILENO, message, strlen (message)));
ignore_value (write (STDERR_FILENO, "\n", 1));
@@ -170,8 +172,10 @@ segv_handler (void *address _GL_UNUSED, int serious)
# if DEBUG
{
char buf[1024];
+ int saved_errno = errno;
sprintf (buf, "segv_handler serious=%d\n", serious);
write (STDERR_FILENO, buf, strlen (buf));
+ errno = saved_errno;
}
# endif
@@ -206,6 +210,7 @@ c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
segv_action = action ? action : null_action;
program_error_message = _("program error");
stack_overflow_message = _("stack overflow");
+ progname = getprogname ();
/* Always install the overflow handler. */
if (stackoverflow_install_handler (overflow_handler,
@@ -298,6 +303,7 @@ c_stack_action (_GL_ASYNC_SAFE void (*action) (int))
segv_action = action ? action : null_action;
program_error_message = _("program error");
stack_overflow_message = _("stack overflow");
+ progname = getprogname ();
sigemptyset (&act.sa_mask);