]> git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
mdadm-0.8
[thirdparty/mdadm.git] / config.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 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 "mdadm.h"
31 #include "dlink.h"
32 #include <glob.h>
33 #include <fnmatch.h>
34
35 /*
36 * Read the config file
37 *
38 * conf_get_uuids gets a list of devicename+uuid pairs
39 * conf_get_devs gets device names after expanding wildcards
40 *
41 * Each keeps the returned list and frees it when asked to make
42 * a new list.
43 *
44 * The format of the config file needs to be fairly extensible.
45 * Now, arrays only have names and uuids and devices merely are.
46 * But later arrays might want names, and devices might want superblock
47 * versions, and who knows what else.
48 * I like free format, abhore backslash line continuation, adore
49 * indentation for structure and am ok about # comments.
50 *
51 * So, each line that isn't blank or a #comment must either start
52 * with a key word, and not be indented, or must start with a
53 * non-key-word and must be indented.
54 *
55 * Keywords are DEVICE and ARRAY
56 * DEV{ICE} introduces some devices that might contain raid components.
57 * e.g.
58 * DEV style=0 /dev/sda* /dev/hd*
59 * DEV style=1 /dev/sd[b-f]*
60 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
61 * e.g.
62 * ARRAY /dev/md0 uuid=whatever name=something
63 * Spaces separate words on each line. Quoting, with "" or '' protects them,
64 * but may not wrap over lines
65 *
66 */
67
68 #ifndef CONFFILE
69 #define CONFFILE "/etc/mdadm.conf"
70 #endif
71 char DefaultConfFile[] = CONFFILE;
72
73 char *keywords[] = { "device", "array", "mailaddr", "program", NULL };
74
75 /*
76 * match_keyword returns an index into the keywords array, or -1 for no match
77 * case is ignored, and at least three characters must be given
78 */
79
80 int match_keyword(char *word)
81 {
82 int len = strlen(word);
83 int n;
84
85 if (len < 3) return -1;
86 for (n=0; keywords[n]; n++) {
87 if (strncasecmp(word, keywords[n], len)==0)
88 return n;
89 }
90 return -1;
91 }
92
93 /* conf_word gets one word from the conf file.
94 * if "allow_key", then accept words at the start of a line,
95 * otherwise stop when such a word is found.
96 * We assume that the file pointer is at the end of a word, so the
97 * next character is a space, or a newline. If not, it is the start of a line.
98 */
99
100 char *conf_word(FILE *file, int allow_key)
101 {
102 int wsize = 100;
103 int len = 0;
104 int c;
105 int quote;
106 int wordfound = 0;
107 char *word = malloc(wsize);
108
109 if (!word) abort();
110
111 while (wordfound==0) {
112 /* at the end of a word.. */
113 c = getc(file);
114 if (c == '#')
115 while (c != EOF && c != '\n')
116 c = getc(file);
117 if (c == EOF) break;
118 if (c == '\n') continue;
119
120 if (c != ' ' && c != '\t' && ! allow_key) {
121 ungetc(c, file);
122 break;
123 }
124 /* looks like it is safe to get a word here, if there is one */
125 quote = 0;
126 /* first, skip any spaces */
127 while (c == ' ' || c == '\t')
128 c = getc(file);
129 if (c != EOF && c != '\n' && c != '#') {
130 /* we really have a character of a word, so start saving it */
131 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
132 wordfound = 1;
133 if (quote && c == quote) quote = 0;
134 else if (quote == 0 && (c == '\'' || c == '"'))
135 quote = c;
136 else {
137 if (len == wsize-1) {
138 wsize += 100;
139 word = realloc(word, wsize);
140 if (!word) abort();
141 }
142 word[len++] = c;
143 }
144 c = getc(file);
145 }
146 }
147 if (c != EOF) ungetc(c, file);
148 }
149 word[len] = 0;
150 /* printf("word is <%s>\n", word); */
151 if (!wordfound) {
152 free(word);
153 word = NULL;
154 }
155 return word;
156 }
157
158 /*
159 * conf_line reads one logical line from the conffile.
160 * It skips comments and continues until it finds a line that starts
161 * with a non blank/comment. This character is pushed back for the next call
162 * A doubly linked list of words is returned.
163 * the first word will be a keyword. Other words will have had quotes removed.
164 */
165
166 char *conf_line(FILE *file)
167 {
168 char *w;
169 char *list;
170
171 w = conf_word(file, 1);
172 if (w == NULL) return NULL;
173
174 list = dl_strdup(w);
175 free(w);
176 dl_init(list);
177
178 while ((w = conf_word(file,0))){
179 char *w2 = dl_strdup(w);
180 free(w);
181 dl_add(list, w2);
182 }
183 /* printf("got a line\n");*/
184 return list;
185 }
186
187 void free_line(char *line)
188 {
189 char *w;
190 for (w=dl_next(line); w != line; w=dl_next(line)) {
191 dl_del(w);
192 dl_free(w);
193 }
194 dl_free(line);
195 }
196
197
198 struct conf_dev {
199 struct conf_dev *next;
200 char *name;
201 } *cdevlist = NULL;
202
203
204
205 void devline(char *line)
206 {
207 char *w;
208 struct conf_dev *cd;
209
210 for (w=dl_next(line); w != line; w=dl_next(w)) {
211 if (w[0] == '/') {
212 cd = malloc(sizeof(*cd));
213 cd->name = strdup(w);
214 cd->next = cdevlist;
215 cdevlist = cd;
216 } else {
217 fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
218 w);
219 }
220 }
221 }
222
223 mddev_ident_t mddevlist = NULL;
224 mddev_ident_t *mddevlp = &mddevlist;
225
226 void arrayline(char *line)
227 {
228 char *w;
229
230 struct mddev_ident_s mis;
231 mddev_ident_t mi;
232
233 mis.uuid_set = 0;
234 mis.super_minor = -1;
235 mis.level = -10;
236 mis.raid_disks = -1;
237 mis.devices = NULL;
238 mis.devname = NULL;
239 mis.spare_group = NULL;
240
241 for (w=dl_next(line); w!=line; w=dl_next(w)) {
242 if (w[0] == '/') {
243 if (mis.devname)
244 fprintf(stderr, Name ": only give one device per ARRAY line: %s and %s\n",
245 mis.devname, w);
246 else mis.devname = w;
247 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
248 if (mis.uuid_set)
249 fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
250 w);
251 else {
252 if (parse_uuid(w+5, mis.uuid))
253 mis.uuid_set = 1;
254 else
255 fprintf(stderr, Name ": bad uuid: %s\n", w);
256 }
257 } else if (strncasecmp(w, "super-minor=", 12)==0 ) {
258 if (mis.super_minor >= 0)
259 fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n",
260 w);
261 else {
262 char *endptr;
263 mis.super_minor= strtol(w+12, &endptr, 10);
264 if (w[12]==0 || endptr[0]!=0 || mis.super_minor < 0) {
265 fprintf(stderr, Name ": invalid super-minor number: %s\n",
266 w);
267 mis.super_minor = -1;
268 }
269 }
270 } else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
271 if (mis.devices)
272 fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n",
273 w);
274 else
275 mis.devices = strdup(w+8);
276 } else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
277 if (mis.spare_group)
278 fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n",
279 w);
280 else
281 mis.spare_group = strdup(w+12);
282 } else if (strncasecmp(w, "level=", 6) == 0 ) {
283 /* this is mainly for compatability with --brief output */
284 mis.level = map_name(pers, w+6);
285 } else if (strncasecmp(w, "disks=", 6) == 0 ) {
286 /* again, for compat */
287 mis.raid_disks = atoi(w+6);
288 } else {
289 fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
290 w);
291 }
292 }
293 if (mis.devname == NULL)
294 fprintf(stderr, Name ": ARRAY line with a device\n");
295 else if (mis.uuid_set == 0 && mis.devices == NULL && mis.super_minor < 0)
296 fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname);
297 else {
298 mi = malloc(sizeof(*mi));
299 *mi = mis;
300 mi->devname = strdup(mis.devname);
301 mi->next = NULL;
302 *mddevlp = mi;
303 mddevlp = &mi->next;
304 }
305 }
306
307 static char *alert_email = NULL;
308 void mailline(char *line)
309 {
310 char *w;
311
312 for (w=dl_next(line); w != line ; w=dl_next(w)) {
313 if (alert_email == NULL)
314 alert_email = strdup(w);
315 else
316 fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n",
317 w);
318 }
319 }
320
321
322 static char *alert_program = NULL;
323 void programline(char *line)
324 {
325 char *w;
326
327 for (w=dl_next(line); w != line ; w=dl_next(w)) {
328 if (alert_program == NULL)
329 alert_program = strdup(w);
330 else
331 fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n",
332 w);
333 }
334 }
335
336
337 int loaded = 0;
338
339 void load_conffile(char *conffile)
340 {
341 FILE *f;
342 char *line;
343
344 if (loaded) return;
345 if (conffile == NULL)
346 conffile = DefaultConfFile;
347
348 f = fopen(conffile, "r");
349 if (f ==NULL)
350 return;
351
352 loaded = 1;
353 while ((line=conf_line(f))) {
354 switch(match_keyword(line)) {
355 case 0: /* DEVICE */
356 devline(line);
357 break;
358 case 1: /* ARRAY */
359 arrayline(line);
360 break;
361 case 2: /* MAIL */
362 mailline(line);
363 break;
364 case 3: /* PROGRAM */
365 programline(line);
366 break;
367 default:
368 fprintf(stderr, Name ": Unknown keyword %s\n", line);
369 }
370 free_line(line);
371 }
372
373
374 /* printf("got file\n"); */
375 }
376
377 char *conf_get_mailaddr(char *conffile)
378 {
379 load_conffile(conffile);
380 return alert_email;
381 }
382
383 char *conf_get_program(char *conffile)
384 {
385 load_conffile(conffile);
386 return alert_program;
387 }
388
389
390 mddev_ident_t conf_get_ident(char *conffile, char *dev)
391 {
392 mddev_ident_t rv;
393 load_conffile(conffile);
394 rv = mddevlist;
395 while (dev && rv && strcmp(dev, rv->devname)!=0)
396 rv = rv->next;
397 return rv;
398 }
399
400 mddev_dev_t conf_get_devs(char *conffile)
401 {
402 glob_t globbuf;
403 struct conf_dev *cd;
404 int flags = 0;
405 static mddev_dev_t dlist = NULL;
406 int i;
407
408 while (dlist) {
409 mddev_dev_t t = dlist;
410 dlist = dlist->next;
411 free(t->devname);
412 free(t);
413 }
414
415 load_conffile(conffile);
416
417 for (cd=cdevlist; cd; cd=cd->next) {
418 glob(cd->name, flags, NULL, &globbuf);
419 flags |= GLOB_APPEND;
420 }
421 if (flags & GLOB_APPEND) {
422 for (i=0; i<globbuf.gl_pathc; i++) {
423 mddev_dev_t t = malloc(sizeof(*t));
424 t->devname = strdup(globbuf.gl_pathv[i]);
425 t->next = dlist;
426 dlist = t;
427 /* printf("one dev is %s\n", t->devname);*/
428 }
429 globfree(&globbuf);
430 }
431
432 return dlist;
433 }
434
435 int match_oneof(char *devices, char *devname)
436 {
437 /* check if one of the comma separated patterns in devices
438 * matches devname
439 */
440
441
442 while (devices && *devices) {
443 char patn[1024];
444 char *p = devices;
445 devices = strchr(devices, ',');
446 if (!devices)
447 devices = p + strlen(p);
448 if (devices-p < 1024) {
449 strncpy(patn, p, devices-p);
450 patn[devices-p] = 0;
451 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
452 return 1;
453 }
454 if (*devices == ',')
455 devices++;
456 }
457 return 0;
458 }