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