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