]> git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
mdctl-v0.4.1
[thirdparty/mdadm.git] / config.c
1 /*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdctl.h"
31 #include "dlink.h"
32 #include <glob.h>
33 /*
34 * Read the config file
35 *
36 * conf_get_uuids gets a list of devicename+uuid pairs
37 * conf_get_devs gets device names after expanding wildcards
38 *
39 * Each keeps the returned list and frees it when asked to make
40 * a new list.
41 *
42 * The format of the config file needs to be fairly extensible.
43 * Now, arrays only have names and uuids and devices merely are.
44 * But later arrays might want names, and devices might want superblock
45 * versions, and who knows what else.
46 * I like free format, abhore backslash line continuation, adore
47 * indentation for structure and am ok about # comments.
48 *
49 * So, each line that isn't blank or a #comment must either start
50 * with a key word, and not be indented, or must start with a
51 * non-key-word and must be indented.
52 *
53 * Keywords are DEVICE and ARRAY
54 * DEV{ICE} introduces some devices that might contain raid components.
55 * e.g.
56 * DEV style=0 /dev/sda* /dev/hd*
57 * DEV style=1 /dev/sd[b-f]*
58 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
59 * e.g.
60 * ARRAY /dev/md0 uuid=whatever name=something
61 * Spaces separate words on each line. Quoting, with "" or '' protects them,
62 * but may not wrap over lines
63 *
64 */
65
66 char DefaultConfFile[] = "/etc/mdctl.conf";
67
68 char *keywords[] = { "device", "array", NULL };
69
70 /*
71 * match_keyword returns an index into the keywords array, or -1 for no match
72 * case is ignored, and at least three characters must be given
73 */
74
75 int match_keyword(char *word)
76 {
77 int len = strlen(word);
78 int n;
79
80 if (len < 3) return -1;
81 for (n=0; keywords[n]; n++) {
82 if (strncasecmp(word, keywords[n], len)==0)
83 return n;
84 }
85 return -1;
86 }
87
88 /* conf_word gets one word from the conf file.
89 * if "allow_key", then accept words at the start of a line,
90 * otherwise stop when such a word is found.
91 * We assume that the file pointer is at the end of a word, so the
92 * next character is a space, or a newline. If not, it is the start of a line.
93 */
94
95 char *conf_word(FILE *file, int allow_key)
96 {
97 int wsize = 100;
98 int len = 0;
99 int c;
100 int quote;
101 int wordfound = 0;
102 char *word = malloc(wsize);
103
104 if (!word) abort();
105
106 while (wordfound==0) {
107 /* at the end of a word.. */
108 c = getc(file);
109 if (c == '#')
110 while (c != EOF && c != '\n')
111 c = getc(file);
112 if (c == EOF) break;
113 if (c == '\n') continue;
114
115 if (c != ' ' && c != '\t' && ! allow_key) {
116 ungetc(c, file);
117 break;
118 }
119 /* looks like it is safe to get a word here, if there is one */
120 quote = 0;
121 /* first, skip any spaces */
122 while (c == ' ' || c == '\t')
123 c = getc(file);
124 if (c != EOF && c != '\n' && c != '#') {
125 /* we really have a character of a word, so start saving it */
126 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
127 wordfound = 1;
128 if (quote && c == quote) quote = 0;
129 else if (quote == 0 && (c == '\'' || c == '"'))
130 quote = c;
131 else {
132 if (len == wsize-1) {
133 wsize += 100;
134 word = realloc(word, wsize);
135 if (!word) abort();
136 }
137 word[len++] = c;
138 }
139 c = getc(file);
140 }
141 }
142 if (c != EOF) ungetc(c, file);
143 }
144 word[len] = 0;
145 /* printf("word is <%s>\n", word); */
146 if (!wordfound) {
147 free(word);
148 word = NULL;
149 }
150 return word;
151 }
152
153 /*
154 * conf_line reads one logical line from the conffile.
155 * It skips comments and continues until it finds a line that starts
156 * with a non blank/comment. This character is pushed back for the next call
157 * A doubly linked list of words is returned.
158 * the first word will be a keyword. Other words will have had quotes removed.
159 */
160
161 char *conf_line(FILE *file)
162 {
163 char *w;
164 char *list;
165
166 w = conf_word(file, 1);
167 if (w == NULL) return NULL;
168
169 list = dl_strdup(w);
170 free(w);
171 dl_init(list);
172
173 while ((w = conf_word(file,0))){
174 char *w2 = dl_strdup(w);
175 free(w);
176 dl_add(list, w2);
177 }
178 /* printf("got a line\n");*/
179 return list;
180 }
181
182 void free_line(char *line)
183 {
184 char *w;
185 for (w=dl_next(line); w != line; w=dl_next(line)) {
186 dl_del(w);
187 dl_free(w);
188 }
189 dl_free(line);
190 }
191
192
193 struct conf_dev {
194 struct conf_dev *next;
195 char *name;
196 } *cdevlist = NULL;
197
198
199
200 int devline(char *line)
201 {
202 char *w;
203 struct conf_dev *cd;
204
205 for (w=dl_next(line); w != line; w=dl_next(w)) {
206 if (w[0] == '/') {
207 cd = malloc(sizeof(*cd));
208 cd->name = strdup(w);
209 cd->next = cdevlist;
210 cdevlist = cd;
211 } else {
212 fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
213 w);
214 }
215 }
216 }
217
218 mddev_uuid_t uuidlist = NULL;
219
220 void arrayline(char *line)
221 {
222 char *w;
223 char *dev = NULL;
224 __u32 uuid[4];
225 int uidset=0;
226 mddev_uuid_t mu;
227
228 for (w=dl_next(line); w!=line; w=dl_next(w)) {
229 if (w[0] == '/') {
230 if (dev)
231 fprintf(stderr, Name ": only give one device per ARRAY line: %s and %s\n",
232 dev, w);
233 else dev = w;
234 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
235 if (uidset)
236 fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
237 w);
238 else {
239 if (parse_uuid(w+5, uuid))
240 uidset = 1;
241 else
242 fprintf(stderr, Name ": bad uuid: %s\n", w);
243 }
244 } else {
245 fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
246 w);
247 }
248 }
249 if (dev == NULL)
250 fprintf(stderr, Name ": ARRAY line with a device\n");
251 else if (uidset == 0)
252 fprintf(stderr, Name ": ARRAY line %s has no uuid\n", dev);
253 else {
254 mu = malloc(sizeof(*mu));
255 mu->devname = strdup(dev);
256 memcpy(mu->uuid, uuid, sizeof(uuid));
257 mu->next = uuidlist;
258 uuidlist = mu;
259 }
260 }
261
262 int loaded = 0;
263
264 void load_conffile(char *conffile)
265 {
266 FILE *f;
267 char *line;
268
269 if (loaded) return;
270 if (conffile == NULL)
271 conffile = DefaultConfFile;
272
273 f = fopen(conffile, "r");
274 if (f ==NULL)
275 return;
276
277 loaded = 1;
278 while ((line=conf_line(f))) {
279 switch(match_keyword(line)) {
280 case 0: /* DEVICE */
281 devline(line);
282 break;
283 case 1:
284 arrayline(line);
285 break;
286 default:
287 fprintf(stderr, Name ": Unknown keyword %s\n", line);
288 }
289 free_line(line);
290 }
291
292
293 /* printf("got file\n"); */
294 }
295
296
297 mddev_uuid_t conf_get_uuids(char *conffile)
298 {
299 load_conffile(conffile);
300 return uuidlist;
301 }
302
303 mddev_dev_t conf_get_devs(char *conffile)
304 {
305 glob_t globbuf;
306 struct conf_dev *cd;
307 int flags = 0;
308 static mddev_dev_t dlist = NULL;
309 int i;
310
311 while (dlist) {
312 mddev_dev_t t = dlist;
313 dlist = dlist->next;
314 free(t->devname);
315 free(t);
316 }
317
318 load_conffile(conffile);
319
320 for (cd=cdevlist; cd; cd=cd->next) {
321 glob(cd->name, flags, NULL, &globbuf);
322 flags |= GLOB_APPEND;
323 }
324
325 for (i=0; i<globbuf.gl_pathc; i++) {
326 mddev_dev_t t = malloc(sizeof(*t));
327 t->devname = strdup(globbuf.gl_pathv[i]);
328 t->next = dlist;
329 dlist = t;
330 /* printf("one dev is %s\n", t->devname);*/
331 }
332 globfree(&globbuf);
333
334
335 return dlist;
336 }
337