forked from xapi-project/blktap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtap-ctl-list.c
430 lines (337 loc) · 7.9 KB
/
tap-ctl-list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
* Copyright (c) 2016, Citrix Systems, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <glob.h>
#include "tap-ctl.h"
#include "blktap2.h"
#include "list.h"
static tap_list_t*
_tap_list_alloc(void)
{
const size_t sz = sizeof(tap_list_t);
tap_list_t *tl;
tl = malloc(sz);
if (!tl)
return NULL;
tl->pid = -1;
tl->minor = -1;
tl->state = -1;
tl->type = NULL;
tl->path = NULL;
INIT_LIST_HEAD(&tl->entry);
return tl;
}
static void
_tap_list_free(tap_list_t *tl)
{
list_del_init(&tl->entry);
if (tl->type) {
free(tl->type);
tl->type = NULL;
}
if (tl->path) {
free(tl->path);
tl->path = NULL;
}
free(tl);
}
int
_parse_params(const char *params, char **type, char **path)
{
char *ptr;
size_t len;
ptr = strchr(params, ':');
if (!ptr)
return -EINVAL;
len = ptr - params;
*type = strndup(params, len);
*path = strdup(params + len + 1);
if (!*type || !*path) {
free(*type);
*type = NULL;
free(*path);
*path = NULL;
return -errno;
}
return 0;
}
void
tap_ctl_list_free(struct list_head *list)
{
tap_list_t *tl, *n;
tap_list_for_each_entry_safe(tl, n, list)
_tap_list_free(tl);
}
/**
* Returns a list running tapdisks. tapdisks are searched for by looking for
* their control socket.
*/
static int
_tap_ctl_find_minors(struct list_head *list)
{
const char *pattern;
glob_t glbuf = { 0 };
tap_list_t *tl;
int i, err;
INIT_LIST_HEAD(list);
pattern = BLKTAP2_SYSFS_DIR"/blktap*";
err = glob(pattern, 0, NULL, &glbuf);
switch (err) {
case GLOB_NOMATCH:
goto done;
case GLOB_ABORTED:
case GLOB_NOSPACE:
err = -errno;
EPRINTF("%s: glob failed, err %d", pattern, err);
goto fail;
}
for (i = 0; i < glbuf.gl_pathc; ++i) {
int n;
tl = _tap_list_alloc();
if (!tl) {
err = -ENOMEM;
goto fail;
}
n = sscanf(glbuf.gl_pathv[i], BLKTAP2_SYSFS_DIR"/blktap!blktap%d", &tl->minor);
if (n != 1) {
_tap_list_free(tl);
continue;
}
list_add_tail(&tl->entry, list);
}
done:
err = 0;
out:
if (glbuf.gl_pathv)
globfree(&glbuf);
return err;
fail:
tap_ctl_list_free(list);
goto out;
}
int
_tap_ctl_find_tapdisks(struct list_head *list)
{
const char *pattern;
glob_t glbuf = { 0 };
int err, i, n_taps = 0;
pattern = BLKTAP2_CONTROL_DIR"/"BLKTAP2_CONTROL_SOCKET"*";
INIT_LIST_HEAD(list);
err = glob(pattern, 0, NULL, &glbuf);
switch (err) {
case GLOB_NOMATCH:
goto done;
case GLOB_ABORTED:
case GLOB_NOSPACE:
err = -errno;
EPRINTF("%s: glob failed, err %d", pattern, err);
goto fail;
}
for (i = 0; i < glbuf.gl_pathc; ++i) {
tap_list_t *tl;
int n;
tl = _tap_list_alloc();
if (!tl) {
err = -ENOMEM;
goto fail;
}
n = sscanf(glbuf.gl_pathv[i],
BLKTAP2_CONTROL_DIR"/"BLKTAP2_CONTROL_SOCKET"%d",
&tl->pid);
if (n != 1)
goto skip;
tl->pid = tap_ctl_get_pid(tl->pid);
if (tl->pid < 0)
goto skip;
list_add_tail(&tl->entry, list);
n_taps++;
continue;
skip:
_tap_list_free(tl);
}
done:
err = 0;
out:
if (glbuf.gl_pathv)
globfree(&glbuf);
return err ? : n_taps;
fail:
tap_ctl_list_free(list);
goto out;
}
/**
* Retrieves all the VBDs a tapdisk is serving.
*
* @param pid the process ID of the tapdisk whose VBDs should be retrieved
* @param list output parameter that receives the list of VBD
* @returns 0 on success, an error code otherwise
*/
int
_tap_ctl_list_tapdisk(pid_t pid, struct list_head *list)
{
struct timeval timeout = { .tv_sec = 10, .tv_usec = 0 };
tapdisk_message_t message;
tap_list_t *tl;
int err, sfd;
err = tap_ctl_connect_id(pid, &sfd);
if (err)
return err;
memset(&message, 0, sizeof(message));
message.type = TAPDISK_MESSAGE_LIST;
message.cookie = -1;
err = tap_ctl_write_message(sfd, &message, &timeout);
if (err)
goto out;
INIT_LIST_HEAD(list);
do {
err = tap_ctl_read_message(sfd, &message, &timeout);
if (err) {
err = -EPROTO;
goto fail;
}
if (message.u.list.count == 0)
break;
tl = _tap_list_alloc();
if (!tl) {
err = -ENOMEM;
goto fail;
}
tl->pid = pid;
tl->minor = message.u.list.minor;
tl->state = message.u.list.state;
if (message.u.list.path[0] != 0) {
err = _parse_params(message.u.list.path,
&tl->type, &tl->path);
if (err) {
_tap_list_free(tl);
goto fail;
}
}
list_add(&tl->entry, list);
} while (1);
err = 0;
out:
close(sfd);
return err;
fail:
tap_ctl_list_free(list);
goto out;
}
int
tap_ctl_list(struct list_head *list)
{
struct list_head minors, tapdisks, vbds;
tap_list_t *t, *next_t, *v, *next_v, *m, *next_m;
int err;
/*
* Find all minors, find all tapdisks, then list all minors
* they attached to. Output is a 3-way outer join.
*/
err = _tap_ctl_find_minors(&minors);
if (err < 0)
goto fail;
err = _tap_ctl_find_tapdisks(&tapdisks);
if (err < 0) {
EPRINTF("error finding tapdisks: %s\n", strerror(-err));
goto fail;
}
INIT_LIST_HEAD(list);
tap_list_for_each_entry_safe(t, next_t, &tapdisks) {
err = _tap_ctl_list_tapdisk(t->pid, &vbds);
if (err || list_empty(&vbds)) {
list_move_tail(&t->entry, list);
continue;
}
tap_list_for_each_entry_safe(v, next_v, &vbds) {
tap_list_for_each_entry_safe(m, next_m, &minors)
if (m->minor == v->minor) {
_tap_list_free(m);
break;
}
list_move_tail(&v->entry, list);
}
_tap_list_free(t);
}
/* orphaned minors */
list_splice_tail(&minors, list);
return 0;
fail:
tap_ctl_list_free(list);
tap_ctl_list_free(&vbds);
tap_ctl_list_free(&tapdisks);
tap_ctl_list_free(&minors);
return err;
}
int
tap_ctl_list_pid(pid_t pid, struct list_head *list)
{
tap_list_t *t;
int err;
t = _tap_list_alloc();
if (!t)
return -ENOMEM;
t->pid = tap_ctl_get_pid(pid);
if (t->pid < 0) {
_tap_list_free(t);
return 0;
}
err = _tap_ctl_list_tapdisk(t->pid, list);
if (err || list_empty(list))
list_add_tail(&t->entry, list);
return 0;
}
int
tap_ctl_find_minor(const char *type, const char *path)
{
struct list_head list = LIST_HEAD_INIT(list);
tap_list_t *entry;
int minor, err;
err = tap_ctl_list(&list);
if (err)
return err;
minor = -1;
tap_list_for_each_entry(entry, &list) {
if (type && (!entry->type || strcmp(entry->type, type)))
continue;
if (path && (!entry->path || strcmp(entry->path, path)))
continue;
minor = entry->minor;
break;
}
tap_ctl_list_free(&list);
return minor >= 0 ? minor : -ENOENT;
}