os-cmpi-xen

view src/Xen_VirtualSystemManagementService_Resource.c @ 121:4868ace2726b

Add initial consistence checks in test suite for Xen_MemoryPool, Xen_ProcessorPool and Xen_VirtualSystemManagementService.

Signed-off-by: Luke Szymanski <Lukasz.Szymanski@Unisys.com>
author Jim Fehlig <jfehlig@novell.com>
date Fri Jun 08 10:22:05 2007 -0600 (2007-06-08)
parents 891f3ab3933b
children
line source
1 // Copyright (C) 2006 IBM Corporation
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 // ============================================================================
17 // Authors: Dr. Gareth S. Bestor, <bestor@us.ibm.com>
18 // Contributors: Jim Fehlig, <jfehlig@novell.com>
20 // Description:
21 // ============================================================================
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
27 #include <xen_common.h>
28 #include <xen_host.h>
30 #include "Xen_VirtualSystemManagementService_Resource.h"
32 /* Include the required CMPI data types, function headers, and macros. */
33 #include "cmpidt.h"
34 #include "cmpift.h"
35 #include "cmpimacs.h"
37 #include "xen_utils.h"
39 // ----------------------------------------------------------------------------
41 static int populate_resource(xen_utils_session *session,
42 _RESOURCES *resources,
43 _RESOURCE **resource)
44 {
45 /* Generate raw data for the single resource. */
46 *resource = (_RESOURCE *)calloc(1, sizeof(_RESOURCE));
47 if (*resource == NULL) {
48 *resource = NULL;
49 return 0;
50 }
52 (*resource)->name = strdup("Xen Hypervisor");
53 //strcpy((*resource)->name, "Xen Hypervisor");
55 /* Check if xend is running. */
56 // FILE * cmd = popen("ps -ef | grep -q [x]end; echo $?","r");
57 FILE * cmd = popen("xend status; echo $?","r");
58 int rc;
59 fscanf(cmd,"%d",&rc);
60 pclose(cmd);
61 (*resource)->running = (rc == 0);
63 /* We're done if xend is not running */
64 if (rc)
65 return 1;
67 /* Validate xend session. We're not running if unable to talk to xend */
68 if (!xen_utils_validate_session(&session)) {
69 (*resource)->running = 0;
70 return 1;
71 }
73 /* Get version */
74 xen_host host = NULL;
75 xen_session_get_this_host(session->xen, &host, session->xen);
76 xen_host_get_software_version(session->xen, &((*resource)->versions), host);
77 xen_host_free(host);
79 return 1;
80 }
82 /* Get a handle to the list of all system resources for this class. */
83 int Xen_VirtualSystemManagementService_getResources(_RESOURCES ** resources)
84 {
85 /* malloc a new handle for the resources list. */
86 *resources = (_RESOURCES *)calloc(1, sizeof(_RESOURCES));
87 if (*resources == NULL) return 0;
89 (*resources)->finished = 0;
91 return 1;
92 }
94 // ----------------------------------------------------------------------------
96 /* Free/deallocate/cleanup the resources list after use. */
97 int Xen_VirtualSystemManagementService_freeResources(_RESOURCES * resources)
98 {
99 if (resources != NULL) {
100 free(resources);
101 resources = NULL;
102 }
104 return 1;
105 }
107 // ----------------------------------------------------------------------------
109 /* Iterator to get the next resource from the resources list. */
110 int Xen_VirtualSystemManagementService_getNextResource(xen_utils_session *session,
111 _RESOURCES * resources,
112 _RESOURCE ** resource)
113 {
114 if (resources == NULL) return 0;
115 if (resources->finished) return 0;
117 if (!populate_resource(session, resources, resource)) return 0;
119 /* Single resource so no more resources in the list. */
120 resources->finished = 1;
122 return 1;
123 }
125 // ----------------------------------------------------------------------------
127 /* Get the specific resource that matches the CMPI object path. */
128 int Xen_VirtualSystemManagementService_getResourceForObjectPath(xen_utils_session *session,
129 _RESOURCES * resources,
130 _RESOURCE ** resource,
131 const CMPIObjectPath * objectpath)
132 {
133 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
135 if (resources == NULL) return 0;
136 if (CMIsNullObject(objectpath)) return 0;
138 /* Obtain the name from the CMPIObjectPath key. */
139 CMPIData namedata = CMGetKey(objectpath, "Name", &status);
140 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(namedata)) return 0;
142 /* Extract the name string from the CMPIString. */
143 char * name = CMGetCharPtr(namedata.value.string);
144 if ((name == NULL) || (*name == '\0')) return 0;
146 /* Check that the name is correct. */
147 if (strcmp(name, "Xen Hypervisor") != 0) return 0;
149 /* Generate raw data for the single resource. */
150 if (!populate_resource(session, resources, resource)) return 0;
152 return 1;
153 }
155 // ----------------------------------------------------------------------------
157 /* Free/deallocate/cleanup the resource after use. */
158 int Xen_VirtualSystemManagementService_freeResource(_RESOURCE * resource)
159 {
160 if (resource) {
161 if (resource->name)
162 free(resource->name);
163 if (resource->versions)
164 xen_string_string_map_free(resource->versions);
165 free(resource);
166 }
168 return 1;
169 }
171 // ----------------------------------------------------------------------------
173 /* Set the property values of a CMPI instance from a specific resource. */
174 int Xen_VirtualSystemManagementService_setInstanceFromResource(_RESOURCE * resource,
175 const CMPIInstance * instance,
176 const CMPIBroker * broker)
177 {
178 if (resource == NULL) return 0;
179 if (CMIsNullObject(instance)) return 0;
181 #ifdef HOST_NAME_MAX
182 char systemname[HOST_NAME_MAX];
183 gethostname(systemname,HOST_NAME_MAX);
184 #else
185 char systemname[255];
186 gethostname(systemname,255);
187 #endif
189 /* Set the CMPIInstance properties from the resource data. */
190 CMSetProperty(instance, "CreationClassName",(CMPIValue *)"Xen_VirtualSystemManagementService", CMPI_chars);
191 CMSetProperty(instance, "Name",(CMPIValue *)resource->name, CMPI_chars);
192 CMSetProperty(instance, "SystemCreationClassName",(CMPIValue *)"CIM_ComputerSystem", CMPI_chars);
193 CMSetProperty(instance, "SystemName",(CMPIValue *)systemname, CMPI_chars);
195 if (resource->versions) {
196 /*
197 * TODO:
198 * resource->version is a string map. What element of the map do we want?
199 * Currently the keys 'Xen' and 'Xend' exist. 'Xend' seems like a better
200 * choice.
201 */
202 size_t i;
203 for (i = 0; i < resource->versions->size; i++)
204 {
205 if (strcmp(resource->versions->contents[i].key, "Xend") == 0) {
206 CMSetProperty(instance, "Version",(CMPIValue *)resource->versions->contents[i].val, CMPI_chars);
207 }
208 }
209 }
211 CMSetProperty(instance, "Started",(CMPIValue *)&(resource->running), CMPI_boolean);
212 int enabled_state = 3; // 3 == Disabled
213 if (resource->running)
214 enabled_state = 2; // 2 == Enabled
215 CMSetProperty(instance, "EnabledState",(CMPIValue *)&enabled_state, CMPI_uint16);
217 return 1;
218 }
220 // ----------------------------------------------------------------------------
222 /* Delete the specified resource from the system. */
223 int Xen_VirtualSystemManagementService_deleteResource(_RESOURCES * resources,
224 _RESOURCE * resource)
225 {
226 /* Unsupported. */
227 return -1;
228 }
230 // ----------------------------------------------------------------------------
232 /* Modify the specified resource using the property values of a CMPI instance. */
233 int Xen_VirtualSystemManagementService_setResourceFromInstance(_RESOURCE * resource,
234 const CMPIInstance * instance,
235 const CMPIBroker * broker)
236 {
237 /* Unsupported. */
238 return -1;
239 }
241 // ----------------------------------------------------------------------------
243 /* Create a new resource using the property values of a CMPI instance. */
244 int Xen_VirtualSystemManagementService_createResourceFromInstance(_RESOURCES * resources,
245 _RESOURCE ** resource,
246 const CMPIInstance * instance,
247 const CMPIBroker * broker)
248 {
249 /* Unsupported. */
250 return -1;
251 }
253 // ----------------------------------------------------------------------------
255 /* Disable the specified resource. */
256 int Xen_VirtualSystemManagementService_disableResource(xen_utils_session *session,
257 _RESOURCES * resources,
258 _RESOURCE * resource)
259 {
260 int rc;
262 xen_host host;
263 if (!xen_session_get_this_host(session->xen, &host, session->xen)) {
264 /* Error description in session object. */
265 return 0;
266 }
268 rc = xen_host_disable(session->xen, host);
270 /* If error occured, description is in session object. */
271 xen_host_free(host);
273 return rc;
274 }
276 // ----------------------------------------------------------------------------
278 /* Enable the specified resource. */
279 int Xen_VirtualSystemManagementService_enableResource(xen_utils_session *session,
280 _RESOURCES * resources,
281 _RESOURCE * resource)
282 {
283 int rc;
285 xen_host host;
286 if (!xen_session_get_this_host(session->xen, &host, session->xen)) {
287 /* Error description in session object. */
288 return 0;
289 }
291 rc = xen_host_enable(session->xen, host);
293 /* If error occured, description is in session object. */
294 xen_host_free(host);
296 return rc;
297 }