-
Notifications
You must be signed in to change notification settings - Fork 1
/
defcon.c
86 lines (69 loc) · 1.65 KB
/
defcon.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
A program demonstrating signal handlers and masks.
We block the signal that makes us test launch a missle
during a nuclear crisis.
*/
#include<stdlib.h>
#include<unistd.h>
#include<signal.h>
#include<string.h>
#include<sys/time.h>
static sigset_t vtalrm;
void alrm_safe_puts(char *s){
sigprocmask(SIG_BLOCK, &vtalrm, NULL);
puts(s);
sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);
}
void alrm_handler(int sig){
puts("Missile Test");
}
int main(){
struct itimerval *T;
struct sigaction act;
int i,j;
/*
Setting up the signal mask
*/
sigemptyset(&vtalrm);
sigaddset(&vtalrm, SIGVTALRM);
sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);
/*
Setting up the alarm
*/
T = (struct itimerval*) malloc(sizeof(struct itimerval));
T->it_value.tv_sec = T->it_interval.tv_sec = 0;
T->it_value.tv_usec = T->it_interval.tv_usec = 1000;
setitimer(ITIMER_VIRTUAL, T, NULL);
/*
Setting up the handler
*/
memset (&act, '\0', sizeof(act));
act.sa_handler = &alrm_handler;
if (sigaction(SIGVTALRM, &act, NULL) < 0) {
perror ("sigaction");
return 1;
}
puts("Usually, missile tests are safe.");
for(i = 0; i < 10; i++){
for(j = 0; j < 1000000;j++);
alrm_safe_puts(".");
}
/*
blocking the alarm
*/
sigprocmask(SIG_BLOCK, &vtalrm, NULL);
puts("But when there is a nuclear crisis....(DEFCON 2!).... we halt them,");
for(i = 0; i < 10; i++){
for(j = 0; j < 1000000;j++);
puts(".");
}
puts("until the crisis is past.");
/*
unblocking the alarm
*/
sigprocmask(SIG_UNBLOCK, &vtalrm, NULL);
for(i = 0; i < 10; i++){
for(j = 0; j < 1000000;j++);
alrm_safe_puts(".");
}
}