tools/ctdb: CTDB_BASE is the default location of configuration files
[ctdb.git] / tools / ctdb.c
1 /* 
2    ctdb control tool
3
4    Copyright (C) Andrew Tridgell  2007
5    Copyright (C) Ronnie Sahlberg  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/time.h"
23 #include "system/filesys.h"
24 #include "system/network.h"
25 #include "system/locale.h"
26 #include "popt.h"
27 #include "cmdline.h"
28 #include "../include/ctdb_version.h"
29 #include "../include/ctdb_client.h"
30 #include "../include/ctdb_private.h"
31 #include "../common/rb_tree.h"
32 #include "db_wrap.h"
33
34 #define ERR_TIMEOUT     20      /* timed out trying to reach node */
35 #define ERR_NONODE      21      /* node does not exist */
36 #define ERR_DISNODE     22      /* node is disconnected */
37
38 static void usage(void);
39
40 static struct {
41         int timelimit;
42         uint32_t pnn;
43         uint32_t *nodes;
44         int machinereadable;
45         int verbose;
46         int maxruntime;
47         int printemptyrecords;
48         int printdatasize;
49         int printlmaster;
50         int printhash;
51         int printrecordflags;
52 } options;
53
54 #define LONGTIMEOUT options.timelimit*10
55
56 #define TIMELIMIT() timeval_current_ofs(options.timelimit, 0)
57 #define LONGTIMELIMIT() timeval_current_ofs(LONGTIMEOUT, 0)
58
59 static int control_version(struct ctdb_context *ctdb, int argc, const char **argv)
60 {
61         printf("CTDB version: %s\n", CTDB_VERSION_STRING);
62         return 0;
63 }
64
65 #define CTDB_NOMEM_ABORT(p) do { if (!(p)) {                            \
66                 DEBUG(DEBUG_ALERT,("ctdb fatal error: %s\n",            \
67                                    "Out of memory in " __location__ )); \
68                 abort();                                                \
69         }} while (0)
70
71 static uint32_t getpnn(struct ctdb_context *ctdb)
72 {
73         if ((options.pnn == CTDB_BROADCAST_ALL) ||
74             (options.pnn == CTDB_MULTICAST)) {
75                 DEBUG(DEBUG_ERR,
76                       ("Cannot get PNN for node %u\n", options.pnn));
77                 exit(1);
78         }
79
80         if (options.pnn == CTDB_CURRENT_NODE) {
81                 return ctdb_get_pnn(ctdb);
82         } else {
83                 return options.pnn;
84         }
85 }
86
87 static void assert_single_node_only(void)
88 {
89         if ((options.pnn == CTDB_BROADCAST_ALL) ||
90             (options.pnn == CTDB_MULTICAST)) {
91                 DEBUG(DEBUG_ERR,
92                       ("This control can not be applied to multiple PNNs\n"));
93                 exit(1);
94         }
95 }
96
97 /* Pretty print the flags to a static buffer in human-readable format.
98  * This never returns NULL!
99  */
100 static const char *pretty_print_flags(uint32_t flags)
101 {
102         int j;
103         static const struct {
104                 uint32_t flag;
105                 const char *name;
106         } flag_names[] = {
107                 { NODE_FLAGS_DISCONNECTED,          "DISCONNECTED" },
108                 { NODE_FLAGS_PERMANENTLY_DISABLED,  "DISABLED" },
109                 { NODE_FLAGS_BANNED,                "BANNED" },
110                 { NODE_FLAGS_UNHEALTHY,             "UNHEALTHY" },
111                 { NODE_FLAGS_DELETED,               "DELETED" },
112                 { NODE_FLAGS_STOPPED,               "STOPPED" },
113                 { NODE_FLAGS_INACTIVE,              "INACTIVE" },
114         };
115         static char flags_str[512]; /* Big enough to contain all flag names */
116
117         flags_str[0] = '\0';
118         for (j=0;j<ARRAY_SIZE(flag_names);j++) {
119                 if (flags & flag_names[j].flag) {
120                         if (flags_str[0] == '\0') {
121                                 (void) strcpy(flags_str, flag_names[j].name);
122                         } else {
123                                 (void) strcat(flags_str, "|");
124                                 (void) strcat(flags_str, flag_names[j].name);
125                         }
126                 }
127         }
128         if (flags_str[0] == '\0') {
129                 (void) strcpy(flags_str, "OK");
130         }
131
132         return flags_str;
133 }
134
135 static int h2i(char h)
136 {
137         if (h >= 'a' && h <= 'f') return h - 'a' + 10;
138         if (h >= 'A' && h <= 'F') return h - 'f' + 10;
139         return h - '0';
140 }
141
142 static TDB_DATA hextodata(TALLOC_CTX *mem_ctx, const char *str)
143 {
144         int i, len;
145         TDB_DATA key = {NULL, 0};
146
147         len = strlen(str);
148         if (len & 0x01) {
149                 DEBUG(DEBUG_ERR,("Key specified with odd number of hexadecimal digits\n"));
150                 return key;
151         }
152
153         key.dsize = len>>1;
154         key.dptr  = talloc_size(mem_ctx, key.dsize);
155
156         for (i=0; i < len/2; i++) {
157                 key.dptr[i] = h2i(str[i*2]) << 4 | h2i(str[i*2+1]);
158         }
159         return key;
160 }
161
162 /* Parse a nodestring.  Parameter dd_ok controls what happens to nodes
163  * that are disconnected or deleted.  If dd_ok is true those nodes are
164  * included in the output list of nodes.  If dd_ok is false, those
165  * nodes are filtered from the "all" case and cause an error if
166  * explicitly specified.
167  */
168 static bool parse_nodestring(struct ctdb_context *ctdb,
169                              TALLOC_CTX *mem_ctx,
170                              const char * nodestring,
171                              uint32_t current_pnn,
172                              bool dd_ok,
173                              uint32_t **nodes,
174                              uint32_t *pnn_mode)
175 {
176         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
177         int n;
178         uint32_t i;
179         struct ctdb_node_map *nodemap;
180         int ret;
181
182         *nodes = NULL;
183
184         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
185         if (ret != 0) {
186                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
187                 talloc_free(tmp_ctx);
188                 exit(10);
189         }
190
191         if (nodestring != NULL) {
192                 *nodes = talloc_array(mem_ctx, uint32_t, 0);
193                 if (*nodes == NULL) {
194                         goto failed;
195                 }
196
197                 n = 0;
198
199                 if (strcmp(nodestring, "all") == 0) {
200                         *pnn_mode = CTDB_BROADCAST_ALL;
201
202                         /* all */
203                         for (i = 0; i < nodemap->num; i++) {
204                                 if ((nodemap->nodes[i].flags &
205                                      (NODE_FLAGS_DISCONNECTED |
206                                       NODE_FLAGS_DELETED)) && !dd_ok) {
207                                         continue;
208                                 }
209                                 *nodes = talloc_realloc(mem_ctx, *nodes,
210                                                         uint32_t, n+1);
211                                 if (*nodes == NULL) {
212                                         goto failed;
213                                 }
214                                 (*nodes)[n] = i;
215                                 n++;
216                         }
217                 } else {
218                         /* x{,y...} */
219                         char *ns, *tok;
220
221                         ns = talloc_strdup(tmp_ctx, nodestring);
222                         tok = strtok(ns, ",");
223                         while (tok != NULL) {
224                                 uint32_t pnn;
225                                 i = (uint32_t)strtoul(tok, NULL, 0);
226                                 if (i >= nodemap->num) {
227                                         DEBUG(DEBUG_ERR, ("Node %u does not exist\n", i));
228                                         talloc_free(tmp_ctx);
229                                         exit(ERR_NONODE);
230                                 }
231                                 if ((nodemap->nodes[i].flags & 
232                                      (NODE_FLAGS_DISCONNECTED |
233                                       NODE_FLAGS_DELETED)) && !dd_ok) {
234                                         DEBUG(DEBUG_ERR, ("Node %u has status %s\n", i, pretty_print_flags(nodemap->nodes[i].flags)));
235                                         talloc_free(tmp_ctx);
236                                         exit(ERR_DISNODE);
237                                 }
238                                 if ((pnn = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), i)) < 0) {
239                                         DEBUG(DEBUG_ERR, ("Can not access node %u. Node is not operational.\n", i));
240                                         talloc_free(tmp_ctx);
241                                         exit(10);
242                                 }
243
244                                 *nodes = talloc_realloc(mem_ctx, *nodes,
245                                                         uint32_t, n+1);
246                                 if (*nodes == NULL) {
247                                         goto failed;
248                                 }
249
250                                 (*nodes)[n] = i;
251                                 n++;
252
253                                 tok = strtok(NULL, ",");
254                         }
255                         talloc_free(ns);
256
257                         if (n == 1) {
258                                 *pnn_mode = (*nodes)[0];
259                         } else {
260                                 *pnn_mode = CTDB_MULTICAST;
261                         }
262                 }
263         } else {
264                 /* default - no nodes specified */
265                 *nodes = talloc_array(mem_ctx, uint32_t, 1);
266                 if (*nodes == NULL) {
267                         goto failed;
268                 }
269                 *pnn_mode = CTDB_CURRENT_NODE;
270
271                 if (((*nodes)[0] = ctdb_ctrl_getpnn(ctdb, TIMELIMIT(), current_pnn)) < 0) {
272                         goto failed;
273                 }
274         }
275
276         talloc_free(tmp_ctx);
277         return true;
278
279 failed:
280         talloc_free(tmp_ctx);
281         return false;
282 }
283
284 /*
285  check if a database exists
286 */
287 static bool db_exists(struct ctdb_context *ctdb, const char *dbarg, uint32_t *dbid, uint8_t *flags)
288 {
289         int i, ret;
290         struct ctdb_dbid_map *dbmap=NULL;
291         bool dbid_given = false, found = false;
292         uint32_t id;
293         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
294
295         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &dbmap);
296         if (ret != 0) {
297                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
298                 goto fail;
299         }
300
301         if (strncmp(dbarg, "0x", 2) == 0) {
302                 id = strtoul(dbarg, NULL, 0);
303                 dbid_given = true;
304         }
305
306         for(i=0; i<dbmap->num; i++) {
307                 if (dbid_given) {
308                         if (id == dbmap->dbs[i].dbid) {
309                                 found = true;
310                                 break;
311                         }
312                 } else {
313                         const char *name;
314                         ret = ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, tmp_ctx, &name);
315                         if (ret != 0) {
316                                 DEBUG(DEBUG_ERR, ("Unable to get dbname from dbid %u\n", dbmap->dbs[i].dbid));
317                                 goto fail;
318                         }
319
320                         if (strcmp(name, dbarg) == 0) {
321                                 id = dbmap->dbs[i].dbid;
322                                 found = true;
323                                 break;
324                         }
325                 }
326         }
327
328         if (found) {
329                 if (dbid) *dbid = id;
330                 if (flags) *flags = dbmap->dbs[i].flags;
331         } else {
332                 DEBUG(DEBUG_ERR,("No database matching '%s' found\n", dbarg));
333         }
334
335 fail:
336         talloc_free(tmp_ctx);
337         return found;
338 }
339
340 /*
341   see if a process exists
342  */
343 static int control_process_exists(struct ctdb_context *ctdb, int argc, const char **argv)
344 {
345         uint32_t pnn, pid;
346         int ret;
347         if (argc < 1) {
348                 usage();
349         }
350
351         if (sscanf(argv[0], "%u:%u", &pnn, &pid) != 2) {
352                 DEBUG(DEBUG_ERR, ("Badly formed pnn:pid\n"));
353                 return -1;
354         }
355
356         ret = ctdb_ctrl_process_exists(ctdb, pnn, pid);
357         if (ret == 0) {
358                 printf("%u:%u exists\n", pnn, pid);
359         } else {
360                 printf("%u:%u does not exist\n", pnn, pid);
361         }
362         return ret;
363 }
364
365 /*
366   display statistics structure
367  */
368 static void show_statistics(struct ctdb_statistics *s, int show_header)
369 {
370         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
371         int i;
372         const char *prefix=NULL;
373         int preflen=0;
374         int tmp, days, hours, minutes, seconds;
375         const struct {
376                 const char *name;
377                 uint32_t offset;
378         } fields[] = {
379 #define STATISTICS_FIELD(n) { #n, offsetof(struct ctdb_statistics, n) }
380                 STATISTICS_FIELD(num_clients),
381                 STATISTICS_FIELD(frozen),
382                 STATISTICS_FIELD(recovering),
383                 STATISTICS_FIELD(num_recoveries),
384                 STATISTICS_FIELD(client_packets_sent),
385                 STATISTICS_FIELD(client_packets_recv),
386                 STATISTICS_FIELD(node_packets_sent),
387                 STATISTICS_FIELD(node_packets_recv),
388                 STATISTICS_FIELD(keepalive_packets_sent),
389                 STATISTICS_FIELD(keepalive_packets_recv),
390                 STATISTICS_FIELD(node.req_call),
391                 STATISTICS_FIELD(node.reply_call),
392                 STATISTICS_FIELD(node.req_dmaster),
393                 STATISTICS_FIELD(node.reply_dmaster),
394                 STATISTICS_FIELD(node.reply_error),
395                 STATISTICS_FIELD(node.req_message),
396                 STATISTICS_FIELD(node.req_control),
397                 STATISTICS_FIELD(node.reply_control),
398                 STATISTICS_FIELD(client.req_call),
399                 STATISTICS_FIELD(client.req_message),
400                 STATISTICS_FIELD(client.req_control),
401                 STATISTICS_FIELD(timeouts.call),
402                 STATISTICS_FIELD(timeouts.control),
403                 STATISTICS_FIELD(timeouts.traverse),
404                 STATISTICS_FIELD(locks.num_calls),
405                 STATISTICS_FIELD(locks.num_current),
406                 STATISTICS_FIELD(locks.num_pending),
407                 STATISTICS_FIELD(locks.num_failed),
408                 STATISTICS_FIELD(total_calls),
409                 STATISTICS_FIELD(pending_calls),
410                 STATISTICS_FIELD(childwrite_calls),
411                 STATISTICS_FIELD(pending_childwrite_calls),
412                 STATISTICS_FIELD(memory_used),
413                 STATISTICS_FIELD(max_hop_count),
414                 STATISTICS_FIELD(total_ro_delegations),
415                 STATISTICS_FIELD(total_ro_revokes),
416         };
417         
418         tmp = s->statistics_current_time.tv_sec - s->statistics_start_time.tv_sec;
419         seconds = tmp%60;
420         tmp    /= 60;
421         minutes = tmp%60;
422         tmp    /= 60;
423         hours   = tmp%24;
424         tmp    /= 24;
425         days    = tmp;
426
427         if (options.machinereadable){
428                 if (show_header) {
429                         printf("CTDB version:");
430                         printf("Current time of statistics:");
431                         printf("Statistics collected since:");
432                         for (i=0;i<ARRAY_SIZE(fields);i++) {
433                                 printf("%s:", fields[i].name);
434                         }
435                         printf("num_reclock_ctdbd_latency:");
436                         printf("min_reclock_ctdbd_latency:");
437                         printf("avg_reclock_ctdbd_latency:");
438                         printf("max_reclock_ctdbd_latency:");
439
440                         printf("num_reclock_recd_latency:");
441                         printf("min_reclock_recd_latency:");
442                         printf("avg_reclock_recd_latency:");
443                         printf("max_reclock_recd_latency:");
444
445                         printf("num_call_latency:");
446                         printf("min_call_latency:");
447                         printf("avg_call_latency:");
448                         printf("max_call_latency:");
449
450                         printf("num_lockwait_latency:");
451                         printf("min_lockwait_latency:");
452                         printf("avg_lockwait_latency:");
453                         printf("max_lockwait_latency:");
454
455                         printf("num_childwrite_latency:");
456                         printf("min_childwrite_latency:");
457                         printf("avg_childwrite_latency:");
458                         printf("max_childwrite_latency:");
459                         printf("\n");
460                 }
461                 printf("%d:", CTDB_VERSION);
462                 printf("%d:", (int)s->statistics_current_time.tv_sec);
463                 printf("%d:", (int)s->statistics_start_time.tv_sec);
464                 for (i=0;i<ARRAY_SIZE(fields);i++) {
465                         printf("%d:", *(uint32_t *)(fields[i].offset+(uint8_t *)s));
466                 }
467                 printf("%d:", s->reclock.ctdbd.num);
468                 printf("%.6f:", s->reclock.ctdbd.min);
469                 printf("%.6f:", s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0);
470                 printf("%.6f:", s->reclock.ctdbd.max);
471
472                 printf("%d:", s->reclock.recd.num);
473                 printf("%.6f:", s->reclock.recd.min);
474                 printf("%.6f:", s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0);
475                 printf("%.6f:", s->reclock.recd.max);
476
477                 printf("%d:", s->call_latency.num);
478                 printf("%.6f:", s->call_latency.min);
479                 printf("%.6f:", s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0);
480                 printf("%.6f:", s->call_latency.max);
481
482                 printf("%d:", s->childwrite_latency.num);
483                 printf("%.6f:", s->childwrite_latency.min);
484                 printf("%.6f:", s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0);
485                 printf("%.6f:", s->childwrite_latency.max);
486                 printf("\n");
487         } else {
488                 printf("CTDB version %u\n", CTDB_VERSION);
489                 printf("Current time of statistics  :                %s", ctime(&s->statistics_current_time.tv_sec));
490                 printf("Statistics collected since  : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&s->statistics_start_time.tv_sec));
491
492                 for (i=0;i<ARRAY_SIZE(fields);i++) {
493                         if (strchr(fields[i].name, '.')) {
494                                 preflen = strcspn(fields[i].name, ".")+1;
495                                 if (!prefix || strncmp(prefix, fields[i].name, preflen) != 0) {
496                                         prefix = fields[i].name;
497                                         printf(" %*.*s\n", preflen-1, preflen-1, fields[i].name);
498                                 }
499                         } else {
500                                 preflen = 0;
501                         }
502                         printf(" %*s%-22s%*s%10u\n", 
503                                preflen?4:0, "",
504                                fields[i].name+preflen, 
505                                preflen?0:4, "",
506                                *(uint32_t *)(fields[i].offset+(uint8_t *)s));
507                 }
508                 printf(" hop_count_buckets:");
509                 for (i=0;i<MAX_COUNT_BUCKETS;i++) {
510                         printf(" %d", s->hop_count_bucket[i]);
511                 }
512                 printf("\n");
513                 printf(" lock_buckets:");
514                 for (i=0; i<MAX_COUNT_BUCKETS; i++) {
515                         printf(" %d", s->locks.buckets[i]);
516                 }
517                 printf("\n");
518                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "locks_latency      MIN/AVG/MAX", s->locks.latency.min, s->locks.latency.num?s->locks.latency.total/s->locks.latency.num:0.0, s->locks.latency.max, s->locks.latency.num);
519
520                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_ctdbd      MIN/AVG/MAX", s->reclock.ctdbd.min, s->reclock.ctdbd.num?s->reclock.ctdbd.total/s->reclock.ctdbd.num:0.0, s->reclock.ctdbd.max, s->reclock.ctdbd.num);
521
522                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "reclock_recd       MIN/AVG/MAX", s->reclock.recd.min, s->reclock.recd.num?s->reclock.recd.total/s->reclock.recd.num:0.0, s->reclock.recd.max, s->reclock.recd.num);
523
524                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "call_latency       MIN/AVG/MAX", s->call_latency.min, s->call_latency.num?s->call_latency.total/s->call_latency.num:0.0, s->call_latency.max, s->call_latency.num);
525                 printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n", "childwrite_latency MIN/AVG/MAX", s->childwrite_latency.min, s->childwrite_latency.num?s->childwrite_latency.total/s->childwrite_latency.num:0.0, s->childwrite_latency.max, s->childwrite_latency.num);
526         }
527
528         talloc_free(tmp_ctx);
529 }
530
531 /*
532   display remote ctdb statistics combined from all nodes
533  */
534 static int control_statistics_all(struct ctdb_context *ctdb)
535 {
536         int ret, i;
537         struct ctdb_statistics statistics;
538         uint32_t *nodes;
539         uint32_t num_nodes;
540
541         nodes = ctdb_get_connected_nodes(ctdb, TIMELIMIT(), ctdb, &num_nodes);
542         CTDB_NO_MEMORY(ctdb, nodes);
543         
544         ZERO_STRUCT(statistics);
545
546         for (i=0;i<num_nodes;i++) {
547                 struct ctdb_statistics s1;
548                 int j;
549                 uint32_t *v1 = (uint32_t *)&s1;
550                 uint32_t *v2 = (uint32_t *)&statistics;
551                 uint32_t num_ints = 
552                         offsetof(struct ctdb_statistics, __last_counter) / sizeof(uint32_t);
553                 ret = ctdb_ctrl_statistics(ctdb, nodes[i], &s1);
554                 if (ret != 0) {
555                         DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", nodes[i]));
556                         return ret;
557                 }
558                 for (j=0;j<num_ints;j++) {
559                         v2[j] += v1[j];
560                 }
561                 statistics.max_hop_count = 
562                         MAX(statistics.max_hop_count, s1.max_hop_count);
563                 statistics.call_latency.max = 
564                         MAX(statistics.call_latency.max, s1.call_latency.max);
565         }
566         talloc_free(nodes);
567         printf("Gathered statistics for %u nodes\n", num_nodes);
568         show_statistics(&statistics, 1);
569         return 0;
570 }
571
572 /*
573   display remote ctdb statistics
574  */
575 static int control_statistics(struct ctdb_context *ctdb, int argc, const char **argv)
576 {
577         int ret;
578         struct ctdb_statistics statistics;
579
580         if (options.pnn == CTDB_BROADCAST_ALL) {
581                 return control_statistics_all(ctdb);
582         }
583
584         ret = ctdb_ctrl_statistics(ctdb, options.pnn, &statistics);
585         if (ret != 0) {
586                 DEBUG(DEBUG_ERR, ("Unable to get statistics from node %u\n", options.pnn));
587                 return ret;
588         }
589         show_statistics(&statistics, 1);
590         return 0;
591 }
592
593
594 /*
595   reset remote ctdb statistics
596  */
597 static int control_statistics_reset(struct ctdb_context *ctdb, int argc, const char **argv)
598 {
599         int ret;
600
601         ret = ctdb_statistics_reset(ctdb, options.pnn);
602         if (ret != 0) {
603                 DEBUG(DEBUG_ERR, ("Unable to reset statistics on node %u\n", options.pnn));
604                 return ret;
605         }
606         return 0;
607 }
608
609
610 /*
611   display remote ctdb rolling statistics
612  */
613 static int control_stats(struct ctdb_context *ctdb, int argc, const char **argv)
614 {
615         int ret;
616         struct ctdb_statistics_wire *stats;
617         int i, num_records = -1;
618
619         assert_single_node_only();
620
621         if (argc ==1) {
622                 num_records = atoi(argv[0]) - 1;
623         }
624
625         ret = ctdb_ctrl_getstathistory(ctdb, TIMELIMIT(), options.pnn, ctdb, &stats);
626         if (ret != 0) {
627                 DEBUG(DEBUG_ERR, ("Unable to get rolling statistics from node %u\n", options.pnn));
628                 return ret;
629         }
630         for (i=0;i<stats->num;i++) {
631                 if (stats->stats[i].statistics_start_time.tv_sec == 0) {
632                         continue;
633                 }
634                 show_statistics(&stats->stats[i], i==0);
635                 if (i == num_records) {
636                         break;
637                 }
638         }
639         return 0;
640 }
641
642
643 /*
644   display remote ctdb db statistics
645  */
646 static int control_dbstatistics(struct ctdb_context *ctdb, int argc, const char **argv)
647 {
648         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
649         struct ctdb_db_statistics *dbstat;
650         int i;
651         uint32_t db_id;
652         int num_hot_keys;
653         int ret;
654
655         if (argc < 1) {
656                 usage();
657         }
658
659         if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
660                 return -1;
661         }
662
663         ret = ctdb_ctrl_dbstatistics(ctdb, options.pnn, db_id, tmp_ctx, &dbstat);
664         if (ret != 0) {
665                 DEBUG(DEBUG_ERR,("Failed to read db statistics from node\n"));
666                 talloc_free(tmp_ctx);
667                 return -1;
668         }
669
670         printf("DB Statistics: %s\n", argv[0]);
671         printf(" %*s%-22s%*s%10u\n", 0, "", "ro_delegations", 4, "",
672                 dbstat->db_ro_delegations);
673         printf(" %*s%-22s%*s%10u\n", 0, "", "ro_revokes", 4, "",
674                 dbstat->db_ro_delegations);
675         printf(" %s\n", "locks");
676         printf(" %*s%-22s%*s%10u\n", 4, "", "total", 0, "",
677                 dbstat->locks.num_calls);
678         printf(" %*s%-22s%*s%10u\n", 4, "", "failed", 0, "",
679                 dbstat->locks.num_failed);
680         printf(" %*s%-22s%*s%10u\n", 4, "", "current", 0, "",
681                 dbstat->locks.num_current);
682         printf(" %*s%-22s%*s%10u\n", 4, "", "pending", 0, "",
683                 dbstat->locks.num_pending);
684         printf(" %s", "hop_count_buckets:");
685         for (i=0; i<MAX_COUNT_BUCKETS; i++) {
686                 printf(" %d", dbstat->hop_count_bucket[i]);
687         }
688         printf("\n");
689         printf(" %s", "lock_buckets:");
690         for (i=0; i<MAX_COUNT_BUCKETS; i++) {
691                 printf(" %d", dbstat->locks.buckets[i]);
692         }
693         printf("\n");
694         printf(" %-30s     %.6f/%.6f/%.6f sec out of %d\n",
695                 "locks_latency      MIN/AVG/MAX",
696                 dbstat->locks.latency.min,
697                 (dbstat->locks.latency.num ?
698                  dbstat->locks.latency.total /dbstat->locks.latency.num :
699                  0.0),
700                 dbstat->locks.latency.max,
701                 dbstat->locks.latency.num);
702         num_hot_keys = 0;
703         for (i=0; i<dbstat->num_hot_keys; i++) {
704                 if (dbstat->hot_keys[i].count > 0) {
705                         num_hot_keys++;
706                 }
707         }
708         dbstat->num_hot_keys = num_hot_keys;
709
710         printf(" Num Hot Keys:     %d\n", dbstat->num_hot_keys);
711         for (i = 0; i < dbstat->num_hot_keys; i++) {
712                 int j;
713                 printf("     Count:%d Key:", dbstat->hot_keys[i].count);
714                 for (j = 0; j < dbstat->hot_keys[i].key.dsize; j++) {
715                         printf("%02x", dbstat->hot_keys[i].key.dptr[j]&0xff);
716                 }
717                 printf("\n");
718         }
719
720         talloc_free(tmp_ctx);
721         return 0;
722 }
723
724 /*
725   display uptime of remote node
726  */
727 static int control_uptime(struct ctdb_context *ctdb, int argc, const char **argv)
728 {
729         int ret;
730         struct ctdb_uptime *uptime = NULL;
731         int tmp, days, hours, minutes, seconds;
732
733         ret = ctdb_ctrl_uptime(ctdb, ctdb, TIMELIMIT(), options.pnn, &uptime);
734         if (ret != 0) {
735                 DEBUG(DEBUG_ERR, ("Unable to get uptime from node %u\n", options.pnn));
736                 return ret;
737         }
738
739         if (options.machinereadable){
740                 printf(":Current Node Time:Ctdb Start Time:Last Recovery/Failover Time:Last Recovery/IPFailover Duration:\n");
741                 printf(":%u:%u:%u:%lf\n",
742                         (unsigned int)uptime->current_time.tv_sec,
743                         (unsigned int)uptime->ctdbd_start_time.tv_sec,
744                         (unsigned int)uptime->last_recovery_finished.tv_sec,
745                         timeval_delta(&uptime->last_recovery_finished,
746                                       &uptime->last_recovery_started)
747                 );
748                 return 0;
749         }
750
751         printf("Current time of node          :                %s", ctime(&uptime->current_time.tv_sec));
752
753         tmp = uptime->current_time.tv_sec - uptime->ctdbd_start_time.tv_sec;
754         seconds = tmp%60;
755         tmp    /= 60;
756         minutes = tmp%60;
757         tmp    /= 60;
758         hours   = tmp%24;
759         tmp    /= 24;
760         days    = tmp;
761         printf("Ctdbd start time              : (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->ctdbd_start_time.tv_sec));
762
763         tmp = uptime->current_time.tv_sec - uptime->last_recovery_finished.tv_sec;
764         seconds = tmp%60;
765         tmp    /= 60;
766         minutes = tmp%60;
767         tmp    /= 60;
768         hours   = tmp%24;
769         tmp    /= 24;
770         days    = tmp;
771         printf("Time of last recovery/failover: (%03d %02d:%02d:%02d) %s", days, hours, minutes, seconds, ctime(&uptime->last_recovery_finished.tv_sec));
772         
773         printf("Duration of last recovery/failover: %lf seconds\n",
774                 timeval_delta(&uptime->last_recovery_finished,
775                               &uptime->last_recovery_started));
776
777         return 0;
778 }
779
780 /*
781   show the PNN of the current node
782  */
783 static int control_pnn(struct ctdb_context *ctdb, int argc, const char **argv)
784 {
785         uint32_t mypnn;
786
787         mypnn = getpnn(ctdb);
788
789         printf("PNN:%d\n", mypnn);
790         return 0;
791 }
792
793
794 struct pnn_node {
795         struct pnn_node *next;
796         const char *addr;
797         int pnn;
798 };
799
800 static struct pnn_node *read_nodes_file(TALLOC_CTX *mem_ctx)
801 {
802         const char *nodes_list;
803         int nlines;
804         char **lines;
805         int i, pnn;
806         struct pnn_node *pnn_nodes = NULL;
807         struct pnn_node *pnn_node;
808         struct pnn_node *tmp_node;
809
810         /* read the nodes file */
811         nodes_list = getenv("CTDB_NODES");
812         if (nodes_list == NULL) {
813                 nodes_list = talloc_asprintf(mem_ctx, "%s/nodes",
814                                              getenv("CTDB_BASE"));
815                 if (nodes_list == NULL) {
816                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
817                         exit(1);
818                 }
819         }
820         lines = file_lines_load(nodes_list, &nlines, mem_ctx);
821         if (lines == NULL) {
822                 return NULL;
823         }
824         while (nlines > 0 && strcmp(lines[nlines-1], "") == 0) {
825                 nlines--;
826         }
827         for (i=0, pnn=0; i<nlines; i++) {
828                 char *node;
829
830                 node = lines[i];
831                 /* strip leading spaces */
832                 while((*node == ' ') || (*node == '\t')) {
833                         node++;
834                 }
835                 if (*node == '#') {
836                         pnn++;
837                         continue;
838                 }
839                 if (strcmp(node, "") == 0) {
840                         continue;
841                 }
842                 pnn_node = talloc(mem_ctx, struct pnn_node);
843                 pnn_node->pnn = pnn++;
844                 pnn_node->addr = talloc_strdup(pnn_node, node);
845                 pnn_node->next = pnn_nodes;
846                 pnn_nodes = pnn_node;
847         }
848
849         /* swap them around so we return them in incrementing order */
850         pnn_node = pnn_nodes;
851         pnn_nodes = NULL;
852         while (pnn_node) {
853                 tmp_node = pnn_node;
854                 pnn_node = pnn_node->next;
855
856                 tmp_node->next = pnn_nodes;
857                 pnn_nodes = tmp_node;
858         }
859
860         return pnn_nodes;
861 }
862
863 /*
864   show the PNN of the current node
865   discover the pnn by loading the nodes file and try to bind to all
866   addresses one at a time until the ip address is found.
867  */
868 static int control_xpnn(struct ctdb_context *ctdb, int argc, const char **argv)
869 {
870         TALLOC_CTX *mem_ctx = talloc_new(NULL);
871         struct pnn_node *pnn_nodes;
872         struct pnn_node *pnn_node;
873
874         assert_single_node_only();
875
876         pnn_nodes = read_nodes_file(mem_ctx);
877         if (pnn_nodes == NULL) {
878                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
879                 talloc_free(mem_ctx);
880                 return -1;
881         }
882
883         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
884                 ctdb_sock_addr addr;
885
886                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
887                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
888                         talloc_free(mem_ctx);
889                         return -1;
890                 }
891
892                 if (ctdb_sys_have_ip(&addr)) {
893                         printf("PNN:%d\n", pnn_node->pnn);
894                         talloc_free(mem_ctx);
895                         return 0;
896                 }
897         }
898
899         printf("Failed to detect which PNN this node is\n");
900         talloc_free(mem_ctx);
901         return -1;
902 }
903
904 /* Helpers for ctdb status
905  */
906 static bool is_partially_online(struct ctdb_context *ctdb, struct ctdb_node_and_flags *node)
907 {
908         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
909         int j;
910         bool ret = false;
911
912         if (node->flags == 0) {
913                 struct ctdb_control_get_ifaces *ifaces;
914
915                 if (ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), node->pnn,
916                                          tmp_ctx, &ifaces) == 0) {
917                         for (j=0; j < ifaces->num; j++) {
918                                 if (ifaces->ifaces[j].link_state != 0) {
919                                         continue;
920                                 }
921                                 ret = true;
922                                 break;
923                         }
924                 }
925         }
926         talloc_free(tmp_ctx);
927
928         return ret;
929 }
930
931 static void control_status_header_machine(void)
932 {
933         printf(":Node:IP:Disconnected:Banned:Disabled:Unhealthy:Stopped"
934                ":Inactive:PartiallyOnline:ThisNode:\n");
935 }
936
937 static int control_status_1_machine(struct ctdb_context *ctdb, int mypnn,
938                                     struct ctdb_node_and_flags *node)
939 {
940         printf(":%d:%s:%d:%d:%d:%d:%d:%d:%d:%c:\n", node->pnn,
941                ctdb_addr_to_str(&node->addr),
942                !!(node->flags&NODE_FLAGS_DISCONNECTED),
943                !!(node->flags&NODE_FLAGS_BANNED),
944                !!(node->flags&NODE_FLAGS_PERMANENTLY_DISABLED),
945                !!(node->flags&NODE_FLAGS_UNHEALTHY),
946                !!(node->flags&NODE_FLAGS_STOPPED),
947                !!(node->flags&NODE_FLAGS_INACTIVE),
948                is_partially_online(ctdb, node) ? 1 : 0,
949                (node->pnn == mypnn)?'Y':'N');
950
951         return node->flags;
952 }
953
954 static int control_status_1_human(struct ctdb_context *ctdb, int mypnn,
955                                   struct ctdb_node_and_flags *node)
956 {
957        printf("pnn:%d %-16s %s%s\n", node->pnn,
958               ctdb_addr_to_str(&node->addr),
959               is_partially_online(ctdb, node) ? "PARTIALLYONLINE" : pretty_print_flags(node->flags),
960               node->pnn == mypnn?" (THIS NODE)":"");
961
962        return node->flags;
963 }
964
965 /*
966   display remote ctdb status
967  */
968 static int control_status(struct ctdb_context *ctdb, int argc, const char **argv)
969 {
970         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
971         int i;
972         struct ctdb_vnn_map *vnnmap=NULL;
973         struct ctdb_node_map *nodemap=NULL;
974         uint32_t recmode, recmaster, mypnn;
975         int num_deleted_nodes = 0;
976         int ret;
977
978         mypnn = getpnn(ctdb);
979
980         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
981         if (ret != 0) {
982                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
983                 talloc_free(tmp_ctx);
984                 return -1;
985         }
986
987         if (options.machinereadable) {
988                 control_status_header_machine();
989                 for (i=0;i<nodemap->num;i++) {
990                         if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
991                                 continue;
992                         }
993                         (void) control_status_1_machine(ctdb, mypnn,
994                                                         &nodemap->nodes[i]);
995                 }
996                 talloc_free(tmp_ctx);
997                 return 0;
998         }
999
1000         for (i=0; i<nodemap->num; i++) {
1001                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1002                         num_deleted_nodes++;
1003                 }
1004         }
1005         if (num_deleted_nodes == 0) {
1006                 printf("Number of nodes:%d\n", nodemap->num);
1007         } else {
1008                 printf("Number of nodes:%d (including %d deleted nodes)\n",
1009                        nodemap->num, num_deleted_nodes);
1010         }
1011         for(i=0;i<nodemap->num;i++){
1012                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1013                         continue;
1014                 }
1015                 (void) control_status_1_human(ctdb, mypnn, &nodemap->nodes[i]);
1016         }
1017
1018         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
1019         if (ret != 0) {
1020                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
1021                 talloc_free(tmp_ctx);
1022                 return -1;
1023         }
1024         if (vnnmap->generation == INVALID_GENERATION) {
1025                 printf("Generation:INVALID\n");
1026         } else {
1027                 printf("Generation:%d\n",vnnmap->generation);
1028         }
1029         printf("Size:%d\n",vnnmap->size);
1030         for(i=0;i<vnnmap->size;i++){
1031                 printf("hash:%d lmaster:%d\n", i, vnnmap->map[i]);
1032         }
1033
1034         ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmode);
1035         if (ret != 0) {
1036                 DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
1037                 talloc_free(tmp_ctx);
1038                 return -1;
1039         }
1040         printf("Recovery mode:%s (%d)\n",recmode==CTDB_RECOVERY_NORMAL?"NORMAL":"RECOVERY",recmode);
1041
1042         ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), options.pnn, &recmaster);
1043         if (ret != 0) {
1044                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1045                 talloc_free(tmp_ctx);
1046                 return -1;
1047         }
1048         printf("Recovery master:%d\n",recmaster);
1049
1050         talloc_free(tmp_ctx);
1051         return 0;
1052 }
1053
1054 static int control_nodestatus(struct ctdb_context *ctdb, int argc, const char **argv)
1055 {
1056         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1057         int i, ret;
1058         struct ctdb_node_map *nodemap=NULL;
1059         uint32_t * nodes;
1060         uint32_t pnn_mode, mypnn;
1061
1062         if (argc > 1) {
1063                 usage();
1064         }
1065
1066         if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1067                               options.pnn, true, &nodes, &pnn_mode)) {
1068                 return -1;
1069         }
1070
1071         if (options.machinereadable) {
1072                 control_status_header_machine();
1073         } else if (pnn_mode == CTDB_BROADCAST_ALL) {
1074                 printf("Number of nodes:%d\n", (int) talloc_array_length(nodes));
1075         }
1076
1077         mypnn = getpnn(ctdb);
1078
1079         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1080         if (ret != 0) {
1081                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1082                 talloc_free(tmp_ctx);
1083                 return -1;
1084         }
1085
1086         ret = 0;
1087
1088         for (i = 0; i < talloc_array_length(nodes); i++) {
1089                 if (options.machinereadable) {
1090                         ret |= control_status_1_machine(ctdb, mypnn,
1091                                                         &nodemap->nodes[nodes[i]]);
1092                 } else {
1093                         ret |= control_status_1_human(ctdb, mypnn,
1094                                                       &nodemap->nodes[nodes[i]]);
1095                 }
1096         }
1097
1098         talloc_free(tmp_ctx);
1099         return ret;
1100 }
1101
1102 struct natgw_node {
1103         struct natgw_node *next;
1104         const char *addr;
1105 };
1106
1107 static int find_natgw(struct ctdb_context *ctdb,
1108                        struct ctdb_node_map *nodemap, uint32_t flags,
1109                        uint32_t *pnn, const char **ip)
1110 {
1111         int i;
1112         uint32_t capabilities;
1113         int ret;
1114
1115         for (i=0;i<nodemap->num;i++) {
1116                 if (!(nodemap->nodes[i].flags & flags)) {
1117                         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(),
1118                                                         nodemap->nodes[i].pnn,
1119                                                         &capabilities);
1120                         if (ret != 0) {
1121                                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n",
1122                                                   nodemap->nodes[i].pnn));
1123                                 return -1;
1124                         }
1125                         if (!(capabilities&CTDB_CAP_NATGW)) {
1126                                 continue;
1127                         }
1128                         *pnn = nodemap->nodes[i].pnn;
1129                         *ip = ctdb_addr_to_str(&nodemap->nodes[i].addr);
1130                         return 0;
1131                 }
1132         }
1133
1134         return 2; /* matches ENOENT */
1135 }
1136
1137 /*
1138   display the list of nodes belonging to this natgw configuration
1139  */
1140 static int control_natgwlist(struct ctdb_context *ctdb, int argc, const char **argv)
1141 {
1142         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1143         int i, ret;
1144         const char *natgw_list;
1145         int nlines;
1146         char **lines;
1147         struct natgw_node *natgw_nodes = NULL;
1148         struct natgw_node *natgw_node;
1149         struct ctdb_node_map *nodemap=NULL;
1150         uint32_t mypnn, pnn;
1151         const char *ip;
1152
1153         /* When we have some nodes that could be the NATGW, make a
1154          * series of attempts to find the first node that doesn't have
1155          * certain status flags set.
1156          */
1157         uint32_t exclude_flags[] = {
1158                 /* Look for a nice healthy node */
1159                 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED|NODE_FLAGS_BANNED|NODE_FLAGS_UNHEALTHY,
1160                 /* If not found, an UNHEALTHY/BANNED node will do */
1161                 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_STOPPED|NODE_FLAGS_DELETED,
1162                 /* If not found, a STOPPED node will do */
1163                 NODE_FLAGS_DISCONNECTED|NODE_FLAGS_DELETED,
1164                 0,
1165         };
1166
1167         /* read the natgw nodes file into a linked list */
1168         natgw_list = getenv("CTDB_NATGW_NODES");
1169         if (natgw_list == NULL) {
1170                 natgw_list = talloc_asprintf(tmp_ctx, "%s/natgw_nodes",
1171                                              getenv("CTDB_BASE"));
1172                 if (natgw_list == NULL) {
1173                         DEBUG(DEBUG_ALERT,(__location__ " Out of memory\n"));
1174                         exit(1);
1175                 }
1176         }
1177         lines = file_lines_load(natgw_list, &nlines, ctdb);
1178         if (lines == NULL) {
1179                 ctdb_set_error(ctdb, "Failed to load natgw node list '%s'\n", natgw_list);
1180                 talloc_free(tmp_ctx);
1181                 return -1;
1182         }
1183         for (i=0;i<nlines;i++) {
1184                 char *node;
1185
1186                 node = lines[i];
1187                 /* strip leading spaces */
1188                 while((*node == ' ') || (*node == '\t')) {
1189                         node++;
1190                 }
1191                 if (*node == '#') {
1192                         continue;
1193                 }
1194                 if (strcmp(node, "") == 0) {
1195                         continue;
1196                 }
1197                 natgw_node = talloc(ctdb, struct natgw_node);
1198                 natgw_node->addr = talloc_strdup(natgw_node, node);
1199                 CTDB_NO_MEMORY(ctdb, natgw_node->addr);
1200                 natgw_node->next = natgw_nodes;
1201                 natgw_nodes = natgw_node;
1202         }
1203
1204         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1205         if (ret != 0) {
1206                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node.\n"));
1207                 talloc_free(tmp_ctx);
1208                 return -1;
1209         }
1210
1211         /* Trim the nodemap so it only includes connected nodes in the
1212          * current natgw group.
1213          */
1214         i=0;
1215         while(i<nodemap->num) {
1216                 for(natgw_node=natgw_nodes;natgw_node;natgw_node=natgw_node->next) {
1217                         if (!strcmp(natgw_node->addr, ctdb_addr_to_str(&nodemap->nodes[i].addr))) {
1218                                 break;
1219                         }
1220                 }
1221
1222                 /* this node was not in the natgw so we just remove it from
1223                  * the list
1224                  */
1225                 if ((natgw_node == NULL) 
1226                 ||  (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) ) {
1227                         int j;
1228
1229                         for (j=i+1; j<nodemap->num; j++) {
1230                                 nodemap->nodes[j-1] = nodemap->nodes[j];
1231                         }
1232                         nodemap->num--;
1233                         continue;
1234                 }
1235
1236                 i++;
1237         }
1238
1239         ret = 2; /* matches ENOENT */
1240         pnn = -1;
1241         ip = "0.0.0.0";
1242         for (i = 0; exclude_flags[i] != 0; i++) {
1243                 ret = find_natgw(ctdb, nodemap,
1244                                  exclude_flags[i],
1245                                  &pnn, &ip);
1246                 if (ret == -1) {
1247                         goto done;
1248                 }
1249                 if (ret == 0) {
1250                         break;
1251                 }
1252         }
1253
1254         if (options.machinereadable) {
1255                 printf(":Node:IP:\n");
1256                 printf(":%d:%s:\n", pnn, ip);
1257         } else {
1258                 printf("%d %s\n", pnn, ip);
1259         }
1260
1261         /* print the pruned list of nodes belonging to this natgw list */
1262         mypnn = getpnn(ctdb);
1263         if (options.machinereadable) {
1264                 control_status_header_machine();
1265         } else {
1266                 printf("Number of nodes:%d\n", nodemap->num);
1267         }
1268         for(i=0;i<nodemap->num;i++){
1269                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
1270                         continue;
1271                 }
1272                 if (options.machinereadable) {
1273                         control_status_1_machine(ctdb, mypnn, &(nodemap->nodes[i]));
1274                 } else {
1275                         control_status_1_human(ctdb, mypnn, &(nodemap->nodes[i]));
1276                 }
1277         }
1278
1279 done:
1280         talloc_free(tmp_ctx);
1281         return ret;
1282 }
1283
1284 /*
1285   display the status of the scripts for monitoring (or other events)
1286  */
1287 static int control_one_scriptstatus(struct ctdb_context *ctdb,
1288                                     enum ctdb_eventscript_call type)
1289 {
1290         struct ctdb_scripts_wire *script_status;
1291         int ret, i;
1292
1293         ret = ctdb_ctrl_getscriptstatus(ctdb, TIMELIMIT(), options.pnn, ctdb, type, &script_status);
1294         if (ret != 0) {
1295                 DEBUG(DEBUG_ERR, ("Unable to get script status from node %u\n", options.pnn));
1296                 return ret;
1297         }
1298
1299         if (script_status == NULL) {
1300                 if (!options.machinereadable) {
1301                         printf("%s cycle never run\n",
1302                                ctdb_eventscript_call_names[type]);
1303                 }
1304                 return 0;
1305         }
1306
1307         if (!options.machinereadable) {
1308                 printf("%d scripts were executed last %s cycle\n",
1309                        script_status->num_scripts,
1310                        ctdb_eventscript_call_names[type]);
1311         }
1312         for (i=0; i<script_status->num_scripts; i++) {
1313                 const char *status = NULL;
1314
1315                 switch (script_status->scripts[i].status) {
1316                 case -ETIME:
1317                         status = "TIMEDOUT";
1318                         break;
1319                 case -ENOEXEC:
1320                         status = "DISABLED";
1321                         break;
1322                 case 0:
1323                         status = "OK";
1324                         break;
1325                 default:
1326                         if (script_status->scripts[i].status > 0)
1327                                 status = "ERROR";
1328                         break;
1329                 }
1330                 if (options.machinereadable) {
1331                         printf(":%s:%s:%i:%s:%lu.%06lu:%lu.%06lu:%s:\n",
1332                                ctdb_eventscript_call_names[type],
1333                                script_status->scripts[i].name,
1334                                script_status->scripts[i].status,
1335                                status,
1336                                (long)script_status->scripts[i].start.tv_sec,
1337                                (long)script_status->scripts[i].start.tv_usec,
1338                                (long)script_status->scripts[i].finished.tv_sec,
1339                                (long)script_status->scripts[i].finished.tv_usec,
1340                                script_status->scripts[i].output);
1341                         continue;
1342                 }
1343                 if (status)
1344                         printf("%-20s Status:%s    ",
1345                                script_status->scripts[i].name, status);
1346                 else
1347                         /* Some other error, eg from stat. */
1348                         printf("%-20s Status:CANNOT RUN (%s)",
1349                                script_status->scripts[i].name,
1350                                strerror(-script_status->scripts[i].status));
1351
1352                 if (script_status->scripts[i].status >= 0) {
1353                         printf("Duration:%.3lf ",
1354                         timeval_delta(&script_status->scripts[i].finished,
1355                               &script_status->scripts[i].start));
1356                 }
1357                 if (script_status->scripts[i].status != -ENOEXEC) {
1358                         printf("%s",
1359                                ctime(&script_status->scripts[i].start.tv_sec));
1360                         if (script_status->scripts[i].status != 0) {
1361                                 printf("   OUTPUT:%s\n",
1362                                        script_status->scripts[i].output);
1363                         }
1364                 } else {
1365                         printf("\n");
1366                 }
1367         }
1368         return 0;
1369 }
1370
1371
1372 static int control_scriptstatus(struct ctdb_context *ctdb,
1373                                 int argc, const char **argv)
1374 {
1375         int ret;
1376         enum ctdb_eventscript_call type, min, max;
1377         const char *arg;
1378
1379         if (argc > 1) {
1380                 DEBUG(DEBUG_ERR, ("Unknown arguments to scriptstatus\n"));
1381                 return -1;
1382         }
1383
1384         if (argc == 0)
1385                 arg = ctdb_eventscript_call_names[CTDB_EVENT_MONITOR];
1386         else
1387                 arg = argv[0];
1388
1389         for (type = 0; type < CTDB_EVENT_MAX; type++) {
1390                 if (strcmp(arg, ctdb_eventscript_call_names[type]) == 0) {
1391                         min = type;
1392                         max = type+1;
1393                         break;
1394                 }
1395         }
1396         if (type == CTDB_EVENT_MAX) {
1397                 if (strcmp(arg, "all") == 0) {
1398                         min = 0;
1399                         max = CTDB_EVENT_MAX;
1400                 } else {
1401                         DEBUG(DEBUG_ERR, ("Unknown event type %s\n", argv[0]));
1402                         return -1;
1403                 }
1404         }
1405
1406         if (options.machinereadable) {
1407                 printf(":Type:Name:Code:Status:Start:End:Error Output...:\n");
1408         }
1409
1410         for (type = min; type < max; type++) {
1411                 ret = control_one_scriptstatus(ctdb, type);
1412                 if (ret != 0) {
1413                         return ret;
1414                 }
1415         }
1416
1417         return 0;
1418 }
1419
1420 /*
1421   enable an eventscript
1422  */
1423 static int control_enablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1424 {
1425         int ret;
1426
1427         if (argc < 1) {
1428                 usage();
1429         }
1430
1431         ret = ctdb_ctrl_enablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1432         if (ret != 0) {
1433           DEBUG(DEBUG_ERR, ("Unable to enable script %s on node %u\n", argv[0], options.pnn));
1434                 return ret;
1435         }
1436
1437         return 0;
1438 }
1439
1440 /*
1441   disable an eventscript
1442  */
1443 static int control_disablescript(struct ctdb_context *ctdb, int argc, const char **argv)
1444 {
1445         int ret;
1446
1447         if (argc < 1) {
1448                 usage();
1449         }
1450
1451         ret = ctdb_ctrl_disablescript(ctdb, TIMELIMIT(), options.pnn, argv[0]);
1452         if (ret != 0) {
1453           DEBUG(DEBUG_ERR, ("Unable to disable script %s on node %u\n", argv[0], options.pnn));
1454                 return ret;
1455         }
1456
1457         return 0;
1458 }
1459
1460 /*
1461   display the pnn of the recovery master
1462  */
1463 static int control_recmaster(struct ctdb_context *ctdb, int argc, const char **argv)
1464 {
1465         uint32_t recmaster;
1466         int ret;
1467
1468         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
1469         if (ret != 0) {
1470                 DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
1471                 return -1;
1472         }
1473         printf("%d\n",recmaster);
1474
1475         return 0;
1476 }
1477
1478 /*
1479   add a tickle to a public address
1480  */
1481 static int control_add_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1482 {
1483         struct ctdb_tcp_connection t;
1484         TDB_DATA data;
1485         int ret;
1486
1487         assert_single_node_only();
1488
1489         if (argc < 2) {
1490                 usage();
1491         }
1492
1493         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1494                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1495                 return -1;
1496         }
1497         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1498                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1499                 return -1;
1500         }
1501
1502         data.dptr = (uint8_t *)&t;
1503         data.dsize = sizeof(t);
1504
1505         /* tell all nodes about this tcp connection */
1506         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_ADD_DELAYED_UPDATE,
1507                            0, data, ctdb, NULL, NULL, NULL, NULL);
1508         if (ret != 0) {
1509                 DEBUG(DEBUG_ERR,("Failed to add tickle\n"));
1510                 return -1;
1511         }
1512         
1513         return 0;
1514 }
1515
1516
1517 /*
1518   delete a tickle from a node
1519  */
1520 static int control_del_tickle(struct ctdb_context *ctdb, int argc, const char **argv)
1521 {
1522         struct ctdb_tcp_connection t;
1523         TDB_DATA data;
1524         int ret;
1525
1526         assert_single_node_only();
1527
1528         if (argc < 2) {
1529                 usage();
1530         }
1531
1532         if (parse_ip_port(argv[0], &t.src_addr) == 0) {
1533                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1534                 return -1;
1535         }
1536         if (parse_ip_port(argv[1], &t.dst_addr) == 0) {
1537                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[1]));
1538                 return -1;
1539         }
1540
1541         data.dptr = (uint8_t *)&t;
1542         data.dsize = sizeof(t);
1543
1544         /* tell all nodes about this tcp connection */
1545         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_TCP_REMOVE,
1546                            0, data, ctdb, NULL, NULL, NULL, NULL);
1547         if (ret != 0) {
1548                 DEBUG(DEBUG_ERR,("Failed to remove tickle\n"));
1549                 return -1;
1550         }
1551         
1552         return 0;
1553 }
1554
1555
1556 /*
1557   get a list of all tickles for this pnn
1558  */
1559 static int control_get_tickles(struct ctdb_context *ctdb, int argc, const char **argv)
1560 {
1561         struct ctdb_control_tcp_tickle_list *list;
1562         ctdb_sock_addr addr;
1563         int i, ret;
1564         unsigned port = 0;
1565
1566         assert_single_node_only();
1567
1568         if (argc < 1) {
1569                 usage();
1570         }
1571
1572         if (argc == 2) {
1573                 port = atoi(argv[1]);
1574         }
1575
1576         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1577                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1578                 return -1;
1579         }
1580
1581         ret = ctdb_ctrl_get_tcp_tickles(ctdb, TIMELIMIT(), options.pnn, ctdb, &addr, &list);
1582         if (ret == -1) {
1583                 DEBUG(DEBUG_ERR, ("Unable to list tickles\n"));
1584                 return -1;
1585         }
1586
1587         if (options.machinereadable){
1588                 printf(":source ip:port:destination ip:port:\n");
1589                 for (i=0;i<list->tickles.num;i++) {
1590                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1591                                 continue;
1592                         }
1593                         printf(":%s:%u", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1594                         printf(":%s:%u:\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1595                 }
1596         } else {
1597                 printf("Tickles for ip:%s\n", ctdb_addr_to_str(&list->addr));
1598                 printf("Num tickles:%u\n", list->tickles.num);
1599                 for (i=0;i<list->tickles.num;i++) {
1600                         if (port && port != ntohs(list->tickles.connections[i].dst_addr.ip.sin_port)) {
1601                                 continue;
1602                         }
1603                         printf("SRC: %s:%u   ", ctdb_addr_to_str(&list->tickles.connections[i].src_addr), ntohs(list->tickles.connections[i].src_addr.ip.sin_port));
1604                         printf("DST: %s:%u\n", ctdb_addr_to_str(&list->tickles.connections[i].dst_addr), ntohs(list->tickles.connections[i].dst_addr.ip.sin_port));
1605                 }
1606         }
1607
1608         talloc_free(list);
1609         
1610         return 0;
1611 }
1612
1613
1614 static int move_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1615 {
1616         struct ctdb_all_public_ips *ips;
1617         struct ctdb_public_ip ip;
1618         int i, ret;
1619         uint32_t *nodes;
1620         uint32_t disable_time;
1621         TDB_DATA data;
1622         struct ctdb_node_map *nodemap=NULL;
1623         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1624
1625         disable_time = 30;
1626         data.dptr  = (uint8_t*)&disable_time;
1627         data.dsize = sizeof(disable_time);
1628         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1629         if (ret != 0) {
1630                 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1631                 return -1;
1632         }
1633
1634
1635
1636         /* read the public ip list from the node */
1637         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), pnn, ctdb, &ips);
1638         if (ret != 0) {
1639                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", pnn));
1640                 talloc_free(tmp_ctx);
1641                 return -1;
1642         }
1643
1644         for (i=0;i<ips->num;i++) {
1645                 if (ctdb_same_ip(addr, &ips->ips[i].addr)) {
1646                         break;
1647                 }
1648         }
1649         if (i==ips->num) {
1650                 DEBUG(DEBUG_ERR, ("Node %u can not host ip address '%s'\n",
1651                         pnn, ctdb_addr_to_str(addr)));
1652                 talloc_free(tmp_ctx);
1653                 return -1;
1654         }
1655
1656         ip.pnn  = pnn;
1657         ip.addr = *addr;
1658
1659         data.dptr  = (uint8_t *)&ip;
1660         data.dsize = sizeof(ip);
1661
1662         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1663         if (ret != 0) {
1664                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1665                 talloc_free(tmp_ctx);
1666                 return ret;
1667         }
1668
1669         nodes = list_of_nodes(ctdb, nodemap, tmp_ctx, NODE_FLAGS_INACTIVE, pnn);
1670         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1671                                         nodes, 0,
1672                                         LONGTIMELIMIT(),
1673                                         false, data,
1674                                         NULL, NULL,
1675                                         NULL);
1676         if (ret != 0) {
1677                 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1678                 talloc_free(tmp_ctx);
1679                 return -1;
1680         }
1681
1682         ret = ctdb_ctrl_takeover_ip(ctdb, LONGTIMELIMIT(), pnn, &ip);
1683         if (ret != 0) {
1684                 DEBUG(DEBUG_ERR,("Failed to take over IP on node %d\n", pnn));
1685                 talloc_free(tmp_ctx);
1686                 return -1;
1687         }
1688
1689         /* update the recovery daemon so it now knows to expect the new
1690            node assignment for this ip.
1691         */
1692         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_RECD_UPDATE_IP, data);
1693         if (ret != 0) {
1694                 DEBUG(DEBUG_ERR,("Failed to send message to update the ip on the recovery master.\n"));
1695                 return -1;
1696         }
1697
1698         talloc_free(tmp_ctx);
1699         return 0;
1700 }
1701
1702
1703 /* 
1704  * scans all other nodes and returns a pnn for another node that can host this 
1705  * ip address or -1
1706  */
1707 static int
1708 find_other_host_for_public_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1709 {
1710         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1711         struct ctdb_all_public_ips *ips;
1712         struct ctdb_node_map *nodemap=NULL;
1713         int i, j, ret;
1714
1715         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
1716         if (ret != 0) {
1717                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1718                 talloc_free(tmp_ctx);
1719                 return ret;
1720         }
1721
1722         for(i=0;i<nodemap->num;i++){
1723                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
1724                         continue;
1725                 }
1726                 if (nodemap->nodes[i].pnn == options.pnn) {
1727                         continue;
1728                 }
1729
1730                 /* read the public ip list from this node */
1731                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
1732                 if (ret != 0) {
1733                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
1734                         return -1;
1735                 }
1736
1737                 for (j=0;j<ips->num;j++) {
1738                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
1739                                 talloc_free(tmp_ctx);
1740                                 return nodemap->nodes[i].pnn;
1741                         }
1742                 }
1743                 talloc_free(ips);
1744         }
1745
1746         talloc_free(tmp_ctx);
1747         return -1;
1748 }
1749
1750 /* If pnn is -1 then try to find a node to move IP to... */
1751 static bool try_moveip(struct ctdb_context *ctdb, ctdb_sock_addr *addr, uint32_t pnn)
1752 {
1753         bool pnn_specified = (pnn == -1 ? false : true);
1754         int retries = 0;
1755
1756         while (retries < 5) {
1757                 if (!pnn_specified) {
1758                         pnn = find_other_host_for_public_ip(ctdb, addr);
1759                         if (pnn == -1) {
1760                                 return false;
1761                         }
1762                         DEBUG(DEBUG_NOTICE,
1763                               ("Trying to move public IP to node %u\n", pnn));
1764                 }
1765
1766                 if (move_ip(ctdb, addr, pnn) == 0) {
1767                         return true;
1768                 }
1769
1770                 sleep(3);
1771                 retries++;
1772         }
1773
1774         return false;
1775 }
1776
1777
1778 /*
1779   move/failover an ip address to a specific node
1780  */
1781 static int control_moveip(struct ctdb_context *ctdb, int argc, const char **argv)
1782 {
1783         uint32_t pnn;
1784         ctdb_sock_addr addr;
1785
1786         assert_single_node_only();
1787
1788         if (argc < 2) {
1789                 usage();
1790                 return -1;
1791         }
1792
1793         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1794                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1795                 return -1;
1796         }
1797
1798
1799         if (sscanf(argv[1], "%u", &pnn) != 1) {
1800                 DEBUG(DEBUG_ERR, ("Badly formed pnn\n"));
1801                 return -1;
1802         }
1803
1804         if (!try_moveip(ctdb, &addr, pnn)) {
1805                 DEBUG(DEBUG_ERR,("Failed to move IP to node %d.\n", pnn));
1806                 return -1;
1807         }
1808
1809         return 0;
1810 }
1811
1812 static int rebalance_node(struct ctdb_context *ctdb, uint32_t pnn)
1813 {
1814         TDB_DATA data;
1815
1816         data.dptr  = (uint8_t *)&pnn;
1817         data.dsize = sizeof(uint32_t);
1818         if (ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_REBALANCE_NODE, data) != 0) {
1819                 DEBUG(DEBUG_ERR,
1820                       ("Failed to send message to force node %u to be a rebalancing target\n",
1821                        pnn));
1822                 return -1;
1823         }
1824
1825         return 0;
1826 }
1827
1828
1829 /*
1830   rebalance a node by setting it to allow failback and triggering a
1831   takeover run
1832  */
1833 static int control_rebalancenode(struct ctdb_context *ctdb, int argc, const char **argv)
1834 {
1835         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1836         uint32_t *nodes;
1837         uint32_t pnn_mode;
1838         int i, ret;
1839
1840         assert_single_node_only();
1841
1842         if (argc > 1) {
1843                 usage();
1844         }
1845
1846         /* Determine the nodes where IPs need to be reloaded */
1847         if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
1848                               options.pnn, true, &nodes, &pnn_mode)) {
1849                 ret = -1;
1850                 goto done;
1851         }
1852
1853         for (i = 0; i < talloc_array_length(nodes); i++) {
1854                 if (!rebalance_node(ctdb, nodes[i])) {
1855                         ret = -1;
1856                 }
1857         }
1858
1859 done:
1860         talloc_free(tmp_ctx);
1861         return ret;
1862 }
1863
1864 static int rebalance_ip(struct ctdb_context *ctdb, ctdb_sock_addr *addr)
1865 {
1866         struct ctdb_public_ip ip;
1867         int ret;
1868         uint32_t *nodes;
1869         uint32_t disable_time;
1870         TDB_DATA data;
1871         struct ctdb_node_map *nodemap=NULL;
1872         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
1873
1874         disable_time = 30;
1875         data.dptr  = (uint8_t*)&disable_time;
1876         data.dsize = sizeof(disable_time);
1877         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, CTDB_SRVID_DISABLE_IP_CHECK, data);
1878         if (ret != 0) {
1879                 DEBUG(DEBUG_ERR,("Failed to send message to disable ipcheck\n"));
1880                 return -1;
1881         }
1882
1883         ip.pnn  = -1;
1884         ip.addr = *addr;
1885
1886         data.dptr  = (uint8_t *)&ip;
1887         data.dsize = sizeof(ip);
1888
1889         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
1890         if (ret != 0) {
1891                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
1892                 talloc_free(tmp_ctx);
1893                 return ret;
1894         }
1895
1896         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
1897         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_RELEASE_IP,
1898                                         nodes, 0,
1899                                         LONGTIMELIMIT(),
1900                                         false, data,
1901                                         NULL, NULL,
1902                                         NULL);
1903         if (ret != 0) {
1904                 DEBUG(DEBUG_ERR,("Failed to release IP on nodes\n"));
1905                 talloc_free(tmp_ctx);
1906                 return -1;
1907         }
1908
1909         talloc_free(tmp_ctx);
1910         return 0;
1911 }
1912
1913 /*
1914   release an ip form all nodes and have it re-assigned by recd
1915  */
1916 static int control_rebalanceip(struct ctdb_context *ctdb, int argc, const char **argv)
1917 {
1918         ctdb_sock_addr addr;
1919
1920         assert_single_node_only();
1921
1922         if (argc < 1) {
1923                 usage();
1924                 return -1;
1925         }
1926
1927         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
1928                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
1929                 return -1;
1930         }
1931
1932         if (rebalance_ip(ctdb, &addr) != 0) {
1933                 DEBUG(DEBUG_ERR,("Error when trying to reassign ip\n"));
1934                 return -1;
1935         }
1936
1937         return 0;
1938 }
1939
1940 static int getips_store_callback(void *param, void *data)
1941 {
1942         struct ctdb_public_ip *node_ip = (struct ctdb_public_ip *)data;
1943         struct ctdb_all_public_ips *ips = param;
1944         int i;
1945
1946         i = ips->num++;
1947         ips->ips[i].pnn  = node_ip->pnn;
1948         ips->ips[i].addr = node_ip->addr;
1949         return 0;
1950 }
1951
1952 static int getips_count_callback(void *param, void *data)
1953 {
1954         uint32_t *count = param;
1955
1956         (*count)++;
1957         return 0;
1958 }
1959
1960 #define IP_KEYLEN       4
1961 static uint32_t *ip_key(ctdb_sock_addr *ip)
1962 {
1963         static uint32_t key[IP_KEYLEN];
1964
1965         bzero(key, sizeof(key));
1966
1967         switch (ip->sa.sa_family) {
1968         case AF_INET:
1969                 key[0]  = ip->ip.sin_addr.s_addr;
1970                 break;
1971         case AF_INET6: {
1972                 uint32_t *s6_a32 = (uint32_t *)&(ip->ip6.sin6_addr.s6_addr);
1973                 key[0]  = s6_a32[3];
1974                 key[1]  = s6_a32[2];
1975                 key[2]  = s6_a32[1];
1976                 key[3]  = s6_a32[0];
1977                 break;
1978         }
1979         default:
1980                 DEBUG(DEBUG_ERR, (__location__ " ERROR, unknown family passed :%u\n", ip->sa.sa_family));
1981                 return key;
1982         }
1983
1984         return key;
1985 }
1986
1987 static void *add_ip_callback(void *parm, void *data)
1988 {
1989         return parm;
1990 }
1991
1992 static int
1993 control_get_all_public_ips(struct ctdb_context *ctdb, TALLOC_CTX *tmp_ctx, struct ctdb_all_public_ips **ips)
1994 {
1995         struct ctdb_all_public_ips *tmp_ips;
1996         struct ctdb_node_map *nodemap=NULL;
1997         trbt_tree_t *ip_tree;
1998         int i, j, len, ret;
1999         uint32_t count;
2000
2001         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2002         if (ret != 0) {
2003                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
2004                 return ret;
2005         }
2006
2007         ip_tree = trbt_create(tmp_ctx, 0);
2008
2009         for(i=0;i<nodemap->num;i++){
2010                 if (nodemap->nodes[i].flags & NODE_FLAGS_DELETED) {
2011                         continue;
2012                 }
2013                 if (nodemap->nodes[i].flags & NODE_FLAGS_DISCONNECTED) {
2014                         continue;
2015                 }
2016
2017                 /* read the public ip list from this node */
2018                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &tmp_ips);
2019                 if (ret != 0) {
2020                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", nodemap->nodes[i].pnn));
2021                         return -1;
2022                 }
2023         
2024                 for (j=0; j<tmp_ips->num;j++) {
2025                         struct ctdb_public_ip *node_ip;
2026
2027                         node_ip = talloc(tmp_ctx, struct ctdb_public_ip);
2028                         node_ip->pnn  = tmp_ips->ips[j].pnn;
2029                         node_ip->addr = tmp_ips->ips[j].addr;
2030
2031                         trbt_insertarray32_callback(ip_tree,
2032                                 IP_KEYLEN, ip_key(&tmp_ips->ips[j].addr),
2033                                 add_ip_callback,
2034                                 node_ip);
2035                 }
2036                 talloc_free(tmp_ips);
2037         }
2038
2039         /* traverse */
2040         count = 0;
2041         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_count_callback, &count);
2042
2043         len = offsetof(struct ctdb_all_public_ips, ips) + 
2044                 count*sizeof(struct ctdb_public_ip);
2045         tmp_ips = talloc_zero_size(tmp_ctx, len);
2046         trbt_traversearray32(ip_tree, IP_KEYLEN, getips_store_callback, tmp_ips);
2047
2048         *ips = tmp_ips;
2049
2050         return 0;
2051 }
2052
2053
2054 static void ctdb_every_second(struct event_context *ev, struct timed_event *te, struct timeval t, void *p)
2055 {
2056         struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
2057
2058         event_add_timed(ctdb->ev, ctdb, 
2059                                 timeval_current_ofs(1, 0),
2060                                 ctdb_every_second, ctdb);
2061 }
2062
2063 struct srvid_reply_handler_data {
2064         bool done;
2065         bool wait_for_all;
2066         uint32_t *nodes;
2067         const char *srvid_str;
2068 };
2069
2070 static void srvid_broadcast_reply_handler(struct ctdb_context *ctdb,
2071                                          uint64_t srvid,
2072                                          TDB_DATA data,
2073                                          void *private_data)
2074 {
2075         struct srvid_reply_handler_data *d =
2076                 (struct srvid_reply_handler_data *)private_data;
2077         int i;
2078         int32_t ret;
2079
2080         if (data.dsize != sizeof(ret)) {
2081                 DEBUG(DEBUG_ERR, (__location__ " Wrong reply size\n"));
2082                 return;
2083         }
2084
2085         /* ret will be a PNN (i.e. >=0) on success, or negative on error */
2086         ret = *(int32_t *)data.dptr;
2087         if (ret < 0) {
2088                 DEBUG(DEBUG_ERR,
2089                       ("%s failed with result %d\n", d->srvid_str, ret));
2090                 return;
2091         }
2092
2093         if (!d->wait_for_all) {
2094                 d->done = true;
2095                 return;
2096         }
2097
2098         /* Wait for all replies */
2099         d->done = true;
2100         for (i = 0; i < talloc_array_length(d->nodes); i++) {
2101                 if (d->nodes[i] == ret) {
2102                         DEBUG(DEBUG_INFO,
2103                               ("%s reply received from node %u\n",
2104                                d->srvid_str, ret));
2105                         d->nodes[i] = -1;
2106                 }
2107                 if (d->nodes[i] != -1) {
2108                         /* Found a node that hasn't yet replied */
2109                         d->done = false;
2110                 }
2111         }
2112 }
2113
2114 /* Broadcast the given SRVID to all connected nodes.  Wait for 1 reply
2115  * or replies from all connected nodes.  arg is the data argument to
2116  * pass in the srvid_request structure - pass 0 if this isn't needed.
2117  */
2118 static int srvid_broadcast(struct ctdb_context *ctdb,
2119                            uint64_t srvid, uint32_t arg,
2120                            const char *srvid_str, bool wait_for_all)
2121 {
2122         int ret;
2123         TDB_DATA data;
2124         struct srvid_request request;
2125         struct srvid_reply_handler_data reply_data;
2126         struct timeval tv;
2127
2128         ZERO_STRUCT(request);
2129
2130         /* Time ticks to enable timeouts to be processed */
2131         event_add_timed(ctdb->ev, ctdb, 
2132                                 timeval_current_ofs(1, 0),
2133                                 ctdb_every_second, ctdb);
2134
2135         request.pnn = ctdb_get_pnn(ctdb);
2136         request.srvid = getpid();
2137         request.data = arg;
2138
2139         /* Register message port for reply from recovery master */
2140         ctdb_client_set_message_handler(ctdb, request.srvid,
2141                                         srvid_broadcast_reply_handler,
2142                                         &reply_data);
2143
2144         data.dptr = (uint8_t *)&request;
2145         data.dsize = sizeof(request);
2146
2147         reply_data.wait_for_all = wait_for_all;
2148         reply_data.nodes = NULL;
2149         reply_data.srvid_str = srvid_str;
2150
2151 again:
2152         reply_data.done = false;
2153
2154         if (wait_for_all) {
2155                 struct ctdb_node_map *nodemap;
2156
2157                 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(),
2158                                            CTDB_CURRENT_NODE, ctdb, &nodemap);
2159                 if (ret != 0) {
2160                         DEBUG(DEBUG_ERR,
2161                               ("Unable to get nodemap from current node, try again\n"));
2162                         sleep(1);
2163                         goto again;
2164                 }
2165
2166                 if (reply_data.nodes != NULL) {
2167                         talloc_free(reply_data.nodes);
2168                 }
2169                 reply_data.nodes = list_of_connected_nodes(ctdb, nodemap,
2170                                                            NULL, true);
2171
2172                 talloc_free(nodemap);
2173         }
2174
2175         /* Send to all connected nodes. Only recmaster replies */
2176         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED,
2177                                        srvid, data);
2178         if (ret != 0) {
2179                 /* This can only happen if the socket is closed and
2180                  * there's no way to recover from that, so don't try
2181                  * again.
2182                  */
2183                 DEBUG(DEBUG_ERR,
2184                       ("Failed to send %s request to connected nodes\n",
2185                        srvid_str));
2186                 return -1;
2187         }
2188
2189         tv = timeval_current();
2190         /* This loop terminates the reply is received */
2191         while (timeval_elapsed(&tv) < 5.0 && !reply_data.done) {
2192                 event_loop_once(ctdb->ev);
2193         }
2194
2195         if (!reply_data.done) {
2196                 DEBUG(DEBUG_NOTICE,
2197                       ("Still waiting for confirmation of %s\n", srvid_str));
2198                 sleep(1);
2199                 goto again;
2200         }
2201
2202         ctdb_client_remove_message_handler(ctdb, request.srvid, &reply_data);
2203
2204         talloc_free(reply_data.nodes);
2205
2206         return 0;
2207 }
2208
2209 static int ipreallocate(struct ctdb_context *ctdb)
2210 {
2211         return srvid_broadcast(ctdb, CTDB_SRVID_TAKEOVER_RUN, 0,
2212                                "IP reallocation", false);
2213 }
2214
2215
2216 static int control_ipreallocate(struct ctdb_context *ctdb, int argc, const char **argv)
2217 {
2218         return ipreallocate(ctdb);
2219 }
2220
2221 /*
2222   add a public ip address to a node
2223  */
2224 static int control_addip(struct ctdb_context *ctdb, int argc, const char **argv)
2225 {
2226         int i, ret;
2227         int len, retries = 0;
2228         unsigned mask;
2229         ctdb_sock_addr addr;
2230         struct ctdb_control_ip_iface *pub;
2231         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2232         struct ctdb_all_public_ips *ips;
2233
2234
2235         if (argc != 2) {
2236                 talloc_free(tmp_ctx);
2237                 usage();
2238         }
2239
2240         if (!parse_ip_mask(argv[0], argv[1], &addr, &mask)) {
2241                 DEBUG(DEBUG_ERR, ("Badly formed ip/mask : %s\n", argv[0]));
2242                 talloc_free(tmp_ctx);
2243                 return -1;
2244         }
2245
2246         /* read the public ip list from the node */
2247         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2248         if (ret != 0) {
2249                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %u\n", options.pnn));
2250                 talloc_free(tmp_ctx);
2251                 return -1;
2252         }
2253         for (i=0;i<ips->num;i++) {
2254                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2255                         DEBUG(DEBUG_ERR,("Can not add ip to node. Node already hosts this ip\n"));
2256                         return 0;
2257                 }
2258         }
2259
2260
2261
2262         /* Dont timeout. This command waits for an ip reallocation
2263            which sometimes can take wuite a while if there has
2264            been a recent recovery
2265         */
2266         alarm(0);
2267
2268         len = offsetof(struct ctdb_control_ip_iface, iface) + strlen(argv[1]) + 1;
2269         pub = talloc_size(tmp_ctx, len); 
2270         CTDB_NO_MEMORY(ctdb, pub);
2271
2272         pub->addr  = addr;
2273         pub->mask  = mask;
2274         pub->len   = strlen(argv[1])+1;
2275         memcpy(&pub->iface[0], argv[1], strlen(argv[1])+1);
2276
2277         do {
2278                 ret = ctdb_ctrl_add_public_ip(ctdb, TIMELIMIT(), options.pnn, pub);
2279                 if (ret != 0) {
2280                         DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Wait 3 seconds and try again.\n", options.pnn));
2281                         sleep(3);
2282                         retries++;
2283                 }
2284         } while (retries < 5 && ret != 0);
2285         if (ret != 0) {
2286                 DEBUG(DEBUG_ERR, ("Unable to add public ip to node %u. Giving up.\n", options.pnn));
2287                 talloc_free(tmp_ctx);
2288                 return ret;
2289         }
2290
2291         if (rebalance_node(ctdb, options.pnn) != 0) {
2292                 DEBUG(DEBUG_ERR,("Error when trying to rebalance node\n"));
2293                 return ret;
2294         }
2295
2296         talloc_free(tmp_ctx);
2297         return 0;
2298 }
2299
2300 /*
2301   add a public ip address to a node
2302  */
2303 static int control_ipiface(struct ctdb_context *ctdb, int argc, const char **argv)
2304 {
2305         ctdb_sock_addr addr;
2306
2307         if (argc != 1) {
2308                 usage();
2309         }
2310
2311         if (!parse_ip(argv[0], NULL, 0, &addr)) {
2312                 printf("Badly formed ip : %s\n", argv[0]);
2313                 return -1;
2314         }
2315
2316         printf("IP on interface %s\n", ctdb_sys_find_ifname(&addr));
2317
2318         return 0;
2319 }
2320
2321 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv);
2322
2323 static int control_delip_all(struct ctdb_context *ctdb, int argc, const char **argv, ctdb_sock_addr *addr)
2324 {
2325         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2326         struct ctdb_node_map *nodemap=NULL;
2327         struct ctdb_all_public_ips *ips;
2328         int ret, i, j;
2329
2330         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, tmp_ctx, &nodemap);
2331         if (ret != 0) {
2332                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from current node\n"));
2333                 return ret;
2334         }
2335
2336         /* remove it from the nodes that are not hosting the ip currently */
2337         for(i=0;i<nodemap->num;i++){
2338                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2339                         continue;
2340                 }
2341                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2342                 if (ret != 0) {
2343                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2344                         continue;
2345                 }
2346
2347                 for (j=0;j<ips->num;j++) {
2348                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2349                                 break;
2350                         }
2351                 }
2352                 if (j==ips->num) {
2353                         continue;
2354                 }
2355
2356                 if (ips->ips[j].pnn == nodemap->nodes[i].pnn) {
2357                         continue;
2358                 }
2359
2360                 options.pnn = nodemap->nodes[i].pnn;
2361                 control_delip(ctdb, argc, argv);
2362         }
2363
2364
2365         /* remove it from every node (also the one hosting it) */
2366         for(i=0;i<nodemap->num;i++){
2367                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
2368                         continue;
2369                 }
2370                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), nodemap->nodes[i].pnn, tmp_ctx, &ips);
2371                 if (ret != 0) {
2372                         DEBUG(DEBUG_ERR, ("Unable to get public ip list from node %d\n", nodemap->nodes[i].pnn));
2373                         continue;
2374                 }
2375
2376                 for (j=0;j<ips->num;j++) {
2377                         if (ctdb_same_ip(addr, &ips->ips[j].addr)) {
2378                                 break;
2379                         }
2380                 }
2381                 if (j==ips->num) {
2382                         continue;
2383                 }
2384
2385                 options.pnn = nodemap->nodes[i].pnn;
2386                 control_delip(ctdb, argc, argv);
2387         }
2388
2389         talloc_free(tmp_ctx);
2390         return 0;
2391 }
2392         
2393 /*
2394   delete a public ip address from a node
2395  */
2396 static int control_delip(struct ctdb_context *ctdb, int argc, const char **argv)
2397 {
2398         int i, ret;
2399         ctdb_sock_addr addr;
2400         struct ctdb_control_ip_iface pub;
2401         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2402         struct ctdb_all_public_ips *ips;
2403
2404         if (argc != 1) {
2405                 talloc_free(tmp_ctx);
2406                 usage();
2407         }
2408
2409         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2410                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2411                 return -1;
2412         }
2413
2414         if (options.pnn == CTDB_BROADCAST_ALL) {
2415                 return control_delip_all(ctdb, argc, argv, &addr);
2416         }
2417
2418         pub.addr  = addr;
2419         pub.mask  = 0;
2420         pub.len   = 0;
2421
2422         ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2423         if (ret != 0) {
2424                 DEBUG(DEBUG_ERR, ("Unable to get public ip list from cluster\n"));
2425                 talloc_free(tmp_ctx);
2426                 return ret;
2427         }
2428         
2429         for (i=0;i<ips->num;i++) {
2430                 if (ctdb_same_ip(&addr, &ips->ips[i].addr)) {
2431                         break;
2432                 }
2433         }
2434
2435         if (i==ips->num) {
2436                 DEBUG(DEBUG_ERR, ("This node does not support this public address '%s'\n",
2437                         ctdb_addr_to_str(&addr)));
2438                 talloc_free(tmp_ctx);
2439                 return -1;
2440         }
2441
2442         /* This is an optimisation.  If this node is hosting the IP
2443          * then try to move it somewhere else without invoking a full
2444          * takeover run.  We don't care if this doesn't work!
2445          */
2446         if (ips->ips[i].pnn == options.pnn) {
2447                 (void) try_moveip(ctdb, &addr, -1);
2448         }
2449
2450         ret = ctdb_ctrl_del_public_ip(ctdb, TIMELIMIT(), options.pnn, &pub);
2451         if (ret != 0) {
2452                 DEBUG(DEBUG_ERR, ("Unable to del public ip from node %u\n", options.pnn));
2453                 talloc_free(tmp_ctx);
2454                 return ret;
2455         }
2456
2457         talloc_free(tmp_ctx);
2458         return 0;
2459 }
2460
2461 static int kill_tcp_from_file(struct ctdb_context *ctdb,
2462                               int argc, const char **argv)
2463 {
2464         struct ctdb_control_killtcp *killtcp;
2465         int max_entries, current, i;
2466         struct timeval timeout;
2467         char line[128], src[128], dst[128];
2468         int linenum;
2469         TDB_DATA data;
2470         struct client_async_data *async_data;
2471         struct ctdb_client_control_state *state;
2472
2473         if (argc != 0) {
2474                 usage();
2475         }
2476
2477         linenum = 1;
2478         killtcp = NULL;
2479         max_entries = 0;
2480         current = 0;
2481         while (!feof(stdin)) {
2482                 if (fgets(line, sizeof(line), stdin) == NULL) {
2483                         continue;
2484                 }
2485
2486                 /* Silently skip empty lines */
2487                 if (line[0] == '\n') {
2488                         continue;
2489                 }
2490
2491                 if (sscanf(line, "%s %s\n", src, dst) != 2) {
2492                         DEBUG(DEBUG_ERR, ("Bad line [%d]: '%s'\n",
2493                                           linenum, line));
2494                         talloc_free(killtcp);
2495                         return -1;
2496                 }
2497
2498                 if (current >= max_entries) {
2499                         max_entries += 1024;
2500                         killtcp = talloc_realloc(ctdb, killtcp,
2501                                                  struct ctdb_control_killtcp,
2502                                                  max_entries);
2503                         CTDB_NO_MEMORY(ctdb, killtcp);
2504                 }
2505
2506                 if (!parse_ip_port(src, &killtcp[current].src_addr)) {
2507                         DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2508                                           linenum, src));
2509                         talloc_free(killtcp);
2510                         return -1;
2511                 }
2512
2513                 if (!parse_ip_port(dst, &killtcp[current].dst_addr)) {
2514                         DEBUG(DEBUG_ERR, ("Bad IP:port on line [%d]: '%s'\n",
2515                                           linenum, dst));
2516                         talloc_free(killtcp);
2517                         return -1;
2518                 }
2519
2520                 current++;
2521         }
2522
2523         async_data = talloc_zero(ctdb, struct client_async_data);
2524         if (async_data == NULL) {
2525                 talloc_free(killtcp);
2526                 return -1;
2527         }
2528
2529         for (i = 0; i < current; i++) {
2530
2531                 data.dsize = sizeof(struct ctdb_control_killtcp);
2532                 data.dptr  = (unsigned char *)&killtcp[i];
2533
2534                 timeout = TIMELIMIT();
2535                 state = ctdb_control_send(ctdb, options.pnn, 0,
2536                                           CTDB_CONTROL_KILL_TCP, 0, data,
2537                                           async_data, &timeout, NULL);
2538
2539                 if (state == NULL) {
2540                         DEBUG(DEBUG_ERR,
2541                               ("Failed to call async killtcp control to node %u\n",
2542                                options.pnn));
2543                         talloc_free(killtcp);
2544                         return -1;
2545                 }
2546                 
2547                 ctdb_client_async_add(async_data, state);
2548         }
2549
2550         if (ctdb_client_async_wait(ctdb, async_data) != 0) {
2551                 DEBUG(DEBUG_ERR,("killtcp failed\n"));
2552                 talloc_free(killtcp);
2553                 return -1;
2554         }
2555
2556         talloc_free(killtcp);
2557         return 0;
2558 }
2559
2560
2561 /*
2562   kill a tcp connection
2563  */
2564 static int kill_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2565 {
2566         int ret;
2567         struct ctdb_control_killtcp killtcp;
2568
2569         assert_single_node_only();
2570
2571         if (argc == 0) {
2572                 return kill_tcp_from_file(ctdb, argc, argv);
2573         }
2574
2575         if (argc < 2) {
2576                 usage();
2577         }
2578
2579         if (!parse_ip_port(argv[0], &killtcp.src_addr)) {
2580                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2581                 return -1;
2582         }
2583
2584         if (!parse_ip_port(argv[1], &killtcp.dst_addr)) {
2585                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2586                 return -1;
2587         }
2588
2589         ret = ctdb_ctrl_killtcp(ctdb, TIMELIMIT(), options.pnn, &killtcp);
2590         if (ret != 0) {
2591                 DEBUG(DEBUG_ERR, ("Unable to killtcp from node %u\n", options.pnn));
2592                 return ret;
2593         }
2594
2595         return 0;
2596 }
2597
2598
2599 /*
2600   send a gratious arp
2601  */
2602 static int control_gratious_arp(struct ctdb_context *ctdb, int argc, const char **argv)
2603 {
2604         int ret;
2605         ctdb_sock_addr addr;
2606
2607         assert_single_node_only();
2608
2609         if (argc < 2) {
2610                 usage();
2611         }
2612
2613         if (!parse_ip(argv[0], NULL, 0, &addr)) {
2614                 DEBUG(DEBUG_ERR, ("Bad IP '%s'\n", argv[0]));
2615                 return -1;
2616         }
2617
2618         ret = ctdb_ctrl_gratious_arp(ctdb, TIMELIMIT(), options.pnn, &addr, argv[1]);
2619         if (ret != 0) {
2620                 DEBUG(DEBUG_ERR, ("Unable to send gratious_arp from node %u\n", options.pnn));
2621                 return ret;
2622         }
2623
2624         return 0;
2625 }
2626
2627 /*
2628   register a server id
2629  */
2630 static int regsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2631 {
2632         int ret;
2633         struct ctdb_server_id server_id;
2634
2635         if (argc < 3) {
2636                 usage();
2637         }
2638
2639         server_id.pnn       = strtoul(argv[0], NULL, 0);
2640         server_id.type      = strtoul(argv[1], NULL, 0);
2641         server_id.server_id = strtoul(argv[2], NULL, 0);
2642
2643         ret = ctdb_ctrl_register_server_id(ctdb, TIMELIMIT(), &server_id);
2644         if (ret != 0) {
2645                 DEBUG(DEBUG_ERR, ("Unable to register server_id from node %u\n", options.pnn));
2646                 return ret;
2647         }
2648         DEBUG(DEBUG_ERR,("Srvid registered. Sleeping for 999 seconds\n"));
2649         sleep(999);
2650         return -1;
2651 }
2652
2653 /*
2654   unregister a server id
2655  */
2656 static int unregsrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2657 {
2658         int ret;
2659         struct ctdb_server_id server_id;
2660
2661         if (argc < 3) {
2662                 usage();
2663         }
2664
2665         server_id.pnn       = strtoul(argv[0], NULL, 0);
2666         server_id.type      = strtoul(argv[1], NULL, 0);
2667         server_id.server_id = strtoul(argv[2], NULL, 0);
2668
2669         ret = ctdb_ctrl_unregister_server_id(ctdb, TIMELIMIT(), &server_id);
2670         if (ret != 0) {
2671                 DEBUG(DEBUG_ERR, ("Unable to unregister server_id from node %u\n", options.pnn));
2672                 return ret;
2673         }
2674         return -1;
2675 }
2676
2677 /*
2678   check if a server id exists
2679  */
2680 static int chksrvid(struct ctdb_context *ctdb, int argc, const char **argv)
2681 {
2682         uint32_t status;
2683         int ret;
2684         struct ctdb_server_id server_id;
2685
2686         if (argc < 3) {
2687                 usage();
2688         }
2689
2690         server_id.pnn       = strtoul(argv[0], NULL, 0);
2691         server_id.type      = strtoul(argv[1], NULL, 0);
2692         server_id.server_id = strtoul(argv[2], NULL, 0);
2693
2694         ret = ctdb_ctrl_check_server_id(ctdb, TIMELIMIT(), options.pnn, &server_id, &status);
2695         if (ret != 0) {
2696                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n", options.pnn));
2697                 return ret;
2698         }
2699
2700         if (status) {
2701                 printf("Server id %d:%d:%d EXISTS\n", server_id.pnn, server_id.type, server_id.server_id);
2702         } else {
2703                 printf("Server id %d:%d:%d does NOT exist\n", server_id.pnn, server_id.type, server_id.server_id);
2704         }
2705         return 0;
2706 }
2707
2708 /*
2709   get a list of all server ids that are registered on a node
2710  */
2711 static int getsrvids(struct ctdb_context *ctdb, int argc, const char **argv)
2712 {
2713         int i, ret;
2714         struct ctdb_server_id_list *server_ids;
2715
2716         ret = ctdb_ctrl_get_server_id_list(ctdb, ctdb, TIMELIMIT(), options.pnn, &server_ids);
2717         if (ret != 0) {
2718                 DEBUG(DEBUG_ERR, ("Unable to get server_id list from node %u\n", options.pnn));
2719                 return ret;
2720         }
2721
2722         for (i=0; i<server_ids->num; i++) {
2723                 printf("Server id %d:%d:%d\n", 
2724                         server_ids->server_ids[i].pnn, 
2725                         server_ids->server_ids[i].type, 
2726                         server_ids->server_ids[i].server_id); 
2727         }
2728
2729         return -1;
2730 }
2731
2732 /*
2733   check if a server id exists
2734  */
2735 static int check_srvids(struct ctdb_context *ctdb, int argc, const char **argv)
2736 {
2737         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
2738         uint64_t *ids;
2739         uint8_t *result;
2740         int i;
2741
2742         if (argc < 1) {
2743                 talloc_free(tmp_ctx);
2744                 usage();
2745         }
2746
2747         ids    = talloc_array(tmp_ctx, uint64_t, argc);
2748         result = talloc_array(tmp_ctx, uint8_t, argc);
2749
2750         for (i = 0; i < argc; i++) {
2751                 ids[i] = strtoull(argv[i], NULL, 0);
2752         }
2753
2754         if (!ctdb_client_check_message_handlers(ctdb, ids, argc, result)) {
2755                 DEBUG(DEBUG_ERR, ("Unable to check server_id from node %u\n",
2756                                   options.pnn));
2757                 talloc_free(tmp_ctx);
2758                 return -1;
2759         }
2760
2761         for (i=0; i < argc; i++) {
2762                 printf("Server id %d:%llu %s\n", options.pnn, (long long)ids[i],
2763                        result[i] ? "exists" : "does not exist");
2764         }
2765
2766         talloc_free(tmp_ctx);
2767         return 0;
2768 }
2769
2770 /*
2771   send a tcp tickle ack
2772  */
2773 static int tickle_tcp(struct ctdb_context *ctdb, int argc, const char **argv)
2774 {
2775         int ret;
2776         ctdb_sock_addr  src, dst;
2777
2778         if (argc < 2) {
2779                 usage();
2780         }
2781
2782         if (!parse_ip_port(argv[0], &src)) {
2783                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[0]));
2784                 return -1;
2785         }
2786
2787         if (!parse_ip_port(argv[1], &dst)) {
2788                 DEBUG(DEBUG_ERR, ("Bad IP:port '%s'\n", argv[1]));
2789                 return -1;
2790         }
2791
2792         ret = ctdb_sys_send_tcp(&src, &dst, 0, 0, 0);
2793         if (ret==0) {
2794                 return 0;
2795         }
2796         DEBUG(DEBUG_ERR, ("Error while sending tickle ack\n"));
2797
2798         return -1;
2799 }
2800
2801
2802 /*
2803   display public ip status
2804  */
2805 static int control_ip(struct ctdb_context *ctdb, int argc, const char **argv)
2806 {
2807         int i, ret;
2808         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2809         struct ctdb_all_public_ips *ips;
2810
2811         if (options.pnn == CTDB_BROADCAST_ALL) {
2812                 /* read the list of public ips from all nodes */
2813                 ret = control_get_all_public_ips(ctdb, tmp_ctx, &ips);
2814         } else {
2815                 /* read the public ip list from this node */
2816                 ret = ctdb_ctrl_get_public_ips(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ips);
2817         }
2818         if (ret != 0) {
2819                 DEBUG(DEBUG_ERR, ("Unable to get public ips from node %u\n", options.pnn));
2820                 talloc_free(tmp_ctx);
2821                 return ret;
2822         }
2823
2824         if (options.machinereadable){
2825                 printf(":Public IP:Node:");
2826                 if (options.verbose){
2827                         printf("ActiveInterface:AvailableInterfaces:ConfiguredInterfaces:");
2828                 }
2829                 printf("\n");
2830         } else {
2831                 if (options.pnn == CTDB_BROADCAST_ALL) {
2832                         printf("Public IPs on ALL nodes\n");
2833                 } else {
2834                         printf("Public IPs on node %u\n", options.pnn);
2835                 }
2836         }
2837
2838         for (i=1;i<=ips->num;i++) {
2839                 struct ctdb_control_public_ip_info *info = NULL;
2840                 int32_t pnn;
2841                 char *aciface = NULL;
2842                 char *avifaces = NULL;
2843                 char *cifaces = NULL;
2844
2845                 if (options.pnn == CTDB_BROADCAST_ALL) {
2846                         pnn = ips->ips[ips->num-i].pnn;
2847                 } else {
2848                         pnn = options.pnn;
2849                 }
2850
2851                 if (pnn != -1) {
2852                         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), pnn, ctdb,
2853                                                    &ips->ips[ips->num-i].addr, &info);
2854                 } else {
2855                         ret = -1;
2856                 }
2857
2858                 if (ret == 0) {
2859                         int j;
2860                         for (j=0; j < info->num; j++) {
2861                                 if (cifaces == NULL) {
2862                                         cifaces = talloc_strdup(info,
2863                                                                 info->ifaces[j].name);
2864                                 } else {
2865                                         cifaces = talloc_asprintf_append(cifaces,
2866                                                                          ",%s",
2867                                                                          info->ifaces[j].name);
2868                                 }
2869
2870                                 if (info->active_idx == j) {
2871                                         aciface = info->ifaces[j].name;
2872                                 }
2873
2874                                 if (info->ifaces[j].link_state == 0) {
2875                                         continue;
2876                                 }
2877
2878                                 if (avifaces == NULL) {
2879                                         avifaces = talloc_strdup(info, info->ifaces[j].name);
2880                                 } else {
2881                                         avifaces = talloc_asprintf_append(avifaces,
2882                                                                           ",%s",
2883                                                                           info->ifaces[j].name);
2884                                 }
2885                         }
2886                 }
2887
2888                 if (options.machinereadable){
2889                         printf(":%s:%d:",
2890                                 ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2891                                 ips->ips[ips->num-i].pnn);
2892                         if (options.verbose){
2893                                 printf("%s:%s:%s:",
2894                                         aciface?aciface:"",
2895                                         avifaces?avifaces:"",
2896                                         cifaces?cifaces:"");
2897                         }
2898                         printf("\n");
2899                 } else {
2900                         if (options.verbose) {
2901                                 printf("%s node[%d] active[%s] available[%s] configured[%s]\n",
2902                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2903                                         ips->ips[ips->num-i].pnn,
2904                                         aciface?aciface:"",
2905                                         avifaces?avifaces:"",
2906                                         cifaces?cifaces:"");
2907                         } else {
2908                                 printf("%s %d\n",
2909                                         ctdb_addr_to_str(&ips->ips[ips->num-i].addr),
2910                                         ips->ips[ips->num-i].pnn);
2911                         }
2912                 }
2913                 talloc_free(info);
2914         }
2915
2916         talloc_free(tmp_ctx);
2917         return 0;
2918 }
2919
2920 /*
2921   public ip info
2922  */
2923 static int control_ipinfo(struct ctdb_context *ctdb, int argc, const char **argv)
2924 {
2925         int i, ret;
2926         ctdb_sock_addr addr;
2927         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2928         struct ctdb_control_public_ip_info *info;
2929
2930         if (argc != 1) {
2931                 talloc_free(tmp_ctx);
2932                 usage();
2933         }
2934
2935         if (parse_ip(argv[0], NULL, 0, &addr) == 0) {
2936                 DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s'\n", argv[0]));
2937                 return -1;
2938         }
2939
2940         /* read the public ip info from this node */
2941         ret = ctdb_ctrl_get_public_ip_info(ctdb, TIMELIMIT(), options.pnn,
2942                                            tmp_ctx, &addr, &info);
2943         if (ret != 0) {
2944                 DEBUG(DEBUG_ERR, ("Unable to get public ip[%s]info from node %u\n",
2945                                   argv[0], options.pnn));
2946                 talloc_free(tmp_ctx);
2947                 return ret;
2948         }
2949
2950         printf("Public IP[%s] info on node %u\n",
2951                ctdb_addr_to_str(&info->ip.addr),
2952                options.pnn);
2953
2954         printf("IP:%s\nCurrentNode:%d\nNumInterfaces:%u\n",
2955                ctdb_addr_to_str(&info->ip.addr),
2956                info->ip.pnn, info->num);
2957
2958         for (i=0; i<info->num; i++) {
2959                 info->ifaces[i].name[CTDB_IFACE_SIZE] = '\0';
2960
2961                 printf("Interface[%u]: Name:%s Link:%s References:%u%s\n",
2962                        i+1, info->ifaces[i].name,
2963                        info->ifaces[i].link_state?"up":"down",
2964                        (unsigned int)info->ifaces[i].references,
2965                        (i==info->active_idx)?" (active)":"");
2966         }
2967
2968         talloc_free(tmp_ctx);
2969         return 0;
2970 }
2971
2972 /*
2973   display interfaces status
2974  */
2975 static int control_ifaces(struct ctdb_context *ctdb, int argc, const char **argv)
2976 {
2977         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
2978         int i;
2979         struct ctdb_control_get_ifaces *ifaces;
2980         int ret;
2981
2982         /* read the public ip list from this node */
2983         ret = ctdb_ctrl_get_ifaces(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &ifaces);
2984         if (ret != 0) {
2985                 DEBUG(DEBUG_ERR, ("Unable to get interfaces from node %u\n",
2986                                   options.pnn));
2987                 talloc_free(tmp_ctx);
2988                 return -1;
2989         }
2990
2991         if (options.machinereadable){
2992                 printf(":Name:LinkStatus:References:\n");
2993         } else {
2994                 printf("Interfaces on node %u\n", options.pnn);
2995         }
2996
2997         for (i=0; i<ifaces->num; i++) {
2998                 if (options.machinereadable){
2999                         printf(":%s:%s:%u\n",
3000                                ifaces->ifaces[i].name,
3001                                ifaces->ifaces[i].link_state?"1":"0",
3002                                (unsigned int)ifaces->ifaces[i].references);
3003                 } else {
3004                         printf("name:%s link:%s references:%u\n",
3005                                ifaces->ifaces[i].name,
3006                                ifaces->ifaces[i].link_state?"up":"down",
3007                                (unsigned int)ifaces->ifaces[i].references);
3008                 }
3009         }
3010
3011         talloc_free(tmp_ctx);
3012         return 0;
3013 }
3014
3015
3016 /*
3017   set link status of an interface
3018  */
3019 static int control_setifacelink(struct ctdb_context *ctdb, int argc, const char **argv)
3020 {
3021         int ret;
3022         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3023         struct ctdb_control_iface_info info;
3024
3025         ZERO_STRUCT(info);
3026
3027         if (argc != 2) {
3028                 usage();
3029         }
3030
3031         if (strlen(argv[0]) > CTDB_IFACE_SIZE) {
3032                 DEBUG(DEBUG_ERR, ("interfaces name '%s' too long\n",
3033                                   argv[0]));
3034                 talloc_free(tmp_ctx);
3035                 return -1;
3036         }
3037         strcpy(info.name, argv[0]);
3038
3039         if (strcmp(argv[1], "up") == 0) {
3040                 info.link_state = 1;
3041         } else if (strcmp(argv[1], "down") == 0) {
3042                 info.link_state = 0;
3043         } else {
3044                 DEBUG(DEBUG_ERR, ("link state invalid '%s' should be 'up' or 'down'\n",
3045                                   argv[1]));
3046                 talloc_free(tmp_ctx);
3047                 return -1;
3048         }
3049
3050         /* read the public ip list from this node */
3051         ret = ctdb_ctrl_set_iface_link(ctdb, TIMELIMIT(), options.pnn,
3052                                    tmp_ctx, &info);
3053         if (ret != 0) {
3054                 DEBUG(DEBUG_ERR, ("Unable to set link state for interfaces %s node %u\n",
3055                                   argv[0], options.pnn));
3056                 talloc_free(tmp_ctx);
3057                 return ret;
3058         }
3059
3060         talloc_free(tmp_ctx);
3061         return 0;
3062 }
3063
3064 /*
3065   display pid of a ctdb daemon
3066  */
3067 static int control_getpid(struct ctdb_context *ctdb, int argc, const char **argv)
3068 {
3069         uint32_t pid;
3070         int ret;
3071
3072         ret = ctdb_ctrl_getpid(ctdb, TIMELIMIT(), options.pnn, &pid);
3073         if (ret != 0) {
3074                 DEBUG(DEBUG_ERR, ("Unable to get daemon pid from node %u\n", options.pnn));
3075                 return ret;
3076         }
3077         printf("Pid:%d\n", pid);
3078
3079         return 0;
3080 }
3081
3082 typedef bool update_flags_handler_t(struct ctdb_context *ctdb, void *data);
3083
3084 static int update_flags_and_ipreallocate(struct ctdb_context *ctdb,
3085                                               void *data,
3086                                               update_flags_handler_t handler,
3087                                               uint32_t flag,
3088                                               const char *desc,
3089                                               bool set_flag)
3090 {
3091         struct ctdb_node_map *nodemap = NULL;
3092         bool flag_is_set;
3093         int ret;
3094
3095         /* Check if the node is already in the desired state */
3096         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3097         if (ret != 0) {
3098                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3099                 exit(10);
3100         }
3101         flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3102         if (set_flag == flag_is_set) {
3103                 DEBUG(DEBUG_NOTICE, ("Node %d is %s %s\n", options.pnn,
3104                                      (set_flag ? "already" : "not"), desc));
3105                 return 0;
3106         }
3107
3108         do {
3109                 if (!handler(ctdb, data)) {
3110                         DEBUG(DEBUG_WARNING,
3111                               ("Failed to send control to set state %s on node %u, try again\n",
3112                                desc, options.pnn));
3113                 }
3114
3115                 sleep(1);
3116
3117                 /* Read the nodemap and verify the change took effect.
3118                  * Even if the above control/hanlder timed out then it
3119                  * could still have worked!
3120                  */
3121                 ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE,
3122                                          ctdb, &nodemap);
3123                 if (ret != 0) {
3124                         DEBUG(DEBUG_WARNING,
3125                               ("Unable to get nodemap from local node, try again\n"));
3126                 }
3127                 flag_is_set = nodemap->nodes[options.pnn].flags & flag;
3128         } while (nodemap == NULL || (set_flag != flag_is_set));
3129
3130         return ipreallocate(ctdb);
3131 }
3132
3133 /* Administratively disable a node */
3134 static bool update_flags_disabled(struct ctdb_context *ctdb, void *data)
3135 {
3136         int ret;
3137
3138         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3139                                  NODE_FLAGS_PERMANENTLY_DISABLED, 0);
3140         return ret == 0;
3141 }
3142
3143 static int control_disable(struct ctdb_context *ctdb, int argc, const char **argv)
3144 {
3145         return update_flags_and_ipreallocate(ctdb, NULL,
3146                                                   update_flags_disabled,
3147                                                   NODE_FLAGS_PERMANENTLY_DISABLED,
3148                                                   "disabled",
3149                                                   true /* set_flag*/);
3150 }
3151
3152 /* Administratively re-enable a node */
3153 static bool update_flags_not_disabled(struct ctdb_context *ctdb, void *data)
3154 {
3155         int ret;
3156
3157         ret = ctdb_ctrl_modflags(ctdb, TIMELIMIT(), options.pnn,
3158                                  0, NODE_FLAGS_PERMANENTLY_DISABLED);
3159         return ret == 0;
3160 }
3161
3162 static int control_enable(struct ctdb_context *ctdb,  int argc, const char **argv)
3163 {
3164         return update_flags_and_ipreallocate(ctdb, NULL,
3165                                                   update_flags_not_disabled,
3166                                                   NODE_FLAGS_PERMANENTLY_DISABLED,
3167                                                   "disabled",
3168                                                   false /* set_flag*/);
3169 }
3170
3171 /* Stop a node */
3172 static bool update_flags_stopped(struct ctdb_context *ctdb, void *data)
3173 {
3174         int ret;
3175
3176         ret = ctdb_ctrl_stop_node(ctdb, TIMELIMIT(), options.pnn);
3177
3178         return ret == 0;
3179 }
3180
3181 static int control_stop(struct ctdb_context *ctdb, int argc, const char **argv)
3182 {
3183         return update_flags_and_ipreallocate(ctdb, NULL,
3184                                                   update_flags_stopped,
3185                                                   NODE_FLAGS_STOPPED,
3186                                                   "stopped",
3187                                                   true /* set_flag*/);
3188 }
3189
3190 /* Continue a stopped node */
3191 static bool update_flags_not_stopped(struct ctdb_context *ctdb, void *data)
3192 {
3193         int ret;
3194
3195         ret = ctdb_ctrl_continue_node(ctdb, TIMELIMIT(), options.pnn);
3196
3197         return ret == 0;
3198 }
3199
3200 static int control_continue(struct ctdb_context *ctdb, int argc, const char **argv)
3201 {
3202         return update_flags_and_ipreallocate(ctdb, NULL,
3203                                                   update_flags_not_stopped,
3204                                                   NODE_FLAGS_STOPPED,
3205                                                   "stopped",
3206                                                   false /* set_flag */);
3207 }
3208
3209 static uint32_t get_generation(struct ctdb_context *ctdb)
3210 {
3211         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3212         struct ctdb_vnn_map *vnnmap=NULL;
3213         int ret;
3214         uint32_t generation;
3215
3216         /* wait until the recmaster is not in recovery mode */
3217         while (1) {
3218                 uint32_t recmode, recmaster;
3219                 
3220                 if (vnnmap != NULL) {
3221                         talloc_free(vnnmap);
3222                         vnnmap = NULL;
3223                 }
3224
3225                 /* get the recmaster */
3226                 ret = ctdb_ctrl_getrecmaster(ctdb, tmp_ctx, TIMELIMIT(), CTDB_CURRENT_NODE, &recmaster);
3227                 if (ret != 0) {
3228                         DEBUG(DEBUG_ERR, ("Unable to get recmaster from node %u\n", options.pnn));
3229                         talloc_free(tmp_ctx);
3230                         exit(10);
3231                 }
3232
3233                 /* get recovery mode */
3234                 ret = ctdb_ctrl_getrecmode(ctdb, tmp_ctx, TIMELIMIT(), recmaster, &recmode);
3235                 if (ret != 0) {
3236                         DEBUG(DEBUG_ERR, ("Unable to get recmode from node %u\n", options.pnn));
3237                         talloc_free(tmp_ctx);
3238                         exit(10);
3239                 }
3240
3241                 /* get the current generation number */
3242                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), recmaster, tmp_ctx, &vnnmap);
3243                 if (ret != 0) {
3244                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from recmaster (%u)\n", recmaster));
3245                         talloc_free(tmp_ctx);
3246                         exit(10);
3247                 }
3248
3249                 if ((recmode == CTDB_RECOVERY_NORMAL) && (vnnmap->generation != 1)) {
3250                         generation = vnnmap->generation;
3251                         talloc_free(tmp_ctx);
3252                         return generation;
3253                 }
3254                 sleep(1);
3255         }
3256 }
3257
3258 /* Ban a node */
3259 static bool update_state_banned(struct ctdb_context *ctdb, void *data)
3260 {
3261         struct ctdb_ban_time *bantime = (struct ctdb_ban_time *)data;
3262         int ret;
3263
3264         ret = ctdb_ctrl_set_ban(ctdb, TIMELIMIT(), options.pnn, bantime);
3265
3266         return ret == 0;
3267 }
3268
3269 static int control_ban(struct ctdb_context *ctdb, int argc, const char **argv)
3270 {
3271         struct ctdb_ban_time bantime;
3272
3273         if (argc < 1) {
3274                 usage();
3275         }
3276         
3277         bantime.pnn  = options.pnn;
3278         bantime.time = strtoul(argv[0], NULL, 0);
3279
3280         if (bantime.time == 0) {
3281                 DEBUG(DEBUG_ERR, ("Invalid ban time specified - must be >0\n"));
3282                 return -1;
3283         }
3284
3285         return update_flags_and_ipreallocate(ctdb, &bantime,
3286                                                   update_state_banned,
3287                                                   NODE_FLAGS_BANNED,
3288                                                   "banned",
3289                                                   true /* set_flag*/);
3290 }
3291
3292
3293 /* Unban a node */
3294 static int control_unban(struct ctdb_context *ctdb, int argc, const char **argv)
3295 {
3296         struct ctdb_ban_time bantime;
3297
3298         bantime.pnn  = options.pnn;
3299         bantime.time = 0;
3300
3301         return update_flags_and_ipreallocate(ctdb, &bantime,
3302                                                   update_state_banned,
3303                                                   NODE_FLAGS_BANNED,
3304                                                   "banned",
3305                                                   false /* set_flag*/);
3306 }
3307
3308 /*
3309   show ban information for a node
3310  */
3311 static int control_showban(struct ctdb_context *ctdb, int argc, const char **argv)
3312 {
3313         int ret;
3314         struct ctdb_node_map *nodemap=NULL;
3315         struct ctdb_ban_time *bantime;
3316
3317         /* verify the node exists */
3318         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
3319         if (ret != 0) {
3320                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
3321                 return ret;
3322         }
3323
3324         ret = ctdb_ctrl_get_ban(ctdb, TIMELIMIT(), options.pnn, ctdb, &bantime);
3325         if (ret != 0) {
3326                 DEBUG(DEBUG_ERR,("Showing ban info for node %d failed.\n", options.pnn));
3327                 return -1;
3328         }       
3329
3330         if (bantime->time == 0) {
3331                 printf("Node %u is not banned\n", bantime->pnn);
3332         } else {
3333                 printf("Node %u is banned, %d seconds remaining\n",
3334                        bantime->pnn, bantime->time);
3335         }
3336
3337         return 0;
3338 }
3339
3340 /*
3341   shutdown a daemon
3342  */
3343 static int control_shutdown(struct ctdb_context *ctdb, int argc, const char **argv)
3344 {
3345         int ret;
3346
3347         ret = ctdb_ctrl_shutdown(ctdb, TIMELIMIT(), options.pnn);
3348         if (ret != 0) {
3349                 DEBUG(DEBUG_ERR, ("Unable to shutdown node %u\n", options.pnn));
3350                 return ret;
3351         }
3352
3353         return 0;
3354 }
3355
3356 /*
3357   trigger a recovery
3358  */
3359 static int control_recover(struct ctdb_context *ctdb, int argc, const char **argv)
3360 {
3361         int ret;
3362         uint32_t generation, next_generation;
3363         bool force;
3364
3365         /* "force" option ignores freeze failure and forces recovery */
3366         force = (argc == 1) && (strcasecmp(argv[0], "force") == 0);
3367
3368         /* record the current generation number */
3369         generation = get_generation(ctdb);
3370
3371         ret = ctdb_ctrl_freeze_priority(ctdb, TIMELIMIT(), options.pnn, 1);
3372         if (ret != 0) {
3373                 if (!force) {
3374                         DEBUG(DEBUG_ERR, ("Unable to freeze node\n"));
3375                         return ret;
3376                 }
3377                 DEBUG(DEBUG_WARNING, ("Unable to freeze node but proceeding because \"force\" option given\n"));
3378         }
3379
3380         ret = ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
3381         if (ret != 0) {
3382                 DEBUG(DEBUG_ERR, ("Unable to set recovery mode\n"));
3383                 return ret;
3384         }
3385
3386         /* wait until we are in a new generation */
3387         while (1) {
3388                 next_generation = get_generation(ctdb);
3389                 if (next_generation != generation) {
3390                         return 0;
3391                 }
3392                 sleep(1);
3393         }
3394
3395         return 0;
3396 }
3397
3398
3399 /*
3400   display monitoring mode of a remote node
3401  */
3402 static int control_getmonmode(struct ctdb_context *ctdb, int argc, const char **argv)
3403 {
3404         uint32_t monmode;
3405         int ret;
3406
3407         ret = ctdb_ctrl_getmonmode(ctdb, TIMELIMIT(), options.pnn, &monmode);
3408         if (ret != 0) {
3409                 DEBUG(DEBUG_ERR, ("Unable to get monmode from node %u\n", options.pnn));
3410                 return ret;
3411         }
3412         if (!options.machinereadable){
3413                 printf("Monitoring mode:%s (%d)\n",monmode==CTDB_MONITORING_ACTIVE?"ACTIVE":"DISABLED",monmode);
3414         } else {
3415                 printf(":mode:\n");
3416                 printf(":%d:\n",monmode);
3417         }
3418         return 0;
3419 }
3420
3421
3422 /*
3423   display capabilities of a remote node
3424  */
3425 static int control_getcapabilities(struct ctdb_context *ctdb, int argc, const char **argv)
3426 {
3427         uint32_t capabilities;
3428         int ret;
3429
3430         ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), options.pnn, &capabilities);
3431         if (ret != 0) {
3432                 DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", options.pnn));
3433                 return -1;
3434         }
3435         
3436         if (!options.machinereadable){
3437                 printf("RECMASTER: %s\n", (capabilities&CTDB_CAP_RECMASTER)?"YES":"NO");
3438                 printf("LMASTER: %s\n", (capabilities&CTDB_CAP_LMASTER)?"YES":"NO");
3439                 printf("LVS: %s\n", (capabilities&CTDB_CAP_LVS)?"YES":"NO");
3440                 printf("NATGW: %s\n", (capabilities&CTDB_CAP_NATGW)?"YES":"NO");
3441         } else {
3442                 printf(":RECMASTER:LMASTER:LVS:NATGW:\n");
3443                 printf(":%d:%d:%d:%d:\n",
3444                         !!(capabilities&CTDB_CAP_RECMASTER),
3445                         !!(capabilities&CTDB_CAP_LMASTER),
3446                         !!(capabilities&CTDB_CAP_LVS),
3447                         !!(capabilities&CTDB_CAP_NATGW));
3448         }
3449         return 0;
3450 }
3451
3452 /*
3453   display lvs configuration
3454  */
3455 static int control_lvs(struct ctdb_context *ctdb, int argc, const char **argv)
3456 {
3457         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3458         uint32_t *capabilities;
3459         struct ctdb_node_map *nodemap=NULL;
3460         int i, ret;
3461         int healthy_count = 0;
3462
3463         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3464         if (ret != 0) {
3465                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3466                 talloc_free(tmp_ctx);
3467                 return -1;
3468         }
3469
3470         capabilities = talloc_array(ctdb, uint32_t, nodemap->num);
3471         CTDB_NO_MEMORY(ctdb, capabilities);
3472         
3473         ret = 0;
3474
3475         /* collect capabilities for all connected nodes */
3476         for (i=0; i<nodemap->num; i++) {
3477                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3478                         continue;
3479                 }
3480                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3481                         continue;
3482                 }
3483
3484                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3485                 if (ret != 0) {
3486                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3487                         ret = -1;
3488                         goto done;
3489                 }
3490
3491                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3492                         continue;
3493                 }
3494
3495                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3496                         healthy_count++;
3497                 }
3498         }
3499
3500         /* Print all LVS nodes */
3501         for (i=0; i<nodemap->num; i++) {
3502                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3503                         continue;
3504                 }
3505                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3506                         continue;
3507                 }
3508                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3509                         continue;
3510                 }
3511
3512                 if (healthy_count != 0) {
3513                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3514                                 continue;
3515                         }
3516                 }
3517
3518                 printf("%d:%s\n", i, 
3519                         ctdb_addr_to_str(&nodemap->nodes[i].addr));
3520         }
3521
3522 done:
3523         talloc_free(tmp_ctx);
3524         return ret;
3525 }
3526
3527 /*
3528   display who is the lvs master
3529  */
3530 static int control_lvsmaster(struct ctdb_context *ctdb, int argc, const char **argv)
3531 {
3532         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3533         uint32_t *capabilities;
3534         struct ctdb_node_map *nodemap=NULL;
3535         int i, ret;
3536         int healthy_count = 0;
3537
3538         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &nodemap);
3539         if (ret != 0) {
3540                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
3541                 talloc_free(tmp_ctx);
3542                 return -1;
3543         }
3544
3545         capabilities = talloc_array(tmp_ctx, uint32_t, nodemap->num);
3546         if (capabilities == NULL) {
3547                 talloc_free(tmp_ctx);
3548                 CTDB_NO_MEMORY(ctdb, capabilities);
3549         }
3550
3551         /* collect capabilities for all connected nodes */
3552         for (i=0; i<nodemap->num; i++) {
3553                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3554                         continue;
3555                 }
3556                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3557                         continue;
3558                 }
3559         
3560                 ret = ctdb_ctrl_getcapabilities(ctdb, TIMELIMIT(), i, &capabilities[i]);
3561                 if (ret != 0) {
3562                         DEBUG(DEBUG_ERR, ("Unable to get capabilities from node %u\n", i));
3563                         ret = -1;
3564                         goto done;
3565                 }
3566
3567                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3568                         continue;
3569                 }
3570
3571                 if (!(nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY)) {
3572                         healthy_count++;
3573                 }
3574         }
3575
3576         ret = -1;
3577
3578         /* find and show the lvsmaster */
3579         for (i=0; i<nodemap->num; i++) {
3580                 if (nodemap->nodes[i].flags & NODE_FLAGS_INACTIVE) {
3581                         continue;
3582                 }
3583                 if (nodemap->nodes[i].flags & NODE_FLAGS_PERMANENTLY_DISABLED) {
3584                         continue;
3585                 }
3586                 if (!(capabilities[i] & CTDB_CAP_LVS)) {
3587                         continue;
3588                 }
3589
3590                 if (healthy_count != 0) {
3591                         if (nodemap->nodes[i].flags & NODE_FLAGS_UNHEALTHY) {
3592                                 continue;
3593                         }
3594                 }
3595
3596                 if (options.machinereadable){
3597                         printf("%d\n", i);
3598                 } else {
3599                         printf("Node %d is LVS master\n", i);
3600                 }
3601                 ret = 0;
3602                 goto done;
3603         }
3604
3605         printf("There is no LVS master\n");
3606 done:
3607         talloc_free(tmp_ctx);
3608         return ret;
3609 }
3610
3611 /*
3612   disable monitoring on a  node
3613  */
3614 static int control_disable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3615 {
3616         
3617         int ret;
3618
3619         ret = ctdb_ctrl_disable_monmode(ctdb, TIMELIMIT(), options.pnn);
3620         if (ret != 0) {
3621                 DEBUG(DEBUG_ERR, ("Unable to disable monmode on node %u\n", options.pnn));
3622                 return ret;
3623         }
3624         printf("Monitoring mode:%s\n","DISABLED");
3625
3626         return 0;
3627 }
3628
3629 /*
3630   enable monitoring on a  node
3631  */
3632 static int control_enable_monmode(struct ctdb_context *ctdb, int argc, const char **argv)
3633 {
3634         
3635         int ret;
3636
3637         ret = ctdb_ctrl_enable_monmode(ctdb, TIMELIMIT(), options.pnn);
3638         if (ret != 0) {
3639                 DEBUG(DEBUG_ERR, ("Unable to enable monmode on node %u\n", options.pnn));
3640                 return ret;
3641         }
3642         printf("Monitoring mode:%s\n","ACTIVE");
3643
3644         return 0;
3645 }
3646
3647 /*
3648   display remote list of keys/data for a db
3649  */
3650 static int control_catdb(struct ctdb_context *ctdb, int argc, const char **argv)
3651 {
3652         const char *db_name;
3653         struct ctdb_db_context *ctdb_db;
3654         int ret;
3655         struct ctdb_dump_db_context c;
3656         uint8_t flags;
3657
3658         if (argc < 1) {
3659                 usage();
3660         }
3661
3662         db_name = argv[0];
3663
3664         if (!db_exists(ctdb, db_name, NULL, &flags)) {
3665                 return -1;
3666         }
3667
3668         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3669         if (ctdb_db == NULL) {
3670                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3671                 return -1;
3672         }
3673
3674         if (options.printlmaster) {
3675                 ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn,
3676                                           ctdb, &ctdb->vnn_map);
3677                 if (ret != 0) {
3678                         DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
3679                                           options.pnn));
3680                         return ret;
3681                 }
3682         }
3683
3684         ZERO_STRUCT(c);
3685         c.f = stdout;
3686         c.printemptyrecords = (bool)options.printemptyrecords;
3687         c.printdatasize = (bool)options.printdatasize;
3688         c.printlmaster = (bool)options.printlmaster;
3689         c.printhash = (bool)options.printhash;
3690         c.printrecordflags = (bool)options.printrecordflags;
3691
3692         /* traverse and dump the cluster tdb */
3693         ret = ctdb_dump_db(ctdb_db, &c);
3694         if (ret == -1) {
3695                 DEBUG(DEBUG_ERR, ("Unable to dump database\n"));
3696                 DEBUG(DEBUG_ERR, ("Maybe try 'ctdb getdbstatus %s'"
3697                                   " and 'ctdb getvar AllowUnhealthyDBRead'\n",
3698                                   db_name));
3699                 return -1;
3700         }
3701         talloc_free(ctdb_db);
3702
3703         printf("Dumped %d records\n", ret);
3704         return 0;
3705 }
3706
3707 struct cattdb_data {
3708         struct ctdb_context *ctdb;
3709         uint32_t count;
3710 };
3711
3712 static int cattdb_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
3713 {
3714         struct cattdb_data *d = private_data;
3715         struct ctdb_dump_db_context c;
3716
3717         d->count++;
3718
3719         ZERO_STRUCT(c);
3720         c.f = stdout;
3721         c.printemptyrecords = (bool)options.printemptyrecords;
3722         c.printdatasize = (bool)options.printdatasize;
3723         c.printlmaster = false;
3724         c.printhash = (bool)options.printhash;
3725         c.printrecordflags = true;
3726
3727         return ctdb_dumpdb_record(d->ctdb, key, data, &c);
3728 }
3729
3730 /*
3731   cat the local tdb database using same format as catdb
3732  */
3733 static int control_cattdb(struct ctdb_context *ctdb, int argc, const char **argv)
3734 {
3735         const char *db_name;
3736         struct ctdb_db_context *ctdb_db;
3737         struct cattdb_data d;
3738         uint8_t flags;
3739
3740         if (argc < 1) {
3741                 usage();
3742         }
3743
3744         db_name = argv[0];
3745
3746         if (!db_exists(ctdb, db_name, NULL, &flags)) {
3747                 return -1;
3748         }
3749
3750         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3751         if (ctdb_db == NULL) {
3752                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3753                 return -1;
3754         }
3755
3756         /* traverse the local tdb */
3757         d.count = 0;
3758         d.ctdb  = ctdb;
3759         if (tdb_traverse_read(ctdb_db->ltdb->tdb, cattdb_traverse, &d) == -1) {
3760                 printf("Failed to cattdb data\n");
3761                 exit(10);
3762         }
3763         talloc_free(ctdb_db);
3764
3765         printf("Dumped %d records\n", d.count);
3766         return 0;
3767 }
3768
3769 /*
3770   display the content of a database key
3771  */
3772 static int control_readkey(struct ctdb_context *ctdb, int argc, const char **argv)
3773 {
3774         const char *db_name;
3775         struct ctdb_db_context *ctdb_db;
3776         struct ctdb_record_handle *h;
3777         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3778         TDB_DATA key, data;
3779         uint8_t flags;
3780
3781         if (argc < 2) {
3782                 usage();
3783         }
3784
3785         db_name = argv[0];
3786
3787         if (!db_exists(ctdb, db_name, NULL, &flags)) {
3788                 return -1;
3789         }
3790
3791         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3792         if (ctdb_db == NULL) {
3793                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3794                 return -1;
3795         }
3796
3797         key.dptr  = discard_const(argv[1]);
3798         key.dsize = strlen((char *)key.dptr);
3799
3800         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3801         if (h == NULL) {
3802                 printf("Failed to fetch record '%s' on node %d\n", 
3803                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3804                 talloc_free(tmp_ctx);
3805                 exit(10);
3806         }
3807
3808         printf("Data: size:%d ptr:[%.*s]\n", (int)data.dsize, (int)data.dsize, data.dptr);
3809
3810         talloc_free(ctdb_db);
3811         talloc_free(tmp_ctx);
3812         return 0;
3813 }
3814
3815 /*
3816   display the content of a database key
3817  */
3818 static int control_writekey(struct ctdb_context *ctdb, int argc, const char **argv)
3819 {
3820         const char *db_name;
3821         struct ctdb_db_context *ctdb_db;
3822         struct ctdb_record_handle *h;
3823         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3824         TDB_DATA key, data;
3825         uint8_t flags;
3826
3827         if (argc < 3) {
3828                 usage();
3829         }
3830
3831         db_name = argv[0];
3832
3833         if (!db_exists(ctdb, db_name, NULL, &flags)) {
3834                 return -1;
3835         }
3836
3837         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, flags & CTDB_DB_FLAGS_PERSISTENT, 0);
3838         if (ctdb_db == NULL) {
3839                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3840                 return -1;
3841         }
3842
3843         key.dptr  = discard_const(argv[1]);
3844         key.dsize = strlen((char *)key.dptr);
3845
3846         h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
3847         if (h == NULL) {
3848                 printf("Failed to fetch record '%s' on node %d\n", 
3849                         (const char *)key.dptr, ctdb_get_pnn(ctdb));
3850                 talloc_free(tmp_ctx);
3851                 exit(10);
3852         }
3853
3854         data.dptr  = discard_const(argv[2]);
3855         data.dsize = strlen((char *)data.dptr);
3856
3857         if (ctdb_record_store(h, data) != 0) {
3858                 printf("Failed to store record\n");
3859         }
3860
3861         talloc_free(h);
3862         talloc_free(ctdb_db);
3863         talloc_free(tmp_ctx);
3864         return 0;
3865 }
3866
3867 /*
3868   fetch a record from a persistent database
3869  */
3870 static int control_pfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3871 {
3872         const char *db_name;
3873         struct ctdb_db_context *ctdb_db;
3874         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
3875         struct ctdb_transaction_handle *h;
3876         TDB_DATA key, data;
3877         int fd, ret;
3878         bool persistent;
3879         uint8_t flags;
3880
3881         if (argc < 2) {
3882                 talloc_free(tmp_ctx);
3883                 usage();
3884         }
3885
3886         db_name = argv[0];
3887
3888         if (!db_exists(ctdb, db_name, NULL, &flags)) {
3889                 talloc_free(tmp_ctx);
3890                 return -1;
3891         }
3892
3893         persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
3894         if (!persistent) {
3895                 DEBUG(DEBUG_ERR,("Database '%s' is not persistent\n", db_name));
3896                 talloc_free(tmp_ctx);
3897                 return -1;
3898         }
3899
3900         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
3901         if (ctdb_db == NULL) {
3902                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
3903                 talloc_free(tmp_ctx);
3904                 return -1;
3905         }
3906
3907         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
3908         if (h == NULL) {
3909                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
3910                 talloc_free(tmp_ctx);
3911                 return -1;
3912         }
3913
3914         key.dptr  = discard_const(argv[1]);
3915         key.dsize = strlen(argv[1]);
3916         ret = ctdb_transaction_fetch(h, tmp_ctx, key, &data);
3917         if (ret != 0) {
3918                 DEBUG(DEBUG_ERR,("Failed to fetch record\n"));
3919                 talloc_free(tmp_ctx);
3920                 return -1;
3921         }
3922
3923         if (data.dsize == 0 || data.dptr == NULL) {
3924                 DEBUG(DEBUG_ERR,("Record is empty\n"));
3925                 talloc_free(tmp_ctx);
3926                 return -1;
3927         }
3928
3929         if (argc == 3) {
3930           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3931                 if (fd == -1) {
3932                         DEBUG(DEBUG_ERR,("Failed to open output file %s\n", argv[2]));
3933                         talloc_free(tmp_ctx);
3934                         return -1;
3935                 }
3936                 write(fd, data.dptr, data.dsize);
3937                 close(fd);
3938         } else {
3939                 write(1, data.dptr, data.dsize);
3940         }
3941
3942         /* abort the transaction */
3943         talloc_free(h);
3944
3945
3946         talloc_free(tmp_ctx);
3947         return 0;
3948 }
3949
3950 /*
3951   fetch a record from a tdb-file
3952  */
3953 static int control_tfetch(struct ctdb_context *ctdb, int argc, const char **argv)
3954 {
3955         const char *tdb_file;
3956         TDB_CONTEXT *tdb;
3957         TDB_DATA key, data;
3958         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
3959         int fd;
3960
3961         if (argc < 2) {
3962                 usage();
3963         }
3964
3965         tdb_file = argv[0];
3966
3967         tdb = tdb_open(tdb_file, 0, 0, O_RDONLY, 0);
3968         if (tdb == NULL) {
3969                 printf("Failed to open TDB file %s\n", tdb_file);
3970                 return -1;
3971         }
3972
3973         if (!strncmp(argv[1], "0x", 2)) {
3974                 key = hextodata(tmp_ctx, argv[1] + 2);
3975                 if (key.dsize == 0) {
3976                         printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
3977                         return -1;
3978                 }
3979         } else {
3980                 key.dptr  = discard_const(argv[1]);
3981                 key.dsize = strlen(argv[1]);
3982         }
3983
3984         data = tdb_fetch(tdb, key);
3985         if (data.dptr == NULL || data.dsize < sizeof(struct ctdb_ltdb_header)) {
3986                 printf("Failed to read record %s from tdb %s\n", argv[1], tdb_file);
3987                 tdb_close(tdb);
3988                 return -1;
3989         }
3990
3991         tdb_close(tdb);
3992
3993         if (argc == 3) {
3994           fd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC, 0600);
3995                 if (fd == -1) {
3996                         printf("Failed to open output file %s\n", argv[2]);
3997                         return -1;
3998                 }
3999                 if (options.verbose){
4000                         write(fd, data.dptr, data.dsize);
4001                 } else {
4002                         write(fd, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4003                 }
4004                 close(fd);
4005         } else {
4006                 if (options.verbose){
4007                         write(1, data.dptr, data.dsize);
4008                 } else {
4009                         write(1, data.dptr+sizeof(struct ctdb_ltdb_header), data.dsize-sizeof(struct ctdb_ltdb_header));
4010                 }
4011         }
4012
4013         talloc_free(tmp_ctx);
4014         return 0;
4015 }
4016
4017 /*
4018   store a record and header to a tdb-file
4019  */
4020 static int control_tstore(struct ctdb_context *ctdb, int argc, const char **argv)
4021 {
4022         const char *tdb_file;
4023         TDB_CONTEXT *tdb;
4024         TDB_DATA key, data;
4025         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
4026
4027         if (argc < 3) {
4028                 usage();
4029         }
4030
4031         tdb_file = argv[0];
4032
4033         tdb = tdb_open(tdb_file, 0, 0, O_RDWR, 0);
4034         if (tdb == NULL) {
4035                 printf("Failed to open TDB file %s\n", tdb_file);
4036                 return -1;
4037         }
4038
4039         if (!strncmp(argv[1], "0x", 2)) {
4040                 key = hextodata(tmp_ctx, argv[1] + 2);
4041                 if (key.dsize == 0) {
4042                         printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[1]);
4043                         return -1;
4044                 }
4045         } else {
4046                 key.dptr  = discard_const(argv[1]);
4047                 key.dsize = strlen(argv[1]);
4048         }
4049
4050         if (!strncmp(argv[2], "0x", 2)) {
4051                 data = hextodata(tmp_ctx, argv[2] + 2);
4052                 if (data.dsize == 0) {
4053                         printf("Failed to convert \"%s\" into a TDB_DATA\n", argv[2]);
4054                         return -1;
4055                 }
4056         } else {
4057                 data.dptr  = discard_const(argv[2]);
4058                 data.dsize = strlen(argv[2]);
4059         }
4060
4061         if (data.dsize < sizeof(struct ctdb_ltdb_header)) {
4062                 printf("Not enough data. You must specify the full ctdb_ltdb_header too when storing\n");
4063                 return -1;
4064         }
4065         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0) {
4066                 printf("Failed to write record %s to tdb %s\n", argv[1], tdb_file);
4067                 tdb_close(tdb);
4068                 return -1;
4069         }
4070
4071         tdb_close(tdb);
4072
4073         talloc_free(tmp_ctx);
4074         return 0;
4075 }
4076
4077 /*
4078   write a record to a persistent database
4079  */
4080 static int control_pstore(struct ctdb_context *ctdb, int argc, const char **argv)
4081 {
4082         const char *db_name;
4083         struct ctdb_db_context *ctdb_db;
4084         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4085         struct ctdb_transaction_handle *h;
4086         struct stat st;
4087         TDB_DATA key, data;
4088         int fd, ret;
4089
4090         if (argc < 3) {
4091                 talloc_free(tmp_ctx);
4092                 usage();
4093         }
4094
4095         fd = open(argv[2], O_RDONLY);
4096         if (fd == -1) {
4097                 DEBUG(DEBUG_ERR,("Failed to open file containing record data : %s  %s\n", argv[2], strerror(errno)));
4098                 talloc_free(tmp_ctx);
4099                 return -1;
4100         }
4101         
4102         ret = fstat(fd, &st);
4103         if (ret == -1) {
4104                 DEBUG(DEBUG_ERR,("fstat of file %s failed: %s\n", argv[2], strerror(errno)));
4105                 close(fd);
4106                 talloc_free(tmp_ctx);
4107                 return -1;
4108         }
4109
4110         if (!S_ISREG(st.st_mode)) {
4111                 DEBUG(DEBUG_ERR,("Not a regular file %s\n", argv[2]));
4112                 close(fd);
4113                 talloc_free(tmp_ctx);
4114                 return -1;
4115         }
4116
4117         data.dsize = st.st_size;
4118         if (data.dsize == 0) {
4119                 data.dptr  = NULL;
4120         } else {
4121                 data.dptr = talloc_size(tmp_ctx, data.dsize);
4122                 if (data.dptr == NULL) {
4123                         DEBUG(DEBUG_ERR,("Failed to talloc %d of memory to store record data\n", (int)data.dsize));
4124                         close(fd);
4125                         talloc_free(tmp_ctx);
4126                         return -1;
4127                 }
4128                 ret = read(fd, data.dptr, data.dsize);
4129                 if (ret != data.dsize) {
4130                         DEBUG(DEBUG_ERR,("Failed to read %d bytes of record data\n", (int)data.dsize));
4131                         close(fd);
4132                         talloc_free(tmp_ctx);
4133                         return -1;
4134                 }
4135         }
4136         close(fd);
4137
4138
4139         db_name = argv[0];
4140
4141         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, true, 0);
4142         if (ctdb_db == NULL) {
4143                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4144                 talloc_free(tmp_ctx);
4145                 return -1;
4146         }
4147
4148         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4149         if (h == NULL) {
4150                 DEBUG(DEBUG_ERR,("Failed to start transaction on database %s\n", db_name));
4151                 talloc_free(tmp_ctx);
4152                 return -1;
4153         }
4154
4155         key.dptr  = discard_const(argv[1]);
4156         key.dsize = strlen(argv[1]);
4157         ret = ctdb_transaction_store(h, key, data);
4158         if (ret != 0) {
4159                 DEBUG(DEBUG_ERR,("Failed to store record\n"));
4160                 talloc_free(tmp_ctx);
4161                 return -1;
4162         }
4163
4164         ret = ctdb_transaction_commit(h);
4165         if (ret != 0) {
4166                 DEBUG(DEBUG_ERR,("Failed to commit transaction\n"));
4167                 talloc_free(tmp_ctx);
4168                 return -1;
4169         }
4170
4171
4172         talloc_free(tmp_ctx);
4173         return 0;
4174 }
4175
4176 /*
4177  * delete a record from a persistent database
4178  */
4179 static int control_pdelete(struct ctdb_context *ctdb, int argc, const char **argv)
4180 {
4181         const char *db_name;
4182         struct ctdb_db_context *ctdb_db;
4183         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4184         struct ctdb_transaction_handle *h;
4185         TDB_DATA key;
4186         int ret;
4187         bool persistent;
4188         uint8_t flags;
4189
4190         if (argc < 2) {
4191                 talloc_free(tmp_ctx);
4192                 usage();
4193         }
4194
4195         db_name = argv[0];
4196
4197         if (!db_exists(ctdb, db_name, NULL, &flags)) {
4198                 talloc_free(tmp_ctx);
4199                 return -1;
4200         }
4201
4202         persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
4203         if (!persistent) {
4204                 DEBUG(DEBUG_ERR, ("Database '%s' is not persistent\n", db_name));
4205                 talloc_free(tmp_ctx);
4206                 return -1;
4207         }
4208
4209         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4210         if (ctdb_db == NULL) {
4211                 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n", db_name));
4212                 talloc_free(tmp_ctx);
4213                 return -1;
4214         }
4215
4216         h = ctdb_transaction_start(ctdb_db, tmp_ctx);
4217         if (h == NULL) {
4218                 DEBUG(DEBUG_ERR, ("Failed to start transaction on database %s\n", db_name));
4219                 talloc_free(tmp_ctx);
4220                 return -1;
4221         }
4222
4223         key.dptr = discard_const(argv[1]);
4224         key.dsize = strlen(argv[1]);
4225         ret = ctdb_transaction_store(h, key, tdb_null);
4226         if (ret != 0) {
4227                 DEBUG(DEBUG_ERR, ("Failed to delete record\n"));
4228                 talloc_free(tmp_ctx);
4229                 return -1;
4230         }
4231
4232         ret = ctdb_transaction_commit(h);
4233         if (ret != 0) {
4234                 DEBUG(DEBUG_ERR, ("Failed to commit transaction\n"));
4235                 talloc_free(tmp_ctx);
4236                 return -1;
4237         }
4238
4239         talloc_free(tmp_ctx);
4240         return 0;
4241 }
4242
4243 /*
4244   check if a service is bound to a port or not
4245  */
4246 static int control_chktcpport(struct ctdb_context *ctdb, int argc, const char **argv)
4247 {
4248         int s, ret;
4249         unsigned v;
4250         int port;
4251         struct sockaddr_in sin;
4252
4253         if (argc != 1) {
4254                 printf("Use: ctdb chktcport <port>\n");
4255                 return EINVAL;
4256         }
4257
4258         port = atoi(argv[0]);
4259
4260         s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
4261         if (s == -1) {
4262                 printf("Failed to open local socket\n");
4263                 return errno;
4264         }
4265
4266         v = fcntl(s, F_GETFL, 0);
4267         fcntl(s, F_SETFL, v | O_NONBLOCK);
4268
4269         bzero(&sin, sizeof(sin));
4270         sin.sin_family = PF_INET;
4271         sin.sin_port   = htons(port);
4272         ret = bind(s, (struct sockaddr *)&sin, sizeof(sin));
4273         close(s);
4274         if (ret == -1) {
4275                 printf("Failed to bind to local socket: %d %s\n", errno, strerror(errno));
4276                 return errno;
4277         }
4278
4279         return 0;
4280 }
4281
4282
4283
4284 static void log_handler(struct ctdb_context *ctdb, uint64_t srvid, 
4285                              TDB_DATA data, void *private_data)
4286 {
4287         DEBUG(DEBUG_ERR,("Log data received\n"));
4288         if (data.dsize > 0) {
4289                 printf("%s", data.dptr);
4290         }
4291
4292         exit(0);
4293 }
4294
4295 /*
4296   display a list of log messages from the in memory ringbuffer
4297  */
4298 static int control_getlog(struct ctdb_context *ctdb, int argc, const char **argv)
4299 {
4300         int ret, i;
4301         bool main_daemon;
4302         struct ctdb_get_log_addr log_addr;
4303         TDB_DATA data;
4304         struct timeval tv;
4305
4306         /* Process options */
4307         main_daemon = true;
4308         log_addr.pnn = ctdb_get_pnn(ctdb);
4309         log_addr.level = DEBUG_NOTICE;
4310         for (i = 0; i < argc; i++) {
4311                 if (strcmp(argv[i], "recoverd") == 0) {
4312                         main_daemon = false;
4313                 } else {
4314                         if (isalpha(argv[i][0]) || argv[i][0] == '-') { 
4315                                 log_addr.level = get_debug_by_desc(argv[i]);
4316                         } else {
4317                                 log_addr.level = strtol(argv[i], NULL, 0);
4318                         }
4319                 }
4320         }
4321
4322         /* Our message port is our PID */
4323         log_addr.srvid = getpid();
4324
4325         data.dptr = (unsigned char *)&log_addr;
4326         data.dsize = sizeof(log_addr);
4327
4328         DEBUG(DEBUG_ERR, ("Pulling logs from node %u\n", options.pnn));
4329
4330         ctdb_client_set_message_handler(ctdb, log_addr.srvid, log_handler, NULL);
4331         sleep(1);
4332
4333         DEBUG(DEBUG_ERR,("Listen for response on %d\n", (int)log_addr.srvid));
4334
4335         if (main_daemon) {
4336                 int32_t res;
4337                 char *errmsg;
4338                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4339
4340                 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_GET_LOG,
4341                                    0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
4342                 if (ret != 0 || res != 0) {
4343                         DEBUG(DEBUG_ERR,("Failed to get logs - %s\n", errmsg));
4344                         talloc_free(tmp_ctx);
4345                         return -1;
4346                 }
4347                 talloc_free(tmp_ctx);
4348         } else {
4349                 ret = ctdb_client_send_message(ctdb, options.pnn,
4350                                                CTDB_SRVID_GETLOG, data);
4351                 if (ret != 0) {
4352                         DEBUG(DEBUG_ERR,("Failed to send getlog request message to %u\n", options.pnn));
4353                         return -1;
4354                 }
4355         }
4356
4357         tv = timeval_current();
4358         /* this loop will terminate when we have received the reply */
4359         while (timeval_elapsed(&tv) < (double)options.timelimit) {
4360                 event_loop_once(ctdb->ev);
4361         }
4362
4363         DEBUG(DEBUG_INFO,("Timed out waiting for log data.\n"));
4364
4365         return 0;
4366 }
4367
4368 /*
4369   clear the in memory log area
4370  */
4371 static int control_clearlog(struct ctdb_context *ctdb, int argc, const char **argv)
4372 {
4373         int ret;
4374
4375         if (argc == 0 || (argc >= 1 && strcmp(argv[0], "recoverd") != 0)) {
4376                 /* "recoverd" not given - get logs from main daemon */
4377                 int32_t res;
4378                 char *errmsg;
4379                 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4380
4381                 ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_CLEAR_LOG,
4382                                    0, tdb_null, tmp_ctx, NULL, &res, NULL, &errmsg);
4383                 if (ret != 0 || res != 0) {
4384                         DEBUG(DEBUG_ERR,("Failed to clear logs\n"));
4385                         talloc_free(tmp_ctx);
4386                         return -1;
4387                 }
4388
4389                 talloc_free(tmp_ctx);
4390         } else {
4391                 TDB_DATA data; /* unused in recoverd... */
4392                 data.dsize = 0;
4393
4394                 ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_CLEARLOG, data);
4395                 if (ret != 0) {
4396                         DEBUG(DEBUG_ERR,("Failed to send clearlog request message to %u\n", options.pnn));
4397                         return -1;
4398                 }
4399         }
4400
4401         return 0;
4402 }
4403
4404 /* Reload public IPs on a specified nodes */
4405 static int control_reloadips(struct ctdb_context *ctdb, int argc, const char **argv)
4406 {
4407         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
4408         uint32_t *nodes;
4409         uint32_t pnn_mode;
4410         int ret;
4411
4412         assert_single_node_only();
4413
4414         if (argc > 1) {
4415                 usage();
4416         }
4417
4418         /* Determine the nodes where IPs need to be reloaded */
4419         if (!parse_nodestring(ctdb, tmp_ctx, argc == 1 ? argv[0] : NULL,
4420                               options.pnn, true, &nodes, &pnn_mode)) {
4421                 ret = -1;
4422                 goto done;
4423         }
4424
4425 again:
4426         /* Disable takeover runs on all connected nodes.  A reply
4427          * indicating success is needed from each node so all nodes
4428          * will need to be active.  This will retry until maxruntime
4429          * is exceeded, hence no error handling.
4430          * 
4431          * A check could be added to not allow reloading of IPs when
4432          * there are disconnected nodes.  However, this should
4433          * probably be left up to the administrator.
4434          */
4435         srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, LONGTIMEOUT,
4436                         "Disable takeover runs", true);
4437
4438         /* Now tell all the desired nodes to reload their public IPs.
4439          * Keep trying this until it succeeds.  This assumes all
4440          * failures are transient, which might not be true...
4441          */
4442         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_RELOAD_PUBLIC_IPS,
4443                                       nodes, 0, LONGTIMELIMIT(),
4444                                       false, tdb_null,
4445                                       NULL, NULL, NULL) != 0) {
4446                 DEBUG(DEBUG_ERR,
4447                       ("Unable to reload IPs on some nodes, try again.\n"));
4448                 goto again;
4449         }
4450
4451         /* It isn't strictly necessary to wait until takeover runs are
4452          * re-enabled but doing so can't hurt.
4453          */
4454         srvid_broadcast(ctdb, CTDB_SRVID_DISABLE_TAKEOVER_RUNS, 0,
4455                         "Enable takeover runs", true);
4456
4457         ipreallocate(ctdb);
4458
4459         ret = 0;
4460 done:
4461         talloc_free(tmp_ctx);
4462         return ret;
4463 }
4464
4465 /*
4466   display a list of the databases on a remote ctdb
4467  */
4468 static int control_getdbmap(struct ctdb_context *ctdb, int argc, const char **argv)
4469 {
4470         int i, ret;
4471         struct ctdb_dbid_map *dbmap=NULL;
4472
4473         ret = ctdb_ctrl_getdbmap(ctdb, TIMELIMIT(), options.pnn, ctdb, &dbmap);
4474         if (ret != 0) {
4475                 DEBUG(DEBUG_ERR, ("Unable to get dbids from node %u\n", options.pnn));
4476                 return ret;
4477         }
4478
4479         if(options.machinereadable){
4480                 printf(":ID:Name:Path:Persistent:Sticky:Unhealthy:ReadOnly:\n");
4481                 for(i=0;i<dbmap->num;i++){
4482                         const char *path;
4483                         const char *name;
4484                         const char *health;
4485                         bool persistent;
4486                         bool readonly;
4487                         bool sticky;
4488
4489                         ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn,
4490                                             dbmap->dbs[i].dbid, ctdb, &path);
4491                         ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn,
4492                                             dbmap->dbs[i].dbid, ctdb, &name);
4493                         ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
4494                                               dbmap->dbs[i].dbid, ctdb, &health);
4495                         persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4496                         readonly   = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4497                         sticky     = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4498                         printf(":0x%08X:%s:%s:%d:%d:%d:%d:\n",
4499                                dbmap->dbs[i].dbid, name, path,
4500                                !!(persistent), !!(sticky),
4501                                !!(health), !!(readonly));
4502                 }
4503                 return 0;
4504         }
4505
4506         printf("Number of databases:%d\n", dbmap->num);
4507         for(i=0;i<dbmap->num;i++){
4508                 const char *path;
4509                 const char *name;
4510                 const char *health;
4511                 bool persistent;
4512                 bool readonly;
4513                 bool sticky;
4514
4515                 ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &path);
4516                 ctdb_ctrl_getdbname(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &name);
4517                 ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, dbmap->dbs[i].dbid, ctdb, &health);
4518                 persistent = dbmap->dbs[i].flags & CTDB_DB_FLAGS_PERSISTENT;
4519                 readonly   = dbmap->dbs[i].flags & CTDB_DB_FLAGS_READONLY;
4520                 sticky     = dbmap->dbs[i].flags & CTDB_DB_FLAGS_STICKY;
4521                 printf("dbid:0x%08x name:%s path:%s%s%s%s%s\n",
4522                        dbmap->dbs[i].dbid, name, path,
4523                        persistent?" PERSISTENT":"",
4524                        sticky?" STICKY":"",
4525                        readonly?" READONLY":"",
4526                        health?" UNHEALTHY":"");
4527         }
4528
4529         return 0;
4530 }
4531
4532 /*
4533   display the status of a database on a remote ctdb
4534  */
4535 static int control_getdbstatus(struct ctdb_context *ctdb, int argc, const char **argv)
4536 {
4537         const char *db_name;
4538         uint32_t db_id;
4539         uint8_t flags;
4540         const char *path;
4541         const char *health;
4542
4543         if (argc < 1) {
4544                 usage();
4545         }
4546
4547         db_name = argv[0];
4548
4549         if (!db_exists(ctdb, db_name, &db_id, &flags)) {
4550                 return -1;
4551         }
4552
4553         ctdb_ctrl_getdbpath(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &path);
4554         ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn, db_id, ctdb, &health);
4555         printf("dbid: 0x%08x\nname: %s\npath: %s\nPERSISTENT: %s\nSTICKY: %s\nREADONLY: %s\nHEALTH: %s\n",
4556                db_id, db_name, path,
4557                (flags & CTDB_DB_FLAGS_PERSISTENT ? "yes" : "no"),
4558                (flags & CTDB_DB_FLAGS_STICKY ? "yes" : "no"),
4559                (flags & CTDB_DB_FLAGS_READONLY ? "yes" : "no"),
4560                (health ? health : "OK"));
4561
4562         return 0;
4563 }
4564
4565 /*
4566   check if the local node is recmaster or not
4567   it will return 1 if this node is the recmaster and 0 if it is not
4568   or if the local ctdb daemon could not be contacted
4569  */
4570 static int control_isnotrecmaster(struct ctdb_context *ctdb, int argc, const char **argv)
4571 {
4572         uint32_t mypnn, recmaster;
4573         int ret;
4574
4575         assert_single_node_only();
4576
4577         mypnn = getpnn(ctdb);
4578
4579         ret = ctdb_ctrl_getrecmaster(ctdb, ctdb, TIMELIMIT(), options.pnn, &recmaster);
4580         if (ret != 0) {
4581                 printf("Failed to get the recmaster\n");
4582                 return 1;
4583         }
4584
4585         if (recmaster != mypnn) {
4586                 printf("this node is not the recmaster\n");
4587                 return 1;
4588         }
4589
4590         printf("this node is the recmaster\n");
4591         return 0;
4592 }
4593
4594 /*
4595   ping a node
4596  */
4597 static int control_ping(struct ctdb_context *ctdb, int argc, const char **argv)
4598 {
4599         int ret;
4600         struct timeval tv = timeval_current();
4601         ret = ctdb_ctrl_ping(ctdb, options.pnn);
4602         if (ret == -1) {
4603                 printf("Unable to get ping response from node %u\n", options.pnn);
4604                 return -1;
4605         } else {
4606                 printf("response from %u time=%.6f sec  (%d clients)\n", 
4607                        options.pnn, timeval_elapsed(&tv), ret);
4608         }
4609         return 0;
4610 }
4611
4612
4613 /*
4614   get a node's runstate
4615  */
4616 static int control_runstate(struct ctdb_context *ctdb, int argc, const char **argv)
4617 {
4618         int ret;
4619         enum ctdb_runstate runstate;
4620
4621         ret = ctdb_ctrl_get_runstate(ctdb, TIMELIMIT(), options.pnn, &runstate);
4622         if (ret == -1) {
4623                 printf("Unable to get runstate response from node %u\n",
4624                        options.pnn);
4625                 return -1;
4626         } else {
4627                 bool found = true;
4628                 enum ctdb_runstate t;
4629                 int i;
4630                 for (i=0; i<argc; i++) {
4631                         found = false;
4632                         t = runstate_from_string(argv[i]);
4633                         if (t == CTDB_RUNSTATE_UNKNOWN) {
4634                                 printf("Invalid run state (%s)\n", argv[i]);
4635                                 return -1;
4636                         }
4637
4638                         if (t == runstate) {
4639                                 found = true;
4640                                 break;
4641                         }
4642                 }
4643
4644                 if (!found) {
4645                         printf("CTDB not in required run state (got %s)\n", 
4646                                runstate_to_string((enum ctdb_runstate)runstate));
4647                         return -1;
4648                 }
4649         }
4650
4651         printf("%s\n", runstate_to_string(runstate));
4652         return 0;
4653 }
4654
4655
4656 /*
4657   get a tunable
4658  */
4659 static int control_getvar(struct ctdb_context *ctdb, int argc, const char **argv)
4660 {
4661         const char *name;
4662         uint32_t value;
4663         int ret;
4664
4665         if (argc < 1) {
4666                 usage();
4667         }
4668
4669         name = argv[0];
4670         ret = ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn, name, &value);
4671         if (ret != 0) {
4672                 DEBUG(DEBUG_ERR, ("Unable to get tunable variable '%s'\n", name));
4673                 return -1;
4674         }
4675
4676         printf("%-23s = %u\n", name, value);
4677         return 0;
4678 }
4679
4680 /*
4681   set a tunable
4682  */
4683 static int control_setvar(struct ctdb_context *ctdb, int argc, const char **argv)
4684 {
4685         const char *name;
4686         uint32_t value;
4687         int ret;
4688
4689         if (argc < 2) {
4690                 usage();
4691         }
4692
4693         name = argv[0];
4694         value = strtoul(argv[1], NULL, 0);
4695
4696         ret = ctdb_ctrl_set_tunable(ctdb, TIMELIMIT(), options.pnn, name, value);
4697         if (ret == -1) {
4698                 DEBUG(DEBUG_ERR, ("Unable to set tunable variable '%s'\n", name));
4699                 return -1;
4700         }
4701         return 0;
4702 }
4703
4704 /*
4705   list all tunables
4706  */
4707 static int control_listvars(struct ctdb_context *ctdb, int argc, const char **argv)
4708 {
4709         uint32_t count;
4710         const char **list;
4711         int ret, i;
4712
4713         ret = ctdb_ctrl_list_tunables(ctdb, TIMELIMIT(), options.pnn, ctdb, &list, &count);
4714         if (ret == -1) {
4715                 DEBUG(DEBUG_ERR, ("Unable to list tunable variables\n"));
4716                 return -1;
4717         }
4718
4719         for (i=0;i<count;i++) {
4720                 control_getvar(ctdb, 1, &list[i]);
4721         }
4722
4723         talloc_free(list);
4724         
4725         return 0;
4726 }
4727
4728 /*
4729   display debug level on a node
4730  */
4731 static int control_getdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4732 {
4733         int ret;
4734         int32_t level;
4735
4736         ret = ctdb_ctrl_get_debuglevel(ctdb, options.pnn, &level);
4737         if (ret != 0) {
4738                 DEBUG(DEBUG_ERR, ("Unable to get debuglevel response from node %u\n", options.pnn));
4739                 return ret;
4740         } else {
4741                 if (options.machinereadable){
4742                         printf(":Name:Level:\n");
4743                         printf(":%s:%d:\n",get_debug_by_level(level),level);
4744                 } else {
4745                         printf("Node %u is at debug level %s (%d)\n", options.pnn, get_debug_by_level(level), level);
4746                 }
4747         }
4748         return 0;
4749 }
4750
4751 /*
4752   display reclock file of a node
4753  */
4754 static int control_getreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4755 {
4756         int ret;
4757         const char *reclock;
4758
4759         ret = ctdb_ctrl_getreclock(ctdb, TIMELIMIT(), options.pnn, ctdb, &reclock);
4760         if (ret != 0) {
4761                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4762                 return ret;
4763         } else {
4764                 if (options.machinereadable){
4765                         if (reclock != NULL) {
4766                                 printf("%s", reclock);
4767                         }
4768                 } else {
4769                         if (reclock == NULL) {
4770                                 printf("No reclock file used.\n");
4771                         } else {
4772                                 printf("Reclock file:%s\n", reclock);
4773                         }
4774                 }
4775         }
4776         return 0;
4777 }
4778
4779 /*
4780   set the reclock file of a node
4781  */
4782 static int control_setreclock(struct ctdb_context *ctdb, int argc, const char **argv)
4783 {
4784         int ret;
4785         const char *reclock;
4786
4787         if (argc == 0) {
4788                 reclock = NULL;
4789         } else if (argc == 1) {
4790                 reclock = argv[0];
4791         } else {
4792                 usage();
4793         }
4794
4795         ret = ctdb_ctrl_setreclock(ctdb, TIMELIMIT(), options.pnn, reclock);
4796         if (ret != 0) {
4797                 DEBUG(DEBUG_ERR, ("Unable to get reclock file from node %u\n", options.pnn));
4798                 return ret;
4799         }
4800         return 0;
4801 }
4802
4803 /*
4804   set the natgw state on/off
4805  */
4806 static int control_setnatgwstate(struct ctdb_context *ctdb, int argc, const char **argv)
4807 {
4808         int ret;
4809         uint32_t natgwstate;
4810
4811         if (argc == 0) {
4812                 usage();
4813         }
4814
4815         if (!strcmp(argv[0], "on")) {
4816                 natgwstate = 1;
4817         } else if (!strcmp(argv[0], "off")) {
4818                 natgwstate = 0;
4819         } else {
4820                 usage();
4821         }
4822
4823         ret = ctdb_ctrl_setnatgwstate(ctdb, TIMELIMIT(), options.pnn, natgwstate);
4824         if (ret != 0) {
4825                 DEBUG(DEBUG_ERR, ("Unable to set the natgw state for node %u\n", options.pnn));
4826                 return ret;
4827         }
4828
4829         return 0;
4830 }
4831
4832 /*
4833   set the lmaster role on/off
4834  */
4835 static int control_setlmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4836 {
4837         int ret;
4838         uint32_t lmasterrole;
4839
4840         if (argc == 0) {
4841                 usage();
4842         }
4843
4844         if (!strcmp(argv[0], "on")) {
4845                 lmasterrole = 1;
4846         } else if (!strcmp(argv[0], "off")) {
4847                 lmasterrole = 0;
4848         } else {
4849                 usage();
4850         }
4851
4852         ret = ctdb_ctrl_setlmasterrole(ctdb, TIMELIMIT(), options.pnn, lmasterrole);
4853         if (ret != 0) {
4854                 DEBUG(DEBUG_ERR, ("Unable to set the lmaster role for node %u\n", options.pnn));
4855                 return ret;
4856         }
4857
4858         return 0;
4859 }
4860
4861 /*
4862   set the recmaster role on/off
4863  */
4864 static int control_setrecmasterrole(struct ctdb_context *ctdb, int argc, const char **argv)
4865 {
4866         int ret;
4867         uint32_t recmasterrole;
4868
4869         if (argc == 0) {
4870                 usage();
4871         }
4872
4873         if (!strcmp(argv[0], "on")) {
4874                 recmasterrole = 1;
4875         } else if (!strcmp(argv[0], "off")) {
4876                 recmasterrole = 0;
4877         } else {
4878                 usage();
4879         }
4880
4881         ret = ctdb_ctrl_setrecmasterrole(ctdb, TIMELIMIT(), options.pnn, recmasterrole);
4882         if (ret != 0) {
4883                 DEBUG(DEBUG_ERR, ("Unable to set the recmaster role for node %u\n", options.pnn));
4884                 return ret;
4885         }
4886
4887         return 0;
4888 }
4889
4890 /*
4891   set debug level on a node or all nodes
4892  */
4893 static int control_setdebug(struct ctdb_context *ctdb, int argc, const char **argv)
4894 {
4895         int i, ret;
4896         int32_t level;
4897
4898         if (argc == 0) {
4899                 printf("You must specify the debug level. Valid levels are:\n");
4900                 for (i=0; debug_levels[i].description != NULL; i++) {
4901                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4902                 }
4903
4904                 return 0;
4905         }
4906
4907         if (isalpha(argv[0][0]) || argv[0][0] == '-') { 
4908                 level = get_debug_by_desc(argv[0]);
4909         } else {
4910                 level = strtol(argv[0], NULL, 0);
4911         }
4912
4913         for (i=0; debug_levels[i].description != NULL; i++) {
4914                 if (level == debug_levels[i].level) {
4915                         break;
4916                 }
4917         }
4918         if (debug_levels[i].description == NULL) {
4919                 printf("Invalid debug level, must be one of\n");
4920                 for (i=0; debug_levels[i].description != NULL; i++) {
4921                         printf("%s (%d)\n", debug_levels[i].description, debug_levels[i].level);
4922                 }
4923                 return -1;
4924         }
4925
4926         ret = ctdb_ctrl_set_debuglevel(ctdb, options.pnn, level);
4927         if (ret != 0) {
4928                 DEBUG(DEBUG_ERR, ("Unable to set debug level on node %u\n", options.pnn));
4929         }
4930         return 0;
4931 }
4932
4933
4934 /*
4935   thaw a node
4936  */
4937 static int control_thaw(struct ctdb_context *ctdb, int argc, const char **argv)
4938 {
4939         int ret;
4940         uint32_t priority;
4941         
4942         if (argc == 1) {
4943                 priority = strtol(argv[0], NULL, 0);
4944         } else {
4945                 priority = 0;
4946         }
4947         DEBUG(DEBUG_ERR,("Thaw by priority %u\n", priority));
4948
4949         ret = ctdb_ctrl_thaw_priority(ctdb, TIMELIMIT(), options.pnn, priority);
4950         if (ret != 0) {
4951                 DEBUG(DEBUG_ERR, ("Unable to thaw node %u\n", options.pnn));
4952         }               
4953         return 0;
4954 }
4955
4956
4957 /*
4958   attach to a database
4959  */
4960 static int control_attach(struct ctdb_context *ctdb, int argc, const char **argv)
4961 {
4962         const char *db_name;
4963         struct ctdb_db_context *ctdb_db;
4964         bool persistent = false;
4965
4966         if (argc < 1) {
4967                 usage();
4968         }
4969         db_name = argv[0];
4970         if (argc > 2) {
4971                 usage();
4972         }
4973         if (argc == 2) {
4974                 if (strcmp(argv[1], "persistent") != 0) {
4975                         usage();
4976                 }
4977                 persistent = true;
4978         }
4979
4980         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), db_name, persistent, 0);
4981         if (ctdb_db == NULL) {
4982                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", db_name));
4983                 return -1;
4984         }
4985
4986         return 0;
4987 }
4988
4989 /*
4990   set db priority
4991  */
4992 static int control_setdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
4993 {
4994         struct ctdb_db_priority db_prio;
4995         int ret;
4996
4997         if (argc < 2) {
4998                 usage();
4999         }
5000
5001         db_prio.db_id    = strtoul(argv[0], NULL, 0);
5002         db_prio.priority = strtoul(argv[1], NULL, 0);
5003
5004         ret = ctdb_ctrl_set_db_priority(ctdb, TIMELIMIT(), options.pnn, &db_prio);
5005         if (ret != 0) {
5006                 DEBUG(DEBUG_ERR,("Unable to set db prio\n"));
5007                 return -1;
5008         }
5009
5010         return 0;
5011 }
5012
5013 /*
5014   get db priority
5015  */
5016 static int control_getdbprio(struct ctdb_context *ctdb, int argc, const char **argv)
5017 {
5018         uint32_t db_id, priority;
5019         int ret;
5020
5021         if (argc < 1) {
5022                 usage();
5023         }
5024
5025         if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5026                 return -1;
5027         }
5028
5029         ret = ctdb_ctrl_get_db_priority(ctdb, TIMELIMIT(), options.pnn, db_id, &priority);
5030         if (ret != 0) {
5031                 DEBUG(DEBUG_ERR,("Unable to get db prio\n"));
5032                 return -1;
5033         }
5034
5035         DEBUG(DEBUG_ERR,("Priority:%u\n", priority));
5036
5037         return 0;
5038 }
5039
5040 /*
5041   set the sticky records capability for a database
5042  */
5043 static int control_setdbsticky(struct ctdb_context *ctdb, int argc, const char **argv)
5044 {
5045         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5046         uint32_t db_id;
5047         int ret;
5048
5049         if (argc < 1) {
5050                 usage();
5051         }
5052
5053         if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5054                 return -1;
5055         }
5056
5057         ret = ctdb_ctrl_set_db_sticky(ctdb, options.pnn, db_id);
5058         if (ret != 0) {
5059                 DEBUG(DEBUG_ERR,("Unable to set db to support sticky records\n"));
5060                 talloc_free(tmp_ctx);
5061                 return -1;
5062         }
5063
5064         talloc_free(tmp_ctx);
5065         return 0;
5066 }
5067
5068 /*
5069   set the readonly capability for a database
5070  */
5071 static int control_setdbreadonly(struct ctdb_context *ctdb, int argc, const char **argv)
5072 {
5073         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5074         uint32_t db_id;
5075         int ret;
5076
5077         if (argc < 1) {
5078                 usage();
5079         }
5080
5081         if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5082                 return -1;
5083         }
5084
5085         ret = ctdb_ctrl_set_db_readonly(ctdb, options.pnn, db_id);
5086         if (ret != 0) {
5087                 DEBUG(DEBUG_ERR,("Unable to set db to support readonly\n"));
5088                 talloc_free(tmp_ctx);
5089                 return -1;
5090         }
5091
5092         talloc_free(tmp_ctx);
5093         return 0;
5094 }
5095
5096 /*
5097   get db seqnum
5098  */
5099 static int control_getdbseqnum(struct ctdb_context *ctdb, int argc, const char **argv)
5100 {
5101         uint32_t db_id;
5102         uint64_t seqnum;
5103         int ret;
5104
5105         if (argc < 1) {
5106                 usage();
5107         }
5108
5109         if (!db_exists(ctdb, argv[0], &db_id, NULL)) {
5110                 return -1;
5111         }
5112
5113         ret = ctdb_ctrl_getdbseqnum(ctdb, TIMELIMIT(), options.pnn, db_id, &seqnum);
5114         if (ret != 0) {
5115                 DEBUG(DEBUG_ERR, ("Unable to get seqnum from node."));
5116                 return -1;
5117         }
5118
5119         printf("Sequence number:%lld\n", (long long)seqnum);
5120
5121         return 0;
5122 }
5123
5124 /*
5125   run an eventscript on a node
5126  */
5127 static int control_eventscript(struct ctdb_context *ctdb, int argc, const char **argv)
5128 {
5129         TDB_DATA data;
5130         int ret;
5131         int32_t res;
5132         char *errmsg;
5133         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5134
5135         if (argc != 1) {
5136                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5137                 return -1;
5138         }
5139
5140         data.dptr = (unsigned char *)discard_const(argv[0]);
5141         data.dsize = strlen((char *)data.dptr) + 1;
5142
5143         DEBUG(DEBUG_ERR, ("Running eventscripts with arguments \"%s\" on node %u\n", data.dptr, options.pnn));
5144
5145         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_RUN_EVENTSCRIPTS,
5146                            0, data, tmp_ctx, NULL, &res, NULL, &errmsg);
5147         if (ret != 0 || res != 0) {
5148                 DEBUG(DEBUG_ERR,("Failed to run eventscripts - %s\n", errmsg));
5149                 talloc_free(tmp_ctx);
5150                 return -1;
5151         }
5152         talloc_free(tmp_ctx);
5153         return 0;
5154 }
5155
5156 #define DB_VERSION 1
5157 #define MAX_DB_NAME 64
5158 struct db_file_header {
5159         unsigned long version;
5160         time_t timestamp;
5161         unsigned long persistent;
5162         unsigned long size;
5163         const char name[MAX_DB_NAME];
5164 };
5165
5166 struct backup_data {
5167         struct ctdb_marshall_buffer *records;
5168         uint32_t len;
5169         uint32_t total;
5170         bool traverse_error;
5171 };
5172
5173 static int backup_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
5174 {
5175         struct backup_data *bd = talloc_get_type(private, struct backup_data);
5176         struct ctdb_rec_data *rec;
5177
5178         /* add the record */
5179         rec = ctdb_marshall_record(bd->records, 0, key, NULL, data);
5180         if (rec == NULL) {
5181                 bd->traverse_error = true;
5182                 DEBUG(DEBUG_ERR,("Failed to marshall record\n"));
5183                 return -1;
5184         }
5185         bd->records = talloc_realloc_size(NULL, bd->records, rec->length + bd->len);
5186         if (bd->records == NULL) {
5187                 DEBUG(DEBUG_ERR,("Failed to expand marshalling buffer\n"));
5188                 bd->traverse_error = true;
5189                 return -1;
5190         }
5191         bd->records->count++;
5192         memcpy(bd->len+(uint8_t *)bd->records, rec, rec->length);
5193         bd->len += rec->length;
5194         talloc_free(rec);
5195
5196         bd->total++;
5197         return 0;
5198 }
5199
5200 /*
5201  * backup a database to a file 
5202  */
5203 static int control_backupdb(struct ctdb_context *ctdb, int argc, const char **argv)
5204 {
5205         int ret;
5206         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5207         struct db_file_header dbhdr;
5208         struct ctdb_db_context *ctdb_db;
5209         struct backup_data *bd;
5210         int fh = -1;
5211         int status = -1;
5212         const char *reason = NULL;
5213         uint32_t db_id;
5214         uint8_t flags;
5215
5216         assert_single_node_only();
5217
5218         if (argc != 2) {
5219                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5220                 return -1;
5221         }
5222
5223         if (!db_exists(ctdb, argv[0], &db_id, &flags)) {
5224                 return -1;
5225         }
5226
5227         ret = ctdb_ctrl_getdbhealth(ctdb, TIMELIMIT(), options.pnn,
5228                                     db_id, tmp_ctx, &reason);
5229         if (ret != 0) {
5230                 DEBUG(DEBUG_ERR,("Unable to get dbhealth for database '%s'\n",
5231                                  argv[0]));
5232                 talloc_free(tmp_ctx);
5233                 return -1;
5234         }
5235         if (reason) {
5236                 uint32_t allow_unhealthy = 0;
5237
5238                 ctdb_ctrl_get_tunable(ctdb, TIMELIMIT(), options.pnn,
5239                                       "AllowUnhealthyDBRead",
5240                                       &allow_unhealthy);
5241
5242                 if (allow_unhealthy != 1) {
5243                         DEBUG(DEBUG_ERR,("database '%s' is unhealthy: %s\n",
5244                                          argv[0], reason));
5245
5246                         DEBUG(DEBUG_ERR,("disallow backup : tunable AllowUnhealthyDBRead = %u\n",
5247                                          allow_unhealthy));
5248                         talloc_free(tmp_ctx);
5249                         return -1;
5250                 }
5251
5252                 DEBUG(DEBUG_WARNING,("WARNING database '%s' is unhealthy - see 'ctdb getdbstatus %s'\n",
5253                                      argv[0], argv[0]));
5254                 DEBUG(DEBUG_WARNING,("WARNING! allow backup of unhealthy database: "
5255                                      "tunnable AllowUnhealthyDBRead = %u\n",
5256                                      allow_unhealthy));
5257         }
5258
5259         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5260         if (ctdb_db == NULL) {
5261                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", argv[0]));
5262                 talloc_free(tmp_ctx);
5263                 return -1;
5264         }
5265
5266
5267         ret = tdb_transaction_start(ctdb_db->ltdb->tdb);
5268         if (ret == -1) {
5269                 DEBUG(DEBUG_ERR,("Failed to start transaction\n"));
5270                 talloc_free(tmp_ctx);
5271                 return -1;
5272         }
5273
5274
5275         bd = talloc_zero(tmp_ctx, struct backup_data);
5276         if (bd == NULL) {
5277                 DEBUG(DEBUG_ERR,("Failed to allocate backup_data\n"));
5278                 talloc_free(tmp_ctx);
5279                 return -1;
5280         }
5281
5282         bd->records = talloc_zero(bd, struct ctdb_marshall_buffer);
5283         if (bd->records == NULL) {
5284                 DEBUG(DEBUG_ERR,("Failed to allocate ctdb_marshall_buffer\n"));
5285                 talloc_free(tmp_ctx);
5286                 return -1;
5287         }
5288
5289         bd->len = offsetof(struct ctdb_marshall_buffer, data);
5290         bd->records->db_id = ctdb_db->db_id;
5291         /* traverse the database collecting all records */
5292         if (tdb_traverse_read(ctdb_db->ltdb->tdb, backup_traverse, bd) == -1 ||
5293             bd->traverse_error) {
5294                 DEBUG(DEBUG_ERR,("Traverse error\n"));
5295                 talloc_free(tmp_ctx);
5296                 return -1;              
5297         }
5298
5299         tdb_transaction_cancel(ctdb_db->ltdb->tdb);
5300
5301
5302         fh = open(argv[1], O_RDWR|O_CREAT, 0600);
5303         if (fh == -1) {
5304                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[1]));
5305                 talloc_free(tmp_ctx);
5306                 return -1;
5307         }
5308
5309         dbhdr.version = DB_VERSION;
5310         dbhdr.timestamp = time(NULL);
5311         dbhdr.persistent = flags & CTDB_DB_FLAGS_PERSISTENT;
5312         dbhdr.size = bd->len;
5313         if (strlen(argv[0]) >= MAX_DB_NAME) {
5314                 DEBUG(DEBUG_ERR,("Too long dbname\n"));
5315                 goto done;
5316         }
5317         strncpy(discard_const(dbhdr.name), argv[0], MAX_DB_NAME);
5318         ret = write(fh, &dbhdr, sizeof(dbhdr));
5319         if (ret == -1) {
5320                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5321                 goto done;
5322         }
5323         ret = write(fh, bd->records, bd->len);
5324         if (ret == -1) {
5325                 DEBUG(DEBUG_ERR,("write failed: %s\n", strerror(errno)));
5326                 goto done;
5327         }
5328
5329         status = 0;
5330 done:
5331         if (fh != -1) {
5332                 ret = close(fh);
5333                 if (ret == -1) {
5334                         DEBUG(DEBUG_ERR,("close failed: %s\n", strerror(errno)));
5335                 }
5336         }
5337
5338         DEBUG(DEBUG_ERR,("Database backed up to %s\n", argv[1]));
5339
5340         talloc_free(tmp_ctx);
5341         return status;
5342 }
5343
5344 /*
5345  * restore a database from a file 
5346  */
5347 static int control_restoredb(struct ctdb_context *ctdb, int argc, const char **argv)
5348 {
5349         int ret;
5350         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5351         TDB_DATA outdata;
5352         TDB_DATA data;
5353         struct db_file_header dbhdr;
5354         struct ctdb_db_context *ctdb_db;
5355         struct ctdb_node_map *nodemap=NULL;
5356         struct ctdb_vnn_map *vnnmap=NULL;
5357         int i, fh;
5358         struct ctdb_control_wipe_database w;
5359         uint32_t *nodes;
5360         uint32_t generation;
5361         struct tm *tm;
5362         char tbuf[100];
5363         char *dbname;
5364
5365         assert_single_node_only();
5366
5367         if (argc < 1 || argc > 2) {
5368                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5369                 return -1;
5370         }
5371
5372         fh = open(argv[0], O_RDONLY);
5373         if (fh == -1) {
5374                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5375                 talloc_free(tmp_ctx);
5376                 return -1;
5377         }
5378
5379         read(fh, &dbhdr, sizeof(dbhdr));
5380         if (dbhdr.version != DB_VERSION) {
5381                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5382                 talloc_free(tmp_ctx);
5383                 return -1;
5384         }
5385
5386         dbname = discard_const(dbhdr.name);
5387         if (argc == 2) {
5388                 dbname = discard_const(argv[1]);
5389         }
5390
5391         outdata.dsize = dbhdr.size;
5392         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5393         if (outdata.dptr == NULL) {
5394                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5395                 close(fh);
5396                 talloc_free(tmp_ctx);
5397                 return -1;
5398         }               
5399         read(fh, outdata.dptr, outdata.dsize);
5400         close(fh);
5401
5402         tm = localtime(&dbhdr.timestamp);
5403         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5404         printf("Restoring database '%s' from backup @ %s\n",
5405                 dbname, tbuf);
5406
5407
5408         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), dbname, dbhdr.persistent, 0);
5409         if (ctdb_db == NULL) {
5410                 DEBUG(DEBUG_ERR,("Unable to attach to database '%s'\n", dbname));
5411                 talloc_free(tmp_ctx);
5412                 return -1;
5413         }
5414
5415         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb, &nodemap);
5416         if (ret != 0) {
5417                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n", options.pnn));
5418                 talloc_free(tmp_ctx);
5419                 return ret;
5420         }
5421
5422
5423         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx, &vnnmap);
5424         if (ret != 0) {
5425                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n", options.pnn));
5426                 talloc_free(tmp_ctx);
5427                 return ret;
5428         }
5429
5430         /* freeze all nodes */
5431         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5432         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5433                 if (ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5434                                         nodes, i,
5435                                         TIMELIMIT(),
5436                                         false, tdb_null,
5437                                         NULL, NULL,
5438                                         NULL) != 0) {
5439                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5440                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5441                         talloc_free(tmp_ctx);
5442                         return -1;
5443                 }
5444         }
5445
5446         generation = vnnmap->generation;
5447         data.dptr = (void *)&generation;
5448         data.dsize = sizeof(generation);
5449
5450         /* start a cluster wide transaction */
5451         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5452         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5453                                         nodes, 0,
5454                                         TIMELIMIT(), false, data,
5455                                         NULL, NULL,
5456                                         NULL) != 0) {
5457                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide transactions.\n"));
5458                 return -1;
5459         }
5460
5461
5462         w.db_id = ctdb_db->db_id;
5463         w.transaction_id = generation;
5464
5465         data.dptr = (void *)&w;
5466         data.dsize = sizeof(w);
5467
5468         /* wipe all the remote databases. */
5469         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5470         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5471                                         nodes, 0,
5472                                         TIMELIMIT(), false, data,
5473                                         NULL, NULL,
5474                                         NULL) != 0) {
5475                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5476                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5477                 talloc_free(tmp_ctx);
5478                 return -1;
5479         }
5480         
5481         /* push the database */
5482         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5483         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_PUSH_DB,
5484                                         nodes, 0,
5485                                         TIMELIMIT(), false, outdata,
5486                                         NULL, NULL,
5487                                         NULL) != 0) {
5488                 DEBUG(DEBUG_ERR, ("Failed to push database.\n"));
5489                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5490                 talloc_free(tmp_ctx);
5491                 return -1;
5492         }
5493
5494         data.dptr = (void *)&ctdb_db->db_id;
5495         data.dsize = sizeof(ctdb_db->db_id);
5496
5497         /* mark the database as healthy */
5498         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5499         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5500                                         nodes, 0,
5501                                         TIMELIMIT(), false, data,
5502                                         NULL, NULL,
5503                                         NULL) != 0) {
5504                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5505                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5506                 talloc_free(tmp_ctx);
5507                 return -1;
5508         }
5509
5510         data.dptr = (void *)&generation;
5511         data.dsize = sizeof(generation);
5512
5513         /* commit all the changes */
5514         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5515                                         nodes, 0,
5516                                         TIMELIMIT(), false, data,
5517                                         NULL, NULL,
5518                                         NULL) != 0) {
5519                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5520                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5521                 talloc_free(tmp_ctx);
5522                 return -1;
5523         }
5524
5525
5526         /* thaw all nodes */
5527         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5528         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5529                                         nodes, 0,
5530                                         TIMELIMIT(),
5531                                         false, tdb_null,
5532                                         NULL, NULL,
5533                                         NULL) != 0) {
5534                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5535                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5536                 talloc_free(tmp_ctx);
5537                 return -1;
5538         }
5539
5540
5541         talloc_free(tmp_ctx);
5542         return 0;
5543 }
5544
5545 /*
5546  * dump a database backup from a file
5547  */
5548 static int control_dumpdbbackup(struct ctdb_context *ctdb, int argc, const char **argv)
5549 {
5550         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5551         TDB_DATA outdata;
5552         struct db_file_header dbhdr;
5553         int i, fh;
5554         struct tm *tm;
5555         char tbuf[100];
5556         struct ctdb_rec_data *rec = NULL;
5557         struct ctdb_marshall_buffer *m;
5558         struct ctdb_dump_db_context c;
5559
5560         assert_single_node_only();
5561
5562         if (argc != 1) {
5563                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5564                 return -1;
5565         }
5566
5567         fh = open(argv[0], O_RDONLY);
5568         if (fh == -1) {
5569                 DEBUG(DEBUG_ERR,("Failed to open file '%s'\n", argv[0]));
5570                 talloc_free(tmp_ctx);
5571                 return -1;
5572         }
5573
5574         read(fh, &dbhdr, sizeof(dbhdr));
5575         if (dbhdr.version != DB_VERSION) {
5576                 DEBUG(DEBUG_ERR,("Invalid version of database dump. File is version %lu but expected version was %u\n", dbhdr.version, DB_VERSION));
5577                 talloc_free(tmp_ctx);
5578                 return -1;
5579         }
5580
5581         outdata.dsize = dbhdr.size;
5582         outdata.dptr = talloc_size(tmp_ctx, outdata.dsize);
5583         if (outdata.dptr == NULL) {
5584                 DEBUG(DEBUG_ERR,("Failed to allocate data of size '%lu'\n", dbhdr.size));
5585                 close(fh);
5586                 talloc_free(tmp_ctx);
5587                 return -1;
5588         }
5589         read(fh, outdata.dptr, outdata.dsize);
5590         close(fh);
5591         m = (struct ctdb_marshall_buffer *)outdata.dptr;
5592
5593         tm = localtime(&dbhdr.timestamp);
5594         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
5595         printf("Backup of database name:'%s' dbid:0x%x08x from @ %s\n",
5596                 dbhdr.name, m->db_id, tbuf);
5597
5598         ZERO_STRUCT(c);
5599         c.f = stdout;
5600         c.printemptyrecords = (bool)options.printemptyrecords;
5601         c.printdatasize = (bool)options.printdatasize;
5602         c.printlmaster = false;
5603         c.printhash = (bool)options.printhash;
5604         c.printrecordflags = (bool)options.printrecordflags;
5605
5606         for (i=0; i < m->count; i++) {
5607                 uint32_t reqid = 0;
5608                 TDB_DATA key, data;
5609
5610                 /* we do not want the header splitted, so we pass NULL*/
5611                 rec = ctdb_marshall_loop_next(m, rec, &reqid,
5612                                               NULL, &key, &data);
5613
5614                 ctdb_dumpdb_record(ctdb, key, data, &c);
5615         }
5616
5617         printf("Dumped %d records\n", i);
5618         talloc_free(tmp_ctx);
5619         return 0;
5620 }
5621
5622 /*
5623  * wipe a database from a file
5624  */
5625 static int control_wipedb(struct ctdb_context *ctdb, int argc,
5626                           const char **argv)
5627 {
5628         int ret;
5629         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5630         TDB_DATA data;
5631         struct ctdb_db_context *ctdb_db;
5632         struct ctdb_node_map *nodemap = NULL;
5633         struct ctdb_vnn_map *vnnmap = NULL;
5634         int i;
5635         struct ctdb_control_wipe_database w;
5636         uint32_t *nodes;
5637         uint32_t generation;
5638         uint8_t flags;
5639
5640         assert_single_node_only();
5641
5642         if (argc != 1) {
5643                 DEBUG(DEBUG_ERR,("Invalid arguments\n"));
5644                 return -1;
5645         }
5646
5647         if (!db_exists(ctdb, argv[0], NULL, &flags)) {
5648                 return -1;
5649         }
5650
5651         ctdb_db = ctdb_attach(ctdb, TIMELIMIT(), argv[0], flags & CTDB_DB_FLAGS_PERSISTENT, 0);
5652         if (ctdb_db == NULL) {
5653                 DEBUG(DEBUG_ERR, ("Unable to attach to database '%s'\n",
5654                                   argv[0]));
5655                 talloc_free(tmp_ctx);
5656                 return -1;
5657         }
5658
5659         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), options.pnn, ctdb,
5660                                    &nodemap);
5661         if (ret != 0) {
5662                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from node %u\n",
5663                                   options.pnn));
5664                 talloc_free(tmp_ctx);
5665                 return ret;
5666         }
5667
5668         ret = ctdb_ctrl_getvnnmap(ctdb, TIMELIMIT(), options.pnn, tmp_ctx,
5669                                   &vnnmap);
5670         if (ret != 0) {
5671                 DEBUG(DEBUG_ERR, ("Unable to get vnnmap from node %u\n",
5672                                   options.pnn));
5673                 talloc_free(tmp_ctx);
5674                 return ret;
5675         }
5676
5677         /* freeze all nodes */
5678         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5679         for (i=1; i<=NUM_DB_PRIORITIES; i++) {
5680                 ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_FREEZE,
5681                                                 nodes, i,
5682                                                 TIMELIMIT(),
5683                                                 false, tdb_null,
5684                                                 NULL, NULL,
5685                                                 NULL);
5686                 if (ret != 0) {
5687                         DEBUG(DEBUG_ERR, ("Unable to freeze nodes.\n"));
5688                         ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn,
5689                                              CTDB_RECOVERY_ACTIVE);
5690                         talloc_free(tmp_ctx);
5691                         return -1;
5692                 }
5693         }
5694
5695         generation = vnnmap->generation;
5696         data.dptr = (void *)&generation;
5697         data.dsize = sizeof(generation);
5698
5699         /* start a cluster wide transaction */
5700         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5701         ret = ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_START,
5702                                         nodes, 0,
5703                                         TIMELIMIT(), false, data,
5704                                         NULL, NULL,
5705                                         NULL);
5706         if (ret!= 0) {
5707                 DEBUG(DEBUG_ERR, ("Unable to start cluster wide "
5708                                   "transactions.\n"));
5709                 return -1;
5710         }
5711
5712         w.db_id = ctdb_db->db_id;
5713         w.transaction_id = generation;
5714
5715         data.dptr = (void *)&w;
5716         data.dsize = sizeof(w);
5717
5718         /* wipe all the remote databases. */
5719         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5720         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_WIPE_DATABASE,
5721                                         nodes, 0,
5722                                         TIMELIMIT(), false, data,
5723                                         NULL, NULL,
5724                                         NULL) != 0) {
5725                 DEBUG(DEBUG_ERR, ("Unable to wipe database.\n"));
5726                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5727                 talloc_free(tmp_ctx);
5728                 return -1;
5729         }
5730
5731         data.dptr = (void *)&ctdb_db->db_id;
5732         data.dsize = sizeof(ctdb_db->db_id);
5733
5734         /* mark the database as healthy */
5735         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5736         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_DB_SET_HEALTHY,
5737                                         nodes, 0,
5738                                         TIMELIMIT(), false, data,
5739                                         NULL, NULL,
5740                                         NULL) != 0) {
5741                 DEBUG(DEBUG_ERR, ("Failed to mark database as healthy.\n"));
5742                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5743                 talloc_free(tmp_ctx);
5744                 return -1;
5745         }
5746
5747         data.dptr = (void *)&generation;
5748         data.dsize = sizeof(generation);
5749
5750         /* commit all the changes */
5751         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_TRANSACTION_COMMIT,
5752                                         nodes, 0,
5753                                         TIMELIMIT(), false, data,
5754                                         NULL, NULL,
5755                                         NULL) != 0) {
5756                 DEBUG(DEBUG_ERR, ("Unable to commit databases.\n"));
5757                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5758                 talloc_free(tmp_ctx);
5759                 return -1;
5760         }
5761
5762         /* thaw all nodes */
5763         nodes = list_of_active_nodes(ctdb, nodemap, tmp_ctx, true);
5764         if (ctdb_client_async_control(ctdb, CTDB_CONTROL_THAW,
5765                                         nodes, 0,
5766                                         TIMELIMIT(),
5767                                         false, tdb_null,
5768                                         NULL, NULL,
5769                                         NULL) != 0) {
5770                 DEBUG(DEBUG_ERR, ("Unable to thaw nodes.\n"));
5771                 ctdb_ctrl_setrecmode(ctdb, TIMELIMIT(), options.pnn, CTDB_RECOVERY_ACTIVE);
5772                 talloc_free(tmp_ctx);
5773                 return -1;
5774         }
5775
5776         DEBUG(DEBUG_ERR, ("Database wiped.\n"));
5777
5778         talloc_free(tmp_ctx);
5779         return 0;
5780 }
5781
5782 /*
5783   dump memory usage
5784  */
5785 static int control_dumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5786 {
5787         TDB_DATA data;
5788         int ret;
5789         int32_t res;
5790         char *errmsg;
5791         TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
5792         ret = ctdb_control(ctdb, options.pnn, 0, CTDB_CONTROL_DUMP_MEMORY,
5793                            0, tdb_null, tmp_ctx, &data, &res, NULL, &errmsg);
5794         if (ret != 0 || res != 0) {
5795                 DEBUG(DEBUG_ERR,("Failed to dump memory - %s\n", errmsg));
5796                 talloc_free(tmp_ctx);
5797                 return -1;
5798         }
5799         write(1, data.dptr, data.dsize);
5800         talloc_free(tmp_ctx);
5801         return 0;
5802 }
5803
5804 /*
5805   handler for memory dumps
5806 */
5807 static void mem_dump_handler(struct ctdb_context *ctdb, uint64_t srvid, 
5808                              TDB_DATA data, void *private_data)
5809 {
5810         write(1, data.dptr, data.dsize);
5811         exit(0);
5812 }
5813
5814 /*
5815   dump memory usage on the recovery daemon
5816  */
5817 static int control_rddumpmemory(struct ctdb_context *ctdb, int argc, const char **argv)
5818 {
5819         int ret;
5820         TDB_DATA data;
5821         struct srvid_request rd;
5822
5823         rd.pnn = ctdb_get_pnn(ctdb);
5824         rd.srvid = getpid();
5825
5826         /* register a message port for receiveing the reply so that we
5827            can receive the reply
5828         */
5829         ctdb_client_set_message_handler(ctdb, rd.srvid, mem_dump_handler, NULL);
5830
5831
5832         data.dptr = (uint8_t *)&rd;
5833         data.dsize = sizeof(rd);
5834
5835         ret = ctdb_client_send_message(ctdb, options.pnn, CTDB_SRVID_MEM_DUMP, data);
5836         if (ret != 0) {
5837                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5838                 return -1;
5839         }
5840
5841         /* this loop will terminate when we have received the reply */
5842         while (1) {     
5843                 event_loop_once(ctdb->ev);
5844         }
5845
5846         return 0;
5847 }
5848
5849 /*
5850   send a message to a srvid
5851  */
5852 static int control_msgsend(struct ctdb_context *ctdb, int argc, const char **argv)
5853 {
5854         unsigned long srvid;
5855         int ret;
5856         TDB_DATA data;
5857
5858         if (argc < 2) {
5859                 usage();
5860         }
5861
5862         srvid      = strtoul(argv[0], NULL, 0);
5863
5864         data.dptr = (uint8_t *)discard_const(argv[1]);
5865         data.dsize= strlen(argv[1]);
5866
5867         ret = ctdb_client_send_message(ctdb, CTDB_BROADCAST_CONNECTED, srvid, data);
5868         if (ret != 0) {
5869                 DEBUG(DEBUG_ERR,("Failed to send memdump request message to %u\n", options.pnn));
5870                 return -1;
5871         }
5872
5873         return 0;
5874 }
5875
5876 /*
5877   handler for msglisten
5878 */
5879 static void msglisten_handler(struct ctdb_context *ctdb, uint64_t srvid, 
5880                              TDB_DATA data, void *private_data)
5881 {
5882         int i;
5883
5884         printf("Message received: ");
5885         for (i=0;i<data.dsize;i++) {
5886                 printf("%c", data.dptr[i]);
5887         }
5888         printf("\n");
5889 }
5890
5891 /*
5892   listen for messages on a messageport
5893  */
5894 static int control_msglisten(struct ctdb_context *ctdb, int argc, const char **argv)
5895 {
5896         uint64_t srvid;
5897
5898         srvid = getpid();
5899
5900         /* register a message port and listen for messages
5901         */
5902         ctdb_client_set_message_handler(ctdb, srvid, msglisten_handler, NULL);
5903         printf("Listening for messages on srvid:%d\n", (int)srvid);
5904
5905         while (1) {     
5906                 event_loop_once(ctdb->ev);
5907         }
5908
5909         return 0;
5910 }
5911
5912 /*
5913   list all nodes in the cluster
5914   we parse the nodes file directly
5915  */
5916 static int control_listnodes(struct ctdb_context *ctdb, int argc, const char **argv)
5917 {
5918         TALLOC_CTX *mem_ctx = talloc_new(NULL);
5919         struct pnn_node *pnn_nodes;
5920         struct pnn_node *pnn_node;
5921
5922         assert_single_node_only();
5923
5924         pnn_nodes = read_nodes_file(mem_ctx);
5925         if (pnn_nodes == NULL) {
5926                 DEBUG(DEBUG_ERR,("Failed to read nodes file\n"));
5927                 talloc_free(mem_ctx);
5928                 return -1;
5929         }
5930
5931         for(pnn_node=pnn_nodes;pnn_node;pnn_node=pnn_node->next) {
5932                 ctdb_sock_addr addr;
5933                 if (parse_ip(pnn_node->addr, NULL, 63999, &addr) == 0) {
5934                         DEBUG(DEBUG_ERR,("Wrongly formed ip address '%s' in nodes file\n", pnn_node->addr));
5935                         talloc_free(mem_ctx);
5936                         return -1;
5937                 }
5938                 if (options.machinereadable){
5939                         printf(":%d:%s:\n", pnn_node->pnn, pnn_node->addr);
5940                 } else {
5941                         printf("%s\n", pnn_node->addr);
5942                 }
5943         }
5944         talloc_free(mem_ctx);
5945
5946         return 0;
5947 }
5948
5949 /*
5950   reload the nodes file on the local node
5951  */
5952 static int control_reload_nodes_file(struct ctdb_context *ctdb, int argc, const char **argv)
5953 {
5954         int i, ret;
5955         int mypnn;
5956         struct ctdb_node_map *nodemap=NULL;
5957
5958         assert_single_node_only();
5959
5960         mypnn = ctdb_get_pnn(ctdb);
5961
5962         ret = ctdb_ctrl_getnodemap(ctdb, TIMELIMIT(), CTDB_CURRENT_NODE, ctdb, &nodemap);
5963         if (ret != 0) {
5964                 DEBUG(DEBUG_ERR, ("Unable to get nodemap from local node\n"));
5965                 return ret;
5966         }
5967
5968         /* reload the nodes file on all remote nodes */
5969         for (i=0;i<nodemap->num;i++) {
5970                 if (nodemap->nodes[i].pnn == mypnn) {
5971                         continue;
5972                 }
5973                 DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", nodemap->nodes[i].pnn));
5974                 ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(),
5975                         nodemap->nodes[i].pnn);
5976                 if (ret != 0) {
5977                         DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", nodemap->nodes[i].pnn));
5978                 }
5979         }
5980
5981         /* reload the nodes file on the local node */
5982         DEBUG(DEBUG_NOTICE, ("Reloading nodes file on node %u\n", mypnn));
5983         ret = ctdb_ctrl_reload_nodes_file(ctdb, TIMELIMIT(), mypnn);
5984         if (ret != 0) {
5985                 DEBUG(DEBUG_ERR, ("ERROR: Failed to reload nodes file on node %u. You MUST fix that node manually!\n", mypnn));
5986         }
5987
5988         /* initiate a recovery */
5989         control_recover(ctdb, argc, argv);
5990
5991         return 0;
5992 }
5993
5994
5995 static const struct {
5996         const char *name;
5997         int (*fn)(struct ctdb_context *, int, const char **);
5998         bool auto_all;
5999         bool without_daemon; /* can be run without daemon running ? */
6000         const char *msg;
6001         const char *args;
6002 } ctdb_commands[] = {
6003         { "version",         control_version,           true,   true,   "show version of ctdb" },
6004         { "status",          control_status,            true,   false,  "show node status" },
6005         { "uptime",          control_uptime,            true,   false,  "show node uptime" },
6006         { "ping",            control_ping,              true,   false,  "ping all nodes" },
6007         { "runstate",        control_runstate,          true,   false,  "get/check runstate of a node", "[setup|first_recovery|startup|running]" },
6008         { "getvar",          control_getvar,            true,   false,  "get a tunable variable",               "<name>"},
6009         { "setvar",          control_setvar,            true,   false,  "set a tunable variable",               "<name> <value>"},
6010         { "listvars",        control_listvars,          true,   false,  "list tunable variables"},
6011         { "statistics",      control_statistics,        false,  false, "show statistics" },
6012         { "statisticsreset", control_statistics_reset,  true,   false,  "reset statistics"},
6013         { "stats",           control_stats,             false,  false,  "show rolling statistics", "[number of history records]" },
6014         { "ip",              control_ip,                false,  false,  "show which public ip's that ctdb manages" },
6015         { "ipinfo",          control_ipinfo,            true,   false,  "show details about a public ip that ctdb manages", "<ip>" },
6016         { "ifaces",          control_ifaces,            true,   false,  "show which interfaces that ctdb manages" },
6017         { "setifacelink",    control_setifacelink,      true,   false,  "set interface link status", "<iface> <status>" },
6018         { "process-exists",  control_process_exists,    true,   false,  "check if a process exists on a node",  "<pid>"},
6019         { "getdbmap",        control_getdbmap,          true,   false,  "show the database map" },
6020         { "getdbstatus",     control_getdbstatus,       true,   false,  "show the status of a database", "<dbname|dbid>" },
6021         { "catdb",           control_catdb,             true,   false,  "dump a ctdb database" ,                     "<dbname|dbid>"},
6022         { "cattdb",          control_cattdb,            true,   false,  "dump a local tdb database" ,                     "<dbname|dbid>"},
6023         { "getmonmode",      control_getmonmode,        true,   false,  "show monitoring mode" },
6024         { "getcapabilities", control_getcapabilities,   true,   false,  "show node capabilities" },
6025         { "pnn",             control_pnn,               true,   false,  "show the pnn of the currnet node" },
6026         { "lvs",             control_lvs,               true,   false,  "show lvs configuration" },
6027         { "lvsmaster",       control_lvsmaster,         true,   false,  "show which node is the lvs master" },
6028         { "disablemonitor",      control_disable_monmode,true,  false,  "set monitoring mode to DISABLE" },
6029         { "enablemonitor",      control_enable_monmode, true,   false,  "set monitoring mode to ACTIVE" },
6030         { "setdebug",        control_setdebug,          true,   false,  "set debug level",                      "<EMERG|ALERT|CRIT|ERR|WARNING|NOTICE|INFO|DEBUG>" },
6031         { "getdebug",        control_getdebug,          true,   false,  "get debug level" },
6032         { "getlog",          control_getlog,            true,   false,  "get the log data from the in memory ringbuffer", "[<level>] [recoverd]" },
6033         { "clearlog",          control_clearlog,        true,   false,  "clear the log data from the in memory ringbuffer", "[recoverd]" },
6034         { "attach",          control_attach,            true,   false,  "attach to a database",                 "<dbname> [persistent]" },
6035         { "dumpmemory",      control_dumpmemory,        true,   false,  "dump memory map to stdout" },
6036         { "rddumpmemory",    control_rddumpmemory,      true,   false,  "dump memory map from the recovery daemon to stdout" },
6037         { "getpid",          control_getpid,            true,   false,  "get ctdbd process ID" },
6038         { "disable",         control_disable,           true,   false,  "disable a nodes public IP" },
6039         { "enable",          control_enable,            true,   false,  "enable a nodes public IP" },
6040         { "stop",            control_stop,              true,   false,  "stop a node" },
6041         { "continue",        control_continue,          true,   false,  "re-start a stopped node" },
6042         { "ban",             control_ban,               true,   false,  "ban a node from the cluster",          "<bantime|0>"},
6043         { "unban",           control_unban,             true,   false,  "unban a node" },
6044         { "showban",         control_showban,           true,   false,  "show ban information"},
6045         { "shutdown",        control_shutdown,          true,   false,  "shutdown ctdbd" },
6046         { "recover",         control_recover,           true,   false,  "force recovery" },
6047         { "sync",            control_ipreallocate,      false,  false,  "wait until ctdbd has synced all state changes" },
6048         { "ipreallocate",    control_ipreallocate,      false,  false,  "force the recovery daemon to perform a ip reallocation procedure" },
6049         { "thaw",            control_thaw,              true,   false,  "thaw databases", "[priority:1-3]" },
6050         { "isnotrecmaster",  control_isnotrecmaster,    false,  false,  "check if the local node is recmaster or not" },
6051         { "killtcp",         kill_tcp,                  false,  false, "kill a tcp connection.", "[<srcip:port> <dstip:port>]" },
6052         { "gratiousarp",     control_gratious_arp,      false,  false, "send a gratious arp", "<ip> <interface>" },
6053         { "tickle",          tickle_tcp,                false,  false, "send a tcp tickle ack", "<srcip:port> <dstip:port>" },
6054         { "gettickles",      control_get_tickles,       false,  false, "get the list of tickles registered for this ip", "<ip> [<port>]" },
6055         { "addtickle",       control_add_tickle,        false,  false, "add a tickle for this ip", "<ip>:<port> <ip>:<port>" },
6056
6057         { "deltickle",       control_del_tickle,        false,  false, "delete a tickle from this ip", "<ip>:<port> <ip>:<port>" },
6058
6059         { "regsrvid",        regsrvid,                  false,  false, "register a server id", "<pnn> <type> <id>" },
6060         { "unregsrvid",      unregsrvid,                false,  false, "unregister a server id", "<pnn> <type> <id>" },
6061         { "chksrvid",        chksrvid,                  false,  false, "check if a server id exists", "<pnn> <type> <id>" },
6062         { "getsrvids",       getsrvids,                 false,  false, "get a list of all server ids"},
6063         { "check_srvids",    check_srvids,              false,  false, "check if a srvid exists", "<id>+" },
6064         { "repack",          ctdb_repack,               false,  false, "repack all databases", "[max_freelist]"},
6065         { "listnodes",       control_listnodes,         false,  true, "list all nodes in the cluster"},
6066         { "reloadnodes",     control_reload_nodes_file, false,  false, "reload the nodes file and restart the transport on all nodes"},
6067         { "moveip",          control_moveip,            false,  false, "move/failover an ip address to another node", "<ip> <node>"},
6068         { "rebalanceip",     control_rebalanceip,       false,  false, "release an ip from the node and let recd rebalance it", "<ip>"},
6069         { "addip",           control_addip,             true,   false, "add a ip address to a node", "<ip/mask> <iface>"},
6070         { "delip",           control_delip,             false,  false, "delete an ip address from a node", "<ip>"},
6071         { "eventscript",     control_eventscript,       true,   false, "run the eventscript with the given parameters on a node", "<arguments>"},
6072         { "backupdb",        control_backupdb,          false,  false, "backup the database into a file.", "<dbname|dbid> <file>"},
6073         { "restoredb",        control_restoredb,        false,  false, "restore the database from a file.", "<file> [dbname]"},
6074         { "dumpdbbackup",    control_dumpdbbackup,      false,  true,  "dump database backup from a file.", "<file>"},
6075         { "wipedb",           control_wipedb,        false,     false, "wipe the contents of a database.", "<dbname|dbid>"},
6076         { "recmaster",        control_recmaster,        true,   false, "show the pnn for the recovery master."},
6077         { "scriptstatus",     control_scriptstatus,     true,   false, "show the status of the monitoring scripts (or all scripts)", "[all]"},
6078         { "enablescript",     control_enablescript,  true,      false, "enable an eventscript", "<script>"},
6079         { "disablescript",    control_disablescript,  true,     false, "disable an eventscript", "<script>"},
6080         { "natgwlist",        control_natgwlist,        true,   false, "show the nodes belonging to this natgw configuration"},
6081         { "xpnn",             control_xpnn,             false,  true,  "find the pnn of the local node without talking to the daemon (unreliable)" },
6082         { "getreclock",       control_getreclock,       true,   false, "Show the reclock file of a node"},
6083         { "setreclock",       control_setreclock,       true,   false, "Set/clear the reclock file of a node", "[filename]"},
6084         { "setnatgwstate",    control_setnatgwstate,    false,  false, "Set NATGW state to on/off", "{on|off}"},
6085         { "setlmasterrole",   control_setlmasterrole,   false,  false, "Set LMASTER role to on/off", "{on|off}"},
6086         { "setrecmasterrole", control_setrecmasterrole, false,  false, "Set RECMASTER role to on/off", "{on|off}"},
6087         { "setdbprio",        control_setdbprio,        false,  false, "Set DB priority", "<dbname|dbid> <prio:1-3>"},
6088         { "getdbprio",        control_getdbprio,        false,  false, "Get DB priority", "<dbname|dbid>"},
6089         { "setdbreadonly",    control_setdbreadonly,    false,  false, "Set DB readonly capable", "<dbname|dbid>"},
6090         { "setdbsticky",      control_setdbsticky,      false,  false, "Set DB sticky-records capable", "<dbname|dbid>"},
6091         { "msglisten",        control_msglisten,        false,  false, "Listen on a srvid port for messages", "<msg srvid>"},
6092         { "msgsend",          control_msgsend,  false,  false, "Send a message to srvid", "<srvid> <message>"},
6093         { "pfetch",          control_pfetch,            false,  false,  "fetch a record from a persistent database", "<dbname|dbid> <key> [<file>]" },
6094         { "pstore",          control_pstore,            false,  false,  "write a record to a persistent database", "<dbname|dbid> <key> <file containing record>" },
6095         { "pdelete",         control_pdelete,           false,  false,  "delete a record from a persistent database", "<dbname|dbid> <key>" },
6096         { "tfetch",          control_tfetch,            false,  true,  "fetch a record from a [c]tdb-file [-v]", "<tdb-file> <key> [<file>]" },
6097         { "tstore",          control_tstore,            false,  true,  "store a record (including ltdb header)", "<tdb-file> <key> <data+header>" },
6098         { "readkey",         control_readkey,           true,   false,  "read the content off a database key", "<tdb-file> <key>" },
6099         { "writekey",        control_writekey,          true,   false,  "write to a database key", "<tdb-file> <key> <value>" },
6100         { "checktcpport",    control_chktcpport,        false,  true,  "check if a service is bound to a specific tcp port or not", "<port>" },
6101         { "rebalancenode",     control_rebalancenode,   false,  false, "mark nodes as forced IP rebalancing targets", "[<pnn-list>]"},
6102         { "getdbseqnum",     control_getdbseqnum,       false,  false, "get the sequence number off a database", "<dbname|dbid>" },
6103         { "nodestatus",      control_nodestatus,        true,   false,  "show and return node status", "[<pnn-list>]" },
6104         { "dbstatistics",    control_dbstatistics,      false,  false, "show db statistics", "<dbname|dbid>" },
6105         { "reloadips",       control_reloadips,         false,  false, "reload the public addresses file on specified nodes" , "[<pnn-list>]" },
6106         { "ipiface",         control_ipiface,           false,  true,  "Find which interface an ip address is hosted on", "<ip>" },
6107 };
6108
6109 /*
6110   show usage message
6111  */
6112 static void usage(void)
6113 {
6114         int i;
6115         printf(
6116 "Usage: ctdb [options] <control>\n" \
6117 "Options:\n" \
6118 "   -n <node>          choose node number, or 'all' (defaults to local node)\n"
6119 "   -Y                 generate machinereadable output\n"
6120 "   -v                 generate verbose output\n"
6121 "   -t <timelimit>     set timelimit for control in seconds (default %u)\n", options.timelimit);
6122         printf("Controls:\n");
6123         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6124                 printf("  %-15s %-27s  %s\n", 
6125                        ctdb_commands[i].name, 
6126                        ctdb_commands[i].args?ctdb_commands[i].args:"",
6127                        ctdb_commands[i].msg);
6128         }
6129         exit(1);
6130 }
6131
6132
6133 static void ctdb_alarm(int sig)
6134 {
6135         printf("Maximum runtime exceeded - exiting\n");
6136         _exit(ERR_TIMEOUT);
6137 }
6138
6139 /*
6140   main program
6141 */
6142 int main(int argc, const char *argv[])
6143 {
6144         struct ctdb_context *ctdb;
6145         char *nodestring = NULL;
6146         struct poptOption popt_options[] = {
6147                 POPT_AUTOHELP
6148                 POPT_CTDB_CMDLINE
6149                 { "timelimit", 't', POPT_ARG_INT, &options.timelimit, 0, "timelimit", "integer" },
6150                 { "node",      'n', POPT_ARG_STRING, &nodestring, 0, "node", "integer|all" },
6151                 { "machinereadable", 'Y', POPT_ARG_NONE, &options.machinereadable, 0, "enable machinereadable output", NULL },
6152                 { "verbose",    'v', POPT_ARG_NONE, &options.verbose, 0, "enable verbose output", NULL },
6153                 { "maxruntime", 'T', POPT_ARG_INT, &options.maxruntime, 0, "die if runtime exceeds this limit (in seconds)", "integer" },
6154                 { "print-emptyrecords", 0, POPT_ARG_NONE, &options.printemptyrecords, 0, "print the empty records when dumping databases (catdb, cattdb, dumpdbbackup)", NULL },
6155                 { "print-datasize", 0, POPT_ARG_NONE, &options.printdatasize, 0, "do not print record data when dumping databases, only the data size", NULL },
6156                 { "print-lmaster", 0, POPT_ARG_NONE, &options.printlmaster, 0, "print the record's lmaster in catdb", NULL },
6157                 { "print-hash", 0, POPT_ARG_NONE, &options.printhash, 0, "print the record's hash when dumping databases", NULL },
6158                 { "print-recordflags", 0, POPT_ARG_NONE, &options.printrecordflags, 0, "print the record flags in catdb and dumpdbbackup", NULL },
6159                 POPT_TABLEEND
6160         };
6161         int opt;
6162         const char **extra_argv;
6163         int extra_argc = 0;
6164         int ret=-1, i;
6165         poptContext pc;
6166         struct event_context *ev;
6167         const char *control;
6168
6169         setlinebuf(stdout);
6170         
6171         /* set some defaults */
6172         options.maxruntime = 0;
6173         options.timelimit = 10;
6174         options.pnn = CTDB_CURRENT_NODE;
6175
6176         pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
6177
6178         while ((opt = poptGetNextOpt(pc)) != -1) {
6179                 switch (opt) {
6180                 default:
6181                         DEBUG(DEBUG_ERR, ("Invalid option %s: %s\n", 
6182                                 poptBadOption(pc, 0), poptStrerror(opt)));
6183                         exit(1);
6184                 }
6185         }
6186
6187         /* setup the remaining options for the main program to use */
6188         extra_argv = poptGetArgs(pc);
6189         if (extra_argv) {
6190                 extra_argv++;
6191                 while (extra_argv[extra_argc]) extra_argc++;
6192         }
6193
6194         if (extra_argc < 1) {
6195                 usage();
6196         }
6197
6198         if (options.maxruntime == 0) {
6199                 const char *ctdb_timeout;
6200                 ctdb_timeout = getenv("CTDB_TIMEOUT");
6201                 if (ctdb_timeout != NULL) {
6202                         options.maxruntime = strtoul(ctdb_timeout, NULL, 0);
6203                 } else {
6204                         /* default timeout is 120 seconds */
6205                         options.maxruntime = 120;
6206                 }
6207         }
6208
6209         signal(SIGALRM, ctdb_alarm);
6210         alarm(options.maxruntime);
6211
6212         control = extra_argv[0];
6213
6214         /* Default value for CTDB_BASE - don't override */
6215         setenv("CTDB_BASE", ETCDIR "/ctdb", 0);
6216
6217         ev = event_context_init(NULL);
6218         if (!ev) {
6219                 DEBUG(DEBUG_ERR, ("Failed to initialize event system\n"));
6220                 exit(1);
6221         }
6222
6223         for (i=0;i<ARRAY_SIZE(ctdb_commands);i++) {
6224                 if (strcmp(control, ctdb_commands[i].name) == 0) {
6225                         break;
6226                 }
6227         }
6228
6229         if (i == ARRAY_SIZE(ctdb_commands)) {
6230                 DEBUG(DEBUG_ERR, ("Unknown control '%s'\n", control));
6231                 exit(1);
6232         }
6233
6234         if (ctdb_commands[i].without_daemon == true) {
6235                 if (nodestring != NULL) {
6236                         DEBUG(DEBUG_ERR, ("Can't specify node(s) with \"ctdb %s\"\n", control));
6237                         exit(1);
6238                 }
6239                 close(2);
6240                 return ctdb_commands[i].fn(NULL, extra_argc-1, extra_argv+1);
6241         }
6242
6243         /* initialise ctdb */
6244         ctdb = ctdb_cmdline_client(ev, TIMELIMIT());
6245
6246         if (ctdb == NULL) {
6247                 DEBUG(DEBUG_ERR, ("Failed to init ctdb\n"));
6248                 exit(1);
6249         }
6250
6251         /* setup the node number(s) to contact */
6252         if (!parse_nodestring(ctdb, ctdb, nodestring, CTDB_CURRENT_NODE, false,
6253                               &options.nodes, &options.pnn)) {
6254                 usage();
6255         }
6256
6257         if (options.pnn == CTDB_CURRENT_NODE) {
6258                 options.pnn = options.nodes[0];
6259         }
6260
6261         if (ctdb_commands[i].auto_all && 
6262             ((options.pnn == CTDB_BROADCAST_ALL) ||
6263              (options.pnn == CTDB_MULTICAST))) {
6264                 int j;
6265
6266                 ret = 0;
6267                 for (j = 0; j < talloc_array_length(options.nodes); j++) {
6268                         options.pnn = options.nodes[j];
6269                         ret |= ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6270                 }
6271         } else {
6272                 ret = ctdb_commands[i].fn(ctdb, extra_argc-1, extra_argv+1);
6273         }
6274
6275         talloc_free(ctdb);
6276         talloc_free(ev);
6277         (void)poptFreeContext(pc);
6278
6279         return ret;
6280
6281 }