ia64/xen-unstable

view tools/console/daemon/io.c @ 6691:c2f3f18c0d95

Don't leak memory when realloc fails.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Wed Sep 07 19:13:38 2005 +0000 (2005-09-07)
parents 8db9c5873b9b
children a39b1fa10edc
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;
282 char *s;
284 dom = (struct domain *)malloc(sizeof(struct domain));
285 if (dom == NULL) {
286 dolog(LOG_ERR, "Out of memory %s:%s():L%d",
287 __FILE__, __FUNCTION__, __LINE__);
288 exit(ENOMEM);
289 }
291 dom->domid = domid;
292 dom->tty_fd = domain_create_tty(dom);
293 dom->is_dead = false;
294 dom->buffer.data = 0;
295 dom->buffer.size = 0;
296 dom->buffer.capacity = 0;
297 dom->buffer.max_capacity = 0;
298 dom->next = NULL;
300 dom->ring_ref = -1;
301 dom->local_port = -1;
302 dom->page = NULL;
303 dom->evtchn_fd = -1;
305 dom->conspath = NULL;
307 dom->conspath = xs_get_domain_path(xs, dom->domid);
308 if (dom->conspath == NULL)
309 goto out;
310 s = realloc(dom->conspath, strlen(dom->conspath) +
311 strlen("/console") + 1);
312 if (s == NULL)
313 goto out;
314 dom->conspath = s;
315 strcat(dom->conspath, "/console");
317 if (!watch_domain(dom, true))
318 goto out;
320 dom->next = dom_head;
321 dom_head = dom;
323 dolog(LOG_DEBUG, "New domain %d", domid);
325 return dom;
326 out:
327 if (dom->conspath)
328 free(dom->conspath);
329 free(dom);
330 return NULL;
331 }
333 static struct domain *lookup_domain(int domid)
334 {
335 struct domain *dom;
337 for (dom = dom_head; dom; dom = dom->next)
338 if (dom->domid == domid)
339 return dom;
340 return NULL;
341 }
343 static void remove_domain(struct domain *dom)
344 {
345 struct domain **pp;
347 dolog(LOG_DEBUG, "Removing domain-%d", dom->domid);
349 for (pp = &dom_head; *pp; pp = &(*pp)->next) {
350 if (dom == *pp) {
351 *pp = dom->next;
352 free(dom);
353 break;
354 }
355 }
356 }
358 static void cleanup_domain(struct domain *d)
359 {
360 if (!buffer_empty(&d->buffer))
361 return;
363 if (d->buffer.data)
364 free(d->buffer.data);
365 d->buffer.data = NULL;
366 if (d->tty_fd != -1)
367 close(d->tty_fd);
368 d->tty_fd = -1;
369 remove_domain(d);
370 }
372 static void shutdown_domain(struct domain *d)
373 {
374 d->is_dead = true;
375 watch_domain(d, false);
376 if (d->page)
377 munmap(d->page, getpagesize());
378 d->page = NULL;
379 if (d->evtchn_fd != -1)
380 close(d->evtchn_fd);
381 d->evtchn_fd = -1;
382 cleanup_domain(d);
383 }
385 void enum_domains(void)
386 {
387 int domid = 1;
388 xc_dominfo_t dominfo;
389 struct domain *dom;
391 while (xc_domain_getinfo(xc, domid, 1, &dominfo) == 1) {
392 dom = lookup_domain(dominfo.domid);
393 if (dominfo.dying || dominfo.crashed || dominfo.shutdown) {
394 if (dom)
395 shutdown_domain(dom);
396 } else {
397 if (dom == NULL)
398 create_domain(dominfo.domid);
399 }
400 domid = dominfo.domid + 1;
401 }
402 }
404 static void handle_tty_read(struct domain *dom)
405 {
406 ssize_t len;
407 char msg[80];
408 struct ring_head *inring =
409 (struct ring_head *)(dom->page + PAGE_SIZE/2);
410 int i;
412 len = read(dom->tty_fd, msg, MIN(XENCONS_SPACE(inring), sizeof(msg)));
413 if (len < 1) {
414 close(dom->tty_fd);
415 dom->tty_fd = -1;
417 if (domain_is_valid(dom->domid)) {
418 dom->tty_fd = domain_create_tty(dom);
419 } else {
420 shutdown_domain(dom);
421 }
422 } else if (domain_is_valid(dom->domid)) {
423 for (i = 0; i < len; i++) {
424 inring->buf[XENCONS_IDX(inring->prod)] = msg[i];
425 inring->prod++;
426 }
427 xc_evtchn_send(xc, dom->local_port);
428 } else {
429 close(dom->tty_fd);
430 dom->tty_fd = -1;
431 shutdown_domain(dom);
432 }
433 }
435 static void handle_tty_write(struct domain *dom)
436 {
437 ssize_t len;
439 len = write(dom->tty_fd, dom->buffer.data, dom->buffer.size);
440 if (len < 1) {
441 close(dom->tty_fd);
442 dom->tty_fd = -1;
444 if (domain_is_valid(dom->domid)) {
445 dom->tty_fd = domain_create_tty(dom);
446 } else {
447 shutdown_domain(dom);
448 }
449 } else {
450 buffer_advance(&dom->buffer, len);
451 }
452 }
454 static void handle_ring_read(struct domain *dom)
455 {
456 u16 v;
458 if (!read_sync(dom->evtchn_fd, &v, sizeof(v)))
459 return;
461 buffer_append(dom);
463 (void)write_sync(dom->evtchn_fd, &v, sizeof(v));
464 }
466 static void handle_xs(int fd)
467 {
468 char **vec;
469 int domid;
470 struct domain *dom;
472 vec = xs_read_watch(xs);
473 if (!vec)
474 return;
476 if (!strcmp(vec[1], "domlist"))
477 enum_domains();
478 else if (sscanf(vec[1], "dom%u", &domid) == 1) {
479 dom = lookup_domain(domid);
480 if (dom->is_dead == false)
481 domain_create_ring(dom);
482 }
484 xs_acknowledge_watch(xs, vec[1]);
485 free(vec);
486 }
488 void handle_io(void)
489 {
490 fd_set readfds, writefds;
491 int ret;
493 do {
494 struct domain *d, *n;
495 struct timeval tv = { 100, 0 };
496 int max_fd = -1;
498 FD_ZERO(&readfds);
499 FD_ZERO(&writefds);
501 FD_SET(xs_fileno(xs), &readfds);
502 max_fd = MAX(xs_fileno(xs), max_fd);
504 for (d = dom_head; d; d = d->next) {
505 if (d->evtchn_fd != -1) {
506 FD_SET(d->evtchn_fd, &readfds);
507 max_fd = MAX(d->evtchn_fd, max_fd);
508 }
510 if (d->tty_fd != -1) {
511 if (!d->is_dead)
512 FD_SET(d->tty_fd, &readfds);
514 if (!buffer_empty(&d->buffer))
515 FD_SET(d->tty_fd, &writefds);
516 max_fd = MAX(d->tty_fd, max_fd);
517 }
518 }
520 ret = select(max_fd + 1, &readfds, &writefds, 0, &tv);
522 if (FD_ISSET(xs_fileno(xs), &readfds))
523 handle_xs(xs_fileno(xs));
525 for (d = dom_head; d; d = n) {
526 n = d->next;
527 if (d->evtchn_fd != -1 &&
528 FD_ISSET(d->evtchn_fd, &readfds))
529 handle_ring_read(d);
531 if (d->tty_fd != -1) {
532 if (FD_ISSET(d->tty_fd, &readfds))
533 handle_tty_read(d);
535 if (FD_ISSET(d->tty_fd, &writefds))
536 handle_tty_write(d);
538 if (d->is_dead)
539 cleanup_domain(d);
540 }
541 }
542 } while (ret > -1);
543 }