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