service.c 14 KB

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