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