4
0

service.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  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.
  22. *
  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.
  34. */
  35. #include <config.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <assert.h>
  39. #include <corosync/lcr/lcr_ifact.h>
  40. #include <corosync/swab.h>
  41. #include <corosync/totem/totem.h>
  42. #include <corosync/corotypes.h>
  43. #include <corosync/coroipc_types.h>
  44. #include "mainconfig.h"
  45. #include "util.h"
  46. #include <corosync/engine/logsys.h>
  47. #include "timer.h"
  48. #include <corosync/totem/totempg.h>
  49. #include <corosync/totem/totemip.h>
  50. #include "main.h"
  51. #include <corosync/engine/coroapi.h>
  52. #include "service.h"
  53. LOGSYS_DECLARE_SUBSYS ("SERV");
  54. struct default_service {
  55. const char *name;
  56. int ver;
  57. };
  58. static struct default_service default_services[] = {
  59. {
  60. .name = "corosync_evs",
  61. .ver = 0,
  62. },
  63. {
  64. .name = "corosync_cfg",
  65. .ver = 0,
  66. },
  67. {
  68. .name = "corosync_cpg",
  69. .ver = 0,
  70. },
  71. {
  72. .name = "corosync_confdb",
  73. .ver = 0,
  74. },
  75. {
  76. .name = "corosync_pload",
  77. .ver = 0,
  78. }
  79. };
  80. struct corosync_service_engine *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
  81. static hdb_handle_t object_internal_configuration_handle;
  82. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  83. {
  84. hdb_handle_t object_service_handle;
  85. hdb_handle_t object_find_handle;
  86. char *value;
  87. /*
  88. * Don't link default services if they have been disabled
  89. */
  90. corosync_api->object_find_create (
  91. OBJECT_PARENT_HANDLE,
  92. "aisexec",
  93. strlen ("aisexec"),
  94. &object_find_handle);
  95. if (corosync_api->object_find_next (
  96. object_find_handle,
  97. &object_service_handle) == 0) {
  98. if ( ! corosync_api->object_key_get (object_service_handle,
  99. "defaultservices",
  100. strlen ("defaultservices"),
  101. (void *)&value,
  102. NULL)) {
  103. if (value && strcmp (value, "no") == 0) {
  104. return 0;
  105. }
  106. }
  107. }
  108. corosync_api->object_find_destroy (object_find_handle);
  109. return (-1);
  110. }
  111. unsigned int corosync_service_link_and_init (
  112. struct corosync_api_v1 *corosync_api,
  113. const char *service_name,
  114. unsigned int service_ver)
  115. {
  116. struct corosync_service_engine_iface_ver0 *iface_ver0;
  117. void *iface_ver0_p;
  118. hdb_handle_t handle;
  119. struct corosync_service_engine *service;
  120. unsigned int res;
  121. hdb_handle_t object_service_handle;
  122. /*
  123. * reference the service interface
  124. */
  125. iface_ver0_p = NULL;
  126. lcr_ifact_reference (
  127. &handle,
  128. service_name,
  129. service_ver,
  130. &iface_ver0_p,
  131. (void *)0);
  132. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  133. if (iface_ver0 == 0) {
  134. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  135. return (-1);
  136. }
  137. /*
  138. * Initialize service
  139. */
  140. service = iface_ver0->corosync_get_service_engine_ver0();
  141. ais_service[service->id] = service;
  142. if (service->config_init_fn) {
  143. res = service->config_init_fn (corosync_api);
  144. }
  145. if (service->exec_init_fn) {
  146. res = service->exec_init_fn (corosync_api);
  147. }
  148. /*
  149. * Store service in object database
  150. */
  151. corosync_api->object_create (object_internal_configuration_handle,
  152. &object_service_handle,
  153. "service",
  154. strlen ("service"));
  155. corosync_api->object_key_create (object_service_handle,
  156. "name",
  157. strlen ("name"),
  158. service_name,
  159. strlen (service_name) + 1);
  160. corosync_api->object_key_create (object_service_handle,
  161. "ver",
  162. strlen ("ver"),
  163. &service_ver,
  164. sizeof (service_ver));
  165. res = corosync_api->object_key_create (object_service_handle,
  166. "handle",
  167. strlen ("handle"),
  168. &handle,
  169. sizeof (handle));
  170. corosync_api->object_key_create (object_service_handle,
  171. "service_id",
  172. strlen ("service_id"),
  173. &service->id,
  174. sizeof (service->id));
  175. log_printf (LOGSYS_LEVEL_NOTICE, "Service initialized '%s'\n", service->name);
  176. return (res);
  177. }
  178. static int corosync_service_unlink_common (
  179. struct corosync_api_v1 *corosync_api,
  180. hdb_handle_t object_service_handle,
  181. const char *service_name,
  182. unsigned int service_version)
  183. {
  184. unsigned int res;
  185. unsigned short *service_id;
  186. hdb_handle_t *found_service_handle;
  187. res = corosync_api->object_key_get (object_service_handle,
  188. "handle",
  189. strlen ("handle"),
  190. (void *)&found_service_handle,
  191. NULL);
  192. res = corosync_api->object_key_get (object_service_handle,
  193. "service_id",
  194. strlen ("service_id"),
  195. (void *)&service_id,
  196. NULL);
  197. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading corosync component: %s v%u\n",
  198. service_name, service_version);
  199. if (ais_service[*service_id]->exec_exit_fn) {
  200. ais_service[*service_id]->exec_exit_fn ();
  201. }
  202. ais_service[*service_id] = NULL;
  203. return lcr_ifact_release (*found_service_handle);
  204. }
  205. extern unsigned int corosync_service_unlink_and_exit (
  206. struct corosync_api_v1 *corosync_api,
  207. const char *service_name,
  208. unsigned int service_ver)
  209. {
  210. unsigned int res;
  211. hdb_handle_t object_service_handle;
  212. char *found_service_name;
  213. unsigned int *found_service_ver;
  214. hdb_handle_t object_find_handle;
  215. corosync_api->object_find_create (
  216. object_internal_configuration_handle,
  217. "service",
  218. strlen ("service"),
  219. &object_find_handle);
  220. while (corosync_api->object_find_next (
  221. object_find_handle,
  222. &object_service_handle) == 0) {
  223. corosync_api->object_key_get (object_service_handle,
  224. "name",
  225. strlen ("name"),
  226. (void *)&found_service_name,
  227. NULL);
  228. corosync_api->object_key_get (object_service_handle,
  229. "ver",
  230. strlen ("ver"),
  231. (void *)&found_service_ver,
  232. NULL);
  233. /*
  234. * If service found and linked exit it
  235. */
  236. if ((strcmp (service_name, found_service_name) == 0) &&
  237. (service_ver == *found_service_ver)) {
  238. res = corosync_service_unlink_common (
  239. corosync_api, object_service_handle,
  240. service_name, service_ver);
  241. corosync_api->object_destroy (object_service_handle);
  242. return res;
  243. }
  244. }
  245. corosync_api->object_find_destroy (object_find_handle);
  246. return (-1);
  247. }
  248. extern unsigned int corosync_service_unlink_all (
  249. struct corosync_api_v1 *corosync_api)
  250. {
  251. char *service_name;
  252. unsigned int *service_ver;
  253. hdb_handle_t object_service_handle;
  254. hdb_handle_t object_find_handle;
  255. int found;
  256. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading all corosync components\n");
  257. /*
  258. * TODO
  259. * Deleting of keys not supported during iteration at this time
  260. * hence this ugly hack
  261. */
  262. while(corosync_api->object_find_create (
  263. object_internal_configuration_handle,
  264. "service",
  265. strlen ("service"),
  266. &object_find_handle) == 0)
  267. {
  268. found = 0;
  269. while(corosync_api->object_find_next (
  270. object_find_handle,
  271. &object_service_handle) == 0)
  272. found = 1;
  273. if(!found)
  274. break;
  275. corosync_api->object_key_get (
  276. object_service_handle,
  277. "name",
  278. strlen ("name"),
  279. (void *)&service_name,
  280. NULL);
  281. corosync_api->object_key_get (
  282. object_service_handle,
  283. "ver",
  284. strlen ("ver"),
  285. (void *)&service_ver,
  286. NULL);
  287. corosync_service_unlink_common (
  288. corosync_api, object_service_handle,
  289. service_name, *service_ver);
  290. corosync_api->object_destroy (object_service_handle);
  291. corosync_api->object_find_destroy (object_find_handle);
  292. }
  293. return (0);
  294. }
  295. /*
  296. * Links default services into the executive
  297. */
  298. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  299. {
  300. unsigned int i;
  301. hdb_handle_t object_service_handle;
  302. char *found_service_name;
  303. char *found_service_ver;
  304. unsigned int found_service_ver_atoi;
  305. hdb_handle_t object_find_handle;
  306. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  307. &object_internal_configuration_handle,
  308. "internal_configuration",
  309. strlen ("internal_configuration"));
  310. corosync_api->object_find_create (
  311. OBJECT_PARENT_HANDLE,
  312. "service",
  313. strlen ("service"),
  314. &object_find_handle);
  315. while (corosync_api->object_find_next (
  316. object_find_handle,
  317. &object_service_handle) == 0) {
  318. corosync_api->object_key_get (object_service_handle,
  319. "name",
  320. strlen ("name"),
  321. (void *)&found_service_name,
  322. NULL);
  323. corosync_api->object_key_get (object_service_handle,
  324. "ver",
  325. strlen ("ver"),
  326. (void *)&found_service_ver,
  327. NULL);
  328. found_service_ver_atoi = atoi (found_service_ver);
  329. corosync_service_link_and_init (
  330. corosync_api,
  331. found_service_name,
  332. found_service_ver_atoi);
  333. }
  334. corosync_api->object_find_destroy (object_find_handle);
  335. if (default_services_requested (corosync_api) == 0) {
  336. return (0);
  337. }
  338. for (i = 0;
  339. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  340. corosync_service_link_and_init (
  341. corosync_api,
  342. default_services[i].name,
  343. default_services[i].ver);
  344. }
  345. return (0);
  346. }