service.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. hdb_handle_t *found_service_handle;
  300. char *name_sufix;
  301. int res;
  302. name_sufix = strrchr (service_name, '_');
  303. if (name_sufix)
  304. name_sufix++;
  305. else
  306. name_sufix = (char*)service_name;
  307. corosync_api->object_find_create (
  308. object_stats_services_handle,
  309. name_sufix, strlen (name_sufix),
  310. &object_find_handle);
  311. if (corosync_api->object_find_next (
  312. object_find_handle,
  313. &object_service_handle) == 0) {
  314. corosync_api->object_destroy (object_service_handle);
  315. }
  316. corosync_api->object_find_destroy (object_find_handle);
  317. corosync_api->object_find_create (
  318. object_internal_configuration_handle,
  319. "service",
  320. strlen ("service"),
  321. &object_find_handle);
  322. while (corosync_api->object_find_next (
  323. object_find_handle,
  324. &object_service_handle) == 0) {
  325. corosync_api->object_key_get (object_service_handle,
  326. "name",
  327. strlen ("name"),
  328. (void *)&found_service_name,
  329. NULL);
  330. if (strcmp (service_name, found_service_name) != 0) {
  331. continue;
  332. }
  333. corosync_api->object_key_get (object_service_handle,
  334. "ver",
  335. strlen ("ver"),
  336. (void *)&found_service_ver,
  337. NULL);
  338. /*
  339. * If service found and linked exit it
  340. */
  341. if (service_ver != *found_service_ver) {
  342. continue;
  343. }
  344. corosync_api->object_key_get (
  345. object_service_handle,
  346. "service_id", strlen ("service_id"),
  347. (void *)&service_id, NULL);
  348. if(service_id != NULL
  349. && *service_id > 0
  350. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  351. && ais_service[*service_id] != NULL) {
  352. corosync_api->object_find_destroy (object_find_handle);
  353. if (ais_service[*service_id]->exec_exit_fn) {
  354. res = ais_service[*service_id]->exec_exit_fn ();
  355. if (res == -1) {
  356. return (-1);
  357. }
  358. }
  359. log_printf(LOGSYS_LEVEL_NOTICE,
  360. "Service engine unloaded: %s\n",
  361. ais_service[*service_id]->name);
  362. ais_service[*service_id] = NULL;
  363. res = corosync_api->object_key_get (
  364. object_service_handle,
  365. "handle", strlen ("handle"),
  366. (void *)&found_service_handle,
  367. NULL);
  368. lcr_ifact_release (*found_service_handle);
  369. corosync_api->object_destroy (object_service_handle);
  370. }
  371. }
  372. corosync_api->object_find_destroy (object_find_handle);
  373. return (0);
  374. }
  375. /*
  376. * Links default services into the executive
  377. */
  378. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  379. {
  380. unsigned int i;
  381. hdb_handle_t object_service_handle;
  382. char *found_service_name;
  383. char *found_service_ver;
  384. unsigned int found_service_ver_atoi;
  385. hdb_handle_t object_find_handle;
  386. hdb_handle_t object_find2_handle;
  387. hdb_handle_t object_runtime_handle;
  388. corosync_api->object_find_create (
  389. OBJECT_PARENT_HANDLE,
  390. "runtime",
  391. strlen ("runtime"),
  392. &object_find2_handle);
  393. if (corosync_api->object_find_next (
  394. object_find2_handle,
  395. &object_runtime_handle) == 0) {
  396. corosync_api->object_create (object_runtime_handle,
  397. &object_stats_services_handle,
  398. "services", strlen ("services"));
  399. }
  400. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  401. &object_internal_configuration_handle,
  402. "internal_configuration",
  403. strlen ("internal_configuration"));
  404. corosync_api->object_find_create (
  405. OBJECT_PARENT_HANDLE,
  406. "service",
  407. strlen ("service"),
  408. &object_find_handle);
  409. while (corosync_api->object_find_next (
  410. object_find_handle,
  411. &object_service_handle) == 0) {
  412. corosync_api->object_key_get (object_service_handle,
  413. "name",
  414. strlen ("name"),
  415. (void *)&found_service_name,
  416. NULL);
  417. found_service_ver = NULL;
  418. corosync_api->object_key_get (object_service_handle,
  419. "ver",
  420. strlen ("ver"),
  421. (void *)&found_service_ver,
  422. NULL);
  423. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  424. corosync_service_link_and_init (
  425. corosync_api,
  426. found_service_name,
  427. found_service_ver_atoi);
  428. }
  429. corosync_api->object_find_destroy (object_find_handle);
  430. if (default_services_requested (corosync_api) == 0) {
  431. return (0);
  432. }
  433. for (i = 0;
  434. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  435. corosync_service_link_and_init (
  436. corosync_api,
  437. default_services[i].name,
  438. default_services[i].ver);
  439. }
  440. return (0);
  441. }
  442. static int unlink_all_schedwrk_handler (const void *data) {
  443. int res;
  444. static int current_priority = 0;
  445. static int current_service_engine = 0;
  446. static int called = 0;
  447. struct corosync_api_v1 *api = (struct corosync_api_v1 *)data;
  448. if (called == 0) {
  449. log_printf(LOGSYS_LEVEL_NOTICE,
  450. "Unloading all Corosync service engines.\n");
  451. current_priority = service_priority_max ();
  452. called = 1;
  453. }
  454. res = corosync_service_unlink_priority (
  455. api,
  456. 0,
  457. &current_priority,
  458. &current_service_engine);
  459. if (res == 0) {
  460. service_unlink_all_complete();
  461. }
  462. return (res);
  463. }
  464. void corosync_service_unlink_all (
  465. struct corosync_api_v1 *api,
  466. void (*unlink_all_complete) (void))
  467. {
  468. static int called = 0;
  469. assert (api);
  470. service_unlink_all_complete = unlink_all_complete;
  471. if (called) {
  472. return;
  473. }
  474. if (called == 0) {
  475. called = 1;
  476. }
  477. api->schedwrk_create (
  478. &unlink_all_handle,
  479. &unlink_all_schedwrk_handler,
  480. api);
  481. }
  482. struct service_unlink_and_exit_data {
  483. hdb_handle_t handle;
  484. struct corosync_api_v1 *api;
  485. const char *name;
  486. unsigned int ver;
  487. };
  488. static int service_unlink_and_exit_schedwrk_handler (void *data)
  489. {
  490. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  491. data;
  492. int res;
  493. res = service_unlink_and_exit (
  494. service_unlink_and_exit_data->api,
  495. service_unlink_and_exit_data->name,
  496. service_unlink_and_exit_data->ver);
  497. if (res == 0) {
  498. free (service_unlink_and_exit_data);
  499. }
  500. return (res);
  501. }
  502. typedef int (*schedwrk_cast) (const void *);
  503. unsigned int corosync_service_unlink_and_exit (
  504. struct corosync_api_v1 *api,
  505. const char *service_name,
  506. unsigned int service_ver)
  507. {
  508. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  509. assert (api);
  510. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  511. service_unlink_and_exit_data->api = api;
  512. service_unlink_and_exit_data->name = strdup (service_name);
  513. service_unlink_and_exit_data->ver = service_ver;
  514. api->schedwrk_create (
  515. &service_unlink_and_exit_data->handle,
  516. (schedwrk_cast)service_unlink_and_exit_schedwrk_handler,
  517. service_unlink_and_exit_data);
  518. return (0);
  519. }