|
| 1 | +/// Use case: there is an application, producing logs. Logs are initially stored |
| 2 | +/// in some in-memory buffer. Periodically, a flush operation is triggered and all |
| 3 | +/// logs are written to various outputs: to remote udp server and to file. But what if |
| 4 | +/// an app receives malicious signal (such as SIGTERM when std::terminate is called or |
| 5 | +/// SIGFPE if current c++ implementation raises it on zero division) then in-buffer |
| 6 | +/// logs are lost by default. To prevent this we have to write custom signal handler. |
| 7 | +/// And to speed this sighandler up we will use corosig-powered async io |
| 8 | + |
| 9 | +#include "boost/outcome/success_failure.hpp" |
| 10 | +#include "corosig/reactor/Default.hpp" |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <boost/outcome/try.hpp> |
| 14 | +#include <corosig/Coro.hpp> |
| 15 | +#include <corosig/ErrorTypes.hpp> |
| 16 | +#include <corosig/Parallel.hpp> |
| 17 | +#include <corosig/Result.hpp> |
| 18 | +#include <corosig/Sighandler.hpp> |
| 19 | +#include <corosig/io/File.hpp> |
| 20 | +#include <corosig/io/TcpSocket.hpp> |
| 21 | +#include <csignal> |
| 22 | +#include <fstream> |
| 23 | +#include <netinet/in.h> |
| 24 | +#include <thread> |
| 25 | + |
| 26 | +namespace { |
| 27 | + |
| 28 | +sockaddr_storage const SERVER_ADDR = [] { |
| 29 | + ::sockaddr_storage addr; |
| 30 | + std::memset(&addr, 0, sizeof(addr)); |
| 31 | + |
| 32 | + auto *addr4 = (::sockaddr_in *)&addr; |
| 33 | + addr4->sin_family = AF_INET; |
| 34 | + addr4->sin_port = htons(8080); |
| 35 | + addr4->sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 36 | + return addr; |
| 37 | +}(); |
| 38 | + |
| 39 | +constexpr std::string_view FILE1 = "file1.log"; |
| 40 | +constexpr std::string_view FILE2 = "file2.log"; |
| 41 | + |
| 42 | +std::vector<std::string> logs_buffer; |
| 43 | + |
| 44 | +corosig::Fut<void, corosig::Error<corosig::AllocationError, corosig::SyscallError>> |
| 45 | +sighandler(int) noexcept { |
| 46 | + using namespace corosig; |
| 47 | + |
| 48 | + auto write_to_file = [](char const *path) -> Fut<void, Error<AllocationError, SyscallError>> { |
| 49 | + using enum File::OpenFlags; |
| 50 | + BOOST_OUTCOME_CO_TRY(auto file, co_await File::open(path, CREATE | TRUNCATE | WRONLY)); |
| 51 | + for (auto &log : logs_buffer) { |
| 52 | + BOOST_OUTCOME_CO_TRY(co_await file.write(log)); |
| 53 | + } |
| 54 | + co_return success(); |
| 55 | + }; |
| 56 | + |
| 57 | + auto send_via_tcp = []() -> Fut<void, Error<AllocationError, SyscallError>> { |
| 58 | + BOOST_OUTCOME_CO_TRY(auto socket, co_await TcpSocket::connect(SERVER_ADDR)); |
| 59 | + for (auto &log : logs_buffer) { |
| 60 | + BOOST_OUTCOME_CO_TRY(co_await socket.write(log)); |
| 61 | + } |
| 62 | + co_return success(); |
| 63 | + }; |
| 64 | + |
| 65 | + BOOST_OUTCOME_CO_TRY(co_await when_all_succeed(write_to_file(FILE1.data()), |
| 66 | + write_to_file(FILE2.data()), send_via_tcp())); |
| 67 | + |
| 68 | + co_return success(); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace |
| 72 | + |
| 73 | +int main() { |
| 74 | + constexpr auto REACTOR_MEMORY = 8 * 1024; |
| 75 | + for (auto signal : {SIGILL, SIGFPE, SIGTERM, SIGABRT}) { |
| 76 | + corosig::set_sighandler<REACTOR_MEMORY, sighandler>(signal); |
| 77 | + } |
| 78 | + |
| 79 | + std::string remote_server_data; |
| 80 | + auto remote_server_thread = std::jthread([&] { |
| 81 | + int srv_fd = ::socket(AF_INET, SOCK_STREAM, 0); |
| 82 | + assert(srv_fd >= 0); |
| 83 | + |
| 84 | + sockaddr_in addr{}; |
| 85 | + addr.sin_family = AF_INET; |
| 86 | + addr.sin_port = ::htons(8080); |
| 87 | + addr.sin_addr.s_addr = ::htonl(INADDR_LOOPBACK); |
| 88 | + |
| 89 | + int opt = 1; |
| 90 | + setsockopt(srv_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
| 91 | + ::bind(srv_fd, (sockaddr *)&addr, sizeof(addr)); // NOLINT |
| 92 | + ::listen(srv_fd, 1); |
| 93 | + |
| 94 | + int client = ::accept(srv_fd, nullptr, nullptr); |
| 95 | + char buf[1024]; // NOLINT |
| 96 | + while (true) { |
| 97 | + ssize_t n = ::read(client, buf, sizeof(buf)); |
| 98 | + if (n <= 0) { |
| 99 | + break; |
| 100 | + } |
| 101 | + remote_server_data += std::string_view{buf, size_t(n)}; |
| 102 | + } |
| 103 | + ::close(client); |
| 104 | + ::close(srv_fd); |
| 105 | + }); |
| 106 | + |
| 107 | + logs_buffer.emplace_back("Log message 1\n"); |
| 108 | + logs_buffer.emplace_back("Log message 2\n"); |
| 109 | + logs_buffer.emplace_back("Log message 3\n"); |
| 110 | + logs_buffer.emplace_back("Log message 4\n"); |
| 111 | + |
| 112 | + ::raise(SIGFPE); |
| 113 | + |
| 114 | + { |
| 115 | + std::ifstream file{FILE1.data()}; |
| 116 | + if (!file.is_open()) { |
| 117 | + std::cerr << "Failed to open file 1\n"; |
| 118 | + } else { |
| 119 | + std::cout << "File 1 contains\n" << file.rdbuf() << '\n'; |
| 120 | + } |
| 121 | + } |
| 122 | + { |
| 123 | + std::ifstream file{FILE2.data()}; |
| 124 | + if (!file.is_open()) { |
| 125 | + std::cerr << "Failed to open file 2\n"; |
| 126 | + } else { |
| 127 | + std::cout << "File 2 contains\n" << file.rdbuf() << '\n'; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + remote_server_thread.join(); |
| 132 | + std::cout << "Remote server received\n" << remote_server_data << '\n'; |
| 133 | + |
| 134 | + return 0; |
| 135 | +} |
0 commit comments