Whamcloud - gitweb
Fix compiler warnings.
[fs/lustre-release.git] / lustre / tests / write_time_limit.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 /* for O_DIRECT */
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9
10 #include<stdio.h>
11 #include<errno.h>
12 #include<fcntl.h>
13 #include<sys/types.h>
14 #include<sys/stat.h>
15 #include<unistd.h>
16 #include<signal.h>
17 #include<stdlib.h>
18 #include<string.h>
19
20 #define BUFFERSIZE 4096
21
22 /* This flag controls termination of the main loop. */
23 volatile sig_atomic_t keep_going = 1;
24
25 /* The signal handler just clears the flag and re-enables itself. */
26 void catch_alarm(int sig)
27 {
28         keep_going = 0;
29         signal(sig, catch_alarm);
30 }
31
32 int main(int argc, char **argv)
33 {
34         char *file;
35         unsigned char buf[BUFFERSIZE];
36         int fd, i, rc;
37         unsigned int test_time;
38         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
39
40         if (argc != 3) {
41                 printf("Invalid number of arguments.\n");
42                 printf("Usage %s <file> <test_time_in_seconds>\n", argv[0]);
43                 return -1;
44         }
45
46         file = argv[1];
47         test_time = atoi(argv[2]);
48
49         /* Establish a handler for SIGALRM signals. */
50         signal(SIGALRM, catch_alarm);
51
52         /* Set an alarm to go off in a little while. */
53         alarm(test_time);
54
55         fd = open(file, O_RDWR|O_TRUNC|O_CREAT|O_SYNC|O_LARGEFILE, mode);
56         if (fd < 0) {
57                 fprintf(stderr, "Error: Cannot open file named ");
58                 perror(file);
59                 return -1;
60         }
61
62         memset(buf, 1, BUFFERSIZE);
63         while (keep_going) {
64                 for (i = 0; i < 1024; i++) {
65                         rc = write(fd, buf, BUFFERSIZE);
66                         if (rc < 0) {
67                                 fprintf(stderr, "Error: Write error ");
68                                 perror(file);
69                                 exit(-1);
70                         } else if (rc != BUFFERSIZE) {
71                                 fprintf(stderr, "Error: Ddidn't write all data \n");
72                         }
73                 }
74
75                 if (ftruncate(fd, 0) < 0) {
76                         fprintf(stderr, "Error: Truncate error ");
77                         perror(file);
78                         exit(-1);
79                 }
80         }
81
82         if (close(fd) < 0) {
83                 fprintf(stderr, "Error: Cannot close ");
84                 perror(file);
85                 return -1;
86         }
87
88         return 0;
89 }