xen-vtx-unstable

view tools/console/daemon/io.c @ 6774:4d899a738d59

merge?
author cl349@firebug.cl.cam.ac.uk
date Tue Sep 13 15:05:49 2005 +0000 (2005-09-13)
parents bd951d23d713 7bc32f4c67fb
children e7c7196fa329 8ca0f98ba8e2
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;
90 u32 oldcons;
92 while ((size = ring->prod - ring->cons) != 0) {
93 if ((buffer->capacity - buffer->size) < size) {
94 buffer->capacity += (size + 1024);
95 buffer->data = realloc(buffer->data, buffer->capacity);
96 if (buffer->data == NULL) {
97 dolog(LOG_ERR, "Memory allocation failed");
98 exit(ENOMEM);
99 }
100 }
102 oldcons = ring->cons;
103 while (ring->cons < (oldcons + size)) {
104 buffer->data[buffer->size] =
105 ring->buf[XENCONS_IDX(ring->cons)];
106 buffer->size++;
107 ring->cons++;
108 }
110 if (buffer->max_capacity &&
111 buffer->size > buffer->max_capacity) {
112 memmove(buffer->data + (buffer->size -
113 buffer->max_capacity),
114 buffer->data, buffer->max_capacity);
115 buffer->data = realloc(buffer->data,
116 buffer->max_capacity);
117 buffer->capacity = buffer->max_capacity;
118 }
119 }
120 }
122 static bool buffer_empty(struct buffer *buffer)
123 {
124 return buffer->size == 0;
125 }
127 static void buffer_advance(struct buffer *buffer, size_t size)
128 {
129 size = MIN(size, buffer->size);
130 memmove(buffer->data, buffer + size, buffer->size - size);
131 buffer->size -= size;
132 }
134 static bool domain_is_valid(int domid)
135 {
136 bool ret;
137 xc_dominfo_t info;
139 ret = (xc_domain_getinfo(xc, domid, 1, &info) == 1 &&
140 info.domid == domid);
142 return ret;
143 }
145 static int domain_create_tty(struct domain *dom)
146 {
147 char *path;
148 int master;
149 bool success;
151 if ((master = getpt()) == -1 ||
152 grantpt(master) == -1 || unlockpt(master) == -1) {
153 dolog(LOG_ERR, "Failed to create tty for domain-%d",
154 dom->domid);
155 master = -1;
156 } else {
157 const char *slave = ptsname(master);
158 struct termios term;
159 char *data;
160 unsigned int len;
162 if (tcgetattr(master, &term) != -1) {
163 cfmakeraw(&term);
164 tcsetattr(master, TCSAFLUSH, &term);
165 }
167 success = asprintf(&path, "%s/tty", dom->conspath) != -1;
168 if (!success)
169 goto out;
170 success = xs_write(xs, path, slave, strlen(slave), O_CREAT);
171 free(path);
172 if (!success)
173 goto out;
175 success = asprintf(&path, "%s/limit", dom->conspath) != -1;
176 if (!success)
177 goto out;
178 data = xs_read(xs, path, &len);
179 if (data) {
180 dom->buffer.max_capacity = strtoul(data, 0, 0);
181 free(data);
182 }
183 free(path);
184 }
186 return master;
187 out:
188 close(master);
189 return -1;
190 }
192 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
193 int xs_gather(struct xs_handle *xs, const char *dir, ...)
194 {
195 va_list ap;
196 const char *name;
197 char *path;
198 int ret = 0;
200 va_start(ap, dir);
201 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
202 const char *fmt = va_arg(ap, char *);
203 void *result = va_arg(ap, void *);
204 char *p;
206 asprintf(&path, "%s/%s", dir, name);
207 p = xs_read(xs, path, NULL);
208 free(path);
209 if (p == NULL) {
210 ret = ENOENT;
211 break;
212 }
213 if (fmt) {
214 if (sscanf(p, fmt, result) == 0)
215 ret = EINVAL;
216 free(p);
217 } else
218 *(char **)result = p;
219 }
220 va_end(ap);
221 return ret;
222 }
224 #define EVENTCHN_BIND _IO('E', 2)
225 #define EVENTCHN_UNBIND _IO('E', 3)
227 static int domain_create_ring(struct domain *dom)
228 {
229 int err, local_port, ring_ref;
231 err = xs_gather(xs, dom->conspath,
232 "ring-ref", "%u", &ring_ref,
233 "port", "%i", &local_port,
234 NULL);
235 if (err)
236 goto out;
238 if (ring_ref != dom->ring_ref) {
239 if (dom->page)
240 munmap(dom->page, getpagesize());
241 dom->page = xc_map_foreign_range(xc, dom->domid, getpagesize(),
242 PROT_READ|PROT_WRITE,
243 (unsigned long)ring_ref);
244 if (dom->page == NULL) {
245 err = EINVAL;
246 goto out;
247 }
248 dom->ring_ref = ring_ref;
249 }
251 if (local_port != dom->local_port) {
252 dom->local_port = -1;
253 if (dom->evtchn_fd != -1)
254 close(dom->evtchn_fd);
255 /* Opening evtchn independently for each console is a bit
256 * wastefule, but that's how the code is structured... */
257 dom->evtchn_fd = open("/dev/xen/evtchn", O_RDWR);
258 if (dom->evtchn_fd == -1) {
259 err = errno;
260 goto out;
261 }
263 if (ioctl(dom->evtchn_fd, EVENTCHN_BIND, local_port) == -1) {
264 err = errno;
265 close(dom->evtchn_fd);
266 dom->evtchn_fd = -1;
267 goto out;
268 }
269 dom->local_port = local_port;
270 }
272 out:
273 return err;
274 }
276 static bool watch_domain(struct domain *dom, bool watch)
277 {
278 char domid_str[3 + MAX_STRLEN(dom->domid)];
279 bool success;
281 sprintf(domid_str, "dom%u", dom->domid);
282 if (watch)
283 success = xs_watch(xs, dom->conspath, domid_str);
284 else
285 success = xs_unwatch(xs, dom->conspath, domid_str);
286 if (success)
287 domain_create_ring(dom);
288 return success;
289 }
291 static struct domain *create_domain(int domid)
292 {
293 struct domain *dom;
294 char *s;
296 dom = (struct domain *)malloc(sizeof(struct domain));
297 if (dom == NULL) {
298 dolog(LOG_ERR, "Out of memory %s:%s():L%d",
299 __FILE__, __FUNCTION__, __LINE__);
300 exit(ENOMEM);
301 }
303 dom->domid = domid;
305 dom->conspath = xs_get_domain_path(xs, dom->domid);
306 if (dom->conspath == NULL)
307 goto out;
308 s = realloc(dom->conspath, strlen(dom->conspath) +
309 strlen("/console") + 1);
310 if (s == NULL)
311 goto out;
312 dom->conspath = s;
313 strcat(dom->conspath, "/console");
315 dom->tty_fd = domain_create_tty(dom);
316 dom->is_dead = false;
317 dom->buffer.data = 0;
318 dom->buffer.size = 0;
319 dom->buffer.capacity = 0;
320 dom->buffer.max_capacity = 0;
321 dom->next = NULL;
323 dom->ring_ref = -1;
324 dom->local_port = -1;
325 dom->page = NULL;
326 dom->evtchn_fd = -1;
328 if (!watch_domain(dom, true))
329 goto out;
331 dom->next = dom_head;
332 dom_head = dom;
334 dolog(LOG_DEBUG, "New domain %d", domid);
336 return dom;
337 out:
338 if (dom->conspath)
339 free(dom->conspath);
340 free(dom);
341 return NULL;
342 }
344 static struct domain *lookup_domain(int domid)
345 {
346 struct domain *dom;
348 for (dom = dom_head; dom; dom = dom->next)
349 if (dom->domid == domid)
350 return dom;
351 return NULL;
352 }
354 static void remove_domain(struct domain *dom)
355 {
356 struct domain **pp;
358 dolog(LOG_DEBUG, "Removing domain-%d", dom->domid);
360 for (pp = &dom_head; *pp; pp = &(*pp)->next) {
361 if (dom == *pp) {
362 *pp = dom->next;
363 free(dom);
364 break;
365 }
366 }
367 }
369 static void cleanup_domain(struct domain *d)
370 {
371 if (!buffer_empty(&d->buffer))
372 return;
374 if (d->buffer.data)
375 free(d->buffer.data);
376 d->buffer.data = NULL;
377 if (d->tty_fd != -1)
378 close(d->tty_fd);
379 d->tty_fd = -1;
380 remove_domain(d);
381 }
383 static void shutdown_domain(struct domain *d)
384 {
385 d->is_dead = true;
386 watch_domain(d, false);
387 if (d->page)
388 munmap(d->page, getpagesize());
389 d->page = NULL;
390 if (d->evtchn_fd != -1)
391 close(d->evtchn_fd);
392 d->evtchn_fd = -1;
393 cleanup_domain(d);
394 }
396 void enum_domains(void)
397 {
398 int domid = 1;
399 xc_dominfo_t dominfo;
400 struct domain *dom;
402 while (xc_domain_getinfo(xc, domid, 1, &dominfo) == 1) {
403 dom = lookup_domain(dominfo.domid);
404 if (dominfo.dying || dominfo.crashed || dominfo.shutdown) {
405 if (dom)
406 shutdown_domain(dom);
407 } else {
408 if (dom == NULL)
409 create_domain(dominfo.domid);
410 }
411 domid = dominfo.domid + 1;
412 }
413 }
415 static void handle_tty_read(struct domain *dom)
416 {
417 ssize_t len;
418 char msg[80];
419 struct ring_head *inring =
420 (struct ring_head *)(dom->page + PAGE_SIZE/2);
421 int i;
423 len = read(dom->tty_fd, msg, MIN(XENCONS_SPACE(inring), sizeof(msg)));
424 if (len < 1) {
425 close(dom->tty_fd);
426 dom->tty_fd = -1;
428 if (domain_is_valid(dom->domid)) {
429 dom->tty_fd = domain_create_tty(dom);
430 } else {
431 shutdown_domain(dom);
432 }
433 } else if (domain_is_valid(dom->domid)) {
434 for (i = 0; i < len; i++) {
435 inring->buf[XENCONS_IDX(inring->prod)] = msg[i];
436 inring->prod++;
437 }
438 xc_evtchn_send(xc, dom->local_port);
439 } else {
440 close(dom->tty_fd);
441 dom->tty_fd = -1;
442 shutdown_domain(dom);
443 }
444 }
446 static void handle_tty_write(struct domain *dom)
447 {
448 ssize_t len;
450 len = write(dom->tty_fd, dom->buffer.data, dom->buffer.size);
451 if (len < 1) {
452 close(dom->tty_fd);
453 dom->tty_fd = -1;
455 if (domain_is_valid(dom->domid)) {
456 dom->tty_fd = domain_create_tty(dom);
457 } else {
458 shutdown_domain(dom);
459 }
460 } else {
461 buffer_advance(&dom->buffer, len);
462 }
463 }
465 static void handle_ring_read(struct domain *dom)
466 {
467 u16 v;
469 if (!read_sync(dom->evtchn_fd, &v, sizeof(v)))
470 return;
472 buffer_append(dom);
474 (void)write_sync(dom->evtchn_fd, &v, sizeof(v));
475 }
477 static void handle_xs(int fd)
478 {
479 char **vec;
480 int domid;
481 struct domain *dom;
483 vec = xs_read_watch(xs);
484 if (!vec)
485 return;
487 if (!strcmp(vec[1], "domlist"))
488 enum_domains();
489 else if (sscanf(vec[1], "dom%u", &domid) == 1) {
490 dom = lookup_domain(domid);
491 if (dom->is_dead == false)
492 domain_create_ring(dom);
493 }
495 xs_acknowledge_watch(xs, vec[1]);
496 free(vec);
497 }
499 void handle_io(void)
500 {
501 fd_set readfds, writefds;
502 int ret;
504 do {
505 struct domain *d, *n;
506 struct timeval tv = { 100, 0 };
507 int max_fd = -1;
509 FD_ZERO(&readfds);
510 FD_ZERO(&writefds);
512 FD_SET(xs_fileno(xs), &readfds);
513 max_fd = MAX(xs_fileno(xs), max_fd);
515 for (d = dom_head; d; d = d->next) {
516 if (d->evtchn_fd != -1) {
517 FD_SET(d->evtchn_fd, &readfds);
518 max_fd = MAX(d->evtchn_fd, max_fd);
519 }
521 if (d->tty_fd != -1) {
522 if (!d->is_dead)
523 FD_SET(d->tty_fd, &readfds);
525 if (!buffer_empty(&d->buffer))
526 FD_SET(d->tty_fd, &writefds);
527 max_fd = MAX(d->tty_fd, max_fd);
528 }
529 }
531 ret = select(max_fd + 1, &readfds, &writefds, 0, &tv);
533 if (FD_ISSET(xs_fileno(xs), &readfds))
534 handle_xs(xs_fileno(xs));
536 for (d = dom_head; d; d = n) {
537 n = d->next;
538 if (d->evtchn_fd != -1 &&
539 FD_ISSET(d->evtchn_fd, &readfds))
540 handle_ring_read(d);
542 if (d->tty_fd != -1) {
543 if (FD_ISSET(d->tty_fd, &readfds))
544 handle_tty_read(d);
546 if (FD_ISSET(d->tty_fd, &writefds))
547 handle_tty_write(d);
549 if (d->is_dead)
550 cleanup_domain(d);
551 }
552 }
553 } while (ret > -1);
554 }