forked from ak-fortuna/fortuna
add chance namespace; handle signals
This commit is contained in:
parent
710ee80f91
commit
d401bb6c25
93
main.cpp
93
main.cpp
|
@ -1,23 +1,96 @@
|
|||
#include "fortuna.h"
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <csignal>
|
||||
#include <cstdlib>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
namespace chance {
|
||||
static constexpr const bool AUTOSTOP{false};
|
||||
const std::unique_ptr<fortuna::Fortuna> f{std::make_unique<fortuna::Fortuna>()};
|
||||
|
||||
auto sigint_handler(int sig_n) -> void {
|
||||
assert(sig_n == 2);
|
||||
std::atomic<unsigned int> madness{100000};
|
||||
try {
|
||||
std::atomic<unsigned int> i{0};
|
||||
constexpr auto madmsg{"HAHA no not ending on SIGINT, send a SIGTERM"};
|
||||
while (madness-- > 0) {
|
||||
fmt::print("{:<{}}\n", madmsg, 16);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
fmt::print("[!] bailing, something (bad) happened: {}\n", e.what());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
auto sigterm_handler(int sig_n) -> void {
|
||||
assert(sig_n == 15);
|
||||
try {
|
||||
constexpr auto news{" BREAKING NEWS "};
|
||||
fmt::print("\n\n{:*^{}}\n\n", news, 79);
|
||||
fmt::print("[i] main: received signal {} (SIGTERM)\n\tpid: {}\n\twill "
|
||||
"attempt to stop\n",
|
||||
sig_n,
|
||||
getpid());
|
||||
if (f) {
|
||||
f->stop_running();
|
||||
}
|
||||
else {
|
||||
throw("fortuna pointer is invalid");
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
fmt::print("[!] bailing, something (bad) happened: {}\n", e.what());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
} // namespace chance
|
||||
|
||||
|
||||
int main() {
|
||||
fmt::print("[*] doing evil stuff\n");
|
||||
fortuna::Fortuna f;
|
||||
|
||||
signal(SIGTERM, chance::sigterm_handler);
|
||||
signal(SIGINT, chance::sigint_handler);
|
||||
signal(SIGUSR1, SIG_IGN);
|
||||
signal(SIGUSR2, SIG_IGN);
|
||||
|
||||
try {
|
||||
f.random_data(4); // number of bytes requested
|
||||
using std::chrono_literals::operator""ms;
|
||||
std::this_thread::sleep_for(1000ms);
|
||||
f.random_data(4);
|
||||
chance::f->random_data(4); // number of bytes requested
|
||||
std::this_thread::sleep_for(7777ms); // enough time for p0 == 64
|
||||
chance::f->random_data(4);
|
||||
std::this_thread::sleep_for(120ms);
|
||||
f.random_data(3);
|
||||
f.random_data(3);
|
||||
chance::f->random_data(3);
|
||||
chance::f->random_data(3);
|
||||
std::this_thread::sleep_for(10ms);
|
||||
f.random_data(3);
|
||||
} catch (std::exception& e) {
|
||||
fmt::print("[!] exiting due to \"{}\"\n", e.what());
|
||||
exit(0);
|
||||
chance::f->random_data(3);
|
||||
std::this_thread::sleep_for(5000ms);
|
||||
chance::f->random_data(30);
|
||||
std::this_thread::sleep_for(5000ms);
|
||||
chance::f->random_data(30);
|
||||
std::this_thread::sleep_for(2000ms);
|
||||
|
||||
if (chance::AUTOSTOP) {
|
||||
auto err{chance::f->stop_running()};
|
||||
if (err) {
|
||||
fmt::print("[!] ERROR: failed to stop Fortuna!\n");
|
||||
throw std::runtime_error("failed to stop Fortuna nicely");
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
fmt::print("[!] exiting due to \"{}\"\n", e.what());
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue