From 324af400623a336eb74f51360822d134cad97537 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 18 Jun 2026 17:19:35 +0200 Subject: lib: improve signal handling --- ttwhy/io.cppm | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'ttwhy/io.cppm') diff --git a/ttwhy/io.cppm b/ttwhy/io.cppm index fd84dfd..be87d36 100644 --- a/ttwhy/io.cppm +++ b/ttwhy/io.cppm @@ -14,19 +14,48 @@ import :scanner; namespace ttwhy::io { - export template - auto handle_signals(Stream & stream) -> asio::awaitable + export template + auto handle_signals(Stream & stream, TerminalAttributes & attributes) -> asio::awaitable { auto executor = co_await asio::this_coro::executor; auto signals = asio::signal_set{executor, SIGINT, SIGTERM}; + signals.add(SIGTSTP); + signals.add(SIGCONT); - auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable)); - if (!error) + while (true) { - auto message = std::format("Received signal {}, exiting ...\n", signal); - co_await asio::async_write(stream, asio::buffer(message), asio::use_awaitable); + auto [error, signal] = co_await signals.async_wait(asio::as_tuple(asio::use_awaitable)); + if (error) + { + co_return error; + } + + switch (signal) + { + case SIGINT: + case SIGTERM: + { + auto message = std::format("Received signal {}, exiting ...\n", signal); + co_await asio::async_write(stream, asio::buffer(message), asio::use_awaitable); + co_return asio::error_code{}; + } + case SIGTSTP: + { + attributes.canonical_mode(true).echo(true); + ::raise(SIGSTOP); + break; + } + case SIGCONT: + { + attributes.canonical_mode(false).echo(false); + break; + } + } + + if (!error) + { + } } - co_return error; } export template @@ -45,6 +74,10 @@ namespace ttwhy::io if (error) { + if (error == asio::error::interrupted) + { + continue; + } co_return; } -- cgit v1.2.3