cfg.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /*
  2. * Copyright (c) 2005-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2018 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 <string.h>
  49. #include <assert.h>
  50. #include <corosync/corotypes.h>
  51. #include <qb/qbipc_common.h>
  52. #include <corosync/cfg.h>
  53. #include <qb/qblist.h>
  54. #include <corosync/mar_gen.h>
  55. #include <corosync/totem/totemip.h>
  56. #include <corosync/totem/totem.h>
  57. #include <corosync/ipc_cfg.h>
  58. #include <corosync/logsys.h>
  59. #include <corosync/coroapi.h>
  60. #include <corosync/icmap.h>
  61. #include <corosync/corodefs.h>
  62. #include "service.h"
  63. #include "main.h"
  64. LOGSYS_DECLARE_SUBSYS ("CFG");
  65. enum cfg_message_req_types {
  66. MESSAGE_REQ_EXEC_CFG_RINGREENABLE = 0,
  67. MESSAGE_REQ_EXEC_CFG_KILLNODE = 1,
  68. MESSAGE_REQ_EXEC_CFG_SHUTDOWN = 2,
  69. MESSAGE_REQ_EXEC_CFG_RELOAD_CONFIG = 3
  70. };
  71. #define DEFAULT_SHUTDOWN_TIMEOUT 5
  72. static struct qb_list_head trackers_list;
  73. /*
  74. * Variables controlling a requested shutdown
  75. */
  76. static corosync_timer_handle_t shutdown_timer;
  77. static struct cfg_info *shutdown_con;
  78. static uint32_t shutdown_flags;
  79. static int shutdown_yes;
  80. static int shutdown_no;
  81. static int shutdown_expected;
  82. struct cfg_info
  83. {
  84. struct qb_list_head list;
  85. void *conn;
  86. void *tracker_conn;
  87. enum {SHUTDOWN_REPLY_UNKNOWN, SHUTDOWN_REPLY_YES, SHUTDOWN_REPLY_NO} shutdown_reply;
  88. };
  89. static void cfg_confchg_fn (
  90. enum totem_configuration_type configuration_type,
  91. const unsigned int *member_list, size_t member_list_entries,
  92. const unsigned int *left_list, size_t left_list_entries,
  93. const unsigned int *joined_list, size_t joined_list_entries,
  94. const struct memb_ring_id *ring_id);
  95. static char *cfg_exec_init_fn (struct corosync_api_v1 *corosync_api_v1);
  96. static struct corosync_api_v1 *api;
  97. static int cfg_lib_init_fn (void *conn);
  98. static int cfg_lib_exit_fn (void *conn);
  99. static void message_handler_req_exec_cfg_ringreenable (
  100. const void *message,
  101. unsigned int nodeid);
  102. static void message_handler_req_exec_cfg_killnode (
  103. const void *message,
  104. unsigned int nodeid);
  105. static void message_handler_req_exec_cfg_shutdown (
  106. const void *message,
  107. unsigned int nodeid);
  108. static void message_handler_req_exec_cfg_reload_config (
  109. const void *message,
  110. unsigned int nodeid);
  111. static void exec_cfg_killnode_endian_convert (void *msg);
  112. static void message_handler_req_lib_cfg_ringstatusget (
  113. void *conn,
  114. const void *msg);
  115. static void message_handler_req_lib_cfg_ringreenable (
  116. void *conn,
  117. const void *msg);
  118. static void message_handler_req_lib_cfg_killnode (
  119. void *conn,
  120. const void *msg);
  121. static void message_handler_req_lib_cfg_tryshutdown (
  122. void *conn,
  123. const void *msg);
  124. static void message_handler_req_lib_cfg_replytoshutdown (
  125. void *conn,
  126. const void *msg);
  127. static void message_handler_req_lib_cfg_get_node_addrs (
  128. void *conn,
  129. const void *msg);
  130. static void message_handler_req_lib_cfg_local_get (
  131. void *conn,
  132. const void *msg);
  133. static void message_handler_req_lib_cfg_reload_config (
  134. void *conn,
  135. const void *msg);
  136. static void message_handler_req_lib_cfg_reopen_log_files (
  137. void *conn,
  138. const void *msg);
  139. /*
  140. * Service Handler Definition
  141. */
  142. static struct corosync_lib_handler cfg_lib_engine[] =
  143. {
  144. { /* 0 */
  145. .lib_handler_fn = message_handler_req_lib_cfg_ringstatusget,
  146. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  147. },
  148. { /* 1 */
  149. .lib_handler_fn = message_handler_req_lib_cfg_ringreenable,
  150. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  151. },
  152. { /* 2 */
  153. .lib_handler_fn = message_handler_req_lib_cfg_killnode,
  154. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  155. },
  156. { /* 3 */
  157. .lib_handler_fn = message_handler_req_lib_cfg_tryshutdown,
  158. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  159. },
  160. { /* 4 */
  161. .lib_handler_fn = message_handler_req_lib_cfg_replytoshutdown,
  162. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  163. },
  164. { /* 5 */
  165. .lib_handler_fn = message_handler_req_lib_cfg_get_node_addrs,
  166. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  167. },
  168. { /* 6 */
  169. .lib_handler_fn = message_handler_req_lib_cfg_local_get,
  170. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  171. },
  172. { /* 7 */
  173. .lib_handler_fn = message_handler_req_lib_cfg_reload_config,
  174. .flow_control = CS_LIB_FLOW_CONTROL_REQUIRED
  175. },
  176. { /* 8 */
  177. .lib_handler_fn = message_handler_req_lib_cfg_reopen_log_files,
  178. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED
  179. }
  180. };
  181. static struct corosync_exec_handler cfg_exec_engine[] =
  182. {
  183. { /* 0 */
  184. .exec_handler_fn = message_handler_req_exec_cfg_ringreenable,
  185. },
  186. { /* 1 */
  187. .exec_handler_fn = message_handler_req_exec_cfg_killnode,
  188. .exec_endian_convert_fn = exec_cfg_killnode_endian_convert
  189. },
  190. { /* 2 */
  191. .exec_handler_fn = message_handler_req_exec_cfg_shutdown,
  192. },
  193. { /* 3 */
  194. .exec_handler_fn = message_handler_req_exec_cfg_reload_config,
  195. }
  196. };
  197. /*
  198. * Exports the interface for the service
  199. */
  200. struct corosync_service_engine cfg_service_engine = {
  201. .name = "corosync configuration service",
  202. .id = CFG_SERVICE,
  203. .priority = 1,
  204. .private_data_size = sizeof(struct cfg_info),
  205. .flow_control = CS_LIB_FLOW_CONTROL_NOT_REQUIRED,
  206. .allow_inquorate = CS_LIB_ALLOW_INQUORATE,
  207. .lib_init_fn = cfg_lib_init_fn,
  208. .lib_exit_fn = cfg_lib_exit_fn,
  209. .lib_engine = cfg_lib_engine,
  210. .lib_engine_count = sizeof (cfg_lib_engine) / sizeof (struct corosync_lib_handler),
  211. .exec_init_fn = cfg_exec_init_fn,
  212. .exec_engine = cfg_exec_engine,
  213. .exec_engine_count = sizeof (cfg_exec_engine) / sizeof (struct corosync_exec_handler),
  214. .confchg_fn = cfg_confchg_fn
  215. };
  216. struct corosync_service_engine *cfg_get_service_engine_ver0 (void)
  217. {
  218. return (&cfg_service_engine);
  219. }
  220. struct req_exec_cfg_ringreenable {
  221. struct qb_ipc_request_header header __attribute__((aligned(8)));
  222. mar_message_source_t source __attribute__((aligned(8)));
  223. };
  224. struct req_exec_cfg_reload_config {
  225. struct qb_ipc_request_header header __attribute__((aligned(8)));
  226. mar_message_source_t source __attribute__((aligned(8)));
  227. };
  228. struct req_exec_cfg_killnode {
  229. struct qb_ipc_request_header header __attribute__((aligned(8)));
  230. mar_uint32_t nodeid __attribute__((aligned(8)));
  231. mar_name_t reason __attribute__((aligned(8)));
  232. };
  233. struct req_exec_cfg_shutdown {
  234. struct qb_ipc_request_header header __attribute__((aligned(8)));
  235. };
  236. /* IMPL */
  237. static char *cfg_exec_init_fn (
  238. struct corosync_api_v1 *corosync_api_v1)
  239. {
  240. api = corosync_api_v1;
  241. qb_list_init(&trackers_list);
  242. return (NULL);
  243. }
  244. static void cfg_confchg_fn (
  245. enum totem_configuration_type configuration_type,
  246. const unsigned int *member_list, size_t member_list_entries,
  247. const unsigned int *left_list, size_t left_list_entries,
  248. const unsigned int *joined_list, size_t joined_list_entries,
  249. const struct memb_ring_id *ring_id)
  250. {
  251. }
  252. /*
  253. * Tell other nodes we are shutting down
  254. */
  255. static int send_shutdown(void)
  256. {
  257. struct req_exec_cfg_shutdown req_exec_cfg_shutdown;
  258. struct iovec iovec;
  259. ENTER();
  260. req_exec_cfg_shutdown.header.size =
  261. sizeof (struct req_exec_cfg_shutdown);
  262. req_exec_cfg_shutdown.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  263. MESSAGE_REQ_EXEC_CFG_SHUTDOWN);
  264. iovec.iov_base = (char *)&req_exec_cfg_shutdown;
  265. iovec.iov_len = sizeof (struct req_exec_cfg_shutdown);
  266. assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
  267. LEAVE();
  268. return 0;
  269. }
  270. static void send_test_shutdown(void *only_conn, void *exclude_conn, int status)
  271. {
  272. struct res_lib_cfg_testshutdown res_lib_cfg_testshutdown;
  273. struct qb_list_head *iter;
  274. ENTER();
  275. res_lib_cfg_testshutdown.header.size = sizeof(struct res_lib_cfg_testshutdown);
  276. res_lib_cfg_testshutdown.header.id = MESSAGE_RES_CFG_TESTSHUTDOWN;
  277. res_lib_cfg_testshutdown.header.error = status;
  278. res_lib_cfg_testshutdown.flags = shutdown_flags;
  279. if (only_conn) {
  280. TRACE1("sending testshutdown to only %p", only_conn);
  281. api->ipc_dispatch_send(only_conn, &res_lib_cfg_testshutdown,
  282. sizeof(res_lib_cfg_testshutdown));
  283. } else {
  284. qb_list_for_each(iter, &trackers_list) {
  285. struct cfg_info *ci = qb_list_entry(iter, struct cfg_info, list);
  286. if (ci->conn != exclude_conn) {
  287. TRACE1("sending testshutdown to %p", ci->tracker_conn);
  288. api->ipc_dispatch_send(ci->tracker_conn, &res_lib_cfg_testshutdown,
  289. sizeof(res_lib_cfg_testshutdown));
  290. }
  291. }
  292. }
  293. LEAVE();
  294. }
  295. static void check_shutdown_status(void)
  296. {
  297. ENTER();
  298. /*
  299. * Shutdown client might have gone away
  300. */
  301. if (!shutdown_con) {
  302. LEAVE();
  303. return;
  304. }
  305. /*
  306. * All replies safely gathered in ?
  307. */
  308. if (shutdown_yes + shutdown_no >= shutdown_expected) {
  309. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  310. api->timer_delete(shutdown_timer);
  311. if (shutdown_yes >= shutdown_expected ||
  312. shutdown_flags == CFG_SHUTDOWN_FLAG_REGARDLESS) {
  313. TRACE1("shutdown confirmed");
  314. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  315. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  316. res_lib_cfg_tryshutdown.header.error = CS_OK;
  317. /*
  318. * Tell originator that shutdown was confirmed
  319. */
  320. api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
  321. sizeof(res_lib_cfg_tryshutdown));
  322. shutdown_con = NULL;
  323. /*
  324. * Tell other nodes we are going down
  325. */
  326. send_shutdown();
  327. }
  328. else {
  329. TRACE1("shutdown cancelled");
  330. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  331. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  332. res_lib_cfg_tryshutdown.header.error = CS_ERR_BUSY;
  333. /*
  334. * Tell originator that shutdown was cancelled
  335. */
  336. api->ipc_response_send(shutdown_con->conn, &res_lib_cfg_tryshutdown,
  337. sizeof(res_lib_cfg_tryshutdown));
  338. shutdown_con = NULL;
  339. }
  340. log_printf(LOGSYS_LEVEL_DEBUG, "shutdown decision is: (yes count: %d, no count: %d) flags=%x",
  341. shutdown_yes, shutdown_no, shutdown_flags);
  342. }
  343. LEAVE();
  344. }
  345. /*
  346. * Not all nodes responded to the shutdown (in time)
  347. */
  348. static void shutdown_timer_fn(void *arg)
  349. {
  350. ENTER();
  351. /*
  352. * Mark undecideds as "NO"
  353. */
  354. shutdown_no = shutdown_expected;
  355. check_shutdown_status();
  356. send_test_shutdown(NULL, NULL, CS_ERR_TIMEOUT);
  357. LEAVE();
  358. }
  359. static void remove_ci_from_shutdown(struct cfg_info *ci)
  360. {
  361. ENTER();
  362. /*
  363. * If the controlling shutdown process has quit, then cancel the
  364. * shutdown session
  365. */
  366. if (ci == shutdown_con) {
  367. shutdown_con = NULL;
  368. api->timer_delete(shutdown_timer);
  369. }
  370. if (!qb_list_empty(&ci->list)) {
  371. qb_list_del(&ci->list);
  372. qb_list_init(&ci->list);
  373. /*
  374. * Remove our option
  375. */
  376. if (shutdown_con) {
  377. if (ci->shutdown_reply == SHUTDOWN_REPLY_YES)
  378. shutdown_yes--;
  379. if (ci->shutdown_reply == SHUTDOWN_REPLY_NO)
  380. shutdown_no--;
  381. }
  382. /*
  383. * If we are leaving, then that's an implicit YES to shutdown
  384. */
  385. ci->shutdown_reply = SHUTDOWN_REPLY_YES;
  386. shutdown_yes++;
  387. check_shutdown_status();
  388. }
  389. LEAVE();
  390. }
  391. int cfg_lib_exit_fn (void *conn)
  392. {
  393. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  394. ENTER();
  395. remove_ci_from_shutdown(ci);
  396. LEAVE();
  397. return (0);
  398. }
  399. static int cfg_lib_init_fn (void *conn)
  400. {
  401. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  402. ENTER();
  403. qb_list_init(&ci->list);
  404. LEAVE();
  405. return (0);
  406. }
  407. /*
  408. * Executive message handlers
  409. */
  410. static void message_handler_req_exec_cfg_ringreenable (
  411. const void *message,
  412. unsigned int nodeid)
  413. {
  414. ENTER();
  415. LEAVE();
  416. }
  417. static void exec_cfg_killnode_endian_convert (void *msg)
  418. {
  419. struct req_exec_cfg_killnode *req_exec_cfg_killnode =
  420. (struct req_exec_cfg_killnode *)msg;
  421. ENTER();
  422. swab_mar_name_t(&req_exec_cfg_killnode->reason);
  423. LEAVE();
  424. }
  425. static void message_handler_req_exec_cfg_killnode (
  426. const void *message,
  427. unsigned int nodeid)
  428. {
  429. const struct req_exec_cfg_killnode *req_exec_cfg_killnode = message;
  430. cs_name_t reason;
  431. ENTER();
  432. log_printf(LOGSYS_LEVEL_DEBUG, "request to kill node %d(us=%d)",
  433. req_exec_cfg_killnode->nodeid, api->totem_nodeid_get());
  434. if (req_exec_cfg_killnode->nodeid == api->totem_nodeid_get()) {
  435. marshall_from_mar_name_t(&reason, &req_exec_cfg_killnode->reason);
  436. log_printf(LOGSYS_LEVEL_NOTICE, "Killed by node %d: %s",
  437. nodeid, reason.value);
  438. corosync_fatal_error(COROSYNC_FATAL_ERROR_EXIT);
  439. }
  440. LEAVE();
  441. }
  442. /*
  443. * Self shutdown
  444. */
  445. static void message_handler_req_exec_cfg_shutdown (
  446. const void *message,
  447. unsigned int nodeid)
  448. {
  449. ENTER();
  450. log_printf(LOGSYS_LEVEL_NOTICE, "Node %d was shut down by sysadmin", nodeid);
  451. if (nodeid == api->totem_nodeid_get()) {
  452. api->shutdown_request();
  453. }
  454. LEAVE();
  455. }
  456. /* strcmp replacement that can handle NULLs */
  457. static int nullcheck_strcmp(const char* left, const char *right)
  458. {
  459. if (!left && right)
  460. return -1;
  461. if (left && !right)
  462. return 1;
  463. if (!left && !right)
  464. return 0;
  465. return strcmp(left, right);
  466. }
  467. /*
  468. * If a key has changed value in the new file, then warn the user and remove it from the temp_map
  469. */
  470. static void delete_and_notify_if_changed(icmap_map_t temp_map, const char *key_name)
  471. {
  472. if (!(icmap_key_value_eq(temp_map, key_name, icmap_get_global_map(), key_name))) {
  473. if (icmap_delete_r(temp_map, key_name) == CS_OK) {
  474. log_printf(LOGSYS_LEVEL_NOTICE, "Modified entry '%s' in corosync.conf cannot be changed at run-time", key_name);
  475. }
  476. }
  477. }
  478. /*
  479. * Remove any keys from the new config file that in the new corosync.conf but that
  480. * cannot be changed at run time. A log message will be issued for each
  481. * entry that the user wants to change but they cannot.
  482. *
  483. * Add more here as needed.
  484. */
  485. static void remove_ro_entries(icmap_map_t temp_map)
  486. {
  487. delete_and_notify_if_changed(temp_map, "totem.secauth");
  488. delete_and_notify_if_changed(temp_map, "totem.crypto_hash");
  489. delete_and_notify_if_changed(temp_map, "totem.crypto_cipher");
  490. delete_and_notify_if_changed(temp_map, "totem.version");
  491. delete_and_notify_if_changed(temp_map, "totem.threads");
  492. delete_and_notify_if_changed(temp_map, "totem.ip_version");
  493. delete_and_notify_if_changed(temp_map, "totem.rrp_mode");
  494. delete_and_notify_if_changed(temp_map, "totem.netmtu");
  495. delete_and_notify_if_changed(temp_map, "totem.interface.ringnumber");
  496. delete_and_notify_if_changed(temp_map, "totem.interface.bindnetaddr");
  497. delete_and_notify_if_changed(temp_map, "totem.interface.mcastaddr");
  498. delete_and_notify_if_changed(temp_map, "totem.interface.broadcast");
  499. delete_and_notify_if_changed(temp_map, "totem.interface.mcastport");
  500. delete_and_notify_if_changed(temp_map, "totem.interface.ttl");
  501. delete_and_notify_if_changed(temp_map, "totem.vsftype");
  502. delete_and_notify_if_changed(temp_map, "totem.transport");
  503. delete_and_notify_if_changed(temp_map, "totem.cluster_name");
  504. delete_and_notify_if_changed(temp_map, "quorum.provider");
  505. delete_and_notify_if_changed(temp_map, "qb.ipc_type");
  506. }
  507. /*
  508. * Remove entries that exist in the global map, but not in the temp_map, this will
  509. * cause delete notifications to be sent to any listeners.
  510. *
  511. * NOTE: This routine depends entirely on the keys returned by the iterators
  512. * being in alpha-sorted order.
  513. */
  514. static void remove_deleted_entries(icmap_map_t temp_map, const char *prefix)
  515. {
  516. icmap_iter_t old_iter;
  517. icmap_iter_t new_iter;
  518. const char *old_key, *new_key;
  519. int ret;
  520. old_iter = icmap_iter_init(prefix);
  521. new_iter = icmap_iter_init_r(temp_map, prefix);
  522. old_key = icmap_iter_next(old_iter, NULL, NULL);
  523. new_key = icmap_iter_next(new_iter, NULL, NULL);
  524. while (old_key || new_key) {
  525. ret = nullcheck_strcmp(old_key, new_key);
  526. if ((ret < 0 && old_key) || !new_key) {
  527. /*
  528. * new_key is greater, a line (or more) has been deleted
  529. * Continue until old is >= new
  530. */
  531. do {
  532. /* Remove it from icmap & send notifications */
  533. icmap_delete(old_key);
  534. old_key = icmap_iter_next(old_iter, NULL, NULL);
  535. ret = nullcheck_strcmp(old_key, new_key);
  536. } while (ret < 0 && old_key);
  537. }
  538. else if ((ret > 0 && new_key) || !old_key) {
  539. /*
  540. * old_key is greater, a line (or more) has been added
  541. * Continue until new is >= old
  542. *
  543. * we don't need to do anything special with this like tell
  544. * icmap. That will happen when we copy the values over
  545. */
  546. do {
  547. new_key = icmap_iter_next(new_iter, NULL, NULL);
  548. ret = nullcheck_strcmp(old_key, new_key);
  549. } while (ret > 0 && new_key);
  550. }
  551. if (ret == 0) {
  552. new_key = icmap_iter_next(new_iter, NULL, NULL);
  553. old_key = icmap_iter_next(old_iter, NULL, NULL);
  554. }
  555. }
  556. icmap_iter_finalize(new_iter);
  557. icmap_iter_finalize(old_iter);
  558. }
  559. /*
  560. * Reload configuration file
  561. */
  562. static void message_handler_req_exec_cfg_reload_config (
  563. const void *message,
  564. unsigned int nodeid)
  565. {
  566. const struct req_exec_cfg_reload_config *req_exec_cfg_reload_config = message;
  567. struct res_lib_cfg_reload_config res_lib_cfg_reload_config;
  568. icmap_map_t temp_map;
  569. const char *error_string;
  570. int res = CS_OK;
  571. ENTER();
  572. log_printf(LOGSYS_LEVEL_NOTICE, "Config reload requested by node %d", nodeid);
  573. /*
  574. * Set up a new hashtable as a staging area.
  575. */
  576. if ((res = icmap_init_r(&temp_map)) != CS_OK) {
  577. log_printf(LOGSYS_LEVEL_ERROR, "Unable to create temporary icmap. config file reload cancelled\n");
  578. goto reload_fini;
  579. }
  580. /*
  581. * Load new config into the temporary map
  582. */
  583. res = coroparse_configparse(temp_map, &error_string);
  584. if (res == -1) {
  585. log_printf (LOGSYS_LEVEL_ERROR, "Unable to reload config file: %s", error_string);
  586. res = CS_ERR_LIBRARY;
  587. goto reload_return;
  588. }
  589. /* Tell interested listeners that we have started a reload */
  590. icmap_set_uint8("config.reload_in_progress", 1);
  591. /* Detect deleted entries and remove them from the main icmap hashtable */
  592. remove_deleted_entries(temp_map, "logging.");
  593. remove_deleted_entries(temp_map, "totem.");
  594. remove_deleted_entries(temp_map, "nodelist.");
  595. remove_deleted_entries(temp_map, "quorum.");
  596. remove_deleted_entries(temp_map, "uidgid.config.");
  597. /* Remove entries that cannot be changed */
  598. remove_ro_entries(temp_map);
  599. /*
  600. * Copy new keys into live config.
  601. * If this fails we will have a partially loaded config because some keys (above) might
  602. * have been reset to defaults - I'm not sure what to do here, we might have to quit.
  603. */
  604. if ( (res = icmap_copy_map(icmap_get_global_map(), temp_map)) != CS_OK) {
  605. log_printf (LOGSYS_LEVEL_ERROR, "Error making new config live. cmap database may be inconsistent\n");
  606. }
  607. /* All done - let clients know */
  608. icmap_set_uint8("config.reload_in_progress", 0);
  609. reload_fini:
  610. /* Finished with the temporary storage */
  611. icmap_fini_r(temp_map);
  612. reload_return:
  613. /* All done, return result to the caller if it was on this system */
  614. if (nodeid == api->totem_nodeid_get()) {
  615. res_lib_cfg_reload_config.header.size = sizeof(res_lib_cfg_reload_config);
  616. res_lib_cfg_reload_config.header.id = MESSAGE_RES_CFG_RELOAD_CONFIG;
  617. res_lib_cfg_reload_config.header.error = res;
  618. api->ipc_response_send(req_exec_cfg_reload_config->source.conn,
  619. &res_lib_cfg_reload_config,
  620. sizeof(res_lib_cfg_reload_config));
  621. api->ipc_refcnt_dec(req_exec_cfg_reload_config->source.conn);;
  622. }
  623. LEAVE();
  624. }
  625. /*
  626. * Library Interface Implementation
  627. */
  628. static void message_handler_req_lib_cfg_ringstatusget (
  629. void *conn,
  630. const void *msg)
  631. {
  632. struct res_lib_cfg_ringstatusget res_lib_cfg_ringstatusget;
  633. struct totem_ip_address interfaces[INTERFACE_MAX];
  634. unsigned int iface_count;
  635. char **status;
  636. const char *totem_ip_string;
  637. char ifname[CFG_INTERFACE_NAME_MAX_LEN];
  638. unsigned int iface_ids[INTERFACE_MAX];
  639. unsigned int i;
  640. cs_error_t res = CS_OK;
  641. ENTER();
  642. res_lib_cfg_ringstatusget.header.id = MESSAGE_RES_CFG_RINGSTATUSGET;
  643. res_lib_cfg_ringstatusget.header.size = sizeof (struct res_lib_cfg_ringstatusget);
  644. api->totem_ifaces_get (
  645. api->totem_nodeid_get(),
  646. iface_ids,
  647. interfaces,
  648. INTERFACE_MAX,
  649. &status,
  650. &iface_count);
  651. assert(iface_count <= CFG_MAX_INTERFACES);
  652. res_lib_cfg_ringstatusget.interface_count = iface_count;
  653. for (i = 0; i < iface_count; i++) {
  654. totem_ip_string
  655. = (const char *)api->totem_ip_print (&interfaces[i]);
  656. if (!totem_ip_string) {
  657. totem_ip_string="";
  658. }
  659. /* Allow for i/f number at the start */
  660. if (strlen(totem_ip_string) >= CFG_INTERFACE_NAME_MAX_LEN-3) {
  661. log_printf(LOGSYS_LEVEL_ERROR, "String representation of interface %u is too long", i);
  662. res = CS_ERR_NAME_TOO_LONG;
  663. goto send_response;
  664. }
  665. snprintf(ifname, sizeof(ifname), "%d %s", iface_ids[i], totem_ip_string);
  666. if (strlen(status[i]) >= CFG_INTERFACE_STATUS_MAX_LEN) {
  667. log_printf(LOGSYS_LEVEL_ERROR, "Status string for interface %u is too long", i);
  668. res = CS_ERR_NAME_TOO_LONG;
  669. goto send_response;
  670. }
  671. strcpy ((char *)&res_lib_cfg_ringstatusget.interface_status[i],
  672. status[i]);
  673. strcpy ((char *)&res_lib_cfg_ringstatusget.interface_name[i],
  674. ifname);
  675. }
  676. send_response:
  677. res_lib_cfg_ringstatusget.header.error = res;
  678. api->ipc_response_send (
  679. conn,
  680. &res_lib_cfg_ringstatusget,
  681. sizeof (struct res_lib_cfg_ringstatusget));
  682. LEAVE();
  683. }
  684. static void message_handler_req_lib_cfg_ringreenable (
  685. void *conn,
  686. const void *msg)
  687. {
  688. struct res_lib_cfg_ringreenable res_lib_cfg_ringreenable;
  689. ENTER();
  690. res_lib_cfg_ringreenable.header.id = MESSAGE_RES_CFG_RINGREENABLE;
  691. res_lib_cfg_ringreenable.header.size = sizeof (struct res_lib_cfg_ringreenable);
  692. res_lib_cfg_ringreenable.header.error = CS_ERR_NOT_SUPPORTED;
  693. api->ipc_response_send (
  694. conn, &res_lib_cfg_ringreenable,
  695. sizeof (struct res_lib_cfg_ringreenable));
  696. LEAVE();
  697. }
  698. static void message_handler_req_lib_cfg_killnode (
  699. void *conn,
  700. const void *msg)
  701. {
  702. const struct req_lib_cfg_killnode *req_lib_cfg_killnode = msg;
  703. struct res_lib_cfg_killnode res_lib_cfg_killnode;
  704. struct req_exec_cfg_killnode req_exec_cfg_killnode;
  705. struct iovec iovec;
  706. ENTER();
  707. req_exec_cfg_killnode.header.size =
  708. sizeof (struct req_exec_cfg_killnode);
  709. req_exec_cfg_killnode.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  710. MESSAGE_REQ_EXEC_CFG_KILLNODE);
  711. req_exec_cfg_killnode.nodeid = req_lib_cfg_killnode->nodeid;
  712. marshall_to_mar_name_t(&req_exec_cfg_killnode.reason, &req_lib_cfg_killnode->reason);
  713. iovec.iov_base = (char *)&req_exec_cfg_killnode;
  714. iovec.iov_len = sizeof (struct req_exec_cfg_killnode);
  715. (void)api->totem_mcast (&iovec, 1, TOTEM_SAFE);
  716. res_lib_cfg_killnode.header.size = sizeof(struct res_lib_cfg_killnode);
  717. res_lib_cfg_killnode.header.id = MESSAGE_RES_CFG_KILLNODE;
  718. res_lib_cfg_killnode.header.error = CS_OK;
  719. api->ipc_response_send(conn, &res_lib_cfg_killnode,
  720. sizeof(res_lib_cfg_killnode));
  721. LEAVE();
  722. }
  723. static void message_handler_req_lib_cfg_tryshutdown (
  724. void *conn,
  725. const void *msg)
  726. {
  727. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  728. const struct req_lib_cfg_tryshutdown *req_lib_cfg_tryshutdown = msg;
  729. struct qb_list_head *iter;
  730. ENTER();
  731. if (req_lib_cfg_tryshutdown->flags == CFG_SHUTDOWN_FLAG_IMMEDIATE) {
  732. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  733. /*
  734. * Tell other nodes
  735. */
  736. send_shutdown();
  737. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  738. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  739. res_lib_cfg_tryshutdown.header.error = CS_OK;
  740. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  741. sizeof(res_lib_cfg_tryshutdown));
  742. LEAVE();
  743. return;
  744. }
  745. /*
  746. * Shutdown in progress, return an error
  747. */
  748. if (shutdown_con) {
  749. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  750. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  751. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  752. res_lib_cfg_tryshutdown.header.error = CS_ERR_EXIST;
  753. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  754. sizeof(res_lib_cfg_tryshutdown));
  755. LEAVE();
  756. return;
  757. }
  758. ci->conn = conn;
  759. shutdown_con = (struct cfg_info *)api->ipc_private_data_get (conn);
  760. shutdown_flags = req_lib_cfg_tryshutdown->flags;
  761. shutdown_yes = 0;
  762. shutdown_no = 0;
  763. /*
  764. * Count the number of listeners
  765. */
  766. shutdown_expected = 0;
  767. qb_list_for_each(iter, &trackers_list) {
  768. struct cfg_info *testci = qb_list_entry(iter, struct cfg_info, list);
  769. /*
  770. * It is assumed that we will allow shutdown
  771. */
  772. if (testci != ci) {
  773. testci->shutdown_reply = SHUTDOWN_REPLY_UNKNOWN;
  774. shutdown_expected++;
  775. }
  776. }
  777. /*
  778. * If no-one is listening for events then we can just go down now
  779. */
  780. if (shutdown_expected == 0) {
  781. struct res_lib_cfg_tryshutdown res_lib_cfg_tryshutdown;
  782. res_lib_cfg_tryshutdown.header.size = sizeof(struct res_lib_cfg_tryshutdown);
  783. res_lib_cfg_tryshutdown.header.id = MESSAGE_RES_CFG_TRYSHUTDOWN;
  784. res_lib_cfg_tryshutdown.header.error = CS_OK;
  785. /*
  786. * Tell originator that shutdown was confirmed
  787. */
  788. api->ipc_response_send(conn, &res_lib_cfg_tryshutdown,
  789. sizeof(res_lib_cfg_tryshutdown));
  790. send_shutdown();
  791. LEAVE();
  792. return;
  793. }
  794. else {
  795. unsigned int shutdown_timeout = DEFAULT_SHUTDOWN_TIMEOUT;
  796. /*
  797. * Look for a shutdown timeout in configuration map
  798. */
  799. icmap_get_uint32("cfg.shutdown_timeout", &shutdown_timeout);
  800. /*
  801. * Start the timer. If we don't get a full set of replies before this goes
  802. * off we'll cancel the shutdown
  803. */
  804. api->timer_add_duration((unsigned long long)shutdown_timeout*1000000000, NULL,
  805. shutdown_timer_fn, &shutdown_timer);
  806. /*
  807. * Tell the users we would like to shut down
  808. */
  809. send_test_shutdown(NULL, conn, CS_OK);
  810. }
  811. /*
  812. * We don't sent a reply to the caller here.
  813. * We send it when we know if we can shut down or not
  814. */
  815. LEAVE();
  816. }
  817. static void message_handler_req_lib_cfg_replytoshutdown (
  818. void *conn,
  819. const void *msg)
  820. {
  821. struct cfg_info *ci = (struct cfg_info *)api->ipc_private_data_get (conn);
  822. const struct req_lib_cfg_replytoshutdown *req_lib_cfg_replytoshutdown = msg;
  823. struct res_lib_cfg_replytoshutdown res_lib_cfg_replytoshutdown;
  824. int status = CS_OK;
  825. ENTER();
  826. if (!shutdown_con) {
  827. status = CS_ERR_ACCESS;
  828. goto exit_fn;
  829. }
  830. if (req_lib_cfg_replytoshutdown->response) {
  831. shutdown_yes++;
  832. ci->shutdown_reply = SHUTDOWN_REPLY_YES;
  833. }
  834. else {
  835. shutdown_no++;
  836. ci->shutdown_reply = SHUTDOWN_REPLY_NO;
  837. }
  838. check_shutdown_status();
  839. exit_fn:
  840. res_lib_cfg_replytoshutdown.header.error = status;
  841. res_lib_cfg_replytoshutdown.header.id = MESSAGE_RES_CFG_REPLYTOSHUTDOWN;
  842. res_lib_cfg_replytoshutdown.header.size = sizeof(res_lib_cfg_replytoshutdown);
  843. api->ipc_response_send(conn, &res_lib_cfg_replytoshutdown,
  844. sizeof(res_lib_cfg_replytoshutdown));
  845. LEAVE();
  846. }
  847. static void message_handler_req_lib_cfg_get_node_addrs (void *conn,
  848. const void *msg)
  849. {
  850. struct totem_ip_address node_ifs[INTERFACE_MAX];
  851. unsigned int iface_ids[INTERFACE_MAX];
  852. char buf[PIPE_BUF];
  853. char **status;
  854. unsigned int num_interfaces = 0;
  855. struct sockaddr_storage *ss;
  856. int ret = CS_OK;
  857. int i;
  858. int live_addrs = 0;
  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. char *addr_buf;
  863. if (nodeid == 0)
  864. nodeid = api->totem_nodeid_get();
  865. if (api->totem_ifaces_get(nodeid, iface_ids, node_ifs, INTERFACE_MAX, &status, &num_interfaces)) {
  866. ret = CS_ERR_EXIST;
  867. num_interfaces = 0;
  868. }
  869. res_lib_cfg_get_node_addrs->header.size = sizeof(struct res_lib_cfg_get_node_addrs) + (num_interfaces * TOTEMIP_ADDRLEN);
  870. res_lib_cfg_get_node_addrs->header.id = MESSAGE_RES_CFG_GET_NODE_ADDRS;
  871. res_lib_cfg_get_node_addrs->header.error = ret;
  872. if (num_interfaces) {
  873. res_lib_cfg_get_node_addrs->family = node_ifs[0].family;
  874. for (i = 0, addr_buf = (char *)res_lib_cfg_get_node_addrs->addrs;
  875. i < num_interfaces; i++) {
  876. ss = (struct sockaddr_storage *)&node_ifs[i].addr;
  877. if (ss->ss_family) {
  878. memcpy(addr_buf, node_ifs[i].addr, TOTEMIP_ADDRLEN);
  879. live_addrs++;
  880. addr_buf += TOTEMIP_ADDRLEN;
  881. }
  882. }
  883. res_lib_cfg_get_node_addrs->num_addrs = live_addrs;
  884. } else {
  885. res_lib_cfg_get_node_addrs->header.error = CS_ERR_NOT_EXIST;
  886. }
  887. api->ipc_response_send(conn, res_lib_cfg_get_node_addrs, res_lib_cfg_get_node_addrs->header.size);
  888. }
  889. static void message_handler_req_lib_cfg_local_get (void *conn, const void *msg)
  890. {
  891. struct res_lib_cfg_local_get res_lib_cfg_local_get;
  892. res_lib_cfg_local_get.header.size = sizeof(res_lib_cfg_local_get);
  893. res_lib_cfg_local_get.header.id = MESSAGE_RES_CFG_LOCAL_GET;
  894. res_lib_cfg_local_get.header.error = CS_OK;
  895. res_lib_cfg_local_get.local_nodeid = api->totem_nodeid_get ();
  896. api->ipc_response_send(conn, &res_lib_cfg_local_get,
  897. sizeof(res_lib_cfg_local_get));
  898. }
  899. static void message_handler_req_lib_cfg_reload_config (void *conn, const void *msg)
  900. {
  901. struct req_exec_cfg_reload_config req_exec_cfg_reload_config;
  902. struct iovec iovec;
  903. ENTER();
  904. req_exec_cfg_reload_config.header.size =
  905. sizeof (struct req_exec_cfg_reload_config);
  906. req_exec_cfg_reload_config.header.id = SERVICE_ID_MAKE (CFG_SERVICE,
  907. MESSAGE_REQ_EXEC_CFG_RELOAD_CONFIG);
  908. api->ipc_source_set (&req_exec_cfg_reload_config.source, conn);
  909. api->ipc_refcnt_inc(conn);
  910. iovec.iov_base = (char *)&req_exec_cfg_reload_config;
  911. iovec.iov_len = sizeof (struct req_exec_cfg_reload_config);
  912. assert (api->totem_mcast (&iovec, 1, TOTEM_SAFE) == 0);
  913. LEAVE();
  914. }
  915. static void message_handler_req_lib_cfg_reopen_log_files (void *conn, const void *msg)
  916. {
  917. struct res_lib_cfg_reopen_log_files res_lib_cfg_reopen_log_files;
  918. cs_error_t res;
  919. ENTER();
  920. log_printf(LOGSYS_LEVEL_DEBUG, "Reopening logging files\n");
  921. res = logsys_reopen_log_files();
  922. res_lib_cfg_reopen_log_files.header.size = sizeof(res_lib_cfg_reopen_log_files);
  923. res_lib_cfg_reopen_log_files.header.id = MESSAGE_RES_CFG_REOPEN_LOG_FILES;
  924. res_lib_cfg_reopen_log_files.header.error = res;
  925. api->ipc_response_send(conn,
  926. &res_lib_cfg_reopen_log_files,
  927. sizeof(res_lib_cfg_reopen_log_files));
  928. LEAVE();
  929. }