4
0

service.c 9.8 KB

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