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