service.c 15 KB

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