ia64/xen-unstable

view tools/console/daemon/io.c @ 6695:7bc32f4c67fb

merge?
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 21:34:17 2005 +0000 (2005-09-07)
parents a39b1fa10edc 8db9c5873b9b
children b2f4823b6ff0 4d018790ff8a bd951d23d713 4d899a738d59 e7c7196fa329
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;
147 bool success;
149 if ((master = getpt()) == -1 ||
150 grantpt(master) == -1 || unlockpt(master) == -1) {
151 dolog(LOG_ERR, "Failed to create tty for domain-%d",
152 dom->domid);
153 master = -1;
154 } else {
155 const char *slave = ptsname(master);
156 struct termios term;
157 char *data;
158 unsigned int len;
160 if (tcgetattr(master, &term) != -1) {
161 cfmakeraw(&term);
162 tcsetattr(master, TCSAFLUSH, &term);
163 }
165 success = asprintf(&path, "%s/tty", dom->conspath) != -1;
166 if (!success)
167 goto out;
168 success = xs_write(xs, path, slave, strlen(slave), O_CREAT);
169 free(path);
170 if (!success)
171 goto out;
173 success = asprintf(&path, "%s/limit", dom->conspath) != -1;
174 if (!success)
175 goto out;
176 data = xs_read(xs, path, &len);
177 if (data) {
178 dom->buffer.max_capacity = strtoul(data, 0, 0);
179 free(data);
180 }
181 free(path);
182 }
184 return master;
185 out:
186 close(master);
187 return -1;
188 }
190 /* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
191 int xs_gather(struct xs_handle *xs, const char *dir, ...)
192 {
193 va_list ap;
194 const char *name;
195 char *path;
196 int ret = 0;
198 va_start(ap, dir);
199 while (ret == 0 && (name = va_arg(ap, char *)) != NULL) {
200 const char *fmt = va_arg(ap, char *);
201 void *result = va_arg(ap, void *);
202 char *p;
204 asprintf(&path, "%s/%s", dir, name);
205 p = xs_read(xs, path, NULL);
206 free(path);
207 if (p == NULL) {
208 ret = ENOENT;
209 break;
210 }
211 if (fmt) {
212 if (sscanf(p, fmt, result) == 0)
213 ret = EINVAL;
214 free(p);
215 } else
216 *(char **)result = p;
217 }
218 va_end(ap);
219 return ret;
220 }
222 #define EVENTCHN_BIND _IO('E', 2)
223 #define EVENTCHN_UNBIND _IO('E', 3)
225 static int domain_create_ring(struct domain *dom)
226 {
227 int err, local_port, ring_ref;
229 err = xs_gather(xs, dom->conspath,
230 "ring-ref", "%u", &ring_ref,
231 "port", "%i", &local_port,
232 NULL);
233 if (err)
234 goto out;
236 if (ring_ref != dom->ring_ref) {
237 if (dom->page)
238 munmap(dom->page, getpagesize());
239 dom->page = xc_map_foreign_range(xc, dom->domid, getpagesize(),
240 PROT_READ|PROT_WRITE,
241 (unsigned long)ring_ref);
242 if (dom->page == NULL) {
243 err = EINVAL;
244 goto out;
245 }
246 dom->ring_ref = ring_ref;
247 }
249 if (local_port != dom->local_port) {
250 dom->local_port = -1;
251 if (dom->evtchn_fd != -1)
252 close(dom->evtchn_fd);
253 /* Opening evtchn independently for each console is a bit
254 * wastefule, but that's how the code is structured... */
255 dom->evtchn_fd = open("/dev/xen/evtchn", O_RDWR);
256 if (dom->evtchn_fd == -1) {
257 err = errno;
258 goto out;
259 }
261 if (ioctl(dom->evtchn_fd, EVENTCHN_BIND, local_port) == -1) {
262 err = errno;
263 close(dom->evtchn_fd);
264 dom->evtchn_fd = -1;
265 goto out;
266 }
267 dom->local_port = local_port;
268 }
270 out:
271 return err;
272 }
274 static bool watch_domain(struct domain *dom, bool watch)
275 {
276 char domid_str[3 + MAX_STRLEN(dom->domid)];
277 bool success;
279 sprintf(domid_str, "dom%u", dom->domid);
280 if (watch)
281 success = xs_watch(xs, dom->conspath, domid_str);
282 else
283 success = xs_unwatch(xs, dom->conspath, domid_str);
284 if (success)
285 domain_create_ring(dom);
286 return success;
287 }
289 static struct domain *create_domain(int domid)
290 {
291 struct domain *dom;
292 char *s;
294 dom = (struct domain *)malloc(sizeof(struct domain));
295 if (dom == NULL) {
296 dolog(LOG_ERR, "Out of memory %s:%s():L%d",
297 __FILE__, __FUNCTION__, __LINE__);
298 exit(ENOMEM);
299 }
301 dom->domid = domid;
303 dom->conspath = xs_get_domain_path(xs, dom->domid);
304 if (dom->conspath == NULL)
305 goto out;
306 s = realloc(dom->conspath, strlen(dom->conspath) +
307 strlen("/console") + 1);
308 if (s == NULL)
309 goto out;
310 dom->conspath = s;
311 strcat(dom->conspath, "/console");
313 dom->tty_fd = domain_create_tty(dom);
314 dom->is_dead = false;
315 dom->buffer.data = 0;
316 dom->buffer.size = 0;
317 dom->buffer.capacity = 0;
318 dom->buffer.max_capacity = 0;
319 dom->next = NULL;
321 dom->ring_ref = -1;
322 dom->local_port = -1;
323 dom->page = NULL;
324 dom->evtchn_fd = -1;
326 if (!watch_domain(dom, true))
327 goto out;
329 dom->next = dom_head;
330 dom_head = dom;
332 dolog(LOG_DEBUG, "New domain %d", domid);
334 return dom;
335 out:
336 if (dom->conspath)
337 free(dom->conspath);
338 free(dom);
339 return NULL;
340 }
342 static struct domain *lookup_domain(int domid)
343 {
344 struct domain *dom;
346 for (dom = dom_head; dom; dom = dom->next)
347 if (dom->domid == domid)
348 return dom;
349 return NULL;
350 }
352 static void remove_domain(struct domain *dom)
353 {
354 struct domain **pp;
356 dolog(LOG_DEBUG, "Removing domain-%d", dom->domid);
358 for (pp = &dom_head; *pp; pp = &(*pp)->next) {
359 if (dom == *pp) {
360 *pp = dom->next;
361 free(dom);
362 break;
363 }
364 }
365 }
367 static void cleanup_domain(struct domain *d)
368 {
369 if (!buffer_empty(&d->buffer))
370 return;
372 if (d->buffer.data)
373 free(d->buffer.data);
374 d->buffer.data = NULL;
375 if (d->tty_fd != -1)
376 close(d->tty_fd);
377 d->tty_fd = -1;
378 remove_domain(d);
379 }
381 static void shutdown_domain(struct domain *d)
382 {
383 d->is_dead = true;
384 watch_domain(d, false);
385 if (d->page)
386 munmap(d->page, getpagesize());
387 d->page = NULL;
388 if (d->evtchn_fd != -1)
389 close(d->evtchn_fd);
390 d->evtchn_fd = -1;
391 cleanup_domain(d);
392 }
394 void enum_domains(void)
395 {
396 int domid = 1;
397 xc_dominfo_t dominfo;
398 struct domain *dom;
400 while (xc_domain_getinfo(xc, domid, 1, &dominfo) == 1) {
401 dom = lookup_domain(dominfo.domid);
402 if (dominfo.dying || dominfo.crashed || dominfo.shutdown) {
403 if (dom)
404 shutdown_domain(dom);
405 } else {
406 if (dom == NULL)
407 create_domain(dominfo.domid);
408 }
409 domid = dominfo.domid + 1;
410 }
411 }
413 static void handle_tty_read(struct domain *dom)
414 {
415 ssize_t len;
416 char msg[80];
417 struct ring_head *inring =
418 (struct ring_head *)(dom->page + PAGE_SIZE/2);
419 int i;
421 len = read(dom->tty_fd, msg, MIN(XENCONS_SPACE(inring), sizeof(msg)));
422 if (len < 1) {
423 close(dom->tty_fd);
424 dom->tty_fd = -1;
426 if (domain_is_valid(dom->domid)) {
427 dom->tty_fd = domain_create_tty(dom);
428 } else {
429 shutdown_domain(dom);
430 }
431 } else if (domain_is_valid(dom->domid)) {
432 for (i = 0; i < len; i++) {
433 inring->buf[XENCONS_IDX(inring->prod)] = msg[i];
434 inring->prod++;
435 }
436 xc_evtchn_send(xc, dom->local_port);
437 } else {
438 close(dom->tty_fd);
439 dom->tty_fd = -1;
440 shutdown_domain(dom);
441 }
442 }
444 static void handle_tty_write(struct domain *dom)
445 {
446 ssize_t len;
448 len = write(dom->tty_fd, dom->buffer.data, dom->buffer.size);
449 if (len < 1) {
450 close(dom->tty_fd);
451 dom->tty_fd = -1;
453 if (domain_is_valid(dom->domid)) {
454 dom->tty_fd = domain_create_tty(dom);
455 } else {
456 shutdown_domain(dom);
457 }
458 } else {
459 buffer_advance(&dom->buffer, len);
460 }
461 }
463 static void handle_ring_read(struct domain *dom)
464 {
465 u16 v;
467 if (!read_sync(dom->evtchn_fd, &v, sizeof(v)))
468 return;
470 buffer_append(dom);
472 (void)write_sync(dom->evtchn_fd, &v, sizeof(v));
473 }
475 static void handle_xs(int fd)
476 {
477 char **vec;
478 int domid;
479 struct domain *dom;
481 vec = xs_read_watch(xs);
482 if (!vec)
483 return;
485 if (!strcmp(vec[1], "domlist"))
486 enum_domains();
487 else if (sscanf(vec[1], "dom%u", &domid) == 1) {
488 dom = lookup_domain(domid);
489 if (dom->is_dead == false)
490 domain_create_ring(dom);
491 }
493 xs_acknowledge_watch(xs, vec[1]);
494 free(vec);
495 }
497 void handle_io(void)
498 {
499 fd_set readfds, writefds;
500 int ret;
502 do {
503 struct domain *d, *n;
504 struct timeval tv = { 100, 0 };
505 int max_fd = -1;
507 FD_ZERO(&readfds);
508 FD_ZERO(&writefds);
510 FD_SET(xs_fileno(xs), &readfds);
511 max_fd = MAX(xs_fileno(xs), max_fd);
513 for (d = dom_head; d; d = d->next) {
514 if (d->evtchn_fd != -1) {
515 FD_SET(d->evtchn_fd, &readfds);
516 max_fd = MAX(d->evtchn_fd, max_fd);
517 }
519 if (d->tty_fd != -1) {
520 if (!d->is_dead)
521 FD_SET(d->tty_fd, &readfds);
523 if (!buffer_empty(&d->buffer))
524 FD_SET(d->tty_fd, &writefds);
525 max_fd = MAX(d->tty_fd, max_fd);
526 }
527 }
529 ret = select(max_fd + 1, &readfds, &writefds, 0, &tv);
531 if (FD_ISSET(xs_fileno(xs), &readfds))
532 handle_xs(xs_fileno(xs));
534 for (d = dom_head; d; d = n) {
535 n = d->next;
536 if (d->evtchn_fd != -1 &&
537 FD_ISSET(d->evtchn_fd, &readfds))
538 handle_ring_read(d);
540 if (d->tty_fd != -1) {
541 if (FD_ISSET(d->tty_fd, &readfds))
542 handle_tty_read(d);
544 if (FD_ISSET(d->tty_fd, &writefds))
545 handle_tty_write(d);
547 if (d->is_dead)
548 cleanup_domain(d);
549 }
550 }
551 } while (ret > -1);
552 }