cfg.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. /*
  2. * Copyright (c) 2005-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 <sys/types.h>
  37. #include <sys/uio.h>
  38. #include <sys/socket.h>
  39. #include <sys/un.h>
  40. #include <netinet/in.h>
  41. #include <arpa/inet.h>
  42. #include <unistd.h>
  43. #include <fcntl.h>
  44. #include <stdlib.h>
  45. #include <stdio.h>
  46. #include <limits.h>
  47. #include <errno.h>
  48. #include <signal.h>
  49. #include <string.h>
  50. #include <corosync/corotypes.h>
  51. #include <corosync/coroipc_types.h>
  52. #include <corosync/cfg.h>
  53. #include <corosync/list.h>
  54. #include <corosync/queue.h>
  55. #include <corosync/mar_gen.h>
  56. #include <corosync/totem/totemip.h>
  57. #include <corosync/ipc_cfg.h>
  58. #include <corosync/lcr/lcr_comp.h>
  59. #include <corosync/engine/logsys.h>
  60. #include <corosync/engine/coroapi.h>
  61. #include <corosync/corodefs.h>
  62. LOGSYS_DECLARE_SUBSYS ("CFG");
  63. enum cfg_message_req_types {
  64. MESSAGE_REQ_EXEC_CFG_RINGREENABLE = 0,
  65. MESSAGE_REQ_EXEC_CFG_KILLNODE = 1,
  66. MESSAGE_REQ_EXEC_CFG_SHUTDOWN = 2
  67. };
  68. #define DEFAULT_SHUTDOWN_TIMEOUT 5
  69. static struct list_head trackers_list;
  70. /*
  71. * Variables controlling a requested shutdown
  72. */
  73. static corosync_timer_handle_t shutdown_timer;
  74. static struct cfg_info *shutdown_con;
  75. static uint32_t shutdown_flags;
  76. static int shutdown_yes;
  77. static int shutdown_no;
  78. static int shutdown_expected;
  79. struct cfg_info
  80. {
  81. struct list_head list;
  82. void *conn;
  83. void *tracker_conn;
  84. enum {SHUTDOWN_REPLY_UNKNOWN, SHUTDOWN_REPLY_YES, SHUTDOWN_REPLY_NO} shutdown_reply;
  85. };
  86. static void cfg_confchg_fn (
  87. enum totem_configuration_type configuration_type,
  88. const unsigned int *member_list, size_t member_list_entries,
  89. const unsigned int *left_list, size_t left_list_entries,
  90. const unsigned int *joined_list, size_t joined_list_entries,
  91. const struct memb_ring_id *ring_id);
  92. static int cfg_exec_init_fn (struct corosync_api_v1 *corosync_api_v1);
  93. static struct corosync_api_v1 *api;
  94. static int cfg_lib_init_fn (void *conn);
  95. static int cfg_lib_exit_fn (void *conn);
  96. static void message_handler_req_exec_cfg_ringreenable (
  97. const void *message,
  98. unsigned int nodeid);
  99. static void message_handler_req_exec_cfg_killnode (
  100. const void *message,
  101. unsigned int nodeid);
  102. static void message_handler_req_exec_cfg_shutdown (
  103. const void *message,
  104. unsigned int nodeid);
  105. static void exec_cfg_killnode_endian_convert (void *msg);
  106. static void message_handler_req_lib_cfg_ringstatusget (
  107. void *conn,
  108. const void *msg);
  109. static void message_handler_req_lib_cfg_ringreenable (
  110. void *conn,
  111. const void *msg);
  112. static void message_handler_req_lib_cfg_statetrack (
  113. void *conn,
  114. const void *msg);
  115. static void message_handler_req_lib_cfg_statetrackstop (
  116. void *conn,
  117. const void *msg);
  118. static void message_handler_req_lib_cfg_administrativestateset (
  119. void *conn,
  120. const void *msg);
  121. static void message_handler_req_lib_cfg_administrativestateget (
  122. void *conn,
  123. const void *msg);
  124. static void message_handler_req_lib_cfg_serviceload (
  125. void *conn,
  126. const void *msg);
  127. static void message_handler_req_lib_cfg_serviceunload (
  128. void *conn,
  129. const void *msg);
  130. static void message_handler_req_lib_cfg_killnode (
  131. void *conn,
  132. const void *msg);
  133. static void message_handler_req_lib_cfg_tryshutdown (
  134. void *conn,
  135. const void *msg);
  136. static void message_handler_req_lib_cfg_replytoshutdown (
  137. void *conn,
  138. const void *msg);
  139. static void message_handler_req_lib_cfg_get_node_addrs (
  140. void *conn,
  141. const void *msg);
  142. static void message_handler_req_lib_cfg_local_get (
  143. void *conn,
  144. const void *msg);
  145. /*
  146. * Service Handler Definition
  147. */
  148. static struct corosync_lib_handler cfg_lib_engine[] =
  149. {
  150. { /* 0 */
  151. .lib_handler_fn = message_handler_req_lib_cfg_ringstatusget,
  152. .response_size = sizeof (struct res_lib_cfg_ringstatusget),
  153. .response_id = MESSAGE_RES_CFG_RINGSTATUSGET,
  154. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  155. },
  156. { /* 1 */
  157. .lib_handler_fn = message_handler_req_lib_cfg_ringreenable,
  158. .response_size = sizeof (struct res_lib_cfg_ringreenable),
  159. .response_id = MESSAGE_RES_CFG_RINGREENABLE,
  160. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  161. },
  162. { /* 2 */
  163. .lib_handler_fn = message_handler_req_lib_cfg_statetrack,
  164. .response_size = sizeof (struct res_lib_cfg_statetrack),
  165. .response_id = MESSAGE_RES_CFG_STATETRACKSTART,
  166. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  167. },
  168. { /* 3 */
  169. .lib_handler_fn = message_handler_req_lib_cfg_statetrackstop,
  170. .response_size = sizeof (struct res_lib_cfg_statetrackstop),
  171. .response_id = MESSAGE_RES_CFG_STATETRACKSTOP,
  172. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  173. },
  174. { /* 4 */
  175. .lib_handler_fn = message_handler_req_lib_cfg_administrativestateset,
  176. .response_size = sizeof (struct res_lib_cfg_administrativestateset),
  177. .response_id = MESSAGE_RES_CFG_ADMINISTRATIVESTATESET,
  178. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  179. },
  180. { /* 5 */
  181. .lib_handler_fn = message_handler_req_lib_cfg_administrativestateget,
  182. .response_size = sizeof (struct res_lib_cfg_administrativestateget),
  183. .response_id = MESSAGE_RES_CFG_ADMINISTRATIVESTATEGET,
  184. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  185. },
  186. { /* 6 */
  187. .lib_handler_fn = message_handler_req_lib_cfg_serviceload,
  188. .response_size = sizeof (struct res_lib_cfg_serviceload),
  189. .response_id = MESSAGE_RES_CFG_SERVICELOAD,
  190. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  191. },
  192. { /* 7 */
  193. .lib_handler_fn = message_handler_req_lib_cfg_serviceunload,
  194. .response_size = sizeof (struct res_lib_cfg_serviceunload),
  195. .response_id = MESSAGE_RES_CFG_SERVICEUNLOAD,
  196. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  197. },
  198. { /* 8 */
  199. .lib_handler_fn = message_handler_req_lib_cfg_killnode,
  200. .response_size = sizeof (struct res_lib_cfg_killnode),
  201. .response_id = MESSAGE_RES_CFG_KILLNODE,
  202. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  203. },
  204. { /* 9 */
  205. .lib_handler_fn = message_handler_req_lib_cfg_tryshutdown,
  206. .response_size = sizeof (struct res_lib_cfg_tryshutdown),
  207. .response_id = MESSAGE_RES_CFG_TRYSHUTDOWN,
  208. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  209. },
  210. { /* 10 */
  211. .lib_handler_fn = message_handler_req_lib_cfg_replytoshutdown,
  212. .response_size = sizeof (struct res_lib_cfg_replytoshutdown),
  213. .response_id = MESSAGE_RES_CFG_REPLYTOSHUTDOWN,
  214. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  215. },
  216. { /* 11 */
  217. .lib_handler_fn = message_handler_req_lib_cfg_get_node_addrs,
  218. .response_size = sizeof (struct res_lib_cfg_get_node_addrs),
  219. .response_id = MESSAGE_RES_CFG_GET_NODE_ADDRS,
  220. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  221. },
  222. { /* 12 */
  223. .lib_handler_fn = message_handler_req_lib_cfg_local_get,
  224. .response_size = sizeof (struct res_lib_cfg_local_get),
  225. .response_id = MESSAGE_RES_CFG_LOCAL_GET,
  226. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  227. }
  228. };
  229. static struct corosync_exec_handler cfg_exec_engine[] =
  230. {
  231. { /* 0 */
  232. .exec_handler_fn = message_handler_req_exec_cfg_ringreenable,
  233. },
  234. { /* 1 */
  235. .exec_handler_fn = message_handler_req_exec_cfg_killnode,
  236. .exec_endian_convert_fn = exec_cfg_killnode_endian_convert
  237. },
  238. { /* 2 */
  239. .exec_handler_fn = message_handler_req_exec_cfg_shutdown,
  240. }
  241. };
  242. /*
  243. * Exports the interface for the service
  244. */
  245. struct corosync_service_engine cfg_service_engine = {
  246. .name = "corosync configuration service",
  247. .id = CFG_SERVICE,
  248. .private_data_size = sizeof(struct cfg_info),
  249. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  250. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  251. .lib_init_fn = cfg_lib_init_fn,
  252. .lib_exit_fn = cfg_lib_exit_fn,
  253. .lib_engine = cfg_lib_engine,
  254. .lib_engine_count = sizeof (cfg_lib_engine) / sizeof (struct corosync_lib_handler),
  255. .exec_init_fn = cfg_exec_init_fn,
  256. .exec_engine = cfg_exec_engine,
  257. .exec_engine_count = 0, /* sizeof (cfg_aisexec_handler_fns) / sizeof (coroync_exec_handler), */
  258. .confchg_fn = cfg_confchg_fn,
  259. };
  260. /*
  261. * Dynamic Loader definition
  262. */
  263. static struct corosync_service_engine *cfg_get_service_engine_ver0 (void);
  264. static struct corosync_service_engine_iface_ver0 cfg_service_engine_iface = {
  265. .corosync_get_service_engine_ver0 = cfg_get_service_engine_ver0
  266. };
  267. static struct lcr_iface corosync_cfg_ver0[1] = {
  268. {
  269. .name = "corosync_cfg",
  270. .version = 0,
  271. .versions_replace = 0,
  272. .versions_replace_count = 0,
  273. .dependencies = 0,
  274. .dependency_count = 0,
  275. .constructor = NULL,
  276. .destructor = NULL,
  277. .interfaces = NULL
  278. }
  279. };
  280. static struct lcr_comp cfg_comp_ver0 = {
  281. .iface_count = 1,
  282. .ifaces = corosync_cfg_ver0
  283. };
  284. static struct corosync_service_engine *cfg_get_service_engine_ver0 (void)
  285. {
  286. return (&cfg_service_engine);
  287. }
  288. __attribute__ ((constructor)) static void register_this_component (void) {
  289. lcr_interfaces_set (&corosync_cfg_ver0[0], &cfg_service_engine_iface);
  290. lcr_component_register (&cfg_comp_ver0);
  291. }
  292. struct req_exec_cfg_ringreenable {
  293. coroipc_request_header_t header __attribute__((aligned(8)));
  294. mar_message_source_t source __attribute__((aligned(8)));
  295. };
  296. struct req_exec_cfg_killnode {
  297. coroipc_request_header_t header __attribute__((aligned(8)));
  298. mar_uint32_t nodeid __attribute__((aligned(8)));
  299. mar_name_t reason __attribute__((aligned(8)));
  300. };
  301. struct req_exec_cfg_shutdown {
  302. coroipc_request_header_t header __attribute__((aligned(8)));
  303. };
  304. /* IMPL */
  305. static int cfg_exec_init_fn (
  306. struct corosync_api_v1 *corosync_api_v1)
  307. {
  308. api = corosync_api_v1;
  309. list_init(&trackers_list);
  310. return (0);
  311. }
  312. static void cfg_confchg_fn (
  313. enum totem_configuration_type configuration_type,
  314. const unsigned int *member_list, size_t member_list_entries,
  315. const unsigned int *left_list, size_t left_list_entries,
  316. const unsigned int *joined_list, size_t joined_list_entries,
  317. const struct memb_ring_id *ring_id)
  318. {
  319. }
  320. /*
  321. * Tell other nodes we are shutting down
  322. */
  323. static int send_shutdown(void)
  324. {
  325. struct req_exec_cfg_shutdown req_exec_cfg_shutdown;
  326. struct iovec iovec;
  327. ENTER();
  328. req_exec_cfg_shutdown.header.size =
  329. sizeof (struct req_exec_cfg_shutdown);
  330. req_exec_cfg_shutdown.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  331. MESSAGE_REQ_EXEC_CFG_SHUTDOWN);
  332. iovec.iov_base = (char *)&req_exec_cfg_shutdown;
  333. iovec.iov_len = sizeof (struct req_exec_cfg_shutdown);
  334. assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
  335. LEAVE();
  336. return 0;
  337. }
  338. static void send_test_shutdown(void *only_conn, void *exclude_conn, int status)
  339. {
  340. struct res_lib_cfg_testshutdown res_lib_cfg_testshutdown;
  341. struct list_head *iter;
  342. ENTER();
  343. res_lib_cfg_testshutdown.header.size = sizeof(struct res_lib_cfg_testshutdown);
  344. res_lib_cfg_testshutdown.header.id = MESSAGE_RES_CFG_TESTSHUTDOWN;
  345. res_lib_cfg_testshutdown.header.error = status;
  346. res_lib_cfg_testshutdown.flags = shutdown_flags;
  347. if (only_conn) {
  348. TRACE1("sending testshutdown to only %p", only_conn);
  349. api->ipc_dispatch_send(only_conn, &res_lib_cfg_testshutdown,
  350. sizeof(res_lib_cfg_testshutdown));
  351. } else {
  352. for (iter = trackers_list.next; iter != &trackers_list; iter = iter->next) {
  353. struct cfg_info *ci = list_entry(iter, struct cfg_info, list);
  354. if (ci->conn != exclude_conn) {
  355. TRACE1("sending testshutdown to %p", ci->tracker_conn);
  356. api->ipc_dispatch_send(ci->tracker_conn, &res_lib_cfg_testshutdown,
  357. sizeof(res_lib_cfg_testshutdown));
  358. }
  359. }
  360. }
  361. LEAVE();
  362. }
  363. static void check_shutdown_status(void)
  364. {
  365. ENTER();
  366. /*
  367. * Shutdown client might have gone away
  368. */
  369. if (!shutdown_con) {
  370. LEAVE();
  371. return;
  372. }
  373. /*
  374. * All replies safely gathered in ?
  375. */
  376. if (shutdown_yes + shutdown_no >= shutdown_expected) {
  377. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  378. api->timer_delete(shutdown_timer);
  379. if (shutdown_yes >= shutdown_expected ||
  380. shutdown_flags == CFG_SHUTDOWN_FLAG_REGARDLESS) {
  381. TRACE1("shutdown confirmed");
  382. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  383. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  384. res_lib_cfg_tryshutdown.header.error = CS_OK;
  385. /*
  386. * Tell originator that shutdown was confirmed
  387. */
  388. api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
  389. sizeof(res_lib_cfg_tryshutdown));
  390. shutdown_con = NULL;
  391. /*
  392. * Tell other nodes we are going down
  393. */
  394. send_shutdown();
  395. }
  396. else {
  397. TRACE1("shutdown cancelled");
  398. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  399. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  400. res_lib_cfg_tryshutdown.header.error = CS_ERR_BUSY;
  401. /*
  402. * Tell originator that shutdown was cancelled
  403. */
  404. api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
  405. sizeof(res_lib_cfg_tryshutdown));
  406. shutdown_con = NULL;
  407. }
  408. log_printf(LOGSYS_LEVEL_DEBUG, "shutdown decision is: (yes count: %d, no count: %d) flags=%x\n", shutdown_yes, shutdown_no, shutdown_flags);
  409. }
  410. LEAVE();
  411. }
  412. /*
  413. * Not all nodes responded to the shutdown (in time)
  414. */
  415. static void shutdown_timer_fn(void *arg)
  416. {
  417. ENTER();
  418. /*
  419. * Mark undecideds as "NO"
  420. */
  421. shutdown_no = shutdown_expected;
  422. check_shutdown_status();
  423. send_test_shutdown(NULL, NULL, CS_ERR_TIMEOUT);
  424. LEAVE();
  425. }
  426. static void remove_ci_from_shutdown(struct cfg_info *ci)
  427. {
  428. ENTER();
  429. /*
  430. * If the controlling shutdown process has quit, then cancel the
  431. * shutdown session
  432. */
  433. if (ci == shutdown_con) {
  434. shutdown_con = NULL;
  435. api->timer_delete(shutdown_timer);
  436. }
  437. if (!list_empty(&ci->list)) {
  438. list_del(&ci->list);
  439. list_init(&ci->list);
  440. /*
  441. * Remove our option
  442. */
  443. if (shutdown_con) {
  444. if (ci->shutdown_reply == SHUTDOWN_REPLY_YES)
  445. shutdown_yes--;
  446. if (ci->shutdown_reply == SHUTDOWN_REPLY_NO)
  447. shutdown_no--;
  448. }
  449. /*
  450. * If we are leaving, then that's an implicit YES to shutdown
  451. */
  452. ci->shutdown_reply = SHUTDOWN_REPLY_YES;
  453. shutdown_yes++;
  454. check_shutdown_status();
  455. }
  456. LEAVE();
  457. }
  458. int cfg_lib_exit_fn (void *conn)
  459. {
  460. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  461. ENTER();
  462. if (!list_empty(&ci->list)) {
  463. list_del(&ci->list);
  464. remove_ci_from_shutdown(ci);
  465. }
  466. LEAVE();
  467. return (0);
  468. }
  469. static int cfg_lib_init_fn (void *conn)
  470. {
  471. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  472. ENTER();
  473. list_init(&ci->list);
  474. LEAVE();
  475. return (0);
  476. }
  477. /*
  478. * Executive message handlers
  479. */
  480. static void message_handler_req_exec_cfg_ringreenable (
  481. const void *message,
  482. unsigned int nodeid)
  483. {
  484. const struct req_exec_cfg_ringreenable *req_exec_cfg_ringreenable
  485. = message;
  486. struct res_lib_cfg_ringreenable res_lib_cfg_ringreenable;
  487. ENTER();
  488. api->totem_ring_reenable ();
  489. if (api->ipc_source_is_local(&req_exec_cfg_ringreenable->source)) {
  490. res_lib_cfg_ringreenable.header.id = MESSAGE_RES_CFG_RINGREENABLE;
  491. res_lib_cfg_ringreenable.header.size = sizeof (struct res_lib_cfg_ringreenable);
  492. res_lib_cfg_ringreenable.header.error = CS_OK;
  493. api->ipc_response_send (
  494. req_exec_cfg_ringreenable->source.conn,
  495. &res_lib_cfg_ringreenable,
  496. sizeof (struct res_lib_cfg_ringreenable));
  497. }
  498. LEAVE();
  499. }
  500. static void exec_cfg_killnode_endian_convert (void *msg)
  501. {
  502. struct req_exec_cfg_killnode *req_exec_cfg_killnode =
  503. (struct req_exec_cfg_killnode *)msg;
  504. ENTER();
  505. swab_mar_name_t(&req_exec_cfg_killnode->reason);
  506. LEAVE();
  507. }
  508. static void message_handler_req_exec_cfg_killnode (
  509. const void *message,
  510. unsigned int nodeid)
  511. {
  512. const struct req_exec_cfg_killnode *req_exec_cfg_killnode = message;
  513. cs_name_t reason;
  514. ENTER();
  515. log_printf(LOGSYS_LEVEL_DEBUG, "request to kill node %d(us=%d): %s\n", req_exec_cfg_killnode->nodeid, api->totem_nodeid_get(), reason.value);
  516. if (req_exec_cfg_killnode->nodeid == api->totem_nodeid_get()) {
  517. marshall_from_mar_name_t(&reason, &req_exec_cfg_killnode->reason);
  518. log_printf(LOGSYS_LEVEL_NOTICE, "Killed by node %d: %s\n",
  519. nodeid, reason.value);
  520. corosync_fatal_error(COROSYNC_FATAL_ERROR_EXIT);
  521. }
  522. LEAVE();
  523. }
  524. /*
  525. * Self shutdown
  526. */
  527. static void message_handler_req_exec_cfg_shutdown (
  528. const void *message,
  529. unsigned int nodeid)
  530. {
  531. ENTER();
  532. log_printf(LOGSYS_LEVEL_NOTICE, "Node %d was shut down by sysadmin\n", nodeid);
  533. if (nodeid == api->totem_nodeid_get()) {
  534. corosync_fatal_error(COROSYNC_FATAL_ERROR_EXIT);
  535. }
  536. LEAVE();
  537. }
  538. /*
  539. * Library Interface Implementation
  540. */
  541. static void message_handler_req_lib_cfg_ringstatusget (
  542. void *conn,
  543. const void *msg)
  544. {
  545. struct res_lib_cfg_ringstatusget res_lib_cfg_ringstatusget;
  546. struct totem_ip_address interfaces[INTERFACE_MAX];
  547. unsigned int iface_count;
  548. char **status;
  549. const char *totem_ip_string;
  550. unsigned int i;
  551. ENTER();
  552. res_lib_cfg_ringstatusget.header.id = MESSAGE_RES_CFG_RINGSTATUSGET;
  553. res_lib_cfg_ringstatusget.header.size = sizeof (struct res_lib_cfg_ringstatusget);
  554. res_lib_cfg_ringstatusget.header.error = CS_OK;
  555. api->totem_ifaces_get (
  556. api->totem_nodeid_get(),
  557. interfaces,
  558. &status,
  559. &iface_count);
  560. res_lib_cfg_ringstatusget.interface_count = iface_count;
  561. for (i = 0; i < iface_count; i++) {
  562. totem_ip_string
  563. = (const char *)api->totem_ip_print (&interfaces[i]);
  564. strcpy ((char *)&res_lib_cfg_ringstatusget.interface_status[i],
  565. status[i]);
  566. strcpy ((char *)&res_lib_cfg_ringstatusget.interface_name[i],
  567. totem_ip_string);
  568. }
  569. api->ipc_response_send (
  570. conn,
  571. &res_lib_cfg_ringstatusget,
  572. sizeof (struct res_lib_cfg_ringstatusget));
  573. LEAVE();
  574. }
  575. static void message_handler_req_lib_cfg_ringreenable (
  576. void *conn,
  577. const void *msg)
  578. {
  579. struct req_exec_cfg_ringreenable req_exec_cfg_ringreenable;
  580. struct iovec iovec;
  581. ENTER();
  582. req_exec_cfg_ringreenable.header.size =
  583. sizeof (struct req_exec_cfg_ringreenable);
  584. req_exec_cfg_ringreenable.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  585. MESSAGE_REQ_EXEC_CFG_RINGREENABLE);
  586. api->ipc_source_set (&req_exec_cfg_ringreenable.source, conn);
  587. iovec.iov_base = (char *)&req_exec_cfg_ringreenable;
  588. iovec.iov_len = sizeof (struct req_exec_cfg_ringreenable);
  589. assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
  590. LEAVE();
  591. }
  592. static void message_handler_req_lib_cfg_statetrack (
  593. void *conn,
  594. const void *msg)
  595. {
  596. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  597. struct res_lib_cfg_statetrack res_lib_cfg_statetrack;
  598. ENTER();
  599. /*
  600. * We only do shutdown tracking at the moment
  601. */
  602. if (list_empty(&ci->list)) {
  603. list_add(&ci->list, &trackers_list);
  604. ci->tracker_conn = conn;
  605. if (shutdown_con) {
  606. /*
  607. * Shutdown already in progress, ask the newcomer's opinion
  608. */
  609. ci->shutdown_reply = SHUTDOWN_REPLY_UNKNOWN;
  610. shutdown_expected++;
  611. send_test_shutdown(conn, NULL, CS_OK);
  612. }
  613. }
  614. res_lib_cfg_statetrack.header.size = sizeof(struct res_lib_cfg_statetrack);
  615. res_lib_cfg_statetrack.header.id = MESSAGE_RES_CFG_STATETRACKSTART;
  616. res_lib_cfg_statetrack.header.error = CS_OK;
  617. api->ipc_response_send(conn, &res_lib_cfg_statetrack,
  618. sizeof(res_lib_cfg_statetrack));
  619. LEAVE();
  620. }
  621. static void message_handler_req_lib_cfg_statetrackstop (
  622. void *conn,
  623. const void *msg)
  624. {
  625. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  626. // struct req_lib_cfg_statetrackstop *req_lib_cfg_statetrackstop = (struct req_lib_cfg_statetrackstop *)message;
  627. ENTER();
  628. remove_ci_from_shutdown(ci);
  629. LEAVE();
  630. }
  631. static void message_handler_req_lib_cfg_administrativestateset (
  632. void *conn,
  633. const void *msg)
  634. {
  635. // struct req_lib_cfg_administrativestateset *req_lib_cfg_administrativestateset = (struct req_lib_cfg_administrativestateset *)message;
  636. ENTER();
  637. LEAVE();
  638. }
  639. static void message_handler_req_lib_cfg_administrativestateget (
  640. void *conn,
  641. const void *msg)
  642. {
  643. // struct req_lib_cfg_administrativestateget *req_lib_cfg_administrativestateget = (struct req_lib_cfg_administrativestateget *)message;
  644. ENTER();
  645. LEAVE();
  646. }
  647. static void message_handler_req_lib_cfg_serviceload (
  648. void *conn,
  649. const void *msg)
  650. {
  651. const struct req_lib_cfg_serviceload *req_lib_cfg_serviceload = msg;
  652. struct res_lib_cfg_serviceload res_lib_cfg_serviceload;
  653. ENTER();
  654. api->service_link_and_init (
  655. api,
  656. (const char *)req_lib_cfg_serviceload->service_name,
  657. req_lib_cfg_serviceload->service_ver);
  658. res_lib_cfg_serviceload.header.id = MESSAGE_RES_CFG_SERVICEUNLOAD;
  659. res_lib_cfg_serviceload.header.size = sizeof (struct res_lib_cfg_serviceload);
  660. res_lib_cfg_serviceload.header.error = CS_OK;
  661. api->ipc_response_send (
  662. conn,
  663. &res_lib_cfg_serviceload,
  664. sizeof (struct res_lib_cfg_serviceload));
  665. LEAVE();
  666. }
  667. static void message_handler_req_lib_cfg_serviceunload (
  668. void *conn,
  669. const void *msg)
  670. {
  671. const struct req_lib_cfg_serviceunload *req_lib_cfg_serviceunload = msg;
  672. struct res_lib_cfg_serviceunload res_lib_cfg_serviceunload;
  673. ENTER();
  674. api->service_unlink_and_exit (
  675. api,
  676. (const char *)req_lib_cfg_serviceunload->service_name,
  677. req_lib_cfg_serviceunload->service_ver);
  678. res_lib_cfg_serviceunload.header.id = MESSAGE_RES_CFG_SERVICEUNLOAD;
  679. res_lib_cfg_serviceunload.header.size = sizeof (struct res_lib_cfg_serviceunload);
  680. res_lib_cfg_serviceunload.header.error = CS_OK;
  681. api->ipc_response_send (
  682. conn,
  683. &res_lib_cfg_serviceunload,
  684. sizeof (struct res_lib_cfg_serviceunload));
  685. LEAVE();
  686. }
  687. static void message_handler_req_lib_cfg_killnode (
  688. void *conn,
  689. const void *msg)
  690. {
  691. const struct req_lib_cfg_killnode *req_lib_cfg_killnode = msg;
  692. struct res_lib_cfg_killnode res_lib_cfg_killnode;
  693. struct req_exec_cfg_killnode req_exec_cfg_killnode;
  694. struct iovec iovec;
  695. int res;
  696. ENTER();
  697. req_exec_cfg_killnode.header.size =
  698. sizeof (struct req_exec_cfg_killnode);
  699. req_exec_cfg_killnode.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  700. MESSAGE_REQ_EXEC_CFG_KILLNODE);
  701. req_exec_cfg_killnode.nodeid = req_lib_cfg_killnode->nodeid;
  702. marshall_to_mar_name_t(&req_exec_cfg_killnode.reason, &req_lib_cfg_killnode->reason);
  703. iovec.iov_base = (char *)&req_exec_cfg_killnode;
  704. iovec.iov_len = sizeof (struct req_exec_cfg_killnode);
  705. res = api->totem_mcast (&iovec, 1, TOTEM_SAFE);
  706. res_lib_cfg_killnode.header.size = sizeof(struct res_lib_cfg_killnode);
  707. res_lib_cfg_killnode.header.id = MESSAGE_RES_CFG_KILLNODE;
  708. res_lib_cfg_killnode.header.error = CS_OK;
  709. api->ipc_response_send(conn, &res_lib_cfg_killnode,
  710. sizeof(res_lib_cfg_killnode));
  711. LEAVE();
  712. }
  713. static void message_handler_req_lib_cfg_tryshutdown (
  714. void *conn,
  715. const void *msg)
  716. {
  717. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  718. const struct req_lib_cfg_tryshutdown *req_lib_cfg_tryshutdown = msg;
  719. struct list_head *iter;
  720. ENTER();
  721. if (req_lib_cfg_tryshutdown->flags == CFG_SHUTDOWN_FLAG_IMMEDIATE) {
  722. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  723. /*
  724. * Tell other nodes
  725. */
  726. send_shutdown();
  727. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  728. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  729. res_lib_cfg_tryshutdown.header.error = CS_OK;
  730. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  731. sizeof(res_lib_cfg_tryshutdown));
  732. LEAVE();
  733. return;
  734. }
  735. /*
  736. * Shutdown in progress, return an error
  737. */
  738. if (shutdown_con) {
  739. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  740. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  741. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  742. res_lib_cfg_tryshutdown.header.error = CS_ERR_EXIST;
  743. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  744. sizeof(res_lib_cfg_tryshutdown));
  745. LEAVE();
  746. return;
  747. }
  748. ci->conn = conn;
  749. shutdown_con = (struct cfg_info *)api->ipc_private_data_get (conn);
  750. shutdown_flags = req_lib_cfg_tryshutdown->flags;
  751. shutdown_yes = 0;
  752. shutdown_no = 0;
  753. /*
  754. * Count the number of listeners
  755. */
  756. shutdown_expected = 0;
  757. for (iter = trackers_list.next; iter != &trackers_list; iter = iter->next) {
  758. struct cfg_info *testci = list_entry(iter, struct cfg_info, list);
  759. /*
  760. * It is assumed that we will allow shutdown
  761. */
  762. if (testci != ci) {
  763. testci->shutdown_reply = SHUTDOWN_REPLY_UNKNOWN;
  764. shutdown_expected++;
  765. }
  766. }
  767. /*
  768. * If no-one is listening for events then we can just go down now
  769. */
  770. if (shutdown_expected == 0) {
  771. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  772. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  773. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  774. res_lib_cfg_tryshutdown.header.error = CS_OK;
  775. /*
  776. * Tell originator that shutdown was confirmed
  777. */
  778. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  779. sizeof(res_lib_cfg_tryshutdown));
  780. send_shutdown();
  781. LEAVE();
  782. return;
  783. }
  784. else {
  785. hdb_handle_t cfg_handle;
  786. hdb_handle_t find_handle;
  787. char *timeout_str;
  788. unsigned int shutdown_timeout = DEFAULT_SHUTDOWN_TIMEOUT;
  789. /*
  790. * Look for a shutdown timeout in objdb
  791. */
  792. api->object_find_create(OBJECT_PARENT_HANDLE, "cfg", strlen("cfg"), &find_handle);
  793. api->object_find_next(find_handle, &cfg_handle);
  794. api->object_find_destroy(find_handle);
  795. if (cfg_handle) {
  796. if ( !api->object_key_get(cfg_handle,
  797. "shutdown_timeout",
  798. strlen("shutdown_timeout"),
  799. (void *)&timeout_str,
  800. NULL)) {
  801. shutdown_timeout = atoi(timeout_str);
  802. }
  803. }
  804. /*
  805. * Start the timer. If we don't get a full set of replies before this goes
  806. * off we'll cancel the shutdown
  807. */
  808. api->timer_add_duration((unsigned long long)shutdown_timeout*1000000000, NULL,
  809. shutdown_timer_fn, &shutdown_timer);
  810. /*
  811. * Tell the users we would like to shut down
  812. */
  813. send_test_shutdown(NULL, conn, CS_OK);
  814. }
  815. /*
  816. * We don't sent a reply to the caller here.
  817. * We send it when we know if we can shut down or not
  818. */
  819. LEAVE();
  820. }
  821. static void message_handler_req_lib_cfg_replytoshutdown (
  822. void *conn,
  823. const void *msg)
  824. {
  825. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  826. const struct req_lib_cfg_replytoshutdown *req_lib_cfg_replytoshutdown = msg;
  827. struct res_lib_cfg_replytoshutdown res_lib_cfg_replytoshutdown;
  828. int status = CS_OK;
  829. ENTER();
  830. if (!shutdown_con) {
  831. status = CS_ERR_ACCESS;
  832. goto exit_fn;
  833. }
  834. if (req_lib_cfg_replytoshutdown->response) {
  835. shutdown_yes++;
  836. ci->shutdown_reply = SHUTDOWN_REPLY_YES;
  837. }
  838. else {
  839. shutdown_no++;
  840. ci->shutdown_reply = SHUTDOWN_REPLY_NO;
  841. }
  842. check_shutdown_status();
  843. exit_fn:
  844. res_lib_cfg_replytoshutdown.header.error = status;
  845. res_lib_cfg_replytoshutdown.header.id = MESSAGE_RES_CFG_REPLYTOSHUTDOWN;
  846. res_lib_cfg_replytoshutdown.header.size = sizeof(res_lib_cfg_replytoshutdown);
  847. api->ipc_response_send(conn, &res_lib_cfg_replytoshutdown,
  848. sizeof(res_lib_cfg_replytoshutdown));
  849. LEAVE();
  850. }
  851. static void message_handler_req_lib_cfg_get_node_addrs (void *conn,
  852. const void *msg)
  853. {
  854. struct totem_ip_address node_ifs[INTERFACE_MAX];
  855. char buf[PIPE_BUF];
  856. char **status;
  857. unsigned int num_interfaces = 0;
  858. int ret = CS_OK;
  859. int i;
  860. const struct req_lib_cfg_get_node_addrs *req_lib_cfg_get_node_addrs = msg;
  861. struct res_lib_cfg_get_node_addrs *res_lib_cfg_get_node_addrs = (struct res_lib_cfg_get_node_addrs *)buf;
  862. unsigned int nodeid = req_lib_cfg_get_node_addrs->nodeid;
  863. if (nodeid == 0)
  864. nodeid = api->totem_nodeid_get();
  865. api->totem_ifaces_get(nodeid, node_ifs, &status, &num_interfaces);
  866. res_lib_cfg_get_node_addrs->header.size = sizeof(struct res_lib_cfg_get_node_addrs) + (num_interfaces * TOTEMIP_ADDRLEN);
  867. res_lib_cfg_get_node_addrs->header.id = MESSAGE_RES_CFG_GET_NODE_ADDRS;
  868. res_lib_cfg_get_node_addrs->header.error = ret;
  869. res_lib_cfg_get_node_addrs->num_addrs = num_interfaces;
  870. if (num_interfaces) {
  871. res_lib_cfg_get_node_addrs->family = node_ifs[0].family;
  872. for (i = 0; i<num_interfaces; i++) {
  873. memcpy(&res_lib_cfg_get_node_addrs->addrs[i][0], node_ifs[i].addr, TOTEMIP_ADDRLEN);
  874. }
  875. }
  876. else {
  877. res_lib_cfg_get_node_addrs->header.error = CS_ERR_NOT_EXIST;
  878. }
  879. api->ipc_response_send(conn, res_lib_cfg_get_node_addrs, res_lib_cfg_get_node_addrs->header.size);
  880. }
  881. static void message_handler_req_lib_cfg_local_get (void *conn, const void *msg)
  882. {
  883. struct res_lib_cfg_local_get res_lib_cfg_local_get;
  884. res_lib_cfg_local_get.header.size = sizeof(res_lib_cfg_local_get);
  885. res_lib_cfg_local_get.header.id = MESSAGE_RES_CFG_LOCAL_GET;
  886. res_lib_cfg_local_get.header.error = CS_OK;
  887. res_lib_cfg_local_get.local_nodeid = api->totem_nodeid_get ();
  888. api->ipc_response_send(conn, &res_lib_cfg_local_get,
  889. sizeof(res_lib_cfg_local_get));
  890. }