1 /*
2  * Copyright 2014-2016, Björn Ståhl
3  * License: 3-Clause BSD, see COPYING file in arcan source repository.
4  * Reference: http://arcan-fe.com
5  */
6 
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <time.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <assert.h>
16 #include <pthread.h>
17 #include <poll.h>
18 
19 #include <sys/types.h>
20 #include <sys/select.h>
21 #include <sys/wait.h>
22 #include <sys/socket.h>
23 #include <sys/un.h>
24 #include <sys/param.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 
28 #include <signal.h>
29 #include <errno.h>
30 #include <setjmp.h>
31 
32 #include <arcan_math.h>
33 #include <arcan_general.h>
34 #include <arcan_shmif.h>
35 #include <arcan_event.h>
36 #include <arcan_video.h>
37 #include <arcan_audio.h>
38 #include <arcan_frameserver.h>
39 
40 static struct arcan_frameserver* tag;
41 static sigjmp_buf recover;
42 static size_t counter;
43 
bus_handler(int signo)44 static void bus_handler(int signo)
45 {
46 	if (!tag)
47 		abort();
48 
49 	siglongjmp(recover, 0);
50 }
51 
platform_fsrv_enter(struct arcan_frameserver * m,jmp_buf out)52 void platform_fsrv_enter(struct arcan_frameserver* m, jmp_buf out)
53 {
54 	static bool initialized;
55 	counter++;
56 
57 	if (!initialized){
58 		initialized = true;
59 		if (signal(SIGBUS, bus_handler) == SIG_ERR)
60 			arcan_warning("(posix/fsrv_guard) can't install sigbus handler.\n");
61 		}
62 
63 	if (sigsetjmp(recover, 0)){
64 		arcan_warning("(posix/fsrv_guard) DoS attempt from client.\n");
65 		platform_fsrv_dropshared(tag);
66 		tag = NULL;
67 		longjmp(out, -1);
68 	}
69 
70 	tag = m;
71 }
72 
platform_fsrv_clock()73 size_t platform_fsrv_clock()
74 {
75 	return counter;
76 }
77 
platform_fsrv_leave()78 void platform_fsrv_leave()
79 {
80 	tag = NULL;
81 }
82