]>
Commit | Line | Data |
---|---|---|
64c4757e | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
64c4757e | 3 | * |
cd29a5c8 | 4 | * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au> |
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 NB |
34 | #include <fnmatch.h> |
35 | ||
64c4757e NB |
36 | /* |
37 | * Read the config file | |
38 | * | |
39 | * conf_get_uuids gets a list of devicename+uuid pairs | |
40 | * conf_get_devs gets device names after expanding wildcards | |
41 | * | |
42 | * Each keeps the returned list and frees it when asked to make | |
43 | * a new list. | |
44 | * | |
82b27616 NB |
45 | * The format of the config file needs to be fairly extensible. |
46 | * Now, arrays only have names and uuids and devices merely are. | |
47 | * But later arrays might want names, and devices might want superblock | |
48 | * versions, and who knows what else. | |
49 | * I like free format, abhore backslash line continuation, adore | |
50 | * indentation for structure and am ok about # comments. | |
51 | * | |
52 | * So, each line that isn't blank or a #comment must either start | |
53 | * with a key word, and not be indented, or must start with a | |
54 | * non-key-word and must be indented. | |
55 | * | |
56 | * Keywords are DEVICE and ARRAY | |
57 | * DEV{ICE} introduces some devices that might contain raid components. | |
58 | * e.g. | |
59 | * DEV style=0 /dev/sda* /dev/hd* | |
60 | * DEV style=1 /dev/sd[b-f]* | |
61 | * ARR{AY} describes an array giving md device and attributes like uuid=whatever | |
62 | * e.g. | |
63 | * ARRAY /dev/md0 uuid=whatever name=something | |
64 | * Spaces separate words on each line. Quoting, with "" or '' protects them, | |
65 | * but may not wrap over lines | |
66 | * | |
64c4757e NB |
67 | */ |
68 | ||
11a3e71d NB |
69 | #ifndef CONFFILE |
70 | #define CONFFILE "/etc/mdadm.conf" | |
71 | #endif | |
72 | char DefaultConfFile[] = CONFFILE; | |
64c4757e | 73 | |
e0d19036 | 74 | char *keywords[] = { "device", "array", "mailaddr", "program", NULL }; |
82b27616 NB |
75 | |
76 | /* | |
77 | * match_keyword returns an index into the keywords array, or -1 for no match | |
78 | * case is ignored, and at least three characters must be given | |
79 | */ | |
80 | ||
81 | int match_keyword(char *word) | |
82 | { | |
52826846 NB |
83 | int len = strlen(word); |
84 | int n; | |
82b27616 | 85 | |
52826846 NB |
86 | if (len < 3) return -1; |
87 | for (n=0; keywords[n]; n++) { | |
88 | if (strncasecmp(word, keywords[n], len)==0) | |
89 | return n; | |
90 | } | |
91 | return -1; | |
82b27616 NB |
92 | } |
93 | ||
94 | /* conf_word gets one word from the conf file. | |
95 | * if "allow_key", then accept words at the start of a line, | |
96 | * otherwise stop when such a word is found. | |
97 | * We assume that the file pointer is at the end of a word, so the | |
98 | * next character is a space, or a newline. If not, it is the start of a line. | |
99 | */ | |
100 | ||
101 | char *conf_word(FILE *file, int allow_key) | |
102 | { | |
52826846 NB |
103 | int wsize = 100; |
104 | int len = 0; | |
105 | int c; | |
106 | int quote; | |
107 | int wordfound = 0; | |
108 | char *word = malloc(wsize); | |
82b27616 | 109 | |
52826846 NB |
110 | if (!word) abort(); |
111 | ||
112 | while (wordfound==0) { | |
113 | /* at the end of a word.. */ | |
82b27616 | 114 | c = getc(file); |
52826846 NB |
115 | if (c == '#') |
116 | while (c != EOF && c != '\n') | |
117 | c = getc(file); | |
118 | if (c == EOF) break; | |
119 | if (c == '\n') continue; | |
120 | ||
121 | if (c != ' ' && c != '\t' && ! allow_key) { | |
122 | ungetc(c, file); | |
123 | break; | |
124 | } | |
125 | /* looks like it is safe to get a word here, if there is one */ | |
126 | quote = 0; | |
127 | /* first, skip any spaces */ | |
128 | while (c == ' ' || c == '\t') | |
129 | c = getc(file); | |
130 | if (c != EOF && c != '\n' && c != '#') { | |
131 | /* we really have a character of a word, so start saving it */ | |
132 | while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) { | |
133 | wordfound = 1; | |
134 | if (quote && c == quote) quote = 0; | |
135 | else if (quote == 0 && (c == '\'' || c == '"')) | |
136 | quote = c; | |
137 | else { | |
138 | if (len == wsize-1) { | |
139 | wsize += 100; | |
140 | word = realloc(word, wsize); | |
141 | if (!word) abort(); | |
142 | } | |
143 | word[len++] = c; | |
144 | } | |
145 | c = getc(file); | |
146 | } | |
147 | } | |
148 | if (c != EOF) ungetc(c, file); | |
82b27616 | 149 | } |
52826846 | 150 | word[len] = 0; |
82b27616 | 151 | /* printf("word is <%s>\n", word); */ |
52826846 NB |
152 | if (!wordfound) { |
153 | free(word); | |
154 | word = NULL; | |
155 | } | |
156 | return word; | |
82b27616 NB |
157 | } |
158 | ||
159 | /* | |
160 | * conf_line reads one logical line from the conffile. | |
161 | * It skips comments and continues until it finds a line that starts | |
162 | * with a non blank/comment. This character is pushed back for the next call | |
163 | * A doubly linked list of words is returned. | |
164 | * the first word will be a keyword. Other words will have had quotes removed. | |
165 | */ | |
166 | ||
167 | char *conf_line(FILE *file) | |
168 | { | |
52826846 NB |
169 | char *w; |
170 | char *list; | |
82b27616 | 171 | |
52826846 NB |
172 | w = conf_word(file, 1); |
173 | if (w == NULL) return NULL; | |
82b27616 | 174 | |
52826846 | 175 | list = dl_strdup(w); |
82b27616 | 176 | free(w); |
52826846 NB |
177 | dl_init(list); |
178 | ||
179 | while ((w = conf_word(file,0))){ | |
180 | char *w2 = dl_strdup(w); | |
181 | free(w); | |
182 | dl_add(list, w2); | |
183 | } | |
82b27616 | 184 | /* printf("got a line\n");*/ |
52826846 | 185 | return list; |
82b27616 NB |
186 | } |
187 | ||
188 | void free_line(char *line) | |
189 | { | |
52826846 NB |
190 | char *w; |
191 | for (w=dl_next(line); w != line; w=dl_next(line)) { | |
192 | dl_del(w); | |
193 | dl_free(w); | |
194 | } | |
195 | dl_free(line); | |
82b27616 NB |
196 | } |
197 | ||
198 | ||
199 | struct conf_dev { | |
200 | struct conf_dev *next; | |
201 | char *name; | |
202 | } *cdevlist = NULL; | |
203 | ||
5787fa49 NB |
204 | void load_partitions(void) |
205 | { | |
206 | FILE *f = fopen("/proc/partitions", "r"); | |
207 | char buf[1024]; | |
208 | if (f == NULL) { | |
209 | fprintf(stderr, Name ": cannot open /proc/partitions\n"); | |
210 | return; | |
211 | } | |
212 | while (fgets(buf, 1024, f)) { | |
213 | int major, minor; | |
98c6faba | 214 | char *name, *mp; |
5787fa49 NB |
215 | buf[1023] = '\0'; |
216 | if (buf[0] != ' ') | |
217 | continue; | |
98c6faba NB |
218 | major = strtoul(buf, &mp, 10); |
219 | if (mp == buf || *mp != ' ') | |
5787fa49 | 220 | continue; |
98c6faba NB |
221 | minor = strtoul(mp, NULL, 10); |
222 | ||
5787fa49 NB |
223 | name = map_dev(major, minor); |
224 | if (name) { | |
225 | struct conf_dev *cd; | |
226 | ||
227 | cd = malloc(sizeof(*cd)); | |
228 | cd->name = strdup(name); | |
229 | cd->next = cdevlist; | |
230 | cdevlist = cd; | |
231 | } | |
232 | } | |
233 | } | |
82b27616 NB |
234 | |
235 | ||
e0d19036 | 236 | void devline(char *line) |
82b27616 | 237 | { |
52826846 NB |
238 | char *w; |
239 | struct conf_dev *cd; | |
240 | ||
241 | for (w=dl_next(line); w != line; w=dl_next(w)) { | |
242 | if (w[0] == '/') { | |
243 | cd = malloc(sizeof(*cd)); | |
244 | cd->name = strdup(w); | |
245 | cd->next = cdevlist; | |
246 | cdevlist = cd; | |
5787fa49 NB |
247 | } else if (strcasecmp(w, "partitions") == 0) { |
248 | /* read /proc/partitions, and look major/minor up in /dev */ | |
249 | load_partitions(); | |
52826846 NB |
250 | } else { |
251 | fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n", | |
252 | w); | |
253 | } | |
82b27616 | 254 | } |
82b27616 NB |
255 | } |
256 | ||
52826846 NB |
257 | mddev_ident_t mddevlist = NULL; |
258 | mddev_ident_t *mddevlp = &mddevlist; | |
82b27616 NB |
259 | |
260 | void arrayline(char *line) | |
261 | { | |
52826846 NB |
262 | char *w; |
263 | ||
264 | struct mddev_ident_s mis; | |
265 | mddev_ident_t mi; | |
266 | ||
267 | mis.uuid_set = 0; | |
98c6faba NB |
268 | mis.super_minor = UnSet; |
269 | mis.level = UnSet; | |
270 | mis.raid_disks = UnSet; | |
271 | mis.spare_disks = UnSet; | |
52826846 NB |
272 | mis.devices = NULL; |
273 | mis.devname = NULL; | |
e0d19036 | 274 | mis.spare_group = NULL; |
52826846 NB |
275 | |
276 | for (w=dl_next(line); w!=line; w=dl_next(w)) { | |
277 | if (w[0] == '/') { | |
278 | if (mis.devname) | |
279 | fprintf(stderr, Name ": only give one device per ARRAY line: %s and %s\n", | |
280 | mis.devname, w); | |
281 | else mis.devname = w; | |
282 | } else if (strncasecmp(w, "uuid=", 5)==0 ) { | |
283 | if (mis.uuid_set) | |
284 | fprintf(stderr, Name ": only specify uuid once, %s ignored.\n", | |
285 | w); | |
286 | else { | |
287 | if (parse_uuid(w+5, mis.uuid)) | |
288 | mis.uuid_set = 1; | |
289 | else | |
290 | fprintf(stderr, Name ": bad uuid: %s\n", w); | |
291 | } | |
292 | } else if (strncasecmp(w, "super-minor=", 12)==0 ) { | |
293 | if (mis.super_minor >= 0) | |
294 | fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n", | |
295 | w); | |
296 | else { | |
297 | char *endptr; | |
298 | mis.super_minor= strtol(w+12, &endptr, 10); | |
299 | if (w[12]==0 || endptr[0]!=0 || mis.super_minor < 0) { | |
300 | fprintf(stderr, Name ": invalid super-minor number: %s\n", | |
301 | w); | |
98c6faba | 302 | mis.super_minor = UnSet; |
52826846 NB |
303 | } |
304 | } | |
305 | } else if (strncasecmp(w, "devices=", 8 ) == 0 ) { | |
306 | if (mis.devices) | |
307 | fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n", | |
308 | w); | |
309 | else | |
310 | mis.devices = strdup(w+8); | |
311 | } else if (strncasecmp(w, "spare-group=", 12) == 0 ) { | |
312 | if (mis.spare_group) | |
313 | fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n", | |
314 | w); | |
315 | else | |
316 | mis.spare_group = strdup(w+12); | |
cd29a5c8 NB |
317 | } else if (strncasecmp(w, "level=", 6) == 0 ) { |
318 | /* this is mainly for compatability with --brief output */ | |
319 | mis.level = map_name(pers, w+6); | |
320 | } else if (strncasecmp(w, "disks=", 6) == 0 ) { | |
321 | /* again, for compat */ | |
feb716e9 | 322 | mis.raid_disks = atoi(w+6); |
b83d95f3 NB |
323 | } else if (strncasecmp(w, "num-devices=", 12) == 0 ) { |
324 | /* again, for compat */ | |
feb716e9 NB |
325 | mis.raid_disks = atoi(w+12); |
326 | } else if (strncasecmp(w, "spares=", 7) == 0 ) { | |
327 | /* for warning if not all spares present */ | |
328 | mis.spare_disks = atoi(w+7); | |
52826846 NB |
329 | } else { |
330 | fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n", | |
331 | w); | |
332 | } | |
333 | } | |
334 | if (mis.devname == NULL) | |
335 | fprintf(stderr, Name ": ARRAY line with a device\n"); | |
336 | else if (mis.uuid_set == 0 && mis.devices == NULL && mis.super_minor < 0) | |
337 | fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname); | |
338 | else { | |
339 | mi = malloc(sizeof(*mi)); | |
340 | *mi = mis; | |
341 | mi->devname = strdup(mis.devname); | |
342 | mi->next = NULL; | |
343 | *mddevlp = mi; | |
344 | mddevlp = &mi->next; | |
82b27616 | 345 | } |
82b27616 | 346 | } |
e0d19036 NB |
347 | |
348 | static char *alert_email = NULL; | |
349 | void mailline(char *line) | |
350 | { | |
351 | char *w; | |
352 | ||
353 | for (w=dl_next(line); w != line ; w=dl_next(w)) { | |
354 | if (alert_email == NULL) | |
355 | alert_email = strdup(w); | |
356 | else | |
357 | fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n", | |
358 | w); | |
359 | } | |
360 | } | |
361 | ||
362 | ||
363 | static char *alert_program = NULL; | |
364 | void programline(char *line) | |
365 | { | |
366 | char *w; | |
367 | ||
368 | for (w=dl_next(line); w != line ; w=dl_next(w)) { | |
369 | if (alert_program == NULL) | |
370 | alert_program = strdup(w); | |
371 | else | |
372 | fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n", | |
373 | w); | |
374 | } | |
375 | } | |
376 | ||
377 | ||
82b27616 NB |
378 | int loaded = 0; |
379 | ||
380 | void load_conffile(char *conffile) | |
381 | { | |
52826846 NB |
382 | FILE *f; |
383 | char *line; | |
384 | ||
385 | if (loaded) return; | |
386 | if (conffile == NULL) | |
387 | conffile = DefaultConfFile; | |
388 | ||
d013a55e NB |
389 | if (strcmp(conffile, "none") == 0) { |
390 | loaded = 1; | |
391 | return; | |
392 | } | |
5787fa49 NB |
393 | if (strcmp(conffile, "partitions")==0) { |
394 | load_partitions(); | |
d013a55e | 395 | loaded = 1; |
5787fa49 NB |
396 | return; |
397 | } | |
52826846 NB |
398 | f = fopen(conffile, "r"); |
399 | if (f ==NULL) | |
400 | return; | |
401 | ||
402 | loaded = 1; | |
403 | while ((line=conf_line(f))) { | |
404 | switch(match_keyword(line)) { | |
405 | case 0: /* DEVICE */ | |
406 | devline(line); | |
407 | break; | |
e0d19036 | 408 | case 1: /* ARRAY */ |
52826846 NB |
409 | arrayline(line); |
410 | break; | |
e0d19036 NB |
411 | case 2: /* MAIL */ |
412 | mailline(line); | |
413 | break; | |
414 | case 3: /* PROGRAM */ | |
415 | programline(line); | |
416 | break; | |
52826846 NB |
417 | default: |
418 | fprintf(stderr, Name ": Unknown keyword %s\n", line); | |
419 | } | |
420 | free_line(line); | |
82b27616 | 421 | } |
82b27616 NB |
422 | |
423 | ||
424 | /* printf("got file\n"); */ | |
425 | } | |
426 | ||
e0d19036 NB |
427 | char *conf_get_mailaddr(char *conffile) |
428 | { | |
429 | load_conffile(conffile); | |
430 | return alert_email; | |
431 | } | |
432 | ||
433 | char *conf_get_program(char *conffile) | |
434 | { | |
435 | load_conffile(conffile); | |
436 | return alert_program; | |
437 | } | |
438 | ||
82b27616 | 439 | |
52826846 | 440 | mddev_ident_t conf_get_ident(char *conffile, char *dev) |
64c4757e | 441 | { |
52826846 NB |
442 | mddev_ident_t rv; |
443 | load_conffile(conffile); | |
444 | rv = mddevlist; | |
445 | while (dev && rv && strcmp(dev, rv->devname)!=0) | |
446 | rv = rv->next; | |
447 | return rv; | |
64c4757e NB |
448 | } |
449 | ||
450 | mddev_dev_t conf_get_devs(char *conffile) | |
451 | { | |
52826846 NB |
452 | glob_t globbuf; |
453 | struct conf_dev *cd; | |
454 | int flags = 0; | |
455 | static mddev_dev_t dlist = NULL; | |
98c6faba | 456 | unsigned int i; |
52826846 NB |
457 | |
458 | while (dlist) { | |
459 | mddev_dev_t t = dlist; | |
460 | dlist = dlist->next; | |
461 | free(t->devname); | |
462 | free(t); | |
463 | } | |
82b27616 | 464 | |
52826846 | 465 | load_conffile(conffile); |
82b27616 | 466 | |
52826846 NB |
467 | for (cd=cdevlist; cd; cd=cd->next) { |
468 | glob(cd->name, flags, NULL, &globbuf); | |
469 | flags |= GLOB_APPEND; | |
470 | } | |
e0d19036 NB |
471 | if (flags & GLOB_APPEND) { |
472 | for (i=0; i<globbuf.gl_pathc; i++) { | |
473 | mddev_dev_t t = malloc(sizeof(*t)); | |
474 | t->devname = strdup(globbuf.gl_pathv[i]); | |
475 | t->next = dlist; | |
476 | dlist = t; | |
82b27616 | 477 | /* printf("one dev is %s\n", t->devname);*/ |
e0d19036 NB |
478 | } |
479 | globfree(&globbuf); | |
52826846 | 480 | } |
82b27616 | 481 | |
52826846 | 482 | return dlist; |
64c4757e NB |
483 | } |
484 | ||
52826846 NB |
485 | int match_oneof(char *devices, char *devname) |
486 | { | |
487 | /* check if one of the comma separated patterns in devices | |
488 | * matches devname | |
489 | */ | |
490 | ||
491 | ||
492 | while (devices && *devices) { | |
493 | char patn[1024]; | |
494 | char *p = devices; | |
495 | devices = strchr(devices, ','); | |
496 | if (!devices) | |
497 | devices = p + strlen(p); | |
498 | if (devices-p < 1024) { | |
499 | strncpy(patn, p, devices-p); | |
500 | patn[devices-p] = 0; | |
501 | if (fnmatch(patn, devname, FNM_PATHNAME)==0) | |
502 | return 1; | |
503 | } | |
504 | if (*devices == ',') | |
505 | devices++; | |
506 | } | |
507 | return 0; | |
508 | } |