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