4
0

cfg.c 29 KB

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