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