Whamcloud - gitweb
LU-12616 obclass: fix MDS start/stop race
[fs/lustre-release.git] / lustre / tests / openfile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #if 0
32 #define DEBUG
33 #endif
34
35 /* for O_DIRECTORY and O_DIRECT */
36 #ifndef _GNU_SOURCE
37 #define _GNU_SOURCE
38 #endif
39
40 #include <stdio.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <linux/lustre/lustre_user.h>
49
50 typedef struct flag_mapping {
51        const char *string;
52        const int  flag;
53 } FLAG_MAPPING;
54
55 FLAG_MAPPING flag_table[] = {
56        {"O_RDONLY", O_RDONLY},
57        {"O_WRONLY", O_WRONLY},
58        {"O_RDWR", O_RDWR},
59        {"O_CREAT", O_CREAT},
60        {"O_EXCL", O_EXCL},
61        {"O_NOCTTY", O_NOCTTY},
62        {"O_TRUNC", O_TRUNC},
63        {"O_APPEND", O_APPEND},
64        {"O_NONBLOCK", O_NONBLOCK},
65        {"O_NDELAY", O_NDELAY},
66        {"O_SYNC", O_SYNC},
67 #ifdef O_DIRECT
68        {"O_DIRECT", O_DIRECT},
69 #endif
70        {"O_LARGEFILE", O_LARGEFILE},
71        {"O_DIRECTORY", O_DIRECTORY},
72        {"O_NOFOLLOW", O_NOFOLLOW},
73        {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
74        {"", -1}
75 };
76
77 void Usage_and_abort(void)
78 {
79        fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename \n");
80        fprintf(stderr, "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
81        exit(-1);
82 }
83
84 int main(int argc, char** argv)
85 {
86         int    fd;
87         int    flags = 0;
88         mode_t mode = 0644;
89         char  *fname = NULL;
90         int    mode_set = 0;
91         int    flag_set = 0;
92         int    c;
93         int    save_errno = 0;
94         int    print_usage = 0;
95         char  *cloned_flags = NULL;
96
97         if (argc == 1)
98                 Usage_and_abort();
99
100         while ((c = getopt (argc, argv, "f:m:")) != -1) {
101                 switch (c) {
102                 case 'f': {
103                         char *tmp;
104
105                         cloned_flags = strdup(optarg);
106                         if (cloned_flags == NULL) {
107                                 fprintf(stderr, "Insufficient memory.\n");
108                                 save_errno = -1;
109                                 goto out;
110                         }
111
112                         flags = atoi(cloned_flags);
113                         if (flags > 0) {
114                                 flag_set = 1;
115 #ifdef DEBUG
116                                 printf("flags = %d\n",flags);
117 #endif
118                                 break;
119                         } else 
120                                 flags = 0;
121
122                         for (tmp = strtok(cloned_flags, ":|"); tmp;
123                              tmp = strtok(NULL, ":|")) {
124                                 int i = 0;
125 #ifdef DEBUG
126                                 printf("flags = %s\n",tmp);
127 #endif
128                                 flag_set = 1;
129                                 for (i = 0; flag_table[i].flag != -1; i++) {
130                                         if (!strcmp(tmp, flag_table[i].string)){
131                                                 flags |= flag_table[i].flag;
132                                                 break;
133                                         }
134                                 }
135
136                                 if (flag_table[i].flag == -1) {
137                                         fprintf(stderr, "No such flag: %s\n",
138                                                 tmp);
139                                         save_errno = -1;
140                                         goto out;
141                                 }
142                         }
143 #ifdef DEBUG
144                         printf("flags = %x\n", flags);
145 #endif
146                         break;
147                 }
148                 case 'm':
149 #ifdef DEBUG
150                         printf("mode = %s\n", optarg);
151 #endif
152                         mode = strtol(optarg, NULL, 8);
153                         mode_set = 1;
154 #ifdef DEBUG
155                         printf("mode = %o\n", mode);
156 #endif
157                         break;
158                 default:
159                         fprintf(stderr, "Bad parameters.\n");
160                         print_usage = 1;
161                         goto out;
162                 }
163         }
164
165         if (optind == argc) {
166                 fprintf(stderr, "Bad parameters.\n");
167                 print_usage = 1;
168                 goto out;
169         }
170
171         fname = argv[optind];
172
173         if (!flag_set) {
174                 fprintf(stderr, "Missing flag or file-name\n");
175                 save_errno = -1;
176                 goto out;
177         }
178
179
180         if (flags & O_CREAT)
181                 fd = open(fname, flags, mode);
182         else
183                 fd = open(fname, flags);
184
185         save_errno = errno;
186
187         if (fd != -1) {
188                 printf("Succeed in opening file \"%s\"(flags=%s",
189                        fname, cloned_flags);
190
191                 if (mode_set)
192                         printf(", mode=%o", mode);
193                 printf(")\n");
194                 close(fd);
195         } else {
196                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
197                         fname, cloned_flags);
198                 if (mode_set)
199                         fprintf(stderr, ", mode=%o", mode);
200                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
201         }
202
203 out:
204         if (cloned_flags)
205                 free(cloned_flags);
206         if (print_usage)
207                 Usage_and_abort();
208
209         return save_errno;
210 }