[libmultipath] simplify the blacklist handling
[multipath-tools/.git] / libmultipath / blacklist.c
1 #include <stdio.h>
2
3 #include "memory.h"
4 #include "vector.h"
5 #include "util.h"
6 #include "debug.h"
7 #include "regex.h"
8 #include "blacklist.h"
9
10 static int
11 store_ble (vector blist, char * str)
12 {
13         regex_t * ble;
14         
15         if (!str)
16                 return 0;
17
18         ble = MALLOC(sizeof(regex_t));
19
20         if (!ble)
21                 goto out;
22
23         if (regcomp(ble, str, REG_EXTENDED|REG_NOSUB))
24                 goto out1;
25
26         if (!vector_alloc_slot(blist))
27                 goto out1;
28
29         vector_set_slot(blist, ble);
30         return 0;
31 out1:
32         FREE(ble);
33 out:
34         return 1;
35 }
36
37 int
38 setup_default_blist (vector blist)
39 {
40         int r = 0;
41
42         r += store_ble(blist, "^(ram|raw|loop|fd|md|dm-|sr|scd|st)[0-9]*");
43         r += store_ble(blist, "^hd[a-z]");
44         r += store_ble(blist, "^cciss!c[0-9]d[0-9]*");
45
46         return r;
47 }
48
49 int
50 blacklist (vector blist, char * dev)
51 {
52         int i;
53         regex_t * ble;
54
55         vector_foreach_slot (blist, ble, i) {
56                 if (!regexec(ble, dev, 0, NULL, 0)) {
57                         condlog(3, "%s blacklisted", dev);
58                         return 1;
59                 }
60         }
61         return 0;
62 }
63
64 int
65 store_regex (vector blist, char * regex)
66 {
67         if (!blist)
68                 return 1;
69
70         if (!regex)
71                 return 1;
72
73         return store_ble(blist, regex);
74 }       
75
76 void
77 free_blacklist (vector blist)
78 {
79         regex_t * ble;
80         int i;
81
82         if (!blist)
83                 return;
84
85         vector_foreach_slot (blist, ble, i)
86                 if (ble)
87                         FREE(ble);
88
89         vector_free(blist);
90 }