Whamcloud - gitweb
4fbae4771ce62bbcc47182a36be353924cb96975
[fs/lustre-release.git] / lustre / tests / openclose.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <sys/wait.h>
10
11 #ifndef O_DIRECT
12 #define O_DIRECT         040000 /* direct disk access hint */
13 #endif
14
15 int main(int argc, char *argv[])
16 {
17         char filename[1024];
18         unsigned long count, i;
19         int thread = 0;
20         int threads = 0;
21         int rc;
22         int fd;
23
24         if (argc < 3 || argc > 4) {
25                 fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
26                         argv[0]);
27                 exit(1);
28         }
29
30         count = strtoul(argv[2], NULL, 0);
31         if (argc == 4)
32                 threads = strtoul(argv[3], NULL, 0);
33
34         for (i = 1; i <= threads; i++) {
35                 rc = fork();
36                 if (rc < 0) {
37                         fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
38                                 strerror(rc = errno));
39                         break;
40                 } else if (rc == 0) {
41                         thread = i;
42                         argv[2] = "--device";
43                         break;
44                 } else
45                         printf("%s: thread #%ld (PID %d) started\n",
46                                argv[0], i, rc);
47                 rc = 0;
48         }
49
50         if (threads && thread == 0) {   /* parent process */
51                 int live_threads = threads;
52
53                 while (live_threads > 0) {
54                         int status;
55                         pid_t ret;
56
57                         ret = waitpid(0, &status, 0);
58                         if (ret == 0) {
59                                 continue;
60                         }
61
62                         if (ret < 0) {
63                                 fprintf(stderr, "error: %s: wait - %s\n",
64                                         argv[0], strerror(errno));
65                                 if (!rc)
66                                         rc = errno;
67                         } else {
68                                 /*
69                                  * This is a hack.  We _should_ be able to use
70                                  * WIFEXITED(status) to see if there was an
71                                  * error, but it appears to be broken and it
72                                  * always returns 1 (OK).  See wait(2).
73                                  */
74                                 int err = WEXITSTATUS(status);
75                                 if (err || WIFSIGNALED(status))
76                                         fprintf(stderr,
77                                                 "%s: PID %d had rc=%d\n",
78                                                 argv[0], ret, err);
79                                 if (!rc)
80                                         rc = err;
81
82                                 live_threads--;
83                         }
84                 }
85         } else {
86                 if (threads)
87                         sprintf(filename, "%s-%d", argv[1], thread);
88                 else
89                         strcpy(filename, argv[1]);
90
91                 fd = open(filename, O_RDWR|O_CREAT, 0644);
92                 if (fd < 0) {
93                         fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
94                                 strerror(errno));
95                         exit(errno);
96                 }
97                 if (close(fd) < 0) {
98                         fprintf(stderr, "close(): %s\n", strerror(errno));
99                         rc = errno;
100                         goto unlink;
101                 }
102
103                 for (i = 0; i < count; i++) {
104                         fd = open(filename, O_RDWR|O_LARGEFILE|O_DIRECT);
105                         if (fd < 0) {
106                                 fprintf(stderr, "open(%s, O_RDWR): %s\n",
107                                         filename, strerror(errno));
108                                 rc = errno;
109                                 break;
110                         }
111                         if (close(fd) < 0) {
112                                 fprintf(stderr, "close(): %s\n",
113                                         strerror(errno));
114                                 rc = errno;
115                                 break;
116                         }
117                 }
118         unlink:
119                 if (unlink(filename) < 0) {
120                         fprintf(stderr, "unlink(%s): %s\n", filename,
121                                 strerror(errno));
122                         rc = errno;
123                 }
124                 if (threads)
125                         printf("Thread %d done: rc = %d\n", thread, rc);
126                 else
127                         printf("Done: rc = %d\n", rc);
128         }
129         return rc;
130 }