Whamcloud - gitweb
Some racy problems happened when sanity-quota.sh run on buffalo.
[fs/lustre-release.git] / lustre / tests / sleeptest.c
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9
10 #define BUFSIZE (4096)
11
12 #define min(a,b) ((a) < (b) ? (a) : (b))
13
14 int main(int argc, char *argv[])
15 {
16
17         FILE *w_str;
18         int read_fd;
19         int rc, iter;
20         int line, delta, next;
21         int sleeptime = 0;
22         char *now_time;
23         const char ok_chars[] = "MonTueWedThuFriSatSun"
24                                 "JanFebMarAprMayJunJulAugSepOctNovDec"
25                                 "Line 0123456789 of file, written at:\n";
26
27         char buf_r[BUFSIZE];
28
29         char pathname[256] = "/mnt/lustre/linetest_";
30         char *host;
31
32         if (argc > 1) {
33                 strncpy(pathname, argv[1], 255);
34                 pathname[255] = '\0';
35         }
36
37         host = getenv("HOSTNAME");
38         if (host)
39                 strcat(pathname, host);
40
41         if (argc > 2)
42                 sleeptime = strtoul(argv[2], NULL, 0);
43
44         if (sleeptime == 0)
45                 sleeptime = 30;
46
47         printf("Test file used is: %s at %ds intervals\n", pathname, sleeptime);
48
49         w_str = fopen(pathname, "wb");
50         if (w_str == NULL) {
51                 perror("fopen");
52                 exit(1);
53         }
54         read_fd = open(pathname, O_RDONLY);
55         if (read_fd < 0) {
56                 perror("open");
57                 exit(1);
58         }
59
60         next = 1;
61         delta = 17;
62         iter = 1;
63         while (1) {
64                 time_t now;
65                 struct tm *t;
66                 long offset;
67
68                 now = time((time_t *)NULL);
69                 t = localtime(&now);
70                 now_time = asctime(t);
71
72                 printf("iter: %d\n", iter);
73
74                 for (line=next; line<(next+delta); line++) {
75                         rc = fprintf(w_str, "Line %8d of file, written at: %s",
76                                      line, now_time);
77                         /* \n comes from ctime() result */
78                         if (rc <= 0) {
79                                 perror("fprintf");
80                                 exit(4);
81                         }
82                         rc = fflush(w_str);
83                         if (rc != 0) {
84                                 perror("fflush");
85                                 exit(5);
86                         }
87                 }
88                 next += delta;
89
90                 /* Check for corruption */
91                 offset = ftell(w_str);
92                 rc = lseek(read_fd, offset & ~4095, SEEK_SET);
93                 if (rc != (offset & ~4095)) {
94                         perror("lseek");
95                         exit(7);
96                 }
97
98                 rc = read(read_fd, buf_r, min(100, offset & 4095));
99                 if (rc != min(100, offset & 4095)) {
100                         printf("rc: %d, off %lu buf: '%s'\n", rc,offset,buf_r);
101                         exit(8);
102                 }
103                 buf_r[rc] = 0;
104                 /* Chars from "C" days/months, and above Line */
105                 if (strspn(buf_r, ok_chars) != rc) {
106                         printf("Corruption detected at %lu on %s",
107                                offset & ~4095, now_time);
108                         exit(9);
109                 }
110
111                 sleep(sleeptime);
112                 iter++;
113         }
114
115 }