aboutsummaryrefslogtreecommitdiff
path: root/src/util/timeout.c
blob: d273ecb9d376b2bdd5084c0bee15e38d23757ee8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * ecgen, tool for generating Elliptic curve domain parameters
 * Copyright (C) 2017-2018 J08nY
 */
#include "timeout.h"

__thread jmp_buf timeout_ptr;
__thread bool timeout_in = false;
__thread timer_t timeout_timer;

void timeout_handle(int signum, siginfo_t *siginfo, void *other) {
	sigset_t mask;
	sigemptyset(&mask);
	sigaddset(&mask, SIGALRM);
	sigprocmask(SIG_UNBLOCK, &mask, NULL);

	if (timeout_in) siglongjmp(timeout_ptr, 1);
}

bool timeout_init() {
	struct sigaction new_action;

	new_action.sa_sigaction = timeout_handle;
	sigemptyset(&new_action.sa_mask);
	new_action.sa_flags = SA_SIGINFO;

	sigaction(SIGALRM, &new_action, NULL);
	return true;
}