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
pull/1163/merge
Andy Getzendanner 3 years ago committed by Copybara-Service
parent e8cda74967
commit 173dfe4a4c
  1. 1
      absl/base/BUILD.bazel
  2. 1
      absl/base/CMakeLists.txt
  3. 8
      absl/base/internal/raw_logging.cc
  4. 9
      absl/base/internal/raw_logging.h
  5. 1
      absl/debugging/BUILD.bazel
  6. 1
      absl/debugging/CMakeLists.txt
  7. 4
      absl/debugging/failure_signal_handler.cc

@ -75,6 +75,7 @@ cc_library(
":atomic_hook",
":config",
":core_headers",
":errno_saver",
":log_severity",
],
)

@ -66,6 +66,7 @@ absl_cc_library(
absl::atomic_hook
absl::config
absl::core_headers
absl::errno_saver
absl::log_severity
COPTS
${ABSL_DEFAULT_COPTS}

@ -24,6 +24,7 @@
#include "absl/base/attributes.h"
#include "absl/base/config.h"
#include "absl/base/internal/atomic_hook.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/log_severity.h"
// 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,
} else {
DoRawLog(&buf, &size, "%s", kTruncated);
}
SafeWriteToStderr(buffer, strlen(buffer));
AsyncSignalSafeWriteToStderr(buffer, strlen(buffer));
}
#else
static_cast<void>(format);
@ -196,8 +197,11 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
} // 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)
// We prefer calling write via `syscall` to minimize the risk of libc doing
// something "helpful".
syscall(SYS_write, STDERR_FILENO, s, len);
#elif defined(ABSL_HAVE_POSIX_WRITE)
write(STDERR_FILENO, s, len);

@ -109,12 +109,9 @@ namespace raw_logging_internal {
void RawLog(absl::LogSeverity severity, const char* file, int line,
const char* format, ...) ABSL_PRINTF_ATTRIBUTE(4, 5);
// Writes the provided buffer directly to stderr, in a safe, low-level manner.
//
// In POSIX this means calling write(), which is async-signal safe and does
// 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);
// Writes the provided buffer directly to stderr, in a signal-safe, low-level
// manner.
void AsyncSignalSafeWriteToStderr(const char* s, size_t len);
// 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

@ -143,7 +143,6 @@ cc_library(
"//absl/base",
"//absl/base:config",
"//absl/base:core_headers",
"//absl/base:errno_saver",
"//absl/base:raw_logging_internal",
],
)

@ -126,7 +126,6 @@ absl_cc_library(
absl::base
absl::config
absl::core_headers
absl::errno_saver
absl::raw_logging_internal
PUBLIC
)

@ -42,7 +42,6 @@
#include <ctime>
#include "absl/base/attributes.h"
#include "absl/base/internal/errno_saver.h"
#include "absl/base/internal/raw_logging.h"
#include "absl/base/internal/sysinfo.h"
#include "absl/debugging/internal/examine_stack.h"
@ -217,8 +216,7 @@ static void InstallOneFailureHandler(FailureSignalData* data,
#endif
static void WriteToStderr(const char* data) {
absl::base_internal::ErrnoSaver errno_saver;
absl::raw_logging_internal::SafeWriteToStderr(data, strlen(data));
absl::raw_logging_internal::AsyncSignalSafeWriteToStderr(data, strlen(data));
}
static void WriteSignalMessage(int signo, int cpu,

Loading…
Cancel
Save