vqmain.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. #include <config.h>
  2. #include <stdio.h>
  3. #include <sys/types.h>
  4. #include <sys/wait.h>
  5. #include <qb/qblog.h>
  6. #include <qb/qbloop.h>
  7. #include <sys/poll.h>
  8. #include <netinet/in.h>
  9. #include <sys/queue.h>
  10. #ifdef HAVE_READLINE_READLINE_H
  11. #include <readline/readline.h>
  12. #else
  13. #include <unistd.h> /* isatty */
  14. #endif
  15. #include "../exec/votequorum.h"
  16. #include "../exec/service.h"
  17. #include <corosync/logsys.h>
  18. #include <corosync/coroapi.h>
  19. #include "icmap.h"
  20. #include "vqsim.h"
  21. /* Easier than including the config file with a ton of conflicting dependencies */
  22. extern int coroparse_configparse (icmap_map_t config_map, const char **error_string);
  23. extern int corosync_log_config_read (const char **error_string);
  24. static int stdin_read_fn(int32_t fd, int32_t revents, void *data);
  25. /* 'Keep the compiler happy' time */
  26. const char *corosync_get_config_file(void);
  27. /* One of these per partition */
  28. struct vq_partition {
  29. TAILQ_HEAD(, vq_node) nodelist;
  30. struct memb_ring_id ring_id;
  31. int num;
  32. };
  33. /* One of these per node */
  34. struct vq_node {
  35. vq_object_t instance;
  36. unsigned int nodeid;
  37. int fd;
  38. struct vq_partition *partition;
  39. TAILQ_ENTRY(vq_node) entries;
  40. /* Last status */
  41. int last_quorate;
  42. struct memb_ring_id last_ring_id;
  43. int last_view_list[MAX_NODES];
  44. int last_view_list_entries;
  45. };
  46. static struct vq_partition partitions[MAX_PARTITIONS];
  47. static qb_loop_t *poll_loop;
  48. static int autofence;
  49. static int check_for_quorum;
  50. static FILE *output_file;
  51. static int sync_cmds = 1;
  52. static qb_loop_timer_handle kb_timer;
  53. static int waiting_for_sync = 0;
  54. static int is_tty;
  55. static int assert_on_timeout;
  56. static uint64_t command_timeout = 250000000L;
  57. static struct vq_node *find_by_pid(pid_t pid);
  58. static void send_partition_to_nodes(struct vq_partition *partition, int newring);
  59. static void start_kb_input_timeout(void *data);
  60. static void finish_wait_timeout(void *data);
  61. #ifndef HAVE_READLINE_READLINE_H
  62. #define INPUT_BUF_SIZE 1024
  63. static char input_buf[INPUT_BUF_SIZE];
  64. static size_t input_buf_term = 0;
  65. #endif
  66. /* 'Keep the compiler happy' time */
  67. static char corosync_config_file[PATH_MAX + 1] = COROSYSCONFDIR "/corosync.conf";
  68. const char *corosync_get_config_file(void)
  69. {
  70. return (corosync_config_file);
  71. }
  72. /* Tell all non-quorate nodes to quit */
  73. static void force_fence(void)
  74. {
  75. int i;
  76. struct vq_node *vqn;
  77. for (i=0; i<MAX_PARTITIONS; i++) {
  78. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  79. vq_quit_if_inquorate(vqn->instance);
  80. }
  81. }
  82. }
  83. /* Save quorum state from the incoming message */
  84. static void save_quorum_state(struct vq_node *node, struct vqsim_quorum_msg *qmsg)
  85. {
  86. node->last_quorate = qmsg->quorate;
  87. memcpy(&node->last_ring_id, &qmsg->ring_id, sizeof(struct memb_ring_id));
  88. memcpy(node->last_view_list, qmsg->view_list, sizeof(int) * qmsg->view_list_entries);
  89. node->last_view_list_entries = qmsg->view_list_entries;
  90. /* If at least one node is quorate and autofence is enabled, then fence everyone who is not quorate */
  91. if (check_for_quorum && qmsg->quorate & autofence) {
  92. check_for_quorum = 0;
  93. force_fence();
  94. }
  95. }
  96. /* Print current node state */
  97. static void print_quorum_state(struct vq_node *node)
  98. {
  99. int i;
  100. if (node->last_quorate < 0) {
  101. fprintf(output_file, "%d:" CS_PRI_NODE_ID ": q=UNINITIALIZED\n",
  102. node->partition->num, node->nodeid);
  103. return;
  104. }
  105. fprintf(output_file, "%d:" CS_PRI_NODE_ID ": q=%d ring=[" CS_PRI_RING_ID "] ", node->partition->num, node->nodeid, node->last_quorate,
  106. node->last_ring_id.nodeid, (uint64_t)node->last_ring_id.seq);
  107. fprintf(output_file, "nodes=[");
  108. for (i = 0; i < node->last_view_list_entries; i++) {
  109. if (i) {
  110. fprintf(output_file, " ");
  111. }
  112. fprintf(output_file, CS_PRI_NODE_ID, node->last_view_list[i]);
  113. }
  114. fprintf(output_file, "]\n");
  115. }
  116. static void propogate_vq_message(struct vq_node *vqn, const char *msg, int len)
  117. {
  118. struct vq_node *other_vqn;
  119. ssize_t write_res;
  120. /* Send it to everyone in that node's partition (including itself) */
  121. TAILQ_FOREACH(other_vqn, &vqn->partition->nodelist, entries) {
  122. write_res = write(other_vqn->fd, msg, len);
  123. /*
  124. * Read counterpart is not ready for receiving non-complete message so
  125. * ensure all required information was send.
  126. */
  127. assert(write_res == len);
  128. }
  129. }
  130. static void cmd_show_prompt_if_needed(void)
  131. {
  132. qb_loop_timer_del(poll_loop, kb_timer);
  133. if (is_tty) {
  134. printf("vqsim> ");
  135. fflush(stdout);
  136. } else {
  137. printf("#vqsim> ");
  138. fflush(stdout);
  139. }
  140. }
  141. void resume_kb_input(int show_status)
  142. {
  143. /* If running synchronously, we don't display
  144. the quorum messages as they come in. So run 'show' commamnd
  145. */
  146. if (show_status && waiting_for_sync) {
  147. cmd_show_node_states();
  148. }
  149. waiting_for_sync = 0;
  150. if (qb_loop_poll_add(poll_loop,
  151. QB_LOOP_MED,
  152. STDIN_FILENO,
  153. POLLIN | POLLERR,
  154. NULL,
  155. stdin_read_fn)) {
  156. if (errno != EEXIST) {
  157. perror("qb_loop_poll_add1 returned error");
  158. }
  159. }
  160. /* Always shows the prompt here, cos we cleared waiting_for_sync */
  161. cmd_show_prompt_if_needed();
  162. }
  163. /* Return true (1) if all nodes in each partition have the same ring id, false(0) otherwise */
  164. static int all_nodes_consistent(void)
  165. {
  166. int i;
  167. struct vq_node *vqn;
  168. struct memb_ring_id last_ring_id;
  169. for (i=0; i<MAX_PARTITIONS; i++) {
  170. memset(&last_ring_id, 0, sizeof(last_ring_id));
  171. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  172. if (last_ring_id.seq &&
  173. last_ring_id.seq != vqn->last_ring_id.seq) {
  174. return 0;
  175. }
  176. last_ring_id.seq = vqn->last_ring_id.seq;
  177. }
  178. }
  179. return 1;
  180. }
  181. static int vq_parent_read_fn(int32_t fd, int32_t revents, void *data)
  182. {
  183. char msgbuf[8192];
  184. int msglen;
  185. struct vqsim_msg_header *msg;
  186. struct vqsim_quorum_msg *qmsg;
  187. struct vq_node *vqn = data;
  188. if (revents == POLLIN) {
  189. msglen = read(fd, msgbuf, sizeof(msgbuf));
  190. if (msglen < 0) {
  191. perror("read failed");
  192. } else if (msglen < sizeof(*msg)) {
  193. fprintf(stderr, "Received message is too short\n");
  194. } else {
  195. msg = (void*)msgbuf;
  196. switch (msg->type) {
  197. case VQMSG_QUORUM:
  198. qmsg = (void*)msgbuf;
  199. /*
  200. * Check length of message.
  201. * SOCK_SEQPACKET is used so this check is not strictly needed.
  202. */
  203. if (msglen < sizeof(*qmsg) ||
  204. qmsg->view_list_entries > MAX_NODES ||
  205. msglen < sizeof(*qmsg) + sizeof(qmsg->view_list[0]) * qmsg->view_list_entries) {
  206. fprintf(stderr, "Received quorum message is too short or corrupted\n");
  207. return (0);
  208. }
  209. save_quorum_state(vqn, qmsg);
  210. if (!sync_cmds) {
  211. print_quorum_state(vqn);
  212. }
  213. /* Have the partitions stabilised? */
  214. if (sync_cmds && waiting_for_sync &&
  215. all_nodes_consistent()) {
  216. qb_loop_timer_del(poll_loop, kb_timer);
  217. resume_kb_input(sync_cmds);
  218. }
  219. break;
  220. case VQMSG_EXEC:
  221. /* Message from votequorum, pass around the partition */
  222. propogate_vq_message(vqn, msgbuf, msglen);
  223. break;
  224. case VQMSG_QUIT:
  225. case VQMSG_SYNC:
  226. case VQMSG_QDEVICE:
  227. case VQMSG_QUORUMQUIT:
  228. /* not used here */
  229. break;
  230. }
  231. }
  232. }
  233. if (revents == POLLERR) {
  234. fprintf(stderr, "pollerr on " CS_PRI_NODE_ID "\n", vqn->nodeid);
  235. }
  236. return 0;
  237. }
  238. static int read_corosync_conf(void)
  239. {
  240. int res;
  241. const char *error_string;
  242. int err = icmap_init();
  243. if (!err) {
  244. fprintf(stderr, "icmap_init failed\n");
  245. }
  246. /* Load corosync.conf */
  247. logsys_format_set(NULL);
  248. res = coroparse_configparse(icmap_get_global_map(), &error_string);
  249. if (res == -1) {
  250. log_printf (LOGSYS_LEVEL_INFO, "Error loading corosync.conf %s", error_string);
  251. return -1;
  252. }
  253. else {
  254. res = corosync_log_config_read (&error_string);
  255. if (res < 0) {
  256. log_printf (LOGSYS_LEVEL_INFO, "error reading log config %s", error_string);
  257. syslog (LOGSYS_LEVEL_INFO, "error reading log config %s", error_string);
  258. }
  259. else {
  260. logsys_config_apply();
  261. }
  262. }
  263. if (logsys_thread_start() != 0) {
  264. log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
  265. return -1;
  266. }
  267. return 0;
  268. }
  269. static void remove_node(struct vq_node *node)
  270. {
  271. struct vq_partition *part;
  272. part = node->partition;
  273. /* Remove from partition list */
  274. TAILQ_REMOVE(&part->nodelist, node, entries);
  275. free(node);
  276. /* Rebuild quorum */
  277. send_partition_to_nodes(part, 1);
  278. }
  279. static int32_t sigchld_handler(int32_t sig, void *data)
  280. {
  281. pid_t pid;
  282. int status;
  283. struct vq_node *vqn;
  284. const char *exit_status="";
  285. char text[132];
  286. pid = wait(&status);
  287. if (WIFEXITED(status)) {
  288. vqn = find_by_pid(pid);
  289. if (vqn) {
  290. switch (WEXITSTATUS(status)) {
  291. case 0:
  292. exit_status = "(on request)";
  293. break;
  294. case 1:
  295. exit_status = "(autofenced)";
  296. break;
  297. default:
  298. sprintf(text, "(exit code %d)", WEXITSTATUS(status));
  299. break;
  300. }
  301. printf("%d:" CS_PRI_NODE_ID ": Quit %s\n", vqn->partition->num, vqn->nodeid, exit_status);
  302. remove_node(vqn);
  303. }
  304. else {
  305. fprintf(stderr, "Unknown child %d exited with status %d\n", pid, WEXITSTATUS(status));
  306. }
  307. }
  308. if (WIFSIGNALED(status)) {
  309. vqn = find_by_pid(pid);
  310. if (vqn) {
  311. printf("%d:" CS_PRI_NODE_ID " exited on signal %d%s\n", vqn->partition->num, vqn->nodeid, WTERMSIG(status), WCOREDUMP(status)?" (core dumped)":"");
  312. remove_node(vqn);
  313. }
  314. else {
  315. fprintf(stderr, "Unknown child %d exited with status %d%s\n", pid, WTERMSIG(status), WCOREDUMP(status)?" (core dumped)":"");
  316. }
  317. }
  318. return 0;
  319. }
  320. static void send_partition_to_nodes(struct vq_partition *partition, int newring)
  321. {
  322. struct vq_node *vqn;
  323. int nodelist[MAX_NODES];
  324. int nodes = 0;
  325. int first = 1;
  326. if (newring) {
  327. /* Simulate corosync incrementing the seq by 4 for added authenticity */
  328. partition->ring_id.seq += 4;
  329. }
  330. /* Build the node list */
  331. TAILQ_FOREACH(vqn, &partition->nodelist, entries) {
  332. nodelist[nodes++] = vqn->nodeid;
  333. if (first) {
  334. partition->ring_id.nodeid = vqn->nodeid;
  335. first = 0;
  336. }
  337. }
  338. TAILQ_FOREACH(vqn, &partition->nodelist, entries) {
  339. vq_set_nodelist(vqn->instance, &partition->ring_id, nodelist, nodes);
  340. }
  341. }
  342. static void init_partitions(void)
  343. {
  344. int i;
  345. for (i=0; i<MAX_PARTITIONS; i++) {
  346. TAILQ_INIT(&partitions[i].nodelist);
  347. partitions[i].ring_id.nodeid = 1000+i;
  348. partitions[i].ring_id.seq = 0;
  349. partitions[i].num = i;
  350. }
  351. }
  352. static pid_t create_node(int nodeid, int partno)
  353. {
  354. struct vq_node *newvq;
  355. newvq = malloc(sizeof(struct vq_node));
  356. if (newvq) {
  357. newvq->last_quorate = -1; /* mark "uninitialized" */
  358. newvq->instance = vq_create_instance(poll_loop, nodeid);
  359. if (!newvq->instance) {
  360. fprintf(stderr,
  361. "ERR: could not create vq instance nodeid " CS_PRI_NODE_ID "\n",
  362. nodeid);
  363. free(newvq);
  364. return (pid_t) -1;
  365. }
  366. newvq->partition = &partitions[partno];
  367. newvq->nodeid = nodeid;
  368. newvq->fd = vq_get_parent_fd(newvq->instance);
  369. TAILQ_INSERT_TAIL(&partitions[partno].nodelist, newvq, entries);
  370. if (qb_loop_poll_add(poll_loop,
  371. QB_LOOP_MED,
  372. newvq->fd,
  373. POLLIN | POLLERR,
  374. newvq,
  375. vq_parent_read_fn)) {
  376. perror("qb_loop_poll_add returned error");
  377. return (pid_t) -1;
  378. }
  379. /* Send sync with all the nodes so far in it. */
  380. send_partition_to_nodes(&partitions[partno], 1);
  381. return vq_get_pid(newvq->instance);
  382. }
  383. return (pid_t) -1;
  384. }
  385. static size_t create_nodes_from_config(void)
  386. {
  387. icmap_iter_t iter;
  388. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  389. uint32_t node_pos;
  390. uint32_t nodeid;
  391. const char *iter_key;
  392. int res;
  393. pid_t pid;
  394. size_t ret = 0;
  395. init_partitions();
  396. iter = icmap_iter_init("nodelist.node.");
  397. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  398. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  399. if (res != 2) {
  400. continue;
  401. }
  402. if (strcmp(tmp_key, "ring0_addr") != 0) {
  403. continue;
  404. }
  405. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  406. if (icmap_get_uint32(tmp_key, &nodeid) == CS_OK) {
  407. pid = create_node(nodeid, 0);
  408. if (pid == (pid_t) -1) {
  409. fprintf(stderr,
  410. "ERR: nodeid " CS_PRI_NODE_ID " could not be spawned\n",
  411. nodeid);
  412. exit(1);
  413. }
  414. ret++;
  415. }
  416. }
  417. icmap_iter_finalize(iter);
  418. return ret;
  419. }
  420. static struct vq_node *find_node(int nodeid)
  421. {
  422. int i;
  423. struct vq_node *vqn;
  424. for (i=0; i<MAX_PARTITIONS; i++) {
  425. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  426. if (vqn->nodeid == nodeid) {
  427. return vqn;
  428. }
  429. }
  430. }
  431. return NULL;
  432. }
  433. static struct vq_node *find_by_pid(pid_t pid)
  434. {
  435. int i;
  436. struct vq_node *vqn;
  437. for (i=0; i<MAX_PARTITIONS; i++) {
  438. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  439. if (vq_get_pid(vqn->instance) == pid) {
  440. return vqn;
  441. }
  442. }
  443. }
  444. return NULL;
  445. }
  446. /* Routines called from the parser */
  447. /*
  448. * The parser calls this before running a command where
  449. * we might have to wait for a result to come back.
  450. */
  451. void cmd_start_sync_command()
  452. {
  453. if (sync_cmds) {
  454. qb_loop_poll_del(poll_loop, STDIN_FILENO);
  455. qb_loop_timer_add(poll_loop,
  456. QB_LOOP_MED,
  457. command_timeout,
  458. NULL,
  459. finish_wait_timeout,
  460. &kb_timer);
  461. waiting_for_sync = 1;
  462. }
  463. }
  464. int cmd_start_new_node(int nodeid, int partition)
  465. {
  466. struct vq_node *node;
  467. node = find_node(nodeid);
  468. if (node) {
  469. fprintf(stderr, "ERR: nodeid " CS_PRI_NODE_ID " already exists in partition %d\n", nodeid, node->partition->num);
  470. return -1;
  471. }
  472. if (create_node(nodeid, partition) == -1) {
  473. return -1;
  474. }
  475. return 0;
  476. }
  477. void cmd_stop_all_nodes()
  478. {
  479. int i;
  480. struct vq_node *vqn;
  481. for (i=0; i<MAX_PARTITIONS; i++) {
  482. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  483. vq_quit(vqn->instance);
  484. }
  485. }
  486. }
  487. void cmd_show_node_states()
  488. {
  489. int i;
  490. struct vq_node *vqn;
  491. for (i=0; i<MAX_PARTITIONS; i++) {
  492. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  493. print_quorum_state(vqn);
  494. }
  495. }
  496. fprintf(output_file, "#autofence: %s\n", autofence?"on":"off");
  497. }
  498. int cmd_stop_node(int nodeid)
  499. {
  500. struct vq_node *node;
  501. node = find_node(nodeid);
  502. if (!node) {
  503. fprintf(stderr, "ERR: nodeid " CS_PRI_NODE_ID " is not up\n", nodeid);
  504. return -1;
  505. }
  506. /* Remove processor */
  507. vq_quit(node->instance);
  508. /* Node will be removed when the child process exits */
  509. return 0;
  510. }
  511. /* Move all nodes in 'nodelist' into partition 'partition' */
  512. void cmd_move_nodes(int partition, int num_nodes, int *nodelist)
  513. {
  514. int i;
  515. struct vq_node *node;
  516. struct vq_node *vqn;
  517. int total_nodes = num_nodes;
  518. /* Work out the number of nodes affected */
  519. TAILQ_FOREACH(vqn, &partitions[partition].nodelist, entries) {
  520. total_nodes++;
  521. }
  522. for (i=0; i<num_nodes; i++) {
  523. node = find_node(nodelist[i]);
  524. if (node) {
  525. /* Remove it from the current partition */
  526. TAILQ_REMOVE(&node->partition->nodelist, node, entries);
  527. /* Add it to the new partition */
  528. TAILQ_INSERT_TAIL(&partitions[partition].nodelist, node, entries);
  529. node->partition = &partitions[partition];
  530. }
  531. else {
  532. printf("ERR: node " CS_PRI_NODE_ID " does not exist\n", nodelist[i]);
  533. }
  534. }
  535. }
  536. /* Take all the nodes in part2 and join them to part1 */
  537. void cmd_join_partitions(int part1, int part2)
  538. {
  539. struct vq_node *vqn;
  540. while (!TAILQ_EMPTY(&partitions[part2].nodelist)) {
  541. vqn = TAILQ_FIRST(&partitions[part2].nodelist);
  542. TAILQ_REMOVE(&vqn->partition->nodelist, vqn, entries);
  543. TAILQ_INSERT_TAIL(&partitions[part1].nodelist, vqn, entries);
  544. vqn->partition = &partitions[part1];
  545. }
  546. }
  547. void cmd_set_autofence(int onoff)
  548. {
  549. autofence = onoff;
  550. fprintf(output_file, "#autofence: %s\n", onoff?"on":"off");
  551. }
  552. void cmd_set_sync(int onoff)
  553. {
  554. autofence = onoff;
  555. fprintf(output_file, "#sync: %s\n", onoff?"on":"off");
  556. sync_cmds = onoff;
  557. }
  558. void cmd_set_assert(int onoff)
  559. {
  560. assert_on_timeout = onoff;
  561. }
  562. void cmd_update_all_partitions(int newring)
  563. {
  564. int i;
  565. check_for_quorum = 1;
  566. for (i=0; i<MAX_PARTITIONS; i++) {
  567. send_partition_to_nodes(&partitions[i], newring);
  568. }
  569. }
  570. void cmd_qdevice_poll(int nodeid, int onoff)
  571. {
  572. struct vq_node *node;
  573. node = find_node(nodeid);
  574. if (node) {
  575. vq_set_qdevice(node->instance, &node->partition->ring_id, onoff);
  576. }
  577. }
  578. /* If we get called then a command has timed-out */
  579. static void finish_wait_timeout(void *data)
  580. {
  581. if (command_timeout) {
  582. fprintf(stderr, "ERR: Partition(s) not stable within timeout\n");
  583. if (assert_on_timeout) {
  584. exit(2);
  585. }
  586. }
  587. resume_kb_input(sync_cmds);
  588. }
  589. void cmd_set_timeout(uint64_t seconds)
  590. {
  591. command_timeout = seconds * QB_TIME_NS_IN_MSEC;
  592. }
  593. /* ---------------------------------- */
  594. #ifndef HAVE_READLINE_READLINE_H
  595. static void dummy_read_char(void);
  596. static void dummy_read_char()
  597. {
  598. int c, flush = 0;
  599. while (!flush) {
  600. c = getchar();
  601. if (++input_buf_term >= INPUT_BUF_SIZE) {
  602. if (c != '\n' && c != EOF)
  603. fprintf(stderr, "User input overflows the limit: %zu\n",
  604. (size_t) INPUT_BUF_SIZE);
  605. input_buf[INPUT_BUF_SIZE - 1] = '\0';
  606. flush = 1;
  607. } else if (c == '\n' || c == EOF) {
  608. input_buf[input_buf_term - 1] = '\0';
  609. flush = 1;
  610. } else {
  611. input_buf[input_buf_term - 1] = c;
  612. }
  613. }
  614. parse_input_command((c == EOF) ? NULL : input_buf);
  615. input_buf_term = 0;
  616. }
  617. #endif
  618. static int stdin_read_fn(int32_t fd, int32_t revents, void *data)
  619. {
  620. #ifdef HAVE_READLINE_READLINE_H
  621. /* Send it to readline */
  622. rl_callback_read_char();
  623. #else
  624. dummy_read_char();
  625. #endif
  626. return 0;
  627. }
  628. static void start_kb_input_timeout(void *data)
  629. {
  630. resume_kb_input(1);
  631. }
  632. static void usage(char *program)
  633. {
  634. printf("Usage:\n");
  635. printf("\n");
  636. printf("%s [-c <config-file>] [-o <output-file>]\n", program);
  637. printf("\n");
  638. printf(" -c config file. defaults to /etc/corosync/corosync.conf\n");
  639. printf(" -o output file. defaults to stdout\n");
  640. printf(" -n no synchronization (on adding a node)\n");
  641. printf(" -h display this help text\n");
  642. printf("\n");
  643. printf("%s always takes input from STDIN, but cannot use a file.\n", program);
  644. printf("If you want to script it then use\n cat | %s\n", program);
  645. printf("\n");
  646. }
  647. int main(int argc, char **argv)
  648. {
  649. qb_loop_signal_handle sigchld_qb_handle;
  650. int ch;
  651. char *output_file_name = NULL;
  652. while ((ch = getopt (argc, argv, "c:o:nh")) != EOF) {
  653. switch (ch) {
  654. case 'c':
  655. if (strlen(optarg) >= sizeof(sizeof(corosync_config_file) - 1)) {
  656. fprintf(stderr, "Corosync config file path too long\n");
  657. exit(1);
  658. }
  659. strncpy(corosync_config_file, optarg, sizeof(corosync_config_file) - 1);
  660. break;
  661. case 'o':
  662. output_file_name = optarg;
  663. break;
  664. case 'n':
  665. sync_cmds = 0;
  666. break;
  667. default:
  668. usage(argv[0]);
  669. exit(0);
  670. }
  671. }
  672. if (output_file_name) {
  673. output_file = fopen(output_file_name, "w");
  674. if (!output_file) {
  675. fprintf(stderr, "Unable to open %s for output: %s\n", output_file_name, strerror(errno));
  676. exit(3);
  677. }
  678. }
  679. else {
  680. output_file = stdout;
  681. }
  682. is_tty = isatty(STDIN_FILENO);
  683. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD,
  684. QB_LOG_FILTER_FUNCTION, "*", LOG_DEBUG);
  685. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  686. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  687. QB_LOG_FILTER_FUNCTION, "*", LOG_DEBUG);
  688. poll_loop = qb_loop_create();
  689. /* SIGCHLD handler to reap sub-processes and reconfigure the cluster */
  690. qb_loop_signal_add(poll_loop,
  691. QB_LOOP_MED,
  692. SIGCHLD,
  693. NULL,
  694. sigchld_handler,
  695. &sigchld_qb_handle);
  696. #ifdef HAVE_READLINE_READLINE_H
  697. /* Readline will deal with completed lines when they arrive */
  698. /*
  699. * For scripting add '#' to the start of the prompt so that
  700. * parsers can ignore input lines
  701. */
  702. rl_already_prompted = 1;
  703. if (is_tty) {
  704. rl_callback_handler_install("vqsim> ", parse_input_command);
  705. } else {
  706. rl_callback_handler_install("#vqsim> ", parse_input_command);
  707. }
  708. #endif
  709. /* Create a full cluster of nodes from corosync.conf */
  710. read_corosync_conf();
  711. if (create_nodes_from_config() && sync_cmds) {
  712. /* Delay kb input handling by 1 second when we've just
  713. added the nodes from corosync.conf; expect that
  714. the delay will be cancelled substantially earlier
  715. once they all have reported their quorum info
  716. (the delay is in fact a failsafe input enabler here) */
  717. qb_loop_timer_add(poll_loop,
  718. QB_LOOP_MED,
  719. 1000000000,
  720. NULL,
  721. start_kb_input_timeout,
  722. &kb_timer);
  723. waiting_for_sync = 1;
  724. } else {
  725. resume_kb_input(0);
  726. }
  727. qb_loop_run(poll_loop);
  728. return 0;
  729. }