-
Notifications
You must be signed in to change notification settings - Fork 0
/
fallocate.c
277 lines (224 loc) · 4.3 KB
/
fallocate.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#define _GNU_SOURCE
#include <stdio.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#define FILE_FLAGS (O_CREAT | O_RDWR | O_TRUNC)
#define FILE_MODE (0775)
#define FILE_SIZE_1 (0x123460)
#define FILE_SIZE_2 (0x233459)
#define MMAP_PROT (PROT_READ | PROT_WRITE)
#define MMAP_FLAGS (MAP_SHARED | MAP_FILE)
#define WRITE_DATA (0xab)
#define BASE_NAME "/tmp/_g_mmap"
typedef struct outbuf {
char *name;
unsigned char *buf;
uint64_t len;
int fd;
int eintr_count;
} outbuf_t;
static int _g_mmap_(outbuf_t *out, uint64_t filesize);
static void _g_munmap_(outbuf_t *out);
static int _g_open_(outbuf_t *out, char *name);
static void _g_close_(outbuf_t *out);
static int _g_open_(outbuf_t *out, char *name)
{
int ret;
ret = open(name, FILE_FLAGS, FILE_MODE);
if (ret < 0) {
printf("Open %s failed: %d\n", name, errno);
return -1;
}
out->fd = ret;
return 0;
}
static void _g_close_(outbuf_t *out)
{
if (out->len != 0) {
_g_munmap_(out);
}
close(out->fd);
out->fd = -1;
}
static void _g_munmap_(outbuf_t *out)
{
if (out->buf != NULL) {
munmap(out->buf, out->len);
out->buf = NULL;
out->len = 0;
}
}
static int _g_mmap_(outbuf_t *out, uint64_t filesize)
{
int ret;
void *addr;
if (out->len != 0) {
_g_munmap_(out);
}
out->eintr_count = 0;
for (;;) {
#if 0
ret = syscall(__NR_fallocate, out->fd, 0,0, filesize);
#else
ret = fallocate(out->fd, 0, 0, filesize);
#endif
if (ret == 0) {
break;
}
if (errno == EINTR) {
out->eintr_count++;
continue;
}
}
if (ret != 0) {
printf("fallocate failed: %d\n", errno);
return -1;
}
ret = ftruncate(out->fd, filesize);
if (ret == -1) {
printf("ftruncate failed: %d\n", errno);
return -1;
}
addr = mmap(NULL, filesize, MMAP_PROT, MMAP_FLAGS, out->fd, 0);
if (addr == (void *)(-1)) {
printf("mmap failed: %d\n", errno);
return -1;
}
out->buf = addr;
out->len = filesize;
return 0;
}
static void _g_write_(outbuf_t *out, uint64_t len)
{
if ((out->buf == NULL) || (out->len == 0)) {
return;
}
memset((void *)(out->buf), WRITE_DATA, len);
}
static int _g_check_(outbuf_t *out, uint64_t offset)
{
unsigned int i;
unsigned char d;
int ret = 0;
if ((out->buf == NULL) || (out->len == 0)) {
return 0;
}
for (i = 0; i < 32; i++) {
d = *(out->buf + i);
if (d != WRITE_DATA) {
ret = -1;
break;
}
}
return ret;
}
int run(long long loop)
{
outbuf_t outbuf;
int ret;
char name[128];
long long s;
for (s = 0; s < loop; s++) {
srandom(time(NULL));
sprintf(name, BASE_NAME, "_%ld_%ld", random(), s);
outbuf.buf = NULL;
outbuf.len = 0;
outbuf.fd = -1;
/* printf("test: %ld, %s\n", s, name); */
ret = _g_open_(&outbuf, name);
if (ret < 0) {
return -1;
}
ret = _g_mmap_(&outbuf, FILE_SIZE_1);
if (ret < 0) {
_g_close_(&outbuf);
remove((const char *)name);
return -1;
}
_g_write_(&outbuf, FILE_SIZE_1);
ret = _g_mmap_(&outbuf, FILE_SIZE_2);
if (ret < 0) {
_g_close_(&outbuf);
remove((const char *)name);
return -1;
}
ret = _g_check_(&outbuf, 0);
if (ret < 0) {
printf("check file %s error, please hexdump it, eintr %d.\n", name, outbuf.eintr_count);
return -1;
}
_g_munmap_(&outbuf);
_g_close_(&outbuf);
remove((const char *)name);
}
return 0;
}
void send_signal()
{
pid_t ppid;
ppid = getppid();
while(1) {
usleep(100);
kill(ppid, SIGUSR1);
usleep(125);
kill(ppid, SIGUSR2);
}
}
void sig_handle(int signum)
{
static int i = 0, j = 0, k = 0;
switch(signum) {
case SIGUSR1:
i = i + 1;
break;
case SIGUSR2:
j = j + 1;
break;
default:
k = k + 1;
break;
}
i = k + 1;
j = i + 1;
k = j + 1;
}
int main(int argc, char **argv)
{
long long loop = 10;
pid_t pid;
int ret, wstatus;
if (argc == 1) {
loop = 2000;
} else if (argc == 2) {
loop = atoll((const char *)(argv[1]));
} else {
printf("invalid input args.\n");
return 0;
}
signal(SIGUSR1, sig_handle);
signal(SIGUSR2, sig_handle);
pid = fork();
if (pid == 0) {
/* child */
send_signal();
} else {
ret = run(loop);
kill(pid, SIGKILL);
waitpid(pid, &wstatus, 0);
if (ret < 0) {
return -1;
}
}
return 0;
}