service.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. #include <corosync/coroipcs.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 hdb_handle_t swrk_service_exit_handle;
  111. static hdb_handle_t swrk_service_unlink_handle;
  112. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  113. {
  114. hdb_handle_t object_service_handle;
  115. hdb_handle_t object_find_handle;
  116. char *value;
  117. /*
  118. * Don't link default services if they have been disabled
  119. */
  120. corosync_api->object_find_create (
  121. OBJECT_PARENT_HANDLE,
  122. "aisexec",
  123. strlen ("aisexec"),
  124. &object_find_handle);
  125. if (corosync_api->object_find_next (
  126. object_find_handle,
  127. &object_service_handle) == 0) {
  128. if ( ! corosync_api->object_key_get (object_service_handle,
  129. "defaultservices",
  130. strlen ("defaultservices"),
  131. (void *)&value,
  132. NULL)) {
  133. if (value && strcmp (value, "no") == 0) {
  134. return 0;
  135. }
  136. }
  137. }
  138. corosync_api->object_find_destroy (object_find_handle);
  139. return (-1);
  140. }
  141. unsigned int corosync_service_link_and_init (
  142. struct corosync_api_v1 *corosync_api,
  143. const char *service_name,
  144. unsigned int service_ver)
  145. {
  146. struct corosync_service_engine_iface_ver0 *iface_ver0;
  147. void *iface_ver0_p;
  148. hdb_handle_t handle;
  149. struct corosync_service_engine *service;
  150. int res;
  151. hdb_handle_t object_service_handle;
  152. hdb_handle_t object_stats_handle;
  153. int fn;
  154. char object_name[32];
  155. char *name_sufix;
  156. uint64_t zero_64 = 0;
  157. /*
  158. * reference the service interface
  159. */
  160. iface_ver0_p = NULL;
  161. res = lcr_ifact_reference (
  162. &handle,
  163. service_name,
  164. service_ver,
  165. &iface_ver0_p,
  166. (void *)0);
  167. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  168. if (res == -1 || iface_ver0 == 0) {
  169. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  170. return (-1);
  171. }
  172. /*
  173. * Initialize service
  174. */
  175. service = iface_ver0->corosync_get_service_engine_ver0();
  176. ais_service[service->id] = service;
  177. if (service->config_init_fn) {
  178. res = service->config_init_fn (corosync_api);
  179. }
  180. if (service->exec_init_fn) {
  181. res = service->exec_init_fn (corosync_api);
  182. }
  183. /*
  184. * Store service in object database
  185. */
  186. corosync_api->object_create (object_internal_configuration_handle,
  187. &object_service_handle,
  188. "service",
  189. strlen ("service"));
  190. corosync_api->object_key_create_typed (object_service_handle,
  191. "name",
  192. service_name,
  193. strlen (service_name) + 1, OBJDB_VALUETYPE_STRING);
  194. corosync_api->object_key_create_typed (object_service_handle,
  195. "ver",
  196. &service_ver,
  197. sizeof (service_ver), OBJDB_VALUETYPE_UINT32);
  198. res = corosync_api->object_key_create_typed (object_service_handle,
  199. "handle",
  200. &handle,
  201. sizeof (handle), OBJDB_VALUETYPE_UINT64);
  202. corosync_api->object_key_create_typed (object_service_handle,
  203. "service_id",
  204. &service->id,
  205. sizeof (service->id), OBJDB_VALUETYPE_UINT16);
  206. name_sufix = strrchr (service_name, '_');
  207. if (name_sufix)
  208. name_sufix++;
  209. else
  210. name_sufix = (char*)service_name;
  211. corosync_api->object_create (object_stats_services_handle,
  212. &object_stats_handle,
  213. name_sufix, strlen (name_sufix));
  214. corosync_api->object_key_create_typed (object_stats_handle,
  215. "service_id",
  216. &service->id, sizeof (service->id),
  217. OBJDB_VALUETYPE_INT16);
  218. for (fn = 0; fn < service->exec_engine_count; fn++) {
  219. snprintf (object_name, 32, "%d", fn);
  220. corosync_api->object_create (object_stats_handle,
  221. &service_stats_handle[service->id][fn],
  222. object_name, strlen (object_name));
  223. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  224. "tx",
  225. &zero_64, sizeof (zero_64),
  226. OBJDB_VALUETYPE_UINT64);
  227. corosync_api->object_key_create_typed (service_stats_handle[service->id][fn],
  228. "rx",
  229. &zero_64, sizeof (zero_64),
  230. OBJDB_VALUETYPE_UINT64);
  231. }
  232. log_printf (LOGSYS_LEVEL_NOTICE, "Service engine loaded: %s\n", service->name);
  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. lcr_ifact_release (*found_service_handle);
  395. corosync_api->object_destroy (object_service_handle);
  396. }
  397. }
  398. corosync_api->object_find_destroy (object_find_handle);
  399. return (0);
  400. }
  401. /*
  402. * Links default services into the executive
  403. */
  404. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  405. {
  406. unsigned int i;
  407. hdb_handle_t object_service_handle;
  408. char *found_service_name;
  409. char *found_service_ver;
  410. unsigned int found_service_ver_atoi;
  411. hdb_handle_t object_find_handle;
  412. hdb_handle_t object_find2_handle;
  413. hdb_handle_t object_runtime_handle;
  414. corosync_api->object_find_create (
  415. OBJECT_PARENT_HANDLE,
  416. "runtime",
  417. strlen ("runtime"),
  418. &object_find2_handle);
  419. if (corosync_api->object_find_next (
  420. object_find2_handle,
  421. &object_runtime_handle) == 0) {
  422. corosync_api->object_create (object_runtime_handle,
  423. &object_stats_services_handle,
  424. "services", strlen ("services"));
  425. }
  426. corosync_api->object_create (OBJECT_PARENT_HANDLE,
  427. &object_internal_configuration_handle,
  428. "internal_configuration",
  429. strlen ("internal_configuration"));
  430. corosync_api->object_find_create (
  431. OBJECT_PARENT_HANDLE,
  432. "service",
  433. strlen ("service"),
  434. &object_find_handle);
  435. while (corosync_api->object_find_next (
  436. object_find_handle,
  437. &object_service_handle) == 0) {
  438. corosync_api->object_key_get (object_service_handle,
  439. "name",
  440. strlen ("name"),
  441. (void *)&found_service_name,
  442. NULL);
  443. found_service_ver = NULL;
  444. corosync_api->object_key_get (object_service_handle,
  445. "ver",
  446. strlen ("ver"),
  447. (void *)&found_service_ver,
  448. NULL);
  449. found_service_ver_atoi = (found_service_ver ? atoi (found_service_ver) : 0);
  450. corosync_service_link_and_init (
  451. corosync_api,
  452. found_service_name,
  453. found_service_ver_atoi);
  454. }
  455. corosync_api->object_find_destroy (object_find_handle);
  456. if (default_services_requested (corosync_api) == 0) {
  457. return (0);
  458. }
  459. for (i = 0;
  460. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  461. corosync_service_link_and_init (
  462. corosync_api,
  463. default_services[i].name,
  464. default_services[i].ver);
  465. }
  466. return (0);
  467. }
  468. /*
  469. * Declaration of exit_schedwrk_handler, because of cycle
  470. * (service_exit_schedwrk_handler calls service_unlink_schedwrk_handler, and vice-versa)
  471. */
  472. static int service_exit_schedwrk_handler (const void *data);
  473. static int service_unlink_schedwrk_handler (const void *data) {
  474. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  475. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  476. /*
  477. * Exit all ipc connections dependent on this service
  478. */
  479. if (coroipcs_ipc_service_exit (cb_data->service_engine) == -1)
  480. return -1;
  481. log_printf(LOGSYS_LEVEL_NOTICE,
  482. "Service engine unloaded: %s\n",
  483. ais_service[cb_data->service_engine]->name);
  484. ais_service[cb_data->service_engine] = NULL;
  485. lcr_ifact_release (cb_data->service_handle);
  486. api->schedwrk_create (
  487. &swrk_service_exit_handle,
  488. &service_exit_schedwrk_handler,
  489. data);
  490. return 0;
  491. }
  492. static int service_exit_schedwrk_handler (const void *data) {
  493. int res;
  494. static int current_priority = 0;
  495. static int current_service_engine = 0;
  496. static int called = 0;
  497. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  498. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  499. hdb_handle_t service_handle;
  500. if (called == 0) {
  501. log_printf(LOGSYS_LEVEL_NOTICE,
  502. "Unloading all Corosync service engines.\n");
  503. current_priority = service_priority_max ();
  504. called = 1;
  505. }
  506. res = corosync_service_unlink_priority (
  507. api,
  508. 0,
  509. &current_priority,
  510. &current_service_engine,
  511. &service_handle);
  512. if (res == 0) {
  513. service_unlink_all_complete();
  514. return (res);
  515. }
  516. if (res == 1) {
  517. cb_data->service_engine = current_service_engine;
  518. cb_data->service_handle = service_handle;
  519. api->schedwrk_create_nolock (
  520. &swrk_service_unlink_handle,
  521. &service_unlink_schedwrk_handler,
  522. data);
  523. return (0);
  524. }
  525. return (res);
  526. }
  527. void corosync_service_unlink_all (
  528. struct corosync_api_v1 *api,
  529. void (*unlink_all_complete) (void))
  530. {
  531. static int called = 0;
  532. static struct seus_handler_data cb_data;
  533. assert (api);
  534. service_unlink_all_complete = unlink_all_complete;
  535. if (called) {
  536. return;
  537. }
  538. if (called == 0) {
  539. called = 1;
  540. }
  541. cb_data.api = api;
  542. api->schedwrk_create (
  543. &swrk_service_exit_handle,
  544. &service_exit_schedwrk_handler,
  545. &cb_data);
  546. }
  547. struct service_unlink_and_exit_data {
  548. hdb_handle_t handle;
  549. struct corosync_api_v1 *api;
  550. const char *name;
  551. unsigned int ver;
  552. };
  553. static int service_unlink_and_exit_schedwrk_handler (void *data)
  554. {
  555. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  556. data;
  557. int res;
  558. res = service_unlink_and_exit (
  559. service_unlink_and_exit_data->api,
  560. service_unlink_and_exit_data->name,
  561. service_unlink_and_exit_data->ver);
  562. if (res == 0) {
  563. free (service_unlink_and_exit_data);
  564. }
  565. return (res);
  566. }
  567. typedef int (*schedwrk_cast) (const void *);
  568. unsigned int corosync_service_unlink_and_exit (
  569. struct corosync_api_v1 *api,
  570. const char *service_name,
  571. unsigned int service_ver)
  572. {
  573. struct service_unlink_and_exit_data *service_unlink_and_exit_data;
  574. assert (api);
  575. service_unlink_and_exit_data = malloc (sizeof (struct service_unlink_and_exit_data));
  576. service_unlink_and_exit_data->api = api;
  577. service_unlink_and_exit_data->name = strdup (service_name);
  578. service_unlink_and_exit_data->ver = service_ver;
  579. api->schedwrk_create (
  580. &service_unlink_and_exit_data->handle,
  581. (schedwrk_cast)service_unlink_and_exit_schedwrk_handler,
  582. service_unlink_and_exit_data);
  583. return (0);
  584. }