service.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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. 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. #ifdef HAVE_MONITORING
  79. {
  80. .name = "corosync_mon",
  81. .ver = 0,
  82. },
  83. #endif
  84. #ifdef HAVE_WATCHDOG
  85. {
  86. .name = "corosync_wd",
  87. .ver = 0,
  88. },
  89. #endif
  90. {
  91. .name = "corosync_quorum",
  92. .ver = 0,
  93. }
  94. };
  95. /*
  96. * service exit and unlink schedwrk handler data structure
  97. */
  98. struct seus_handler_data {
  99. hdb_handle_t service_handle;
  100. int service_engine;
  101. struct corosync_api_v1 *api;
  102. };
  103. struct corosync_service_engine *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
  104. hdb_handle_t service_stats_handle[SERVICE_HANDLER_MAXIMUM_COUNT][64];
  105. int ais_service_exiting[SERVICE_HANDLER_MAXIMUM_COUNT];
  106. static hdb_handle_t object_internal_configuration_handle;
  107. static hdb_handle_t object_stats_services_handle;
  108. static void (*service_unlink_all_complete) (void) = NULL;
  109. static hdb_handle_t swrk_service_exit_handle;
  110. static hdb_handle_t swrk_service_unlink_handle;
  111. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  112. {
  113. hdb_handle_t object_service_handle;
  114. hdb_handle_t object_find_handle;
  115. char *value;
  116. /*
  117. * Don't link default services if they have been disabled
  118. */
  119. corosync_api->object_find_create (
  120. OBJECT_PARENT_HANDLE,
  121. "aisexec",
  122. strlen ("aisexec"),
  123. &object_find_handle);
  124. if (corosync_api->object_find_next (
  125. object_find_handle,
  126. &object_service_handle) == 0) {
  127. if ( ! corosync_api->object_key_get (object_service_handle,
  128. "defaultservices",
  129. strlen ("defaultservices"),
  130. (void *)&value,
  131. NULL)) {
  132. if (value && strcmp (value, "no") == 0) {
  133. return 0;
  134. }
  135. }
  136. }
  137. corosync_api->object_find_destroy (object_find_handle);
  138. return (-1);
  139. }
  140. unsigned int corosync_service_link_and_init (
  141. struct corosync_api_v1 *corosync_api,
  142. const char *service_name,
  143. unsigned int service_ver)
  144. {
  145. struct corosync_service_engine_iface_ver0 *iface_ver0;
  146. void *iface_ver0_p;
  147. hdb_handle_t handle;
  148. struct corosync_service_engine *service;
  149. int res;
  150. hdb_handle_t object_service_handle;
  151. hdb_handle_t object_stats_handle;
  152. int fn;
  153. char object_name[32];
  154. char *name_sufix;
  155. uint64_t zero_64 = 0;
  156. /*
  157. * reference the service interface
  158. */
  159. iface_ver0_p = NULL;
  160. res = lcr_ifact_reference (
  161. &handle,
  162. service_name,
  163. service_ver,
  164. &iface_ver0_p,
  165. (void *)0);
  166. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  167. if (res == -1 || iface_ver0 == 0) {
  168. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  169. return (-1);
  170. }
  171. /*
  172. * Initialize service
  173. */
  174. service = iface_ver0->corosync_get_service_engine_ver0();
  175. ais_service[service->id] = service;
  176. if (service->config_init_fn) {
  177. res = service->config_init_fn (corosync_api);
  178. }
  179. if (service->exec_init_fn) {
  180. res = service->exec_init_fn (corosync_api);
  181. }
  182. /*
  183. * Store service in object database
  184. */
  185. corosync_api->object_create (object_internal_configuration_handle,
  186. &object_service_handle,
  187. "service",
  188. strlen ("service"));
  189. corosync_api->object_key_create_typed (object_service_handle,
  190. "name",
  191. service_name,
  192. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  193. corosync_api->object_key_create_typed (object_service_handle,
  194. "ver",
  195. &service_ver,
  196. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  197. res = corosync_api->object_key_create_typed (object_service_handle,
  198. "handle",
  199. &handle,
  200. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  201. corosync_api->object_key_create_typed (object_service_handle,
  202. "service_id",
  203. &service->id,
  204. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  205. name_sufix = strrchr (service_name, '_');
  206. if (name_sufix)
  207. name_sufix++;
  208. else
  209. name_sufix = (char*)service_name;
  210. corosync_api->object_create (object_stats_services_handle,
  211. &object_stats_handle,
  212. name_sufix, strlen (name_sufix));
  213. corosync_api->object_key_create_typed (object_stats_handle,
  214. "service_id",
  215. &service->id, sizeof (service->id),
  216. OBJDB_VALUETYPE_INT16);
  217. for (fn = 0; fn < service->exec_engine_count; fn++) {
  218. snprintf (object_name, 32, "%d", fn);
  219. corosync_api->object_create (object_stats_handle,
  220. &service_stats_handle[service->id][fn],
  221. object_name, strlen (object_name));
  222. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  223. "tx",
  224. &zero_64, sizeof (zero_64),
  225. OBJDB_VALUETYPE_UINT64);
  226. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  227. "rx",
  228. &zero_64, sizeof (zero_64),
  229. OBJDB_VALUETYPE_UINT64);
  230. }
  231. log_printf (LOGSYS_LEVEL_NOTICE,
  232. "Service engine loaded: %s [%d]\n", service->name, service->id);
  233. cs_ipcs_service_init(service);
  234. return (res);
  235. }
  236. static int service_priority_max(void)
  237. {
  238. int lpc = 0, max = 0;
  239. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  240. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  241. max = ais_service[lpc]->priority;
  242. }
  243. }
  244. return max;
  245. }
  246. /*
  247. * use the force
  248. */
  249. static unsigned int
  250. corosync_service_unlink_priority (
  251. struct corosync_api_v1 *corosync_api,
  252. int lowest_priority,
  253. int *current_priority,
  254. int *current_service_engine,
  255. hdb_handle_t *current_service_handle)
  256. {
  257. unsigned short *service_id;
  258. hdb_handle_t object_service_handle;
  259. hdb_handle_t object_find_handle;
  260. hdb_handle_t *found_service_handle;
  261. for(; *current_priority >= lowest_priority; *current_priority = *current_priority - 1) {
  262. for(*current_service_engine = 0;
  263. *current_service_engine < SERVICE_HANDLER_MAXIMUM_COUNT;
  264. *current_service_engine = *current_service_engine + 1) {
  265. if(ais_service[*current_service_engine] == NULL ||
  266. ais_service[*current_service_engine]->priority != *current_priority) {
  267. continue;
  268. }
  269. /*
  270. * find service object in object database by service id
  271. * and unload it if possible.
  272. *
  273. * If the service engine's exec_exit_fn returns -1 indicating
  274. * it was busy, this function returns -1 and can be called again
  275. * at a later time (usually via the schedwrk api).
  276. */
  277. corosync_api->object_find_create (
  278. object_internal_configuration_handle,
  279. "service", strlen ("service"), &object_find_handle);
  280. while (corosync_api->object_find_next (
  281. object_find_handle, &object_service_handle) == 0) {
  282. int res = corosync_api->object_key_get (
  283. object_service_handle,
  284. "service_id", strlen ("service_id"),
  285. (void *)&service_id, NULL);
  286. if (res == 0 && *service_id ==
  287. ais_service[*current_service_engine]->id) {
  288. if (ais_service[*service_id]->exec_exit_fn) {
  289. res = ais_service[*service_id]->exec_exit_fn ();
  290. if (res == -1) {
  291. corosync_api->object_find_destroy (object_find_handle);
  292. return (-1);
  293. }
  294. }
  295. res = corosync_api->object_key_get (
  296. object_service_handle,
  297. "handle", strlen ("handle"),
  298. (void *)&found_service_handle,
  299. NULL);
  300. *current_service_handle = *found_service_handle;
  301. ais_service_exiting[*current_service_engine] = 1;
  302. corosync_api->object_find_destroy (object_find_handle);
  303. /*
  304. * Call should call this function again
  305. */
  306. return (1);
  307. }
  308. }
  309. corosync_api->object_find_destroy (object_find_handle);
  310. }
  311. }
  312. /*
  313. * We finish unlink of all services -> no need to call this function again
  314. */
  315. return (0);
  316. }
  317. static unsigned int service_unlink_and_exit (
  318. struct corosync_api_v1 *corosync_api,
  319. const char *service_name,
  320. unsigned int service_ver)
  321. {
  322. hdb_handle_t object_service_handle;
  323. char *found_service_name;
  324. unsigned short *service_id;
  325. unsigned int *found_service_ver;
  326. hdb_handle_t object_find_handle;
  327. hdb_handle_t *found_service_handle;
  328. char *name_sufix;
  329. int res;
  330. name_sufix = strrchr (service_name, '_');
  331. if (name_sufix)
  332. name_sufix++;
  333. else
  334. name_sufix = (char*)service_name;
  335. corosync_api->object_find_create (
  336. object_stats_services_handle,
  337. name_sufix, strlen (name_sufix),
  338. &object_find_handle);
  339. if (corosync_api->object_find_next (
  340. object_find_handle,
  341. &object_service_handle) == 0) {
  342. corosync_api->object_destroy (object_service_handle);
  343. }
  344. corosync_api->object_find_destroy (object_find_handle);
  345. corosync_api->object_find_create (
  346. object_internal_configuration_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. if (strcmp (service_name, found_service_name) != 0) {
  359. continue;
  360. }
  361. corosync_api->object_key_get (object_service_handle,
  362. "ver",
  363. strlen ("ver"),
  364. (void *)&found_service_ver,
  365. NULL);
  366. /*
  367. * If service found and linked exit it
  368. */
  369. if (service_ver != *found_service_ver) {
  370. continue;
  371. }
  372. corosync_api->object_key_get (
  373. object_service_handle,
  374. "service_id", strlen ("service_id"),
  375. (void *)&service_id, NULL);
  376. if(service_id != NULL
  377. && *service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  378. && ais_service[*service_id] != NULL) {
  379. corosync_api->object_find_destroy (object_find_handle);
  380. if (ais_service[*service_id]->exec_exit_fn) {
  381. res = ais_service[*service_id]->exec_exit_fn ();
  382. if (res == -1) {
  383. return (-1);
  384. }
  385. }
  386. log_printf(LOGSYS_LEVEL_NOTICE,
  387. "Service engine unloaded: %s\n",
  388. ais_service[*service_id]->name);
  389. ais_service[*service_id] = NULL;
  390. res = corosync_api->object_key_get (
  391. object_service_handle,
  392. "handle", strlen ("handle"),
  393. (void *)&found_service_handle,
  394. NULL);
  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 int service_exit_schedwrk_handler (const void *data);
  474. static int service_unlink_schedwrk_handler (const void *data) {
  475. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  476. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  477. /*
  478. * Exit all ipc connections dependent on this service
  479. */
  480. if (cs_ipcs_service_destroy (cb_data->service_engine) == -1)
  481. return -1;
  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. api->schedwrk_create (
  488. &swrk_service_exit_handle,
  489. &service_exit_schedwrk_handler,
  490. data);
  491. return 0;
  492. }
  493. static int service_exit_schedwrk_handler (const void *data) {
  494. int res;
  495. static int current_priority = 0;
  496. static int current_service_engine = 0;
  497. static int called = 0;
  498. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  499. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  500. hdb_handle_t service_handle;
  501. if (called == 0) {
  502. log_printf(LOGSYS_LEVEL_NOTICE,
  503. "Unloading all Corosync service engines.\n");
  504. current_priority = service_priority_max ();
  505. called = 1;
  506. }
  507. res = corosync_service_unlink_priority (
  508. api,
  509. 0,
  510. &current_priority,
  511. &current_service_engine,
  512. &service_handle);
  513. if (res == 0) {
  514. service_unlink_all_complete();
  515. return (res);
  516. }
  517. if (res == 1) {
  518. cb_data->service_engine = current_service_engine;
  519. cb_data->service_handle = service_handle;
  520. api->schedwrk_create_nolock (
  521. &swrk_service_unlink_handle,
  522. &service_unlink_schedwrk_handler,
  523. data);
  524. return (0);
  525. }
  526. return (res);
  527. }
  528. void corosync_service_unlink_all (
  529. struct corosync_api_v1 *api,
  530. void (*unlink_all_complete) (void))
  531. {
  532. static int called = 0;
  533. static struct seus_handler_data cb_data;
  534. assert (api);
  535. service_unlink_all_complete = unlink_all_complete;
  536. if (called) {
  537. return;
  538. }
  539. if (called == 0) {
  540. called = 1;
  541. }
  542. cb_data.api = api;
  543. api->schedwrk_create (
  544. &swrk_service_exit_handle,
  545. &service_exit_schedwrk_handler,
  546. &cb_data);
  547. }
  548. struct service_unlink_and_exit_data {
  549. hdb_handle_t handle;
  550. struct corosync_api_v1 *api;
  551. const char *name;
  552. unsigned int ver;
  553. };
  554. static int service_unlink_and_exit_schedwrk_handler (void *data)
  555. {
  556. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  557. data;
  558. int res;
  559. res = service_unlink_and_exit (
  560. service_unlink_and_exit_data->api,
  561. service_unlink_and_exit_data->name,
  562. service_unlink_and_exit_data->ver);
  563. if (res == 0) {
  564. free (service_unlink_and_exit_data);
  565. }
  566. return (res);
  567. }
  568. typedef int (*schedwrk_cast) (const void *);
  569. unsigned int corosync_service_unlink_and_exit (
  570. struct corosync_api_v1 *api,
  571. const char *service_name,
  572. unsigned int service_ver)
  573. {
  574. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  575. assert (api);
  576. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  577. service_unlink_and_exit_data->api = api;
  578. service_unlink_and_exit_data->name = strdup (service_name);
  579. service_unlink_and_exit_data->ver = service_ver;
  580. api->schedwrk_create (
  581. &service_unlink_and_exit_data->handle,
  582. (schedwrk_cast)service_unlink_and_exit_schedwrk_handler,
  583. service_unlink_and_exit_data);
  584. return (0);
  585. }