direct-io.hg

view tools/libxc/xc_solaris.c @ 12765:2dd4569e0640

[LIBXC] Add an error reporting API to the libxc library.

- An 'xc_error' struct is used to pass around error
details. Currently contains two members 'code' an enumeration of
error types, and 'message' a free text description of the specific
problem.

- The xc_get_last_error() method returns a const pointer to the
internal instance of this struct manged by libxc. By returning a
const pointer we can add extra members to the end of the struct at
any time without worrying about ABI of callers. This will let us
provide more fine-grained info if needed in the future.

- The xc_error instance is statically defined inside libxc and marked
__thread. This ensures that errors are recorded per-thread, and
that when dealing with errors we never need to call malloc - all
storage needed is statically allocated.

- The xc_clear_last_error() method resets any currently recorded
error details

- The xc_error_code_to_desc() method converts the integer error code
into a generic user facing messsage. eg "Invalid kernel". Together
with the 'message' field from xc_error, this provides the user
visible feedback. eg "Invalid kernel: Non PAE-kernel on PAE host."

- A callback can be registered with xc_set_error_handler to receive
notification whenever an error is recorded, rather than querying
for error details after the fact with xc_get_last_error

- If built with -DDEBUG set, a default error handler will be
registered which calls fprintf(stderr), thus maintaining current
behaviour of logging errors to stderr during developer builds.

- The python binding for libxc is updated to use xc_get_last_error
to pull out error details whenever appropriate, instead of
returning info based on 'errno'

- The xc_set_error method is private to libxc internals, and is used
for setting error details

- The ERROR and PERROR macros have been updated to call xc_set_error
automatically specifying XC_INTERNAL_ERROR as the error code. This
gives a generic error report for all current failure points

- Some uses of the ERROR macro have been replaced with explicit
calls to xc_set_error to enable finer grained error reporting. In
particular the code dealing with invalid kernel types uses this
to report about PAE/architecture/wordsize mismatches

The patch has been tested by calling xm create against a varietry of
config files defining invalid kernels of various kinds. It has also
been tested with libvirt talking to xend. In both cases the error
messages were propagated all the way back up the stack.

There is only one place where I need to do further work. The suspend
& restore APIs in Xend invoke external helper programs rather than
calling libxc directly. This means that error details are essentially
lost. Since there is already code in XenD which scans STDERR from
these programs I will investigate adapting this to extract actual
error messages from these helpers.

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
author kfraser@localhost.localdomain
date Thu Dec 07 11:36:26 2006 +0000 (2006-12-07)
parents f7b7daed94d6
children 59b8d5168cc1
line source
1 /******************************************************************************
2 *
3 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2 of the
9 * License.
10 */
12 #include "xc_private.h"
14 #include <xen/memory.h>
15 #include <xen/sys/evtchn.h>
16 #include <unistd.h>
17 #include <fcntl.h>
19 int xc_interface_open(void)
20 {
21 int flags, saved_errno;
22 int fd = open("/dev/xen/privcmd", O_RDWR);
24 if ( fd == -1 )
25 {
26 PERROR("Could not obtain handle on privileged command interface");
27 return -1;
28 }
30 /* Although we return the file handle as the 'xc handle' the API
31 does not specify / guarentee that this integer is in fact
32 a file handle. Thus we must take responsiblity to ensure
33 it doesn't propagate (ie leak) outside the process */
34 if ( (flags = fcntl(fd, F_GETFD)) < 0 )
35 {
36 PERROR("Could not get file handle flags");
37 goto error;
38 }
39 flags |= FD_CLOEXEC;
40 if ( fcntl(fd, F_SETFD, flags) < 0 )
41 {
42 PERROR("Could not set file handle flags");
43 goto error;
44 }
46 return fd;
48 error:
49 saved_errno = errno;
50 close(fd);
51 errno = saved_errno;
52 return -1;
53 }
55 int xc_interface_close(int xc_handle)
56 {
57 return close(xc_handle);
58 }
60 void *xc_map_foreign_batch(int xc_handle, uint32_t dom, int prot,
61 xen_pfn_t *arr, int num)
62 {
63 privcmd_mmapbatch_t ioctlx;
64 void *addr;
65 addr = mmap(NULL, num*PAGE_SIZE, prot, MAP_SHARED, xc_handle, 0);
66 if ( addr == MAP_FAILED )
67 return NULL;
69 ioctlx.num=num;
70 ioctlx.dom=dom;
71 ioctlx.addr=(unsigned long)addr;
72 ioctlx.arr=arr;
73 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
74 {
75 int saved_errno = errno;
76 perror("XXXXXXXX");
77 (void)munmap(addr, num*PAGE_SIZE);
78 errno = saved_errno;
79 return NULL;
80 }
81 return addr;
83 }
85 void *xc_map_foreign_range(int xc_handle, uint32_t dom,
86 int size, int prot,
87 unsigned long mfn)
88 {
89 privcmd_mmap_t ioctlx;
90 privcmd_mmap_entry_t entry;
91 void *addr;
92 addr = mmap(NULL, size, prot, MAP_SHARED, xc_handle, 0);
93 if ( addr == MAP_FAILED )
94 return NULL;
96 ioctlx.num=1;
97 ioctlx.dom=dom;
98 ioctlx.entry=&entry;
99 entry.va=(unsigned long) addr;
100 entry.mfn=mfn;
101 entry.npages=(size+PAGE_SIZE-1)>>PAGE_SHIFT;
102 if ( ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
103 {
104 int saved_errno = errno;
105 (void)munmap(addr, size);
106 errno = saved_errno;
107 return NULL;
108 }
109 return addr;
110 }
112 int xc_map_foreign_ranges(int xc_handle, uint32_t dom,
113 privcmd_mmap_entry_t *entries, int nr)
114 {
115 privcmd_mmap_t ioctlx;
117 ioctlx.num = nr;
118 ioctlx.dom = dom;
119 ioctlx.entry = entries;
121 return ioctl(xc_handle, IOCTL_PRIVCMD_MMAP, &ioctlx);
122 }
124 static int do_privcmd(int xc_handle, unsigned int cmd, unsigned long data)
125 {
126 return ioctl(xc_handle, cmd, data);
127 }
129 int do_xen_hypercall(int xc_handle, privcmd_hypercall_t *hypercall)
130 {
131 return do_privcmd(xc_handle,
132 IOCTL_PRIVCMD_HYPERCALL,
133 (unsigned long)hypercall);
134 }
136 int xc_evtchn_open(void)
137 {
138 int fd;
140 if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
141 {
142 PERROR("Could not open event channel interface");
143 return -1;
144 }
146 return fd;
147 }
149 int xc_evtchn_close(int xce_handle)
150 {
151 return close(xce_handle);
152 }
154 int xc_evtchn_fd(int xce_handle)
155 {
156 return xce_handle;
157 }
159 int xc_evtchn_notify(int xce_handle, evtchn_port_t port)
160 {
161 struct ioctl_evtchn_notify notify;
163 notify.port = port;
165 return ioctl(xce_handle, IOCTL_EVTCHN_NOTIFY, &notify);
166 }
168 evtchn_port_t xc_evtchn_bind_unbound_port(int xce_handle, int domid)
169 {
170 struct ioctl_evtchn_bind_unbound_port bind;
172 bind.remote_domain = domid;
174 return ioctl(xce_handle, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
175 }
177 evtchn_port_t xc_evtchn_bind_interdomain(int xce_handle, int domid,
178 evtchn_port_t remote_port)
179 {
180 struct ioctl_evtchn_bind_interdomain bind;
182 bind.remote_domain = domid;
183 bind.remote_port = remote_port;
185 return ioctl(xce_handle, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
186 }
188 int xc_evtchn_unbind(int xce_handle, evtchn_port_t port)
189 {
190 struct ioctl_evtchn_unbind unbind;
192 unbind.port = port;
194 return ioctl(xce_handle, IOCTL_EVTCHN_UNBIND, &unbind);
195 }
197 evtchn_port_t xc_evtchn_bind_virq(int xce_handle, unsigned int virq)
198 {
199 struct ioctl_evtchn_bind_virq bind;
201 bind.virq = virq;
203 return ioctl(xce_handle, IOCTL_EVTCHN_BIND_VIRQ, &bind);
204 }
206 static int dorw(int fd, char *data, size_t size, int do_write)
207 {
208 size_t offset = 0;
209 ssize_t len;
211 while ( offset < size )
212 {
213 if (do_write)
214 len = write(fd, data + offset, size - offset);
215 else
216 len = read(fd, data + offset, size - offset);
218 if ( len == -1 )
219 {
220 if ( errno == EINTR )
221 continue;
222 return -1;
223 }
225 offset += len;
226 }
228 return 0;
229 }
231 evtchn_port_t xc_evtchn_pending(int xce_handle)
232 {
233 evtchn_port_t port;
235 if ( dorw(xce_handle, (char *)&port, sizeof(port), 0) == -1 )
236 return -1;
238 return port;
239 }
241 int xc_evtchn_unmask(int xce_handle, evtchn_port_t port)
242 {
243 return dorw(xce_handle, (char *)&port, sizeof(port), 1);
244 }