File: mdns-publish-xendom.c

package info (click to toggle)
xenwatch 0.5.4-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 428 kB
  • ctags: 741
  • sloc: ansic: 7,167; sh: 36; makefile: 4
file content (245 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download | duplicates (4)
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
#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 "mdns-publish.h"

/* --------------------------------------------------------------------- */

static struct xs_handle *xenstore;

static int  debug        = 0;
static char *appname     = "mdns-publish-xendom";

static struct utsname uts;
static char *service     = "_xendom._tcp";
static int  port         = 9;
static char vm_uuid[256];
static char dom_id[256];

static struct mdns_pub       *mdns;
static struct mdns_pub_entry *entry;

/* --------------------------------------------------------------------- */

static int xen_init(void)
{
    struct stat st;

    if (-1 == stat("/proc/xen", &st)) {
	mdns_log_printf(mdns, LOG_ERR,
			"/proc/xen not found, running on bare metal?\n");
	return -1;
    }

    xenstore = xs_domain_open();
    if (NULL == xenstore) {
	mdns_log_printf(mdns, 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))) {
	mdns_log_printf(mdns, 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) {
	mdns_log_printf(mdns, 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))) {
	mdns_log_printf(mdns, 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) {
	mdns_log_printf(mdns, 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")) {
	mdns_log_printf(mdns, LOG_ERR, "%s: xs_watch for \"%s\" failed\n",
			__FUNCTION__, path);
	ret = -1;
    }
    return ret;
}

static int xen_publish(void)
{
    static int count = 1;
    char buf[128];

    if (debug)
	snprintf(buf, sizeof(buf), "Xen domain %s (%d)", uts.nodename, count++);
    else
	snprintf(buf, sizeof(buf), "Xen domain %s", uts.nodename);

    if (entry)
	mdns_pub_del(entry);
    entry = mdns_pub_add(mdns, buf, service, port,
			 vm_uuid, dom_id, NULL);
    return 0;
}

static int xen_watch_data(void)
{
    char **vec = NULL;
    unsigned int count;
    
    vec = xs_read_watch(xenstore, &count);
    mdns_log_printf(mdns, LOG_DEBUG, "%s: \"%s\"\n", __FUNCTION__, vec[XS_WATCH_PATH]);
    xen_get_info();
    xen_publish();
    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@redhat.com>\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;
}

int main(int argc, char*argv[])
{
    int ret = 1;
    int c;

    /* 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);
        }
    }

    /* figure name */
    uname(&uts);
    mdns_pub_appname = appname;
    if (!debug)
	mdns_daemonize();

    mdns = mdns_pub_init(debug);
    if (NULL == mdns)
	goto fail;
    mdns_sigsetup(mdns);

    /* figure domain info */
    if (0 != xen_init())
	goto fail;
    xen_get_info();

    xen_watch_add("vm");
    xen_watch_add("domid");

    /* enter main loop */
    ret = 0;
    mdns_pub_start(mdns);
    xen_publish();

    for (;;) {
	if (mdns_pub_appquit)
	    break;
	switch (wait_fd(xs_fileno(xenstore),1)) {
	case -1:
            mdns_log_printf(mdns, LOG_ERR, "select: %s\n", strerror(errno));
	    mdns_pub_appquit = 1;
	case 0:
	    /* timeout */
	    break;
	default:
	    /* data to read */
	    xen_watch_data();
	}
    }

    mdns_pub_del_all(mdns);
    mdns_pub_stop(mdns);

fail:
    mdns_log_printf(mdns, ret ? LOG_ERR : LOG_INFO, "exiting (%d)%s%s\n", ret,
		    mdns_pub_termsig ? ", on signal " : "",
		    mdns_pub_termsig ? strsignal(mdns_pub_termsig) : "");

    mdns_pub_fini(mdns);
    return ret;
}