service.c 13 KB

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