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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 <libcfs/libcfs.h>
54 #include <lustre/lustre_user.h>
55 #ifndef O_DIRECT
56 #define O_DIRECT 0
57 #endif
58
59 int main(int argc, char *argv[])
60 {
61         char filename[1024];
62         unsigned long count, i;
63         int thread = 0;
64         int threads = 0;
65         int rc = 0;
66         int fd, ioctl_flags = 0;
67
68         if (argc < 3 || argc > 4) {
69                 fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
70                         argv[0]);
71                 exit(1);
72         }
73
74         count = strtoul(argv[2], NULL, 0);
75         if (argc == 4)
76                 threads = strtoul(argv[3], NULL, 0);
77
78         for (i = 1; i <= threads; i++) {
79                 rc = fork();
80                 if (rc < 0) {
81                         fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
82                                 strerror(rc = errno));
83                         break;
84                 } else if (rc == 0) {
85                         thread = i;
86                         argv[2] = "--device";
87                         break;
88                 } else
89                         printf("%s: thread #%ld (PID %d) started\n",
90                                argv[0], i, rc);
91                 rc = 0;
92         }
93
94         if (threads && thread == 0) {        /* parent process */
95                 int live_threads = threads;
96
97                 while (live_threads > 0) {
98                         int status;
99                         pid_t ret;
100
101                         ret = waitpid(0, &status, 0);
102                         if (ret == 0)
103                                 continue;
104
105                         if (ret < 0) {
106                                 if (!rc)
107                                         rc = errno;
108                                 fprintf(stderr, "error: %s: wait - %s\n",
109                                         argv[0], strerror(rc));
110                         } else {
111                                 /*
112                                  * This is a hack.  We _should_ be able to use
113                                  * WIFEXITED(status) to see if there was an
114                                  * error, but it appears to be broken and it
115                                  * always returns 1 (OK).  See wait(2).
116                                  */
117                                 int err = WEXITSTATUS(status);
118                                 if (err || WIFSIGNALED(status))
119                                         fprintf(stderr,
120                                                 "%s: PID %d had rc=%d\n",
121                                                 argv[0], ret, err);
122                                 if (!rc)
123                                         rc = err;
124                         }
125                         live_threads--;
126                 }
127         } else {
128                 if (threads)
129                         sprintf(filename, "%s-%d", argv[1], thread);
130                 else
131                         strcpy(filename, argv[1]);
132
133                 fd = open(filename, O_RDWR|O_CREAT, 0644);
134                 if (fd < 0) {
135                         rc = errno;
136                         fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
137                                 strerror(rc));
138                         exit(rc);
139                 }
140                 if (close(fd) < 0) {
141                         rc = errno;
142                         fprintf(stderr, "close(): %s\n", strerror(rc));
143                         goto unlink;
144                 }
145
146                 for (i = 0; i < count; i++) {
147                         fd = open(filename, O_RDWR|O_LARGEFILE|O_DIRECT);
148                         if (fd < 0) {
149                                 rc = errno;
150                                 fprintf(stderr, "open(%s, O_RDWR): %s\n",
151                                         filename, strerror(rc));
152                                 break;
153                         }
154                         if (ioctl(fd, LL_IOC_SETFLAGS, &ioctl_flags) < 0 &&
155                             errno != ENOTTY) {
156                                 rc = errno;
157                                 fprintf(stderr, "ioctl(): %s\n", strerror(rc));
158                                 break;
159                         }
160                         if (close(fd) < 0) {
161                                 rc = errno;
162                                 fprintf(stderr, "close(): %s\n", strerror(rc));
163                                 break;
164                         }
165                 }
166         unlink:
167                 if (unlink(filename) < 0) {
168                         rc = errno;
169                         fprintf(stderr, "unlink(%s): %s\n", filename,
170                                 strerror(rc));
171                 }
172                 if (threads)
173                         printf("Thread %d done: rc = %d\n", thread, rc);
174                 else
175                         printf("Done: rc = %d\n", rc);
176         }
177         return rc;
178 }