4
0

vqmain.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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:%02d: q=UNINITIALIZED\n",
  102. node->partition->num, node->nodeid);
  103. return;
  104. }
  105. fprintf(output_file, "%d:%02d: q=%d ring=[%d/%lld] ", node->partition->num, node->nodeid, node->last_quorate,
  106. node->last_ring_id.nodeid, 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, "%d", 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. }
  193. if (msglen > 0) {
  194. msg = (void*)msgbuf;
  195. switch (msg->type) {
  196. case VQMSG_QUORUM:
  197. qmsg = (void*)msgbuf;
  198. save_quorum_state(vqn, qmsg);
  199. if (!sync_cmds) {
  200. print_quorum_state(vqn);
  201. }
  202. /* Have the partitions stabilised? */
  203. if (sync_cmds && waiting_for_sync &&
  204. all_nodes_consistent()) {
  205. qb_loop_timer_del(poll_loop, kb_timer);
  206. resume_kb_input(sync_cmds);
  207. }
  208. break;
  209. case VQMSG_EXEC:
  210. /* Message from votequorum, pass around the partition */
  211. propogate_vq_message(vqn, msgbuf, msglen);
  212. break;
  213. case VQMSG_QUIT:
  214. case VQMSG_SYNC:
  215. case VQMSG_QDEVICE:
  216. case VQMSG_QUORUMQUIT:
  217. /* not used here */
  218. break;
  219. }
  220. }
  221. }
  222. if (revents == POLLERR) {
  223. fprintf(stderr, "pollerr on %d\n", vqn->nodeid);
  224. }
  225. return 0;
  226. }
  227. static int read_corosync_conf(void)
  228. {
  229. int res;
  230. const char *error_string;
  231. int err = icmap_init();
  232. if (!err) {
  233. fprintf(stderr, "icmap_init failed\n");
  234. }
  235. /* Load corosync.conf */
  236. logsys_format_set(NULL);
  237. res = coroparse_configparse(icmap_get_global_map(), &error_string);
  238. if (res == -1) {
  239. log_printf (LOGSYS_LEVEL_INFO, "Error loading corosync.conf %s", error_string);
  240. return -1;
  241. }
  242. else {
  243. res = corosync_log_config_read (&error_string);
  244. if (res < 0) {
  245. log_printf (LOGSYS_LEVEL_INFO, "error reading log config %s", error_string);
  246. syslog (LOGSYS_LEVEL_INFO, "error reading log config %s", error_string);
  247. }
  248. else {
  249. logsys_config_apply();
  250. }
  251. }
  252. if (logsys_thread_start() != 0) {
  253. log_printf (LOGSYS_LEVEL_ERROR, "Can't initialize log thread");
  254. return -1;
  255. }
  256. return 0;
  257. }
  258. static void remove_node(struct vq_node *node)
  259. {
  260. struct vq_partition *part;
  261. part = node->partition;
  262. /* Remove from partition list */
  263. TAILQ_REMOVE(&part->nodelist, node, entries);
  264. free(node);
  265. /* Rebuild quorum */
  266. send_partition_to_nodes(part, 1);
  267. }
  268. static int32_t sigchld_handler(int32_t sig, void *data)
  269. {
  270. pid_t pid;
  271. int status;
  272. struct vq_node *vqn;
  273. const char *exit_status="";
  274. char text[132];
  275. pid = wait(&status);
  276. if (WIFEXITED(status)) {
  277. vqn = find_by_pid(pid);
  278. if (vqn) {
  279. switch (WEXITSTATUS(status)) {
  280. case 0:
  281. exit_status = "(on request)";
  282. break;
  283. case 1:
  284. exit_status = "(autofenced)";
  285. break;
  286. default:
  287. sprintf(text, "(exit code %d)", WEXITSTATUS(status));
  288. break;
  289. }
  290. printf("%d:%02d: Quit %s\n", vqn->partition->num, vqn->nodeid, exit_status);
  291. remove_node(vqn);
  292. }
  293. else {
  294. fprintf(stderr, "Unknown child %d exited with status %d\n", pid, WEXITSTATUS(status));
  295. }
  296. }
  297. if (WIFSIGNALED(status)) {
  298. vqn = find_by_pid(pid);
  299. if (vqn) {
  300. printf("%d:%02d exited on signal %d%s\n", vqn->partition->num, vqn->nodeid, WTERMSIG(status), WCOREDUMP(status)?" (core dumped)":"");
  301. remove_node(vqn);
  302. }
  303. else {
  304. fprintf(stderr, "Unknown child %d exited with status %d%s\n", pid, WTERMSIG(status), WCOREDUMP(status)?" (core dumped)":"");
  305. }
  306. }
  307. return 0;
  308. }
  309. static void send_partition_to_nodes(struct vq_partition *partition, int newring)
  310. {
  311. struct vq_node *vqn;
  312. int nodelist[MAX_NODES];
  313. int nodes = 0;
  314. int first = 1;
  315. if (newring) {
  316. /* Simulate corosync incrementing the seq by 4 for added authenticity */
  317. partition->ring_id.seq += 4;
  318. }
  319. /* Build the node list */
  320. TAILQ_FOREACH(vqn, &partition->nodelist, entries) {
  321. nodelist[nodes++] = vqn->nodeid;
  322. if (first) {
  323. partition->ring_id.nodeid = vqn->nodeid;
  324. first = 0;
  325. }
  326. }
  327. TAILQ_FOREACH(vqn, &partition->nodelist, entries) {
  328. vq_set_nodelist(vqn->instance, &partition->ring_id, nodelist, nodes);
  329. }
  330. }
  331. static void init_partitions(void)
  332. {
  333. int i;
  334. for (i=0; i<MAX_PARTITIONS; i++) {
  335. TAILQ_INIT(&partitions[i].nodelist);
  336. partitions[i].ring_id.nodeid = 1000+i;
  337. partitions[i].ring_id.seq = 0;
  338. partitions[i].num = i;
  339. }
  340. }
  341. static int nodes_in_partition(int part)
  342. {
  343. struct vq_node *vqn;
  344. int partnodes = 0;
  345. TAILQ_FOREACH(vqn, &partitions[part].nodelist, entries) {
  346. partnodes++;
  347. }
  348. return partnodes;
  349. }
  350. static pid_t create_node(int nodeid, int partno)
  351. {
  352. struct vq_node *newvq;
  353. newvq = malloc(sizeof(struct vq_node));
  354. if (newvq) {
  355. newvq->last_quorate = -1; /* mark "uninitialized" */
  356. newvq->instance = vq_create_instance(poll_loop, nodeid);
  357. if (!newvq->instance) {
  358. fprintf(stderr,
  359. "ERR: could not create vq instance nodeid %d\n",
  360. nodeid);
  361. return (pid_t) -1;
  362. }
  363. newvq->partition = &partitions[partno];
  364. newvq->nodeid = nodeid;
  365. newvq->fd = vq_get_parent_fd(newvq->instance);
  366. TAILQ_INSERT_TAIL(&partitions[partno].nodelist, newvq, entries);
  367. if (qb_loop_poll_add(poll_loop,
  368. QB_LOOP_MED,
  369. newvq->fd,
  370. POLLIN | POLLERR,
  371. newvq,
  372. vq_parent_read_fn)) {
  373. perror("qb_loop_poll_add returned error");
  374. return (pid_t) -1;
  375. }
  376. /* Send sync with all the nodes so far in it. */
  377. send_partition_to_nodes(&partitions[partno], 1);
  378. return vq_get_pid(newvq->instance);
  379. }
  380. return (pid_t) -1;
  381. }
  382. static size_t create_nodes_from_config(void)
  383. {
  384. icmap_iter_t iter;
  385. char tmp_key[ICMAP_KEYNAME_MAXLEN];
  386. uint32_t node_pos;
  387. uint32_t nodeid;
  388. const char *iter_key;
  389. int res;
  390. pid_t pid;
  391. size_t ret = 0;
  392. init_partitions();
  393. iter = icmap_iter_init("nodelist.node.");
  394. while ((iter_key = icmap_iter_next(iter, NULL, NULL)) != NULL) {
  395. res = sscanf(iter_key, "nodelist.node.%u.%s", &node_pos, tmp_key);
  396. if (res != 2) {
  397. continue;
  398. }
  399. if (strcmp(tmp_key, "ring0_addr") != 0) {
  400. continue;
  401. }
  402. snprintf(tmp_key, ICMAP_KEYNAME_MAXLEN, "nodelist.node.%u.nodeid", node_pos);
  403. if (icmap_get_uint32(tmp_key, &nodeid) == CS_OK) {
  404. pid = create_node(nodeid, 0);
  405. if (pid == (pid_t) -1) {
  406. fprintf(stderr,
  407. "ERR: nodeid %d could not be spawned\n",
  408. nodeid);
  409. exit(1);
  410. }
  411. ret++;
  412. }
  413. }
  414. icmap_iter_finalize(iter);
  415. return ret;
  416. }
  417. static struct vq_node *find_node(int nodeid)
  418. {
  419. int i;
  420. struct vq_node *vqn;
  421. for (i=0; i<MAX_PARTITIONS; i++) {
  422. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  423. if (vqn->nodeid == nodeid) {
  424. return vqn;
  425. }
  426. }
  427. }
  428. return NULL;
  429. }
  430. static struct vq_node *find_by_pid(pid_t pid)
  431. {
  432. int i;
  433. struct vq_node *vqn;
  434. for (i=0; i<MAX_PARTITIONS; i++) {
  435. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  436. if (vq_get_pid(vqn->instance) == pid) {
  437. return vqn;
  438. }
  439. }
  440. }
  441. return NULL;
  442. }
  443. /* Routines called from the parser */
  444. /*
  445. * The parser calls this before running a command where
  446. * we might have to wait for a result to come back.
  447. */
  448. void cmd_start_sync_command()
  449. {
  450. if (sync_cmds) {
  451. qb_loop_poll_del(poll_loop, STDIN_FILENO);
  452. qb_loop_timer_add(poll_loop,
  453. QB_LOOP_MED,
  454. command_timeout,
  455. NULL,
  456. finish_wait_timeout,
  457. &kb_timer);
  458. waiting_for_sync = 1;
  459. }
  460. }
  461. int cmd_start_new_node(int nodeid, int partition)
  462. {
  463. struct vq_node *node;
  464. node = find_node(nodeid);
  465. if (node) {
  466. fprintf(stderr, "ERR: nodeid %d already exists in partition %d\n", nodeid, node->partition->num);
  467. return -1;
  468. }
  469. if (create_node(nodeid, partition) == -1) {
  470. return -1;
  471. }
  472. return 0;
  473. }
  474. void cmd_stop_all_nodes()
  475. {
  476. int i;
  477. struct vq_node *vqn;
  478. for (i=0; i<MAX_PARTITIONS; i++) {
  479. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  480. vq_quit(vqn->instance);
  481. }
  482. }
  483. }
  484. void cmd_show_node_states()
  485. {
  486. int i;
  487. struct vq_node *vqn;
  488. for (i=0; i<MAX_PARTITIONS; i++) {
  489. TAILQ_FOREACH(vqn, &partitions[i].nodelist, entries) {
  490. print_quorum_state(vqn);
  491. }
  492. }
  493. fprintf(output_file, "#autofence: %s\n", autofence?"on":"off");
  494. }
  495. int cmd_stop_node(int nodeid)
  496. {
  497. struct vq_node *node;
  498. node = find_node(nodeid);
  499. if (!node) {
  500. fprintf(stderr, "ERR: nodeid %d is not up\n", nodeid);
  501. return -1;
  502. }
  503. /* Remove processor */
  504. vq_quit(node->instance);
  505. /* Node will be removed when the child process exits */
  506. return 0;
  507. }
  508. /* Move all nodes in 'nodelist' into partition 'partition' */
  509. void cmd_move_nodes(int partition, int num_nodes, int *nodelist)
  510. {
  511. int i;
  512. struct vq_node *node;
  513. struct vq_node *vqn;
  514. int total_nodes = num_nodes;
  515. /* Work out the number of nodes affected */
  516. TAILQ_FOREACH(vqn, &partitions[partition].nodelist, entries) {
  517. total_nodes++;
  518. }
  519. for (i=0; i<num_nodes; i++) {
  520. node = find_node(nodelist[i]);
  521. if (node) {
  522. /* Remove it from the current partition */
  523. TAILQ_REMOVE(&node->partition->nodelist, node, entries);
  524. /* Add it to the new partition */
  525. TAILQ_INSERT_TAIL(&partitions[partition].nodelist, node, entries);
  526. node->partition = &partitions[partition];
  527. }
  528. else {
  529. printf("ERR: node %d does not exist\n", nodelist[i]);
  530. }
  531. }
  532. }
  533. /* Take all the nodes in part2 and join them to part1 */
  534. void cmd_join_partitions(int part1, int part2)
  535. {
  536. struct vq_node *vqn;
  537. int total_nodes=0;
  538. /* Work out the number of nodes affected */
  539. total_nodes += nodes_in_partition(part1);
  540. total_nodes += nodes_in_partition(part2);
  541. /* TAILQ_FOREACH is not delete safe *sigh* */
  542. retry:
  543. TAILQ_FOREACH(vqn, &partitions[part2].nodelist, entries) {
  544. TAILQ_REMOVE(&vqn->partition->nodelist, vqn, entries);
  545. TAILQ_INSERT_TAIL(&partitions[part1].nodelist, vqn, entries);
  546. vqn->partition = &partitions[part1];
  547. goto retry;
  548. }
  549. }
  550. void cmd_set_autofence(int onoff)
  551. {
  552. autofence = onoff;
  553. fprintf(output_file, "#autofence: %s\n", onoff?"on":"off");
  554. }
  555. void cmd_set_sync(int onoff)
  556. {
  557. autofence = onoff;
  558. fprintf(output_file, "#sync: %s\n", onoff?"on":"off");
  559. sync_cmds = onoff;
  560. }
  561. void cmd_set_assert(int onoff)
  562. {
  563. assert_on_timeout = onoff;
  564. }
  565. void cmd_update_all_partitions(int newring)
  566. {
  567. int i;
  568. check_for_quorum = 1;
  569. for (i=0; i<MAX_PARTITIONS; i++) {
  570. send_partition_to_nodes(&partitions[i], newring);
  571. }
  572. }
  573. void cmd_qdevice_poll(int nodeid, int onoff)
  574. {
  575. struct vq_node *node;
  576. node = find_node(nodeid);
  577. if (node) {
  578. vq_set_qdevice(node->instance, &node->partition->ring_id, onoff);
  579. }
  580. }
  581. /* If we get called then a command has timed-out */
  582. static void finish_wait_timeout(void *data)
  583. {
  584. if (command_timeout) {
  585. fprintf(stderr, "ERR: Partition(s) not stable within timeout\n");
  586. if (assert_on_timeout) {
  587. exit(2);
  588. }
  589. }
  590. resume_kb_input(sync_cmds);
  591. }
  592. void cmd_set_timeout(uint64_t seconds)
  593. {
  594. command_timeout = seconds * QB_TIME_NS_IN_MSEC;
  595. }
  596. /* ---------------------------------- */
  597. #ifndef HAVE_READLINE_READLINE_H
  598. static void dummy_read_char(void);
  599. static void dummy_read_char()
  600. {
  601. int c, flush = 0;
  602. while (!flush) {
  603. c = getchar();
  604. if (++input_buf_term >= INPUT_BUF_SIZE) {
  605. if (c != '\n' && c != EOF)
  606. fprintf(stderr, "User input overflows the limit: %zu\n",
  607. (size_t) INPUT_BUF_SIZE);
  608. input_buf[INPUT_BUF_SIZE - 1] = '\0';
  609. flush = 1;
  610. } else if (c == '\n' || c == EOF) {
  611. input_buf[input_buf_term - 1] = '\0';
  612. flush = 1;
  613. } else {
  614. input_buf[input_buf_term - 1] = c;
  615. }
  616. }
  617. parse_input_command((c == EOF) ? NULL : input_buf);
  618. input_buf_term = 0;
  619. }
  620. #endif
  621. static int stdin_read_fn(int32_t fd, int32_t revents, void *data)
  622. {
  623. #ifdef HAVE_READLINE_READLINE_H
  624. /* Send it to readline */
  625. rl_callback_read_char();
  626. #else
  627. dummy_read_char();
  628. #endif
  629. return 0;
  630. }
  631. static void start_kb_input_timeout(void *data)
  632. {
  633. resume_kb_input(1);
  634. }
  635. static void usage(char *program)
  636. {
  637. printf("Usage:\n");
  638. printf("\n");
  639. printf("%s [-c <config-file>] [-o <output-file>]\n", program);
  640. printf("\n");
  641. printf(" -c config file. defaults to /etc/corosync/corosync.conf\n");
  642. printf(" -o output file. defaults to stdout\n");
  643. printf(" -n no synchronization (on adding a node)\n");
  644. printf(" -h display this help text\n");
  645. printf("\n");
  646. printf("%s always takes input from STDIN, but cannot use a file.\n", program);
  647. printf("If you want to script it then use\n cat | %s\n", program);
  648. printf("\n");
  649. }
  650. int main(int argc, char **argv)
  651. {
  652. qb_loop_signal_handle sigchld_qb_handle;
  653. int ch;
  654. char *output_file_name = NULL;
  655. while ((ch = getopt (argc, argv, "c:o:nh")) != EOF) {
  656. switch (ch) {
  657. case 'c':
  658. strncpy(corosync_config_file, optarg, sizeof(corosync_config_file));
  659. break;
  660. case 'o':
  661. output_file_name = optarg;
  662. break;
  663. case 'n':
  664. sync_cmds = 0;
  665. break;
  666. default:
  667. usage(argv[0]);
  668. exit(0);
  669. }
  670. }
  671. if (output_file_name) {
  672. output_file = fopen(output_file_name, "w");
  673. if (!output_file) {
  674. fprintf(stderr, "Unable to open %s for output: %s\n", output_file_name, strerror(errno));
  675. exit(-1);
  676. }
  677. }
  678. else {
  679. output_file = stdout;
  680. }
  681. is_tty = isatty(STDIN_FILENO);
  682. qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD,
  683. QB_LOG_FILTER_FUNCTION, "*", LOG_DEBUG);
  684. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  685. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  686. QB_LOG_FILTER_FUNCTION, "*", LOG_DEBUG);
  687. poll_loop = qb_loop_create();
  688. /* SIGCHLD handler to reap sub-processes and reconfigure the cluster */
  689. qb_loop_signal_add(poll_loop,
  690. QB_LOOP_MED,
  691. SIGCHLD,
  692. NULL,
  693. sigchld_handler,
  694. &sigchld_qb_handle);
  695. #ifdef HAVE_READLINE_READLINE_H
  696. /* Readline will deal with completed lines when they arrive */
  697. /*
  698. * For scripting add '#' to the start of the prompt so that
  699. * parsers can ignore input lines
  700. */
  701. rl_already_prompted = 1;
  702. if (is_tty) {
  703. rl_callback_handler_install("vqsim> ", parse_input_command);
  704. } else {
  705. rl_callback_handler_install("#vqsim> ", parse_input_command);
  706. }
  707. #endif
  708. /* Create a full cluster of nodes from corosync.conf */
  709. read_corosync_conf();
  710. if (create_nodes_from_config() && sync_cmds) {
  711. /* Delay kb input handling by 1 second when we've just
  712. added the nodes from corosync.conf; expect that
  713. the delay will be cancelled substantially earlier
  714. once they all have reported their quorum info
  715. (the delay is in fact a failsafe input enabler here) */
  716. qb_loop_timer_add(poll_loop,
  717. QB_LOOP_MED,
  718. 1000000000,
  719. NULL,
  720. start_kb_input_timeout,
  721. &kb_timer);
  722. waiting_for_sync = 1;
  723. } else {
  724. resume_kb_input(0);
  725. }
  726. qb_loop_run(poll_loop);
  727. return 0;
  728. }