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