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
|
#define _GNU_SOURCE
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/signal.h>
#include <xs.h>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/thread-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
/* --------------------------------------------------------------------- */
static AvahiEntryGroup *group = NULL;
static AvahiThreadedPoll *thread_poll = NULL;
static struct xs_handle *xenstore;
static int debug = 0;
static int have_tty = 1;
static int have_syslog = 0;
static char *appname = "mdns-publish-xendom";
static int app_quit = 0;
static int termsig = 0;
static char *name;
static char *service = "_xendom._tcp";
static int port = 9;
static char vm_uuid[256];
static char dom_id[256];
/* --------------------------------------------------------------------- */
static int __attribute__ ((format (printf, 2, 0)))
log_printf(int priority, char *fmt, ...)
{
va_list args;
char msgbuf[1024];
int rc;
va_start(args, fmt);
rc = vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
va_end(args);
if (have_tty)
fprintf(stderr, "%s: %s", appname, msgbuf);
if (have_syslog)
syslog(priority, "%s", msgbuf);
return rc;
}
/* --------------------------------------------------------------------- */
static void create_services(AvahiClient *c);
static void entry_group_callback(AvahiEntryGroup *g,
AvahiEntryGroupState state,
void *userdata)
{
char *n;
switch (state) {
case AVAHI_ENTRY_GROUP_ESTABLISHED:
log_printf(LOG_INFO, "service '%s' successfully established\n", name);
break;
case AVAHI_ENTRY_GROUP_COLLISION:
n = avahi_alternative_service_name(name);
log_printf(LOG_NOTICE, "service name collision, renaming '%s' to '%s'\n",
name, n);
avahi_free(name);
name = n;
create_services(avahi_entry_group_get_client(g));
break;
case AVAHI_ENTRY_GROUP_FAILURE:
app_quit = 1;
break;
default:
break;
}
}
static void create_services(AvahiClient *c)
{
int ret;
/* If this is the first time we're called, let's create a new entry group */
if (!group) {
if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
log_printf(LOG_ERR, "avahi_entry_group_new() failed: %s\n",
avahi_strerror(avahi_client_errno(c)));
goto fail;
}
}
/* Add the service */
ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0,
name, service, NULL, NULL, port,
vm_uuid, dom_id, NULL);
if (ret < 0) {
log_printf(LOG_ERR, "failed to add '%s' service: %s\n", service, avahi_strerror(ret));
goto fail;
}
/* Tell the server to register the service */
if ((ret = avahi_entry_group_commit(group)) < 0) {
log_printf(LOG_ERR, "failed to commit entry_group: %s\n", avahi_strerror(ret));
goto fail;
}
return;
fail:
app_quit = 1;
}
static void client_callback(AvahiClient *c,
AvahiClientState state,
void * userdata)
{
int error;
switch (state) {
case AVAHI_CLIENT_CONNECTING:
log_printf(LOG_NOTICE, "avahi daemon not running (yet), I'll keep trying ...\n");
break;
case AVAHI_CLIENT_S_RUNNING:
if (!group)
create_services(c);
break;
case AVAHI_CLIENT_S_COLLISION:
if (group)
avahi_entry_group_reset(group);
break;
case AVAHI_CLIENT_FAILURE:
switch (avahi_client_errno(c)) {
case AVAHI_ERR_DISCONNECTED:
avahi_entry_group_free(group);
avahi_client_free(c);
group = NULL;
log_printf(LOG_NOTICE, "disconnected from avahi daemon, reconnecting ...\n");
c = avahi_client_new(avahi_threaded_poll_get(thread_poll),
AVAHI_CLIENT_NO_FAIL,
client_callback, NULL, &error);
if (!c) {
log_printf(LOG_ERR, "failed to create client: %s\n",
avahi_strerror(error));
goto fail;
}
break;
default:
log_printf(LOG_ERR, "client failure: %s\n",
avahi_strerror(avahi_client_errno(c)));
goto fail;
break;
}
break;
default:
break;
}
return;
fail:
app_quit = 1;
}
/* --------------------------------------------------------------------- */
static int xen_init(void)
{
struct stat st;
if (-1 == stat("/proc/xen", &st)) {
log_printf(LOG_ERR, "/proc/xen not found, running on bare metal?\n");
return -1;
}
xenstore = xs_domain_open();
if (NULL == xenstore) {
log_printf(LOG_ERR, "can't connect to xenstore (%s)\n", xs_domain_dev());
return -1;
}
return 0;
}
static int xen_get_info(void)
{
xs_transaction_t xst;
char *xs_value;
if (!(xst = xs_transaction_start(xenstore))) {
log_printf(LOG_ERR, "can't start xenstore transaction\n");
goto out;
}
xs_value = xs_read(xenstore, xst, "vm", NULL);
xs_transaction_end(xenstore, xst, 0);
if (!xs_value) {
log_printf(LOG_ERR, "can't read 'vm' value from xenstore\n");
goto out;
}
snprintf(vm_uuid, sizeof(vm_uuid), "vm-uuid=%s", xs_value+4);
if (!(xst = xs_transaction_start(xenstore))) {
log_printf(LOG_ERR, "can't start xenstore transaction\n");
goto out;
}
xs_value = xs_read(xenstore, xst, "domid", NULL);
xs_transaction_end(xenstore, xst, 0);
if (!xs_value) {
log_printf(LOG_ERR, "can't read 'domid' value from xenstore\n");
goto out;
}
snprintf(dom_id, sizeof(dom_id), "dom-id=%s", xs_value);
return 0;
out:
return -1;
}
static int xen_watch_add(char *path)
{
int ret = 0;
/* Hmm, not working ... */
if (!xs_watch(xenstore, path, "token")) {
log_printf(LOG_ERR, "%s: xs_watch for \"%s\" failed\n",
__FUNCTION__, path);
ret = -1;
}
return ret;
}
static int xen_watch_data(void)
{
char **vec = NULL;
unsigned int count;
vec = xs_read_watch(xenstore, &count);
log_printf(LOG_DEBUG, "%s: \"%s\"\n", __FUNCTION__, vec[XS_WATCH_PATH]);
if (vec)
free(vec);
return 0;
}
/* --------------------------------------------------------------------- */
static void usage(FILE *fp)
{
fprintf(fp,
"This little daemon publishes xen domain info,\n"
"via mDNS (using avahi), as service '_xendom._tcp'.\n"
"\n"
"usage: %s [options]\n"
"options:\n"
" -h print this text\n"
" -d enable debug mode\n"
"\n"
"-- \n"
"(c) 2006 Gerd Hoffmann <kraxel@suse.de>\n",
appname);
}
static int wait_fd(int fd, int secs)
{
struct timeval tv;
fd_set rd;
int rc;
FD_ZERO(&rd);
FD_SET(fd,&rd);
tv.tv_sec = secs;
tv.tv_usec = 0;
rc = select(fd+1, &rd, NULL, NULL, &tv);
if (-1 == rc) {
if (EINTR == errno)
return 0;
}
return rc;
}
static void catchsig(int signal)
{
termsig = signal;
app_quit = 1;
}
int main(int argc, char*argv[])
{
struct sigaction act,old;
AvahiClient *client = NULL;
char buf[128];
struct utsname uts;
int error,c;
int ret = 1;
/* parse options */
for (;;) {
if (-1 == (c = getopt(argc, argv, "hd")))
break;
switch (c) {
case 'd':
debug = 1;
break;
case 'h':
usage(stdout);
exit(0);
default:
usage(stderr);
exit(1);
}
}
/* open syslog */
openlog(appname, 0, LOG_LOCAL0);
have_syslog = 1;
/* figure name */
uname(&uts);
snprintf(buf, sizeof(buf), "Xen domain %s", uts.nodename);
name = avahi_strdup(buf);
/* figure domain info */
if (0 != xen_init())
goto fail;
xen_get_info();
/* prepare nDNS bits */
thread_poll = avahi_threaded_poll_new();
if (!thread_poll) {
log_printf(LOG_ERR, "failed to create simple poll object\n");
goto fail;
}
client = avahi_client_new(avahi_threaded_poll_get(thread_poll),
AVAHI_CLIENT_NO_FAIL,
client_callback, NULL, &error);
if (!client) {
log_printf(LOG_ERR, "failed to create client: %s\n", avahi_strerror(error));
goto fail;
}
/* setup signal handler */
memset(&act,0,sizeof(act));
sigemptyset(&act.sa_mask);
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE,&act,&old);
act.sa_handler = catchsig;
sigaction(SIGHUP,&act,&old);
sigaction(SIGUSR1,&act,&old);
sigaction(SIGTERM,&act,&old);
if (debug)
sigaction(SIGINT,&act,&old);
/* damonize */
if (!debug) {
switch (fork()) {
case -1:
log_printf(LOG_ERR, "fork: %s", strerror(errno));
goto fail;
case 0:
/* child */
close(0); close(1); close(2);
setsid();
open("/dev/null", O_RDWR); dup(0); dup(0);
have_tty = 0;
break;
default:
/* parent */
exit(0);
}
}
/* enter main loop */
ret = 0;
avahi_threaded_poll_start(thread_poll);
if (0) {
/* hmm, not working */
if (0 != xen_watch_add("memory/target"))
goto fail;
xen_watch_add("vm");
xen_watch_add("domid");
}
for (;;) {
if (app_quit)
break;
switch (wait_fd(xs_fileno(xenstore),1)) {
case -1:
log_printf(LOG_ERR, "select: %s\n", strerror(errno));
app_quit = 1;
case 0:
/* timeout */
break;
default:
/* data to read */
xen_watch_data();
}
}
avahi_threaded_poll_stop(thread_poll);
fail:
log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)%s%s\n", ret,
termsig ? ", on signal " : "",
termsig ? strsignal(termsig) : "");
/* Cleanup things */
if (client)
avahi_client_free(client);
if (thread_poll)
avahi_threaded_poll_free(thread_poll);
avahi_free(name);
return ret;
}
|