Whamcloud - gitweb
Some racy problems happened when sanity-quota.sh run on buffalo.
[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                                 if (!rc)
72                                         rc = errno;
73                                 fprintf(stderr, "error: %s: wait - %s\n",
74                                         argv[0], strerror(rc));
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                         rc = errno;
101                         fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
102                                 strerror(rc));
103                         exit(rc);
104                 }
105                 if (close(fd) < 0) {
106                         rc = errno;
107                         fprintf(stderr, "close(): %s\n", strerror(rc));
108                         goto unlink;
109                 }
110
111                 for (i = 0; i < count; i++) {
112                         fd = open(filename, O_RDWR|O_LARGEFILE|O_DIRECT);
113                         if (fd < 0) {
114                                 rc = errno;
115                                 fprintf(stderr, "open(%s, O_RDWR): %s\n",
116                                         filename, strerror(rc));
117                                 break;
118                         }
119                         if (ioctl(fd, LL_IOC_SETFLAGS, &ioctl_flags) < 0 &&
120                             errno != ENOTTY) {
121                                 rc = errno;
122                                 fprintf(stderr, "ioctl(): %s\n", strerror(rc));
123                                 break;
124                         }
125                         if (close(fd) < 0) {
126                                 rc = errno;
127                                 fprintf(stderr, "close(): %s\n", strerror(rc));
128                                 break;
129                         }
130                 }
131         unlink:
132                 if (unlink(filename) < 0) {
133                         rc = errno;
134                         fprintf(stderr, "unlink(%s): %s\n", filename,
135                                 strerror(rc));
136                 }
137                 if (threads)
138                         printf("Thread %d done: rc = %d\n", thread, rc);
139                 else
140                         printf("Done: rc = %d\n", rc);
141         }
142         return rc;
143 }