[libmultipath] forgoten fd init in struct.c:alloc_path()
[platform/upstream/multipath-tools.git] / libmultipath / structs.c
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include "memory.h"
5 #include "vector.h"
6 #include "util.h"
7 #include "structs.h"
8 #include "config.h"
9 #include "debug.h"
10
11 struct path *
12 alloc_path (void)
13 {
14         struct path * pp;
15         
16         pp = (struct path *)MALLOC(sizeof(struct path));
17
18         if (pp) {
19                 pp->sg_id.host_no = -1;
20                 pp->sg_id.channel = -1;
21                 pp->sg_id.scsi_id = -1;
22                 pp->sg_id.lun = -1;
23                 pp->fd = -1;
24         }
25         return pp;
26 }
27
28 void
29 free_path (struct path * pp)
30 {
31         if (!pp)
32                 return;
33
34         if (pp->checker_context)
35                 free(pp->checker_context);
36
37         if (pp->fd >= 0)
38                 close(pp->fd);
39
40         FREE(pp);
41 }
42
43 void
44 free_pathvec (vector vec, int free_paths)
45 {
46         int i;
47         struct path * pp;
48
49         if (!vec)
50                 return;
51
52         if (free_paths)
53                 vector_foreach_slot(vec, pp, i)
54                         free_path(pp);
55
56         vector_free(vec);
57 }
58
59 struct pathgroup *
60 alloc_pathgroup (void)
61 {
62         struct pathgroup * pgp;
63
64         pgp = (struct pathgroup *)MALLOC(sizeof(struct pathgroup));
65
66         if (!pgp)
67                 return NULL;
68
69         pgp->paths = vector_alloc();
70
71         if (!pgp->paths)
72                 FREE(pgp);
73
74         return pgp;
75 }
76
77 void
78 free_pathgroup (struct pathgroup * pgp, int free_paths)
79 {
80         if (!pgp)
81                 return;
82
83         free_pathvec(pgp->paths, free_paths);
84         FREE(pgp);
85 }
86
87 void
88 free_pgvec (vector pgvec, int free_paths)
89 {
90         int i;
91         struct pathgroup * pgp;
92
93         if (!pgvec)
94                 return;
95
96         vector_foreach_slot(pgvec, pgp, i)
97                 free_pathgroup(pgp, free_paths);
98
99         vector_free(pgvec);
100 }
101
102 struct multipath *
103 alloc_multipath (void)
104 {
105         struct multipath * mpp;
106
107         mpp = (struct multipath *)MALLOC(sizeof(struct multipath));
108
109         if (mpp)
110                 mpp->nextpg = 1;
111
112         return mpp;
113 }
114
115 void
116 free_multipath (struct multipath * mpp, int free_paths)
117 {
118         if (!mpp)
119                 return;
120
121         if (mpp->selector &&
122             mpp->selector != conf->default_selector &&
123             (!mpp->mpe || (mpp->mpe && mpp->selector != mpp->mpe->selector)) &&
124             (!mpp->hwe || (mpp->hwe && mpp->selector != mpp->hwe->selector)))
125                 FREE(mpp->selector);
126
127         if (mpp->alias &&
128             (!mpp->mpe || (mpp->mpe && mpp->alias != mpp->mpe->alias)) &&
129             (mpp->wwid && mpp->alias != mpp->wwid))
130                 FREE(mpp->alias);
131
132         if (mpp->features &&
133             mpp->features != conf->default_features &&
134             (!mpp->hwe || (mpp->hwe && mpp->features != mpp->hwe->features)))
135                 FREE(mpp->features);
136
137         if (mpp->hwhandler &&
138             mpp->hwhandler != conf->default_hwhandler &&
139             (!mpp->hwe || (mpp->hwe && mpp->hwhandler != mpp->hwe->hwhandler)))
140                 FREE(mpp->hwhandler);
141
142         free_pathvec(mpp->paths, free_paths);
143         free_pgvec(mpp->pg, free_paths);
144         FREE(mpp);
145 }
146
147 void
148 free_multipathvec (vector mpvec, int free_paths)
149 {
150         int i;
151         struct multipath * mpp;
152
153         if (!mpvec)
154                 return;
155
156         vector_foreach_slot (mpvec, mpp, i)
157                 free_multipath(mpp, free_paths);
158
159         vector_free(mpvec);
160 }
161
162 int
163 store_path (vector pathvec, struct path * pp)
164 {
165         if (!vector_alloc_slot(pathvec))
166                 return 1;
167
168         vector_set_slot(pathvec, pp);
169
170         return 0;
171 }
172
173 int
174 store_pathgroup (vector pgvec, struct pathgroup * pgp)
175 {
176         if (!vector_alloc_slot(pgvec))
177                 return 1;
178
179         vector_set_slot(pgvec, pgp);
180
181         return 0;
182 }
183
184 struct multipath *
185 find_mp_by_minor (vector mp, int minor)
186 {
187         int i;
188         struct multipath * mpp;
189         
190         vector_foreach_slot (mp, mpp, i)
191                 if (mpp->minor == minor)
192                         return mpp;
193
194         return NULL;
195 }
196
197 struct multipath *
198 find_mp_by_wwid (vector mp, char * wwid)
199 {
200         int i;
201         struct multipath * mpp;
202         
203         vector_foreach_slot (mp, mpp, i)
204                 if (!strncmp(mpp->wwid, wwid, WWID_SIZE))
205                         return mpp;
206
207         return NULL;
208 }
209
210 struct multipath *
211 find_mp (vector mp, char * alias)
212 {
213         int i;
214         int len;
215         struct multipath * mpp;
216         
217         len = strlen(alias);
218
219         if (!len)
220                 return NULL;
221         
222         vector_foreach_slot (mp, mpp, i) {
223                 if (strlen(mpp->alias) == len &&
224                     !strncmp(mpp->alias, alias, len))
225                         return mpp;
226         }
227         return NULL;
228 }
229
230 struct path *
231 find_path_by_dev (vector pathvec, char * dev)
232 {
233         int i;
234         struct path * pp;
235         
236         vector_foreach_slot (pathvec, pp, i)
237                 if (!strcmp_chomp(pp->dev, dev))
238                         return pp;
239
240         condlog(3, "path %s not found in pathvec\n", dev);
241         return NULL;
242 }
243
244 struct path *
245 find_path_by_devt (vector pathvec, char * dev_t)
246 {
247         int i;
248         struct path * pp;
249
250         vector_foreach_slot (pathvec, pp, i)
251                 if (!strcmp_chomp(pp->dev_t, dev_t))
252                         return pp;
253
254         condlog(3, "path %s not found in pathvec\n", dev_t);
255         return NULL;
256 }
257