service.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 void (*service_unlink_all_complete) (void) = NULL;
  88. static hdb_handle_t unlink_all_handle;
  89. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  90. {
  91. hdb_handle_t object_service_handle;
  92. hdb_handle_t object_find_handle;
  93. char *value;
  94. /*
  95. * Don't link default services if they have been disabled
  96. */
  97. corosync_api->object_find_create (
  98. OBJECT_PARENT_HANDLE,
  99. "aisexec",
  100. strlen ("aisexec"),
  101. &object_find_handle);
  102. if (corosync_api->object_find_next (
  103. object_find_handle,
  104. &object_service_handle) == 0) {
  105. if ( ! corosync_api->object_key_get (object_service_handle,
  106. "defaultservices",
  107. strlen ("defaultservices"),
  108. (void *)&value,
  109. NULL)) {
  110. if (value && strcmp (value, "no") == 0) {
  111. return 0;
  112. }
  113. }
  114. }
  115. corosync_api->object_find_destroy (object_find_handle);
  116. return (-1);
  117. }
  118. unsigned int corosync_service_link_and_init (
  119. struct corosync_api_v1 *corosync_api,
  120. const char *service_name,
  121. unsigned int service_ver)
  122. {
  123. struct corosync_service_engine_iface_ver0 *iface_ver0;
  124. void *iface_ver0_p;
  125. hdb_handle_t handle;
  126. struct corosync_service_engine *service;
  127. unsigned int res;
  128. hdb_handle_t object_service_handle;
  129. hdb_handle_t object_stats_handle;
  130. int fn;
  131. char object_name[32];
  132. char *name_sufix;
  133. uint64_t zero_64 = 0;
  134. /*
  135. * reference the service interface
  136. */
  137. iface_ver0_p = NULL;
  138. lcr_ifact_reference (
  139. &handle,
  140. service_name,
  141. service_ver,
  142. &iface_ver0_p,
  143. (void *)0);
  144. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  145. if (iface_ver0 == 0) {
  146. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  147. return (-1);
  148. }
  149. /*
  150. * Initialize service
  151. */
  152. service = iface_ver0->corosync_get_service_engine_ver0();
  153. ais_service[service->id] = service;
  154. if (service->config_init_fn) {
  155. res = service->config_init_fn (corosync_api);
  156. }
  157. if (service->exec_init_fn) {
  158. res = service->exec_init_fn (corosync_api);
  159. }
  160. /*
  161. * Store service in object database
  162. */
  163. corosync_api->object_create (object_internal_configuration_handle,
  164. &object_service_handle,
  165. "service",
  166. strlen ("service"));
  167. corosync_api->object_key_create_typed (object_service_handle,
  168. "name",
  169. service_name,
  170. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  171. corosync_api->object_key_create_typed (object_service_handle,
  172. "ver",
  173. &service_ver,
  174. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  175. res = corosync_api->object_key_create_typed (object_service_handle,
  176. "handle",
  177. &handle,
  178. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  179. corosync_api->object_key_create_typed (object_service_handle,
  180. "service_id",
  181. &service->id,
  182. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  183. name_sufix = strrchr (service_name, '_');
  184. if (name_sufix)
  185. name_sufix++;
  186. else
  187. name_sufix = (char*)service_name;
  188. corosync_api->object_create (object_stats_services_handle,
  189. &object_stats_handle,
  190. name_sufix, strlen (name_sufix));
  191. corosync_api->object_key_create_typed (object_stats_handle,
  192. "service_id",
  193. &service->id, sizeof (service->id),
  194. OBJDB_VALUETYPE_INT16);
  195. for (fn = 0; fn < service->exec_engine_count; fn++) {
  196. snprintf (object_name, 32, "%d", fn);
  197. corosync_api->object_create (object_stats_handle,
  198. &service_stats_handle[service->id][fn],
  199. object_name, strlen (object_name));
  200. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  201. "tx",
  202. &zero_64, sizeof (zero_64),
  203. OBJDB_VALUETYPE_UINT64);
  204. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  205. "rx",
  206. &zero_64, sizeof (zero_64),
  207. OBJDB_VALUETYPE_UINT64);
  208. }
  209. log_printf (LOGSYS_LEVEL_NOTICE, "Service engine loaded: %s\n", service->name);
  210. return (res);
  211. }
  212. static int service_priority_max(void)
  213. {
  214. int lpc = 0, max = 0;
  215. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  216. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  217. max = ais_service[lpc]->priority;
  218. }
  219. }
  220. return max;
  221. }
  222. /*
  223. * use the force
  224. */
  225. static unsigned int
  226. corosync_service_unlink_priority (
  227. struct corosync_api_v1 *corosync_api,
  228. int lowest_priority,
  229. int *current_priority,
  230. int *current_service_engine)
  231. {
  232. unsigned short *service_id;
  233. hdb_handle_t object_service_handle;
  234. hdb_handle_t object_find_handle;
  235. hdb_handle_t *found_service_handle;
  236. for(; *current_priority >= lowest_priority; *current_priority = *current_priority - 1) {
  237. for(*current_service_engine = 0;
  238. *current_service_engine < SERVICE_HANDLER_MAXIMUM_COUNT;
  239. *current_service_engine = *current_service_engine + 1) {
  240. if(ais_service[*current_service_engine] == NULL ||
  241. ais_service[*current_service_engine]->priority != *current_priority) {
  242. continue;
  243. }
  244. /*
  245. * find service object in object database by service id
  246. * and unload it if possible.
  247. *
  248. * If the service engine's exec_exit_fn returns -1 indicating
  249. * it was busy, this function returns -1 and can be called again
  250. * at a later time (usually via the schedwrk api).
  251. */
  252. corosync_api->object_find_create (
  253. object_internal_configuration_handle,
  254. "service", strlen ("service"), &object_find_handle);
  255. while (corosync_api->object_find_next (
  256. object_find_handle, &object_service_handle) == 0) {
  257. int res = corosync_api->object_key_get (
  258. object_service_handle,
  259. "service_id", strlen ("service_id"),
  260. (void *)&service_id, NULL);
  261. if (res == 0 && *service_id ==
  262. ais_service[*current_service_engine]->id) {
  263. if (ais_service[*service_id]->exec_exit_fn) {
  264. res = ais_service[*service_id]->exec_exit_fn ();
  265. if (res == -1) {
  266. corosync_api->object_find_destroy (object_find_handle);
  267. return (-1);
  268. }
  269. }
  270. log_printf(LOGSYS_LEVEL_NOTICE,
  271. "Service engine unloaded: %s\n",
  272. ais_service[*current_service_engine]->name);
  273. ais_service[*current_service_engine] = NULL;
  274. res = corosync_api->object_key_get (
  275. object_service_handle,
  276. "handle", strlen ("handle"),
  277. (void *)&found_service_handle,
  278. NULL);
  279. lcr_ifact_release (*found_service_handle);
  280. corosync_api->object_destroy (object_service_handle);
  281. break;
  282. }
  283. }
  284. corosync_api->object_find_destroy (object_find_handle);
  285. }
  286. }
  287. return 0;
  288. }
  289. static unsigned int service_unlink_and_exit (
  290. struct corosync_api_v1 *corosync_api,
  291. const char *service_name,
  292. unsigned int service_ver)
  293. {
  294. hdb_handle_t object_service_handle;
  295. char *found_service_name;
  296. unsigned short *service_id;
  297. unsigned int *found_service_ver;
  298. hdb_handle_t object_find_handle;
  299. char *name_sufix;
  300. int res;
  301. name_sufix = strrchr (service_name, '_');
  302. if (name_sufix)
  303. name_sufix++;
  304. else
  305. name_sufix = (char*)service_name;
  306. corosync_api->object_find_create (
  307. object_stats_services_handle,
  308. name_sufix, strlen (name_sufix),
  309. &object_find_handle);
  310. if (corosync_api->object_find_next (
  311. object_find_handle,
  312. &object_service_handle) == 0) {
  313. corosync_api->object_destroy (object_service_handle);
  314. }
  315. corosync_api->object_find_destroy (object_find_handle);
  316. corosync_api->object_find_create (
  317. object_internal_configuration_handle,
  318. "service",
  319. strlen ("service"),
  320. &object_find_handle);
  321. while (corosync_api->object_find_next (
  322. object_find_handle,
  323. &object_service_handle) == 0) {
  324. corosync_api->object_key_get (object_service_handle,
  325. "name",
  326. strlen ("name"),
  327. (void *)&found_service_name,
  328. NULL);
  329. if (strcmp (service_name, found_service_name) != 0) {
  330. continue;
  331. }
  332. corosync_api->object_key_get (object_service_handle,
  333. "ver",
  334. strlen ("ver"),
  335. (void *)&found_service_ver,
  336. NULL);
  337. /*
  338. * If service found and linked exit it
  339. */
  340. if (service_ver != *found_service_ver) {
  341. continue;
  342. }
  343. corosync_api->object_key_get (
  344. object_service_handle,
  345. "service_id", strlen ("service_id"),
  346. (void *)&service_id, NULL);
  347. if(service_id != NULL
  348. && *service_id > 0
  349. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  350. && ais_service[*service_id] != NULL) {
  351. corosync_api->object_find_destroy (object_find_handle);
  352. if (ais_service[*service_id]->exec_exit_fn) {
  353. res = ais_service[*service_id]->exec_exit_fn ();
  354. if (res == -1) {
  355. return (-1);
  356. }
  357. }
  358. log_printf(LOGSYS_LEVEL_NOTICE,
  359. "Service engine unloaded: %s\n",
  360. ais_service[*service_id]->name);
  361. ais_service[*service_id] = NULL;
  362. res = corosync_api->object_key_get (
  363. object_service_handle,
  364. "handle", strlen ("handle"),
  365. (void *)&found_service_handle,
  366. NULL);
  367. lcr_ifact_release (*found_service_handle);
  368. corosync_api->object_destroy (object_service_handle);
  369. }
  370. }
  371. corosync_api->object_find_destroy (object_find_handle);
  372. return (0);
  373. }
  374. /*
  375. * Links default services into the executive
  376. */
  377. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  378. {
  379. unsigned int i;
  380. hdb_handle_t object_service_handle;
  381. char *found_service_name;
  382. char *found_service_ver;
  383. unsigned int found_service_ver_atoi;
  384. hdb_handle_t object_find_handle;
  385. hdb_handle_t object_find2_handle;
  386. hdb_handle_t object_runtime_handle;
  387. corosync_api->object_find_create (
  388. OBJECT_PARENT_HANDLE,
  389. "runtime",
  390. strlen ("runtime"),
  391. &object_find2_handle);
  392. if (corosync_api->object_find_next (
  393. object_find2_handle,
  394. &object_runtime_handle) == 0) {
  395. corosync_api->object_create (object_runtime_handle,
  396. &object_stats_services_handle,
  397. "services", strlen ("services"));
  398. }
  399. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  400. &object_internal_configuration_handle,
  401. "internal_configuration",
  402. strlen ("internal_configuration"));
  403. corosync_api->object_find_create (
  404. OBJECT_PARENT_HANDLE,
  405. "service",
  406. strlen ("service"),
  407. &object_find_handle);
  408. while (corosync_api->object_find_next (
  409. object_find_handle,
  410. &object_service_handle) == 0) {
  411. corosync_api->object_key_get (object_service_handle,
  412. "name",
  413. strlen ("name"),
  414. (void *)&found_service_name,
  415. NULL);
  416. found_service_ver = NULL;
  417. corosync_api->object_key_get (object_service_handle,
  418. "ver",
  419. strlen ("ver"),
  420. (void *)&found_service_ver,
  421. NULL);
  422. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  423. corosync_service_link_and_init (
  424. corosync_api,
  425. found_service_name,
  426. found_service_ver_atoi);
  427. }
  428. corosync_api->object_find_destroy (object_find_handle);
  429. if (default_services_requested (corosync_api) == 0) {
  430. return (0);
  431. }
  432. for (i = 0;
  433. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  434. corosync_service_link_and_init (
  435. corosync_api,
  436. default_services[i].name,
  437. default_services[i].ver);
  438. }
  439. return (0);
  440. }
  441. static int unlink_all_schedwrk_handler (const void *data) {
  442. int res;
  443. static int current_priority = 0;
  444. static int current_service_engine = 0;
  445. static int called = 0;
  446. struct corosync_api_v1 *api = (struct corosync_api_v1 *)data;
  447. if (called == 0) {
  448. log_printf(LOGSYS_LEVEL_NOTICE,
  449. "Unloading all Corosync service engines.\n");
  450. current_priority = service_priority_max ();
  451. called = 1;
  452. }
  453. res = corosync_service_unlink_priority (
  454. api,
  455. 0,
  456. &current_priority,
  457. &current_service_engine);
  458. if (res == 0) {
  459. service_unlink_all_complete();
  460. }
  461. return (res);
  462. }
  463. void corosync_service_unlink_all (
  464. struct corosync_api_v1 *api,
  465. void (*unlink_all_complete) (void))
  466. {
  467. static int called = 0;
  468. assert (api);
  469. service_unlink_all_complete = unlink_all_complete;
  470. if (called) {
  471. return;
  472. }
  473. if (called == 0) {
  474. called = 1;
  475. }
  476. api->schedwrk_create (
  477. &unlink_all_handle,
  478. &unlink_all_schedwrk_handler,
  479. api);
  480. }
  481. struct service_unlink_and_exit_data {
  482. hdb_handle_t handle;
  483. struct corosync_api_v1 *api;
  484. const char *name;
  485. unsigned int ver;
  486. };
  487. static int service_unlink_and_exit_schedwrk_handler (void *data)
  488. {
  489. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  490. data;
  491. int res;
  492. res = service_unlink_and_exit (
  493. service_unlink_and_exit_data->api,
  494. service_unlink_and_exit_data->name,
  495. service_unlink_and_exit_data->ver);
  496. if (res == 0) {
  497. free (service_unlink_and_exit_data);
  498. }
  499. return (res);
  500. }
  501. typedef int (*schedwrk_cast) (const void *);
  502. unsigned int corosync_service_unlink_and_exit (
  503. struct corosync_api_v1 *api,
  504. const char *service_name,
  505. unsigned int service_ver)
  506. {
  507. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  508. assert (api);
  509. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  510. service_unlink_and_exit_data->api = api;
  511. service_unlink_and_exit_data->name = strdup (service_name);
  512. service_unlink_and_exit_data->ver = service_ver;
  513. api->schedwrk_create (
  514. &service_unlink_and_exit_data->handle,
  515. (schedwrk_cast)service_unlink_and_exit_schedwrk_handler,
  516. service_unlink_and_exit_data);
  517. return (0);
  518. }