forked from skeeto/ptrace-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example2.c
105 lines (85 loc) · 2.21 KB
/
example2.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <pthread.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// for gettid
#include <sys/types.h>
#include <sys/syscall.h>
#include <stddef.h>
// for fprintf
#include <stdint.h>
static pid_t gettid(void)
{
return syscall(SYS_gettid);
}
static void* fileio_thread_main(void* args)
{
pid_t pid = getpid();
pid_t tid = gettid();
fprintf(stdout, "start fileio thread. (pid, tid): %jd %jd\n", (intmax_t) pid, (intmax_t) tid);
while (1) {
FILE *urandom = fopen("/dev/urandom", "r");
if (!urandom) {
printf("open(\"/dev/urandom\")[1]: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
unsigned x = 0;
fread(&x, sizeof(x), 1, urandom);
printf("fread(\"/dev/urandom\")[1] = 0x%08x\n", x);
FILE *urandom2 = fopen("/dev/urandom", "r");
if (!urandom2) {
printf("fopen(\"/dev/urandom\")[2]: %s\n", strerror(errno));
} else {
x = 0;
fread(&x, sizeof(x), 1, urandom2);
printf("fread(\"/dev/urandom\")[2] = 0x%08x\n", x);
}
x = 0;
fread(&x, sizeof(x), 1, urandom);
printf("fread(\"/dev/urandom\")[1] = 0x%08x\n",x);
if (urandom2)
fclose(urandom2);
fclose(urandom);
sleep(1);
}
}
static void* others_main(void* args)
{
while(1) {
sleep(1);
}
}
int main(void)
{
const int nr_threads = 3;
pid_t pid = getpid();
pid_t tid = gettid();
fprintf(stdout, "start fileio thread. (pid, tid): %jd %jd\n", (intmax_t) pid, (intmax_t) tid);
while (1) {
FILE *urandom = fopen("/dev/urandom", "r");
if (!urandom) {
printf("open(\"/dev/urandom\")[1]: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
unsigned x = 0;
fread(&x, sizeof(x), 1, urandom);
printf("fread(\"/dev/urandom\")[1] = 0x%08x\n", x);
FILE *urandom2 = fopen("/dev/urandom", "r");
if (!urandom2) {
printf("fopen(\"/dev/urandom\")[2]: %s\n", strerror(errno));
} else {
x = 0;
fread(&x, sizeof(x), 1, urandom2);
printf("fread(\"/dev/urandom\")[2] = 0x%08x\n", x);
}
x = 0;
fread(&x, sizeof(x), 1, urandom);
printf("fread(\"/dev/urandom\")[1] = 0x%08x\n",x);
if (urandom2)
fclose(urandom2);
fclose(urandom);
sleep(1);
}
return 0;
}