Whamcloud - gitweb
- Make lustre_lite.h userspace-include-safe
[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;
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
69                         if (ret < 0) {
70                                 fprintf(stderr, "error: %s: wait - %s\n",
71                                         argv[0], strerror(errno));
72                                 if (!rc)
73                                         rc = errno;
74                         } else {
75                                 /*
76                                  * This is a hack.  We _should_ be able to use
77                                  * WIFEXITED(status) to see if there was an
78                                  * error, but it appears to be broken and it
79                                  * always returns 1 (OK).  See wait(2).
80                                  */
81                                 int err = WEXITSTATUS(status);
82                                 if (err || WIFSIGNALED(status))
83                                         fprintf(stderr,
84                                                 "%s: PID %d had rc=%d\n",
85                                                 argv[0], ret, err);
86                                 if (!rc)
87                                         rc = err;
88
89                                 live_threads--;
90                         }
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                                 fprintf(stderr, "ioctl(): %s\n",
120                                         strerror(errno));
121                                 rc = errno;
122                                 break;
123                         }
124                         if (close(fd) < 0) {
125                                 fprintf(stderr, "close(): %s\n",
126                                         strerror(errno));
127                                 rc = errno;
128                                 break;
129                         }
130                 }
131         unlink:
132                 if (unlink(filename) < 0) {
133                         fprintf(stderr, "unlink(%s): %s\n", filename,
134                                 strerror(errno));
135                         rc = errno;
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 }