libmultipath: fix wrong output of "multipath -t"
[multipath-tools/.git] / libmultipath / dict.c
1 /*
2  * Based on Alexandre Cassen template for keepalived
3  * Copyright (c) 2004, 2005, 2006  Christophe Varoqui
4  * Copyright (c) 2005 Benjamin Marzinski, Redhat
5  * Copyright (c) 2005 Kiyoshi Ueda, NEC
6  */
7 #include <sys/types.h>
8 #include <pwd.h>
9 #include <string.h>
10 #include "checkers.h"
11 #include "vector.h"
12 #include "hwtable.h"
13 #include "structs.h"
14 #include "parser.h"
15 #include "config.h"
16 #include "debug.h"
17 #include "memory.h"
18 #include "pgpolicies.h"
19 #include "blacklist.h"
20 #include "defaults.h"
21 #include "prio.h"
22 #include "util.h"
23 #include <errno.h>
24 #include <inttypes.h>
25 #include "mpath_cmd.h"
26
27 static int
28 set_int(vector strvec, void *ptr)
29 {
30         int *int_ptr = (int *)ptr;
31         char * buff;
32
33         buff = VECTOR_SLOT(strvec, 1);
34         *int_ptr = atoi(buff);
35
36         return 0;
37 }
38
39 static int
40 set_str(vector strvec, void *ptr)
41 {
42         char **str_ptr = (char **)ptr;
43
44         if (*str_ptr)
45                 FREE(*str_ptr);
46         *str_ptr = set_value(strvec);
47
48         if (!*str_ptr)
49                 return 1;
50
51         return 0;
52 }
53
54 static int
55 set_yes_no(vector strvec, void *ptr)
56 {
57         char * buff;
58         int *int_ptr = (int *)ptr;
59
60         buff = set_value(strvec);
61         if (!buff)
62                 return 1;
63
64         if (strcmp(buff, "yes") == 0 || strcmp(buff, "1") == 0)
65                 *int_ptr = YN_YES;
66         else
67                 *int_ptr = YN_NO;
68
69         FREE(buff);
70         return 0;
71 }
72
73 static int
74 set_yes_no_undef(vector strvec, void *ptr)
75 {
76         char * buff;
77         int *int_ptr = (int *)ptr;
78
79         buff = set_value(strvec);
80         if (!buff)
81                 return 1;
82
83         if (strcmp(buff, "no") == 0 || strcmp(buff, "0") == 0)
84                 *int_ptr = YNU_NO;
85         else if (strcmp(buff, "yes") == 0 || strcmp(buff, "1") == 0)
86                 *int_ptr = YNU_YES;
87         else
88                 *int_ptr = YNU_UNDEF;
89
90         FREE(buff);
91         return 0;
92 }
93
94 static int
95 print_int (char *buff, int len, long v)
96 {
97         return snprintf(buff, len, "%li", v);
98 }
99
100 static int
101 print_nonzero (char *buff, int len, long v)
102 {
103         return snprintf(buff, len, "%li", v);
104 }
105
106 static int
107 print_str (char *buff, int len, const char *ptr)
108 {
109         return snprintf(buff, len, "\"%s\"", ptr);
110 }
111
112 static int
113 print_yes_no (char *buff, int len, long v)
114 {
115         return snprintf(buff, len, "\"%s\"",
116                         (v == YN_NO)? "no" : "yes");
117 }
118
119 static int
120 print_yes_no_undef (char *buff, int len, long v)
121 {
122         return snprintf(buff, len, "\"%s\"",
123                         (v == YNU_NO)? "no" : "yes");
124 }
125
126 #define declare_def_handler(option, function)                           \
127 static int                                                              \
128 def_ ## option ## _handler (struct config *conf, vector strvec)         \
129 {                                                                       \
130         return function (strvec, &conf->option);                        \
131 }
132
133 #define declare_def_snprint(option, function)                           \
134 static int                                                              \
135 snprint_def_ ## option (struct config *conf, char * buff, int len,      \
136                         const void * data)                              \
137 {                                                                       \
138         return function (buff, len, conf->option);                      \
139 }
140
141 #define declare_def_snprint_defint(option, function, value)             \
142 static int                                                              \
143 snprint_def_ ## option (struct config *conf, char * buff, int len,      \
144                         const void * data)                              \
145 {                                                                       \
146         int i = value;                                                  \
147         if (!conf->option)                                              \
148                 return function (buff, len, i);                         \
149         return function (buff, len, conf->option);                      \
150 }
151
152 #define declare_def_snprint_defstr(option, function, value)             \
153 static int                                                              \
154 snprint_def_ ## option (struct config *conf, char * buff, int len,      \
155                         const void * data)                              \
156 {                                                                       \
157         static const char *s = value;                                   \
158         if (!conf->option)                                              \
159                 return function (buff, len, s);                         \
160         return function (buff, len, conf->option);                      \
161 }
162
163 #define declare_hw_handler(option, function)                            \
164 static int                                                              \
165 hw_ ## option ## _handler (struct config *conf, vector strvec)          \
166 {                                                                       \
167         struct hwentry * hwe = VECTOR_LAST_SLOT(conf->hwtable);         \
168         if (!hwe)                                                       \
169                 return 1;                                               \
170         return function (strvec, &hwe->option);                         \
171 }
172
173 #define declare_hw_snprint(option, function)                            \
174 static int                                                              \
175 snprint_hw_ ## option (struct config *conf, char * buff, int len,       \
176                        const void * data)                               \
177 {                                                                       \
178         const struct hwentry * hwe = (const struct hwentry *)data;      \
179         return function (buff, len, hwe->option);                       \
180 }
181
182 #define declare_ovr_handler(option, function)                           \
183 static int                                                              \
184 ovr_ ## option ## _handler (struct config *conf, vector strvec)         \
185 {                                                                       \
186         if (!conf->overrides)                                           \
187                 return 1;                                               \
188         return function (strvec, &conf->overrides->option);             \
189 }
190
191 #define declare_ovr_snprint(option, function)                           \
192 static int                                                              \
193 snprint_ovr_ ## option (struct config *conf, char * buff, int len,      \
194                         const void * data)                              \
195 {                                                                       \
196         return function (buff, len, conf->overrides->option);           \
197 }
198
199 #define declare_mp_handler(option, function)                            \
200 static int                                                              \
201 mp_ ## option ## _handler (struct config *conf, vector strvec)          \
202 {                                                                       \
203         struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);         \
204         if (!mpe)                                                       \
205                 return 1;                                               \
206         return function (strvec, &mpe->option);                         \
207 }
208
209 #define declare_mp_snprint(option, function)                            \
210 static int                                                              \
211 snprint_mp_ ## option (struct config *conf, char * buff, int len,       \
212                        const void * data)                               \
213 {                                                                       \
214         const struct mpentry * mpe = (const struct mpentry *)data;      \
215         return function (buff, len, mpe->option);                       \
216 }
217
218 declare_def_handler(checkint, set_int)
219 declare_def_snprint(checkint, print_int)
220
221 declare_def_handler(max_checkint, set_int)
222 declare_def_snprint(max_checkint, print_int)
223
224 declare_def_handler(verbosity, set_int)
225 declare_def_snprint(verbosity, print_int)
226
227 declare_def_handler(reassign_maps, set_yes_no)
228 declare_def_snprint(reassign_maps, print_yes_no)
229
230 declare_def_handler(multipath_dir, set_str)
231 declare_def_snprint(multipath_dir, print_str)
232
233 declare_def_handler(partition_delim, set_str)
234 declare_def_snprint(partition_delim, print_str)
235
236 declare_def_handler(find_multipaths, set_yes_no)
237 declare_def_snprint(find_multipaths, print_yes_no)
238
239 declare_def_handler(selector, set_str)
240 declare_def_snprint_defstr(selector, print_str, DEFAULT_SELECTOR)
241 declare_hw_handler(selector, set_str)
242 declare_hw_snprint(selector, print_str)
243 declare_ovr_handler(selector, set_str)
244 declare_ovr_snprint(selector, print_str)
245 declare_mp_handler(selector, set_str)
246 declare_mp_snprint(selector, print_str)
247
248 declare_def_handler(uid_attrs, set_str)
249 declare_def_snprint(uid_attrs, print_str)
250 declare_def_handler(uid_attribute, set_str)
251 declare_def_snprint_defstr(uid_attribute, print_str, DEFAULT_UID_ATTRIBUTE)
252 declare_ovr_handler(uid_attribute, set_str)
253 declare_ovr_snprint(uid_attribute, print_str)
254 declare_hw_handler(uid_attribute, set_str)
255 declare_hw_snprint(uid_attribute, print_str)
256
257 declare_def_handler(getuid, set_str)
258 declare_def_snprint(getuid, print_str)
259 declare_ovr_handler(getuid, set_str)
260 declare_ovr_snprint(getuid, print_str)
261 declare_hw_handler(getuid, set_str)
262 declare_hw_snprint(getuid, print_str)
263
264 declare_def_handler(prio_name, set_str)
265 declare_def_snprint_defstr(prio_name, print_str, DEFAULT_PRIO)
266 declare_ovr_handler(prio_name, set_str)
267 declare_ovr_snprint(prio_name, print_str)
268 declare_hw_handler(prio_name, set_str)
269 declare_hw_snprint(prio_name, print_str)
270 declare_mp_handler(prio_name, set_str)
271 declare_mp_snprint(prio_name, print_str)
272
273 declare_def_handler(alias_prefix, set_str)
274 declare_def_snprint_defstr(alias_prefix, print_str, DEFAULT_ALIAS_PREFIX)
275 declare_ovr_handler(alias_prefix, set_str)
276 declare_ovr_snprint(alias_prefix, print_str)
277 declare_hw_handler(alias_prefix, set_str)
278 declare_hw_snprint(alias_prefix, print_str)
279
280 declare_def_handler(prio_args, set_str)
281 declare_def_snprint_defstr(prio_args, print_str, DEFAULT_PRIO_ARGS)
282 declare_ovr_handler(prio_args, set_str)
283 declare_ovr_snprint(prio_args, print_str)
284 declare_hw_handler(prio_args, set_str)
285 declare_hw_snprint(prio_args, print_str)
286 declare_mp_handler(prio_args, set_str)
287 declare_mp_snprint(prio_args, print_str)
288
289 declare_def_handler(features, set_str)
290 declare_def_snprint_defstr(features, print_str, DEFAULT_FEATURES)
291 declare_ovr_handler(features, set_str)
292 declare_ovr_snprint(features, print_str)
293 declare_hw_handler(features, set_str)
294 declare_hw_snprint(features, print_str)
295 declare_mp_handler(features, set_str)
296 declare_mp_snprint(features, print_str)
297
298 declare_def_handler(checker_name, set_str)
299 declare_def_snprint_defstr(checker_name, print_str, DEFAULT_CHECKER)
300 declare_ovr_handler(checker_name, set_str)
301 declare_ovr_snprint(checker_name, print_str)
302 declare_hw_handler(checker_name, set_str)
303 declare_hw_snprint(checker_name, print_str)
304
305 declare_def_handler(minio, set_int)
306 declare_def_snprint_defint(minio, print_int, DEFAULT_MINIO)
307 declare_ovr_handler(minio, set_int)
308 declare_ovr_snprint(minio, print_nonzero)
309 declare_hw_handler(minio, set_int)
310 declare_hw_snprint(minio, print_nonzero)
311 declare_mp_handler(minio, set_int)
312 declare_mp_snprint(minio, print_nonzero)
313
314 declare_def_handler(minio_rq, set_int)
315 declare_def_snprint_defint(minio_rq, print_int, DEFAULT_MINIO_RQ)
316 declare_ovr_handler(minio_rq, set_int)
317 declare_ovr_snprint(minio_rq, print_nonzero)
318 declare_hw_handler(minio_rq, set_int)
319 declare_hw_snprint(minio_rq, print_nonzero)
320 declare_mp_handler(minio_rq, set_int)
321 declare_mp_snprint(minio_rq, print_nonzero)
322
323 declare_def_handler(queue_without_daemon, set_yes_no)
324 static int
325 snprint_def_queue_without_daemon (struct config *conf,
326                                   char * buff, int len, const void * data)
327 {
328         switch (conf->queue_without_daemon) {
329         case QUE_NO_DAEMON_OFF:
330                 return snprintf(buff, len, "\"no\"");
331         case QUE_NO_DAEMON_ON:
332                 return snprintf(buff, len, "\"yes\"");
333         case QUE_NO_DAEMON_FORCE:
334                 return snprintf(buff, len, "\"forced\"");
335         }
336         return 0;
337 }
338
339 declare_def_handler(checker_timeout, set_int)
340 declare_def_snprint(checker_timeout, print_nonzero)
341
342 declare_def_handler(flush_on_last_del, set_yes_no_undef)
343 declare_def_snprint_defint(flush_on_last_del, print_yes_no_undef, DEFAULT_FLUSH)
344 declare_ovr_handler(flush_on_last_del, set_yes_no_undef)
345 declare_ovr_snprint(flush_on_last_del, print_yes_no_undef)
346 declare_hw_handler(flush_on_last_del, set_yes_no_undef)
347 declare_hw_snprint(flush_on_last_del, print_yes_no_undef)
348 declare_mp_handler(flush_on_last_del, set_yes_no_undef)
349 declare_mp_snprint(flush_on_last_del, print_yes_no_undef)
350
351 declare_def_handler(user_friendly_names, set_yes_no_undef)
352 declare_def_snprint_defint(user_friendly_names, print_yes_no_undef,
353                            DEFAULT_USER_FRIENDLY_NAMES)
354 declare_ovr_handler(user_friendly_names, set_yes_no_undef)
355 declare_ovr_snprint(user_friendly_names, print_yes_no_undef)
356 declare_hw_handler(user_friendly_names, set_yes_no_undef)
357 declare_hw_snprint(user_friendly_names, print_yes_no_undef)
358 declare_mp_handler(user_friendly_names, set_yes_no_undef)
359 declare_mp_snprint(user_friendly_names, print_yes_no_undef)
360
361 declare_def_handler(bindings_file, set_str)
362 declare_def_snprint(bindings_file, print_str)
363
364 declare_def_handler(wwids_file, set_str)
365 declare_def_snprint(wwids_file, print_str)
366
367 declare_def_handler(prkeys_file, set_str)
368 declare_def_snprint(prkeys_file, print_str)
369
370 declare_def_handler(retain_hwhandler, set_yes_no_undef)
371 declare_def_snprint_defint(retain_hwhandler, print_yes_no_undef,
372                            DEFAULT_RETAIN_HWHANDLER)
373 declare_ovr_handler(retain_hwhandler, set_yes_no_undef)
374 declare_ovr_snprint(retain_hwhandler, print_yes_no_undef)
375 declare_hw_handler(retain_hwhandler, set_yes_no_undef)
376 declare_hw_snprint(retain_hwhandler, print_yes_no_undef)
377
378 declare_def_handler(detect_prio, set_yes_no_undef)
379 declare_def_snprint_defint(detect_prio, print_yes_no_undef,
380                            DEFAULT_DETECT_PRIO)
381 declare_ovr_handler(detect_prio, set_yes_no_undef)
382 declare_ovr_snprint(detect_prio, print_yes_no_undef)
383 declare_hw_handler(detect_prio, set_yes_no_undef)
384 declare_hw_snprint(detect_prio, print_yes_no_undef)
385
386 declare_def_handler(detect_checker, set_yes_no_undef)
387 declare_def_snprint_defint(detect_checker, print_yes_no_undef,
388                            DEFAULT_DETECT_CHECKER)
389 declare_ovr_handler(detect_checker, set_yes_no_undef)
390 declare_ovr_snprint(detect_checker, print_yes_no_undef)
391 declare_hw_handler(detect_checker, set_yes_no_undef)
392 declare_hw_snprint(detect_checker, print_yes_no_undef)
393
394 declare_def_handler(force_sync, set_yes_no)
395 declare_def_snprint(force_sync, print_yes_no)
396
397 declare_def_handler(deferred_remove, set_yes_no_undef)
398 declare_def_snprint_defint(deferred_remove, print_yes_no_undef,
399                            DEFAULT_DEFERRED_REMOVE)
400 declare_ovr_handler(deferred_remove, set_yes_no_undef)
401 declare_ovr_snprint(deferred_remove, print_yes_no_undef)
402 declare_hw_handler(deferred_remove, set_yes_no_undef)
403 declare_hw_snprint(deferred_remove, print_yes_no_undef)
404 declare_mp_handler(deferred_remove, set_yes_no_undef)
405 declare_mp_snprint(deferred_remove, print_yes_no_undef)
406
407 declare_def_handler(retrigger_tries, set_int)
408 declare_def_snprint(retrigger_tries, print_int)
409
410 declare_def_handler(retrigger_delay, set_int)
411 declare_def_snprint(retrigger_delay, print_int)
412
413 declare_def_handler(uev_wait_timeout, set_int)
414 declare_def_snprint(uev_wait_timeout, print_int)
415
416 declare_def_handler(strict_timing, set_yes_no)
417 declare_def_snprint(strict_timing, print_yes_no)
418
419 declare_def_handler(skip_kpartx, set_yes_no_undef)
420 declare_def_snprint_defint(skip_kpartx, print_yes_no_undef,
421                            DEFAULT_SKIP_KPARTX)
422 declare_ovr_handler(skip_kpartx, set_yes_no_undef)
423 declare_ovr_snprint(skip_kpartx, print_yes_no_undef)
424 declare_hw_handler(skip_kpartx, set_yes_no_undef)
425 declare_hw_snprint(skip_kpartx, print_yes_no_undef)
426 declare_mp_handler(skip_kpartx, set_yes_no_undef)
427 declare_mp_snprint(skip_kpartx, print_yes_no_undef)
428
429 declare_def_handler(disable_changed_wwids, set_yes_no)
430 declare_def_snprint(disable_changed_wwids, print_yes_no)
431
432 declare_def_handler(remove_retries, set_int)
433 declare_def_snprint(remove_retries, print_int)
434
435 declare_def_handler(max_sectors_kb, set_int)
436 declare_def_snprint(max_sectors_kb, print_nonzero)
437 declare_ovr_handler(max_sectors_kb, set_int)
438 declare_ovr_snprint(max_sectors_kb, print_nonzero)
439 declare_hw_handler(max_sectors_kb, set_int)
440 declare_hw_snprint(max_sectors_kb, print_nonzero)
441 declare_mp_handler(max_sectors_kb, set_int)
442 declare_mp_snprint(max_sectors_kb, print_nonzero)
443
444 static int
445 def_config_dir_handler(struct config *conf, vector strvec)
446 {
447         /* this is only valid in the main config file */
448         if (conf->processed_main_config)
449                 return 0;
450         return set_str(strvec, &conf->config_dir);
451 }
452 declare_def_snprint(config_dir, print_str)
453
454 #define declare_def_attr_handler(option, function)                      \
455 static int                                                              \
456 def_ ## option ## _handler (struct config *conf, vector strvec)         \
457 {                                                                       \
458         return function (strvec, &conf->option, &conf->attribute_flags);\
459 }
460
461 #define declare_def_attr_snprint(option, function)                      \
462 static int                                                              \
463 snprint_def_ ## option (struct config *conf, char * buff, int len,      \
464                         const void * data)                              \
465 {                                                                       \
466         return function (buff, len, conf->option,                       \
467                          conf->attribute_flags);                        \
468 }
469
470 #define declare_mp_attr_handler(option, function)                       \
471 static int                                                              \
472 mp_ ## option ## _handler (struct config *conf, vector strvec)          \
473 {                                                                       \
474         struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);         \
475         if (!mpe)                                                       \
476                 return 1;                                               \
477         return function (strvec, &mpe->option, &mpe->attribute_flags);  \
478 }
479
480 #define declare_mp_attr_snprint(option, function)                       \
481 static int                                                              \
482 snprint_mp_ ## option (struct config *conf, char * buff, int len,       \
483                        const void * data)                               \
484 {                                                                       \
485         const struct mpentry * mpe = (const struct mpentry *)data;      \
486         return function (buff, len, mpe->option,                        \
487                          mpe->attribute_flags);                         \
488 }
489
490 static int
491 set_mode(vector strvec, void *ptr, int *flags)
492 {
493         mode_t mode;
494         mode_t *mode_ptr = (mode_t *)ptr;
495         char *buff;
496
497         buff = set_value(strvec);
498
499         if (!buff)
500                 return 1;
501
502         if (sscanf(buff, "%o", &mode) == 1 && mode <= 0777) {
503                 *flags |= (1 << ATTR_MODE);
504                 *mode_ptr = mode;
505         }
506
507         FREE(buff);
508         return 0;
509 }
510
511 static int
512 set_uid(vector strvec, void *ptr, int *flags)
513 {
514         uid_t uid;
515         uid_t *uid_ptr = (uid_t *)ptr;
516         char *buff;
517         char passwd_buf[1024];
518         struct passwd info, *found;
519
520         buff = set_value(strvec);
521         if (!buff)
522                 return 1;
523         if (getpwnam_r(buff, &info, passwd_buf, 1024, &found) == 0 && found) {
524                 *flags |= (1 << ATTR_UID);
525                 *uid_ptr = info.pw_uid;
526         }
527         else if (sscanf(buff, "%u", &uid) == 1){
528                 *flags |= (1 << ATTR_UID);
529                 *uid_ptr = uid;
530         }
531
532         FREE(buff);
533         return 0;
534 }
535
536 static int
537 set_gid(vector strvec, void *ptr, int *flags)
538 {
539         gid_t gid;
540         gid_t *gid_ptr = (gid_t *)ptr;
541         char *buff;
542         char passwd_buf[1024];
543         struct passwd info, *found;
544
545         buff = set_value(strvec);
546         if (!buff)
547                 return 1;
548
549         if (getpwnam_r(buff, &info, passwd_buf, 1024, &found) == 0 && found) {
550                 *flags |= (1 << ATTR_GID);
551                 *gid_ptr = info.pw_gid;
552         }
553         else if (sscanf(buff, "%u", &gid) == 1){
554                 *flags |= (1 << ATTR_GID);
555                 *gid_ptr = gid;
556         }
557         FREE(buff);
558         return 0;
559 }
560
561 static int
562 print_mode(char * buff, int len, long v, int flags)
563 {
564         mode_t mode = (mode_t)v;
565         if ((flags & (1 << ATTR_MODE)) == 0)
566                 return 0;
567         return snprintf(buff, len, "0%o", mode);
568 }
569
570 static int
571 print_uid(char * buff, int len, long v, int flags)
572 {
573         uid_t uid = (uid_t)v;
574         if ((flags & (1 << ATTR_UID)) == 0)
575                 return 0;
576         return snprintf(buff, len, "0%o", uid);
577 }
578
579 static int
580 print_gid(char * buff, int len, long v, int flags)
581 {
582         gid_t gid = (gid_t)v;
583         if ((flags & (1 << ATTR_GID)) == 0)
584                 return 0;
585         return snprintf(buff, len, "0%o", gid);
586 }
587
588 declare_def_attr_handler(mode, set_mode)
589 declare_def_attr_snprint(mode, print_mode)
590 declare_mp_attr_handler(mode, set_mode)
591 declare_mp_attr_snprint(mode, print_mode)
592
593 declare_def_attr_handler(uid, set_uid)
594 declare_def_attr_snprint(uid, print_uid)
595 declare_mp_attr_handler(uid, set_uid)
596 declare_mp_attr_snprint(uid, print_uid)
597
598 declare_def_attr_handler(gid, set_gid)
599 declare_def_attr_snprint(gid, print_gid)
600 declare_mp_attr_handler(gid, set_gid)
601 declare_mp_attr_snprint(gid, print_gid)
602
603 static int
604 set_fast_io_fail(vector strvec, void *ptr)
605 {
606         char * buff;
607         int *int_ptr = (int *)ptr;
608
609         buff = set_value(strvec);
610         if (!buff)
611                 return 1;
612
613         if (strcmp(buff, "off") == 0)
614                 *int_ptr = MP_FAST_IO_FAIL_OFF;
615         else if (sscanf(buff, "%d", int_ptr) != 1 ||
616                  *int_ptr < MP_FAST_IO_FAIL_ZERO)
617                 *int_ptr = MP_FAST_IO_FAIL_UNSET;
618         else if (*int_ptr == 0)
619                 *int_ptr = MP_FAST_IO_FAIL_ZERO;
620
621         FREE(buff);
622         return 0;
623 }
624
625 int
626 print_fast_io_fail(char * buff, int len, long v)
627 {
628         if (v == MP_FAST_IO_FAIL_UNSET)
629                 return 0;
630         if (v == MP_FAST_IO_FAIL_OFF)
631                 return snprintf(buff, len, "\"off\"");
632         if (v == MP_FAST_IO_FAIL_ZERO)
633                 return snprintf(buff, len, "0");
634         return snprintf(buff, len, "%ld", v);
635 }
636
637 declare_def_handler(fast_io_fail, set_fast_io_fail)
638 declare_def_snprint_defint(fast_io_fail, print_fast_io_fail,
639                            DEFAULT_FAST_IO_FAIL)
640 declare_ovr_handler(fast_io_fail, set_fast_io_fail)
641 declare_ovr_snprint(fast_io_fail, print_fast_io_fail)
642 declare_hw_handler(fast_io_fail, set_fast_io_fail)
643 declare_hw_snprint(fast_io_fail, print_fast_io_fail)
644
645 static int
646 set_dev_loss(vector strvec, void *ptr)
647 {
648         char * buff;
649         unsigned int *uint_ptr = (unsigned int *)ptr;
650
651         buff = set_value(strvec);
652         if (!buff)
653                 return 1;
654
655         if (!strcmp(buff, "infinity"))
656                 *uint_ptr = MAX_DEV_LOSS_TMO;
657         else if (sscanf(buff, "%u", uint_ptr) != 1)
658                 *uint_ptr = 0;
659
660         FREE(buff);
661         return 0;
662 }
663
664 int
665 print_dev_loss(char * buff, int len, unsigned long v)
666 {
667         if (v >= MAX_DEV_LOSS_TMO)
668                 return snprintf(buff, len, "\"infinity\"");
669         return snprintf(buff, len, "%lu", v);
670 }
671
672 declare_def_handler(dev_loss, set_dev_loss)
673 declare_def_snprint(dev_loss, print_dev_loss)
674 declare_ovr_handler(dev_loss, set_dev_loss)
675 declare_ovr_snprint(dev_loss, print_dev_loss)
676 declare_hw_handler(dev_loss, set_dev_loss)
677 declare_hw_snprint(dev_loss, print_dev_loss)
678
679 static int
680 set_pgpolicy(vector strvec, void *ptr)
681 {
682         char * buff;
683         int *int_ptr = (int *)ptr;
684
685         buff = set_value(strvec);
686         if (!buff)
687                 return 1;
688
689         *int_ptr = get_pgpolicy_id(buff);
690         FREE(buff);
691
692         return 0;
693 }
694
695 int
696 print_pgpolicy(char * buff, int len, long pgpolicy)
697 {
698         char str[POLICY_NAME_SIZE];
699
700         if (!pgpolicy)
701                 return 0;
702
703         get_pgpolicy_name(str, POLICY_NAME_SIZE, pgpolicy);
704
705         return snprintf(buff, len, "\"%s\"", str);
706 }
707
708 declare_def_handler(pgpolicy, set_pgpolicy)
709 declare_def_snprint_defint(pgpolicy, print_pgpolicy, DEFAULT_PGPOLICY)
710 declare_ovr_handler(pgpolicy, set_pgpolicy)
711 declare_ovr_snprint(pgpolicy, print_pgpolicy)
712 declare_hw_handler(pgpolicy, set_pgpolicy)
713 declare_hw_snprint(pgpolicy, print_pgpolicy)
714 declare_mp_handler(pgpolicy, set_pgpolicy)
715 declare_mp_snprint(pgpolicy, print_pgpolicy)
716
717 int
718 get_sys_max_fds(int *max_fds)
719 {
720         FILE *file;
721         int nr_open;
722         int ret = 1;
723
724         file = fopen("/proc/sys/fs/nr_open", "r");
725         if (!file) {
726                 fprintf(stderr, "Cannot open /proc/sys/fs/nr_open : %s\n",
727                         strerror(errno));
728                 return 1;
729         }
730         if (fscanf(file, "%d", &nr_open) != 1) {
731                 fprintf(stderr, "Cannot read max open fds from /proc/sys/fs/nr_open");
732                 if (ferror(file))
733                         fprintf(stderr, " : %s\n", strerror(errno));
734                 else
735                         fprintf(stderr, "\n");
736         } else {
737                 *max_fds = nr_open;
738                 ret = 0;
739         }
740         fclose(file);
741         return ret;
742 }
743
744
745 static int
746 max_fds_handler(struct config *conf, vector strvec)
747 {
748         char * buff;
749         int r = 0, max_fds;
750
751         buff = set_value(strvec);
752
753         if (!buff)
754                 return 1;
755
756         r = get_sys_max_fds(&max_fds);
757         if (r) {
758                 /* Assume safe limit */
759                 max_fds = 4096;
760         }
761         if (strlen(buff) == 3 &&
762             !strcmp(buff, "max"))
763                 conf->max_fds = max_fds;
764         else
765                 conf->max_fds = atoi(buff);
766
767         if (conf->max_fds > max_fds)
768                 conf->max_fds = max_fds;
769
770         FREE(buff);
771
772         return r;
773 }
774
775 static int
776 snprint_max_fds (struct config *conf, char * buff, int len, const void * data)
777 {
778         int r = 0, max_fds;
779
780         if (!conf->max_fds)
781                 return 0;
782
783         r = get_sys_max_fds(&max_fds);
784         if (!r && conf->max_fds >= max_fds)
785                 return snprintf(buff, len, "\"max\"");
786         else
787                 return snprintf(buff, len, "%d", conf->max_fds);
788 }
789
790 static int
791 set_rr_weight(vector strvec, void *ptr)
792 {
793         int *int_ptr = (int *)ptr;
794         char * buff;
795
796         buff = set_value(strvec);
797
798         if (!buff)
799                 return 1;
800
801         if (!strcmp(buff, "priorities"))
802                 *int_ptr = RR_WEIGHT_PRIO;
803
804         if (!strcmp(buff, "uniform"))
805                 *int_ptr = RR_WEIGHT_NONE;
806
807         FREE(buff);
808
809         return 0;
810 }
811
812 int
813 print_rr_weight (char * buff, int len, long v)
814 {
815         if (!v)
816                 return 0;
817         if (v == RR_WEIGHT_PRIO)
818                 return snprintf(buff, len, "\"priorities\"");
819         if (v == RR_WEIGHT_NONE)
820                 return snprintf(buff, len, "\"uniform\"");
821
822         return 0;
823 }
824
825 declare_def_handler(rr_weight, set_rr_weight)
826 declare_def_snprint_defint(rr_weight, print_rr_weight, DEFAULT_RR_WEIGHT)
827 declare_ovr_handler(rr_weight, set_rr_weight)
828 declare_ovr_snprint(rr_weight, print_rr_weight)
829 declare_hw_handler(rr_weight, set_rr_weight)
830 declare_hw_snprint(rr_weight, print_rr_weight)
831 declare_mp_handler(rr_weight, set_rr_weight)
832 declare_mp_snprint(rr_weight, print_rr_weight)
833
834 static int
835 set_pgfailback(vector strvec, void *ptr)
836 {
837         int *int_ptr = (int *)ptr;
838         char * buff;
839
840         buff = set_value(strvec);
841         if (!buff)
842                 return 1;
843
844         if (strlen(buff) == 6 && !strcmp(buff, "manual"))
845                 *int_ptr = -FAILBACK_MANUAL;
846         else if (strlen(buff) == 9 && !strcmp(buff, "immediate"))
847                 *int_ptr = -FAILBACK_IMMEDIATE;
848         else if (strlen(buff) == 10 && !strcmp(buff, "followover"))
849                 *int_ptr = -FAILBACK_FOLLOWOVER;
850         else
851                 *int_ptr = atoi(buff);
852
853         FREE(buff);
854
855         return 0;
856 }
857
858 int
859 print_pgfailback (char * buff, int len, long v)
860 {
861         switch(v) {
862         case  FAILBACK_UNDEF:
863                 return 0;
864         case -FAILBACK_MANUAL:
865                 return snprintf(buff, len, "\"manual\"");
866         case -FAILBACK_IMMEDIATE:
867                 return snprintf(buff, len, "\"immediate\"");
868         case -FAILBACK_FOLLOWOVER:
869                 return snprintf(buff, len, "\"followover\"");
870         default:
871                 return snprintf(buff, len, "%li", v);
872         }
873 }
874
875 declare_def_handler(pgfailback, set_pgfailback)
876 declare_def_snprint_defint(pgfailback, print_pgfailback, DEFAULT_FAILBACK)
877 declare_ovr_handler(pgfailback, set_pgfailback)
878 declare_ovr_snprint(pgfailback, print_pgfailback)
879 declare_hw_handler(pgfailback, set_pgfailback)
880 declare_hw_snprint(pgfailback, print_pgfailback)
881 declare_mp_handler(pgfailback, set_pgfailback)
882 declare_mp_snprint(pgfailback, print_pgfailback)
883
884 static int
885 set_no_path_retry(vector strvec, void *ptr)
886 {
887         int *int_ptr = (int *)ptr;
888         char * buff;
889
890         buff = set_value(strvec);
891         if (!buff)
892                 return 1;
893
894         if (!strcmp(buff, "fail") || !strcmp(buff, "0"))
895                 *int_ptr = NO_PATH_RETRY_FAIL;
896         else if (!strcmp(buff, "queue"))
897                 *int_ptr = NO_PATH_RETRY_QUEUE;
898         else if ((*int_ptr = atoi(buff)) < 1)
899                 *int_ptr = NO_PATH_RETRY_UNDEF;
900
901         FREE(buff);
902         return 0;
903 }
904
905 int
906 print_no_path_retry(char * buff, int len, long v)
907 {
908         switch(v) {
909         case NO_PATH_RETRY_UNDEF:
910                 return 0;
911         case NO_PATH_RETRY_FAIL:
912                 return snprintf(buff, len, "\"fail\"");
913         case NO_PATH_RETRY_QUEUE:
914                 return snprintf(buff, len, "\"queue\"");
915         default:
916                 return snprintf(buff, len, "%li", v);
917         }
918 }
919
920 declare_def_handler(no_path_retry, set_no_path_retry)
921 declare_def_snprint(no_path_retry, print_no_path_retry)
922 declare_ovr_handler(no_path_retry, set_no_path_retry)
923 declare_ovr_snprint(no_path_retry, print_no_path_retry)
924 declare_hw_handler(no_path_retry, set_no_path_retry)
925 declare_hw_snprint(no_path_retry, print_no_path_retry)
926 declare_mp_handler(no_path_retry, set_no_path_retry)
927 declare_mp_snprint(no_path_retry, print_no_path_retry)
928
929 static int
930 def_log_checker_err_handler(struct config *conf, vector strvec)
931 {
932         char * buff;
933
934         buff = set_value(strvec);
935
936         if (!buff)
937                 return 1;
938
939         if (strlen(buff) == 4 && !strcmp(buff, "once"))
940                 conf->log_checker_err = LOG_CHKR_ERR_ONCE;
941         else if (strlen(buff) == 6 && !strcmp(buff, "always"))
942                 conf->log_checker_err = LOG_CHKR_ERR_ALWAYS;
943
944         free(buff);
945         return 0;
946 }
947
948 static int
949 snprint_def_log_checker_err (struct config *conf, char * buff, int len,
950                              const void * data)
951 {
952         if (conf->log_checker_err == LOG_CHKR_ERR_ONCE)
953                 return snprintf(buff, len, "once");
954         return snprintf(buff, len, "always");
955 }
956
957 static int
958 set_reservation_key(vector strvec, struct be64 *be64_ptr, int *source_ptr)
959 {
960         char *buff;
961         uint64_t prkey;
962
963         buff = set_value(strvec);
964         if (!buff)
965                 return 1;
966
967         if (strcmp(buff, "file") == 0) {
968                 *source_ptr = PRKEY_SOURCE_FILE;
969                 put_be64(*be64_ptr, 0);
970                 FREE(buff);
971                 return 0;
972         }
973
974         if (parse_prkey(buff, &prkey) != 0) {
975                 FREE(buff);
976                 return 1;
977         }
978         *source_ptr = PRKEY_SOURCE_CONF;
979         put_be64(*be64_ptr, prkey);
980         FREE(buff);
981         return 0;
982 }
983
984 int
985 print_reservation_key(char * buff, int len, struct be64 key, int source)
986 {
987         if (source == PRKEY_SOURCE_NONE)
988                 return 0;
989         if (source == PRKEY_SOURCE_FILE)
990                 return snprintf(buff, len, "file");
991         return snprintf(buff, len, "0x%" PRIx64, get_be64(key));
992 }
993
994 static int
995 def_reservation_key_handler(struct config *conf, vector strvec)
996 {
997         return set_reservation_key(strvec, &conf->reservation_key,
998                                    &conf->prkey_source);
999 }
1000
1001 static int
1002 snprint_def_reservation_key (struct config *conf, char * buff, int len,
1003                              const void * data)
1004 {
1005         return print_reservation_key(buff, len, conf->reservation_key,
1006                                      conf->prkey_source);
1007 }
1008
1009 static int
1010 mp_reservation_key_handler(struct config *conf, vector strvec)
1011 {
1012         struct mpentry * mpe = VECTOR_LAST_SLOT(conf->mptable);
1013         if (!mpe)
1014                 return 1;
1015         return set_reservation_key(strvec, &mpe->reservation_key,
1016                                    &mpe->prkey_source);
1017 }
1018
1019 static int
1020 snprint_mp_reservation_key (struct config *conf, char * buff, int len,
1021                             const void * data)
1022 {
1023         const struct mpentry * mpe = (const struct mpentry *)data;
1024         return print_reservation_key(buff, len, mpe->reservation_key,
1025                                      mpe->prkey_source);
1026 }
1027
1028 static int
1029 set_off_int_undef(vector strvec, void *ptr)
1030 {
1031         int *int_ptr = (int *)ptr;
1032         char * buff;
1033
1034         buff = set_value(strvec);
1035         if (!buff)
1036                 return 1;
1037
1038         if (!strcmp(buff, "no") || !strcmp(buff, "0"))
1039                 *int_ptr = NU_NO;
1040         else if ((*int_ptr = atoi(buff)) < 1)
1041                 *int_ptr = NU_UNDEF;
1042
1043         FREE(buff);
1044         return 0;
1045 }
1046
1047 int
1048 print_off_int_undef(char * buff, int len, long v)
1049 {
1050         switch(v) {
1051         case NU_UNDEF:
1052                 return 0;
1053         case NU_NO:
1054                 return snprintf(buff, len, "\"no\"");
1055         default:
1056                 return snprintf(buff, len, "%li", v);
1057         }
1058 }
1059
1060 declare_def_handler(delay_watch_checks, set_off_int_undef)
1061 declare_def_snprint(delay_watch_checks, print_off_int_undef)
1062 declare_ovr_handler(delay_watch_checks, set_off_int_undef)
1063 declare_ovr_snprint(delay_watch_checks, print_off_int_undef)
1064 declare_hw_handler(delay_watch_checks, set_off_int_undef)
1065 declare_hw_snprint(delay_watch_checks, print_off_int_undef)
1066 declare_mp_handler(delay_watch_checks, set_off_int_undef)
1067 declare_mp_snprint(delay_watch_checks, print_off_int_undef)
1068 declare_def_handler(delay_wait_checks, set_off_int_undef)
1069 declare_def_snprint(delay_wait_checks, print_off_int_undef)
1070 declare_ovr_handler(delay_wait_checks, set_off_int_undef)
1071 declare_ovr_snprint(delay_wait_checks, print_off_int_undef)
1072 declare_hw_handler(delay_wait_checks, set_off_int_undef)
1073 declare_hw_snprint(delay_wait_checks, print_off_int_undef)
1074 declare_mp_handler(delay_wait_checks, set_off_int_undef)
1075 declare_mp_snprint(delay_wait_checks, print_off_int_undef)
1076 declare_def_handler(marginal_path_err_sample_time, set_off_int_undef)
1077 declare_def_snprint_defint(marginal_path_err_sample_time, print_off_int_undef,
1078                            DEFAULT_ERR_CHECKS)
1079 declare_ovr_handler(marginal_path_err_sample_time, set_off_int_undef)
1080 declare_ovr_snprint(marginal_path_err_sample_time, print_off_int_undef)
1081 declare_hw_handler(marginal_path_err_sample_time, set_off_int_undef)
1082 declare_hw_snprint(marginal_path_err_sample_time, print_off_int_undef)
1083 declare_mp_handler(marginal_path_err_sample_time, set_off_int_undef)
1084 declare_mp_snprint(marginal_path_err_sample_time, print_off_int_undef)
1085 declare_def_handler(marginal_path_err_rate_threshold, set_off_int_undef)
1086 declare_def_snprint_defint(marginal_path_err_rate_threshold, print_off_int_undef,
1087                            DEFAULT_ERR_CHECKS)
1088 declare_ovr_handler(marginal_path_err_rate_threshold, set_off_int_undef)
1089 declare_ovr_snprint(marginal_path_err_rate_threshold, print_off_int_undef)
1090 declare_hw_handler(marginal_path_err_rate_threshold, set_off_int_undef)
1091 declare_hw_snprint(marginal_path_err_rate_threshold, print_off_int_undef)
1092 declare_mp_handler(marginal_path_err_rate_threshold, set_off_int_undef)
1093 declare_mp_snprint(marginal_path_err_rate_threshold, print_off_int_undef)
1094 declare_def_handler(marginal_path_err_recheck_gap_time, set_off_int_undef)
1095 declare_def_snprint_defint(marginal_path_err_recheck_gap_time, print_off_int_undef,
1096                            DEFAULT_ERR_CHECKS)
1097 declare_ovr_handler(marginal_path_err_recheck_gap_time, set_off_int_undef)
1098 declare_ovr_snprint(marginal_path_err_recheck_gap_time, print_off_int_undef)
1099 declare_hw_handler(marginal_path_err_recheck_gap_time, set_off_int_undef)
1100 declare_hw_snprint(marginal_path_err_recheck_gap_time, print_off_int_undef)
1101 declare_mp_handler(marginal_path_err_recheck_gap_time, set_off_int_undef)
1102 declare_mp_snprint(marginal_path_err_recheck_gap_time, print_off_int_undef)
1103 declare_def_handler(marginal_path_double_failed_time, set_off_int_undef)
1104 declare_def_snprint_defint(marginal_path_double_failed_time, print_off_int_undef,
1105                            DEFAULT_ERR_CHECKS)
1106 declare_ovr_handler(marginal_path_double_failed_time, set_off_int_undef)
1107 declare_ovr_snprint(marginal_path_double_failed_time, print_off_int_undef)
1108 declare_hw_handler(marginal_path_double_failed_time, set_off_int_undef)
1109 declare_hw_snprint(marginal_path_double_failed_time, print_off_int_undef)
1110 declare_mp_handler(marginal_path_double_failed_time, set_off_int_undef)
1111 declare_mp_snprint(marginal_path_double_failed_time, print_off_int_undef)
1112
1113 declare_def_handler(ghost_delay, set_off_int_undef)
1114 declare_def_snprint(ghost_delay, print_off_int_undef)
1115 declare_ovr_handler(ghost_delay, set_off_int_undef)
1116 declare_ovr_snprint(ghost_delay, print_off_int_undef)
1117 declare_hw_handler(ghost_delay, set_off_int_undef)
1118 declare_hw_snprint(ghost_delay, print_off_int_undef)
1119 declare_mp_handler(ghost_delay, set_off_int_undef)
1120 declare_mp_snprint(ghost_delay, print_off_int_undef)
1121
1122
1123 static int
1124 def_uxsock_timeout_handler(struct config *conf, vector strvec)
1125 {
1126         unsigned int uxsock_timeout;
1127         char *buff;
1128
1129         buff = set_value(strvec);
1130         if (!buff)
1131                 return 1;
1132
1133         if (sscanf(buff, "%u", &uxsock_timeout) == 1 &&
1134             uxsock_timeout > DEFAULT_REPLY_TIMEOUT)
1135                 conf->uxsock_timeout = uxsock_timeout;
1136         else
1137                 conf->uxsock_timeout = DEFAULT_REPLY_TIMEOUT;
1138
1139         free(buff);
1140         return 0;
1141 }
1142
1143 /*
1144  * blacklist block handlers
1145  */
1146 static int
1147 blacklist_handler(struct config *conf, vector strvec)
1148 {
1149         if (!conf->blist_devnode)
1150                 conf->blist_devnode = vector_alloc();
1151         if (!conf->blist_wwid)
1152                 conf->blist_wwid = vector_alloc();
1153         if (!conf->blist_device)
1154                 conf->blist_device = vector_alloc();
1155         if (!conf->blist_property)
1156                 conf->blist_property = vector_alloc();
1157
1158         if (!conf->blist_devnode || !conf->blist_wwid ||
1159             !conf->blist_device || !conf->blist_property)
1160                 return 1;
1161
1162         return 0;
1163 }
1164
1165 static int
1166 blacklist_exceptions_handler(struct config *conf, vector strvec)
1167 {
1168         if (!conf->elist_devnode)
1169                 conf->elist_devnode = vector_alloc();
1170         if (!conf->elist_wwid)
1171                 conf->elist_wwid = vector_alloc();
1172         if (!conf->elist_device)
1173                 conf->elist_device = vector_alloc();
1174         if (!conf->elist_property)
1175                 conf->elist_property = vector_alloc();
1176
1177         if (!conf->elist_devnode || !conf->elist_wwid ||
1178             !conf->elist_device || !conf->elist_property)
1179                 return 1;
1180
1181         return 0;
1182 }
1183
1184 #define declare_ble_handler(option)                                     \
1185 static int                                                              \
1186 ble_ ## option ## _handler (struct config *conf, vector strvec)         \
1187 {                                                                       \
1188         char * buff;                                                    \
1189                                                                         \
1190         if (!conf->option)                                              \
1191                 return 1;                                               \
1192                                                                         \
1193         buff = set_value(strvec);                                       \
1194         if (!buff)                                                      \
1195                 return 1;                                               \
1196                                                                         \
1197         return store_ble(conf->option, buff, ORIGIN_CONFIG);            \
1198 }
1199
1200 #define declare_ble_device_handler(name, option, vend, prod)            \
1201 static int                                                              \
1202 ble_ ## option ## _ ## name ## _handler (struct config *conf, vector strvec) \
1203 {                                                                       \
1204         char * buff;                                                    \
1205                                                                         \
1206         if (!conf->option)                                              \
1207                 return 1;                                               \
1208                                                                         \
1209         buff = set_value(strvec);                                       \
1210         if (!buff)                                                      \
1211                 return 1;                                               \
1212                                                                         \
1213         return set_ble_device(conf->option, vend, prod, ORIGIN_CONFIG); \
1214 }
1215
1216 declare_ble_handler(blist_devnode)
1217 declare_ble_handler(elist_devnode)
1218 declare_ble_handler(blist_wwid)
1219 declare_ble_handler(elist_wwid)
1220 declare_ble_handler(blist_property)
1221 declare_ble_handler(elist_property)
1222
1223 static int
1224 snprint_def_uxsock_timeout(struct config *conf, char * buff, int len,
1225                            const void * data)
1226 {
1227         return snprintf(buff, len, "%u", conf->uxsock_timeout);
1228 }
1229
1230 static int
1231 snprint_ble_simple (struct config *conf, char * buff, int len,
1232                     const void * data)
1233 {
1234         const struct blentry * ble = (const struct blentry *)data;
1235
1236         return snprintf(buff, len, "\"%s\"", ble->str);
1237 }
1238
1239 static int
1240 ble_device_handler(struct config *conf, vector strvec)
1241 {
1242         return alloc_ble_device(conf->blist_device);
1243 }
1244
1245 static int
1246 ble_except_device_handler(struct config *conf, vector strvec)
1247 {
1248         return alloc_ble_device(conf->elist_device);
1249 }
1250
1251 declare_ble_device_handler(vendor, blist_device, buff, NULL)
1252 declare_ble_device_handler(vendor, elist_device, buff, NULL)
1253 declare_ble_device_handler(product, blist_device, NULL, buff)
1254 declare_ble_device_handler(product, elist_device, NULL, buff)
1255
1256 static int
1257 snprint_bled_vendor (struct config *conf, char * buff, int len,
1258                      const void * data)
1259 {
1260         const struct blentry_device * bled =
1261                 (const struct blentry_device *)data;
1262
1263         return snprintf(buff, len, "\"%s\"", bled->vendor);
1264 }
1265
1266 static int
1267 snprint_bled_product (struct config *conf, char * buff, int len,
1268                       const void * data)
1269 {
1270         const struct blentry_device * bled =
1271                 (const struct blentry_device *)data;
1272
1273         return snprintf(buff, len, "\"%s\"", bled->product);
1274 }
1275
1276 /*
1277  * devices block handlers
1278  */
1279 static int
1280 devices_handler(struct config *conf, vector strvec)
1281 {
1282         if (!conf->hwtable)
1283                 conf->hwtable = vector_alloc();
1284
1285         if (!conf->hwtable)
1286                 return 1;
1287
1288         return 0;
1289 }
1290
1291 static int
1292 device_handler(struct config *conf, vector strvec)
1293 {
1294         struct hwentry * hwe;
1295
1296         hwe = alloc_hwe();
1297
1298         if (!hwe)
1299                 return 1;
1300
1301         if (!vector_alloc_slot(conf->hwtable)) {
1302                 free_hwe(hwe);
1303                 return 1;
1304         }
1305         vector_set_slot(conf->hwtable, hwe);
1306
1307         return 0;
1308 }
1309
1310 declare_hw_handler(vendor, set_str)
1311 declare_hw_snprint(vendor, print_str)
1312
1313 declare_hw_handler(product, set_str)
1314 declare_hw_snprint(product, print_str)
1315
1316 declare_hw_handler(revision, set_str)
1317 declare_hw_snprint(revision, print_str)
1318
1319 declare_hw_handler(bl_product, set_str)
1320 declare_hw_snprint(bl_product, print_str)
1321
1322 declare_hw_handler(hwhandler, set_str)
1323 declare_hw_snprint(hwhandler, print_str)
1324
1325 /*
1326  * overrides handlers
1327  */
1328 static int
1329 overrides_handler(struct config *conf, vector strvec)
1330 {
1331         if (!conf->overrides)
1332                 conf->overrides = alloc_hwe();
1333
1334         if (!conf->overrides)
1335                 return 1;
1336
1337         return 0;
1338 }
1339
1340
1341
1342 /*
1343  * multipaths block handlers
1344  */
1345 static int
1346 multipaths_handler(struct config *conf, vector strvec)
1347 {
1348         if (!conf->mptable)
1349                 conf->mptable = vector_alloc();
1350
1351         if (!conf->mptable)
1352                 return 1;
1353
1354         return 0;
1355 }
1356
1357 static int
1358 multipath_handler(struct config *conf, vector strvec)
1359 {
1360         struct mpentry * mpe;
1361
1362         mpe = alloc_mpe();
1363
1364         if (!mpe)
1365                 return 1;
1366
1367         if (!vector_alloc_slot(conf->mptable)) {
1368                 free_mpe(mpe);
1369                 return 1;
1370         }
1371         vector_set_slot(conf->mptable, mpe);
1372
1373         return 0;
1374 }
1375
1376 declare_mp_handler(wwid, set_str)
1377 declare_mp_snprint(wwid, print_str)
1378
1379 declare_mp_handler(alias, set_str)
1380 declare_mp_snprint(alias, print_str)
1381
1382 /*
1383  * deprecated handlers
1384  */
1385
1386 static int
1387 deprecated_handler(struct config *conf, vector strvec)
1388 {
1389         char * buff;
1390
1391         buff = set_value(strvec);
1392
1393         if (!buff)
1394                 return 1;
1395
1396         FREE(buff);
1397         return 0;
1398 }
1399
1400 static int
1401 snprint_deprecated (struct config *conf, char * buff, int len,
1402                     const void * data)
1403 {
1404         return 0;
1405 }
1406
1407 #define __deprecated
1408
1409 /*
1410  * If you add or remove a keyword also update multipath/multipath.conf.5
1411  */
1412 void
1413 init_keywords(vector keywords)
1414 {
1415         install_keyword_root("defaults", NULL);
1416         install_keyword("verbosity", &def_verbosity_handler, &snprint_def_verbosity);
1417         install_keyword("polling_interval", &def_checkint_handler, &snprint_def_checkint);
1418         install_keyword("max_polling_interval", &def_max_checkint_handler, &snprint_def_max_checkint);
1419         install_keyword("reassign_maps", &def_reassign_maps_handler, &snprint_def_reassign_maps);
1420         install_keyword("multipath_dir", &def_multipath_dir_handler, &snprint_def_multipath_dir);
1421         install_keyword("path_selector", &def_selector_handler, &snprint_def_selector);
1422         install_keyword("path_grouping_policy", &def_pgpolicy_handler, &snprint_def_pgpolicy);
1423         install_keyword("uid_attrs", &def_uid_attrs_handler, &snprint_def_uid_attrs);
1424         install_keyword("uid_attribute", &def_uid_attribute_handler, &snprint_def_uid_attribute);
1425         install_keyword("getuid_callout", &def_getuid_handler, &snprint_def_getuid);
1426         install_keyword("prio", &def_prio_name_handler, &snprint_def_prio_name);
1427         install_keyword("prio_args", &def_prio_args_handler, &snprint_def_prio_args);
1428         install_keyword("features", &def_features_handler, &snprint_def_features);
1429         install_keyword("path_checker", &def_checker_name_handler, &snprint_def_checker_name);
1430         install_keyword("checker", &def_checker_name_handler, NULL);
1431         install_keyword("alias_prefix", &def_alias_prefix_handler, &snprint_def_alias_prefix);
1432         install_keyword("failback", &def_pgfailback_handler, &snprint_def_pgfailback);
1433         install_keyword("rr_min_io", &def_minio_handler, &snprint_def_minio);
1434         install_keyword("rr_min_io_rq", &def_minio_rq_handler, &snprint_def_minio_rq);
1435         install_keyword("max_fds", &max_fds_handler, &snprint_max_fds);
1436         install_keyword("rr_weight", &def_rr_weight_handler, &snprint_def_rr_weight);
1437         install_keyword("no_path_retry", &def_no_path_retry_handler, &snprint_def_no_path_retry);
1438         install_keyword("queue_without_daemon", &def_queue_without_daemon_handler, &snprint_def_queue_without_daemon);
1439         install_keyword("checker_timeout", &def_checker_timeout_handler, &snprint_def_checker_timeout);
1440         install_keyword("pg_timeout", &deprecated_handler, &snprint_deprecated);
1441         install_keyword("flush_on_last_del", &def_flush_on_last_del_handler, &snprint_def_flush_on_last_del);
1442         install_keyword("user_friendly_names", &def_user_friendly_names_handler, &snprint_def_user_friendly_names);
1443         install_keyword("mode", &def_mode_handler, &snprint_def_mode);
1444         install_keyword("uid", &def_uid_handler, &snprint_def_uid);
1445         install_keyword("gid", &def_gid_handler, &snprint_def_gid);
1446         install_keyword("fast_io_fail_tmo", &def_fast_io_fail_handler, &snprint_def_fast_io_fail);
1447         install_keyword("dev_loss_tmo", &def_dev_loss_handler, &snprint_def_dev_loss);
1448         install_keyword("bindings_file", &def_bindings_file_handler, &snprint_def_bindings_file);
1449         install_keyword("wwids_file", &def_wwids_file_handler, &snprint_def_wwids_file);
1450         install_keyword("prkeys_file", &def_prkeys_file_handler, &snprint_def_prkeys_file);
1451         install_keyword("log_checker_err", &def_log_checker_err_handler, &snprint_def_log_checker_err);
1452         install_keyword("reservation_key", &def_reservation_key_handler, &snprint_def_reservation_key);
1453         install_keyword("retain_attached_hw_handler", &def_retain_hwhandler_handler, &snprint_def_retain_hwhandler);
1454         install_keyword("detect_prio", &def_detect_prio_handler, &snprint_def_detect_prio);
1455         install_keyword("detect_checker", &def_detect_checker_handler, &snprint_def_detect_checker);
1456         install_keyword("force_sync", &def_force_sync_handler, &snprint_def_force_sync);
1457         install_keyword("strict_timing", &def_strict_timing_handler, &snprint_def_strict_timing);
1458         install_keyword("deferred_remove", &def_deferred_remove_handler, &snprint_def_deferred_remove);
1459         install_keyword("partition_delimiter", &def_partition_delim_handler, &snprint_def_partition_delim);
1460         install_keyword("config_dir", &def_config_dir_handler, &snprint_def_config_dir);
1461         install_keyword("delay_watch_checks", &def_delay_watch_checks_handler, &snprint_def_delay_watch_checks);
1462         install_keyword("delay_wait_checks", &def_delay_wait_checks_handler, &snprint_def_delay_wait_checks);
1463         install_keyword("marginal_path_err_sample_time", &def_marginal_path_err_sample_time_handler, &snprint_def_marginal_path_err_sample_time);
1464         install_keyword("marginal_path_err_rate_threshold", &def_marginal_path_err_rate_threshold_handler, &snprint_def_marginal_path_err_rate_threshold);
1465         install_keyword("marginal_path_err_recheck_gap_time", &def_marginal_path_err_recheck_gap_time_handler, &snprint_def_marginal_path_err_recheck_gap_time);
1466         install_keyword("marginal_path_double_failed_time", &def_marginal_path_double_failed_time_handler, &snprint_def_marginal_path_double_failed_time);
1467
1468         install_keyword("find_multipaths", &def_find_multipaths_handler, &snprint_def_find_multipaths);
1469         install_keyword("uxsock_timeout", &def_uxsock_timeout_handler, &snprint_def_uxsock_timeout);
1470         install_keyword("retrigger_tries", &def_retrigger_tries_handler, &snprint_def_retrigger_tries);
1471         install_keyword("retrigger_delay", &def_retrigger_delay_handler, &snprint_def_retrigger_delay);
1472         install_keyword("missing_uev_wait_timeout", &def_uev_wait_timeout_handler, &snprint_def_uev_wait_timeout);
1473         install_keyword("skip_kpartx", &def_skip_kpartx_handler, &snprint_def_skip_kpartx);
1474         install_keyword("disable_changed_wwids", &def_disable_changed_wwids_handler, &snprint_def_disable_changed_wwids);
1475         install_keyword("remove_retries", &def_remove_retries_handler, &snprint_def_remove_retries);
1476         install_keyword("max_sectors_kb", &def_max_sectors_kb_handler, &snprint_def_max_sectors_kb);
1477         install_keyword("ghost_delay", &def_ghost_delay_handler, &snprint_def_ghost_delay);
1478         __deprecated install_keyword("default_selector", &def_selector_handler, NULL);
1479         __deprecated install_keyword("default_path_grouping_policy", &def_pgpolicy_handler, NULL);
1480         __deprecated install_keyword("default_uid_attribute", &def_uid_attribute_handler, NULL);
1481         __deprecated install_keyword("default_getuid_callout", &def_getuid_handler, NULL);
1482         __deprecated install_keyword("default_features", &def_features_handler, NULL);
1483         __deprecated install_keyword("default_path_checker", &def_checker_name_handler, NULL);
1484
1485         install_keyword_root("blacklist", &blacklist_handler);
1486         install_keyword_multi("devnode", &ble_blist_devnode_handler, &snprint_ble_simple);
1487         install_keyword_multi("wwid", &ble_blist_wwid_handler, &snprint_ble_simple);
1488         install_keyword_multi("property", &ble_blist_property_handler, &snprint_ble_simple);
1489         install_keyword_multi("device", &ble_device_handler, NULL);
1490         install_sublevel();
1491         install_keyword("vendor", &ble_blist_device_vendor_handler, &snprint_bled_vendor);
1492         install_keyword("product", &ble_blist_device_product_handler, &snprint_bled_product);
1493         install_sublevel_end();
1494         install_keyword_root("blacklist_exceptions", &blacklist_exceptions_handler);
1495         install_keyword_multi("devnode", &ble_elist_devnode_handler, &snprint_ble_simple);
1496         install_keyword_multi("wwid", &ble_elist_wwid_handler, &snprint_ble_simple);
1497         install_keyword_multi("property", &ble_elist_property_handler, &snprint_ble_simple);
1498         install_keyword_multi("device", &ble_except_device_handler, NULL);
1499         install_sublevel();
1500         install_keyword("vendor", &ble_elist_device_vendor_handler, &snprint_bled_vendor);
1501         install_keyword("product", &ble_elist_device_product_handler, &snprint_bled_product);
1502         install_sublevel_end();
1503
1504 #if 0
1505         __deprecated install_keyword_root("devnode_blacklist", &blacklist_handler);
1506         __deprecated install_keyword("devnode", &ble_devnode_handler, &snprint_ble_simple);
1507         __deprecated install_keyword("wwid", &ble_wwid_handler, &snprint_ble_simple);
1508         __deprecated install_keyword("device", &ble_device_handler, NULL);
1509         __deprecated install_sublevel();
1510         __deprecated install_keyword("vendor", &ble_vendor_handler, &snprint_bled_vendor);
1511         __deprecated install_keyword("product", &ble_product_handler, &snprint_bled_product);
1512         __deprecated install_sublevel_end();
1513 #endif
1514 /*
1515  * If you add or remove a "device subsection" keyword also update
1516  * multipath/multipath.conf.5 and the TEMPLATE in libmultipath/hwtable.c
1517  */
1518         install_keyword_root("devices", &devices_handler);
1519         install_keyword_multi("device", &device_handler, NULL);
1520         install_sublevel();
1521         install_keyword("vendor", &hw_vendor_handler, &snprint_hw_vendor);
1522         install_keyword("product", &hw_product_handler, &snprint_hw_product);
1523         install_keyword("revision", &hw_revision_handler, &snprint_hw_revision);
1524         install_keyword("product_blacklist", &hw_bl_product_handler, &snprint_hw_bl_product);
1525         install_keyword("path_grouping_policy", &hw_pgpolicy_handler, &snprint_hw_pgpolicy);
1526         install_keyword("uid_attribute", &hw_uid_attribute_handler, &snprint_hw_uid_attribute);
1527         install_keyword("getuid_callout", &hw_getuid_handler, &snprint_hw_getuid);
1528         install_keyword("path_selector", &hw_selector_handler, &snprint_hw_selector);
1529         install_keyword("path_checker", &hw_checker_name_handler, &snprint_hw_checker_name);
1530         install_keyword("checker", &hw_checker_name_handler, NULL);
1531         install_keyword("alias_prefix", &hw_alias_prefix_handler, &snprint_hw_alias_prefix);
1532         install_keyword("features", &hw_features_handler, &snprint_hw_features);
1533         install_keyword("hardware_handler", &hw_hwhandler_handler, &snprint_hw_hwhandler);
1534         install_keyword("prio", &hw_prio_name_handler, &snprint_hw_prio_name);
1535         install_keyword("prio_args", &hw_prio_args_handler, &snprint_hw_prio_args);
1536         install_keyword("failback", &hw_pgfailback_handler, &snprint_hw_pgfailback);
1537         install_keyword("rr_weight", &hw_rr_weight_handler, &snprint_hw_rr_weight);
1538         install_keyword("no_path_retry", &hw_no_path_retry_handler, &snprint_hw_no_path_retry);
1539         install_keyword("rr_min_io", &hw_minio_handler, &snprint_hw_minio);
1540         install_keyword("rr_min_io_rq", &hw_minio_rq_handler, &snprint_hw_minio_rq);
1541         install_keyword("pg_timeout", &deprecated_handler, &snprint_deprecated);
1542         install_keyword("flush_on_last_del", &hw_flush_on_last_del_handler, &snprint_hw_flush_on_last_del);
1543         install_keyword("fast_io_fail_tmo", &hw_fast_io_fail_handler, &snprint_hw_fast_io_fail);
1544         install_keyword("dev_loss_tmo", &hw_dev_loss_handler, &snprint_hw_dev_loss);
1545         install_keyword("user_friendly_names", &hw_user_friendly_names_handler, &snprint_hw_user_friendly_names);
1546         install_keyword("retain_attached_hw_handler", &hw_retain_hwhandler_handler, &snprint_hw_retain_hwhandler);
1547         install_keyword("detect_prio", &hw_detect_prio_handler, &snprint_hw_detect_prio);
1548         install_keyword("detect_checker", &hw_detect_checker_handler, &snprint_hw_detect_checker);
1549         install_keyword("deferred_remove", &hw_deferred_remove_handler, &snprint_hw_deferred_remove);
1550         install_keyword("delay_watch_checks", &hw_delay_watch_checks_handler, &snprint_hw_delay_watch_checks);
1551         install_keyword("delay_wait_checks", &hw_delay_wait_checks_handler, &snprint_hw_delay_wait_checks);
1552         install_keyword("marginal_path_err_sample_time", &hw_marginal_path_err_sample_time_handler, &snprint_hw_marginal_path_err_sample_time);
1553         install_keyword("marginal_path_err_rate_threshold", &hw_marginal_path_err_rate_threshold_handler, &snprint_hw_marginal_path_err_rate_threshold);
1554         install_keyword("marginal_path_err_recheck_gap_time", &hw_marginal_path_err_recheck_gap_time_handler, &snprint_hw_marginal_path_err_recheck_gap_time);
1555         install_keyword("marginal_path_double_failed_time", &hw_marginal_path_double_failed_time_handler, &snprint_hw_marginal_path_double_failed_time);
1556         install_keyword("skip_kpartx", &hw_skip_kpartx_handler, &snprint_hw_skip_kpartx);
1557         install_keyword("max_sectors_kb", &hw_max_sectors_kb_handler, &snprint_hw_max_sectors_kb);
1558         install_keyword("ghost_delay", &hw_ghost_delay_handler, &snprint_hw_ghost_delay);
1559         install_sublevel_end();
1560
1561         install_keyword_root("overrides", &overrides_handler);
1562         install_keyword("path_grouping_policy", &ovr_pgpolicy_handler, &snprint_ovr_pgpolicy);
1563         install_keyword("uid_attribute", &ovr_uid_attribute_handler, &snprint_ovr_uid_attribute);
1564         install_keyword("getuid_callout", &ovr_getuid_handler, &snprint_ovr_getuid);
1565         install_keyword("path_selector", &ovr_selector_handler, &snprint_ovr_selector);
1566         install_keyword("path_checker", &ovr_checker_name_handler, &snprint_ovr_checker_name);
1567         install_keyword("checker", &ovr_checker_name_handler, NULL);
1568         install_keyword("alias_prefix", &ovr_alias_prefix_handler, &snprint_ovr_alias_prefix);
1569         install_keyword("features", &ovr_features_handler, &snprint_ovr_features);
1570         install_keyword("prio", &ovr_prio_name_handler, &snprint_ovr_prio_name);
1571         install_keyword("prio_args", &ovr_prio_args_handler, &snprint_ovr_prio_args);
1572         install_keyword("failback", &ovr_pgfailback_handler, &snprint_ovr_pgfailback);
1573         install_keyword("rr_weight", &ovr_rr_weight_handler, &snprint_ovr_rr_weight);
1574         install_keyword("no_path_retry", &ovr_no_path_retry_handler, &snprint_ovr_no_path_retry);
1575         install_keyword("rr_min_io", &ovr_minio_handler, &snprint_ovr_minio);
1576         install_keyword("rr_min_io_rq", &ovr_minio_rq_handler, &snprint_ovr_minio_rq);
1577         install_keyword("flush_on_last_del", &ovr_flush_on_last_del_handler, &snprint_ovr_flush_on_last_del);
1578         install_keyword("fast_io_fail_tmo", &ovr_fast_io_fail_handler, &snprint_ovr_fast_io_fail);
1579         install_keyword("dev_loss_tmo", &ovr_dev_loss_handler, &snprint_ovr_dev_loss);
1580         install_keyword("user_friendly_names", &ovr_user_friendly_names_handler, &snprint_ovr_user_friendly_names);
1581         install_keyword("retain_attached_hw_handler", &ovr_retain_hwhandler_handler, &snprint_ovr_retain_hwhandler);
1582         install_keyword("detect_prio", &ovr_detect_prio_handler, &snprint_ovr_detect_prio);
1583         install_keyword("detect_checker", &ovr_detect_checker_handler, &snprint_ovr_detect_checker);
1584         install_keyword("deferred_remove", &ovr_deferred_remove_handler, &snprint_ovr_deferred_remove);
1585         install_keyword("delay_watch_checks", &ovr_delay_watch_checks_handler, &snprint_ovr_delay_watch_checks);
1586         install_keyword("delay_wait_checks", &ovr_delay_wait_checks_handler, &snprint_ovr_delay_wait_checks);
1587         install_keyword("marginal_path_err_sample_time", &ovr_marginal_path_err_sample_time_handler, &snprint_ovr_marginal_path_err_sample_time);
1588         install_keyword("marginal_path_err_rate_threshold", &ovr_marginal_path_err_rate_threshold_handler, &snprint_ovr_marginal_path_err_rate_threshold);
1589         install_keyword("marginal_path_err_recheck_gap_time", &ovr_marginal_path_err_recheck_gap_time_handler, &snprint_ovr_marginal_path_err_recheck_gap_time);
1590         install_keyword("marginal_path_double_failed_time", &ovr_marginal_path_double_failed_time_handler, &snprint_ovr_marginal_path_double_failed_time);
1591
1592         install_keyword("skip_kpartx", &ovr_skip_kpartx_handler, &snprint_ovr_skip_kpartx);
1593         install_keyword("max_sectors_kb", &ovr_max_sectors_kb_handler, &snprint_ovr_max_sectors_kb);
1594         install_keyword("ghost_delay", &ovr_ghost_delay_handler, &snprint_ovr_ghost_delay);
1595
1596         install_keyword_root("multipaths", &multipaths_handler);
1597         install_keyword_multi("multipath", &multipath_handler, NULL);
1598         install_sublevel();
1599         install_keyword("wwid", &mp_wwid_handler, &snprint_mp_wwid);
1600         install_keyword("alias", &mp_alias_handler, &snprint_mp_alias);
1601         install_keyword("path_grouping_policy", &mp_pgpolicy_handler, &snprint_mp_pgpolicy);
1602         install_keyword("path_selector", &mp_selector_handler, &snprint_mp_selector);
1603         install_keyword("prio", &mp_prio_name_handler, &snprint_mp_prio_name);
1604         install_keyword("prio_args", &mp_prio_args_handler, &snprint_mp_prio_args);
1605         install_keyword("failback", &mp_pgfailback_handler, &snprint_mp_pgfailback);
1606         install_keyword("rr_weight", &mp_rr_weight_handler, &snprint_mp_rr_weight);
1607         install_keyword("no_path_retry", &mp_no_path_retry_handler, &snprint_mp_no_path_retry);
1608         install_keyword("rr_min_io", &mp_minio_handler, &snprint_mp_minio);
1609         install_keyword("rr_min_io_rq", &mp_minio_rq_handler, &snprint_mp_minio_rq);
1610         install_keyword("pg_timeout", &deprecated_handler, &snprint_deprecated);
1611         install_keyword("flush_on_last_del", &mp_flush_on_last_del_handler, &snprint_mp_flush_on_last_del);
1612         install_keyword("features", &mp_features_handler, &snprint_mp_features);
1613         install_keyword("mode", &mp_mode_handler, &snprint_mp_mode);
1614         install_keyword("uid", &mp_uid_handler, &snprint_mp_uid);
1615         install_keyword("gid", &mp_gid_handler, &snprint_mp_gid);
1616         install_keyword("reservation_key", &mp_reservation_key_handler, &snprint_mp_reservation_key);
1617         install_keyword("user_friendly_names", &mp_user_friendly_names_handler, &snprint_mp_user_friendly_names);
1618         install_keyword("deferred_remove", &mp_deferred_remove_handler, &snprint_mp_deferred_remove);
1619         install_keyword("delay_watch_checks", &mp_delay_watch_checks_handler, &snprint_mp_delay_watch_checks);
1620         install_keyword("delay_wait_checks", &mp_delay_wait_checks_handler, &snprint_mp_delay_wait_checks);
1621         install_keyword("marginal_path_err_sample_time", &mp_marginal_path_err_sample_time_handler, &snprint_mp_marginal_path_err_sample_time);
1622         install_keyword("marginal_path_err_rate_threshold", &mp_marginal_path_err_rate_threshold_handler, &snprint_mp_marginal_path_err_rate_threshold);
1623         install_keyword("marginal_path_err_recheck_gap_time", &mp_marginal_path_err_recheck_gap_time_handler, &snprint_mp_marginal_path_err_recheck_gap_time);
1624         install_keyword("marginal_path_double_failed_time", &mp_marginal_path_double_failed_time_handler, &snprint_mp_marginal_path_double_failed_time);
1625         install_keyword("skip_kpartx", &mp_skip_kpartx_handler, &snprint_mp_skip_kpartx);
1626         install_keyword("max_sectors_kb", &mp_max_sectors_kb_handler, &snprint_mp_max_sectors_kb);
1627         install_keyword("ghost_delay", &mp_ghost_delay_handler, &snprint_mp_ghost_delay);
1628         install_sublevel_end();
1629 }