aboutsummaryrefslogtreecommitdiff
path: root/src/util/timeout.c
blob: fb3053d18d1d62afa99eafaad20af704b0c22103 (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 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;
}