service.c 14 KB

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