corosync-quorumtool.c 20 KB

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