source: trunk/packages/xen-common/xen-common/tools/xenstore/fake_libxc.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 17 years ago

Add xen and xen-common

File size: 3.3 KB
Line 
1/*
2    Fake libxc which doesn't require hypervisor but talks to xs_test.
3    Copyright (C) 2005 Rusty Russell IBM Corporation
4
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.
9
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.
14
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*/
19
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"
33#include <xenctrl.h>
34
35static int sigfd;
36static int xs_test_pid;
37static evtchn_port_t port;
38
39/* The event channel maps to a signal, shared page to an mmapped file. */
40void xc_evtchn_notify(int xce_handle, int local_port)
41{
42        assert(local_port == port);
43        if (kill(xs_test_pid, SIGUSR2) != 0)
44                barf_perror("fake event channel failed");
45}
46
47void *xc_map_foreign_range(int xc_handle, uint32_t dom __attribute__((unused)),
48                           int size, int prot,
49                           unsigned long mfn __attribute__((unused)))
50{
51        void *ret;
52
53        ret = mmap(NULL, size, prot, MAP_SHARED, xc_handle, 0);
54        if (ret == MAP_FAILED)
55                return NULL;
56
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}
63
64int xc_interface_open(void)
65{
66        int fd;
67        char page[getpagesize()];
68
69        fd = open("/tmp/xcmap", O_RDWR|O_CREAT|O_TRUNC, 0600);
70        if (fd < 0)
71                return fd;
72
73        memset(page, 0, sizeof(page));
74        if (!xs_write_all(fd, page, sizeof(page)))
75                barf_perror("Failed to write /tmp/xcmap page");
76       
77        return fd;
78}
79
80int xc_interface_close(int xc_handle)
81{
82        close(xc_handle);
83        return 0;
84}
85
86int xc_domain_getinfo(int xc_handle __attribute__((unused)),
87                      uint32_t first_domid, unsigned int max_doms,
88                      xc_dominfo_t *info)
89{
90        assert(max_doms == 1);
91        info->domid = first_domid;
92
93        info->dying    = 0;
94        info->shutdown = 0;
95        info->paused   = 0;
96        info->blocked  = 0;
97        info->running  = 1;
98
99        info->shutdown_reason = 0;
100
101        if ( info->shutdown && (info->shutdown_reason == SHUTDOWN_crash) )
102        {
103            info->shutdown = 0;
104            info->crashed  = 1;
105        }
106
107        return 1;
108}
109
110static void send_to_fd(int signo __attribute__((unused)))
111{
112        int saved_errno = errno;
113        write(sigfd, &port, sizeof(port));
114        errno = saved_errno;
115}
116
117void fake_block_events(void)
118{
119        signal(SIGUSR2, SIG_IGN);
120}
121
122void fake_ack_event(void)
123{
124        signal(SIGUSR2, send_to_fd);
125}
126
127int xc_evtchn_open(void)
128{
129        int fds[2];
130
131        if (pipe(fds) != 0)
132                return -1;
133
134        if (signal(SIGUSR2, send_to_fd) == SIG_ERR) {
135                int saved_errno = errno;
136                close(fds[0]);
137                close(fds[1]);
138                errno = saved_errno;
139                return -1;
140        }
141        sigfd = fds[1];
142        return fds[0];
143}
Note: See TracBrowser for help on using the repository browser.