From e2e340823a42277a472e3eb912c701a4abeac573 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 19 Jun 2022 16:00:05 -0700 Subject: [PATCH] Bravely replace ttyname with ttyname_r This is more thread safe. We'll see if any platforms don't have this. --- src/common.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index b067c2870..75964dca6 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1939,14 +1939,14 @@ std::string get_path_to_tmp_dir() { // bullet-proof and that's OK. bool is_console_session() { static const bool console_session = [] { - ASSERT_IS_MAIN_THREAD(); - - const char *tty_name = ttyname(0); + char tty_name[PATH_MAX]; + if (ttyname_r(STDIN_FILENO, tty_name, sizeof tty_name) != 0) { + return false; + } constexpr auto len = const_strlen("/dev/tty"); const char *TERM = getenv("TERM"); return // Test that the tty matches /dev/(console|dcons|tty[uv\d]) - tty_name && ((strncmp(tty_name, "/dev/tty", len) == 0 && (tty_name[len] == 'u' || tty_name[len] == 'v' || isdigit(tty_name[len]))) || strcmp(tty_name, "/dev/dcons") == 0 || strcmp(tty_name, "/dev/console") == 0)