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