aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ecgen.c4
-rw-r--r--src/util/timeout.c21
-rw-r--r--src/util/timeout.h39
3 files changed, 64 insertions, 0 deletions
diff --git a/src/ecgen.c b/src/ecgen.c
index c7c5871..822b0af 100644
--- a/src/ecgen.c
+++ b/src/ecgen.c
@@ -29,6 +29,7 @@
#include "invalid/invalid.h"
#include "io/input.h"
#include "io/output.h"
+#include "util/timeout.h"
const char *argp_program_version =
"ecgen 0.6.0\n"
@@ -50,6 +51,9 @@ bool init(void) {
// init PARI PRNG
if (!random_init()) return false;
+ // init the signal handlers, etc. for timeout handling
+ if (!timeout_init(&cfg)) return false;
+
// set datadir if specified
if (cfg.datadir) {
default0("datadir", cfg.datadir);
diff --git a/src/util/timeout.c b/src/util/timeout.c
new file mode 100644
index 0000000..51138fd
--- /dev/null
+++ b/src/util/timeout.c
@@ -0,0 +1,21 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#include "timeout.h"
+#include <signal.h>
+
+__thread jmp_buf exception;
+
+void timeout_handle(int signum) { longjmp(exception, 1); }
+
+bool timeout_init(const config_t *cfg) {
+ struct sigaction new_action;
+
+ new_action.sa_handler = timeout_handle;
+ sigemptyset(&new_action.sa_mask);
+ new_action.sa_flags = 0;
+
+ sigaction(SIGALRM, &new_action, NULL);
+ return true;
+} \ No newline at end of file
diff --git a/src/util/timeout.h b/src/util/timeout.h
new file mode 100644
index 0000000..7a1ebb2
--- /dev/null
+++ b/src/util/timeout.h
@@ -0,0 +1,39 @@
+/*
+ * ecgen, tool for generating Elliptic curve domain parameters
+ * Copyright (C) 2017 J08nY
+ */
+#ifndef ECGEN_TIMEOUT_H
+#define ECGEN_TIMEOUT_H
+
+#include <setjmp.h>
+#include <sys/syscall.h>
+#include <time.h>
+#include <unistd.h>
+#include "misc/config.h"
+
+extern __thread jmp_buf exception;
+
+#define timeout_start(seconds) \
+ do { \
+ struct sigevent sevp; \
+ sevp.sigev_notify = SIGEV_THREAD_ID; \
+ sevp.sigev_signo = SIGALRM; \
+ sevp._sigev_un._tid = (__pid_t)syscall(SYS_gettid); \
+ \
+ timer_t timer; \
+ timer_create(CLOCK_MONOTONIC, &sevp, &timer); \
+ struct itimerspec timer_time = { \
+ .it_interval = {.tv_sec = (seconds), .tv_nsec = 0}, \
+ .it_value = {.tv_sec = 0, .tv_nsec = 0}}; \
+ timer_settime(timer, 0, &timer_time, NULL); \
+ setjmp(exception); \
+ } while (0);
+
+/**
+ * @brief
+ * @param cfg
+ * @return
+ */
+bool timeout_init(const config_t *cfg);
+
+#endif // ECGEN_TIMEOUT_H