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