service.c 16 KB

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