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