From ec27853181d898f738ad20fed00f37a00d1d09c3 Mon Sep 17 00:00:00 2001 From: Zhang Boyang Date: Thu, 2 Dec 2021 00:21:48 +0800 Subject: Fix incorrect SIGWINCH handling Previously, status line is redrawn in signal handler. However, the drawing code make heavy use of std::string and other syscalls, which may not be async-signal-safe. This will cause deadlock, overwritten errno, even silent memory corruption. This patch implemented Anders Kaseorg's idea. The signal handler will only set a flag, which is async-signal-safe, and actual redrawing will be deferred to PackageManagerFancy::Pulse(). Note that the virtual function PackageManagerFancy::Pulse() already exists in base class but newly overridden in PackageManagerFancy, so the ABI compatibility should be OK. However, existing compiled programs may not aware of this new function and continue to use old Pulse() if compiler had done heavy optimization. Fortunately this is not too harmful because this will only cause status line not redrawing, which may consider acceptable. Closes: #852757 --- apt-pkg/install-progress.cc | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'apt-pkg/install-progress.cc') diff --git a/apt-pkg/install-progress.cc b/apt-pkg/install-progress.cc index aadd28e51..8a6d87cd2 100644 --- a/apt-pkg/install-progress.cc +++ b/apt-pkg/install-progress.cc @@ -222,22 +222,42 @@ PackageManagerFancy::PackageManagerFancy() : d(NULL), child_pty(-1) { // setup terminal size - old_SIGWINCH = signal(SIGWINCH, PackageManagerFancy::staticSIGWINCH); + if (instances.empty()) + SIGWINCH_orig = signal(SIGWINCH, PackageManagerFancy::staticSIGWINCH); instances.push_back(this); } std::vector PackageManagerFancy::instances; +sighandler_t PackageManagerFancy::SIGWINCH_orig; +volatile sig_atomic_t PackageManagerFancy::SIGWINCH_flag = 0; PackageManagerFancy::~PackageManagerFancy() { instances.erase(find(instances.begin(), instances.end(), this)); - signal(SIGWINCH, old_SIGWINCH); + if (instances.empty()) + signal(SIGWINCH, SIGWINCH_orig); } void PackageManagerFancy::staticSIGWINCH(int signum) { - std::vector::const_iterator I; - for(I = instances.begin(); I != instances.end(); ++I) - (*I)->HandleSIGWINCH(signum); + SIGWINCH_flag = 1; +} + +void PackageManagerFancy::CheckSIGWINCH() +{ + if (SIGWINCH_flag) + { + SIGWINCH_flag = 0; + int errsv = errno; + int const nr_terminal_rows = GetTerminalSize().rows; + SetupTerminalScrollArea(nr_terminal_rows); + DrawStatusLine(); + errno = errsv; + } +} + +void PackageManagerFancy::Pulse() +{ + CheckSIGWINCH(); } PackageManagerFancy::TermSize @@ -296,9 +316,7 @@ void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows) void PackageManagerFancy::HandleSIGWINCH(int) { - int const nr_terminal_rows = GetTerminalSize().rows; - SetupTerminalScrollArea(nr_terminal_rows); - DrawStatusLine(); + // for abi compatibility, do not use } void PackageManagerFancy::Start(int a_child_pty) -- cgit v1.2.3-70-g09d2