2021-11-10 23:55:58 +01:00
|
|
|
#include "fortuna.h"
|
2021-10-19 15:05:18 +02:00
|
|
|
|
2022-01-23 22:22:28 +01:00
|
|
|
#include <fmt/core.h>
|
|
|
|
|
2022-01-03 07:49:53 +01:00
|
|
|
#include <chrono>
|
2022-01-23 22:22:28 +01:00
|
|
|
#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 {
|
|
|
|
constexpr auto madmsg{"HAHA no not ending on SIGINT, send a SIGTERM"};
|
|
|
|
while (madness-- > 0) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "{:<{}}\n", madmsg, 16);
|
2022-01-23 22:22:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(
|
|
|
|
stderr, "[!] bailing, something (bad) happened: {}\n", e.what());
|
2022-01-23 22:22:28 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto sigterm_handler(int sig_n) -> void {
|
|
|
|
assert(sig_n == 15);
|
|
|
|
try {
|
|
|
|
constexpr auto news{" BREAKING NEWS "};
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "\n\n{:*^{}}\n\n", news, 79);
|
|
|
|
fmt::print(stderr,
|
|
|
|
"[i] main: received signal {} (SIGTERM)\n\tpid: {}\n\twill "
|
2022-01-23 22:22:28 +01:00
|
|
|
"attempt to stop\n",
|
|
|
|
sig_n,
|
|
|
|
getpid());
|
|
|
|
if (f) {
|
|
|
|
f->stop_running();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw("fortuna pointer is invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(
|
|
|
|
stderr, "[!] bailing, something (bad) happened: {}\n", e.what());
|
2022-01-23 22:22:28 +01:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
2022-02-03 03:05:09 +01:00
|
|
|
|
|
|
|
auto lil_demo() -> void {
|
|
|
|
using std::chrono_literals::operator""ms;
|
|
|
|
chance::f->random_data(4);
|
|
|
|
std::this_thread::sleep_for(120ms);
|
|
|
|
chance::f->random_data(3);
|
|
|
|
chance::f->random_data(3);
|
|
|
|
std::this_thread::sleep_for(10ms);
|
|
|
|
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);
|
|
|
|
chance::f->random_data(1000);
|
|
|
|
}
|
2022-01-23 22:22:28 +01:00
|
|
|
} // namespace chance
|
|
|
|
|
2022-01-03 07:49:53 +01:00
|
|
|
|
2021-10-19 15:05:18 +02:00
|
|
|
int main() {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[*] doing evil stuff\n");
|
2022-01-23 22:22:28 +01:00
|
|
|
|
|
|
|
signal(SIGTERM, chance::sigterm_handler);
|
|
|
|
signal(SIGINT, chance::sigint_handler);
|
|
|
|
signal(SIGUSR1, SIG_IGN);
|
|
|
|
signal(SIGUSR2, SIG_IGN);
|
|
|
|
|
2021-12-13 04:59:11 +01:00
|
|
|
try {
|
2022-01-03 07:49:53 +01:00
|
|
|
using std::chrono_literals::operator""ms;
|
2022-02-03 03:15:53 +01:00
|
|
|
// this may fail, p0 probaby has not collected enough entropy by now
|
2022-01-23 22:22:28 +01:00
|
|
|
chance::f->random_data(4); // number of bytes requested
|
2022-02-03 03:15:53 +01:00
|
|
|
|
2022-01-23 22:22:28 +01:00
|
|
|
std::this_thread::sleep_for(7777ms); // enough time for p0 == 64
|
2022-02-03 03:15:53 +01:00
|
|
|
chance::f->moar_random_data(1000000000000); // 1000 GB --> for dieharder
|
2022-01-23 22:22:28 +01:00
|
|
|
|
|
|
|
if (chance::AUTOSTOP) {
|
|
|
|
auto err{chance::f->stop_running()};
|
|
|
|
if (err) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[!] ERROR: failed to stop Fortuna!\n");
|
2022-01-23 22:22:28 +01:00
|
|
|
throw std::runtime_error("failed to stop Fortuna nicely");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (std::exception& e) {
|
2022-02-03 00:37:20 +01:00
|
|
|
fmt::print(stderr, "[!] exiting due to \"{}\"\n", e.what());
|
2022-01-23 22:22:28 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2021-12-13 04:59:11 +01:00
|
|
|
}
|
2022-01-23 22:22:28 +01:00
|
|
|
|
2021-10-19 15:05:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|