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