service.c 13 KB

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