-
Notifications
You must be signed in to change notification settings - Fork 534
/
app.c
193 lines (174 loc) · 3.21 KB
/
app.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
#define _GNU_SOURCE
#define BUF_SIZE 64 * 1024 * 1024
#define BUF_COUNT 20
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <dirent.h>
#include <string.h>
#include <sys/file.h>
#include <fcntl.h>
#include <ctype.h>
char *select_disk()
{
DIR *dirptr = opendir("/dev/");
if (dirptr == NULL)
{
perror("Failed to open dir");
return NULL;
}
struct dirent *entry;
char *result = (char *)calloc(512, sizeof(char));
const char *sd_prefix = "sd";
const char *xvd_prefix = "xvd";
while (entry = readdir(dirptr))
{
if (strncmp(sd_prefix, entry->d_name, 2) == 0 || strncmp(xvd_prefix, entry->d_name, 3) == 0)
{
snprintf(result, 512 * sizeof(char), "/dev/%s", entry->d_name);
closedir(dirptr);
return result;
}
}
closedir(dirptr);
dirptr = opendir("/dev/mapper/");
if (dirptr == NULL)
{
perror("Failed to open dir");
return NULL;
}
while (entry = readdir(dirptr))
{
if (strncmp(".", entry->d_name, 1) != 0)
{
snprintf(result, 512 * sizeof(char), "/dev/mapper/%s", entry->d_name);
closedir(dirptr);
return result;
}
}
closedir(dirptr);
free(result);
return NULL;
}
long int get_value(char *str)
{
char *endptr = NULL;
long int value = strtol(str, &endptr, 10);
if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) || (errno != 0 && value == 0))
{
perror("strtol");
return -1;
}
if (endptr == str)
{
perror("not number");
return -1;
}
if (value <= 0)
{
perror("not positive number");
return -1;
}
return value;
}
void sub_process(const char *disk, size_t buffer_size, size_t count)
{
int fd = open(disk, O_RDONLY | O_DIRECT | O_LARGEFILE, 0755);
if (fd < 0)
{
perror("failed to open disk");
_exit(1);
}
unsigned char *buf;
posix_memalign((void **)&buf, 512, buffer_size);
size_t read_bytes = 0;
while (read_bytes < count * buffer_size)
{
size_t ret = read(fd, buf, buffer_size);
if (ret < 0)
{
perror("failed to read contents");
close(fd);
free(buf);
_exit(1);
}
read_bytes += ret;
}
close(fd);
free(buf);
_exit(0);
}
int main(int argc, char **argv)
{
int status = 0;
int c = 0;
char *disk = NULL;
char *size = NULL;
char *count = NULL;
while ((c = getopt(argc, argv, "d:s:c:")) != -1)
{
switch (c)
{
case 'd':
disk = optarg;
break;
case 's':
size = optarg;
break;
case 'c':
count = optarg;
break;
case '?':
printf("Illegal option: -%c\n", isprint(optopt) ? optopt : '#');
_exit(1);
default:
_exit(1);
}
}
if (disk == NULL)
{
disk = select_disk();
}
if (disk == NULL)
{
_exit(1);
}
long int buffer_size = BUF_SIZE;
long int buffer_count = BUF_COUNT;
if (size != NULL)
{
buffer_size = get_value(size);
if (buffer_size < 0)
{
exit(1);
}
}
if (count != NULL)
{
buffer_count = get_value(count);
if (buffer_count < 0)
{
exit(1);
}
}
printf("Reading data from disk %s with buffer size %ld and count %ld\n", disk, buffer_size, buffer_count);
int i = 0;
for (;;)
{
for (i = 0; i < 2; i++)
{
if (fork() == 0)
{
sub_process(disk, buffer_size, buffer_count);
}
}
sleep(5);
}
while (wait(&status) > 0);
return 0;
}