>From b88da6aca8d12d0aee9703e54eee4f8a97c4d297 Mon Sep 17 00:00:00 2001 From: Paul Eggert
Date: Sat, 6 Jul 2019 17:25:24 -0700 Subject: [PATCH] Avoid interleaving stderr in a few cases * src/sysdep.c (buferr): New static var. (init_standard_fds) [!DOS_NT]: Initialize it. (errstream, errputc, verrprintf, errwrite): New functions. (close_output_streams): Close buferr too. * src/xdisp.c: Include sysstdio.h instead of stdio.h. (message_to_stderr, vmessage): Use the new functions to avoid interleaving stderr. --- src/sysdep.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++--- src/sysstdio.h | 3 +++ src/xdisp.c | 35 +++++++++-------------------- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/sysdep.c b/src/sysdep.c index 48eebb594f..ee17e08482 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -232,6 +232,10 @@ force_open (int fd, int flags) } } +/* A stream that is like stderr, except line buffered when possible. + It is NULL during startup or if initializing it failed. */ +static FILE *buferr; + /* Make sure stdin, stdout, and stderr are open to something, so that their file descriptors are not hijacked by later system calls. */ void @@ -244,6 +248,15 @@ init_standard_fds (void) force_open (STDIN_FILENO, O_WRONLY); force_open (STDOUT_FILENO, O_RDONLY); force_open (STDERR_FILENO, O_RDONLY); + +#ifndef DOS_NT /* _IOLBF does not work on MS-Windows. */ + buferr = fdopen (STDERR_FILENO, "w"); + if (buferr && setvbuf (buferr, NULL, _IOLBF, 0) != 0) + { + fclose (buferr); + buferr = NULL; + } +#endif } /* Return the current working directory. The result should be freed @@ -2769,6 +2782,46 @@ safe_strsignal (int code) return signame; } +/* Output to stderr. */ + +/* Return the error output stream. */ +static FILE * +errstream (void) +{ + FILE *err = buferr; + if (!err) + return stderr; + fflush_unlocked (stderr); + return err; +} + +/* These functions are like fputc, vfprintf, and fwrite, + except that they output to stderr and buffer better on + platforms that support line buffering. This avoids interleaving + output when Emacs and other processes write to stderr + simultaneously, so long as the lines are short enough. When a + single diagnostic is emitted via a sequence of calls of one or more + of these functions, the caller should arrange for the last called + function to output a newline at the end. */ + +void +errputc (int c) +{ + fputc_unlocked (c, errstream ()); +} + +void +verrprintf (char const *fmt, va_list ap) +{ + vfprintf (errstream (), fmt, ap); +} + +void +errwrite (void const *buf, ptrdiff_t nbuf) +{ + fwrite_unlocked (buf, 1, nbuf, errstream ()); +} + /* Close standard output and standard error, reporting any write errors as best we can. This is intended for use with atexit. */ void @@ -2782,9 +2835,10 @@ close_output_streams (void) /* Do not close stderr if addresses are being sanitized, as the sanitizer might report to stderr after this function is invoked. */ - if (ADDRESS_SANITIZER - ? fflush_unlocked (stderr) != 0 || ferror (stderr) - : close_stream (stderr) != 0) + bool err = buferr && (fflush_unlocked (buferr) != 0 || ferror (buferr)); + if (err | (ADDRESS_SANITIZER + ? fflush_unlocked (stderr) != 0 || ferror (stderr) + : close_stream (stderr) != 0)) _exit (EXIT_FAILURE); } diff --git a/src/sysstdio.h b/src/sysstdio.h index a2364c4e1f..4dc9287e43 100644 --- a/src/sysstdio.h +++ b/src/sysstdio.h @@ -24,6 +24,9 @@ #define EMACS_SYSSTDIO_H #include