service.c 13 KB

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