4 Copyright (C) Amitay Isaacs 2015
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/network.h"
22 #include "system/filesys.h"
23 #include "system/time.h"
24 #include "system/wait.h"
25 #include "system/dir.h"
33 #include "common/version.h"
34 #include "lib/util/debug.h"
35 #include "lib/util/samba_util.h"
36 #include "lib/util/sys_rw.h"
38 #include "common/db_hash.h"
39 #include "common/logging.h"
40 #include "common/path.h"
41 #include "protocol/protocol.h"
42 #include "protocol/protocol_api.h"
43 #include "protocol/protocol_util.h"
44 #include "common/system_socket.h"
45 #include "client/client.h"
46 #include "client/client_sync.h"
48 #define TIMEOUT() timeval_current_ofs(options.timelimit, 0)
50 #define SRVID_CTDB_TOOL (CTDB_SRVID_TOOL_RANGE | 0x0001000000000000LL)
51 #define SRVID_CTDB_PUSHDB (CTDB_SRVID_TOOL_RANGE | 0x0002000000000000LL)
54 const char *debuglevelstr;
62 int printemptyrecords;
69 static poptContext pc;
72 struct tevent_context *ev;
73 struct ctdb_client_context *client;
74 struct ctdb_node_map *nodemap;
75 uint32_t pnn, cmd_pnn;
79 static void usage(const char *command);
85 static double timeval_delta(struct timeval *tv2, struct timeval *tv)
87 return (tv2->tv_sec - tv->tv_sec) +
88 (tv2->tv_usec - tv->tv_usec) * 1.0e-6;
91 static struct ctdb_node_and_flags *get_node_by_pnn(
92 struct ctdb_node_map *nodemap,
97 for (i=0; i<nodemap->num; i++) {
98 if (nodemap->node[i].pnn == pnn) {
99 return &nodemap->node[i];
105 static const char *pretty_print_flags(TALLOC_CTX *mem_ctx, uint32_t flags)
107 static const struct {
111 { NODE_FLAGS_DISCONNECTED, "DISCONNECTED" },
112 { NODE_FLAGS_PERMANENTLY_DISABLED, "DISABLED" },
113 { NODE_FLAGS_BANNED, "BANNED" },
114 { NODE_FLAGS_UNHEALTHY, "UNHEALTHY" },
115 { NODE_FLAGS_DELETED, "DELETED" },
116 { NODE_FLAGS_STOPPED, "STOPPED" },
117 { NODE_FLAGS_INACTIVE, "INACTIVE" },
119 char *flags_str = NULL;
122 for (i=0; i<ARRAY_SIZE(flag_names); i++) {
123 if (flags & flag_names[i].flag) {
124 if (flags_str == NULL) {
125 flags_str = talloc_asprintf(mem_ctx,
126 "%s", flag_names[i].name);
128 flags_str = talloc_asprintf_append(flags_str,
129 "|%s", flag_names[i].name);
131 if (flags_str == NULL) {
132 return "OUT-OF-MEMORY";
136 if (flags_str == NULL) {
143 static uint64_t next_srvid(struct ctdb_context *ctdb)
150 * Get consistent nodemap information.
152 * If nodemap is already cached, use that. If not get it.
153 * If the current node is BANNED, then get nodemap from "better" node.
155 static struct ctdb_node_map *get_nodemap(struct ctdb_context *ctdb, bool force)
158 struct ctdb_node_map *nodemap;
159 struct ctdb_node_and_flags *node;
160 uint32_t current_node;
164 TALLOC_FREE(ctdb->nodemap);
167 if (ctdb->nodemap != NULL) {
168 return ctdb->nodemap;
171 tmp_ctx = talloc_new(ctdb);
172 if (tmp_ctx == NULL) {
176 current_node = ctdb->pnn;
178 ret = ctdb_ctrl_get_nodemap(tmp_ctx, ctdb->ev, ctdb->client,
179 current_node, TIMEOUT(), &nodemap);
181 fprintf(stderr, "Failed to get nodemap from node %u\n",
186 node = get_node_by_pnn(nodemap, current_node);
187 if (node->flags & NODE_FLAGS_BANNED) {
190 current_node = (current_node + 1) % nodemap->num;
191 node = get_node_by_pnn(nodemap, current_node);
193 (NODE_FLAGS_DELETED|NODE_FLAGS_DISCONNECTED))) {
196 } while (current_node != ctdb->pnn);
198 if (current_node == ctdb->pnn) {
199 /* Tried all nodes in the cluster */
200 fprintf(stderr, "Warning: All nodes are banned.\n");
207 ctdb->nodemap = talloc_steal(ctdb, nodemap);
211 talloc_free(tmp_ctx);
215 static bool verify_pnn(struct ctdb_context *ctdb, int pnn)
217 struct ctdb_node_map *nodemap;
225 nodemap = get_nodemap(ctdb, false);
226 if (nodemap == NULL) {
231 for (i=0; i<nodemap->num; i++) {
232 if (nodemap->node[i].pnn == pnn) {
238 fprintf(stderr, "Node %u does not exist\n", pnn);
242 if (nodemap->node[i].flags &
243 (NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED)) {
244 fprintf(stderr, "Node %u has status %s\n", pnn,
245 pretty_print_flags(ctdb, nodemap->node[i].flags));
252 static struct ctdb_node_map *talloc_nodemap(TALLOC_CTX *mem_ctx,
253 struct ctdb_node_map *nodemap)
255 struct ctdb_node_map *nodemap2;
257 nodemap2 = talloc_zero(mem_ctx, struct ctdb_node_map);
258 if (nodemap2 == NULL) {
262 nodemap2->node = talloc_array(nodemap2, struct ctdb_node_and_flags,
264 if (nodemap2->node == NULL) {
265 talloc_free(nodemap2);
273 * Get the number and the list of matching nodes
275 * nodestring := NULL | all | pnn,[pnn,...]
277 * If nodestring is NULL, use the current node.
279 static bool parse_nodestring(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
280 const char *nodestring,
281 struct ctdb_node_map **out)
283 struct ctdb_node_map *nodemap, *nodemap2;
284 struct ctdb_node_and_flags *node;
287 nodemap = get_nodemap(ctdb, false);
288 if (nodemap == NULL) {
292 nodemap2 = talloc_nodemap(mem_ctx, nodemap);
293 if (nodemap2 == NULL) {
297 if (nodestring == NULL) {
298 for (i=0; i<nodemap->num; i++) {
299 if (nodemap->node[i].pnn == ctdb->cmd_pnn) {
300 nodemap2->node[0] = nodemap->node[i];
309 if (strcmp(nodestring, "all") == 0) {
310 for (i=0; i<nodemap->num; i++) {
311 nodemap2->node[i] = nodemap->node[i];
313 nodemap2->num = nodemap->num;
319 ns = talloc_strdup(mem_ctx, nodestring);
324 tok = strtok(ns, ",");
325 while (tok != NULL) {
329 pnn = (uint32_t)strtoul(tok, &endptr, 0);
330 if (pnn == 0 && tok == endptr) {
331 fprintf(stderr, "Invalid node %s\n", tok);
335 node = get_node_by_pnn(nodemap, pnn);
337 fprintf(stderr, "Node %u does not exist\n",
342 nodemap2->node[nodemap2->num] = *node;
345 tok = strtok(NULL, ",");
354 /* Compare IP address */
355 static bool ctdb_same_ip(ctdb_sock_addr *ip1, ctdb_sock_addr *ip2)
359 if (ip1->sa.sa_family != ip2->sa.sa_family) {
363 switch (ip1->sa.sa_family) {
365 ret = (memcmp(&ip1->ip.sin_addr, &ip2->ip.sin_addr,
366 sizeof(struct in_addr)) == 0);
370 ret = (memcmp(&ip1->ip6.sin6_addr, &ip2->ip6.sin6_addr,
371 sizeof(struct in6_addr)) == 0);
378 /* Append a node to a node map with given address and flags */
379 static bool node_map_add(struct ctdb_node_map *nodemap,
380 const char *nstr, uint32_t flags)
384 struct ctdb_node_and_flags *n;
387 ret = ctdb_sock_addr_from_string(nstr, &addr, false);
389 fprintf(stderr, "Invalid IP address %s\n", nstr);
394 nodemap->node = talloc_realloc(nodemap, nodemap->node,
395 struct ctdb_node_and_flags, num+1);
396 if (nodemap->node == NULL) {
400 n = &nodemap->node[num];
405 nodemap->num = num+1;
409 /* Read a nodes file into a node map */
410 static struct ctdb_node_map *ctdb_read_nodes_file(TALLOC_CTX *mem_ctx,
416 struct ctdb_node_map *nodemap;
418 nodemap = talloc_zero(mem_ctx, struct ctdb_node_map);
419 if (nodemap == NULL) {
423 lines = file_lines_load(nlist, &nlines, 0, mem_ctx);
428 while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
432 for (i=0; i<nlines; i++) {
438 /* strip leading spaces */
439 while((*node == ' ') || (*node == '\t')) {
445 /* strip trailing spaces */
447 ((node[len-1] == ' ') || (node[len-1] == '\t')))
457 /* A "deleted" node is a node that is
458 commented out in the nodes file. This is
459 used instead of removing a line, which
460 would cause subsequent nodes to change
462 flags = NODE_FLAGS_DELETED;
463 node = discard_const("0.0.0.0");
467 if (! node_map_add(nodemap, node, flags)) {
469 TALLOC_FREE(nodemap);
478 static struct ctdb_node_map *read_nodes_file(TALLOC_CTX *mem_ctx, uint32_t pnn)
480 struct ctdb_node_map *nodemap;
481 const char *nodes_list = NULL;
483 const char *basedir = getenv("CTDB_BASE");
484 if (basedir == NULL) {
485 basedir = CTDB_ETCDIR;
487 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes", basedir);
488 if (nodes_list == NULL) {
489 fprintf(stderr, "Memory allocation error\n");
493 nodemap = ctdb_read_nodes_file(mem_ctx, nodes_list);
494 if (nodemap == NULL) {
495 fprintf(stderr, "Failed to read nodes file \"%s\"\n",
503 static struct ctdb_dbid *db_find(TALLOC_CTX *mem_ctx,
504 struct ctdb_context *ctdb,
505 struct ctdb_dbid_map *dbmap,
508 struct ctdb_dbid *db = NULL;
512 for (i=0; i<dbmap->num; i++) {
513 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
514 ctdb->pnn, TIMEOUT(),
515 dbmap->dbs[i].db_id, &name);
520 if (strcmp(db_name, name) == 0) {
521 talloc_free(discard_const(name));
530 static bool db_exists(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
531 const char *db_arg, uint32_t *db_id,
532 const char **db_name, uint8_t *db_flags)
534 struct ctdb_dbid_map *dbmap;
535 struct ctdb_dbid *db = NULL;
537 const char *name = NULL;
540 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
541 ctdb->pnn, TIMEOUT(), &dbmap);
546 if (strncmp(db_arg, "0x", 2) == 0) {
547 id = strtoul(db_arg, NULL, 0);
548 for (i=0; i<dbmap->num; i++) {
549 if (id == dbmap->dbs[i].db_id) {
556 db = db_find(mem_ctx, ctdb, dbmap, name);
560 fprintf(stderr, "No database matching '%s' found\n", db_arg);
565 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
566 ctdb->pnn, TIMEOUT(), id, &name);
575 if (db_name != NULL) {
576 *db_name = talloc_strdup(mem_ctx, name);
578 if (db_flags != NULL) {
579 *db_flags = db->flags;
584 static int h2i(char h)
586 if (h >= 'a' && h <= 'f') {
589 if (h >= 'A' && h <= 'F') {
595 static int hex_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
602 fprintf(stderr, "Key (%s) contains odd number of hex digits\n",
607 data.dsize = len / 2;
608 data.dptr = talloc_size(mem_ctx, data.dsize);
609 if (data.dptr == NULL) {
613 for (i=0; i<data.dsize; i++) {
614 data.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
621 static int str_to_data(const char *str, size_t len, TALLOC_CTX *mem_ctx,
627 if (strncmp(str, "0x", 2) == 0) {
628 ret = hex_to_data(str+2, len-2, mem_ctx, &data);
633 data.dptr = talloc_memdup(mem_ctx, str, len);
634 if (data.dptr == NULL) {
644 static int run_helper(TALLOC_CTX *mem_ctx, const char *command,
645 const char *path, int argc, const char **argv)
648 int save_errno, status, ret;
649 const char **new_argv;
652 new_argv = talloc_array(mem_ctx, const char *, argc + 2);
653 if (new_argv == NULL) {
658 for (i=0; i<argc; i++) {
659 new_argv[i+1] = argv[i];
661 new_argv[argc+1] = NULL;
666 talloc_free(new_argv);
667 fprintf(stderr, "Failed to fork %s (%s) - %s\n",
668 command, path, strerror(save_errno));
673 ret = execv(path, discard_const(new_argv));
677 /* Should not happen */
681 talloc_free(new_argv);
683 ret = waitpid(pid, &status, 0);
686 fprintf(stderr, "waitpid() failed for %s - %s\n",
687 command, strerror(save_errno));
691 if (WIFEXITED(status)) {
692 int pstatus = WEXITSTATUS(status);
693 if (WIFSIGNALED(status)) {
694 fprintf(stderr, "%s terminated with signal %d\n",
695 command, WTERMSIG(status));
697 } else if (pstatus >= 64 && pstatus < 255) {
698 fprintf(stderr, "%s failed with error %d\n",
699 command, pstatus-64);
705 } else if (WIFSIGNALED(status)) {
706 fprintf(stderr, "%s terminated with signal %d\n",
707 command, WTERMSIG(status));
718 static int control_version(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
719 int argc, const char **argv)
721 printf("%s\n", ctdb_version_string);
725 static bool partially_online(TALLOC_CTX *mem_ctx,
726 struct ctdb_context *ctdb,
727 struct ctdb_node_and_flags *node)
729 struct ctdb_iface_list *iface_list;
733 if (node->flags != 0) {
737 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
738 node->pnn, TIMEOUT(), &iface_list);
744 for (i=0; i < iface_list->num; i++) {
745 if (iface_list->iface[i].link_state == 0) {
754 static void print_nodemap_machine(TALLOC_CTX *mem_ctx,
755 struct ctdb_context *ctdb,
756 struct ctdb_node_map *nodemap,
759 struct ctdb_node_and_flags *node;
762 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
766 "Disconnected", options.sep,
767 "Banned", options.sep,
768 "Disabled", options.sep,
769 "Unhealthy", options.sep,
770 "Stopped", options.sep,
771 "Inactive", options.sep,
772 "PartiallyOnline", options.sep,
773 "ThisNode", options.sep);
775 for (i=0; i<nodemap->num; i++) {
776 node = &nodemap->node[i];
777 if (node->flags & NODE_FLAGS_DELETED) {
781 printf("%s%u%s%s%s%d%s%d%s%d%s%d%s%d%s%d%s%d%s%c%s\n",
783 node->pnn, options.sep,
784 ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
786 !! (node->flags & NODE_FLAGS_DISCONNECTED), options.sep,
787 !! (node->flags & NODE_FLAGS_BANNED), options.sep,
788 !! (node->flags & NODE_FLAGS_PERMANENTLY_DISABLED),
790 !! (node->flags & NODE_FLAGS_UNHEALTHY), options.sep,
791 !! (node->flags & NODE_FLAGS_STOPPED), options.sep,
792 !! (node->flags & NODE_FLAGS_INACTIVE), options.sep,
793 partially_online(mem_ctx, ctdb, node), options.sep,
794 (node->pnn == mypnn)?'Y':'N', options.sep);
799 static void print_nodemap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
800 struct ctdb_node_map *nodemap, uint32_t mypnn,
803 struct ctdb_node_and_flags *node;
804 int num_deleted_nodes = 0;
807 for (i=0; i<nodemap->num; i++) {
808 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
814 if (num_deleted_nodes == 0) {
815 printf("Number of nodes:%d\n", nodemap->num);
817 printf("Number of nodes:%d "
818 "(including %d deleted nodes)\n",
819 nodemap->num, num_deleted_nodes);
823 for (i=0; i<nodemap->num; i++) {
824 node = &nodemap->node[i];
825 if (node->flags & NODE_FLAGS_DELETED) {
829 printf("pnn:%u %-16s %s%s\n",
831 ctdb_sock_addr_to_string(mem_ctx, &node->addr, false),
832 partially_online(mem_ctx, ctdb, node) ?
834 pretty_print_flags(mem_ctx, node->flags),
835 node->pnn == mypnn ? " (THIS NODE)" : "");
839 static void print_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
840 struct ctdb_node_map *nodemap, uint32_t mypnn,
841 struct ctdb_vnn_map *vnnmap, int recmode,
846 print_nodemap(mem_ctx, ctdb, nodemap, mypnn, true);
848 if (vnnmap->generation == INVALID_GENERATION) {
849 printf("Generation:INVALID\n");
851 printf("Generation:%u\n", vnnmap->generation);
853 printf("Size:%d\n", vnnmap->size);
854 for (i=0; i<vnnmap->size; i++) {
855 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
858 printf("Recovery mode:%s (%d)\n",
859 recmode == CTDB_RECOVERY_NORMAL ? "NORMAL" : "RECOVERY",
861 printf("Recovery master:%d\n", recmaster);
864 static int control_status(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
865 int argc, const char **argv)
867 struct ctdb_node_map *nodemap;
868 struct ctdb_vnn_map *vnnmap;
877 nodemap = get_nodemap(ctdb, false);
878 if (nodemap == NULL) {
882 if (options.machinereadable == 1) {
883 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
887 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
888 ctdb->cmd_pnn, TIMEOUT(), &vnnmap);
893 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
894 ctdb->cmd_pnn, TIMEOUT(), &recmode);
899 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
900 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
905 print_status(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, vnnmap,
910 static int control_uptime(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
911 int argc, const char **argv)
913 struct ctdb_uptime *uptime;
914 int ret, tmp, days, hours, minutes, seconds;
916 ret = ctdb_ctrl_uptime(mem_ctx, ctdb->ev, ctdb->client,
917 ctdb->cmd_pnn, TIMEOUT(), &uptime);
922 printf("Current time of node %-4u : %s",
923 ctdb->cmd_pnn, ctime(&uptime->current_time.tv_sec));
925 tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
926 seconds = tmp % 60; tmp /= 60;
927 minutes = tmp % 60; tmp /= 60;
928 hours = tmp % 24; tmp /= 24;
931 printf("Ctdbd start time : (%03d %02d:%02d:%02d) %s",
932 days, hours, minutes, seconds,
933 ctime(&uptime->ctdbd_start_time.tv_sec));
935 tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
936 seconds = tmp % 60; tmp /= 60;
937 minutes = tmp % 60; tmp /= 60;
938 hours = tmp % 24; tmp /= 24;
941 printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s",
942 days, hours, minutes, seconds,
943 ctime(&uptime->last_recovery_finished.tv_sec));
945 printf("Duration of last recovery/failover: %lf seconds\n",
946 timeval_delta(&uptime->last_recovery_finished,
947 &uptime->last_recovery_started));
952 static int control_ping(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
953 int argc, const char **argv)
956 int ret, num_clients;
958 tv = timeval_current();
959 ret = ctdb_ctrl_ping(mem_ctx, ctdb->ev, ctdb->client,
960 ctdb->cmd_pnn, TIMEOUT(), &num_clients);
965 printf("response from %u time=%.6f sec (%d clients)\n",
966 ctdb->cmd_pnn, timeval_elapsed(&tv), num_clients);
970 const char *runstate_to_string(enum ctdb_runstate runstate);
971 enum ctdb_runstate runstate_from_string(const char *runstate_str);
973 static int control_runstate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
974 int argc, const char **argv)
976 enum ctdb_runstate runstate;
980 ret = ctdb_ctrl_get_runstate(mem_ctx, ctdb->ev, ctdb->client,
981 ctdb->cmd_pnn, TIMEOUT(), &runstate);
987 for (i=0; i<argc; i++) {
988 enum ctdb_runstate t;
991 t = ctdb_runstate_from_string(argv[i]);
992 if (t == CTDB_RUNSTATE_UNKNOWN) {
993 printf("Invalid run state (%s)\n", argv[i]);
1004 printf("CTDB not in required run state (got %s)\n",
1005 ctdb_runstate_to_string(runstate));
1009 printf("%s\n", ctdb_runstate_to_string(runstate));
1013 static int control_getvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1014 int argc, const char **argv)
1016 struct ctdb_var_list *tun_var_list;
1025 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1026 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1029 "Failed to get list of variables from node %u\n",
1035 for (i=0; i<tun_var_list->count; i++) {
1036 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1043 printf("No such tunable %s\n", argv[0]);
1047 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
1048 ctdb->cmd_pnn, TIMEOUT(), argv[0], &value);
1053 printf("%-26s = %u\n", argv[0], value);
1057 static int control_setvar(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1058 int argc, const char **argv)
1060 struct ctdb_var_list *tun_var_list;
1061 struct ctdb_tunable tunable;
1069 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1070 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1073 "Failed to get list of variables from node %u\n",
1079 for (i=0; i<tun_var_list->count; i++) {
1080 if (strcasecmp(tun_var_list->var[i], argv[0]) == 0) {
1087 printf("No such tunable %s\n", argv[0]);
1091 tunable.name = argv[0];
1092 tunable.value = strtoul(argv[1], NULL, 0);
1094 ret = ctdb_ctrl_set_tunable(mem_ctx, ctdb->ev, ctdb->client,
1095 ctdb->cmd_pnn, TIMEOUT(), &tunable);
1099 "Setting obsolete tunable variable '%s'\n",
1108 static int control_listvars(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1109 int argc, const char **argv)
1111 struct ctdb_var_list *tun_var_list;
1118 ret = ctdb_ctrl_list_tunables(mem_ctx, ctdb->ev, ctdb->client,
1119 ctdb->cmd_pnn, TIMEOUT(), &tun_var_list);
1124 for (i=0; i<tun_var_list->count; i++) {
1125 control_getvar(mem_ctx, ctdb, 1, &tun_var_list->var[i]);
1134 } stats_fields[] = {
1135 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
1136 STATISTICS_FIELD(num_clients),
1137 STATISTICS_FIELD(frozen),
1138 STATISTICS_FIELD(recovering),
1139 STATISTICS_FIELD(num_recoveries),
1140 STATISTICS_FIELD(client_packets_sent),
1141 STATISTICS_FIELD(client_packets_recv),
1142 STATISTICS_FIELD(node_packets_sent),
1143 STATISTICS_FIELD(node_packets_recv),
1144 STATISTICS_FIELD(keepalive_packets_sent),
1145 STATISTICS_FIELD(keepalive_packets_recv),
1146 STATISTICS_FIELD(node.req_call),
1147 STATISTICS_FIELD(node.reply_call),
1148 STATISTICS_FIELD(node.req_dmaster),
1149 STATISTICS_FIELD(node.reply_dmaster),
1150 STATISTICS_FIELD(node.reply_error),
1151 STATISTICS_FIELD(node.req_message),
1152 STATISTICS_FIELD(node.req_control),
1153 STATISTICS_FIELD(node.reply_control),
1154 STATISTICS_FIELD(node.req_tunnel),
1155 STATISTICS_FIELD(client.req_call),
1156 STATISTICS_FIELD(client.req_message),
1157 STATISTICS_FIELD(client.req_control),
1158 STATISTICS_FIELD(client.req_tunnel),
1159 STATISTICS_FIELD(timeouts.call),
1160 STATISTICS_FIELD(timeouts.control),
1161 STATISTICS_FIELD(timeouts.traverse),
1162 STATISTICS_FIELD(locks.num_calls),
1163 STATISTICS_FIELD(locks.num_current),
1164 STATISTICS_FIELD(locks.num_pending),
1165 STATISTICS_FIELD(locks.num_failed),
1166 STATISTICS_FIELD(total_calls),
1167 STATISTICS_FIELD(pending_calls),
1168 STATISTICS_FIELD(childwrite_calls),
1169 STATISTICS_FIELD(pending_childwrite_calls),
1170 STATISTICS_FIELD(memory_used),
1171 STATISTICS_FIELD(max_hop_count),
1172 STATISTICS_FIELD(total_ro_delegations),
1173 STATISTICS_FIELD(total_ro_revokes),
1176 #define LATENCY_AVG(v) ((v).num ? (v).total / (v).num : 0.0 )
1178 static void print_statistics_machine(struct ctdb_statistics *s,
1184 printf("CTDB version%s", options.sep);
1185 printf("Current time of statistics%s", options.sep);
1186 printf("Statistics collected since%s", options.sep);
1187 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1188 printf("%s%s", stats_fields[i].name, options.sep);
1190 printf("num_reclock_ctdbd_latency%s", options.sep);
1191 printf("min_reclock_ctdbd_latency%s", options.sep);
1192 printf("avg_reclock_ctdbd_latency%s", options.sep);
1193 printf("max_reclock_ctdbd_latency%s", options.sep);
1195 printf("num_reclock_recd_latency%s", options.sep);
1196 printf("min_reclock_recd_latency%s", options.sep);
1197 printf("avg_reclock_recd_latency%s", options.sep);
1198 printf("max_reclock_recd_latency%s", options.sep);
1200 printf("num_call_latency%s", options.sep);
1201 printf("min_call_latency%s", options.sep);
1202 printf("avg_call_latency%s", options.sep);
1203 printf("max_call_latency%s", options.sep);
1205 printf("num_lockwait_latency%s", options.sep);
1206 printf("min_lockwait_latency%s", options.sep);
1207 printf("avg_lockwait_latency%s", options.sep);
1208 printf("max_lockwait_latency%s", options.sep);
1210 printf("num_childwrite_latency%s", options.sep);
1211 printf("min_childwrite_latency%s", options.sep);
1212 printf("avg_childwrite_latency%s", options.sep);
1213 printf("max_childwrite_latency%s", options.sep);
1217 printf("%u%s", CTDB_PROTOCOL, options.sep);
1218 printf("%u%s", (uint32_t)s->statistics_current_time.tv_sec, options.sep);
1219 printf("%u%s", (uint32_t)s->statistics_start_time.tv_sec, options.sep);
1220 for (i=0;i<ARRAY_SIZE(stats_fields);i++) {
1222 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s),
1225 printf("%u%s", s->reclock.ctdbd.num, options.sep);
1226 printf("%.6f%s", s->reclock.ctdbd.min, options.sep);
1227 printf("%.6f%s", LATENCY_AVG(s->reclock.ctdbd), options.sep);
1228 printf("%.6f%s", s->reclock.ctdbd.max, options.sep);
1230 printf("%u%s", s->reclock.recd.num, options.sep);
1231 printf("%.6f%s", s->reclock.recd.min, options.sep);
1232 printf("%.6f%s", LATENCY_AVG(s->reclock.recd), options.sep);
1233 printf("%.6f%s", s->reclock.recd.max, options.sep);
1235 printf("%d%s", s->call_latency.num, options.sep);
1236 printf("%.6f%s", s->call_latency.min, options.sep);
1237 printf("%.6f%s", LATENCY_AVG(s->call_latency), options.sep);
1238 printf("%.6f%s", s->call_latency.max, options.sep);
1240 printf("%u%s", s->locks.latency.num, options.sep);
1241 printf("%.6f%s", s->locks.latency.min, options.sep);
1242 printf("%.6f%s", LATENCY_AVG(s->locks.latency), options.sep);
1243 printf("%.6f%s", s->locks.latency.max, options.sep);
1245 printf("%d%s", s->childwrite_latency.num, options.sep);
1246 printf("%.6f%s", s->childwrite_latency.min, options.sep);
1247 printf("%.6f%s", LATENCY_AVG(s->childwrite_latency), options.sep);
1248 printf("%.6f%s", s->childwrite_latency.max, options.sep);
1252 static void print_statistics(struct ctdb_statistics *s)
1254 int tmp, days, hours, minutes, seconds;
1256 const char *prefix = NULL;
1259 tmp = s->statistics_current_time.tv_sec -
1260 s->statistics_start_time.tv_sec;
1261 seconds = tmp % 60; tmp /= 60;
1262 minutes = tmp % 60; tmp /= 60;
1263 hours = tmp % 24; tmp /= 24;
1266 printf("CTDB version %u\n", CTDB_PROTOCOL);
1267 printf("Current time of statistics : %s",
1268 ctime(&s->statistics_current_time.tv_sec));
1269 printf("Statistics collected since : (%03d %02d:%02d:%02d) %s",
1270 days, hours, minutes, seconds,
1271 ctime(&s->statistics_start_time.tv_sec));
1273 for (i=0; i<ARRAY_SIZE(stats_fields); i++) {
1274 if (strchr(stats_fields[i].name, '.') != NULL) {
1275 preflen = strcspn(stats_fields[i].name, ".") + 1;
1277 strncmp(prefix, stats_fields[i].name, preflen) != 0) {
1278 prefix = stats_fields[i].name;
1279 printf(" %*.*s\n", preflen-1, preflen-1,
1280 stats_fields[i].name);
1285 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
1286 stats_fields[i].name+preflen, preflen ? 0 : 4, "",
1287 *(uint32_t *)(stats_fields[i].offset+(uint8_t *)s));
1290 printf(" hop_count_buckets:");
1291 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1292 printf(" %d", s->hop_count_bucket[i]);
1295 printf(" lock_buckets:");
1296 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
1297 printf(" %d", s->locks.buckets[i]);
1300 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1301 "locks_latency MIN/AVG/MAX",
1302 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
1303 s->locks.latency.max, s->locks.latency.num);
1305 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1306 "reclock_ctdbd MIN/AVG/MAX",
1307 s->reclock.ctdbd.min, LATENCY_AVG(s->reclock.ctdbd),
1308 s->reclock.ctdbd.max, s->reclock.ctdbd.num);
1310 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1311 "reclock_recd MIN/AVG/MAX",
1312 s->reclock.recd.min, LATENCY_AVG(s->reclock.recd),
1313 s->reclock.recd.max, s->reclock.recd.num);
1315 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1316 "call_latency MIN/AVG/MAX",
1317 s->call_latency.min, LATENCY_AVG(s->call_latency),
1318 s->call_latency.max, s->call_latency.num);
1320 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
1321 "childwrite_latency MIN/AVG/MAX",
1322 s->childwrite_latency.min,
1323 LATENCY_AVG(s->childwrite_latency),
1324 s->childwrite_latency.max, s->childwrite_latency.num);
1327 static int control_statistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1328 int argc, const char **argv)
1330 struct ctdb_statistics *stats;
1334 usage("statistics");
1337 ret = ctdb_ctrl_statistics(mem_ctx, ctdb->ev, ctdb->client,
1338 ctdb->cmd_pnn, TIMEOUT(), &stats);
1343 if (options.machinereadable) {
1344 print_statistics_machine(stats, true);
1346 print_statistics(stats);
1352 static int control_statistics_reset(TALLOC_CTX *mem_ctx,
1353 struct ctdb_context *ctdb,
1354 int argc, const char **argv)
1359 usage("statisticsreset");
1362 ret = ctdb_ctrl_statistics_reset(mem_ctx, ctdb->ev, ctdb->client,
1363 ctdb->cmd_pnn, TIMEOUT());
1371 static int control_stats(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1372 int argc, const char **argv)
1374 struct ctdb_statistics_list *slist;
1375 int ret, count = 0, i;
1376 bool show_header = true;
1383 count = atoi(argv[0]);
1386 ret = ctdb_ctrl_get_stat_history(mem_ctx, ctdb->ev, ctdb->client,
1387 ctdb->cmd_pnn, TIMEOUT(), &slist);
1392 for (i=0; i<slist->num; i++) {
1393 if (slist->stats[i].statistics_start_time.tv_sec == 0) {
1396 if (options.machinereadable == 1) {
1397 print_statistics_machine(&slist->stats[i],
1399 show_header = false;
1401 print_statistics(&slist->stats[i]);
1403 if (count > 0 && i == count) {
1411 static int ctdb_public_ip_cmp(const void *a, const void *b)
1413 const struct ctdb_public_ip *ip_a = a;
1414 const struct ctdb_public_ip *ip_b = b;
1416 return ctdb_sock_addr_cmp(&ip_a->addr, &ip_b->addr);
1419 static void print_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1420 struct ctdb_public_ip_list *ips,
1421 struct ctdb_public_ip_info **ipinfo,
1425 char *conf, *avail, *active;
1427 if (options.machinereadable == 1) {
1428 printf("%s%s%s%s%s", options.sep,
1429 "Public IP", options.sep,
1430 "Node", options.sep);
1431 if (options.verbose == 1) {
1432 printf("%s%s%s%s%s%s\n",
1433 "ActiveInterfaces", options.sep,
1434 "AvailableInterfaces", options.sep,
1435 "ConfiguredInterfaces", options.sep);
1441 printf("Public IPs on ALL nodes\n");
1443 printf("Public IPs on node %u\n", ctdb->cmd_pnn);
1447 for (i = 0; i < ips->num; i++) {
1449 if (options.machinereadable == 1) {
1450 printf("%s%s%s%d%s", options.sep,
1451 ctdb_sock_addr_to_string(
1452 mem_ctx, &ips->ip[i].addr, false),
1454 (int)ips->ip[i].pnn, options.sep);
1456 printf("%s", ctdb_sock_addr_to_string(
1457 mem_ctx, &ips->ip[i].addr, false));
1460 if (options.verbose == 0) {
1461 if (options.machinereadable == 1) {
1464 printf(" %d\n", (int)ips->ip[i].pnn);
1473 if (ipinfo[i] == NULL) {
1477 for (j=0; j<ipinfo[i]->ifaces->num; j++) {
1478 struct ctdb_iface *iface;
1480 iface = &ipinfo[i]->ifaces->iface[j];
1482 conf = talloc_strdup(mem_ctx, iface->name);
1484 conf = talloc_asprintf_append(
1485 conf, ",%s", iface->name);
1488 if (ipinfo[i]->active_idx == j) {
1489 active = iface->name;
1492 if (iface->link_state == 0) {
1496 if (avail == NULL) {
1497 avail = talloc_strdup(mem_ctx, iface->name);
1499 avail = talloc_asprintf_append(
1500 avail, ",%s", iface->name);
1506 if (options.machinereadable == 1) {
1507 printf("%s%s%s%s%s%s\n",
1508 active ? active : "", options.sep,
1509 avail ? avail : "", options.sep,
1510 conf ? conf : "", options.sep);
1512 printf(" node[%d] active[%s] available[%s]"
1513 " configured[%s]\n",
1514 (int)ips->ip[i].pnn, active ? active : "",
1515 avail ? avail : "", conf ? conf : "");
1520 static int collect_ips(uint8_t *keybuf, size_t keylen, uint8_t *databuf,
1521 size_t datalen, void *private_data)
1523 struct ctdb_public_ip_list *ips = talloc_get_type_abort(
1524 private_data, struct ctdb_public_ip_list);
1525 struct ctdb_public_ip *ip;
1527 ip = (struct ctdb_public_ip *)databuf;
1528 ips->ip[ips->num] = *ip;
1534 static int get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *mem_ctx,
1535 struct ctdb_public_ip_list **out)
1537 struct ctdb_node_map *nodemap;
1538 struct ctdb_public_ip_list *ips;
1539 struct db_hash_context *ipdb;
1541 int ret, count, i, j;
1543 nodemap = get_nodemap(ctdb, false);
1544 if (nodemap == NULL) {
1548 ret = db_hash_init(mem_ctx, "ips", 101, DB_HASH_COMPLEX, &ipdb);
1553 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
1559 for (i=0; i<count; i++) {
1560 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1561 pnn_list[i], TIMEOUT(),
1567 for (j=0; j<ips->num; j++) {
1568 struct ctdb_public_ip ip;
1570 ip.pnn = ips->ip[j].pnn;
1571 ip.addr = ips->ip[j].addr;
1573 if (pnn_list[i] == ip.pnn) {
1574 /* Node claims IP is hosted on it, so
1575 * save that information
1577 ret = db_hash_add(ipdb, (uint8_t *)&ip.addr,
1579 (uint8_t *)&ip, sizeof(ip));
1584 /* Node thinks IP is hosted elsewhere,
1585 * so overwrite with CTDB_UNKNOWN_PNN
1586 * if there's no existing entry
1588 ret = db_hash_exists(ipdb, (uint8_t *)&ip.addr,
1590 if (ret == ENOENT) {
1591 ip.pnn = CTDB_UNKNOWN_PNN;
1592 ret = db_hash_add(ipdb,
1593 (uint8_t *)&ip.addr,
1607 talloc_free(pnn_list);
1609 ret = db_hash_traverse(ipdb, NULL, NULL, &count);
1614 ips = talloc_zero(mem_ctx, struct ctdb_public_ip_list);
1619 ips->ip = talloc_array(ips, struct ctdb_public_ip, count);
1620 if (ips->ip == NULL) {
1624 ret = db_hash_traverse(ipdb, collect_ips, ips, &count);
1629 if (count != ips->num) {
1643 static int control_ip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1644 int argc, const char **argv)
1646 struct ctdb_public_ip_list *ips;
1647 struct ctdb_public_ip_info **ipinfo;
1649 bool do_all = false;
1656 if (strcmp(argv[0], "all") == 0) {
1664 ret = get_all_public_ips(ctdb, mem_ctx, &ips);
1666 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
1667 ctdb->cmd_pnn, TIMEOUT(),
1674 qsort(ips->ip, ips->num, sizeof(struct ctdb_public_ip),
1675 ctdb_public_ip_cmp);
1677 ipinfo = talloc_array(mem_ctx, struct ctdb_public_ip_info *, ips->num);
1678 if (ipinfo == NULL) {
1682 for (i=0; i<ips->num; i++) {
1685 pnn = ips->ip[i].pnn;
1687 pnn = ctdb->cmd_pnn;
1689 if (pnn == CTDB_UNKNOWN_PNN) {
1693 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev,
1695 TIMEOUT(), &ips->ip[i].addr,
1702 print_ip(mem_ctx, ctdb, ips, ipinfo, do_all);
1706 static int control_ipinfo(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1707 int argc, const char **argv)
1709 struct ctdb_public_ip_info *ipinfo;
1710 ctdb_sock_addr addr;
1717 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
1719 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
1723 ret = ctdb_ctrl_get_public_ip_info(mem_ctx, ctdb->ev, ctdb->client,
1724 ctdb->cmd_pnn, TIMEOUT(), &addr,
1728 printf("Node %u does not know about IP %s\n",
1729 ctdb->cmd_pnn, argv[0]);
1734 printf("Public IP[%s] info on node %u\n",
1735 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
1738 printf("IP:%s\nCurrentNode:%u\nNumInterfaces:%u\n",
1739 ctdb_sock_addr_to_string(mem_ctx, &ipinfo->ip.addr, false),
1740 ipinfo->ip.pnn, ipinfo->ifaces->num);
1742 for (i=0; i<ipinfo->ifaces->num; i++) {
1743 struct ctdb_iface *iface;
1745 iface = &ipinfo->ifaces->iface[i];
1746 iface->name[CTDB_IFACE_SIZE] = '\0';
1747 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
1749 iface->link_state == 0 ? "down" : "up",
1751 (i == ipinfo->active_idx) ? " (active)" : "");
1757 static int control_ifaces(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1758 int argc, const char **argv)
1760 struct ctdb_iface_list *ifaces;
1767 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1768 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1773 if (ifaces->num == 0) {
1774 printf("No interfaces configured on node %u\n",
1779 if (options.machinereadable) {
1780 printf("%s%s%s%s%s%s%s\n", options.sep,
1781 "Name", options.sep,
1782 "LinkStatus", options.sep,
1783 "References", options.sep);
1785 printf("Interfaces on node %u\n", ctdb->cmd_pnn);
1788 for (i=0; i<ifaces->num; i++) {
1789 if (options.machinereadable) {
1790 printf("%s%s%s%u%s%u%s\n", options.sep,
1791 ifaces->iface[i].name, options.sep,
1792 ifaces->iface[i].link_state, options.sep,
1793 ifaces->iface[i].references, options.sep);
1795 printf("name:%s link:%s references:%u\n",
1796 ifaces->iface[i].name,
1797 ifaces->iface[i].link_state ? "up" : "down",
1798 ifaces->iface[i].references);
1805 static int control_setifacelink(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1806 int argc, const char **argv)
1808 struct ctdb_iface_list *ifaces;
1809 struct ctdb_iface *iface;
1813 usage("setifacelink");
1816 if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
1817 fprintf(stderr, "Interface name '%s' too long\n", argv[0]);
1821 ret = ctdb_ctrl_get_ifaces(mem_ctx, ctdb->ev, ctdb->client,
1822 ctdb->cmd_pnn, TIMEOUT(), &ifaces);
1825 "Failed to get interface information from node %u\n",
1831 for (i=0; i<ifaces->num; i++) {
1832 if (strcmp(ifaces->iface[i].name, argv[0]) == 0) {
1833 iface = &ifaces->iface[i];
1838 if (iface == NULL) {
1839 printf("Interface %s not configured on node %u\n",
1840 argv[0], ctdb->cmd_pnn);
1844 if (strcmp(argv[1], "up") == 0) {
1845 iface->link_state = 1;
1846 } else if (strcmp(argv[1], "down") == 0) {
1847 iface->link_state = 0;
1849 usage("setifacelink");
1853 iface->references = 0;
1855 ret = ctdb_ctrl_set_iface_link_state(mem_ctx, ctdb->ev, ctdb->client,
1856 ctdb->cmd_pnn, TIMEOUT(), iface);
1864 static int control_process_exists(TALLOC_CTX *mem_ctx,
1865 struct ctdb_context *ctdb,
1866 int argc, const char **argv)
1872 if (argc != 1 && argc != 2) {
1873 usage("process-exists");
1876 pid = atoi(argv[0]);
1878 srvid = strtoull(argv[1], NULL, 0);
1882 ret = ctdb_ctrl_process_exists(mem_ctx, ctdb->ev, ctdb->client,
1883 ctdb->cmd_pnn, TIMEOUT(), pid, &status);
1885 struct ctdb_pid_srvid pid_srvid;
1887 pid_srvid.pid = pid;
1888 pid_srvid.srvid = srvid;
1890 ret = ctdb_ctrl_check_pid_srvid(mem_ctx, ctdb->ev,
1891 ctdb->client, ctdb->cmd_pnn,
1892 TIMEOUT(), &pid_srvid,
1901 printf("PID %d %s\n", pid,
1902 (status == 0 ? "exists" : "does not exist"));
1904 printf("PID %d with SRVID 0x%"PRIx64" %s\n", pid, srvid,
1905 (status == 0 ? "exists" : "does not exist"));
1910 static int control_getdbmap(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
1911 int argc, const char **argv)
1913 struct ctdb_dbid_map *dbmap;
1920 ret = ctdb_ctrl_get_dbmap(mem_ctx, ctdb->ev, ctdb->client,
1921 ctdb->cmd_pnn, TIMEOUT(), &dbmap);
1926 if (options.machinereadable == 1) {
1927 printf("%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n",
1930 "Name", options.sep,
1931 "Path", options.sep,
1932 "Persistent", options.sep,
1933 "Sticky", options.sep,
1934 "Unhealthy", options.sep,
1935 "Readonly", options.sep,
1936 "Replicated", options.sep);
1938 printf("Number of databases:%d\n", dbmap->num);
1941 for (i=0; i<dbmap->num; i++) {
1951 db_id = dbmap->dbs[i].db_id;
1953 ret = ctdb_ctrl_get_dbname(mem_ctx, ctdb->ev, ctdb->client,
1954 ctdb->cmd_pnn, TIMEOUT(), db_id,
1960 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
1961 ctdb->cmd_pnn, TIMEOUT(), db_id,
1967 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
1968 ctdb->cmd_pnn, TIMEOUT(), db_id,
1974 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
1975 readonly = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
1976 sticky = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
1977 replicated = dbmap->dbs[i].flags & CTDB_DB_FLAGS_REPLICATED;
1979 if (options.machinereadable == 1) {
1980 printf("%s0x%08X%s%s%s%s%s%d%s%d%s%d%s%d%s%d%s\n",
1985 !! (persistent), options.sep,
1986 !! (sticky), options.sep,
1987 !! (health), options.sep,
1988 !! (readonly), options.sep,
1989 !! (replicated), options.sep);
1991 printf("dbid:0x%08x name:%s path:%s%s%s%s%s%s\n",
1993 persistent ? " PERSISTENT" : "",
1994 sticky ? " STICKY" : "",
1995 readonly ? " READONLY" : "",
1996 replicated ? " REPLICATED" : "",
1997 health ? " UNHEALTHY" : "");
2000 talloc_free(discard_const(name));
2001 talloc_free(discard_const(path));
2002 talloc_free(discard_const(health));
2008 static int control_getdbstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2009 int argc, const char **argv)
2012 const char *db_name, *db_path, *db_health;
2017 usage("getdbstatus");
2020 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2024 ret = ctdb_ctrl_getdbpath(mem_ctx, ctdb->ev, ctdb->client,
2025 ctdb->cmd_pnn, TIMEOUT(), db_id,
2031 ret = ctdb_ctrl_db_get_health(mem_ctx, ctdb->ev, ctdb->client,
2032 ctdb->cmd_pnn, TIMEOUT(), db_id,
2038 printf("dbid: 0x%08x\nname: %s\npath: %s\n", db_id, db_name, db_path);
2039 printf("PERSISTENT: %s\nREPLICATED: %s\nSTICKY: %s\nREADONLY: %s\n",
2040 (db_flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
2041 (db_flags & CTDB_DB_FLAGS_REPLICATED ? "yes" : "no"),
2042 (db_flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
2043 (db_flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"));
2044 printf("HEALTH: %s\n", (db_health ? db_health : "OK"));
2048 struct dump_record_state {
2052 #define ISASCII(x) (isprint(x) && ! strchr("\"\\", (x)))
2054 static void dump_tdb_data(const char *name, TDB_DATA val)
2058 fprintf(stdout, "%s(%zu) = \"", name, val.dsize);
2059 for (i=0; i<val.dsize; i++) {
2060 if (ISASCII(val.dptr[i])) {
2061 fprintf(stdout, "%c", val.dptr[i]);
2063 fprintf(stdout, "\\%02X", val.dptr[i]);
2066 fprintf(stdout, "\"\n");
2069 static void dump_ltdb_header(struct ctdb_ltdb_header *header)
2071 fprintf(stdout, "dmaster: %u\n", header->dmaster);
2072 fprintf(stdout, "rsn: %" PRIu64 "\n", header->rsn);
2073 fprintf(stdout, "flags: 0x%08x", header->flags);
2074 if (header->flags & CTDB_REC_FLAG_MIGRATED_WITH_DATA) {
2075 fprintf(stdout, " MIGRATED_WITH_DATA");
2077 if (header->flags & CTDB_REC_FLAG_VACUUM_MIGRATED) {
2078 fprintf(stdout, " VACUUM_MIGRATED");
2080 if (header->flags & CTDB_REC_FLAG_AUTOMATIC) {
2081 fprintf(stdout, " AUTOMATIC");
2083 if (header->flags & CTDB_REC_RO_HAVE_DELEGATIONS) {
2084 fprintf(stdout, " RO_HAVE_DELEGATIONS");
2086 if (header->flags & CTDB_REC_RO_HAVE_READONLY) {
2087 fprintf(stdout, " RO_HAVE_READONLY");
2089 if (header->flags & CTDB_REC_RO_REVOKING_READONLY) {
2090 fprintf(stdout, " RO_REVOKING_READONLY");
2092 if (header->flags & CTDB_REC_RO_REVOKE_COMPLETE) {
2093 fprintf(stdout, " RO_REVOKE_COMPLETE");
2095 fprintf(stdout, "\n");
2099 static int dump_record(uint32_t reqid, struct ctdb_ltdb_header *header,
2100 TDB_DATA key, TDB_DATA data, void *private_data)
2102 struct dump_record_state *state =
2103 (struct dump_record_state *)private_data;
2107 dump_tdb_data("key", key);
2108 dump_ltdb_header(header);
2109 dump_tdb_data("data", data);
2110 fprintf(stdout, "\n");
2115 static int control_catdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2116 int argc, const char **argv)
2118 struct ctdb_db_context *db;
2119 const char *db_name;
2122 struct dump_record_state state;
2129 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2133 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2136 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2142 ret = ctdb_db_traverse(mem_ctx, ctdb->ev, ctdb->client, db,
2143 ctdb->cmd_pnn, TIMEOUT(),
2144 dump_record, &state);
2146 printf("Dumped %u records\n", state.count);
2151 static int control_cattdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2152 int argc, const char **argv)
2154 struct ctdb_db_context *db;
2155 const char *db_name;
2158 struct dump_record_state state;
2165 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
2169 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2172 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
2177 ret = ctdb_db_traverse_local(db, true, true, dump_record, &state);
2179 printf("Dumped %u record(s)\n", state.count);
2184 static int control_getcapabilities(TALLOC_CTX *mem_ctx,
2185 struct ctdb_context *ctdb,
2186 int argc, const char **argv)
2192 usage("getcapabilities");
2195 ret = ctdb_ctrl_get_capabilities(mem_ctx, ctdb->ev, ctdb->client,
2196 ctdb->cmd_pnn, TIMEOUT(), &caps);
2201 if (options.machinereadable == 1) {
2202 printf("%s%s%s%s%s\n",
2204 "RECMASTER", options.sep,
2205 "LMASTER", options.sep);
2206 printf("%s%d%s%d%s\n", options.sep,
2207 !! (caps & CTDB_CAP_RECMASTER), options.sep,
2208 !! (caps & CTDB_CAP_LMASTER), options.sep);
2210 printf("RECMASTER: %s\n",
2211 (caps & CTDB_CAP_RECMASTER) ? "YES" : "NO");
2212 printf("LMASTER: %s\n",
2213 (caps & CTDB_CAP_LMASTER) ? "YES" : "NO");
2219 static int control_pnn(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2220 int argc, const char **argv)
2222 printf("%u\n", ctdb_client_pnn(ctdb->client));
2226 static int control_lvs(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2227 int argc, const char **argv)
2229 char *t, *lvs_helper = NULL;
2235 t = getenv("CTDB_LVS_HELPER");
2237 lvs_helper = talloc_strdup(mem_ctx, t);
2239 lvs_helper = talloc_asprintf(mem_ctx, "%s/ctdb_lvs",
2240 CTDB_HELPER_BINDIR);
2243 if (lvs_helper == NULL) {
2244 fprintf(stderr, "Unable to set LVS helper\n");
2248 return run_helper(mem_ctx, "LVS helper", lvs_helper, argc, argv);
2251 static int control_setdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2252 int argc, const char **argv)
2262 found = debug_level_parse(argv[0], &log_level);
2265 "Invalid debug level '%s'. Valid levels are:\n",
2267 fprintf(stderr, "\tERROR | WARNING | NOTICE | INFO | DEBUG\n");
2271 ret = ctdb_ctrl_setdebug(mem_ctx, ctdb->ev, ctdb->client,
2272 ctdb->cmd_pnn, TIMEOUT(), log_level);
2280 static int control_getdebug(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2281 int argc, const char **argv)
2284 const char *log_str;
2291 ret = ctdb_ctrl_getdebug(mem_ctx, ctdb->ev, ctdb->client,
2292 ctdb->cmd_pnn, TIMEOUT(), &loglevel);
2297 log_str = debug_level_to_string(loglevel);
2298 printf("%s\n", log_str);
2303 static int control_attach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2304 int argc, const char **argv)
2306 const char *db_name;
2307 uint8_t db_flags = 0;
2310 if (argc < 1 || argc > 2) {
2316 if (strcmp(argv[1], "persistent") == 0) {
2317 db_flags = CTDB_DB_FLAGS_PERSISTENT;
2318 } else if (strcmp(argv[1], "readonly") == 0) {
2319 db_flags = CTDB_DB_FLAGS_READONLY;
2320 } else if (strcmp(argv[1], "sticky") == 0) {
2321 db_flags = CTDB_DB_FLAGS_STICKY;
2322 } else if (strcmp(argv[1], "replicated") == 0) {
2323 db_flags = CTDB_DB_FLAGS_REPLICATED;
2329 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
2338 static int control_detach(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2339 int argc, const char **argv)
2341 const char *db_name;
2344 struct ctdb_node_map *nodemap;
2352 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2353 ctdb->cmd_pnn, TIMEOUT(), &recmode);
2358 if (recmode == CTDB_RECOVERY_ACTIVE) {
2359 fprintf(stderr, "Database cannot be detached"
2360 " when recovery is active\n");
2364 nodemap = get_nodemap(ctdb, false);
2365 if (nodemap == NULL) {
2369 for (i=0; i<nodemap->num; i++) {
2372 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
2375 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
2378 if (nodemap->node[i].flags & NODE_FLAGS_INACTIVE) {
2379 fprintf(stderr, "Database cannot be detached on"
2380 " inactive (stopped or banned) node %u\n",
2381 nodemap->node[i].pnn);
2385 ret = ctdb_ctrl_get_tunable(mem_ctx, ctdb->ev, ctdb->client,
2386 nodemap->node[i].pnn, TIMEOUT(),
2387 "AllowClientDBAttach", &value);
2390 "Unable to get tunable AllowClientDBAttach"
2391 " from node %u\n", nodemap->node[i].pnn);
2397 "Database access is still active on node %u."
2398 " Set AllowclientDBAttach=0 on all nodes.\n",
2399 nodemap->node[i].pnn);
2405 for (i=0; i<argc; i++) {
2406 if (! db_exists(mem_ctx, ctdb, argv[i], &db_id, &db_name,
2412 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
2414 "Only volatile databases can be detached\n");
2418 ret = ctdb_detach(ctdb->ev, ctdb->client, TIMEOUT(), db_id);
2420 fprintf(stderr, "Database %s detach failed\n", db_name);
2428 static int control_dumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2429 int argc, const char **argv)
2431 const char *mem_str;
2435 ret = ctdb_ctrl_dump_memory(mem_ctx, ctdb->ev, ctdb->client,
2436 ctdb->cmd_pnn, TIMEOUT(), &mem_str);
2441 n = write(1, mem_str, strlen(mem_str)+1);
2442 if (n < 0 || n != strlen(mem_str)+1) {
2443 fprintf(stderr, "Failed to write talloc summary\n");
2450 static void dump_memory(uint64_t srvid, TDB_DATA data, void *private_data)
2452 bool *done = (bool *)private_data;
2455 n = write(1, data.dptr, data.dsize);
2456 if (n < 0 || n != data.dsize) {
2457 fprintf(stderr, "Failed to write talloc summary\n");
2463 static int control_rddumpmemory(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2464 int argc, const char **argv)
2466 struct ctdb_srvid_message msg = { 0 };
2470 msg.pnn = ctdb->pnn;
2471 msg.srvid = next_srvid(ctdb);
2473 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2474 msg.srvid, dump_memory, &done);
2479 ret = ctdb_message_mem_dump(mem_ctx, ctdb->ev, ctdb->client,
2480 ctdb->cmd_pnn, &msg);
2485 ctdb_client_wait(ctdb->ev, &done);
2489 static int control_getpid(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2490 int argc, const char **argv)
2495 ret = ctdb_ctrl_get_pid(mem_ctx, ctdb->ev, ctdb->client,
2496 ctdb->cmd_pnn, TIMEOUT(), &pid);
2501 printf("%u\n", pid);
2505 static int check_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2506 const char *desc, uint32_t flag, bool set_flag)
2508 struct ctdb_node_map *nodemap;
2511 nodemap = get_nodemap(ctdb, false);
2512 if (nodemap == NULL) {
2516 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2517 if (set_flag == flag_is_set) {
2519 fprintf(stderr, "Node %u is already %s\n",
2520 ctdb->cmd_pnn, desc);
2522 fprintf(stderr, "Node %u is not %s\n",
2523 ctdb->cmd_pnn, desc);
2531 static void wait_for_flags(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2532 uint32_t flag, bool set_flag)
2534 struct ctdb_node_map *nodemap;
2538 nodemap = get_nodemap(ctdb, true);
2539 if (nodemap == NULL) {
2541 "Failed to get nodemap, trying again\n");
2546 flag_is_set = nodemap->node[ctdb->cmd_pnn].flags & flag;
2547 if (flag_is_set == set_flag) {
2555 static int ctdb_ctrl_modflags(TALLOC_CTX *mem_ctx, struct tevent_context *ev,
2556 struct ctdb_client_context *client,
2557 uint32_t destnode, struct timeval timeout,
2558 uint32_t set, uint32_t clear)
2560 struct ctdb_node_map *nodemap;
2561 struct ctdb_node_flag_change flag_change;
2562 struct ctdb_req_control request;
2566 ret = ctdb_ctrl_get_nodemap(mem_ctx, ev, client, destnode,
2567 tevent_timeval_zero(), &nodemap);
2572 flag_change.pnn = destnode;
2573 flag_change.old_flags = nodemap->node[destnode].flags;
2574 flag_change.new_flags = flag_change.old_flags | set;
2575 flag_change.new_flags &= ~clear;
2577 count = list_of_connected_nodes(nodemap, -1, mem_ctx, &pnn_list);
2582 ctdb_req_control_modify_flags(&request, &flag_change);
2583 ret = ctdb_client_control_multi(mem_ctx, ev, client, pnn_list, count,
2584 tevent_timeval_zero(), &request,
2589 struct ipreallocate_state {
2594 static void ipreallocate_handler(uint64_t srvid, TDB_DATA data,
2597 struct ipreallocate_state *state =
2598 (struct ipreallocate_state *)private_data;
2600 if (data.dsize != sizeof(int)) {
2605 state->status = *(int *)data.dptr;
2609 static int ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb)
2611 struct ctdb_srvid_message msg = { 0 };
2612 struct ipreallocate_state state;
2615 msg.pnn = ctdb->pnn;
2616 msg.srvid = next_srvid(ctdb);
2619 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
2621 ipreallocate_handler, &state);
2627 ret = ctdb_message_takeover_run(mem_ctx, ctdb->ev,
2629 CTDB_BROADCAST_CONNECTED,
2635 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done,
2641 if (state.status >= 0) {
2650 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
2655 static int control_disable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2656 int argc, const char **argv)
2664 ret = check_flags(mem_ctx, ctdb, "disabled",
2665 NODE_FLAGS_PERMANENTLY_DISABLED, true);
2670 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2671 ctdb->cmd_pnn, TIMEOUT(),
2672 NODE_FLAGS_PERMANENTLY_DISABLED, 0);
2675 "Failed to set DISABLED flag on node %u\n",
2680 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, true);
2681 return ipreallocate(mem_ctx, ctdb);
2684 static int control_enable(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2685 int argc, const char **argv)
2693 ret = check_flags(mem_ctx, ctdb, "disabled",
2694 NODE_FLAGS_PERMANENTLY_DISABLED, false);
2699 ret = ctdb_ctrl_modflags(mem_ctx, ctdb->ev, ctdb->client,
2700 ctdb->cmd_pnn, TIMEOUT(),
2701 0, NODE_FLAGS_PERMANENTLY_DISABLED);
2703 fprintf(stderr, "Failed to reset DISABLED flag on node %u\n",
2708 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_PERMANENTLY_DISABLED, false);
2709 return ipreallocate(mem_ctx, ctdb);
2712 static int control_stop(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2713 int argc, const char **argv)
2721 ret = check_flags(mem_ctx, ctdb, "stopped",
2722 NODE_FLAGS_STOPPED, true);
2727 ret = ctdb_ctrl_stop_node(mem_ctx, ctdb->ev, ctdb->client,
2728 ctdb->cmd_pnn, TIMEOUT());
2730 fprintf(stderr, "Failed to stop node %u\n", ctdb->cmd_pnn);
2734 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, true);
2735 return ipreallocate(mem_ctx, ctdb);
2738 static int control_continue(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2739 int argc, const char **argv)
2747 ret = check_flags(mem_ctx, ctdb, "stopped",
2748 NODE_FLAGS_STOPPED, false);
2753 ret = ctdb_ctrl_continue_node(mem_ctx, ctdb->ev, ctdb->client,
2754 ctdb->cmd_pnn, TIMEOUT());
2756 fprintf(stderr, "Failed to continue stopped node %u\n",
2761 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_STOPPED, false);
2762 return ipreallocate(mem_ctx, ctdb);
2765 static int control_ban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2766 int argc, const char **argv)
2768 struct ctdb_ban_state ban_state;
2775 ret = check_flags(mem_ctx, ctdb, "banned",
2776 NODE_FLAGS_BANNED, true);
2781 ban_state.pnn = ctdb->cmd_pnn;
2782 ban_state.time = strtoul(argv[0], NULL, 0);
2784 if (ban_state.time == 0) {
2785 fprintf(stderr, "Ban time cannot be zero\n");
2789 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2790 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2792 fprintf(stderr, "Failed to ban node %u\n", ctdb->cmd_pnn);
2796 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, true);
2797 return ipreallocate(mem_ctx, ctdb);
2801 static int control_unban(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2802 int argc, const char **argv)
2804 struct ctdb_ban_state ban_state;
2811 ret = check_flags(mem_ctx, ctdb, "banned",
2812 NODE_FLAGS_BANNED, false);
2817 ban_state.pnn = ctdb->cmd_pnn;
2820 ret = ctdb_ctrl_set_ban_state(mem_ctx, ctdb->ev, ctdb->client,
2821 ctdb->cmd_pnn, TIMEOUT(), &ban_state);
2823 fprintf(stderr, "Failed to unban node %u\n", ctdb->cmd_pnn);
2827 wait_for_flags(mem_ctx, ctdb, NODE_FLAGS_BANNED, false);
2828 return ipreallocate(mem_ctx, ctdb);
2832 static void wait_for_shutdown(void *private_data)
2834 bool *done = (bool *)private_data;
2839 static int control_shutdown(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2840 int argc, const char **argv)
2849 if (ctdb->pnn == ctdb->cmd_pnn) {
2850 ctdb_client_set_disconnect_callback(ctdb->client,
2855 ret = ctdb_ctrl_shutdown(mem_ctx, ctdb->ev, ctdb->client,
2856 ctdb->cmd_pnn, TIMEOUT());
2858 fprintf(stderr, "Unable to shutdown node %u\n", ctdb->cmd_pnn);
2862 if (ctdb->pnn == ctdb->cmd_pnn) {
2863 ctdb_client_wait(ctdb->ev, &done);
2869 static int get_generation(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2870 uint32_t *generation)
2874 struct ctdb_vnn_map *vnnmap;
2878 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2879 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
2881 fprintf(stderr, "Failed to find recovery master\n");
2885 ret = ctdb_ctrl_get_recmode(mem_ctx, ctdb->ev, ctdb->client,
2886 recmaster, TIMEOUT(), &recmode);
2888 fprintf(stderr, "Failed to get recovery mode from node %u\n",
2893 if (recmode == CTDB_RECOVERY_ACTIVE) {
2898 ret = ctdb_ctrl_getvnnmap(mem_ctx, ctdb->ev, ctdb->client,
2899 recmaster, TIMEOUT(), &vnnmap);
2901 fprintf(stderr, "Failed to get generation from node %u\n",
2906 if (vnnmap->generation == INVALID_GENERATION) {
2907 talloc_free(vnnmap);
2912 *generation = vnnmap->generation;
2913 talloc_free(vnnmap);
2918 static int control_recover(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2919 int argc, const char **argv)
2921 uint32_t generation, next_generation;
2928 ret = get_generation(mem_ctx, ctdb, &generation);
2933 ret = ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
2934 ctdb->cmd_pnn, TIMEOUT(),
2935 CTDB_RECOVERY_ACTIVE);
2937 fprintf(stderr, "Failed to set recovery mode active\n");
2942 ret = get_generation(mem_ctx, ctdb, &next_generation);
2945 "Failed to confirm end of recovery\n");
2949 if (next_generation != generation) {
2959 static int control_ipreallocate(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2960 int argc, const char **argv)
2963 usage("ipreallocate");
2966 return ipreallocate(mem_ctx, ctdb);
2969 static int control_isnotrecmaster(TALLOC_CTX *mem_ctx,
2970 struct ctdb_context *ctdb,
2971 int argc, const char **argv)
2977 usage("isnotrecmaster");
2980 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
2981 ctdb->pnn, TIMEOUT(), &recmaster);
2983 fprintf(stderr, "Failed to get recmaster\n");
2987 if (recmaster != ctdb->pnn) {
2988 printf("this node is not the recmaster\n");
2992 printf("this node is the recmaster\n");
2996 static int control_gratarp(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
2997 int argc, const char **argv)
2999 struct ctdb_addr_info addr_info;
3006 ret = ctdb_sock_addr_from_string(argv[0], &addr_info.addr, false);
3008 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3011 addr_info.iface = argv[1];
3013 ret = ctdb_ctrl_send_gratuitous_arp(mem_ctx, ctdb->ev, ctdb->client,
3014 ctdb->cmd_pnn, TIMEOUT(),
3017 fprintf(stderr, "Unable to send gratuitous arp from node %u\n",
3025 static int control_tickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3026 int argc, const char **argv)
3028 ctdb_sock_addr src, dst;
3031 if (argc != 0 && argc != 2) {
3036 struct ctdb_connection_list *clist;
3038 unsigned int num_failed;
3040 /* Client first but the src/dst logic is confused */
3041 ret = ctdb_connection_list_read(mem_ctx, 0, false, &clist);
3047 for (i = 0; i < clist->num; i++) {
3048 ret = ctdb_sys_send_tcp(&clist->conn[i].src,
3049 &clist->conn[i].dst,
3058 if (num_failed > 0) {
3059 fprintf(stderr, "Failed to send %d tickles\n",
3068 ret = ctdb_sock_addr_from_string(argv[0], &src, true);
3070 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3074 ret = ctdb_sock_addr_from_string(argv[1], &dst, true);
3076 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3080 ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
3082 fprintf(stderr, "Failed to send tickle ack\n");
3089 static int control_gettickles(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3090 int argc, const char **argv)
3092 ctdb_sock_addr addr;
3093 struct ctdb_tickle_list *tickles;
3097 if (argc < 1 || argc > 2) {
3098 usage("gettickles");
3102 port = strtoul(argv[1], NULL, 10);
3105 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3107 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3110 ctdb_sock_addr_set_port(&addr, port);
3112 ret = ctdb_ctrl_get_tcp_tickle_list(mem_ctx, ctdb->ev, ctdb->client,
3113 ctdb->cmd_pnn, TIMEOUT(), &addr,
3116 fprintf(stderr, "Failed to get list of connections\n");
3120 if (options.machinereadable) {
3121 printf("%s%s%s%s%s%s%s%s%s\n",
3123 "Source IP", options.sep,
3124 "Port", options.sep,
3125 "Destiation IP", options.sep,
3126 "Port", options.sep);
3127 for (i=0; i<tickles->num; i++) {
3128 printf("%s%s%s%u%s%s%s%u%s\n", options.sep,
3129 ctdb_sock_addr_to_string(
3130 mem_ctx, &tickles->conn[i].src, false),
3132 ntohs(tickles->conn[i].src.ip.sin_port),
3134 ctdb_sock_addr_to_string(
3135 mem_ctx, &tickles->conn[i].dst, false),
3137 ntohs(tickles->conn[i].dst.ip.sin_port),
3141 printf("Connections for IP: %s\n",
3142 ctdb_sock_addr_to_string(mem_ctx,
3143 &tickles->addr, false));
3144 printf("Num connections: %u\n", tickles->num);
3145 for (i=0; i<tickles->num; i++) {
3146 printf("SRC: %s DST: %s\n",
3147 ctdb_sock_addr_to_string(
3148 mem_ctx, &tickles->conn[i].src, true),
3149 ctdb_sock_addr_to_string(
3150 mem_ctx, &tickles->conn[i].dst, true));
3154 talloc_free(tickles);
3158 typedef void (*clist_request_func)(struct ctdb_req_control *request,
3159 struct ctdb_connection *conn);
3161 typedef int (*clist_reply_func)(struct ctdb_reply_control *reply);
3163 struct process_clist_state {
3164 struct ctdb_connection_list *clist;
3166 int num_failed, num_total;
3167 clist_reply_func reply_func;
3170 static void process_clist_done(struct tevent_req *subreq);
3172 static struct tevent_req *process_clist_send(
3173 TALLOC_CTX *mem_ctx,
3174 struct ctdb_context *ctdb,
3175 struct ctdb_connection_list *clist,
3176 clist_request_func request_func,
3177 clist_reply_func reply_func)
3179 struct tevent_req *req, *subreq;
3180 struct process_clist_state *state;
3181 struct ctdb_req_control request;
3184 req = tevent_req_create(mem_ctx, &state, struct process_clist_state);
3189 state->clist = clist;
3190 state->reply_func = reply_func;
3192 for (i = 0; i < clist->num; i++) {
3193 request_func(&request, &clist->conn[i]);
3194 subreq = ctdb_client_control_send(state, ctdb->ev,
3195 ctdb->client, ctdb->cmd_pnn,
3196 TIMEOUT(), &request);
3197 if (tevent_req_nomem(subreq, req)) {
3198 return tevent_req_post(req, ctdb->ev);
3200 tevent_req_set_callback(subreq, process_clist_done, req);
3206 static void process_clist_done(struct tevent_req *subreq)
3208 struct tevent_req *req = tevent_req_callback_data(
3209 subreq, struct tevent_req);
3210 struct process_clist_state *state = tevent_req_data(
3211 req, struct process_clist_state);
3212 struct ctdb_reply_control *reply;
3216 status = ctdb_client_control_recv(subreq, NULL, state, &reply);
3217 TALLOC_FREE(subreq);
3219 state->num_failed += 1;
3223 ret = state->reply_func(reply);
3225 state->num_failed += 1;
3230 state->num_total += 1;
3231 if (state->num_total == state->clist->num) {
3232 tevent_req_done(req);
3236 static int process_clist_recv(struct tevent_req *req)
3238 struct process_clist_state *state = tevent_req_data(
3239 req, struct process_clist_state);
3241 return state->num_failed;
3244 static int control_addtickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3245 int argc, const char **argv)
3247 struct ctdb_connection conn;
3250 if (argc != 0 && argc != 2) {
3255 struct ctdb_connection_list *clist;
3256 struct tevent_req *req;
3258 /* Client first but the src/dst logic is confused */
3259 ret = ctdb_connection_list_read(mem_ctx, 0, false, &clist);
3263 if (clist->num == 0) {
3267 req = process_clist_send(mem_ctx, ctdb, clist,
3268 ctdb_req_control_tcp_add_delayed_update,
3269 ctdb_reply_control_tcp_add_delayed_update);
3275 tevent_req_poll(req, ctdb->ev);
3278 ret = process_clist_recv(req);
3280 fprintf(stderr, "Failed to add %d tickles\n", ret);
3287 ret = ctdb_sock_addr_from_string(argv[0], &conn.src, true);
3289 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3292 ret = ctdb_sock_addr_from_string(argv[1], &conn.dst, true);
3294 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3298 ret = ctdb_ctrl_tcp_add_delayed_update(mem_ctx, ctdb->ev,
3299 ctdb->client, ctdb->cmd_pnn,
3302 fprintf(stderr, "Failed to register connection\n");
3309 static int control_deltickle(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3310 int argc, const char **argv)
3312 struct ctdb_connection conn;
3315 if (argc != 0 && argc != 2) {
3320 struct ctdb_connection_list *clist;
3321 struct tevent_req *req;
3323 /* Client first but the src/dst logic is confused */
3324 ret = ctdb_connection_list_read(mem_ctx, 0, false, &clist);
3328 if (clist->num == 0) {
3332 req = process_clist_send(mem_ctx, ctdb, clist,
3333 ctdb_req_control_tcp_remove,
3334 ctdb_reply_control_tcp_remove);
3340 tevent_req_poll(req, ctdb->ev);
3343 ret = process_clist_recv(req);
3345 fprintf(stderr, "Failed to remove %d tickles\n", ret);
3352 ret = ctdb_sock_addr_from_string(argv[0], &conn.src, true);
3354 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3357 ret = ctdb_sock_addr_from_string(argv[1], &conn.dst, true);
3359 fprintf(stderr, "Invalid IP address %s\n", argv[1]);
3363 ret = ctdb_ctrl_tcp_remove(mem_ctx, ctdb->ev, ctdb->client,
3364 ctdb->cmd_pnn, TIMEOUT(), &conn);
3366 fprintf(stderr, "Failed to unregister connection\n");
3373 static int control_listnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3374 int argc, const char **argv)
3376 struct ctdb_node_map *nodemap;
3383 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
3384 if (nodemap == NULL) {
3388 for (i=0; i<nodemap->num; i++) {
3389 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
3393 if (options.machinereadable) {
3394 printf("%s%u%s%s%s\n", options.sep,
3395 nodemap->node[i].pnn, options.sep,
3396 ctdb_sock_addr_to_string(
3397 mem_ctx, &nodemap->node[i].addr, false),
3401 ctdb_sock_addr_to_string(
3402 mem_ctx, &nodemap->node[i].addr, false));
3409 static bool nodemap_identical(struct ctdb_node_map *nodemap1,
3410 struct ctdb_node_map *nodemap2)
3414 if (nodemap1->num != nodemap2->num) {
3418 for (i=0; i<nodemap1->num; i++) {
3419 struct ctdb_node_and_flags *n1, *n2;
3421 n1 = &nodemap1->node[i];
3422 n2 = &nodemap2->node[i];
3424 if ((n1->pnn != n2->pnn) ||
3425 (n1->flags != n2->flags) ||
3426 ! ctdb_sock_addr_same_ip(&n1->addr, &n2->addr)) {
3434 static int check_node_file_changes(TALLOC_CTX *mem_ctx,
3435 struct ctdb_node_map *nm,
3436 struct ctdb_node_map *fnm,
3440 bool check_failed = false;
3444 for (i=0; i<nm->num; i++) {
3445 if (i >= fnm->num) {
3447 "Node %u (%s) missing from nodes file\n",
3449 ctdb_sock_addr_to_string(
3450 mem_ctx, &nm->node[i].addr, false));
3451 check_failed = true;
3454 if (nm->node[i].flags & NODE_FLAGS_DELETED &&
3455 fnm->node[i].flags & NODE_FLAGS_DELETED) {
3456 /* Node remains deleted */
3460 if (! (nm->node[i].flags & NODE_FLAGS_DELETED) &&
3461 ! (fnm->node[i].flags & NODE_FLAGS_DELETED)) {
3462 /* Node not newly nor previously deleted */
3463 if (! ctdb_same_ip(&nm->node[i].addr,
3464 &fnm->node[i].addr)) {
3466 "Node %u has changed IP address"
3467 " (was %s, now %s)\n",
3469 ctdb_sock_addr_to_string(
3471 &nm->node[i].addr, false),
3472 ctdb_sock_addr_to_string(
3474 &fnm->node[i].addr, false));
3475 check_failed = true;
3477 if (nm->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3479 "WARNING: Node %u is disconnected."
3480 " You MUST fix this node manually!\n",
3487 if (fnm->node[i].flags & NODE_FLAGS_DELETED) {
3488 /* Node is being deleted */
3489 printf("Node %u is DELETED\n", nm->node[i].pnn);
3491 if (! (nm->node[i].flags & NODE_FLAGS_DISCONNECTED)) {
3493 "ERROR: Node %u is still connected\n",
3495 check_failed = true;
3500 if (nm->node[i].flags & NODE_FLAGS_DELETED) {
3501 /* Node was previously deleted */
3502 printf("Node %u is UNDELETED\n", nm->node[i].pnn);
3509 "ERROR: Nodes will not be reloaded due to previous error\n");
3513 /* Leftover nodes in file are NEW */
3514 for (; i < fnm->num; i++) {
3515 printf("Node %u is NEW\n", fnm->node[i].pnn);
3522 struct disable_recoveries_state {
3530 static void disable_recoveries_handler(uint64_t srvid, TDB_DATA data,
3533 struct disable_recoveries_state *state =
3534 (struct disable_recoveries_state *)private_data;
3537 if (data.dsize != sizeof(int)) {
3542 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
3543 ret = *(int *)data.dptr;
3545 state->status = ret;
3549 for (i=0; i<state->node_count; i++) {
3550 if (state->pnn_list[i] == ret) {
3551 state->reply[i] = true;
3557 for (i=0; i<state->node_count; i++) {
3558 if (! state->reply[i]) {
3559 state->done = false;
3565 static int disable_recoveries(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3566 uint32_t timeout, uint32_t *pnn_list, int count)
3568 struct ctdb_disable_message disable = { 0 };
3569 struct disable_recoveries_state state;
3572 disable.pnn = ctdb->pnn;
3573 disable.srvid = next_srvid(ctdb);
3574 disable.timeout = timeout;
3576 state.pnn_list = pnn_list;
3577 state.node_count = count;
3580 state.reply = talloc_zero_array(mem_ctx, bool, count);
3581 if (state.reply == NULL) {
3585 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
3587 disable_recoveries_handler,
3593 for (i=0; i<count; i++) {
3594 ret = ctdb_message_disable_recoveries(mem_ctx, ctdb->ev,
3603 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
3605 fprintf(stderr, "Timed out waiting to disable recoveries\n");
3607 ret = (state.status >= 0 ? 0 : 1);
3611 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
3612 disable.srvid, &state);
3616 static int control_reloadnodes(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3617 int argc, const char **argv)
3619 struct ctdb_node_map *nodemap = NULL;
3620 struct ctdb_node_map *file_nodemap;
3621 struct ctdb_node_map *remote_nodemap;
3622 struct ctdb_req_control request;
3623 struct ctdb_reply_control **reply;
3629 nodemap = get_nodemap(ctdb, false);
3630 if (nodemap == NULL) {
3634 file_nodemap = read_nodes_file(mem_ctx, ctdb->pnn);
3635 if (file_nodemap == NULL) {
3639 for (i=0; i<nodemap->num; i++) {
3640 if (nodemap->node[i].flags & NODE_FLAGS_DISCONNECTED) {
3644 ret = ctdb_ctrl_get_nodes_file(mem_ctx, ctdb->ev, ctdb->client,
3645 nodemap->node[i].pnn, TIMEOUT(),
3649 "ERROR: Failed to get nodes file from node %u\n",
3650 nodemap->node[i].pnn);
3654 if (! nodemap_identical(file_nodemap, remote_nodemap)) {
3656 "ERROR: Nodes file on node %u differs"
3657 " from current node (%u)\n",
3658 nodemap->node[i].pnn, ctdb->pnn);
3663 ret = check_node_file_changes(mem_ctx, nodemap, file_nodemap, &reload);
3669 fprintf(stderr, "No change in nodes file,"
3670 " skipping unnecessary reload\n");
3674 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
3675 mem_ctx, &pnn_list);
3677 fprintf(stderr, "Memory allocation error\n");
3681 ret = disable_recoveries(mem_ctx, ctdb, 2*options.timelimit,
3684 fprintf(stderr, "Failed to disable recoveries\n");
3688 ctdb_req_control_reload_nodes_file(&request);
3689 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3690 pnn_list, count, TIMEOUT(),
3691 &request, NULL, &reply);
3693 bool failed = false;
3695 for (i=0; i<count; i++) {
3696 ret = ctdb_reply_control_reload_nodes_file(reply[i]);
3699 "Node %u failed to reload nodes\n",
3706 "You MUST fix failed nodes manually!\n");
3710 ret = disable_recoveries(mem_ctx, ctdb, 0, pnn_list, count);
3712 fprintf(stderr, "Failed to enable recoveries\n");
3719 static int moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3720 ctdb_sock_addr *addr, uint32_t pnn)
3722 struct ctdb_public_ip_list *pubip_list;
3723 struct ctdb_public_ip pubip;
3724 struct ctdb_node_map *nodemap;
3725 struct ctdb_req_control request;
3729 ret = ctdb_message_disable_ip_check(mem_ctx, ctdb->ev, ctdb->client,
3730 CTDB_BROADCAST_CONNECTED,
3731 2*options.timelimit);
3733 fprintf(stderr, "Failed to disable IP check\n");
3737 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3738 pnn, TIMEOUT(), false, &pubip_list);
3740 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3745 for (i=0; i<pubip_list->num; i++) {
3746 if (ctdb_same_ip(addr, &pubip_list->ip[i].addr)) {
3751 if (i == pubip_list->num) {
3752 fprintf(stderr, "Node %u CANNOT host IP address %s\n",
3753 pnn, ctdb_sock_addr_to_string(mem_ctx, addr, false));
3757 nodemap = get_nodemap(ctdb, false);
3758 if (nodemap == NULL) {
3762 count = list_of_active_nodes(nodemap, pnn, mem_ctx, &pnn_list);
3764 fprintf(stderr, "Memory allocation error\n");
3770 ctdb_req_control_release_ip(&request, &pubip);
3772 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
3773 pnn_list, count, TIMEOUT(),
3774 &request, NULL, NULL);
3776 fprintf(stderr, "Failed to release IP on nodes\n");
3780 ret = ctdb_ctrl_takeover_ip(mem_ctx, ctdb->ev, ctdb->client,
3781 pnn, TIMEOUT(), &pubip);
3783 fprintf(stderr, "Failed to takeover IP on node %u\n", pnn);
3790 static int control_moveip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3791 int argc, const char **argv)
3793 ctdb_sock_addr addr;
3795 int ret, retries = 0;
3801 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3803 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3807 pnn = strtoul(argv[1], NULL, 10);
3808 if (pnn == CTDB_UNKNOWN_PNN) {
3809 fprintf(stderr, "Invalid PNN %s\n", argv[1]);
3813 while (retries < 5) {
3814 ret = moveip(mem_ctx, ctdb, &addr, pnn);
3824 fprintf(stderr, "Failed to move IP %s to node %u\n",
3832 static int rebalancenode(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3837 ret = ctdb_message_rebalance_node(mem_ctx, ctdb->ev, ctdb->client,
3838 CTDB_BROADCAST_CONNECTED, pnn);
3841 "Failed to ask recovery master to distribute IPs\n");
3848 static int control_addip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3849 int argc, const char **argv)
3851 ctdb_sock_addr addr;
3852 struct ctdb_public_ip_list *pubip_list;
3853 struct ctdb_addr_info addr_info;
3855 int ret, i, retries = 0;
3861 ret = ctdb_sock_addr_mask_from_string(argv[0], &addr, &mask);
3863 fprintf(stderr, "Invalid IP/Mask %s\n", argv[0]);
3867 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3868 ctdb->cmd_pnn, TIMEOUT(),
3869 false, &pubip_list);
3871 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3876 for (i=0; i<pubip_list->num; i++) {
3877 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3878 fprintf(stderr, "Node already knows about IP %s\n",
3879 ctdb_sock_addr_to_string(mem_ctx,
3885 addr_info.addr = addr;
3886 addr_info.mask = mask;
3887 addr_info.iface = argv[1];
3889 while (retries < 5) {
3890 ret = ctdb_ctrl_add_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3891 ctdb->cmd_pnn, TIMEOUT(),
3902 fprintf(stderr, "Failed to add public IP to node %u."
3903 " Giving up\n", ctdb->cmd_pnn);
3907 ret = rebalancenode(mem_ctx, ctdb, ctdb->cmd_pnn);
3915 static int control_delip(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
3916 int argc, const char **argv)
3918 ctdb_sock_addr addr;
3919 struct ctdb_public_ip_list *pubip_list;
3920 struct ctdb_addr_info addr_info;
3927 ret = ctdb_sock_addr_from_string(argv[0], &addr, false);
3929 fprintf(stderr, "Invalid IP address %s\n", argv[0]);
3933 ret = ctdb_ctrl_get_public_ips(mem_ctx, ctdb->ev, ctdb->client,
3934 ctdb->cmd_pnn, TIMEOUT(),
3935 false, &pubip_list);
3937 fprintf(stderr, "Failed to get Public IPs from node %u\n",
3942 for (i=0; i<pubip_list->num; i++) {
3943 if (ctdb_same_ip(&addr, &pubip_list->ip[i].addr)) {
3948 if (i == pubip_list->num) {
3949 fprintf(stderr, "Node does not know about IP address %s\n",
3950 ctdb_sock_addr_to_string(mem_ctx, &addr, false));
3954 addr_info.addr = addr;
3956 addr_info.iface = NULL;
3958 ret = ctdb_ctrl_del_public_ip(mem_ctx, ctdb->ev, ctdb->client,
3959 ctdb->cmd_pnn, TIMEOUT(), &addr_info);
3961 fprintf(stderr, "Failed to delete public IP from node %u\n",
3969 #define DB_VERSION 3
3970 #define MAX_DB_NAME 64
3971 #define MAX_REC_BUFFER_SIZE (100*1000)
3974 unsigned long version;
3976 unsigned long flags;
3979 char name[MAX_DB_NAME];
3982 struct backup_state {
3983 TALLOC_CTX *mem_ctx;
3984 struct ctdb_rec_buffer *recbuf;
3987 unsigned int nbuf, nrec;
3990 static int backup_handler(uint32_t reqid, struct ctdb_ltdb_header *header,
3991 TDB_DATA key, TDB_DATA data, void *private_data)
3993 struct backup_state *state = (struct backup_state *)private_data;
3997 if (state->recbuf == NULL) {
3998 state->recbuf = ctdb_rec_buffer_init(state->mem_ctx,
4000 if (state->recbuf == NULL) {
4005 ret = ctdb_rec_buffer_add(state->recbuf, state->recbuf, reqid,
4011 len = ctdb_rec_buffer_len(state->recbuf);
4012 if (len < MAX_REC_BUFFER_SIZE) {
4016 ret = ctdb_rec_buffer_write(state->recbuf, state->fd);
4018 fprintf(stderr, "Failed to write records to backup file\n");
4023 state->nrec += state->recbuf->count;
4024 TALLOC_FREE(state->recbuf);
4029 static int control_backupdb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4030 int argc, const char **argv)
4032 const char *db_name;
4033 struct ctdb_db_context *db;
4036 struct backup_state state;
4037 struct db_header db_hdr;
4044 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4048 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4051 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4055 fd = open(argv[1], O_RDWR|O_CREAT, 0600);
4058 fprintf(stderr, "Failed to open file %s for writing\n",
4063 /* Write empty header first */
4064 ZERO_STRUCT(db_hdr);
4065 ret = write(fd, &db_hdr, sizeof(struct db_header));
4069 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4073 state.mem_ctx = mem_ctx;
4074 state.recbuf = NULL;
4079 ret = ctdb_db_traverse_local(db, true, false, backup_handler, &state);
4081 fprintf(stderr, "Failed to collect records from DB %s\n",
4087 if (state.recbuf != NULL) {
4088 ret = ctdb_rec_buffer_write(state.recbuf, state.fd);
4091 "Failed to write records to backup file\n");
4097 state.nrec += state.recbuf->count;
4098 TALLOC_FREE(state.recbuf);
4101 db_hdr.version = DB_VERSION;
4102 db_hdr.timestamp = time(NULL);
4103 db_hdr.flags = db_flags;
4104 db_hdr.nbuf = state.nbuf;
4105 db_hdr.nrec = state.nrec;
4106 strncpy(db_hdr.name, db_name, MAX_DB_NAME-1);
4108 lseek(fd, 0, SEEK_SET);
4109 ret = write(fd, &db_hdr, sizeof(struct db_header));
4113 fprintf(stderr, "Failed to write header to file %s\n", argv[1]);
4118 printf("Database backed up to %s\n", argv[1]);
4122 static int control_restoredb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4123 int argc, const char **argv)
4125 const char *db_name = NULL;
4126 struct ctdb_db_context *db;
4127 struct db_header db_hdr;
4128 struct ctdb_node_map *nodemap;
4129 struct ctdb_req_control request;
4130 struct ctdb_reply_control **reply;
4131 struct ctdb_transdb wipedb;
4132 struct ctdb_pulldb_ext pulldb;
4133 struct ctdb_rec_buffer *recbuf;
4134 uint32_t generation;
4142 if (argc < 1 || argc > 2) {
4146 fd = open(argv[0], O_RDONLY, 0600);
4149 fprintf(stderr, "Failed to open file %s for reading\n",
4158 n = read(fd, &db_hdr, sizeof(struct db_header));
4162 fprintf(stderr, "Failed to read db header from file %s\n",
4166 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4168 if (db_hdr.version != DB_VERSION) {
4170 "Wrong version of backup file, expected %u, got %lu\n",
4171 DB_VERSION, db_hdr.version);
4176 if (db_name == NULL) {
4177 db_name = db_hdr.name;
4180 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4181 localtime(&db_hdr.timestamp));
4182 printf("Restoring database %s from backup @ %s\n", db_name, timebuf);
4184 db_flags = db_hdr.flags & 0xff;
4185 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4188 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4193 nodemap = get_nodemap(ctdb, false);
4194 if (nodemap == NULL) {
4195 fprintf(stderr, "Failed to get nodemap\n");
4200 ret = get_generation(mem_ctx, ctdb, &generation);
4202 fprintf(stderr, "Failed to get current generation\n");
4207 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4214 wipedb.db_id = ctdb_db_id(db);
4215 wipedb.tid = generation;
4217 ctdb_req_control_db_freeze(&request, wipedb.db_id);
4218 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4219 ctdb->client, pnn_list, count,
4220 TIMEOUT(), &request, NULL, NULL);
4226 ctdb_req_control_db_transaction_start(&request, &wipedb);
4227 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4228 pnn_list, count, TIMEOUT(),
4229 &request, NULL, NULL);
4234 ctdb_req_control_wipe_database(&request, &wipedb);
4235 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4236 pnn_list, count, TIMEOUT(),
4237 &request, NULL, NULL);
4242 pulldb.db_id = ctdb_db_id(db);
4244 pulldb.srvid = SRVID_CTDB_PUSHDB;
4246 ctdb_req_control_db_push_start(&request, &pulldb);
4247 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4248 pnn_list, count, TIMEOUT(),
4249 &request, NULL, NULL);
4254 for (i=0; i<db_hdr.nbuf; i++) {
4255 struct ctdb_req_message message;
4259 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4264 data.dsize = ctdb_rec_buffer_len(recbuf);
4265 data.dptr = talloc_size(mem_ctx, data.dsize);
4266 if (data.dptr == NULL) {
4270 ctdb_rec_buffer_push(recbuf, data.dptr, &np);
4272 message.srvid = pulldb.srvid;
4273 message.data.data = data;
4275 ret = ctdb_client_message_multi(mem_ctx, ctdb->ev,
4283 talloc_free(recbuf);
4284 talloc_free(data.dptr);
4287 ctdb_req_control_db_push_confirm(&request, pulldb.db_id);
4288 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4289 pnn_list, count, TIMEOUT(),
4290 &request, NULL, &reply);
4295 for (i=0; i<count; i++) {
4296 uint32_t num_records;
4298 ret = ctdb_reply_control_db_push_confirm(reply[i],
4301 fprintf(stderr, "Invalid response from node %u\n",
4306 if (num_records != db_hdr.nrec) {
4307 fprintf(stderr, "Node %u received %u of %lu records\n",
4308 pnn_list[i], num_records, db_hdr.nrec);
4313 ctdb_req_control_db_set_healthy(&request, wipedb.db_id);
4314 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4315 pnn_list, count, TIMEOUT(),
4316 &request, NULL, NULL);
4321 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4322 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4323 pnn_list, count, TIMEOUT(),
4324 &request, NULL, NULL);
4329 ctdb_req_control_db_thaw(&request, wipedb.db_id);
4330 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4331 ctdb->client, pnn_list, count,
4332 TIMEOUT(), &request, NULL, NULL);
4337 printf("Database %s restored\n", db_name);
4344 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4345 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4349 struct dumpdbbackup_state {
4350 ctdb_rec_parser_func_t parser;
4351 struct dump_record_state sub_state;
4354 static int dumpdbbackup_handler(uint32_t reqid,
4355 struct ctdb_ltdb_header *header,
4356 TDB_DATA key, TDB_DATA data,
4359 struct dumpdbbackup_state *state =
4360 (struct dumpdbbackup_state *)private_data;
4361 struct ctdb_ltdb_header hdr;
4364 ret = ctdb_ltdb_header_extract(&data, &hdr);
4369 return state->parser(reqid, &hdr, key, data, &state->sub_state);
4372 static int control_dumpdbbackup(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4373 int argc, const char **argv)
4375 struct db_header db_hdr;
4377 struct dumpdbbackup_state state;
4382 usage("dumpbackup");
4385 fd = open(argv[0], O_RDONLY, 0600);
4388 fprintf(stderr, "Failed to open file %s for reading\n",
4393 n = read(fd, &db_hdr, sizeof(struct db_header));
4397 fprintf(stderr, "Failed to read db header from file %s\n",
4401 db_hdr.name[sizeof(db_hdr.name)-1] = '\0';
4403 if (db_hdr.version != DB_VERSION) {
4405 "Wrong version of backup file, expected %u, got %lu\n",
4406 DB_VERSION, db_hdr.version);
4411 strftime(timebuf, sizeof(timebuf)-1, "%Y/%m/%d %H:%M:%S",
4412 localtime(&db_hdr.timestamp));
4413 printf("Dumping database %s from backup @ %s\n",
4414 db_hdr.name, timebuf);
4416 state.parser = dump_record;
4417 state.sub_state.count = 0;
4419 for (i=0; i<db_hdr.nbuf; i++) {
4420 struct ctdb_rec_buffer *recbuf;
4422 ret = ctdb_rec_buffer_read(fd, mem_ctx, &recbuf);
4424 fprintf(stderr, "Failed to read records\n");
4429 ret = ctdb_rec_buffer_traverse(recbuf, dumpdbbackup_handler,
4432 fprintf(stderr, "Failed to dump records\n");
4439 printf("Dumped %u record(s)\n", state.sub_state.count);
4443 static int control_wipedb(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4444 int argc, const char **argv)
4446 const char *db_name;
4447 struct ctdb_db_context *db;
4450 struct ctdb_node_map *nodemap;
4451 struct ctdb_req_control request;
4452 struct ctdb_transdb wipedb;
4453 uint32_t generation;
4461 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, &db_flags)) {
4465 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4468 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4472 nodemap = get_nodemap(ctdb, false);
4473 if (nodemap == NULL) {
4474 fprintf(stderr, "Failed to get nodemap\n");
4478 ret = get_generation(mem_ctx, ctdb, &generation);
4480 fprintf(stderr, "Failed to get current generation\n");
4484 count = list_of_active_nodes(nodemap, CTDB_UNKNOWN_PNN, mem_ctx,
4490 ctdb_req_control_db_freeze(&request, db_id);
4491 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4492 ctdb->client, pnn_list, count,
4493 TIMEOUT(), &request, NULL, NULL);
4498 wipedb.db_id = db_id;
4499 wipedb.tid = generation;
4501 ctdb_req_control_db_transaction_start(&request, &wipedb);
4502 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4503 pnn_list, count, TIMEOUT(),
4504 &request, NULL, NULL);
4509 ctdb_req_control_wipe_database(&request, &wipedb);
4510 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4511 pnn_list, count, TIMEOUT(),
4512 &request, NULL, NULL);
4517 ctdb_req_control_db_set_healthy(&request, db_id);
4518 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4519 pnn_list, count, TIMEOUT(),
4520 &request, NULL, NULL);
4525 ctdb_req_control_db_transaction_commit(&request, &wipedb);
4526 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
4527 pnn_list, count, TIMEOUT(),
4528 &request, NULL, NULL);
4533 ctdb_req_control_db_thaw(&request, db_id);
4534 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev,
4535 ctdb->client, pnn_list, count,
4536 TIMEOUT(), &request, NULL, NULL);
4541 printf("Database %s wiped\n", db_name);
4546 ctdb_ctrl_set_recmode(mem_ctx, ctdb->ev, ctdb->client,
4547 ctdb->pnn, TIMEOUT(), CTDB_RECOVERY_ACTIVE);
4551 static int control_recmaster(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4552 int argc, const char **argv)
4557 ret = ctdb_ctrl_get_recmaster(mem_ctx, ctdb->ev, ctdb->client,
4558 ctdb->cmd_pnn, TIMEOUT(), &recmaster);
4563 printf("%u\n", recmaster);
4567 static int control_event(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4568 int argc, const char **argv)
4570 char *t, *event_helper = NULL;
4572 t = getenv("CTDB_EVENT_HELPER");
4574 event_helper = talloc_strdup(mem_ctx, t);
4576 event_helper = talloc_asprintf(mem_ctx, "%s/ctdb-event",
4577 CTDB_HELPER_BINDIR);
4580 if (event_helper == NULL) {
4581 fprintf(stderr, "Unable to set event daemon helper\n");
4585 return run_helper(mem_ctx, "event daemon helper", event_helper,
4589 static int control_scriptstatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4590 int argc, const char **argv)
4592 const char *new_argv[4];
4595 usage("scriptstatus");
4598 new_argv[0] = "status";
4599 new_argv[1] = "legacy";
4600 new_argv[2] = (argc == 0) ? "monitor" : argv[0];
4603 (void) control_event(mem_ctx, ctdb, 3, new_argv);
4607 static int control_natgw(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4608 int argc, const char **argv)
4610 char *t, *natgw_helper = NULL;
4616 t = getenv("CTDB_NATGW_HELPER");
4618 natgw_helper = talloc_strdup(mem_ctx, t);
4620 natgw_helper = talloc_asprintf(mem_ctx, "%s/ctdb_natgw",
4621 CTDB_HELPER_BINDIR);
4624 if (natgw_helper == NULL) {
4625 fprintf(stderr, "Unable to set NAT gateway helper\n");
4629 return run_helper(mem_ctx, "NAT gateway helper", natgw_helper,
4634 * Find the PNN of the current node
4635 * discover the pnn by loading the nodes file and try to bind
4636 * to all addresses one at a time until the ip address is found.
4638 static bool find_node_xpnn(TALLOC_CTX *mem_ctx, uint32_t *pnn)
4640 struct ctdb_node_map *nodemap;
4643 nodemap = read_nodes_file(mem_ctx, CTDB_UNKNOWN_PNN);
4644 if (nodemap == NULL) {
4648 for (i=0; i<nodemap->num; i++) {
4649 if (nodemap->node[i].flags & NODE_FLAGS_DELETED) {
4652 if (ctdb_sys_have_ip(&nodemap->node[i].addr)) {
4654 *pnn = nodemap->node[i].pnn;
4656 talloc_free(nodemap);
4661 fprintf(stderr, "Failed to detect PNN of the current node.\n");
4662 talloc_free(nodemap);
4666 static int control_getreclock(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4667 int argc, const char **argv)
4669 const char *reclock;
4673 usage("getreclock");
4676 ret = ctdb_ctrl_get_reclock_file(mem_ctx, ctdb->ev, ctdb->client,
4677 ctdb->cmd_pnn, TIMEOUT(), &reclock);
4682 if (reclock != NULL) {
4683 printf("%s\n", reclock);
4689 static int control_setlmasterrole(TALLOC_CTX *mem_ctx,
4690 struct ctdb_context *ctdb,
4691 int argc, const char **argv)
4693 uint32_t lmasterrole = 0;
4697 usage("setlmasterrole");
4700 if (strcmp(argv[0], "on") == 0) {
4702 } else if (strcmp(argv[0], "off") == 0) {
4705 usage("setlmasterrole");
4708 ret = ctdb_ctrl_set_lmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4709 ctdb->cmd_pnn, TIMEOUT(), lmasterrole);
4717 static int control_setrecmasterrole(TALLOC_CTX *mem_ctx,
4718 struct ctdb_context *ctdb,
4719 int argc, const char **argv)
4721 uint32_t recmasterrole = 0;
4725 usage("setrecmasterrole");
4728 if (strcmp(argv[0], "on") == 0) {
4730 } else if (strcmp(argv[0], "off") == 0) {
4733 usage("setrecmasterrole");
4736 ret = ctdb_ctrl_set_recmasterrole(mem_ctx, ctdb->ev, ctdb->client,
4737 ctdb->cmd_pnn, TIMEOUT(),
4746 static int control_setdbreadonly(TALLOC_CTX *mem_ctx,
4747 struct ctdb_context *ctdb,
4748 int argc, const char **argv)
4755 usage("setdbreadonly");
4758 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4762 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
4763 fprintf(stderr, "READONLY can be set only on volatile DB\n");
4767 ret = ctdb_ctrl_set_db_readonly(mem_ctx, ctdb->ev, ctdb->client,
4768 ctdb->cmd_pnn, TIMEOUT(), db_id);
4776 static int control_setdbsticky(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4777 int argc, const char **argv)
4784 usage("setdbsticky");
4787 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, NULL, &db_flags)) {
4791 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
4792 fprintf(stderr, "STICKY can be set only on volatile DB\n");
4796 ret = ctdb_ctrl_set_db_sticky(mem_ctx, ctdb->ev, ctdb->client,
4797 ctdb->cmd_pnn, TIMEOUT(), db_id);
4805 static int control_pfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4806 int argc, const char **argv)
4808 const char *db_name;
4809 struct ctdb_db_context *db;
4810 struct ctdb_transaction_handle *h;
4819 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4824 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4825 fprintf(stderr, "Transactions not supported on DB %s\n",
4830 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4833 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4837 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4839 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4843 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4844 TIMEOUT(), db, true, &h);
4846 fprintf(stderr, "Failed to start transaction on db %s\n",
4851 ret = ctdb_transaction_fetch_record(h, key, mem_ctx, &data);
4853 fprintf(stderr, "Failed to read record for key %s\n",
4855 ctdb_transaction_cancel(h);
4859 printf("%.*s\n", (int)data.dsize, data.dptr);
4861 ctdb_transaction_cancel(h);
4865 static int control_pstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4866 int argc, const char **argv)
4868 const char *db_name;
4869 struct ctdb_db_context *db;
4870 struct ctdb_transaction_handle *h;
4879 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4884 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4885 fprintf(stderr, "Transactions not supported on DB %s\n",
4890 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4893 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4897 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4899 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4903 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
4905 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
4909 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4910 TIMEOUT(), db, false, &h);
4912 fprintf(stderr, "Failed to start transaction on db %s\n",
4917 ret = ctdb_transaction_store_record(h, key, data);
4919 fprintf(stderr, "Failed to store record for key %s\n",
4921 ctdb_transaction_cancel(h);
4925 ret = ctdb_transaction_commit(h);
4927 fprintf(stderr, "Failed to commit transaction on db %s\n",
4929 ctdb_transaction_cancel(h);
4936 static int control_pdelete(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
4937 int argc, const char **argv)
4939 const char *db_name;
4940 struct ctdb_db_context *db;
4941 struct ctdb_transaction_handle *h;
4950 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
4955 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
4956 fprintf(stderr, "Transactions not supported on DB %s\n",
4961 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
4964 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
4968 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
4970 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
4974 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
4975 TIMEOUT(), db, false, &h);
4977 fprintf(stderr, "Failed to start transaction on db %s\n",
4982 ret = ctdb_transaction_delete_record(h, key);
4984 fprintf(stderr, "Failed to delete record for key %s\n",
4986 ctdb_transaction_cancel(h);
4990 ret = ctdb_transaction_commit(h);
4992 fprintf(stderr, "Failed to commit transaction on db %s\n",
4994 ctdb_transaction_cancel(h);
5001 static int ptrans_parse_string(TALLOC_CTX *mem_ctx, const char **ptr, TDB_DATA *data)
5009 /* Skip whitespace */
5010 n = strspn(*ptr, " \t");
5014 /* Quoted ASCII string - no wide characters! */
5016 n = strcspn(t, "\"");
5019 ret = str_to_data(t, n, mem_ctx, data);
5026 fprintf(stderr, "Unmatched \" in input %s\n", *ptr);
5030 fprintf(stderr, "Unsupported input format in %s\n", *ptr);
5037 #define MAX_LINE_SIZE 1024
5039 static bool ptrans_get_key_value(TALLOC_CTX *mem_ctx, FILE *file,
5040 TDB_DATA *key, TDB_DATA *value)
5042 char line [MAX_LINE_SIZE]; /* FIXME: make this more flexible? */
5046 ptr = fgets(line, MAX_LINE_SIZE, file);
5052 ret = ptrans_parse_string(mem_ctx, &ptr, key);
5053 if (ret != 0 || ptr == NULL || key->dptr == NULL) {
5054 /* Line Ignored but not EOF */
5060 ret = ptrans_parse_string(mem_ctx, &ptr, value);
5062 /* Line Ignored but not EOF */
5063 talloc_free(key->dptr);
5071 static int control_ptrans(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5072 int argc, const char **argv)
5074 const char *db_name;
5075 struct ctdb_db_context *db;
5076 struct ctdb_transaction_handle *h;
5079 TDB_DATA key, value;
5082 if (argc < 1 || argc > 2) {
5086 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5091 (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED))) {
5092 fprintf(stderr, "Transactions not supported on DB %s\n",
5098 file = fopen(argv[1], "r");
5100 fprintf(stderr, "Failed to open file %s\n", argv[1]);
5107 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5110 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5114 ret = ctdb_transaction_start(mem_ctx, ctdb->ev, ctdb->client,
5115 TIMEOUT(), db, false, &h);
5117 fprintf(stderr, "Failed to start transaction on db %s\n",
5122 while (ptrans_get_key_value(mem_ctx, file, &key, &value)) {
5123 if (key.dsize != 0) {
5124 ret = ctdb_transaction_store_record(h, key, value);
5126 fprintf(stderr, "Failed to store record\n");
5127 ctdb_transaction_cancel(h);
5130 talloc_free(key.dptr);
5131 talloc_free(value.dptr);
5135 ret = ctdb_transaction_commit(h);
5137 fprintf(stderr, "Failed to commit transaction on db %s\n",
5139 ctdb_transaction_cancel(h);
5143 if (file != stdin) {
5149 static int control_tfetch(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5150 int argc, const char **argv)
5152 struct tdb_context *tdb;
5154 struct ctdb_ltdb_header header;
5157 if (argc < 2 || argc > 3) {
5161 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5163 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5167 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5169 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5174 data = tdb_fetch(tdb, key);
5175 if (data.dptr == NULL) {
5176 fprintf(stderr, "No record for key %s\n", argv[1]);
5181 if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
5182 fprintf(stderr, "Invalid record for key %s\n", argv[1]);
5193 fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
5195 fprintf(stderr, "Failed to open output file %s\n",
5200 nwritten = sys_write(fd, data.dptr, data.dsize);
5201 if (nwritten != data.dsize) {
5202 fprintf(stderr, "Failed to write record to file\n");
5211 ret = ctdb_ltdb_header_extract(&data, &header);
5213 fprintf(stderr, "Failed to parse header from data\n");
5217 dump_ltdb_header(&header);
5218 dump_tdb_data("data", data);
5223 static int control_tstore(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5224 int argc, const char **argv)
5226 struct tdb_context *tdb;
5227 TDB_DATA key, data[2], value;
5228 struct ctdb_ltdb_header header;
5229 uint8_t header_buf[sizeof(struct ctdb_ltdb_header)];
5233 if (argc < 3 || argc > 5) {
5237 tdb = tdb_open(argv[0], 0, 0, O_RDWR, 0);
5239 fprintf(stderr, "Failed to open TDB file %s\n", argv[0]);
5243 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5245 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5250 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &value);
5252 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5257 ZERO_STRUCT(header);
5260 header.rsn = (uint64_t)strtoull(argv[3], NULL, 0);
5263 header.dmaster = (uint32_t)atol(argv[4]);
5266 header.flags = (uint32_t)atol(argv[5]);
5269 ctdb_ltdb_header_push(&header, header_buf, &np);
5272 data[0].dptr = header_buf;
5274 data[1].dsize = value.dsize;
5275 data[1].dptr = value.dptr;
5277 ret = tdb_storev(tdb, key, data, 2, TDB_REPLACE);
5279 fprintf(stderr, "Failed to write record %s to file %s\n",
5288 static int control_readkey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5289 int argc, const char **argv)
5291 const char *db_name;
5292 struct ctdb_db_context *db;
5293 struct ctdb_record_handle *h;
5296 bool readonly = false;
5299 if (argc < 2 || argc > 3) {
5304 if (strcmp(argv[2], "readonly") == 0) {
5311 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5315 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5316 fprintf(stderr, "DB %s is not a volatile database\n",
5321 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5324 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5328 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5330 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5334 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5335 db, key, readonly, &h, NULL, &data);
5337 fprintf(stderr, "Failed to read record for key %s\n",
5340 printf("Data: size:%zu ptr:[%.*s]\n", data.dsize,
5341 (int)data.dsize, data.dptr);
5348 static int control_writekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5349 int argc, const char **argv)
5351 const char *db_name;
5352 struct ctdb_db_context *db;
5353 struct ctdb_record_handle *h;
5362 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5366 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5367 fprintf(stderr, "DB %s is not a volatile database\n",
5372 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5375 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5379 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5381 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5385 ret = str_to_data(argv[2], strlen(argv[2]), mem_ctx, &data);
5387 fprintf(stderr, "Failed to parse value %s\n", argv[2]);
5391 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5392 db, key, false, &h, NULL, NULL);
5394 fprintf(stderr, "Failed to lock record for key %s\n", argv[0]);
5398 ret = ctdb_store_record(h, data);
5400 fprintf(stderr, "Failed to store record for key %s\n",
5408 static int control_deletekey(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5409 int argc, const char **argv)
5411 const char *db_name;
5412 struct ctdb_db_context *db;
5413 struct ctdb_record_handle *h;
5422 if (! db_exists(mem_ctx, ctdb, argv[0], NULL, &db_name, &db_flags)) {
5426 if (db_flags & (CTDB_DB_FLAGS_PERSISTENT | CTDB_DB_FLAGS_REPLICATED)) {
5427 fprintf(stderr, "DB %s is not a volatile database\n",
5432 ret = ctdb_attach(ctdb->ev, ctdb->client, TIMEOUT(), db_name,
5435 fprintf(stderr, "Failed to attach to DB %s\n", db_name);
5439 ret = str_to_data(argv[1], strlen(argv[1]), mem_ctx, &key);
5441 fprintf(stderr, "Failed to parse key %s\n", argv[1]);
5445 ret = ctdb_fetch_lock(mem_ctx, ctdb->ev, ctdb->client,
5446 db, key, false, &h, NULL, &data);
5448 fprintf(stderr, "Failed to fetch record for key %s\n",
5453 ret = ctdb_delete_record(h);
5455 fprintf(stderr, "Failed to delete record for key %s\n",
5463 static int control_checktcpport(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5464 int argc, const char **argv)
5466 struct sockaddr_in sin;
5472 usage("chktcpport");
5475 port = atoi(argv[0]);
5477 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
5479 fprintf(stderr, "Failed to open local socket\n");
5483 v = fcntl(s, F_GETFL, 0);
5484 if (v == -1 || fcntl(s, F_SETFL, v | O_NONBLOCK)) {
5485 fprintf(stderr, "Unable to set socket non-blocking\n");
5490 bzero(&sin, sizeof(sin));
5491 sin.sin_family = AF_INET;
5492 sin.sin_port = htons(port);
5493 ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
5496 fprintf(stderr, "Failed to bind to TCP port %u\n", port);
5503 static int control_getdbseqnum(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5504 int argc, const char **argv)
5507 const char *db_name;
5512 usage("getdbseqnum");
5515 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5519 ret = ctdb_ctrl_get_db_seqnum(mem_ctx, ctdb->ev, ctdb->client,
5520 ctdb->cmd_pnn, TIMEOUT(), db_id,
5523 fprintf(stderr, "Failed to get sequence number for DB %s\n",
5528 printf("0x%"PRIx64"\n", seqnum);
5532 static int control_nodestatus(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5533 int argc, const char **argv)
5535 const char *nodestring = NULL;
5536 struct ctdb_node_map *nodemap;
5538 bool print_hdr = false;
5541 usage("nodestatus");
5545 nodestring = argv[0];
5546 if (strcmp(nodestring, "all") == 0) {
5551 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap)) {
5555 if (options.machinereadable) {
5556 print_nodemap_machine(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn);
5558 print_nodemap(mem_ctx, ctdb, nodemap, ctdb->cmd_pnn, print_hdr);
5562 for (i=0; i<nodemap->num; i++) {
5563 ret |= nodemap->node[i].flags;
5572 } db_stats_fields[] = {
5573 #define DBSTATISTICS_FIELD(n) { #n, offsetof(struct ctdb_db_statistics, n) }
5574 DBSTATISTICS_FIELD(db_ro_delegations),
5575 DBSTATISTICS_FIELD(db_ro_revokes),
5576 DBSTATISTICS_FIELD(locks.num_calls),
5577 DBSTATISTICS_FIELD(locks.num_current),
5578 DBSTATISTICS_FIELD(locks.num_pending),
5579 DBSTATISTICS_FIELD(locks.num_failed),
5582 static void print_dbstatistics(const char *db_name,
5583 struct ctdb_db_statistics *s)
5586 const char *prefix = NULL;
5589 printf("DB Statistics %s\n", db_name);
5591 for (i=0; i<ARRAY_SIZE(db_stats_fields); i++) {
5592 if (strchr(db_stats_fields[i].name, '.') != NULL) {
5593 preflen = strcspn(db_stats_fields[i].name, ".") + 1;
5595 strncmp(prefix, db_stats_fields[i].name, preflen) != 0) {
5596 prefix = db_stats_fields[i].name;
5597 printf(" %*.*s\n", preflen-1, preflen-1,
5598 db_stats_fields[i].name);
5603 printf(" %*s%-22s%*s%10u\n", preflen ? 4 : 0, "",
5604 db_stats_fields[i].name+preflen, preflen ? 0 : 4, "",
5605 *(uint32_t *)(db_stats_fields[i].offset+(uint8_t *)s));
5608 printf(" hop_count_buckets:");
5609 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5610 printf(" %d", s->hop_count_bucket[i]);
5614 printf(" lock_buckets:");
5615 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
5616 printf(" %d", s->locks.buckets[i]);
5620 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5621 "locks_latency MIN/AVG/MAX",
5622 s->locks.latency.min, LATENCY_AVG(s->locks.latency),
5623 s->locks.latency.max, s->locks.latency.num);
5625 printf(" %-30s %.6f/%.6f/%.6f sec out of %d\n",
5626 "vacuum_latency MIN/AVG/MAX",
5627 s->vacuum.latency.min, LATENCY_AVG(s->vacuum.latency),
5628 s->vacuum.latency.max, s->vacuum.latency.num);
5630 printf(" Num Hot Keys: %d\n", s->num_hot_keys);
5631 for (i=0; i<s->num_hot_keys; i++) {
5633 printf(" Count:%d Key:", s->hot_keys[i].count);
5634 for (j=0; j<s->hot_keys[i].key.dsize; j++) {
5635 printf("%02x", s->hot_keys[i].key.dptr[j] & 0xff);
5641 static int control_dbstatistics(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5642 int argc, const char **argv)
5645 const char *db_name;
5646 struct ctdb_db_statistics *dbstats;
5650 usage("dbstatistics");
5653 if (! db_exists(mem_ctx, ctdb, argv[0], &db_id, &db_name, NULL)) {
5657 ret = ctdb_ctrl_get_db_statistics(mem_ctx, ctdb->ev, ctdb->client,
5658 ctdb->cmd_pnn, TIMEOUT(), db_id,
5661 fprintf(stderr, "Failed to get statistics for DB %s\n",
5666 print_dbstatistics(db_name, dbstats);
5670 struct disable_takeover_runs_state {
5678 static void disable_takeover_run_handler(uint64_t srvid, TDB_DATA data,
5681 struct disable_takeover_runs_state *state =
5682 (struct disable_takeover_runs_state *)private_data;
5685 if (data.dsize != sizeof(int)) {
5690 /* ret will be a PNN (i.e. >=0) on success, or negative on error */
5691 ret = *(int *)data.dptr;
5693 state->status = ret;
5697 for (i=0; i<state->node_count; i++) {
5698 if (state->pnn_list[i] == ret) {
5699 state->reply[i] = true;
5705 for (i=0; i<state->node_count; i++) {
5706 if (! state->reply[i]) {
5707 state->done = false;
5713 static int disable_takeover_runs(TALLOC_CTX *mem_ctx,
5714 struct ctdb_context *ctdb, uint32_t timeout,
5715 uint32_t *pnn_list, int count)
5717 struct ctdb_disable_message disable = { 0 };
5718 struct disable_takeover_runs_state state;
5721 disable.pnn = ctdb->pnn;
5722 disable.srvid = next_srvid(ctdb);
5723 disable.timeout = timeout;
5725 state.pnn_list = pnn_list;
5726 state.node_count = count;
5729 state.reply = talloc_zero_array(mem_ctx, bool, count);
5730 if (state.reply == NULL) {
5734 ret = ctdb_client_set_message_handler(ctdb->ev, ctdb->client,
5736 disable_takeover_run_handler,
5742 for (i=0; i<count; i++) {
5743 ret = ctdb_message_disable_takeover_runs(mem_ctx, ctdb->ev,
5752 ret = ctdb_client_wait_timeout(ctdb->ev, &state.done, TIMEOUT());
5754 fprintf(stderr, "Timed out waiting to disable takeover runs\n");
5756 ret = (state.status >= 0 ? 0 : 1);
5760 ctdb_client_remove_message_handler(ctdb->ev, ctdb->client,
5761 disable.srvid, &state);
5765 static int control_reloadips(TALLOC_CTX *mem_ctx, struct ctdb_context *ctdb,
5766 int argc, const char **argv)
5768 const char *nodestring = NULL;
5769 struct ctdb_node_map *nodemap, *nodemap2;
5770 struct ctdb_req_control request;
5771 uint32_t *pnn_list, *pnn_list2;
5772 int ret, count, count2;
5779 nodestring = argv[0];
5782 nodemap = get_nodemap(ctdb, false);
5783 if (nodemap == NULL) {
5787 if (! parse_nodestring(mem_ctx, ctdb, nodestring, &nodemap2)) {
5791 count = list_of_connected_nodes(nodemap, CTDB_UNKNOWN_PNN,
5792 mem_ctx, &pnn_list);
5794 fprintf(stderr, "Memory allocation error\n");
5798 count2 = list_of_active_nodes(nodemap2, CTDB_UNKNOWN_PNN,
5799 mem_ctx, &pnn_list2);
5801 fprintf(stderr, "Memory allocation error\n");
5805 /* Disable takeover runs on all connected nodes. A reply
5806 * indicating success is needed from each node so all nodes
5807 * will need to be active.
5809 * A check could be added to not allow reloading of IPs when
5810 * there are disconnected nodes. However, this should
5811 * probably be left up to the administrator.
5813 ret = disable_takeover_runs(mem_ctx, ctdb, 2*options.timelimit,
5816 fprintf(stderr, "Failed to disable takeover runs\n");
5820 /* Now tell all the desired nodes to reload their public IPs.
5821 * Keep trying this until it succeeds. This assumes all
5822 * failures are transient, which might not be true...
5824 ctdb_req_control_reload_public_ips(&request);
5825 ret = ctdb_client_control_multi(mem_ctx, ctdb->ev, ctdb->client,
5826 pnn_list2, count2, TIMEOUT(),
5827 &request, NULL, NULL);
5829 fprintf(stderr, "Failed to reload IPs on some nodes.\n");
5832 /* It isn't strictly necessary to wait until takeover runs are
5833 * re-enabled but doing so can't hurt.
5835 ret = disable_takeover_runs(mem_ctx, ctdb, 0, pnn_list, count);
5837 fprintf(stderr, "Failed to enable takeover runs\n");
5841 return ipreallocate(mem_ctx, ctdb);
5845 static const struct ctdb_cmd {
5847 int (*fn)(TALLOC_CTX *, struct ctdb_context *, int, const char **);
5848 bool without_daemon; /* can be run without daemon running ? */
5849 bool remote; /* can be run on remote nodes */
5852 } ctdb_commands[] = {
5853 { "version", control_version, true, false,
5854 "show version of ctdb", NULL },
5855 { "status", control_status, false, true,
5856 "show node status", NULL },
5857 { "uptime", control_uptime, false, true,
5858 "show node uptime", NULL },
5859 { "ping", control_ping, false, true,
5860 "ping a node", NULL },
5861 { "runstate", control_runstate, false, true,
5862 "get/check runstate of a node",
5863 "[setup|first_recovery|startup|running]" },
5864 { "getvar", control_getvar, false, true,
5865 "get a tunable variable", "<name>" },
5866 { "setvar", control_setvar, false, true,
5867 "set a tunable variable", "<name> <value>" },
5868 { "listvars", control_listvars, false, true,
5869 "list tunable variables", NULL },
5870 { "statistics", control_statistics, false, true,
5871 "show ctdb statistics", NULL },
5872 { "statisticsreset", control_statistics_reset, false, true,
5873 "reset ctdb statistics", NULL },
5874 { "stats", control_stats, false, true,
5875 "show rolling statistics", "[count]" },
5876 { "ip", control_ip, false, true,
5877 "show public ips", "[all]" },
5878 { "ipinfo", control_ipinfo, false, true,
5879 "show public ip details", "<ip>" },
5880 { "ifaces", control_ifaces, false, true,
5881 "show interfaces", NULL },
5882 { "setifacelink", control_setifacelink, false, true,
5883 "set interface link status", "<iface> up|down" },
5884 { "process-exists", control_process_exists, false, true,
5885 "check if a process exists on a node", "<pid> [<srvid>]" },
5886 { "getdbmap", control_getdbmap, false, true,
5887 "show attached databases", NULL },
5888 { "getdbstatus", control_getdbstatus, false, true,
5889 "show database status", "<dbname|dbid>" },
5890 { "catdb", control_catdb, false, false,
5891 "dump cluster-wide ctdb database", "<dbname|dbid>" },
5892 { "cattdb", control_cattdb, false, false,
5893 "dump local ctdb database", "<dbname|dbid>" },
5894 { "getcapabilities", control_getcapabilities, false, true,
5895 "show node capabilities", NULL },
5896 { "pnn", control_pnn, false, false,
5897 "show the pnn of the currnet node", NULL },
5898 { "lvs", control_lvs, false, false,
5899 "show lvs configuration", "master|list|status" },
5900 { "setdebug", control_setdebug, false, true,
5901 "set debug level", "ERROR|WARNING|NOTICE|INFO|DEBUG" },
5902 { "getdebug", control_getdebug, false, true,
5903 "get debug level", NULL },
5904 { "attach", control_attach, false, false,
5905 "attach a database", "<dbname> [persistent|replicated]" },
5906 { "detach", control_detach, false, false,
5907 "detach database(s)", "<dbname|dbid> ..." },
5908 { "dumpmemory", control_dumpmemory, false, true,
5909 "dump ctdbd memory map", NULL },
5910 { "rddumpmemory", control_rddumpmemory, false, true,
5911 "dump recoverd memory map", NULL },
5912 { "getpid", control_getpid, false, true,
5913 "get ctdbd process ID", NULL },
5914 { "disable", control_disable, false, true,
5915 "disable a node", NULL },
5916 { "enable", control_enable, false, true,
5917 "enable a node", NULL },
5918 { "stop", control_stop, false, true,
5919 "stop a node", NULL },
5920 { "continue", control_continue, false, true,
5921 "continue a stopped node", NULL },
5922 { "ban", control_ban, false, true,
5923 "ban a node", "<bantime>"},
5924 { "unban", control_unban, false, true,
5925 "unban a node", NULL },
5926 { "shutdown", control_shutdown, false, true,
5927 "shutdown ctdb daemon", NULL },
5928 { "recover", control_recover, false, true,
5929 "force recovery", NULL },
5930 { "sync", control_ipreallocate, false, true,
5931 "run ip reallocation (deprecated)", NULL },
5932 { "ipreallocate", control_ipreallocate, false, true,
5933 "run ip reallocation", NULL },
5934 { "isnotrecmaster", control_isnotrecmaster, false, false,
5935 "check if local node is the recmaster", NULL },
5936 { "gratarp", control_gratarp, false, true,
5937 "send a gratuitous arp", "<ip> <interface>" },
5938 { "tickle", control_tickle, true, false,
5939 "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
5940 { "gettickles", control_gettickles, false, true,
5941 "get the list of tickles", "<ip> [<port>]" },
5942 { "addtickle", control_addtickle, false, true,
5943 "add a tickle", "<ip>:<port> <ip>:<port>" },
5944 { "deltickle", control_deltickle, false, true,
5945 "delete a tickle", "<ip>:<port> <ip>:<port>" },
5946 { "listnodes", control_listnodes, true, true,
5947 "list nodes in the cluster", NULL },
5948 { "reloadnodes", control_reloadnodes, false, false,
5949 "reload the nodes file all nodes", NULL },
5950 { "moveip", control_moveip, false, false,
5951 "move an ip address to another node", "<ip> <node>" },
5952 { "addip", control_addip, false, true,
5953 "add an ip address to a node", "<ip/mask> <iface>" },
5954 { "delip", control_delip, false, true,
5955 "delete an ip address from a node", "<ip>" },
5956 { "backupdb", control_backupdb, false, false,
5957 "backup a database into a file", "<dbname|dbid> <file>" },
5958 { "restoredb", control_restoredb, false, false,
5959 "restore a database from a file", "<file> [dbname]" },
5960 { "dumpdbbackup", control_dumpdbbackup, true, false,
5961 "dump database from a backup file", "<file>" },
5962 { "wipedb", control_wipedb, false, false,
5963 "wipe the contents of a database.", "<dbname|dbid>"},
5964 { "recmaster", control_recmaster, false, true,
5965 "show the pnn for the recovery master", NULL },
5966 { "event", control_event, true, false,
5967 "event and event script commands", NULL },
5968 { "scriptstatus", control_scriptstatus, true, false,
5969 "show event script status",
5970 "[init|setup|startup|monitor|takeip|releaseip|ipreallocated]" },
5971 { "natgw", control_natgw, false, false,
5972 "show natgw configuration", "master|list|status" },
5973 { "getreclock", control_getreclock, false, true,
5974 "get recovery lock file", NULL },
5975 { "setlmasterrole", control_setlmasterrole, false, true,
5976 "set LMASTER role", "on|off" },
5977 { "setrecmasterrole", control_setrecmasterrole, false, true,
5978 "set RECMASTER role", "on|off"},
5979 { "setdbreadonly", control_setdbreadonly, false, true,
5980 "enable readonly records", "<dbname|dbid>" },
5981 { "setdbsticky", control_setdbsticky, false, true,
5982 "enable sticky records", "<dbname|dbid>"},
5983 { "pfetch", control_pfetch, false, false,
5984 "fetch record from persistent database", "<dbname|dbid> <key>" },
5985 { "pstore", control_pstore, false, false,
5986 "write record to persistent database", "<dbname|dbid> <key> <value>" },
5987 { "pdelete", control_pdelete, false, false,
5988 "delete record from persistent database", "<dbname|dbid> <key>" },
5989 { "ptrans", control_ptrans, false, false,
5990 "update a persistent database (from file or stdin)", "<dbname|dbid> [<file>]" },
5991 { "tfetch", control_tfetch, false, true,
5992 "fetch a record", "<tdb-file> <key> [<file>]" },
5993 { "tstore", control_tstore, false, true,
5994 "store a record", "<tdb-file> <key> <data> [<rsn> <dmaster> <flags>]" },
5995 { "readkey", control_readkey, false, false,
5996 "read value of a database key", "<dbname|dbid> <key> [readonly]" },
5997 { "writekey", control_writekey, false, false,
5998 "write value for a database key", "<dbname|dbid> <key> <value>" },
5999 { "deletekey", control_deletekey, false, false,
6000 "delete a database key", "<dbname|dbid> <key>" },
6001 { "checktcpport", control_checktcpport, true, false,
6002 "check if a service is bound to a specific tcp port or not", "<port>" },
6003 { "getdbseqnum", control_getdbseqnum, false, false,
6004 "get database sequence number", "<dbname|dbid>" },
6005 { "nodestatus", control_nodestatus, false, true,
6006 "show and return node status", "[all|<pnn-list>]" },
6007 { "dbstatistics", control_dbstatistics, false, true,
6008 "show database statistics", "<dbname|dbid>" },
6009 { "reloadips", control_reloadips, false, false,
6010 "reload the public addresses file", "[all|<pnn-list>]" },
6013 static const struct ctdb_cmd *match_command(const char *command)
6015 const struct ctdb_cmd *cmd;
6018 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6019 cmd = &ctdb_commands[i];
6020 if (strlen(command) == strlen(cmd->name) &&
6021 strncmp(command, cmd->name, strlen(command)) == 0) {
6031 * Show usage message
6033 static void usage_full(void)
6037 poptPrintHelp(pc, stdout, 0);
6038 printf("\nCommands:\n");
6039 for (i=0; i<ARRAY_SIZE(ctdb_commands); i++) {
6040 printf(" %-15s %-27s %s\n",
6041 ctdb_commands[i].name,
6042 ctdb_commands[i].args ? ctdb_commands[i].args : "",
6043 ctdb_commands[i].msg);
6047 static void usage(const char *command)
6049 const struct ctdb_cmd *cmd;
6051 if (command == NULL) {
6056 cmd = match_command(command);
6060 poptPrintUsage(pc, stdout, 0);
6061 printf("\nCommands:\n");
6062 printf(" %-15s %-27s %s\n",
6063 cmd->name, cmd->args ? cmd->args : "", cmd->msg);
6069 struct poptOption cmdline_options[] = {
6071 { "debug", 'd', POPT_ARG_STRING, &options.debuglevelstr, 0,
6073 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0,
6074 "timelimit (in seconds)" },
6075 { "node", 'n', POPT_ARG_INT, &options.pnn, 0,
6076 "node specification - integer" },
6077 { NULL, 'Y', POPT_ARG_NONE, &options.machinereadable, 0,
6078 "enable machine readable output", NULL },
6079 { "separator", 'x', POPT_ARG_STRING, &options.sep, 0,
6080 "specify separator for machine readable output", "CHAR" },
6081 { NULL, 'X', POPT_ARG_NONE, &options.machineparsable, 0,
6082 "enable machine parsable output with separator |", NULL },
6083 { "verbose", 'v', POPT_ARG_NONE, &options.verbose, 0,
6084 "enable verbose output", NULL },
6085 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0,
6086 "die if runtime exceeds this limit (in seconds)" },
6090 static int process_command(const struct ctdb_cmd *cmd, int argc,
6093 TALLOC_CTX *tmp_ctx;
6094 struct ctdb_context *ctdb;
6095 const char *ctdb_socket;
6098 uint64_t srvid_offset;
6100 tmp_ctx = talloc_new(NULL);
6101 if (tmp_ctx == NULL) {
6102 fprintf(stderr, "Memory allocation error\n");
6106 if (cmd->without_daemon) {
6107 if (options.pnn != -1) {
6109 "Cannot specify node for command %s\n",
6114 ret = cmd->fn(tmp_ctx, NULL, argc-1, argv+1);
6115 talloc_free(tmp_ctx);
6119 ctdb = talloc_zero(tmp_ctx, struct ctdb_context);
6121 fprintf(stderr, "Memory allocation error\n");
6125 ctdb->ev = tevent_context_init(ctdb);
6126 if (ctdb->ev == NULL) {
6127 fprintf(stderr, "Failed to initialize tevent\n");
6131 ctdb_socket = path_socket(ctdb, "ctdbd");
6132 if (ctdb_socket == NULL) {
6133 fprintf(stderr, "Memory allocation error\n");
6137 ret = ctdb_client_init(ctdb, ctdb->ev, ctdb_socket, &ctdb->client);
6139 fprintf(stderr, "Failed to connect to CTDB daemon (%s)\n",
6142 if (!find_node_xpnn(ctdb, NULL)) {
6143 fprintf(stderr, "Is this node part of CTDB cluster?\n");
6148 ctdb->pnn = ctdb_client_pnn(ctdb->client);
6149 srvid_offset = getpid() & 0xFFFF;
6150 ctdb->srvid = SRVID_CTDB_TOOL | (srvid_offset << 16);
6152 if (options.pnn != -1) {
6153 status = verify_pnn(ctdb, options.pnn);
6158 ctdb->cmd_pnn = options.pnn;
6160 ctdb->cmd_pnn = ctdb->pnn;
6163 if (! cmd->remote && ctdb->pnn != ctdb->cmd_pnn) {
6164 fprintf(stderr, "Node cannot be specified for command %s\n",
6169 ret = cmd->fn(tmp_ctx, ctdb, argc-1, argv+1);
6170 talloc_free(tmp_ctx);
6174 talloc_free(tmp_ctx);
6178 static void signal_handler(int sig)
6180 fprintf(stderr, "Maximum runtime exceeded - exiting\n");
6183 static void alarm_handler(int sig)
6185 /* Kill any child processes */
6186 signal(SIGTERM, signal_handler);
6192 int main(int argc, const char *argv[])
6195 const char **extra_argv;
6197 const struct ctdb_cmd *cmd;
6204 /* Set default options */
6205 options.debuglevelstr = NULL;
6206 options.timelimit = 10;
6208 options.maxruntime = 0;
6211 pc = poptGetContext(argv[0], argc, argv, cmdline_options,
6212 POPT_CONTEXT_KEEP_FIRST);
6213 while ((opt = poptGetNextOpt(pc)) != -1) {
6214 fprintf(stderr, "Invalid option %s: %s\n",
6215 poptBadOption(pc, 0), poptStrerror(opt));
6219 if (options.maxruntime == 0) {
6220 const char *ctdb_timeout;
6222 ctdb_timeout = getenv("CTDB_TIMEOUT");
6223 if (ctdb_timeout != NULL) {
6224 options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6226 options.maxruntime = 120;
6229 if (options.maxruntime <= 120) {
6230 /* default timeout is 120 seconds */
6231 options.maxruntime = 120;
6234 if (options.machineparsable) {
6235 options.machinereadable = 1;
6238 /* setup the remaining options for the commands */
6240 extra_argv = poptGetArgs(pc);
6243 while (extra_argv[extra_argc]) extra_argc++;
6246 if (extra_argc < 1) {
6250 cmd = match_command(extra_argv[0]);
6252 fprintf(stderr, "Unknown command '%s'\n", extra_argv[0]);
6256 /* Enable logging */
6257 setup_logging("ctdb", DEBUG_STDERR);
6258 ok = debug_level_parse(options.debuglevelstr, &loglevel);
6260 loglevel = DEBUG_ERR;
6262 debuglevel_set(loglevel);
6264 signal(SIGALRM, alarm_handler);
6265 alarm(options.maxruntime);
6267 ret = process_command(cmd, extra_argc, extra_argv);
6272 (void)poptFreeContext(pc);