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