1 #include "uwsgi.h"
2 
3 /*
4 
5 	uWSGI timebomb
6 
7 	this is a simple thread waiting for a timeout and calling exit
8 	with the specified value.
9 
10 	You can use it as a last resort in async apps that could block
11 	normal uWSGI behaviours
12 
13 */
14 
15 struct time_bomb {
16 	int timeout;
17 	int exit_code;
18 };
19 
time_bomb(void * arg)20 static void *time_bomb(void *arg) {
21 
22 	// block all signals
23         sigset_t smask;
24         sigfillset(&smask);
25         pthread_sigmask(SIG_BLOCK, &smask, NULL);
26 
27 	struct time_bomb *tb = (struct time_bomb *) arg;
28 
29 	struct timeval tv;
30 	tv.tv_sec = tb->timeout;
31 	tv.tv_usec = 0;
32 
33 	select(0, NULL, NULL, NULL, &tv);
34 	uwsgi_log_verbose("*** BOOOOOOM ***\n");
35 	exit(tb->exit_code);
36 
37 }
38 
uwsgi_time_bomb(int timeout,int exit_code)39 void uwsgi_time_bomb(int timeout, int exit_code) {
40 
41 	pthread_t time_bomb_thread;
42 
43 	struct time_bomb *tb = uwsgi_malloc(sizeof(struct time_bomb));
44 	tb->timeout = timeout;
45 	tb->exit_code = exit_code;
46 
47 	if (pthread_create(&time_bomb_thread, NULL, time_bomb, (void *) tb)) {
48         	uwsgi_error("pthread_create()");
49                 uwsgi_log("unable to setup the time bomb, goodbye\n");
50 		exit(exit_code);
51 	}
52         else {
53         	uwsgi_log_verbose("Fire in the hole !!! (%d seconds to detonation)\n", timeout);
54         }
55 
56 }
57