-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathece391sigtest.c
81 lines (69 loc) · 2.17 KB
/
ece391sigtest.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
#include <stdint.h>
#include "ece391support.h"
#include "ece391syscall.h"
#define BUFSIZE 1024
static uint8_t charbuf;
static volatile uint8_t* badbuf = 0;
void segfault_sighandler (int signum);
void alarm_sighandler (int signum);
int main ()
{
int32_t cnt;
uint8_t buf[BUFSIZE];
if (0 != ece391_getargs (buf, BUFSIZE)) {
ece391_fdputs (1, (uint8_t*)"could not read argument\n");
return 3;
}
if (buf[0] == '1') {
ece391_fdputs(1, (uint8_t*)"Installing signal handlers\n");
ece391_set_handler(SEGFAULT, segfault_sighandler);
ece391_set_handler(ALARM, alarm_sighandler);
}
ece391_fdputs (1, (uint8_t*)"Hi, what's your name? ");
if (-1 == (cnt = ece391_read (0, buf, BUFSIZE-1))) {
ece391_fdputs (1, (uint8_t*)"Can't read name from keyboard.\n");
return 3;
}
(*badbuf) = 1;
buf[cnt] = '\0';
ece391_fdputs (1, (uint8_t*)"Hello, ");
ece391_fdputs (1, buf);
if (charbuf == 1) {
ece391_fdputs(1, (uint8_t*)"success\n");
} else {
ece391_fdputs(1, (uint8_t*)"failure\n");
}
return 0;
}
void
segfault_sighandler (int signum)
{
char buf;
uint32_t* eax;
ece391_fdputs(1, (uint8_t*)"Segfault signal handler called, signum: ");
switch (signum) {
case 0: ece391_fdputs(1, (uint8_t*)"0\n"); break;
case 1: ece391_fdputs(1, (uint8_t*)"1\n"); break;
case 2: ece391_fdputs(1, (uint8_t*)"2\n"); break;
case 3: ece391_fdputs(1, (uint8_t*)"3\n"); break;
default: ece391_fdputs(1, (uint8_t*)"invalid\n"); break;
}
ece391_fdputs(1, (uint8_t*)"Press enter to continue...\n");
ece391_read(0, &buf, 1);
badbuf = &charbuf;
eax = (uint32_t*)(&signum + 7);
*eax = (uint32_t)&charbuf;
ece391_fdputs(1, (uint8_t*)"Signal handler returning\n");
}
void
alarm_sighandler (int signum)
{
ece391_fdputs(1, (uint8_t*)"Alarm signal handler called, signum: ");
switch (signum) {
case 0: ece391_fdputs(1, (uint8_t*)"0\n"); break;
case 1: ece391_fdputs(1, (uint8_t*)"1\n"); break;
case 2: ece391_fdputs(1, (uint8_t*)"2\n"); break;
case 3: ece391_fdputs(1, (uint8_t*)"3\n"); break;
default: ece391_fdputs(1, (uint8_t*)"invalid\n"); break;
}
}