Merge with branch 'master'
[platform/upstream/multipath-tools.git] / libmultipath / config.c
1 /*
2  * Copyright (c) 2004, 2005 Christophe Varoqui
3  * Copyright (c) 2005 Benjamin Marzinski, Redhat
4  * Copyright (c) 2005 Edward Goggin, EMC
5  */
6 #include <stdio.h>
7 #include <string.h>
8
9 #include <checkers.h>
10
11 #include "memory.h"
12 #include "util.h"
13 #include "debug.h"
14 #include "parser.h"
15 #include "dict.h"
16 #include "hwtable.h"
17 #include "vector.h"
18 #include "structs.h"
19 #include "config.h"
20 #include "blacklist.h"
21 #include "defaults.h"
22
23 static struct hwentry *
24 find_hwe_strmatch (vector hwtable, char * vendor, char * product, char * revision)
25 {
26         int i;
27         struct hwentry *hwe, *ret = NULL;
28
29         vector_foreach_slot (hwtable, hwe, i) {
30                 if (hwe->vendor && vendor && strcmp(hwe->vendor, vendor))
31                         continue;
32
33                 if (hwe->product && product && strcmp(hwe->product, product))
34                         continue;
35
36                 if (hwe->revision && revision && strcmp(hwe->revision, revision))
37                         continue;
38
39                 ret = hwe;
40                 break;
41         }
42         return ret;
43 }
44
45 struct hwentry *
46 find_hwe (vector hwtable, char * vendor, char * product, char * revision)
47 {
48         int i;
49         struct hwentry *hwe, *ret = NULL;
50         regex_t vre, pre, rre;
51
52         vector_foreach_slot (hwtable, hwe, i) {
53                 if (hwe->vendor &&
54                     regcomp(&vre, hwe->vendor, REG_EXTENDED|REG_NOSUB))
55                         break;
56                 if (hwe->product &&
57                     regcomp(&pre, hwe->product, REG_EXTENDED|REG_NOSUB)) {
58                         regfree(&vre);
59                         break;
60                 }
61                 if (hwe->revision &&
62                     regcomp(&rre, hwe->revision, REG_EXTENDED|REG_NOSUB)) {
63                         regfree(&vre);
64                         regfree(&pre);
65                         break;
66                 }
67                 if ((!hwe->vendor || !regexec(&vre, vendor, 0, NULL, 0)) &&
68                     (!hwe->product || !regexec(&pre, product, 0, NULL, 0)) &&
69                     (!hwe->revision || !regexec(&rre, revision, 0, NULL, 0)))
70                         ret = hwe;
71
72                 if (hwe->revision)
73                         regfree(&rre);
74                 if (hwe->product)
75                         regfree(&pre);
76                 if (hwe->vendor)
77                         regfree(&vre);
78
79                 if (ret)
80                         break;
81         }
82         return ret;
83 }
84
85 extern struct mpentry *
86 find_mpe (char * wwid)
87 {
88         int i;
89         struct mpentry * mpe;
90
91         if (!wwid)
92                 return NULL;
93
94         vector_foreach_slot (conf->mptable, mpe, i)
95                 if (mpe->wwid && !strcmp(mpe->wwid, wwid))
96                         return mpe;
97
98         return NULL;
99 }
100
101 extern char *
102 get_mpe_wwid (char * alias)
103 {
104         int i;
105         struct mpentry * mpe;
106
107         if (!alias)
108                 return NULL;
109
110         vector_foreach_slot (conf->mptable, mpe, i)
111                 if (mpe->alias && strcmp(mpe->alias, alias) == 0)
112                         return mpe->wwid;
113
114         return NULL;
115 }
116
117 void
118 free_hwe (struct hwentry * hwe)
119 {
120         if (!hwe)
121                 return;
122
123         if (hwe->vendor)
124                 FREE(hwe->vendor);
125
126         if (hwe->product)
127                 FREE(hwe->product);
128
129         if (hwe->revision)
130                 FREE(hwe->revision);
131
132         if (hwe->selector)
133                 FREE(hwe->selector);
134
135         if (hwe->getuid)
136                 FREE(hwe->getuid);
137
138         if (hwe->getprio)
139                 FREE(hwe->getprio);
140
141         if (hwe->features)
142                 FREE(hwe->features);
143
144         if (hwe->hwhandler)
145                 FREE(hwe->hwhandler);
146
147         if (hwe->bl_product)
148                 FREE(hwe->bl_product);
149
150         FREE(hwe);
151 }
152
153 void
154 free_hwtable (vector hwtable)
155 {
156         int i;
157         struct hwentry * hwe;
158
159         if (!hwtable)
160                 return;
161
162         vector_foreach_slot (hwtable, hwe, i)
163                 free_hwe(hwe);
164
165         vector_free(hwtable);
166 }
167
168 void
169 free_mpe (struct mpentry * mpe)
170 {
171         if (!mpe)
172                 return;
173
174         if (mpe->wwid)
175                 FREE(mpe->wwid);
176
177         if (mpe->selector)
178                 FREE(mpe->selector);
179
180         if (mpe->getuid)
181                 FREE(mpe->getuid);
182
183         if (mpe->alias)
184                 FREE(mpe->alias);
185
186         FREE(mpe);
187 }
188
189 void
190 free_mptable (vector mptable)
191 {
192         int i;
193         struct mpentry * mpe;
194
195         if (!mptable)
196                 return;
197
198         vector_foreach_slot (mptable, mpe, i)
199                 free_mpe(mpe);
200
201         vector_free(mptable);
202 }
203
204 struct mpentry *
205 alloc_mpe (void)
206 {
207         struct mpentry * mpe = (struct mpentry *)
208                                 MALLOC(sizeof(struct mpentry));
209
210         return mpe;
211 }
212
213 static struct hwentry *
214 alloc_hwe (void)
215 {
216         struct hwentry * hwe = (struct hwentry *)
217                                 MALLOC(sizeof(struct hwentry));
218
219         return hwe;
220 }
221
222 static char *
223 set_param_str(char * str)
224 {
225         char * dst;
226         int len;
227
228         if (!str)
229                 return NULL;
230
231         len = strlen(str);
232
233         if (!len)
234                 return NULL;
235
236         dst = (char *)MALLOC(len + 1);
237
238         if (!dst)
239                 return NULL;
240
241         strcpy(dst, str);
242         return dst;
243 }
244
245 int
246 store_hwe (vector hwtable, struct hwentry * dhwe)
247 {
248         struct hwentry * hwe;
249
250         if (find_hwe_strmatch(hwtable, dhwe->vendor, dhwe->product, dhwe->revision))
251                 return 0;
252         
253         if (!(hwe = alloc_hwe()))
254                 return 1;
255
256         if (!dhwe->vendor || !(hwe->vendor = set_param_str(dhwe->vendor)))
257                 goto out;
258         
259         if (!dhwe->product || !(hwe->product = set_param_str(dhwe->product)))
260                 goto out;
261         
262         if (dhwe->revision && !(hwe->revision = set_param_str(dhwe->revision)))
263                 goto out;
264         
265         if (dhwe->getuid && !(hwe->getuid = set_param_str(dhwe->getuid)))
266                 goto out;
267
268         if (dhwe->getprio && !(hwe->getprio = set_param_str(dhwe->getprio)))
269                 goto out;
270                                 
271         if (dhwe->features && !(hwe->features = set_param_str(dhwe->features)))
272                 goto out;
273         
274         if (dhwe->hwhandler && !(hwe->hwhandler = set_param_str(dhwe->hwhandler)))
275                 goto out;
276
277         if (dhwe->selector && !(hwe->selector = set_param_str(dhwe->selector)))
278                 goto out;
279                                 
280         hwe->pgpolicy = dhwe->pgpolicy;
281         hwe->pgfailback = dhwe->pgfailback;
282         hwe->rr_weight = dhwe->rr_weight;
283         hwe->no_path_retry = dhwe->no_path_retry;
284         hwe->minio = dhwe->minio;
285         hwe->checker = dhwe->checker;
286
287         if (dhwe->bl_product && !(hwe->bl_product = set_param_str(dhwe->bl_product)))
288                 goto out;
289
290         if (!vector_alloc_slot(hwtable))
291                 goto out;
292
293         vector_set_slot(hwtable, hwe);
294         return 0;
295 out:
296         free_hwe(hwe);
297         return 1;
298 }
299
300 struct config *
301 alloc_config (void)
302 {
303         return (struct config *)MALLOC(sizeof(struct config));
304 }
305
306 void
307 free_config (struct config * conf)
308 {
309         if (!conf)
310                 return;
311
312         if (conf->dev)
313                 FREE(conf->dev);
314
315         if (conf->udev_dir)
316                 FREE(conf->udev_dir);
317
318         if (conf->selector)
319                 FREE(conf->selector);
320
321         if (conf->getuid)
322                 FREE(conf->getuid);
323
324         if (conf->getprio)
325                 FREE(conf->getprio);
326
327         if (conf->features)
328                 FREE(conf->features);
329
330         if (conf->hwhandler)
331                 FREE(conf->hwhandler);
332
333         free_blacklist(conf->blist_devnode);
334         free_blacklist(conf->blist_wwid);
335         free_blacklist_device(conf->blist_device);
336         free_mptable(conf->mptable);
337         free_hwtable(conf->hwtable);
338         free_keywords(conf->keywords);
339         FREE(conf);
340 }
341
342 int
343 load_config (char * file)
344 {
345         if (!conf)
346                 conf = alloc_config();
347
348         if (!conf)
349                 return 1;
350
351         /*
352          * internal defaults
353          */
354         if (!conf->verbosity)
355                 conf->verbosity = 2;
356
357         conf->dev_type = DEV_NONE;
358         conf->minio = 1000;
359         conf->bindings_file = DEFAULT_BINDINGS_FILE;
360
361         /*
362          * read the config file
363          */
364         if (filepresent(file)) {
365                 set_current_keywords(&conf->keywords);
366                 if (init_data(file, init_keywords)) {
367                         condlog(0, "error parsing config file");
368                         goto out;
369                 }
370         }
371         
372         /*
373          * fill the voids left in the config file
374          */
375         if (conf->hwtable == NULL) {
376                 conf->hwtable = vector_alloc();
377                 
378                 if (!conf->hwtable)
379                         goto out;
380                 
381         }
382         if (setup_default_hwtable(conf->hwtable))
383                 goto out;
384
385         if (conf->blist_devnode == NULL) {
386                 conf->blist_devnode = vector_alloc();
387                 
388                 if (!conf->blist_devnode)
389                         goto out;
390         }
391         if (conf->blist_wwid == NULL) {
392                 conf->blist_wwid = vector_alloc();
393                 
394                 if (!conf->blist_wwid)
395                         goto out;
396         }
397         if (conf->blist_device == NULL) {
398                 conf->blist_device = vector_alloc();
399                 
400                 if (!conf->blist_device)
401                         goto out;
402         }
403         if (setup_default_blist(conf))
404                 goto out;
405
406         if (conf->mptable == NULL) {
407                 conf->mptable = vector_alloc();
408
409                 if (!conf->mptable)
410                         goto out;
411         }
412         if (conf->selector == NULL)
413                 conf->selector = set_default(DEFAULT_SELECTOR);
414
415         if (conf->udev_dir == NULL)
416                 conf->udev_dir = set_default(DEFAULT_UDEVDIR);
417
418         if (conf->getuid == NULL)
419                 conf->getuid = set_default(DEFAULT_GETUID);
420
421         if (conf->features == NULL)
422                 conf->features = set_default(DEFAULT_FEATURES);
423
424         if (conf->hwhandler == NULL)
425                 conf->hwhandler = set_default(DEFAULT_HWHANDLER);
426
427         if (!conf->selector  || !conf->udev_dir         ||
428             !conf->getuid    || !conf->features ||
429             !conf->hwhandler)
430                 goto out;
431
432         if (!conf->checker)
433                 conf->checker = checker_lookup(DEFAULT_CHECKER);
434
435         return 0;
436 out:
437         free_config(conf);
438         return 1;
439 }
440