xen-vtx-unstable

view tools/console/daemon/io.c @ 6649:8db9c5873b9b

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 06 18:29:53 2005 +0000 (2005-09-06)
parents 83bcc68aaf32 d4d69c509371
children c2f3f18c0d95 7bc32f4c67fb
line source
1 /*\
2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Anthony Liguori <aliguori@us.ibm.com>
4 *
5 * Xen Console Daemon
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 \*/
21 #define _GNU_SOURCE
23 #include "utils.h"
24 #include "io.h"
26 #include "xenctrl.h"
27 #include "xs.h"
28 #include "xen/io/domain_controller.h"
30 #include <malloc.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <sys/select.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <termios.h>
38 #include <stdarg.h>
39 #include <sys/ioctl.h>
40 #include <sys/mman.h>
42 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
43 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
45 /* Each 10 bits takes ~ 3 digits, plus one, plus one for nul terminator. */
46 #define MAX_STRLEN(x) ((sizeof(x) * CHAR_BIT + CHAR_BIT-1) / 10 * 3 + 2)
48 struct buffer
49 {
50 char *data;
51 size_t size;
52 size_t capacity;
53 size_t max_capacity;
54 };
56 struct domain
57 {
58 int domid;
59 int tty_fd;
60 bool is_dead;
61 struct buffer buffer;
62 struct domain *next;
63 char *conspath;
64 int ring_ref;
65 int local_port;
66 char *page;
67 int evtchn_fd;
68 };
70 static struct domain *dom_head;
72 struct ring_head
73 {
74 u32 cons;
75 u32 prod;
76 char buf[0];
77 } __attribute__((packed));
79 #define PAGE_SIZE (getpagesize())
80 #define XENCONS_RING_SIZE (PAGE_SIZE/2 - sizeof (struct ring_head))
81 #define XENCONS_IDX(cnt) ((cnt) % XENCONS_RING_SIZE)
82 #define XENCONS_FULL(ring) (((ring)->prod - (ring)->cons) == XENCONS_RING_SIZE)
83 #define XENCONS_SPACE(ring) (XENCONS_RING_SIZE - ((ring)->prod - (ring)->cons))
85 static void buffer_append(struct domain *dom)
86 {
87 struct buffer *buffer = &dom->buffer;
88 struct ring_head *ring = (struct ring_head *)dom->page;
89 size_t size;
91 while ((size = ring->prod - ring->cons) != 0) {
92 if ((buffer->capacity - buffer->size) < size) {
93 buffer->capacity += (size + 1024);
94 buffer->data = realloc(buffer->data, buffer->capacity);
95 if (buffer->data == NULL) {
96 dolog(LOG_ERR, "Memory allocation failed");
97 exit(ENOMEM);
98 }
99 }
101 while (ring->cons < ring->prod) {
102 buffer->data[buffer->size] =
103 ring->buf[XENCONS_IDX(ring->cons)];
104 buffer->size++;
105 ring->cons++;
106 }
108 if (buffer->max_capacity &&
109 buffer->size > buffer->max_capacity) {
110 memmove(buffer->data + (buffer->size -
111 buffer->max_capacity),
112 buffer->data, buffer->max_capacity);
113 buffer->data = realloc(buffer->data,
114 buffer->max_capacity);
115 buffer->capacity = buffer->max_capacity;
116 }
117 }
118 }
120 static bool buffer_empty(struct buffer *buffer)
121 {
122 return buffer->size == 0;
123 }
125 static void buffer_advance(struct buffer *buffer, size_t size)
126 {
127 size = MIN(size, buffer->size);
128 memmove(buffer->data, buffer + size, buffer->size - size);
129 buffer->size -= size;
130 }
132 static bool domain_is_valid(int domid)
133 {
134 bool ret;
135 xc_dominfo_t info;
137 ret = (xc_domain_getinfo(xc, domid, 1, &info) == 1 &&
138 info.domid == domid);
140 return ret;
141 }
143 static int domain_create_tty(struct domain *dom)
144 {
145 char *path;
146 int master;
148 if ((master = getpt()) == -1 ||
149 grantpt(master) == -1 || unlockpt(master) == -1) {
150 dolog(LOG_ERR, "Failed to create tty for domain-%d",
151 dom->domid);
152 master = -1;
153 } else {
154 const char *slave = ptsname(master);
155 struct termios term;
156 char *data;
157 unsigned int len;
159 if (tcgetattr(master, &term) != -1) {
160 cfmakeraw(&term);
161 tcsetattr(master, TCSAFLUSH, &term);
162 }
164 asprintf(&path, "/console/%d/tty", dom->domid);
165 xs_write(xs, path, slave, strlen(slave), O_CREAT);
166 free(path);
168 asprintf(&path, "/console/%d/limit", dom->domid);
169 data = xs_read(xs, path, &len);
170 if (data) {
171 dom->buffer.max_capacity = strtoul(data, 0, 0);
172 free(data);
173 }
174 free(path);
175 }
177 return master;
178 }
180 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
181 int xs_gather(struct xs_handle *xs, const char *dir, ...)
182 {
183 va_list ap;
184 const char *name;
185 char *path;
186 int ret = 0;
188 va_start(ap, dir);
189 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
190 const char *fmt = va_arg(ap, char *);
191 void *result = va_arg(ap, void *);
192 char *p;
194 asprintf(&path, "%s/%s", dir, name);
195 p = xs_read(xs, path, NULL);
196 free(path);
197 if (p == NULL) {
198 ret = ENOENT;
199 break;
200 }
201 if (fmt) {
202 if (sscanf(p, fmt, result) == 0)
203 ret = EINVAL;
204 free(p);
205 } else
206 *(char **)result = p;
207 }
208 va_end(ap);
209 return ret;
210 }
212 #define EVENTCHN_BIND _IO('E', 2)
213 #define EVENTCHN_UNBIND _IO('E', 3)
215 static int domain_create_ring(struct domain *dom)
216 {
217 int err, local_port, ring_ref;
219 err = xs_gather(xs, dom->conspath,
220 "ring-ref", "%u", &ring_ref,
221 "console_channel/port1", "%i", &local_port,
222 NULL);
223 if (err)
224 goto out;
226 if (ring_ref != dom->ring_ref) {
227 if (dom->page)
228 munmap(dom->page, getpagesize());
229 dom->page = xc_map_foreign_range(xc, dom->domid, getpagesize(),
230 PROT_READ|PROT_WRITE,
231 (unsigned long)ring_ref);
232 if (dom->page == NULL) {
233 err = EINVAL;
234 goto out;
235 }
236 dom->ring_ref = ring_ref;
237 }
239 if (local_port != dom->local_port) {
240 dom->local_port = -1;
241 if (dom->evtchn_fd != -1)
242 close(dom->evtchn_fd);
243 /* Opening evtchn independently for each console is a bit
244 * wastefule, but that's how the code is structured... */
245 dom->evtchn_fd = open("/dev/xen/evtchn", O_RDWR);
246 if (dom->evtchn_fd == -1) {
247 err = errno;
248 goto out;
249 }
251 if (ioctl(dom->evtchn_fd, EVENTCHN_BIND, local_port) == -1) {
252 err = errno;
253 close(dom->evtchn_fd);
254 dom->evtchn_fd = -1;
255 goto out;
256 }
257 dom->local_port = local_port;
258 }
260 out:
261 return err;
262 }
264 static bool watch_domain(struct domain *dom, bool watch)
265 {
266 char domid_str[3 + MAX_STRLEN(dom->domid)];
267 bool success;
269 sprintf(domid_str, "dom%u", dom->domid);
270 if (watch)
271 success = xs_watch(xs, dom->conspath, domid_str);
272 else
273 success = xs_unwatch(xs, dom->conspath, domid_str);
274 if (success)
275 domain_create_ring(dom);
276 return success;
277 }
279 static struct domain *create_domain(int domid)
280 {
281 struct domain *dom;
283 dom = (struct domain *)malloc(sizeof(struct domain));
284 if (dom == NULL) {
285 dolog(LOG_ERR, "Out of memory %s:%s():L%d",
286 __FILE__, __FUNCTION__, __LINE__);
287 exit(ENOMEM);
288 }
290 dom->domid = domid;
291 dom->tty_fd = domain_create_tty(dom);
292 dom->is_dead = false;
293 dom->buffer.data = 0;
294 dom->buffer.size = 0;
295 dom->buffer.capacity = 0;
296 dom->buffer.max_capacity = 0;
297 dom->next = NULL;
299 dom->ring_ref = -1;
300 dom->local_port = -1;
301 dom->page = NULL;
302 dom->evtchn_fd = -1;
304 dom->conspath = NULL;
306 dom->conspath = xs_get_domain_path(xs, dom->domid);
307 if (dom->conspath == NULL)
308 goto out;
309 dom->conspath = realloc(dom->conspath, strlen(dom->conspath) +
310 strlen("/console") + 1);
311 if (dom->conspath == NULL)
312 goto out;
313 strcat(dom->conspath, "/console");
315 if (!watch_domain(dom, true))
316 goto out;
318 dom->next = dom_head;
319 dom_head = dom;
321 dolog(LOG_DEBUG, "New domain %d", domid);
323 return dom;
324 out:
325 if (dom->conspath)
326 free(dom->conspath);
327 free(dom);
328 return NULL;
329 }
331 static struct domain *lookup_domain(int domid)
332 {
333 struct domain *dom;
335 for (dom = dom_head; dom; dom = dom->next)
336 if (dom->domid == domid)
337 return dom;
338 return NULL;
339 }
341 static void remove_domain(struct domain *dom)
342 {
343 struct domain **pp;
345 dolog(LOG_DEBUG, "Removing domain-%d", dom->domid);
347 for (pp = &dom_head; *pp; pp = &(*pp)->next) {
348 if (dom == *pp) {
349 *pp = dom->next;
350 free(dom);
351 break;
352 }
353 }
354 }
356 static void cleanup_domain(struct domain *d)
357 {
358 if (!buffer_empty(&d->buffer))
359 return;
361 if (d->buffer.data)
362 free(d->buffer.data);
363 d->buffer.data = NULL;
364 if (d->tty_fd != -1)
365 close(d->tty_fd);
366 d->tty_fd = -1;
367 remove_domain(d);
368 }
370 static void shutdown_domain(struct domain *d)
371 {
372 d->is_dead = true;
373 watch_domain(d, false);
374 if (d->page)
375 munmap(d->page, getpagesize());
376 d->page = NULL;
377 if (d->evtchn_fd != -1)
378 close(d->evtchn_fd);
379 d->evtchn_fd = -1;
380 cleanup_domain(d);
381 }
383 void enum_domains(void)
384 {
385 int domid = 1;
386 xc_dominfo_t dominfo;
387 struct domain *dom;
389 while (xc_domain_getinfo(xc, domid, 1, &dominfo) == 1) {
390 dom = lookup_domain(dominfo.domid);
391 if (dominfo.dying || dominfo.crashed || dominfo.shutdown) {
392 if (dom)
393 shutdown_domain(dom);
394 } else {
395 if (dom == NULL)
396 create_domain(dominfo.domid);
397 }
398 domid = dominfo.domid + 1;
399 }
400 }
402 static void handle_tty_read(struct domain *dom)
403 {
404 ssize_t len;
405 char msg[80];
406 struct ring_head *inring =
407 (struct ring_head *)(dom->page + PAGE_SIZE/2);
408 int i;
410 len = read(dom->tty_fd, msg, MIN(XENCONS_SPACE(inring), sizeof(msg)));
411 if (len < 1) {
412 close(dom->tty_fd);
413 dom->tty_fd = -1;
415 if (domain_is_valid(dom->domid)) {
416 dom->tty_fd = domain_create_tty(dom);
417 } else {
418 shutdown_domain(dom);
419 }
420 } else if (domain_is_valid(dom->domid)) {
421 for (i = 0; i < len; i++) {
422 inring->buf[XENCONS_IDX(inring->prod)] = msg[i];
423 inring->prod++;
424 }
425 xc_evtchn_send(xc, dom->local_port);
426 } else {
427 close(dom->tty_fd);
428 dom->tty_fd = -1;
429 shutdown_domain(dom);
430 }
431 }
433 static void handle_tty_write(struct domain *dom)
434 {
435 ssize_t len;
437 len = write(dom->tty_fd, dom->buffer.data, dom->buffer.size);
438 if (len < 1) {
439 close(dom->tty_fd);
440 dom->tty_fd = -1;
442 if (domain_is_valid(dom->domid)) {
443 dom->tty_fd = domain_create_tty(dom);
444 } else {
445 shutdown_domain(dom);
446 }
447 } else {
448 buffer_advance(&dom->buffer, len);
449 }
450 }
452 static void handle_ring_read(struct domain *dom)
453 {
454 u16 v;
456 if (!read_sync(dom->evtchn_fd, &v, sizeof(v)))
457 return;
459 buffer_append(dom);
461 (void)write_sync(dom->evtchn_fd, &v, sizeof(v));
462 }
464 static void handle_xs(int fd)
465 {
466 char **vec;
467 int domid;
468 struct domain *dom;
470 vec = xs_read_watch(xs);
471 if (!vec)
472 return;
474 if (!strcmp(vec[1], "domlist"))
475 enum_domains();
476 else if (sscanf(vec[1], "dom%u", &domid) == 1) {
477 dom = lookup_domain(domid);
478 if (dom->is_dead == false)
479 domain_create_ring(dom);
480 }
482 xs_acknowledge_watch(xs, vec[1]);
483 free(vec);
484 }
486 void handle_io(void)
487 {
488 fd_set readfds, writefds;
489 int ret;
491 do {
492 struct domain *d, *n;
493 struct timeval tv = { 100, 0 };
494 int max_fd = -1;
496 FD_ZERO(&readfds);
497 FD_ZERO(&writefds);
499 FD_SET(xs_fileno(xs), &readfds);
500 max_fd = MAX(xs_fileno(xs), max_fd);
502 for (d = dom_head; d; d = d->next) {
503 if (d->evtchn_fd != -1) {
504 FD_SET(d->evtchn_fd, &readfds);
505 max_fd = MAX(d->evtchn_fd, max_fd);
506 }
508 if (d->tty_fd != -1) {
509 if (!d->is_dead)
510 FD_SET(d->tty_fd, &readfds);
512 if (!buffer_empty(&d->buffer))
513 FD_SET(d->tty_fd, &writefds);
514 max_fd = MAX(d->tty_fd, max_fd);
515 }
516 }
518 ret = select(max_fd + 1, &readfds, &writefds, 0, &tv);
520 if (FD_ISSET(xs_fileno(xs), &readfds))
521 handle_xs(xs_fileno(xs));
523 for (d = dom_head; d; d = n) {
524 n = d->next;
525 if (d->evtchn_fd != -1 &&
526 FD_ISSET(d->evtchn_fd, &readfds))
527 handle_ring_read(d);
529 if (d->tty_fd != -1) {
530 if (FD_ISSET(d->tty_fd, &readfds))
531 handle_tty_read(d);
533 if (FD_ISSET(d->tty_fd, &writefds))
534 handle_tty_write(d);
536 if (d->is_dead)
537 cleanup_domain(d);
538 }
539 }
540 } while (ret > -1);
541 }