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 sleep for the main thread to run. (XXX)
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)
523 ASSERT_IS_WUI_THREAD ();
525 /* Make sure the buffer is NUL-terminated before returning it. */
526 write_fn_buffer = g_realloc (write_fn_buffer, write_fn_len+1);
527 write_fn_buffer[write_fn_len] = '\0';
528 ret = write_fn_buffer;
530 write_fn_buffer = NULL;
535 /* Stop capturing and discard the capture buffer, if any. */
537 write_fn_discard_capture_buffer (void)
539 ASSERT_IS_WUI_THREAD ();
541 g_free (write_fn_buffer);
542 write_fn_buffer = NULL;
547 header_fn (void *ptr, size_t size, size_t nmemb, void *stream)
549 int bytes = size * nmemb;
551 ASSERT_IS_WUI_THREAD ();
556 /* Called from the message loop to initialize the CURL handle. */
560 DEBUG ("initializing libcurl");
562 ASSERT_IS_WUI_THREAD ();
564 curl = curl_easy_init ();
565 if (!curl) { /* This is probably quite bad, so abort. */
566 DEBUG ("curl_easy_init failed");
570 CURL_CHECK_ERROR (curl_easy_setopt,
571 (curl, CURLOPT_CAINFO, cainfo));
572 CURL_CHECK_ERROR (curl_easy_setopt,
573 (curl, CURLOPT_SSL_VERIFYHOST, check_cert ? 2 : 0));
574 CURL_CHECK_ERROR (curl_easy_setopt,
575 (curl, CURLOPT_SSL_VERIFYPEER, check_cert ? 1 : 0));
577 CURL_CHECK_ERROR (curl_easy_setopt,
578 (curl, CURLOPT_WRITEFUNCTION, write_fn));
579 CURL_CHECK_ERROR (curl_easy_setopt,
580 (curl, CURLOPT_HEADERFUNCTION, header_fn));
582 /* This enables error messages in curl_easy_perform. */
583 CURL_CHECK_ERROR (curl_easy_setopt,
584 (curl, CURLOPT_ERRORBUFFER, curl_error_buffer));
586 /* This enables cookie handling, using an internal cookiejar. */
587 CURL_CHECK_ERROR (curl_easy_setopt,
588 (curl, CURLOPT_COOKIEFILE, ""));
591 /* Called from the message loop. Try to connect to the current URI.
592 * Returns true on success.
601 DEBUG ("connecting to uri %s", uri);
602 ASSERT_IS_WUI_THREAD ();
604 /* Set the URI for libcurl. */
605 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, uri));
607 /* Try to fetch the URI. */
608 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
610 /* Signal an error back to the main thread. */
611 error_str = g_strdup (curl_easy_strerror (r));
612 g_idle_add (main_connection_error, error_str);
616 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
617 DEBUG ("HTTP return code is %ld", code);
618 if (code != 200 && code != 302 && code != 401) {
619 /* XXX If only glib had g_asprintf. */
620 error_str = g_strdup ("unexpected HTTP return code from server");
621 g_idle_add (main_connection_error, error_str);
628 /* Called from the message loop. Try to login to 'URI/login' with the
629 * current username and password. Returns true on success.
641 DEBUG ("logging in with username %s, password *****", username);
642 ASSERT_IS_WUI_THREAD ();
644 /* Generate the login URI from the base URI. */
645 len = strlen (uri) + 6 + 1;
646 login_uri = g_alloca (len);
647 snprintf (login_uri, len, "%s/login", uri);
649 DEBUG ("login URI %s", login_uri);
651 /* Set the URI for libcurl. */
652 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, login_uri));
654 /* Construct the username:password for CURL. */
655 len = strlen (username) + strlen (password) + 2;
656 userpwd = g_alloca (len);
657 snprintf (userpwd, len, "%s:%s", username, password);
659 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_USERPWD, userpwd));
661 /* HTTP Basic authentication is OK since we should be sending
662 * this only over HTTPS.
664 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC));
666 /* Follow redirects. */
667 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_FOLLOWLOCATION, (long) 1));
668 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_MAXREDIRS, (long) 10));
670 /* Try to fetch the URI. */
671 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
673 /* Signal an error back to the main thread. */
674 error_str = g_strdup (curl_easy_strerror (r));
675 g_idle_add (main_login_error, error_str);
679 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
680 DEBUG ("HTTP return code is %ld", code);
684 DEBUG ("login was successful");
688 error_str = g_strdup ("server rejected the username or password");
689 g_idle_add (main_login_error, error_str);
693 /* XXX If only glib had g_asprintf. */
694 error_str = g_strdup ("unexpected HTTP return code from server");
695 g_idle_add (main_login_error, error_str);
700 /* Called from the message loop. Refresh the list of VMs. */
702 refresh_vm_list (void)
711 DEBUG ("refreshing list of VMs");
712 ASSERT_IS_WUI_THREAD ();
714 /* Generate the vms URI from the base URI. */
715 len = strlen (uri) + 4 + 1;
716 vms_uri = g_alloca (len);
717 snprintf (vms_uri, len, "%s/vms", uri);
719 DEBUG ("vms URI %s", vms_uri);
721 /* Set the URI for libcurl. */
722 CURL_CHECK_ERROR (curl_easy_setopt, (curl, CURLOPT_URL, vms_uri));
724 /* We want to capture the output, so tell our write function
725 * to place the output into a buffer.
727 write_fn_start_capture ();
729 /* Try to fetch the URI. */
730 r = CURL_CHECK_ERROR (curl_easy_perform, (curl));
732 /* Signal an error back to the main thread. */
733 error_str = g_strdup (curl_easy_strerror (r));
734 g_idle_add (main_login_error, error_str);
738 CURL_CHECK_ERROR (curl_easy_getinfo, (curl, CURLINFO_RESPONSE_CODE, &code));
739 DEBUG ("HTTP return code is %ld", code);
744 /* Hmm - even though we previously logged in, the server is
745 * rejecting our attempts now with an authorization error.
746 * We move to the logged out state.
749 set_logged_in (FALSE);
750 error_str = g_strdup ("server rejected the username or password");
751 g_idle_add (main_login_error, error_str);
755 /* XXX If only glib had g_asprintf. */
756 error_str = g_strdup ("unexpected HTTP return code from server");
757 g_idle_add (main_status_error, error_str);
761 /* If we got here then we appear to have a correct
762 * XML document describing the list of VMs.
764 secs_between_refresh <<= 1;
766 xml = write_fn_finish_capture ();
768 parse_vmlist_from_xml (xml);
774 /* Functions to deal with the list of VMs, parsing it from the XML, etc.
776 * A vmlist is a GSList (glib singly-linked list) of vm structures.
777 * The caller must call free_vmlist to free up this list correctly.
781 free_vm (gpointer _vm, gpointer data)
783 struct vm *vm = (struct vm *) _vm;
785 g_free (vm->description);
788 g_free (vm->mac_addr);
793 free_vmlist (GSList *vmlist)
795 g_slist_foreach (vmlist, free_vm, NULL);
796 g_slist_free (vmlist);
800 copy_vm (struct vm *vm)
804 vm2 = g_memdup (vm, sizeof (*vm));
805 vm2->description = g_strdup (vm->description);
806 vm2->uuid = g_strdup (vm->uuid);
807 vm2->state = vm->state ? g_strdup (vm->state) : NULL;
808 vm2->mac_addr = vm->mac_addr ? g_strdup (vm->mac_addr) : NULL;
813 compare_vm (gconstpointer _vm1, gconstpointer _vm2)
815 const struct vm *vm1 = (const struct vm *) _vm1;
816 const struct vm *vm2 = (const struct vm *) _vm2;
818 return strcmp (vm1->description, vm2->description);
821 static GSList *vmlist = NULL;
822 static gboolean vmlist_valid = FALSE;
823 static GStaticMutex vmlist_mutex;
825 /* Called from the main thread to find out if we have a valid vmlist. */
827 wui_thread_has_valid_vmlist (void)
831 ASSERT_IS_MAIN_THREAD ();
833 g_static_mutex_lock (&vmlist_mutex);
835 g_static_mutex_unlock (&vmlist_mutex);
839 /* Called from the main thread to find return the current vmlist. This
840 * actually returns a deep copy of it that the caller must free.
843 duplicate_and_insert_vm (gpointer _vm, gpointer _ret)
845 struct vm *vm = (struct vm *) _vm;
846 GSList **ret = (GSList **) _ret;
848 *ret = g_slist_prepend (*ret, copy_vm (vm));
852 wui_thread_get_vmlist (GSList **ret)
856 ASSERT_IS_MAIN_THREAD ();
861 g_static_mutex_lock (&vmlist_mutex);
862 if (!vmlist_valid) goto done;
864 g_slist_foreach (vmlist, duplicate_and_insert_vm, ret);
865 *ret = g_slist_sort (*ret, compare_vm);
869 g_static_mutex_unlock (&vmlist_mutex);
873 /* Called from the message loop in the WUI thread, with an XML document
874 * which we turn into a list of VM structures, and update the vmlist
878 parse_vmlist_from_xml (const char *xml)
880 xmlDocPtr doc = NULL;
884 GSList *new_vmlist = NULL;
888 /*DEBUG ("XML =\n%s", xml);*/
889 ASSERT_IS_WUI_THREAD ();
891 /* We don't really expect that we won't be able to parse the XML ... */
893 doc = xmlReadMemory (xml, len, NULL, NULL, 0);
896 DEBUG ("error parsing XML document, xml =\n%s", xml);
897 error_str = g_strdup ("error parsing XML document from remote server");
898 g_idle_add (main_status_error, error_str);
902 root = xmlDocGetRootElement (doc);
904 DEBUG ("XML document was empty");
905 error_str = g_strdup ("XML document was empty");
906 g_idle_add (main_status_error, error_str);
910 /* We expect the root element will be either "nil-classes"
911 * or "vms", with the former indicating an empty list of VMs.
913 if (xmlStrcmp (root->name, (const xmlChar *) "nil-classes") == 0) {
914 g_static_mutex_lock (&vmlist_mutex);
916 free_vmlist (vmlist);
918 g_static_mutex_unlock (&vmlist_mutex);
920 /* Signal to the main UI thread that the list has been updated. */
921 g_idle_add (main_vmlist_updated, NULL);
926 if (xmlStrcmp (root->name, (const xmlChar *) "vms") != 0) {
927 DEBUG ("unexpected root node in XML document, xml =\n%s", xml);
928 error_str = g_strdup ("unexpected root node in XML document");
929 g_idle_add (main_status_error, error_str);
933 /* The document is <vms> with a list of <vm> elements which
934 * we process in turn.
936 for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
937 if (xmlStrcmp (node->name, (const xmlChar *) "vm") == 0) {
938 vm = parse_vm_from_xml (node);
940 error_str = g_strdup ("could not parse <vm> element");
941 g_idle_add (main_status_error, error_str);
943 free_vmlist (new_vmlist);
946 new_vmlist = g_slist_prepend (new_vmlist, vm);
950 /* Successfully parsed all the <vm> nodes, so swap the old and new
953 g_static_mutex_lock (&vmlist_mutex);
955 free_vmlist (vmlist);
957 g_static_mutex_unlock (&vmlist_mutex);
959 /* Signal that the vmlist has been updated. */
960 g_idle_add (main_vmlist_updated, NULL);
963 /* Free up XML resources used before returning. */
964 if (doc) xmlFreeDoc (doc);
968 parse_vm_from_xml (xmlNodePtr node)
974 memset (&vm, 0, sizeof vm);
978 vm.mem_allocated = -1;
980 vm.vcpus_allocated = -1;
983 for (p = node->xmlChildrenNode; p != NULL; p = p->next) {
984 if (xmlStrcmp (p->name, (const xmlChar *) "description") == 0) {
985 str = xmlNodeGetContent (p);
987 vm.description = g_strdup ((char *) str);
991 else if (xmlStrcmp (p->name, (const xmlChar *) "host-id") == 0) {
992 str = xmlNodeGetContent (p);
994 vm.hostid = strtol ((char *) str, NULL, 10);
998 else if (xmlStrcmp (p->name, (const xmlChar *) "id") == 0) {
999 str = xmlNodeGetContent (p);
1001 vm.id = strtol ((char *) str, NULL, 10);
1005 else if (xmlStrcmp (p->name, (const xmlChar *) "memory-allocated") == 0) {
1006 str = xmlNodeGetContent (p);
1008 vm.mem_allocated = strtol ((char *) str, NULL, 10);
1012 else if (xmlStrcmp (p->name, (const xmlChar *) "memory-used") == 0) {
1013 str = xmlNodeGetContent (p);
1015 vm.mem_used = strtol ((char *) str, NULL, 10);
1019 else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-allocated") == 0) {
1020 str = xmlNodeGetContent (p);
1022 vm.vcpus_allocated = strtol ((char *) str, NULL, 10);
1026 else if (xmlStrcmp (p->name, (const xmlChar *) "num-vcpus-used") == 0) {
1027 str = xmlNodeGetContent (p);
1029 vm.vcpus_used = strtol ((char *) str, NULL, 10);
1033 else if (xmlStrcmp (p->name, (const xmlChar *) "state") == 0) {
1034 str = xmlNodeGetContent (p);
1036 vm.state = g_strdup ((char *) str);
1040 else if (xmlStrcmp (p->name, (const xmlChar *) "uuid") == 0) {
1041 str = xmlNodeGetContent (p);
1043 vm.uuid = g_strdup ((char *) str);
1047 else if (xmlStrcmp (p->name, (const xmlChar *) "vnc-port") == 0) {
1048 str = xmlNodeGetContent (p);
1050 vm.vnc_port = strtol ((char *) str, NULL, 10);
1054 else if (xmlStrcmp (p->name, (const xmlChar *) "vnic-mac-addr") == 0) {
1055 str = xmlNodeGetContent (p);
1057 vm.mac_addr = g_strdup ((char *) str);
1063 /* Make sure we've got the required fields. */
1065 if (vm.description == NULL)
1066 DEBUG ("required field \"description\" missing from <vm> structure");
1067 else if (vm.hostid == -1)
1068 DEBUG ("required field \"description\" missing from <vm> structure");
1069 else if (vm.id == -1)
1070 DEBUG ("required field \"description\" missing from <vm> structure");
1071 else if (vm.vnc_port == -1)
1072 DEBUG ("required field \"vnc-port\" missing from <vm> structure");
1073 else if (vm.uuid == NULL)
1074 DEBUG ("required field \"uuid\" missing from <vm> structure");
1076 ret = g_memdup (&vm, sizeof vm);