corosync-quorumtool.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * Copyright (c) 2009-2012 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Authors: Christine Caulfield <ccaulfie@redhat.com>
  7. * Fabio M. Di Nitto (fdinitto@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 Red Hat 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 <stdio.h>
  37. #include <string.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <netdb.h>
  41. #include <limits.h>
  42. #include <corosync/totem/totem.h>
  43. #include <corosync/cfg.h>
  44. #include <corosync/cmap.h>
  45. #include <corosync/quorum.h>
  46. #include <corosync/votequorum.h>
  47. typedef enum {
  48. NODEID_FORMAT_DECIMAL,
  49. NODEID_FORMAT_HEX
  50. } nodeid_format_t;
  51. typedef enum {
  52. ADDRESS_FORMAT_NAME,
  53. ADDRESS_FORMAT_IP
  54. } name_format_t;
  55. typedef enum {
  56. CMD_UNKNOWN,
  57. CMD_SHOWNODES,
  58. CMD_SHOWSTATUS,
  59. CMD_SETVOTES,
  60. CMD_SETEXPECTED,
  61. CMD_MONITOR,
  62. CMD_UNREGISTER_QDEVICE
  63. } command_t;
  64. /*
  65. * global vars
  66. */
  67. /*
  68. * cmap bits
  69. */
  70. static cmap_handle_t cmap_handle;
  71. /*
  72. * quorum bits
  73. */
  74. static void quorum_notification_fn(
  75. quorum_handle_t handle,
  76. uint32_t quorate,
  77. uint64_t ring_id,
  78. uint32_t view_list_entries,
  79. uint32_t *view_list);
  80. static quorum_handle_t q_handle;
  81. static uint32_t q_type;
  82. static quorum_callbacks_t q_callbacks = {
  83. .quorum_notify_fn = quorum_notification_fn
  84. };
  85. /*
  86. * quorum call back vars
  87. */
  88. static uint32_t g_quorate;
  89. static uint64_t g_ring_id;
  90. static uint32_t g_view_list_entries;
  91. static uint32_t *g_view_list = NULL;
  92. static uint32_t g_called;
  93. /*
  94. * votequorum bits
  95. */
  96. static votequorum_handle_t v_handle;
  97. static votequorum_callbacks_t v_callbacks = {
  98. .votequorum_notify_fn = NULL,
  99. .votequorum_expectedvotes_notify_fn = NULL
  100. };
  101. static uint32_t our_nodeid = 0;
  102. /*
  103. * cfg bits
  104. */
  105. static corosync_cfg_handle_t c_handle;
  106. static corosync_cfg_callbacks_t c_callbacks = {
  107. .corosync_cfg_shutdown_callback = NULL
  108. };
  109. /*
  110. * global
  111. */
  112. static int machine_parsable = 0;
  113. static void show_usage(const char *name)
  114. {
  115. printf("usage: \n");
  116. printf("%s <options>\n", name);
  117. printf("\n");
  118. printf(" options:\n");
  119. printf("\n");
  120. printf(" -s show quorum status\n");
  121. printf(" -m monitor quorum status\n");
  122. printf(" -l list nodes\n");
  123. printf(" -p when used with -s or -l, generates machine parsable output\n");
  124. printf(" -v <votes> change the number of votes for a node (*)\n");
  125. printf(" -n <nodeid> optional nodeid of node for -v (*)\n");
  126. printf(" -e <expected> change expected votes for the cluster (*)\n");
  127. printf(" -H show nodeids in hexadecimal rather than decimal\n");
  128. printf(" -i show node IP addresses instead of the resolved name\n");
  129. printf(" -f forcefully unregister a quorum device *DANGEROUS* (*)\n");
  130. printf(" -h show this help text\n");
  131. printf(" -V show version and exit\n");
  132. printf("\n");
  133. printf(" (*) Starred items only work if votequorum is the quorum provider for corosync\n");
  134. printf("\n");
  135. }
  136. static int get_quorum_type(char *quorum_type, size_t quorum_type_len)
  137. {
  138. int err;
  139. char *str = NULL;
  140. if ((!quorum_type) || (quorum_type_len <= 0)) {
  141. return -1;
  142. }
  143. if (q_type == QUORUM_FREE) {
  144. return -1;
  145. }
  146. if ((err = cmap_get_string(cmap_handle, "quorum.provider", &str)) != CS_OK) {
  147. goto out;
  148. }
  149. if (!str) {
  150. return -1;
  151. }
  152. strncpy(quorum_type, str, quorum_type_len - 1);
  153. free(str);
  154. return 0;
  155. out:
  156. return err;
  157. }
  158. /*
  159. * Returns 1 if 'votequorum' is active. The called then knows that
  160. * votequorum calls should work and can provide extra information
  161. */
  162. static int using_votequorum(void)
  163. {
  164. char quorumtype[256];
  165. int using_voteq;
  166. memset(quorumtype, 0, sizeof(quorumtype));
  167. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  168. return -1;
  169. }
  170. if (strcmp(quorumtype, "corosync_votequorum") == 0) {
  171. using_voteq = 1;
  172. } else {
  173. using_voteq = 0;
  174. }
  175. return using_voteq;
  176. }
  177. static int set_votes(uint32_t nodeid, int votes)
  178. {
  179. int err;
  180. if ((err=votequorum_setvotes(v_handle, nodeid, votes)) != CS_OK) {
  181. fprintf(stderr, "Unable to set votes %d for nodeid: %u: %s\n",
  182. votes, nodeid, cs_strerror(err));
  183. }
  184. return err==CS_OK?0:err;
  185. }
  186. static int set_expected(int expected_votes)
  187. {
  188. int err;
  189. if ((err=votequorum_setexpected(v_handle, expected_votes)) != CS_OK) {
  190. fprintf(stderr, "Unable to set expected votes: %s\n", cs_strerror(err));
  191. }
  192. return err==CS_OK?0:err;
  193. }
  194. /*
  195. * node name by nodelist
  196. */
  197. static const char *node_name_by_nodelist(uint32_t nodeid)
  198. {
  199. cmap_iter_handle_t iter;
  200. char key_name[CMAP_KEYNAME_MAXLEN];
  201. char tmp_key[CMAP_KEYNAME_MAXLEN];
  202. static char ret_buf[_POSIX_HOST_NAME_MAX];
  203. char *str = NULL;
  204. uint32_t node_pos, cur_nodeid;
  205. int res = 0;
  206. if (cmap_iter_init(cmap_handle, "nodelist.node.", &iter) != CS_OK) {
  207. return "";
  208. }
  209. memset(ret_buf, 0, sizeof(ret_buf));
  210. while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
  211. res = sscanf(key_name, "nodelist.node.%u.%s", &node_pos, tmp_key);
  212. if (res != 2) {
  213. continue;
  214. }
  215. if (strcmp(tmp_key, "ring0_addr") != 0) {
  216. continue;
  217. }
  218. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  219. if (cmap_get_uint32(cmap_handle, tmp_key, &cur_nodeid) != CS_OK) {
  220. continue;
  221. }
  222. if (cur_nodeid != nodeid) {
  223. continue;
  224. }
  225. snprintf(tmp_key, CMAP_KEYNAME_MAXLEN, "nodelist.node.%u.ring0_addr", node_pos);
  226. if (cmap_get_string(cmap_handle, tmp_key, &str) != CS_OK) {
  227. continue;
  228. }
  229. if (!str) {
  230. continue;
  231. }
  232. strncpy(ret_buf, str, sizeof(ret_buf) - 1);
  233. free(str);
  234. break;
  235. }
  236. cmap_iter_finalize(cmap_handle, iter);
  237. return ret_buf;
  238. }
  239. /*
  240. * This resolves the first address assigned to a node
  241. * and returns the name or IP address. Use cfgtool if you need more information.
  242. */
  243. static const char *node_name(uint32_t nodeid, name_format_t name_format)
  244. {
  245. int err;
  246. int numaddrs;
  247. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  248. static char buf[INET6_ADDRSTRLEN];
  249. const char *nodelist_name = NULL;
  250. socklen_t addrlen;
  251. struct sockaddr_storage *ss;
  252. if (name_format == ADDRESS_FORMAT_NAME) {
  253. nodelist_name = node_name_by_nodelist(nodeid);
  254. }
  255. if ((nodelist_name) &&
  256. (strlen(nodelist_name) > 0)) {
  257. return nodelist_name;
  258. }
  259. err = corosync_cfg_get_node_addrs(c_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs);
  260. if (err != CS_OK) {
  261. fprintf(stderr, "Unable to get node address for nodeid %u: %s\n", nodeid, cs_strerror(err));
  262. return "";
  263. }
  264. ss = (struct sockaddr_storage *)addrs[0].address;
  265. if (ss->ss_family == AF_INET6) {
  266. addrlen = sizeof(struct sockaddr_in6);
  267. } else {
  268. addrlen = sizeof(struct sockaddr_in);
  269. }
  270. if (!getnameinfo(
  271. (struct sockaddr *)addrs[0].address, addrlen,
  272. buf, sizeof(buf),
  273. NULL, 0,
  274. (name_format == ADDRESS_FORMAT_IP)?NI_NUMERICHOST:0)) {
  275. return buf;
  276. }
  277. return "";
  278. }
  279. static void quorum_notification_fn(
  280. quorum_handle_t handle,
  281. uint32_t quorate,
  282. uint64_t ring_id,
  283. uint32_t view_list_entries,
  284. uint32_t *view_list)
  285. {
  286. g_called = 1;
  287. g_quorate = quorate;
  288. g_ring_id = ring_id;
  289. g_view_list_entries = view_list_entries;
  290. if (g_view_list) {
  291. free(g_view_list);
  292. }
  293. g_view_list = malloc(sizeof(uint32_t) * view_list_entries);
  294. if (g_view_list) {
  295. memcpy(g_view_list, view_list,sizeof(uint32_t) * view_list_entries);
  296. }
  297. }
  298. static void print_string_padded(const char *buf)
  299. {
  300. int len, delta;
  301. len = strlen(buf);
  302. delta = 10 - len;
  303. while (delta > 0) {
  304. printf(" ");
  305. delta--;
  306. }
  307. printf("%s ", buf);
  308. }
  309. static void print_uint32_padded(uint32_t value)
  310. {
  311. char buf[12];
  312. snprintf(buf, sizeof(buf) - 1, "%u", value);
  313. print_string_padded(buf);
  314. }
  315. static void display_nodes_data(nodeid_format_t nodeid_format, name_format_t name_format)
  316. {
  317. int i, display_qdevice = 0;
  318. struct votequorum_info info[g_view_list_entries];
  319. /*
  320. * cache node info because we need to parse them twice
  321. */
  322. if (v_handle) {
  323. for (i=0; i < g_view_list_entries; i++) {
  324. if (votequorum_getinfo(v_handle, g_view_list[i], &info[i]) != CS_OK) {
  325. printf("Unable to get node %u info\n", g_view_list[i]);
  326. }
  327. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  328. display_qdevice = 1;
  329. }
  330. }
  331. }
  332. printf("\nMembership information\n");
  333. printf("----------------------\n");
  334. print_string_padded("Nodeid");
  335. if (v_handle) {
  336. print_string_padded("Votes");
  337. if ((display_qdevice) || (machine_parsable)) {
  338. print_string_padded("Qdevice");
  339. }
  340. }
  341. printf("Name\n");
  342. for (i=0; i < g_view_list_entries; i++) {
  343. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  344. print_uint32_padded(g_view_list[i]);
  345. } else {
  346. printf("0x%08x ", g_view_list[i]);
  347. }
  348. if (v_handle) {
  349. int votes = -1;
  350. votes = info[i].node_votes;
  351. print_uint32_padded(votes);
  352. if ((display_qdevice) || (machine_parsable)) {
  353. if (info[i].flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) {
  354. char buf[10];
  355. snprintf(buf, sizeof(buf) - 1,
  356. "%s,%s,%s",
  357. info[i].flags & VOTEQUORUM_INFO_QDEVICE_ALIVE?"A":"NA",
  358. info[i].flags & VOTEQUORUM_INFO_QDEVICE_CAST_VOTE?"V":"NV",
  359. info[i].flags & VOTEQUORUM_INFO_QDEVICE_MASTER_WINS?"MW":"NMW");
  360. print_string_padded(buf);
  361. } else {
  362. print_string_padded("NR");
  363. }
  364. }
  365. }
  366. printf("%s", node_name(g_view_list[i], name_format));
  367. if (g_view_list[i] == our_nodeid) {
  368. printf(" (local)");
  369. }
  370. printf("\n");
  371. }
  372. if (g_view_list_entries) {
  373. free(g_view_list);
  374. g_view_list = NULL;
  375. }
  376. if (display_qdevice) {
  377. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  378. print_uint32_padded(VOTEQUORUM_QDEVICE_NODEID);
  379. } else {
  380. printf("0x%08x ", VOTEQUORUM_QDEVICE_NODEID);
  381. }
  382. print_uint32_padded(info[0].qdevice_votes);
  383. printf(" %s\n", info[0].qdevice_name);
  384. }
  385. }
  386. static int display_quorum_data(int is_quorate,
  387. nodeid_format_t nodeid_format, name_format_t name_format,
  388. int loop)
  389. {
  390. struct votequorum_info info;
  391. int err;
  392. char quorumtype[256];
  393. time_t t;
  394. memset(quorumtype, 0, sizeof(quorumtype));
  395. printf("Quorum information\n");
  396. printf("------------------\n");
  397. time(&t);
  398. printf("Date: %s", ctime((const time_t *)&t));
  399. if (get_quorum_type(quorumtype, sizeof(quorumtype))) {
  400. strncpy(quorumtype, "Not configured", sizeof(quorumtype) - 1);
  401. }
  402. printf("Quorum provider: %s\n", quorumtype);
  403. printf("Nodes: %d\n", g_view_list_entries);
  404. if (nodeid_format == NODEID_FORMAT_DECIMAL) {
  405. printf("Node ID: %u\n", our_nodeid);
  406. } else {
  407. printf("Node ID: 0x%08x\n", our_nodeid);
  408. }
  409. printf("Ring ID: %" PRIu64 "\n", g_ring_id);
  410. printf("Quorate: %s\n", is_quorate?"Yes":"No");
  411. if (!v_handle) {
  412. return CS_OK;
  413. }
  414. err=votequorum_getinfo(v_handle, our_nodeid, &info);
  415. if ((err == CS_OK) || (err == CS_ERR_NOT_EXIST)) {
  416. printf("\nVotequorum information\n");
  417. printf("----------------------\n");
  418. printf("Expected votes: %d\n", info.node_expected_votes);
  419. printf("Highest expected: %d\n", info.highest_expected);
  420. printf("Total votes: %d\n", info.total_votes);
  421. printf("Quorum: %d %s\n", info.quorum, info.flags & VOTEQUORUM_INFO_QUORATE?" ":"Activity blocked");
  422. printf("Flags: ");
  423. if (info.flags & VOTEQUORUM_INFO_TWONODE) printf("2Node ");
  424. if (info.flags & VOTEQUORUM_INFO_QUORATE) printf("Quorate ");
  425. if (info.flags & VOTEQUORUM_INFO_WAIT_FOR_ALL) printf("WaitForAll ");
  426. if (info.flags & VOTEQUORUM_INFO_LAST_MAN_STANDING) printf("LastManStanding ");
  427. if (info.flags & VOTEQUORUM_INFO_AUTO_TIE_BREAKER) printf("AutoTieBreaker ");
  428. if (info.flags & VOTEQUORUM_INFO_ALLOW_DOWNSCALE) printf("AllowDownscale ");
  429. if (info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED) printf("Qdevice ");
  430. printf("\n");
  431. } else {
  432. fprintf(stderr, "Unable to get node info: %s\n", cs_strerror(err));
  433. }
  434. display_nodes_data(nodeid_format, name_format);
  435. return err;
  436. }
  437. /*
  438. * return 1 if quorate
  439. * 0 if not quorate
  440. * -1 on error
  441. */
  442. static int show_status(nodeid_format_t nodeid_format, name_format_t name_format)
  443. {
  444. int is_quorate;
  445. int err;
  446. err=quorum_getquorate(q_handle, &is_quorate);
  447. if (err != CS_OK) {
  448. fprintf(stderr, "Unable to get cluster quorate status: %s\n", cs_strerror(err));
  449. goto quorum_err;
  450. }
  451. err=quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  452. if (err != CS_OK) {
  453. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  454. goto quorum_err;
  455. }
  456. g_called = 0;
  457. while (g_called == 0 && err == CS_OK) {
  458. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  459. if (err != CS_OK) {
  460. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  461. }
  462. }
  463. if (quorum_trackstop(q_handle) != CS_OK) {
  464. fprintf(stderr, "Unable to stop quorum status tracking: %s\n", cs_strerror(err));
  465. }
  466. quorum_err:
  467. if (err != CS_OK) {
  468. return -1;
  469. }
  470. err = display_quorum_data(is_quorate, nodeid_format, name_format, 0);
  471. if (err != CS_OK) {
  472. return -1;
  473. }
  474. return is_quorate;
  475. }
  476. static int monitor_status(nodeid_format_t nodeid_format, name_format_t name_format) {
  477. int err;
  478. int loop = 0;
  479. if (q_type == QUORUM_FREE) {
  480. printf("\nQuorum is not configured - cannot monitor\n");
  481. return show_status(nodeid_format, name_format);
  482. }
  483. err=quorum_trackstart(q_handle, CS_TRACK_CHANGES);
  484. if (err != CS_OK) {
  485. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  486. goto quorum_err;
  487. }
  488. while (1) {
  489. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  490. if (err != CS_OK) {
  491. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  492. goto quorum_err;
  493. }
  494. err = display_quorum_data(g_quorate, nodeid_format, name_format, loop);
  495. printf("\n");
  496. loop = 1;
  497. if (err != CS_OK) {
  498. fprintf(stderr, "Unable to display quorum data: %s\n", cs_strerror(err));
  499. goto quorum_err;
  500. }
  501. }
  502. quorum_err:
  503. return -1;
  504. }
  505. static int show_nodes(nodeid_format_t nodeid_format, name_format_t name_format)
  506. {
  507. int err;
  508. int result = EXIT_FAILURE;
  509. err = quorum_trackstart(q_handle, CS_TRACK_CURRENT);
  510. if (err != CS_OK) {
  511. fprintf(stderr, "Unable to start quorum status tracking: %s\n", cs_strerror(err));
  512. goto err_exit;
  513. }
  514. g_called = 0;
  515. while (g_called == 0) {
  516. err = quorum_dispatch(q_handle, CS_DISPATCH_ONE);
  517. if (err != CS_OK) {
  518. fprintf(stderr, "Unable to dispatch quorum status: %s\n", cs_strerror(err));
  519. goto err_exit;
  520. }
  521. }
  522. display_nodes_data(nodeid_format, name_format);
  523. result = EXIT_SUCCESS;
  524. err_exit:
  525. return result;
  526. }
  527. static int unregister_qdevice(void)
  528. {
  529. int err;
  530. struct votequorum_info info;
  531. err = votequorum_getinfo(v_handle, our_nodeid, &info);
  532. if (err != CS_OK) {
  533. fprintf(stderr, "Unable to get quorum device info: %s\n", cs_strerror(err));
  534. return -1;
  535. }
  536. if (!(info.flags & VOTEQUORUM_INFO_QDEVICE_REGISTERED)) {
  537. return 0;
  538. }
  539. err = votequorum_qdevice_unregister(v_handle, info.qdevice_name);
  540. if (err != CS_OK) {
  541. fprintf(stderr, "Unable to unregister quorum device: %s\n", cs_strerror(err));
  542. return -1;
  543. }
  544. return 0;
  545. }
  546. /*
  547. * return -1 on error
  548. * 0 if OK
  549. */
  550. static int init_all(void) {
  551. cmap_handle = 0;
  552. q_handle = 0;
  553. v_handle = 0;
  554. c_handle = 0;
  555. if (cmap_initialize(&cmap_handle) != CS_OK) {
  556. fprintf(stderr, "Cannot initialize CMAP service\n");
  557. cmap_handle = 0;
  558. goto out;
  559. }
  560. if (quorum_initialize(&q_handle, &q_callbacks, &q_type) != CS_OK) {
  561. fprintf(stderr, "Cannot initialize QUORUM service\n");
  562. q_handle = 0;
  563. goto out;
  564. }
  565. if (corosync_cfg_initialize(&c_handle, &c_callbacks) != CS_OK) {
  566. fprintf(stderr, "Cannot initialise CFG service\n");
  567. c_handle = 0;
  568. goto out;
  569. }
  570. if (using_votequorum() <= 0) {
  571. return 0;
  572. }
  573. if (votequorum_initialize(&v_handle, &v_callbacks) != CS_OK) {
  574. fprintf(stderr, "Cannot initialise VOTEQUORUM service\n");
  575. v_handle = 0;
  576. goto out;
  577. }
  578. if (cmap_get_uint32(cmap_handle, "runtime.votequorum.this_node_id", &our_nodeid) != CS_OK) {
  579. fprintf(stderr, "Unable to retrive this node nodeid\n");
  580. goto out;
  581. }
  582. return 0;
  583. out:
  584. return -1;
  585. }
  586. static void close_all(void) {
  587. if (cmap_handle) {
  588. cmap_finalize(cmap_handle);
  589. }
  590. if (q_handle) {
  591. quorum_finalize(q_handle);
  592. }
  593. if (c_handle) {
  594. corosync_cfg_finalize(c_handle);
  595. }
  596. if (v_handle) {
  597. votequorum_finalize(v_handle);
  598. }
  599. }
  600. int main (int argc, char *argv[]) {
  601. const char *options = "VHslpmfe:v:hin:";
  602. char *endptr;
  603. int opt;
  604. int votes = 0;
  605. int ret = 0;
  606. uint32_t nodeid = 0;
  607. uint32_t nodeid_set = 0;
  608. nodeid_format_t nodeid_format = NODEID_FORMAT_DECIMAL;
  609. name_format_t address_format = ADDRESS_FORMAT_NAME;
  610. command_t command_opt = CMD_SHOWSTATUS;
  611. if (init_all()) {
  612. close_all();
  613. exit(1);
  614. }
  615. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  616. switch (opt) {
  617. case 'f':
  618. if (using_votequorum() > 0) {
  619. command_opt = CMD_UNREGISTER_QDEVICE;
  620. } else {
  621. fprintf(stderr, "You cannot unregister quorum device, corosync is not using votequorum\n");
  622. exit(2);
  623. }
  624. break;
  625. case 's':
  626. command_opt = CMD_SHOWSTATUS;
  627. break;
  628. case 'm':
  629. command_opt = CMD_MONITOR;
  630. break;
  631. case 'i':
  632. address_format = ADDRESS_FORMAT_IP;
  633. break;
  634. case 'H':
  635. nodeid_format = NODEID_FORMAT_HEX;
  636. break;
  637. case 'l':
  638. command_opt = CMD_SHOWNODES;
  639. break;
  640. case 'p':
  641. machine_parsable = 1;
  642. break;
  643. case 'e':
  644. if (using_votequorum() > 0) {
  645. votes = strtol(optarg, &endptr, 0);
  646. if ((votes == 0 && endptr == optarg) || votes <= 0) {
  647. fprintf(stderr, "New expected votes value was not valid, try a positive number\n");
  648. } else {
  649. command_opt = CMD_SETEXPECTED;
  650. }
  651. } else {
  652. fprintf(stderr, "You cannot change expected votes, corosync is not using votequorum\n");
  653. exit(2);
  654. }
  655. break;
  656. case 'n':
  657. nodeid = strtol(optarg, &endptr, 0);
  658. if ((nodeid == 0 && endptr == optarg) || nodeid < 0) {
  659. fprintf(stderr, "The nodeid was not valid, try a positive number\n");
  660. exit(2);
  661. }
  662. nodeid_set = 1;
  663. break;
  664. case 'v':
  665. if (using_votequorum() > 0) {
  666. votes = strtol(optarg, &endptr, 0);
  667. if ((votes == 0 && endptr == optarg) || votes < 0) {
  668. fprintf(stderr, "New votes value was not valid, try a positive number or zero\n");
  669. exit(2);
  670. } else {
  671. command_opt = CMD_SETVOTES;
  672. }
  673. }
  674. else {
  675. fprintf(stderr, "You cannot change node votes, corosync is not using votequorum\n");
  676. exit(2);
  677. }
  678. break;
  679. case 'V':
  680. printf("corosync-quorumtool version: %s\n", VERSION);
  681. exit(0);
  682. case ':':
  683. case 'h':
  684. case '?':
  685. default:
  686. command_opt = CMD_UNKNOWN;
  687. break;
  688. }
  689. }
  690. switch (command_opt) {
  691. case CMD_UNKNOWN:
  692. show_usage(argv[0]);
  693. ret = -1;
  694. break;
  695. case CMD_SHOWNODES:
  696. ret = show_nodes(nodeid_format, address_format);
  697. break;
  698. case CMD_SHOWSTATUS:
  699. ret = show_status(nodeid_format, address_format);
  700. break;
  701. case CMD_SETVOTES:
  702. if (!nodeid_set) {
  703. nodeid = our_nodeid;
  704. }
  705. ret = set_votes(nodeid, votes);
  706. break;
  707. case CMD_SETEXPECTED:
  708. ret = set_expected(votes);
  709. break;
  710. case CMD_MONITOR:
  711. ret = monitor_status(nodeid_format, address_format);
  712. break;
  713. case CMD_UNREGISTER_QDEVICE:
  714. ret = unregister_qdevice();
  715. break;
  716. }
  717. close_all();
  718. return (ret);
  719. }