Whamcloud - gitweb
b=20331
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 /* for O_DIRECT */
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41
42 #include<stdio.h>
43 #include<errno.h>
44 #include<fcntl.h>
45 #include<sys/types.h>
46 #include<sys/stat.h>
47 #include<unistd.h>
48 #include<signal.h>
49 #include<stdlib.h>
50 #include<string.h>
51
52 #define BUFFERSIZE 4096
53
54 /* This flag controls termination of the main loop. */
55 volatile sig_atomic_t keep_going = 1;
56
57 /* The signal handler just clears the flag and re-enables itself. */
58 void catch_alarm(int sig)
59 {
60         keep_going = 0;
61         signal(sig, catch_alarm);
62 }
63
64 int main(int argc, char **argv)
65 {
66         char *file;
67         unsigned char buf[BUFFERSIZE];
68         int fd, i, rc;
69         unsigned int test_time;
70         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH;
71
72         if (argc != 3) {
73                 printf("Invalid number of arguments.\n");
74                 printf("Usage %s <file> <test_time_in_seconds>\n", argv[0]);
75                 return -1;
76         }
77
78         file = argv[1];
79         test_time = atoi(argv[2]);
80
81         /* Establish a handler for SIGALRM signals. */
82         signal(SIGALRM, catch_alarm);
83
84         /* Set an alarm to go off in a little while. */
85         alarm(test_time);
86
87         fd = open(file, O_RDWR|O_TRUNC|O_CREAT|O_SYNC|O_LARGEFILE, mode);
88         if (fd < 0) {
89                 fprintf(stderr, "Error: Cannot open file named ");
90                 perror(file);
91                 return -1;
92         }
93
94         memset(buf, 1, BUFFERSIZE);
95         while (keep_going) {
96                 for (i = 0; i < 1024; i++) {
97                         rc = write(fd, buf, BUFFERSIZE);
98                         if (rc < 0) {
99                                 fprintf(stderr, "Error: Write error ");
100                                 perror(file);
101                                 exit(-1);
102                         } else if (rc != BUFFERSIZE) {
103                                 fprintf(stderr, "Error: Ddidn't write all data \n");
104                         }
105                 }
106
107                 if (ftruncate(fd, 0) < 0) {
108                         fprintf(stderr, "Error: Truncate error ");
109                         perror(file);
110                         exit(-1);
111                 }
112         }
113
114         if (close(fd) < 0) {
115                 fprintf(stderr, "Error: Cannot close ");
116                 perror(file);
117                 return -1;
118         }
119
120         return 0;
121 }