diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2026-06-17 10:36:20 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2026-06-17 10:36:20 +0200 |
| commit | 6e05501e8786f651fbf871c414c50573e5fecfb8 (patch) | |
| tree | 7dd8ef5944cc65d8f113e54b44443872292de681 | |
| parent | f47fda7c8061303c0762c6b11b5f916993ead7ca (diff) | |
| download | ttwhy-6e05501e8786f651fbf871c414c50573e5fecfb8.tar.xz ttwhy-6e05501e8786f651fbf871c414c50573e5fecfb8.zip | |
app: implement basic signal handling
| -rw-r--r-- | ttwhy/main.cpp | 26 | ||||
| -rw-r--r-- | ttwhy/scoped_attributes.cppm | 5 |
2 files changed, 27 insertions, 4 deletions
diff --git a/ttwhy/main.cpp b/ttwhy/main.cpp index 32324c0..c210bee 100644 --- a/ttwhy/main.cpp +++ b/ttwhy/main.cpp @@ -1,15 +1,35 @@ -#include <asio/io_context.hpp> - +#include <asio.hpp> #include <stdio.h> import ttwhy; +auto handle_signals(asio::cancellation_signal & cancellation_signal) -> asio::awaitable<void> +{ + co_await asio::this_coro::reset_cancellation_state(asio::disable_cancellation()); + co_await asio::this_coro::throw_if_cancelled(false); + + auto executor = co_await asio::this_coro::executor; + auto signals = asio::signal_set{executor, SIGINT, SIGTERM}; + auto log_stream = asio::posix::stream_descriptor{executor, fileno(stdout)}; + + auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable)); + + if (!error) + { + co_await asio::async_write(log_stream, asio::buffer("shutting down...\n"), asio::as_tuple(asio::use_awaitable)); + cancellation_signal.emit(asio::cancellation_type::all); + } +} + auto main() -> int { auto terminal_attributes = ttwhy::scoped_attributes{fileno(stdin)} // .canonical_mode(false) .echo(false); - auto context = asio::io_context{}; + auto cancellation_signal = asio::cancellation_signal{}; + + asio::co_spawn(context, handle_signals(cancellation_signal), asio::detached); + context.run(); } diff --git a/ttwhy/scoped_attributes.cppm b/ttwhy/scoped_attributes.cppm index dd66b54..7c32ddf 100644 --- a/ttwhy/scoped_attributes.cppm +++ b/ttwhy/scoped_attributes.cppm @@ -45,7 +45,10 @@ namespace ttwhy active_attributes.c_lflag = active_attributes.c_lflag & ~flag; } - tcsetattr(file_descriptor, TCSANOW, &active_attributes); + if (tcsetattr(file_descriptor, TCSANOW, &active_attributes)) + { + throw std::system_error(errno, std::system_category(), "failed to write termios attributes"); + } } //! Store the current TC attributes and restore them on destruction. |
