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