service.c 14 KB

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