Commit 173dfe4a by Andy Getzendanner Committed by Copybara-Service

raw_logging: Rename SafeWriteToStderr to indicate what about it is safe (answer:…

raw_logging: Rename SafeWriteToStderr to indicate what about it is safe (answer: it's async-signal-safe).

Also, preserve errno across calls to make it actually signal-safe.

PiperOrigin-RevId: 446620926
Change-Id: I875fbec02b909e8424ddf763303b0d6007f8548f
parent e8cda749
...@@ -75,6 +75,7 @@ cc_library( ...@@ -75,6 +75,7 @@ cc_library(
":atomic_hook", ":atomic_hook",
":config", ":config",
":core_headers", ":core_headers",
":errno_saver",
":log_severity", ":log_severity",
], ],
) )
......
...@@ -66,6 +66,7 @@ absl_cc_library( ...@@ -66,6 +66,7 @@ absl_cc_library(
absl::atomic_hook absl::atomic_hook
absl::config absl::config
absl::core_headers absl::core_headers
absl::errno_saver
absl::log_severity absl::log_severity
COPTS COPTS
${ABSL_DEFAULT_COPTS} ${ABSL_DEFAULT_COPTS}
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/internal/atomic_hook.h" #include "absl/base/internal/atomic_hook.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/log_severity.h" #include "absl/base/log_severity.h"
// We know how to perform low-level writes to stderr in POSIX and Windows. For // We know how to perform low-level writes to stderr in POSIX and Windows. For
...@@ -169,7 +170,7 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line, ...@@ -169,7 +170,7 @@ void RawLogVA(absl::LogSeverity severity, const char* file, int line,
} else { } else {
DoRawLog(&buf, &size, "%s", kTruncated); DoRawLog(&buf, &size, "%s", kTruncated);
} }
SafeWriteToStderr(buffer, strlen(buffer)); AsyncSignalSafeWriteToStderr(buffer, strlen(buffer));
} }
#else #else
static_cast<void>(format); static_cast<void>(format);
...@@ -196,8 +197,11 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line, ...@@ -196,8 +197,11 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
} // namespace } // namespace
void SafeWriteToStderr(const char *s, size_t len) { void AsyncSignalSafeWriteToStderr(const char* s, size_t len) {
absl::base_internal::ErrnoSaver errno_saver;
#if defined(ABSL_HAVE_SYSCALL_WRITE) #if defined(ABSL_HAVE_SYSCALL_WRITE)
// We prefer calling write via `syscall` to minimize the risk of libc doing
// something "helpful".
syscall(SYS_write, STDERR_FILENO, s, len); syscall(SYS_write, STDERR_FILENO, s, len);
#elif defined(ABSL_HAVE_POSIX_WRITE) #elif defined(ABSL_HAVE_POSIX_WRITE)
write(STDERR_FILENO, s, len); write(STDERR_FILENO, s, len);
......
...@@ -109,12 +109,9 @@ namespace raw_logging_internal { ...@@ -109,12 +109,9 @@ namespace raw_logging_internal {
void RawLog(absl::LogSeverity severity, const char* file, int line, void RawLog(absl::LogSeverity severity, const char* file, int line,
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5); const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
// Writes the provided buffer directly to stderr, in a safe, low-level manner. // Writes the provided buffer directly to stderr, in a signal-safe, low-level
// // manner.
// In POSIX this means calling write(), which is async-signal safe and does void AsyncSignalSafeWriteToStderr(const char* s, size_t len);
// not malloc. If the platform supports the SYS_write syscall, we invoke that
// directly to side-step any libc interception.
void SafeWriteToStderr(const char *s, size_t len);
// compile-time function to get the "base" filename, that is, the part of // compile-time function to get the "base" filename, that is, the part of
// a filename after the last "/" or "\" path separator. The search starts at // a filename after the last "/" or "\" path separator. The search starts at
......
...@@ -143,7 +143,6 @@ cc_library( ...@@ -143,7 +143,6 @@ cc_library(
"//absl/base", "//absl/base",
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:errno_saver",
"//absl/base:raw_logging_internal", "//absl/base:raw_logging_internal",
], ],
) )
......
...@@ -126,7 +126,6 @@ absl_cc_library( ...@@ -126,7 +126,6 @@ absl_cc_library(
absl::base absl::base
absl::config absl::config
absl::core_headers absl::core_headers
absl::errno_saver
absl::raw_logging_internal absl::raw_logging_internal
PUBLIC PUBLIC
) )
......
...@@ -42,7 +42,6 @@ ...@@ -42,7 +42,6 @@
#include <ctime> #include <ctime>
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
#include "absl/base/internal/sysinfo.h" #include "absl/base/internal/sysinfo.h"
#include "absl/debugging/internal/examine_stack.h" #include "absl/debugging/internal/examine_stack.h"
...@@ -217,8 +216,7 @@ static void InstallOneFailureHandler(FailureSignalData* data, ...@@ -217,8 +216,7 @@ static void InstallOneFailureHandler(FailureSignalData* data,
#endif #endif
static void WriteToStderr(const char* data) { static void WriteToStderr(const char* data) {
absl::base_internal::ErrnoSaver errno_saver; absl::raw_logging_internal::AsyncSignalSafeWriteToStderr(data, strlen(data));
absl::raw_logging_internal::SafeWriteToStderr(data, strlen(data));
} }
static void WriteSignalMessage(int signo, int cpu, static void WriteSignalMessage(int signo, int cpu,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment