Whamcloud - gitweb
LU-10657 utils: fd leak in mirror_split()
[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 = (char *)malloc(strlen(optarg)+1);
106                         if (cloned_flags == NULL) {
107                                 fprintf(stderr, "Insufficient memory.\n");
108                                 save_errno = -1;
109                                 goto out;
110                         }
111
112                         strncpy(cloned_flags, optarg, strlen(optarg)+1);
113                         flags = atoi(cloned_flags);
114                         if (flags > 0) {
115                                 flag_set = 1;
116 #ifdef DEBUG
117                                 printf("flags = %d\n",flags);
118 #endif
119                                 break;
120                         } else 
121                                 flags = 0;
122
123                         for (tmp = strtok(cloned_flags, ":|"); tmp;
124                              tmp = strtok(NULL, ":|")) {
125                                 int i = 0;
126 #ifdef DEBUG
127                                 printf("flags = %s\n",tmp);
128 #endif
129                                 flag_set = 1;
130                                 for (i = 0; flag_table[i].flag != -1; i++) {
131                                         if (!strcmp(tmp, flag_table[i].string)){
132                                                 flags |= flag_table[i].flag;
133                                                 break;
134                                         }
135                                 }
136
137                                 if (flag_table[i].flag == -1) {
138                                         fprintf(stderr, "No such flag: %s\n",
139                                                 tmp);
140                                         save_errno = -1;
141                                         goto out;
142                                 }
143                         }
144 #ifdef DEBUG
145                         printf("flags = %x\n", flags);
146 #endif
147                         break;
148                 }
149                 case 'm':
150 #ifdef DEBUG
151                         printf("mode = %s\n", optarg);
152 #endif
153                         mode = strtol(optarg, NULL, 8);
154                         mode_set = 1;
155 #ifdef DEBUG
156                         printf("mode = %o\n", mode);
157 #endif
158                         break;
159                 default:
160                         fprintf(stderr, "Bad parameters.\n");
161                         print_usage = 1;
162                         goto out;
163                 }
164         }
165
166         if (optind == argc) {
167                 fprintf(stderr, "Bad parameters.\n");
168                 print_usage = 1;
169                 goto out;
170         }
171
172         fname = argv[optind];
173
174         if (!flag_set) {
175                 fprintf(stderr, "Missing flag or file-name\n");
176                 save_errno = -1;
177                 goto out;
178         }
179
180
181         if (flags & O_CREAT)
182                 fd = open(fname, flags, mode);
183         else
184                 fd = open(fname, flags);
185
186         save_errno = errno;
187
188         if (fd != -1) {
189                 printf("Succeed in opening file \"%s\"(flags=%s",
190                        fname, cloned_flags);
191
192                 if (mode_set)
193                         printf(", mode=%o", mode);
194                 printf(")\n");
195                 close(fd);
196         } else {
197                 fprintf(stderr, "Error in opening file \"%s\"(flags=%s",
198                         fname, cloned_flags);
199                 if (mode_set)
200                         fprintf(stderr, ", mode=%o", mode);
201                 fprintf(stderr, ") %d: %s\n", save_errno, strerror(save_errno));
202         }
203
204 out:
205         if (cloned_flags)
206                 free(cloned_flags);
207         if (print_usage)
208                 Usage_and_abort();
209
210         return save_errno;
211 }