-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Win32 Signals] Add term and ctrl-c signal handlers (#13954)
Part 1 of #13188. Adds support for ctrl+c and ctrl+break for Envoy on Windows. The implementation for this following: * On platform_impl we register a CtrlHandler which runs on a separate thread. * On signal_impl we register a read event reader. Thread (1) and (2) communicate via a socket pair and the event is handled on Windows the same way as it is handled on POSIX Signed-off-by: Sotiris Nanopoulos <[email protected]>
- Loading branch information
Sotiris Nanopoulos
authored
Dec 8, 2020
1 parent
9093131
commit 9e2df02
Showing
19 changed files
with
309 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
source/common/event/signal_impl.cc → source/common/event/posix/signal_impl.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "common/api/os_sys_calls_impl.h" | ||
#include "common/event/dispatcher_impl.h" | ||
#include "common/event/signal_impl.h" | ||
|
||
#include "event2/event.h" | ||
|
||
namespace Envoy { | ||
namespace Event { | ||
|
||
SignalEventImpl::SignalEventImpl(DispatcherImpl& dispatcher, signal_t signal_num, SignalCb cb) | ||
: cb_(cb) { | ||
|
||
if (signal_num > eventBridgeHandlersSingleton::get().size()) { | ||
PANIC("Attempting to create SignalEventImpl with a signal id that exceeds the number of " | ||
"supported signals."); | ||
} | ||
|
||
if (eventBridgeHandlersSingleton::get()[signal_num]) { | ||
return; | ||
} | ||
os_fd_t socks[2]; | ||
Api::SysCallIntResult result = | ||
Api::OsSysCallsSingleton::get().socketpair(AF_INET, SOCK_STREAM, IPPROTO_TCP, socks); | ||
ASSERT(result.rc_ == 0); | ||
|
||
read_handle_ = std::make_unique<Network::IoSocketHandleImpl>(socks[0], false, AF_INET); | ||
result = read_handle_->setBlocking(false); | ||
ASSERT(result.rc_ == 0); | ||
auto write_handle = std::make_shared<Network::IoSocketHandleImpl>(socks[1], false, AF_INET); | ||
result = write_handle->setBlocking(false); | ||
ASSERT(result.rc_ == 0); | ||
|
||
read_handle_->initializeFileEvent( | ||
dispatcher, | ||
[this](uint32_t events) -> void { | ||
ASSERT(events == Event::FileReadyType::Read); | ||
cb_(); | ||
}, | ||
Event::FileTriggerType::Level, Event::FileReadyType::Read); | ||
eventBridgeHandlersSingleton::get()[signal_num] = write_handle; | ||
} | ||
|
||
} // namespace Event | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#pragma once | ||
|
||
#include "envoy/event/signal.h" | ||
#include "envoy/network/io_handle.h" | ||
|
||
#include "common/event/dispatcher_impl.h" | ||
#include "common/event/event_impl_base.h" | ||
#include "common/network/io_socket_handle_impl.h" | ||
#include "common/singleton/threadsafe_singleton.h" | ||
|
||
#include "absl/container/flat_hash_map.h" | ||
|
||
namespace Envoy { | ||
namespace Event { | ||
|
||
/** | ||
* libevent implementation of Event::SignalEvent. | ||
*/ | ||
class SignalEventImpl : public SignalEvent { | ||
public: | ||
SignalEventImpl(DispatcherImpl& dispatcher, signal_t signal_num, SignalCb cb); | ||
|
||
private: | ||
SignalCb cb_; | ||
Network::IoHandlePtr read_handle_; | ||
}; | ||
|
||
// Windows ConsoleControlHandler does not allow for a context. As a result the thread | ||
// spawned to handle the console events communicates with the main program with this socketpair. | ||
// Here we have a map from signal types to IoHandle. When we write to this handle we trigger an | ||
// event that notifies Envoy to act on the signal. | ||
using eventBridgeHandlersSingleton = | ||
ThreadSafeSingleton<std::array<std::shared_ptr<Network::IoHandle>, ENVOY_WIN32_SIGNAL_COUNT>>; | ||
} // namespace Event | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.