Whamcloud - gitweb
LU-15103 tests: clean up busy cifs mount
[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  */
29
30 /* for O_DIRECTORY and O_DIRECT */
31 #ifndef _GNU_SOURCE
32 #define _GNU_SOURCE
33 #endif
34
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <linux/lustre/lustre_user.h>
44
45 typedef struct flag_mapping {
46         const char *string;
47         const int  flag;
48 } FLAG_MAPPING;
49
50 FLAG_MAPPING flag_table[] = {
51         {"O_RDONLY", O_RDONLY},
52         {"O_WRONLY", O_WRONLY},
53         {"O_RDWR", O_RDWR},
54         {"O_CREAT", O_CREAT},
55         {"O_EXCL", O_EXCL},
56         {"O_NOCTTY", O_NOCTTY},
57         {"O_TRUNC", O_TRUNC},
58         {"O_APPEND", O_APPEND},
59         {"O_NONBLOCK", O_NONBLOCK},
60         {"O_NDELAY", O_NDELAY},
61         {"O_SYNC", O_SYNC},
62 #ifdef O_DIRECT
63         {"O_DIRECT", O_DIRECT},
64 #endif
65         {"O_LARGEFILE", O_LARGEFILE},
66         {"O_DIRECTORY", O_DIRECTORY},
67         {"O_NOFOLLOW", O_NOFOLLOW},
68         {"O_LOV_DELAY_CREATE", O_LOV_DELAY_CREATE},
69         {"", -1}
70 };
71
72 void Usage_and_abort(void)
73 {
74         fprintf(stderr, "Usage: openfile -f flags [ -m mode ] filename\n");
75         fprintf(stderr,
76                 "e.g. openfile -f O_RDWR:O_CREAT -m 0755 /etc/passwd\n");
77         exit(-1);
78 }
79
80 int main(int argc, char **argv)
81 {
82         int fd;
83         int flags = 0;
84         mode_t mode = 0644;
85         char *fname = NULL;
86         int mode_set = 0;
87         int flag_set = 0;
88         int c;
89         int save_errno = 0;
90         int print_usage = 0;
91         char *cloned_flags = NULL;
92
93         if (argc == 1)
94                 Usage_and_abort();
95
96         while ((c = getopt(argc, argv, "f:m:")) != -1) {
97                 switch (c) {
98                 case 'f': {
99                         char *tmp;
100
101                         cloned_flags = strdup(optarg);
102                         if (!cloned_flags) {
103                                 fprintf(stderr, "Insufficient memory.\n");
104                                 save_errno = -1;
105                                 goto out;
106                         }
107
108                         flags = atoi(cloned_flags);
109                         if (flags > 0) {
110                                 flag_set = 1;
111 #ifdef DEBUG
112                                 printf("flags = %d\n", flags);
113 #endif
114                                 break;
115                         }
116
117                         flags = 0;
118
119                         for (tmp = strtok(cloned_flags, ":|"); tmp;
120                              tmp = strtok(NULL, ":|")) {
121                                 int i = 0;
122 #ifdef DEBUG
123                                 printf("flags = %s\n", tmp);
124 #endif
125                                 flag_set = 1;
126                                 for (i = 0; flag_table[i].flag != -1; i++) {
127                                         if (!strcmp(tmp,
128                                                     flag_table[i].string)) {
129                                                 flags |= flag_table[i].flag;
130                                                 break;
131                                         }
132                                 }
133
134                                 if (flag_table[i].flag == -1) {
135                                         fprintf(stderr, "No such flag: %s\n",
136                                                 tmp);
137                                         save_errno = -1;
138                                         goto out;
139                                 }
140                         }
141 #ifdef DEBUG
142                         printf("flags = %x\n", flags);
143 #endif
144                         break;
145                 }
146                 case 'm':
147 #ifdef DEBUG
148                         printf("mode = %s\n", optarg);
149 #endif
150                         mode = strtol(optarg, NULL, 8);
151                         mode_set = 1;
152 #ifdef DEBUG
153                         printf("mode = %o\n", mode);
154 #endif
155                         break;
156                 default:
157                         fprintf(stderr, "Bad parameters.\n");
158                         print_usage = 1;
159                         goto out;
160                 }
161         }
162
163         if (optind == argc) {
164                 fprintf(stderr, "Bad parameters.\n");
165                 print_usage = 1;
166                 goto out;
167         }
168
169         fname = argv[optind];
170
171         if (!flag_set) {
172                 fprintf(stderr, "Missing flag or file-name\n");
173                 save_errno = -1;
174                 goto out;
175         }
176
177         if (flags & O_CREAT)
178                 fd = open(fname, flags, mode);
179         else
180                 fd = open(fname, flags);
181
182         save_errno = errno;
183
184         if (fd != -1) {
185                 printf("Succeed in opening file \"%s\"(flags=%s",
186                        fname, cloned_flags);
187
188                 if (mode_set)
189                         printf(", mode=%o", mode);
190                 printf(")\n");
191                 close(fd);
192         } else {
193                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
194                         fname, cloned_flags);
195                 if (mode_set)
196                         fprintf(stderr, ", mode=%o", mode);
197                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
198         }
199 out:
200         if (cloned_flags)
201                 free(cloned_flags);
202         if (print_usage)
203                 Usage_and_abort();
204
205         return save_errno;
206 }