service.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 (object_service_handle,
  159. "name",
  160. strlen ("name"),
  161. service_name,
  162. strlen (service_name) + 1);
  163. corosync_api->object_key_create (object_service_handle,
  164. "ver",
  165. strlen ("ver"),
  166. &service_ver,
  167. sizeof (service_ver));
  168. res = corosync_api->object_key_create (object_service_handle,
  169. "handle",
  170. strlen ("handle"),
  171. &handle,
  172. sizeof (handle));
  173. corosync_api->object_key_create (object_service_handle,
  174. "service_id",
  175. strlen ("service_id"),
  176. &service->id,
  177. sizeof (service->id));
  178. log_printf (LOGSYS_LEVEL_NOTICE, "Service initialized '%s'\n", service->name);
  179. return (res);
  180. }
  181. static int service_priority_max(void)
  182. {
  183. int lpc = 0, max = 0;
  184. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  185. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  186. max = ais_service[lpc]->priority;
  187. }
  188. }
  189. return max;
  190. }
  191. extern unsigned int corosync_service_unlink_priority (struct corosync_api_v1 *corosync_api, int priority)
  192. {
  193. char *service_name;
  194. unsigned int *service_ver;
  195. unsigned short *service_id;
  196. hdb_handle_t object_service_handle;
  197. hdb_handle_t object_find_handle;
  198. int p = service_priority_max();
  199. int lpc = 0;
  200. if(priority == 0) {
  201. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading all corosync components\n");
  202. } else {
  203. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading corosync components up to (and including) priority %d\n", priority);
  204. }
  205. for( ; p >= priority; p--) {
  206. for(lpc = 0; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  207. if(ais_service[lpc] == NULL || ais_service[lpc]->priority != p) {
  208. continue;
  209. }
  210. /* unload
  211. *
  212. * If we had a pointer to the objdb entry, we'd not need to go looking again...
  213. */
  214. corosync_api->object_find_create (
  215. object_internal_configuration_handle,
  216. "service", strlen ("service"), &object_find_handle);
  217. while(corosync_api->object_find_next (
  218. object_find_handle, &object_service_handle) == 0) {
  219. int res = corosync_api->object_key_get (
  220. object_service_handle,
  221. "service_id", strlen ("service_id"), (void *)&service_id, NULL);
  222. service_name = NULL;
  223. if(res == 0 && *service_id == ais_service[lpc]->id) {
  224. hdb_handle_t *found_service_handle;
  225. corosync_api->object_key_get (
  226. object_service_handle,
  227. "name", strlen ("name"), (void *)&service_name, NULL);
  228. corosync_api->object_key_get (
  229. object_service_handle,
  230. "ver", strlen ("ver"), (void *)&service_ver, NULL);
  231. res = corosync_api->object_key_get (
  232. object_service_handle,
  233. "handle", strlen ("handle"), (void *)&found_service_handle, NULL);
  234. res = corosync_api->object_key_get (
  235. object_service_handle,
  236. "service_id", strlen ("service_id"), (void *)&service_id, NULL);
  237. log_printf(LOGSYS_LEVEL_NOTICE, "Unloading corosync component: %s v%u\n",
  238. service_name, *service_ver);
  239. if (ais_service[*service_id]->exec_exit_fn) {
  240. ais_service[*service_id]->exec_exit_fn ();
  241. }
  242. ais_service[*service_id] = NULL;
  243. lcr_ifact_release (*found_service_handle);
  244. corosync_api->object_destroy (object_service_handle);
  245. break;
  246. }
  247. }
  248. corosync_api->object_find_destroy (object_find_handle);
  249. }
  250. }
  251. return 0;
  252. }
  253. extern unsigned int corosync_service_unlink_and_exit (
  254. struct corosync_api_v1 *corosync_api,
  255. const char *service_name,
  256. unsigned int service_ver)
  257. {
  258. hdb_handle_t object_service_handle;
  259. char *found_service_name;
  260. unsigned short *service_id;
  261. unsigned int *found_service_ver;
  262. hdb_handle_t object_find_handle;
  263. corosync_api->object_find_create (
  264. object_internal_configuration_handle,
  265. "service",
  266. strlen ("service"),
  267. &object_find_handle);
  268. while (corosync_api->object_find_next (
  269. object_find_handle,
  270. &object_service_handle) == 0) {
  271. corosync_api->object_key_get (object_service_handle,
  272. "name",
  273. strlen ("name"),
  274. (void *)&found_service_name,
  275. NULL);
  276. if (strcmp (service_name, found_service_name) != 0) {
  277. continue;
  278. }
  279. corosync_api->object_key_get (object_service_handle,
  280. "ver",
  281. strlen ("ver"),
  282. (void *)&found_service_ver,
  283. NULL);
  284. /*
  285. * If service found and linked exit it
  286. */
  287. if (service_ver != *found_service_ver) {
  288. continue;
  289. }
  290. corosync_api->object_key_get (
  291. object_service_handle,
  292. "service_id", strlen ("service_id"),
  293. (void *)&service_id, NULL);
  294. if(service_id != NULL
  295. && *service_id > 0
  296. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  297. && ais_service[*service_id] != NULL) {
  298. corosync_api->object_find_destroy (object_find_handle);
  299. return corosync_service_unlink_priority (corosync_api, ais_service[*service_id]->priority);
  300. }
  301. }
  302. corosync_api->object_find_destroy (object_find_handle);
  303. return (-1);
  304. }
  305. extern unsigned int corosync_service_unlink_all (
  306. struct corosync_api_v1 *corosync_api)
  307. {
  308. return corosync_service_unlink_priority (corosync_api, 0);
  309. }
  310. /*
  311. * Links default services into the executive
  312. */
  313. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  314. {
  315. unsigned int i;
  316. hdb_handle_t object_service_handle;
  317. char *found_service_name;
  318. char *found_service_ver;
  319. unsigned int found_service_ver_atoi;
  320. hdb_handle_t object_find_handle;
  321. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  322. &object_internal_configuration_handle,
  323. "internal_configuration",
  324. strlen ("internal_configuration"));
  325. corosync_api->object_find_create (
  326. OBJECT_PARENT_HANDLE,
  327. "service",
  328. strlen ("service"),
  329. &object_find_handle);
  330. while (corosync_api->object_find_next (
  331. object_find_handle,
  332. &object_service_handle) == 0) {
  333. corosync_api->object_key_get (object_service_handle,
  334. "name",
  335. strlen ("name"),
  336. (void *)&found_service_name,
  337. NULL);
  338. found_service_ver = NULL;
  339. corosync_api->object_key_get (object_service_handle,
  340. "ver",
  341. strlen ("ver"),
  342. (void *)&found_service_ver,
  343. NULL);
  344. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  345. corosync_service_link_and_init (
  346. corosync_api,
  347. found_service_name,
  348. found_service_ver_atoi);
  349. }
  350. corosync_api->object_find_destroy (object_find_handle);
  351. if (default_services_requested (corosync_api) == 0) {
  352. return (0);
  353. }
  354. for (i = 0;
  355. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  356. corosync_service_link_and_init (
  357. corosync_api,
  358. default_services[i].name,
  359. default_services[i].ver);
  360. }
  361. return (0);
  362. }