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