ia64/xen-unstable

view tools/xfrd/xen_domain.c @ 4556:c35967b5863a

bitkeeper revision 1.1159.258.100 (4264223aKtYgG3NrSKfbwM7nfkpGcg)

Fix curldebug function in xendomain.c of xfrd. Function takes argument
"size_t buf_num" and uses it as the field width to print, that has to be
an int. While size_t on 32 bit systems is 4bytes (or size of an int) it
is 8bytes on 64bit systems (not size of an int). So this function should
just use "int buf_num". This function is not used in the code anywhere.
This will fix compilation on x86-64.

Signed-off-by: Jerone Young <jyoung5@us.ibm.com>
author iap10@freefall.cl.cam.ac.uk
date Mon Apr 18 21:10:18 2005 +0000 (2005-04-18)
parents fff34e3e2b40
children 4b759824a678 18d709f72233
line source
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
5 #ifdef _XEN_XFR_STUB_
6 typedef unsigned long u32;
7 #else
8 #include "xc.h"
9 #include "xc_io.h"
10 #endif
12 #include "xen_domain.h"
13 #include "marshal.h"
14 #include "xdr.h"
15 #include "xfrd.h"
17 #define MODULE_NAME "XFRD"
18 #define DEBUG 1
19 #undef DEBUG
20 #include "debug.h"
22 int domain_suspend(void *data, u32 dom){
23 int err = 0;
24 Conn *xend = data;
26 dprintf("> dom=%lu data=%p\n", dom, data);
27 err = xfr_vm_suspend(xend, dom);
28 dprintf("< err=%d\n", err);
29 return err;
30 }
32 int domain_configure(void *data, u32 dom, char *vmconfig, int vmconfig_n){
33 return xen_domain_configure(dom, vmconfig, vmconfig_n);
34 }
36 #ifndef _XEN_XFR_STUB_
37 static int xc_handle = 0;
39 int xcinit(void){
40 if(xc_handle <= 0){
41 xc_handle = xc_interface_open();
42 }
43 dprintf("< xc_handle=%d\n", xc_handle);
44 return xc_handle;
45 }
47 void xcfini(void){
48 if(xc_handle > 0){
49 xc_interface_close(xc_handle);
50 xc_handle = 0;
51 }
52 }
53 #endif
55 /** Write domain state.
56 *
57 * At some point during this the domain is suspended, and then there's no way back.
58 * Even if something later goes wrong we can't restart the domain.
59 */
60 int xen_domain_snd(Conn *xend, IOStream *io,
61 uint32_t dom,
62 char *vmconfig, int vmconfig_n,
63 int live, int resource){
64 int err = 0;
65 #ifdef _XEN_XFR_STUB_
66 char buf[1024];
67 int n, k, d, buf_n;
68 dprintf("> dom=%d\n", dom);
69 err = marshal_uint32(io, dom);
70 if(err) goto exit;
71 err = marshal_string(io, vmconfig, vmconfig_n);
72 if(err) goto exit;
73 n = 32 * 1024 * 1024;
74 n = 32 * 1024;
75 buf_n = sizeof(buf);
76 err = marshal_uint32(io, n);
77 for(k = 0; k < n; k += d){
78 d = n - k;
79 if(d > buf_n) d = buf_n;
80 err = marshal_bytes(io, buf, d);
81 if(err) goto exit;
82 dprintf("> k=%d n=%d\n", k, n);
83 }
85 dom = 99;
86 err = domain_suspend(xend, dom);
87 IOStream_close(io);
88 exit:
89 #else
90 XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt;
91 ioctxt->domain = dom;
92 ioctxt->io = io;
93 ioctxt->info = iostdout;
94 ioctxt->err = iostderr;
95 ioctxt->data = xend;
96 ioctxt->suspend = domain_suspend;
97 ioctxt->vmconfig = vmconfig;
98 ioctxt->vmconfig_n = vmconfig_n;
99 if(live){
100 ioctxt->flags |= XCFLAGS_LIVE;
101 }
102 ioctxt->resource = resource;
103 err = xc_linux_save(xcinit(), ioctxt);
104 #endif
105 dprintf("< err=%d\n", err);
106 return err;
107 }
109 /** Receive domain state.
110 * Create a new domain and store the received state into it.
111 */
112 int xen_domain_rcv(IOStream *io,
113 uint32_t *dom,
114 char **vmconfig, int *vmconfig_n,
115 int *configured){
116 int err = 0;
117 #ifdef _XEN_XFR_STUB_
118 char buf[1024];
119 int n, k, d, buf_n;
120 dprintf(">\n");
121 err = unmarshal_uint32(io, dom);
122 if(err) goto exit;
123 err = unmarshal_new_string(io, vmconfig, vmconfig_n);
124 if(err) goto exit;
125 err = unmarshal_uint32(io, &n);
126 buf_n = sizeof(buf);
127 for(k = 0; k < n; k += d){
128 d = n - k;
129 if(d > buf_n) d = buf_n;
130 err = unmarshal_bytes(io, buf, d);
131 if(err) goto exit;
132 dprintf("> k=%d n=%d\n", k, n);
133 }
134 exit:
135 #else
136 XcIOContext _ioctxt = {}, *ioctxt = &_ioctxt;
137 dprintf(">\n");
138 ioctxt->io = io;
139 ioctxt->info = iostdout;
140 ioctxt->err = iostderr;
141 ioctxt->configure = domain_configure;
142 if ( !*configured )
143 ioctxt->flags |= XCFLAGS_CONFIGURE;
145 err = xc_linux_restore(xcinit(), ioctxt);
146 *dom = ioctxt->domain;
147 *vmconfig = ioctxt->vmconfig;
148 *vmconfig_n = ioctxt->vmconfig_n;
149 *configured = (ioctxt->flags & XCFLAGS_CONFIGURE);
150 #endif
151 dprintf("< err=%d\n", err);
152 return err;
153 }
155 #include <curl/curl.h>
156 #include "http.h"
158 /** Flag indicating whether we need to initialize libcurl.
159 */
160 static int do_curl_global_init = 1;
162 /** Get a curl handle, initializing libcurl if needed.
163 *
164 * @return curl handle
165 */
166 static CURL *curlinit(void){
167 if(do_curl_global_init){
168 do_curl_global_init = 0;
169 // Stop libcurl using the proxy. There's a curl option to
170 // set the proxy - but no option to defeat it.
171 unsetenv("http_proxy");
172 curl_global_init(CURL_GLOBAL_ALL);
173 }
174 return curl_easy_init();
175 }
177 /** Curl debug function.
178 */
179 int curldebug(CURL *curl, curl_infotype ty, char *buf, int buf_n, void *data){
180 // printf("%*s\n", buf_n, buf); /* Does not compile correctly on non 32bit platforms */
181 fwrite(data, buf_n, 1, stdout);
182 printf("\n");
183 return 0;
184 }
186 /** Setup a curl handle with a url.
187 * Creates the url by formatting 'fmt' and the remaining arguments.
188 *
189 * @param pcurl return parameter for the curl handle
190 * @param url url buffer
191 * @param url_n size of url
192 * @param fmt url format string, followed by parameters
193 * @return 0 on success, error code otherwise
194 */
195 static int curlsetup(CURL **pcurl, struct curl_slist **pheaders, char *url, int url_n, char *fmt, ...){
196 int err = 0;
197 va_list args;
198 CURL *curl = NULL;
199 struct curl_slist *headers = NULL;
200 int n = 0;
202 curl = curlinit();
203 if(!curl){
204 eprintf("> Could not init libcurl\n");
205 err = -ENOMEM;
206 goto exit;
207 }
208 url_n -= 1;
209 va_start(args, fmt);
210 n = vsnprintf(url, url_n, fmt, args);
211 va_end(args);
212 if(n <= 0 || n >= url_n){
213 err = -ENOMEM;
214 eprintf("> Out of memory in url\n");
215 goto exit;
216 }
217 dprintf("> url=%s\n", url);
218 #if DEBUG
219 // Verbose.
220 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
221 // Call the debug function on data received.
222 curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, curldebug);
223 #else
224 // No progress meter.
225 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
226 // Completely quiet.
227 curl_easy_setopt(curl, CURLOPT_MUTE, 1);
228 #endif
229 // Set the URL.
230 curl_easy_setopt(curl, CURLOPT_URL, url);
232 headers = curl_slist_append(headers, "Expect:");
233 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
235 exit:
236 if(err && curl){
237 curl_easy_cleanup(curl);
238 curl = NULL;
239 }
240 *pcurl = curl;
241 if (pheaders)
242 *pheaders = headers;
243 return err;
244 }
246 static void curlcleanup(CURL **pcurl, struct curl_slist **pheaders){
247 if (*pcurl)
248 curl_easy_cleanup(*pcurl);
249 if (*pheaders)
250 curl_slist_free_all(*pheaders);
251 *pcurl = NULL;
252 *pheaders = NULL;
253 }
254 /** Make the http request stored in the curl handle and get
255 * the result code from the curl code and the http return code.
256 *
257 * @param curl curl handle
258 * @return 0 for success, error code otherwise
259 */
260 int curlresult(CURL *curl){
261 int err = 0;
262 CURLcode curlcode = 0;
263 long httpcode = 0;
265 curlcode = curl_easy_perform(curl);
266 if(curlcode){
267 eprintf("> curlcode=%d\n", curlcode);
268 err = -EINVAL;
269 goto exit;
270 }
271 curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
272 if(httpcode != HTTP_OK){
273 eprintf("> httpcode=%d\n", (int)httpcode);
274 err = -EINVAL;
275 goto exit;
276 }
277 exit:
278 return err;
279 }
281 /** Get xend to list domains.
282 * We use this to force xend to refresh its domain list.
283 *
284 * @return 0 on success, error code otherwise
285 */
286 int xen_domain_ls(void){
287 int err = 0;
288 CURL *curl = NULL;
289 struct curl_slist *headers = NULL;
290 char url[128] = {};
291 int url_n = sizeof(url);
293 dprintf(">\n");
294 err = curlsetup(&curl, &headers, url, url_n, "http://localhost:%d/xend/domain", XEND_PORT);
295 if(err) goto exit;
296 err = curlresult(curl);
297 exit:
298 curlcleanup(&curl, &headers);
299 dprintf("< err=%d\n", err);
300 return err;
301 }
303 /** Get xend to configure a new domain.
304 *
305 * @param dom domain id
306 * @param vmconfig configuration string
307 * @param vmconfig_n length of vmconfig
308 * @return 0 on success, error code otherwise
309 */
310 int xen_domain_configure(uint32_t dom, char *vmconfig, int vmconfig_n){
311 int err = 0;
312 CURL *curl = NULL;
313 struct curl_slist *headers = NULL;
314 char url[128] = {};
315 int url_n = sizeof(url);
316 struct curl_httppost *form = NULL, *last = NULL;
317 CURLFORMcode formcode = 0;
319 dprintf("> dom=%u\n", dom);
320 // List domains so that xend will update its domain list and notice the new domain.
321 xen_domain_ls();
323 err = curlsetup(&curl, &headers, url, url_n, "http://localhost:%d/xend/domain/%u", XEND_PORT, dom);
324 if(err) goto exit;
326 // Config field - set from vmconfig.
327 formcode = curl_formadd(&form, &last,
328 CURLFORM_COPYNAME, "config",
329 CURLFORM_BUFFER, "config",
330 CURLFORM_BUFFERPTR, vmconfig,
331 CURLFORM_BUFFERLENGTH, vmconfig_n,
332 CURLFORM_CONTENTTYPE, "application/octet-stream",
333 CURLFORM_END);
334 if(formcode){
335 eprintf("> Error adding config field.\n");
336 goto exit;
337 }
338 // Op field.
339 formcode = curl_formadd(&form, &last,
340 CURLFORM_COPYNAME, "op",
341 CURLFORM_COPYCONTENTS, "configure",
342 CURLFORM_END);
343 if(formcode){
344 eprintf("> Error adding op field.\n");
345 err = -EINVAL;
346 goto exit;
347 }
348 // POST the form.
349 curl_easy_setopt(curl, CURLOPT_HTTPPOST, form);
350 err = curlresult(curl);
351 exit:
352 curlcleanup(&curl, &headers);
353 if(form) curl_formfree(form);
354 dprintf("< err=%d\n", err);
355 return err;
356 }
358 /** Get xend to unpause a domain.
359 *
360 * @param dom domain id
361 * @return 0 on success, error code otherwise
362 */
363 int xen_domain_unpause(uint32_t dom){
364 int err = 0;
365 CURL *curl = NULL;
366 struct curl_slist *headers = NULL;
367 char url[128] = {};
368 int url_n = sizeof(url);
369 struct curl_httppost *form = NULL, *last = NULL;
370 CURLFORMcode formcode = 0;
372 dprintf("> dom=%u\n", dom);
374 err = curlsetup(&curl, &headers, url, url_n, "http://localhost:%d/xend/domain/%u", XEND_PORT, dom);
375 if(err) goto exit;
377 // Op field.
378 formcode = curl_formadd(&form, &last,
379 CURLFORM_COPYNAME, "op",
380 CURLFORM_COPYCONTENTS, "unpause",
381 CURLFORM_END);
382 if(formcode){
383 eprintf("> Error adding op field.\n");
384 err = -EINVAL;
385 goto exit;
386 }
387 // POST the form.
388 curl_easy_setopt(curl, CURLOPT_HTTPPOST, form);
389 err = curlresult(curl);
390 exit:
391 curlcleanup(&curl, &headers);
392 if(form) curl_formfree(form);
393 dprintf("< err=%d\n", err);
394 return err;
395 }