1 /* ovirt viewer console application
2 * Copyright (C) 2008 Red Hat Inc.
3 * Written by Richard W.M. Jones <rjones@redhat.com>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <glib/gprintf.h>
30 #include <libxml/parser.h>
32 #include <curl/curl.h>
36 /* Private functions. */
37 static gpointer wui_thread (gpointer data);
38 static void wui_thread_send_quit (void);
39 static void do_curl_init (void);
40 static void write_fn_start_capture (void);
41 static char *write_fn_finish_capture (void);
42 static void write_fn_discard_capture_buffer (void);
43 static gboolean do_connect (void);
44 static gboolean do_login (void);
45 static gboolean refresh_vm_list (void);
46 static void parse_vmlist_from_xml (const char *xml);
47 static struct vm *parse_vm_from_xml (xmlNodePtr node);
49 /* Messages (main thread -> WUI thread only).
51 * These are passed by reference. They are allocated by the sender
52 * (ie. the main thread) and freed by the receiver (ie. the WUI thread).
55 QUIT, /* Tell the WUI thread to quit cleanly. */
56 CONNECT, /* Tell to connect (just fetch the URI). */
57 DISCONNECT, /* Tell to disconnect, forget state. */
58 LOGIN, /* Tell to login, and pass credentials. */
59 REFRESH_VM_LIST, /* Tell to refresh the VM list right away. */
63 enum message_type type;
68 /* Start the WUI thread. See main() for explanation of the threading model. */
69 static GThread *wui_gthread = NULL;
70 static GThread *main_gthread = NULL;
71 static GAsyncQueue *wui_thread_queue = NULL;
74 start_wui_thread (void)
78 DEBUG ("starting the WUI thread");
80 assert (wui_gthread == NULL);
82 main_gthread = g_thread_self ();
84 /* Create the message queue for main -> WUI thread communications. */
85 wui_thread_queue = g_async_queue_new ();
87 wui_gthread = g_thread_create (wui_thread, wui_thread_queue, TRUE, &error);
89 g_print ("%s\n", error->message);
96 stop_wui_thread (void)
98 DEBUG ("stopping the WUI thread");
100 assert (wui_gthread != NULL);
101 ASSERT_IS_MAIN_THREAD ();
103 /* Send a quit message then wait for the WUI thread to join.
105 * This "nice" shutdown could cause the UI to hang for an
106 * indefinite period (eg. if the WUI thread is engaged in some
107 * long connection or download from the remote server). But
108 * I want to keep it this way for the moment so that we can
109 * diagnose problems with the WUI thread.
111 * (This could be solved with some sort of interruptible
112 * join, but glib doesn't support that AFAICT).
114 wui_thread_send_quit ();
115 (void) g_thread_join (wui_gthread);
116 g_async_queue_unref (wui_thread_queue);
121 assert_is_wui_thread (const char *filename, int lineno)
123 if (g_thread_self () != wui_gthread) {
124 fprintf (stderr, "%s:%d: internal error: this function should only run in the context of the WUI thread\n", filename, lineno);
130 assert_is_main_thread (const char *filename, int lineno)
132 if (g_thread_self () != main_gthread) {
133 fprintf (stderr, "%s:%d: internal error: this function should only run in the context of the main thread\n", filename, lineno);
138 /* Send the quit message to the WUI thread. */
140 wui_thread_send_quit (void)
144 ASSERT_IS_MAIN_THREAD ();
146 msg = g_new (struct message, 1);
148 g_async_queue_push (wui_thread_queue, msg);
151 /* Send the connect message to the WUI thread. */
153 wui_thread_send_connect (const char *uri)
157 ASSERT_IS_MAIN_THREAD ();
159 msg = g_new (struct message, 1);
161 msg->str1 = g_strdup (uri);
162 g_async_queue_push (wui_thread_queue, msg);
165 /* Send the disconnect message to the WUI thread. */
167 wui_thread_send_disconnect (void)
171 ASSERT_IS_MAIN_THREAD ();
173 msg = g_new (struct message, 1);
174 msg->type = DISCONNECT;
175 g_async_queue_push (wui_thread_queue, msg);
178 /* Send the login message to the WUI thread. */
180 wui_thread_send_login (const char *username, const char *password)
184 ASSERT_IS_MAIN_THREAD ();
186 msg = g_new (struct message, 1);
188 msg->str1 = g_strdup (username);
189 msg->str2 = g_strdup (password);
190 g_async_queue_push (wui_thread_queue, msg);
193 /* Send the refresh VM list message to the WUI thread. */
195 wui_thread_send_refresh_vm_list (void)
199 ASSERT_IS_MAIN_THREAD ();
201 msg = g_new (struct message, 1);
202 msg->type = REFRESH_VM_LIST;
203 g_async_queue_push (wui_thread_queue, msg);
206 /* The current state.
208 * For safety, the main thread must lock this before reading, and the
209 * WUI thread must lock this before writing. However the WUI thread
210 * does not need to lock before reading, because no other thread
213 static gboolean connected = FALSE;
214 static gboolean logged_in = FALSE;
215 static gboolean busy = FALSE;
216 static GStaticMutex state_mutex;
218 static void set_connected (gboolean);
219 static void set_logged_in (gboolean);
220 static void set_busy (gboolean);
222 /* The private state of the WUI thread. */
223 static int secs_between_refresh = 60;
224 static CURL *curl = NULL;
225 static char curl_error_buffer[CURL_ERROR_SIZE];
226 static char *uri = NULL;
227 static char *username = NULL;
228 static char *password = NULL;
230 static gboolean process_message (struct message *);
232 /* The WUI thread. See main() above for explanation of
233 * the threading model.
236 wui_thread (gpointer _queue)
238 GAsyncQueue *queue = (GAsyncQueue *) _queue;
239 gboolean quit = FALSE;
244 DEBUG ("WUI thread starting up");
246 /* This checks wui_gthread global which is actually set in the
247 * main thread. Of course, it might not be set if the WUI thread
248 * runs first. Hence we yield for the main thread to run.
251 ASSERT_IS_WUI_THREAD ();
253 g_async_queue_ref (queue);
255 /* In the thread's loop we check for new instructions from the main
256 * thread and carry them out. Also, if we are connected and logged
257 * in then we periodically recheck the list of VMs.
263 g_get_current_time (&tv);
264 g_time_val_add (&tv, secs_between_refresh * 1000000);
265 _msg = g_async_queue_timed_pop (queue, &tv);
267 _msg = g_async_queue_pop (queue);
271 msg = (struct message *) _msg;
274 DEBUG ("received message with msg->type = %d", msg->type);
275 quit = process_message (msg);
276 /* Don't free any strings in the message - we've saved them. */
279 /* No message, must have got a timeout instead, which means
280 * we are logged in and we should refresh the list of VMs.
281 * Note it's not an error if we temporarily lose contact
288 DEBUG ("WUI thread shutting down cleanly");
290 g_async_queue_unref (queue);
291 g_thread_exit (NULL);
292 return NULL; /* not reached? */
295 /* The WUI thread calls this to safely update the state variables.
296 * This also updates elements in the UI by setting idle callbacks
297 * which are executed in the context of the main thread.
300 set_connected (gboolean new_connected)
302 ASSERT_IS_WUI_THREAD ();
304 g_static_mutex_lock (&state_mutex);
305 connected = new_connected;
306 g_static_mutex_unlock (&state_mutex);
309 g_idle_add (main_connected, NULL);
311 g_idle_add (main_disconnected, NULL);
315 set_logged_in (gboolean new_logged_in)
317 ASSERT_IS_WUI_THREAD ();
319 g_static_mutex_lock (&state_mutex);
320 logged_in = new_logged_in;
321 g_static_mutex_unlock (&state_mutex);
324 g_idle_add (main_logged_in, NULL);
326 g_idle_add (main_logged_out, NULL);
330 set_busy (gboolean new_busy)
332 ASSERT_IS_WUI_THREAD ();
334 g_static_mutex_lock (&state_mutex);
336 g_static_mutex_unlock (&state_mutex);
339 g_idle_add (main_busy, NULL);
341 g_idle_add (main_idle, NULL);
344 /* The main thread should call these functions to get the WUI thread's
348 wui_thread_is_connected (void)
352 ASSERT_IS_MAIN_THREAD ();
354 g_static_mutex_lock (&state_mutex);
356 g_static_mutex_unlock (&state_mutex);
361 wui_thread_is_logged_in (void)
365 ASSERT_IS_MAIN_THREAD ();
367 g_static_mutex_lock (&state_mutex);
369 g_static_mutex_unlock (&state_mutex);
374 wui_thread_is_busy (void)
378 ASSERT_IS_MAIN_THREAD ();
380 g_static_mutex_lock (&state_mutex);
382 g_static_mutex_unlock (&state_mutex);
386 /* Process a message from the main thread. */
388 process_message (struct message *msg)
390 ASSERT_IS_WUI_THREAD ();
394 write_fn_discard_capture_buffer ();
395 if (curl) curl_easy_cleanup (curl);
396 if (uri) g_free (uri);
397 if (username) g_free (username);
398 if (password) g_free (password);
399 set_connected (FALSE);
400 set_logged_in (FALSE);
404 write_fn_discard_capture_buffer ();
405 if (curl) curl_easy_cleanup (curl);
407 if (uri) g_free (uri);
411 set_connected (TRUE);
413 set_connected (FALSE);
414 set_logged_in (FALSE);
419 /* This just forgets the state. REST is connectionless. */
420 write_fn_discard_capture_buffer ();
421 if (curl) curl_easy_cleanup (curl);
423 if (uri) g_free (uri);
425 if (username) g_free (username);
427 if (password) g_free (password);
429 set_connected (FALSE);
430 set_logged_in (FALSE);
434 if (username) g_free (username);
435 username = msg->str1;
436 if (password) g_free (password);
437 password = msg->str2;
439 /* If we're not connected, this message just updates the
440 * username and password. Otherwise if we are connected,
441 * try to login and grab the initial list of VMs.
445 set_logged_in (TRUE);
446 if (refresh_vm_list ())
447 secs_between_refresh = 60;
449 set_logged_in (FALSE);
454 case REFRESH_VM_LIST:
455 if (connected && logged_in) {
457 secs_between_refresh = 60;
462 DEBUG ("unknown message type %d", msg->type);
469 /* Macro for easy handling of CURL errors. */
470 #define CURL_CHECK_ERROR(fn, args) \
472 CURLcode __r = fn args; \
473 if (__r != CURLE_OK) { \
474 fprintf (stderr, "%s: %s\n", #fn, curl_easy_strerror (__r)); \
479 /* Libcurl has a really crufty method for handling HTTP headers and
480 * data. We set these functions as callbacks, because the default
481 * callback functions print the data out to stderr. In order to
482 * capture the data, we have to keep state here.
485 static char *write_fn_buffer = NULL;
486 static ssize_t write_fn_len = -1;
489 write_fn (void *ptr, size_t size, size_t nmemb, void *stream)
491 int bytes = size * nmemb;
494 ASSERT_IS_WUI_THREAD ();
496 if (write_fn_len >= 0) { /* We're capturing. */
497 old_start = write_fn_len;
498 write_fn_len += bytes;
499 write_fn_buffer = g_realloc (write_fn_buffer, write_fn_len);
500 memcpy (write_fn_buffer + old_start, ptr, bytes);
506 /* Start capturing HTTP response data. */
508 write_fn_start_capture (void)
510 ASSERT_IS_WUI_THREAD ();
512 write_fn_discard_capture_buffer ();
513 write_fn_buffer = NULL;
517 /* Finish capture and return the capture buffer. Caller must free. */
519 write_fn_finish_capture (void)
521 char *ret = write_fn_buffer;
523 ASSERT_IS_WUI_THREAD ();
525 write_fn_buffer = NULL;
530 /* Stop capturing and discard the capture buffer, if any. */
532 write_fn_discard_capture_buffer (void)
534 ASSERT_IS_WUI_THREAD ();
536 g_free (write_fn_buffer);
537 write_fn_buffer = NULL;
542 header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
544 int bytes = size * nmemb;
546 ASSERT_IS_WUI_THREAD ();
551 /* Called from the message loop to initialize the CURL handle. */
555 DEBUG ("initializing libcurl");
557 ASSERT_IS_WUI_THREAD ();
559 curl = curl_easy_init ();
560 if (!curl) { /* This is probably quite bad, so abort. */
561 DEBUG ("curl_easy_init failed");
565 CURL_CHECK_ERROR (curl_easy_setopt,
566 (curl, CURLOPT_CAINFO, cainfo));
567 CURL_CHECK_ERROR (curl_easy_setopt,
568 (curl, CURLOPT_SSL_VERIFYHOST, check_cert ? 2 : 0));
569 CURL_CHECK_ERROR (curl_easy_setopt,
570 (curl, CURLOPT_SSL_VERIFYPEER, check_cert ? 1 : 0));
572 CURL_CHECK_ERROR (curl_easy_setopt,
573 (curl, CURLOPT_WRITEFUNCTION, write_fn));
574 CURL_CHECK_ERROR (curl_easy_setopt,
575 (curl, CURLOPT_HEADERFUNCTION, header_fn));
577 /* This enables error messages in curl_easy_perform. */
578 CURL_CHECK_ERROR (curl_easy_setopt,
579 (curl, CURLOPT_ERRORBUFFER, curl_error_buffer));
581 /* This enables cookie handling, using an internal cookiejar. */
582 CURL_CHECK_ERROR (curl_easy_setopt,
583 (curl, CURLOPT_COOKIEFILE, ""));
586 /* Called from the message loop. Try to connect to the current URI.
587 * Returns true on success.
596 DEBUG ("connecting to uri %s", uri);
597 ASSERT_IS_WUI_THREAD ();
599 /* Set the URI for libcurl. */
600 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, uri));
602 /* Try to fetch the URI. */
603 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
605 /* Signal an error back to the main thread. */
606 error_str = g_strdup (curl_easy_strerror (r));
607 g_idle_add (main_connection_error, error_str);
611 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
612 DEBUG ("HTTP return code is %ld", code);
613 if (code != 200 && code != 302 && code != 401) {
614 /* XXX If only glib had g_asprintf. */
615 error_str = g_strdup ("unexpected HTTP return code from server");
616 g_idle_add (main_connection_error, error_str);
623 /* Called from the message loop. Try to login to 'URI/login' with the
624 * current username and password. Returns true on success.
636 DEBUG ("logging in with username %s, password *****", username);
637 ASSERT_IS_WUI_THREAD ();
639 /* Generate the login URI from the base URI. */
640 len = strlen (uri) + 6 + 1;
641 login_uri = g_alloca (len);
642 snprintf (login_uri, len, "%s/login", uri);
644 DEBUG ("login URI %s", login_uri);
646 /* Set the URI for libcurl. */
647 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, login_uri));
649 /* Construct the username:password for CURL. */
650 len = strlen (username) + strlen (password) + 2;
651 userpwd = g_alloca (len);
652 snprintf (userpwd, len, "%s:%s", username, password);
654 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_USERPWD, userpwd));
656 /* HTTP Basic authentication is OK since we should be sending
657 * this only over HTTPS.
659 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC));
661 /* Follow redirects. */
662 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_FOLLOWLOCATION, (long) 1));
663 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_MAXREDIRS, (long) 10));
665 /* Try to fetch the URI. */
666 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
668 /* Signal an error back to the main thread. */
669 error_str = g_strdup (curl_easy_strerror (r));
670 g_idle_add (main_login_error, error_str);
674 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
675 DEBUG ("HTTP return code is %ld", code);
679 DEBUG ("login was successful");
683 error_str = g_strdup ("server rejected the username or password");
684 g_idle_add (main_login_error, error_str);
688 /* XXX If only glib had g_asprintf. */
689 error_str = g_strdup ("unexpected HTTP return code from server");
690 g_idle_add (main_login_error, error_str);
695 /* Called from the message loop. Refresh the list of VMs. */
697 refresh_vm_list (void)
706 DEBUG ("refreshing list of VMs");
707 ASSERT_IS_WUI_THREAD ();
709 /* Generate the vms URI from the base URI. */
710 len = strlen (uri) + 4 + 1;
711 vms_uri = g_alloca (len);
712 snprintf (vms_uri, len, "%s/vms", uri);
714 DEBUG ("vms URI %s", vms_uri);
716 /* Set the URI for libcurl. */
717 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, vms_uri));
719 /* We want to capture the output, so tell our write function
720 * to place the output into a buffer.
722 write_fn_start_capture ();
724 /* Try to fetch the URI. */
725 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
727 /* Signal an error back to the main thread. */
728 error_str = g_strdup (curl_easy_strerror (r));
729 g_idle_add (main_login_error, error_str);
733 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
734 DEBUG ("HTTP return code is %ld", code);
739 /* Hmm - even though we previously logged in, the server is
740 * rejecting our attempts now with an authorization error.
741 * We move to the logged out state.
744 set_logged_in (FALSE);
745 error_str = g_strdup ("server rejected the username or password");
746 g_idle_add (main_login_error, error_str);
750 /* XXX If only glib had g_asprintf. */
751 error_str = g_strdup ("unexpected HTTP return code from server");
752 g_idle_add (main_status_error, error_str);
756 /* If we got here then we appear to have a correct
757 * XML document describing the list of VMs.
759 secs_between_refresh <<= 1;
761 xml = write_fn_finish_capture ();
763 parse_vmlist_from_xml (xml);
769 /* Functions to deal with the list of VMs, parsing it from the XML, etc.
771 * A vmlist is a GSList (glib singly-linked list) of vm structures.
772 * The caller must call free_vmlist to free up this list correctly.
776 free_vm (gpointer _vm, gpointer data)
778 struct vm *vm = (struct vm *) _vm;
780 g_free (vm->description);
783 g_free (vm->mac_addr);
788 free_vmlist (GSList *vmlist)
790 g_slist_foreach (vmlist, free_vm, NULL);
791 g_slist_free (vmlist);
795 copy_vm (struct vm *vm)
799 vm2 = g_memdup (vm, sizeof (*vm));
800 vm2->description = g_strdup (vm->description);
801 vm2->state = vm->state ? g_strdup (vm->state) : NULL;
802 vm2->uuid = vm->uuid ? g_strdup (vm->uuid) : NULL;
803 vm2->mac_addr = vm->mac_addr ? g_strdup (vm->mac_addr) : NULL;
808 compare_vm (gconstpointer _vm1, gconstpointer _vm2)
810 const struct vm *vm1 = (const struct vm *) _vm1;
811 const struct vm *vm2 = (const struct vm *) _vm2;
813 return strcmp (vm1->description, vm2->description);
816 static GSList *vmlist = NULL;
817 static gboolean vmlist_valid = FALSE;
818 static GStaticMutex vmlist_mutex;
820 /* Called from the main thread to find out if we have a valid vmlist. */
822 wui_thread_has_valid_vmlist (void)
826 ASSERT_IS_MAIN_THREAD ();
828 g_static_mutex_lock (&vmlist_mutex);
830 g_static_mutex_unlock (&vmlist_mutex);
834 /* Called from the main thread to find return the current vmlist. This
835 * actually returns a deep copy of it that the caller must free.
838 duplicate_and_insert_vm (gpointer _vm, gpointer _ret)
840 struct vm *vm = (struct vm *) _vm;
841 GSList **ret = (GSList **) _ret;
843 *ret = g_slist_prepend (*ret, copy_vm (vm));
847 wui_thread_get_vmlist (GSList **ret)
851 ASSERT_IS_MAIN_THREAD ();
856 g_static_mutex_lock (&vmlist_mutex);
857 if (!vmlist_valid) goto done;
859 g_slist_foreach (vmlist, duplicate_and_insert_vm, ret);
860 *ret = g_slist_sort (*ret, compare_vm);
864 g_static_mutex_unlock (&vmlist_mutex);
868 /* Called from the message loop in the WUI thread, with an XML document
869 * which we turn into a list of VM structures, and update the vmlist
873 parse_vmlist_from_xml (const char *xml)
875 xmlDocPtr doc = NULL;
879 GSList *new_vmlist = NULL;
882 /*DEBUG ("XML =\n%s", xml);*/
883 ASSERT_IS_WUI_THREAD ();
885 /* We don't really expect that we won't be able to parse the XML ... */
886 doc = xmlParseDoc ((const xmlChar *) xml);
888 DEBUG ("error parsing XML document, xml =\n%s", xml);
889 error_str = g_strdup ("error parsing XML document from remote server");
890 g_idle_add (main_status_error, error_str);
894 root = xmlDocGetRootElement (doc);
896 DEBUG ("XML document was empty");
897 error_str = g_strdup ("XML document was empty");
898 g_idle_add (main_status_error, error_str);
902 /* We expect the root element will be either "nil-classes"
903 * or "vms", with the former indicating an empty list of VMs.
905 if (xmlStrcmp (root->name, (const xmlChar *) "nil-classes") == 0) {
906 g_static_mutex_lock (&vmlist_mutex);
908 free_vmlist (vmlist);
910 g_static_mutex_unlock (&vmlist_mutex);
912 /* Signal to the main UI thread that the list has been updated. */
913 g_idle_add (main_vmlist_updated, NULL);
918 if (xmlStrcmp (root->name, (const xmlChar *) "vms") != 0) {
919 DEBUG ("unexpected root node in XML document, xml =\n%s", xml);
920 error_str = g_strdup ("unexpected root node in XML document");
921 g_idle_add (main_status_error, error_str);
925 /* The document is <vms> with a list of <vm> elements which
926 * we process in turn.
928 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
929 if (xmlStrcmp (node->name, (const xmlChar *) "vm") == 0) {
930 vm = parse_vm_from_xml (node);
932 error_str = g_strdup ("could not parse <vm> element");
933 g_idle_add (main_status_error, error_str);
935 free_vmlist (new_vmlist);
938 new_vmlist = g_slist_prepend (new_vmlist, vm);
942 /* Successfully parsed all the <vm> nodes, so swap the old and new
945 g_static_mutex_lock (&vmlist_mutex);
947 free_vmlist (vmlist);
949 g_static_mutex_unlock (&vmlist_mutex);
951 /* Signal that the vmlist has been updated. */
952 g_idle_add (main_vmlist_updated, NULL);
955 /* Free up XML resources used before returning. */
956 if (doc) xmlFreeDoc (doc);
960 parse_vm_from_xml (xmlNodePtr node)
966 memset (&vm, 0, sizeof vm);
970 vm.mem_allocated = -1;
972 vm.vcpus_allocated = -1;
975 for (p = node->xmlChildrenNode; p != NULL; p = p->next) {
976 if (xmlStrcmp (p->name, (const xmlChar *) "description") == 0) {
977 str = xmlNodeGetContent (p);
979 vm.description = g_strdup ((char *) str);
983 else if (xmlStrcmp (p->name, (const xmlChar *) "host-id") == 0) {
984 str = xmlNodeGetContent (p);
986 vm.hostid = strtol ((char *) str, NULL, 10);
990 else if (xmlStrcmp (p->name, (const xmlChar *) "id") == 0) {
991 str = xmlNodeGetContent (p);
993 vm.id = strtol ((char *) str, NULL, 10);
997 else if (xmlStrcmp (p->name, (const xmlChar *) "memory-allocated") == 0) {
998 str = xmlNodeGetContent (p);
1000 vm.mem_allocated = strtol ((char *) str, NULL, 10);
1004 else if (xmlStrcmp (p->name, (const xmlChar *) "memory-used") == 0) {
1005 str = xmlNodeGetContent (p);
1007 vm.mem_used = strtol ((char *) str, NULL, 10);
1011 else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-allocated") == 0) {
1012 str = xmlNodeGetContent (p);
1014 vm.vcpus_allocated = strtol ((char *) str, NULL, 10);
1018 else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-used") == 0) {
1019 str = xmlNodeGetContent (p);
1021 vm.vcpus_used = strtol ((char *) str, NULL, 10);
1025 else if (xmlStrcmp (p->name, (const xmlChar *) "state") == 0) {
1026 str = xmlNodeGetContent (p);
1028 vm.state = g_strdup ((char *) str);
1032 else if (xmlStrcmp (p->name, (const xmlChar *) "uuid") == 0) {
1033 str = xmlNodeGetContent (p);
1035 vm.uuid = g_strdup ((char *) str);
1039 else if (xmlStrcmp (p->name, (const xmlChar *) "vnc-port") == 0) {
1040 str = xmlNodeGetContent (p);
1042 vm.vnc_port = strtol ((char *) str, NULL, 10);
1046 else if (xmlStrcmp (p->name, (const xmlChar *) "vnic-mac-addr") == 0) {
1047 str = xmlNodeGetContent (p);
1049 vm.mac_addr = g_strdup ((char *) str);
1055 /* Make sure we've got the required fields. */
1057 if (vm.description == NULL)
1058 DEBUG ("required field \"description\" missing from <vm> structure");
1059 else if (vm.hostid == -1)
1060 DEBUG ("required field \"description\" missing from <vm> structure");
1061 else if (vm.id == -1)
1062 DEBUG ("required field \"description\" missing from <vm> structure");
1063 else if (vm.vnc_port == -1)
1064 DEBUG ("required field \"vnc-port\" missing from <vm> structure");
1066 ret = g_memdup (&vm, sizeof vm);