4
0

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