38e911acdd218db6dc3182f3926baabb94214d8c
[thirdparty/mdadm.git] / config.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31 #include "dlink.h"
32 #include <sys/dir.h>
33 #include <glob.h>
34 #include <fnmatch.h>
35 #include <ctype.h>
36 #include <pwd.h>
37 #include <grp.h>
38
39 /*
40 * Read the config file
41 *
42 * conf_get_uuids gets a list of devicename+uuid pairs
43 * conf_get_devs gets device names after expanding wildcards
44 *
45 * Each keeps the returned list and frees it when asked to make
46 * a new list.
47 *
48 * The format of the config file needs to be fairly extensible.
49 * Now, arrays only have names and uuids and devices merely are.
50 * But later arrays might want names, and devices might want superblock
51 * versions, and who knows what else.
52 * I like free format, abhore backslash line continuation, adore
53 * indentation for structure and am ok about # comments.
54 *
55 * So, each line that isn't blank or a #comment must either start
56 * with a key word, and not be indented, or must start with a
57 * non-key-word and must be indented.
58 *
59 * Keywords are DEVICE and ARRAY
60 * DEV{ICE} introduces some devices that might contain raid components.
61 * e.g.
62 * DEV style=0 /dev/sda* /dev/hd*
63 * DEV style=1 /dev/sd[b-f]*
64 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
65 * e.g.
66 * ARRAY /dev/md0 uuid=whatever name=something
67 * Spaces separate words on each line. Quoting, with "" or '' protects them,
68 * but may not wrap over lines
69 *
70 */
71
72 #ifndef CONFFILE
73 #define CONFFILE "/etc/mdadm.conf"
74 #endif
75 #ifndef CONFFILE2
76 /* for Debian compatibility .... */
77 #define CONFFILE2 "/etc/mdadm/mdadm.conf"
78 #endif
79 char DefaultConfFile[] = CONFFILE;
80 char DefaultAltConfFile[] = CONFFILE2;
81
82 enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev, Homehost, LTEnd };
83 char *keywords[] = {
84 [Devices] = "devices",
85 [Array] = "array",
86 [Mailaddr] = "mailaddr",
87 [Mailfrom] = "mailfrom",
88 [Program] = "program",
89 [CreateDev] = "create",
90 [Homehost] = "homehost",
91 [LTEnd] = NULL
92 };
93
94 /*
95 * match_keyword returns an index into the keywords array, or -1 for no match
96 * case is ignored, and at least three characters must be given
97 */
98
99 int match_keyword(char *word)
100 {
101 int len = strlen(word);
102 int n;
103
104 if (len < 3) return -1;
105 for (n=0; keywords[n]; n++) {
106 if (strncasecmp(word, keywords[n], len)==0)
107 return n;
108 }
109 return -1;
110 }
111
112 /* conf_word gets one word from the conf file.
113 * if "allow_key", then accept words at the start of a line,
114 * otherwise stop when such a word is found.
115 * We assume that the file pointer is at the end of a word, so the
116 * next character is a space, or a newline. If not, it is the start of a line.
117 */
118
119 char *conf_word(FILE *file, int allow_key)
120 {
121 int wsize = 100;
122 int len = 0;
123 int c;
124 int quote;
125 int wordfound = 0;
126 char *word = malloc(wsize);
127
128 if (!word) abort();
129
130 while (wordfound==0) {
131 /* at the end of a word.. */
132 c = getc(file);
133 if (c == '#')
134 while (c != EOF && c != '\n')
135 c = getc(file);
136 if (c == EOF) break;
137 if (c == '\n') continue;
138
139 if (c != ' ' && c != '\t' && ! allow_key) {
140 ungetc(c, file);
141 break;
142 }
143 /* looks like it is safe to get a word here, if there is one */
144 quote = 0;
145 /* first, skip any spaces */
146 while (c == ' ' || c == '\t')
147 c = getc(file);
148 if (c != EOF && c != '\n' && c != '#') {
149 /* we really have a character of a word, so start saving it */
150 while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
151 wordfound = 1;
152 if (quote && c == quote) quote = 0;
153 else if (quote == 0 && (c == '\'' || c == '"'))
154 quote = c;
155 else {
156 if (len == wsize-1) {
157 wsize += 100;
158 word = realloc(word, wsize);
159 if (!word) abort();
160 }
161 word[len++] = c;
162 }
163 c = getc(file);
164 }
165 }
166 if (c != EOF) ungetc(c, file);
167 }
168 word[len] = 0;
169 /* printf("word is <%s>\n", word); */
170 if (!wordfound) {
171 free(word);
172 word = NULL;
173 }
174 return word;
175 }
176
177 /*
178 * conf_line reads one logical line from the conffile.
179 * It skips comments and continues until it finds a line that starts
180 * with a non blank/comment. This character is pushed back for the next call
181 * A doubly linked list of words is returned.
182 * the first word will be a keyword. Other words will have had quotes removed.
183 */
184
185 char *conf_line(FILE *file)
186 {
187 char *w;
188 char *list;
189
190 w = conf_word(file, 1);
191 if (w == NULL) return NULL;
192
193 list = dl_strdup(w);
194 free(w);
195 dl_init(list);
196
197 while ((w = conf_word(file,0))){
198 char *w2 = dl_strdup(w);
199 free(w);
200 dl_add(list, w2);
201 }
202 /* printf("got a line\n");*/
203 return list;
204 }
205
206 void free_line(char *line)
207 {
208 char *w;
209 for (w=dl_next(line); w != line; w=dl_next(line)) {
210 dl_del(w);
211 dl_free(w);
212 }
213 dl_free(line);
214 }
215
216
217 struct conf_dev {
218 struct conf_dev *next;
219 char *name;
220 } *cdevlist = NULL;
221
222 mddev_dev_t load_partitions(void)
223 {
224 FILE *f = fopen("/proc/partitions", "r");
225 char buf[1024];
226 mddev_dev_t rv = NULL;
227 if (f == NULL) {
228 fprintf(stderr, Name ": cannot open /proc/partitions\n");
229 return NULL;
230 }
231 while (fgets(buf, 1024, f)) {
232 int major, minor;
233 char *name, *mp;
234 mddev_dev_t d;
235
236 buf[1023] = '\0';
237 if (buf[0] != ' ')
238 continue;
239 major = strtoul(buf, &mp, 10);
240 if (mp == buf || *mp != ' ')
241 continue;
242 minor = strtoul(mp, NULL, 10);
243
244 name = map_dev(major, minor, 1);
245 if (!name)
246 continue;
247 d = malloc(sizeof(*d));
248 d->devname = strdup(name);
249 d->next = rv;
250 d->used = 0;
251 rv = d;
252 }
253 fclose(f);
254 return rv;
255 }
256
257 struct createinfo createinfo = {
258 .symlinks = 1,
259 #ifdef DEBIAN
260 .gid = 6, /* disk */
261 .mode = 0660,
262 #else
263 .mode = 0600,
264 #endif
265 };
266
267 int parse_auto(char *str, char *msg, int config)
268 {
269 int autof;
270 if (str == NULL || *str == 0)
271 autof = 2;
272 else if (strcasecmp(str,"no")==0)
273 autof = 1;
274 else if (strcasecmp(str,"yes")==0)
275 autof = 2;
276 else if (strcasecmp(str,"md")==0)
277 autof = config?5:3;
278 else {
279 /* There might be digits, and maybe a hypen, at the end */
280 char *e = str + strlen(str);
281 int num = 4;
282 int len;
283 while (e > str && isdigit(e[-1]))
284 e--;
285 if (*e) {
286 num = atoi(e);
287 if (num <= 0) num = 1;
288 }
289 if (e > str && e[-1] == '-')
290 e--;
291 len = e - str;
292 if ((len == 2 && strncasecmp(str,"md",2)==0)) {
293 autof = config ? 5 : 3;
294 } else if ((len == 3 && strncasecmp(str,"yes",3)==0)) {
295 autof = 2;
296 } else if ((len == 3 && strncasecmp(str,"mdp",3)==0)) {
297 autof = config ? 6 : 4;
298 } else if ((len == 1 && strncasecmp(str,"p",1)==0) ||
299 (len >= 4 && strncasecmp(str,"part",4)==0)) {
300 autof = 6;
301 } else {
302 fprintf(stderr, Name ": %s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
303 " optionally followed by a number.\n",
304 msg, str);
305 exit(2);
306 }
307 autof |= num << 3;
308 }
309 return autof;
310 }
311
312 static void createline(char *line)
313 {
314 char *w;
315 char *ep;
316
317 for (w=dl_next(line); w!=line; w=dl_next(w)) {
318 if (strncasecmp(w, "auto=", 5) == 0)
319 createinfo.autof = parse_auto(w+5, "auto=", 1);
320 else if (strncasecmp(w, "owner=", 6) == 0) {
321 if (w[6] == 0) {
322 fprintf(stderr, Name ": missing owner name\n");
323 continue;
324 }
325 createinfo.uid = strtoul(w+6, &ep, 10);
326 if (*ep != 0) {
327 struct passwd *pw;
328 /* must be a name */
329 pw = getpwnam(w+6);
330 if (pw)
331 createinfo.uid = pw->pw_uid;
332 else
333 fprintf(stderr, Name ": CREATE user %s not found\n", w+6);
334 }
335 } else if (strncasecmp(w, "group=", 6) == 0) {
336 if (w[6] == 0) {
337 fprintf(stderr, Name ": missing group name\n");
338 continue;
339 }
340 createinfo.gid = strtoul(w+6, &ep, 10);
341 if (*ep != 0) {
342 struct group *gr;
343 /* must be a name */
344 gr = getgrnam(w+6);
345 if (gr)
346 createinfo.gid = gr->gr_gid;
347 else
348 fprintf(stderr, Name ": CREATE group %s not found\n", w+6);
349 }
350 } else if (strncasecmp(w, "mode=", 5) == 0) {
351 if (w[5] == 0) {
352 fprintf(stderr, Name ": missing CREATE mode\n");
353 continue;
354 }
355 createinfo.mode = strtoul(w+5, &ep, 8);
356 if (*ep != 0) {
357 createinfo.mode = 0600;
358 fprintf(stderr, Name ": unrecognised CREATE mode %s\n",
359 w+5);
360 }
361 } else if (strncasecmp(w, "metadata=", 9) == 0) {
362 /* style of metadata to use by default */
363 int i;
364 for (i=0; superlist[i] && !createinfo.supertype; i++)
365 createinfo.supertype =
366 superlist[i]->match_metadata_desc(w+9);
367 if (!createinfo.supertype)
368 fprintf(stderr, Name ": metadata format %s unknown, ignoring\n",
369 w+9);
370 } else if (strncasecmp(w, "symlinks=yes", 12) == 0)
371 createinfo.symlinks = 1;
372 else if (strncasecmp(w, "symlinks=no", 11) == 0)
373 createinfo.symlinks = 0;
374 else {
375 fprintf(stderr, Name ": unrecognised word on CREATE line: %s\n",
376 w);
377 }
378 }
379 }
380
381 void devline(char *line)
382 {
383 char *w;
384 struct conf_dev *cd;
385
386 for (w=dl_next(line); w != line; w=dl_next(w)) {
387 if (w[0] == '/' || strcasecmp(w, "partitions") == 0) {
388 cd = malloc(sizeof(*cd));
389 cd->name = strdup(w);
390 cd->next = cdevlist;
391 cdevlist = cd;
392 } else {
393 fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
394 w);
395 }
396 }
397 }
398
399 mddev_ident_t mddevlist = NULL;
400 mddev_ident_t *mddevlp = &mddevlist;
401
402 void arrayline(char *line)
403 {
404 char *w;
405
406 struct mddev_ident_s mis;
407 mddev_ident_t mi;
408
409 mis.uuid_set = 0;
410 mis.super_minor = UnSet;
411 mis.level = UnSet;
412 mis.raid_disks = UnSet;
413 mis.spare_disks = 0;
414 mis.devices = NULL;
415 mis.devname = NULL;
416 mis.spare_group = NULL;
417 mis.autof = 0;
418 mis.next = NULL;
419 mis.st = NULL;
420 mis.bitmap_fd = -1;
421 mis.bitmap_file = NULL;
422 mis.name[0] = 0;
423
424 for (w=dl_next(line); w!=line; w=dl_next(w)) {
425 if (w[0] == '/') {
426 if (mis.devname)
427 fprintf(stderr, Name ": only give one device per ARRAY line: %s and %s\n",
428 mis.devname, w);
429 else mis.devname = w;
430 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
431 if (mis.uuid_set)
432 fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
433 w);
434 else {
435 if (parse_uuid(w+5, mis.uuid))
436 mis.uuid_set = 1;
437 else
438 fprintf(stderr, Name ": bad uuid: %s\n", w);
439 }
440 } else if (strncasecmp(w, "super-minor=", 12)==0 ) {
441 if (mis.super_minor != UnSet)
442 fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n",
443 w);
444 else {
445 char *endptr;
446 mis.super_minor= strtol(w+12, &endptr, 10);
447 if (w[12]==0 || endptr[0]!=0 || mis.super_minor < 0) {
448 fprintf(stderr, Name ": invalid super-minor number: %s\n",
449 w);
450 mis.super_minor = UnSet;
451 }
452 }
453 } else if (strncasecmp(w, "name=", 5)==0) {
454 if (mis.name[0])
455 fprintf(stderr, Name ": only specify name once, %s ignored.\n",
456 w);
457 else if (strlen(w+5) > 32)
458 fprintf(stderr, Name ": name too long, ignoring %s\n", w);
459 else
460 strcpy(mis.name, w+5);
461
462 } else if (strncasecmp(w, "bitmap=", 7) == 0) {
463 if (mis.bitmap_file)
464 fprintf(stderr, Name ": only specify bitmap file once. %s ignored\n",
465 w);
466 else
467 mis.bitmap_file = strdup(w+7);
468
469 } else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
470 if (mis.devices)
471 fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n",
472 w);
473 else
474 mis.devices = strdup(w+8);
475 } else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
476 if (mis.spare_group)
477 fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n",
478 w);
479 else
480 mis.spare_group = strdup(w+12);
481 } else if (strncasecmp(w, "level=", 6) == 0 ) {
482 /* this is mainly for compatability with --brief output */
483 mis.level = map_name(pers, w+6);
484 } else if (strncasecmp(w, "disks=", 6) == 0 ) {
485 /* again, for compat */
486 mis.raid_disks = atoi(w+6);
487 } else if (strncasecmp(w, "num-devices=", 12) == 0 ) {
488 /* again, for compat */
489 mis.raid_disks = atoi(w+12);
490 } else if (strncasecmp(w, "spares=", 7) == 0 ) {
491 /* for warning if not all spares present */
492 mis.spare_disks = atoi(w+7);
493 } else if (strncasecmp(w, "metadata=", 9) == 0) {
494 /* style of metadata on the devices. */
495 int i;
496
497 for(i=0; superlist[i] && !mis.st; i++)
498 mis.st = superlist[i]->match_metadata_desc(w+9);
499
500 if (!mis.st)
501 fprintf(stderr, Name ": metadata format %s unknown, ignored.\n", w+9);
502 } else if (strncasecmp(w, "auto=", 5) == 0 ) {
503 /* whether to create device special files as needed */
504 mis.autof = parse_auto(w+5, "auto type", 0);
505 } else {
506 fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
507 w);
508 }
509 }
510 if (mis.devname == NULL)
511 fprintf(stderr, Name ": ARRAY line with no device\n");
512 else if (mis.uuid_set == 0 && mis.devices == NULL && mis.super_minor == UnSet && mis.name[0] == 0)
513 fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname);
514 else {
515 mi = malloc(sizeof(*mi));
516 *mi = mis;
517 mi->devname = strdup(mis.devname);
518 mi->next = NULL;
519 *mddevlp = mi;
520 mddevlp = &mi->next;
521 }
522 }
523
524 static char *alert_email = NULL;
525 void mailline(char *line)
526 {
527 char *w;
528
529 for (w=dl_next(line); w != line ; w=dl_next(w)) {
530 if (alert_email == NULL)
531 alert_email = strdup(w);
532 else
533 fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n",
534 w);
535 }
536 }
537
538 static char *alert_mail_from = NULL;
539 void mailfromline(char *line)
540 {
541 char *w;
542
543 for (w=dl_next(line); w != line ; w=dl_next(w)) {
544 if (alert_mail_from == NULL)
545 alert_mail_from = strdup(w);
546 else {
547 char *t= NULL;
548 asprintf(&t, "%s %s", alert_mail_from, w);
549 free(alert_mail_from);
550 alert_mail_from = t;
551 }
552 }
553 }
554
555
556 static char *alert_program = NULL;
557 void programline(char *line)
558 {
559 char *w;
560
561 for (w=dl_next(line); w != line ; w=dl_next(w)) {
562 if (alert_program == NULL)
563 alert_program = strdup(w);
564 else
565 fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n",
566 w);
567 }
568 }
569
570 static char *home_host = NULL;
571 void homehostline(char *line)
572 {
573 char *w;
574
575 for (w=dl_next(line); w != line ; w=dl_next(w)) {
576 if (home_host == NULL)
577 home_host = strdup(w);
578 else
579 fprintf(stderr, Name ": excess host name on HOMEHOST line: %s - ignored\n",
580 w);
581 }
582 }
583
584
585 int loaded = 0;
586
587 static char *conffile = NULL;
588 void set_conffile(char *file)
589 {
590 conffile = file;
591 }
592
593 void load_conffile(void)
594 {
595 FILE *f;
596 char *line;
597
598 if (loaded) return;
599 if (conffile == NULL)
600 conffile = DefaultConfFile;
601
602 if (strcmp(conffile, "none") == 0) {
603 loaded = 1;
604 return;
605 }
606 if (strcmp(conffile, "partitions")==0) {
607 char *list = dl_strdup("DEV");
608 dl_init(list);
609 dl_add(list, dl_strdup("partitions"));
610 devline(list);
611 free_line(list);
612 loaded = 1;
613 return;
614 }
615 f = fopen(conffile, "r");
616 /* Debian chose to relocate mdadm.conf into /etc/mdadm/.
617 * To allow Debian users to compile from clean source and still
618 * have a working mdadm, we read /etc/mdadm/mdadm.conf
619 * if /etc/mdadm.conf doesn't exist
620 */
621 if (f == NULL &&
622 conffile == DefaultConfFile) {
623 f = fopen(DefaultAltConfFile, "r");
624 if (f)
625 conffile = DefaultAltConfFile;
626 }
627 if (f == NULL)
628 return;
629
630 loaded = 1;
631 while ((line=conf_line(f))) {
632 switch(match_keyword(line)) {
633 case Devices:
634 devline(line);
635 break;
636 case Array:
637 arrayline(line);
638 break;
639 case Mailaddr:
640 mailline(line);
641 break;
642 case Mailfrom:
643 mailfromline(line);
644 break;
645 case Program:
646 programline(line);
647 break;
648 case CreateDev:
649 createline(line);
650 break;
651 case Homehost:
652 homehostline(line);
653 break;
654 default:
655 fprintf(stderr, Name ": Unknown keyword %s\n", line);
656 }
657 free_line(line);
658 }
659
660 fclose(f);
661
662 /* printf("got file\n"); */
663 }
664
665 char *conf_get_mailaddr(void)
666 {
667 load_conffile();
668 return alert_email;
669 }
670
671 char *conf_get_mailfrom(void)
672 {
673 load_conffile();
674 return alert_mail_from;
675 }
676
677 char *conf_get_program(void)
678 {
679 load_conffile();
680 return alert_program;
681 }
682
683 char *conf_get_homehost(void)
684 {
685 load_conffile();
686 return home_host;
687 }
688
689 struct createinfo *conf_get_create_info(void)
690 {
691 load_conffile();
692 return &createinfo;
693 }
694
695 mddev_ident_t conf_get_ident(char *dev)
696 {
697 mddev_ident_t rv;
698 load_conffile();
699 rv = mddevlist;
700 while (dev && rv && strcmp(dev, rv->devname)!=0)
701 rv = rv->next;
702 return rv;
703 }
704
705 mddev_dev_t conf_get_devs()
706 {
707 glob_t globbuf;
708 struct conf_dev *cd;
709 int flags = 0;
710 static mddev_dev_t dlist = NULL;
711 unsigned int i;
712
713 while (dlist) {
714 mddev_dev_t t = dlist;
715 dlist = dlist->next;
716 free(t->devname);
717 free(t);
718 }
719
720 load_conffile();
721
722 if (cdevlist == NULL)
723 /* default to 'partitions */
724 dlist = load_partitions();
725
726 for (cd=cdevlist; cd; cd=cd->next) {
727 if (strcasecmp(cd->name, "partitions")==0 && dlist == NULL)
728 dlist = load_partitions();
729 else {
730 glob(cd->name, flags, NULL, &globbuf);
731 flags |= GLOB_APPEND;
732 }
733 }
734 if (flags & GLOB_APPEND) {
735 for (i=0; i<globbuf.gl_pathc; i++) {
736 mddev_dev_t t = malloc(sizeof(*t));
737 t->devname = strdup(globbuf.gl_pathv[i]);
738 t->next = dlist;
739 t->used = 0;
740 dlist = t;
741 /* printf("one dev is %s\n", t->devname);*/
742 }
743 globfree(&globbuf);
744 }
745
746 return dlist;
747 }
748
749 int match_oneof(char *devices, char *devname)
750 {
751 /* check if one of the comma separated patterns in devices
752 * matches devname
753 */
754
755
756 while (devices && *devices) {
757 char patn[1024];
758 char *p = devices;
759 devices = strchr(devices, ',');
760 if (!devices)
761 devices = p + strlen(p);
762 if (devices-p < 1024) {
763 strncpy(patn, p, devices-p);
764 patn[devices-p] = 0;
765 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
766 return 1;
767 }
768 if (*devices == ',')
769 devices++;
770 }
771 return 0;
772 }