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