@@ -100,7 +100,6 @@ typedef int mode_t;
100
100
#else
101
101
#include < pthread.h>
102
102
#include < sys/resource.h> // getrlimit, setrlimit
103
- #include < termios.h> // tcgetattr, tcsetattr
104
103
#include < unistd.h> // setuid, getuid
105
104
#endif
106
105
@@ -173,9 +172,6 @@ using v8::Value;
173
172
static Mutex process_mutex;
174
173
static Mutex environ_mutex;
175
174
176
- // Safe to call more than once and from signal handlers.
177
- inline void PlatformExit ();
178
-
179
175
static bool print_eval = false ;
180
176
static bool force_repl = false ;
181
177
static bool syntax_check_only = false ;
@@ -886,7 +882,7 @@ void AppendExceptionLine(Environment* env,
886
882
Mutex::ScopedLock lock (process_mutex);
887
883
env->set_printed_error (true );
888
884
889
- PlatformExit ();
885
+ uv_tty_reset_mode ();
890
886
PrintErrorString (" \n %s" , arrow);
891
887
return ;
892
888
}
@@ -2797,7 +2793,7 @@ void SetupProcessObject(Environment* env,
2797
2793
2798
2794
2799
2795
void SignalExit (int signo) {
2800
- PlatformExit ();
2796
+ uv_tty_reset_mode ();
2801
2797
v8_platform.StopTracingAgent ();
2802
2798
#ifdef __FreeBSD__
2803
2799
// FreeBSD has a nasty bug, see RegisterSignalHandler for details
@@ -3629,27 +3625,6 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
3629
3625
}
3630
3626
3631
3627
3632
- #ifdef __POSIX__
3633
- static struct {
3634
- int flags;
3635
- bool isatty;
3636
- struct stat stat;
3637
- struct termios termios;
3638
- } stdio[1 + STDERR_FILENO];
3639
-
3640
-
3641
- inline int GetFileDescriptorFlags (int fd) {
3642
- int flags;
3643
-
3644
- do {
3645
- flags = fcntl (fd, F_GETFL);
3646
- } while (flags == -1 && errno == EINTR);
3647
-
3648
- return flags;
3649
- }
3650
- #endif // __POSIX__
3651
-
3652
-
3653
3628
inline void PlatformInit () {
3654
3629
#ifdef __POSIX__
3655
3630
#if HAVE_INSPECTOR
@@ -3660,18 +3635,16 @@ inline void PlatformInit() {
3660
3635
#endif // HAVE_INSPECTOR
3661
3636
3662
3637
// Make sure file descriptors 0-2 are valid before we start logging anything.
3663
- for (auto & s : stdio ) {
3664
- const int fd = &s - stdio ;
3665
- if (fstat (fd, &s. stat ) == 0 )
3638
+ for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1 ) {
3639
+ struct stat ignored ;
3640
+ if (fstat (fd, &ignored ) == 0 )
3666
3641
continue ;
3667
3642
// Anything but EBADF means something is seriously wrong. We don't
3668
3643
// have to special-case EINTR, fstat() is not interruptible.
3669
3644
if (errno != EBADF)
3670
3645
ABORT ();
3671
3646
if (fd != open (" /dev/null" , O_RDWR))
3672
3647
ABORT ();
3673
- if (fstat (fd, &s.stat ) != 0 )
3674
- ABORT ();
3675
3648
}
3676
3649
3677
3650
#if HAVE_INSPECTOR
@@ -3694,24 +3667,6 @@ inline void PlatformInit() {
3694
3667
}
3695
3668
#endif // !NODE_SHARED_MODE
3696
3669
3697
- // Record the state of the stdio file descriptors so we can restore it
3698
- // on exit. Needs to happen before installing signal handlers because
3699
- // they make use of that information.
3700
- for (auto & s : stdio) {
3701
- const int fd = &s - stdio;
3702
- int err;
3703
-
3704
- s.flags = GetFileDescriptorFlags (fd);
3705
- CHECK_NE (s.flags , -1 );
3706
-
3707
- if (!isatty (fd)) continue ;
3708
- s.isatty = true ;
3709
- do {
3710
- err = tcgetattr (fd, &s.termios );
3711
- } while (err == -1 && errno == EINTR);
3712
- CHECK_EQ (err, 0 );
3713
- }
3714
-
3715
3670
RegisterSignalHandler (SIGINT, SignalExit, true );
3716
3671
RegisterSignalHandler (SIGTERM, SignalExit, true );
3717
3672
@@ -3752,49 +3707,6 @@ inline void PlatformInit() {
3752
3707
}
3753
3708
3754
3709
3755
- // This function must be safe to call more than once and from signal handlers.
3756
- inline void PlatformExit () {
3757
- #ifdef __POSIX__
3758
- for (auto & s : stdio) {
3759
- const int fd = &s - stdio;
3760
-
3761
- struct stat tmp;
3762
- if (-1 == fstat (fd, &tmp)) {
3763
- CHECK_EQ (errno, EBADF); // Program closed file descriptor.
3764
- continue ;
3765
- }
3766
-
3767
- bool is_same_file =
3768
- (s.stat .st_dev == tmp.st_dev && s.stat .st_ino == tmp.st_ino );
3769
- if (!is_same_file) continue ; // Program reopened file descriptor.
3770
-
3771
- int flags = GetFileDescriptorFlags (fd);
3772
- CHECK_NE (flags, -1 );
3773
-
3774
- // Restore the O_NONBLOCK flag if it changed.
3775
- if (O_NONBLOCK & (flags ^ s.flags )) {
3776
- flags &= ~O_NONBLOCK;
3777
- flags |= s.flags & O_NONBLOCK;
3778
-
3779
- int err;
3780
- do {
3781
- err = fcntl (fd, F_SETFL, flags);
3782
- } while (err == -1 && errno == EINTR);
3783
- CHECK_NE (err, -1 );
3784
- }
3785
-
3786
- if (s.isatty ) {
3787
- int err;
3788
- do {
3789
- err = tcsetattr (fd, TCSANOW, &s.termios );
3790
- } while (err == -1 && errno == EINTR);
3791
- CHECK_NE (err, -1 );
3792
- }
3793
- }
3794
- #endif // __POSIX__
3795
- }
3796
-
3797
-
3798
3710
void ProcessArgv (int * argc,
3799
3711
const char ** argv,
3800
3712
int * exec_argc,
@@ -4265,7 +4177,7 @@ inline int Start(uv_loop_t* event_loop,
4265
4177
}
4266
4178
4267
4179
int Start (int argc, char ** argv) {
4268
- atexit ([] () { PlatformExit (); });
4180
+ atexit ([] () { uv_tty_reset_mode (); });
4269
4181
PlatformInit ();
4270
4182
performance::performance_node_start = PERFORMANCE_NOW ();
4271
4183
0 commit comments