4
0

parser.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Parses the interactive commands */
  2. #include <config.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <netinet/in.h>
  7. #ifdef HAVE_READLINE_HISTORY_H
  8. #include <readline/history.h>
  9. #endif
  10. #include <corosync/coroapi.h>
  11. #include "vqsim.h"
  12. static void do_usage(void)
  13. {
  14. printf(" All node IDs in the cluster are unique and belong to a numbered 'partition' (default=0)\n");
  15. printf("\n");
  16. printf("up [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
  17. printf(" bring node(s) online in the specified partition(s)\n");
  18. printf("down <nodeid>,[<nodeid>...]\n");
  19. printf(" send nodes offline (shut them down)\n");
  20. printf("move/split [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
  21. printf(" Move nodes from one partition to another (netsplit)\n");
  22. printf(" <partition> here is the partition to move the nodes to\n");
  23. printf("join <partition> <partition> [<partition>] ... \n");
  24. printf(" Join partitions together (reverse of a netsplit)\n");
  25. printf("qdevice on|off [<partition>:][<nodeid>[,<nodeid>] ...] [[<partition>:][<nodeid>...]] [...]\n");
  26. printf(" Enable quorum device in specified nodes\n");
  27. printf("autofence on|off\n");
  28. printf(" automatically 'down' nodes on inquorate side on netsplit\n");
  29. printf("show Show current nodes status\n");
  30. printf("exit\n\n");
  31. }
  32. typedef void (*cmd_routine_t)(int argc, char **argv);
  33. static void run_up_cmd(int argc, char **argv);
  34. static void run_down_cmd(int argc, char **argv);
  35. static void run_join_cmd(int argc, char **argv);
  36. static void run_move_cmd(int argc, char **argv);
  37. static void run_exit_cmd(int argc, char **argv);
  38. static void run_show_cmd(int argc, char **argv);
  39. static void run_autofence_cmd(int argc, char **argv);
  40. static void run_qdevice_cmd(int argc, char **argv);
  41. static struct cmd_list_struct {
  42. const char *cmd;
  43. int min_args;
  44. cmd_routine_t cmd_runner;
  45. } cmd_list[] = {
  46. { "up", 1, run_up_cmd},
  47. { "down", 1, run_down_cmd},
  48. { "move", 2, run_move_cmd},
  49. { "split", 2, run_move_cmd},
  50. { "join", 2, run_join_cmd},
  51. { "autofence", 1, run_autofence_cmd},
  52. { "qdevice", 1, run_qdevice_cmd},
  53. { "show", 0, run_show_cmd},
  54. { "exit", 0, run_exit_cmd},
  55. { "quit", 0, run_exit_cmd},
  56. { "q", 0, run_exit_cmd},
  57. };
  58. static int num_cmds = (sizeof(cmd_list)) / sizeof(struct cmd_list_struct);
  59. #define MAX_ARGS 1024
  60. /* Takes a <partition>:[<node>[,<node>]...] list and return it
  61. as a partition and a list of nodes.
  62. Returns 0 if successful, -1 if not
  63. */
  64. static int parse_partition_nodelist(char *string, int *partition, int *num_nodes, int **retnodes)
  65. {
  66. int i;
  67. int nodecount;
  68. int len;
  69. int last_comma;
  70. char *nodeptr;
  71. int *nodes;
  72. char *colonptr = strchr(string, ':');
  73. if (colonptr) {
  74. *colonptr = '\0';
  75. nodeptr = colonptr+1;
  76. *partition = atoi(string);
  77. }
  78. else {
  79. /* Default to partition 0 */
  80. *partition = 0;
  81. nodeptr = string;
  82. }
  83. /* Count the number of commas and allocate space for the nodes */
  84. nodecount = 0;
  85. for (i=0; i<strlen(nodeptr); i++) {
  86. if (nodeptr[i] == ',') {
  87. nodecount++;
  88. }
  89. }
  90. nodecount++; /* The one between the last comma and the trailing NUL */
  91. if (nodecount < 1 || nodecount > MAX_NODES) {
  92. return -1;
  93. }
  94. nodes = malloc(sizeof(int) * nodecount);
  95. if (!nodes) {
  96. return -1;
  97. }
  98. nodecount = 0;
  99. last_comma = 0;
  100. len = strlen(nodeptr);
  101. for (i=0; i<=len; i++) {
  102. if (nodeptr[i] == ',' || nodeptr[i] == '\0') {
  103. nodeptr[i] = '\0';
  104. nodes[nodecount++] = atoi(&nodeptr[last_comma]);
  105. last_comma = i+1;
  106. }
  107. }
  108. *num_nodes = nodecount;
  109. *retnodes = nodes;
  110. return 0;
  111. }
  112. void parse_input_command(char *rl_cmd)
  113. {
  114. int i;
  115. int argc = 0;
  116. int valid_cmd = 0;
  117. char *argv[MAX_ARGS];
  118. int last_arg_start = 0;
  119. int last_was_space = 0;
  120. int len;
  121. char *cmd;
  122. /* ^D quits */
  123. if (rl_cmd == NULL) {
  124. run_exit_cmd(0, NULL);
  125. }
  126. cmd = strdup(rl_cmd);
  127. /* Split cmd up into args
  128. * destroying the original string mwahahahaha
  129. */
  130. len = strlen(cmd);
  131. /* Span leading spaces */
  132. for (i=0; cmd[i] == ' '; i++)
  133. ;
  134. last_arg_start = i;
  135. for (; i<=len; i++) {
  136. if (cmd[i] == ' ' || cmd[i] == '\0') {
  137. /* Allow multiple spaces */
  138. if (last_was_space) {
  139. continue;
  140. }
  141. cmd[i] = '\0';
  142. last_was_space = 1;
  143. argv[argc] = &cmd[last_arg_start];
  144. argc++;
  145. }
  146. else {
  147. if (last_was_space) {
  148. last_arg_start = i;
  149. }
  150. last_was_space = 0;
  151. }
  152. }
  153. /* Ignore null commands */
  154. if (strlen(argv[0]) == 0) {
  155. free(cmd);
  156. return;
  157. }
  158. #ifdef HAVE_READLINE_HISTORY_H
  159. add_history(rl_cmd);
  160. #endif
  161. /* Dispatch command */
  162. for (i=0; i<num_cmds; i++) {
  163. if (strcasecmp(argv[0], cmd_list[i].cmd) == 0) {
  164. if (argc < cmd_list[i].min_args) {
  165. break;
  166. }
  167. cmd_list[i].cmd_runner(argc, argv);
  168. valid_cmd = 1;
  169. }
  170. }
  171. if (!valid_cmd) {
  172. do_usage();
  173. }
  174. free(cmd);
  175. }
  176. static void run_up_cmd(int argc, char **argv)
  177. {
  178. int partition;
  179. int num_nodes;
  180. int *nodelist;
  181. int i,j;
  182. if (argc <= 1) {
  183. return;
  184. }
  185. for (i=1; i<argc; i++) {
  186. if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
  187. for (j=0; j<num_nodes; j++) {
  188. cmd_start_new_node(nodelist[j], partition);
  189. }
  190. free(nodelist);
  191. }
  192. }
  193. }
  194. static void run_down_cmd(int argc, char **argv)
  195. {
  196. int nodeid;
  197. int i;
  198. for (i=1; i<argc; i++) {
  199. nodeid = atoi(argv[1]);
  200. cmd_stop_node(nodeid);
  201. }
  202. }
  203. static void run_join_cmd(int argc, char **argv)
  204. {
  205. int i;
  206. if (argc < 2) {
  207. printf("join needs at least two partition numbers\n");
  208. return;
  209. }
  210. for (i=2; i<argc; i++) {
  211. cmd_join_partitions(atoi(argv[1]), atoi(argv[i]));
  212. }
  213. cmd_update_all_partitions(1);
  214. }
  215. static void run_move_cmd(int argc, char **argv)
  216. {
  217. int i;
  218. int partition;
  219. int num_nodes;
  220. int *nodelist;
  221. for (i=1; i<argc; i++) {
  222. if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
  223. cmd_move_nodes(partition, num_nodes, nodelist);
  224. free(nodelist);
  225. }
  226. }
  227. cmd_update_all_partitions(1);
  228. }
  229. static void run_autofence_cmd(int argc, char **argv)
  230. {
  231. int onoff = -1;
  232. if (strcasecmp(argv[1], "on") == 0) {
  233. onoff = 1;
  234. }
  235. if (strcasecmp(argv[1], "off") == 0) {
  236. onoff = 0;
  237. }
  238. if (onoff == -1) {
  239. fprintf(stderr, "ERR: autofence value must be 'on' or 'off'\n");
  240. }
  241. else {
  242. cmd_set_autofence(onoff);
  243. }
  244. }
  245. static void run_qdevice_cmd(int argc, char **argv)
  246. {
  247. int i,j;
  248. int partition;
  249. int num_nodes;
  250. int *nodelist;
  251. int onoff = -1;
  252. if (strcasecmp(argv[1], "on") == 0) {
  253. onoff = 1;
  254. }
  255. if (strcasecmp(argv[1], "off") == 0) {
  256. onoff = 0;
  257. }
  258. if (onoff == -1) {
  259. fprintf(stderr, "ERR: qdevice should be 'on' or 'off'\n");
  260. return;
  261. }
  262. for (i=2; i<argc; i++) {
  263. if (parse_partition_nodelist(argv[i], &partition, &num_nodes, &nodelist) == 0) {
  264. for (j=0; j<num_nodes; j++) {
  265. cmd_qdevice_poll(nodelist[j], onoff);
  266. }
  267. free(nodelist);
  268. }
  269. }
  270. cmd_update_all_partitions(0);
  271. }
  272. static void run_show_cmd(int argc, char **argv)
  273. {
  274. cmd_show_node_states();
  275. }
  276. static void run_exit_cmd(int argc, char **argv)
  277. {
  278. cmd_stop_all_nodes();
  279. exit(0);
  280. }