os-cmpi-xen
changeset 118:8ecc856bd445
Some optimizations and cleanup in xen_utils.
Signed-off-by: Jim Fehlig <jfehlig@novell.com>
Signed-off-by: Jim Fehlig <jfehlig@novell.com>
author | Jim Fehlig <jfehlig@novell.com> |
---|---|
date | Mon Jun 04 16:47:54 2007 -0600 (2007-06-04) |
parents | 27a7e302adf7 |
children | 5a5f465c4191 |
files | src/xen_utils.c src/xen_utils.h |
line diff
1.1 --- a/src/xen_utils.c Mon Jun 04 14:45:46 2007 -0600 1.2 +++ b/src/xen_utils.c Mon Jun 04 16:47:54 2007 -0600 1.3 @@ -307,27 +307,17 @@ static int call_func(const void *data, s 1.4 } 1.5 1.6 1.7 -static xen_vm_set* xen_utils_enum_domains(xen_session *session) 1.8 +static xen_vm_set* xen_utils_enum_domains(xen_utils_session *session) 1.9 { 1.10 - xen_host host; 1.11 xen_vm_set *resident_vms; 1.12 char error_msg[XEN_UTILS_ERROR_BUF_LEN]; 1.13 1.14 - if (!xen_session_get_this_host(session, &host, session)) { 1.15 + if(!xen_host_get_resident_vms(session->xen, &resident_vms, session->host)) { 1.16 /* Error description in session object. */ 1.17 - XEN_UTILS_GET_ERROR_STRING(error_msg, session); 1.18 - _SBLIM_TRACE(1, ("--- xen_session_get_this_host failed: \"%s\"", error_msg)); 1.19 + XEN_UTILS_GET_ERROR_STRING(error_msg, session->xen); 1.20 + _SBLIM_TRACE(1, ("--- xen_host_get_resident_vms failed: \"%s\"", error_msg)); 1.21 return NULL; 1.22 } 1.23 - 1.24 - if(!xen_host_get_resident_vms(session, &resident_vms, host)) { 1.25 - /* Error description in session object. */ 1.26 - XEN_UTILS_GET_ERROR_STRING(error_msg, session); 1.27 - _SBLIM_TRACE(1, ("--- xen_host_get_resident_vms failed: \"%s\"", error_msg)); 1.28 - xen_host_free(host); 1.29 - return NULL; 1.30 - } 1.31 - xen_host_free(host); 1.32 1.33 return resident_vms; 1.34 } 1.35 @@ -348,8 +338,7 @@ static int xen_utils_get_session(xen_uti 1.36 s->socket = xen_utils_xend_connect_unix("/var/run/xend/xen-api.sock"); 1.37 if (s->socket == -1) { 1.38 _SBLIM_TRACE(1, ("Unable to connect to Xen Daemon")); 1.39 - free(s); 1.40 - return 0; 1.41 + goto Error; 1.42 } 1.43 1.44 s->xen = xen_session_login_with_password(call_func, 1.45 @@ -357,13 +346,22 @@ static int xen_utils_get_session(xen_uti 1.46 NULL, NULL); 1.47 if (s->xen == NULL || !s->xen->ok) { 1.48 _SBLIM_TRACE(1, ("Login to Xen Daemon failed")); 1.49 - close(s->socket); 1.50 - free(s); 1.51 - return 0; 1.52 + goto Error; 1.53 + } 1.54 + 1.55 + if (!xen_session_get_this_host(s->xen, &(s->host), s->xen)) { 1.56 + _SBLIM_TRACE(1, ("Failed to get session host")); 1.57 + goto Error; 1.58 } 1.59 1.60 *session = s; 1.61 return 1; 1.62 + 1.63 + Error: 1.64 + if (s->socket > 0) 1.65 + close(s->socket); 1.66 + free(s); 1.67 + return 0; 1.68 } 1.69 1.70 1.71 @@ -430,6 +428,7 @@ void xen_utils_xen_close(xen_utils_sessi 1.72 if (session) { 1.73 xen_session_logout(session->xen); 1.74 close(session->socket); 1.75 + xen_host_free(session->host); 1.76 free(session); 1.77 } 1.78 1.79 @@ -465,35 +464,15 @@ int xen_utils_validate_session(xen_utils 1.80 1.81 /* Clear any errors and attempt a simple call */ 1.82 s->xen->ok = 1; 1.83 - xen_host host; 1.84 - if (xen_session_get_this_host(s->xen, &host, s->xen) && s->xen->ok) { 1.85 - xen_host_free(host); 1.86 + xen_host_free(s->host); 1.87 + if (xen_session_get_this_host(s->xen, &(s->host), s->xen) && s->xen->ok) 1.88 return 1; 1.89 - } 1.90 1.91 /* Simple call failed. Reconnect. */ 1.92 xen_session_logout(s->xen); 1.93 close(s->socket); 1.94 - s->socket = xen_utils_xend_connect_unix("/var/run/xend/xen-api.sock"); 1.95 - if (s->socket == -1) { 1.96 - _SBLIM_TRACE(1, ("Unable to connect to Xen Daemon")); 1.97 - free(s); 1.98 - *session = NULL; 1.99 - return 0; 1.100 - } 1.101 - 1.102 - s->xen = xen_session_login_with_password(call_func, 1.103 - (void *)s, 1.104 - NULL, NULL); 1.105 - if (s->xen == NULL || !s->xen->ok) { 1.106 - _SBLIM_TRACE(1, ("Login to Xen Daemon failed")); 1.107 - close(s->socket); 1.108 - free(s); 1.109 - *session = NULL; 1.110 - return 0; 1.111 - } 1.112 - 1.113 - return 1; 1.114 + free(s); 1.115 + return xen_utils_get_session(session); 1.116 } 1.117 1.118 1.119 @@ -510,23 +489,17 @@ int xen_utils_get_domain_resources(xen_u 1.120 return 0; 1.121 1.122 /* malloc a new handle for the resources list. */ 1.123 - *resources = (xen_domain_resources *)malloc(sizeof(xen_domain_resources)); 1.124 + *resources = (xen_domain_resources *)calloc(1, sizeof(xen_domain_resources)); 1.125 if (*resources == NULL) 1.126 return 0; 1.127 1.128 - /* 1.129 - * Get the list of Xen domains. If there are no domains, we'll 1.130 - * explicitly set numdomains to 0. 1.131 - */ 1.132 - (*resources)->domains = xen_utils_enum_domains(session->xen); 1.133 + /* Get the list of Xen domains. */ 1.134 + (*resources)->domains = xen_utils_enum_domains(session); 1.135 if ((*resources)->domains == NULL) 1.136 return 0; 1.137 1.138 (*resources)->numdomains = (*resources)->domains->size; 1.139 1.140 - /* Start iterating from the first Xen domain name. */ 1.141 - (*resources)->currentdomain = 0; 1.142 - 1.143 return 1; 1.144 } 1.145