load and unload service engines at runtime.
[openais.git] / exec / service.c
blob119e9271c1b32abd0d4e1a69e673a85d984f2678
1 /*
2 * Copyright (c) 2006 MontaVista Software, Inc.
3 * Copyright (c) 2006-2007 Red Hat, Inc.
5 * All rights reserved.
7 * Author: Steven Dake (sdake@redhat.com)
9 * This software licensed under BSD license, the text of which follows:
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
14 * - Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * - Neither the name of the MontaVista Software, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived from this
21 * software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 * THE POSSIBILITY OF SUCH DAMAGE.
36 #include <stdlib.h>
37 #include <string.h>
38 #include <assert.h>
40 #include "../lcr/lcr_ifact.h"
41 #include "swab.h"
42 #include "objdb.h"
43 #include "totem.h"
44 #include "service.h"
45 #include "mainconfig.h"
46 #include "util.h"
47 #include "logsys.h"
49 LOGSYS_DECLARE_SUBSYS ("SERV", LOG_INFO);
51 struct default_service {
52 char *name;
53 int ver;
56 static struct default_service default_services[] = {
58 .name = "openais_evs",
59 .ver = 0,
62 .name = "openais_clm",
63 .ver = 0,
66 .name = "openais_amf",
67 .ver = 0,
70 .name = "openais_ckpt",
71 .ver = 0,
74 .name = "openais_evt",
75 .ver = 0,
78 .name = "openais_lck",
79 .ver = 0,
82 .name = "openais_msg",
83 .ver = 0,
86 .name = "openais_cfg",
87 .ver = 0,
90 .name = "openais_cpg",
91 .ver = 0,
94 .name = "openais_confdb",
95 .ver = 0,
99 struct openais_service_handler *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
101 static unsigned int default_services_requested (struct objdb_iface_ver0 *objdb)
103 unsigned int object_service_handle;
104 char *value;
107 * Don't link default services if they have been disabled
109 objdb->object_find_reset (OBJECT_PARENT_HANDLE);
110 if (objdb->object_find (
111 OBJECT_PARENT_HANDLE,
112 "aisexec",
113 strlen ("aisexec"),
114 &object_service_handle) == 0) {
116 if ( ! objdb->object_key_get (object_service_handle,
117 "defaultservices",
118 strlen ("defaultservices"),
119 (void *)&value,
120 NULL)) {
122 if (value && strcmp (value, "no") == 0) {
123 return 0;
127 return (-1);
130 unsigned int openais_service_link_and_init (
131 struct objdb_iface_ver0 *objdb,
132 char *service_name,
133 unsigned int service_ver)
135 struct openais_service_handler_iface_ver0 *iface_ver0;
136 void *iface_ver0_p;
137 unsigned int handle;
138 struct openais_service_handler *service;
139 unsigned int res;
140 unsigned int object_handle;
143 * reference the service interface
145 iface_ver0_p = NULL;
146 lcr_ifact_reference (
147 &handle,
148 service_name,
149 service_ver,
150 &iface_ver0_p,
151 (void *)0);
153 iface_ver0 = (struct openais_service_handler_iface_ver0 *)iface_ver0_p;
155 if (iface_ver0 == 0) {
156 log_printf(LOG_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
157 return (-1);
162 * Initialize service
164 service = iface_ver0->openais_get_service_handler_ver0();
166 ais_service[service->id] = service;
167 if (service->config_init_fn) {
168 res = service->config_init_fn (objdb);
171 if (service->exec_init_fn) {
172 res = service->exec_init_fn (objdb);
176 * Store service in object database
178 objdb->object_create (OBJECT_PARENT_HANDLE,
179 &object_handle,
180 "service",
181 strlen ("service"));
183 objdb->object_key_create (object_handle,
184 "name",
185 strlen ("name"),
186 service_name,
187 strlen (service_name) + 1);
189 objdb->object_key_create (object_handle,
190 "ver",
191 strlen ("ver"),
192 &service_ver,
193 sizeof (service_ver));
195 res = objdb->object_key_create (object_handle,
196 "handle",
197 strlen ("handle"),
198 &handle,
199 sizeof (handle));
201 objdb->object_key_create (object_handle,
202 "service_id",
203 strlen ("service_id"),
204 &service->id,
205 sizeof (service->id));
207 log_printf (LOG_LEVEL_NOTICE, "Service initialized '%s'\n", service->name);
208 return (res);
211 extern unsigned int openais_service_unlink_and_exit (
212 struct objdb_iface_ver0 *objdb,
213 char *service_name,
214 unsigned int service_ver)
216 unsigned int object_service_handle;
217 char *found_service_name;
218 unsigned int *found_service_ver;
219 unsigned int *found_service_handle;
220 unsigned short *service_id;
221 unsigned int res;
223 objdb->object_find_reset (OBJECT_PARENT_HANDLE);
224 while (objdb->object_find (
225 OBJECT_PARENT_HANDLE,
226 "service",
227 strlen ("service"),
228 &object_service_handle) == 0) {
230 objdb->object_key_get (object_service_handle,
231 "name",
232 strlen ("name"),
233 (void *)&found_service_name,
234 NULL);
236 objdb->object_key_get (object_service_handle,
237 "ver",
238 strlen ("ver"),
239 (void *)&found_service_ver,
240 NULL);
243 * If service found and linked exit it
245 if ((strcmp (service_name, found_service_name) == 0) &&
246 (service_ver == *found_service_ver)) {
248 res = objdb->object_key_get (object_service_handle,
249 "handle",
250 strlen ("handle"),
251 (void *)&found_service_handle,
252 NULL);
254 res = objdb->object_key_get (object_service_handle,
255 "service_id",
256 strlen ("service_id"),
257 (void *)&service_id,
258 NULL);
260 if (ais_service[*service_id]->exec_exit_fn) {
261 ais_service[*service_id]->exec_exit_fn (objdb);
263 ais_service[*service_id] = NULL;
265 res = lcr_ifact_release (*found_service_handle);
266 objdb->object_destroy (object_service_handle);
267 return (res);
270 return (-1);
274 * Links default services into the executive
276 unsigned int openais_service_defaults_link_and_init (struct objdb_iface_ver0 *objdb)
278 unsigned int i;
280 if (default_services_requested (objdb) == 0) {
281 return (0);
283 for (i = 0;
284 i < sizeof (default_services) / sizeof (struct default_service); i++) {
286 openais_service_link_and_init (
287 objdb,
288 default_services[i].name,
289 default_services[i].ver);
292 return (0);