service.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 void (*service_unlink_all_complete) (void) = NULL;
  86. static hdb_handle_t unlink_all_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. /*
  128. * reference the service interface
  129. */
  130. iface_ver0_p = NULL;
  131. lcr_ifact_reference (
  132. &handle,
  133. service_name,
  134. service_ver,
  135. &iface_ver0_p,
  136. (void *)0);
  137. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  138. if (iface_ver0 == 0) {
  139. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  140. return (-1);
  141. }
  142. /*
  143. * Initialize service
  144. */
  145. service = iface_ver0->corosync_get_service_engine_ver0();
  146. ais_service[service->id] = service;
  147. if (service->config_init_fn) {
  148. res = service->config_init_fn (corosync_api);
  149. }
  150. if (service->exec_init_fn) {
  151. res = service->exec_init_fn (corosync_api);
  152. }
  153. /*
  154. * Store service in object database
  155. */
  156. corosync_api->object_create (object_internal_configuration_handle,
  157. &object_service_handle,
  158. "service",
  159. strlen ("service"));
  160. corosync_api->object_key_create_typed (object_service_handle,
  161. "name",
  162. service_name,
  163. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  164. corosync_api->object_key_create_typed (object_service_handle,
  165. "ver",
  166. &service_ver,
  167. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  168. res = corosync_api->object_key_create_typed (object_service_handle,
  169. "handle",
  170. &handle,
  171. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  172. corosync_api->object_key_create_typed (object_service_handle,
  173. "service_id",
  174. &service->id,
  175. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  176. log_printf (LOGSYS_LEVEL_NOTICE, "Service engine loaded: %s\n", service->name);
  177. return (res);
  178. }
  179. static int service_priority_max(void)
  180. {
  181. int lpc = 0, max = 0;
  182. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  183. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  184. max = ais_service[lpc]->priority;
  185. }
  186. }
  187. return max;
  188. }
  189. /*
  190. * use the force
  191. */
  192. static unsigned int
  193. corosync_service_unlink_priority (
  194. struct corosync_api_v1 *corosync_api,
  195. int lowest_priority,
  196. int *current_priority,
  197. int *current_service_engine)
  198. {
  199. unsigned short *service_id;
  200. hdb_handle_t object_service_handle;
  201. hdb_handle_t object_find_handle;
  202. hdb_handle_t *found_service_handle;
  203. for(; *current_priority >= lowest_priority; *current_priority = *current_priority - 1) {
  204. for(*current_service_engine = 0;
  205. *current_service_engine < SERVICE_HANDLER_MAXIMUM_COUNT;
  206. *current_service_engine = *current_service_engine + 1) {
  207. if(ais_service[*current_service_engine] == NULL ||
  208. ais_service[*current_service_engine]->priority != *current_priority) {
  209. continue;
  210. }
  211. /*
  212. * find service object in object database by service id
  213. * and unload it if possible.
  214. *
  215. * If the service engine's exec_exit_fn returns -1 indicating
  216. * it was busy, this function returns -1 and can be called again
  217. * at a later time (usually via the schedwrk api).
  218. */
  219. corosync_api->object_find_create (
  220. object_internal_configuration_handle,
  221. "service", strlen ("service"), &object_find_handle);
  222. while (corosync_api->object_find_next (
  223. object_find_handle, &object_service_handle) == 0) {
  224. int res = corosync_api->object_key_get (
  225. object_service_handle,
  226. "service_id", strlen ("service_id"),
  227. (void *)&service_id, NULL);
  228. if (res == 0 && *service_id ==
  229. ais_service[*current_service_engine]->id) {
  230. if (ais_service[*service_id]->exec_exit_fn) {
  231. res = ais_service[*service_id]->exec_exit_fn ();
  232. if (res == -1) {
  233. corosync_api->object_find_destroy (object_find_handle);
  234. return (-1);
  235. }
  236. }
  237. log_printf(LOGSYS_LEVEL_NOTICE,
  238. "Service engine unloaded: %s\n",
  239. ais_service[*current_service_engine]->name);
  240. ais_service[*current_service_engine] = NULL;
  241. res = corosync_api->object_key_get (
  242. object_service_handle,
  243. "handle", strlen ("handle"),
  244. (void *)&found_service_handle,
  245. NULL);
  246. lcr_ifact_release (*found_service_handle);
  247. corosync_api->object_destroy (object_service_handle);
  248. break;
  249. }
  250. }
  251. corosync_api->object_find_destroy (object_find_handle);
  252. }
  253. }
  254. return 0;
  255. }
  256. static unsigned int service_unlink_and_exit (
  257. struct corosync_api_v1 *corosync_api,
  258. const char *service_name,
  259. unsigned int service_ver)
  260. {
  261. hdb_handle_t object_service_handle;
  262. char *found_service_name;
  263. unsigned short *service_id;
  264. unsigned int *found_service_ver;
  265. hdb_handle_t object_find_handle;
  266. hdb_handle_t *found_service_handle;
  267. int res;
  268. corosync_api->object_find_create (
  269. object_internal_configuration_handle,
  270. "service",
  271. strlen ("service"),
  272. &object_find_handle);
  273. while (corosync_api->object_find_next (
  274. object_find_handle,
  275. &object_service_handle) == 0) {
  276. corosync_api->object_key_get (object_service_handle,
  277. "name",
  278. strlen ("name"),
  279. (void *)&found_service_name,
  280. NULL);
  281. if (strcmp (service_name, found_service_name) != 0) {
  282. continue;
  283. }
  284. corosync_api->object_key_get (object_service_handle,
  285. "ver",
  286. strlen ("ver"),
  287. (void *)&found_service_ver,
  288. NULL);
  289. /*
  290. * If service found and linked exit it
  291. */
  292. if (service_ver != *found_service_ver) {
  293. continue;
  294. }
  295. corosync_api->object_key_get (
  296. object_service_handle,
  297. "service_id", strlen ("service_id"),
  298. (void *)&service_id, NULL);
  299. if(service_id != NULL
  300. && *service_id > 0
  301. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  302. && ais_service[*service_id] != NULL) {
  303. corosync_api->object_find_destroy (object_find_handle);
  304. if (ais_service[*service_id]->exec_exit_fn) {
  305. res = ais_service[*service_id]->exec_exit_fn ();
  306. if (res == -1) {
  307. return (-1);
  308. }
  309. }
  310. log_printf(LOGSYS_LEVEL_NOTICE,
  311. "Service engine unloaded: %s\n",
  312. ais_service[*service_id]->name);
  313. ais_service[*service_id] = NULL;
  314. res = corosync_api->object_key_get (
  315. object_service_handle,
  316. "handle", strlen ("handle"),
  317. (void *)&found_service_handle,
  318. NULL);
  319. lcr_ifact_release (*found_service_handle);
  320. corosync_api->object_destroy (object_service_handle);
  321. }
  322. }
  323. corosync_api->object_find_destroy (object_find_handle);
  324. return (0);
  325. }
  326. /*
  327. * Links default services into the executive
  328. */
  329. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  330. {
  331. unsigned int i;
  332. hdb_handle_t object_service_handle;
  333. char *found_service_name;
  334. char *found_service_ver;
  335. unsigned int found_service_ver_atoi;
  336. hdb_handle_t object_find_handle;
  337. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  338. &object_internal_configuration_handle,
  339. "internal_configuration",
  340. strlen ("internal_configuration"));
  341. corosync_api->object_find_create (
  342. OBJECT_PARENT_HANDLE,
  343. "service",
  344. strlen ("service"),
  345. &object_find_handle);
  346. while (corosync_api->object_find_next (
  347. object_find_handle,
  348. &object_service_handle) == 0) {
  349. corosync_api->object_key_get (object_service_handle,
  350. "name",
  351. strlen ("name"),
  352. (void *)&found_service_name,
  353. NULL);
  354. found_service_ver = NULL;
  355. corosync_api->object_key_get (object_service_handle,
  356. "ver",
  357. strlen ("ver"),
  358. (void *)&found_service_ver,
  359. NULL);
  360. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  361. corosync_service_link_and_init (
  362. corosync_api,
  363. found_service_name,
  364. found_service_ver_atoi);
  365. }
  366. corosync_api->object_find_destroy (object_find_handle);
  367. if (default_services_requested (corosync_api) == 0) {
  368. return (0);
  369. }
  370. for (i = 0;
  371. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  372. corosync_service_link_and_init (
  373. corosync_api,
  374. default_services[i].name,
  375. default_services[i].ver);
  376. }
  377. return (0);
  378. }
  379. static int unlink_all_schedwrk_handler (const void *data) {
  380. int res;
  381. static int current_priority = 0;
  382. static int current_service_engine = 0;
  383. static int called = 0;
  384. struct corosync_api_v1 *api = (struct corosync_api_v1 *)data;
  385. if (called == 0) {
  386. log_printf(LOGSYS_LEVEL_NOTICE,
  387. "Unloading all Corosync service engines.\n");
  388. current_priority = service_priority_max ();
  389. called = 1;
  390. }
  391. res = corosync_service_unlink_priority (
  392. api,
  393. 0,
  394. &current_priority,
  395. &current_service_engine);
  396. if (res == 0) {
  397. service_unlink_all_complete();
  398. }
  399. return (res);
  400. }
  401. void corosync_service_unlink_all (
  402. struct corosync_api_v1 *api,
  403. void (*unlink_all_complete) (void))
  404. {
  405. static int called = 0;
  406. assert (api);
  407. service_unlink_all_complete = unlink_all_complete;
  408. if (called) {
  409. return;
  410. }
  411. if (called == 0) {
  412. called = 1;
  413. }
  414. api->schedwrk_create (
  415. &unlink_all_handle,
  416. &unlink_all_schedwrk_handler,
  417. api);
  418. }
  419. struct service_unlink_and_exit_data {
  420. hdb_handle_t handle;
  421. struct corosync_api_v1 *api;
  422. const char *name;
  423. unsigned int ver;
  424. };
  425. static int service_unlink_and_exit_schedwrk_handler (void *data)
  426. {
  427. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  428. data;
  429. int res;
  430. res = service_unlink_and_exit (
  431. service_unlink_and_exit_data->api,
  432. service_unlink_and_exit_data->name,
  433. service_unlink_and_exit_data->ver);
  434. if (res == 0) {
  435. free (service_unlink_and_exit_data);
  436. }
  437. return (res);
  438. }
  439. typedef int (*schedwrk_cast) (const void *);
  440. unsigned int corosync_service_unlink_and_exit (
  441. struct corosync_api_v1 *api,
  442. const char *service_name,
  443. unsigned int service_ver)
  444. {
  445. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  446. assert (api);
  447. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  448. service_unlink_and_exit_data->api = api;
  449. service_unlink_and_exit_data->name = strdup (service_name);
  450. service_unlink_and_exit_data->ver = service_ver;
  451. api->schedwrk_create (
  452. &service_unlink_and_exit_data->handle,
  453. (schedwrk_cast)service_unlink_and_exit_schedwrk_handler,
  454. service_unlink_and_exit_data);
  455. return (0);
  456. }