]> git.ipfire.org Git - thirdparty/mdadm.git/blame - config.c
mdadm-1.0.9
[thirdparty/mdadm.git] / config.c
CommitLineData
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
72char DefaultConfFile[] = CONFFILE;
64c4757e 73
e0d19036 74char *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
81int 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
101char *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
167char *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
188void 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
199struct conf_dev {
200 struct conf_dev *next;
201 char *name;
202} *cdevlist = NULL;
203
5787fa49
NB
204void 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;
214 char *name;
215 buf[1023] = '\0';
216 if (buf[0] != ' ')
217 continue;
218 if (sscanf(buf, " %d %d ", &major, &minor) != 2)
219 continue;
220 name = map_dev(major, minor);
221 if (name) {
222 struct conf_dev *cd;
223
224 cd = malloc(sizeof(*cd));
225 cd->name = strdup(name);
226 cd->next = cdevlist;
227 cdevlist = cd;
228 }
229 }
230}
82b27616
NB
231
232
e0d19036 233void devline(char *line)
82b27616 234{
52826846
NB
235 char *w;
236 struct conf_dev *cd;
237
238 for (w=dl_next(line); w != line; w=dl_next(w)) {
239 if (w[0] == '/') {
240 cd = malloc(sizeof(*cd));
241 cd->name = strdup(w);
242 cd->next = cdevlist;
243 cdevlist = cd;
5787fa49
NB
244 } else if (strcasecmp(w, "partitions") == 0) {
245 /* read /proc/partitions, and look major/minor up in /dev */
246 load_partitions();
52826846
NB
247 } else {
248 fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
249 w);
250 }
82b27616 251 }
82b27616
NB
252}
253
52826846
NB
254mddev_ident_t mddevlist = NULL;
255mddev_ident_t *mddevlp = &mddevlist;
82b27616
NB
256
257void arrayline(char *line)
258{
52826846
NB
259 char *w;
260
261 struct mddev_ident_s mis;
262 mddev_ident_t mi;
263
264 mis.uuid_set = 0;
265 mis.super_minor = -1;
cd29a5c8
NB
266 mis.level = -10;
267 mis.raid_disks = -1;
52826846
NB
268 mis.devices = NULL;
269 mis.devname = NULL;
e0d19036 270 mis.spare_group = NULL;
52826846
NB
271
272 for (w=dl_next(line); w!=line; w=dl_next(w)) {
273 if (w[0] == '/') {
274 if (mis.devname)
275 fprintf(stderr, Name ": only give one device per ARRAY line: %s and %s\n",
276 mis.devname, w);
277 else mis.devname = w;
278 } else if (strncasecmp(w, "uuid=", 5)==0 ) {
279 if (mis.uuid_set)
280 fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
281 w);
282 else {
283 if (parse_uuid(w+5, mis.uuid))
284 mis.uuid_set = 1;
285 else
286 fprintf(stderr, Name ": bad uuid: %s\n", w);
287 }
288 } else if (strncasecmp(w, "super-minor=", 12)==0 ) {
289 if (mis.super_minor >= 0)
290 fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n",
291 w);
292 else {
293 char *endptr;
294 mis.super_minor= strtol(w+12, &endptr, 10);
295 if (w[12]==0 || endptr[0]!=0 || mis.super_minor < 0) {
296 fprintf(stderr, Name ": invalid super-minor number: %s\n",
297 w);
298 mis.super_minor = -1;
299 }
300 }
301 } else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
302 if (mis.devices)
303 fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n",
304 w);
305 else
306 mis.devices = strdup(w+8);
307 } else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
308 if (mis.spare_group)
309 fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n",
310 w);
311 else
312 mis.spare_group = strdup(w+12);
cd29a5c8
NB
313 } else if (strncasecmp(w, "level=", 6) == 0 ) {
314 /* this is mainly for compatability with --brief output */
315 mis.level = map_name(pers, w+6);
316 } else if (strncasecmp(w, "disks=", 6) == 0 ) {
317 /* again, for compat */
318 mis.raid_disks = atoi(w+6);
b83d95f3
NB
319 } else if (strncasecmp(w, "num-devices=", 12) == 0 ) {
320 /* again, for compat */
321 mis.raid_disks = atoi(w+12);
52826846
NB
322 } else {
323 fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
324 w);
325 }
326 }
327 if (mis.devname == NULL)
328 fprintf(stderr, Name ": ARRAY line with a device\n");
329 else if (mis.uuid_set == 0 && mis.devices == NULL && mis.super_minor < 0)
330 fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname);
331 else {
332 mi = malloc(sizeof(*mi));
333 *mi = mis;
334 mi->devname = strdup(mis.devname);
335 mi->next = NULL;
336 *mddevlp = mi;
337 mddevlp = &mi->next;
82b27616 338 }
82b27616 339}
e0d19036
NB
340
341static char *alert_email = NULL;
342void mailline(char *line)
343{
344 char *w;
345
346 for (w=dl_next(line); w != line ; w=dl_next(w)) {
347 if (alert_email == NULL)
348 alert_email = strdup(w);
349 else
350 fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n",
351 w);
352 }
353}
354
355
356static char *alert_program = NULL;
357void programline(char *line)
358{
359 char *w;
360
361 for (w=dl_next(line); w != line ; w=dl_next(w)) {
362 if (alert_program == NULL)
363 alert_program = strdup(w);
364 else
365 fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n",
366 w);
367 }
368}
369
370
82b27616
NB
371int loaded = 0;
372
373void load_conffile(char *conffile)
374{
52826846
NB
375 FILE *f;
376 char *line;
377
378 if (loaded) return;
379 if (conffile == NULL)
380 conffile = DefaultConfFile;
381
5787fa49
NB
382 if (strcmp(conffile, "partitions")==0) {
383 load_partitions();
384 return;
385 }
52826846
NB
386 f = fopen(conffile, "r");
387 if (f ==NULL)
388 return;
389
390 loaded = 1;
391 while ((line=conf_line(f))) {
392 switch(match_keyword(line)) {
393 case 0: /* DEVICE */
394 devline(line);
395 break;
e0d19036 396 case 1: /* ARRAY */
52826846
NB
397 arrayline(line);
398 break;
e0d19036
NB
399 case 2: /* MAIL */
400 mailline(line);
401 break;
402 case 3: /* PROGRAM */
403 programline(line);
404 break;
52826846
NB
405 default:
406 fprintf(stderr, Name ": Unknown keyword %s\n", line);
407 }
408 free_line(line);
82b27616 409 }
82b27616
NB
410
411
412/* printf("got file\n"); */
413}
414
e0d19036
NB
415char *conf_get_mailaddr(char *conffile)
416{
417 load_conffile(conffile);
418 return alert_email;
419}
420
421char *conf_get_program(char *conffile)
422{
423 load_conffile(conffile);
424 return alert_program;
425}
426
82b27616 427
52826846 428mddev_ident_t conf_get_ident(char *conffile, char *dev)
64c4757e 429{
52826846
NB
430 mddev_ident_t rv;
431 load_conffile(conffile);
432 rv = mddevlist;
433 while (dev && rv && strcmp(dev, rv->devname)!=0)
434 rv = rv->next;
435 return rv;
64c4757e
NB
436}
437
438mddev_dev_t conf_get_devs(char *conffile)
439{
52826846
NB
440 glob_t globbuf;
441 struct conf_dev *cd;
442 int flags = 0;
443 static mddev_dev_t dlist = NULL;
444 int i;
445
446 while (dlist) {
447 mddev_dev_t t = dlist;
448 dlist = dlist->next;
449 free(t->devname);
450 free(t);
451 }
82b27616 452
52826846 453 load_conffile(conffile);
82b27616 454
52826846
NB
455 for (cd=cdevlist; cd; cd=cd->next) {
456 glob(cd->name, flags, NULL, &globbuf);
457 flags |= GLOB_APPEND;
458 }
e0d19036
NB
459 if (flags & GLOB_APPEND) {
460 for (i=0; i<globbuf.gl_pathc; i++) {
461 mddev_dev_t t = malloc(sizeof(*t));
462 t->devname = strdup(globbuf.gl_pathv[i]);
463 t->next = dlist;
464 dlist = t;
82b27616 465/* printf("one dev is %s\n", t->devname);*/
e0d19036
NB
466 }
467 globfree(&globbuf);
52826846 468 }
82b27616 469
52826846 470 return dlist;
64c4757e
NB
471}
472
52826846
NB
473int match_oneof(char *devices, char *devname)
474{
475 /* check if one of the comma separated patterns in devices
476 * matches devname
477 */
478
479
480 while (devices && *devices) {
481 char patn[1024];
482 char *p = devices;
483 devices = strchr(devices, ',');
484 if (!devices)
485 devices = p + strlen(p);
486 if (devices-p < 1024) {
487 strncpy(patn, p, devices-p);
488 patn[devices-p] = 0;
489 if (fnmatch(patn, devname, FNM_PATHNAME)==0)
490 return 1;
491 }
492 if (*devices == ',')
493 devices++;
494 }
495 return 0;
496}