service.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. .name = "corosync_quorum",
  80. .ver = 0,
  81. }
  82. };
  83. struct corosync_service_engine *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
  84. static hdb_handle_t object_internal_configuration_handle;
  85. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  86. {
  87. hdb_handle_t object_service_handle;
  88. hdb_handle_t object_find_handle;
  89. char *value;
  90. /*
  91. * Don't link default services if they have been disabled
  92. */
  93. corosync_api->object_find_create (
  94. OBJECT_PARENT_HANDLE,
  95. "aisexec",
  96. strlen ("aisexec"),
  97. &object_find_handle);
  98. if (corosync_api->object_find_next (
  99. object_find_handle,
  100. &object_service_handle) == 0) {
  101. if ( ! corosync_api->object_key_get (object_service_handle,
  102. "defaultservices",
  103. strlen ("defaultservices"),
  104. (void *)&value,
  105. NULL)) {
  106. if (value && strcmp (value, "no") == 0) {
  107. return 0;
  108. }
  109. }
  110. }
  111. corosync_api->object_find_destroy (object_find_handle);
  112. return (-1);
  113. }
  114. unsigned int corosync_service_link_and_init (
  115. struct corosync_api_v1 *corosync_api,
  116. const char *service_name,
  117. unsigned int service_ver)
  118. {
  119. struct corosync_service_engine_iface_ver0 *iface_ver0;
  120. void *iface_ver0_p;
  121. hdb_handle_t handle;
  122. struct corosync_service_engine *service;
  123. unsigned int res;
  124. hdb_handle_t object_service_handle;
  125. /*
  126. * reference the service interface
  127. */
  128. iface_ver0_p = NULL;
  129. lcr_ifact_reference (
  130. &handle,
  131. service_name,
  132. service_ver,
  133. &iface_ver0_p,
  134. (void *)0);
  135. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  136. if (iface_ver0 == 0) {
  137. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  138. return (-1);
  139. }
  140. /*
  141. * Initialize service
  142. */
  143. service = iface_ver0->corosync_get_service_engine_ver0();
  144. ais_service[service->id] = service;
  145. if (service->config_init_fn) {
  146. res = service->config_init_fn (corosync_api);
  147. }
  148. if (service->exec_init_fn) {
  149. res = service->exec_init_fn (corosync_api);
  150. }
  151. /*
  152. * Store service in object database
  153. */
  154. corosync_api->object_create (object_internal_configuration_handle,
  155. &object_service_handle,
  156. "service",
  157. strlen ("service"));
  158. corosync_api->object_key_create_typed (object_service_handle,
  159. "name",
  160. service_name,
  161. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  162. corosync_api->object_key_create_typed (object_service_handle,
  163. "ver",
  164. &service_ver,
  165. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  166. res = corosync_api->object_key_create_typed (object_service_handle,
  167. "handle",
  168. &handle,
  169. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  170. corosync_api->object_key_create_typed (object_service_handle,
  171. "service_id",
  172. &service->id,
  173. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  174. log_printf (LOGSYS_LEVEL_NOTICE, "Service initialized '%s'\n", service->name);
  175. return (res);
  176. }
  177. static int service_priority_max(void)
  178. {
  179. int lpc = 0, max = 0;
  180. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  181. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  182. max = ais_service[lpc]->priority;
  183. }
  184. }
  185. return max;
  186. }
  187. extern unsigned int corosync_service_unlink_priority (struct corosync_api_v1 *corosync_api, int priority)
  188. {
  189. char *service_name;
  190. unsigned int *service_ver;
  191. unsigned short *service_id;
  192. hdb_handle_t object_service_handle;
  193. hdb_handle_t object_find_handle;
  194. int p = service_priority_max();
  195. int lpc = 0;
  196. if(priority == 0) {
  197. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading all corosync components\n");
  198. } else {
  199. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading corosync components up to (and including) priority %d\n", priority);
  200. }
  201. for( ; p >= priority; p--) {
  202. for(lpc = 0; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  203. if(ais_service[lpc] == NULL || ais_service[lpc]->priority != p) {
  204. continue;
  205. }
  206. /* unload
  207. *
  208. * If we had a pointer to the objdb entry, we'd not need to go looking again...
  209. */
  210. corosync_api->object_find_create (
  211. object_internal_configuration_handle,
  212. "service", strlen ("service"), &object_find_handle);
  213. while(corosync_api->object_find_next (
  214. object_find_handle, &object_service_handle) == 0) {
  215. int res = corosync_api->object_key_get (
  216. object_service_handle,
  217. "service_id", strlen ("service_id"), (void *)&service_id, NULL);
  218. service_name = NULL;
  219. if(res == 0 && *service_id == ais_service[lpc]->id) {
  220. hdb_handle_t *found_service_handle;
  221. corosync_api->object_key_get (
  222. object_service_handle,
  223. "name", strlen ("name"), (void *)&service_name, NULL);
  224. corosync_api->object_key_get (
  225. object_service_handle,
  226. "ver", strlen ("ver"), (void *)&service_ver, NULL);
  227. res = corosync_api->object_key_get (
  228. object_service_handle,
  229. "handle", strlen ("handle"), (void *)&found_service_handle, NULL);
  230. res = corosync_api->object_key_get (
  231. object_service_handle,
  232. "service_id", strlen ("service_id"), (void *)&service_id, NULL);
  233. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading corosync component: %s v%u\n",
  234. service_name, *service_ver);
  235. if (ais_service[*service_id]->exec_exit_fn) {
  236. ais_service[*service_id]->exec_exit_fn ();
  237. }
  238. ais_service[*service_id] = NULL;
  239. lcr_ifact_release (*found_service_handle);
  240. corosync_api->object_destroy (object_service_handle);
  241. break;
  242. }
  243. }
  244. corosync_api->object_find_destroy (object_find_handle);
  245. }
  246. }
  247. return 0;
  248. }
  249. extern unsigned int corosync_service_unlink_and_exit (
  250. struct corosync_api_v1 *corosync_api,
  251. const char *service_name,
  252. unsigned int service_ver)
  253. {
  254. hdb_handle_t object_service_handle;
  255. char *found_service_name;
  256. unsigned short *service_id;
  257. unsigned int *found_service_ver;
  258. hdb_handle_t object_find_handle;
  259. corosync_api->object_find_create (
  260. object_internal_configuration_handle,
  261. "service",
  262. strlen ("service"),
  263. &object_find_handle);
  264. while (corosync_api->object_find_next (
  265. object_find_handle,
  266. &object_service_handle) == 0) {
  267. corosync_api->object_key_get (object_service_handle,
  268. "name",
  269. strlen ("name"),
  270. (void *)&found_service_name,
  271. NULL);
  272. if (strcmp (service_name, found_service_name) != 0) {
  273. continue;
  274. }
  275. corosync_api->object_key_get (object_service_handle,
  276. "ver",
  277. strlen ("ver"),
  278. (void *)&found_service_ver,
  279. NULL);
  280. /*
  281. * If service found and linked exit it
  282. */
  283. if (service_ver != *found_service_ver) {
  284. continue;
  285. }
  286. corosync_api->object_key_get (
  287. object_service_handle,
  288. "service_id", strlen ("service_id"),
  289. (void *)&service_id, NULL);
  290. if(service_id != NULL
  291. && *service_id > 0
  292. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  293. && ais_service[*service_id] != NULL) {
  294. corosync_api->object_find_destroy (object_find_handle);
  295. return corosync_service_unlink_priority (corosync_api, ais_service[*service_id]->priority);
  296. }
  297. }
  298. corosync_api->object_find_destroy (object_find_handle);
  299. return (-1);
  300. }
  301. extern unsigned int corosync_service_unlink_all (
  302. struct corosync_api_v1 *corosync_api)
  303. {
  304. return corosync_service_unlink_priority (corosync_api, 0);
  305. }
  306. /*
  307. * Links default services into the executive
  308. */
  309. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  310. {
  311. unsigned int i;
  312. hdb_handle_t object_service_handle;
  313. char *found_service_name;
  314. char *found_service_ver;
  315. unsigned int found_service_ver_atoi;
  316. hdb_handle_t object_find_handle;
  317. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  318. &object_internal_configuration_handle,
  319. "internal_configuration",
  320. strlen ("internal_configuration"));
  321. corosync_api->object_find_create (
  322. OBJECT_PARENT_HANDLE,
  323. "service",
  324. strlen ("service"),
  325. &object_find_handle);
  326. while (corosync_api->object_find_next (
  327. object_find_handle,
  328. &object_service_handle) == 0) {
  329. corosync_api->object_key_get (object_service_handle,
  330. "name",
  331. strlen ("name"),
  332. (void *)&found_service_name,
  333. NULL);
  334. found_service_ver = NULL;
  335. corosync_api->object_key_get (object_service_handle,
  336. "ver",
  337. strlen ("ver"),
  338. (void *)&found_service_ver,
  339. NULL);
  340. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  341. corosync_service_link_and_init (
  342. corosync_api,
  343. found_service_name,
  344. found_service_ver_atoi);
  345. }
  346. corosync_api->object_find_destroy (object_find_handle);
  347. if (default_services_requested (corosync_api) == 0) {
  348. return (0);
  349. }
  350. for (i = 0;
  351. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  352. corosync_service_link_and_init (
  353. corosync_api,
  354. default_services[i].name,
  355. default_services[i].ver);
  356. }
  357. return (0);
  358. }