File: mdns-publish-vnc.c

package info (click to toggle)
xenwatch 0.5.4-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 488 kB
  • sloc: ansic: 7,167; sh: 36; makefile: 4
file content (238 lines) | stat: -rw-r--r-- 5,612 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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <sys/syslog.h>
#include <sys/utsname.h>

#include <xs.h>

#include "list.h"
#include "xs_tools.h"
#include "mdns-publish.h"

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

struct dom {
    int                    domid;
    char                   name[128];
    int                    destroyed;

    char                   label[256];
    int                    vnc_port;
    int                    published;
    struct mdns_pub_entry  *entry;
    
    struct list_head       next;
};

static LIST_HEAD(doms);
static struct utsname uts;

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

static struct dom *find_dom(int domid)
{
    struct dom *dom;
    struct list_head *item;

    list_for_each(item, &doms) {
	dom = list_entry(item, struct dom, next);
	if (dom->domid == domid)
	    return dom;
    }
    return NULL;
}

static struct dom *get_dom(int domid)
{
    struct dom *dom;

    dom = find_dom(domid);
    if (!dom) {
	dom = malloc(sizeof(*dom));
	memset(dom,0,sizeof(*dom));
	dom->domid = domid;
	list_add_tail(&dom->next, &doms);
    }
    return dom;
}

static void try_attach_domain(struct mdns_pub *mdns, struct dom *dom, int boot)
{
    if (dom->published)
	return;
    if (!strlen(dom->name))
	return;
    if (!dom->vnc_port)
	return;

    snprintf(dom->label, sizeof(dom->label), "Xen guest %s (%s, #%d)",
	     dom->name, uts.nodename, dom->domid);
    mdns_log_printf(mdns, LOG_INFO, "pub: %s (%d)\n", dom->name, dom->domid);
    dom->entry = mdns_pub_add(mdns, dom->label, "_rfb._tcp", dom->vnc_port, NULL);

    dom->published = 1;
}

static void try_release_domain(struct mdns_pub *mdns, struct dom *dom)
{
    if (!dom->destroyed)
	return;
    if (dom->published) {
	mdns_log_printf(mdns, LOG_INFO, "del: %s (%d)\n", dom->name, dom->domid);
	mdns_pub_del(dom->entry);
    }
    list_del(&dom->next);
    free(dom);
}

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

static void usage(FILE *fp)
{
    fprintf(fp, "usage: [fixme]\n");
}

int main(int argc, char *argv[])
{
    struct mdns_pub *mdns = NULL;
    struct xs_handle *xenstore = NULL;
    xs_transaction_t xst;
    char **vec = NULL;
    int domid, c;
    int debug = 0;
    char path[128], value[128];
    unsigned int count, i, rc;
    struct dom *dom;
    fd_set set;

    for (;;) {
        if (-1 == (c = getopt(argc, argv, "hd")))
            break;
        switch (c) {

	case 'd':
	    debug++;
	    break;

        case 'h':
            usage(stdout);
            exit(0);
        default:
            usage(stderr);
            exit(1);
        }
    }

    mdns_pub_appname = "mdns-publish-vnc";
    uname(&uts);
    if (!debug)
	mdns_daemonize();

    /* connect to xenstore */
    xenstore = xenstore_open(1,1,1,1);
    if (NULL == xenstore) {
	mdns_log_printf(mdns, LOG_ERR, "can't access xenstore, exiting\n");
	exit(1);
    }
    xs_watch(xenstore, "/local/domain", "token");

    mdns = mdns_pub_init(debug);
    if (NULL == mdns)
	exit(1);
    mdns_sigsetup(mdns);
    mdns_pub_start(mdns);

    /* look for running domains */
    if (!(xst = xs_transaction_start(xenstore))) {
	mdns_log_printf(mdns, LOG_ERR, "Oops, can't start xenstore transaction\n");
	exit(1);
    }
    vec = xs_directory(xenstore, xst, "/local/domain", &count);
    xs_transaction_end(xenstore, xst, 0);

    fprintf(stderr,"looking for existing domains\n");
    for (i = 0; i < count; i++) {
	domid = atoi(vec[i]);
	dom = get_dom(domid);
	snprintf(path, sizeof(path), "/local/domain/%d/name", domid);
	xenstore_read(xenstore, path, dom->name, sizeof(dom->name));
	snprintf(path, sizeof(path), "/local/domain/%d/console/vnc-port", domid);
	if (0 == xenstore_read(xenstore, path, value, sizeof(value)))
	    dom->vnc_port = atoi(value);
	try_attach_domain(mdns, dom, 1);
    }

    /* main loop */
    fprintf(stderr,"ok, watching out for changes now\n");
    for (;;) {
	if (mdns_pub_appquit)
	    break;

	FD_ZERO(&set);
	FD_SET(xs_fileno(xenstore), &set);
	switch (select(xs_fileno(xenstore)+1, &set, NULL, NULL, NULL)) {
	case -1:
	    if (EINTR == errno)
		continue; /* termsig check */
	    perror("select");
	    break;
	case 0:
	    fprintf(stderr,"Huh, select() timeout?\n");
	    mdns_pub_appquit++;
	    break;
	default:
	    break;
	}
	
	if (vec)
	    free(vec);
	vec = xs_read_watch(xenstore, &count);
	if (NULL == vec) {
	    fprintf(stderr,"xs_read_watch() failed\n");
	    exit(1);
	}
	if (2 != sscanf(vec[XS_WATCH_PATH], "/local/domain/%d/%64s", &domid, path)) {
	    if (1 != sscanf(vec[XS_WATCH_PATH], "/local/domain/%d", &domid))
		continue;
	    strcpy(path, "");
	}
	dom = get_dom(domid);

	if (0 == strcmp(path,"")) {
	    rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
	    if (0 != rc)
		dom->destroyed = 1;

	} else if (0 == strcmp(path, "name")) {
	    rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
	    if (0 != rc)
		continue;
	    strcpy(dom->name, value);
	    mdns_log_printf(mdns, LOG_INFO, "new: %s (%d)\n", dom->name, dom->domid);

	} else if (0 == strcmp(path, "console/vnc-port")) {
	    rc = xenstore_read(xenstore, vec[XS_WATCH_PATH], value, sizeof(value));
	    if (0 != rc)
		continue;
	    dom->vnc_port = atoi(value);

	} else {
	    continue;

	}

	try_attach_domain(mdns, dom, 0);
	try_release_domain(mdns, dom);
    }

    mdns_pub_del_all(mdns);
    mdns_pub_stop(mdns);
    mdns_pub_fini(mdns);
    return 0;
}