Whamcloud - gitweb
b=16098
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 /* for O_DIRECT */
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <string.h>
50 #include <sys/wait.h>
51 #include <sys/ioctl.h>
52
53 #include <lustre/lustre_user.h>
54 #ifndef O_DIRECT
55 #define O_DIRECT 0
56 #endif
57
58 int main(int argc, char *argv[])
59 {
60         char filename[1024];
61         unsigned long count, i;
62         int thread = 0;
63         int threads = 0;
64         int rc = 0;
65         int fd, ioctl_flags = 0;
66
67         if (argc < 3 || argc > 4) {
68                 fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
69                         argv[0]);
70                 exit(1);
71         }
72
73         count = strtoul(argv[2], NULL, 0);
74         if (argc == 4)
75                 threads = strtoul(argv[3], NULL, 0);
76
77         for (i = 1; i <= threads; i++) {
78                 rc = fork();
79                 if (rc < 0) {
80                         fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
81                                 strerror(rc = errno));
82                         break;
83                 } else if (rc == 0) {
84                         thread = i;
85                         argv[2] = "--device";
86                         break;
87                 } else
88                         printf("%s: thread #%ld (PID %d) started\n",
89                                argv[0], i, rc);
90                 rc = 0;
91         }
92
93         if (threads && thread == 0) {        /* parent process */
94                 int live_threads = threads;
95
96                 while (live_threads > 0) {
97                         int status;
98                         pid_t ret;
99
100                         ret = waitpid(0, &status, 0);
101                         if (ret == 0)
102                                 continue;
103
104                         if (ret < 0) {
105                                 if (!rc)
106                                         rc = errno;
107                                 fprintf(stderr, "error: %s: wait - %s\n",
108                                         argv[0], strerror(rc));
109                         } else {
110                                 /*
111                                  * This is a hack.  We _should_ be able to use
112                                  * WIFEXITED(status) to see if there was an
113                                  * error, but it appears to be broken and it
114                                  * always returns 1 (OK).  See wait(2).
115                                  */
116                                 int err = WEXITSTATUS(status);
117                                 if (err || WIFSIGNALED(status))
118                                         fprintf(stderr,
119                                                 "%s: PID %d had rc=%d\n",
120                                                 argv[0], ret, err);
121                                 if (!rc)
122                                         rc = err;
123                         }
124                         live_threads--;
125                 }
126         } else {
127                 if (threads)
128                         sprintf(filename, "%s-%d", argv[1], thread);
129                 else
130                         strcpy(filename, argv[1]);
131
132                 fd = open(filename, O_RDWR|O_CREAT, 0644);
133                 if (fd < 0) {
134                         rc = errno;
135                         fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
136                                 strerror(rc));
137                         exit(rc);
138                 }
139                 if (close(fd) < 0) {
140                         rc = errno;
141                         fprintf(stderr, "close(): %s\n", strerror(rc));
142                         goto unlink;
143                 }
144
145                 for (i = 0; i < count; i++) {
146                         fd = open(filename, O_RDWR|O_LARGEFILE|O_DIRECT);
147                         if (fd < 0) {
148                                 rc = errno;
149                                 fprintf(stderr, "open(%s, O_RDWR): %s\n",
150                                         filename, strerror(rc));
151                                 break;
152                         }
153                         if (ioctl(fd, LL_IOC_SETFLAGS, &ioctl_flags) < 0 &&
154                             errno != ENOTTY) {
155                                 rc = errno;
156                                 fprintf(stderr, "ioctl(): %s\n", strerror(rc));
157                                 break;
158                         }
159                         if (close(fd) < 0) {
160                                 rc = errno;
161                                 fprintf(stderr, "close(): %s\n", strerror(rc));
162                                 break;
163                         }
164                 }
165         unlink:
166                 if (unlink(filename) < 0) {
167                         rc = errno;
168                         fprintf(stderr, "unlink(%s): %s\n", filename,
169                                 strerror(rc));
170                 }
171                 if (threads)
172                         printf("Thread %d done: rc = %d\n", thread, rc);
173                 else
174                         printf("Done: rc = %d\n", rc);
175         }
176         return rc;
177 }