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