service.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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 <corosync/engine/icmap.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 <qb/qbipcs.h>
  53. #include <qb/qbloop.h>
  54. LOGSYS_DECLARE_SUBSYS ("SERV");
  55. struct default_service {
  56. const char *name;
  57. int ver;
  58. };
  59. static struct default_service default_services[] = {
  60. {
  61. .name = "corosync_evs",
  62. .ver = 0,
  63. },
  64. {
  65. .name = "corosync_cfg",
  66. .ver = 0,
  67. },
  68. {
  69. .name = "corosync_cpg",
  70. .ver = 0,
  71. },
  72. {
  73. .name = "corosync_pload",
  74. .ver = 0,
  75. },
  76. #ifdef HAVE_MONITORING
  77. {
  78. .name = "corosync_mon",
  79. .ver = 0,
  80. },
  81. #endif
  82. #ifdef HAVE_WATCHDOG
  83. {
  84. .name = "corosync_wd",
  85. .ver = 0,
  86. },
  87. #endif
  88. {
  89. .name = "corosync_quorum",
  90. .ver = 0,
  91. },
  92. {
  93. .name = "corosync_cmap",
  94. .ver = 0,
  95. },
  96. };
  97. /*
  98. * service exit and unlink schedwrk handler data structure
  99. */
  100. struct seus_handler_data {
  101. hdb_handle_t service_handle;
  102. int service_engine;
  103. struct corosync_api_v1 *api;
  104. };
  105. struct corosync_service_engine *ais_service[SERVICE_HANDLER_MAXIMUM_COUNT];
  106. const char *service_stats_rx[SERVICE_HANDLER_MAXIMUM_COUNT][64];
  107. const char *service_stats_tx[SERVICE_HANDLER_MAXIMUM_COUNT][64];
  108. int ais_service_exiting[SERVICE_HANDLER_MAXIMUM_COUNT];
  109. static void (*service_unlink_all_complete) (void) = NULL;
  110. static unsigned int default_services_requested (struct corosync_api_v1 *corosync_api)
  111. {
  112. char *value = NULL;
  113. int res;
  114. /*
  115. * Don't link default services if they have been disabled
  116. */
  117. if (icmap_get_string("aisexec.defaultservices", &value) == CS_OK &&
  118. value != NULL && strcmp(value, "no") == 0) {
  119. res = 0;
  120. } else {
  121. res = -1;
  122. }
  123. free(value);
  124. return (res);
  125. }
  126. unsigned int corosync_service_link_and_init (
  127. struct corosync_api_v1 *corosync_api,
  128. const char *service_name,
  129. unsigned int service_ver)
  130. {
  131. struct corosync_service_engine_iface_ver0 *iface_ver0;
  132. void *iface_ver0_p;
  133. hdb_handle_t handle;
  134. struct corosync_service_engine *service;
  135. int res;
  136. int fn;
  137. char *name_sufix;
  138. void* _start;
  139. void* _stop;
  140. char key_name[ICMAP_KEYNAME_MAXLEN];
  141. /*
  142. * reference the service interface
  143. */
  144. iface_ver0_p = NULL;
  145. res = lcr_ifact_reference (
  146. &handle,
  147. service_name,
  148. service_ver,
  149. &iface_ver0_p,
  150. (void *)0);
  151. iface_ver0 = (struct corosync_service_engine_iface_ver0 *)iface_ver0_p;
  152. if (res == -1 || iface_ver0 == 0) {
  153. log_printf(LOGSYS_LEVEL_ERROR, "Service failed to load '%s'.\n", service_name);
  154. return (-1);
  155. }
  156. /*
  157. * Initialize service
  158. */
  159. service = iface_ver0->corosync_get_service_engine_ver0();
  160. ais_service[service->id] = service;
  161. /*
  162. * Register the log sites with libqb
  163. */
  164. _start = lcr_ifact_addr_get(handle, "__start___verbose");
  165. _stop = lcr_ifact_addr_get(handle, "__stop___verbose");
  166. qb_log_callsites_register(_start, _stop);
  167. if (service->config_init_fn) {
  168. res = service->config_init_fn (corosync_api);
  169. }
  170. if (service->exec_init_fn) {
  171. res = service->exec_init_fn (corosync_api);
  172. }
  173. /*
  174. * Store service in cmap db
  175. */
  176. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.name", service->id);
  177. icmap_set_string(key_name, service_name);
  178. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.ver", service->id);
  179. icmap_set_uint32(key_name, service_ver);
  180. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.handle", service->id);
  181. icmap_set_uint64(key_name, handle);
  182. name_sufix = strrchr (service_name, '_');
  183. if (name_sufix)
  184. name_sufix++;
  185. else
  186. name_sufix = (char*)service_name;
  187. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.services.%s.service_id", name_sufix);
  188. icmap_set_uint16(key_name, service->id);
  189. for (fn = 0; fn < service->exec_engine_count; fn++) {
  190. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.services.%s.%d.tx", name_sufix, fn);
  191. icmap_set_uint64(key_name, 0);
  192. service_stats_tx[service->id][fn] = strdup(key_name);
  193. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "runtime.services.%s.%d.rx", name_sufix, fn);
  194. icmap_set_uint64(key_name, 0);
  195. service_stats_rx[service->id][fn] = strdup(key_name);
  196. }
  197. log_printf (LOGSYS_LEVEL_NOTICE,
  198. "Service engine loaded: %s [%d]\n", service->name, service->id);
  199. cs_ipcs_service_init(service);
  200. return (res);
  201. }
  202. static int service_priority_max(void)
  203. {
  204. int lpc = 0, max = 0;
  205. for(; lpc < SERVICE_HANDLER_MAXIMUM_COUNT; lpc++) {
  206. if(ais_service[lpc] != NULL && ais_service[lpc]->priority > max) {
  207. max = ais_service[lpc]->priority;
  208. }
  209. }
  210. return max;
  211. }
  212. /*
  213. * use the force
  214. */
  215. static unsigned int
  216. corosync_service_unlink_priority (
  217. struct corosync_api_v1 *corosync_api,
  218. int lowest_priority,
  219. int *current_priority,
  220. int *current_service_engine,
  221. hdb_handle_t *current_service_handle)
  222. {
  223. unsigned short service_id;
  224. hdb_handle_t found_service_handle;
  225. char key_name[ICMAP_KEYNAME_MAXLEN];
  226. int res;
  227. for(; *current_priority >= lowest_priority; *current_priority = *current_priority - 1) {
  228. for(*current_service_engine = 0;
  229. *current_service_engine < SERVICE_HANDLER_MAXIMUM_COUNT;
  230. *current_service_engine = *current_service_engine + 1) {
  231. if(ais_service[*current_service_engine] == NULL ||
  232. ais_service[*current_service_engine]->priority != *current_priority) {
  233. continue;
  234. }
  235. /*
  236. * find service handle and unload it if possible.
  237. *
  238. * If the service engine's exec_exit_fn returns -1 indicating
  239. * it was busy, this function returns -1 and can be called again
  240. * at a later time (usually via the schedwrk api).
  241. */
  242. snprintf(key_name, ICMAP_KEYNAME_MAXLEN,
  243. "internal_configuration.service.%u.handle",
  244. ais_service[*current_service_engine]->id);
  245. if (icmap_get_uint64(key_name, &found_service_handle) == CS_OK) {
  246. service_id = ais_service[*current_service_engine]->id;
  247. if (ais_service[service_id]->exec_exit_fn) {
  248. res = ais_service[service_id]->exec_exit_fn ();
  249. if (res == -1) {
  250. return (-1);
  251. }
  252. }
  253. *current_service_handle = found_service_handle;
  254. ais_service_exiting[*current_service_engine] = 1;
  255. /*
  256. * Call should call this function again
  257. */
  258. return (1);
  259. }
  260. }
  261. }
  262. /*
  263. * We finish unlink of all services -> no need to call this function again
  264. */
  265. return (0);
  266. }
  267. static unsigned int service_unlink_and_exit (
  268. struct corosync_api_v1 *corosync_api,
  269. const char *service_name,
  270. unsigned int service_ver)
  271. {
  272. unsigned short service_id;
  273. hdb_handle_t found_service_handle;
  274. char *name_sufix;
  275. int res;
  276. const char *iter_key_name;
  277. icmap_iter_t iter;
  278. char key_name[ICMAP_KEYNAME_MAXLEN];
  279. unsigned int found_service_ver;
  280. char *found_service_name;
  281. int service_found;
  282. name_sufix = strrchr (service_name, '_');
  283. if (name_sufix)
  284. name_sufix++;
  285. else
  286. name_sufix = (char*)service_name;
  287. service_found = 0;
  288. found_service_name = NULL;
  289. iter = icmap_iter_init("internal_configuration.service.");
  290. while ((iter_key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  291. res = sscanf(iter_key_name, "internal_configuration.service.%hu.%s", &service_id, key_name);
  292. if (res != 2) {
  293. continue;
  294. }
  295. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%hu.name", service_id);
  296. free(found_service_name);
  297. if (icmap_get_string(key_name, &found_service_name) != CS_OK) {
  298. continue;
  299. }
  300. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.ver", service_id);
  301. if (icmap_get_uint32(key_name, &found_service_ver) != CS_OK) {
  302. continue;
  303. }
  304. if (service_ver == found_service_ver && strcmp(found_service_name, service_name) == 0) {
  305. free(found_service_name);
  306. service_found = 1;
  307. break;
  308. }
  309. }
  310. icmap_iter_finalize(iter);
  311. if (service_found && service_id < SERVICE_HANDLER_MAXIMUM_COUNT
  312. && ais_service[service_id] != NULL) {
  313. if (ais_service[service_id]->exec_exit_fn) {
  314. res = ais_service[service_id]->exec_exit_fn ();
  315. if (res == -1) {
  316. return (-1);
  317. }
  318. }
  319. log_printf(LOGSYS_LEVEL_NOTICE,
  320. "Service engine unloaded: %s\n",
  321. ais_service[service_id]->name);
  322. ais_service[service_id] = NULL;
  323. cs_ipcs_service_destroy (service_id);
  324. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.handle", service_id);
  325. if (icmap_get_uint64(key_name, &found_service_handle) == CS_OK) {
  326. lcr_ifact_release (found_service_handle);
  327. }
  328. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.handle", service_id);
  329. icmap_delete(key_name);
  330. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.name", service_id);
  331. icmap_delete(key_name);
  332. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "internal_configuration.service.%u.ver", service_id);
  333. icmap_delete(key_name);
  334. }
  335. return (0);
  336. }
  337. /*
  338. * Links default services into the executive
  339. */
  340. unsigned int corosync_service_defaults_link_and_init (struct corosync_api_v1 *corosync_api)
  341. {
  342. unsigned int i;
  343. icmap_iter_t iter;
  344. char *found_service_name;
  345. int res;
  346. unsigned int found_service_ver;
  347. const char *iter_key_name;
  348. unsigned int service_pos;
  349. char key_name[ICMAP_KEYNAME_MAXLEN];
  350. icmap_set_ro_access("internal_configuration.", 1, 1);
  351. icmap_set_ro_access("runtime.services.", 1, 1);
  352. found_service_name = NULL;
  353. iter = icmap_iter_init("service.");
  354. while ((iter_key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  355. res = sscanf(iter_key_name, "service.%u.%s", &service_pos, key_name);
  356. if (res != 2) {
  357. continue;
  358. }
  359. if (strcmp(key_name, "name") != 0) {
  360. continue;
  361. }
  362. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "service.%u.name", service_pos);
  363. free(found_service_name);
  364. if (icmap_get_string(key_name, &found_service_name) != CS_OK) {
  365. continue;
  366. }
  367. snprintf(key_name, ICMAP_KEYNAME_MAXLEN, "service.%u.ver", service_pos);
  368. if (icmap_get_uint32(key_name, &found_service_ver) != CS_OK) {
  369. continue;
  370. }
  371. corosync_service_link_and_init (
  372. corosync_api,
  373. found_service_name,
  374. found_service_ver);
  375. }
  376. icmap_iter_finalize(iter);
  377. if (default_services_requested (corosync_api) == 0) {
  378. return (0);
  379. }
  380. for (i = 0;
  381. i < sizeof (default_services) / sizeof (struct default_service); i++) {
  382. corosync_service_link_and_init (
  383. corosync_api,
  384. default_services[i].name,
  385. default_services[i].ver);
  386. }
  387. return (0);
  388. }
  389. /*
  390. * Declaration of exit_schedwrk_handler, because of cycle
  391. * (service_exit_schedwrk_handler calls service_unlink_schedwrk_handler, and vice-versa)
  392. */
  393. static void service_exit_schedwrk_handler (void *data);
  394. static void service_unlink_schedwrk_handler (void *data) {
  395. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  396. /*
  397. * Exit all ipc connections dependent on this service
  398. */
  399. if (cs_ipcs_service_destroy (cb_data->service_engine) == -1) {
  400. goto redo_this_function;
  401. }
  402. log_printf(LOGSYS_LEVEL_NOTICE,
  403. "Service engine unloaded: %s\n",
  404. ais_service[cb_data->service_engine]->name);
  405. ais_service[cb_data->service_engine] = NULL;
  406. lcr_ifact_release (cb_data->service_handle);
  407. qb_loop_job_add(cs_poll_handle_get(),
  408. QB_LOOP_HIGH,
  409. data,
  410. service_exit_schedwrk_handler);
  411. return;
  412. redo_this_function:
  413. qb_loop_job_add(cs_poll_handle_get(),
  414. QB_LOOP_HIGH,
  415. data,
  416. service_unlink_schedwrk_handler);
  417. }
  418. static void service_exit_schedwrk_handler (void *data) {
  419. int res;
  420. static int current_priority = 0;
  421. static int current_service_engine = 0;
  422. static int called = 0;
  423. struct seus_handler_data *cb_data = (struct seus_handler_data *)data;
  424. struct corosync_api_v1 *api = (struct corosync_api_v1 *)cb_data->api;
  425. hdb_handle_t service_handle;
  426. if (called == 0) {
  427. log_printf(LOGSYS_LEVEL_NOTICE,
  428. "Unloading all Corosync service engines.\n");
  429. current_priority = service_priority_max ();
  430. called = 1;
  431. }
  432. res = corosync_service_unlink_priority (
  433. api,
  434. 0,
  435. &current_priority,
  436. &current_service_engine,
  437. &service_handle);
  438. if (res == 0) {
  439. service_unlink_all_complete();
  440. return;
  441. }
  442. if (res == 1) {
  443. cb_data->service_engine = current_service_engine;
  444. cb_data->service_handle = service_handle;
  445. qb_loop_job_add(cs_poll_handle_get(),
  446. QB_LOOP_HIGH,
  447. data,
  448. service_unlink_schedwrk_handler);
  449. return;
  450. }
  451. qb_loop_job_add(cs_poll_handle_get(),
  452. QB_LOOP_HIGH,
  453. data,
  454. service_exit_schedwrk_handler);
  455. }
  456. void corosync_service_unlink_all (
  457. struct corosync_api_v1 *api,
  458. void (*unlink_all_complete) (void))
  459. {
  460. static int called = 0;
  461. static struct seus_handler_data cb_data;
  462. assert (api);
  463. service_unlink_all_complete = unlink_all_complete;
  464. if (called) {
  465. return;
  466. }
  467. if (called == 0) {
  468. called = 1;
  469. }
  470. cb_data.api = api;
  471. qb_loop_job_add(cs_poll_handle_get(),
  472. QB_LOOP_HIGH,
  473. &cb_data,
  474. service_exit_schedwrk_handler);
  475. }
  476. struct service_unlink_and_exit_data {
  477. hdb_handle_t handle;
  478. struct corosync_api_v1 *api;
  479. const char *name;
  480. unsigned int ver;
  481. };
  482. static void service_unlink_and_exit_schedwrk_handler (void *data)
  483. {
  484. struct service_unlink_and_exit_data *service_unlink_and_exit_data =
  485. data;
  486. int res;
  487. res = service_unlink_and_exit (
  488. service_unlink_and_exit_data->api,
  489. service_unlink_and_exit_data->name,
  490. service_unlink_and_exit_data->ver);
  491. if (res == 0) {
  492. free (service_unlink_and_exit_data);
  493. } else {
  494. qb_loop_job_add(cs_poll_handle_get(),
  495. QB_LOOP_HIGH,
  496. data,
  497. service_unlink_and_exit_schedwrk_handler);
  498. }
  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. qb_loop_job_add(cs_poll_handle_get(),
  513. QB_LOOP_HIGH,
  514. service_unlink_and_exit_data,
  515. service_unlink_and_exit_schedwrk_handler);
  516. return (0);
  517. }