service.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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 "mainconfig.h"
  43. #include "util.h"
  44. #include <corosync/engine/logsys.h>
  45. #include "timer.h"
  46. #include <corosync/totem/totempg.h>
  47. #include <corosync/totem/totemip.h>
  48. #include "main.h"
  49. #include <corosync/engine/coroapi.h>
  50. #include "service.h"
  51. #include <qb/qbipcs.h>
  52. #include <qb/qbloop.h>
  53. LOGSYS_DECLARE_SUBSYS ("SERV");
  54. struct default_service {
  55. const char *name;
  56. int ver;
  57. };
  58. static struct default_service default_services[] = {
  59. {
  60. .name = "corosync_evs",
  61. .ver = 0,
  62. },
  63. {
  64. .name = "corosync_cfg",
  65. .ver = 0,
  66. },
  67. {
  68. .name = "corosync_cpg",
  69. .ver = 0,
  70. },
  71. {
  72. .name = "corosync_confdb",
  73. .ver = 0,
  74. },
  75. {
  76. .name = "corosync_pload",
  77. .ver = 0,
  78. },
  79. #ifdef HAVE_MONITORING
  80. {
  81. .name = "corosync_mon",
  82. .ver = 0,
  83. },
  84. #endif
  85. #ifdef HAVE_WATCHDOG
  86. {
  87. .name = "corosync_wd",
  88. .ver = 0,
  89. },
  90. #endif
  91. {
  92. .name = "corosync_quorum",
  93. .ver = 0,
  94. }
  95. };
  96. /*
  97. * service exit and unlink schedwrk handler data structure
  98. */
  99. struct seus_handler_data {
  100. hdb_handle_t service_handle;
  101. int service_engine;
  102. struct corosync_api_v1 *api;
  103. };
  104. struct corosync_service_engine *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
  105. hdb_handle_t service_stats_handle[SERVICE_HANDLER_MAXIMUM_COUNT][64];
  106. int ais_service_exiting[SERVICE_HANDLER_MAXIMUM_COUNT];
  107. static hdb_handle_t object_internal_configuration_handle;
  108. static hdb_handle_t object_stats_services_handle;
  109. static void (*service_unlink_all_complete) (void) = NULL;
  110. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  111. {
  112. hdb_handle_t object_service_handle;
  113. hdb_handle_t object_find_handle;
  114. char *value;
  115. /*
  116. * Don't link default services if they have been disabled
  117. */
  118. corosync_api->object_find_create (
  119. OBJECT_PARENT_HANDLE,
  120. "aisexec",
  121. strlen ("aisexec"),
  122. &object_find_handle);
  123. if (corosync_api->object_find_next (
  124. object_find_handle,
  125. &object_service_handle) == 0) {
  126. if ( ! corosync_api->object_key_get (object_service_handle,
  127. "defaultservices",
  128. strlen ("defaultservices"),
  129. (void *)&value,
  130. NULL)) {
  131. if (value && strcmp (value, "no") == 0) {
  132. return 0;
  133. }
  134. }
  135. }
  136. corosync_api->object_find_destroy (object_find_handle);
  137. return (-1);
  138. }
  139. unsigned int corosync_service_link_and_init (
  140. struct corosync_api_v1 *corosync_api,
  141. const char *service_name,
  142. unsigned int service_ver)
  143. {
  144. struct corosync_service_engine_iface_ver0 *iface_ver0;
  145. void *iface_ver0_p;
  146. hdb_handle_t handle;
  147. struct corosync_service_engine *service;
  148. int res;
  149. hdb_handle_t object_service_handle;
  150. hdb_handle_t object_stats_handle;
  151. int fn;
  152. char object_name[32];
  153. char *name_sufix;
  154. uint64_t zero_64 = 0;
  155. /*
  156. * reference the service interface
  157. */
  158. iface_ver0_p = NULL;
  159. res = lcr_ifact_reference (
  160. &handle,
  161. service_name,
  162. service_ver,
  163. &iface_ver0_p,
  164. (void *)0);
  165. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  166. if (res == -1 || iface_ver0 == 0) {
  167. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  168. return (-1);
  169. }
  170. /*
  171. * Initialize service
  172. */
  173. service = iface_ver0->corosync_get_service_engine_ver0();
  174. ais_service[service->id] = service;
  175. if (service->config_init_fn) {
  176. res = service->config_init_fn (corosync_api);
  177. }
  178. if (service->exec_init_fn) {
  179. res = service->exec_init_fn (corosync_api);
  180. }
  181. /*
  182. * Store service in object database
  183. */
  184. corosync_api->object_create (object_internal_configuration_handle,
  185. &object_service_handle,
  186. "service",
  187. strlen ("service"));
  188. corosync_api->object_key_create_typed (object_service_handle,
  189. "name",
  190. service_name,
  191. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  192. corosync_api->object_key_create_typed (object_service_handle,
  193. "ver",
  194. &service_ver,
  195. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  196. res = corosync_api->object_key_create_typed (object_service_handle,
  197. "handle",
  198. &handle,
  199. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  200. corosync_api->object_key_create_typed (object_service_handle,
  201. "service_id",
  202. &service->id,
  203. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  204. name_sufix = strrchr (service_name, '_');
  205. if (name_sufix)
  206. name_sufix++;
  207. else
  208. name_sufix = (char*)service_name;
  209. corosync_api->object_create (object_stats_services_handle,
  210. &object_stats_handle,
  211. name_sufix, strlen (name_sufix));
  212. corosync_api->object_key_create_typed (object_stats_handle,
  213. "service_id",
  214. &service->id, sizeof (service->id),
  215. OBJDB_VALUETYPE_INT16);
  216. for (fn = 0; fn < service->exec_engine_count; fn++) {
  217. snprintf (object_name, 32, "%d", fn);
  218. corosync_api->object_create (object_stats_handle,
  219. &service_stats_handle[service->id][fn],
  220. object_name, strlen (object_name));
  221. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  222. "tx",
  223. &zero_64, sizeof (zero_64),
  224. OBJDB_VALUETYPE_UINT64);
  225. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  226. "rx",
  227. &zero_64, sizeof (zero_64),
  228. OBJDB_VALUETYPE_UINT64);
  229. }
  230. log_printf (LOGSYS_LEVEL_NOTICE,
  231. "Service engine loaded: %s [%d]\n", service->name, service->id);
  232. cs_ipcs_service_init(service);
  233. return (res);
  234. }
  235. static int service_priority_max(void)
  236. {
  237. int lpc = 0, max = 0;
  238. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  239. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  240. max = ais_service[lpc]->priority;
  241. }
  242. }
  243. return max;
  244. }
  245. /*
  246. * use the force
  247. */
  248. static unsigned int
  249. corosync_service_unlink_priority (
  250. struct corosync_api_v1 *corosync_api,
  251. int lowest_priority,
  252. int *current_priority,
  253. int *current_service_engine,
  254. hdb_handle_t *current_service_handle)
  255. {
  256. unsigned short *service_id;
  257. hdb_handle_t object_service_handle;
  258. hdb_handle_t object_find_handle;
  259. hdb_handle_t *found_service_handle;
  260. for(; *current_priority >= lowest_priority; *current_priority = *current_priority - 1) {
  261. for(*current_service_engine = 0;
  262. *current_service_engine < SERVICE_HANDLER_MAXIMUM_COUNT;
  263. *current_service_engine = *current_service_engine + 1) {
  264. if(ais_service[*current_service_engine] == NULL ||
  265. ais_service[*current_service_engine]->priority != *current_priority) {
  266. continue;
  267. }
  268. /*
  269. * find service object in object database by service id
  270. * and unload it if possible.
  271. *
  272. * If the service engine's exec_exit_fn returns -1 indicating
  273. * it was busy, this function returns -1 and can be called again
  274. * at a later time (usually via the schedwrk api).
  275. */
  276. corosync_api->object_find_create (
  277. object_internal_configuration_handle,
  278. "service", strlen ("service"), &object_find_handle);
  279. while (corosync_api->object_find_next (
  280. object_find_handle, &object_service_handle) == 0) {
  281. int res = corosync_api->object_key_get (
  282. object_service_handle,
  283. "service_id", strlen ("service_id"),
  284. (void *)&service_id, NULL);
  285. if (res == 0 && *service_id ==
  286. ais_service[*current_service_engine]->id) {
  287. if (ais_service[*service_id]->exec_exit_fn) {
  288. res = ais_service[*service_id]->exec_exit_fn ();
  289. if (res == -1) {
  290. corosync_api->object_find_destroy (object_find_handle);
  291. return (-1);
  292. }
  293. }
  294. res = corosync_api->object_key_get (
  295. object_service_handle,
  296. "handle", strlen ("handle"),
  297. (void *)&found_service_handle,
  298. NULL);
  299. *current_service_handle = *found_service_handle;
  300. ais_service_exiting[*current_service_engine] = 1;
  301. corosync_api->object_find_destroy (object_find_handle);
  302. /*
  303. * Call should call this function again
  304. */
  305. return (1);
  306. }
  307. }
  308. corosync_api->object_find_destroy (object_find_handle);
  309. }
  310. }
  311. /*
  312. * We finish unlink of all services -> no need to call this function again
  313. */
  314. return (0);
  315. }
  316. static unsigned int service_unlink_and_exit (
  317. struct corosync_api_v1 *corosync_api,
  318. const char *service_name,
  319. unsigned int service_ver)
  320. {
  321. hdb_handle_t object_service_handle;
  322. char *found_service_name;
  323. unsigned short *service_id;
  324. unsigned int *found_service_ver;
  325. hdb_handle_t object_find_handle;
  326. hdb_handle_t *found_service_handle;
  327. char *name_sufix;
  328. int res;
  329. name_sufix = strrchr (service_name, '_');
  330. if (name_sufix)
  331. name_sufix++;
  332. else
  333. name_sufix = (char*)service_name;
  334. corosync_api->object_find_create (
  335. object_stats_services_handle,
  336. name_sufix, strlen (name_sufix),
  337. &object_find_handle);
  338. if (corosync_api->object_find_next (
  339. object_find_handle,
  340. &object_service_handle) == 0) {
  341. corosync_api->object_destroy (object_service_handle);
  342. }
  343. corosync_api->object_find_destroy (object_find_handle);
  344. corosync_api->object_find_create (
  345. object_internal_configuration_handle,
  346. "service",
  347. strlen ("service"),
  348. &object_find_handle);
  349. while (corosync_api->object_find_next (
  350. object_find_handle,
  351. &object_service_handle) == 0) {
  352. corosync_api->object_key_get (object_service_handle,
  353. "name",
  354. strlen ("name"),
  355. (void *)&found_service_name,
  356. NULL);
  357. if (strcmp (service_name, found_service_name) != 0) {
  358. continue;
  359. }
  360. corosync_api->object_key_get (object_service_handle,
  361. "ver",
  362. strlen ("ver"),
  363. (void *)&found_service_ver,
  364. NULL);
  365. /*
  366. * If service found and linked exit it
  367. */
  368. if (service_ver != *found_service_ver) {
  369. continue;
  370. }
  371. corosync_api->object_key_get (
  372. object_service_handle,
  373. "service_id", strlen ("service_id"),
  374. (void *)&service_id, NULL);
  375. if(service_id != NULL
  376. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  377. && ais_service[*service_id] != NULL) {
  378. corosync_api->object_find_destroy (object_find_handle);
  379. if (ais_service[*service_id]->exec_exit_fn) {
  380. res = ais_service[*service_id]->exec_exit_fn ();
  381. if (res == -1) {
  382. return (-1);
  383. }
  384. }
  385. log_printf(LOGSYS_LEVEL_NOTICE,
  386. "Service engine unloaded: %s\n",
  387. ais_service[*service_id]->name);
  388. ais_service[*service_id] = NULL;
  389. res = corosync_api->object_key_get (
  390. object_service_handle,
  391. "handle", strlen ("handle"),
  392. (void *)&found_service_handle,
  393. NULL);
  394. cs_ipcs_service_destroy (*service_id);
  395. lcr_ifact_release (*found_service_handle);
  396. corosync_api->object_destroy (object_service_handle);
  397. }
  398. }
  399. corosync_api->object_find_destroy (object_find_handle);
  400. return (0);
  401. }
  402. /*
  403. * Links default services into the executive
  404. */
  405. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  406. {
  407. unsigned int i;
  408. hdb_handle_t object_service_handle;
  409. char *found_service_name;
  410. char *found_service_ver;
  411. unsigned int found_service_ver_atoi;
  412. hdb_handle_t object_find_handle;
  413. hdb_handle_t object_find2_handle;
  414. hdb_handle_t object_runtime_handle;
  415. corosync_api->object_find_create (
  416. OBJECT_PARENT_HANDLE,
  417. "runtime",
  418. strlen ("runtime"),
  419. &object_find2_handle);
  420. if (corosync_api->object_find_next (
  421. object_find2_handle,
  422. &object_runtime_handle) == 0) {
  423. corosync_api->object_create (object_runtime_handle,
  424. &object_stats_services_handle,
  425. "services", strlen ("services"));
  426. }
  427. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  428. &object_internal_configuration_handle,
  429. "internal_configuration",
  430. strlen ("internal_configuration"));
  431. corosync_api->object_find_create (
  432. OBJECT_PARENT_HANDLE,
  433. "service",
  434. strlen ("service"),
  435. &object_find_handle);
  436. while (corosync_api->object_find_next (
  437. object_find_handle,
  438. &object_service_handle) == 0) {
  439. corosync_api->object_key_get (object_service_handle,
  440. "name",
  441. strlen ("name"),
  442. (void *)&found_service_name,
  443. NULL);
  444. found_service_ver = NULL;
  445. corosync_api->object_key_get (object_service_handle,
  446. "ver",
  447. strlen ("ver"),
  448. (void *)&found_service_ver,
  449. NULL);
  450. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  451. corosync_service_link_and_init (
  452. corosync_api,
  453. found_service_name,
  454. found_service_ver_atoi);
  455. }
  456. corosync_api->object_find_destroy (object_find_handle);
  457. if (default_services_requested (corosync_api) == 0) {
  458. return (0);
  459. }
  460. for (i = 0;
  461. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  462. corosync_service_link_and_init (
  463. corosync_api,
  464. default_services[i].name,
  465. default_services[i].ver);
  466. }
  467. return (0);
  468. }
  469. /*
  470. * Declaration of exit_schedwrk_handler, because of cycle
  471. * (service_exit_schedwrk_handler calls service_unlink_schedwrk_handler, and vice-versa)
  472. */
  473. static void service_exit_schedwrk_handler (void *data);
  474. static void service_unlink_schedwrk_handler (void *data) {
  475. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  476. /*
  477. * Exit all ipc connections dependent on this service
  478. */
  479. if (cs_ipcs_service_destroy (cb_data->service_engine) == -1) {
  480. goto redo_this_function;
  481. }
  482. log_printf(LOGSYS_LEVEL_NOTICE,
  483. "Service engine unloaded: %s\n",
  484. ais_service[cb_data->service_engine]->name);
  485. ais_service[cb_data->service_engine] = NULL;
  486. lcr_ifact_release (cb_data->service_handle);
  487. qb_loop_job_add(cs_poll_handle_get(),
  488. QB_LOOP_HIGH,
  489. data,
  490. service_exit_schedwrk_handler);
  491. return;
  492. redo_this_function:
  493. qb_loop_job_add(cs_poll_handle_get(),
  494. QB_LOOP_HIGH,
  495. data,
  496. service_unlink_schedwrk_handler);
  497. }
  498. static void service_exit_schedwrk_handler (void *data) {
  499. int res;
  500. static int current_priority = 0;
  501. static int current_service_engine = 0;
  502. static int called = 0;
  503. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  504. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  505. hdb_handle_t service_handle;
  506. if (called == 0) {
  507. log_printf(LOGSYS_LEVEL_NOTICE,
  508. "Unloading all Corosync service engines.\n");
  509. current_priority = service_priority_max ();
  510. called = 1;
  511. }
  512. res = corosync_service_unlink_priority (
  513. api,
  514. 0,
  515. &current_priority,
  516. &current_service_engine,
  517. &service_handle);
  518. if (res == 0) {
  519. service_unlink_all_complete();
  520. return;
  521. }
  522. if (res == 1) {
  523. cb_data->service_engine = current_service_engine;
  524. cb_data->service_handle = service_handle;
  525. qb_loop_job_add(cs_poll_handle_get(),
  526. QB_LOOP_HIGH,
  527. data,
  528. service_unlink_schedwrk_handler);
  529. return;
  530. }
  531. qb_loop_job_add(cs_poll_handle_get(),
  532. QB_LOOP_HIGH,
  533. data,
  534. service_exit_schedwrk_handler);
  535. }
  536. void corosync_service_unlink_all (
  537. struct corosync_api_v1 *api,
  538. void (*unlink_all_complete) (void))
  539. {
  540. static int called = 0;
  541. static struct seus_handler_data cb_data;
  542. assert (api);
  543. service_unlink_all_complete = unlink_all_complete;
  544. if (called) {
  545. return;
  546. }
  547. if (called == 0) {
  548. called = 1;
  549. }
  550. cb_data.api = api;
  551. qb_loop_job_add(cs_poll_handle_get(),
  552. QB_LOOP_HIGH,
  553. &cb_data,
  554. service_exit_schedwrk_handler);
  555. }
  556. struct service_unlink_and_exit_data {
  557. hdb_handle_t handle;
  558. struct corosync_api_v1 *api;
  559. const char *name;
  560. unsigned int ver;
  561. };
  562. static void service_unlink_and_exit_schedwrk_handler (void *data)
  563. {
  564. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  565. data;
  566. int res;
  567. res = service_unlink_and_exit (
  568. service_unlink_and_exit_data->api,
  569. service_unlink_and_exit_data->name,
  570. service_unlink_and_exit_data->ver);
  571. if (res == 0) {
  572. free (service_unlink_and_exit_data);
  573. } else {
  574. qb_loop_job_add(cs_poll_handle_get(),
  575. QB_LOOP_HIGH,
  576. data,
  577. service_unlink_and_exit_schedwrk_handler);
  578. }
  579. }
  580. typedef int (*schedwrk_cast) (const void *);
  581. unsigned int corosync_service_unlink_and_exit (
  582. struct corosync_api_v1 *api,
  583. const char *service_name,
  584. unsigned int service_ver)
  585. {
  586. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  587. assert (api);
  588. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  589. service_unlink_and_exit_data->api = api;
  590. service_unlink_and_exit_data->name = strdup (service_name);
  591. service_unlink_and_exit_data->ver = service_ver;
  592. qb_loop_job_add(cs_poll_handle_get(),
  593. QB_LOOP_HIGH,
  594. service_unlink_and_exit_data,
  595. service_unlink_and_exit_schedwrk_handler);
  596. return (0);
  597. }