@@ -100,7 +100,6 @@ typedef int mode_t;
100100#else
101101#include < pthread.h>
102102#include < sys/resource.h> // getrlimit, setrlimit
103- #include < termios.h> // tcgetattr, tcsetattr
104103#include < unistd.h> // setuid, getuid
105104#endif
106105
@@ -173,9 +172,6 @@ using v8::Value;
173172static Mutex process_mutex;
174173static Mutex environ_mutex;
175174
176- // Safe to call more than once and from signal handlers.
177- inline void PlatformExit ();
178-
179175static bool print_eval = false ;
180176static bool force_repl = false ;
181177static bool syntax_check_only = false ;
@@ -1095,7 +1091,7 @@ void AppendExceptionLine(Environment* env,
10951091 Mutex::ScopedLock lock (process_mutex);
10961092 env->set_printed_error (true );
10971093
1098- PlatformExit ();
1094+ uv_tty_reset_mode ();
10991095 PrintErrorString (" \n %s" , arrow);
11001096 return ;
11011097 }
@@ -3029,7 +3025,7 @@ void SetupProcessObject(Environment* env,
30293025
30303026
30313027void SignalExit (int signo) {
3032- PlatformExit ();
3028+ uv_tty_reset_mode ();
30333029 v8_platform.StopTracingAgent ();
30343030#ifdef __FreeBSD__
30353031 // FreeBSD has a nasty bug, see RegisterSignalHandler for details
@@ -3850,27 +3846,6 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
38503846}
38513847
38523848
3853- #ifdef __POSIX__
3854- static struct {
3855- int flags;
3856- bool isatty;
3857- struct stat stat;
3858- struct termios termios;
3859- } stdio[1 + STDERR_FILENO];
3860-
3861-
3862- inline int GetFileDescriptorFlags (int fd) {
3863- int flags;
3864-
3865- do {
3866- flags = fcntl (fd, F_GETFL);
3867- } while (flags == -1 && errno == EINTR);
3868-
3869- return flags;
3870- }
3871- #endif // __POSIX__
3872-
3873-
38743849inline void PlatformInit () {
38753850#ifdef __POSIX__
38763851#if HAVE_INSPECTOR
@@ -3881,18 +3856,16 @@ inline void PlatformInit() {
38813856#endif // HAVE_INSPECTOR
38823857
38833858 // Make sure file descriptors 0-2 are valid before we start logging anything.
3884- for (auto & s : stdio ) {
3885- const int fd = &s - stdio ;
3886- if (fstat (fd, &s. stat ) == 0 )
3859+ for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1 ) {
3860+ struct stat ignored ;
3861+ if (fstat (fd, &ignored ) == 0 )
38873862 continue ;
38883863 // Anything but EBADF means something is seriously wrong. We don't
38893864 // have to special-case EINTR, fstat() is not interruptible.
38903865 if (errno != EBADF)
38913866 ABORT ();
38923867 if (fd != open (" /dev/null" , O_RDWR))
38933868 ABORT ();
3894- if (fstat (fd, &s.stat ) != 0 )
3895- ABORT ();
38963869 }
38973870
38983871#if HAVE_INSPECTOR
@@ -3915,24 +3888,6 @@ inline void PlatformInit() {
39153888 }
39163889#endif // !NODE_SHARED_MODE
39173890
3918- // Record the state of the stdio file descriptors so we can restore it
3919- // on exit. Needs to happen before installing signal handlers because
3920- // they make use of that information.
3921- for (auto & s : stdio) {
3922- const int fd = &s - stdio;
3923- int err;
3924-
3925- s.flags = GetFileDescriptorFlags (fd);
3926- CHECK_NE (s.flags , -1 );
3927-
3928- if (!isatty (fd)) continue ;
3929- s.isatty = true ;
3930- do {
3931- err = tcgetattr (fd, &s.termios );
3932- } while (err == -1 && errno == EINTR);
3933- CHECK_EQ (err, 0 );
3934- }
3935-
39363891 RegisterSignalHandler (SIGINT, SignalExit, true );
39373892 RegisterSignalHandler (SIGTERM, SignalExit, true );
39383893
@@ -3973,49 +3928,6 @@ inline void PlatformInit() {
39733928}
39743929
39753930
3976- // This function must be safe to call more than once and from signal handlers.
3977- inline void PlatformExit () {
3978- #ifdef __POSIX__
3979- for (auto & s : stdio) {
3980- const int fd = &s - stdio;
3981-
3982- struct stat tmp;
3983- if (-1 == fstat (fd, &tmp)) {
3984- CHECK_EQ (errno, EBADF); // Program closed file descriptor.
3985- continue ;
3986- }
3987-
3988- bool is_same_file =
3989- (s.stat .st_dev == tmp.st_dev && s.stat .st_ino == tmp.st_ino );
3990- if (!is_same_file) continue ; // Program reopened file descriptor.
3991-
3992- int flags = GetFileDescriptorFlags (fd);
3993- CHECK_NE (flags, -1 );
3994-
3995- // Restore the O_NONBLOCK flag if it changed.
3996- if (O_NONBLOCK & (flags ^ s.flags )) {
3997- flags &= ~O_NONBLOCK;
3998- flags |= s.flags & O_NONBLOCK;
3999-
4000- int err;
4001- do {
4002- err = fcntl (fd, F_SETFL, flags);
4003- } while (err == -1 && errno == EINTR);
4004- CHECK_NE (err, -1 );
4005- }
4006-
4007- if (s.isatty ) {
4008- int err;
4009- do {
4010- err = tcsetattr (fd, TCSANOW, &s.termios );
4011- } while (err == -1 && errno == EINTR);
4012- CHECK_NE (err, -1 );
4013- }
4014- }
4015- #endif // __POSIX__
4016- }
4017-
4018-
40193931void ProcessArgv (int * argc,
40203932 const char ** argv,
40213933 int * exec_argc,
@@ -4482,7 +4394,7 @@ inline int Start(uv_loop_t* event_loop,
44824394}
44834395
44844396int Start (int argc, char ** argv) {
4485- atexit ([] () { PlatformExit (); });
4397+ atexit ([] () { uv_tty_reset_mode (); });
44864398 PlatformInit ();
44874399 performance::performance_node_start = PERFORMANCE_NOW ();
44884400
0 commit comments