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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
|
// #include <config.h>
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <libvirt/libvirt.h>
#include <libxml/xmlreader.h>
#include "simpleconfig.h"
#include "debug.h"
#define DEBUG0(fmt) dbg_printf(5,"%s:%d :: " fmt "\n", \
__func__, __LINE__)
#define DEBUG1(fmt, ...) dbg_printf(5, "%s:%d: " fmt "\n", \
__func__, __LINE__, __VA_ARGS__)
#include "serial.h"
#define STREQ(a,b) (strcmp((a),(b)) == 0)
static pthread_t event_tid = 0;
static int run = 0;
/* Prototypes */
const char *eventToString(int event);
int myDomainEventCallback1(virConnectPtr conn, virDomainPtr dom,
int event, int detail, void *opaque);
void usage(const char *pname);
struct domain_info {
virDomainPtr dom;
virDomainEventType event;
};
static int
is_in_directory(const char *dir, const char *pathspec)
{
char *last_slash = NULL;
size_t dirlen, pathlen;
if (!dir || !pathspec)
return 0;
dirlen = strlen(dir);
pathlen = strlen(pathspec);
/*
printf("dirlen = %d pathlen = %d\n",
dirlen, pathlen);
*/
/* chop off trailing slashes */
while (dirlen && dir[dirlen-1]=='/')
--dirlen;
/* chop off leading slashes */
while (dirlen && dir[0] == '/') {
++dir;
--dirlen;
}
/* chop off leading slashes */
while (pathlen && pathspec[0] == '/') {
++pathspec;
--pathlen;
}
if (!dirlen || !pathlen)
return 0;
if (pathlen <= dirlen)
return 0;
last_slash = strrchr(pathspec, '/');
if (!last_slash)
return 0;
while (*last_slash == '/' && last_slash > pathspec)
--last_slash;
if (last_slash == pathspec)
return 0;
pathlen = last_slash - pathspec + 1;
/*printf("real dirlen = %d real pathlen = %d\n",
dirlen, pathlen);*/
if (pathlen != dirlen)
return 0;
/* todo - intelligently skip multiple slashes mid-path */
return !strncmp(dir, pathspec, dirlen);
}
static int
domainStarted(virDomainPtr mojaDomain, const char *path, int mode)
{
char dom_uuid[42];
char *xml;
xmlDocPtr doc;
xmlNodePtr cur, devices, child, serial;
xmlAttrPtr attr, attr_mode, attr_path;
if (!mojaDomain)
return -1;
virDomainGetUUIDString(mojaDomain, dom_uuid);
xml = virDomainGetXMLDesc(mojaDomain, 0);
// printf("%s\n", xml);
// @todo: free mojaDomain
// parseXML output
doc = xmlParseMemory(xml, strlen(xml));
xmlFree(xml);
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr, "Empty doc\n");
xmlFreeDoc(doc);
return -1;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "domain")) {
fprintf(stderr, "no domain?\n");
xmlFreeDoc(doc);
return -1;
}
devices = cur->xmlChildrenNode;
for (devices = cur->xmlChildrenNode; devices != NULL;
devices = devices->next) {
if (xmlStrcmp(devices->name, (const xmlChar *) "devices")) {
continue;
}
for (child = devices->xmlChildrenNode; child != NULL;
child = child->next) {
if ((!mode && xmlStrcmp(child->name, (const xmlChar *) "serial")) ||
(mode && xmlStrcmp(child->name, (const xmlChar *) "channel"))) {
continue;
}
attr = xmlHasProp(child, (const xmlChar *)"type");
if (attr == NULL)
continue;
if (xmlStrcmp(attr->children->content,
(const xmlChar *) "unix")) {
continue;
}
for (serial = child->xmlChildrenNode; serial != NULL;
serial = serial->next) {
if (xmlStrcmp(serial->name,
(const xmlChar *) "source")) {
continue;
}
attr_mode = xmlHasProp(serial, (const xmlChar *)"mode");
attr_path = xmlHasProp(serial, (const xmlChar *)"path");
if (!attr_path || !attr_mode)
continue;
if (xmlStrcmp(attr_mode->children->content,
(const xmlChar *) "bind"))
continue;
if (path && !is_in_directory(path, (const char *)
attr_path->children->content))
continue;
domain_sock_setup(dom_uuid, (const char *)
attr_path->children->content);
}
}
}
xmlFreeDoc(doc);
return 0;
}
static int
registerExisting(virConnectPtr vp, const char *path, int mode)
{
int *d_ids = NULL;
int d_count, x;
virDomainPtr dom;
virDomainInfo d_info;
errno = EINVAL;
if (!vp)
return -1;
d_count = virConnectNumOfDomains(vp);
if (d_count <= 0) {
if (d_count == 0) {
/* Successful, but no domains running */
errno = 0;
return 0;
}
goto out_fail;
}
d_ids = malloc(sizeof (int) * d_count);
if (!d_ids)
goto out_fail;
if (virConnectListDomains(vp, d_ids, d_count) < 0)
goto out_fail;
/* Ok, we have the domain IDs - let's get their names and states */
for (x = 0; x < d_count; x++) {
dom = virDomainLookupByID(vp, d_ids[x]);
if (!dom) {
/* XXX doom */
goto out_fail;
}
if (virDomainGetInfo(dom, &d_info) < 0) {
/* XXX no info for the domain?!! */
virDomainFree(dom);
goto out_fail;
}
if (d_info.state != VIR_DOMAIN_SHUTOFF &&
d_info.state != VIR_DOMAIN_CRASHED)
domainStarted(dom, path, mode);
virDomainFree(dom);
}
out_fail:
free(d_ids);
return 0;
}
static int
domainStopped(virDomainPtr mojaDomain)
{
char dom_uuid[42];
if (!mojaDomain)
return -1;
virDomainGetUUIDString(mojaDomain, dom_uuid);
domain_sock_close(dom_uuid);
return 0;
}
struct event_args {
char *uri;
char *path;
int mode;
int wake_fd;
};
static void
connectClose(virConnectPtr conn ATTRIBUTE_UNUSED,
int reason,
void *opaque ATTRIBUTE_UNUSED)
{
switch (reason) {
case VIR_CONNECT_CLOSE_REASON_ERROR:
dbg_printf(2, "Connection closed due to I/O error\n");
break;
case VIR_CONNECT_CLOSE_REASON_EOF:
dbg_printf(2, "Connection closed due to end of file\n");
break;
case VIR_CONNECT_CLOSE_REASON_KEEPALIVE:
dbg_printf(2, "Connection closed due to keepalive timeout\n");
break;
case VIR_CONNECT_CLOSE_REASON_CLIENT:
dbg_printf(2, "Connection closed due to client request\n");
break;
default:
dbg_printf(2, "Connection closed due to unknown reason\n");
break;
};
run = 0;
}
int
myDomainEventCallback1(virConnectPtr conn,
virDomainPtr dom, int event, int detail, void *opaque)
{
struct event_args *args = (struct event_args *)opaque;
if (event == VIR_DOMAIN_EVENT_STARTED ||
event == VIR_DOMAIN_EVENT_STOPPED) {
virDomainRef(dom);
if (event == VIR_DOMAIN_EVENT_STARTED) {
domainStarted(dom, args->path, args->mode);
virDomainFree(dom);
if (write(args->wake_fd, "x", 1) != 1) {
dbg_printf(3, "Unable to wake up thread\n");
}
} else if (event == VIR_DOMAIN_EVENT_STOPPED) {
domainStopped(dom);
virDomainFree(dom);
}
}
return 0;
}
static void *
event_thread(void *arg)
{
struct event_args *args = (struct event_args *)arg;
virConnectPtr dconn = NULL;
int callback1ret = -1;
dbg_printf(3, "Libvirt event listener starting\n");
if (args->uri)
dbg_printf(3," * URI: %s\n", args->uri);
if (args->path)
dbg_printf(3," * Socket path: %s\n", args->path);
dbg_printf(3," * Mode: %s\n", args->mode ? "VMChannel" : "Serial");
if (virEventRegisterDefaultImpl() < 0) {
dbg_printf(1, "Failed to register default event impl\n");
goto out;
}
dconn = virConnectOpen(args->uri);
if (!dconn) {
dbg_printf(1, "Error connecting to libvirt\n");
goto out;
}
virConnectRegisterCloseCallback(dconn, connectClose, NULL, NULL);
DEBUG0("Registering domain event cbs");
registerExisting(dconn, args->path, args->mode);
callback1ret =
virConnectDomainEventRegister(dconn, myDomainEventCallback1, arg, NULL);
if (callback1ret != -1) {
if (virConnectSetKeepAlive(dconn, 5, 5) < 0) {
dbg_printf(1, "Failed to start keepalive protocol\n");
run = 0;
}
while (run) {
if (virEventRunDefaultImpl() < 0) {
dbg_printf(1, "RunDefaultImpl Failed\n");
}
}
DEBUG0("Deregistering event handlers");
virConnectDomainEventDeregister(dconn, myDomainEventCallback1);
}
DEBUG0("Closing connection");
if (dconn && virConnectClose(dconn) < 0) {
dbg_printf(1, "error closing libvirt connection\n");
}
out:
free(args->uri);
free(args->path);
free(args);
return NULL;
}
int
start_event_listener(const char *uri, const char *path, int mode, int *wake_fd)
{
struct event_args *args = NULL;
int wake_pipe[2];
virInitialize();
args = malloc(sizeof(*args));
if (!args)
return -1;
memset(args, 0, sizeof(*args));
if (pipe2(wake_pipe, O_CLOEXEC) < 0) {
goto out_fail;
}
if (uri) {
args->uri = strdup(uri);
if (args->uri == NULL)
goto out_fail;
}
if (path) {
args->path = strdup(path);
if (args->path == NULL)
goto out_fail;
}
args->mode = mode;
//args->p_tid = pthread_self();
*wake_fd = wake_pipe[0];
args->wake_fd = wake_pipe[1];
run = 1;
return pthread_create(&event_tid, NULL, event_thread, args);
out_fail:
free(args->uri);
free(args->path);
free(args);
return -1;
}
int
stop_event_listener(void)
{
run = 0;
//pthread_cancel(event_tid);
pthread_join(event_tid, NULL);
event_tid = 0;
return 0;
}
|