@@ -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 ;
@@ -1095,7 +1091,7 @@ void AppendExceptionLine(Environment* env,
1095
1091
Mutex::ScopedLock lock (process_mutex);
1096
1092
env->set_printed_error (true );
1097
1093
1098
- PlatformExit ();
1094
+ uv_tty_reset_mode ();
1099
1095
PrintErrorString (" \n %s" , arrow);
1100
1096
return ;
1101
1097
}
@@ -3029,7 +3025,7 @@ void SetupProcessObject(Environment* env,
3029
3025
3030
3026
3031
3027
void SignalExit (int signo) {
3032
- PlatformExit ();
3028
+ uv_tty_reset_mode ();
3033
3029
v8_platform.StopTracingAgent ();
3034
3030
#ifdef __FreeBSD__
3035
3031
// FreeBSD has a nasty bug, see RegisterSignalHandler for details
@@ -3850,27 +3846,6 @@ static void DebugEnd(const FunctionCallbackInfo<Value>& args) {
3850
3846
}
3851
3847
3852
3848
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
-
3874
3849
inline void PlatformInit () {
3875
3850
#ifdef __POSIX__
3876
3851
#if HAVE_INSPECTOR
@@ -3881,18 +3856,16 @@ inline void PlatformInit() {
3881
3856
#endif // HAVE_INSPECTOR
3882
3857
3883
3858
// 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 )
3887
3862
continue ;
3888
3863
// Anything but EBADF means something is seriously wrong. We don't
3889
3864
// have to special-case EINTR, fstat() is not interruptible.
3890
3865
if (errno != EBADF)
3891
3866
ABORT ();
3892
3867
if (fd != open (" /dev/null" , O_RDWR))
3893
3868
ABORT ();
3894
- if (fstat (fd, &s.stat ) != 0 )
3895
- ABORT ();
3896
3869
}
3897
3870
3898
3871
#if HAVE_INSPECTOR
@@ -3915,24 +3888,6 @@ inline void PlatformInit() {
3915
3888
}
3916
3889
#endif // !NODE_SHARED_MODE
3917
3890
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
-
3936
3891
RegisterSignalHandler (SIGINT, SignalExit, true );
3937
3892
RegisterSignalHandler (SIGTERM, SignalExit, true );
3938
3893
@@ -3973,49 +3928,6 @@ inline void PlatformInit() {
3973
3928
}
3974
3929
3975
3930
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
-
4019
3931
void ProcessArgv (int * argc,
4020
3932
const char ** argv,
4021
3933
int * exec_argc,
@@ -4482,7 +4394,7 @@ inline int Start(uv_loop_t* event_loop,
4482
4394
}
4483
4395
4484
4396
int Start (int argc, char ** argv) {
4485
- atexit ([] () { PlatformExit (); });
4397
+ atexit ([] () { uv_tty_reset_mode (); });
4486
4398
PlatformInit ();
4487
4399
performance::performance_node_start = PERFORMANCE_NOW ();
4488
4400
0 commit comments