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