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
|
#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 <xs.h>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/alternative.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
/* --------------------------------------------------------------------- */
static AvahiEntryGroup *group = NULL;
static AvahiSimplePoll *simple_poll = NULL;
static int debug = 0;
static int have_tty = 1;
static int have_syslog = 0;
static char *appname = "mdns-publish-xendom";
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:
avahi_simple_poll_quit(simple_poll);
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:
avahi_simple_poll_quit(simple_poll);
}
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_simple_poll_get(simple_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:
avahi_simple_poll_quit(simple_poll);
}
static int update_xendom_info(void)
{
struct xs_handle *xenstore;
xs_transaction_t xst;
char *xs_value;
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;
}
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 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);
}
int main(int argc, char*argv[])
{
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 != update_xendom_info())
goto fail;
/* prepare nDNS bits */
simple_poll = avahi_simple_poll_new();
if (!simple_poll) {
log_printf(LOG_ERR, "failed to create simple poll object\n");
goto fail;
}
client = avahi_client_new(avahi_simple_poll_get(simple_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;
}
/* 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);
}
}
/* go ahead */
avahi_simple_poll_loop(simple_poll);
ret = 0;
fail:
log_printf(ret ? LOG_ERR : LOG_INFO, "exiting (%d)\n", ret);
/* Cleanup things */
if (client)
avahi_client_free(client);
if (simple_poll)
avahi_simple_poll_free(simple_poll);
avahi_free(name);
return ret;
}
|