service.c 12 KB

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