forked from sysstat/sysstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystest.c
295 lines (255 loc) · 7.69 KB
/
systest.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* sysstat test functions.
* (C) 2019 by Sebastien GODARD (sysstat <at> orange.fr)
*
***************************************************************************
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation; either version 2 of the License, or (at your *
* option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA *
***************************************************************************
*/
#ifdef TEST
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <sys/statvfs.h>
#include "systest.h"
time_t __unix_time = 0;
int __env = 0;
extern long interval;
extern int sigint_caught;
/*
***************************************************************************
* Test mode: Instead of reading system time, use time given on the command
* line.
*
* OUT:
* @t Number of seconds since the epoch, as given on the command line.
***************************************************************************
*/
void get_unix_time(time_t *t)
{
*t = __unix_time;
}
/*
***************************************************************************
* Test mode: Send bogus information about current kernel.
*
* OUT:
* @h Structure with kernel information.
***************************************************************************
*/
void get_uname(struct utsname *h)
{
strcpy(h->sysname, "Linux");
strcpy(h->nodename, "SYSSTAT.TEST");
strcpy(h->release, "1.2.3-TEST");
strcpy(h->machine, "x86_64");
}
/*
***************************************************************************
* Test mode: Send bogus information about current filesystem.
*
* OUT:
* @buf Structure with filesystem information.
***************************************************************************
*/
int get_fs_stat(char *c, struct statvfs *buf)
{
static int p = 0;
unsigned long long bfree[4] = {89739427840, 293286670336, 11696156672, 292616732672};
unsigned long long blocks[4] = {97891291136, 309502345216, 30829043712, 309502345216};
unsigned long long bavail[4] = {84722675712, 277541253120, 10106515456, 276871315456};
unsigned long long files[4] = {6111232, 19202048, 1921360, 19202048};
unsigned long long ffree[4] = {6008414, 19201593, 1621550, 19051710};
buf->f_bfree = bfree[p];
buf->f_blocks = blocks[p];
buf->f_bavail = bavail[p];
buf->f_frsize = 1;
buf->f_files = files[p];
buf->f_ffree = ffree[p];
p = (p + 1) & 0x11;
return 0;
}
/*
***************************************************************************
* Test mode: Ignore environment variable value.
***************************************************************************
*/
char *get_env_value(const char *c)
{
if (!__env)
return NULL;
fprintf(stderr, "Reading contents of %s\n", c);
return getenv(c);
}
/*
***************************************************************************
* Test mode: Go to next time period.
***************************************************************************
*/
void next_time_step(void)
{
int root_nr = 1;
char rootf[64], testf[64];
char *resolved_name;
__unix_time += interval;
if ((resolved_name = realpath(ROOTDIR, NULL)) != NULL) {
if (strlen(resolved_name) > 4) {
root_nr = atoi(resolved_name + strlen(resolved_name) - 1);
}
free(resolved_name);
}
if ((unlink(ROOTDIR) < 0) && (errno != ENOENT)) {
perror("unlink");
exit(1);
}
sprintf(rootf, "%s%d", ROOTFILE, ++root_nr);
sprintf(testf, "%s/%s", TESTDIR, rootf);
if (access(testf, F_OK) < 0) {
if (errno == ENOENT) {
/* No more kernel directories: Simulate a Ctrl/C */
int_handler(0);
return ;
}
}
if (symlink(rootf, ROOTDIR) < 0) {
perror("link");
exit(1);
}
}
/*
***************************************************************************
* If current file is considered as a virtual one ("virtualhd"), then set
* its device ID (major 253, minor 2, corresponding here to dm-2) in the
* stat structure normally filled by the stat() system call.
* Otherwise, open file and read its major and minor numbers.
*
* IN:
* @name Pathname to file.
*
* OUT:
* @statbuf Structure containing device ID.
*
* RETURNS:
* 0 if it actually was the virtual device, 1 otherwise, and -1 on failure.
***************************************************************************
*/
int virtual_stat(const char *name, struct stat *statbuf)
{
FILE *fp;
char line[128];
int major, minor;
if (!strcmp(name, VIRTUALHD)) {
statbuf->st_rdev = (253 << 8) + 2;
return 0;
}
statbuf->st_rdev = 0;
if ((fp = fopen(name, "r")) == NULL)
return -1;
if (fgets(line, sizeof(line), fp) != NULL) {
sscanf(line, "%d %d", &major, &minor);
statbuf->st_rdev = (major << 8) + minor;
}
fclose(fp);
return 1;
}
/*
***************************************************************************
* Open a "_list" file containing a list of files enumerated in a known
* order contained in current directory.
*
* IN:
* @name Pathname to directory containing the "_list" file.
*
* RETURNS:
* A pointer on current "_list" file.
***************************************************************************
*/
DIR *open_list(const char *name)
{
FILE *fp;
char filename[1024];
snprintf(filename, sizeof(filename), "%s/%s", name, _LIST);
filename[sizeof(filename) - 1] = '\0';
if ((fp = fopen(filename, "r")) == NULL)
return NULL;
return (DIR *) fp;
}
/*
***************************************************************************
* Read next file name contained in a "_list" file.
*
* IN:
* @dir Pointer on current "_list" file.
*
* RETURNS:
* A structure containing the name of the next file to read.
***************************************************************************
*/
struct dirent *read_list(DIR *dir)
{
FILE *fp = (FILE *) dir;
static struct dirent drd;
char line[1024];
if ((fgets(line, sizeof(line), fp) != NULL) && (strlen(line) > 1) &&
(strlen(line) < sizeof(drd.d_name))) {
strcpy(drd.d_name, line);
drd.d_name[strlen(line) - 1] = '\0';
return &drd;
}
return NULL;
}
/*
***************************************************************************
* Close a "_list" file.
*
* IN:
* @dir Pointer on "_list" file to close.
***************************************************************************
*/
void close_list(DIR *dir)
{
FILE *fp = (FILE *) dir;
fclose(fp);
}
/*
***************************************************************************
* Replacement function for realpath() system call. Do nothing here.
*
* IN:
* @name Pathname to process.
* @c Unused here.
*
* RETURNS:
* Pathname (unchanged).
***************************************************************************
*/
char *get_realname(char *name, char *c)
{
char *resolved_name;
if ((resolved_name = (char *) malloc(1024)) == NULL) {
perror("malloc");
exit(4);
}
strncpy(resolved_name, name, 1024);
resolved_name[1023] = '\0';
return resolved_name;
}
#endif /* TEST */