xen-vtx-unstable

view tools/xenstore/fake_libxc.c @ 5427:d44caae57112

bitkeeper revision 1.1708 (42a9b4dc79oJAGtbQvr8mTDofvBdxQ)

Merge bk://xen.bkbits.net/xeno-unstable.bk
into bkbits.net:/repos/x/xen-ia64/xeno-unstable-ia64.bk
author xen-ia64.adm@bkbits.net
date Fri Jun 10 15:42:20 2005 +0000 (2005-06-10)
parents 6d3e8f90c2df 61f0cf22d5cd
children f1bb1316b26f f804b28871ba 4d899a738d59 e7c7196fa329
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 static void send_to_fd(int signo __attribute__((unused)))
87 {
88 int saved_errno = errno;
89 write(sigfd, &port, sizeof(port));
90 errno = saved_errno;
91 }
93 void fake_block_events(void)
94 {
95 signal(SIGUSR2, SIG_IGN);
96 }
98 void fake_ack_event(void)
99 {
100 signal(SIGUSR2, send_to_fd);
101 }
103 int fake_open_eventchn(void)
104 {
105 int fds[2];
107 if (pipe(fds) != 0)
108 return -1;
110 if (signal(SIGUSR2, send_to_fd) == SIG_ERR) {
111 int saved_errno = errno;
112 close(fds[0]);
113 close(fds[1]);
114 errno = saved_errno;
115 return -1;
116 }
117 sigfd = fds[1];
118 return fds[0];
119 }