ia64/xen-unstable

view tools/xenstore/fake_libxc.c @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents 06d84bf87159
children 76a7a7aa27e4
line source
1 /*
2 Fake libxc which doesn't require hypervisor but talks to xs_test.
3 Copyright (C) 2005 Rusty Russell IBM Corporation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <sys/mman.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include "utils.h"
30 #include "xenstored_core.h"
31 #include "xenstored_domain.h"
32 #include "xenstored_test.h"
34 static int sigfd;
35 static int xs_test_pid;
36 static u16 port;
38 /* The event channel maps to a signal, shared page to an mmapped file. */
39 int xc_evtchn_send(int xc_handle __attribute__((unused)), int local_port)
40 {
41 assert(local_port == port);
42 if (kill(xs_test_pid, SIGUSR2) != 0)
43 barf_perror("fake event channel failed");
44 return 0;
45 }
47 void *xc_map_foreign_range(int xc_handle, u32 dom __attribute__((unused)),
48 int size, int prot,
49 unsigned long mfn __attribute__((unused)))
50 {
51 void *ret;
53 ret = mmap(NULL, size, prot, MAP_SHARED, xc_handle, 0);
54 if (ret == MAP_FAILED)
55 return NULL;
57 /* xs_test tells us pid and port by putting it in buffer, we reply. */
58 xs_test_pid = *(int *)(ret + 32);
59 port = *(int *)(ret + 36);
60 *(int *)(ret + 32) = getpid();
61 return ret;
62 }
64 int xc_interface_open(void)
65 {
66 int fd;
67 char page[getpagesize()];
69 fd = open("/tmp/xcmap", O_RDWR|O_CREAT|O_TRUNC, 0600);
70 if (fd < 0)
71 return fd;
73 memset(page, 0, sizeof(page));
74 if (!xs_write_all(fd, page, sizeof(page)))
75 barf_perror("Failed to write /tmp/xcmap page");
77 return fd;
78 }
80 int xc_interface_close(int xc_handle)
81 {
82 close(xc_handle);
83 return 0;
84 }
86 int xc_domain_getinfo(int xc_handle __attribute__((unused)),
87 u32 first_domid, unsigned int max_doms,
88 xc_dominfo_t *info)
89 {
90 assert(max_doms == 1);
91 info->domid = first_domid;
93 info->dying = 0;
94 info->shutdown = 0;
95 info->paused = 0;
96 info->blocked = 0;
97 info->running = 1;
99 info->shutdown_reason = 0;
101 if ( info->shutdown && (info->shutdown_reason == SHUTDOWN_crash) )
102 {
103 info->shutdown = 0;
104 info->crashed = 1;
105 }
107 return 1;
108 }
110 int xc_evtchn_bind_virq(int xc_handle __attribute__((unused)),
111 int virq __attribute__((unused)),
112 int *port)
113 {
114 if (port)
115 *port = 0;
116 return 0;
117 }
119 static void send_to_fd(int signo __attribute__((unused)))
120 {
121 int saved_errno = errno;
122 write(sigfd, &port, sizeof(port));
123 errno = saved_errno;
124 }
126 void fake_block_events(void)
127 {
128 signal(SIGUSR2, SIG_IGN);
129 }
131 void fake_ack_event(void)
132 {
133 signal(SIGUSR2, send_to_fd);
134 }
136 int fake_open_eventchn(void)
137 {
138 int fds[2];
140 if (pipe(fds) != 0)
141 return -1;
143 if (signal(SIGUSR2, send_to_fd) == SIG_ERR) {
144 int saved_errno = errno;
145 close(fds[0]);
146 close(fds[1]);
147 errno = saved_errno;
148 return -1;
149 }
150 sigfd = fds[1];
151 return fds[0];
152 }