os-cmpi-xen

view src/Xen_Console_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 315d491aec5f
children
line source
1 // Copyright (C) 2006 Unisys 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: Rajagopalan Subrahmanian, <raj.subrahmanian@unisys.com>
18 //
19 // Contributors:
20 //
21 // Description:
22 // ============================================================================
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
28 #include <xen_common.h>
29 #include <xen_vm.h>
30 #include <xen_console.h>
32 #include "Xen_Console_Resource.h"
34 /* Include the required CMPI data types, function headers, and macros. */
35 #include "cmpidt.h"
36 #include "cmpift.h"
37 #include "cmpimacs.h"
39 #include "xen_utils.h"
41 /* Include _SBLIM_TRACE() logging support */
42 #include "cmpitrace.h"
44 // ----------------------------------------------------------------------------
46 static int concat_console_lists(xen_console_set **target, xen_console_set *source)
47 {
49 int total_size;
50 int i, j;
52 /* Nothing to concatenate if source is empty */
53 if (source == NULL || source->size == 0)
54 return 1;
56 /* If targe is empty, assign source to target */
57 if (*target == NULL) {
58 *target = source;
59 return 1;
60 }
62 /* realloc memory and append source to target */
63 total_size = (*target)->size + source->size;
64 *target = realloc(*target, sizeof(xen_console_set) + (total_size * sizeof(xen_console)));
65 if (*target == NULL)
66 return 0;
68 for (j = (*target)->size, i = 0; i < source->size; i++) {
69 (*target)->contents[j + i] = source->contents[i];
70 source->contents[i] = NULL;
71 }
72 (*target)->size = total_size;
74 /* Free source list - it has been copied to target */
75 xen_console_set_free(source);
76 return 1;
77 }
80 /* Get a handle to the list of all system resources for this class. */
81 int Xen_Console_getResources(xen_utils_session *session,
82 _RESOURCES ** resources)
83 {
84 xen_domain_resources *res;
85 xen_console_set *all_consoles = NULL;
86 xen_vm_record *vm_rec = NULL;
88 /* malloc a new handle for the resources list. */
89 *resources = (_RESOURCES *)calloc(1, sizeof(_RESOURCES));
90 if (*resources == NULL)
91 return 0;
93 /* Get a list of domain resources. */
94 if (!xen_utils_get_domain_resources(session, &res)) {
95 *resources = NULL;
96 return 0;
97 }
99 /* Create list of console resources from consoles found in each domain. */
100 while (xen_utils_get_next_domain_resource(session, res, &vm_rec)) {
101 /* Ignore halted vms */
102 if (vm_rec->power_state == XEN_VM_POWER_STATE_HALTED) {
103 xen_vm_record_free(vm_rec);
104 continue;
105 }
107 xen_console_set *console_set;
108 if (!xen_vm_get_consoles(session->xen, &console_set, vm_rec->handle))
109 goto Error;
111 if (!concat_console_lists(&all_consoles, console_set))
112 goto Error;
114 xen_vm_record_free(vm_rec);
115 }
117 (*resources)->consoles = all_consoles;
118 (*resources)->currentconsolenum = 0;
120 return 1;
122 Error:
123 xen_vm_record_free(vm_rec);
124 xen_utils_free_domain_resources(res);
125 free(*resources);
126 *resources = NULL;
127 return 0;
128 }
130 // ----------------------------------------------------------------------------
132 /* Free/deallocate/cleanup the resources list after use. */
133 int Xen_Console_freeResources(_RESOURCES * resources)
134 {
135 if (resources != NULL) {
136 if (resources->consoles != NULL)
137 xen_console_set_free(resources->consoles);
138 free(resources);
139 resources = NULL;
140 }
142 return 1;
143 }
145 // ----------------------------------------------------------------------------
147 /* Iterator to get the next resource from the resources list. */
148 int Xen_Console_getNextResource(xen_utils_session *session,
149 _RESOURCES * resources,
150 _RESOURCE ** resource)
151 {
152 if (resources == NULL || resources->consoles == NULL)
153 return 0;
155 /* Check if reached the end of the list of consoles. */
156 if (resources->currentconsolenum == resources->consoles->size)
157 return 0;
159 /* Get the current console record. */
160 if (!xen_console_get_record(session->xen, resource, resources->consoles->contents[resources->currentconsolenum]))
161 return 0;
163 resources->currentconsolenum++;
164 return 1;
165 }
167 // ----------------------------------------------------------------------------
169 /* Get the specific resource that matches the CMPI object path. */
170 int Xen_Console_getResourceForObjectPath(xen_utils_session *session,
171 _RESOURCE ** resource,
172 const CMPIObjectPath * objectpath)
173 {
174 CMPIData namedata;
175 char *keyvalue;
176 int ccode;
177 CMPIStatus status = {CMPI_RC_OK, NULL}; /* Return status of CIM operations. */
179 if (CMIsNullObject(objectpath)) return 0;
181 /* Get the console resource based on DeviceID (console uuid) */
182 namedata = CMGetKey(objectpath, "DeviceID", &status);
183 if ((status.rc != CMPI_RC_OK) || CMIsNullValue(namedata))
184 return 0;
186 keyvalue = CMGetCharPtr(namedata.value.string);
187 if ((keyvalue == NULL) || (*keyvalue == '\0'))
188 return 0;
190 xen_console console;
191 if (!xen_console_get_by_uuid(session->xen, &console, keyvalue))
192 return 0;
194 ccode = xen_console_get_record(session->xen, resource, console);
195 xen_console_free(console);
196 return ccode;
197 }
199 // ----------------------------------------------------------------------------
201 /* Free/deallocate/cleanup the resource after use. */
202 int Xen_Console_freeResource(_RESOURCE * resource)
203 {
204 xen_console_record_free(resource);
205 return 1;
206 }
208 // ----------------------------------------------------------------------------
210 /* Set the property values of a CMPI instance from a specific resource. */
211 int Xen_Console_setInstanceFromResource(xen_utils_session *session,
212 _RESOURCE * resource,
213 const CMPIInstance * instance,
214 const CMPIBroker * broker)
215 {
216 xen_vm_record *alloced_vm_rec = NULL;
217 xen_vm_record *vm_rec;
219 if (resource == NULL) return 0;
220 if (CMIsNullObject(instance)) return 0;
222 xen_vm_record_opt *vm_rec_opt = resource->vm;
223 if (vm_rec_opt->is_record) {
224 vm_rec = vm_rec_opt->u.record;
225 }
226 else {
227 if (!xen_vm_get_record(session->xen, &vm_rec, vm_rec_opt->u.handle)) {
228 /* Error description in session object! */
229 return 0;
230 }
231 alloced_vm_rec = vm_rec;
232 }
234 /* Set the CMPIInstance properties from the resource data. */
235 CMSetProperty(instance, "SystemCreationClassName",(CMPIValue *)"Xen_ComputerSystem", CMPI_chars);
236 CMSetProperty(instance, "SystemName",(CMPIValue *)vm_rec->name_label, CMPI_chars);
237 CMSetProperty(instance, "CreationClassName",(CMPIValue *)"Xen_Console", CMPI_chars);
239 CMSetProperty(instance, "DeviceID",(CMPIValue *)resource->uuid, CMPI_chars);
241 CMSetProperty(instance, "Caption",(CMPIValue *)"Console", CMPI_chars);
242 CMSetProperty(instance, "Description",(CMPIValue *)"Console", CMPI_chars);
244 if (resource->location)
245 CMSetProperty(instance, "URI", (CMPIValue *)resource->location, CMPI_chars);
247 int protocol = -1;
248 if (resource->protocol == XEN_CONSOLE_PROTOCOL_VT100)
249 protocol = 0; /* 0 == VT100 */
250 else if (resource->protocol == XEN_CONSOLE_PROTOCOL_RFB)
251 protocol = 1; /* 1 == VNC */
252 else if (resource->protocol == XEN_CONSOLE_PROTOCOL_RDP)
253 protocol = 2; /* 2 == SDL */
255 if (protocol != -1)
256 CMSetProperty(instance, "Protocol",(CMPIValue *)&protocol, CMPI_uint16);
258 if (alloced_vm_rec)
259 xen_vm_record_free(alloced_vm_rec);
261 return 1;
262 }
264 // ----------------------------------------------------------------------------
266 /* Delete the specified resource from the system. */
267 int Xen_Console_deleteResource(_RESOURCES * resources,
268 _RESOURCE * resource)
269 {
270 /* Unsupported. */
271 return -1;
272 }
274 // ----------------------------------------------------------------------------
276 /* Modify the specified resource using the property values of a CMPI instance. */
277 int Xen_Console_setResourceFromInstance(_RESOURCE * resource,
278 const CMPIInstance * instance,
279 const CMPIBroker * broker)
280 {
281 /* Unsupported. */
282 return -1;
283 }
285 // ----------------------------------------------------------------------------
287 /* Create a new resource using the property values of a CMPI instance. */
288 int Xen_Console_createResourceFromInstance(_RESOURCES * resources,
289 _RESOURCE ** resource,
290 const CMPIInstance * instance,
291 const CMPIBroker * broker)
292 {
293 /* Unsupported. */
294 return -1;
295 }