summaryrefslogtreecommitdiff
path: root/core/timebomb.c
blob: d5e63304d5c3266944dd3f6dc8d13148fe95c847 (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "uwsgi.h"

/*

	uWSGI timebomb

	this is a simple thread waiting for a timeout and calling exit
	with the specified value.

	You can use it as a last resort in async apps that could block
	normal uWSGI behaviours

*/

struct time_bomb {
	int timeout;
	int exit_code;
};

static void *time_bomb(void *arg) {

	// block all signals
        sigset_t smask;
        sigfillset(&smask);
        pthread_sigmask(SIG_BLOCK, &smask, NULL);

	struct time_bomb *tb = (struct time_bomb *) arg;

	struct timeval tv;
	tv.tv_sec = tb->timeout;
	tv.tv_usec = 0;

	select(0, NULL, NULL, NULL, &tv);
	uwsgi_log_verbose("*** BOOOOOOM ***\n");
	exit(tb->exit_code);
	
}

void uwsgi_time_bomb(int timeout, int exit_code) {

	pthread_t time_bomb_thread;

	struct time_bomb *tb = uwsgi_malloc(sizeof(struct time_bomb));
	tb->timeout = timeout;
	tb->exit_code = exit_code;
	
	if (pthread_create(&time_bomb_thread, NULL, time_bomb, (void *) tb)) {
        	uwsgi_error("pthread_create()");
                uwsgi_log("unable to setup the time bomb, goodbye\n");
		exit(exit_code);
	}
        else {
        	uwsgi_log_verbose("Fire in the hole !!! (%d seconds to detonation)\n", timeout);
        }
	
}