|
108 | 108 | #else |
109 | 109 | #include <pthread.h> |
110 | 110 | #include <sys/resource.h> // getrlimit, setrlimit |
| 111 | +#include <termios.h> // tcgetattr, tcsetattr |
111 | 112 | #include <unistd.h> // STDIN_FILENO, STDERR_FILENO |
112 | 113 | #endif |
113 | 114 |
|
@@ -194,7 +195,7 @@ void WaitForInspectorDisconnect(Environment* env) { |
194 | 195 |
|
195 | 196 | #ifdef __POSIX__ |
196 | 197 | void SignalExit(int signo, siginfo_t* info, void* ucontext) { |
197 | | - uv_tty_reset_mode(); |
| 198 | + ResetStdio(); |
198 | 199 | raise(signo); |
199 | 200 | } |
200 | 201 | #endif // __POSIX__ |
@@ -486,7 +487,7 @@ void TrapWebAssemblyOrContinue(int signo, siginfo_t* info, void* ucontext) { |
486 | 487 | if (prev != nullptr) { |
487 | 488 | prev(signo, info, ucontext); |
488 | 489 | } else { |
489 | | - uv_tty_reset_mode(); |
| 490 | + ResetStdio(); |
490 | 491 | raise(signo); |
491 | 492 | } |
492 | 493 | } |
@@ -516,6 +517,16 @@ void RegisterSignalHandler(int signal, |
516 | 517 |
|
517 | 518 | #endif // __POSIX__ |
518 | 519 |
|
| 520 | +#ifdef __POSIX__ |
| 521 | +static struct { |
| 522 | + int flags; |
| 523 | + bool isatty; |
| 524 | + struct stat stat; |
| 525 | + struct termios termios; |
| 526 | +} stdio[1 + STDERR_FILENO]; |
| 527 | +#endif // __POSIX__ |
| 528 | + |
| 529 | + |
519 | 530 | inline void PlatformInit() { |
520 | 531 | #ifdef __POSIX__ |
521 | 532 | #if HAVE_INSPECTOR |
@@ -526,16 +537,18 @@ inline void PlatformInit() { |
526 | 537 | #endif // HAVE_INSPECTOR |
527 | 538 |
|
528 | 539 | // Make sure file descriptors 0-2 are valid before we start logging anything. |
529 | | - for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd += 1) { |
530 | | - struct stat ignored; |
531 | | - if (fstat(fd, &ignored) == 0) |
| 540 | + for (auto& s : stdio) { |
| 541 | + const int fd = &s - stdio; |
| 542 | + if (fstat(fd, &s.stat) == 0) |
532 | 543 | continue; |
533 | 544 | // Anything but EBADF means something is seriously wrong. We don't |
534 | 545 | // have to special-case EINTR, fstat() is not interruptible. |
535 | 546 | if (errno != EBADF) |
536 | 547 | ABORT(); |
537 | 548 | if (fd != open("/dev/null", O_RDWR)) |
538 | 549 | ABORT(); |
| 550 | + if (fstat(fd, &s.stat) != 0) |
| 551 | + ABORT(); |
539 | 552 | } |
540 | 553 |
|
541 | 554 | #if HAVE_INSPECTOR |
@@ -558,6 +571,27 @@ inline void PlatformInit() { |
558 | 571 | } |
559 | 572 | #endif // !NODE_SHARED_MODE |
560 | 573 |
|
| 574 | + // Record the state of the stdio file descriptors so we can restore it |
| 575 | + // on exit. Needs to happen before installing signal handlers because |
| 576 | + // they make use of that information. |
| 577 | + for (auto& s : stdio) { |
| 578 | + const int fd = &s - stdio; |
| 579 | + int err; |
| 580 | + |
| 581 | + do |
| 582 | + s.flags = fcntl(fd, F_GETFL); |
| 583 | + while (s.flags == -1 && errno == EINTR); // NOLINT |
| 584 | + CHECK_NE(s.flags, -1); |
| 585 | + |
| 586 | + if (!isatty(fd)) continue; |
| 587 | + s.isatty = true; |
| 588 | + |
| 589 | + do |
| 590 | + err = tcgetattr(fd, &s.termios); |
| 591 | + while (err == -1 && errno == EINTR); // NOLINT |
| 592 | + CHECK_EQ(err, 0); |
| 593 | + } |
| 594 | + |
561 | 595 | RegisterSignalHandler(SIGINT, SignalExit, true); |
562 | 596 | RegisterSignalHandler(SIGTERM, SignalExit, true); |
563 | 597 |
|
@@ -611,6 +645,54 @@ inline void PlatformInit() { |
611 | 645 | #endif // _WIN32 |
612 | 646 | } |
613 | 647 |
|
| 648 | + |
| 649 | +// Safe to call more than once and from signal handlers. |
| 650 | +void ResetStdio() { |
| 651 | + uv_tty_reset_mode(); |
| 652 | +#ifdef __POSIX__ |
| 653 | + for (auto& s : stdio) { |
| 654 | + const int fd = &s - stdio; |
| 655 | + |
| 656 | + struct stat tmp; |
| 657 | + if (-1 == fstat(fd, &tmp)) { |
| 658 | + CHECK_EQ(errno, EBADF); // Program closed file descriptor. |
| 659 | + continue; |
| 660 | + } |
| 661 | + |
| 662 | + bool is_same_file = |
| 663 | + (s.stat.st_dev == tmp.st_dev && s.stat.st_ino == tmp.st_ino); |
| 664 | + if (!is_same_file) continue; // Program reopened file descriptor. |
| 665 | + |
| 666 | + int flags; |
| 667 | + do |
| 668 | + flags = fcntl(fd, F_GETFL); |
| 669 | + while (flags == -1 && errno == EINTR); // NOLINT |
| 670 | + CHECK_NE(flags, -1); |
| 671 | + |
| 672 | + // Restore the O_NONBLOCK flag if it changed. |
| 673 | + if (O_NONBLOCK & (flags ^ s.flags)) { |
| 674 | + flags &= ~O_NONBLOCK; |
| 675 | + flags |= s.flags & O_NONBLOCK; |
| 676 | + |
| 677 | + int err; |
| 678 | + do |
| 679 | + err = fcntl(fd, F_SETFL, flags); |
| 680 | + while (err == -1 && errno == EINTR); // NOLINT |
| 681 | + CHECK_NE(err, -1); |
| 682 | + } |
| 683 | + |
| 684 | + if (s.isatty) { |
| 685 | + int err; |
| 686 | + do |
| 687 | + err = tcsetattr(fd, TCSANOW, &s.termios); |
| 688 | + while (err == -1 && errno == EINTR); // NOLINT |
| 689 | + CHECK_NE(err, -1); |
| 690 | + } |
| 691 | + } |
| 692 | +#endif // __POSIX__ |
| 693 | +} |
| 694 | + |
| 695 | + |
614 | 696 | int ProcessGlobalArgs(std::vector<std::string>* args, |
615 | 697 | std::vector<std::string>* exec_args, |
616 | 698 | std::vector<std::string>* errors, |
@@ -866,7 +948,7 @@ void Init(int* argc, |
866 | 948 | } |
867 | 949 |
|
868 | 950 | InitializationResult InitializeOncePerProcess(int argc, char** argv) { |
869 | | - atexit([] () { uv_tty_reset_mode(); }); |
| 951 | + atexit(ResetStdio); |
870 | 952 | PlatformInit(); |
871 | 953 | per_process::node_start_time = uv_hrtime(); |
872 | 954 |
|
|