]> git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
Add mbr pseudo metadata handler.
[thirdparty/mdadm.git] / config.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "dlink.h"
27 #include <dirent.h>
28 #include <glob.h>
29 #include <fnmatch.h>
30 #include <ctype.h>
31 #include <pwd.h>
32 #include <grp.h>
33
34 /*
35 * Read the config file
36 *
37 * conf_get_uuids gets a list of devicename+uuid pairs
38 * conf_get_devs gets device names after expanding wildcards
39 *
40 * Each keeps the returned list and frees it when asked to make
41 * a new list.
42 *
43 * The format of the config file needs to be fairly extensible.
44 * Now, arrays only have names and uuids and devices merely are.
45 * But later arrays might want names, and devices might want superblock
46 * versions, and who knows what else.
47 * I like free format, abhore backslash line continuation, adore
48 * indentation for structure and am ok about # comments.
49 *
50 * So, each line that isn't blank or a #comment must either start
51 * with a key word, and not be indented, or must start with a
52 * non-key-word and must be indented.
53 *
54 * Keywords are DEVICE and ARRAY ... and several others.
55 * DEV{ICE} introduces some devices that might contain raid components.
56 * e.g.
57 * DEV style=0 /dev/sda* /dev/hd*
58 * DEV style=1 /dev/sd[b-f]*
59 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
60 * e.g.
61 * ARRAY /dev/md0 uuid=whatever name=something
62 * Spaces separate words on each line. Quoting, with "" or '' protects them,
63 * but may not wrap over lines
64 *
65 */
66
67 #ifndef CONFFILE
68 #define CONFFILE "/etc/mdadm.conf"
69 #endif
70 #ifndef CONFFILE2
71 /* for Debian compatibility .... */
72 #define CONFFILE2 "/etc/mdadm/mdadm.conf"
73 #endif
74 char DefaultConfFile[] = CONFFILE;
75 char DefaultAltConfFile[] = CONFFILE2;
76
77 enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev,
78 Homehost, AutoMode, Policy, PartPolicy, LTEnd };
79 char *keywords[] = {
80 [Devices] = "devices",
81 [Array] = "array",
82 [Mailaddr] = "mailaddr",
83 [Mailfrom] = "mailfrom",
84 [Program] = "program",
85 [CreateDev]= "create",
86 [Homehost] = "homehost",
87 [AutoMode] = "auto",
88 [Policy] = "policy",
89 [PartPolicy]="part-policy",
90 [LTEnd] = NULL
91 };
92
93 /*
94 * match_keyword returns an index into the keywords array, or -1 for no match
95 * case is ignored, and at least three characters must be given
96 */
97
98 int match_keyword(char *word)
99 {
100 int len = strlen(word);
101 int n;
102
103 if (len < 3) return -1;
104 for (n=0; keywords[n]; n++) {
105 if (strncasecmp(word, keywords[n], len)==0)
106 return n;
107 }
108 return -1;
109 }
110
111 /* conf_word gets one word from the conf file.
112 * if "allow_key", then accept words at the start of a line,
113 * otherwise stop when such a word is found.
114 * We assume that the file pointer is at the end of a word, so the
115 * next character is a space, or a newline. If not, it is the start of a line.
116 */
117
118 char *conf_word(FILE *file, int allow_key)
119 {
120 int wsize = 100;
121 int len = 0;
122 int c;
123 int quote;
124 int wordfound = 0;
125 char *word = malloc(wsize);
126
127 if (!word) abort();
128
129 while (wordfound==0) {
130 /* at the end of a word.. */
131 c = getc(file);
132 if (c == '#')
133 while (c != EOF && c != '\n')
134 c = getc(file);
135 if (c == EOF) break;
136 if (c == '\n') continue;
137
138 if (c != ' ' && c != '\t' && ! allow_key) {
139 ungetc(c, file);
140 break;
141 }
142 /* looks like it is safe to get a word here, if there is one */
143 quote = 0;
144 /* first, skip any spaces */
145 while (c == ' ' || c == '\t')
146 c = getc(file);
147 if (c != EOF && c != '\n' && c != '#') {
148 /* we really have a character of a word, so start saving it */
149 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
150 wordfound = 1;
151 if (quote && c == quote) quote = 0;
152 else if (quote == 0 && (c == '\'' || c == '"'))
153 quote = c;
154 else {
155 if (len == wsize-1) {
156 wsize += 100;
157 word = realloc(word, wsize);
158 if (!word) abort();
159 }
160 word[len++] = c;
161 }
162 c = getc(file);
163 /* Hack for broken kernels (2.6.14-.24) that put
164 * "active(auto-read-only)"
165 * in /proc/mdstat instead of
166 * "active (auto-read-only)"
167 */
168 if (c == '(' && len >= 6
169 && strncmp(word+len-6, "active", 6) == 0)
170 c = ' ';
171 }
172 }
173 if (c != EOF) ungetc(c, file);
174 }
175 word[len] = 0;
176
177 /* Further HACK for broken kernels.. 2.6.14-2.6.24 */
178 if (strcmp(word, "auto-read-only)") == 0)
179 strcpy(word, "(auto-read-only)");
180
181 /* printf("word is <%s>\n", word); */
182 if (!wordfound) {
183 free(word);
184 word = NULL;
185 }
186 return word;
187 }
188
189 /*
190 * conf_line reads one logical line from the conffile.
191 * It skips comments and continues until it finds a line that starts
192 * with a non blank/comment. This character is pushed back for the next call
193 * A doubly linked list of words is returned.
194 * the first word will be a keyword. Other words will have had quotes removed.
195 */
196
197 char *conf_line(FILE *file)
198 {
199 char *w;
200 char *list;
201
202 w = conf_word(file, 1);
203 if (w == NULL) return NULL;
204
205 list = dl_strdup(w);
206 free(w);
207 dl_init(list);
208
209 while ((w = conf_word(file,0))){
210 char *w2 = dl_strdup(w);
211 free(w);
212 dl_add(list, w2);
213 }
214 /* printf("got a line\n");*/
215 return list;
216 }
217
218 void free_line(char *line)
219 {
220 char *w;
221 for (w=dl_next(line); w != line; w=dl_next(line)) {
222 dl_del(w);
223 dl_free(w);
224 }
225 dl_free(line);
226 }
227
228
229 struct conf_dev {
230 struct conf_dev *next;
231 char *name;
232 } *cdevlist = NULL;
233
234 mddev_dev_t load_partitions(void)
235 {
236 FILE *f = fopen("/proc/partitions", "r");
237 char buf[1024];
238 mddev_dev_t rv = NULL;
239 if (f == NULL) {
240 fprintf(stderr, Name ": cannot open /proc/partitions\n");
241 return NULL;
242 }
243 while (fgets(buf, 1024, f)) {
244 int major, minor;
245 char *name, *mp;
246 mddev_dev_t d;
247
248 buf[1023] = '\0';
249 if (buf[0] != ' ')
250 continue;
251 major = strtoul(buf, &mp, 10);
252 if (mp == buf || *mp != ' ')
253 continue;
254 minor = strtoul(mp, NULL, 10);
255
256 name = map_dev(major, minor, 1);
257 if (!name)
258 continue;
259 d = malloc(sizeof(*d));
260 d->devname = strdup(name);
261 d->next = rv;
262 d->used = 0;
263 d->content = NULL;
264 rv = d;
265 }
266 fclose(f);
267 return rv;
268 }
269
270 mddev_dev_t load_containers(void)
271 {
272 struct mdstat_ent *mdstat = mdstat_read(1, 0);
273 struct mdstat_ent *ent;
274 mddev_dev_t d;
275 mddev_dev_t rv = NULL;
276
277 if (!mdstat)
278 return NULL;
279
280 for (ent = mdstat; ent; ent = ent->next)
281 if (ent->metadata_version &&
282 strncmp(ent->metadata_version, "external:", 9) == 0 &&
283 !is_subarray(&ent->metadata_version[9])) {
284 d = malloc(sizeof(*d));
285 if (!d)
286 continue;
287 if (asprintf(&d->devname, "/dev/%s", ent->dev) < 0) {
288 free(d);
289 continue;
290 }
291 d->next = rv;
292 d->used = 0;
293 d->content = NULL;
294 rv = d;
295 }
296 free_mdstat(mdstat);
297
298 return rv;
299 }
300
301 struct createinfo createinfo = {
302 .autof = 2, /* by default, create devices with standard names */
303 .symlinks = 1,
304 #ifdef DEBIAN
305 .gid = 6, /* disk */
306 .mode = 0660,
307 #else
308 .mode = 0600,
309 #endif
310 };
311
312 int parse_auto(char *str, char *msg, int config)
313 {
314 int autof;
315 if (str == NULL || *str == 0)
316 autof = 2;
317 else if (strcasecmp(str,"no")==0)
318 autof = 1;
319 else if (strcasecmp(str,"yes")==0)
320 autof = 2;
321 else if (strcasecmp(str,"md")==0)
322 autof = config?5:3;
323 else {
324 /* There might be digits, and maybe a hypen, at the end */
325 char *e = str + strlen(str);
326 int num = 4;
327 int len;
328 while (e > str && isdigit(e[-1]))
329 e--;
330 if (*e) {
331 num = atoi(e);
332 if (num <= 0) num = 1;
333 }
334 if (e > str && e[-1] == '-')
335 e--;
336 len = e - str;
337 if ((len == 2 && strncasecmp(str,"md",2)==0)) {
338 autof = config ? 5 : 3;
339 } else if ((len == 3 && strncasecmp(str,"yes",3)==0)) {
340 autof = 2;
341 } else if ((len == 3 && strncasecmp(str,"mdp",3)==0)) {
342 autof = config ? 6 : 4;
343 } else if ((len == 1 && strncasecmp(str,"p",1)==0) ||
344 (len >= 4 && strncasecmp(str,"part",4)==0)) {
345 autof = 6;
346 } else {
347 fprintf(stderr, Name ": %s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
348 " optionally followed by a number.\n",
349 msg, str);
350 exit(2);
351 }
352 autof |= num << 3;
353 }
354 return autof;
355 }
356
357 static void createline(char *line)
358 {
359 char *w;
360 char *ep;
361
362 for (w=dl_next(line); w!=line; w=dl_next(w)) {
363 if (strncasecmp(w, "auto=", 5) == 0)
364 createinfo.autof = parse_auto(w+5, "auto=", 1);
365 else if (strncasecmp(w, "owner=", 6) == 0) {
366 if (w[6] == 0) {
367 fprintf(stderr, Name ": missing owner name\n");
368 continue;
369 }
370 createinfo.uid = strtoul(w+6, &ep, 10);
371 if (*ep != 0) {
372 struct passwd *pw;
373 /* must be a name */
374 pw = getpwnam(w+6);
375 if (pw)
376 createinfo.uid = pw->pw_uid;
377 else
378 fprintf(stderr, Name ": CREATE user %s not found\n", w+6);
379 }
380 } else if (strncasecmp(w, "group=", 6) == 0) {
381 if (w[6] == 0) {
382 fprintf(stderr, Name ": missing group name\n");
383 continue;
384 }
385 createinfo.gid = strtoul(w+6, &ep, 10);
386 if (*ep != 0) {
387 struct group *gr;
388 /* must be a name */
389 gr = getgrnam(w+6);
390 if (gr)
391 createinfo.gid = gr->gr_gid;
392 else
393 fprintf(stderr, Name ": CREATE group %s not found\n", w+6);
394 }
395 } else if (strncasecmp(w, "mode=", 5) == 0) {
396 if (w[5] == 0) {
397 fprintf(stderr, Name ": missing CREATE mode\n");
398 continue;
399 }
400 createinfo.mode = strtoul(w+5, &ep, 8);
401 if (*ep != 0) {
402 createinfo.mode = 0600;
403 fprintf(stderr, Name ": unrecognised CREATE mode %s\n",
404 w+5);
405 }
406 } else if (strncasecmp(w, "metadata=", 9) == 0) {
407 /* style of metadata to use by default */
408 int i;
409 for (i=0; superlist[i] && !createinfo.supertype; i++)
410 createinfo.supertype =
411 superlist[i]->match_metadata_desc(w+9);
412 if (!createinfo.supertype)
413 fprintf(stderr, Name ": metadata format %s unknown, ignoring\n",
414 w+9);
415 } else if (strncasecmp(w, "symlinks=yes", 12) == 0)
416 createinfo.symlinks = 1;
417 else if (strncasecmp(w, "symlinks=no", 11) == 0)
418 createinfo.symlinks = 0;
419 else {
420 fprintf(stderr, Name ": unrecognised word on CREATE line: %s\n",
421 w);
422 }
423 }
424 }
425
426 void devline(char *line)
427 {
428 char *w;
429 struct conf_dev *cd;
430
431 for (w=dl_next(line); w != line; w=dl_next(w)) {
432 if (w[0] == '/' || strcasecmp(w, "partitions") == 0 ||
433 strcasecmp(w, "containers") == 0) {
434 cd = malloc(sizeof(*cd));
435 cd->name = strdup(w);
436 cd->next = cdevlist;
437 cdevlist = cd;
438 } else {
439 fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
440 w);
441 }
442 }
443 }
444
445 mddev_ident_t mddevlist = NULL;
446 mddev_ident_t *mddevlp = &mddevlist;
447
448 static int is_number(char *w)
449 {
450 /* check if there are 1 or more digits and nothing else */
451 int digits = 0;
452 while (*w && isdigit(*w)) {
453 digits++;
454 w++;
455 }
456 return (digits && ! *w);
457 }
458
459 void arrayline(char *line)
460 {
461 char *w;
462
463 struct mddev_ident_s mis;
464 mddev_ident_t mi;
465
466 mis.uuid_set = 0;
467 mis.super_minor = UnSet;
468 mis.level = UnSet;
469 mis.raid_disks = UnSet;
470 mis.spare_disks = 0;
471 mis.devices = NULL;
472 mis.devname = NULL;
473 mis.spare_group = NULL;
474 mis.autof = 0;
475 mis.next = NULL;
476 mis.st = NULL;
477 mis.bitmap_fd = -1;
478 mis.bitmap_file = NULL;
479 mis.name[0] = 0;
480 mis.container = NULL;
481 mis.member = NULL;
482
483 for (w=dl_next(line); w!=line; w=dl_next(w)) {
484 if (w[0] == '/' || strchr(w, '=') == NULL) {
485 /* This names the device, or is '<ignore>'.
486 * The rules match those in create_mddev.
487 * 'w' must be:
488 * /dev/md/{anything}
489 * /dev/mdNN
490 * /dev/md_dNN
491 * <ignore>
492 * or anything that doesn't start '/' or '<'
493 */
494 if (strcasecmp(w, "<ignore>") == 0 ||
495 strncmp(w, "/dev/md/", 8) == 0 ||
496 (w[0] != '/' && w[0] != '<') ||
497 (strncmp(w, "/dev/md", 7) == 0 &&
498 is_number(w+7)) ||
499 (strncmp(w, "/dev/md_d", 9) == 0 &&
500 is_number(w+9))
501 ) {
502 /* This is acceptable */;
503 if (mis.devname)
504 fprintf(stderr, Name ": only give one "
505 "device per ARRAY line: %s and %s\n",
506 mis.devname, w);
507 else
508 mis.devname = w;
509 }else {
510 fprintf(stderr, Name ": %s is an invalid name for "
511 "an md device - ignored.\n", w);
512 }
513 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
514 if (mis.uuid_set)
515 fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
516 w);
517 else {
518 if (parse_uuid(w+5, mis.uuid))
519 mis.uuid_set = 1;
520 else
521 fprintf(stderr, Name ": bad uuid: %s\n", w);
522 }
523 } else if (strncasecmp(w, "super-minor=", 12)==0 ) {
524 if (mis.super_minor != UnSet)
525 fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n",
526 w);
527 else {
528 char *endptr;
529 int minor = strtol(w+12, &endptr, 10);
530
531 if (w[12]==0 || endptr[0]!=0 || minor < 0)
532 fprintf(stderr, Name ": invalid super-minor number: %s\n",
533 w);
534 else
535 mis.super_minor = minor;
536 }
537 } else if (strncasecmp(w, "name=", 5)==0) {
538 if (mis.name[0])
539 fprintf(stderr, Name ": only specify name once, %s ignored.\n",
540 w);
541 else if (strlen(w+5) > 32)
542 fprintf(stderr, Name ": name too long, ignoring %s\n", w);
543 else
544 strcpy(mis.name, w+5);
545
546 } else if (strncasecmp(w, "bitmap=", 7) == 0) {
547 if (mis.bitmap_file)
548 fprintf(stderr, Name ": only specify bitmap file once. %s ignored\n",
549 w);
550 else
551 mis.bitmap_file = strdup(w+7);
552
553 } else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
554 if (mis.devices)
555 fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n",
556 w);
557 else
558 mis.devices = strdup(w+8);
559 } else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
560 if (mis.spare_group)
561 fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n",
562 w);
563 else
564 mis.spare_group = strdup(w+12);
565 } else if (strncasecmp(w, "level=", 6) == 0 ) {
566 /* this is mainly for compatability with --brief output */
567 mis.level = map_name(pers, w+6);
568 } else if (strncasecmp(w, "disks=", 6) == 0 ) {
569 /* again, for compat */
570 mis.raid_disks = atoi(w+6);
571 } else if (strncasecmp(w, "num-devices=", 12) == 0 ) {
572 /* again, for compat */
573 mis.raid_disks = atoi(w+12);
574 } else if (strncasecmp(w, "spares=", 7) == 0 ) {
575 /* for warning if not all spares present */
576 mis.spare_disks = atoi(w+7);
577 } else if (strncasecmp(w, "metadata=", 9) == 0) {
578 /* style of metadata on the devices. */
579 int i;
580
581 for(i=0; superlist[i] && !mis.st; i++)
582 mis.st = superlist[i]->match_metadata_desc(w+9);
583
584 if (!mis.st)
585 fprintf(stderr, Name ": metadata format %s unknown, ignored.\n", w+9);
586 } else if (strncasecmp(w, "auto=", 5) == 0 ) {
587 /* whether to create device special files as needed */
588 mis.autof = parse_auto(w+5, "auto type", 0);
589 } else if (strncasecmp(w, "member=", 7) == 0) {
590 /* subarray within a container */
591 mis.member = strdup(w+7);
592 } else if (strncasecmp(w, "container=", 10) == 0) {
593 /* the container holding this subarray. Either a device name
594 * or a uuid */
595 mis.container = strdup(w+10);
596 } else {
597 fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
598 w);
599 }
600 }
601 if (mis.uuid_set == 0 && mis.devices == NULL &&
602 mis.super_minor == UnSet && mis.name[0] == 0 &&
603 (mis.container == NULL || mis.member == NULL))
604 fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname);
605 else {
606 mi = malloc(sizeof(*mi));
607 *mi = mis;
608 mi->devname = mis.devname ? strdup(mis.devname) : NULL;
609 mi->next = NULL;
610 *mddevlp = mi;
611 mddevlp = &mi->next;
612 }
613 }
614
615 static char *alert_email = NULL;
616 void mailline(char *line)
617 {
618 char *w;
619
620 for (w=dl_next(line); w != line ; w=dl_next(w)) {
621 if (alert_email == NULL)
622 alert_email = strdup(w);
623 else
624 fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n",
625 w);
626 }
627 }
628
629 static char *alert_mail_from = NULL;
630 void mailfromline(char *line)
631 {
632 char *w;
633
634 for (w=dl_next(line); w != line ; w=dl_next(w)) {
635 if (alert_mail_from == NULL)
636 alert_mail_from = strdup(w);
637 else {
638 char *t = NULL;
639
640 if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) {
641 free(alert_mail_from);
642 alert_mail_from = t;
643 }
644 }
645 }
646 }
647
648
649 static char *alert_program = NULL;
650 void programline(char *line)
651 {
652 char *w;
653
654 for (w=dl_next(line); w != line ; w=dl_next(w)) {
655 if (alert_program == NULL)
656 alert_program = strdup(w);
657 else
658 fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n",
659 w);
660 }
661 }
662
663 static char *home_host = NULL;
664 static int require_homehost = 1;
665 void homehostline(char *line)
666 {
667 char *w;
668
669 for (w=dl_next(line); w != line ; w=dl_next(w)) {
670 if (strcasecmp(w, "<ignore>")==0)
671 require_homehost = 0;
672 else if (home_host == NULL)
673 home_host = strdup(w);
674 else
675 fprintf(stderr, Name ": excess host name on HOMEHOST line: %s - ignored\n",
676 w);
677 }
678 }
679
680 static char *auto_options = NULL;
681 void autoline(char *line)
682 {
683 char *w;
684
685 if (auto_options) {
686 fprintf(stderr, Name ": AUTO line may only be give once."
687 " Subsequent lines ignored\n");
688 return;
689 }
690
691 auto_options = dl_strdup(line);
692 dl_init(auto_options);
693
694 for (w=dl_next(line); w != line ; w=dl_next(w)) {
695 char *w2 = dl_strdup(w);
696 dl_add(auto_options, w2);
697 }
698 }
699
700 int loaded = 0;
701
702 static char *conffile = NULL;
703 void set_conffile(char *file)
704 {
705 conffile = file;
706 }
707
708 void load_conffile(void)
709 {
710 FILE *f;
711 char *line;
712
713 if (loaded) return;
714 if (conffile == NULL)
715 conffile = DefaultConfFile;
716
717 if (strcmp(conffile, "none") == 0) {
718 loaded = 1;
719 return;
720 }
721 if (strcmp(conffile, "partitions")==0) {
722 char *list = dl_strdup("DEV");
723 dl_init(list);
724 dl_add(list, dl_strdup("partitions"));
725 devline(list);
726 free_line(list);
727 loaded = 1;
728 return;
729 }
730 f = fopen(conffile, "r");
731 /* Debian chose to relocate mdadm.conf into /etc/mdadm/.
732 * To allow Debian users to compile from clean source and still
733 * have a working mdadm, we read /etc/mdadm/mdadm.conf
734 * if /etc/mdadm.conf doesn't exist
735 */
736 if (f == NULL &&
737 conffile == DefaultConfFile) {
738 f = fopen(DefaultAltConfFile, "r");
739 if (f)
740 conffile = DefaultAltConfFile;
741 }
742 if (f == NULL)
743 return;
744
745 loaded = 1;
746 while ((line=conf_line(f))) {
747 switch(match_keyword(line)) {
748 case Devices:
749 devline(line);
750 break;
751 case Array:
752 arrayline(line);
753 break;
754 case Mailaddr:
755 mailline(line);
756 break;
757 case Mailfrom:
758 mailfromline(line);
759 break;
760 case Program:
761 programline(line);
762 break;
763 case CreateDev:
764 createline(line);
765 break;
766 case Homehost:
767 homehostline(line);
768 break;
769 case AutoMode:
770 autoline(line);
771 break;
772 case Policy:
773 policyline(line, rule_policy);
774 break;
775 case PartPolicy:
776 policyline(line, rule_part);
777 break;
778 default:
779 fprintf(stderr, Name ": Unknown keyword %s\n", line);
780 }
781 free_line(line);
782 }
783
784 fclose(f);
785
786 /* printf("got file\n"); */
787 }
788
789 char *conf_get_mailaddr(void)
790 {
791 load_conffile();
792 return alert_email;
793 }
794
795 char *conf_get_mailfrom(void)
796 {
797 load_conffile();
798 return alert_mail_from;
799 }
800
801 char *conf_get_program(void)
802 {
803 load_conffile();
804 return alert_program;
805 }
806
807 char *conf_get_homehost(int *require_homehostp)
808 {
809 load_conffile();
810 if (require_homehostp)
811 *require_homehostp = require_homehost;
812 return home_host;
813 }
814
815 struct createinfo *conf_get_create_info(void)
816 {
817 load_conffile();
818 return &createinfo;
819 }
820
821 mddev_ident_t conf_get_ident(char *dev)
822 {
823 mddev_ident_t rv;
824 load_conffile();
825 rv = mddevlist;
826 while (dev && rv && (rv->devname == NULL
827 || !devname_matches(dev, rv->devname)))
828 rv = rv->next;
829 return rv;
830 }
831
832 static void append_dlist(mddev_dev_t *dlp, mddev_dev_t list)
833 {
834 while (*dlp)
835 dlp = &(*dlp)->next;
836 *dlp = list;
837 }
838
839 mddev_dev_t conf_get_devs()
840 {
841 glob_t globbuf;
842 struct conf_dev *cd;
843 int flags = 0;
844 static mddev_dev_t dlist = NULL;
845 unsigned int i;
846
847 while (dlist) {
848 mddev_dev_t t = dlist;
849 dlist = dlist->next;
850 free(t->devname);
851 free(t);
852 }
853
854 load_conffile();
855
856 if (cdevlist == NULL) {
857 /* default to 'partitions' and 'containers' */
858 dlist = load_partitions();
859 append_dlist(&dlist, load_containers());
860 }
861
862 for (cd=cdevlist; cd; cd=cd->next) {
863 if (strcasecmp(cd->name, "partitions")==0)
864 append_dlist(&dlist, load_partitions());
865 else if (strcasecmp(cd->name, "containers")==0)
866 append_dlist(&dlist, load_containers());
867 else {
868 glob(cd->name, flags, NULL, &globbuf);
869 flags |= GLOB_APPEND;
870 }
871 }
872 if (flags & GLOB_APPEND) {
873 for (i=0; i<globbuf.gl_pathc; i++) {
874 mddev_dev_t t = malloc(sizeof(*t));
875 t->devname = strdup(globbuf.gl_pathv[i]);
876 t->next = dlist;
877 t->used = 0;
878 t->content = NULL;
879 dlist = t;
880 /* printf("one dev is %s\n", t->devname);*/
881 }
882 globfree(&globbuf);
883 }
884
885 return dlist;
886 }
887
888 int conf_test_dev(char *devname)
889 {
890 struct conf_dev *cd;
891 if (cdevlist == NULL)
892 /* allow anything by default */
893 return 1;
894 for (cd = cdevlist ; cd ; cd = cd->next) {
895 if (strcasecmp(cd->name, "partitions") == 0)
896 return 1;
897 if (fnmatch(cd->name, devname, FNM_PATHNAME) == 0)
898 return 1;
899 }
900 return 0;
901 }
902
903 int conf_test_metadata(const char *version, int is_homehost)
904 {
905 /* Check if the given metadata version is allowed
906 * to be auto-assembled.
907 * The default is 'yes' but the 'auto' line might over-ride that.
908 * Words in auto_options are processed in order with the first
909 * match winning.
910 * word can be:
911 * +version - that version can be assembled
912 * -version - that version cannot be auto-assembled
913 * yes or +all - any other version can be assembled
914 * no or -all - no other version can be assembled.
915 * homehost - any array associated by 'homehost' to this
916 * host can be assembled.
917 *
918 * Thus:
919 * +ddf -0.90 homehost -all
920 * will auto-assemble any ddf array, no 0.90 array, and
921 * any other array (imsm, 1.x) if and only if it is identified
922 * as belonging to this host.
923 */
924 char *w;
925 load_conffile();
926 if (!auto_options)
927 return 1;
928 for (w = dl_next(auto_options); w != auto_options; w = dl_next(w)) {
929 int rv;
930 if (strcasecmp(w, "yes") == 0)
931 return 1;
932 if (strcasecmp(w, "no") == 0)
933 return 0;
934 if (strcasecmp(w, "homehost") == 0) {
935 if (is_homehost)
936 return 1;
937 else
938 continue;
939 }
940 if (w[0] == '+')
941 rv = 1;
942 else if (w[0] == '-')
943 rv = 0;
944 else continue;
945
946 if (strcasecmp(w+1, "all") == 0)
947 return rv;
948 if (strcasecmp(w+1, version) == 0)
949 return rv;
950 /* allow '0' to match version '0.90'
951 * and 1 or 1.whatever to match version '1.x'
952 */
953 if (version[1] == '.' &&
954 strlen(w+1) == 1 &&
955 w[1] == version[0])
956 return rv;
957 if (version[1] == '.' && version[2] == 'x' &&
958 strncmp(w+1, version, 2) == 0)
959 return rv;
960 }
961 return 1;
962 }
963
964 int match_oneof(char *devices, char *devname)
965 {
966 /* check if one of the comma separated patterns in devices
967 * matches devname
968 */
969
970
971 while (devices && *devices) {
972 char patn[1024];
973 char *p = devices;
974 devices = strchr(devices, ',');
975 if (!devices)
976 devices = p + strlen(p);
977 if (devices-p < 1024) {
978 strncpy(patn, p, devices-p);
979 patn[devices-p] = 0;
980 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
981 return 1;
982 }
983 if (*devices == ',')
984 devices++;
985 }
986 return 0;
987 }
988
989 int devname_matches(char *name, char *match)
990 {
991 /* See if the given array name matches the
992 * given match from config file.
993 *
994 * First strip and /dev/md/ or /dev/, then
995 * see if there might be a numeric match of
996 * mdNN with NN
997 * then just strcmp
998 */
999 if (strncmp(name, "/dev/md/", 8) == 0)
1000 name += 8;
1001 else if (strncmp(name, "/dev/", 5) == 0)
1002 name += 5;
1003
1004 if (strncmp(match, "/dev/md/", 8) == 0)
1005 match += 8;
1006 else if (strncmp(match, "/dev/", 5) == 0)
1007 match += 5;
1008
1009
1010 if (strncmp(name, "md", 2) == 0 &&
1011 isdigit(name[2]))
1012 name += 2;
1013 if (strncmp(match, "md", 2) == 0 &&
1014 isdigit(match[2]))
1015 match += 2;
1016
1017 return (strcmp(name, match) == 0);
1018 }
1019
1020 int conf_name_is_free(char *name)
1021 {
1022 /* Check if this name is already take by an ARRAY entry in
1023 * the config file.
1024 * It can be taken either by a match on devname, name, or
1025 * even super-minor.
1026 */
1027 mddev_ident_t dev;
1028
1029 load_conffile();
1030 for (dev = mddevlist; dev; dev = dev->next) {
1031 char nbuf[100];
1032 if (dev->devname && devname_matches(name, dev->devname))
1033 return 0;
1034 if (dev->name[0] && devname_matches(name, dev->name))
1035 return 0;
1036 sprintf(nbuf, "%d", dev->super_minor);
1037 if (dev->super_minor != UnSet &&
1038 devname_matches(name, nbuf))
1039 return 0;
1040 }
1041 return 1;
1042 }
1043
1044 struct mddev_ident_s *conf_match(struct mdinfo *info, struct supertype *st)
1045 {
1046 struct mddev_ident_s *array_list, *match;
1047 int verbose = 0;
1048 char *devname = NULL;
1049 array_list = conf_get_ident(NULL);
1050 match = NULL;
1051 for (; array_list; array_list = array_list->next) {
1052 if (array_list->uuid_set &&
1053 same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
1054 == 0) {
1055 if (verbose >= 2 && array_list->devname)
1056 fprintf(stderr, Name
1057 ": UUID differs from %s.\n",
1058 array_list->devname);
1059 continue;
1060 }
1061 if (array_list->name[0] &&
1062 strcasecmp(array_list->name, info->name) != 0) {
1063 if (verbose >= 2 && array_list->devname)
1064 fprintf(stderr, Name
1065 ": Name differs from %s.\n",
1066 array_list->devname);
1067 continue;
1068 }
1069 if (array_list->devices && devname &&
1070 !match_oneof(array_list->devices, devname)) {
1071 if (verbose >= 2 && array_list->devname)
1072 fprintf(stderr, Name
1073 ": Not a listed device for %s.\n",
1074 array_list->devname);
1075 continue;
1076 }
1077 if (array_list->super_minor != UnSet &&
1078 array_list->super_minor != info->array.md_minor) {
1079 if (verbose >= 2 && array_list->devname)
1080 fprintf(stderr, Name
1081 ": Different super-minor to %s.\n",
1082 array_list->devname);
1083 continue;
1084 }
1085 if (!array_list->uuid_set &&
1086 !array_list->name[0] &&
1087 !array_list->devices &&
1088 array_list->super_minor == UnSet) {
1089 if (verbose >= 2 && array_list->devname)
1090 fprintf(stderr, Name
1091 ": %s doesn't have any identifying information.\n",
1092 array_list->devname);
1093 continue;
1094 }
1095 /* FIXME, should I check raid_disks and level too?? */
1096
1097 if (match) {
1098 if (verbose >= 0) {
1099 if (match->devname && array_list->devname)
1100 fprintf(stderr, Name
1101 ": we match both %s and %s - cannot decide which to use.\n",
1102 match->devname, array_list->devname);
1103 else
1104 fprintf(stderr, Name
1105 ": multiple lines in mdadm.conf match\n");
1106 }
1107 return NULL;
1108 }
1109 match = array_list;
1110 }
1111 return match;
1112 }