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