service.c 9.8 KB

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